Ilovepdf Merged
Ilovepdf Merged
Example
After rotations, .
Function Description
Returns
Input Format
The first line contains two space-separated integers that denote , the number of integers,
and , the number of left rotations to perform.
The second line contains space-separated integers that describe .
Constraints
Input Output
STDIN Function 5 1 2 3 4
----- --------
5 4 n = 5 d = 4
1 2 3 4 5 arr = [1, 2, 3, 4, 5]
Explanation
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
To perform left rotations, the array undergoes the following sequence of changes:
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
You have an empty stack and you are given some queries. These queries are the basic stack
operations such as Push, Pop, and printing the Top element. Now, you should process the
given queries.
Input
Output
For each query 3, print the top element of the stack. If the stack is empty, print 'Empty!'
without quotes.
Example
Input Output
6 15
1 15 Empty!
1 20
2
3
2
3
Warning!
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
You have an empty queue and your boss has some queries. These queries are queue's basic
operations such as Enqueue, Dequeue, and printing some values. Now, your boss asks you to
process his queries.
Input
Output
For each query 3, print the queue's first element's value. If the queue is empty, print
'Empty!' without quotes.
Example
Input Output
6 6
1 5 Empty!
1 6
2
3
2
3
Warning!
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Two brackets are considered to be a matched pair if the an opening bracket (i.e., ( , [ , or { )
occurs to the left of a closing bracket (i.e., ) , ] , or } ) of the exact same type. There are
three types of matched pairs of brackets: [] , {} , and () .
A matching pair of brackets is not balanced if the set of brackets it encloses are not matched.
For example, {[(])} is not balanced because the contents in between { and } are not
balanced. The pair of square brackets encloses a single, unbalanced opening bracket, ( , and
the pair of parentheses encloses a single, unbalanced closing square bracket, ] .
By this logic, we say a sequence of brackets is balanced if the following conditions are met:
Function Description
Returns
Input Format
Constraints
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Output Format
Input Output
Explanation
1. The string {[()]} meets both criteria for being a balanced string.
2. The string {[(])} is not balanced because the brackets enclosed by the matched pair
{ and } are not balanced: [(]) .
3. The string {{[[(())]]}} meets both criteria for being a balanced string.
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Ada the Ladybug has many things to do. She puts them into her queue. Anyway she is very
indecisive, so sometime she uses the top, sometime the back and sometime she decides to
reverses it.
Input
The first line consists of 1 ≤ Q ≤ 106, number of queries. Each of them contains one of
following commands
Output
If you would get this type of query and the queue would be empty, print "No job for Ada?"
instead.
Example
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Input Output
15 93
toFront 93 No job for Ada?
front No job for Ada?
back 80
reverse 53
back 66
reverse
toFront 80
push_back 53
push_back 50
front
front
reverse
push_back 66
reverse
front
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Problem F. Array
Time limit 2000 ms
Mem limit 262144 kB
Input file stdin
Output file stdout
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.
4. Each number from the initial array must occur in exactly one set.
Input
The first line of the input contains integer n (3 ≤ n ≤ 100). The second line contains n space-
Output
In the first line print integer n1 (n1 > 0) — the number of elements in the first set. Then
In the next line print integer n2 (n2 > 0) — the number of elements in the second set. Then
In the next line print integer n3 (n3 > 0) — the number of elements in the third set. Then
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.
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Examples
Input Output
3 1 -1
-1 2 0 1 2
1 0
Input Output
4 1 -1
-1 -2 -3 0 2 -3 -2
1 0
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's
number all the children from 1 to n. The i-th child wants to get at least ai candies.
Jzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line.
Then Jzzhu start distribution of the candies. He follows the algorithm:
Consider all the children in the order they go home. Jzzhu wants to know, which child will
be the last in this order?
Input
The first line contains two integers n, m (1 ≤ n ≤ 100; 1 ≤ m ≤ 100). The second line contains
n integers a1, a2, ..., an (1 ≤ ai ≤ 100).
Output
Examples
Input Output
5 2 4
1 3 1 4 2
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Input Output
6 4 6
1 1 2 2 3 3
Note
Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of
the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line).
Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end
of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home.
Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes
home.
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Implement a simple text editor. The editor initially contains an empty string, . Perform
operations of the following types:
Example
operation
index S ops[index] explanation
----- ------ ---------- -----------
0 abcde 1 fg append fg
1 abcdefg 3 6 print the 6th letter - f
2 abcdefg 2 5 delete the last 5 letters
3 ab 4 undo the last operation, index 2
4 abcdefg 3 7 print the 7th characgter - g
5 abcdefg 4 undo the last operation, index 0
6 abcde 3 4 print the 4th character - d
1 f
2 g
3 d
Input Format
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Constraints
Output Format
Input Output
STDIN Function c
----- -------- y
8 Q = 8 a
1 abc ops[0] = '1 abc'
3 3 ops[1] = '3 3'
2 3 ...
1 xy
3 2
4
4
3 1
Explanation
1. . We append to , so .
2. Print the character on a new line. Currently, the character is c .
3. Delete the last characters in ( ), so .
4. Append to , so .
5. Print the character on a new line. Currently, the character is y .
6. Undo the last update to , making empty again (i.e., ).
7. Undo the next to last update to (the deletion of the last characters), making
.
8. Print the character on a new line. Currently, the character is a .
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a
stairway, she starts counting steps from 1 to the number of steps in this stairway. She
speaks every number aloud. For example, if she climbs two stairways, the first of which
contains 3 steps, and the second contains 4 steps, she will pronounce the numbers
1, 2, 3, 1, 2, 3, 4.
You are given all the numbers pronounced by Tanya. How many stairways did she climb?
Also, output the number of steps in each stairway.
The given sequence will be a valid sequence that Tanya could have pronounced when
climbing one or more stairways.
Input
The first line contains n (1 ≤ n ≤ 1000) — the total number of numbers pronounced by
Tanya.
pronounced while climbing the stairs, in order from the first to the last pronounced
number. Passing a stairway with x steps, she will pronounce the numbers 1, 2, … , x in that
order.
The given sequence will be a valid sequence that Tanya could have pronounced when
climbing one or more stairways.
Output
In the first line, output t — the number of stairways that Tanya climbed. In the second line,
output t numbers — the number of steps in each stairway she climbed. Write the numbers
in the correct order of passage of the stairways.
Examples
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Input Output
7 2
1 2 3 1 2 3 4 3 4
Input Output
4 4
1 1 1 1 1 1 1 1
Input Output
5 1
1 2 3 4 5 5
Input Output
5 3
1 2 1 2 1 2 2 1
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Mad scientist Mike has just finished constructing a new device to search for extraterrestrial
intelligence! He was in such a hurry to launch it for the first time that he plugged in the
power wires without giving it a proper glance and started experimenting right away. After a
while Mike observed that the wires ended up entangled and now have to be untangled again.
The device is powered by two wires "plus" and "minus". The wires run along the floor from
the wall (on the left) to the device (on the right). Both the wall and the device have two
contacts in them on the same level, into which the wires are plugged in some order. The
wires are considered entangled if there are one or more places where one wire runs above
the other one. For example, the picture below has four such places (top view):
Mike knows the sequence in which the wires run above each other. Mike also noticed that on
the left side, the "plus" wire is always plugged into the top contact (as seen on the picture).
He would like to untangle the wires without unplugging them and without moving the
device. Determine if it is possible to do that. A wire can be freely moved and stretched on the
floor, but cannot be cut.
To understand the problem better please read the notes to the test samples.
Input
The single line of the input contains a sequence of characters "+" and "-" of length n
(1 ≤ n ≤ 100000). The i-th (1 ≤ i ≤ n) position of the sequence contains the character "+", if
on the i-th step from the wall the "plus" wire runs above the "minus" wire, and the
character "-" otherwise.
Output
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Print either "Yes" (without the quotes) if the wires can be untangled or "No" (without the
quotes) if the wires cannot be untangled.
Examples
Input Output
-++- Yes
Input Output
+- No
Input Output
++ Yes
Input Output
- No
Note
The first testcase corresponds to the picture in the statement. To untangle the wires, one
can first move the "plus" wire lower, thus eliminating the two crosses in the middle, and
then draw it under the "minus" wire, eliminating also the remaining two crosses.
In the second testcase the "plus" wire makes one full revolution around the "minus" wire.
Thus the wires cannot be untangled:
In the third testcase the "plus" wire simply runs above the "minus" wire twice in sequence.
The wires can be untangled by lifting "plus" and moving it higher:
In the fourth testcase the "minus" wire runs above the "plus" wire once. The wires cannot
be untangled without moving the device itself:
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
You have a deck of n cards, and you'd like to reorder it to a new one.
Each card has a value between 1 and n equal to pi . All pi are pairwise distinct. Cards in a deck
are numbered from bottom to top, i. e. p1 stands for the bottom card, pn is the top card.
In each step you pick some integer k > 0, take the top k cards from the original deck and
place them, in the order they are now, on top of the new deck. You perform this operation
until the original deck is empty. (Refer to the notes section for the better understanding.)
n
Let's define an order of a deck as ∑ n
n−i
⋅ pi .
i=1
Given the original deck, output the deck with maximum possible order you can make using
the operation above.
Input
The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases.
The first line of each test case contains the single integer n (1 ≤ n ≤ 105 ) — the size of
deck you have.
It's guaranteed that the sum of n over all test cases doesn't exceed 105 .
Output
For each test case print the deck with maximum possible order. Print values of cards in the
deck from bottom to top.
Examples
-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025
Input Output
4 4 3 2 1
4 5 2 4 3 1
1 2 3 4 6 1 5 3 4 2
5 1
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Note
In the first test case, one of the optimal strategies is the next one:
1. take 1 card from the top of p and move it to p′ : p becomes [1, 2, 3], p′ becomes [4];
2. take 1 card from the top of p: p becomes [1, 2], p′ becomes [4, 3];
3. take 1 card from the top of p: p becomes [1], p′ becomes [4, 3, 2];
4. take 1 card from the top of p: p becomes empty, p′ becomes [4, 3, 2, 1].
1. take 4 cards from the top of p and move it to p′ : p becomes [1], p′ becomes [5, 2, 4, 3];
2. take 1 card from the top of p and move it to p′ : p becomes empty, p′ becomes
[5, 2, 4, 3, 1];
1. take 2 cards from the top of p and move it to p′ : p becomes [4, 2, 5, 3], p′ becomes [6, 1];
2. take 2 cards from the top of p and move it to p′ : p becomes [4, 2], p′ becomes [6, 1, 5, 3];
3. take 2 cards from the top of p and move it to p′ : p becomes empty, p′ becomes
[6, 1, 5, 3, 4, 2].