contest_id
int32 1
2.13k
| index
stringclasses 62
values | problem_id
stringlengths 2
6
| title
stringlengths 0
67
| rating
int32 0
3.5k
| tags
stringlengths 0
139
| statement
stringlengths 0
6.96k
| input_spec
stringlengths 0
2.32k
| output_spec
stringlengths 0
1.52k
| note
stringlengths 0
5.06k
| sample_tests
stringlengths 0
1.02k
| difficulty_category
stringclasses 6
values | tag_count
int8 0
11
| statement_length
int32 0
6.96k
| input_spec_length
int16 0
2.32k
| output_spec_length
int16 0
1.52k
| contest_year
int16 0
21
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
400 |
D
|
400D
|
D. Dima and Bacteria
| 2,000 |
dsu; graphs; shortest paths
|
Dima took up the biology of bacteria, as a result of his experiments, he invented k types of bacteria. Overall, there are n bacteria at his laboratory right now, and the number of bacteria of type i equals ci. For convenience, we will assume that all the bacteria are numbered from 1 to n. The bacteria of type ci are numbered from to .With the help of special equipment Dima can move energy from some bacteria into some other one. Of course, the use of such equipment is not free. Dima knows m ways to move energy from some bacteria to another one. The way with number i can be described with integers ui, vi and xi mean that this way allows moving energy from bacteria with number ui to bacteria with number vi or vice versa for xi dollars.Dima's Chef (Inna) calls the type-distribution correct if there is a way (may be non-direct) to move energy from any bacteria of the particular type to any other bacteria of the same type (between any two bacteria of the same type) for zero cost.As for correct type-distribution the cost of moving the energy depends only on the types of bacteria help Inna to determine is the type-distribution correct? If it is, print the matrix d with size k × k. Cell d[i][j] of this matrix must be equal to the minimal possible cost of energy-moving from bacteria with type i to bacteria with type j.
|
The first line contains three integers n, m, k (1 ≤ n ≤ 105; 0 ≤ m ≤ 105; 1 ≤ k ≤ 500). The next line contains k integers c1, c2, ..., ck (1 ≤ ci ≤ n). Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ 105; 0 ≤ xi ≤ 104). It is guaranteed that .
|
If Dima's type-distribution is correct, print string «Yes», and then k lines: in the i-th line print integers d[i][1], d[i][2], ..., d[i][k] (d[i][i] = 0). If there is no way to move energy from bacteria i to bacteria j appropriate d[i][j] must equal to -1. If the type-distribution isn't correct print «No».
|
Input: 4 4 21 32 3 03 4 02 4 12 1 2 | Output: Yes0 22 0
|
Hard
| 3 | 1,330 | 269 | 308 | 4 |
|
976 |
A
|
976A
|
A. Minimum Binary Number
| 800 |
implementation
|
String can be called correct if it consists of characters ""0"" and ""1"" and there are no redundant leading zeroes. Here are some examples: ""0"", ""10"", ""1001"".You are given a correct string s.You can perform two different operations on this string: swap any pair of adjacent characters (for example, ""101"" ""110""); replace ""11"" with ""1"" (for example, ""110"" ""10""). Let val(s) be such a number that s is its binary representation.Correct string a is less than some other correct string b iff val(a) < val(b).Your task is to find the minimum correct string that you can obtain from the given one using the operations described above. You can use these operations any number of times in any order (or even use no operations at all).
|
The first line contains integer number n (1 ≤ n ≤ 100) — the length of string s.The second line contains the string s consisting of characters ""0"" and ""1"". It is guaranteed that the string s is correct.
|
Print one string — the minimum correct string that you can obtain from the given one.
|
In the first example you can obtain the answer by the following sequence of operations: ""1001"" ""1010"" ""1100"" ""100"".In the second example you can't obtain smaller answer no matter what operations you use.
|
Input: 41001 | Output: 100
|
Beginner
| 1 | 745 | 206 | 85 | 9 |
2,079 |
B
|
2079B
| 2,600 |
*special; data structures; dp; greedy
|
Expert
| 4 | 0 | 0 | 0 | 20 |
||||||
1,099 |
C
|
1099C
|
C. Postcard
| 1,200 |
constructive algorithms; implementation
|
Andrey received a postcard from Irina. It contained only the words ""Hello, Andrey!"", and a strange string consisting of lowercase Latin letters, snowflakes and candy canes. Andrey thought that this string is an encrypted message, and decided to decrypt it.Andrey noticed that snowflakes and candy canes always stand after the letters, so he supposed that the message was encrypted as follows. Candy cane means that the letter before it can be removed, or can be left. A snowflake means that the letter before it can be removed, left, or repeated several times.For example, consider the following string: This string can encode the message «happynewyear». For this, candy canes and snowflakes should be used as follows: candy cane 1: remove the letter w, snowflake 1: repeat the letter p twice, candy cane 2: leave the letter n, snowflake 2: remove the letter w, snowflake 3: leave the letter e. Please note that the same string can encode different messages. For example, the string above can encode «hayewyar», «happpppynewwwwwyear», and other messages.Andrey knows that messages from Irina usually have a length of \(k\) letters. Help him to find out if a given string can encode a message of \(k\) letters, and if so, give an example of such a message.
|
The first line contains the string received in the postcard. The string consists only of lowercase Latin letters, as well as the characters «*» and «?», meaning snowflake and candy cone, respectively. These characters can only appear immediately after the letter. The length of the string does not exceed \(200\).The second line contains an integer number \(k\) (\(1 \leq k \leq 200\)), the required message length.
|
Print any message of length \(k\) that the given string can encode, or «Impossible» if such a message does not exist.
|
Input: hw?ap*yn?eww*ye*ar 12 | Output: happynewyear
|
Easy
| 2 | 1,257 | 415 | 117 | 10 |
|
958 |
C3
|
958C3
|
C3. Encryption (hard)
| 2,500 |
data structures; dp
|
Heidi is now just one code away from breaking the encryption of the Death Star plans. The screen that should be presenting her with the description of the next code looks almost like the previous one, though who would have thought that the evil Empire engineers would fill this small screen with several million digits! It is just ridiculous to think that anyone would read them all...Heidi is once again given a sequence A and two integers k and p. She needs to find out what the encryption key S is.Let X be a sequence of integers, and p a positive integer. We define the score of X to be the sum of the elements of X modulo p.Heidi is given a sequence A that consists of N integers, and also given integers k and p. Her goal is to split A into k parts such that: Each part contains at least 1 element of A, and each part consists of contiguous elements of A. No two parts overlap. The total sum S of the scores of those parts is minimized (not maximized!). Output the sum S, which is the encryption code.
|
The first line of the input contains three space-separated integers N, k and p (k ≤ N ≤ 500 000, 2 ≤ k ≤ 100, 2 ≤ p ≤ 100) – the number of elements in A, the number of parts A should be split into, and the modulo for computing scores, respectively.The second line contains N space-separated integers that are the elements of A. Each integer is from the interval [1, 1 000 000].
|
Output the number S as described in the problem statement.
|
In the first example, if the input sequence is split as (3), (4, 7), (2), the total score would be . It is easy to see that this score is the smallest possible.In the second example, one possible way to obtain score 13 is to make the following split: (16, 3), (24), (13), (9, 8), (7, 5, 12, 12).
|
Input: 4 3 103 4 7 2 | Output: 6
|
Expert
| 2 | 1,007 | 377 | 58 | 9 |
2,053 |
I2
|
2053I2
|
I2. Affectionate Arrays (Hard Version)
| 3,500 |
data structures; dp; graphs; greedy; math; shortest paths; two pointers
|
Note that this statement is different to the version used in the official round. The statement has been corrected to a solvable version. In the official round, all submissions to this problem have been removed.This is the hard version of the problem. The difference between the versions is that in this version, you need to compute the sum of value of different arrays. You can hack only if you solved all versions of this problem. Iris treasures an integer array \(a_1, a_2, \ldots, a_n\). She knows this array has an interesting property: the maximum absolute value of all elements is less than or equal to the sum of all elements, that is, \(\max(\lvert a_i\rvert) \leq \sum a_i\).Iris defines the boredom of an array as its maximum subarray\(^{\text{∗}}\) sum. Iris's birthday is coming, and Victor is going to send her another array \(b_1, b_2, \ldots, b_m\) as a gift. For some seemingly obvious reasons, he decides the array \(b_1, b_2, \ldots, b_m\) should have the following properties. \(a_1, a_2, \ldots, a_n\) should be a subsequence\(^{\text{†}}\) of \(b_1, b_2, \ldots, b_m\). The two arrays have the same sum. That is, \(\sum\limits_{i=1}^n a_i = \sum\limits_{i=1}^m b_i\). The boredom of \(b_1, b_2, \ldots, b_m\) is the smallest possible. Among the arrays with the smallest boredom, the length of the array \(b\) (i.e., \(m\)) is the smallest possible. And in this case, Iris will understand his regard as soon as possible! For a possible array \(b_1, b_2, \ldots, b_m\) satisfying all the conditions above, Victor defines the value of the array as the number of occurrences of array \(a\) as subsequences in array \(b\). That is, he counts the number of array \(c_1, c_2, \ldots, c_{n}\) that \(1\le c_1< c_2< \ldots< c_n\le m\) and for all integer \(i\) that \(1\le i\le n\), \(b_{c_{i}}=a_i\) is satisfied, and let this be the value of array \(b\).Even constrained as above, there are still too many possible gifts. So Victor asks you to calculate the sum of value of all possible arrays \(b_1, b_2, \ldots, b_m\). Since the answer may be large, Victor only needs the number modulo \(998\,244\,353\). He promises you: if you help him successfully, he will share a bit of Iris's birthday cake with you.\(^{\text{∗}}\)An array \(c\) is a subarray of an array \(d\) if \(c\) can be obtained from \(d\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. \(^{\text{†}}\)A sequence \(c\) is a subsequence of a sequence \(d\) if \(c\) can be obtained from \(d\) by the deletion of several (possibly, zero or all) element from arbitrary positions.
|
Each test contains multiple test cases. The first line of input contains an integer \(t\) (\(1 \leq t \leq 10^5\)) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 3\cdot 10^6\)) — the length of the array \(a_1, a_2, \ldots, a_n\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \leq a_i \leq 10^9\)) — the initial array. It is guaranteed that \(\max(\lvert a_i\rvert) \leq \sum a_i\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3\cdot 10^6\).
|
For each test case, output a single line containing an integer: the sum of values of valid arrays \(b_1, b_2, \ldots, b_m\), modulo \(998\,244\,353\).
|
In the first test case, \(a=[1, 2, 3, 4]\). The only possible array \(b\) is \([1, 2, 3, 4]\), and its value is \(1\).In the second test case, \(a=[2, -3, 2, 2]\). The possible arrays \(b\) are \([1, 2, -3, 2, -1, 2]\) and \([2, 1, -3, 2, -1, 2]\). Both arrays have value \(1\).In the third test case, \(a=[1, -2, 2, 1]\). The only possible array \(b\) is \([1, 1, -2, 2, -1, 1]\). It has value \(2\), because we can find arrays \(c=[1,3,4,6]\) or \([2,3,4,6]\). That is, the array \(a\) occurs twice in \(b\), so the answer is \(2\).
|
Input: 541 2 3 442 -3 2 241 -2 2 1102 -7 6 3 -1 4 2 -5 8 -4204 -2 4 3 -2 1 5 2 3 6 -5 -1 -4 -2 -3 5 -3 1 -4 1 | Output: 1 2 2 20 1472
|
Master
| 7 | 2,646 | 620 | 150 | 20 |
811 |
B
|
811B
|
B. Vladik and Complicated Book
| 1,200 |
implementation; sortings
|
Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.
|
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.
|
For each mom’s sorting on it’s own line print ""Yes"", if page which is interesting to Vladik hasn't changed, or ""No"" otherwise.
|
Explanation of first test case: [1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is ""Yes"". [3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is ""No"". [5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is ""Yes"". [5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is ""Yes"". [5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is ""No"".
|
Input: 5 55 4 3 2 11 5 31 3 12 4 34 4 42 5 3 | Output: YesNoYesYesNo
|
Easy
| 2 | 764 | 508 | 130 | 8 |
883 |
A
|
883A
|
A. Automatic Door
| 2,200 |
implementation
|
There is an automatic door at the entrance of a factory. The door works in the following way: when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, when one or several people come to the door and it is open, all people immediately come inside, opened door immediately closes in d seconds after its opening, if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close. For example, if d = 3 and four people are coming at four different moments of time t1 = 4, t2 = 7, t3 = 9 and t4 = 13 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12.It is known that n employees will enter at moments a, 2·a, 3·a, ..., n·a (the value a is positive integer). Also m clients will enter at moments t1, t2, ..., tm.Write program to find the number of times the automatic door will open. Assume that the door is initially closed.
|
The first line contains four integers n, m, a and d (1 ≤ n, a ≤ 109, 1 ≤ m ≤ 105, 1 ≤ d ≤ 1018) — the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes.The second line contains integer sequence t1, t2, ..., tm (1 ≤ ti ≤ 1018) — moments of time when clients will come. The values ti are given in non-decreasing order.
|
Print the number of times the door will open.
|
In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time.
|
Input: 1 1 3 47 | Output: 1
|
Hard
| 1 | 1,064 | 419 | 45 | 8 |
297 |
A
|
297A
|
A. Parity Game
| 1,700 |
constructive algorithms
|
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character ""0"" and ""1"") a and b. Then you try to turn a into b using two types of operations: Write parity(a) to the end of a. For example, . Remove the first character of a. For example, . You cannot perform this operation if a is empty. You can use as many operations as you want. The problem is, is it possible to turn a into b?The parity of a 01-string is 1 if there is an odd number of ""1""s in the string, and 0 otherwise.
|
The first line contains the string a and the second line contains the string b (1 ≤ |a|, |b| ≤ 1000). Both strings contain only the characters ""0"" and ""1"". Here |x| denotes the length of the string x.
|
Print ""YES"" (without quotes) if it is possible to turn a into b, and ""NO"" (without quotes) otherwise.
|
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
|
Input: 010110110 | Output: YES
|
Medium
| 1 | 640 | 204 | 105 | 2 |
1,132 |
F
|
1132F
|
F. Clear the String
| 2,000 |
dp
|
You are given a string \(s\) of length \(n\) consisting of lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.Calculate the minimum number of operations to delete the whole string \(s\).
|
The first line contains one integer \(n\) (\(1 \le n \le 500\)) — the length of string \(s\).The second line contains the string \(s\) (\(|s| = n\)) consisting of lowercase Latin letters.
|
Output a single integer — the minimal number of operation to delete string \(s\).
|
Input: 5 abaca | Output: 3
|
Hard
| 1 | 425 | 187 | 81 | 11 |
|
1,438 |
D
|
1438D
|
D. Powerful Ksenia
| 2,200 |
bitmasks; constructive algorithms; math
|
Ksenia has an array \(a\) consisting of \(n\) positive integers \(a_1, a_2, \ldots, a_n\). In one operation she can do the following: choose three distinct indices \(i\), \(j\), \(k\), and then change all of \(a_i, a_j, a_k\) to \(a_i \oplus a_j \oplus a_k\) simultaneously, where \(\oplus\) denotes the bitwise XOR operation. She wants to make all \(a_i\) equal in at most \(n\) operations, or to determine that it is impossible to do so. She wouldn't ask for your help, but please, help her!
|
The first line contains one integer \(n\) (\(3 \leq n \leq 10^5\)) — the length of \(a\).The second line contains \(n\) integers, \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) — elements of \(a\).
|
Print YES or NO in the first line depending on whether it is possible to make all elements equal in at most \(n\) operations.If it is possible, print an integer \(m\) (\(0 \leq m \leq n\)), which denotes the number of operations you do.In each of the next \(m\) lines, print three distinct integers \(i, j, k\), representing one operation. If there are many such operation sequences possible, print any. Note that you do not have to minimize the number of operations.
|
In the first example, the array becomes \([4 \oplus 1 \oplus 7, 2, 4 \oplus 1 \oplus 7, 4 \oplus 1 \oplus 7, 2] = [2, 2, 2, 2, 2]\).
|
Input: 5 4 2 1 7 2 | Output: YES 1 1 3 4
|
Hard
| 3 | 493 | 203 | 467 | 14 |
1,540 |
C2
|
1540C2
|
C2. Converging Array (Hard Version)
| 2,900 |
dp; math
|
This is the hard version of the problem. The only difference is that in this version \(1 \le q \le 10^5\). You can make hacks only if both versions of the problem are solved.There is a process that takes place on arrays \(a\) and \(b\) of length \(n\) and length \(n-1\) respectively. The process is an infinite sequence of operations. Each operation is as follows: First, choose a random integer \(i\) (\(1 \le i \le n-1\)). Then, simultaneously set \(a_i = \min\left(a_i, \frac{a_i+a_{i+1}-b_i}{2}\right)\) and \(a_{i+1} = \max\left(a_{i+1}, \frac{a_i+a_{i+1}+b_i}{2}\right)\) without any rounding (so values may become non-integer). See notes for an example of an operation.It can be proven that array \(a\) converges, i. e. for each \(i\) there exists a limit \(a_i\) converges to. Let function \(F(a, b)\) return the value \(a_1\) converges to after a process on \(a\) and \(b\).You are given array \(b\), but not array \(a\). However, you are given a third array \(c\). Array \(a\) is good if it contains only integers and satisfies \(0 \leq a_i \leq c_i\) for \(1 \leq i \leq n\).Your task is to count the number of good arrays \(a\) where \(F(a, b) \geq x\) for \(q\) values of \(x\). Since the number of arrays can be very large, print it modulo \(10^9+7\).
|
The first line contains a single integer \(n\) (\(2 \le n \le 100\)).The second line contains \(n\) integers \(c_1, c_2 \ldots, c_n\) (\(0 \le c_i \le 100\)).The third line contains \(n-1\) integers \(b_1, b_2, \ldots, b_{n-1}\) (\(0 \le b_i \le 100\)).The fourth line contains a single integer \(q\) (\(1 \le q \le 10^5\)).The fifth line contains \(q\) space separated integers \(x_1, x_2, \ldots, x_q\) (\(-10^5 \le x_i \le 10^5\)).
|
Output \(q\) integers, where the \(i\)-th integer is the answer to the \(i\)-th query, i. e. the number of good arrays \(a\) where \(F(a, b) \geq x_i\) modulo \(10^9+7\).
|
The following explanation assumes \(b = [2, 1]\) and \(c=[2, 3, 4]\) (as in the sample).Examples of arrays \(a\) that are not good: \(a = [3, 2, 3]\) is not good because \(a_1 > c_1\); \(a = [0, -1, 3]\) is not good because \(a_2 < 0\). One possible good array \(a\) is \([0, 2, 4]\). We can show that no operation has any effect on this array, so \(F(a, b) = a_1 = 0\).Another possible good array \(a\) is \([0, 1, 4]\). In a single operation with \(i = 1\), we set \(a_1 = \min(\frac{0+1-2}{2}, 0)\) and \(a_2 = \max(\frac{0+1+2}{2}, 1)\). So, after a single operation with \(i = 1\), \(a\) becomes equal to \([-\frac{1}{2}, \frac{3}{2}, 4]\). We can show that no operation has any effect on this array, so \(F(a, b) = -\frac{1}{2}\).
|
Input: 3 2 3 4 2 1 5 -1 0 1 -100000 100000 | Output: 56 28 4 60 0
|
Master
| 2 | 1,266 | 434 | 170 | 15 |
2,052 |
G
|
2052G
|
G. Geometric Balance
| 2,800 |
data structures; geometry; implementation
|
Peter's little brother Ivan likes to play with a turtle. The turtle is a special toy that lives on the plane and can execute three commands: Rotate \(a\) degrees counterclockwise. Draw \(d\) units in the direction it is facing while dispensing ink. No segment of the plane will be covered by ink more than once. Move \(d\) units in the direction it is facing without drawing. Ivan just learned about the compass, so he will only rotate his turtle so it faces one of eight cardinal or ordinal directions (angles \(a\) in rotate commands are always divisible by 45). Also, he will perform at least one draw command.Peter has noted all the commands Ivan has given to his turtle. He thinks that the image drawn by the turtle is adorable. Now Peter wonders about the smallest positive angle \(b\) such that he can perform the following operations: move the turtle to a point of his choosing, rotate it by \(b\) degrees, and execute all the commands in the same order. These operations should produce the same image as the original one. Can you help Peter?Note, two images are considered the same if the sets of points covered by ink on the plane are the same in both of the images.
|
The first line of the input contains a single integer \(n\;(1 \le n \le 50000)\) — the number of commands Ivan has given.The next \(n\) lines contain commands. Each command is one of: ""rotate \(a\)"" (\(45 \le a \le 360\)) where \(a\) is divisible by \(45\); ""draw \(d\)"" (\(1 \le d \le 10^9\)); ""move \(d\)"" (\(1 \le d \le 10^9\)). At least one and at most 2000 of the commands are draw. It is guaranteed that no segment of the plane will be covered by ink more than once.
|
Output a single number, the answer to the question. The answer always exists.
|
Input: 1draw 10 | Output: 180
|
Master
| 3 | 1,176 | 478 | 77 | 20 |
|
1,804 |
F
|
1804F
|
F. Approximate Diameter
| 2,700 |
binary search; divide and conquer; graphs; shortest paths
|
Jack has a graph of \(n\) vertices and \(m\) edges. All edges are bidirectional and of unit length. The graph is connected, i. e. there exists a path between any two of its vertices. There can be more than one edge connecting the same pair of vertices. The graph can contain self-loops, i. e. edges connecting a node to itself.The distance between vertices \(u\) and \(v\) is denoted as \(\rho(u, v)\) and equals the minimum possible number of edges on a path between \(u\) and \(v\). The diameter of graph \(G\) is defined as the maximum possible distance between some pair of its vertices. We denote it as \(d(G)\). In other words, $$$\(d(G) = \max_{1 \le u, v \le n}{\rho(u, v)}.\)\(Jack plans to consecutively apply \)q\( updates to his graph. Each update adds exactly one edge to the graph. Denote as \)G_i\( the graph after exactly \)i\( updates are made. Jack wants to calculate \)q + 1\( values \)d(G_0), d(G_1), d(G_2), \ldots, d(G_q)\(.However, Jack suspects that finding the exact diameters of \)q + 1\( graphs might be a difficult task, so he is fine with approximate answers that differ from the correct answers no more than twice. Formally, Jack wants to find a sequence of positive integers \)a_0, a_1, a_2, \ldots, a_q\( such that \)\(\left\lceil \frac{d(G_i)}{2} \right\rceil \le a_i \le 2 \cdot d(G_i)\)\( for each \)i$$$.HacksYou cannot make hacks in this problem.
|
The first line of the input contains three integers \(n\), \(m\), and \(q\) (\(2 \leq n \leq 10^5\), \(n - 1 \leq m \leq 10^5\), \(0 \leq q \leq 10^5\)), the number of vertices in the given graph, the number of edges and the number of updates, respectively.Then follow \(m\) lines describing the initial edges of the graph. The \(i\)-th of these lines contains two integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\)), the indexes of the vertices connected by the \(i\)-th edge.Then follow \(q\) lines describing the updates. The \(i\)-th of these lines contains two integers \(u'_i\) and \(v'_i\) (\(1 \leq u'_i, v'_i \leq n\)), the indexes of the vertices connected by the edge that is added to the graph in the \(i\)-th update.Important note. For testing purposes, the input data may contain some extra lines after the mentioned input format. These will be used by the checker to verify your answer. They are not a part of the test data, you should not use them in any way and you can even omit reading them.
|
Print a sequence of \(q + 1\) positive integers \(a_0, a_1, a_2, \ldots, a_q\). The \(i\)-th of these integers should differ from the diameter of graph \(G_i\) no more than twice.
|
In the first example, the correct sequence of \(d(G_0), d(G_1), d(G_2), \ldots, d(G_q)\) is \(6, 6, 6, 3, 3, 3, 2, 2, 2\). In the second example, the input contains an extra line that you can omit reading. It is not a part of the test and will be used for verifying your answer. The output of the second example contains the correct values of \(d(G_i)\).
|
Input: 9 10 8 1 2 2 3 2 4 3 5 4 5 5 6 5 7 6 8 7 8 8 9 3 4 6 7 2 8 1 9 1 6 4 9 3 9 7 1 | Output: 10 6 5 6 2 4 2 2 1
|
Master
| 4 | 1,383 | 1,016 | 179 | 18 |
67 |
E
|
67E
|
E. Save the City!
| 2,500 |
geometry
|
In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away from the uncorrupted part, the crimes could be stopped. So, they are trying to set up a compound where they can keep the corrupted people. To ensure that the criminals don't escape the compound, a watchtower needs to be set up, so that they can be watched.Since the people of Aalam-Aara aren't very rich, they met up with a merchant from some rich town who agreed to sell them a land-plot which has already a straight line fence AB along which a few points are set up where they can put up a watchtower. Your task is to help them find out the number of points on that fence where the tower can be put up, so that all the criminals can be watched from there. Only one watchtower can be set up. A criminal is watchable from the watchtower if the line of visibility from the watchtower to him doesn't cross the plot-edges at any point between him and the tower i.e. as shown in figure 1 below, points X, Y, C and A are visible from point B but the points E and D are not. Figure 1 Figure 2 Assume that the land plot is in the shape of a polygon and coordinate axes have been setup such that the fence AB is parallel to x-axis and the points where the watchtower can be set up are the integer points on the line. For example, in given figure 2, watchtower can be setup on any of five integer points on AB i.e. (4, 8), (5, 8), (6, 8), (7, 8) or (8, 8). You can assume that no three consecutive points are collinear and all the corner points other than A and B, lie towards same side of fence AB. The given polygon doesn't contain self-intersections.
|
The first line of the test case will consist of the number of vertices n (3 ≤ n ≤ 1000).Next n lines will contain the coordinates of the vertices in the clockwise order of the polygon. On the i-th line are integers xi and yi (0 ≤ xi, yi ≤ 106) separated by a space.The endpoints of the fence AB are the first two points, (x1, y1) and (x2, y2).
|
Output consists of a single line containing the number of points where the watchtower can be set up.
|
Figure 2 shows the first test case. All the points in the figure are watchable from any point on fence AB. Since, AB has 5 integer coordinates, so answer is 5.For case two, fence CD and DE are not completely visible, thus answer is 0.
|
Input: 54 88 89 44 00 4 | Output: 5
|
Expert
| 1 | 1,861 | 343 | 100 | 0 |
788 |
C
|
788C
|
C. The Great Mixing
| 2,300 |
dfs and similar; graphs; shortest paths
|
Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.
|
The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.
|
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.
|
In the first sample case, we can achieve concentration using one liter of Coke of types and : .In the second case, we can achieve concentration using two liters of type and one liter of type: .
|
Input: 400 4100 300 450 500 | Output: 2
|
Expert
| 3 | 912 | 312 | 130 | 7 |
1,211 |
H
|
1211H
|
H. Road Repair in Treeland
| 3,100 |
*special; binary search; dp; trees
|
There are \(n\) cities and \(n-1\) two-way roads in Treeland. Each road connects a pair of different cities. From any city you can drive to any other, moving only along the roads. Cities are numbered from \(1\) to \(n\). Yes, of course, you recognized an undirected tree in this description.The government plans to repair all the roads. Each road will be repaired by some private company. In total, the country has \(10^6\) private companies that are numbered from \(1\) to \(10^6\). It is possible that some companies will not repair roads at all, and some will repair many roads.To simplify the control over the work of private companies, the following restriction was introduced: for each city, we calculate the number of different companies that repair roads that have this city at one end. This number for each city should not exceed \(2\). In other words, for each city, there should be no more than two different companies that repair roads related to this city.The National Anti-Corruption Committee of Treeland raises concerns that all (or almost all) of the work will go to one private company. For this reason, the committee requires that roads be distributed among companies in such a way as to minimize the value of \(r\). For each company, we calculate the number of roads assigned to it, the maximum among all companies is called the number \(r\).Help the government find such a way to distribute all roads among companies in the required way.
|
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of input cases in the input. Next, the input cases themselves are given. The first line of each test case set contains an integer \(n\) (\(2 \le n \le 3000\)) — the number of cities in Treeland. Next, in the \(n-1\) lines the roads are written: a line contains two integers \(x_i\), \(y_i\) (\(1 \le x_i, y_i \le n\)), indicating that the \(i\)-th road connects the cities \(x_i\) and \(y_i\).It is guaranteed that the sum of all values \(n\) for all test cases in the input does not exceed \(3000\).
|
Print the answers for all \(t\) test cases in the input. Each test case must begin with a line that contains \(r\) — the minimum possible number of roads assigned to the most used company. Next, in the next line print \(n-1\) the number \(c_1, c_2, \dots, c_{n-1}\) (\(1 \le c_i \le 10^6\)), where \(c_i\) indicates the company to repair the \(i\)-th road. If there are several optimal assignments, print any of them.
|
Input: 3 3 1 2 2 3 6 1 2 1 3 1 4 1 5 1 6 7 3 1 1 4 4 6 5 1 2 4 1 7 | Output: 1 10 20 3 1 1 1 2 2 2 11 11 12 13 12 13
|
Master
| 4 | 1,458 | 579 | 417 | 12 |
|
730 |
K
|
730K
|
K. Roads Orientation Problem
| 3,200 |
graphs
|
Berland consists of n cities and m bidirectional roads connecting pairs of cities. There is no road connecting a city to itself, and between any pair of cities there is no more than one road. It is possible to reach any city from any other moving along roads.Currently Mr. President is in the city s and his destination is the city t. He plans to move along roads from s to t (s ≠ t).That's why Ministry of Fools and Roads has difficult days. The minister is afraid that Mr. President could get into a traffic jam or get lost. Who knows what else can happen!To be sure that everything goes as planned, the minister decided to temporarily make all roads one-way. So each road will be oriented in one of two possible directions. The following conditions must be satisfied: There should be no cycles along roads after orientation. The city s should be the only such city that all its roads are oriented out (i.e. there are no ingoing roads to the city s and the city s is the only such city). The city t should be the only such city that all its roads are oriented in (i.e. there are no outgoing roads from the city t and the city t is the only such city). Help the minister solve his problem. Write a program to find any such orientation of all roads or report that no solution exists.
|
Each test in this problem contains one or more test cases to solve. The first line of the input contains positive number T — the number of cases to solve.Each case starts with a line containing four integers n, m, s and t (2 ≤ n ≤ 4·105, 1 ≤ m ≤ 106, 1 ≤ s, t ≤ n, s ≠ t) — the number of cities, the number of roads and indices of departure and destination cities. The cities are numbered from 1 to n.The following m lines contain roads, one road per line. Each road is given as two integer numbers xj, yj (1 ≤ xj, yj ≤ n, xj ≠ yj), which means that the j-th road connects cities xj and yj. There is at most one road between any pair of cities. It is possible to reach any city from any other moving along roads.The sum of values n over all cases in a test doesn't exceed 4·105. The sum of values m over all cases in a test doesn't exceed 106.
|
For each case print ""Yes"" if the answer exists. In the following m lines print roads in the required directions. You can print roads in arbitrary order. If there are multiple answers, print any of them.Print the only line ""No"" if there is no answer for a case.
|
Input: 24 4 1 21 22 33 44 13 2 1 33 12 3 | Output: Yes1 23 24 31 4No
|
Master
| 1 | 1,283 | 843 | 264 | 7 |
|
2,037 |
C
|
2037C
|
C. Superultra's Favorite Permutation
| 1,000 |
constructive algorithms; greedy; math; number theory
|
Superultra, a little red panda, desperately wants primogems. In his dreams, a voice tells him that he must solve the following task to obtain a lifetime supply of primogems. Help Superultra!Construct a permutation\(^{\text{∗}}\) \(p\) of length \(n\) such that \(p_i + p_{i+1}\) is composite\(^{\text{†}}\) over all \(1 \leq i \leq n - 1\). If it's not possible, output \(-1\).\(^{\text{∗}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).\(^{\text{†}}\)An integer \(x\) is composite if it has at least one other divisor besides \(1\) and \(x\). For example, \(4\) is composite because \(2\) is a divisor.
|
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.Each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) — the length of the permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, if it's not possible to construct \(p\), output \(-1\) on a new line. Otherwise, output \(n\) integers \(p_1, p_2, \ldots, p_n\) on a new line.
|
In the first example, it can be shown that all permutation of size \(3\) contain two adjacent elements whose sum is prime. For example, in the permutation \([2,3,1]\) the sum \(2+3=5\) is prime.In the second example, we can verify that the sample output is correct because \(1+8\), \(8+7\), \(7+3\), \(3+6\), \(6+2\), \(2+4\), and \(4+5\) are all composite. There may be other constructions that are correct.
|
Input: 238 | Output: -1 1 8 7 3 6 2 4 5
|
Beginner
| 4 | 882 | 280 | 163 | 20 |
1,140 |
B
|
1140B
|
B. Good String
| 1,200 |
implementation; strings
|
You have a string \(s\) of length \(n\) consisting of only characters > and <. You may do some operations with this string, for each operation you have to choose some character that still remains in the string. If you choose a character >, the character that comes right after it is deleted (if the character you chose was the last one, nothing happens). If you choose a character <, the character that comes right before it is deleted (if the character you chose was the first one, nothing happens).For example, if we choose character > in string > > < >, the string will become to > > >. And if we choose character < in string > <, the string will become to <.The string is good if there is a sequence of operations such that after performing it only one character will remain in the string. For example, the strings >, > > are good. Before applying the operations, you may remove any number of characters from the given string (possibly none, possibly up to \(n - 1\), but not the whole string). You need to calculate the minimum number of characters to be deleted from string \(s\) so that it becomes good.
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) – the number of test cases. Each test case is represented by two lines.The first line of \(i\)-th test case contains one integer \(n\) (\(1 \le n \le 100\)) – the length of string \(s\).The second line of \(i\)-th test case contains string \(s\), consisting of only characters > and <.
|
For each test case print one line.For \(i\)-th test case print the minimum number of characters to be deleted from string \(s\) so that it becomes good.
|
In the first test case we can delete any character in string <>.In the second test case we don't need to delete any characters. The string > < < is good, because we can perform the following sequence of operations: > < < \(\rightarrow\) < < \(\rightarrow\) <.
|
Input: 3 2 <> 3 ><< 1 > | Output: 1 0 0
|
Easy
| 2 | 1,110 | 349 | 152 | 11 |
1,946 |
A
|
1946A
|
A. Median of an Array
| 800 |
greedy; implementation; sortings
|
You are given an array \(a\) of \(n\) integers.The median of an array \(q_1, q_2, \ldots, q_k\) is the number \(p_{\lceil \frac{k}{2} \rceil}\), where \(p\) is the array \(q\) sorted in non-decreasing order. For example, the median of the array \([9, 5, 1, 2, 6]\) is \(5\), as in the sorted array \([1, 2, 5, 6, 9]\), the number at index \(\lceil \frac{5}{2} \rceil = 3\) is \(5\), and the median of the array \([9, 2, 8, 3]\) is \(3\), as in the sorted array \([2, 3, 8, 9]\), the number at index \(\lceil \frac{4}{2} \rceil = 2\) is \(3\).You are allowed to choose an integer \(i\) (\(1 \le i \le n\)) and increase \(a_i\) by \(1\) in one operation.Your task is to find the minimum number of operations required to increase the median of the array.Note that the array \(a\) may not necessarily contain distinct numbers.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — the array \(a\).It is guaranteed that the sum of the values of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer — the minimum number of operations required to increase the median of the array.
|
In the first test case, you can apply one operation to the first number and obtain the array \([3, 2, 8]\), the median of this array is \(3\), as it is the number at index \(\lceil \frac{3}{2} \rceil = 2\) in the non-decreasing sorted array \([2, 3, 8]\). The median of the original array \([2, 2, 8]\) is \(2\), as it is the number at index \(\lceil \frac{3}{2} \rceil = 2\) in the non-decreasing sorted array \([2, 2, 8]\). Thus, the median increased (\(3 > 2\)) in just one operation.In the fourth test case, you can apply one operation to each of the numbers at indices \(1, 2, 3\) and obtain the array \([6, 6, 6, 4, 5]\), the median of this array is \(6\), as it is the number at index \(\lceil \frac{5}{2} \rceil = 3\) in the non-decreasing sorted array \([4, 5, 6, 6, 6]\). The median of the original array \([5, 5, 5, 4, 5]\) is \(5\), as it is the number at index \(\lceil \frac{5}{2} \rceil = 2\) in the non-decreasing sorted array \([4, 5, 5, 5, 5]\). Thus, the median increased (\(6 > 5\)) in three operations. It can be shown that this is the minimum possible number of operations.In the fifth test case, you can apply one operation to each of the numbers at indices \(1, 3\) and obtain the array \([3, 1, 3, 3, 1, 4]\), the median of this array is \(3\), as it is the number at index \(\lceil \frac{6}{2} \rceil = 3\) in the non-decreasing sorted array \([1, 1, 3, 3, 3, 4]\). The median of the original array \([2, 1, 2, 3, 1, 4]\) is \(2\), as it is the number at index \(\lceil \frac{6}{2} \rceil = 3\) in the non-decreasing sorted array \([1, 1, 2, 2, 3, 4]\). Thus, the median increased (\(3 > 2\)) in two operations. It can be shown that this is the minimum possible number of operations.
|
Input: 832 2 847 3 3 11100000000055 5 5 4 562 1 2 3 1 421 221 145 5 5 5 | Output: 1 2 1 3 2 1 2 3
|
Beginner
| 3 | 822 | 541 | 124 | 19 |
1,362 |
C
|
1362C
|
C. Johnny and Another Rating Drop
| 1,400 |
bitmasks; greedy; math
|
The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills.The boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of \(5 = 101_2\) and \(14 = 1110_2\) equals to \(3\), since \(0101\) and \(1110\) differ in \(3\) positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants.Johnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you've got a sequence of consecutive integers from \(0\) to \(n\). That's strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers.
|
The input consists of multiple test cases. The first line contains one integer \(t\) (\(1 \leq t \leq 10\,000\)) — the number of test cases. The following \(t\) lines contain a description of test cases.The first and only line in each test case contains a single integer \(n\) (\(1 \leq n \leq 10^{18})\).
|
Output \(t\) lines. For each test case, you should output a single line with one integer — the unfairness of the contest if the rating sequence equals to \(0\), \(1\), ..., \(n - 1\), \(n\).
|
For \(n = 5\) we calculate unfairness of the following sequence (numbers from \(0\) to \(5\) written in binary with extra leading zeroes, so they all have the same length): \(000\) \(001\) \(010\) \(011\) \(100\) \(101\) The differences are equal to \(1\), \(2\), \(1\), \(3\), \(1\) respectively, so unfairness is equal to \(1 + 2 + 1 + 3 + 1 = 8\).
|
Input: 5 5 7 11 1 2000000000000 | Output: 8 11 19 1 3999999999987
|
Easy
| 3 | 1,231 | 305 | 190 | 13 |
335 |
E
|
335E
|
E. Counting Skyscrapers
| 2,800 |
dp; math; probabilities
|
A number of skyscrapers have been built in a line. The number of skyscrapers was chosen uniformly at random between 2 and 314! (314 factorial, a very large number). The height of each skyscraper was chosen randomly and independently, with height i having probability 2 - i for all positive integers i. The floors of a skyscraper with height i are numbered 0 through i - 1.To speed up transit times, a number of zip lines were installed between skyscrapers. Specifically, there is a zip line connecting the i-th floor of one skyscraper with the i-th floor of another skyscraper if and only if there are no skyscrapers between them that have an i-th floor.Alice and Bob decide to count the number of skyscrapers.Alice is thorough, and wants to know exactly how many skyscrapers there are. She begins at the leftmost skyscraper, with a counter at 1. She then moves to the right, one skyscraper at a time, adding 1 to her counter each time she moves. She continues until she reaches the rightmost skyscraper.Bob is impatient, and wants to finish as fast as possible. He begins at the leftmost skyscraper, with a counter at 1. He moves from building to building using zip lines. At each stage Bob uses the highest available zip line to the right, but ignores floors with a height greater than h due to fear of heights. When Bob uses a zip line, he travels too fast to count how many skyscrapers he passed. Instead, he just adds 2i to his counter, where i is the number of the floor he's currently on. He continues until he reaches the rightmost skyscraper.Consider the following example. There are 6 buildings, with heights 1, 4, 3, 4, 1, 2 from left to right, and h = 2. Alice begins with her counter at 1 and then adds 1 five times for a result of 6. Bob begins with his counter at 1, then he adds 1, 4, 4, and 2, in order, for a result of 12. Note that Bob ignores the highest zip line because of his fear of heights (h = 2). Bob's counter is at the top of the image, and Alice's counter at the bottom. All zip lines are shown. Bob's path is shown by the green dashed line and Alice's by the pink dashed line. The floors of the skyscrapers are numbered, and the zip lines Bob uses are marked with the amount he adds to his counter.When Alice and Bob reach the right-most skyscraper, they compare counters. You will be given either the value of Alice's counter or the value of Bob's counter, and must compute the expected value of the other's counter.
|
The first line of input will be a name, either string ""Alice"" or ""Bob"". The second line of input contains two integers n and h (2 ≤ n ≤ 30000, 0 ≤ h ≤ 30). If the name is ""Alice"", then n represents the value of Alice's counter when she reaches the rightmost skyscraper, otherwise n represents the value of Bob's counter when he reaches the rightmost skyscraper; h represents the highest floor number Bob is willing to use.
|
Output a single real value giving the expected value of the Alice's counter if you were given Bob's counter, or Bob's counter if you were given Alice's counter. You answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9.
|
In the first example, Bob's counter has a 62.5% chance of being 3, a 25% chance of being 4, and a 12.5% chance of being 5.
|
Input: Alice3 1 | Output: 3.500000000
|
Master
| 3 | 2,448 | 428 | 255 | 3 |
269 |
D
|
269D
|
D. Maximum Waterfall
| 2,600 |
data structures; dp; graphs; sortings
|
Emuskald was hired to design an artificial waterfall according to the latest trends in landscape architecture. A modern artificial waterfall consists of multiple horizontal panels affixed to a wide flat wall. The water flows down the top of the wall from panel to panel until it reaches the bottom of the wall.The wall has height t and has n panels on the wall. Each panel is a horizontal segment at height hi which begins at li and ends at ri. The i-th panel connects the points (li, hi) and (ri, hi) of the plane. The top of the wall can be considered a panel connecting the points ( - 109, t) and (109, t). Similarly, the bottom of the wall can be considered a panel connecting the points ( - 109, 0) and (109, 0). No two panels share a common point.Emuskald knows that for the waterfall to be aesthetically pleasing, it can flow from panel i to panel j () only if the following conditions hold: max(li, lj) < min(ri, rj) (horizontal projections of the panels overlap); hj < hi (panel j is below panel i); there is no such panel k (hj < hk < hi) that the first two conditions hold for the pairs (i, k) and (k, j). Then the flow for is equal to min(ri, rj) - max(li, lj), the length of their horizontal projection overlap.Emuskald has decided that in his waterfall the water will flow in a single path from top to bottom. If water flows to a panel (except the bottom of the wall), the water will fall further to exactly one lower panel. The total amount of water flow in the waterfall is then defined as the minimum horizontal projection overlap between two consecutive panels in the path of the waterfall. Formally: the waterfall consists of a single path of panels ; the flow of the waterfall is the minimum flow in the path . To make a truly great waterfall Emuskald must maximize this water flow, but there are too many panels and he is having a hard time planning his creation. Below is an example of a waterfall Emuskald wants: Help Emuskald maintain his reputation and find the value of the maximum possible water flow.
|
The first line of input contains two space-separated integers n and t (1 ≤ n ≤ 105, 2 ≤ t ≤ 109), the number of the panels excluding the top and the bottom panels, and the height of the wall. Each of the n following lines contain three space-separated integers hi, li and ri (0 < hi < t, - 109 ≤ li < ri ≤ 109), the height, left and right ends of the i-th panel segment.It is guaranteed that no two segments share a common point.
|
Output a single integer — the maximum possible amount of water flow in the desired waterfall.
|
The first test case corresponds to the picture.
|
Input: 5 64 1 63 2 75 9 113 10 151 13 16 | Output: 4
|
Expert
| 4 | 2,028 | 429 | 93 | 2 |
1,881 |
E
|
1881E
|
E. Block Sequence
| 1,500 |
dp
|
Given a sequence of integers \(a\) of length \(n\).A sequence is called beautiful if it has the form of a series of blocks, each starting with its length, i.e., first comes the length of the block, and then its elements. For example, the sequences [\(\color{red}{3},\ \color{red}{3},\ \color{red}{4},\ \color{red}{5},\ \color{green}{2},\ \color{green}{6},\ \color{green}{1}\)] and [\(\color{red}{1},\ \color{red}{8},\ \color{green}{4},\ \color{green}{5},\ \color{green}{2},\ \color{green}{6},\ \color{green}{1}\)] are beautiful (different blocks are colored differently), while [\(1\)], [\(1,\ 4,\ 3\)], [\(3,\ 2,\ 1\)] are not.In one operation, you can remove any element from the sequence. What is the minimum number of operations required to make the given sequence beautiful?
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then follows the descriptions of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the sequence \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)) — the elements of the sequence \(a\).It is guaranteed that the sum of the values of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single number — the minimum number of deletions required to make the sequence \(a\) beautiful.
|
In the first test case of the example, the given sequence is already beautiful, as shown in the statement.In the second test case of the example, the sequence can only be made beautiful by removing all elements from it.In the fifth test case of the example, the sequence can be made beautiful by removing the first and last elements. Then the sequence will become [\(2,\ 3,\ 4\)].
|
Input: 773 3 4 5 2 6 145 6 3 263 4 1 6 7 731 4 351 2 3 4 551 2 3 1 254 5 5 1 5 | Output: 0 4 1 1 2 1 0
|
Medium
| 1 | 779 | 541 | 123 | 18 |
720 |
D
|
720D
|
D. Slalom
| 3,100 |
data structures; dp; sortings
|
Little girl Masha likes winter sports, today she's planning to take part in slalom skiing.The track is represented as a grid composed of n × m squares. There are rectangular obstacles at the track, composed of grid squares. Masha must get from the square (1, 1) to the square (n, m). She can move from a square to adjacent square: either to the right, or upwards. If the square is occupied by an obstacle, it is not allowed to move to that square.One can see that each obstacle can actually be passed in two ways: either it is to the right of Masha's path, or to the left. Masha likes to try all ways to do things, so she would like to know how many ways are there to pass the track. Two ways are considered different if there is an obstacle such that it is to the right of the path in one way, and to the left of the path in the other way.Help Masha to find the number of ways to pass the track. The number of ways can be quite big, so Masha would like to know it modulo 109 + 7.The pictures below show different ways to pass the track in sample tests.
|
The first line of input data contains three positive integers: n, m and k (3 ≤ n, m ≤ 106, 0 ≤ k ≤ 105) — the size of the track and the number of obstacles.The following k lines contain four positive integers each: x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — coordinates of bottom left, and top right squares of the obstacle. It is guaranteed that there are no obstacles at squares (1, 1) and (n, m), and no obstacles overlap (but some of them may touch).
|
Output one integer — the number of ways to pass the track modulo 109 + 7.
|
Input: 3 3 0 | Output: 1
|
Master
| 3 | 1,053 | 463 | 73 | 7 |
|
1,854 |
D
|
1854D
|
D. Michael and Hotel
| 3,000 |
binary search; interactive; trees
|
Michael and Brian are stuck in a hotel with \(n\) rooms, numbered from \(1\) to \(n\), and need to find each other. But this hotel's doors are all locked and the only way of getting around is by using the teleporters in each room. Room \(i\) has a teleporter that will take you to room \(a_i\) (it might be that \(a_i = i\)). But they don't know the values of \(a_1,a_2, \dots, a_n\).Instead, they can call up the front desk to ask queries. In one query, they give a room \(u\), a positive integer \(k\), and a set of rooms \(S\). The hotel concierge answers whether a person starting in room \(u\), and using the teleporters \(k\) times, ends up in a room in \(S\).Brian is in room \(1\). Michael wants to know the set \(A\) of rooms so that if he starts in one of those rooms they can use the teleporters to meet up. He can ask at most \(2000\) queries.The values \(a_1, a_2, \dots, a_n\) are fixed before the start of the interaction and do not depend on your queries. In other words, the interactor is not adaptive.
|
The input contains a single integer \(n\) (\(2 \leq n \leq 500\)).
|
In the sample test, there are \(n=5\) rooms and the (hidden) array describing the behavior of the teleporters is \([1, 2, 1, 3, 2]\). The first query asks whether starting from room number \(a=3\), and using the teleporters \(5\) times, one ends up in one of the two rooms \(S=\{2, 3\}\). This action results in ending up in the room \(1\), so the answer is \(0\). The second query asks whether starting from room number \(a=2\), and using the teleporters \(5\) times, one ends up in one of the two rooms \(S=\{2, 3\}\). This action results in ending up in the room \(2\), so the answer is \(1\).
|
Input: 5 0 1 | Output: ? 3 5 2 2 3 ? 2 5 2 2 3 ! 3 1 3 4
|
Master
| 3 | 1,019 | 66 | 0 | 18 |
|
637 |
A
|
637A
|
A. Voting for Photos
| 1,000 |
*special; constructive algorithms; implementation
|
After celebrating the midcourse the students of one of the faculties of the Berland State University decided to conduct a vote for the best photo. They published the photos in the social network and agreed on the rules to choose a winner: the photo which gets most likes wins. If multiple photoes get most likes, the winner is the photo that gets this number first.Help guys determine the winner photo by the records of likes.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the total likes to the published photoes. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000), where ai is the identifier of the photo which got the i-th like.
|
Print the identifier of the photo which won the elections.
|
In the first test sample the photo with id 1 got two likes (first and fifth), photo with id 2 got two likes (third and fourth), and photo with id 3 got one like (second). Thus, the winner is the photo with identifier 2, as it got: more likes than the photo with id 3; as many likes as the photo with id 1, but the photo with the identifier 2 got its second like earlier.
|
Input: 51 3 2 2 1 | Output: 2
|
Beginner
| 3 | 426 | 262 | 58 | 6 |
1,326 |
A
|
1326A
|
A. Bad Ugly Numbers
| 1,000 |
constructive algorithms; number theory
|
You are given a integer \(n\) (\(n > 0\)). Find any integer \(s\) which satisfies these conditions, or report that there are no such numbers:In the decimal representation of \(s\): \(s > 0\), \(s\) consists of \(n\) digits, no digit in \(s\) equals \(0\), \(s\) is not divisible by any of it's digits.
|
The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 400\)), the number of test cases. The next \(t\) lines each describe a test case.Each test case contains one positive integer \(n\) (\(1 \leq n \leq 10^5\)).It is guaranteed that the sum of \(n\) for all test cases does not exceed \(10^5\).
|
For each test case, print an integer \(s\) which satisfies the conditions described above, or ""-1"" (without quotes), if no such number exists. If there are multiple possible solutions for \(s\), print any solution.
|
In the first test case, there are no possible solutions for \(s\) consisting of one digit, because any such solution is divisible by itself.For the second test case, the possible solutions are: \(23\), \(27\), \(29\), \(34\), \(37\), \(38\), \(43\), \(46\), \(47\), \(49\), \(53\), \(54\), \(56\), \(57\), \(58\), \(59\), \(67\), \(68\), \(69\), \(73\), \(74\), \(76\), \(78\), \(79\), \(83\), \(86\), \(87\), \(89\), \(94\), \(97\), and \(98\).For the third test case, one possible solution is \(239\) because \(239\) is not divisible by \(2\), \(3\) or \(9\) and has three digits (none of which equals zero).
|
Input: 4 1 2 3 4 | Output: -1 57 239 6789
|
Beginner
| 2 | 301 | 360 | 216 | 13 |
1,559 |
B
|
1559B
|
B. Mocha and Red and Blue
| 900 |
dp; greedy
|
As their story unravels, a timeless tale is told once again...Shirahime, a friend of Mocha's, is keen on playing the music game Arcaea and sharing Mocha interesting puzzles to solve. This day, Shirahime comes up with a new simple puzzle and wants Mocha to solve them. However, these puzzles are too easy for Mocha to solve, so she wants you to solve them and tell her the answers. The puzzles are described as follow.There are \(n\) squares arranged in a row, and each of them can be painted either red or blue.Among these squares, some of them have been painted already, and the others are blank. You can decide which color to paint on each blank square.Some pairs of adjacent squares may have the same color, which is imperfect. We define the imperfectness as the number of pairs of adjacent squares that share the same color.For example, the imperfectness of ""BRRRBBR"" is \(3\), with ""BB"" occurred once and ""RR"" occurred twice.Your goal is to minimize the imperfectness and print out the colors of the squares after painting.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. Each test case consists of two lines.The first line of each test case contains an integer \(n\) (\(1\leq n\leq 100\)) — the length of the squares row.The second line of each test case contains a string \(s\) with length \(n\), containing characters 'B', 'R' and '?'. Here 'B' stands for a blue square, 'R' for a red square, and '?' for a blank square.
|
For each test case, print a line with a string only containing 'B' and 'R', the colors of the squares after painting, which imperfectness is minimized. If there are multiple solutions, print any of them.
|
In the first test case, if the squares are painted ""BRRBRBR"", the imperfectness is \(1\) (since squares \(2\) and \(3\) have the same color), which is the minimum possible imperfectness.
|
Input: 5 7 ?R???BR 7 ???R??? 1 ? 1 B 10 ?R??RB??B? | Output: BRRBRBR BRBRBRB B B BRRBRBBRBR
|
Beginner
| 2 | 1,034 | 488 | 203 | 15 |
1,115 |
G2
|
1115G2
|
G2. OR oracle
| 1,600 |
*special
|
Implement a quantum oracle on \(N\) qubits which implements the following function: $$$\(f(\vec{x}) = x_0 \vee x_1 \vee \dotsb \vee x_{N-1}\)\(You have to implement an operation which takes the following inputs: an array of \)N\( (\)1 \le N \le 8\() qubits \)x\( in an arbitrary state (input register), a qubit \)y\( in an arbitrary state (output qubit),and performs a transformation \)|x\rangle|y\rangle \rightarrow |x\rangle|y \oplus f(x)\rangle$$$. The operation doesn't have an output; its ""output"" is the state in which it leaves the qubits.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (x : Qubit[], y : Qubit) : Unit { body (...) { // your code here } adjoint auto; }}
|
Medium
| 1 | 778 | 0 | 0 | 11 |
||||
1,477 |
C
|
1477C
|
C. Nezzar and Nice Beatmap
| 2,200 |
constructive algorithms; geometry; greedy; math; sortings
|
Nezzar loves the game osu!.osu! is played on beatmaps, which can be seen as an array consisting of distinct points on a plane. A beatmap is called nice if for any three consecutive points \(A,B,C\) listed in order, the angle between these three points, centered at \(B\), is strictly less than \(90\) degrees. Points \(A,B,C\) on the left have angle less than \(90\) degrees, so they can be three consecutive points of a nice beatmap; Points \(A',B',C'\) on the right have angle greater or equal to \(90\) degrees, so they cannot be three consecutive points of a nice beatmap. Now Nezzar has a beatmap of \(n\) distinct points \(A_1,A_2,\ldots,A_n\). Nezzar would like to reorder these \(n\) points so that the resulting beatmap is nice.Formally, you are required to find a permutation \(p_1,p_2,\ldots,p_n\) of integers from \(1\) to \(n\), such that beatmap \(A_{p_1},A_{p_2},\ldots,A_{p_n}\) is nice. If it is impossible, you should determine it.
|
The first line contains a single integer \(n\) (\(3 \le n \le 5000\)).Then \(n\) lines follow, \(i\)-th of them contains two integers \(x_i\), \(y_i\) (\(-10^9 \le x_i, y_i \le 10^9\)) — coordinates of point \(A_i\).It is guaranteed that all points are distinct.
|
If there is no solution, print \(-1\).Otherwise, print \(n\) integers, representing a valid permutation \(p\).If there are multiple possible answers, you can print any.
|
Here is the illustration for the first test: Please note that the angle between \(A_1\), \(A_2\) and \(A_5\), centered at \(A_2\), is treated as \(0\) degrees. However, angle between \(A_1\), \(A_5\) and \(A_2\), centered at \(A_5\), is treated as \(180\) degrees.
|
Input: 5 0 0 5 0 4 2 2 1 3 0 | Output: 1 2 5 3 4
|
Hard
| 5 | 949 | 262 | 168 | 14 |
101 |
D
|
101D
|
D. Castle
| 2,300 |
dp; greedy; probabilities; sortings; trees
|
Gerald is positioned in an old castle which consists of n halls connected with n - 1 corridors. It is exactly one way to go from any hall to any other one. Thus, the graph is a tree. Initially, at the moment of time 0, Gerald is positioned in hall 1. Besides, some other hall of the castle contains the treasure Gerald is looking for. The treasure's position is not known; it can equiprobably be in any of other n - 1 halls. Gerald can only find out where the treasure is when he enters the hall with the treasure. That very moment Gerald sees the treasure and the moment is regarded is the moment of achieving his goal. The corridors have different lengths. At that, the corridors are considered long and the halls are considered small and well lit. Thus, it is possible not to take the time Gerald spends in the halls into consideration. The castle is very old, that's why a corridor collapses at the moment when somebody visits it two times, no matter in which direction. Gerald can move around the castle using the corridors; he will go until he finds the treasure. Naturally, Gerald wants to find it as quickly as possible. In other words, he wants to act in a manner that would make the average time of finding the treasure as small as possible. Each corridor can be used no more than two times. That's why Gerald chooses the strategy in such a way, so he can visit every hall for sure.More formally, if the treasure is located in the second hall, then Gerald will find it the moment he enters the second hall for the first time — let it be moment t2. If the treasure is in the third hall, then Gerald will find it the moment he enters the third hall for the first time. Let it be the moment of time t3. And so on. Thus, the average time of finding the treasure will be equal to .
|
The first line contains the only integer n (2 ≤ n ≤ 105) — the number of halls in the castle. Next n - 1 lines each contain three integers. The i-th line contains numbers ai, bi and ti (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ti ≤ 1000) — the numbers of halls connected with the i-th corridor and the time needed to go along the corridor. Initially Gerald is in the hall number 1. It is guaranteed that one can get from any hall to any other one using corridors.
|
Print the only real number: the sought expectation of time needed to find the treasure. The answer should differ from the right one in no less than 10 - 6.
|
In the first test the castle only has two halls which means that the treasure is located in the second hall. Gerald will only need one minute to go to the second hall from the first one.In the second test Gerald can only go from the first hall to the third one. He can get from the third room to the first one or to the second one, but he has already visited the first hall and can get nowhere from there. Thus, he needs to go to the second hall. He should go to hall 4 from there, because all other halls have already been visited. If the treasure is located in the third hall, Gerald will find it in a minute, if the treasure is located in the second hall, Gerald finds it in two minutes, if the treasure is in the fourth hall, Gerald will find it in three minutes. The average time makes 2 minutes.In the third test Gerald needs to visit 4 halls: the second, third, fourth and fifth ones. All of them are only reachable from the first hall. Thus, he needs to go to those 4 halls one by one and return. Gerald will enter the first of those halls in a minute, in the second one — in three minutes, in the third one - in 5 minutes, in the fourth one - in 7 minutes. The average time is 4 minutes.
|
Input: 21 2 1 | Output: 1.0
|
Expert
| 5 | 1,786 | 451 | 155 | 1 |
1,820 |
A
|
1820A
|
A. Yura's New Name
| 800 |
implementation; strings
|
After holding one team contest, boy Yura got very tired and wanted to change his life and move to Japan. In honor of such a change, Yura changed his name to something nice.Fascinated by this idea he already thought up a name \(s\) consisting only of characters ""_"" and ""^"". But there's a problem — Yura likes smiley faces ""^_^"" and ""^^"". Therefore any character of the name must be a part of at least one such smiley. Note that only the consecutive characters of the name can be a smiley face.More formally, consider all occurrences of the strings ""^_^"" and ""^^"" in the string \(s\). Then all such occurrences must cover the whole string \(s\), possibly with intersections. For example, in the string ""^^__^_^^__^"" the characters at positions \(3,4,9,10\) and \(11\) are not contained inside any smileys, and the other characters at positions \(1,2,5,6,7\) and \(8\) are contained inside smileys.In one operation Jura can insert one of the characters ""_"" and ""^"" into his name \(s\) (you can insert it at any position in the string). He asks you to tell him the minimum number of operations you need to do to make the name fit Yura's criteria.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) —the number of test cases. The description of test cases follows.The first and only line of each test case contains a single string \(s\) (\(1 \leq |s| \leq 100\)), consisting of characters ""_"" and ""^"", — the name to change.
|
For each test case, output a single integer — the minimum number of characters you need to add to the name to make it fit for Yura. If you don't need to change anything in the name, print \(0\).
|
In the first test case, you can get the following name by adding \(5\) characters: ^_^_^_^_^_^_^In the third test case, we can add one character ""^"" to the end of the name, then we get the name:^_^In the fourth test case, we can add one character ""^"" to the end of the name, then we get the name:^^In the fifth test case, all of the characters are already contained in smiley faces, so the answer is \(0\).In the seventh test case, you can add one character ""^"" at the beginning of the name and one character ""^"" at the end of the name, then you get the name:^_^
|
Input: 7^______^___^_^^^_^___^^_^^_^^^^^_^_^^___^^_ | Output: 5 5 1 1 0 3 2
|
Beginner
| 2 | 1,161 | 340 | 194 | 18 |
1,738 |
H
|
1738H
|
H. Palindrome Addicts
| 3,300 |
data structures; strings
|
Your task is to maintain a queue consisting of lowercase English letters as follows: ""push \(c\)"": insert a letter \(c\) at the back of the queue; ""pop"": delete a letter from the front of the queue. Initially, the queue is empty. After each operation, you are asked to count the number of distinct palindromic substrings in the string that are obtained by concatenating the letters from the front to the back of the queue.Especially, the number of distinct palindromic substrings of the empty string is \(0\). A string \(s[1..n]\) of length \(n\) is palindromic if \(s[i] = s[n-i+1]\) for every \(1 \leq i \leq n\). The string \(s[l..r]\) is a substring of string \(s[1..n]\) for every \(1 \leq l \leq r \leq n\). Two strings \(s[1..n]\) and \(t[1..m]\) are distinct, if at least one of the following holds. \(n \neq m\); \(s[i] \neq t[i]\) for some \(1 \leq i \leq \min\{n,m\}\).
|
The first line is an integer \(q\) (\(1 \leq q \leq 10^6\)), indicating the number of operations.Then \(q\) lines follow. Each of the following lines contains one of the operations as follows. ""push \(c\)"": insert a letter \(c\) at the back of the queue, where \(c\) is a lowercase English letter; ""pop"": delete a letter from the front of the queue. It is guaranteed that no ""pop"" operation will be performed when the queue is empty.
|
After each operation, print the number of distinct palindromic substrings in the string presented in the queue.
|
Let \(s_k\) be the string presented in the queue after the \(k\)-th operation, and let \(c_k\) be the number of distinct palindromic substrings of \(s_k\). The following table shows the details of the example. \(k\)\(s_k\)\(c_k\)\(1\)\(a\)\(1\)\(2\)\(\textsf{empty}\)\(0\)\(3\)\(a\)\(1\)\(4\)\(aa\)\(2\)\(5\)\(aab\)\(3\)\(6\)\(aabb\)\(4\)\(7\)\(aabba\)\(5\)\(8\)\(aabbaa\)\(6\)\(9\)\(abbaa\)\(5\)\(10\)\(bbaa\)\(4\)\(11\)\(baa\)\(3\)\(12\)\(baab\)\(4\) It is worth pointing out that After the \(2\)-nd operation, the string is empty and thus has no substrings. So the answer is \(0\); After the \(8\)-th operation, the string is ""\(aabbaa\)"". The \(6\) distinct palindromic substrings are ""\(a\)"", ""\(aa\)"", ""\(aabbaa\)"", ""\(abba\)"", ""\(b\)"", and ""\(bb\)"".
|
Input: 12 push a pop push a push a push b push b push a push a pop pop pop push b | Output: 1 0 1 2 3 4 5 6 5 4 3 4
|
Master
| 2 | 884 | 439 | 111 | 17 |
2,061 |
G
|
2061G
|
G. Kevin and Teams
| 2,900 |
constructive algorithms; graphs; interactive
|
This is an interactive problem. Kevin has \(n\) classmates, numbered \(1, 2, \ldots, n\). Any two of them may either be friends or not friends.Kevin wants to select \(2k\) classmates to form \(k\) teams, where each team contains exactly \(2\) people. Each person can belong to at most one team.Let \(u_i\) and \(v_i\) be two people in the \(i\)-th team. To avoid potential conflicts during team formation, the team members must satisfy one of the following two conditions: For all \(i\) (\(1\leq i \leq k\)), classmate \(u_i\) and \(v_i\) are friends. For all \(i\) (\(1\leq i \leq k\)), classmate \(u_i\) and \(v_i\) are not friends. Kevin wants to determine the maximum \(k\) such that, regardless of the friendship relationships among the \(n\) people, he can always find \(2k\) people to form the teams. After that, he needs to form \(k\) teams. But asking whether two classmates are friends is awkward, so Kevin wants to achieve this while asking about the friendship status of no more than \(n\) pairs of classmates.The interactor is adaptive. It means that the hidden relationship between classmates is not fixed before the interaction and will change during the interaction.
|
In the first test case:Kevin claims he can form \(1\) team regardless of the friendship relationships among the \(3\) people.Kevin asks about the friendship relationship between people \(1\) and \(2\). The jury responds that they are friends.Kevin answers that he can form a team with people \(1\) and \(2\).In the second test case:Kevin claims he can form \(2\) teams regardless of the friendship relationships among the \(5\) people.Kevin asks about the friendship relationship between people \((1, 2), (3, 4), (3, 5), (1, 3), (2, 4)\). The jury responds with \(1, 0, 1, 0, 0\).Kevin answers that he can form two teams with people \((1, 2)\) and \((3, 5)\).It is also possible to form two teams with people \((1, 3)\) and \((2, 4)\), since they are both not friends.
|
Input: 2 3 1 5 1 0 1 0 0 | Output: 1 ? 1 2 ! 1 2 2 ? 1 2 ? 3 4 ? 3 5 ? 1 3 ? 2 4 ! 1 2 3 5
|
Master
| 3 | 1,182 | 0 | 0 | 20 |
||
472 |
F
|
472F
|
F. Design Tutorial: Change the Goal
| 2,700 |
constructive algorithms; math; matrices
|
There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given n integers x1, x2, ..., xn. You are allowed to perform the assignments (as many as you want) of the following form xi ^= xj (in the original task i and j must be different, but in this task we allow i to equal j). The goal is to maximize the sum of all xi.Now we just change the goal. You are also given n integers y1, y2, ..., yn. You should make x1, x2, ..., xn exactly equal to y1, y2, ..., yn. In other words, for each i number xi should be equal to yi.
|
The first line contains an integer n (1 ≤ n ≤ 10000). The second line contains n integers: x1 to xn (0 ≤ xi ≤ 109). The third line contains n integers: y1 to yn (0 ≤ yi ≤ 109).
|
If there is no solution, output -1.If there is a solution, then in the first line output an integer m (0 ≤ m ≤ 1000000) – the number of assignments you need to perform. Then print m lines, each line should contain two integers i and j (1 ≤ i, j ≤ n), which denote assignment xi ^= xj.If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
|
Assignment a ^= b denotes assignment a = a ^ b, where operation ""^"" is bitwise XOR of two integers.
|
Input: 23 56 0 | Output: 21 22 2
|
Master
| 3 | 814 | 176 | 481 | 4 |
1,844 |
D
|
1844D
|
D. Row Major
| 1,400 |
constructive algorithms; greedy; math; number theory; strings
|
The row-major order of an \(r \times c\) grid of characters \(A\) is the string obtained by concatenating all the rows, i.e. $$$\( A_{11}A_{12} \dots A_{1c}A_{21}A_{22} \dots A_{2c} \dots A_{r1}A_{r2} \dots A_{rc}. \)\(A grid of characters \)A\( is bad if there are some two adjacent cells (cells sharing an edge) with the same character.You are given a positive integer \)n\(. Consider all strings \)s\( consisting of only lowercase Latin letters such that they are not the row-major order of any bad grid. Find any string with the minimum number of distinct characters among all such strings of length \)n$$$.It can be proven that at least one such string exists under the constraints of the problem.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output a string with the minimum number of distinct characters among all suitable strings of length \(n\).If there are multiple solutions, print any of them.
|
In the first test case, there are \(3\) ways \(s\) can be the row-major order of a grid, and they are all not bad: tththathatat It can be proven that \(3\) distinct characters is the minimum possible.In the second test case, there are \(2\) ways \(s\) can be the row-major order of a grid, and they are both not bad: iiss It can be proven that \(2\) distinct characters is the minimum possible.In the third test case, there is only \(1\) way \(s\) can be the row-major order of a grid, and it is not bad.In the fourth test case, there are \(4\) ways \(s\) can be the row-major order of a grid, and they are all not bad: ttotomtomatoomaatomtoato It can be proven that \(4\) distinct characters is the minimum possible. Note that, for example, the string ""orange"" is not an acceptable output because it has \(6 > 4\) distinct characters, and the string ""banana"" is not an acceptable output because it is the row-major order of the following bad grid: banana
|
Input: 44216 | Output: that is a tomato
|
Easy
| 5 | 702 | 332 | 177 | 18 |
1,797 |
C
|
1797C
|
C. Li Hua and Chess
| 1,600 |
constructive algorithms; greedy; interactive
|
This is an interactive problem.Li Ming and Li Hua are playing a game. Li Hua has a chessboard of size \(n\times m\). Denote \((r, c)\) (\(1\le r\le n, 1\le c\le m\)) as the cell on the \(r\)-th row from the top and on the \(c\)-th column from the left. Li Ming put a king on the chessboard and Li Hua needs to guess its position.Li Hua can ask Li Ming no more than \(3\) questions. In each question, he can choose a cell and ask the minimum steps needed to move the king to the chosen cell. Each question is independent, which means the king doesn't actually move.A king can move from \((x,y)\) to \((x',y')\) if and only if \(\max\{|x-x'|,|y-y'|\}=1\) (shown in the following picture). The position of the king is chosen before the interaction.Suppose you were Li Hua, please solve this problem.
|
In test case 1, the king is at \((2,2)\). It takes \(1\) step to move to \((2,3)\) and \(2\) steps to move to \((2,4)\). Note that the questions may not seem sensible. They are just a sample of questions you may ask.
|
Input: 2 3 4 1 2 5 3 3 1 2 | Output: ? 2 3 ? 2 4 ! 2 2 ? 2 2 ? 5 2 ? 5 3 ! 5 1
|
Medium
| 3 | 796 | 0 | 0 | 17 |
||
1,680 |
B
|
1680B
|
B. Robots
| 800 |
implementation
|
There is a field divided into \(n\) rows and \(m\) columns. Some cells are empty (denoted as E), other cells contain robots (denoted as R).You can send a command to all robots at the same time. The command can be of one of the four types: move up; move right; move down; move left. When you send a command, all robots at the same time attempt to take one step in the direction you picked. If a robot tries to move outside the field, it explodes; otherwise, every robot moves to an adjacent cell in the chosen direction.You can send as many commands as you want (possibly, zero), in any order. Your goal is to make at least one robot reach the upper left corner of the field. Can you do this without forcing any of the robots to explode?
|
The first line contains one integer \(t\) (\(1 \le t \le 5000\)) — the number of test cases.Each test case starts with a line containing two integers \(n\) and \(m\) (\(1 \le n, m \le 5\)) — the number of rows and the number of columns, respectively. Then \(n\) lines follow; each of them contains a string of \(m\) characters. Each character is either E (empty cell} or R (robot).Additional constraint on the input: in each test case, there is at least one robot on the field.
|
If it is possible to make at least one robot reach the upper left corner of the field so that no robot explodes, print YES. Otherwise, print NO.
|
Explanations for test cases of the example: in the first test case, it is enough to send a command to move left. in the second test case, if you try to send any command, at least one robot explodes. in the third test case, it is enough to send a command to move left. in the fourth test case, there is already a robot in the upper left corner. in the fifth test case, the sequence ""move up, move left, move up"" leads one robot to the upper left corner; in the sixth test case, if you try to move any robot to the upper left corner, at least one other robot explodes.
|
Input: 61 3ERR2 2ERRE2 2ERER1 1R4 3EEEEEEERREER3 3EEEEERREE | Output: YES NO YES YES YES NO
|
Beginner
| 1 | 736 | 477 | 144 | 16 |
1,656 |
F
|
1656F
|
F. Parametric MST
| 2,600 |
binary search; constructive algorithms; graphs; greedy; math; sortings
|
You are given \(n\) integers \(a_1, a_2, \ldots, a_n\). For any real number \(t\), consider the complete weighted graph on \(n\) vertices \(K_n(t)\) with weight of the edge between vertices \(i\) and \(j\) equal to \(w_{ij}(t) = a_i \cdot a_j + t \cdot (a_i + a_j)\). Let \(f(t)\) be the cost of the minimum spanning tree of \(K_n(t)\). Determine whether \(f(t)\) is bounded above and, if so, output the maximum value it attains.
|
The input consists of multiple test cases. The first line contains a single integer \(T\) (\(1 \leq T \leq 10^4\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) — the number of vertices of the graph.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^6 \leq a_i \leq 10^6\)).The sum of \(n\) for all test cases is at most \(2 \cdot 10^5\).
|
For each test case, print a single line with the maximum value of \(f(t)\) (it can be shown that it is an integer), or INF if \(f(t)\) is not bounded above.
|
Input: 521 02-1 131 -1 -233 -1 -241 2 3 -4 | Output: INF -1 INF -6 -18
|
Expert
| 6 | 429 | 490 | 156 | 16 |
|
23 |
C
|
23C
|
C. Oranges and Apples
| 2,500 |
constructive algorithms; sortings
|
In 2N - 1 boxes there are apples and oranges. Your task is to choose N boxes so, that they will contain not less than half of all the apples and not less than half of all the oranges.
|
The first input line contains one number T — amount of tests. The description of each test starts with a natural number N — amount of boxes. Each of the following 2N - 1 lines contains numbers ai and oi — amount of apples and oranges in the i-th box (0 ≤ ai, oi ≤ 109). The sum of N in all the tests in the input doesn't exceed 105. All the input numbers are integer.
|
For each test output two lines. In the first line output YES, if it's possible to choose N boxes, or NO otherwise. If the answer is positive output in the second line N numbers — indexes of the chosen boxes. Boxes are numbered from 1 in the input order. Otherwise leave the second line empty. Separate the numbers with one space.
|
Input: 2210 155 720 1810 0 | Output: YES1 3YES1
|
Expert
| 2 | 183 | 367 | 329 | 0 |
|
630 |
F
|
630F
|
F. Selection of Personnel
| 1,300 |
combinatorics; math
|
One company of IT City decided to create a group of innovative developments consisting from 5 to 7 people and hire new employees for it. After placing an advertisment the company received n resumes. Now the HR department has to evaluate each possible group composition and select one of them. Your task is to count the number of variants of group composition to evaluate.
|
The only line of the input contains one integer n (7 ≤ n ≤ 777) — the number of potential employees that sent resumes.
|
Output one integer — the number of different variants of group composition.
|
Input: 7 | Output: 29
|
Easy
| 2 | 371 | 118 | 75 | 6 |
|
1,740 |
F
|
1740F
|
F. Conditional Mix
| 2,600 |
combinatorics; dp; math
|
Pak Chanek is given an array \(a\) of \(n\) integers. For each \(i\) (\(1 \leq i \leq n\)), Pak Chanek will write the one-element set \(\{a_i\}\) on a whiteboard.After that, in one operation, Pak Chanek may do the following: Choose two different sets \(S\) and \(T\) on the whiteboard such that \(S \cap T = \varnothing\) (\(S\) and \(T\) do not have any common elements). Erase \(S\) and \(T\) from the whiteboard and write \(S \cup T\) (the union of \(S\) and \(T\)) onto the whiteboard. After performing zero or more operations, Pak Chanek will construct a multiset \(M\) containing the sizes of all sets written on the whiteboard. In other words, each element in \(M\) corresponds to the size of a set after the operations.How many distinct\(^\dagger\) multisets \(M\) can be created by this process? Since the answer may be large, output it modulo \(998\,244\,353\).\(^\dagger\) Multisets \(B\) and \(C\) are different if and only if there exists a value \(k\) such that the number of elements with value \(k\) in \(B\) is different than the number of elements with value \(k\) in \(C\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 2000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)).
|
Output the number of distinct multisets \(M\) modulo \(998\,244\,353\).
|
In the first example, the possible multisets \(M\) are \(\{1,1,1,1,1,1\}\), \(\{1,1,1,1,2\}\), \(\{1,1,1,3\}\), \(\{1,1,2,2\}\), \(\{1,1,4\}\), \(\{1,2,3\}\), and \(\{2,2,2\}\).As an example, let's consider a possible sequence of operations. In the beginning, the sets are \(\{1\}\), \(\{1\}\), \(\{2\}\), \(\{1\}\), \(\{4\}\), and \(\{3\}\). Do an operation on sets \(\{1\}\) and \(\{3\}\). Now, the sets are \(\{1\}\), \(\{1\}\), \(\{2\}\), \(\{4\}\), and \(\{1,3\}\). Do an operation on sets \(\{2\}\) and \(\{4\}\). Now, the sets are \(\{1\}\), \(\{1\}\), \(\{1,3\}\), and \(\{2,4\}\). Do an operation on sets \(\{1,3\}\) and \(\{2,4\}\). Now, the sets are \(\{1\}\), \(\{1\}\), and \(\{1,2,3,4\}\). The multiset \(M\) that is constructed is \(\{1,1,4\}\).
|
Input: 6 1 1 2 1 4 3 | Output: 7
|
Expert
| 3 | 1,092 | 160 | 71 | 17 |
1,921 |
E
|
1921E
|
E. Eat the Chip
| 1,600 |
brute force; games; greedy; math
|
Alice and Bob are playing a game on a checkered board. The board has \(h\) rows, numbered from top to bottom, and \(w\) columns, numbered from left to right. Both players have a chip each. Initially, Alice's chip is located at the cell with coordinates \((x_a, y_a)\) (row \(x_a\), column \(y_a\)), and Bob's chip is located at \((x_b, y_b)\). It is guaranteed that the initial positions of the chips do not coincide. Players take turns making moves, with Alice starting.On her turn, Alice can move her chip one cell down or one cell down-right or down-left (diagonally). Bob, on the other hand, moves his chip one cell up, up-right, or up-left. It is not allowed to make moves that go beyond the board boundaries.More formally, if at the beginning of Alice's turn she is in the cell with coordinates \((x_a, y_a)\), then she can move her chip to one of the cells \((x_a + 1, y_a)\), \((x_a + 1, y_a - 1)\), or \((x_a + 1, y_a + 1)\). Bob, on his turn, from the cell \((x_b, y_b)\) can move to \((x_b - 1, y_b)\), \((x_b - 1, y_b - 1)\), or \((x_b - 1, y_b + 1)\). The new chip coordinates \((x', y')\) must satisfy the conditions \(1 \le x' \le h\) and \(1 \le y' \le w\). Example game state. Alice plays with the white chip, Bob with the black one. Arrows indicate possible moves. A player immediately wins if they place their chip in a cell occupied by the other player's chip. If either player cannot make a move (Alice—if she is in the last row, i.e. \(x_a = h\), Bob—if he is in the first row, i.e. \(x_b = 1\)), the game immediately ends in a draw.What will be the outcome of the game if both opponents play optimally?
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. This is followed by the description of the test cases.Each test case consists of a single line containing six integers \(h\), \(w\), \(x_a\), \(y_a\), \(x_b\), \(y_b\) (\(1 \le x_a, x_b \le h \le 10^6\), \(1 \le y_a, y_b \le w \le 10^9\)) — the dimensions of the board and the initial positions of Alice's and Bob's chips. It is guaranteed that either \(x_a \ne x_b\) or \(y_a \ne y_b\).It is guaranteed that the sum of \(h\) over all test cases does not exceed \(10^6\).
|
For each test case, output ""Alice"" if Alice wins, ""Bob"" if Bob wins, and ""Draw"" if neither player can secure a victory. You can output each letter in any case (lowercase or uppercase). For example, the strings ""bOb"", ""bob"", ""Bob"", and ""BOB"" will be accepted as Bob's victory.
|
Input: 126 5 2 2 5 34 1 2 1 4 11 4 1 3 1 15 5 1 4 5 24 4 1 1 4 410 10 1 6 10 810 10 2 6 10 710 10 9 1 8 110 10 8 1 10 210 10 1 1 2 110 10 1 3 4 110 10 3 1 1 1 | Output: Alice Bob Draw Draw Draw Alice Draw Draw Bob Alice Alice Draw
|
Medium
| 4 | 1,625 | 612 | 289 | 19 |
|
645 |
E
|
645E
|
E. Intellectual Inquiry
| 2,200 |
dp; greedy; strings
|
After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first k English letters.Each morning, Bessie travels to school along a sidewalk consisting of m + n tiles. In order to help Bessie review, Mr. Moozing has labeled each of the first m sidewalk tiles with one of the first k lowercase English letters, spelling out a string t. Mr. Moozing, impressed by Bessie's extensive knowledge of farm animals, plans to let her finish labeling the last n tiles of the sidewalk by herself.Consider the resulting string s (|s| = m + n) consisting of letters labeled on tiles in order from home to school. For any sequence of indices p1 < p2 < ... < pq we can define subsequence of the string s as string sp1sp2... spq. Two subsequences are considered to be distinct if they differ as strings. Bessie wants to label the remaining part of the sidewalk such that the number of distinct subsequences of tiles is maximum possible. However, since Bessie hasn't even finished learning the alphabet, she needs your help!Note that empty subsequence also counts.
|
The first line of the input contains two integers n and k (0 ≤ n ≤ 1 000 000, 1 ≤ k ≤ 26).The second line contains a string t (|t| = m, 1 ≤ m ≤ 1 000 000) consisting of only first k lowercase English letters.
|
Determine the maximum number of distinct subsequences Bessie can form after labeling the last n sidewalk tiles each with one of the first k lowercase English letters. Since this number can be rather large, you should print it modulo 109 + 7.Please note, that you are not asked to maximize the remainder modulo 109 + 7! The goal is to maximize the initial value and then print the remainder.
|
In the first sample, the optimal labeling gives 8 different subsequences: """" (the empty string), ""a"", ""c"", ""b"", ""ac"", ""ab"", ""cb"", and ""acb"". In the second sample, the entire sidewalk is already labeled. The are 10 possible different subsequences: """" (the empty string), ""a"", ""b"", ""aa"", ""ab"", ""ba"", ""aaa"", ""aab"", ""aba"", and ""aaba"". Note that some strings, including ""aa"", can be obtained with multiple sequences of tiles, but are only counted once.
|
Input: 1 3ac | Output: 8
|
Hard
| 3 | 1,204 | 208 | 390 | 6 |
527 |
D
|
527D
|
D. Clique Problem
| 1,800 |
data structures; dp; greedy; implementation; sortings
|
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.Find the size of the maximum clique in such graph.
|
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
|
Print a single number — the number of vertexes in the maximum clique of the given graph.
|
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!The picture for the sample test.
|
Input: 42 33 16 10 2 | Output: 3
|
Medium
| 5 | 921 | 226 | 88 | 5 |
2,060 |
E
|
2060E
|
E. Graph Composition
| 1,500 |
dfs and similar; dsu; graphs; greedy
|
You are given two simple undirected graphs \(F\) and \(G\) with \(n\) vertices. \(F\) has \(m_1\) edges while \(G\) has \(m_2\) edges. You may perform one of the following two types of operations any number of times: Select two integers \(u\) and \(v\) (\(1 \leq u,v \leq n\)) such that there is an edge between \(u\) and \(v\) in \(F\). Then, remove that edge from \(F\). Select two integers \(u\) and \(v\) (\(1 \leq u,v \leq n\)) such that there is no edge between \(u\) and \(v\) in \(F\). Then, add an edge between \(u\) and \(v\) in \(F\). Determine the minimum number of operations required such that for all integers \(u\) and \(v\) (\(1 \leq u,v \leq n\)), there is a path from \(u\) to \(v\) in \(F\) if and only if there is a path from \(u\) to \(v\) in \(G\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of independent test cases.The first line of each test case contains three integers \(n\), \(m_1\), and \(m_2\) (\(1 \leq n \leq 2\cdot 10^5\), \(0 \leq m_1,m_2 \leq 2\cdot 10^5\)) — the number of vertices, the number of edges in \(F\), and the number of edges in \(G\).The following \(m_1\) lines each contain two integers \(u\) and \(v\) (\(1 \leq u, v\leq n\)) — there is an edge between \(u\) and \(v\) in \(F\). It is guaranteed that there are no repeated edges or self loops.The following \(m_2\) lines each contain two integers \(u\) and \(v\) (\(1 \leq u,v\leq n\)) — there is an edge between \(u\) and \(v\) in \(G\). It is guaranteed that there are no repeated edges or self loops.It is guaranteed that the sum of \(n\), the sum of \(m_1\), and the sum of \(m_2\) over all test cases do not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer denoting the minimum operations required on a new line.
|
In the first test case you can perform the following three operations: Add an edge between vertex \(1\) and vertex \(3\). Remove the edge between vertex \(1\) and vertex \(2\). Remove the edge between vertex \(2\) and vertex \(3\). It can be shown that fewer operations cannot be achieved.In the second test case, \(F\) and \(G\) already fulfill the condition in the beginning.In the fifth test case, the edges from \(1\) to \(3\) and from \(2\) to \(3\) must both be removed.
|
Input: 53 2 11 22 31 32 1 11 21 23 2 03 21 21 0 03 3 11 21 32 31 2 | Output: 3 0 2 0 2
|
Medium
| 4 | 771 | 903 | 99 | 20 |
2,030 |
D
|
2030D
|
D. QED's Favorite Permutation
| 1,700 |
data structures; implementation; sortings
|
QED is given a permutation\(^{\text{∗}}\) \(p\) of length \(n\). He also has a string \(s\) of length \(n\) containing only characters \(\texttt{L}\) and \(\texttt{R}\). QED only likes permutations that are sorted in non-decreasing order. To sort \(p\), he can select any of the following operations and perform them any number of times: Choose an index \(i\) such that \(s_i = \texttt{L}\). Then, swap \(p_i\) and \(p_{i-1}\). It is guaranteed that \(s_1 \neq \texttt{L}\). Choose an index \(i\) such that \(s_i = \texttt{R}\). Then, swap \(p_i\) and \(p_{i+1}\). It is guaranteed that \(s_n \neq \texttt{R}\). He is also given \(q\) queries. In each query, he selects an index \(i\) and changes \(s_i\) from \(\texttt{L}\) to \(\texttt{R}\) (or from \(\texttt{R}\) to \(\texttt{L}\)). Note that the changes are persistent. After each query, he asks you if it is possible to sort \(p\) in non-decreasing order by performing the aforementioned operations any number of times. Note that before answering each query, the permutation \(p\) is reset to its original form.\(^{\text{∗}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(3 \leq n \leq 2 \cdot 10^5\), \(1 \leq q \leq 2 \cdot 10^5\)) – the length of the permutation and the number of queries.The following line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\), \(p\) is a permutation).The following line contains \(n\) characters \(s_1s_2 \ldots s_n\). It is guaranteed that \(s_i\) is either \(\texttt{L}\) or \(\texttt{R}\), \(s_1 = \texttt{R}\), and \(s_n = \texttt{L}\).The following \(q\) lines contain an integer \(i\) (\(2 \leq i \leq n-1\)), denoting that \(s_i\) is changed from \(\texttt{L}\) to \(\texttt{R}\) (or from \(\texttt{R}\) to \(\texttt{L}\)). It is guaranteed that the sum of \(n\) and \(q\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each query, output ""YES"" (without quotes) if it is possible, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yES"", ""yes"" and ""Yes"" will be recognized as a positive response).
|
In the first testcase, \(s = \texttt{RRRLL}\) after the first query. QED may sort \(p\) using the following operations: Initially, \(p = [1,4,2,5,3]\). Select \(i = 2\) and swap \(p_2\) with \(p_{3}\). Now, \(p = [1,2,4,5,3]\). Select \(i = 5\) and swap \(p_5\) with \(p_{4}\). Now, \(p = [1,2,4,3,5]\). Select \(i = 4\) and swap \(p_4\) with \(p_{3}\). Now, \(p = [1,2,3,4,5]\), which is in non-decreasing order. It can be shown that it is impossible to sort the array after all three updates of the first testcase.
|
Input: 35 31 4 2 5 3RLRLL2438 51 5 2 4 8 3 6 7RRLLRRRL435346 21 2 3 4 5 6RLRLRL45 | Output: YES YES NO NO YES NO NO NO YES YES
|
Medium
| 3 | 1,406 | 875 | 245 | 20 |
597 |
B
|
597B
|
B. Restaurant
| 1,600 |
dp; greedy; sortings
|
A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri).Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?No two accepted orders can intersect, i.e. they can't share even a moment of time. If one order ends in the moment other starts, they can't be accepted both.
|
The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109).
|
Print the maximal number of orders that can be accepted.
|
Input: 27 114 7 | Output: 1
|
Medium
| 3 | 497 | 157 | 56 | 5 |
|
1,977 |
C
|
1977C
|
C. Nikita and LCM
| 1,900 |
brute force; data structures; dp; greedy; math; number theory; sortings
|
Nikita is a student passionate about number theory and algorithms. He faces an interesting problem related to an array of numbers.Suppose Nikita has an array of integers \(a\) of length \(n\). He will call a subsequence\(^\dagger\) of the array special if its least common multiple (LCM) is not contained in \(a\). The LCM of an empty subsequence is equal to \(0\).Nikita wonders: what is the length of the longest special subsequence of \(a\)? Help him answer this question!\(^\dagger\) A sequence \(b\) is a subsequence of \(a\) if \(b\) can be obtained from \(a\) by the deletion of several (possibly, zero or all) elements, without changing the order of the remaining elements. For example, \([5,2,3]\) is a subsequence of \([1,5,7,8,2,4,3]\).
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 2000\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)) — the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\).
|
For each test case, output a single integer — the length of the longest special subsequence of \(a\).
|
In the first test case, the LCM of any non-empty subsequence is contained in \(a\), so the answer is \(0\).In the second test case, we can take the subsequence \([3, 2, 10, 1]\), its LCM is equal to \(30\), which is not contained in \(a\).In the third test case, we can take the subsequence \([2, 3, 6, 100\,003]\), its LCM is equal to \(600\,018\), which is not contained in \(a\).
|
Input: 651 2 4 8 1663 2 10 20 60 172 3 4 6 12 100003 120003692 42 7 3 6 7 7 1 684 99 57 179 10203 2 11 4081211 | Output: 0 4 4 5 8 0
|
Hard
| 7 | 747 | 536 | 101 | 19 |
313 |
A
|
313A
|
A. Ilya and Bank Account
| 900 |
implementation; number theory
|
Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obligations. Moreover, they even have their own bank accounts. The state of a bank account is an integer. The state of a bank account can be a negative number. This means that the owner of the account owes the bank money.Ilya the Lion has recently had a birthday, so he got a lot of gifts. One of them (the gift of the main ZooVille bank) is the opportunity to delete the last digit or the digit before last from the state of his bank account no more than once. For example, if the state of Ilya's bank account is -123, then Ilya can delete the last digit and get his account balance equal to -12, also he can remove its digit before last and get the account balance equal to -13. Of course, Ilya is permitted not to use the opportunity to delete a digit from the balance.Ilya is not very good at math, and that's why he asks you to help him maximize his bank account. Find the maximum state of the bank account that can be obtained using the bank's gift.
|
The single line contains integer n (10 ≤ |n| ≤ 109) — the state of Ilya's bank account.
|
In a single line print an integer — the maximum state of the bank account that Ilya can get.
|
In the first test sample Ilya doesn't profit from using the present.In the second test sample you can delete digit 1 and get the state of the account equal to 0.
|
Input: 2230 | Output: 2230
|
Beginner
| 2 | 1,072 | 87 | 92 | 3 |
1,186 |
A
|
1186A
|
A. Vus the Cossack and a Contest
| 800 |
implementation
|
Vus the Cossack holds a programming competition, in which \(n\) people participate. He decided to award them all with pens and notebooks. It is known that Vus has exactly \(m\) pens and \(k\) notebooks.Determine whether the Cossack can reward all participants, giving each of them at least one pen and at least one notebook.
|
The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \leq n, m, k \leq 100\)) — the number of participants, the number of pens, and the number of notebooks respectively.
|
Print ""Yes"" if it possible to reward all the participants. Otherwise, print ""No"".You can print each letter in any case (upper or lower).
|
In the first example, there are \(5\) participants. The Cossack has \(8\) pens and \(6\) notebooks. Therefore, he has enough pens and notebooks.In the second example, there are \(3\) participants. The Cossack has \(9\) pens and \(3\) notebooks. He has more than enough pens but only the minimum needed number of notebooks.In the third example, there are \(8\) participants but only \(5\) pens. Since the Cossack does not have enough pens, the answer is ""No"".
|
Input: 5 8 6 | Output: Yes
|
Beginner
| 1 | 324 | 184 | 140 | 11 |
1,948 |
A
|
1948A
|
A. Special Characters
| 800 |
brute force; constructive algorithms
|
You are given an integer \(n\).Your task is to build a string of uppercase Latin letters. There must be exactly \(n\) special characters in this string. Let's call a character special if it is equal to exactly one of its neighbors.For example, there are \(6\) special characters in the AAABAACC string (at positions: \(1\), \(3\), \(5\), \(6\), \(7\) and \(8\)).Print any suitable string or report that there is no such string.
|
The first line contains a single integer \(t\) (\(1 \le t \le 50\)) — the number of test cases.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)).
|
For each test case, print the answer as follows: if there is no suitable string, print one line containing the string NO; otherwise, print two lines. The first line should contain the string YES; on the second line print a string of length at most \(200\) — the answer itself (it can be shown that if some answers exist, then there is an answer of length at most \(200\)). If there are several solutions, print any of them.
|
Input: 3612 | Output: YES AAABAACC NO YES MM
|
Beginner
| 2 | 427 | 180 | 423 | 19 |
|
181 |
B
|
181B
|
B. Number of Triplets
| 1,300 |
binary search; brute force
|
You are given n points on a plane. All points are different.Find the number of different groups of three points (A, B, C) such that point B is the middle of segment AC. The groups of three points are considered unordered, that is, if point B is the middle of segment AC, then groups (A, B, C) and (C, B, A) are considered the same.
|
The first line contains a single integer n (3 ≤ n ≤ 3000) — the number of points. Next n lines contain the points. The i-th line contains coordinates of the i-th point: two space-separated integers xi, yi ( - 1000 ≤ xi, yi ≤ 1000).It is guaranteed that all given points are different.
|
Print the single number — the answer to the problem.
|
Input: 31 12 23 3 | Output: 1
|
Easy
| 2 | 331 | 284 | 52 | 1 |
|
162 |
D
|
162D
|
D. Remove digits
| 1,900 |
*special
|
You are given a string. Remove all digits from it. When a character is removed from a string, all characters to the right of it are shifted one position to the left.
|
The only line of input contains a string between 1 and 100 characters long. Each character of the string has ASCII-code between 33 (exclamation mark) and 126 (tilde), inclusive.
|
Output the given string with all digits removed from it. If the original string had only digits, output an empty string.
|
Input: VK-Cup-2012! | Output: VK-Cup-!
|
Hard
| 1 | 165 | 177 | 120 | 1 |
|
686 |
A
|
686A
|
A. Free Ice Cream
| 800 |
constructive algorithms; implementation
|
After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the queue wants either to take several ice cream packs for himself and his friends or to give several ice cream packs to Kay and Gerda (carriers that bring ice cream have to stand in the same queue).If a carrier with d ice cream packs comes to the house, then Kay and Gerda take all his packs. If a child who wants to take d ice cream packs comes to the house, then Kay and Gerda will give him d packs if they have enough ice cream, otherwise the child will get no ice cream at all and will leave in distress.Kay wants to find the amount of ice cream they will have after all people will leave from the queue, and Gerda wants to find the number of distressed kids.
|
The first line contains two space-separated integers n and x (1 ≤ n ≤ 1000, 0 ≤ x ≤ 109).Each of the next n lines contains a character '+' or '-', and an integer di, separated by a space (1 ≤ di ≤ 109). Record ""+ di"" in i-th line means that a carrier with di ice cream packs occupies i-th place from the start of the queue, and record ""- di"" means that a child who wants to take di packs stands in i-th place.
|
Print two space-separated integers — number of ice cream packs left after all operations, and number of kids that left the house in distress.
|
Consider the first sample. Initially Kay and Gerda have 7 packs of ice cream. Carrier brings 5 more, so now they have 12 packs. A kid asks for 10 packs and receives them. There are only 2 packs remaining. Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed. Carrier bring 40 packs, now Kay and Gerda have 42 packs. Kid asks for 20 packs and receives them. There are 22 packs remaining.
|
Input: 5 7+ 5- 10- 20+ 40- 20 | Output: 22 1
|
Beginner
| 2 | 977 | 413 | 141 | 6 |
31 |
E
|
31E
|
E. TV Game
| 2,400 |
dp
|
There is a new TV game on BerTV. In this game two players get a number A consisting of 2n digits. Before each turn players determine who will make the next move. Each player should make exactly n moves. On it's turn i-th player takes the leftmost digit of A and appends it to his or her number Si. After that this leftmost digit is erased from A. Initially the numbers of both players (S1 and S2) are «empty». Leading zeroes in numbers A, S1, S2 are allowed. In the end of the game the first player gets S1 dollars, and the second gets S2 dollars.One day Homer and Marge came to play the game. They managed to know the number A beforehand. They want to find such sequence of their moves that both of them makes exactly n moves and which maximizes their total prize. Help them.
|
The first line contains integer n (1 ≤ n ≤ 18). The second line contains integer A consisting of exactly 2n digits. This number can have leading zeroes.
|
Output the line of 2n characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly n moves. If there are several solutions, output any of them.
|
Input: 21234 | Output: HHMM
|
Expert
| 1 | 776 | 152 | 226 | 0 |
|
1,776 |
F
|
1776F
|
F. Train Splitting
| 1,700 |
constructive algorithms; graphs; greedy
|
There are \(n\) big cities in Italy, and there are \(m\) train routes between pairs of cities. Each route connects two different cities bidirectionally. Moreover, using the trains one can reach every city starting from any other city.Right now, all the routes are operated by the government-owned Italian Carriage Passenger Company, but the government wants to privatize the routes. The government does not want to give too much power to a single company, but it also does not want to make people buy a lot of different subscriptions. Also, it would like to give a fair chance to all companies. In order to formalize all these wishes, the following model was proposed.There will be \(k \ge 2\) private companies indexed by \(1, \, 2, \, \dots, \, k\). Each train route will be operated by exactly one of the \(k\) companies. Then: For any company, there should exist two cities such that it is impossible to reach one from the other using only routes operated by that company. On the other hand, for any two companies, it should be possible to reach every city from any other city using only routes operated by these two companies. Find a plan satisfying all these criteria. It can be shown that a viable plan always exists. Please note that you can choose the number \(k\) and you do not have to minimize or maximize it.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \le n \le 50\), \(n-1 \le m \le n(n-1)/2\)) — the number of cities and the number of train routes.The next \(m\) lines contain two integers \(u_i\) and \(v_i\) each (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)) — the \(i\)-th train route connects cities \(u_i\) and \(v_i\).It is guaranteed that the routes connect \(m\) distinct pairs of cities. It is guaranteed that using the trains one can reach every city starting from any other city.The sum of the values of \(n\) over all test cases does not exceed \(5000\).
|
For each test case, on the first line print an integer \(k\) (\(2 \le k \le m\)) — the number of companies in your plan; on the second line print \(m\) integers \(c_1, \, c_2, \, \dots, \, c_m\) (\(1 \le c_i \le k\)) — in your plan company \(c_i\) operates the \(i\)-th route.If there are multiple valid plans, you may print any of them.
|
In the first test case, the output is illustrated in the following picture, where different colors correspond to different companies (blue for \(1\), red for \(2\), green for \(3\), and yellow for \(4\)): If we consider, for example, only companies \(2\) and \(3\), we can see that from any city it is possible to reach every other city (picture on the left below). However, if we restrict to company \(2\) alone, it becomes impossible to reach city \(5\) from city \(1\) (picture on the right). In the second test case, the output is illustrated in the following picture:
|
Input: 25 91 21 31 41 52 32 42 53 43 53 31 23 12 3 | Output: 4 1 2 3 1 4 2 2 4 3 3 2 3 1
|
Medium
| 3 | 1,321 | 770 | 337 | 17 |
852 |
F
|
852F
|
F. Product transformation
| 2,200 |
combinatorics; math; number theory
|
Consider an array A with N elements, all being the same integer a.Define the product transformation as a simultaneous update Ai = Ai·Ai + 1, that is multiplying each element to the element right to it for , with the last number AN remaining the same. For example, if we start with an array A with a = 2 and N = 4, then after one product transformation A = [4, 4, 4, 2], and after two product transformations A = [16, 16, 8, 2].Your simple task is to calculate the array A after M product transformations. Since the numbers can get quite big you should output them modulo Q.
|
The first and only line of input contains four integers N, M, a, Q (7 ≤ Q ≤ 109 + 123, 2 ≤ a ≤ 106 + 123, , is prime), where is the multiplicative order of the integer a modulo Q, see notes for definition.
|
You should output the array A from left to right.
|
The multiplicative order of a number a modulo Q , is the smallest natural number x such that ax mod Q = 1. For example, .
|
Input: 2 2 2 7 | Output: 1 2
|
Hard
| 3 | 573 | 205 | 49 | 8 |
1,912 |
H
|
1912H
|
H. Hypercatapult Commute
| 2,400 |
graphs
|
A revolutionary new transport system is currently operating in Byteland. This system requires neither roads nor sophisticated mechanisms, only giant catapults. The system works as follows. There are \(n\) cities in Byteland. In every city there is a catapult, right in the city center. People who want to travel are put in a special capsule, and a catapult throws this capsule to the center of some other city. Every catapult is powerful enough to throw the capsule to any other city, with any number of passengers inside the capsule. The only problem is that it takes a long time to charge the catapult, so it is only possible to use it once a day.The passenger may need to use the catapults multiple times. For example, if the passenger wants to travel from city A to city B, they can first use one catapult to move from A to C, and then transfer to another catapult to move from C to B.Today there are \(m\) passengers. Passenger \(i\) wants to travel from city \(a_i\) to city \(b_i\). Your task is to find the way to deliver all the passengers to their destinations in a single day, using the minimal possible number of catapults, or say that it is impossible.
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \leq n \leq 1000\), \(0 \leq m \leq 10^5\)) — the number of cities and the number of passengers. The next \(m\) lines contain pairs of numbers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n\), \(a_i \neq b_i\)).
|
In the first line print the number \(k\) — the minimal number of catapults you need to use.In the next \(k\) lines, print descriptions of each catapult launch, in the order they need to be performed. Each description should consist of two integers \(c_i\), \(d_i\), the index of the city to launch from, and the index of destination city.Note that you don't need to print what passengers should be put into the capsule on each launch, but it should be possible for each passenger to reach their destination city using the plan you provide.If it is impossible to deliver all passengers, print the single number \(-1\).
|
Input: 5 61 31 22 34 21 55 1 | Output: 5 5 1 1 2 4 2 2 3 3 5
|
Expert
| 1 | 1,165 | 281 | 617 | 19 |
|
1,545 |
B
|
1545B
|
B. AquaMoon and Chess
| 1,900 |
combinatorics; math
|
Cirno gave AquaMoon a chessboard of size \(1 \times n\). Its cells are numbered with integers from \(1\) to \(n\) from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied.In each operation, AquaMoon can choose a cell \(i\) with a pawn, and do either of the following (if possible): Move pawn from it to the \((i+2)\)-th cell, if \(i+2 \leq n\) and the \((i+1)\)-th cell is occupied and the \((i+2)\)-th cell is unoccupied. Move pawn from it to the \((i-2)\)-th cell, if \(i-2 \geq 1\) and the \((i-1)\)-th cell is occupied and the \((i-2)\)-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo \(998\,244\,353\).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10\,000\)) — the number of test cases.The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) — the size of the chessboard.The second line contains a string of \(n\) characters, consists of characters ""0"" and ""1"". If the \(i\)-th character is ""1"", the \(i\)-th cell is initially occupied; otherwise, the \(i\)-th cell is initially unoccupied.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo \(998\,244\,353\).
|
In the first test case the strings ""1100"", ""0110"" and ""0011"" are reachable from the initial state with some sequence of operations.
|
Input: 6 4 0110 6 011011 5 01010 20 10001111110110111000 20 00110110100110111101 20 11101111011000100010 | Output: 3 6 1 1287 1287 715
|
Hard
| 2 | 906 | 555 | 143 | 15 |
762 |
D
|
762D
|
D. Maximum path
| 2,300 |
dp; greedy; implementation
|
You are given a rectangular table 3 × n. Each cell contains an integer. You can move from one cell to another if they share a side.Find such path from the upper left cell to the bottom right cell of the table that doesn't visit any of the cells twice, and the sum of numbers written in the cells of this path is maximum possible.
|
The first line contains an integer n (1 ≤ n ≤ 105) — the number of columns in the table.Next three lines contain n integers each — the description of the table. The j-th number in the i-th line corresponds to the cell aij ( - 109 ≤ aij ≤ 109) of the table.
|
Output the maximum sum of numbers on a path from the upper left cell to the bottom right cell of the table, that doesn't visit any of the cells twice.
|
The path for the first example: The path for the second example:
|
Input: 31 1 11 -1 11 1 1 | Output: 7
|
Expert
| 3 | 329 | 256 | 150 | 7 |
1,190 |
E
|
1190E
|
E. Tokitsukaze and Explosion
| 3,100 |
binary search; greedy
|
Tokitsukaze and her friends are trying to infiltrate a secret base built by Claris. However, Claris has been aware of that and set a bomb which is going to explode in a minute. Although they try to escape, they have no place to go after they find that the door has been locked.At this very moment, CJB, Father of Tokitsukaze comes. With his magical power given by Ereshkigal, the goddess of the underworld, CJB is able to set \(m\) barriers to protect them from the explosion. Formally, let's build a Cartesian coordinate system on the plane and assume the bomb is at \(O(0, 0)\). There are \(n\) persons in Tokitsukaze's crew, the \(i\)-th one of whom is at \(P_i(X_i, Y_i)\). Every barrier can be considered as a line with infinity length and they can intersect each other. For every person from Tokitsukaze's crew, there must be at least one barrier separating the bomb and him, which means the line between the bomb and him intersects with at least one barrier. In this definition, if there exists a person standing at the position of the bomb, any line through \(O(0, 0)\) will satisfy the requirement.Although CJB is very powerful, he still wants his barriers to be as far from the bomb as possible, in order to conserve his energy. Please help him calculate the maximum distance between the bomb and the closest barrier while all of Tokitsukaze's crew are safe.
|
The first line contains two integers \(n\), \(m\) (\(1 \leq n, m \leq 10^5\)), indicating the number of people and the number of barriers respectively.The \(i\)-th line of the next \(n\) lines contains two integers \(X_i\), \(Y_i\) (\(-10^5 \leq X_i, Y_i \leq 10^5\)), indicating the \(i\)-th person's location \(P_i(X_i, Y_i)\). Note that \(P_i\) may have the same coordinates as \(P_j\) (\(j \neq i\)) or even \(O\).
|
Print a single real number — the maximum distance meeting the requirement. Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a - b|}{\max(1, |b|)} \leq 10^{-6}\).
|
In the first two examples, CJB must set the barrier crossing \(O(0, 0)\).In the last two examples, CJB can set each barrier crossing some \(P_i\) such that the barrier is perpendicular to the line between \(P_i\) and \(O\).
|
Input: 3 1 2 0 0 2 -1 0 | Output: 0.0000000000
|
Master
| 2 | 1,368 | 418 | 324 | 11 |
1,552 |
G
|
1552G
|
G. A Serious Referee
| 3,000 |
bitmasks; brute force; dfs and similar; sortings
|
Andrea has come up with what he believes to be a novel sorting algorithm for arrays of length \(n\). The algorithm works as follows.Initially there is an array of \(n\) integers \(a_1,\, a_2,\, \dots,\, a_n\). Then, \(k\) steps are executed.For each \(1\le i\le k\), during the \(i\)-th step the subsequence of the array \(a\) with indexes \(j_{i,1}< j_{i,2}< \dots< j_{i, q_i}\) is sorted, without changing the values with the remaining indexes. So, the subsequence \(a_{j_{i,1}},\, a_{j_{i,2}},\, \dots,\, a_{j_{i,q_i}}\) is sorted and all other elements of \(a\) are left untouched.Andrea, being eager to share his discovery with the academic community, sent a short paper describing his algorithm to the journal ""Annals of Sorting Algorithms"" and you are the referee of the paper (that is, the person who must judge the correctness of the paper). You must decide whether Andrea's algorithm is correct, that is, if it sorts any array \(a\) of \(n\) integers.
|
The first line contains two integers \(n\) and \(k\) (\(1\le n\le 40\), \(0\le k\le 10\)) — the length of the arrays handled by Andrea's algorithm and the number of steps of Andrea's algorithm.Then \(k\) lines follow, each describing the subsequence considered in a step of Andrea's algorithm.The \(i\)-th of these lines contains the integer \(q_i\) (\(1\le q_i\le n\)) followed by \(q_i\) integers \(j_{i,1},\,j_{i,2},\,\dots,\, j_{i,q_i}\) (\(1\le j_{i,1}<j_{i,2}<\cdots<j_{i,q_i}\le n\)) — the length of the subsequence considered in the \(i\)-th step and the indexes of the subsequence.
|
If Andrea's algorithm is correct print ACCEPTED, otherwise print REJECTED.
|
Explanation of the first sample: The algorithm consists of \(3\) steps. The first one sorts the subsequence \([a_1, a_2, a_3]\), the second one sorts the subsequence \([a_2, a_3, a_4]\), the third one sorts the subsequence \([a_1,a_2]\). For example, if initially \(a=[6, 5, 6, 3]\), the algorithm transforms the array as follows (the subsequence that gets sorted is highlighted in red) $$$\( [{\color{red}6},{\color{red}5},{\color{red}6},3] \rightarrow [5, {\color{red}6}, {\color{red}6}, {\color{red}3}] \rightarrow [{\color{red}5}, {\color{red}3}, 6, 6] \rightarrow [3, 5, 6, 6] \,.\)\( One can prove that, for any initial array \)a\(, at the end of the algorithm the array \)a\( will be sorted.Explanation of the second sample: The algorithm consists of \)3\( steps. The first one sorts the subsequence \)[a_1, a_2, a_3]\(, the second one sorts the subsequence \)[a_2, a_3, a_4]\(, the third one sorts the subsequence \)[a_1,a_3,a_4]\(. For example, if initially \)a=[6, 5, 6, 3]\(, the algorithm transforms the array as follows (the subsequence that gets sorted is highlighted in red) \)\( [{\color{red}6},{\color{red}5},{\color{red}6},3] \rightarrow [5, {\color{red}6}, {\color{red}6}, {\color{red}3}] \rightarrow [{\color{red}5}, 3, {\color{red}6}, {\color{red}6}] \rightarrow [5, 3, 6, 6] \,.\)\( Notice that \)a=[6,5,6,3]\( is an example of an array that is not sorted by the algorithm.Explanation of the third sample: The algorithm consists of \)4\( steps. The first \)3\( steps do nothing because they sort subsequences of length \)1\(, whereas the fourth step sorts the subsequence \)[a_1,a_3]\(. For example, if initially \)a=[5,6,4]\(, the algorithm transforms the array as follows (the subsequence that gets sorted is highlighted in red) \)\( [{\color{red}5},6,4] \rightarrow [5,{\color{red}6},4] \rightarrow [5,{\color{red}6},4] \rightarrow [{\color{red}5},6,{\color{red}4}]\rightarrow [4,6,5] \,.\)\( Notice that \)a=[5,6,4]\( is an example of an array that is not sorted by the algorithm.Explanation of the fourth sample: The algorithm consists of \)2\( steps. The first step sorts the subsequences \)[a_2,a_3,a_4]\(, the second step sorts the whole array \)[a_1,a_2,a_3,a_4,a_5]\(. For example, if initially \)a=[9,8,1,1,1]\(, the algorithm transforms the array as follows (the subsequence that gets sorted is highlighted in red) \)\( [9,{\color{red}8},{\color{red}1},{\color{red}1},1] \rightarrow [{\color{red}9},{\color{red}1},{\color{red}1},{\color{red}8},{\color{red}1}] \rightarrow [1,1,1,8,9] \,.\)\( Since in the last step the whole array is sorted, it is clear that, for any initial array \)a\(, at the end of the algorithm the array \)a$$$ will be sorted.
|
Input: 4 3 3 1 2 3 3 2 3 4 2 1 2 | Output: ACCEPTED
|
Master
| 4 | 963 | 590 | 74 | 15 |
263 |
C
|
263C
|
C. Circle of Numbers
| 2,000 |
brute force; dfs and similar; implementation
|
One day Vasya came up to the blackboard and wrote out n distinct integers from 1 to n in some order in a circle. Then he drew arcs to join the pairs of integers (a, b) (a ≠ b), that are either each other's immediate neighbors in the circle, or there is number c, such that a and с are immediate neighbors, and b and c are immediate neighbors. As you can easily deduce, in the end Vasya drew 2·n arcs.For example, if the numbers are written in the circle in the order 1, 2, 3, 4, 5 (in the clockwise direction), then the arcs will join pairs of integers (1, 2), (2, 3), (3, 4), (4, 5), (5, 1), (1, 3), (2, 4), (3, 5), (4, 1) and (5, 2).Much time has passed ever since, the numbers we wiped off the blackboard long ago, but recently Vasya has found a piece of paper with 2·n written pairs of integers that were joined with the arcs on the board. Vasya asks you to find the order of numbers in the circle by these pairs.
|
The first line of the input contains a single integer n (5 ≤ n ≤ 105) that shows, how many numbers were written on the board. Next 2·n lines contain pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the numbers that were connected by the arcs.It is guaranteed that no pair of integers, connected by a arc, occurs in the input more than once. The pairs of numbers and the numbers in the pairs are given in the arbitrary order.
|
If Vasya made a mistake somewhere and there isn't any way to place numbers from 1 to n on the circle according to the statement, then print a single number ""-1"" (without the quotes). Otherwise, print any suitable sequence of n distinct integers from 1 to n. If there are multiple solutions, you are allowed to print any of them. Specifically, it doesn't matter which number you write first to describe the sequence of the order. It also doesn't matter whether you write out the numbers in the clockwise or counter-clockwise direction.
|
Input: 51 22 33 44 55 11 32 43 54 15 2 | Output: 1 2 3 4 5
|
Hard
| 3 | 917 | 428 | 536 | 2 |
|
1,360 |
F
|
1360F
|
F. Spy-string
| 1,700 |
bitmasks; brute force; constructive algorithms; dp; hashing; strings
|
You are given \(n\) strings \(a_1, a_2, \ldots, a_n\): all of them have the same length \(m\). The strings consist of lowercase English letters.Find any string \(s\) of length \(m\) such that each of the given \(n\) strings differs from \(s\) in at most one position. Formally, for each given string \(a_i\), there is no more than one position \(j\) such that \(a_i[j] \ne s[j]\).Note that the desired string \(s\) may be equal to one of the given strings \(a_i\), or it may differ from all the given strings.For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first.
|
The first line contains an integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. Then \(t\) test cases follow.Each test case starts with a line containing two positive integers \(n\) (\(1 \le n \le 10\)) and \(m\) (\(1 \le m \le 10\)) — the number of strings and their length.Then follow \(n\) strings \(a_i\), one per line. Each of them has length \(m\) and consists of lowercase English letters.
|
Print \(t\) answers to the test cases. Each answer (if it exists) is a string of length \(m\) consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print ""-1"" (""minus one"", without quotes).
|
The first test case was explained in the statement.In the second test case, the answer does not exist.
|
Input: 5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c | Output: abab -1 aaa ab z
|
Medium
| 6 | 713 | 407 | 259 | 13 |
61 |
C
|
61C
|
C. Capture Valerian
| 2,000 |
math
|
It's now 260 AD. Shapur, being extremely smart, became the King of Persia. He is now called Shapur, His majesty King of kings of Iran and Aniran.Recently the Romans declared war on Persia. They dreamed to occupy Armenia. In the recent war, the Romans were badly defeated. Now their senior army general, Philip is captured by Shapur and Shapur is now going to capture Valerian, the Roman emperor.Being defeated, the cowardly Valerian hid in a room at the top of one of his castles. To capture him, Shapur has to open many doors. Fortunately Valerian was too scared to make impenetrable locks for the doors.Each door has 4 parts. The first part is an integer number a. The second part is either an integer number b or some really odd sign which looks like R. The third one is an integer c and the fourth part is empty! As if it was laid for writing something. Being extremely gifted, after opening the first few doors, Shapur found out the secret behind the locks.c is an integer written in base a, to open the door we should write it in base b. The only bad news is that this R is some sort of special numbering system that is used only in Roman empire, so opening the doors is not just a piece of cake!Here's an explanation of this really weird number system that even doesn't have zero:Roman numerals are based on seven symbols: a stroke (identified with the letter I) for a unit, a chevron (identified with the letter V) for a five, a cross-stroke (identified with the letter X) for a ten, a C (identified as an abbreviation of Centum) for a hundred, etc.: I=1 V=5 X=10 L=50 C=100 D=500 M=1000Symbols are iterated to produce multiples of the decimal (1, 10, 100, 1, 000) values, with V, L, D substituted for a multiple of five, and the iteration continuing: I 1, II 2, III 3, V 5, VI 6, VII 7, etc., and the same for other bases: X 10, XX 20, XXX 30, L 50, LXXX 80; CC 200, DCC 700, etc. At the fourth and ninth iteration, a subtractive principle must be employed, with the base placed before the higher base: IV 4, IX 9, XL 40, XC 90, CD 400, CM 900.Also in bases greater than 10 we use A for 10, B for 11, etc.Help Shapur capture Valerian and bring peace back to Persia, especially Armenia.
|
The first line contains two integers a and b (2 ≤ a, b ≤ 25). Only b may be replaced by an R which indicates Roman numbering system.The next line contains a single non-negative integer c in base a which may contain leading zeros but its length doesn't exceed 103. It is guaranteed that if we have Roman numerals included the number would be less than or equal to 300010 and it won't be 0. In any other case the number won't be greater than 101510.
|
Write a single line that contains integer c in base b. You must omit leading zeros.
|
You can find more information about roman numerals here: http://en.wikipedia.org/wiki/Roman_numerals
|
Input: 10 21 | Output: 1
|
Hard
| 1 | 2,194 | 447 | 83 | 0 |
1,579 |
F
|
1579F
|
F. Array Stabilization (AND version)
| 1,700 |
brute force; graphs; math; number theory; shortest paths
|
You are given an array \(a[0 \ldots n - 1] = [a_0, a_1, \ldots, a_{n - 1}]\) of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.In one step, the array \(a\) is replaced by another array of length \(n\) according to the following rules: First, a new array \(a^{\rightarrow d}\) is defined as a cyclic shift of the array \(a\) to the right by \(d\) cells. The elements of this array can be defined as \(a^{\rightarrow d}_i = a_{(i + n - d) \bmod n}\), where \((i + n - d) \bmod n\) is the remainder of integer division of \(i + n - d\) by \(n\). It means that the whole array \(a^{\rightarrow d}\) can be represented as a sequence $$$\(a^{\rightarrow d} = [a_{n - d}, a_{n - d + 1}, \ldots, a_{n - 1}, a_0, a_1, \ldots, a_{n - d - 1}]\)\( Then each element of the array \)a_i\( is replaced by \)a_i \,\&\, a^{\rightarrow d}_i\(, where \)\&\( is a logical ""AND"" operator. For example, if \)a = [0, 0, 1, 1]\( and \)d = 1\(, then \)a^{\rightarrow d} = [1, 0, 0, 1]\( and the value of \)a\( after the first step will be \)[0 \,\&\, 1, 0 \,\&\, 0, 1 \,\&\, 0, 1 \,\&\, 1]\(, that is \)[0, 0, 0, 1]\(.The process ends when the array stops changing. For a given array \)a$$$, determine whether it will consist of only zeros at the end of the process. If yes, also find the number of steps the process will take before it finishes.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases.The next \(2t\) lines contain descriptions of the test cases. The first line of each test case description contains two integers: \(n\) (\(1 \le n \le 10^6\)) — array size and \(d\) (\(1 \le d \le n\)) — cyclic shift offset. The second line of the description contains \(n\) space-separated integers \(a_i\) (\(0 \le a_i \le 1\)) — elements of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to \(1\) in the array after the end of the process, print -1.
|
In the third sample test case the array will change as follows: At the beginning \(a = [1, 1, 0, 1, 0]\), and \(a^{\rightarrow 2} = [1, 0, 1, 1, 0]\). Their element-by-element ""AND"" is equal to $$$\([1 \,\&\, 1, 1 \,\&\, 0, 0 \,\&\, 1, 1 \,\&\, 1, 0 \,\&\, 0] = [1, 0, 0, 1, 0]\)\( Now \)a = [1, 0, 0, 1, 0]\(, then \)a^{\rightarrow 2} = [1, 0, 1, 0, 0]\(. Their element-by-element ""AND"" equals to \)\([1 \,\&\, 1, 0 \,\&\, 0, 0 \,\&\, 1, 1 \,\&\, 0, 0 \,\&\, 0] = [1, 0, 0, 0, 0]\)\( And finally, when \)a = [1, 0, 0, 0, 0]\( we get \)a^{\rightarrow 2} = [0, 0, 1, 0, 0]\(. Their element-by-element ""AND"" equals to \)\([1 \,\&\, 0, 0 \,\&\, 0, 0 \,\&\, 1, 0 \,\&\, 0, 0 \,\&\, 0] = [0, 0, 0, 0, 0]\)\( Thus, the answer is \)3\( steps.In the fourth sample test case, the array will not change as it shifts by \)2\( to the right, so each element will be calculated as \)0 \,\&\, 0\( or \)1 \,\&\, 1$$$ thus not changing its value. So the answer is -1, the array will never contain only zeros.
|
Input: 5 2 1 0 1 3 2 0 1 0 5 2 1 1 0 1 0 4 2 0 1 0 1 1 1 0 | Output: 1 1 3 -1 0
|
Medium
| 5 | 1,399 | 531 | 319 | 15 |
921 |
06
|
92106
|
06. Labyrinth-6
| 3,200 |
See the problem statement here: http://codeforces.com/contest/921/problem/01.
|
Master
| 0 | 77 | 0 | 0 | 9 |
|||||
847 |
I
|
847I
|
I. Noise Level
| 1,900 |
dfs and similar; implementation; math
|
The Berland's capital has the form of a rectangle with sizes n × m quarters. All quarters are divided into three types: regular (labeled with the character '.') — such quarters do not produce the noise but are not obstacles to the propagation of the noise; sources of noise (labeled with an uppercase Latin letter from 'A' to 'Z') — such quarters are noise sources and are not obstacles to the propagation of the noise; heavily built-up (labeled with the character '*') — such quarters are soundproofed, the noise does not penetrate into them and they themselves are obstacles to the propagation of noise. A quarter labeled with letter 'A' produces q units of noise. A quarter labeled with letter 'B' produces 2·q units of noise. And so on, up to a quarter labeled with letter 'Z', which produces 26·q units of noise. There can be any number of quarters labeled with each letter in the city.When propagating from the source of the noise, the noise level is halved when moving from one quarter to a quarter that shares a side with it (when an odd number is to be halved, it's rounded down). The noise spreads along the chain. For example, if some quarter is located at a distance 2 from the noise source, then the value of noise which will reach the quarter is divided by 4. So the noise level that comes from the source to the quarter is determined solely by the length of the shortest path between them. Heavily built-up quarters are obstacles, the noise does not penetrate into them. The values in the cells of the table on the right show the total noise level in the respective quarters for q = 100, the first term in each sum is the noise from the quarter 'A', the second — the noise from the quarter 'B'. The noise level in quarter is defined as the sum of the noise from all sources. To assess the quality of life of the population of the capital of Berland, it is required to find the number of quarters whose noise level exceeds the allowed level p.
|
The first line contains four integers n, m, q and p (1 ≤ n, m ≤ 250, 1 ≤ q, p ≤ 106) — the sizes of Berland's capital, the number of noise units that a quarter 'A' produces, and the allowable noise level.Each of the following n lines contains m characters — the description of the capital quarters, in the format that was described in the statement above. It is possible that in the Berland's capital there are no quarters of any type.
|
Print the number of quarters, in which the noise level exceeds the allowed level p.
|
The illustration to the first example is in the main part of the statement.
|
Input: 3 3 100 140...A*..B. | Output: 3
|
Hard
| 3 | 1,957 | 435 | 83 | 8 |
2,028 |
F
|
2028F
|
F. Alice's Adventures in Addition
| 2,700 |
bitmasks; brute force; dp; implementation
|
Note that the memory limit is unusual.The Cheshire Cat has a riddle for Alice: given \(n\) integers \(a_1, a_2, \ldots, a_n\) and a target \(m\), is there a way to insert \(+\) and \(\times\) into the circles of the expression $$$\(a_1 \circ a_2 \circ \cdots \circ a_n = m\)\( to make it true? We follow the usual order of operations: \)\times\( is done before \)+$$$.Although Alice is excellent at chess, she is not good at math. Please help her so she can find a way out of Wonderland!
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n, m\) (\(1\le n\le 2\cdot 10^5\); \(1\le m\le 10^4\)) — the number of integers and the target, respectively.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0\le a_i\le 10^4\)) — the elements of the array \(a\).The sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output ""YES"" without quotes if it is possible to get the target by inserting \(+\) or \(\times\) and ""NO"" otherwise.You can output each letter in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
|
Possible solutions for the first four test cases are shown below. $$$\(\begin{align*} 2 \times 1 + 1 \times 1 \times 2 &= 4 \\ 2 \times 1 + 1 + 1 \times 2 &= 5 \\ 2 \times 1 + 1 + 1 + 2 &= 6 \\ 2 + 1 + 1 + 1 + 2 &= 7 \\ \end{align*}\)\( It is impossible to get a result of \)8$$$ in the fifth test case.
|
Input: 65 42 1 1 1 25 52 1 1 1 25 62 1 1 1 25 72 1 1 1 25 82 1 1 1 25 62 0 2 2 3 | Output: YES YES YES YES NO YES
|
Master
| 4 | 487 | 565 | 285 | 20 |
1,512 |
C
|
1512C
|
C. A-B Palindrome
| 1,200 |
constructive algorithms; implementation; strings
|
You are given a string \(s\) consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string \(s\) by '0' or '1' so that the string becomes a palindrome and has exactly \(a\) characters '0' and exactly \(b\) characters '1'. Note that each of the characters '?' is replaced independently from the others.A string \(t\) of length \(n\) is called a palindrome if the equality \(t[i] = t[n-i+1]\) is true for all \(i\) (\(1 \le i \le n\)).For example, if \(s=\)""01?????0"", \(a=4\) and \(b=4\), then you can replace the characters '?' in the following ways: ""01011010""; ""01100110"". For the given string \(s\) and the numbers \(a\) and \(b\), replace all the characters with '?' in the string \(s\) by '0' or '1' so that the string becomes a palindrome and has exactly \(a\) characters '0' and exactly \(b\) characters '1'.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains two integers \(a\) and \(b\) (\(0 \le a, b \le 2 \cdot 10^5\), \(a + b \ge 1\)).The second line of each test case contains the string \(s\) of length \(a+b\), consisting of the characters '0', '1', and '?'.It is guaranteed that the sum of the string lengths of \(s\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output: ""-1"", if you can't replace all the characters '?' in the string \(s\) by '0' or '1' so that the string becomes a palindrome and that it contains exactly \(a\) characters '0' and exactly \(b\) characters '1'; the string that is obtained as a result of the replacement, otherwise. If there are several suitable ways to replace characters, you can output any.
|
Input: 9 4 4 01?????0 3 3 ?????? 1 0 ? 2 2 0101 2 2 01?0 0 1 0 0 3 1?1 2 2 ?00? 4 3 ??010?0 | Output: 01011010 -1 0 -1 0110 -1 111 1001 0101010
|
Easy
| 3 | 869 | 462 | 386 | 15 |
|
1,554 |
C
|
1554C
|
C. Mikasa
| 1,800 |
binary search; bitmasks; greedy; implementation
|
You are given two integers \(n\) and \(m\). Find the \(\operatorname{MEX}\) of the sequence \(n \oplus 0, n \oplus 1, \ldots, n \oplus m\). Here, \(\oplus\) is the bitwise XOR operator.\(\operatorname{MEX}\) of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, \(\operatorname{MEX}(0, 1, 2, 4) = 3\), and \(\operatorname{MEX}(1, 2021) = 0\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 30\,000\)) — the number of test cases.The first and only line of each test case contains two integers \(n\) and \(m\) (\(0 \le n, m \le 10^9\)).
|
For each test case, print a single integer — the answer to the problem.
|
In the first test case, the sequence is \(3 \oplus 0, 3 \oplus 1, 3 \oplus 2, 3 \oplus 3, 3 \oplus 4, 3 \oplus 5\), or \(3, 2, 1, 0, 7, 6\). The smallest non-negative integer which isn't present in the sequence i. e. the \(\operatorname{MEX}\) of the sequence is \(4\).In the second test case, the sequence is \(4 \oplus 0, 4 \oplus 1, 4 \oplus 2, 4 \oplus 3, 4 \oplus 4, 4 \oplus 5, 4 \oplus 6\), or \(4, 5, 6, 7, 0, 1, 2\). The smallest non-negative integer which isn't present in the sequence i. e. the \(\operatorname{MEX}\) of the sequence is \(3\).In the third test case, the sequence is \(3 \oplus 0, 3 \oplus 1, 3 \oplus 2\), or \(3, 2, 1\). The smallest non-negative integer which isn't present in the sequence i. e. the \(\operatorname{MEX}\) of the sequence is \(0\).
|
Input: 5 3 5 4 6 3 2 69 696 123456 654321 | Output: 4 3 0 640 530866
|
Medium
| 4 | 417 | 206 | 71 | 15 |
756 |
D
|
756D
|
D. Bacterial Melee
| 2,400 |
brute force; combinatorics; dp; string suffix structures
|
Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters ""a"", ..., ""z"".The testtube is divided into n consecutive regions. Each region is occupied by a single colony of a certain bacteria type at any given moment. Hence, the population of the testtube at any moment can be described by a string of n Latin characters.Sometimes a colony can decide to conquer another colony in one of the adjacent regions. When that happens, the attacked colony is immediately eliminated and replaced by a colony of the same type as the attacking colony, while the attacking colony keeps its type. Note that a colony can only attack its neighbours within the boundaries of the testtube. At any moment, at most one attack can take place.For example, consider a testtube with population ""babb"". There are six options for an attack that may happen next: the first colony attacks the second colony (1 → 2), the resulting population is ""bbbb""; 2 → 1, the result is ""aabb""; 2 → 3, the result is ""baab""; 3 → 2, the result is ""bbbb"" (note that the result is the same as the first option); 3 → 4 or 4 → 3, the population does not change.The pattern of attacks is rather unpredictable. Julia is now wondering how many different configurations of bacteria in the testtube she can obtain after a sequence of attacks takes place (it is possible that no attacks will happen at all). Since this number can be large, find it modulo 109 + 7.
|
The first line contains an integer n — the number of regions in the testtube (1 ≤ n ≤ 5 000).The second line contains n small Latin letters that describe the initial population of the testtube.
|
Print one number — the answer to the problem modulo 109 + 7.
|
In the first sample the population can never change since all bacteria are of the same type.In the second sample three configurations are possible: ""ab"" (no attacks), ""aa"" (the first colony conquers the second colony), and ""bb"" (the second colony conquers the first colony).To get the answer for the third sample, note that more than one attack can happen.
|
Input: 3aaa | Output: 1
|
Expert
| 4 | 1,622 | 193 | 60 | 7 |
1,822 |
F
|
1822F
|
F. Gardening Friends
| 1,700 |
brute force; dfs and similar; dp; graphs; trees
|
Two friends, Alisa and Yuki, planted a tree with \(n\) vertices in their garden. A tree is an undirected graph without cycles, loops, or multiple edges. Each edge in this tree has a length of \(k\). Initially, vertex \(1\) is the root of the tree.Alisa and Yuki are growing the tree not just for fun, they want to sell it. The cost of the tree is defined as the maximum distance from the root to a vertex among all vertices of the tree. The distance between two vertices \(u\) and \(v\) is the sum of the lengths of the edges on the path from \(u\) to \(v\).The girls took a course in gardening, so they know how to modify the tree. Alisa and Yuki can spend \(c\) coins to shift the root of the tree to one of the neighbors of the current root. This operation can be performed any number of times (possibly zero). Note that the structure of the tree is left unchanged; the only change is which vertex is the root.The friends want to sell the tree with the maximum profit. The profit is defined as the difference between the cost of the tree and the total cost of operations. The profit is cost of the tree minus the total cost of operations.Help the girls and find the maximum profit they can get by applying operations to the tree any number of times (possibly zero).
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains integers \(n\), \(k\), \(c\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le k, c \le 10^9\)) — the number of vertices in the tree, the length of each edge, and the cost of the operation.The next \(n - 1\) lines of the test case contain pairs of integers \(u_i\), \(v_i\) (\(1 \le u_i, v_i \le n\)) — the edges of the graph. These edges form a tree.The sum of the values of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer — the maximum profit that Yuki and Alisa can get.
|
Input: 43 2 32 13 15 4 12 14 25 43 46 5 34 16 12 65 13 210 6 41 31 99 77 66 49 22 88 55 10 | Output: 2 12 17 32
|
Medium
| 5 | 1,268 | 614 | 93 | 18 |
|
76 |
E
|
76E
|
E. Points
| 1,700 |
implementation; math
|
You are given N points on a plane. Write a program which will find the sum of squares of distances between all pairs of points.
|
The first line of input contains one integer number N (1 ≤ N ≤ 100 000) — the number of points. Each of the following N lines contain two integer numbers X and Y ( - 10 000 ≤ X, Y ≤ 10 000) — the coordinates of points. Two or more points may coincide.
|
The only line of output should contain the required sum of squares of distances between all pairs of points.
|
Input: 41 1-1 -11 -1-1 1 | Output: 32
|
Medium
| 2 | 127 | 251 | 108 | 0 |
|
1,363 |
B
|
1363B
|
B. Subsequence Hate
| 1,400 |
implementation; strings
|
Shubham has a binary string \(s\). A binary string is a string containing only characters ""0"" and ""1"".He can perform the following operation on the string any amount of times: Select an index of the string, and flip the character at that index. This means, if the character was ""0"", it becomes ""1"", and vice versa. A string is called good if it does not contain ""010"" or ""101"" as a subsequence — for instance, ""1001"" contains ""101"" as a subsequence, hence it is not a good string, while ""1000"" doesn't contain neither ""010"" nor ""101"" as subsequences, so it is a good string.What is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.A string \(a\) is a subsequence of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) characters.
|
The first line of the input contains a single integer \(t\) \((1\le t \le 100)\) — the number of test cases.Each of the next \(t\) lines contains a binary string \(s\) \((1 \le |s| \le 1000)\).
|
For every string, output the minimum number of operations required to make it good.
|
In test cases \(1\), \(2\), \(5\), \(6\) no operations are required since they are already good strings.For the \(3\)rd test case: ""001"" can be achieved by flipping the first character — and is one of the possible ways to get a good string.For the \(4\)th test case: ""000"" can be achieved by flipping the second character — and is one of the possible ways to get a good string.For the \(7\)th test case: ""000000"" can be achieved by flipping the third and fourth characters — and is one of the possible ways to get a good string.
|
Input: 7 001 100 101 010 0 1 001100 | Output: 0 0 1 1 0 0 2
|
Easy
| 2 | 910 | 193 | 83 | 13 |
7 |
B
|
7B
|
B. Memory Manager
| 1,600 |
implementation
|
There is little time left before the release of the first national operating system BerlOS. Some of its components are not finished yet — the memory manager is among them. According to the developers' plan, in the first release the memory manager will be very simple and rectilinear. It will support three operations: alloc n — to allocate n bytes of the memory and return the allocated block's identifier x; erase x — to erase the block with the identifier x; defragment — to defragment the free memory, bringing all the blocks as close to the beginning of the memory as possible and preserving their respective order; The memory model in this case is very simple. It is a sequence of m bytes, numbered for convenience from the first to the m-th.The first operation alloc n takes as the only parameter the size of the memory block that is to be allocated. While processing this operation, a free block of n successive bytes is being allocated in the memory. If the amount of such blocks is more than one, the block closest to the beginning of the memory (i.e. to the first byte) is prefered. All these bytes are marked as not free, and the memory manager returns a 32-bit integer numerical token that is the identifier of this block. If it is impossible to allocate a free block of this size, the function returns NULL.The second operation erase x takes as its parameter the identifier of some block. This operation frees the system memory, marking the bytes of this block as free for further use. In the case when this identifier does not point to the previously allocated block, which has not been erased yet, the function returns ILLEGAL_ERASE_ARGUMENT.The last operation defragment does not have any arguments and simply brings the occupied memory sections closer to the beginning of the memory without changing their respective order.In the current implementation you are to use successive integers, starting with 1, as identifiers. Each successful alloc operation procession should return following number. Unsuccessful alloc operations do not affect numeration.You are to write the implementation of the memory manager. You should output the returned value for each alloc command. You should also output ILLEGAL_ERASE_ARGUMENT for all the failed erase commands.
|
The first line of the input data contains two positive integers t and m (1 ≤ t ≤ 100;1 ≤ m ≤ 100), where t — the amount of operations given to the memory manager for processing, and m — the available memory size in bytes. Then there follow t lines where the operations themselves are given. The first operation is alloc n (1 ≤ n ≤ 100), where n is an integer. The second one is erase x, where x is an arbitrary 32-bit integer numerical token. The third operation is defragment.
|
Output the sequence of lines. Each line should contain either the result of alloc operation procession , or ILLEGAL_ERASE_ARGUMENT as a result of failed erase operation procession. Output lines should go in the same order in which the operations are processed. Successful procession of alloc operation should return integers, starting with 1, as the identifiers of the allocated blocks.
|
Input: 6 10alloc 5alloc 3erase 1alloc 6defragmentalloc 6 | Output: 12NULL3
|
Medium
| 1 | 2,269 | 477 | 386 | 0 |
|
2,129 |
C2
|
2129C2
|
C2. Interactive RBS (Medium Version)
| 2,000 |
binary search; bitmasks; constructive algorithms; interactive
|
This is an interactive problem.This is the medium version of the problem. The only difference is the limit on the number of queries. You can make hacks only if all versions of the problem are solved.There is a hidden bracket sequence \(s\) of length \(n\), where \(s\) only contains \(\texttt{'('}\) and \(\texttt{')'}\). It is guaranteed that \(s\) contains at least one \(\texttt{'('}\) and one \(\texttt{')'}\).To find this bracket sequence, you can ask queries. Each query has the following form: you pick an integer \(k\) and arbitrary indices \(i_1, i_2, \ldots, i_k\) (\(1 \le k \le 1000\), \(1 \le i_1, i_2, \ldots, i_k \le n\)). Note that the indices can be equal. Next, you receive an integer \(f(s_{i_1}s_{i_2}\ldots s_{i_k})\) calculated by the jury.For a bracket sequence \(t\), \(f(t)\) is the number of non-empty regular bracket substrings in \(t\) (the substrings must be contiguous). For example, \(f(\texttt{""()())""}) = 3\).A bracket sequence is called regular if it can be constructed in the following way. The empty sequence \(\varnothing\) is regular. If the bracket sequence \(A\) is regular, then \(\mathtt{(}A\mathtt{)}\) is also regular. If the bracket sequences \(A\) and \(B\) are regular, then the concatenated sequence \(A B\) is also regular. For example, the sequences \(\texttt{""(())()""}\), \(\texttt{""()""}\) are regular, while \(\texttt{""(()""}\) and \(\texttt{""())(""}\) are not.Find the sequence \(s\) using no more than \(200\) queries.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 20\)). The description of the test cases follows.
|
In the first test case, the hidden bracket sequence is \(s=\texttt{"")((""}\).For the query ""? 4 1 2 3 3"", the jury returns \(0\) because \(f(s_{1}s_{2}s_{3}s_{3}) = f(\texttt{"")(((""}) = 0\).For the query ""? 2 2 1"", the jury returns \(1\) because \(f(s_{2}s_{1}) = f(\texttt{""()""}) = 1\).For the query ""? 2 3 1"", the jury returns \(1\) because \(f(s_{3}s_{1}) = f(\texttt{""()""}) = 1\).In the second test case, the hidden bracket sequence is \(s=\texttt{""()""}\).For the query ""? 4 1 2 1 2"", the jury returns \(3\) because \(f(s_{1}s_{2}s_{1}s_{2}) = f(\texttt{""()()""}) = 3\).Note that the example is only for understanding the statement and does not guarantee finding the unique bracket sequence \(s\).
|
Input: 2 3 0 1 1 2 3 | Output: ? 4 1 2 3 3 ? 2 2 1 ? 2 3 1 ! )(( ? 4 1 2 1 2 ! ()
|
Hard
| 4 | 1,480 | 159 | 0 | 21 |
|
1,184 |
D1
|
1184D1
|
D1. Parallel Universes (Easy)
| 1,600 |
implementation
|
The Third Doctor Who once correctly said that travel between parallel universes is ""like travelling sideways"". However, he incorrectly thought that there were infinite parallel universes, whereas in fact, as we now all know, there will never be more than 250.Heidi recently got her hands on a multiverse observation tool. She was able to see all \(n\) universes lined up in a row, with non-existent links between them. She also noticed that the Doctor was in the \(k\)-th universe.The tool also points out that due to restrictions originating from the space-time discontinuum, the number of universes will never exceed \(m\).Obviously, the multiverse is unstable because of free will. Each time a decision is made, one of two events will randomly happen: a new parallel universe is created, or a non-existent link is broken.More specifically, When a universe is created, it will manifest itself between any two adjacent universes or at one of the ends. When a link is broken, it could be cut between any two adjacent universes. After separating the multiverse into two segments, the segment NOT containing the Doctor will cease to exist. Heidi wants to perform a simulation of \(t\) decisions. Each time a decision is made, Heidi wants to know the length of the multiverse (i.e. the number of universes), and the position of the Doctor.
|
The first line contains four integers \(n\), \(k\), \(m\) and \(t\) (\(2 \le k \le n \le m \le 250\), \(1 \le t \le 1000\)).Each of the following \(t\) lines is in one of the following formats: ""\(1\) \(i\)"" — meaning that a universe is inserted at the position \(i\) (\(1 \le i \le l + 1\)), where \(l\) denotes the current length of the multiverse. ""\(0\) \(i\)"" — meaning that the \(i\)-th link is broken (\(1 \le i \le l - 1\)), where \(l\) denotes the current length of the multiverse.
|
Output \(t\) lines. Each line should contain \(l\), the current length of the multiverse and \(k\), the current position of the Doctor.It is guaranteed that the sequence of the steps will be valid, i.e. the multiverse will have length at most \(m\) and when the link breaking is performed, there will be at least one universe in the multiverse.
|
The multiverse initially consisted of 5 universes, with the Doctor being in the second.First, link 1 was broken, leaving the multiverse with 4 universes, and the Doctor in the first.Then, a universe was added to the leftmost end of the multiverse, increasing the multiverse length to 5, and the Doctor was then in the second universe.Then, the rightmost link was broken.Finally, a universe was added between the first and the second universe.
|
Input: 5 2 10 4 0 1 1 1 0 4 1 2 | Output: 4 1 5 2 4 2 5 3
|
Medium
| 1 | 1,338 | 494 | 344 | 11 |
709 |
A
|
709A
|
A. Juicer
| 900 |
implementation
|
Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Kolya will put them in the juicer in the fixed order, starting with orange of size a1, then orange of size a2 and so on. To be put in the juicer the orange must have size not exceeding b, so if Kolya sees an orange that is strictly greater he throws it away and continues with the next one.The juicer has a special section to collect waste. It overflows if Kolya squeezes oranges of the total size strictly greater than d. When it happens Kolya empties the waste section (even if there are no more oranges) and continues to squeeze the juice. How many times will he have to empty the waste section?
|
The first line of the input contains three integers n, b and d (1 ≤ n ≤ 100 000, 1 ≤ b ≤ d ≤ 1 000 000) — the number of oranges, the maximum size of the orange that fits in the juicer and the value d, which determines the condition when the waste section should be emptied.The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1 000 000) — sizes of the oranges listed in the order Kolya is going to try to put them in the juicer.
|
Print one integer — the number of times Kolya will have to empty the waste section.
|
In the first sample, Kolya will squeeze the juice from two oranges and empty the waste section afterwards.In the second sample, the orange won't fit in the juicer so Kolya will have no juice at all.
|
Input: 2 7 105 6 | Output: 1
|
Beginner
| 1 | 684 | 437 | 83 | 7 |
1,569 |
F
|
1569F
|
F. Palindromic Hamiltonian Path
| 3,000 |
brute force; dfs and similar; dp; graphs; hashing
|
You are given a simple undirected graph with \(n\) vertices, \(n\) is even. You are going to write a letter on each vertex. Each letter should be one of the first \(k\) letters of the Latin alphabet.A path in the graph is called Hamiltonian if it visits each vertex exactly once. A string is called palindromic if it reads the same from left to right and from right to left. A path in the graph is called palindromic if the letters on the vertices in it spell a palindromic string without changing the order.A string of length \(n\) is good if: each letter is one of the first \(k\) lowercase Latin letters; if you write the \(i\)-th letter of the string on the \(i\)-th vertex of the graph, there will exist a palindromic Hamiltonian path in the graph. Note that the path doesn't necesserily go through the vertices in order \(1, 2, \dots, n\).Count the number of good strings.
|
The first line contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 12\); \(n\) is even; \(0 \le m \le \frac{n \cdot (n-1)}{2}\); \(1 \le k \le 12\)) — the number of vertices in the graph, the number of edges in the graph and the number of first letters of the Latin alphabet that can be used.Each of the next \(m\) lines contains two integers \(v\) and \(u\) (\(1 \le v, u \le n\); \(v \neq u\)) — the edges of the graph. The graph doesn't contain multiple edges and self-loops.
|
Print a single integer — number of good strings.
|
Input: 4 3 3 1 2 2 3 3 4 | Output: 9
|
Master
| 5 | 878 | 488 | 48 | 15 |
|
1,898 |
F
|
1898F
|
F. Vova Escapes the Matrix
| 2,600 |
brute force; dfs and similar; divide and conquer; shortest paths
|
Following a world tour, Vova got himself trapped inside an \(n \times m\) matrix. Rows of this matrix are numbered by integers from \(1\) to \(n\) from top to bottom, and the columns are numbered by integers from \(1\) to \(m\) from left to right. The cell \((i, j)\) is the cell on the intersection of row \(i\) and column \(j\) for \(1 \leq i \leq n\) and \(1 \leq j \leq m\).Some cells of this matrix are blocked by obstacles, while all other cells are empty. Vova occupies one of the empty cells. It is guaranteed that cells \((1, 1)\), \((1, m)\), \((n, 1)\), \((n, m)\) (that is, corners of the matrix) are blocked.Vova can move from one empty cell to another empty cell if they share a side. Vova can escape the matrix from any empty cell on the boundary of the matrix; these cells are called exits.Vova defines the type of the matrix based on the number of exits he can use to escape the matrix: The \(1\)-st type: matrices with no exits he can use to escape. The \(2\)-nd type: matrices with exactly one exit he can use to escape. The \(3\)-rd type: matrices with multiple (two or more) exits he can use to escape. Before Vova starts moving, Misha can create more obstacles to block more cells. However, he cannot change the type of the matrix. What is the maximum number of cells Misha can block, so that the type of the matrix remains the same? Misha cannot block the cell Vova is currently standing on.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \leq n,m \leq 1000\)) — the dimensions of the matrix.The next \(n\) lines contain the description of the matrix: the \(i\)-th (\(1 \le i \le n\)) of them contains a string of length \(m\), consisting of characters '.', '#', and 'V'. The \(j\)-th character of the \(i\)-th line describes the cell \((i, j)\) of the matrix. The dot '.' denotes an empty cell, the sharp '#' denotes a blocked cell, and the letter 'V' denotes an empty cell where Vova initially is.It is guaranteed that the corners of the matrix are blocked (the first and the last characters of the first and the last lines of the matrix description are '#'). It is guaranteed that the letter 'V' appears in the matrix description exactly once.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(1\,000\,000\).
|
For each test case, output a single integer — the maximum number of cells Misha may block.
|
In the first test case, the matrix is of the \(3\)-rd type. Misha can create obstacles in all empty cells except the cells \((1, 3)\), \((2, 3)\), \((2, 4)\). There are \(9\) such cells, and adding such obstacles does not change the type of the matrix.In the second test case, the matrix is of the \(3\)-rd type. Blocking any cell changes the matrix type to the \(2\)-nd: one of the two exits will become unavailable for Vova. Thus, the answer is \(0\).In the third test case, the matrix is of the \(1\)-st type. No free cell exists (besides Vova's), so Misha cannot block any cell.In the fourth test case, the matrix is of the \(2\)-nd type. Misha can create \(3\) obstacles in cells \((5, 2)\), \((6, 3)\), \((6, 4)\) without changing the type of the matrix.In the fifth test case, the matrix is of the \(3\)-rd type. Misha can create \(4\) obstacles in cells \((2, 2)\), \((3, 2)\), \((4, 2)\), \((5, 2)\) or \(4\) obstacles in cells \((2, 4)\), \((3, 4)\), \((4, 4)\), \((5, 4)\) without changing the type of the matrix.
|
Input: 84 4#..#..V.....#..#3 6#.#####....#####V#3 3####V####6 5#.####...####.##V..##.#####..#7 5######.V.##.#.##.#.##.#.##...##.#.#3 7#.....#.#####.#...V.#5 8####.####..V#..##...#..##...##.####.####5 5#...###.####V####.###...# | Output: 9 0 0 3 4 10 12 5
|
Expert
| 4 | 1,414 | 1,044 | 90 | 18 |
685 |
E
|
685E
|
E. Travelling Through the Snow Queen's Kingdom
| 2,800 |
bitmasks; brute force; divide and conquer; graphs
|
Gerda is travelling to the palace of the Snow Queen.The road network consists of n intersections and m bidirectional roads. Roads are numbered from 1 to m. Snow Queen put a powerful spell on the roads to change the weather conditions there. Now, if Gerda steps on the road i at the moment of time less or equal to i, she will leave the road exactly at the moment i. In case she steps on the road i at the moment of time greater than i, she stays there forever.Gerda starts at the moment of time l at the intersection number s and goes to the palace of the Snow Queen, located at the intersection number t. Moreover, she has to be there at the moment r (or earlier), before the arrival of the Queen.Given the description of the road network, determine for q queries li, ri, si and ti if it's possible for Gerda to get to the palace on time.
|
The first line of the input contains integers n, m and q (2 ≤ n ≤ 1000, 1 ≤ m, q ≤ 200 000) — the number of intersections in the road network of Snow Queen's Kingdom, the number of roads and the number of queries you have to answer.The i-th of the following m lines contains the description of the road number i. The description consists of two integers vi and ui (1 ≤ vi, ui ≤ n, vi ≠ ui) — the indices of the intersections connected by the i-th road. It's possible to get both from vi to ui and from ui to vi using only this road. Each pair of intersection may appear several times, meaning there are several roads connecting this pair.Last q lines contain the queries descriptions. Each of them consists of four integers li, ri, si and ti (1 ≤ li ≤ ri ≤ m, 1 ≤ si, ti ≤ n, si ≠ ti) — the moment of time Gerda starts her journey, the last moment of time she is allowed to arrive to the palace, the index of the starting intersection and the index of the intersection where palace is located.
|
For each query print ""Yes"" (without quotes) if Gerda can be at the Snow Queen palace on time (not later than ri) or ""No"" (without quotes) otherwise.
|
Input: 5 4 61 22 33 43 51 3 1 41 3 2 41 4 4 51 4 4 12 3 1 42 2 2 3 | Output: YesYesYesNoNoYes
|
Master
| 4 | 839 | 993 | 152 | 6 |
|
989 |
B
|
989B
|
B. A Tide of Riverscape
| 1,200 |
constructive algorithms; strings
|
Walking along a riverside, Mino silently takes a note of something.""Time,"" Mino thinks aloud.""What?""""Time and tide wait for no man,"" explains Mino. ""My name, taken from the river, always reminds me of this.""""And what are you recording?""""You see it, tide. Everything has its own period, and I think I've figured out this one,"" says Mino with confidence.Doubtfully, Kanno peeks at Mino's records. The records are expressed as a string \(s\) of characters '0', '1' and '.', where '0' denotes a low tide, '1' denotes a high tide, and '.' denotes an unknown one (either high or low).You are to help Mino determine whether it's possible that after replacing each '.' independently with '0' or '1', a given integer \(p\) is not a period of the resulting string. In case the answer is yes, please also show such a replacement to Mino.In this problem, a positive integer \(p\) is considered a period of string \(s\), if for all \(1 \leq i \leq \lvert s \rvert - p\), the \(i\)-th and \((i + p)\)-th characters of \(s\) are the same. Here \(\lvert s \rvert\) is the length of \(s\).
|
The first line contains two space-separated integers \(n\) and \(p\) (\(1 \leq p \leq n \leq 2000\)) — the length of the given string and the supposed period, respectively.The second line contains a string \(s\) of \(n\) characters — Mino's records. \(s\) only contains characters '0', '1' and '.', and contains at least one '.' character.
|
Output one line — if it's possible that \(p\) is not a period of the resulting string, output any one of such strings; otherwise output ""No"" (without quotes, you can print letters in any case (upper or lower)).
|
In the first example, \(7\) is not a period of the resulting string because the \(1\)-st and \(8\)-th characters of it are different.In the second example, \(6\) is not a period of the resulting string because the \(4\)-th and \(10\)-th characters of it are different.In the third example, \(9\) is always a period because the only constraint that the first and last characters are the same is already satisfied.Note that there are multiple acceptable answers for the first two examples, you can print any of them.
|
Input: 10 71.0.1.0.1. | Output: 1000100010
|
Easy
| 2 | 1,084 | 339 | 212 | 9 |
645 |
C
|
645C
|
C. Enduring Exodus
| 1,600 |
binary search; two pointers
|
In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm and is traveling to the other side of Bovinia. During the journey, he and his k cows have decided to stay at the luxurious Grand Moo-dapest Hotel. The hotel consists of n rooms located in a row, some of which are occupied.Farmer John wants to book a set of k + 1 currently unoccupied rooms for him and his cows. He wants his cows to stay as safe as possible, so he wishes to minimize the maximum distance from his room to the room of his cow. The distance between rooms i and j is defined as |j - i|. Help Farmer John protect his cows by calculating this minimum possible distance.
|
The first line of the input contains two integers n and k (1 ≤ k < n ≤ 100 000) — the number of rooms in the hotel and the number of cows travelling with Farmer John.The second line contains a string of length n describing the rooms. The i-th character of the string will be '0' if the i-th room is free, and '1' if the i-th room is occupied. It is guaranteed that at least k + 1 characters of this string are '0', so there exists at least one possible choice of k + 1 rooms for Farmer John and his cows to stay in.
|
Print the minimum possible distance between Farmer John's room and his farthest cow.
|
In the first sample, Farmer John can book room 3 for himself, and rooms 1 and 4 for his cows. The distance to the farthest cow is 2. Note that it is impossible to make this distance 1, as there is no block of three consecutive unoccupied rooms.In the second sample, Farmer John can book room 1 for himself and room 3 for his single cow. The distance between him and his cow is 2.In the third sample, Farmer John books all three available rooms, taking the middle room for himself so that both cows are next to him. His distance from the farthest cow is 1.
|
Input: 7 20100100 | Output: 2
|
Medium
| 2 | 677 | 515 | 84 | 6 |
538 |
A
|
538A
|
A. Cutting Banner
| 1,400 |
brute force; implementation
|
A large banner with word CODEFORCES was ordered for the 1000-th onsite round of Codeforcesω that takes place on the Miami beach. Unfortunately, the company that made the banner mixed up two orders and delivered somebody else's banner that contains someone else's word. The word on the banner consists only of upper-case English letters.There is very little time to correct the mistake. All that we can manage to do is to cut out some substring from the banner, i.e. several consecutive letters. After that all the resulting parts of the banner will be glued into a single piece (if the beginning or the end of the original banner was cut out, only one part remains); it is not allowed change the relative order of parts of the banner (i.e. after a substring is cut, several first and last letters are left, it is allowed only to glue the last letters to the right of the first letters). Thus, for example, for example, you can cut a substring out from string 'TEMPLATE' and get string 'TEMPLE' (if you cut out string AT), 'PLATE' (if you cut out TEM), 'T' (if you cut out EMPLATE), etc.Help the organizers of the round determine whether it is possible to cut out of the banner some substring in such a way that the remaining parts formed word CODEFORCES.
|
The single line of the input contains the word written on the banner. The word only consists of upper-case English letters. The word is non-empty and its length doesn't exceed 100 characters. It is guaranteed that the word isn't word CODEFORCES.
|
Print 'YES', if there exists a way to cut out the substring, and 'NO' otherwise (without the quotes).
|
Input: CODEWAITFORITFORCES | Output: YES
|
Easy
| 2 | 1,254 | 245 | 101 | 5 |
|
2,121 |
D
|
2121D
|
D. 1709
| 1,300 |
implementation; sortings
|
You are given two arrays of integers \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\). It is guaranteed that each integer from \(1\) to \(2 \cdot n\) appears in exactly one of the arrays.You need to perform a certain number of operations (possibly zero) so that both of the following conditions are satisfied: For each \(1 \leq i < n\), it holds that \(a_i < a_{i + 1}\) and \(b_i < b_{i + 1}\). For each \(1 \leq i \leq n\), it holds that \(a_i < b_i\). During each operation, you can perform exactly one of the following three actions: Choose an index \(1 \leq i < n\) and swap the values \(a_i\) and \(a_{i + 1}\). Choose an index \(1 \leq i < n\) and swap the values \(b_i\) and \(b_{i + 1}\). Choose an index \(1 \leq i \leq n\) and swap the values \(a_i\) and \(b_i\).You do not need to minimize the number of operations, but the total number must not exceed \(1709\). Find any sequence of operations that satisfies both conditions.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 40\)) — the length of the arrays \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 2 \cdot n\)).The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \leq b_i \leq 2 \cdot n\)).It is guaranteed that each integer from \(1\) to \(2 \cdot n\) appears either in array \(a\) or in array \(b\).
|
For each test case, output the sequence of operations. In the first line for each test case, output the number of operations \(k\). Note that \(0 \leq k \leq 1709\). In the following \(k\) lines for each test case, output the operations themselves: If you want to swap the values \(a_i\) and \(a_{i + 1}\), output two integers \(1\) and \(i\). Note that \(1 \leq i < n\). If you want to swap the values \(b_i\) and \(b_{i + 1}\), output two integers \(2\) and \(i\). Note that \(1 \leq i < n\). If you want to swap the values \(a_i\) and \(b_i\), output two integers \(3\) and \(i\). Note that \(1 \leq i \leq n\).It can be shown that under the given constraints, a solution always exists.
|
In the first test case, \(a_1 < b_1\), so no operations need to be applied. In the second test case, \(a_1 > b_1\). After applying the operation, these values will be swapped. In the third test case, after applying the operation, \(a = [1, 3]\) and \(b = [2, 4]\). In the fourth test case, after applying the operation, \(a = [1, 2]\) and \(b = [3, 4]\).
|
Input: 611212121 34 221 43 236 5 43 2 135 3 42 6 1 | Output: 0 1 3 1 1 2 1 1 3 2 9 3 1 3 2 3 3 1 1 2 1 2 2 1 2 1 1 2 1 6 2 2 1 1 1 2 2 1 3 1 3 2
|
Easy
| 2 | 945 | 657 | 689 | 21 |
1,272 |
A
|
1272A
|
A. Three Friends
| 900 |
brute force; greedy; math; sortings
|
Three friends are going to meet each other. Initially, the first friend stays at the position \(x = a\), the second friend stays at the position \(x = b\) and the third friend stays at the position \(x = c\) on the coordinate axis \(Ox\).In one minute each friend independently from other friends can change the position \(x\) by \(1\) to the left or by \(1\) to the right (i.e. set \(x := x - 1\) or \(x := x + 1\)) or even don't change it.Let's introduce the total pairwise distance — the sum of distances between each pair of friends. Let \(a'\), \(b'\) and \(c'\) be the final positions of the first, the second and the third friend, correspondingly. Then the total pairwise distance is \(|a' - b'| + |a' - c'| + |b' - c'|\), where \(|x|\) is the absolute value of \(x\).Friends are interested in the minimum total pairwise distance they can reach if they will move optimally. Each friend will move no more than once. So, more formally, they want to know the minimum total pairwise distance they can reach after one minute.You have to answer \(q\) independent test cases.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) — the number of test cases.The next \(q\) lines describe test cases. The \(i\)-th test case is given as three integers \(a, b\) and \(c\) (\(1 \le a, b, c \le 10^9\)) — initial positions of the first, second and third friend correspondingly. The positions of friends can be equal.
|
For each test case print the answer on it — the minimum total pairwise distance (the minimum sum of distances between each pair of friends) if friends change their positions optimally. Each friend will move no more than once. So, more formally, you have to find the minimum total pairwise distance they can reach after one minute.
|
Input: 8 3 3 4 10 20 30 5 5 5 2 4 3 1 1000000000 1000000000 1 1000000000 999999999 3 2 5 3 2 6 | Output: 0 36 0 0 1999999994 1999999994 2 4
|
Beginner
| 4 | 1,075 | 358 | 330 | 12 |
|
384 |
B
|
384B
|
B. Multitasking
| 1,500 |
greedy; implementation; sortings; two pointers
|
Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort n arrays simultaneously, each array consisting of m integers.Iahub can choose a pair of distinct indices i and j (1 ≤ i, j ≤ m, i ≠ j). Then in each array the values at positions i and j are swapped only if the value at position i is strictly greater than the value at position j.Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the n arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most (at most pairs). Help Iahub, find any suitable array.
|
The first line contains three integers n (1 ≤ n ≤ 1000), m (1 ≤ m ≤ 100) and k. Integer k is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line i of the next n lines contains m integers separated by a space, representing the i-th array. For each element x of the array i, 1 ≤ x ≤ 106 holds.
|
On the first line of the output print an integer p, the size of the array (p can be at most ). Each of the next p lines must contain two distinct integers i and j (1 ≤ i, j ≤ m, i ≠ j), representing the chosen indices.If there are multiple correct answers, you can print any.
|
Consider the first sample. After the first operation, the arrays become [1, 3, 2, 5, 4] and [1, 2, 3, 4, 5]. After the second operation, the arrays become [1, 2, 3, 5, 4] and [1, 2, 3, 4, 5]. After the third operation they become [1, 2, 3, 4, 5] and [1, 2, 3, 4, 5].
|
Input: 2 5 01 3 2 5 41 4 3 2 5 | Output: 32 42 34 5
|
Medium
| 4 | 640 | 356 | 275 | 3 |
1,105 |
B
|
1105B
|
B. Zuhair and Strings
| 1,100 |
brute force; implementation; strings
|
Given a string \(s\) of length \(n\) and integer \(k\) (\(1 \le k \le n\)). The string \(s\) has a level \(x\), if \(x\) is largest non-negative integer, such that it's possible to find in \(s\): \(x\) non-intersecting (non-overlapping) substrings of length \(k\), all characters of these \(x\) substrings are the same (i.e. each substring contains only one distinct character and this character is the same for all the substrings). A substring is a sequence of consecutive (adjacent) characters, it is defined by two integers \(i\) and \(j\) (\(1 \le i \le j \le n\)), denoted as \(s[i \dots j]\) = ""\(s_{i}s_{i+1} \dots s_{j}\)"".For example, if \(k = 2\), then: the string ""aabb"" has level \(1\) (you can select substring ""aa""), the strings ""zzzz"" and ""zzbzz"" has level \(2\) (you can select two non-intersecting substrings ""zz"" in each of them), the strings ""abed"" and ""aca"" have level \(0\) (you can't find at least one substring of the length \(k=2\) containing the only distinct character). Zuhair gave you the integer \(k\) and the string \(s\) of length \(n\). You need to find \(x\), the level of the string \(s\).
|
The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) — the length of the string and the value of \(k\).The second line contains the string \(s\) of length \(n\) consisting only of lowercase Latin letters.
|
Print a single integer \(x\) — the level of the string.
|
In the first example, we can select \(2\) non-intersecting substrings consisting of letter 'a': ""(aa)ac(aa)bb"", so the level is \(2\).In the second example, we can select either substring ""a"" or ""b"" to get the answer \(1\).
|
Input: 8 2 aaacaabb | Output: 2
|
Easy
| 3 | 1,139 | 241 | 55 | 11 |
1,040 |
B
|
1040B
|
B. Shashlik Cooking
| 1,300 |
dp; greedy; math
|
Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.This time Miroslav laid out \(n\) skewers parallel to each other, and enumerated them with consecutive integers from \(1\) to \(n\) in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number \(i\), it leads to turning \(k\) closest skewers from each side of the skewer \(i\), that is, skewers number \(i - k\), \(i - k + 1\), ..., \(i - 1\), \(i + 1\), ..., \(i + k - 1\), \(i + k\) (if they exist). For example, let \(n = 6\) and \(k = 1\). When Miroslav turns skewer number \(3\), then skewers with numbers \(2\), \(3\), and \(4\) will come up turned over. If after that he turns skewer number \(1\), then skewers number \(1\), \(3\), and \(4\) will be turned over, while skewer number \(2\) will be in the initial position (because it is turned again).As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all \(n\) skewers with the minimal possible number of actions. For example, for the above example \(n = 6\) and \(k = 1\), two turnings are sufficient: he can turn over skewers number \(2\) and \(5\).Help Miroslav turn over all \(n\) skewers.
|
The first line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 1000\), \(0 \leq k \leq 1000\)) — the number of skewers and the number of skewers from each side that are turned in one step.
|
The first line should contain integer \(l\) — the minimum number of actions needed by Miroslav to turn over all \(n\) skewers. After than print \(l\) integers from \(1\) to \(n\) denoting the number of the skewer that is to be turned over at the corresponding step.
|
In the first example the first operation turns over skewers \(1\), \(2\) and \(3\), the second operation turns over skewers \(4\), \(5\), \(6\) and \(7\).In the second example it is also correct to turn over skewers \(2\) and \(5\), but turning skewers \(2\) and \(4\), or \(1\) and \(5\) are incorrect solutions because the skewer \(3\) is in the initial state after these operations.
|
Input: 7 2 | Output: 21 6
|
Easy
| 3 | 1,326 | 195 | 265 | 10 |
336 |
C
|
336C
|
C. Vasily the Bear and Sequence
| 1,800 |
brute force; greedy; implementation; number theory
|
Vasily the bear has got a sequence of positive integers a1, a2, ..., an. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b1, b2, ..., bk is such maximum non-negative integer v, that number b1 and b2 and ... and bk is divisible by number 2v without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b1 and b2 and ... and bk is divisible by 2v without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible.Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by ""&"", in Pascal — by ""and"".
|
The first line contains integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ a1 < a2 < ... < an ≤ 109).
|
In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b1, b2, ..., bk — the numbers to write out. You are allowed to print numbers b1, b2, ..., bk in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.
|
Input: 51 2 3 4 5 | Output: 24 5
|
Medium
| 4 | 1,003 | 148 | 456 | 3 |
|
599 |
A
|
599A
|
A. Patrick and Shopping
| 800 |
implementation
|
Today Patrick waits for a visit from his friend Spongebob. To prepare for the visit, Patrick needs to buy some goodies in two stores located near his house. There is a d1 meter long road between his house and the first shop and a d2 meter long road between his house and the second shop. Also, there is a road of length d3 directly connecting these two shops to each other. Help Patrick calculate the minimum distance that he needs to walk in order to go to both shops and return to his house. Patrick always starts at his house. He should visit both shops moving only along the three existing roads and return back to his house. He doesn't mind visiting the same shop or passing the same road multiple times. The only goal is to minimize the total distance traveled.
|
The first line of the input contains three integers d1, d2, d3 (1 ≤ d1, d2, d3 ≤ 108) — the lengths of the paths. d1 is the length of the path connecting Patrick's house and the first shop; d2 is the length of the path connecting Patrick's house and the second shop; d3 is the length of the path connecting both shops.
|
Print the minimum distance that Patrick will have to walk in order to visit both shops and return to his house.
|
The first sample is shown on the picture in the problem statement. One of the optimal routes is: house first shop second shop house.In the second sample one of the optimal routes is: house first shop house second shop house.
|
Input: 10 20 30 | Output: 60
|
Beginner
| 1 | 767 | 318 | 111 | 5 |
1,663 |
E
|
1663E
|
E. Are You Safe?
| 0 |
*special; implementation
|
Seven boys and seven girls from Athens were brought before King Minos. The evil king laughed and said ""I hope you enjoy your stay in Crete"".With a loud clang, the doors behind them shut, and darkness reigned. The terrified children huddled together. In the distance they heard a bellowing, unholy roar.Bravely, they advanced forward, to their doom.
|
\(n\) (\(1 \le n \le 100\)) — the dimensions of the labyrinth. Each of the next \(n\) lines contains a string of lowercase English letters of length \(n\).
|
Input: 10 atzxcodera mzhappyayc mineodteri trxesonaac mtollexcxc hjikeoutlc eascripsni rgvymnjcxm onzazwswwg geothermal | Output: YES
|
Beginner
| 2 | 350 | 155 | 0 | 16 |
||
1,765 |
E
|
1765E
|
E. Exchange
| 1,000 |
brute force; math
|
Monocarp is playing a MMORPG. There are two commonly used types of currency in this MMORPG — gold coins and silver coins. Monocarp wants to buy a new weapon for his character, and that weapon costs \(n\) silver coins. Unfortunately, right now, Monocarp has no coins at all.Monocarp can earn gold coins by completing quests in the game. Each quest yields exactly one gold coin. Monocarp can also exchange coins via the in-game trading system. Monocarp has spent days analyzing the in-game economy; he came to the following conclusion: it is possible to sell one gold coin for \(a\) silver coins (i. e. Monocarp can lose one gold coin to gain \(a\) silver coins), or buy one gold coin for \(b\) silver coins (i. e. Monocarp can lose \(b\) silver coins to gain one gold coin).Now Monocarp wants to calculate the minimum number of quests that he has to complete in order to have at least \(n\) silver coins after some abuse of the in-game economy. Note that Monocarp can perform exchanges of both types (selling and buying gold coins for silver coins) any number of times.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of one line containing three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 10^7\); \(1 \le a, b \le 50\)).
|
For each test case, print one integer — the minimum possible number of quests Monocarp has to complete.
|
In the first test case of the example, Monocarp should complete \(4\) quests, and then sell \(4\) gold coins for \(100\) silver coins.In the second test case, Monocarp should complete \(400000\) quests, and then sell \(400000\) gold coins for \(10\) million silver coins.In the third test case, Monocarp should complete \(1\) quest, sell the gold coin for \(50\) silver coins, buy a gold coin for \(48\) silver coins, and then sell it again for \(50\) coins. So, he will have \(52\) silver coins.In the fourth test case, Monocarp should complete \(1\) quest and then sell the gold coin he has obtained for \(50\) silver coins.
|
Input: 4100 25 309999997 25 5052 50 4849 50 1 | Output: 4 400000 1 1
|
Beginner
| 2 | 1,068 | 223 | 103 | 17 |
1,406 |
D
|
1406D
|
D. Three Sequences
| 2,200 |
constructive algorithms; data structures; greedy; math
|
You are given a sequence of \(n\) integers \(a_1, a_2, \ldots, a_n\).You have to construct two sequences of integers \(b\) and \(c\) with length \(n\) that satisfy: for every \(i\) (\(1\leq i\leq n\)) \(b_i+c_i=a_i\) \(b\) is non-decreasing, which means that for every \(1<i\leq n\), \(b_i\geq b_{i-1}\) must hold \(c\) is non-increasing, which means that for every \(1<i\leq n\), \(c_i\leq c_{i-1}\) must hold You have to minimize \(\max(b_i,c_i)\). In other words, you have to minimize the maximum number in sequences \(b\) and \(c\).Also there will be \(q\) changes, the \(i\)-th change is described by three integers \(l,r,x\). You should add \(x\) to \(a_l,a_{l+1}, \ldots, a_r\). You have to find the minimum possible value of \(\max(b_i,c_i)\) for the initial sequence and for sequence after each change.
|
The first line contains an integer \(n\) (\(1\leq n\leq 10^5\)).The secound line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\leq i\leq n\), \(-10^9\leq a_i\leq 10^9\)).The third line contains an integer \(q\) (\(1\leq q\leq 10^5\)).Each of the next \(q\) lines contains three integers \(l,r,x\) (\(1\leq l\leq r\leq n,-10^9\leq x\leq 10^9\)), desribing the next change.
|
Print \(q+1\) lines.On the \(i\)-th (\(1 \leq i \leq q+1\)) line, print the answer to the problem for the sequence after \(i-1\) changes.
|
In the first test: The initial sequence \(a = (2, -1, 7, 3)\). Two sequences \(b=(-3,-3,5,5),c=(5,2,2,-2)\) is a possible choice. After the first change \(a = (2, -4, 4, 0)\). Two sequences \(b=(-3,-3,5,5),c=(5,-1,-1,-5)\) is a possible choice. After the second change \(a = (2, -4, 6, 2)\). Two sequences \(b=(-4,-4,6,6),c=(6,0,0,-4)\) is a possible choice.
|
Input: 4 2 -1 7 3 2 2 4 -3 3 4 2 | Output: 5 5 6
|
Hard
| 4 | 811 | 377 | 137 | 14 |
2,106 |
D
|
2106D
|
D. Flower Boy
| 1,500 |
binary search; dp; greedy; two pointers
|
Flower boy has a garden of \(n\) flowers that can be represented as an integer sequence \(a_1, a_2, \dots, a_n\), where \(a_i\) is the beauty of the \(i\)-th flower from the left.Igor wants to collect exactly \(m\) flowers. To do so, he will walk the garden from left to right and choose whether to collect the flower at his current position. The \(i\)-th flower among ones he collects must have a beauty of at least \(b_i\).Igor noticed that it might be impossible to collect \(m\) flowers that satisfy his beauty requirements, so before he starts collecting flowers, he can pick any integer \(k\) and use his magic wand to grow a new flower with beauty \(k\) and place it anywhere in the garden (between two flowers, before the first flower, or after the last flower). Because his magic abilities are limited, he may do this at most once.Output the minimum integer \(k\) Igor must pick when he performs the aforementioned operation to ensure that he can collect \(m\) flowers. If he can collect \(m\) flowers without using the operation, output \(0\). If it is impossible to collect \(m\) flowers despite using the operation, output \(-1\).
|
The first line of the input contains a single integer \(t\) \((1 \le t \le 10^4)\) — the number of test cases.The first line of each test case contains exactly two integers \(n\) and \(m\) \((1 \le m \le n \le 2 \cdot 10^5)\) — the number of flowers in the garden and the number of flowers Igor wants to collect, respectively.The second line of each test case contains \(n\) integers \(a_1, a_2, ..., a_n\) \((1 \le a_i \le 10^9)\) — where \(a_i\) is the beauty of the \(i\)-th flower from the left in the garden.The third line of each test case contains \(m\) integers \(b_1, b_2, ..., b_m\) \((1 \le b_i \le 10^9)\) — where \(b_i\) is the minimum beauty the \(i\)-th flower must have that Igor will collect.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, on a separate line, output the minimum integer \(k\) Igor must pick when he performs the aforementioned operation to ensure that he can collect \(m\) flowers. If he can collect \(m\) flowers without using the operation, output \(0\). If it is impossible to collect \(m\) flowers despite using the operation, output \(-1\).
|
In the first test case, suppose Igor grows a flower of beauty \(6\) and places it between the third and the fourth flowers. Then, the garden will look like the following: \([3, 5, 2, 6, 3, 3, 5, 8, 1, 2]\). Then, he can select the second, fourth, sixth, seventh, and eighth flowers with beauties \([5, 6, 3, 5, 8]\). In the third test case, he can grow a flower of beauty \(7\) and place it before the first flower. The garden will look like the following: \([7, 4, 3, 5, 4, 3]\). Now, he can choose the first, second, and fourth flowers.In the fourth test case, Igor does not need to use the operation, so the answer is \(0\).In the sixth test case, no matter how Igor performs the operation, he cannot collect \(3\) flowers such that the \(i\)-th flower he collects has a beauty of at least \(b_i\), therefore the answer is \(-1\).
|
Input: 79 53 5 2 3 3 5 8 1 24 6 2 4 66 31 2 6 8 2 15 4 35 34 3 5 4 37 4 56 38 4 2 1 2 56 1 45 51 2 3 4 55 4 3 2 16 31 2 3 4 5 69 8 75 57 7 6 7 77 7 7 7 7 | Output: 6 3 7 0 -1 -1 7
|
Medium
| 4 | 1,142 | 801 | 342 | 21 |
583 |
A
|
583A
|
A. Asphalting Roads
| 1,000 |
implementation
|
City X consists of n vertical and n horizontal infinite roads, forming n × n intersections. Roads (both vertical and horizontal) are numbered from 1 to n, and the intersections are indicated by the numbers of the roads that form them.Sand roads have long been recognized out of date, so the decision was made to asphalt them. To do this, a team of workers was hired and a schedule of work was made, according to which the intersections should be asphalted.Road repairs are planned for n2 days. On the i-th day of the team arrives at the i-th intersection in the list and if none of the two roads that form the intersection were already asphalted they asphalt both roads. Otherwise, the team leaves the intersection, without doing anything with the roads.According to the schedule of road works tell in which days at least one road will be asphalted.
|
The first line contains integer n (1 ≤ n ≤ 50) — the number of vertical and horizontal roads in the city. Next n2 lines contain the order of intersections in the schedule. The i-th of them contains two numbers hi, vi (1 ≤ hi, vi ≤ n), separated by a space, and meaning that the intersection that goes i-th in the timetable is at the intersection of the hi-th horizontal and vi-th vertical roads. It is guaranteed that all the intersections in the timetable are distinct.
|
In the single line print the numbers of the days when road works will be in progress in ascending order. The days are numbered starting from 1.
|
In the sample the brigade acts like that: On the first day the brigade comes to the intersection of the 1-st horizontal and the 1-st vertical road. As none of them has been asphalted, the workers asphalt the 1-st vertical and the 1-st horizontal road; On the second day the brigade of the workers comes to the intersection of the 1-st horizontal and the 2-nd vertical road. The 2-nd vertical road hasn't been asphalted, but as the 1-st horizontal road has been asphalted on the first day, the workers leave and do not asphalt anything; On the third day the brigade of the workers come to the intersection of the 2-nd horizontal and the 1-st vertical road. The 2-nd horizontal road hasn't been asphalted but as the 1-st vertical road has been asphalted on the first day, the workers leave and do not asphalt anything; On the fourth day the brigade come to the intersection formed by the intersection of the 2-nd horizontal and 2-nd vertical road. As none of them has been asphalted, the workers asphalt the 2-nd vertical and the 2-nd horizontal road.
|
Input: 21 11 22 12 2 | Output: 1 4
|
Beginner
| 1 | 849 | 470 | 143 | 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.