9/10 資訊能競培訓

自我介紹

我們在這幹嘛

校內複賽 10/8 選校隊(11 + 4)
北市賽 11月? 前二等獎進全國
(我猜10個?)
全國賽 12月(19)日 前二等獎進資奧一階選訓
(10個)

選訓??

入營考 3月嗎 選20人進一階
(資格!年級保障!)
資奧一階 3月嗎 模考*2
選12人進二階(推薦、亞太)
資奧二階 4月嗎 模考*2
選4人當國手
國際資奧 6月 新加坡go go
金銀銅牌

除此之外

ISSC 10/17
報名剩九天
一校最多報兩隊
找其它校?
聽說有國際賽
HP codewars 11月? 參加、吃、抽、奇怪項目
NPSC 11月初賽
12月決賽
初賽取25隊,一校最多3隊!
吃、氣球
不知為何推?
YTP 暑假? 吃爆
最容易賺錢
適合同分

三人團體賽!!!

除此之外

享受想題、解題的樂趣!
為人類謀幸福

變強的方法

寫題目
寫題目
寫題目

TIOJ
codeforces

請補充QQ

所以,到底競程是啥?

欸不是,大家都比過初賽了ㄅ

如何解題

1.思考性質(optional)
2.設計算法
3.驗證複雜度
4.開寫
*小測資很重要

(時間)複雜度

一個描述演算法執行時間的函式

舉例來說

班上有\(n\)個人,我們想要知道哪兩個人一起玩兩人三腳最快,於是把所有兩人一組的可能都試過,並假設一次只能讓一組進行兩人三腳,且必須花費10秒(常數時間)

時間複雜度\(T(n) = \frac{n(n-1)}{2}\times 10 = 5n^2 - 5n\) 

Big-O

\(\exist M \in \R^+,x_0 \in \R,s.t. \forall x \geq x_0,|f(x)| \leq M|g(x)| \)

\(f(x) = O(g(x)) \)

簡單來說

保留最高(增長最快)項,常數bye bye

\(T(n) = 5n^2 + 5n = O(n^2)\)

莫非定律

函式中有多個變數?

神秘數字

一秒\(10^8\)(左右)

Ex. \(O(n^2)\) 最多可以跑到\(10^4\)左右

Q&A

為甚麼要去掉低次項跟常數?

簡潔

常數去掉不會有事嗎?

...卡常數的題不該出現在世界上

空間複雜度

1.一樣,只是用來描述所使用的空間大小

3.空間複雜度小於等於時間複雜度

STL(我會的部分)

演算法

#include<iostream>
#include<algorithm>
using namespace std;
int A[5] = {2,4,1,3,5};
int main()
{
    sort(A,A+5);//O(nlogn)
    cout << *lower_bound(A,A+5,4) << endl;//O(logn)
    cout << *upper_bound(A,A+5,4) << endl;//O(logn)
}
//reverse

pair

#include<iostream>
#include<utility>
#include<algorithm>
using namespace std;
int main()
{
    pair<int,int> A[3];
    A[0] = {1,4};
    A[1] = {5,2};
    A[2] = {0,10};
    sort(A,A+3);
    for(int i = 0;i < 3;i++)
    {
        cout << A[i].first << " " << A[i].second << endl;
    }
}

容器

Stack

Queue

#include<iostream>
#include<stack>
using namespace std;
stack<int> stk;
int main()
{
    stk.push(5);
    stk.push(3);
    cout << stk.size() <<endl;
    cout << stk.top() << endl;
    stk.pop();
    //通通O(1)
}
#include<iostream>
#include<stack>
using namespace std;
queue<int> que;
int main()
{
    que.push(5);
    que.push(3);
    cout << que.empty() <<endl;
    cout << que.front() << endl;
    que.pop();
    //通通O(1)
}

priority_queue
取出最大值的queue

#include<iostream>
#include<queue>
using namespace std;
int main()
{
    priority_queue<int,vector<int>,greater<int> > pq;
    //                 照打         小於函式
    pq.push(5);//O(logn)
    pq.push(3);
    pq.push(4);
    cout << pq.top() <<endl;//O(1)
    pq.pop();//O(logn)
    cout << pq.top() <<endl;
    pq.pop();
    cout << pq.top() <<endl;
    pq.pop();
}

vector
可變長度陣列?

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> vec = {1,2,3};
int main()
{
    vec.clear();
    vec.push_back(0);
    vec.push_back(8);
    vec.push_back(5);
    vec.push_back(9);
    vec.pop_back();//以上都O(1)
    cout << vec[0] <<endl;
    sort(vec.begin(),vec.end());//想成指標
    for(auto x : vec)
    {
    	cout << x << " ";
    }
}

deque
double-ended queue

#include<iostream>
#include<deque>
using namespace std;
int main()
{
    deque<int> deq;
    deq.push_back(1);
    deq.push_back(4);
    deq.push_front(5);
    deq.push_front(3);
    cout << deq[2] << endl;
    deq.pop_back();//以上都O(1)
    for(auto x : deq)
    {
        cout << x << " ";
    }
}

set
有序集合?

#include<iostream>
#include<set>
using namespace std;
int main()
{
    set<int> st;
    st.insert(5);
    st.insert(3);
    st.insert(8);
    st.insert(5);
    st.erase(3);//以上都O(logn)
    for(auto x : st)
        cout << x << " ";
    cout << endl;
    cout << (*st.begin()) <<endl;//O(1)
    cout << (*prev(st.end())) << endl;//O(1)
    cout << st.count(5) <<endl;//O(logn)
    cout << *st.lower_bound(3) << endl;//O(logn)
}

multiset

#include<iostream>
#include<set>
using namespace std;
int main()
{
    multiset<int> st;
    st.insert(5);
    st.insert(3);
    st.insert(8);
    st.insert(5);
    st.erase(3);//以上都O(logn)
    for(auto x : st)
        cout << x << " ";
    st.erase(st.find(5));//O(logn)
}

unordered_set

#include<iostream>
#include<unordered_set>
using namespace std;
int main()
{
    unordered_set<int> st;
    s.reserve(32768); //聽說會變快
    s.max_load_factor(0.25); //聽說會變快
    st.insert(5);
    st.insert(3);
    st.insert(8);
    st.insert(5);
    st.erase(3);//以上都O(1)
    for(auto x : st)
        cout << x << " ";
}

unordered_multiset

map
有序映射

#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    map<int,int> mp;
    mp[4] = 3;
    mp[5] = 9;
    mp.insert({3,9});//以上都O(logn)
    for(auto x : mp)
    {
        cout << x.first << " " << x.second << endl;
    }
    cout << mp.count(8) <<endl;//O(logn)
    cout << mp[8] <<endl;//O(logn)
    cout << mp.count(8) <<endl;
}

multimap(沒用過)
unordered_map
unorderd_multimap

習題

ZJ b298

ZJ c123
ZJ b231
​TIOJ 1807

bitset

#include<bits/stdc++.h>
using namespace std;
int main()
{
    bitset<10>a(6);
    bitset<10>b("111");
    cout << a << endl;
    cout << b <<endl;
    b.reset();//O(N)
    b[2] = 1;
    cout << a.to_ulong() + b.to_ulong() <<endl;//O(N)
    cout << (a ^ b) << endl;//32 times faster?
}
Made with Slides.com