0% found this document useful (0 votes)
12 views25 pages

L8a STL Intro

Uploaded by

loxine5672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views25 pages

L8a STL Intro

Uploaded by

loxine5672
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Introduction to Competitive

Programming*

Acknowledgements: ppt by William W.Y. Hsu


entitled “Introduction to Competitive Programming”
Why CP

• Makes you a better programmer and thinker in many


situations
• Intangible skill that will set you apart in the workforce
• Start preparing for ACM-ICPC, and more
• It's fun :)

2
Ultimate goal

• Please note that being well-versed in competitive


programming is not the end goal, but only a means to an end.
• The true end goal is to produce all-rounder computer
scientists/ programmers who are much readier to produce
better software and to face harder CS research problems in
the future.

3
Competitive Programming

• Given well-known computer science problems, solve


them as fast as possible.
– Find a solution that reduces down to a well-known
problems, not research problems.
– Pass all the judge data correctly.
– Solution should run fast enough.

4
How?

• Study common types of problems.


• Show common applications of algorithms and data structures
you already know from:
– the algorithms course
– the data structures course
• Introduce other common algorithms and data structures.
• Go over some commonly used theory.
• Practice problem solving.
• Practice programming.
• More practice.

5
Some Good Books

• Steven Halim, Competitive Programming, 3rd Edition, ASIN:


B00FG8MNN8.
• S. S. Skiena, M. A. Revilla, Programming Challenges: The
Programming Contest Training Manual, Springer, 2003 ed.
• S. Skiena, The Algorithm Design Manual, 2nd ed., Springer.
(ISBN-10: 1848000693)
• R. Graham, D. Knuth, O. Patashnik, Concrete Mathematics: A
Foundation for Computer Science.

6
The problems

• Typical programming contest problems usually consists of:


– Problem description
– Input description
– Output description
– Example input/output
– A time limit in seconds
– A memory limit in megabytes
• You are asked to write a program that solves the problem for
all valid inputs.
– partial solved problem receive partial score.
• The program must not exceed time or memory limits!

7
Example problem*

Problem description
Write a program that multiplies pairs of integers.

Input description
Input starts with one line containing an integer , where ,
denoting the number of test cases. Then lines follow, each
containing a test case. Each test case consists of two integers ,
where , separated by a single space.

Output description
For each test case, output one line containing the value of

*PPT: Introduction to Competitive Programming by Instructor: William W.Y. Hsu 8


Example problem

Sample input Sample output


4 12
34 0
13 0 8
18 10000
100 100

1/27/2025 *PPT: Introduction to Competitive Programming by Instructor: William W.Y. Hsu 9


Correctness?

• The solutions are incorrect!


• If , the output is 0.
– Correct solution is .
• 32-bit integer is too small, overflow will occur.
– Can switch to 64-bit integer mode. (Architecture dependent)
• Remedy:
– long long int

1/27/2025 *PPT: Introduction to Competitive Programming by Instructor: William W.Y. Hsu 10


Judge verdicts (ACM-ICPC)

• Feedback about solutions is limited


• You will (usually) receive one of:
– Accepted
– Wrong Answer
– Compile Error
– Run Time Error
– Time Limit Exceeded
– Memory Limit Exceeded

11
Tips

• There are a couple of tips and guidelines you can keep in mind
towards becoming a more effective programmer and better
problem solver.

12
Tip 0: Faster typing*

• Become a faster/better typist.


• Know your IDE (Different ACM- ICPC competition/sites might
have different IDE)
• Don’t let your fingers be the limiting factor of solving
problems quickly.
• Good problem solvers have simple solutions; they don’t have
to type as much, but it’s still important to type in quickly

• TypeRacer is a fun and effective way to practice:


http://play.typeracer.com/

*PPT: Introduction to Competitive Programming by Instructor: William W.Y. Hsu 13


Tips: Quickly classify problems

• Practice quickly identifying problem types: Array/ stack/


Greedy/ D&C/ DP/ Graph/ Math
• Do the quick algo analysis based on given constraints and
decide which algo is needed: O(n2) or O(n log n) or O (n) or
O (log n) etc
• Aim to think of brute force solution atleast in the initial days,
if other solution is not coming to your mind.

1/27/2025 14
Tips: Master programming languages

• You should know your programming language like the back of


your hand.
• After thinking of a solution, convey the solution in code as
quickly as possible.
• Use libraries, shortcuts, and write simple code.
• This includes your programming language’s library:
– C++’s Standard Template Library (STL)
– The Java Class Library
• If it’s already implemented in the standard library, you usually
don’t need to implement it yourself!

1/27/2025 15
Tips: Master programming languages

• Java:
– Scanner, BigInteger, String static functions,
– Collections, different data types
– Integer.parseInt()
– String.substring()
– etc
• C++
– sort(), vector<T>, STL
• C
– qsort(), malloc(), …

1/27/2025 Introduction to Competitive Programming 16


Tips: Test your solution

• You want to make sure your solution is correct and


• runs within the time limit.
• Or you already know it’s wrong, but don’t know why…
– Try to break your solution by finding a counterexample.
– (an input for which your solution gives incorrect output, or takes too
long to compute an answer)
• Try boundary cases, large inputs, .special & exceptional
inputs..
• Submit correctly!
– Competitions only care about correct code.
– Wrong submissions may have penalty
– The best teams write test cases before submitting their solutions!

17
Most Important Tip

• Problem solving and programming skills come with practice.


• So Practice,
• more practice,
• and more practice

1/27/2025 18
C++: STL
• Standard Template Library
• You have already studied templates., STL is extension to
that
• Collections of useful classes for common data structures
• Ability to store objects of any type (template)
• Containers form the basis for treatment of data
structures
• Container –class that stores a collection of data
• STL consists of 10 container classes:
– Sequence containers
– Adapter containers
– Associative containers
1/27/2025 19
C++: STL
• The standard template library (STL) contains
– Containers
– Algorithms
– Iterators
• A container is a way that stored data is organized in
memory, for example an array of elements.
• Algorithms in the STL are procedures that are applied to
containers to process their data, for example search for
an element in an array, or sort an array.
• Iterators are a generalization of the concept of pointers,
they point to elements in a container, for example you
can increment an iterator to point to the next element in
an array 20
C++: STL
• https://www.topcoder.com/community/data-
science/data-science-tutorials/power-up-c-with-the-
standard-template-library-part-1/
• https://www.topcoder.com/community/data-
science/data-science-tutorials/power-up-c-with-the-
standard-template-library-part-2/
• Good details are given in IIITD’s ppt on STL
• Let us have a look
• I started using STL by copy-paste of simple codes like
vectors, map/hash (let us have a look in DevCpp, code-
generation settings changed to have language standard:
ISO C++ 11)

21
#include <bits/stdc++.h>
/*either include bits/stdc++.h or include indiv header files like
#include<iostream>
#include<vector> */
using namespace std;
int main()
{ // Initializing vector with values
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2; // Declaring new vector
// copy elements of old vector into new vector by Iterative method
for (int i=0; i<vect1.size(); i++)
vect2.push_back(vect1[i]);
cout << "Old vector elements are : ";
for (int i=0; i<vect1.size(); i++)
cout << vect1[i] << " ";
}
Practice Using STL
• Using stack STL, Find whether a given string is palindrome
or not
• Using vector, find average of all numbers, T times, where
each time user tells some new elements or deletes some
old elements. Use resize, shrink_to_fit , reserve aetc.
• Use hash_table and map of STL and solve any simple
problem

24
int T,n,val1; vector<int> vect1;
ifstream infile ("in1.txt"); ofstream outfile("out1.txt");
if(!infile){cout<< "input file missing\n"; return -1;}
infile >> T;
while (T--)
{ infile >>n;
for(int i = 0; i < n; i++)
{ infile >> val1;
vect1.push_back(val1); }
vector<int> vect2;
for (int i=vect1.size() -1; i>=0; i--)
vect2.push_back(vect1[i]);
outfile << "Vector 2: Old vector elements in reverse order are ";
for (int i=0; i<vect1.size(); i++)
outfile << vect2[i] << " ";
outfile << endl;
vect2.clear();//is of no use as recreated everytime, but vect1 gets appended
(not over-written with new inputs in each iterations
//vect1.clear(); //clear vect1 for no append to previous inputs

You might also like