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.
Science 7 - LAND and SEA BREEZE and its Characteristics
Acm aleppo cpc training eighth session
1. ACM Aleppo CPC Training
Part 1 C++ Programming Concepts
By Ahmad Bashar Eter
2. Reminder: Meet in the middle
• We use this technique to reduce the search time complexity by
doing half of the operations and store the result then we do the
other half in revers way and search for the first half in the previously
stored results using binary search.
• We can reduce the search complexity by half time the complexity of
binary search utilizing the memory complexity instead of time
complexity.
• The power of this technique is the binary search which has a
logarithmic complexity that’s why we can use it to search stored
data with size of 𝑶 𝒏 𝒌 in 𝑶 𝒍𝒐𝒈(𝒏) time complexity.
• We can apply this technique if the problem state can be divided to
half and if we can reveres the operations of the second half and if
we have enough memory to store the first half state.
3. The Stack Container
• Stack is a container which follows the LIFO (Last In First Out) order and
the elements are inserted and deleted from one end of the container.
The element which is inserted last will be extracted first.
• stacks are implemented as containers adaptors, which are classes that
use an encapsulated object of a specific container like vector, deque and
list.
• Some of the member functions of Stack are:
push( ): Insert element at the top of stack. Its time complexity is O(1).
pop( ): removes element from top of stack. Its time complexity is O(1).
top( ): access the top element of stack. Its time complexity is O(1).
empty( ): checks if the stack is empty or not. Its time complexity is O(1).
size( ): returns the size of stack. Its time complexity is O(1).
Reference:
4. The Queue Container
• Queue is a container which follows FIFO order (First In First Out) . Here
elements are inserted at one end (rear ) and extracted from another
end(front).
• Like the stacks. The Queue are implemented as containers adaptors,
which are classes that use an encapsulated object of a specific container
like deque and list.
Reference:
5. The Queue Container
• Some member function of Queues are:
push( ): inserts an element in queue at one end(rear ).
Its time complexity is O(1).
pop( ): deletes an element from another end if queue(front).
Its time complexity is O(1).
front( ): access the element on the front end of queue.
Its time complexity is O(1).
empty( ): checks if the queue is empty or not.
Its time complexity is O(1).
size( ): returns the size of queue. Its time complexity is O(1)
Reference:
6. The Deque Container
• Double-ended queues are sequence containers with dynamic sizes that
can be expanded or contracted on both ends (either its front or its
back).
• they provide a functionality similar to vectors, but with efficient
insertion and deletion of elements also at the beginning of the
sequence, and not only at its end. But, unlike vectors, deques are not
guaranteed to store all its elements in contiguous storage locations
Reference:
7. The Deque Container
• Like vectors, deque container have the flowing methods in it:
Reference:
DescriptionMethod
Return iterator to beginningbegin
Return iterator to endend
Return sizesize
Test whether vector is emptyempty
Clear contentClear
Access elementoperator[] ,at
Erase elementserase
Insert elementsinsert
8. The Deque Container
• Like vectors, deque container have the flowing methods in it:
Reference:
DescriptionMethod
Access first elementfront
Access last elementback
Add element at the endpush_back
Delete last elementpop_back
Insert element at beginningpush_front
Delete first elementpop_front
9. Example 1: Biased Chandan
Chandan is an extremely biased person, and he dislikes people who fail to solve
all the problems in the interview he takes for hiring people. There are n people
on a day who came to be interviewed by Chandan.
Chandan rates every candidate from 0 to 10. He has to output the total ratings of
all the people who came in a day. But, here's the problem: Chandan gets
extremely frustrated when someone ends up scoring a 0 in the interview. So in
frustration he ends up removing the candidate who scored that 0, and also
removes the candidate who came before him. If there is no candidate before the
one who scores a 0, he does nothing.
You've to find the summation of all the ratings in a day for Chandan.
Solve At:
10. Example 1: Biased Chandan
Input constraints:
The first line of input will contain an integer — n. The next n lines will contain an
integer, where the ith integer represents the rating of the ith person.
Output constraints:
Print the required sum.
Constraints:
1 ≤ n ≤5 * 103
0 ≤ Value of ratings ≤10
Solve At:
12. Example 2: Monk and Philosopher's Stone
Harry Potter wants to get the Philosopher's stone to protect it from Snape. Monk
being the guard of Philosopher's Stone is very greedy and has a special bag, into
which he can add one gold coin at a time or can remove the last gold coin he
added. Monk will sleep, once he will have the enough number of gold coins
worth amount X. To help Harry, Dumbledore has given a same kind of bag to
Harry (as of Monk) with N gold coins each having worth A[i] where i range from
1≤i≤N.
Solve At:
13. Example 2: Monk and Philosopher's Stone
Dumbledore also gave him a set of instructions which contains two types of
strings:
1) "Harry" (without quotes): It means Harry will remove ithith coin from his bag
and throw it towards Monk and Monk will add it in his bag, where will start
from 1 and go up to N.
2) "Remove" (without quotes): it means Monk will remove the last coin he added
in his bag.
Once the worth of the coins in Monk's bag becomes equal to X, Monk will go to
sleep. In order to report Dumbledore, Harry wants to know the number of coins
in Monk's bag, the first time their worth becomes equal to X.
Help Harry for the same and print the required number of coins. If the required
condition doesn't occur print "-1" (without quotes).
Solve At:
14. Example 2: Monk and Philosopher's Stone
Input: The first line will consists of one integer N denoting the number of gold
coins in Harry's Bag. Second line contains N space separated integers, denoting
the worth of gold coins. Third line contains 2 space separated integers Q and X,
denoting the number of instructions and the value of X respectively.
In next Q lines, each line contains one string either "Harry" (without quotes) or
"Remove" (without quotes).
Output: In one line, print the number of coins in the Monk's bag, the first time
their worth becomes equal to X.
Constraints:
1≤N≤10^4 , 1≤A[i]≤10^4
1≤Q≤10%5 , 1≤X≤10^7
Solve At:
15. Example 2: Monk and Philosopher's Stone
Sample Input
4
3 1 1 4
6 7
Harry
Harry
Harry
Remove
Remove
Harry
Solve At:
Sample Output
2
16. Example 3: Little Monk and Goblet of Fire
Albus Dumbledore announced that the school will host the legendary event
known as Wizard Tournament where four magical schools are going to compete
against each other in a very deadly competition by facing some dangerous
challenges. Since the team selection is very critical in this deadly competition.
Albus Dumbledore asked Little Monk to help him in the team selection process.
There is a long queue of students from all the four magical schools. Each student
of a school have a different roll number. Whenever a new student will come, he
will search for his schoolmate from the end of the queue. As soon as he will find
any of the schoolmate in the queue, he will stand behind him, otherwise he will
stand at the end of the queue. At any moment Little Monk will ask the student,
who is standing in front of the queue, to come and put his name in the Goblet of
Fire and remove him from the queue.
Solve At:
17. Example 3: Little Monk and Goblet of Fire
There are Q operations of one of the following types:
• E x y: A new student of school x (1≤x≤4) whose roll number is y (1≤y≤50000) will
stand in queue according to the method mentioned above.
• D: Little Monk will ask the student, who is standing in front of the queue, to
come and put his name in the Goblet of Fire and remove him from the queue
Now Albus Dumbledore asked Little Monk to tell him the order in which student
put their name. Little Monk is too lazy to that so he asked you to write a program
to print required order.
Note: Number of dequeue operations will never be greater than enqueue
operations at any point of time.
Solve At:
18. Example 3: Little Monk and Goblet of Fire
Input Format:
First line contains an integer Q (1≤Q≤100000), denoting the number of operations.
Next Q lines will contains one of the 2 types of operations.
Output Format:
For each 2nd type of operation, print two space separated integers, the front
student's school and roll number.
Solve At:
19. Example 3: Little Monk and Goblet of Fire
Sample Input
5
E 1 1
E 2 1
E 1 2
D
D
Solve At:
Sample Output
1 1
1 2
20. The Priority Queue Container
• Priority Queue is similar to queue, but with a one difference the
element with highest priority will be moved to the front of the queue.
Thus it is possible that when you enqueue an element at the back in the
queue, it can move to front because of its highest priority.
• In another words Priority queues are a type of container adaptors,
specifically designed such that its first element is always the greatest of
the elements it contains.
• This context is similar to a heap, where elements can be inserted at any
moment, and only the max heap element can be retrieved.
Reference:
21. The Priority Queue Container
• The Priority Queue are implemented as containers adaptors, which are
classes that use an encapsulated object of a specific container like
deque and vector.
• Priority Queue uses algorithm functions make_heap, push_heap and
pop_heap when needed to preserve the heap context to effectively
have the maximum element at the top of the heap orat the front of the
container.
Reference:
22. The Priority Queue Container
• Some member function of Queues are:
empty(): Returns true if the priority queue is empty and false if the
priority queue has at least one element. Its time complexity is
O(1).
pop(): Removes the largest element from the priority queue. Its time
complexity is O(logN) where N is the size of the priority queue.
push(): Inserts a new element in the priority queue. Its time complexity
is O(logN) where N is the size of the priority queue.
size(): Returns the number of element in the priority queue. Its time
complexity is O(1).
top(): Returns a reference to the largest element in the priority queue.
Its time complexity is O(1).
Reference:
23. Example 4: Monk And Champions League
Monk's favourite game is Football and his favourite club is "Manchester United".
Manchester United has qualified for the Champions League Final which is to be
held at the Wembley Stadium in London. So, he decided to go there and watch
his favourite team play. After reaching the stadium, he saw that many people
have lined up for the match tickets. He knows that there are M rows in the
stadium with different seating capacities. They may or may not be equal. The
price of the ticket depends on the row. If the row has K(always greater than 0)
vacant seats, then the price of the ticket will be K pounds(units of British
Currency). Now, every football fan standing in the line will get a ticket one by
one.
Given the seating capacities of different rows, find the maximum possible
pounds that the club will gain with the help of the ticket sales.
Solve At:
24. Example 4: Little Monk and Goblet of Fire
Input: The first line consists of M and N. M denotes the number of seating rows
in the stadium and N denotes the number of football fans waiting in the line to
get a ticket for the match.
Next line consists of M space separated integers X[1],X[2],X[3]....
X[M] where X[i] denotes the number of empty seats initially in the ith row.
Output:
Print in a single line the maximum pounds the club will gain.
Constraints:
1 <= M <= 1000000
1 <= N <= 1000000
1 <= X[i] <= 1000000
Sum of X[i] for all 1 <= i <= M will always be greater than N.
Solve At:
25. Example 4: Little Monk and Goblet of Fire
Sample Input
3 4
1 2 4
Solve At:
Sample Output
11