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

Contest#1

The document outlines a series of programming problems from a contest, each with specific input and output requirements. Problems include calculating a mathematical function, managing police recruits in response to crimes, determining gift exchanges among friends, and manipulating strings to check for palindromes. Each problem is accompanied by examples and a link to the full problem set.
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)
20 views21 pages

Contest#1

The document outlines a series of programming problems from a contest, each with specific input and output requirements. Problems include calculating a mathematical function, managing police recruits in response to crimes, determining gift exchanges among friends, and manipulating strings to check for palindromes. Each problem is accompanied by examples and a link to the full problem set.
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 #1

Problem A: Calculating Function


Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes

For a positive integer 𝑛 let's define a function 𝑓:


𝑓(𝑛) = − 1 + 2 − 3 + . . + (−1)𝑛 𝑛
Your task is to calculate 𝑓(𝑛) for a given integer 𝑛.

Input
The single line contains the positive integer 𝑛 (1 ≤ 𝑛 ≤ 1015 ).

Output
Print 𝑓(𝑛) in a single line.

Standard Input Standard Output


4 2
5 −3

Note
𝑓(4) = − 1 + 2 − 3 + 4 = 2
𝑓(5) = − 1 + 2 − 3 + 4 − 5 = − 3

Problem link: https://codeforces.com/problemset/problem/486/A

2
Problem B: Police Recruits
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes

The police department of your city has just started its journey. Initially, they
don’t have any manpower. So, they started hiring new recruits in groups.
Meanwhile, crimes keeps occurring within the city. One member of the police
force can investigate only one crime during his/her lifetime.
If there is no police officer free (isn't busy with crime) during the occurrence
of a crime, it will go untreated.
Given the chronological order of crime occurrences and recruit hirings, find
the number of crimes which will go untreated.

Input
The first line of input will contain an integer 𝑛 (1 ≤ 𝑛 ≤ 105 ), the number of
events. The next line will contain 𝑛 space-separated integers.
If the integer is −1 then it means a crime has occurred. Otherwise, the integer
will be positive, the number of officers recruited together at that time. No
more than 10 officers will be recruited at a time.

Output
Print a single integer, the number of crimes which will go untreated.

3
Standard Input Standard Output
3 2
−1 − 1 1
8 1
1 −11 −1 −1111
11 8
−1 − 1 2 − 1 − 1 − 1 − 1 − 1 − 1 − 1 − 1

Note
Lets consider the second example:
1. Firstly one person is hired.
2. Then crime appears, the last hired person will investigate this crime.
3. One more person is hired.
4. One more crime appears, the last hired person will investigate this
crime.
5. Crime appears. There is no free policeman at the time, so this crime
will go untreated.
6. One more person is hired.
7. One more person is hired.
8. One more person is hired.
The answer is one, as one crime (on step 5) will go untreated.

Problem link: https://codeforces.com/problemset/problem/427/A

4
Problem C: Presents
Input file: standard input Time limit: 2 seconds
Output file: standard output Memory limit: 256 megabytes

Little Petya very much likes gifts. Recently he has received a new laptop as
a New Year gift from his mother. He immediately decided to give it to
somebody else as what can be more pleasant than giving somebody gifts.
And on this occasion he organized a New Year party at his place and
invited 𝑛 his friends there.
If there's one thing Petya likes more that receiving gifts, that's watching
others giving gifts to somebody else. Thus, he safely hid the laptop until the
next New Year and made up his mind to watch his friends exchanging gifts
while he does not participate in the process. He numbered all his friends with
integers from 1 to 𝑛. Petya remembered that a friend number 𝑖 gave a gift to
a friend number 𝑝𝑖 . He also remembered that each of his friends received
exactly one gift.
Now Petya wants to know for each friend 𝑖 the number of a friend who has
given him a gift.

Input
The first line contains one integer 𝑛 (1 ≤ 𝑛 ≤ 100) — the quantity of friends
Petya invited to the party.
The second line contains 𝑛 space-separated integers: the 𝑖-th number
is 𝑝𝑖 — the number of a friend who gave a gift to friend number 𝑖. It is
guaranteed that each friend received exactly one gift. It is possible that some
friends do not share Petya's ideas of giving gifts to somebody else. Those
friends gave the gifts to themselves.

5
Output
Print 𝑛 space-separated integers: the 𝑖-th number should equal the number
of the friend who gave a gift to friend number 𝑖.

Standard Input Standard Output


4 4123
2341
3 132
132
2 12
12

Problem link: https://codeforces.com/problemset/problem/136/A

6
Problem D: New Palindrome
Input file: standard input Time limit: 2 seconds
Output file: standard output Memory limit: 256 megabytes

A palindrome is a string that reads the same from left to right as from right to
left. For example, abacaba, aaaa, abba, racecar are palindromes.
You are given a string 𝑠 consisting of lowercase Latin letters. The string 𝑠 is
a palindrome.
You have to check whether it is possible to rearrange the letters in it to
get another palindrome (not equal to the given string 𝑠).

Input
The first line contains a single integer 𝑡 (1 ≤ 𝑡 ≤ 1000) — the number of test
cases.
The only line of each test case contains a string 𝑠 (2 ≤ |𝑠| ≤ 50) consisting
of lowercase Latin letters. This string is a palindrome.

Output
For each test case, print YES if it is possible to rearrange the letters in the
given string to get another palindrome. Otherwise, print NO.

You may print each letter in any case (YES, yes, Yes will all be recognized
as positive answer, NO, no and nO will all be recognized as negative
answer).

7
Standard Input Standard Output
3 YES
codedoc NO
gg NO
aabaa

Note
In the first test case, it is possible to rearrange the letters in the
palindrome codedoc to obtain the string ocdedco, which is different from
the given string, but also a palindrome.

Problem link: https://codeforces.com/problemset/problem/1832/A

8
Problem E: Night at the Museum
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes

Grigoriy, like the hero of one famous comedy film, found a job as a night
security guard at the museum. At first night he received embosser and was
to take stock of the whole exposition.
Embosser is a special devise that allows to "print" the text of a plastic tape.
Text is printed sequentially, character by character. The device consists of a
wheel with a lowercase English letters written in a circle, static pointer to the
current letter and a button that print the chosen letter. At one move it's
allowed to rotate the alphabetic wheel one step clockwise or
counterclockwise. Initially, static pointer points to letter 'a'. Other letters are
located as shown on the picture:

After Grigoriy add new item to the base he has to print its name on the plastic
tape and attach it to the corresponding exhibit. It's not required to return the
wheel to its initial position with pointer on the letter 'a'.
Our hero is afraid that some exhibits may become alive and start to attack
him, so he wants to print the names as fast as possible. Help him, for the

9
given string find the minimum number of rotations of the wheel required to
print it.

Input
The only line of input contains the name of some exhibit — the non-empty
string consisting of no more than 100 characters. It's guaranteed that the
string consists of only lowercase English letters.

Output
Print one integer — the minimum number of rotations of the wheel, required
to print the name given in the input.

Standard Input Standard Output


zeus 18
map 35
ares 34

Note

To print the string from the first sample it would be optimal to perform the
following sequence of rotations:
1. from 'a' to 'z' (1 rotation counterclockwise),
2. from 'z' to 'e' (5 clockwise rotations),
3. from 'e' to 'u' (10 rotations counterclockwise),
10
4. from 'u' to 's' (2 counterclockwise rotations).
In total, 1 + 5 + 10 + 2 = 18 rotations are required.

Problem link: https://codeforces.com/problemset/problem/731/A

11
Problem F: Lineland Mail
Input file: standard input Time limit: 3 seconds
Output file: standard output Memory limit: 256 megabytes

All cities of Lineland are located on the 𝑂𝑥 coordinate axis. Thus, each city is
associated with its position 𝑥𝑖 — a coordinate on the 𝑂𝑥 axis. No two cities
are located at a single point.
Lineland residents love to send letters to each other. A person may send a
letter only if the recipient lives in another city (because if they live in the same
city, then it is easier to drop in).
Strange but true, the cost of sending the letter is exactly equal to the distance
between the sender's city and the recipient's city.
For each city calculate two values 𝑚𝑖𝑛𝑖 and 𝑚𝑎𝑥𝑖 , where 𝑚𝑖𝑛𝑖 is the
minimum cost of sending a letter from the 𝑖-th city to some other city,
and 𝑚𝑎𝑥𝑖 is the the maximum cost of sending a letter from the 𝑖-th city to
some other city

Input
The first line of the input contains integer 𝑛 (2 ≤ 𝑛 ≤ 105 ) — the number of
cities in Lineland. The second line contains the sequence of 𝑛 distinct
integers 𝑥1 , 𝑥2 , . . . , 𝑥𝑛 ( − 109 ≤ 𝑥𝑖 ≤ 109 ), where 𝑥𝑖 is the 𝑥-coordinate of
the 𝑖-th city. All the 𝑥𝑖 's are distinct and follow in ascending order.

Output
Print 𝑛 lines, the 𝑖-th line must contain two integers 𝑚𝑖𝑛𝑖 , 𝑚𝑎𝑥𝑖 , separated
by a space, where 𝑚𝑖𝑛𝑖 is the minimum cost of sending a letter from the 𝑖-th
city, and 𝑚𝑎𝑥𝑖 is the maximum cost of sending a letter from the 𝑖-th city.

12
Standard Input Standard Output
4 3 12
−5 − 2 2 7 39
47
5 12
2 22
−1 1 22

Problem link: https://codeforces.com/problemset/problem/567/A

13
Problem G: Mashmokh and Lights
Input file: standard input Time limit: 1 second
Output file: standard output Memory limit: 256 megabytes

Mashmokh works in a factory. At the end of each day he must turn off all of
the lights.
The lights on the factory are indexed from 1 to 𝑛. There are 𝑛 buttons in
Mashmokh's room indexed from 1 to 𝑛 as well. If Mashmokh pushes button
with index 𝑖, then each light with index not less than 𝑖 that is still turned on
turns off.
Mashmokh is not very clever. So instead of pushing the first button he pushes
some of the buttons randomly each night. He pushed 𝑚 distinct buttons 𝑏1 ,
𝑏2 , ..., 𝑏𝑚 (the buttons were pushed consecutively in the given order) this
night. Now he wants to know for each light the index of the button that turned
this light off. Please note that the index of button 𝑏𝑖 is actually 𝑏𝑖 , not 𝑖.
Please, help Mashmokh, print these indices.

Input
The first line of the input contains two space-separated
integers 𝑛 and 𝑚 (1 ≤ 𝑛, 𝑚 ≤ 100), the number of the factory lights and the
pushed buttons respectively. The next line contains 𝑚 distinct space-
separated integers 𝑏1 , 𝑏2 , ..., 𝑏𝑚 (1 ≤ 𝑏𝑖 ≤ 𝑛).
It is guaranteed that all lights will be turned off after pushing all buttons.

Output
Output 𝑛 space-separated integers where the 𝑖-th number is index of the
button that turns the 𝑖-th light off.

14
Standard Input Standard Output
54 11344
4312
55 12345
54321

Note
In the first sample, after pressing button number 4, lights 4 and 5 are turned
off and lights 1, 2 and 3 are still on. Then after pressing button number 3,
light number 3 is turned off as well. Pressing button number 1 turns off lights
number 1 and 2 as well so pressing button number 2 in the end has no effect.
Thus button number 4 turned lights 4 and 5 off, button number 3 turned light
3 off and button number 1 turned light 1 and 2 off.

Problem link: https://codeforces.com/problemset/problem/415/A

15
Problem H: Last Year's Substring
Input file: standard input Time limit: 2 seconds
Output file: standard output Memory limit: 256 megabytes

Polycarp has a string 𝑠[1 … 𝑛] of length 𝑛 consisting of decimal digits.


Polycarp performs the following operation with the string 𝑠 no more than
once (i.e. he can perform operation 0 or 1 time):
• Polycarp selects two numbers 𝑖 and 𝑗 (1 ≤ 𝑖 ≤ 𝑗 ≤ 𝑛) and removes
characters from the 𝑠 string at the positions 𝑖, 𝑖 + 1, 𝑖 + 2, … , 𝑗 (i.e.
removes substring 𝑠[𝑖 … 𝑗]). More formally, Polycarp turns the
string 𝑠 into the string 𝑠1 𝑠2 … 𝑠𝑖−1 𝑠𝑗+1 𝑠𝑗+2 … 𝑠𝑛 .

For example, the string 𝑠 = "20192020" Polycarp can turn into strings:

• "2020" (in this case (𝑖, 𝑗) = (3,6) or (𝑖, 𝑗) = (1,4));

• "2019220" (in this case (𝑖, 𝑗) = (6,6));

• "020" (in this case (𝑖, 𝑗) = (1,5));


• other operations are also possible, only a few of them are listed above.
Polycarp likes the string "2020" very much, so he is wondering if it is
possible to turn the string ss into a string "2020" in no more than one
operation? Note that you can perform zero operations.

Input
The first line contains a positive integer 𝑡 (1 ≤ 𝑡 ≤ 1000) — number of test
cases in the test. Then 𝑡 test cases follow.
The first line of each test case contains an integer 𝑛 (4 ≤ 𝑛 ≤ 200) — length
of the string 𝑠. The next line contains a string 𝑠 of length 𝑛 consisting of
decimal digits. It is allowed that the string 𝑠 starts with digit 0.

16
Output
For each test case, output on a separate line:
• "YES" if Polycarp can turn the string 𝑠 into a string "2020" in no more
than one operation (i.e. he can perform 0 or 1 operation);
• "NO" otherwise.
You may print every letter of "YES" and "NO" in any case you want (so, for
example, the strings yEs, yes, Yes and YES will all be recognized as
positive answer).

Standard Input Standard Output


6 YES
8 YES
20192020 YES
8 NO
22019020 NO
4 NO
2020
5
20002
6
729040
6
200200

Note
In the first test case, Polycarp could choose 𝑖 = 3 and 𝑗 = 6.
In the second test case, Polycarp could choose 𝑖 = 2 and 𝑗 = 5.
In the third test case, Polycarp did not perform any operations with the string.

Problem link: https://codeforces.com/problemset/problem/1462/B

17
Problem I: Keyboard
Input file: standard input Time limit: 2 seconds
Output file: standard output Memory limit: 256 megabytes

Our good friend Mole is trying to code a big message. He is typing on an


unusual keyboard with characters arranged in following way:
qwertyuiop
asdfghjkl;
zxcvbnm,./
Unfortunately Mole is blind, so sometimes it is problem for him to put his
hands accurately. He accidentally moved both his hands with one position to
the left or to the right. That means that now he presses not a button he wants,
but one neighboring button (left or right, as specified in input).
We have a sequence of characters he has typed and we want to find the
original message.

Input
First line of the input contains one letter describing direction of shifting
('L' or 'R' respectively for left or right).
Second line contains a sequence of characters written by Mole. The size of
this sequence will be no more than 100. Sequence contains only symbols
that appear on Mole's keyboard. It doesn't contain spaces as there is no
space on Mole's keyboard.
It is guaranteed that even though Mole hands are moved, he is still pressing
buttons on keyboard and not hitting outside it.

Output
Print a line that contains the original message.

18
Standard Input Standard Output
R allyouneedislove
s;;upimrrfod;pbr

Problem link: https://codeforces.com/problemset/problem/474/A

19
Problem J: IQ Test
Input file: standard input Time limit: 2 seconds
Output file: standard output Memory limit: 256 megabytes

In the city of Ultima Thule job applicants are often offered an IQ test.
The test is as follows: the person gets a piece of squared paper with
a 4 × 4 square painted on it. Some of the square's cells are painted black
and others are painted white. Your task is to repaint at most one cell the
other color so that the picture has a 2 × 2 square, completely consisting of
cells of the same color. If the initial picture already has such a square, the
person should just say so and the test will be completed.
Your task is to write a program that determines whether it is possible to pass
the test. You cannot pass the test if either repainting any cell or no action
doesn't result in a 2 × 2 square, consisting of cells of the same color.

Input
Four lines contain four characters each: the 𝑗-th character of the 𝑖-th line
equals "." if the cell in the 𝑖-th row and the 𝑗-th column of the square is
painted white, and "#", if the cell is black.

Output
Print "YES" (without the quotes), if the test can be passed and "NO" (without
the quotes) otherwise.

20
Standard Input Standard Output
#### YES
.#..
####
....
#### NO
....
####
....

Note
In the first test sample it is enough to repaint the first cell in the second row.
After such repainting the required 2 × 2 square is on the intersection of
the 1-st and 2-nd row with the 1-st and 2-nd column.

Problem link: https://codeforces.com/problemset/problem/287/A

21

You might also like