Введение в олимпиадное программирование.pptx
- Количество слайдов: 20
Introduction to olympic programming Lyzhin Ivan, 2015
Pascal vs С++ Pascal must die C++ is cool
Variables and types int, long double, long double char string struct
Structure of program // Include additional headers #include
Input/output //C-style: fast, remember modifiers! %d, %s, %I 64 d, %c int a; char s[15]; long b; char ch; scanf("%d %s %I 64 d %c", &a, s, &b, &ch); printf("%d %s %I 64 d %c", a, s, b, ch); //C++-style: slow, but easy //Hint: cin. sync_with_stdio(false) int a; string s; long b; char ch; cin >> a >> s >> b >> ch; cout << a << ' ' << s << ' ' << b << ' ' << ch;
If-else statement int a, b; cout << "I can divide 2 numbers! Input them: "; cin >> a >> b; if (b == 0) cout << "Are you sure? " << endl; else cout << "Ok! a/b=" << a / b << " a%b=" << a%b << endl;
Switch statement int day; cin >> day; switch (day) { case 1: case 2: case 3: case 4: cout << break; case 5: cout << break; case 6: case 7: cout << break; default: cout << } "Day for great job!" << endl; "Weekend is very close!" << endl; "Weekend! I like it!" << endl; "Hmm. . . I think it's wrong day. " << endl;
For/while statements int n; cin >> n; vector
Functions
Structs // Define struct Point { int x, y; Point(int _x, int _y) { x = _x; y = _y; } void print() { cout << "(" << x << ", " << y << ")" << endl; } };
Values, pointers and references struct Big. Object { int a[10000]; }; int main() { Big. Object big; f 1(big); // Easy f 2(big); // Easy f 3(&big); // Don't forget &! return 0; } void f 1(Big. Object big) // Slow { big = something; // Easy } void f 2(Big. Object &big) // Fast { big = something; // Easy } void f 3(Big. Object *big) // Fast { *big = something; // Don't forget *! }
Complexity Memory complexity – how much memory your program need? Time complexity – how much time your program need? T(N) = O(F(N)) if there exist constant C>0 such that T(N)<=C*F(N) for N>N 0, N – input size Examples: Quadratic sort O(N^2), Quick sort O(N*log. N), Linear search O(N) Link: http: //bigocheatsheet. com
Rules of “big O” Only the most big part No constants Logarithm without base Examples: § N^4 + 2*N^2 + 500 = O(N^4) § 100*N^2 + 20*N = O(N^2)
Basic algorythms: search bool linear_search(int *a, int n, int x) { for (int i = 0; i < n; ++i) if (a[i] == x) return true; return false; } bool binary_search(vector
Basic algorythms: sorting void sort(int *a, int n) { for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++i) if (a[i]>a[j]) swap(a[i], a[j]); } void quick_sort(vector
STL magic - containers vector (assign, resize, push_back, pop_back, size, []) stack (push, pop, top, empty, size) queue (push, pop, front, empty, size) priority_queue (push, pop, front, empty, size) deque (push_back, push_front, pop_back, pop_front) set (insert, erase, size) map (insert, erase, []) list (deque + insert, erase) Link: http: //www. cplus. com/reference/stl/
STL magic – algorythms #include
STL magic - iterators set
Solve it! http: //codeforces. com/gym/100092 http: //ipc. susu. ac. ru/index. html – qualifying contest
Useful links http: //mostinfo. net/article/8/18. htm - comparing Pascal and C++ operators http: //www. cplus. com/reference/ - excellent documentation of C++ https: //youtu. be/Ni. PGo 5 TZxl. Y – 90 minutes of C++ http: //codeforces. com – problems, contests http: //acm. timus. ru – problems, contests http: //ipc. susu. ac. ru/index. html – 17. 10. 2015 Contest! http: //codeforces. com/gym/100092 - Training http: //vk. com/vmi_programming - VK group http: //vk. com/ivan. lyzhin - my VK page