Problem A. Berstagram: Input
Problem A. Berstagram: Input
Problem A. Berstagram
Polycarp recently signed up to a new social network Berstagram. He immediately published n posts there.
He assigned numbers from 1 to n to all posts and published them one by one. So, just after publishing
Polycarp’s news feed contained posts from 1 to n — the highest post had number 1, the next one had
number 2, . . . , the lowest post had number n.
After that he wrote down all likes from his friends. Likes were coming consecutively from the 1-st one till
the m-th one. You are given a sequence a1 , a2 , . . . , am (1 ≤ aj ≤ n), where aj is the post that received
the j-th like.
News feed in Berstagram works in the following manner. Let’s assume the j-th like was given to post
aj . If this post is not the highest (first) one then it changes its position with the one above. If aj is the
highest post nothing changes.
For example, if n = 3, m = 5 and a = [3, 2, 1, 3, 3], then Polycarp’s news feed had the following states:
• before the first like: [1, 2, 3];
• after the first like: [1, 3, 2];
• after the second like: [1, 2, 3];
• after the third like: [1, 2, 3];
• after the fourth like: [1, 3, 2];
• after the fifth like: [3, 1, 2].
Polycarp wants to know the highest (minimum) and the lowest (maximum) positions for each post.
Polycarp considers all moments of time, including the moment “before all likes”.
Input
The first line contains two integer numbers n and m (1 ≤ n ≤ 105 , 1 ≤ m ≤ 4 · 105 ) — number of posts
and number of likes.
The second line contains integers a1 , a2 , . . . , am (1 ≤ aj ≤ n), where aj is the post that received the j-th
like.
Output
Print n pairs of integer numbers. The i-th line should contain the highest (minimum) and the lowest
(maximum) positions of the i-th post. You should take into account positions at all moments of time:
before all likes, after each like and after all likes. Positions are numbered from 1 (highest) to n (lowest).
Examples
standard input standard output
3 5 1 2
3 2 1 3 3 2 3
1 3
10 6 1 2
7 3 5 7 3 6 2 3
1 3
4 7
4 5
6 7
5 7
8 8
9 9
10 10
Page 1 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line contains two integers n and k (1 ≤ n ≤ 5 · 105 , 1 ≤ k ≤ 8000) — the number of employees
and the number of teams in JebTrains. The second line contains a sequence of integers t1 , t2 , . . . , tn , where
ti (1 ≤ ti ≤ k) is the i-th employee’s team number. Every team contains at least one employee.
Output
Print the minimum cost of the rent.
Examples
standard input standard output
6 3 6
3 1 2 3 2 3
10 1 10
1 1 1 1 1 1 1 1 1 1
12 4 12
1 2 3 1 2 3 4 1 2 1 2 1
Page 2 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line contains two integers n and k (1 ≤ n ≤ 2 · 105 , 1 ≤ k ≤ 1012 ) — the number of projects and
the amount of money you have to spend during each day in Saint Petersburg, respectively.
Then n lines follow, each containing three integers li , ri , pi (1 ≤ li ≤ ri ≤ 2 · 105 , 1 ≤ pi ≤ 1012 ) — the
starting day of the i-th project, the ending day of the i-th project, and the amount of money you get paid
if you choose to participate in it, respectively.
Output
If it is impossible to plan a trip with strictly positive profit, print the only integer 0.
Otherwise, print two lines. The first line should contain four integers p, L, R and m — the maximum profit
you can get, the starting day of your trip, the ending day of your trip and the number of projects you
choose to complete, respectively. The second line should contain m distinct integers s1 , s2 , . . . , sm — the
projects you choose to complete, listed in arbitrary order. If there are multiple answers with maximum
profit, print any of them.
Examples
standard input standard output
4 5 13 3 5 2
1 1 3 3 2
3 3 11
5 5 17
7 7 4
1 3 0
1 2 5
4 8 22 1 5 4
1 5 16 3 2 1 4
2 4 9
3 3 24
1 5 13
Page 3 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line of the input contains integer t (1 ≤ t ≤ 100) — number of test cases. Then the test cases
follow.
The first line of each test case contains integer n (1 ≤ n ≤ 500) — the number of registered conference
participants.
Next n lines follow, each containing three integers li , ri , ci (1 ≤ li ≤ ri ≤ 106 , 0 ≤ ci ≤ 200) — the
day of arrival, the day of departure and the country (or the fact that it was not indicated) for the i-th
participant.
The sum of n among all test cases in the input does not exceed 500.
Output
Output t integers — maximum number of upset scientists for each test case.
Example
standard input standard output
2 4
4 2
1 10 30
5 6 30
6 12 0
1 1 0
4
1 2 1
2 3 0
3 4 0
4 5 2
Page 4 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line contains one integer t (1 ≤ t ≤ 50) — the number of test cases. Then the test cases follow.
Each test case begins with a line containing three integers n, m and k (2 ≤ n ≤ 50, 1 ≤ k ≤ m ≤ 50)
— the number of necklaces, the number of gems in each necklace, and the minimum number of positions
where two necklaces have to have the same type of gems in order to look similar, respectively.
Then n lines follow, the i-th of them contains a binary string si of length m representing the i-th necklace.
Output
For each test case, print the answer as follows.
If it is impossible to avoid the conflict, print -1 on a single line. In this case you should not output
anything else for that test case.
Otherwise, the first line of the test case answer should contain the single integer d — the minimum number
of necklaces that are to be worn backward. The second line of the test case answer should contain the
numbers of these necklaces (integers from 1 to n) in any order. If d = 0 then leave the second line of the
test case answer empty. If there are multiple answers, you may print any of them.
Page 5 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Example
standard input standard output
5 2
5 7 2 1 3
1010100 1
0010101 3
1111010 0
1000010
0000101 -1
6 9 3 1
011111110 1
100111000
111100000
000111111
110100111
111110111
3 4 2
0001
1000
0000
3 4 4
0001
1000
0000
2 4 3
0001
1000
Page 6 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first and only line of the input contains an integer n (1 ≤ n ≤ 105 ), where n is the area of the data
center in square meters.
Output
Print the required minimum perimeter in meters.
Examples
standard input standard output
36 24
13 28
1 4
Note
In the first example, the required shape of the data center is 6 × 6 square. Its area is 36 and the perimeter
is 6 + 6 + 6 + 6 = 24.
In the second example, the required shape of the data center is 1 × 13 rectangle. Its area is 13 and the
perimeter is 1 + 13 + 1 + 13 = 28.
In the third example, the required shape of the data center is 1 × 1 square. Its area is 1 and the perimeter
is 1 + 1 + 1 + 1 = 4.
Page 7 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line of the input contains one integer t (1 ≤ t ≤ 10000) — the number of test cases. Then the
test cases follow.
The first line of each test case contains two integers n and k (1 ≤ n ≤ 2 · 105 , 2 ≤ k ≤ 109 ) — the
maximum possible number of rounds in the game and the number of points, after reaching which a player
loses, respectively.
The second line of each test case contains n integers a1 , a2 , . . . , an (1 ≤ aj < k), where aj is the amount
of points the human gains in the j-th round.
The third line of each test case contains n integers b1 , b2 , . . . , bn (1 ≤ bj < k), where bj is the amount of
points the computer gains in the j-th round.
The sum of n over all test cases in the input does not exceed 2 · 105 .
Output
Print the answers for all test cases in the order they appear in the input.
If Polycarpus cannot win the game, then simply print one line “-1” (without quotes). In this case, you
should not output anything else for that test case. Otherwise, the first line of the test case answer should
contain one integer d — the minimum possible number of “Reset” button pushes, required to win the game.
The next line should contain d distinct integers r1 , r2 , . . . , rd (1 ≤ ri < n) — the numbers of rounds, at
the end of which Polycarpus has to press the “Reset” button, in arbitrary order. If d = 0 then either leave
the second line of the test case answer empty, or do not print the second line at all.
If there are several possible solutions, print any of them.
Page 8 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Example
standard input standard output
3 0
4 17
1 3 5 7 2
3 5 7 9 2 4
11 17 -1
5 2 8 2 4 6 1 2 7 2 5
4 6 3 3 5 1 7 4 2 5 3
6 17
6 1 2 7 2 5
1 7 4 2 5 3
Note
In the second test case, if the human pushes the “Reset” button after the second and the fourth rounds,
the game goes as follows:
1. after the first round the human has 5 points, the computer — 4 points;
2. after the second round the human has 7 points, the computer — 10 points;
3. the human pushes the “Reset” button and now he has 0 points and the computer — 3 points;
4. after the third round the human has 8 points, the computer — 6 points;
5. after the fourth round the human has 10 points, the computer — 9 points;
6. the human pushes “Reset” button again, after it he has 1 point, the computer — 0 points;
7. after the fifth round the human has 5 points, the computer — 5 points;
8. after the sixth round the human has 11 points, the computer — 6 points;
9. after the seventh round the human has 12 points, the computer — 13 points;
10. after the eighth round the human has 14 points, the computer — 17 points;
11. the human wins, as the computer has k or more points and the human — strictly less than k points.
Page 9 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line contains an integer t (1 ≤ t ≤ 104 ) — the number of test cases in the input.
The only line of each test case contains ten integer numbers c0 , c1 , . . . , c9 (0 ≤ ci ≤ 105 ) — the number of
0-candles, 1-candles, 2-candles and so on.
It is guaranteed that the sum of all ci in the input does not exceed 106 .
Output
For each test case, output one integer in single line — the minimum age which cannot be composed by
candles from your set. Please note that the age can be quite large (it may exceed the standard 64-bit
integer types in your programming language).
Example
standard input standard output
4 11
1 1 1 1 1 1 1 1 1 1 1
0 0 1 1 2 2 3 3 4 4 7
1 2 1 2 1 3 1 0 0 0 10
0 1 2 1 4 3 1 1 2 1
Page 10 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line contains one integer t (1 ≤ t ≤ 105 ) — the number of test cases in the input. Then the test
cases follow.
Each test case begins with a line containing three integers n, k and m (1 ≤ n ≤ 106 , 1 ≤ k ≤ 1018 ,
1 ≤ m ≤ 106 ) — the total number of dancers, the maximum acceptable awkwardness of a set of dancers
and the maximum number of concerts, respectively.
The following line contains n integers a1 , a2 , . . . , an (1 ≤ ai ≤ 1012 ), where ai is the awkwardness of the
i-th dancer.
The sum of the values of n over all test cases in the input does not exceed 106 . Similarly, the sum of the
values of m over all test cases in the input does not exceed 106 .
Output
Print the answers to all test cases in the input.
If the troupe cannot give concerts at all, then simply print one line “0”. In this case, you should not print
anything else.
If the troupe gives a positive number of concerts r (r is equal to the minimum of m and the total number
of valid sets), then first print the value of r, then r lines: the j-th line should contain two integers sj and
tj — the number of dancers in the j-th concert and the total awkwardness of the dancers performing in
the j-th concert. Complete the output to a test case with a line that describes the last set: print exactly
sr distinct integers from 1 to n — the numbers of the dancers who will perform at the r-th (last) concert,
in any order. If there are several answers, print any of them.
Page 11 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Example
standard input standard output
3 10
7 13 10 5 12
3 1 5 1 8 2 13 4 7
2 10 1 4 9
12 12 4 10
3 32 100000 4 11
2 1 5 4 11
4 12
4 13
3 4
3 5
2 4 1
0
7
3 8
2 3
2 6
2 7
1 1
1 2
1 5
3
Page 12 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line contains one integer t (1 ≤ t ≤ 10000) — the number of test cases. Then the test cases
follow.
Each test case begins with a line containing two integers n and k (1 ≤ n ≤ 30000, 1 ≤ k ≤ 1012 ) — the
number of different heights of soldiers and the number of rows of soldiers in the parade, respectively.
The second (and final) line of each test case contains n integers c1 , c2 , . . . , cn (0 ≤ ci ≤ 1012 ), where ci
is the number of soldiers having height i in the Berland Army.
It is guaranteed that the sum of n over all test cases does not exceed 30000.
Output
For each test case, print one integer — the maximum number of soldiers that can participate in the parade.
Example
standard input standard output
5 16
3 4 100
7 1 13 99
1 1 2000000000000
100 13
1 3
100
2 1
1000000000000 1000000000000
4 1
10 2 11 1
Note
Explanations for the example test cases:
1. the heights of soldiers in the rows can be: [3, 3, 3, 3], [1, 2, 1, 1], [1, 1, 1, 1], [3, 3, 3, 3] (each list
represents a row);
2. all soldiers can march in the same row;
3. 33 soldiers with height 1 in each of 3 rows;
4. all soldiers can march in the same row;
5. all soldiers with height 2 and 3 can march in the same row.
Page 13 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Problem K. Projectors
There are n lectures and m seminars to be conducted today at the Faculty of Approximate Sciences. The
i-th lecture starts at ai and ends at bi (formally, time of the lecture spans an interval [ai , bi ), the right
bound is exclusive). The j-th seminar starts at pj and ends at qj (similarly, time of the seminar spans an
interval [pj , qj ), the right bound is exclusive).
There are x HD-projectors numbered from 1 to x and y ordinary projectors numbered from x + 1 to x + y
available at the faculty. Projectors should be distributed in such a way that:
• an HD-projector is used in each lecture;
• some projector (ordinary or HD) is used in each seminar;
• a projector (ordinary or HD) can only be used in one event at the same moment of time;
• if a projector is selected for an event, it is used there for the whole duration of the event;
• a projector can be reused in some following event, if it starts not earlier than current event finishes.
You are to find such distribution of projectors, if it exists.
Again, note that the right bound of the event’s time range is not inclusive: if some event starts exactly
when another event finishes, the projector can be reused (suppose that it is instantly transported to the
location of the event).
Input
The first line contains an integer t (1 ≤ t ≤ 300) — the number of test cases.
Each test case starts with a line containing four integers n, m, x, y (0 ≤ n, m, x, y ≤ 300; n + m > 0,
x + y > 0) — the number of lectures, the number of seminars, the number of HD projectors and the
number of ordinary projectors, respectively.
The next n lines describe lectures. Each line contains two integers ai , bi (1 ≤ ai < bi ≤ 106 ) — the start
time (inclusive) and finish time (exclusive) of the i-th lecture.
The next m lines describe seminars. Each line contains two integers pj , qj (1 ≤ pj < qj ≤ 106 ) — the start
time (inclusive) and finish time (exclusive) of the j-th seminar.
Output
For each test case, print YES if it is possible to distribute projectors in order to meet all requirements, or
NO otherwise.
In case of positive answer, output one additional line containing n+m integers. The first n integers should
be not less than 1 and not greater than x, and the i-th of them should be the index of HD projector used
in the i-th lecture. The last m integers should be not less than 1 and not greater than x + y, and the j-th
of them should be the index of projector used in the j-th seminar. If there are multiple answers, print any
of them.
Page 14 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Examples
standard input standard output
2 YES
2 2 2 2 2 1 4 3
1 5 YES
2 5 2 1
1 5
1 4
2 0 2 10
1 3
1 3
3 YES
1 2 1 1 1 2 1
3 4 NO
2 4 YES
1 3 1
3 4 2 3
5 7
1 3
1 7
4 8
2 5
1 6
2 8
0 1 1 0
1 1000000
Page 15 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Input
The first line contains one integer t (1 ≤ t ≤ 5) — the number of test cases in the input. Then test cases
follow.
Each test case consists of one line containing three integers a, b and c (1 ≤ a, b, c ≤ 1000) — the number
of Assembler fans, Basic fans and C++ fans, respectively.
Output
For each test case print one integer — the minimum size of the largest subgroup if the students are divided
in such a way that there is no subgroup that contains at least one Assembler fan and at least one C++
fan simultaneously.
Examples
standard input standard output
5 5
3 5 7 6
4 8 4 13
13 10 13 1000
1000 1000 1000 14
13 22 7
5 3
1 3 4 667
1000 1000 1 3
4 1 2 517
325 226 999 769
939 861 505
Note
Explanation of the answers for the example 1:
1. The first subgroup contains 3 Assembler fans and 2 Basic fans, the second subgroup — 5 C++ fans,
the third subgroup — 2 C++ fans and 3 Basic fans.
2. The first subgroup contains 4 Assembler fans, the second subgroup — 6 Basic fans, the third subgroup
— 2 Basic fans and 4 C++ fans.
Page 16 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
3. The first subgroup contains all Assembler fans, the second subgroup — all Basic fans, the third
subgroup — all C++ fans.
4. The first subgroup contains all Assembler fans, the second subgroup — all Basic fans, the third
subgroup — all C++ fans.
5. The first subgroup contains 12 Assembler fans and 2 Basic fans, the second subgroup — 1 Assembler
fan and 13 Basic fans, the third subgroup — 7 Basic fans and 7 C++ fans.
Page 17 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Problem M. SmartGarden
Berland Gardeners United Inc. hired you for the project called “SmartGarden”. The main feature of this
project is automatic garden watering.
Formally the garden can be represented as a square of n × n cells with rows numbered 1 to n from top to
bottom and columns numbered 1 to n from left to right. Each cell of the garden contains either a plant
or a slab.
It’s known that slabs are located on the main diagonal of the matrix representing the garden, and in the
cells that are below the main diagonal and share a side with at least one cell of the main diagonal. All
the remaining cells of the garden are filled with plants.
During implementation of the project you created a smart robot that takes a list of commands as an
input, which are processed one by one. Each command contains:
• a list of horizontal lines (rows in the matrix representing the garden);
• a list of vertical lines (columns in the matrix representing the garden).
While executing each command robot waters only cells in the intersection of specified rows and specified
columns. So, if you specify r rows and c columns, then exactly r · c cells will be watered.
In the demo for the customer you have tuned robot in such a way that it waters all the garden. To do
that you prepared a single command containing all n rows and all n columns.
Unfortunately, 5 hours before the demo for your customer it turned out that the CEO of Berland Gardeners
United Inc. was going to take part in it. Moreover, most probably he will be standing on a garden slab
during the demo!
Now you need to create a list of commands for the robot so that it waters all the plants and doesn’t water
any cell containing a slab. Since it’s only a beta version of “SmartGarden”, the total number of commands
shouldn’t exceed 50.
Create a program that, for a given size of the garden, will find a list of no more than 50 commands that
allow the robot to water all the plants in the garden without watering the slabs. It is allowed to water a
plant several times.
Input
The first and the only line of the input contains a single integer n (2 ≤ n ≤ 5000), where n is the size of
the garden.
Output
In the first line print the total number of commands for the robot k (1 ≤ k ≤ 50). In the next 2 · k lines
Page 18 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
print all the commands. Each command should be specified by 2 lines. The first line of each command
should describe rows in the command and the second line should describe columns in the command. Each
of these 2 lines should have the following format:
• the first number of the line should specify the total number of items x in the appropriate list;
• then x distinct numbers follow, where each number is in the range 1 . . . n and describes a chosen
row for the first line and a chosen column for the second line.
If there are multiple ways to water the garden, print any of them.
Examples
standard input standard output
2 2
1 1
1 2
1 1
1 2
4 4
2 1 4
1 2
2 1 2
2 3 4
1 3
2 1 4
1 4
1 1
Page 19 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Problem N. Wires
Polycarpus has a complex electronic device. The core of this device is a circuit board. The board has 109
contact points which are numbered from 1 to 109 . Also there are n wires numbered from 1 to n, each
connecting two distinct contact points on the board. An electric signal can pass between wires A and B
if:
• either both wires share the same contact point;
• or there is a sequence of wires starting with A and ending with B, and each pair of adjacent wires
in the sequence share a contact point.
The picture shows a circuit board with 5 wires. Contact points with numbers 2, 5, 7, 8, 10, 13 are used. Here an
electrical signal can pass from wire 2 to wire 3, but not to wire 1.
Currently the circuit board is broken. Polycarpus thinks that the board could be fixed if the wires were
re-soldered so that a signal could pass between any pair of wires.
It takes 1 minute for Polycarpus to re-solder an end of a wire. I.e. it takes one minute to change one of the
two contact points for a wire. Any contact point from range [1, 109 ] can be used as a new contact point.
A wire’s ends must always be soldered to distinct contact points. Both wire’s ends can be re-solded, but
that will require two actions and will take 2 minutes in total.
Find the minimum amount of time Polycarpus needs to re-solder wires so that a signal can pass between
any pair of wires. Also output an optimal sequence of wire re-soldering.
Input
The input contains one or several test cases. The first input line contains a single integer t — number of
test cases. Then, t test cases follow.
The first line of each test case contains a single integer n (1 ≤ n ≤ 105 ) — the number of wires. The
following n lines describe wires, each line containing two space-separated integers xi , yi (1 ≤ xi , yi ≤ 109 ,
xi 6= yi ) — contact points connected by the i-th wire. A couple of contact points can be connected with
more than one wire.
Sum of values of n across all test cases does not exceed 105 .
Output
For each test case first print one line with a single integer k — the minimum number of minutes needed
to re-solder wires so that a signal can pass between any pair of wires. In the following k lines print the
description of re-solderings. Each re-soldering should be described by three integers wj , aj , bj (1 ≤ wj ≤ n,
1 ≤ aj , bj ≤ 109 ). Such triple means that during the j-th re-soldering an end of the wj -th wire, which
was soldered to contact point aj , becomes soldered to contact point bj instead. After each re-soldering of
a wire it must connect two distinct contact points. If there are multiple optimal re-solderings, print any
of them.
Page 20 of 21
2019-2020 ICPC, Northern Eurasia, Southern and Volga Russian Regional Contest
Saratov, Saratov State U, October, 15, 2019
Example
standard input standard output
2 0
1 1
4 7 2 3 5
4
1 2
2 3
4 5
5 6
Page 21 of 21