contestId
int64
0
1.01k
name
stringlengths
2
58
tags
listlengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
listlengths
0
7
demo-output
listlengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
174
Range Increments
[ "data structures", "greedy" ]
null
null
Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array *a* for all indexes in the segment [*l*,<=*r*]. In other words, this function does the following: Polycarpus knows the state of the array...
The first input line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the length of the array *a*[1... *n*]. The second line contains its integer space-separated elements, *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=≤<=*a*[*i*]<=≤<=105) after some series of function calls rangeIncrement(l, r). It is guaranteed that at ...
Print on the first line *t* — the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105. Then print *t* lines — the descriptions of function calls, one per line. Each line should contain two integers *l**i...
[ "6\n1 2 1 1 4 1\n", "5\n1 0 1 0 1\n" ]
[ "5\n2 2\n5 5\n5 5\n5 5\n1 6\n", "3\n1 1\n3 3\n5 5\n" ]
The first sample requires a call for the entire array, and four additional calls: - one for the segment [2,2] (i.e. the second element of the array), - three for the segment [5,5] (i.e. the fifth element of the array).
[ { "input": "6\n1 2 1 1 4 1", "output": "5\n2 2\n5 5\n5 5\n5 5\n1 6" }, { "input": "5\n1 0 1 0 1", "output": "3\n1 1\n3 3\n5 5" }, { "input": "1\n1", "output": "1\n1 1" }, { "input": "1\n100000", "output": "100000\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1...
60
0
0
10,787
281
Nearest Fraction
[ "brute force", "implementation", "two pointers" ]
null
null
You are given three positive integers *x*,<=*y*,<=*n*. Your task is to find the nearest fraction to fraction whose denominator is no more than *n*. Formally, you should find such pair of integers *a*,<=*b* (1<=≤<=*b*<=≤<=*n*; 0<=≤<=*a*) that the value is as minimal as possible. If there are multiple "nearest" frac...
A single line contains three integers *x*,<=*y*,<=*n* (1<=≤<=*x*,<=*y*,<=*n*<=≤<=105).
Print the required fraction in the format "*a*/*b*" (without quotes).
[ "3 7 6\n", "7 2 4\n" ]
[ "2/5\n", "7/2\n" ]
none
[]
60
0
0
10,831
0
none
[ "none" ]
null
null
Once Vasya and Petya assembled a figure of *m* cubes, each of them is associated with a number between 0 and *m*<=-<=1 (inclusive, each number appeared exactly once). Let's consider a coordinate system such that the *OX* is the ground, and the *OY* is directed upwards. Each cube is associated with the coordinates of it...
The first line contains number *m* (2<=≤<=*m*<=≤<=105). The following *m* lines contain the coordinates of the cubes *x**i*,<=*y**i* (<=-<=109<=≤<=*x**i*<=≤<=109, 0<=≤<=*y**i*<=≤<=109) in ascending order of numbers written on them. It is guaranteed that the original figure is stable. No two cubes occupy the same plac...
In the only line print the answer to the problem.
[ "3\n2 1\n1 0\n0 1\n", "5\n0 0\n0 1\n0 2\n0 3\n0 4\n" ]
[ "19\n", "2930\n" ]
none
[ { "input": "3\n2 1\n1 0\n0 1", "output": "19" }, { "input": "5\n0 0\n0 1\n0 2\n0 3\n0 4", "output": "2930" }, { "input": "10\n-1 2\n-3 0\n5 5\n4 4\n-2 1\n1 1\n3 3\n2 2\n0 0\n-1000000000 0", "output": "41236677" }, { "input": "10\n-678318184 2\n-678318182 3\n580731357 2\n-6783...
46
307,200
0
10,850
958
Death Stars (easy)
[ "implementation" ]
null
null
The stardate is 1977 and the science and art of detecting Death Stars is in its infancy. Princess Heidi has received information about the stars in the nearby solar system from the Rebel spies and now, to help her identify the exact location of the Death Star, she needs to know whether this information is correct. Tw...
The first line of the input contains one number *N* (1<=≤<=*N*<=≤<=10) – the dimension of each map. Next *N* lines each contain *N* characters, depicting the first map: 'X' indicates a star, while 'O' indicates an empty quadrant of space. Next *N* lines each contain *N* characters, depicting the second map in the same ...
The only line of output should contain the word Yes if the maps are identical, or No if it is impossible to match them by performing rotations and translations.
[ "4\nXOOO\nXXOO\nOOOO\nXXXX\nXOOO\nXOOO\nXOXO\nXOXX\n", "2\nXX\nOO\nXO\nOX\n" ]
[ "Yes\n", "No\n" ]
In the first test, you can match the first map to the second map by first flipping the first map along the vertical axis, and then by rotating it 90 degrees clockwise.
[ { "input": "4\nXOOO\nXXOO\nOOOO\nXXXX\nXOOO\nXOOO\nXOXO\nXOXX", "output": "Yes" }, { "input": "2\nXX\nOO\nXO\nOX", "output": "No" }, { "input": "1\nO\nO", "output": "Yes" }, { "input": "1\nX\nO", "output": "No" }, { "input": "2\nOX\nXX\nOX\nXX", "output": "Yes...
77
307,200
0
10,853
192
Walking in the Rain
[ "brute force", "implementation" ]
null
null
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of *n* tiles that are lain in a row and are numbered from 1 to *n* from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number *n*. During the walk it is allowed to move...
The first line contains integer *n* (1<=≤<=*n*<=≤<=103) — the boulevard's length in tiles. The second line contains *n* space-separated integers *a**i* — the number of days after which the *i*-th tile gets destroyed (1<=≤<=*a**i*<=≤<=103).
Print a single number — the sought number of days.
[ "4\n10 3 5 10\n", "5\n10 2 8 3 5\n" ]
[ "5\n", "5\n" ]
In the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it. In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed ...
[ { "input": "4\n10 3 5 10", "output": "5" }, { "input": "5\n10 2 8 3 5", "output": "5" }, { "input": "10\n10 3 1 6 7 1 3 3 8 1", "output": "1" }, { "input": "10\n26 72 10 52 2 5 61 2 39 64", "output": "5" }, { "input": "100\n8 2 1 2 8 3 5 8 5 1 9 3 4 1 5 6 4 2 9 10...
92
0
3
10,860
961
Chessboard
[ "bitmasks", "brute force", "implementation" ]
null
null
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size *n* by *n*, *n* is always odd. And what's even worse, some squares were of wrong color. *j*-th square of the *i*-th row of *k*-th piece of the board has color *a**k*...
The first line contains odd integer *n* (1<=≤<=*n*<=≤<=100) — the size of all pieces of the board. Then 4 segments follow, each describes one piece of the board. Each consists of *n* lines of *n* characters; *j*-th one of *i*-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separat...
Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
[ "1\n0\n\n0\n\n1\n\n0\n", "3\n101\n010\n101\n\n101\n000\n101\n\n010\n101\n011\n\n010\n101\n010\n" ]
[ "1\n", "2\n" ]
none
[ { "input": "1\n0\n\n0\n\n1\n\n0", "output": "1" }, { "input": "3\n101\n010\n101\n\n101\n000\n101\n\n010\n101\n011\n\n010\n101\n010", "output": "2" }, { "input": "3\n000\n000\n000\n\n111\n111\n111\n\n111\n111\n111\n\n000\n000\n000", "output": "16" }, { "input": "3\n101\n010\n1...
171
1,945,600
3
10,870
301
Yaroslav and Sequence
[ "constructive algorithms" ]
null
null
Yaroslav has an array, consisting of (2·*n*<=-<=1) integers. In a single operation Yaroslav can change the sign of exactly *n* elements in the array. In other words, in one operation Yaroslav can select exactly *n* array elements, and multiply each of them by -1. Yaroslav is now wondering: what maximum sum of array el...
The first line contains an integer *n* (2<=≤<=*n*<=≤<=100). The second line contains (2·*n*<=-<=1) integers — the array elements. The array elements do not exceed 1000 in their absolute value.
In a single line print the answer to the problem — the maximum sum that Yaroslav can get.
[ "2\n50 50 50\n", "2\n-1 -100 -1\n" ]
[ "150\n", "100\n" ]
In the first sample you do not need to change anything. The sum of elements equals 150. In the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100.
[ { "input": "2\n50 50 50", "output": "150" }, { "input": "2\n-1 -100 -1", "output": "100" }, { "input": "3\n-959 -542 -669 -513 160", "output": "2843" }, { "input": "4\n717 473 344 -51 -548 703 -869", "output": "3603" }, { "input": "5\n270 -181 957 -509 -6 937 -175...
124
0
3
10,879
0
none
[ "none" ]
null
null
Stepan likes to repeat vowel letters when he writes words. For example, instead of the word "pobeda" he can write "pobeeeedaaaaa". Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vowe...
The first line contains the integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of letters in the word written by Stepan. The second line contains the string *s* which has length that equals to *n* and contains only lowercase English letters — the word written by Stepan.
Print the single string — the word written by Stepan converted according to the rules described in the statement.
[ "13\npobeeeedaaaaa\n", "22\niiiimpleeemeentatiioon\n", "18\naeiouyaaeeiioouuyy\n", "24\naaaoooiiiuuuyyyeeeggghhh\n" ]
[ "pobeda\n", "implemeentatioon\n", "aeiouyaeeioouy\n", "aoiuyeggghhh\n" ]
none
[ { "input": "13\npobeeeedaaaaa", "output": "pobeda" }, { "input": "22\niiiimpleeemeentatiioon", "output": "implemeentatioon" }, { "input": "18\naeiouyaaeeiioouuyy", "output": "aeiouyaeeioouy" }, { "input": "24\naaaoooiiiuuuyyyeeeggghhh", "output": "aoiuyeggghhh" }, { ...
46
4,812,800
0
10,904
340
Maximal Area Quadrilateral
[ "brute force", "geometry" ]
null
null
Iahub has drawn a set of *n* points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners). Please note that a quadrilateral doesn't have to be convex. A special quadrilateral is o...
The first line contains integer *n* (4<=≤<=*n*<=≤<=300). Each of the next *n* lines contains two integers: *x**i*, *y**i* (<=-<=1000<=≤<=*x**i*,<=*y**i*<=≤<=1000) — the cartesian coordinates of *i*th special point. It is guaranteed that no three points are on the same line. It is guaranteed that no two points coincide.
Output a single real number — the maximal area of a special quadrilateral. The answer will be considered correct if its absolute or relative error does't exceed 10<=-<=9.
[ "5\n0 0\n0 4\n4 0\n4 4\n2 3\n" ]
[ "16.000000" ]
In the test example we can choose first 4 points to be the vertices of the quadrilateral. They form a square by side 4, so the area is 4·4 = 16.
[ { "input": "5\n0 0\n0 4\n4 0\n4 4\n2 3", "output": "16.000000" }, { "input": "10\n-6 -4\n-7 5\n-7 -7\n5 -7\n4 -9\n-6 7\n2 9\n-4 -6\n2 10\n-10 -4", "output": "166.000000" }, { "input": "4\n-3 3\n0 3\n-2 -1\n2 2", "output": "11.000000" }, { "input": "5\n-4 -3\n-3 -2\n3 3\n-1 2\...
716
1,126,400
0
10,930
69
Subsegments
[ "data structures", "implementation" ]
E. Subsegments
1
256
Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maxi...
The first line contains two positive integers *n* and *k* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*k*<=≤<=*n*) — the number of array elements and the length of the segment. Then follow *n* lines: the *i*-th one contains a single number *a**i* (<=-<=109<=≤<=*a**i*<=≤<=109).
Print *n*–*k*<=+<=1 numbers, one per line: on the *i*-th line print of the maximum number of those numbers from the subarray *a**i* *a**i*<=+<=1 … *a**i*<=+<=*k*<=-<=1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing".
[ "5 3\n1\n2\n2\n3\n3\n", "6 4\n3\n3\n3\n4\n4\n2\n" ]
[ "1\n3\n2\n", "4\nNothing\n3\n" ]
none
[ { "input": "5 3\n1\n2\n2\n3\n3", "output": "1\n3\n2" }, { "input": "6 4\n3\n3\n3\n4\n4\n2", "output": "4\nNothing\n3" }, { "input": "10 3\n-55\n-35\n-80\n91\n-96\n-93\n-39\n-77\n4\n29", "output": "-35\n91\n91\n91\n-39\n-39\n4\n29" }, { "input": "10 3\n-13\n26\n-97\n-38\n43\n-...
1,000
3,891,200
0
10,958
540
School Marks
[ "greedy", "implementation" ]
null
null
Little Vova studies programming in an elite school. Vova and his classmates are supposed to write *n* progress tests, for each test they will get a mark from 1 to *p*. Vova is very smart and he can write every test for any mark, but he doesn't want to stand out from the crowd too much. If the sum of his marks for all t...
The first line contains 5 space-separated integers: *n*, *k*, *p*, *x* and *y* (1<=≤<=*n*<=≤<=999, *n* is odd, 0<=≤<=*k*<=&lt;<=*n*, 1<=≤<=*p*<=≤<=1000, *n*<=≤<=*x*<=≤<=*n*·*p*, 1<=≤<=*y*<=≤<=*p*). Here *n* is the number of tests that Vova is planned to write, *k* is the number of tests he has already written, *p* is t...
If Vova cannot achieve the desired result, print "-1". Otherwise, print *n*<=-<=*k* space-separated integers — the marks that Vova should get for the remaining tests. If there are multiple possible solutions, print any of them.
[ "5 3 5 18 4\n3 5 4\n", "5 3 5 16 4\n5 5 5\n" ]
[ "4 1\n", "-1\n" ]
The median of sequence *a*<sub class="lower-index">1</sub>, ..., *a*<sub class="lower-index">*n*</sub> where *n* is odd (in this problem *n* is always odd) is the element staying on (*n* + 1) / 2 position in the sorted list of *a*<sub class="lower-index">*i*</sub>. In the first sample the sum of marks equals 3 + 5 + 4...
[ { "input": "5 3 5 18 4\n3 5 4", "output": "4 1" }, { "input": "5 3 5 16 4\n5 5 5", "output": "-1" }, { "input": "5 3 5 17 4\n5 5 5", "output": "1 1" }, { "input": "5 3 5 12 1\n5 5 1", "output": "-1" }, { "input": "5 3 5 13 1\n5 5 1", "output": "1 1" }, { ...
62
1,433,600
0
10,979
222
Olympiad
[ "binary search", "greedy", "sortings", "two pointers" ]
null
null
A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least *x* points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya's chances....
The first line contains two space-separated integers *n*,<=*x* (1<=≤<=*n*<=≤<=105; 0<=≤<=*x*<=≤<=2·105) — the number of Olympiad participants and the minimum number of points Vasya earned. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=105) — the participants' point...
Print two space-separated integers — the best and the worst place Vasya could have got on the Olympiad.
[ "5 2\n1 1 1 1 1\n1 1 1 1 1\n", "6 7\n4 3 5 6 4 4\n8 6 0 4 3 4\n" ]
[ "1 5\n", "1 5\n" ]
In the first text sample all 5 participants earn 2 points each in any case. Depending on the jury's decision, Vasya can get the first (the best) as well as the last (the worst) fifth place. In the second test sample in the best case scenario Vasya wins again: he can win 12 points and become the absolute winner if the ...
[ { "input": "5 2\n1 1 1 1 1\n1 1 1 1 1", "output": "1 5" }, { "input": "6 7\n4 3 5 6 4 4\n8 6 0 4 3 4", "output": "1 5" }, { "input": "1 100\n56\n44", "output": "1 1" }, { "input": "5 1\n1 2 3 4 5\n1 2 3 4 5", "output": "1 5" }, { "input": "5 5\n2 2 2 2 2\n3 3 3 3 ...
560
13,312,000
3
10,983
171
A Piece of Cake
[ "*special", "implementation" ]
null
null
How to make a cake you'll never eat. Ingredients. - 2 carrots - 0 calories - 100 g chocolate spread - 1 pack of flour - 1 egg Method. 1. Put calories into the mixing bowl. 1. Take carrots from refrigerator. 1. Chop carrots. 1. Take chocolate spread from refrigerator. 1. Put chocolate spread into the mix...
The only line of input contains a sequence of integers *a*0,<=*a*1,<=... (1<=≤<=*a*0<=≤<=100, 0<=≤<=*a**i*<=≤<=1000 for *i*<=≥<=1).
Output a single integer.
[ "4 1 2 3 4\n" ]
[ "30\n" ]
none
[ { "input": "4 1 2 3 4", "output": "30" }, { "input": "4 802 765 992 1", "output": "5312" }, { "input": "4 220 380 729 969", "output": "7043" }, { "input": "3 887 104 641", "output": "3018" }, { "input": "12 378 724 582 387 583 241 294 159 198 653 369 418", "ou...
218
6,758,400
3
10,985
66
Petya and File System
[ "data structures", "implementation" ]
C. Petya and File System
3
256
Recently, on a programming lesson little Petya showed how quickly he can create files and folders on the computer. But he got soon fed up with this activity, and he decided to do a much more useful thing. He decided to calculate what folder contains most subfolders (including nested folders, nested folders of nested fo...
Each line of input data contains the description of one file path. The length of each line does not exceed 100, and overall there are no more than 100 lines. It is guaranteed, that all the paths are correct and meet the above rules. It is also guaranteed, that there are no two completely equal lines. That is, each file...
Print two space-separated numbers. The first one is the maximal number of possible subfolders in a folder (including nested folders, nested folders of nested folders, and so on). The second one is the maximal number of files in a folder (including nested files in subfolders). Note that the disks are not regarded as fol...
[ "C:\\folder1\\file1.txt", "C:\\folder1\\folder2\\folder3\\file1.txt\nC:\\folder1\\folder2\\folder4\\file1.txt\nD:\\folder1\\file1.txt\n", "C:\\file\\file\\file\\file\\file.txt\nC:\\file\\file\\file\\file2\\file.txt" ]
[ "0 1", "3 2", "4 2" ]
In the first sample we have one folder on the "C" disk. It has no subfolders, which is why the first number in the answer is 0. But this folder contains one file, so the second number of the answer is 1. In the second sample we have several different folders. Consider the "folder1" folder on the "C" disk. This folder ...
[ { "input": "C:\\folder1\\file1.txt", "output": "0 1" }, { "input": "C:\\folder1\\folder2\\folder3\\file1.txt\nC:\\folder1\\folder2\\folder4\\file1.txt\nD:\\folder1\\file1.txt", "output": "3 2" }, { "input": "C:\\file\\file\\file\\file\\file.txt\nC:\\file\\file\\file\\file2\\file.txt", ...
310
24,780,800
0
10,991
946
Largest Beautiful Number
[ "greedy", "implementation" ]
null
null
Yes, that's another problem with definition of "beautiful" numbers. Let's call a positive integer *x* beautiful if its decimal representation without leading zeroes contains even number of digits, and there exists a permutation of this representation which is palindromic. For example, 4242 is a beautiful number, since...
The first line contains one integer *t* (1<=≤<=*t*<=≤<=105) — the number of testcases you have to solve. Then *t* lines follow, each representing one testcase and containing one string which is the decimal representation of number *s*. It is guaranteed that this string has even length, contains no leading zeroes, and ...
For each testcase print one line containing the largest beautiful number which is less than *s* (it is guaranteed that the answer exists).
[ "4\n89\n88\n1000\n28923845\n" ]
[ "88\n77\n99\n28923839\n" ]
none
[ { "input": "4\n89\n88\n1000\n28923845", "output": "88\n77\n99\n28923839" }, { "input": "1\n100001", "output": "9999" }, { "input": "2\n1001\n100001", "output": "99\n9999" }, { "input": "3\n100001\n100000\n100002", "output": "9999\n9999\n100001" }, { "input": "1\n1...
15
0
0
10,992
196
Paint Tree
[ "constructive algorithms", "divide and conquer", "geometry", "sortings", "trees" ]
null
null
You are given a tree with *n* vertexes and *n* points on a plane, no three points lie on one straight line. Your task is to paint the given tree on a plane, using the given points as vertexes. That is, you should correspond each vertex of the tree to exactly one point and each point should correspond to a vertex. If...
The first line contains an integer *n* (1<=≤<=*n*<=≤<=1500) — the number of vertexes on a tree (as well as the number of chosen points on the plane). Each of the next *n*<=-<=1 lines contains two space-separated integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*) — the numbers of tree vertexe...
Print *n* distinct space-separated integers from 1 to *n*: the *i*-th number must equal the number of the vertex to place at the *i*-th point (the points are numbered in the order, in which they are listed in the input). If there are several solutions, print any of them.
[ "3\n1 3\n2 3\n0 0\n1 1\n2 0\n", "4\n1 2\n2 3\n1 4\n-1 -2\n3 5\n-3 3\n2 0\n" ]
[ "1 3 2\n", "4 2 1 3\n" ]
The possible solutions for the sample are given below.
[]
60
0
0
11,002
776
Sherlock and the Encrypted Data
[ "bitmasks", "combinatorics", "dp" ]
null
null
Sherlock found a piece of encrypted data which he thinks will be useful to catch Moriarty. The encrypted data consists of two integer *l* and *r*. He noticed that these integers were in hexadecimal form. He takes each of the integers from *l* to *r*, and performs the following operations: 1. He lists the distinct dig...
First line contains the integer *q* (1<=≤<=*q*<=≤<=10000). Each of the next *q* lines contain two hexadecimal integers *l* and *r* (0<=≤<=*l*<=≤<=*r*<=&lt;<=1615). The hexadecimal integers are written using digits from 0 to 9 and/or lowercase English letters a, b, c, d, e, f. The hexadecimal integers do not contain ...
Output *q* lines, *i*-th line contains answer to the *i*-th query (in decimal notation).
[ "1\n1014 1014\n", "2\n1 1e\n1 f\n", "2\n1 abc\nd0e fe23\n" ]
[ "1\n", "1\n0\n", "412\n28464\n" ]
For the second input, 14<sub class="lower-index">16</sub> = 20<sub class="lower-index">10</sub> *sum* = 2<sup class="upper-index">1</sup> + 2<sup class="upper-index">4</sup> = 18 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fcc2edc423982d6c5d19cc2a89cc99c492aa2427.png" style="max-wid...
[]
31
0
0
11,037
61
Eternal Victory
[ "dfs and similar", "graphs", "greedy", "shortest paths", "trees" ]
D. Eternal Victory
2
256
Valerian was captured by Shapur. The victory was such a great one that Shapur decided to carve a scene of Valerian's defeat on a mountain. So he had to find the best place to make his victory eternal! He decided to visit all *n* cities of Persia to find the best available mountain, but after the recent war he was too ...
First line contains a single natural number *n* (1<=≤<=*n*<=≤<=105) — the amount of cities. Next *n*<=-<=1 lines contain 3 integer numbers each *x**i*, *y**i* and *w**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*,<=0<=≤<=*w**i*<=≤<=2<=×<=104). *x**i* and *y**i* are two ends of a road and *w**i* is the length of that road.
A single integer number, the minimal length of Shapur's travel. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
[ "3\n1 2 3\n2 3 4\n", "3\n1 2 3\n1 3 3\n" ]
[ "7\n", "9\n" ]
none
[ { "input": "3\n1 2 3\n2 3 4", "output": "7" }, { "input": "3\n1 2 3\n1 3 3", "output": "9" }, { "input": "5\n5 3 60\n4 3 63\n2 1 97\n3 1 14", "output": "371" }, { "input": "3\n2 1 63\n3 1 78", "output": "204" }, { "input": "13\n8 2 58\n2 1 49\n13 10 41\n11 9 67\n6...
405
40,243,200
3.823791
11,052
19
World Football Cup
[ "implementation" ]
A. World Football Cup
2
64
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: - the final tournament features *n* teams (*n* is always even) - the first *n*<=/<=2 t...
The first input line contains the only integer *n* (1<=≤<=*n*<=≤<=50) — amount of the teams, taking part in the final tournament of World Cup. The following *n* lines contain the names of these teams, a name is a string of lower-case and upper-case Latin letters, its length doesn't exceed 30 characters. The following *...
Output *n*<=/<=2 lines — names of the teams, which managed to get through to the knockout stage in lexicographical order. Output each name in a separate line. No odd characters (including spaces) are allowed. It's guaranteed that the described regulations help to order the teams without ambiguity.
[ "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3\n", "2\na\nA\na-A 2:1\n" ]
[ "A\nD\n", "a\n" ]
none
[ { "input": "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3", "output": "A\nD" }, { "input": "2\na\nA\na-A 2:1", "output": "a" }, { "input": "2\nEULEUbCmfrmqxtzvg\nuHGRmKUhDcxcfqyruwzen\nuHGRmKUhDcxcfqyruwzen-EULEUbCmfrmqxtzvg 13:92", "output": "EULEUbCmfrmqxtzvg"...
92
4,608,000
-1
11,067
659
Qualifying Contest
[ "constructive algorithms", "sortings" ]
null
null
Very soon Berland will hold a School Team Programming Olympiad. From each of the *m* Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by *n* Berland students. There were at least two schoolboys participating from each of th...
The first line of the input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=100<=000, 1<=≤<=*m*<=≤<=10<=000, *n*<=≥<=2*m*) — the number of participants of the qualifying contest and the number of regions in Berland. Next *n* lines contain the description of the participants of the qualifying contest in the following ...
Print *m* lines. On the *i*-th line print the team of the *i*-th region — the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region.
[ "5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503\n", "5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503\n" ]
[ "Sidorov Ivanov\nAndreev Semenov\n", "?\nAndreev Semenov\n" ]
In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely.
[ { "input": "5 2\nIvanov 1 763\nAndreev 2 800\nPetrov 1 595\nSidorov 1 790\nSemenov 2 503", "output": "Sidorov Ivanov\nAndreev Semenov" }, { "input": "5 2\nIvanov 1 800\nAndreev 2 763\nPetrov 1 800\nSidorov 1 800\nSemenov 2 503", "output": "?\nAndreev Semenov" }, { "input": "10 2\nSHiBIEz...
545
17,203,200
3
11,075
985
Sand Fortress
[ "binary search", "constructive algorithms", "math" ]
null
null
You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right. Obviously, there is not enough sand o...
The only line contains two integer numbers *n* and *H* (1<=≤<=*n*,<=*H*<=≤<=1018) — the number of sand packs you have and the height of the fence, respectively.
Print the minimum number of spots you can occupy so the all the castle building conditions hold.
[ "5 2\n", "6 8\n" ]
[ "3\n", "3\n" ]
Here are the heights of some valid castles: - *n* = 5, *H* = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...] - *n* = 6, *H* = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied) The first list for both cases is the optimal answer, 3 spots are ...
[ { "input": "5 2", "output": "3" }, { "input": "6 8", "output": "3" }, { "input": "20 4", "output": "7" }, { "input": "1000000000000000000 1000000000000000000", "output": "1414213562" }, { "input": "1 1", "output": "1" }, { "input": "1 10000000000000000...
62
0
3
11,078
165
Another Problem on Strings
[ "binary search", "brute force", "dp", "math", "strings", "two pointers" ]
null
null
A string is binary, if it consists only of characters "0" and "1". String *v* is a substring of string *w* if it has a non-zero length and can be read starting from some position in string *w*. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if th...
The first line contains the single integer *k* (0<=≤<=*k*<=≤<=106). The second line contains a non-empty binary string *s*. The length of *s* does not exceed 106 characters.
Print the single number — the number of substrings of the given string, containing exactly *k* characters "1". Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "1\n1010\n", "2\n01010\n", "100\n01010\n" ]
[ "6\n", "4\n", "0\n" ]
In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
[ { "input": "1\n1010", "output": "6" }, { "input": "2\n01010", "output": "4" }, { "input": "100\n01010", "output": "0" }, { "input": "0\n01010", "output": "3" }, { "input": "0\n0010100011", "output": "10" }, { "input": "0\n10000", "output": "10" }...
216
22,220,800
-1
11,091
749
Voting
[ "greedy", "implementation", "two pointers" ]
null
null
There are *n* employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote. Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=200<=000) — the number of employees. The next line contains *n* characters. The *i*-th character is 'D' if the *i*-th employee is from depublicans fraction or 'R' if he is from remocrats.
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
[ "5\nDDRRR\n", "6\nDDRRRR\n" ]
[ "D\n", "R\n" ]
Consider one of the voting scenarios for the first sample: 1. Employee 1 denies employee 5 to vote. 1. Employee 2 denies employee 3 to vote. 1. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). 1. Employee 4 denies employee 2 to vote. 1. Employee 5 has no right to vote and skips h...
[ { "input": "5\nDDRRR", "output": "D" }, { "input": "6\nDDRRRR", "output": "R" }, { "input": "1\nD", "output": "D" }, { "input": "1\nR", "output": "R" }, { "input": "2\nDR", "output": "D" }, { "input": "3\nRDD", "output": "D" }, { "input": "...
171
11,673,600
3
11,122
30
Codeforces World Finals
[ "implementation" ]
B. Codeforces World Finals
2
256
The king Copa often has been reported about the Codeforces site, which is rapidly getting more and more popular among the brightest minds of the humanity, who are using it for training and competing. Recently Copa understood that to conquer the world he needs to organize the world Codeforces tournament. He hopes that a...
The first line contains the date *DD*.*MM*.*YY*, the second line contains the date *BD*.*BM*.*BY*. It is guaranteed that both dates are correct, and *YY* and *BY* are always in [01;99]. It could be that by passport Bob was born after the finals. In this case, he can still change the order of numbers in date.
If it is possible to rearrange the numbers in the date of birth so that Bob will be at least 18 years old on the *DD*.*MM*.*YY*, output YES. In the other case, output NO. Each number contains exactly two digits and stands for day, month or year in a date. Note that it is permitted to rearrange only numbers, not digit...
[ "01.01.98\n01.01.80\n", "20.10.20\n10.02.30\n", "28.02.74\n28.02.64\n" ]
[ "YES\n", "NO\n", "NO\n" ]
none
[ { "input": "01.01.98\n01.01.80", "output": "YES" }, { "input": "20.10.20\n10.02.30", "output": "NO" }, { "input": "28.02.74\n28.02.64", "output": "NO" }, { "input": "05.05.25\n06.02.71", "output": "NO" }, { "input": "19.11.54\n29.11.53", "output": "NO" }, ...
124
5,632,000
3.95851
11,132
832
Petya and Exam
[ "implementation", "strings" ]
null
null
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one.. There is a glob pattern in the statements (a string consisting of lowercase English letters, characters "?" and "*"). It is kno...
The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad. The second line contains the pattern — a string *s* of lowercase English letters, characters "?" and "*" (1<=≤<=|*s*|<=≤<=105). It is guaranteed that chara...
Print *n* lines: in the *i*-th of them print "YES" if the pattern matches the *i*-th query string, and "NO" otherwise. You can choose the case (lower or upper) for each letter arbitrary.
[ "ab\na?a\n2\naaa\naab\n", "abc\na?a?a*\n4\nabacaba\nabaca\napapa\naaaaax\n" ]
[ "YES\nNO\n", "NO\nYES\nNO\nYES\n" ]
In the first example we can replace "?" with good letters "a" and "b", so we can see that the answer for the first query is "YES", and the answer for the second query is "NO", because we can't match the third letter. Explanation of the second example. - The first query: "NO", because character "*" can be replaced w...
[ { "input": "ab\na?a\n2\naaa\naab", "output": "YES\nNO" }, { "input": "abc\na?a?a*\n4\nabacaba\nabaca\napapa\naaaaax", "output": "NO\nYES\nNO\nYES" }, { "input": "s\nc*?cb\n26\nbbaa\nb\ncc\ncbaab\nacacc\nca\na\nc\ncb\nabb\nba\nb\nba\ncac\nccccb\nccb\nbbbc\nabbcb\na\nbc\nc\na\nabb\nca\ncac...
61
0
0
11,136
373
Making Sequences is Fun
[ "binary search", "implementation", "math" ]
null
null
We'll define *S*(*n*) for positive integer *n* as follows: the number of the *n*'s digits in the decimal base. For example, *S*(893)<==<=3, *S*(114514)<==<=6. You want to make a consecutive integer sequence starting from number *m* (*m*,<=*m*<=+<=1,<=...). But you need to pay *S*(*n*)·*k* to add the number *n* to the ...
The first line contains three integers *w* (1<=≤<=*w*<=≤<=1016), *m* (1<=≤<=*m*<=≤<=1016), *k* (1<=≤<=*k*<=≤<=109). Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
The first line should contain a single integer — the answer to the problem.
[ "9 1 1\n", "77 7 7\n", "114 5 14\n", "1 1 2\n" ]
[ "9\n", "7\n", "6\n", "0\n" ]
none
[ { "input": "9 1 1", "output": "9" }, { "input": "77 7 7", "output": "7" }, { "input": "114 5 14", "output": "6" }, { "input": "1 1 2", "output": "0" }, { "input": "462 183 8", "output": "19" }, { "input": "462 183 8", "output": "19" }, { "i...
2,000
0
0
11,176
238
Not Wool Sequences
[ "constructive algorithms", "math" ]
null
null
A sequence of non-negative integers *a*1,<=*a*2,<=...,<=*a**n* of length *n* is called a wool sequence if and only if there exists two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) such that . In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0. The expression means...
The only line of input contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105).
Print the required number of sequences modulo 1000000009 (109<=+<=9) on the only line of output.
[ "3 2\n" ]
[ "6\n" ]
Sequences of length 3 made of integers 0, 1, 2 and 3 that are not a wool sequence are (1, 3, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (3, 1, 3) and (3, 2, 3).
[ { "input": "3 2", "output": "6" }, { "input": "4 2", "output": "0" }, { "input": "1 2", "output": "3" }, { "input": "4 11", "output": "433239206" }, { "input": "5 100", "output": "345449482" }, { "input": "5444 31525", "output": "637906839" }, ...
248
0
0
11,189
190
STL
[ "dfs and similar" ]
null
null
Vasya used to be an accountant before the war began and he is one of the few who knows how to operate a computer, so he was assigned as the programmer. We all know that programs often store sets of integers. For example, if we have a problem about a weighted directed graph, its edge can be represented by three integer...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105), showing how many numbers the type dictated by Gena contains. The second line contains space-separated words, said by Gena. Each of them is either "pair" or "int" (without the quotes). It is guaranteed that the total number of words does not exceed 105 ...
If it is possible to add the punctuation marks so as to get a correct type of language X-- as a result, print a single line that represents the resulting type. Otherwise, print "Error occurred" (without the quotes). Inside the record of a type should not be any extra spaces and other characters. It is guaranteed that...
[ "3\npair pair int int int\n", "1\npair int\n" ]
[ "pair&lt;pair&lt;int,int&gt;,int&gt;", "Error occurred" ]
none
[ { "input": "3\npair pair int int int", "output": "pair<pair<int,int>,int>" }, { "input": "1\npair int", "output": "Error occurred" }, { "input": "4\npair pair int int pair int int", "output": "pair<pair<int,int>,pair<int,int>>" }, { "input": "4\npair pair pair int int int int...
92
0
0
11,205
195
Analyzing Polyline
[ "geometry", "math", "sortings" ]
null
null
As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let them watch football in his bar only if they help his son complete a programming task. The task goes like that. Let's consider a set of fun...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of functions. Each of the following *n* lines contains two space-separated integer numbers *k**i*,<=*b**i* (<=-<=109<=≤<=*k**i*,<=*b**i*<=≤<=109) that determine the *i*-th function.
Print a single number — the number of angles that do not equal 180 degrees in the graph of the polyline that equals the sum of the given functions.
[ "1\n1 0\n", "3\n1 0\n0 2\n-1 1\n", "3\n-2 -4\n1 7\n-5 1\n" ]
[ "1\n", "2\n", "3\n" ]
none
[ { "input": "1\n1 0", "output": "1" }, { "input": "3\n1 0\n0 2\n-1 1", "output": "2" }, { "input": "3\n-2 -4\n1 7\n-5 1", "output": "3" }, { "input": "10\n9 9\n-5 2\n-2 9\n0 6\n6 7\n-1 -10\n-8 6\n3 6\n-3 -9\n0 4", "output": "8" }, { "input": "10\n-4 -9\n5 9\n-4 -1\...
124
4,608,000
-1
11,215
634
Island Puzzle
[ "constructive algorithms", "implementation" ]
null
null
A remote island chain contains *n* islands, labeled 1 through *n*. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands *n* and 1. The center of each island contains an identical pedestal, and all but...
The first line contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000) — the total number of islands. The second line contains *n* space-separated integers *a**i* (0<=≤<=*a**i*<=≤<=*n*<=-<=1) — the statue currently placed on the *i*-th island. If *a**i*<==<=0, then the island has no statue. It is guaranteed that the *a...
Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.
[ "3\n1 0 2\n2 0 1\n", "2\n1 0\n0 1\n", "4\n1 2 3 0\n0 3 2 1\n" ]
[ "YES\n", "YES\n", "NO\n" ]
In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3. In the second sample, the islanders can simply move statue 1 from island 1 to island 2. In the third sample, no sequence of movements...
[ { "input": "3\n1 0 2\n2 0 1", "output": "YES" }, { "input": "2\n1 0\n0 1", "output": "YES" }, { "input": "4\n1 2 3 0\n0 3 2 1", "output": "NO" }, { "input": "9\n3 8 4 6 7 1 5 2 0\n6 4 8 5 3 1 2 0 7", "output": "NO" }, { "input": "4\n2 3 1 0\n2 0 1 3", "output"...
31
4,608,000
0
11,236
813
The Contest
[ "implementation" ]
null
null
Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place! This contest consists of *n* problems, and Pasha solves *i*th problem in *a**i* time units (his solutions are always correct). At any moment of time he can be thinking ab...
The first line contains one integer *n* (1<=≤<=*n*<=≤<=1000) — the number of problems. The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=105) — the time Pasha needs to solve *i*th problem. The third line contains one integer *m* (0<=≤<=*m*<=≤<=1000) — the number of periods of time when the website is work...
If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted. Otherwise print "-1" (without brackets).
[ "2\n3 4\n2\n1 4\n7 9\n", "1\n5\n1\n1 4\n", "1\n5\n1\n1 5\n" ]
[ "7\n", "-1\n", "5\n" ]
In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again. In the second example Pasha invents...
[ { "input": "2\n3 4\n2\n1 4\n7 9", "output": "7" }, { "input": "1\n5\n1\n1 4", "output": "-1" }, { "input": "1\n5\n1\n1 5", "output": "5" }, { "input": "5\n100000 100000 100000 100000 100000\n0", "output": "-1" }, { "input": "5\n886 524 128 4068 298\n3\n416 3755\n4...
77
2,048,000
-1
11,239
991
Bishwock
[ "dp", "greedy" ]
null
null
Bishwock is a chess figure that consists of three squares resembling an "L-bar". This figure can be rotated by 90, 180 and 270 degrees so it can have four possible states: Bishwocks don't attack any squares and can even occupy on the adjacent squares as long as they don't occupy the same square. Vasya has a board wi...
The input contains two nonempty strings that describe Vasya's board. Those strings contain only symbols "0" (zero) that denote the empty squares and symbols "X" (uppercase English letter) that denote the squares occupied by pawns. Strings are nonempty and are of the same length that does not exceed $100$.
Output a single integer — the maximum amount of bishwocks that can be placed onto the given board.
[ "00\n00\n", "00X00X0XXX0\n0XXX0X00X00\n", "0X0X0\n0X0X0\n", "0XXX0\n00000\n" ]
[ "1", "4", "0", "2" ]
none
[ { "input": "00\n00", "output": "1" }, { "input": "00X00X0XXX0\n0XXX0X00X00", "output": "4" }, { "input": "0X0X0\n0X0X0", "output": "0" }, { "input": "0XXX0\n00000", "output": "2" }, { "input": "0\n0", "output": "0" }, { "input": "0\nX", "output": "...
109
6,963,200
3
11,277
638
Road Improvement
[ "*special", "dfs and similar", "graphs", "greedy", "trees" ]
null
null
In Berland there are *n* cities and *n*<=-<=1 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads. In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work s...
The first line of the input contains a positive integer *n* (2<=≤<=*n*<=≤<=200<=000) — the number of cities in Berland. Each of the next *n*<=-<=1 lines contains two numbers *u**i*, *v**i*, meaning that the *i*-th road connects city *u**i* and city *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*).
First print number *k* — the minimum number of days needed to repair all the roads in Berland. In next *k* lines print the description of the roads that should be repaired on each of the *k* days. On the *i*-th line print first number *d**i* — the number of roads that should be repaired on the *i*-th day, and then *d*...
[ "4\n1 2\n3 4\n3 2\n", "6\n3 4\n5 4\n3 2\n1 3\n4 6\n" ]
[ "2\n2 2 1\n1 3\n", "3\n1 1 \n2 2 3 \n2 4 5 \n" ]
In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 — on the second day.
[ { "input": "4\n1 2\n3 4\n3 2", "output": "2\n2 1 2 \n1 3 " }, { "input": "6\n3 4\n5 4\n3 2\n1 3\n4 6", "output": "3\n1 1 \n2 2 3 \n2 4 5 " }, { "input": "8\n1 3\n1 6\n3 4\n6 2\n5 6\n6 7\n7 8", "output": "4\n3 2 3 7 \n2 1 4 \n1 5 \n1 6 " }, { "input": "5\n1 2\n1 3\n1 4\n1 5", ...
2,000
67,686,400
0
11,287
513
Second price auction
[ "bitmasks", "probabilities" ]
null
null
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receive...
The first line of input contains an integer number *n* (2<=≤<=*n*<=≤<=5). *n* lines follow, the *i*-th of them containing two numbers *L**i* and *R**i* (1<=≤<=*L**i*<=≤<=*R**i*<=≤<=10000) describing the *i*-th company's bid preferences. This problem doesn't have subproblems. You will get 8 points for the correct submi...
Output the answer with absolute or relative error no more than 1*e*<=-<=9.
[ "3\n4 7\n8 10\n5 5\n", "3\n2 5\n3 4\n1 6\n" ]
[ "5.7500000000\n", "3.5000000000\n" ]
Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first ...
[ { "input": "3\n4 7\n8 10\n5 5", "output": "5.7500000000" }, { "input": "3\n2 5\n3 4\n1 6", "output": "3.5000000000" }, { "input": "5\n1 10000\n1 10000\n1 10000\n1 10000\n1 10000", "output": "6667.1666666646" }, { "input": "2\n1 2\n1 2", "output": "1.2500000000" }, { ...
405
10,342,400
3
11,346
204
Little Elephant and Cards
[ "binary search", "data structures" ]
null
null
The Little Elephant loves to play with color cards. He has *n* cards, each has exactly two colors (the color of the front side and the color of the back side). Initially, all the cards lay on the table with the front side up. In one move the Little Elephant can turn any card to the other side. The Little Elephant thin...
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of the cards. The following *n* lines contain the description of all cards, one card per line. The cards are described by a pair of positive integers not exceeding 109 — colors of both sides. The first number in a line is the color of the fro...
On a single line print a single integer — the sought minimum number of moves. If it is impossible to make the set funny, print -1.
[ "3\n4 7\n4 7\n7 4\n", "5\n4 7\n7 4\n2 11\n9 7\n1 1\n" ]
[ "0\n", "2\n" ]
In the first sample there initially are three cards lying with colors 4, 4, 7. Since two of the three cards are of the same color 4, you do not need to change anything, so the answer is 0. In the second sample, you can turn the first and the fourth cards. After that three of the five cards will be of color 7.
[ { "input": "3\n4 7\n4 7\n7 4", "output": "0" }, { "input": "5\n4 7\n7 4\n2 11\n9 7\n1 1", "output": "2" }, { "input": "1\n1 1", "output": "0" }, { "input": "2\n1 1\n1 1", "output": "0" }, { "input": "7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8", "output": "-1" }, ...
124
0
0
11,347
635
Orchestra
[ "brute force", "implementation" ]
null
null
Paul is at the orchestra. The string section is arranged in an *r*<=×<=*c* rectangular grid and is filled with violinists with the exception of *n* violists. Paul really likes violas, so he would like to take a picture including at least *k* of them. Paul can take a picture of any axis-parallel rectangle in the orchest...
The first line of input contains four space-separated integers *r*, *c*, *n*, *k* (1<=≤<=*r*,<=*c*,<=*n*<=≤<=10, 1<=≤<=*k*<=≤<=*n*) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively. The next *n* lines ea...
Print a single integer — the number of photographs Paul can take which include at least *k* violas.
[ "2 2 1 1\n1 2\n", "3 2 3 3\n1 1\n3 1\n2 2\n", "3 2 3 2\n1 1\n3 1\n2 2\n" ]
[ "4\n", "1\n", "4\n" ]
We will use '*' to denote violinists and '#' to denote violists. In the first sample, the orchestra looks as follows In the second sample, the orchestra looks as follows In the third sample, the orchestra looks the same as in the second sample.
[ { "input": "2 2 1 1\n1 2", "output": "4" }, { "input": "3 2 3 3\n1 1\n3 1\n2 2", "output": "1" }, { "input": "3 2 3 2\n1 1\n3 1\n2 2", "output": "4" }, { "input": "1 1 1 1\n1 1", "output": "1" }, { "input": "10 10 10 10\n6 1\n3 8\n10 6\n10 3\n10 4\n8 9\n2 3\n5 7\n...
140
3,686,400
3
11,364
510
Fox And Two Dots
[ "dfs and similar" ]
null
null
Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size *n*<=×<=*m* cells, like this: Each cell contains a dot that has some color. We will use different uppercase Latin characters to express different colors. The key of this game is to find a cycle that contain dots...
The first line contains two integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=50): the number of rows and columns of the board. Then *n* lines follow, each line contains a string consisting of *m* characters, expressing colors of dots in each line. Each character is an uppercase Latin letter.
Output "Yes" if there exists a cycle, and "No" otherwise.
[ "3 4\nAAAA\nABCA\nAAAA\n", "3 4\nAAAA\nABCA\nAADA\n", "4 4\nYYYR\nBYBY\nBBBY\nBBBY\n", "7 6\nAAAAAB\nABBBAB\nABAAAB\nABABBB\nABAAAB\nABBBAB\nAAAAAB\n", "2 13\nABCDEFGHIJKLM\nNOPQRSTUVWXYZ\n" ]
[ "Yes\n", "No\n", "Yes\n", "Yes\n", "No\n" ]
In first sample test all 'A' form a cycle. In second sample there is no such cycle. The third sample is displayed on the picture above ('Y' = Yellow, 'B' = Blue, 'R' = Red).
[ { "input": "3 4\nAAAA\nABCA\nAAAA", "output": "Yes" }, { "input": "3 4\nAAAA\nABCA\nAADA", "output": "No" }, { "input": "4 4\nYYYR\nBYBY\nBBBY\nBBBY", "output": "Yes" }, { "input": "7 6\nAAAAAB\nABBBAB\nABAAAB\nABABBB\nABAAAB\nABBBAB\nAAAAAB", "output": "Yes" }, { ...
62
0
-1
11,370
549
The Game Of Parity
[ "games" ]
null
null
There are *n* cities in Westeros. The *i*-th city is inhabited by *a**i* people. Daenerys and Stannis play the following game: in one single move, a player chooses a certain town and burns it to the ground. Thus all its residents, sadly, die. Stannis starts the game. The game ends when Westeros has exactly *k* cities l...
The first line contains two positive space-separated integers, *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=2·105) — the initial number of cities in Westeros and the number of cities at which the game ends. The second line contains *n* space-separated positive integers *a**i* (1<=≤<=*a**i*<=≤<=106), which represent the populat...
Print string "Daenerys" (without the quotes), if Daenerys wins and "Stannis" (without the quotes), if Stannis wins.
[ "3 1\n1 2 1\n", "3 1\n2 2 1\n", "6 3\n5 20 12 7 14 101\n" ]
[ "Stannis\n", "Daenerys\n", "Stannis\n" ]
In the first sample Stannis will use his move to burn a city with two people and Daenerys will be forced to burn a city with one resident. The only survivor city will have one resident left, that is, the total sum is odd, and thus Stannis wins. In the second sample, if Stannis burns a city with two people, Daenerys bu...
[ { "input": "3 1\n1 2 1", "output": "Stannis" }, { "input": "3 1\n2 2 1", "output": "Daenerys" }, { "input": "6 3\n5 20 12 7 14 101", "output": "Stannis" }, { "input": "6 3\n346 118 330 1403 5244 480", "output": "Daenerys" }, { "input": "7 4\n11 3532 99 3512 12 8 2...
155
13,824,000
0
11,379
313
Ilya and Roads
[ "dp" ]
null
null
Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as *n* holes in a row. We will consider the holes numbered from 1 to *n*, from left to right. Ilya is really keep on helping his city. So, he wants to fix at least *k* holes (perharps he can fix more) on a sing...
The first line contains three integers *n*,<=*m*,<=*k* (1<=≤<=*n*<=≤<=300,<=1<=≤<=*m*<=≤<=105,<=1<=≤<=*k*<=≤<=*n*). The next *m* lines contain the companies' description. The *i*-th line contains three integers *l**i*,<=*r**i*,<=*c**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*,<=1<=≤<=*c**i*<=≤<=109).
Print a single integer — the minimum money Ilya needs to fix at least *k* holes. If it is impossible to fix at least *k* holes, print -1. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6\n", "10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13\n", "10 1 9\n5 10 14\n" ]
[ "17\n", "2\n", "-1\n" ]
none
[ { "input": "10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6", "output": "17" }, { "input": "10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13", "output": "2" }, { "input": "10 1 9\n5 10 14", "output": "-1" }, { "input": "10 6 9\n6 8 7\n2 8 11\n2 6 10\n8 10 9\n2 5 8\n2 3 8", ...
60
0
0
11,380
329
The Evil Temple and the Moving Rocks
[ "constructive algorithms" ]
null
null
Important: All possible tests are in the pretest, so you shouldn't hack on this problem. So, if you passed pretests, you will also pass the system test. You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak monsters, you arrived at a square room consisting of tiles forming ...
The first line will consists of two integers *n* and *x*, denoting the size of the room and the number of sounds required to open the door. There will be exactly three test cases for this problem: - *n*<==<=5,<=*x*<==<=5; - *n*<==<=3,<=*x*<==<=2; - *n*<==<=100,<=*x*<==<=105. All of these testcases are in pretest.
Output *n* lines. Each line consists of *n* characters — the *j*-th character of the *i*-th line represents the content of the tile at the *i*-th row and the *j*-th column, and should be one of these: - '^', '&lt;', '&gt;', or 'v': a rock as described in the problem statement. - '.': an empty tile. Then, output two...
[ "5 5\n", "3 2\n" ]
[ "&gt;...v\nv.&lt;..\n..^..\n&gt;....\n..^.&lt;\n1 1\n", "&gt;vv\n^&lt;.\n^.&lt;\n1 3\n" ]
Here's a simulation of the first example, accompanied with the number of sounds produced so far. In the picture above, the activated rock switches between the '^' rock and the '&lt;' rock. However, no sound is produced since the '^' rock didn't move even a single tile. So, still 4 sound. At this point, 5 sound are al...
[ { "input": "5 5", "output": ">...v\nv.<..\n..^..\n>....\n..^.<\n1 1" }, { "input": "3 2", "output": ">vv\n^<.\n^.<\n1 3" }, { "input": "100 100000", "output": ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>v.\n^v<.<.<.<.<.<.<.<.<.<....
122
0
0
11,393
0
none
[ "none" ]
null
null
In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest. V...
The first line on the input contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000, *n* is even) — the number of positions available initially. The second line contains *n* distinct integers *x*1,<=*x*2,<=...,<=*x**n* (0<=≤<=*x**i*<=≤<=109), giving the coordinates of the corresponding positions.
Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.
[ "6\n0 1 3 7 15 31\n", "2\n73 37\n" ]
[ "7\n", "36\n" ]
In the first sample one of the optimum behavior of the players looks like that: 1. Vova bans the position at coordinate 15; 1. Lesha bans the position at coordinate 3; 1. Vova bans the position at coordinate 31; 1. Lesha bans the position at coordinate 1. After these actions only positions 0 and 7 will remain, an...
[ { "input": "6\n0 1 3 7 15 31", "output": "7" }, { "input": "2\n73 37", "output": "36" }, { "input": "2\n0 1000000000", "output": "1000000000" }, { "input": "8\n729541013 135019377 88372488 319157478 682081360 558614617 258129110 790518782", "output": "470242129" }, { ...
46
0
-1
11,398
939
Maximize!
[ "binary search", "greedy", "ternary search", "two pointers" ]
null
null
You are given a multiset *S* consisting of positive integers (initially empty). There are two kind of queries: 1. Add a positive integer to *S*, the newly added integer is not less than any number in it. 1. Find a subset *s* of the set *S* such that the value is maximum possible. Here *max*(*s*) means maximum valu...
The first line contains a single integer *Q* (1<=≤<=*Q*<=≤<=5·105) — the number of queries. Each of the next *Q* lines contains a description of query. For queries of type 1 two integers 1 and *x* are given, where *x* (1<=≤<=*x*<=≤<=109) is a number that you should add to *S*. It's guaranteed that *x* is not less than...
Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line. Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10<=-<=6. Formally, let your answer be *a*, and the jury's answe...
[ "6\n1 3\n2\n1 4\n2\n1 8\n2\n", "4\n1 1\n1 4\n1 5\n2\n" ]
[ "0.0000000000\n0.5000000000\n3.0000000000\n", "2.0000000000\n" ]
none
[ { "input": "6\n1 3\n2\n1 4\n2\n1 8\n2", "output": "0.0000000000\n0.5000000000\n3.0000000000" }, { "input": "4\n1 1\n1 4\n1 5\n2", "output": "2.0000000000" }, { "input": "8\n1 7\n1 26\n1 40\n1 45\n1 64\n2\n1 88\n1 94", "output": "31.6666666667" }, { "input": "9\n1 35\n2\n2\n1 ...
1,980
59,596,800
3
11,418
118
Present from Lena
[ "constructive algorithms", "implementation" ]
null
null
Vasya's birthday is approaching and Lena decided to sew a patterned handkerchief to him as a present. Lena chose digits from 0 to *n* as the pattern. The digits will form a rhombus. The largest digit *n* should be located in the centre. The digits should decrease as they approach the edges. For example, for *n*<==<=5 t...
The first line contains the single integer *n* (2<=≤<=*n*<=≤<=9).
Print a picture for the given *n*. You should strictly observe the number of spaces before the first digit on each line. Every two adjacent digits in the same line should be separated by exactly one space. There should be no spaces after the last digit at the end of each line.
[ "2\n", "3\n" ]
[ "0\n 0 1 0\n0 1 2 1 0\n 0 1 0\n 0\n", "0\n 0 1 0\n 0 1 2 1 0\n0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0\n" ]
none
[ { "input": "2", "output": " 0\n 0 1 0\n0 1 2 1 0\n 0 1 0\n 0" }, { "input": "3", "output": " 0\n 0 1 0\n 0 1 2 1 0\n0 1 2 3 2 1 0\n 0 1 2 1 0\n 0 1 0\n 0" }, { "input": "4", "output": " 0\n 0 1 0\n 0 1 2 1 0\n 0 1 2 3 2 1 0\n0 1 2 3 4 3 2 1 0...
60
0
0
11,422
101
Buses
[ "binary search", "data structures", "dp" ]
B. Buses
2
265
Little boy Gerald studies at school which is quite far from his house. That's why he has to go there by bus every day. The way from home to school is represented by a segment of a straight line; the segment contains exactly *n*<=+<=1 bus stops. All of them are numbered with integers from 0 to *n* in the order in which ...
The first line contains two space-separated integers: *n* and *m* (1<=≤<=*n*<=≤<=109,<=0<=≤<=*m*<=≤<=105). Then follow *m* lines each containing two integers *s**i*,<=*t**i*. They are the numbers of starting stops and end stops of the buses (0<=≤<=*s**i*<=&lt;<=*t**i*<=≤<=*n*).
Print the only number — the number of ways to get to the school modulo 1000000007 (109<=+<=7).
[ "2 2\n0 1\n1 2\n", "3 2\n0 1\n1 2\n", "5 5\n0 1\n0 2\n0 3\n0 4\n0 5\n" ]
[ "1\n", "0\n", "16\n" ]
The first test has the only variant to get to school: first on bus number one to the bus stop number one; then on bus number two to the bus stop number two. In the second test no bus goes to the third bus stop, where the school is positioned. Thus, the correct answer is 0. In the third test Gerald can either get or n...
[ { "input": "2 2\n0 1\n1 2", "output": "1" }, { "input": "3 2\n0 1\n1 2", "output": "0" }, { "input": "5 5\n0 1\n0 2\n0 3\n0 4\n0 5", "output": "16" }, { "input": "3 3\n1 2\n2 3\n1 3", "output": "0" }, { "input": "10 10\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n0 7\n0 8\n0 9\...
2,000
31,027,200
0
11,427
1,005
Tanya and Stairways
[ "implementation" ]
null
null
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$ ...
The first line contains $n$ ($1 \le n \le 1000$) — the total number of numbers pronounced by Tanya. The second line contains integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 1000$) — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway wit...
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.
[ "7\n1 2 3 1 2 3 4\n", "4\n1 1 1 1\n", "5\n1 2 3 4 5\n", "5\n1 2 1 2 1\n" ]
[ "2\n3 4 ", "4\n1 1 1 1 ", "1\n5 ", "3\n2 2 1 " ]
none
[ { "input": "7\n1 2 3 1 2 3 4", "output": "2\n3 4 " }, { "input": "4\n1 1 1 1", "output": "4\n1 1 1 1 " }, { "input": "5\n1 2 3 4 5", "output": "1\n5 " }, { "input": "5\n1 2 1 2 1", "output": "3\n2 2 1 " }, { "input": "1\n1", "output": "1\n1 " }, { "inp...
46
0
0
11,485
629
Babaei and Birthday Cake
[ "data structures", "dp" ]
null
null
As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party's cake. Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has *n* simple cakes and he is going to make a special ...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of simple cakes Babaei has. Each of the following *n* lines contains two integers *r**i* and *h**i* (1<=≤<=*r**i*,<=*h**i*<=≤<=10<=000), giving the radius and height of the *i*-th cake.
Print the maximum volume of the cake that Babaei can make. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "2\n100 30\n40 10\n", "4\n1 1\n9 7\n1 4\n10 7\n" ]
[ "942477.796077000\n", "3983.539484752\n" ]
In first sample, the optimal way is to choose the cake number 1. In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4.
[ { "input": "2\n100 30\n40 10", "output": "942477.796077000" }, { "input": "4\n1 1\n9 7\n1 4\n10 7", "output": "3983.539484752" }, { "input": "3\n2 2\n1 1\n3 3", "output": "109.955742876" }, { "input": "3\n2 2\n3 3\n1 1", "output": "109.955742876" }, { "input": "3\...
0
0
-1
11,499
739
Alyona and towers
[ "data structures" ]
null
null
Alyona has built *n* towers by putting small cubes some on the top of others. Each cube has size 1<=×<=1<=×<=1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row. Sometimes Alyona chooses some segment towers, and put on the top of each tower sever...
The first line contain single integer *n* (1<=≤<=*n*<=≤<=3·105) — the number of towers. The second line contain *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the number of cubes in each tower. The third line contain single integer *m* (1<=≤<=*m*<=≤<=3·105) — the number of additions. The next *m* ...
Print *m* lines. In *i*-th line print the maximum width of the hills after the *i*-th addition.
[ "5\n5 5 5 5 5\n3\n1 3 2\n2 2 1\n4 4 1\n" ]
[ "2\n4\n5\n" ]
The first sample is as follows: After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2. After addition of 1 cube on the second tower, the number of cubes in t...
[]
0
0
-1
11,520
364
Ghd
[ "brute force", "math", "probabilities" ]
null
null
John Doe offered his sister Jane Doe find the gcd of some set of numbers *a*. Gcd is a positive integer *g*, such that all number from the set are evenly divisible by *g* and there isn't such *g*' (*g*'<=&gt;<=*g*), that all numbers of the set are evenly divisible by *g*'. Unfortunately Jane couldn't cope with the ta...
The first line contains an integer *n* (1<=≤<=*n*<=≤<=106) showing how many numbers are in set *a*. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=1012). Please note, that given set can contain equal numbers. Please, do not write the %lld specifier to read or write 64-bi...
Print a single integer *g* — the Ghd of set *a*.
[ "6\n6 2 3 4 5 6\n", "5\n5 5 6 10 15\n" ]
[ "3\n", "5\n" ]
none
[ { "input": "6\n6 2 3 4 5 6", "output": "3" }, { "input": "5\n5 5 6 10 15", "output": "5" }, { "input": "100\n32 40 7 3 7560 21 7560 7560 10 12 3 7560 7560 7560 7560 5 7560 7560 6 7560 7560 7560 35 7560 18 7560 7560 7560 7560 7560 48 2 7 25 7560 2 2 49 7560 7560 15 16 7560 7560 2 7560 27 ...
4,000
114,585,600
0
11,533
679
Bear and Tower of Cubes
[ "binary search", "dp", "greedy" ]
null
null
Limak is a little polar bear. He plays by building towers from blocks. Every block is a cube with positive integer length of side. Limak has infinitely many blocks of each side length. A block with side *a* has volume *a*3. A tower consisting of blocks with sides *a*1,<=*a*2,<=...,<=*a**k* has the total volume *a*13<=...
The only line of the input contains one integer *m* (1<=≤<=*m*<=≤<=1015), meaning that Limak wants you to choose *X* between 1 and *m*, inclusive.
Print two integers — the maximum number of blocks in the tower and the maximum required total volume *X*, resulting in the maximum number of blocks.
[ "48\n", "6\n" ]
[ "9 42\n", "6 6\n" ]
In the first sample test, there will be 9 blocks if you choose *X* = 23 or *X* = 42. Limak wants to maximize *X* secondarily so you should choose 42. In more detail, after choosing *X* = 42 the process of building a tower is: - Limak takes a block with side 3 because it's the biggest block with volume not greater th...
[ { "input": "48", "output": "9 42" }, { "input": "6", "output": "6 6" }, { "input": "1", "output": "1 1" }, { "input": "994", "output": "12 941" }, { "input": "567000123", "output": "16 566998782" }, { "input": "123830583943", "output": "17 12383056...
31
307,200
-1
11,534
0
none
[ "none" ]
null
null
ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, '<=+<=' (plus) and '' (square root). Initially, the number 2 is displayed on the screen. There are *n*<=+<=1 levels in the game and ZS the Coder start at the level 1. When ZS the Coder is at level *k*, he can : 1. Pr...
The first and only line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000), denoting that ZS the Coder wants to reach level *n*<=+<=1.
Print *n* non-negative integers, one per line. *i*-th of them should be equal to the number of times that ZS the Coder needs to press the '<=+<=' button before pressing the '' button at level *i*. Each number in the output should not exceed 1018. However, the number on the screen can be greater than 1018. It is guar...
[ "3\n", "2\n", "4\n" ]
[ "14\n16\n46\n", "999999999999999998\n44500000000\n", "2\n17\n46\n97\n" ]
In the first sample case: On the first level, ZS the Coder pressed the ' + ' button 14 times (and the number on screen is initially 2), so the number became 2 + 14·1 = 16. Then, ZS the Coder pressed the '<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c77ded9b8209a8cb488cc2ec7b7fe1dae32a53...
[ { "input": "3", "output": "2\n17\n46" }, { "input": "2", "output": "2\n17" }, { "input": "4", "output": "2\n17\n46\n97" }, { "input": "1", "output": "2" }, { "input": "100000", "output": "2\n17\n46\n97\n176\n289\n442\n641\n892\n1201\n1574\n2017\n2536\n3137\n38...
342
9,113,600
3
11,552
343
Water Tree
[ "data structures", "dfs and similar", "graphs", "trees" ]
null
null
Mad scientist Mike has constructed a rooted tree, which consists of *n* vertices. Each vertex is a reservoir which can be either empty or filled with water. The vertices of the tree are numbered from 1 to *n* with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of ...
The first line of the input contains an integer *n* (1<=≤<=*n*<=≤<=500000) — the number of vertices in the tree. Each of the following *n*<=-<=1 lines contains two space-separated numbers *a**i*, *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*) — the edges of the tree. The next line contains a number *q* (1<=...
For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.
[ "5\n1 2\n5 1\n2 3\n4 2\n12\n1 1\n2 3\n3 1\n3 2\n3 3\n3 4\n1 2\n2 4\n3 1\n3 3\n3 4\n3 5\n" ]
[ "0\n0\n0\n1\n0\n1\n0\n1\n" ]
none
[ { "input": "5\n1 2\n5 1\n2 3\n4 2\n12\n1 1\n2 3\n3 1\n3 2\n3 3\n3 4\n1 2\n2 4\n3 1\n3 3\n3 4\n3 5", "output": "0\n0\n0\n1\n0\n1\n0\n1" }, { "input": "1\n1\n3 1", "output": "0" }, { "input": "2\n1 2\n13\n1 1\n3 1\n3 2\n2 1\n3 1\n3 2\n2 2\n3 1\n3 2\n1 1\n2 2\n3 1\n3 2", "output": "1\n1...
0
0
-1
11,670
711
Coloring Trees
[ "dp" ]
null
null
ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where *n* trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to *n* from left to right. Initially, tree *i* has color *c**i*. ZS the Coder and Chris the Baboon recognizes ...
The first line contains three integers, *n*, *m* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=100, 1<=≤<=*m*<=≤<=100) — the number of trees, number of colors and beauty of the resulting coloring respectively. The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (0<=≤<=*c**i*<=≤<=*m*), the initial colors of the trees. *c...
Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty *k*, print <=-<=1.
[ "3 2 2\n0 0 0\n1 2\n3 4\n5 6\n", "3 2 2\n2 1 2\n1 3\n2 4\n3 5\n", "3 2 2\n2 0 0\n1 3\n2 4\n3 5\n", "3 2 3\n2 1 2\n1 3\n2 4\n3 5\n" ]
[ "10", "-1", "5", "0" ]
In the first sample case, coloring the trees with colors 2, 1, 1 minimizes the amount of paint used, which equals to 2 + 3 + 5 = 10. Note that 1, 1, 1 would not be valid because the beauty of such coloring equals to 1 ({1, 1, 1} is a way to group the trees into a single group of the same color). In the second sample c...
[ { "input": "3 2 2\n0 0 0\n1 2\n3 4\n5 6", "output": "10" }, { "input": "3 2 2\n2 1 2\n1 3\n2 4\n3 5", "output": "-1" }, { "input": "3 2 2\n2 0 0\n1 3\n2 4\n3 5", "output": "5" }, { "input": "3 2 3\n2 1 2\n1 3\n2 4\n3 5", "output": "0" }, { "input": "3 2 3\n0 0 0\n...
1,606
57,651,200
3
11,699
913
Too Easy Problems
[ "binary search", "brute force", "data structures", "greedy", "sortings" ]
null
null
You are preparing for an exam on scheduling theory. The exam will last for exactly *T* milliseconds and will consist of *n* problems. You can either solve problem *i* in exactly *t**i* milliseconds or ignore it and spend no time. You don't need time to rest after solving a problem, either. Unfortunately, your teacher ...
The first line contains two integers *n* and *T* (1<=≤<=*n*<=≤<=2·105; 1<=≤<=*T*<=≤<=109) — the number of problems in the exam and the length of the exam in milliseconds, respectively. Each of the next *n* lines contains two integers *a**i* and *t**i* (1<=≤<=*a**i*<=≤<=*n*; 1<=≤<=*t**i*<=≤<=104). The problems are numb...
In the first line, output a single integer *s* — your maximum possible final score. In the second line, output a single integer *k* (0<=≤<=*k*<=≤<=*n*) — the number of problems you should solve. In the third line, output *k* distinct integers *p*1,<=*p*2,<=...,<=*p**k* (1<=≤<=*p**i*<=≤<=*n*) — the indexes of problems...
[ "5 300\n3 100\n4 150\n4 80\n2 90\n2 300\n", "2 100\n1 787\n2 788\n", "2 100\n2 42\n2 58\n" ]
[ "2\n3\n3 1 4\n", "0\n0\n\n", "2\n2\n1 2\n" ]
In the first example, you should solve problems 3, 1, and 4. In this case you'll spend 80 + 100 + 90 = 270 milliseconds, falling within the length of the exam, 300 milliseconds (and even leaving yourself 30 milliseconds to have a rest). Problems 3 and 1 will bring you a point each, while problem 4 won't. You'll score t...
[ { "input": "5 300\n3 100\n4 150\n4 80\n2 90\n2 300", "output": "2\n2\n3 4" }, { "input": "2 100\n1 787\n2 788", "output": "0\n0" }, { "input": "2 100\n2 42\n2 58", "output": "2\n2\n1 2" }, { "input": "1 1\n1 1", "output": "1\n1\n1" }, { "input": "10 481\n4 25\n3 8...
1,730
39,321,600
3
11,766
799
Cut the pie
[ "binary search", "data structures", "geometry" ]
null
null
Arkady reached the *n*-th level in Township game, so Masha decided to bake a pie for him! Of course, the pie has a shape of convex *n*-gon, i.e. a polygon with *n* vertices. Arkady decided to cut the pie in two equal in area parts by cutting it by a straight line, so that he can eat one of them and give the other to M...
The first line contains two integers *n* and *q* (3<=≤<=*n*<=≤<=104, 1<=≤<=*q*<=≤<=105) — the number of vertices in the pie and the number of queries. *n* line follow describing the polygon vertices in clockwise order. The *i*-th of these line contains two integers *x**i* and *y**i* (<=-<=106<=≤<=*x**i*,<=*y**i*<=≤<=1...
For each query print single integer — the polar angle of the line that is the answer for the corresponding query, in radians. The angle should be in the segment [0;π], the angles are measured from the direction of *OX* axis in counter-clockwise order. For example, the polar angle of the *OY* axis is . If there is no an...
[ "3 1\n0 0\n0 3\n3 0\n\n1 1\n", "5 3\n6 5\n6 3\n5 0\n0 0\n0 5\n\n5 4\n3 3\n5 2\n" ]
[ "2.67794504460098710000\n", "0.60228734612690049000\n1.27933953226473580000\n2.85805511179015910000\n" ]
none
[]
46
0
0
11,773
461
Appleman and a Sheet of Paper
[ "data structures", "implementation" ]
null
null
Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1<=×<=*n*. Your task is help Appleman with folding of such a sheet. Actually, you need to perform *q* queries. Each query will have one of the following types: 1. Fold the sheet of paper at position *p**i*. After this query the...
The first line contains two integers: *n* and *q* (1<=<=≤<=*n*<=≤<=105; 1<=≤<=*q*<=≤<=105) — the width of the paper and the number of queries. Each of the following *q* lines contains one of the described queries in the following format: - "1 *p**i*" (1<=≤<=*p**i*<=&lt;<=[*current* *width* *of* *sheet*]) — the first...
For each query of the second type, output the answer.
[ "7 4\n1 3\n1 2\n2 0 1\n2 1 2\n", "10 9\n2 2 9\n1 1\n2 0 1\n1 8\n2 0 8\n1 2\n2 1 3\n1 4\n2 2 4\n" ]
[ "4\n3\n", "7\n2\n10\n4\n5\n" ]
The pictures below show the shapes of the paper during the queries of the first example: After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.
[ { "input": "7 4\n1 3\n1 2\n2 0 1\n2 1 2", "output": "4\n3" }, { "input": "10 9\n2 2 9\n1 1\n2 0 1\n1 8\n2 0 8\n1 2\n2 1 3\n1 4\n2 2 4", "output": "7\n2\n10\n4\n5" }, { "input": "10 5\n2 1 9\n2 4 10\n1 1\n2 0 1\n2 0 1", "output": "8\n6\n2\n2" }, { "input": "10 10\n2 5 9\n2 2 9...
46
0
0
11,778
84
Biathlon
[ "binary search", "implementation" ]
C. Biathlon
1
256
Perhaps many have heard that the World Biathlon Championship has finished. Although our hero Valera was not present at this spectacular event himself and only watched it on TV, it excited him so much that he decided to enroll in a biathlon section. Of course, biathlon as any sport, proved very difficult in practice. I...
The first line of the input file contains the integer *n* (1<=≤<=*n*<=≤<=104), which is the number of targets. The next *n* lines contain descriptions of the targets. Each target is a circle whose center is located on the *Ox* axis. Each circle is given by its coordinate of the center *x* (<=-<=2·104<=≤<=*x*<=≤<=2·104)...
Print on the first line a single number, the number of targets hit by Valera. Print on the second line for each of the targets the number of its first hit or "-1" (without quotes) if this number does not exist. Separate numbers with spaces.
[ "3\n2 1\n5 2\n10 1\n5\n0 1\n1 3\n3 0\n4 0\n4 0\n", "3\n3 2\n7 1\n11 2\n4\n2 1\n6 0\n6 4\n11 2\n" ]
[ "2\n3 3 -1 \n", "3\n1 2 4 \n" ]
none
[ { "input": "3\n2 1\n5 2\n10 1\n5\n0 1\n1 3\n3 0\n4 0\n4 0", "output": "2\n3 3 -1 " }, { "input": "3\n3 2\n7 1\n11 2\n4\n2 1\n6 0\n6 4\n11 2", "output": "3\n1 2 4 " }, { "input": "2\n0 5\n10 5\n2\n7 2\n6 1", "output": "1\n-1 1 " }, { "input": "3\n-3 3\n-10 2\n10 2\n4\n10 2\n2 ...
1,000
10,035,200
0
11,789
0
none
[ "none" ]
null
null
Levko loves array *a*1,<=*a*2,<=... ,<=*a**n*, consisting of integers, very much. That is why Levko is playing with array *a*, performing all sorts of operations with it. Each operation Levko performs is of one of two types: 1. Increase all elements from *l**i* to *r**i* by *d**i*. In other words, perform assignments...
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=5000) — the size of the array and the number of operations in Levko's records, correspondingly. Next *m* lines describe the operations, the *i*-th line describes the *i*-th operation. The first integer in the *i*-th line is integer *t**i* (1<=≤<=*t*...
In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise. If the solution exists, then on the second line print *n* integers *a*1,<=*a*2,<=... ,<=*a**n* (|*a**i*|<=≤<=109) — the recovered array.
[ "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8\n", "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13\n" ]
[ "YES\n4 7 4 7", "NO\n" ]
none
[ { "input": "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 8", "output": "YES\n8 7 4 7 " }, { "input": "4 5\n1 2 3 1\n2 1 2 8\n2 3 4 7\n1 1 3 3\n2 3 4 13", "output": "NO" }, { "input": "97 29\n2 78 82 356152\n2 14 29 430177\n1 59 84 3680\n1 49 89 -2247\n1 92 96 3701\n2 54 89 377271\n1 62...
46
0
0
11,799
845
Driving Test
[ "data structures", "dp", "greedy" ]
null
null
Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types. - speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type); - overtake is allowed: this sign means that ...
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=2·105) — number of events. Each of the next *n* lines starts with integer *t* (1<=≤<=*t*<=≤<=6) — the type of the event. An integer *s* (1<=≤<=*s*<=≤<=300) follows in the query of the first and the third type (if it is the query of first type, then it's ne...
Print the minimal number of road signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view.
[ "11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300\n", "5\n1 100\n3 200\n2\n4\n5\n", "7\n1 20\n2\n6\n4\n6\n6\n2\n" ]
[ "2\n", "0\n", "2\n" ]
In the first example Polycarp should say he didn't notice the "speed limit" sign with the limit of 70 and the second "speed limit" sign with the limit of 120. In the second example Polycarp didn't make any rule violation. In the third example Polycarp should say he didn't notice both "no overtake allowed" that came a...
[ { "input": "11\n1 100\n3 70\n4\n2\n3 120\n5\n3 120\n6\n1 150\n4\n3 300", "output": "2" }, { "input": "5\n1 100\n3 200\n2\n4\n5", "output": "0" }, { "input": "7\n1 20\n2\n6\n4\n6\n6\n2", "output": "2" }, { "input": "1\n1 100", "output": "0" }, { "input": "2\n1 100\...
608
1,536,000
3
11,812
0
none
[ "none" ]
null
null
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces you have made *y* submissions, out of which *x* have been successful. Thus, your current success rate on Codeforces is equal to *x*<=/<=*y*. Your favorite rational number in the [0;1] range is *p*<=/<=*q*. Now you wonde...
The first line contains a single integer *t* (1<=≤<=*t*<=≤<=1000) — the number of test cases. Each of the next *t* lines contains four integers *x*, *y*, *p* and *q* (0<=≤<=*x*<=≤<=*y*<=≤<=109; 0<=≤<=*p*<=≤<=*q*<=≤<=109; *y*<=&gt;<=0; *q*<=&gt;<=0). It is guaranteed that *p*<=/<=*q* is an irreducible fraction. Hacks...
For each test case, output a single integer equal to the smallest number of submissions you have to make if you want your success rate to be equal to your favorite rational number, or -1 if this is impossible to achieve.
[ "4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1\n" ]
[ "4\n10\n0\n-1\n" ]
In the first example, you have to make 4 successful submissions. Your success rate will be equal to 7 / 14, or 1 / 2. In the second example, you have to make 2 successful and 8 unsuccessful submissions. Your success rate will be equal to 9 / 24, or 3 / 8. In the third example, there is no need to make any new submiss...
[ { "input": "4\n3 10 1 2\n7 14 3 8\n20 70 2 7\n5 6 1 1", "output": "4\n10\n0\n-1" }, { "input": "8\n0 1 0 1\n0 2 1 2\n0 3 1 1\n1 2 0 1\n1 2 1 1\n2 2 0 1\n3 3 1 2\n4 4 1 1", "output": "0\n2\n-1\n-1\n-1\n-1\n3\n0" }, { "input": "5\n1 1000000000 1 2\n1 1000000000 1 2\n1 1000000000 1 2\n1 100...
124
7,065,600
-1
11,818
0
none
[ "none" ]
null
null
Vasya's telephone contains *n* photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo *n*. Similarly, by swiping right from the last photo you reach photo 1. It takes *...
The first line of the input contains 4 integers *n*,<=*a*,<=*b*,<=*T* (1<=≤<=*n*<=≤<=5·105, 1<=≤<=*a*,<=*b*<=≤<=1000, 1<=≤<=*T*<=≤<=109) — the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a ...
Output the only integer, the maximum number of photos Vasya is able to watch during those *T* seconds.
[ "4 2 3 10\nwwhw\n", "5 2 4 13\nhhwhh\n", "5 2 4 1000\nhhwhh\n", "3 1 100 10\nwhw\n" ]
[ "2\n", "4\n", "5\n", "0\n" ]
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first phot...
[ { "input": "4 2 3 10\nwwhw", "output": "2" }, { "input": "5 2 4 13\nhhwhh", "output": "4" }, { "input": "5 2 4 1000\nhhwhh", "output": "5" }, { "input": "3 1 100 10\nwhw", "output": "0" }, { "input": "10 2 3 32\nhhwwhwhwwh", "output": "7" }, { "input":...
0
0
-1
11,821
463
Caisa and Sugar
[ "brute force", "implementation" ]
null
null
Caisa is going to have a party and he needs to buy the ingredients for a big chocolate cake. For that he is going to the biggest supermarket in town. Unfortunately, he has just *s* dollars for sugar. But that's not a reason to be sad, because there are *n* types of sugar in the supermarket, maybe he able to buy one. B...
The first line contains two space-separated integers *n*,<=*s* (1<=≤<=*n*,<=*s*<=≤<=100). The *i*-th of the next *n* lines contains two integers *x**i*, *y**i* (1<=≤<=*x**i*<=≤<=100; 0<=≤<=*y**i*<=&lt;<=100), where *x**i* represents the number of dollars and *y**i* the number of cents needed in order to buy the *i*-th...
Print a single integer representing the maximum number of sweets he can buy, or -1 if he can't buy any type of sugar.
[ "5 10\n3 90\n12 0\n9 70\n5 50\n7 0\n", "5 5\n10 10\n20 20\n30 30\n40 40\n50 50\n" ]
[ "50\n", "-1\n" ]
In the first test sample Caisa can buy the fourth type of sugar, in such a case he will take 50 sweets as a change.
[ { "input": "5 10\n3 90\n12 0\n9 70\n5 50\n7 0", "output": "50" }, { "input": "5 5\n10 10\n20 20\n30 30\n40 40\n50 50", "output": "-1" }, { "input": "1 2\n1 0", "output": "0" }, { "input": "2 10\n20 99\n30 99", "output": "-1" }, { "input": "15 21\n16 51\n33 44\n32 ...
30
0
0
11,825
22
Bargaining Table
[ "brute force", "dp" ]
B. Bargaining Table
2
256
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room *n*<=×<=*m* meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, t...
The first line contains 2 space-separated numbers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=25) — the office room dimensions. Then there follow *n* lines with *m* characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in ...
Output one number — the maximum possible perimeter of a bargaining table for Bob's office room.
[ "3 3\n000\n010\n000\n", "5 4\n1100\n0000\n0000\n0000\n0000\n" ]
[ "8\n", "16\n" ]
none
[ { "input": "3 3\n000\n010\n000", "output": "8" }, { "input": "5 4\n1100\n0000\n0000\n0000\n0000", "output": "16" }, { "input": "3 3\n000\n110\n000", "output": "8" }, { "input": "4 2\n00\n10\n11\n00", "output": "6" }, { "input": "3 5\n00001\n00000\n10100", "out...
342
26,009,600
3.866053
11,829
272
Dima and Sequence
[ "implementation", "math" ]
null
null
Dima got into number sequences. Now he's got sequence *a*1,<=*a*2,<=...,<=*a**n*, consisting of *n* positive integers. Also, Dima has got a function *f*(*x*), which can be defined with the following recurrence: - *f*(0)<==<=0; - *f*(2·*x*)<==<=*f*(*x*); - *f*(2·*x*<=+<=1)<==<=*f*(*x*)<=+<=1. Dima wonders, how many...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105). The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109). The numbers in the lines are separated by single spaces.
In a single line print the answer to the problem. Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "3\n1 2 4\n", "3\n5 3 1\n" ]
[ "3\n", "1\n" ]
In the first sample any pair (*i*, *j*) will do, so the answer is 3. In the second sample only pair (1, 2) will do.
[ { "input": "3\n1 2 4", "output": "3" }, { "input": "3\n5 3 1", "output": "1" }, { "input": "2\n469264357 996569493", "output": "0" }, { "input": "6\n396640239 62005863 473635171 329666981 510631133 207643327", "output": "2" }, { "input": "8\n851991424 32517099 310...
154
0
0
11,835
659
New Reform
[ "data structures", "dfs and similar", "dsu", "graphs", "greedy" ]
null
null
Berland has *n* cities connected by *m* bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads. The President of Berland decided to make changes to the roa...
The first line of the input contains two positive integers, *n* and *m* — the number of the cities and the number of roads in Berland (2<=≤<=*n*<=≤<=100<=000, 1<=≤<=*m*<=≤<=100<=000). Next *m* lines contain the descriptions of the roads: the *i*-th road is determined by two distinct integers *x**i*,<=*y**i* (1<=≤<=*x...
Print a single integer — the minimum number of separated cities after the reform.
[ "4 3\n2 1\n1 3\n4 3\n", "5 5\n2 1\n1 3\n2 3\n2 5\n4 3\n", "6 5\n1 2\n2 3\n4 5\n4 6\n5 6\n" ]
[ "1\n", "0\n", "1\n" ]
In the first sample the following road orientation is allowed: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/04481ace...
[ { "input": "4 3\n2 1\n1 3\n4 3", "output": "1" }, { "input": "5 5\n2 1\n1 3\n2 3\n2 5\n4 3", "output": "0" }, { "input": "6 5\n1 2\n2 3\n4 5\n4 6\n5 6", "output": "1" }, { "input": "4 4\n1 2\n2 3\n3 4\n4 1", "output": "0" }, { "input": "10 45\n3 5\n2 3\n4 8\n2 5\n...
998
43,417,600
3
11,837
280
Game on Tree
[ "implementation", "math", "probabilities", "trees" ]
null
null
Momiji has got a rooted tree, consisting of *n* nodes. The tree nodes are numbered by integers from 1 to *n*. The root has number 1. Momiji decided to play a game on this tree. The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let's denote it by *v*) and removes all the ...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of nodes in the tree. The next *n*<=-<=1 lines contain the tree edges. The *i*-th line contains integers *a**i*, *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*) — the numbers of the nodes that are connected by the *i*-th edge. It is guarant...
Print a single real number — the expectation of the number of steps in the described game. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=6.
[ "2\n1 2\n", "3\n1 2\n1 3\n" ]
[ "1.50000000000000000000\n", "2.00000000000000000000\n" ]
In the first sample, there are two cases. One is directly remove the root and another is remove the root after one step. Thus the expected steps are: In the second sample, things get more complex. There are two cases that reduce to the first sample, and one case cleaned at once. Thus the expected steps are:
[ { "input": "2\n1 2", "output": "1.50000000000000000000" }, { "input": "3\n1 2\n1 3", "output": "2.00000000000000000000" }, { "input": "10\n1 2\n2 3\n3 4\n1 5\n2 6\n6 7\n4 8\n6 9\n9 10", "output": "3.81666666666666690000" }, { "input": "6\n1 3\n2 4\n5 6\n3 6\n5 4", "output...
1,000
10,342,400
0
11,840
150
Smart Cheater
[ "data structures", "math", "probabilities" ]
null
null
I guess there's not much point in reminding you that Nvodsk winters aren't exactly hot. That increased the popularity of the public transport dramatically. The route of bus 62 has exactly *n* stops (stop 1 goes first on its way and stop *n* goes last). The stops are positioned on a straight line and their coordinates a...
The first line contains three integers *n*, *m* and *c* (2<=≤<=*n*<=≤<=150<=000, 1<=≤<=*m*<=≤<=300<=000, 1<=≤<=*c*<=≤<=10<=000). The next line contains *n* integers *x**i* (0<=≤<=*x**i*<=≤<=109, *x*1<==<=0, *x**i*<=&lt;<=*x**i*<=+<=1) — the coordinates of the stops on the bus's route. The third line contains *n*<=-<=...
Print the single real number — the maximum expectation of the conductor's profit. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, i...
[ "3 3 10\n0 10 100\n100 0\n1 2\n2 3\n1 3\n", "10 8 187\n0 10 30 70 150 310 630 1270 2550 51100\n13 87 65 0 100 44 67 3 4\n1 10\n2 9\n3 8\n1 5\n6 10\n2 7\n4 10\n4 5\n" ]
[ "90.000000000\n", "76859.990000000\n" ]
A comment to the first sample: The first and third passengers get tickets from stop 1 to stop 2. The second passenger doesn't get a ticket. There always is inspection on the segment 1-2 but both passengers have the ticket for it. There never is an inspection on the segment 2-3, that's why the second passenger gets awa...
[]
46
0
0
11,868
691
Xor-sequences
[ "matrices" ]
null
null
You are given *n* integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n*. A sequence of integers *x*1,<=<=*x*2,<=<=...,<=<=*x**k* is called a "xor-sequence" if for every 1<=<=≤<=<=*i*<=<=≤<=<=*k*<=-<=1 the number of ones in the binary representation of the number *x**i* *x**i*<=<=+<=<=1's is a multiple of 3 and for all 1<=≤<=*i*...
The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=1018) — the number of given integers and the length of the "xor-sequences". The second line contains *n* integers *a**i* (0<=≤<=*a**i*<=≤<=1018).
Print the only integer *c* — the number of "xor-sequences" of length *k* modulo 109<=+<=7.
[ "5 2\n15 1 2 4 8\n", "5 1\n15 1 2 4 8\n" ]
[ "13\n", "5\n" ]
none
[ { "input": "5 2\n15 1 2 4 8", "output": "13" }, { "input": "5 1\n15 1 2 4 8", "output": "5" }, { "input": "10 1\n44 65 23 44 100 19 19 23 19 40", "output": "10" }, { "input": "10 2\n93 93 85 48 44 98 93 100 98 98", "output": "52" }, { "input": "10 100\n22 0 41 63 ...
3,000
29,491,200
0
11,877
952
I'm Feeling Lucky!
[ "probabilities" ]
null
null
You have one chip and one chance to play roulette. Are you feeling lucky?
none
Print your bet. Your chip must be placed entirely within some square (not on an edge or a corner shared by adjacent squares).
[]
[]
none
[ { "input": "1", "output": "Red" }, { "input": "2", "output": "Red" } ]
61
0
0
11,901
929
Места в самолёте
[ "*special", "implementation" ]
null
null
В самолёте есть *n* рядов мест. Если смотреть на ряды сверху, то в каждом ряду есть 3 места слева, затем проход между рядами, затем 4 центральных места, затем ещё один проход между рядами, а затем ещё 3 места справа. Известно, что некоторые места уже заняты пассажирами. Всего есть два вида пассажиров — статусные (те, ...
В первой строке следуют два целых числа *n* и *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=10·*n*) — количество рядов мест в самолёте и количество пассажиров, которых нужно рассадить. Далее следует описание рядов мест самолёта по одному ряду в строке. Если очередной символ равен '-', то это проход между рядами. Если очередно...
В первую строку выведите минимальное суммарное число соседей у статусных пассажиров. Далее выведите план рассадки пассажиров, который минимизирует суммарное количество соседей у статусных пассажиров, в том же формате, что и во входных данных. Если в свободное место нужно посадить одного из *k* пассажиров, выведите стр...
[ "1 2\nSP.-SS.S-S.S\n", "4 9\nPP.-PPPS-S.S\nPSP-PPSP-.S.\n.S.-S..P-SS.\nP.S-P.PP-PSP\n" ]
[ "5\nSPx-SSxS-S.S\n", "15\nPPx-PPPS-S.S\nPSP-PPSP-xSx\nxSx-SxxP-SSx\nP.S-PxPP-PSP\n" ]
В первом примере нужно посадить ещё двух обычных пассажиров. Для минимизации соседей у статусных пассажиров, нужно посадить первого из них на третье слева место, а второго на любое из оставшихся двух мест, так как независимо от выбора места он станет соседом двух статусных пассажиров. Изначально, у статусного пассажи...
[ { "input": "1 2\nSP.-SS.S-S.S", "output": "5\nSPx-SSxS-S.S" }, { "input": "4 9\nPP.-PPPS-S.S\nPSP-PPSP-.S.\n.S.-S..P-SS.\nP.S-P.PP-PSP", "output": "15\nPPx-PPPS-S.S\nPSP-PPSP-xSx\nxSx-SxxP-SSx\nP.S-PxPP-PSP" }, { "input": "3 7\n.S.-SSSP-..S\nS..-.SPP-S.P\n.S.-PPPP-PSP", "output": "13...
62
5,734,400
3
11,924
908
New Year and Curling
[ "brute force", "geometry", "implementation", "math" ]
null
null
Carol is currently curling. She has *n* disks each with radius *r* on the 2D plane. Initially she has all these disks above the line *y*<==<=10100. She then will slide the disks towards the line *y*<==<=0 one by one in order from 1 to *n*. When she slides the *i*-th disk, she will place its center at the point (*...
The first line will contain two integers *n* and *r* (1<=≤<=*n*,<=*r*<=≤<=1<=000), the number of disks, and the radius of the disks, respectively. The next line will contain *n* integers *x*1,<=*x*2,<=...,<=*x**n* (1<=≤<=*x**i*<=≤<=1<=000) — the *x*-coordinates of the disks.
Print a single line with *n* numbers. The *i*-th number denotes the *y*-coordinate of the center of the *i*-th disk. The output will be accepted if it has absolute or relative error at most 10<=-<=6. Namely, let's assume that your answer for a particular value of a coordinate is *a* and the answer of the jury is *b*. ...
[ "6 2\n5 5 6 8 3 12\n" ]
[ "2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613\n" ]
The final positions of the disks will look as follows: In particular, note the position of the last disk.
[ { "input": "6 2\n5 5 6 8 3 12", "output": "2 6.0 9.87298334621 13.3370849613 12.5187346573 13.3370849613" }, { "input": "1 1\n5", "output": "1" }, { "input": "5 300\n939 465 129 611 532", "output": "300 667.864105343 1164.9596696 1522.27745533 2117.05388391" }, { "input": "5 ...
46
5,529,600
0
11,926
30
Shooting Gallery
[ "dp", "probabilities" ]
C. Shooting Gallery
2
256
One warm and sunny day king Copa decided to visit the shooting gallery, located at the Central Park, and try to win the main prize — big pink plush panda. The king is not good at shooting, so he invited you to help him. The shooting gallery is an infinite vertical plane with Cartesian coordinate system on it. The targ...
The first line contains integer *n* (1<=≤<=*n*<=≤<=1000) — amount of targets in the shooting gallery. Then *n* lines follow, each describing one target. Each description consists of four numbers *x**i*, *y**i*, *t**i*, *p**i* (where *x**i*, *y**i*, *t**i* — integers, <=-<=1000<=≤<=*x**i*,<=*y**i*<=≤<=1000,<=0<=≤<=*t**i...
Output the maximum expected value of the amount of targets that was shot by the king. Your answer will be accepted if it differs from the correct answer by not more than 10<=-<=6.
[ "1\n0 0 0 0.5\n", "2\n0 0 0 0.6\n5 0 5 0.7\n" ]
[ "0.5000000000\n", "1.3000000000\n" ]
none
[ { "input": "1\n0 0 0 0.5", "output": "0.5000000000" }, { "input": "2\n0 0 0 0.6\n5 0 5 0.7", "output": "1.3000000000" }, { "input": "1\n-5 2 3 0.886986", "output": "0.8869860000" }, { "input": "4\n10 -7 14 0.926305\n-7 -8 12 0.121809\n-7 7 14 0.413446\n3 -8 6 0.859061", "...
216
204,800
-1
11,957
132
Turing Tape
[ "implementation" ]
null
null
INTERCAL is the oldest of esoteric programming languages. One of its many weird features is the method of character-based output, known as Turing Tape method. It converts an array of unsigned 8-bit integers into a sequence of characters to print, using the following method. The integers of the array are processed one ...
The input will consist of a single line *text* which contains the message printed using the described method. String *text* will contain between 1 and 100 characters, inclusive. ASCII-code of each character of *text* will be between 32 (space) and 126 (tilde), inclusive.
Output the initial array, which was used to produce *text*, one integer per line.
[ "Hello, World!\n" ]
[ "238\n108\n112\n0\n64\n194\n48\n26\n244\n168\n24\n16\n162\n" ]
Let's have a closer look at the beginning of the example. The first character is "H" with ASCII-code 72 = 01001000<sub class="lower-index">2</sub>. Its reverse is 00010010<sub class="lower-index">2</sub> = 18, and this number should become the result of the second step of processing. The result of the first step is con...
[ { "input": "Hello, World!", "output": "238\n108\n112\n0\n64\n194\n48\n26\n244\n168\n24\n16\n162" }, { "input": "N", "output": "142" }, { "input": "!{|aPUBKs[k\"HE;>O&(Nf}N4,*#g*<3sQXFJ'?Z/H9L[xx Rc5\"8~v}84+wv]w[oO0e':MaNy&6*]jRkYomz[o?=13Y?!fzA3eC\\", "output": "124\n166\n160\n184\n...
154
2,150,400
-1
12,000
630
Moore's Law
[ "math" ]
null
null
The city administration of IT City decided to fix up a symbol of scientific and technical progress in the city's main square, namely an indicator board that shows the effect of Moore's law in real time. Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately ev...
The only line of the input contains a pair of integers *n* (1000<=≤<=*n*<=≤<=10 000) and *t* (0<=≤<=*t*<=≤<=2 000 000 000) — the number of transistors in the initial time and the number of seconds passed since the initial time.
Output one number — the estimate of the number of transistors in a dence integrated circuit in *t* seconds since the initial time. The relative error of your answer should not be greater than 10<=-<=6.
[ "1000 1000000\n" ]
[ "1011.060722383550382782399454922040\n" ]
none
[ { "input": "1000 1000000", "output": "1011.060722383550382782399454922040" }, { "input": "1000 0", "output": "1000" }, { "input": "1000 1", "output": "1000.000011000" }, { "input": "1000 2", "output": "1000.000022000000121000" }, { "input": "10000 3", "output"...
31
0
3
12,055
734
Anton and Digits
[ "brute force", "greedy", "implementation", "math" ]
null
null
Recently Anton found a box with digits in his room. There are *k*2 digits 2, *k*3 digits 3, *k*5 digits 5 and *k*6 digits 6. Anton's favorite integers are 32 and 256. He decided to compose this integers from digits he has. He wants to make the sum of these integers as large as possible. Help him solve this task! Each...
The only line of the input contains four integers *k*2, *k*3, *k*5 and *k*6 — the number of digits 2, 3, 5 and 6 respectively (0<=≤<=*k*2,<=*k*3,<=*k*5,<=*k*6<=≤<=5·106).
Print one integer — maximum possible sum of Anton's favorite integers that can be composed using digits from the box.
[ "5 1 3 4\n", "1 1 1 1\n" ]
[ "800\n", "256\n" ]
In the first sample, there are five digits 2, one digit 3, three digits 5 and four digits 6. Anton can compose three integers 256 and one integer 32 to achieve the value 256 + 256 + 256 + 32 = 800. Note, that there is one unused integer 2 and one unused integer 6. They are not counted in the answer. In the second samp...
[ { "input": "5 1 3 4", "output": "800" }, { "input": "1 1 1 1", "output": "256" }, { "input": "10 2 1 5", "output": "320" }, { "input": "4 2 7 2", "output": "576" }, { "input": "489 292 127 263", "output": "41856" }, { "input": "9557 5242 1190 7734", ...
46
0
3
12,059
105
Lift and Throw
[ "brute force" ]
E. Lift and Throw
1
256
You are given a straight half-line divided into segments of unit length, which we will call positions. The positions are numbered by positive integers that start with 1 from the end of half-line, i. e. 1, 2, 3 and so on. The distance between the positions is the absolute difference between the respective numbers. Lah...
The first line contains three integers: Laharl's position, his movement range and throwing range. The second and the third lines describe Etna's and Flonne's parameters correspondingly in the similar form. It is guaranteed that the three characters occupy distinct positions. All numbers in the input are between 1 and 1...
Print a single number — the maximum ordinal number of position which either Laharl, Etna or Flonne can reach.
[ "9 3 3\n4 3 1\n2 3 3\n" ]
[ "15" ]
Let us explain how to reach position 15 in the sample. Initially Laharl occupies position 9, Etna — position 4 and Flonne — position 2. First Laharl moves to position 6. Then Flonne moves to position 5 and grabs Etna. Laharl grabs Flonne and throws to position 9. Flonne throws Etna to position 12. Etna moves to p...
[]
46
0
0
12,074
141
Hopscotch
[ "geometry", "math" ]
null
null
So nearly half of the winter is over and Maria is dreaming about summer. She's fed up with skates and sleds, she was dreaming about Hopscotch all night long. It's a very popular children's game. The game field, the court, looks as is shown in the figure (all blocks are square and are numbered from bottom to top, blocks...
The only input line contains three integers: *a*, *x*, *y*, where *a* (1<=≤<=*a*<=≤<=100) is the side of the square, *x* and *y* (<=-<=106<=≤<=*x*<=≤<=106,<=0<=≤<=*y*<=≤<=106) are coordinates of the stone.
Print the number of the square, inside which the stone fell. If the stone is on a border of some stone or outside the court, print "-1" without the quotes.
[ "1 0 0\n", "3 1 1\n", "3 0 10\n", "3 0 7\n", "3 4 0\n" ]
[ "-1\n", "1\n", "5\n", "-1\n", "-1\n" ]
none
[ { "input": "1 0 0", "output": "-1" }, { "input": "3 1 1", "output": "1" }, { "input": "3 0 10", "output": "5" }, { "input": "3 0 7", "output": "-1" }, { "input": "3 4 0", "output": "-1" }, { "input": "9 3 2", "output": "1" }, { "input": "10...
248
8,908,800
3
12,076
0
none
[ "none" ]
null
null
Students went into a class to write a test and sat in some way. The teacher thought: "Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students that were neighbors are not neighbors in a new seating." The class can be represented as a matrix with *n* rows and *m*...
The only line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105; *n*·*m*<=≤<=105) — the number of rows and the number of columns in the required matrix.
If there is no such matrix, output "NO" (without quotes). Otherwise in the first line output "YES" (without quotes), and in the next *n* lines output *m* integers which form the required matrix.
[ "2 4\n", "2 1\n" ]
[ "YES\n5 4 7 2 \n3 6 1 8 \n", "NO\n" ]
In the first test case the matrix initially looks like this: It's easy to see that there are no two students that are adjacent in both matrices. In the second test case there are only two possible seatings and in both of them students with numbers 1 and 2 are neighbors.
[ { "input": "2 4", "output": "YES\n5 4 7 2 \n3 6 1 8 " }, { "input": "2 1", "output": "NO" }, { "input": "1 1", "output": "YES\n1" }, { "input": "1 2", "output": "NO" }, { "input": "1 3", "output": "NO" }, { "input": "2 2", "output": "NO" }, { ...
0
0
-1
12,095
142
Help Caretaker
[ "brute force", "dp" ]
null
null
Autumn came late to the kingdom of Far Far Away. The harvest was exuberant and it is now time to get ready for the winter. As most people celebrate the Harvest festival, Simon the Caretaker tries to solve a very non-trivial task of how to find place for the agricultural equipment in the warehouse. He's got problems wi...
The only line contains two space-separated integers *n* and *m* — the sizes of the warehouse (1<=≤<=*n*,<=*m*<=≤<=9).
In the first line print the maximum number of turboplows that can be positioned in the warehouse. In each of the next *n* lines print *m* characters. Use "." (dot) to mark empty space and use successive capital Latin letters ("A" for the first turboplow, "B" for the second one and so on until you reach the number of tu...
[ "3 3\n", "5 6\n", "2 2\n" ]
[ "1\nAAA\n.A.\n.A.\n", "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D\n", "0\n..\n..\n" ]
none
[ { "input": "3 3", "output": "1\nAAA\n.A.\n.A." }, { "input": "5 6", "output": "4\nA..C..\nAAAC..\nABCCCD\n.B.DDD\nBBB..D" }, { "input": "2 2", "output": "0\n..\n.." }, { "input": "4 2", "output": "0\n..\n..\n..\n.." }, { "input": "3 4", "output": "1\nA...\nAAA...
0
0
-1
12,098
476
Dreamoon and Strings
[ "dp", "strings" ]
null
null
Dreamoon has a string *s* and a pattern string *p*. He first removes exactly *x* characters from *s* obtaining string *s*' as a result. Then he calculates that is defined as the maximal number of non-overlapping substrings equal to *p* that can be found in *s*'. He wants to make this number as big as possible. More f...
The first line of the input contains the string *s* (1<=≤<=|*s*|<=≤<=2<=000). The second line of the input contains the string *p* (1<=≤<=|*p*|<=≤<=500). Both strings will only consist of lower case English letters.
Print |*s*|<=+<=1 space-separated integers in a single line representing the for all *x* from 0 to |*s*|.
[ "aaaaa\naa\n", "axbaxxb\nab\n" ]
[ "2 2 1 1 0 0\n", "0 1 1 2 1 1 0 0\n" ]
For the first sample, the corresponding optimal values of *s*' after removal 0 through |*s*| = 5 characters from *s* are {"aaaaa", "aaaa", "aaa", "aa", "a", ""}. For the second sample, possible corresponding optimal values of *s*' are {"axbaxxb", "abaxxb", "axbab", "abab", "aba", "ab", "a", ""}.
[ { "input": "aaaaa\naa", "output": "2 2 1 1 0 0" }, { "input": "axbaxxb\nab", "output": "0 1 1 2 1 1 0 0" }, { "input": "aabb\nab", "output": "1 1 1 0 0" }, { "input": "aaaaaaaaaaaaaaa\na", "output": "15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0" }, { "input": "aaaaaaaaaa...
171
20,172,800
3
12,100
962
Merge Equals
[ "data structures", "implementation" ]
null
null
You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value $x$ that occurs in the array $2$ or more times. Take the first two occurrences of $x$ in this array (the two leftmost occurrences). Remove the left of these two...
The first line contains a single integer $n$ ($2 \le n \le 150\,000$) — the number of elements in the array. The second line contains a sequence from $n$ elements $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$) — the elements of the array.
In the first line print an integer $k$ — the number of elements in the array after all the performed operations. In the second line print $k$ integers — the elements of the array after all the performed operations.
[ "7\n3 4 1 2 2 1 1\n", "5\n1 1 3 1 1\n", "5\n10 40 20 50 30\n" ]
[ "4\n3 8 2 1 \n", "2\n3 4 \n", "5\n10 40 20 50 30 \n" ]
The first two examples were considered in the statement. In the third example all integers in the given array are distinct, so it will not change.
[ { "input": "7\n3 4 1 2 2 1 1", "output": "4\n3 8 2 1 " }, { "input": "5\n1 1 3 1 1", "output": "2\n3 4 " }, { "input": "5\n10 40 20 50 30", "output": "5\n10 40 20 50 30 " }, { "input": "100\n10 10 15 12 15 13 15 12 10 10 15 11 13 14 13 14 10 13 12 10 14 12 13 11 14 15 12 11 1...
30
0
0
12,118
523
Name Quest
[ "*special", "greedy" ]
null
null
A Martian boy is named *s* — he has got this name quite recently from his parents for his coming of age birthday. Now he enjoys looking for his name everywhere. If he sees that he can obtain his name from some string by removing zero or more letters (at that, the remaining letters remain in the same order), he gets hap...
The first line contains string *s*, consisting of lowercase English letters. The length of string *s* is from 1 to 1000 letters. The second line contains string *t*, that also consists of lowercase English letters. The length of string *t* is from 1 to 106 letters.
Print the sought number of ways to cut string *t* in two so that each part made *s* happy.
[ "aba\nbaobababbah\n", "mars\nsunvenusearthmarsjupitersaturnuranusneptune\n" ]
[ "2\n", "0\n" ]
none
[ { "input": "aba\nbaobababbah", "output": "2" }, { "input": "mars\nsunvenusearthmarsjupitersaturnuranusneptune", "output": "0" }, { "input": "rry\nsorrymercuryismissedabove", "output": "3" }, { "input": "a\naaaaaaaaaaaaaa", "output": "13" }, { "input": "abcaba\nabc...
265
268,390,400
0
12,128
690
Photographs (I)
[]
null
null
The Human-Cow Confederation (*HC*2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated gate which performs a kind of a Turing test: it shows the entering creature a photograph and asks them whether the top and bottom halves ...
The first line of the input contains the number *q* of questions (1<=≤<=*q*<=≤<=220). After that, *q* questions follow, each of which in the format described below. The first line of every question contains two space-separated integers *h* and *w* (1<=≤<=*h*,<=*w*<=≤<=600) – the height (number of rows) and width (numb...
Your program should print *q* lines. The *i*-th line should contain your answer for the *i*-th question: YES if the photograph has been rearranged and NO otherwise. Your answers will be accepted if they all conform to this format and if at least 75% of them are correct. Because the input is rather huge, feel free to p...
[]
[]
The link to download all necessary files is http://assets.codeforces.com/files/690/easy_contestant_package.zip
[]
46
4,915,200
3
12,144
825
String Compression
[ "dp", "hashing", "string suffix structures", "strings" ]
null
null
Ivan wants to write a letter to his friend. The letter is a string *s* consisting of lowercase Latin letters. Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string *s* instea...
The only line of input contains one string *s* consisting of lowercase Latin letters (1<=≤<=|*s*|<=≤<=8000).
Output one integer number — the minimum possible length of a compressed version of *s*.
[ "aaaaaaaaaa\n", "abcab\n", "cczabababab\n" ]
[ "3\n", "6\n", "7\n" ]
In the first example Ivan will choose this compressed version: *c*<sub class="lower-index">1</sub> is 10, *s*<sub class="lower-index">1</sub> is a. In the second example Ivan will choose this compressed version: *c*<sub class="lower-index">1</sub> is 1, *s*<sub class="lower-index">1</sub> is abcab. In the third examp...
[ { "input": "aaaaaaaaaa", "output": "3" }, { "input": "abcab", "output": "6" }, { "input": "cczabababab", "output": "7" }, { "input": "kbyjorwqjk", "output": "11" }, { "input": "baaabbbaba", "output": "9" }, { "input": "aaaaaaaaaa", "output": "3" ...
2,000
450,150,400
0
12,232
38
Queue
[ "data structures" ]
G. Queue
2
256
On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Va...
The first input line contains an integer *n* which is the number of people who has joined the queue (1<=≤<=*n*<=≤<=105). In the next *n* lines descriptions of the people are given in order of their coming — space-separated integers *a**i* and *c**i* (1<=≤<=*a**i*<=≤<=*n*, 0<=≤<=*c**i*<=≤<=*n*). Every description is loc...
Output the permutation of numbers from 1 to *n*, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the *i*-th number stands for the number of a person who will stand in line on the place number *i* after the swaps ends. People are numbere...
[ "2\n1 0\n2 1\n", "3\n1 3\n2 3\n3 3\n", "5\n2 3\n1 4\n4 3\n3 1\n5 2\n" ]
[ "2 1 ", "3 2 1 ", "3 1 5 4 2 " ]
none
[]
61
0
0
12,288
605
Lazy Student
[ "constructive algorithms", "data structures", "graphs" ]
null
null
Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph — something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following defin...
The first line of the input contains two integers *n* and *m* () — the number of vertices and the number of edges in the graph. Each of the next *m* lines describes an edge of the graph and consists of two integers *a**j* and *b**j* (1<=≤<=*a**j*<=≤<=109,<=*b**j*<==<={0,<=1}). The first of these numbers is the weight ...
If Vladislav has made a mistake and such graph doesn't exist, print <=-<=1. Otherwise print *m* lines. On the *j*-th line print a pair of vertices (*u**j*,<=*v**j*) (1<=≤<=*u**j*,<=*v**j*<=≤<=*n*,<=*u**j*<=≠<=*v**j*), that should be connected by the *j*-th edge. The edges are numbered in the same order as in the input...
[ "4 5\n2 1\n3 1\n4 0\n1 1\n5 0\n", "3 3\n1 0\n2 1\n3 1\n" ]
[ "2 4\n1 4\n3 4\n3 1\n3 2\n", "-1\n" ]
none
[ { "input": "4 5\n2 1\n3 1\n4 0\n1 1\n5 0", "output": "2 4\n1 4\n3 4\n3 1\n3 2" }, { "input": "3 3\n1 0\n2 1\n3 1", "output": "-1" }, { "input": "2 1\n7 1", "output": "1 2" }, { "input": "3 2\n8 1\n9 1", "output": "1 2\n1 3" }, { "input": "3 3\n4 1\n5 0\n7 1", ...
109
307,200
0
12,345
933
A Determined Cleanup
[ "math" ]
null
null
In order to put away old things and welcome a fresh new year, a thorough cleaning of the house is a must. Little Tommy finds an old polynomial and cleaned it up by taking it modulo another. But now he regrets doing this... Given two integers *p* and *k*, find a polynomial *f*(*x*) with non-negative integer coefficien...
The only line of input contains two space-separated integers *p* and *k* (1<=≤<=*p*<=≤<=1018, 2<=≤<=*k*<=≤<=2<=000).
If the polynomial does not exist, print a single integer -1, or output two lines otherwise. In the first line print a non-negative integer *d* — the number of coefficients in the polynomial. In the second line print *d* space-separated integers *a*0,<=*a*1,<=...,<=*a**d*<=-<=1, describing a polynomial fulfilling the...
[ "46 2\n", "2018 214\n" ]
[ "7\n0 1 0 0 1 1 1\n", "3\n92 205 1\n" ]
In the first example, *f*(*x*) = *x*<sup class="upper-index">6</sup> + *x*<sup class="upper-index">5</sup> + *x*<sup class="upper-index">4</sup> + *x* = (*x*<sup class="upper-index">5</sup> - *x*<sup class="upper-index">4</sup> + 3*x*<sup class="upper-index">3</sup> - 6*x*<sup class="upper-index">2</sup> + 12*x* - 23)·...
[ { "input": "46 2", "output": "7\n0 1 0 0 1 1 1" }, { "input": "2018 214", "output": "3\n92 205 1" }, { "input": "4 2", "output": "3\n0 0 1" }, { "input": "5 2", "output": "3\n1 0 1" }, { "input": "10 3", "output": "3\n1 0 1" }, { "input": "250 1958", ...
77
19,968,000
3
12,353
32
Flea
[ "math" ]
C. Flea
2
256
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to *s* centimeters. A flea has found herself at the center of some cell of the checked board of the size *n*<=×<=*m* centimeters (each cell is 1<=×<=1 centimeters). She can jump as she wishes for an a...
The first line contains three integers *n*, *m*, *s* (1<=≤<=*n*,<=*m*,<=*s*<=≤<=106) — length of the board, width of the board and length of the flea's jump.
Output the only integer — the number of the required starting positions of the flea.
[ "2 3 1000000\n", "3 3 2\n" ]
[ "6\n", "4\n" ]
none
[ { "input": "2 3 1000000", "output": "6" }, { "input": "3 3 2", "output": "4" }, { "input": "1 2 3", "output": "2" }, { "input": "4 5 6", "output": "20" }, { "input": "9 8 7", "output": "8" }, { "input": "1000 1000 1000", "output": "1000000" }, ...
186
0
0
12,356
750
New Year and Rating
[ "binary search", "greedy", "math" ]
null
null
Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating ch...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=200<=000). The *i*-th of next *n* lines contains two integers *c**i* and *d**i* (<=-<=100<=≤<=*c**i*<=≤<=100, 1<=≤<=*d**i*<=≤<=2), describing Limak's rating change after the *i*-th contest and his division during the *i*-th contest contest.
If Limak's current rating can be arbitrarily big, print "Infinity" (without quotes). If the situation is impossible, print "Impossible" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the *n* contests.
[ "3\n-7 1\n5 2\n8 2\n", "2\n57 1\n22 2\n", "1\n-5 1\n", "4\n27 2\n13 1\n-50 1\n8 2\n" ]
[ "1907\n", "Impossible\n", "Infinity\n", "1897\n" ]
In the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating: - Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7. - With rating 1894 Limak is in the division 2. His rating increases by 5. - Limak has rati...
[ { "input": "3\n-7 1\n5 2\n8 2", "output": "1907" }, { "input": "2\n57 1\n22 2", "output": "Impossible" }, { "input": "1\n-5 1", "output": "Infinity" }, { "input": "4\n27 2\n13 1\n-50 1\n8 2", "output": "1897" }, { "input": "6\n8 1\n-22 1\n9 2\n-7 2\n85 2\n77 1", ...
1,138
9,932,800
3
12,359
335
Palindrome
[ "constructive algorithms", "dp" ]
null
null
Given a string *s*, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of *s* and is as long as possible.
The only line of the input contains one string *s* of length *n* (1<=≤<=*n*<=≤<=5·104) containing only lowercase English letters.
If *s* contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of *s*. If *s* doesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of *s* and is as long as possible. If there exists multiple answers, you are allo...
[ "bbbabcbbb\n", "rquwmzexectvnbanemsmdufrg\n" ]
[ "bbbcbbb\n", "rumenanemur\n" ]
A subsequence of a string is a string that can be derived from it by deleting some characters without changing the order of the remaining characters. A palindrome is a string that reads the same forward or backward.
[ { "input": "bbbabcbbb", "output": "bbbcbbb" }, { "input": "rquwmzexectvnbanemsmdufrg", "output": "rumenanemur" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
62
0
0
12,362
875
Delivery Club
[ "binary search", "data structures", "dp" ]
null
null
Petya and Vasya got employed as couriers. During the working day they are to deliver packages to *n* different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate *s*1, Vasya is at t...
The first line contains three integers *n*, *s*1, *s*2 (1<=≤<=*n*<=≤<=100<=000, 0<=≤<=*s*1,<=*s*2<=≤<=109) — number of points of delivery and starting positions of Petya and Vasya. The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* — customers coordinates (0<=≤<=*x**i*<=≤<=109), in the order to make a de...
Output the only integer, minimum possible maximal distance between couriers during delivery.
[ "2 0 10\n5 6\n", "3 2 1\n3 4 5\n", "1 4 5\n2\n" ]
[ "10\n", "1\n", "2\n" ]
In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point. In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Pe...
[]
30
0
0
12,388
199
Special Olympics
[ "geometry" ]
null
null
A renowned abstract artist Sasha, drawing inspiration from nowhere, decided to paint a picture entitled "Special Olympics". He justly thought that, if the regular Olympic games have five rings, then the Special ones will do with exactly two rings just fine. Let us remind you that a ring is a region located between two...
The input contains two lines. Each line has four space-separated integers *x**i*, *y**i*, *r**i*, *R**i*, that describe the *i*-th ring; *x**i* and *y**i* are coordinates of the ring's center, *r**i* and *R**i* are the internal and external radii of the ring correspondingly (<=-<=100<=≤<=*x**i*,<=*y**i*<=≤<=100; 1<=≤...
A single integer — the number of ways to cut out a circle from the canvas.
[ "60 60 45 55\n80 80 8 32\n", "60 60 45 55\n80 60 15 25\n", "50 50 35 45\n90 50 35 45\n" ]
[ "1", "4", "0" ]
Figures for test samples are given below. The possible cuts are marked with red dotted line.
[ { "input": "60 60 45 55\n80 80 8 32", "output": "1" }, { "input": "60 60 45 55\n80 60 15 25", "output": "4" }, { "input": "50 50 35 45\n90 50 35 45", "output": "0" }, { "input": "0 0 50 70\n1 0 60 80", "output": "2" }, { "input": "0 0 1 2\n10 0 2 20", "output"...
218
6,963,200
3
12,389
883
Photo Processing
[ "binary search", "dp" ]
null
null
Evlampiy has found one more cool application to process photos. However the application has certain limitations. Each photo *i* has a contrast *v**i*. In order for the processing to be truly of high quality, the application must receive at least *k* photos with contrasts which differ as little as possible. Evlampiy a...
The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=3·105) — number of photos and minimum size of a group. The second line contains *n* integers *v*1,<=*v*2,<=...,<=*v**n* (1<=≤<=*v**i*<=≤<=109), where *v**i* is the contrast of the *i*-th photo.
Print the minimal processing time of the division into groups.
[ "5 2\n50 110 130 40 120\n", "4 1\n2 3 4 1\n" ]
[ "20\n", "0\n" ]
In the first example the photos should be split into 2 groups: [40, 50] and [110, 120, 130]. The processing time of the first group is 10, and the processing time of the second group is 20. Maximum among 10 and 20 is 20. It is impossible to split the photos into groups in a such way that the processing time of division...
[ { "input": "5 2\n50 110 130 40 120", "output": "20" }, { "input": "4 1\n2 3 4 1", "output": "0" }, { "input": "1 1\n4", "output": "0" }, { "input": "2 2\n7 5", "output": "2" }, { "input": "3 2\n34 3 75", "output": "72" }, { "input": "5 2\n932 328 886 9...
3,000
25,088,000
0
12,397
58
Trees
[ "brute force" ]
C. Trees
2
256
On Bertown's main street *n* trees are growing, the tree number *i* has the height of *a**i* meters (1<=≤<=*i*<=≤<=*n*). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the ...
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) which is the number of trees. The second line contains integers *a**i* (1<=≤<=*a**i*<=≤<=105) which are the heights of the trees.
Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful.
[ "3\n2 2 2\n", "4\n1 2 2 1\n" ]
[ "1\n", "0\n" ]
none
[ { "input": "3\n2 2 2", "output": "1" }, { "input": "4\n1 2 2 1", "output": "0" }, { "input": "3\n61452 50974 73849", "output": "2" }, { "input": "4\n86002 1199 86003 86002", "output": "1" }, { "input": "5\n92605 92606 41969 98774 92605", "output": "2" }, {...
248
22,220,800
-1
12,444
176
Trading Business
[ "greedy", "sortings" ]
null
null
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the sellin...
The first line contains three space-separated integers *n*, *m* and *k* (2<=≤<=*n*<=≤<=10, 1<=≤<=*m*,<=*k*<=≤<=100) — the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly. Then follow *n* blocks describing each planet. The first line of the *i*-th block has the p...
Print a single number — the maximum profit Qwerty can get.
[ "3 3 10\nVenus\n6 5 3\n7 6 5\n8 6 10\nEarth\n10 9 0\n8 6 4\n10 9 3\nMars\n4 3 0\n8 4 12\n7 2 5\n" ]
[ "16" ]
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3·6 + 7·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3·9 + 7·9 = 90 units of money for the items, he sho...
[ { "input": "3 3 10\nVenus\n6 5 3\n7 6 5\n8 6 10\nEarth\n10 9 0\n8 6 4\n10 9 3\nMars\n4 3 0\n8 4 12\n7 2 5", "output": "16" }, { "input": "2 1 5\nA\n6 5 5\nB\n10 9 0", "output": "15" }, { "input": "2 2 5\nAbcdefghij\n20 15 20\n10 5 13\nKlmopqrstu\n19 16 20\n12 7 14", "output": "0" }...
186
307,200
0
12,487
0
none
[ "none" ]
null
null
You are running for a governor in a small city in Russia. You ran some polls and did some research, and for every person in the city you know whom he will vote for, and how much it will cost to bribe that person to vote for you instead of whomever he wants to vote for right now. You are curious, what is the smallest am...
First line contains one integer *n* (1<=≤<=*n*<=≤<=105) — number of voters in the city. Each of the next *n* lines describes one voter and contains two integers *a**i* and *b**i* (0<=≤<=*a**i*<=≤<=105; 0<=≤<=*b**i*<=≤<=104) — number of the candidate that voter is going to vote for and amount of money you need to pay hi...
Print one integer — smallest amount of money you need to spend to win the elections.
[ "5\n1 2\n1 2\n1 2\n2 1\n0 0\n", "4\n1 2\n1 2\n2 1\n0 0\n", "1\n100000 0\n" ]
[ "3\n", "2\n", "0\n" ]
none
[]
31
0
0
12,492
550
Preparing Olympiad
[ "bitmasks", "brute force" ]
null
null
You have *n* problems. You have estimated the difficulty of the *i*-th one as integer *c**i*. Now you want to prepare a problemset for a contest, using some of the problems you've made. A problemset for the contest must consist of at least two problems. You think that the total difficulty of the problems of the contes...
The first line contains four integers *n*, *l*, *r*, *x* (1<=≤<=*n*<=≤<=15, 1<=≤<=*l*<=≤<=*r*<=≤<=109, 1<=≤<=*x*<=≤<=106) — the number of problems you have, the minimum and maximum value of total difficulty of the problemset and the minimum difference in difficulty between the hardest problem in the pack and the easies...
Print the number of ways to choose a suitable problemset for the contest.
[ "3 5 6 1\n1 2 3\n", "4 40 50 10\n10 20 30 25\n", "5 25 35 10\n10 10 20 10 20\n" ]
[ "2\n", "2\n", "6\n" ]
In the first example two sets are suitable, one consisting of the second and third problem, another one consisting of all three problems. In the second example, two sets of problems are suitable — the set of problems with difficulties 10 and 30 as well as the set of problems with difficulties 20 and 30. In the third ...
[ { "input": "3 5 6 1\n1 2 3", "output": "2" }, { "input": "4 40 50 10\n10 20 30 25", "output": "2" }, { "input": "5 25 35 10\n10 10 20 10 20", "output": "6" }, { "input": "4 15 60 10\n10 20 30 25", "output": "6" }, { "input": "1 10 20 1\n15", "output": "0" },...
389
11,366,400
3
12,525
785
Anton and School - 2
[ "combinatorics", "dp", "math", "number theory" ]
null
null
As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket s...
The only line of the input contains a string *s* — the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200<=000.
Output one number — the answer for the task modulo 109<=+<=7.
[ ")(()()\n", "()()()\n", ")))\n" ]
[ "6\n", "7\n", "0\n" ]
In the first sample the following subsequences are possible: - If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". - If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". - If we delete characters at the positions 1, 2...
[ { "input": ")(()()", "output": "6" }, { "input": "()()()", "output": "7" }, { "input": ")))", "output": "0" }, { "input": ")())((((((()))()((()))()((()()()((()()()))", "output": "4344426" }, { "input": "(", "output": "0" }, { "input": ")", "output"...
30
0
0
12,537
570
Simple Game
[ "constructive algorithms", "games", "greedy", "implementation", "math" ]
null
null
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to *n*. Let's assume that Misha chose number *m*, and Andrew chose number *a*. Then, by using a random generator they choose a random integer *c* in the range between 1 and *n* (any integer from 1 to *n*...
The first line contains two integers *n* and *m* (1<=≤<=*m*<=≤<=*n*<=≤<=109) — the range of numbers in the game, and the number selected by Misha respectively.
Print a single number — such value *a*, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
[ "3 1\n", "4 3\n" ]
[ "2", "2" ]
In the first sample test: Andrew wins if *c* is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses *a* = 3, the probability of winning will be 1 / 3. If *a* = 1, the probability of winning is 0. In the second sample test: Andrew wins if *c* is equal to 1 and 2. The probability that Andrew wi...
[ { "input": "3 1", "output": "2" }, { "input": "4 3", "output": "2" }, { "input": "5 5", "output": "4" }, { "input": "10 5", "output": "6" }, { "input": "20 13", "output": "12" }, { "input": "51 1", "output": "2" }, { "input": "100 50", ...
61
6,758,400
0
12,548
0
none
[ "none" ]
null
null
Bear Limak has *n* colored balls, arranged in one long row. Balls are numbered 1 through *n*, from left to right. There are *n* possible colors, also numbered 1 through *n*. The *i*-th ball has color *t**i*. For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurr...
The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=5000) — the number of balls. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=≤<=*t**i*<=≤<=*n*) where *t**i* is the color of the *i*-th ball.
Print *n* integers. The *i*-th of them should be equal to the number of intervals where *i* is a dominant color.
[ "4\n1 2 1 2\n", "3\n1 1 1\n" ]
[ "7 3 0 0 \n", "6 0 0 \n" ]
In the first sample, color 2 is dominant in three intervals: - An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color. - An interval [4, 4] contains one ball, with color 2 again. - An interval [2, 4] contains two balls of color 2 and one ball of color 1. There are 7 more inte...
[ { "input": "4\n1 2 1 2", "output": "7 3 0 0 " }, { "input": "3\n1 1 1", "output": "6 0 0 " }, { "input": "10\n9 1 5 2 9 2 9 2 1 1", "output": "18 30 0 0 1 0 0 0 6 0 " }, { "input": "50\n17 13 19 19 19 34 32 24 24 13 34 17 19 19 7 32 19 13 13 30 19 34 34 28 41 24 24 47 22 34 2...
2,000
4,915,200
0
12,595
983
NN country
[ "binary search", "data structures", "trees" ]
null
null
In the NN country, there are $n$ cities, numbered from $1$ to $n$, and $n - 1$ roads, connecting them. There is a roads path between any two cities. There are $m$ bidirectional bus routes between cities. Buses drive between two cities taking the shortest path with stops in every city they drive through. Travelling by ...
The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of cities. The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i &lt; i$), where $p_i$ means that cities $p_i$ and $i$ are connected by road. The third line contains a single integer $m$ ($1 \le m \le 2 \cdo...
Print the answer for each question on a separate line. If there is no way to get from one city to another, print $-1$. Otherwise print the minimum number of buses you have to use.
[ "7\n1 1 1 4 5 6\n4\n4 2\n5 4\n1 3\n6 7\n6\n4 5\n3 5\n7 2\n4 5\n3 2\n5 3\n", "7\n1 1 2 3 4 1\n4\n4 7\n3 5\n7 6\n7 6\n6\n4 6\n3 1\n3 2\n2 7\n6 3\n5 3\n" ]
[ "1\n3\n-1\n1\n2\n3\n", "1\n-1\n-1\n1\n-1\n1\n" ]
[]
46
0
0
12,611