0% found this document useful (0 votes)
5 views21 pages

Ilovepdf Merged

The document outlines a programming contest with multiple problems related to data structures and algorithms, including tasks like left rotation of arrays, stack operations, queue management, balanced brackets, and more. Each problem provides a description, input/output format, and examples to clarify the requirements. The problems are designed to test various programming skills and concepts, with constraints and expected outputs specified for each task.

Uploaded by

Elias Peso
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)
5 views21 pages

Ilovepdf Merged

The document outlines a programming contest with multiple problems related to data structures and algorithms, including tasks like left rotation of arrays, stack operations, queue management, balanced brackets, and more. Each problem provides a description, input/output format, and examples to clarify the requirements. The problems are designed to test various programming skills and concepts, with constraints and expected outputs specified for each task.

Uploaded by

Elias Peso
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/ 21

Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025

Problem A. Left Rotation


OS Linux

A operation on a circular array shifts each of the array's elements unit to


the left. The elements that fall off the left end reappear at the right end. Given an integer ,
rotate the array that many steps to the left and return the result.

Example

After rotations, .

Function Description

Complete the function with the following parameters:

: the amount to rotate by


: the array to rotate

Returns

: the rotated array

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

Problem B. Easy Stack


Time limit 500 ms
Mem limit 1572864 kB
Code length Limit 50000 B
OS Linux

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

First line contains an integer T (0 <= T <= 106).


Each of the next T lines contains a query based on these formats.

1 n : Push n (0 < n <= 109) to the top of the stack.


2 : Pop an element from the top of the stack. If the stack is empty, do nothing.
3 : Print the top element of the stack (see Output Format).

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!

Enormous input data!

-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025

Problem C. Easy Queue


Time limit 500 ms
Mem limit 1572864 kB
Code length Limit 50000 B
OS Linux

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

First line contains an integer T (0 <= T <= 106).

Each of the T next lines contains a query based on these formats.

1 n : Enqueue n (-109 <= n <= 109) to the queue.

2 : Dequeue an element from the queue. If the queue is empty, do nothing.

3 : Print the queue's first element's value (see Output).

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!

Enormous input data!

-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025

Problem D. Balanced Brackets


OS Linux

A bracket is considered to be any one of the following characters: ( , ) , { , } , [ , or ] .

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:

It contains no unmatched brackets.


The subset of brackets enclosed within the confines of a matched pair of brackets is
also a matched pair of brackets.

Given strings of brackets, determine whether each sequence of brackets is balanced. If a


string is balanced, return YES . Otherwise, return NO .

Function Description

Complete the function isBalanced in the editor below.

isBalanced has the following parameter(s):

string s: a string of brackets

Returns

string: either YES or NO

Input Format

The first line contains a single integer , the number of strings.


Each of the next lines contains a single string , a sequence of brackets.

Constraints

, where is the length of the sequence.

-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025

All chracters in the sequences ∈ { {, }, (, ), [, ] }.

Output Format

For each string, return YES or NO .

Input Output

STDIN Function YES


----- -------- NO
3 n = 3 YES
{[()]} first s = '{[()]}'
{[(])} second s = '{[(])}'
{{[[(())]]}} third s ='{{[[(())]]}}'

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

Problem E. Ada and Queue


Time limit 3500 ms
Mem limit 1572864 kB
Code length Limit 50000 B
OS Linux

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

back - Print number from back and then erase it

front - Print number from front and then erase it

reverse - Reverses all elements in queue

push_back N - Add element N to back

toFront N - Put element N to front

All numbers will be 0 ≤ N ≤ 100

Output

For each back/front query print appropriate number.

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.

Help Vitaly. Divide the given 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| ≤ 10


3) — 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.

-
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

Problem G. Jzzhu and Children


Time limit 1000 ms
Mem limit 262144 kB
Input file stdin
Output file stdout

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:

1. Give m candies to the first child of the line.


2. If this child still haven't got enough candies, then the child goes to the end of the line,
else the child go home.
3. Repeat the first two steps while the line is not empty.

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

Output a single integer, representing the number of the last child.

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

Let's consider the first sample.

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.

Child 4 is the last one who goes home.

-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025

Problem H. Simple Text Editor


OS Linux

Implement a simple text editor. The editor initially contains an empty string, . Perform
operations of the following types:

1. append - Append string to the end of .


2. delete - Delete the last characters of .
3. print - Print the character of .
4. undo - Undo the last (not previously undone) operation of type or , reverting to
the state it was in prior to that operation.

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

The results should be printed as:

1 f
2 g
3 d

Input Format

The first line contains an integer, , denoting the number of operations.


Each line of the subsequent lines (where ) defines an operation to be
performed. Each operation starts with a single integer, (where ), denoting
a type of operation as defined in the Problem Statement above. If the operation requires an

-
Contest 02 - Sheet 1 (STLs) easy one Apr 10, 2025

argument, is followed by its space-separated argument. For example, if and


, line will be 1 abcd .

Constraints

The sum of the lengths of all in the input .


The sum of over all delete operations .
All input characters are lowercase English letters.
It is guaranteed that the sequence of operations given as input is possible to perform.

Output Format

Each operation of type must print the character on a new line.

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

Initially, is empty. The following sequence of operations are described below:

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

Problem I. Tanya and Stairways


Time limit 1000 ms
Mem limit 262144 kB

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.

The second line contains integers a1 , a2 , … , an (1


​ ​ ​
≤ ai ≤ 1000) — all the numbers 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

Problem J. Alternating Current


Time limit 1000 ms
Mem limit 262144 kB
Input file stdin
Output file stdout

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

Problem K. Card Deck


Time limit 1000 ms
Mem limit 524288 kB

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.

The second line contains n integers p1 , p2 , … , pn (1


​ ​ ​ ≤ pi ≤ n; pi 
​ = pj if i =
​ ​  j ) — values of
card in the deck from bottom to top.

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.

If there are multiple answers, print any of them.

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].

In result, p′ has order equal to 43 ⋅ 4 + 42 ⋅ 3 + 41 ⋅ 2 + 40 ⋅ 1 = 256 + 48 + 8 + 1 = 313.

In the second test case, one of the optimal strategies is:

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];

In result, p′ has order equal to 54 ⋅ 5 + 53 ⋅ 2 + 52 ⋅ 4 + 51 ⋅ 3 + 50 ⋅ 1 = 3125 + 250 +


100 + 15 + 1 = 3491.

In the third test case, one of the optimal strategies is:

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].

In result, p′ has order equal to 65


⋅ 6 + 64 ⋅ 1 + 63 ⋅ 5 + 62 ⋅ 3 + 61 ⋅ 4 + 60 ⋅ 2 = 46656 +
1296 + 1080 + 108 + 24 + 2 = 49166.

You might also like