Petingo
Taiwan
講義 & 題目:petingo.me/cp/
今天ㄉ內容
練習ㄉ地方
Virtual Judge
修幹ㄉ地方
怎麼練習呢?
Judge 的回應
$$10^{10}$$
Judge 的回應
C++ 基本
演算法
時間複雜度
舉例來說
時間複雜度
int n = 100;
int sum = 0;
for( int i = 1 ; i <= n ; i++ ){
sum += i;
}
int n = 100;
int sum = (1 + n) * n / 2;
時間複雜度
int n = 100;
int sum = 0;
for( int i = 1 ; i <= n ; i++ ){
sum += i;
}
int n = 100;
int sum = (1 + n) * n / 2;
$$n 次\rightarrow O(n)$$
$$1 次\rightarrow O(1)$$
時間複雜度
$$O(1)$$
$$O(log(n))$$
$$O(n)$$
$$O(n log(n))$$
$$O(n^2)$$
$$O(n^3)$$
$$O(2^n)$$
$$O(n!)$$
最快
最慢
時間複雜度
$$O(1)$$
$$O(log(n))$$
$$O(n)$$
$$O(n log(n))$$
$$O(n^2)$$
$$O(n^3)$$
$$O(2^n)$$
$$O(n!)$$
一秒大約可以跑 $$10^7~10^9$$
$$無限大$$
$$n = 10^{1?}$$
$$n = 10^7$$
$$n = 100,000$$
$$n = 1000$$
$$n = 100$$
$$n = 20$$
$$n = 12$$
常見
時間複雜度
O(1)
O(n)
int n = 100;
int sum = 0;
for( int i = 1 ; i <= n ; i++ ){
sum += i;
}
O(n^2)
24 | 3 | 7 | 14 | 9 |
23 | 20 | 22 | 5 | 19 |
13 | 2 | 8 | 4 | 10 |
1 | 15 | 6 | 25 | 21 |
16 | 12 | 17 | 18 | 11 |
int n = 5;
int matrix[n][n];
for( int i = 0 ; i < n ; i++ ){
for( int k = 0 ; k < n ; k++ ){
if( matrix[i][k] == 7 ){
...
}
}
}
O(log(n))
阿如果不固定的話呢?
int n = 52;
for( int i = 0 ; i < n ; i++ ){
if( poker[i] == "紅心A" ){
return i;
}
}
阿如果不固定的話呢?
int n = 52;
for( int i = 0 ; i < n ; i++ ){
if( poker[i] == "紅心A" ){
...
}
}
for( int i = 0 ; i < n ; i++ ){
if( poker[i] == "紅心K" ){
...
}
}
各種組合的話呢?
int n = 100;
for( int i = 0 ; i < n ; i++ ){
...
}
for( int i = 0 ; i < n ; i++ ){
for( int i = 0 ; i < n ; i++ ){
...
}
}
$$O(n + n^2) = O(n^2)$$
所以說
$$O(2n) = O(n)$$
$$O(n^2 + 3n) = O(n^2)$$
$$O(n+log(n)+n log(n)) = ?$$
尋找紅心 A
假如資料有單調性
假如資料有單調性
假如資料有單調性
Binary Search
$$O(log(n))$$
Binary Search
#include <cstdio>
int main() {
int array[] = {1, 3, 4, 6, 7, 8, 10, 13, 14, 18, 19, 21, 24, 37, 40, 45, 71};
int l = 0, r = 16;
while (l != r) {
int mid = (l + r) / 2;
if (array[mid] == 7)
r = l = mid;
else if (array[mid] >= 7)
r = mid;
else
l = mid + 1;
}
printf("%d\n", l);
}
除了找東西
STL 是啥東
標準模板庫(Standard Template Library)
包含演算法、一些常用的資料結構
STL - algorithm
$$\rightarrow nlog(n)$$
$$\rightarrow nlog(n)$$
stable vs. unstable
店名 | 評價 |
---|---|
井太郎 | 8 |
自助餐 | 2 |
溫州 | 8 |
上品 | 8 |
地中海 | 6 |
店名 | 評價 |
---|---|
自助餐 | 2 |
地中海 | 6 |
上品 | 8 |
溫州 | 8 |
井太郎 | 8 |
stable
店名 | 評價 |
---|---|
井太郎 | 8 |
自助餐 | 2 |
溫州 | 8 |
上品 | 8 |
地中海 | 6 |
店名 | 評價 |
---|---|
自助餐 | 2 |
地中海 | 6 |
井太郎 | 8 |
溫州 | 8 |
上品 | 8 |
unstable
店名 | 評價 |
---|---|
井太郎 | 8 |
自助餐 | 2 |
溫州 | 8 |
上品 | 8 |
地中海 | 6 |
店名 | 評價 |
---|---|
自助餐 | 2 |
地中海 | 6 |
上品 | 8 |
溫州 | 8 |
井太郎 | 8 |
sort()
#include <algorithm>
using namespace std;
int main() {
int array[7] = {5, 2, 3, 6, 4, 4, 9};
sort(array, array + 7);
// 2, 3, 4, 4, 5, 6, 9
}
sort() + reverse()
#include <algorithm>
using namespace std;
int main() {
int array[7] = {5, 2, 3, 6, 4, 4, 9};
sort(array, array + 7); // 先由小到大排序好
reverse(array, array + 7); // 再反轉
// 9, 6, 5, 4, 4, 3, 2
}
sort() + compare()
#include <algorithm>
using namespace std;
bool compare(int a, int b){
return a > b;
}
int main() {
int array[7] = {5, 2, 3, 6, 4, 4, 9};
sort(array, array + 7, compare);
// 9, 6, 5, 4, 4, 3, 2
}
sort() + greater<int>()
#include <algorithm>
#include <functional>
using namespace std;
int main() {
int array[7] = {5, 2, 3, 6, 4, 4, 9};
// sort(array, array + 7, less<int>());
sort(array, array + 7, greater<int>());
// 9, 6, 5, 4, 4, 3, 2
}
如果要排一組資料呢?
sort() + class
#include <algorithm>
using namespace std;
class Restaurant {
public:
string name;
int rank;
bool operator<(const Restaurant &other) const {
return rank < other.rank;
}
};
來練習ㄅ!
By Petingo