Introduction to olympic programming Lyzhin Ivan, 2015
Pascal vs С++ Pascal must die C++ is cool
Variables and types int, long long double, long double char string struct
Structure of program // Include additional headers #include <iostream> using namespace std; // Some other definitions // Main function - entry point of program int main() { cout << "Hello, students!" << endl; return 0; }
Input/output //C-style: fast, remember modifiers! %d, %s, %I64d, %c int a; char s[15]; long long b; char ch; scanf("%d %s %I64d %c", &a, s, &b, &ch); printf("%d %s %I64d %c", a, s, b, ch); //C++-style: slow, but easy //Hint: cin.sync_with_stdio(false) int a; string s; long 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: cout << "Day for great job!" << endl; break; case 5: cout << "Weekend is very close!" << endl; case 6: case 7: cout << "Weekend! I like it!" << endl; default: cout << "Hmm... I think it's wrong day." << endl; }
For/while statements int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; for (int i = n - 1; i >= 0; --i) cout << v[i] << ' '; stack<int> st; int x; while (cin >> x) st.push(x); while (!st.empty()) { cout << st.top() << ' '; st.pop(); }
Functions <return type> <name>(<args>) {body} // Define functions int fib(int n) { if (n == 1 || n == 2) return 1; else return fib(n - 1) + fib(n - 2); } void printMessage() cout << "C++ is cool!" << endl;
Structs // Define struct 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 BigObject { int a[10000]; }; void f1(BigObject big) // Slow { big = something; // Easy } void f2(BigObject &big) // Fast void f3(BigObject *big) // Fast *big = something; // Don't forget *! int main() { BigObject big; f1(big); // Easy f2(big); // Easy f3(&big); // Don't forget &! return 0; }
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>N0, N – input size Examples: Quadratic sort O(N^2), Quick sort O(N*logN), 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<int> &a, int x) { int l = 0, r = a.size(); while (r - l > 1) int mid = (r + l) / 2; if (x < a[mid]) r = mid; else l = mid; } return a[l] == x;
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<int> &a, int l, int r) { int i = l, j = r; int x = a[(l + r) / 2]; while (i <= j) while (a[i] < x) i++; while (a[j] > x) j--; if (i <= j) swap(a[i], a[j]); i++; j++; } if (j > l) quick_sort(a, l, j); if (i < r) quick_sort(a, j, r);
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.cplusplus.com/reference/stl/
STL magic – algorythms min_element sort max_element find #include <algorithm> min_element sort max_element find minmax_element count next_permutation swap prev_permutation remove lower_bound reverse upper_bound http://www.cplusplus.com/reference/algorithm/
STL magic - iterators set<int> s; s.insert(3); s.insert(5); for (set<int>::iterator iter = s.begin(); iter != s.end(); ++iter) cout << *iter << endl; for (auto x : s) //C++11 cout << x << endl;
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.cplusplus.com/reference/ - excellent documentation of C++ https://youtu.be/NiPGo5TZxlY – 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