演算法是什麼?
[al-guh-rith-uh m]
不同型式的資訊競賽
(只要夠大牌,用法就是對的)
int mx = a[0];
for(int i=1;i<N;++i)
{
mx = max( mx , a[i] );
}
如果\(f(x)\in \mathcal{O}(g(x))\)
就存在\(c\)與\(x_0\)
如果\(x>x_0\)
\(c\times g(x)\geq f(x)\)
如果\(f(x)\in \mathcal{O}(g(x))\)
如果\(f(x)\in \mathcal{O}(g(x))\)
就存在\(c\)與\(x_0\)
\(x_0\)
\(c\times\)
如果\(f(x)\in \mathcal{O}(g(x))\)
就存在\(c\)與\(x_0\)
如果\(x>x_0\)
\(x_0\)
\(c\times\)
\(x\)
如果\(f(x)\in \mathcal{O}(g(x))\)
就存在\(c\)與\(x_0\)
如果\(x>x_0\)
\(c\times g(x)\geq f(x)\)
\(x_0\)
\(c\times\)
\(x\)
\(c\times g(x)\)
\(f(x)\)
如果\(x\geq1\)
\(x^2-x+2\)
\(\leq x^2+x+2\)
\(\leq x^2+x^2+2x^2=4x^2\)
如果\(x\geq1\)
\(x^2-x+2\)
\(\leq x^2+x+2\)
\(\leq x^2+x^2+2x^2=4x^2\)
設\(c=4,x_0=1\)
如果\(x_0\leq x\)
\(x^2-x+2\leq cx^2\)
因此
\(x^2-x+2 \in O(x^2)\)
\(f(x)\in \mathcal{O}(x^n)\)
// Find max value index in array
int index = -1;
for(int i=0;i<N;++i)
{
bool flag = true;
for(int j=0;j<N;++j)
{
if( a[j] > a[i] )
flag = false;
}
if( flag )
index = i;
}
如果一個演算法複雜度是\(O(x^2)\),表示該方法最糟不會比所有\(O(x^2)\)的方法糟糕
// Find max value index in array
int index = 0;
for(int i=1;i<N;++i)
{
if( a[i] > a[index] )
index = i;
}
\(O(x^5)\)
\(O(x^3)\)
\(O(x)\)
\(O(logn!)=O(nlogn)\)
把題目測資上限\((N=20000)\)代入
如果大於\(10^8\),基本上就會TLE
資料數量 | |
---|---|
struct/class | 任意多 |
std::pair | 2 |
std::tuple | 任意多 |
//定義
struct Point{
int x;
int y;
string name;
};
//宣告
Point data, array[666];
//使用變數
data.x = data.y = array[2].x = 7122;
data.name = "StarStar";
struct Point{
int x;
int y;
string name;
Point(){ //建構子
name = "7122";
}
};
//宣告
Point data; //會呼叫建構子
cout<< data.name ;
Output:
7122
struct Point{
int x;
int y;
string name;
Point(int a,int b){ //建構子
x=a; y=b;
}
};
//宣告
Point data(94,87); //會呼叫建構子
cout<< data.x << ' ' <<data.y ;
Output:
94 87
struct Point{
int x;
int y;
string name;
Point(int a,int b){ //建構子
x=a; y=b;
}
};
//宣告
Point data; //會呼叫建構子
Output:
Compile Error!
Can not find Point()
struct Point{
int x;
int y;
string name;
Point(int a,int b){ //建構子
x=a; y=b;
}
Point(){ //建構子 called
name="MAAAA";
}
};
//宣告
Point data;
cout<<data.name;
Output:
MAAAA
#include<utility> //also in algorithm
using pis = std::pair<int,string>;
typedef std::pair<int,string> pis2; //Old C++
pii data;
data.first = 7122;
data.second = "TFcis";
data = {123,"ABC"};
data = make_pair(123,"ABC");
using pii = pair<int,int>;
pii a[3] = { {2,1},{1,2},{3,0} };
sort( a,a+3 );
for( auto v:a )
cout<<v.first<<' '<<v.second<<endl;
Output:
1 2
2 1
3 0
typedef pair<int,pair<int,int>> piii; //C++11
typedef pair<int,pair<int,int> > piii2;
piii a;
a.first = a.second.first = a.second.second = 87
#include<utility> //also in algorithm
using tiii= std::tuple<int,int,int>;
tiii a;
a = {1,2,3}; // C++11 and g++ >= 6
a = make_tuple(1,2,3); // Always OK
#include<utility> //also in algorithm
using tiii= std::tuple<int,int,int>;
tiii a = {1,2,3};
auto [i,j,k] = a; //C++17
int i,j,k;
tie(i,j,k) = a; //Always OK
i = get<0>(a);
j = get<1>(a);
k = get<2>(a); //Always OK
#include<vector>
std::vector<int> arr;
arr.resize(N); //設定大小為N
arr[0] = arr[N-1] = 1; //與一般陣列一樣
vector<int> v;
v.emplace_back(2);
for(int i:v) cout<<i<<' ';
Output:
1 2
vector<tuple<int,int,int>> v;
v.emplace_back(7,8,9);
v.push_back({1,2,3});//C++11 & G++ >= 6.0
v.push_back(make_tuple(4,5,6));
for(auto [i,j,k]:v) //C++17
cout<<i<<' '<<j<<' '<<k<<endl;
Output:
1 2 3
4 5 6
7 8 9