Download presentation
Presentation is loading. Please wait.
1
Introduction to olympic programming
Lyzhin Ivan, 2015
2
Pascal vs С++ Pascal must die C++ is cool
3
Variables and types int, long long double, long double char string
struct
4
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; }
5
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;
6
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;
7
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; }
8
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(); }
9
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;
10
Structs // Define struct struct Point { int x, y;
Point(int _x, int _y) x = _x; y = _y; } void print() cout << "(" << x << ", " << y << ")" << endl; };
11
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; }
12
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:
13
Rules of “big O” Only the most big part No constants
Logarithm without base Examples: N^4 + 2*N^ = O(N^4) 100*N^2 + 20*N = O(N^2)
14
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;
15
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);
16
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:
17
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
18
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;
19
Solve it! http://codeforces.com/gym/100092
– qualifying contest
20
Useful links - comparing Pascal and C++ operators - excellent documentation of C++ – 90 minutes of C++ – problems, contests – problems, contests – Contest! - Training - VK group - my VK page
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.