The first lecture of the ACM Aleppo CPC training. The local contest of ICPC. This lecture will help you get started in programming contests word with the lower bound techniques. The lectures focus on the C++ programming language and the STL library to solve programming problems.
1. ACM Aleppo CPC Training
Part 1 C++ Programming Concepts
By Ahmad Bashar Eter
2. Reminder
This is the most used containers:
vector : dynamic array
list : linked list data structure
set : a sorted binary search tree
map : associated container a sorted binary search tree
unordedset : hash table
unordedmap : associated container hash table
queue : queue data structure
stack : stack data structure
priority queue : max heap data structure
Reference:
3. Example 1: Array
Vitaly has an array of n distinct integers. Vitaly wants to divide this array into
three non-empty sets so as the following conditions hold:
1. The product of all numbers in the first set is less than zero ( < 0).
2. The product of all numbers in the second set is greater than zero ( > 0).
3. The product of all numbers in the third set is equal to zero.
Each number from the initial array must occur in exactly one set.
Help Vitaly. Divide the given array.
Solve At:
4. Example 1: Array
Input: The first line of the input contains integer n (3 ≤ n ≤ 100). The second line
contains n space-separated distinct integers a1, a2, ..., an(|ai| ≤ 103) — the array
elements.
Output: In the first line print integer n1 (n1 > 0) — the number of elements in the
first set. Then print n1 numbers — the elements that got to the first set.
In the next line print integer n2 (n2 > 0) — the number of elements in the second
set. Then print n2 numbers — the elements that got to the second set.
In the next line print integer n3 (n3 > 0) — the number of elements in the third
set. Then print n3 numbers — the elements that got to the third set.
The printed sets must meet the described conditions. It is guaranteed that the
solution exists. If there are several solutions, you are allowed to print any of
them.
Solve At:
6. Some STL Algorithms
• We have seen so far how to sort data in ascending order we.
• Some time we need to sort data in special way, in order to
customize our sort algorithm we can give the sort algorithm a
predict function that tell the algorithm if two element are in the
right order or not.
• This function take two parameters a1,a2 and compare them and
return true if and only if the first one is considered to go before the
second.
• If you want to sort ascending the compare function must return:
a1 < a2
• If you want to sort descending the compare function must return:
a1 > a2
Reference:
7. Some STL Algorithms
• There are a lot of build in algorisms on STL these are some of them:
Reference:
DescriptionFunction
Find value in rangefind
Copy range of elementscopy
Fill range with valuefill
Transform rangetransform
Remove consecutive duplicates in rangeunique
Reverse rangereverse
Sort elements in rangesort
Sort elements preserving order of equivalentsstable_sort
Lexicographical less-than comparisonlexicographical_compare
Transform range to next permutationnext_permutation
Transform range to previous permutationprev_permutation
Return distance between iteratorsdistance
8. Example 2: The Monk and Class Marks
Monk is a multi-talented person, and prepares results for his college in his free
time. (Yes, he is still in love with his old college!) He gets a list of students with
their marks. The maximum marks which can be obtained in the exam is 100.
The Monk is supposed to arrange the list in such a manner that the list is sorted
in decreasing order of marks. And if two students have the same marks, they
should be arranged in lexicographical manner.
Help Monk prepare the same!
Solve At:
9. Example 2: The Monk and Class Marks
Input:
On the first line of the standard input, there is an integer N, denoting the number
of students. N lines follow, which contain a string and an integer, denoting the
name of the student and his marks.
Output:
You must print the required list.
Constraints:
1 <= N <= 105
1 <= | Length of the name | <= 100
1 <= Marks <= 100
Solve At:
10. Example 2: The Monk and Class Marks
Sample Input
3
Eve 78
Bob 99
Alice 78
Solve At:
Sample Output
Bob 99
Alice 78
Eve 78
11. Set container
• Sets are containers that store unique elements following a specific
order.
• In a set, the value of an element also identifies it (the value is itself
the key, of type T), and each value must be unique. The value of the
elements in a set cannot be modified once in the container (the
elements are always const), but they can be inserted or removed
from the container.
• Internally, the elements in a set are always sorted following a
specific strict weak ordering criterion indicated by its internal
comparison object (of type Compare).
• set containers are generally slower than unordered_set containers
to access individual elements by their key, but they allow the direct
iteration on subsets based on their order.
Reference:
12. Set container
• Set containers don’t allow duplicated elements if you want to allow
duplicated elements use mulitset.
• Sets are typically implemented as binary search trees.
• Some common set functions:
Reference:
DescriptionFunction
Return iterator to beginningbegin
Return iterator to endend
Test whether container is emptyempty
Return container sizesize
Insert elementinsert
Erase elementserase
Clear contentclear
Get iterator to elementfind
13. Mulitset container
• mulitset containers allow duplicated elements and has all of the set
member functions with some additional functions.
• Some common mulitset functions:
Reference:
DescriptionFunction
Return iterator to lower boundlower_bound
Return iterator to upper boundupper_bound
Get range of equal elementsequal_range
14. Unordered set container
• unordered_set are containers that store unique elements in no
particular order, and which allow for fast retrieval of individual
elements based on their value.
• In an unordered_set, the value of an element is at the same time its
key, that identifies it uniquely. Keys are immutable, therefore, the
elements in an unordered_set cannot be modified once in the
container - they can be inserted and removed, though.
• Internally, the elements in the unordered_set are not sorted in any
particular order, but organized into buckets depending on their hash
values to allow for fast access to individual elements directly by
their values (with a constant average time complexity on average).
Reference:
15. Unordered set container
• unordered_set containers are faster than set containers to access
individual elements by their key, although they are generally less
efficient for range iteration through a subset of their elements.
• unordered_set have the same common functions in the set.
• Like the set there is unordered_multiset that allow duplicated
elements and have the same common functions of the multiset.
Reference:
16. Map container
• Maps are associative containers that store elements formed by a
combination of a key value and a mapped value, following a specific
order.
• In a map, the key values are generally used to sort and uniquely
identify the elements, while the mapped values store the content
associated to this key. The types of key and mapped value may
differ, and are grouped together in member type value_type, which
is a pair type combining both.
• Internally, the elements in a map are always sorted by its key
following a specific strict weak ordering criterion indicated by its
internal comparison object (of type Compare).
Reference:
17. Map container
• map containers are generally slower than unordered_map
containers to access individual elements by their key, but they allow
the direct iteration on subsets based on their order.
• The mapped values in a map can be accessed directly by their
corresponding key using the bracket operator ((operator[]).
• Maps are typically implemented as binary search trees.
• map containers have the same common functions in the set with
differences in parameters type.
• Like set there is multimap container witch allow duplicated keys and
unordered_map and unordered_multimap which implements hash
table.
Reference:
18. Example 3: Monk's Birthday Party
Monk's birthday is coming this weekend! He wants to plan a Birthday party and
is preparing an invite list with his friend Puchi. He asks Puchi to tell him names to
add to the list.
Puchi is a random guy and keeps coming up with names of people randomly to
add to the invite list, even if the name is already on the list! Monk hates
redundancy and hence, enlists the names only once.
Find the final invite-list, that contain names without any repetition.
Solve At:
19. Example 3: Monk's Birthday Party
Input:
First line contains an integer T. T test cases follow.
First line of each test contains an integer N, the number of names that Puchi pops
up with.
Output:
For each testcase, Output the final invite-list with each name in a new line. The
names in the final invite-list are sorted lexicographically.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 105
1 ≤ Length of each name ≤ 105
Solve At:
21. Example 4: Sumsets
Given S, a set of integers, nd the largest d such that a + b + c = d
where a, b, c, and d are distinct elements of S.
Input
Several S, each consisting of a line containing an integer 1 n
1000 indicating the number of elements in S, followed by the elements
of S, one per line. Each element of S is a distinct integer between -
536870912 and +536870911 inclusive.
The last line of input contains `0'.
Output
For each S, a single line containing d, or a single line containing `no solution'.
Solve At:
23. Example 5: Ananagrams
Most crossword puzzle fans are used to anagrams--groups of words with the
same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST.
Some words however do not have this attribute, no matter how you rearrange
their letters, you cannot form another word. Such words are called ananagrams,
an example is QUIZ.
Obviously such definitions depend on the domain within which we are working;
you might think that ATHENE is an ananagram, whereas any chemist would
quickly produce ETHANE. One possible domain would be the entire English
language, but this could lead to some problems. One could restrict the domain
to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not
in the same domain) but NOTE is not since it can produce TONE.
Solve At:
24. Example 5: Ananagrams
Write a program that will read in the dictionary of a restricted domain and
determine the relative ananagrams. Note that single letter words are, ipso facto,
relative ananagrams since they cannot be ``rearranged'' at all. The dictionary will
contain no more than 1000 words.
Solve At:
25. Example 5: Ananagrams
Input: Input will consist of a series of lines. No line will be more than 80
characters long, but may contain any number of words. Words consist of up to 20
upper and/or lower case letters, and will not be broken across lines. Spaces may
appear freely around words, and at least one space separates multiple words on
the same line. Note that words that contain the same letters but of differing case
are considered to be anagrams of each other, thus tIeD and EdiT are anagrams.
The file will be terminated by a line consisting of a single #.
Output : Output will consist of a series of lines. Each line will consist of a single
word that is a relative ananagram in the input dictionary. Words must be output
in lexicographic (case-sensitive) order. There will always be at least one relative
ananagram.
Solve At:
26. Example 5: Ananagrams
Sample Input
ladder came tape soon leader acme
RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE
derail LaCeS drIed
noel dire Disk mace Rob dries
#
Solve At:
Sample Output
Disk
NotE
derail
drIed
eye
ladder
soon