Скачать презентацию Introduction to olympic programming Lyzhin Ivan 2015 Скачать презентацию Introduction to olympic programming Lyzhin Ivan 2015

Введение в олимпиадное программирование.pptx

  • Количество слайдов: 20

Introduction to olympic programming Lyzhin Ivan, 2015 Introduction to olympic programming Lyzhin Ivan, 2015

Pascal vs С++ Pascal must die C++ is cool Pascal vs С++ Pascal must die C++ is cool

Variables and types int, long double, long double char string struct Variables and types int, long double, long double char string struct

Structure of program // Include additional headers #include <iostream> using namespace std; // Some Structure of program // Include additional headers #include 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, %I 64 d, %c int a; char 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 << 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: 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<int> v(n); for (int i = 0; For/while statements int n; cin >> n; vector v(n); for (int i = 0; i < n; ++i) cin >> v[i]; for (int i = n - 1; i >= 0; --i) cout << v[i] << ' '; stack 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 Functions () {body} // Define functions int fib(int n) { if (n == 1 || n == 2) return 1; else return fib(n - 1) + fib(n - 2); } void print. Message() { cout << "C++ is cool!" << endl; }

Structs // Define struct Point { int x, y; Point(int _x, int _y) { 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() { 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 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 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 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 &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; 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 &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, 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 <algorithm> min_element sort max_element find minmax_element count next_permutation swap STL magic – algorythms #include min_element sort max_element find minmax_element count next_permutation swap prev_permutation remove lower_bound reverse upper_bound http: //www. cplus. com/reference/algorithm/

STL magic - iterators set<int> s; s. insert(3); s. insert(5); s. insert(3); s. insert(2); STL magic - iterators set s; s. insert(3); s. insert(5); s. insert(3); s. insert(2); for (set: : 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 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. 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