Introduction to olympic programming

Slides:



Advertisements
Similar presentations
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Advertisements

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
CSE Lecture 12 – Linked Lists …
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Introduction to the STL
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
COMP 171 Data Structures and Algorithms Tutorial 1 Template and STL.
More on the STL vector list stack queue priority_queue.
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Lecture 11 Standard Template Library Stacks, Queue, and Deque Lists Iterators Sets Maps.
1 LoopsBranching Condition Statement list T F Condition Statement list T F.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
DATA STRUCTURES ACM EXECUTIVE BODY 2k11.  A series of elements of same type  Placed in contiguous memory locations  Can be individually referenced.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
STL !!!generic programming!!! Anar Manafov
Containers Overview and Class Vector
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
2/17/2016IT 2791 Homogeneous aggregate // Using array string name[20]; int mid1[20]; int final[20]; double GPA[20]; name[0] = "Tom"; GPA[2] = 3.21; name:
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Mark Redekopp David Kempe
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 2 Algorithm Analysis
Popping Items Off a Stack Using a Function Lesson xx
Chapter 1.2 Introduction to C++ Programming
Standard Template Library (STL)
Andy Wang Object Oriented Programming in C++ COP 3330
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Basic Data Structures.
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Introduction to C++ STL
TIPS: How to Be Successful
priority_queue<T>
Graphs: As a mathematics branch
Chapter 9 One-Dimensional Arrays
C++ File Structure and Intro to the STL
Recursive Linked List Operations
Pass by Reference.
Level 1 STL – Linear DS fb.com/acmicpcfcicu. Static Arrays Creating 1D, 2D, ND Static Arrays. Accessing Time. Traversing a static array with N dimensions.
Homogeneous aggregate
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
STL Библиотека стандартных шаблонов
C++ File Structure and Intro to the STL
C++ STL Stack, Queue, and Deque
Lab4 problems More about templates Some STL
STL (Standard Template Library)
C++ Programming: chapter 10 – STL
Standard Template Library
An Introduction to STL.
Standard Template Library
Presentation transcript:

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