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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
7 |
D
|
7D
|
D. Palindrome Degree
| 2,200 |
hashing; strings
|
String s of length n is called k-palindrome, if it is a palindrome itself, and its prefix and suffix of length are (k - 1)-palindromes. By definition, any string (even empty) is 0-palindrome.Let's call the palindrome degree of string s such a maximum number k, for which s is k-palindrome. For example, ""abaaba"" has degree equals to 3.You are given a string. Your task is to find the sum of the palindrome degrees of all its prefixes.
|
The first line of the input data contains a non-empty string, consisting of Latin letters and digits. The length of the string does not exceed 5Β·106. The string is case-sensitive.
|
Output the only number β the sum of the polindrome degrees of all the string's prefixes.
|
Input: a2A | Output: 1
|
Hard
| 2 | 436 | 179 | 88 | 0 |
|
670 |
B
|
670B
|
B. Game of Robots
| 1,000 |
implementation
|
In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier β an integer from 1 to 109.At some moment, robots decided to play the game ""Snowball"". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier.Your task is to determine the k-th identifier to be pronounced.
|
The first line contains two positive integers n and k (1 β€ n β€ 100 000, 1 β€ k β€ min(2Β·109, nΒ·(n + 1) / 2).The second line contains the sequence id1, id2, ..., idn (1 β€ idi β€ 109) β identifiers of roborts. It is guaranteed that all identifiers are different.
|
Print the k-th pronounced identifier (assume that the numeration starts from 1).
|
In the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1.In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals to 4.
|
Input: 2 21 2 | Output: 1
|
Beginner
| 1 | 699 | 257 | 80 | 6 |
382 |
D
|
382D
|
D. Ksenia and Pawns
| 2,200 |
dfs and similar; graphs; implementation; trees
|
Ksenia has a chessboard of size n Γ m. Each cell of the chessboard contains one of the characters: ""<"", "">"", ""^"", ""v"", ""#"". The cells that contain character ""#"" are blocked. We know that all chessboard cells that touch the border are blocked.Ksenia is playing with two pawns on this chessboard. Initially, she puts the pawns on the chessboard. One cell of the chessboard can contain two pawns if and only if the cell is blocked. In other cases two pawns can not stand in one cell. The game begins when Ksenia put pawns on the board. In one move, Ksenia moves each pawn to a side adjacent cell in the direction of arrows painted on the cell on which the corresponding pawn sits (if the pawn sits on ""#"", it does not move). Assume that Ksenia moves pawns simultaneously (see the second test case). Of course, Ksenia plays for points. How can one calculate the points per game? Very simply! Let's count how many movements the first pawn made and how many movements the second pawn made, sum these two numbers β it will be the resulting score of the game. Ksenia wonders: what is the maximum number of points she can earn (for that, she should place the pawns optimally well early in the game). Help her and find that number.
|
The first line contains two integers, n and m (1 β€ n, m β€ 2000) β the sizes of the board. Each of the following n lines contains m characters β the board's description. Each character is one of the characters: ""<"", "">"", ""^"", ""v"", ""#"".It is guaranteed that the border cells of the table are blocked cells (with character ""#"").
|
If Ksenia can get infinitely many points, print -1. Otherwise, print the maximum number of points she can get.
|
Input: 1 1# | Output: 0
|
Hard
| 4 | 1,235 | 337 | 110 | 3 |
|
533 |
C
|
533C
|
C. Board Game
| 1,700 |
games; greedy; implementation; math
|
Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (x, y) to (x - 1, y) or (x, y - 1). Vasiliy can move his pawn from (x, y) to one of cells: (x - 1, y), (x - 1, y - 1) and (x, y - 1). Both players are also allowed to skip move. There are some additional restrictions β a player is forbidden to move his pawn to a cell with negative x-coordinate or y-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0, 0). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well.
|
The first line contains four integers: xp, yp, xv, yv (0 β€ xp, yp, xv, yv β€ 105) β Polycarp's and Vasiliy's starting coordinates.It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0, 0).
|
Output the name of the winner: ""Polycarp"" or ""Vasiliy"".
|
In the first sample test Polycarp starts in (2, 1) and will move to (1, 1) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1, 0) and finally to (0, 0) in the third turn.
|
Input: 2 1 2 2 | Output: Polycarp
|
Medium
| 4 | 748 | 240 | 59 | 5 |
516 |
E
|
516E
|
E. Drazil and His Happy Friends
| 3,100 |
math; number theory
|
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if it is happy originally), he stays happy forever.Drazil wants to know on which day all his friends become happy or to determine if they won't become all happy at all.
|
The first line contains two integer n and m (1 β€ n, m β€ 109).The second line contains integer b (0 β€ b β€ min(n, 105)), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 β€ xi < n), denoting the list of indices of happy boys.The third line conatins integer g (0 β€ g β€ min(m, 105)), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 β€ yj < m), denoting the list of indices of happy girls.It is guaranteed that there is at least one person that is unhappy among his friends.
|
Print the number of the first day that all friends of Drazil become happy. If this day won't come at all, you print -1.
|
By we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
|
Input: 2 301 0 | Output: 4
|
Master
| 2 | 716 | 600 | 119 | 5 |
2,007 |
C
|
2007C
|
C. Dora and C++
| 1,500 |
math; number theory
|
Dora has just learned the programming language C++!However, she has completely misunderstood the meaning of C++. She considers it as two kinds of adding operations on the array \(c\) with \(n\) elements. Dora has two integers \(a\) and \(b\). In one operation, she can choose one of the following things to do. Choose an integer \(i\) such that \(1 \leq i \leq n\), and increase \(c_i\) by \(a\). Choose an integer \(i\) such that \(1 \leq i \leq n\), and increase \(c_i\) by \(b\). Note that \(a\) and \(b\) are constants, and they can be the same.Let's define a range of array \(d\) as \(\max(d_i) - \min(d_i)\). For instance, the range of the array \([1, 2, 3, 4]\) is \(4 - 1 = 3\), the range of the array \([5, 2, 8, 2, 2, 1]\) is \(8 - 1 = 7\), and the range of the array \([3, 3, 3]\) is \(3 - 3 = 0\).After any number of operations (possibly, \(0\)), Dora calculates the range of the new array. You need to help Dora minimize this value, but since Dora loves exploring all by herself, you only need to tell her the minimized value.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(a\), and \(b\) (\(1 \leq n \leq 10^5\), \(1 \leq a, b \leq 10^9\)) β the length of the array \(c\) and the constant values, respectively.The second line of each test case contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \leq c_i \leq 10^9\)) β the initial elements of the array \(c\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output a single integer β the minimum possible range of the array after any number of operations.
|
In the first test case, we can increase \(c_1 = 1\) by \(a = 5\). The array \(c\) will become \([6, 3, 4, 4]\), and the range is \(3\). Note that there is more than one way to reach the answer.In the second test case, we can increase \(c_1 = 1\) by \(a = 2\) and then increase \(c_1 = 3\) by \(b = 3\). Also, we can increase \(c_2 = 3\) by \(b = 3\) and increase \(c_3 = 4\) by \(a = 2\). The array \(c\) will become \([6, 6, 6, 6]\), and the range is \(0\).
|
Input: 104 5 51 3 4 44 2 31 3 4 64 7 71 1 2 63 15 91 9 53 18 121 4 57 27 3633 13 23 12 35 24 4110 6 915 5 6 9 8 2 12 15 3 82 1 10000000001 10000000006 336718728 709848696552806726 474775724 15129785 371139304 178408298 131060716 335734893 671469786138885253 70095920 456876775 9345665 214704906 375508929 | Output: 3 0 3 2 3 5 1 0 17 205359241
|
Medium
| 2 | 1,039 | 621 | 117 | 20 |
1,896 |
E
|
1896E
|
E. Permutation Sorting
| 2,100 |
data structures; sortings
|
You are given a permutation\(^\dagger\) \(a\) of size \(n\). We call an index \(i\) good if \(a_i=i\) is satisfied. After each second, we rotate all indices that are not good to the right by one position. Formally, Let \(s_1,s_2,\ldots,s_k\) be the indices of \(a\) that are not good in increasing order. That is, \(s_j < s_{j+1}\) and if index \(i\) is not good, then there exists \(j\) such that \(s_j=i\). For each \(i\) from \(1\) to \(k\), we assign \(a_{s_{(i \% k+1)}} := a_{s_i}\) all at once. For each \(i\) from \(1\) to \(n\), find the first time that index \(i\) becomes good. \(^\dagger\) A permutation 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).
|
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 first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β the size of permutation \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the elements of permutation \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output a single line containing \(n\) integers where the \(i\)-th integer represents the first time that index \(i\) becomes good.
|
In the first test case, \(2\) and \(5\) are already in the correct position so indices \(2\) and \(5\) become good at \(0\) second. After \(1\) second, a cyclic shift will be done with \(s=[1, 3, 4]\), resulting in array \(a=[1, 2, 3, 4, 5]\). Notice that indices \(1\), \(3\) and \(4\) become good at \(1\) second.In the second test case, \(5\) is already in the correct position, so index \(5\) becomes good at \(0\) second. After \(1\) second, a cyclic shift will be done with \(s=[1, 2, 3, 4, 6]\), resulting in array \(a=[3, 2, 1, 4, 5, 6]\). Notice that indices \(2\), \(4\) and \(6\) become good at \(1\) second. After \(2\) seconds, a cyclic shift will be done with \(s=[1, 3]\), resulting in array \(a=[1, 2, 3, 4, 5, 6]\). Notice that indices \(1\) and \(3\) become good at \(2\) second.
|
Input: 253 2 4 1 562 1 4 6 5 3 | Output: 1 0 1 1 0 2 1 2 1 0 1
|
Hard
| 2 | 909 | 507 | 150 | 18 |
2,029 |
G
|
2029G
|
G. Balanced Problem
| 3,000 |
data structures; dp
|
There is an array \(a\) consisting of \(n\) integers. Initially, all elements of \(a\) are equal to \(0\).Kevin can perform several operations on the array. Each operation is one of the following two types: Prefix addition β Kevin first selects an index \(x\) (\(1\le x\le n\)), and then for each \(1\le j\le x\), increases \(a_j\) by \(1\); Suffix addition β Kevin first selects an index \(x\) (\(1\le x\le n\)), and then for each \(x\le j\le n\), increases \(a_j\) by \(1\).In the country of KDOI, people think that the integer \(v\) is balanced. Thus, Iris gives Kevin an array \(c\) consisting of \(n\) integers and defines the beauty of the array \(a\) as follows: Initially, set \(b=0\); For each \(1\le i\le n\), if \(a_i=v\), add \(c_i\) to \(b\); The beauty of \(a\) is the final value of \(b\).Kevin wants to maximize the beauty of \(a\) after all the operations. However, he had already performed \(m\) operations when he was sleepy. Now, he can perform an arbitrary number (possibly zero) of new operations.You have to help Kevin find the maximum possible beauty if he optimally performs the new operations.However, to make sure that you are not just rolling the dice, Kevin gives you an integer \(V\), and you need to solve the problem for each \(1\le v\le V\).
|
Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1\le t\le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(V\) (\(1\le n, m\le 2\cdot 10^5\), \(1\le V\le 2000\)) β the length of the array \(a\), the number of initial operations, and the number that Kevin gives you.The second line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1\le c_i\le 10^9\)) β the elements in the array \(c\).Then \(m\) lines follow, the \(i\)-th line containing a character \(op\) and an integer \(x\) (\(op=\mathtt{L}\) or \(\mathtt{R}\), \(1\le x\le n\)) β the type of the \(i\)-th operation and the selected index. If \(op=\mathtt{L}\), this operation is a prefix addition on index \(x\); If \(op=\mathtt{R}\), this operation is a suffix addition on index \(x\). It is guaranteed that: the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\); the sum of \(m\) over all test cases does not exceed \(2\cdot 10^5\); the sum of \(V^2\) over all test cases does not exceed \(4\cdot 10^6\).
|
For each test case, output \(V\) integers in a single line, the \(i\)-th integer denoting the maximum possible beauty after Kevin performs some new operations when \(v=i\).
|
In the first test case, the array \(a\) changes as follows for the initial operations: \([0, 0, 0] \xrightarrow{\mathtt{L}\ 3} [1, 1, 1] \xrightarrow{\mathtt{R}\ 3} [1, 1, 2] \xrightarrow{\mathtt{L}\ 1} [2, 1, 2]\). For \(v=1\), it is optimal to not perform any new operations, and the beauty is \(b=c_2=2\); For \(v=2\), it is optimal to perform a prefix addition operation on index \(2\). After that, \(a\) becomes \([3,2,2]\), and the beauty is \(b=c_2+c_3=6\). In the second test case, for both \(v=1\) and \(v=2\), it is optimal to not perform any new operations.
|
Input: 53 3 21 2 4L 3R 3L 13 3 25 1 4L 3R 3L 15 4 51 1 1 1 1L 3R 2L 5L 410 12 910 9 8 7 6 5 4 3 2 1L 2L 4R 4R 4L 6R 8L 3L 2R 1R 10L 8L 11 1 41000000000L 1 | Output: 2 6 1 9 0 1 3 5 5 0 0 0 6 25 32 35 44 51 1000000000 1000000000 1000000000 1000000000
|
Master
| 2 | 1,274 | 1,137 | 172 | 20 |
1,419 |
E
|
1419E
|
E. Decryption
| 2,100 |
constructive algorithms; implementation; math; number theory
|
An agent called Cypher is decrypting a message, that contains a composite number \(n\). All divisors of \(n\), which are greater than \(1\), are placed in a circle. Cypher can choose the initial order of numbers in the circle.In one move Cypher can choose two adjacent numbers in a circle and insert their least common multiple between them. He can do that move as many times as needed.A message is decrypted, if every two adjacent numbers are not coprime. Note that for such constraints it's always possible to decrypt the message.Find the minimal number of moves that Cypher should do to decrypt the message, and show the initial order of numbers in the circle for that.
|
The first line contains an integer \(t\) \((1 \le t \le 100)\) β the number of test cases. Next \(t\) lines describe each test case.In a single line of each test case description, there is a single composite number \(n\) \((4 \le n \le 10^9)\) β the number from the message.It's guaranteed that the total number of divisors of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case in the first line output the initial order of divisors, which are greater than \(1\), in the circle. In the second line output, the minimal number of moves needed to decrypt the message.If there are different possible orders with a correct answer, print any of them.
|
In the first test case \(6\) has three divisors, which are greater than \(1\): \(2, 3, 6\). Regardless of the initial order, numbers \(2\) and \(3\) are adjacent, so it's needed to place their least common multiple between them. After that the circle becomes \(2, 6, 3, 6\), and every two adjacent numbers are not coprime.In the second test case \(4\) has two divisors greater than \(1\): \(2, 4\), and they are not coprime, so any initial order is correct, and it's not needed to place any least common multiples.In the third test case all divisors of \(30\) greater than \(1\) can be placed in some order so that there are no two adjacent numbers that are coprime.
|
Input: 3 6 4 30 | Output: 2 3 6 1 2 4 0 2 30 6 3 15 5 10 0
|
Hard
| 4 | 672 | 385 | 285 | 14 |
763 |
B
|
763B
|
B. Timofey and rectangles
| 2,100 |
constructive algorithms; geometry
|
One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length The picture corresponds to the first example
|
The first line contains single integer n (1 β€ n β€ 5Β·105) β the number of rectangles.n lines follow. The i-th of these lines contains four integers x1, y1, x2 and y2 ( - 109 β€ x1 < x2 β€ 109, - 109 β€ y1 < y2 β€ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.
|
Print ""NO"" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.Otherwise, print ""YES"" in the first line. Then print n lines, in the i-th of them print single integer ci (1 β€ ci β€ 4) β the color of i-th rectangle.
|
Input: 80 0 5 32 -1 5 0-3 -4 2 -1-1 -1 2 0-3 0 0 55 2 10 37 -3 10 24 -2 7 -1 | Output: YES12232241
|
Hard
| 2 | 674 | 435 | 342 | 7 |
|
1,272 |
B
|
1272B
|
B. Snow Walking Robot
| 1,200 |
constructive algorithms; greedy; implementation
|
Recently you have bought a snow walking robot and brought it home. Suppose your home is a cell \((0, 0)\) on an infinite grid.You also have the sequence of instructions of this robot. It is written as the string \(s\) consisting of characters 'L', 'R', 'U' and 'D'. If the robot is in the cell \((x, y)\) right now, he can move to one of the adjacent cells (depending on the current instruction). If the current instruction is 'L', then the robot can move to the left to \((x - 1, y)\); if the current instruction is 'R', then the robot can move to the right to \((x + 1, y)\); if the current instruction is 'U', then the robot can move to the top to \((x, y + 1)\); if the current instruction is 'D', then the robot can move to the bottom to \((x, y - 1)\). You've noticed the warning on the last page of the manual: if the robot visits some cell (except \((0, 0)\)) twice then it breaks.So the sequence of instructions is valid if the robot starts in the cell \((0, 0)\), performs the given instructions, visits no cell other than \((0, 0)\) two or more times and ends the path in the cell \((0, 0)\). Also cell \((0, 0)\) should be visited at most two times: at the beginning and at the end (if the path is empty then it is visited only once). For example, the following sequences of instructions are considered valid: ""UD"", ""RL"", ""UUURULLDDDDLDDRRUU"", and the following are considered invalid: ""U"" (the endpoint is not \((0, 0)\)) and ""UUDD"" (the cell \((0, 1)\) is visited twice).The initial sequence of instructions, however, might be not valid. You don't want your robot to break so you decided to reprogram it in the following way: you will remove some (possibly, all or none) instructions from the initial sequence of instructions, then rearrange the remaining instructions as you wish and turn on your robot to move. Your task is to remove as few instructions from the initial sequence as possible and rearrange the remaining ones so that the sequence is valid. Report the valid sequence of the maximum length you can obtain.Note that you can choose any order of remaining instructions (you don't need to minimize the number of swaps or any other similar metric).You have to answer \(q\) independent test cases.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^4\)) β the number of test cases.The next \(q\) lines contain test cases. The \(i\)-th test case is given as the string \(s\) consisting of at least \(1\) and no more than \(10^5\) characters 'L', 'R', 'U' and 'D' β the initial sequence of instructions.It is guaranteed that the sum of \(|s|\) (where \(|s|\) is the length of \(s\)) does not exceed \(10^5\) over all test cases (\(\sum |s| \le 10^5\)).
|
For each test case print the answer on it. In the first line print the maximum number of remaining instructions. In the second line print the valid sequence of remaining instructions \(t\) the robot has to perform. The moves are performed from left to right in the order of the printed sequence. If there are several answers, you can print any. If the answer is \(0\), you are allowed to print an empty line (but you can don't print it).
|
There are only two possible answers in the first test case: ""LR"" and ""RL"".The picture corresponding to the second test case: Note that the direction of traverse does not matter Another correct answer to the third test case: ""URDDLLLUURDR"".
|
Input: 6 LRU DURLDRUDRULRDURDDL LRUDDLRUDRUL LLLLRRRR URDUR LLL | Output: 2 LR 14 RUURDDDDLLLUUR 12 ULDDDRRRUULL 2 LR 2 UD 0
|
Easy
| 3 | 2,231 | 482 | 437 | 12 |
1,566 |
B
|
1566B
|
B. MIN-MEX Cut
| 800 |
bitmasks; constructive algorithms; dp; greedy
|
A binary string is a string that consists of characters \(0\) and \(1\).Let \(\operatorname{MEX}\) of a binary string be the smallest digit among \(0\), \(1\), or \(2\) that does not occur in the string. For example, \(\operatorname{MEX}\) of \(001011\) is \(2\), because \(0\) and \(1\) occur in the string at least once, \(\operatorname{MEX}\) of \(1111\) is \(0\), because \(0\) and \(2\) do not occur in the string and \(0 < 2\).A binary string \(s\) is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring β the whole string.A string \(a\) is a substring of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.What is the minimal sum of \(\operatorname{MEX}\) of all substrings pieces can be?
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Description of the test cases follows.Each test case contains a single binary string \(s\) (\(1 \le |s| \le 10^5\)).It's guaranteed that the sum of lengths of \(s\) over all test cases does not exceed \(10^5\).
|
For each test case print a single integer β the minimal sum of \(\operatorname{MEX}\) of all substrings that it is possible to get by cutting \(s\) optimally.
|
In the first test case the minimal sum is \(\operatorname{MEX}(0) + \operatorname{MEX}(1) = 1 + 0 = 1\).In the second test case the minimal sum is \(\operatorname{MEX}(1111) = 0\).In the third test case the minimal sum is \(\operatorname{MEX}(01100) = 2\).
|
Input: 6 01 1111 01100 101 0000 01010 | Output: 1 0 2 1 1 2
|
Beginner
| 4 | 946 | 351 | 158 | 15 |
1,120 |
D
|
1120D
|
D. Power Tree
| 2,500 |
dfs and similar; dp; dsu; graphs; greedy; trees
|
You are given a rooted tree with \(n\) vertices, the root of the tree is the vertex \(1\). Each vertex has some non-negative price. A leaf of the tree is a non-root vertex that has degree \(1\).Arkady and Vasily play a strange game on the tree. The game consists of three stages. On the first stage Arkady buys some non-empty set of vertices of the tree. On the second stage Vasily puts some integers into all leaves of the tree. On the third stage Arkady can perform several (possibly none) operations of the following kind: choose some vertex \(v\) he bought on the first stage and some integer \(x\), and then add \(x\) to all integers in the leaves in the subtree of \(v\). The integer \(x\) can be positive, negative of zero.A leaf \(a\) is in the subtree of a vertex \(b\) if and only if the simple path between \(a\) and the root goes through \(b\).Arkady's task is to make all integers in the leaves equal to zero. What is the minimum total cost \(s\) he has to pay on the first stage to guarantee his own win independently of the integers Vasily puts on the second stage? Also, we ask you to find all such vertices that there is an optimal (i.e. with cost \(s\)) set of vertices containing this one such that Arkady can guarantee his own win buying this set on the first stage.
|
The first line contains a single integer \(n\) (\(2 \le n \le 200\,000\)) β the number of vertices in the tree.The second line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(0 \le c_i \le 10^9\)), where \(c_i\) is the price of the \(i\)-th vertex.Each of the next \(n - 1\) lines contains two integers \(a\) and \(b\) (\(1 \le a, b \le n\)), denoting an edge of the tree.
|
In the first line print two integers: the minimum possible cost \(s\) Arkady has to pay to guarantee his own win, and the number of vertices \(k\) that belong to at least one optimal set.In the second line print \(k\) distinct integers in increasing order the indices of the vertices that belong to at least one optimal set.
|
In the second example all sets of two vertices are optimal. So, each vertex is in at least one optimal set.
|
Input: 5 5 1 3 2 1 1 2 2 3 2 4 1 5 | Output: 4 3 2 4 5
|
Expert
| 6 | 1,286 | 377 | 324 | 11 |
375 |
E
|
375E
|
E. Red and Black Tree
| 3,000 |
dp; implementation; math
|
You have a weighted tree, consisting of n vertices. Each vertex is either painted black or is painted red. A red and black tree is called beautiful, if for any its vertex we can find a black vertex at distance at most x.The distance between two nodes is the shortest path between them.You have a red and black tree. Your task is to make it beautiful in the minimum number of color swap operations. In one color swap operation, you can choose two vertices of different colors and paint each of them the other color. In other words, if you choose a red vertex p and a black vertex q, then in one operation you are allowed to paint p black and paint q red.Print the minimum number of required actions.
|
The first line contains two integers n and x (2 β€ n β€ 500; 1 β€ x β€ 109). The next line contains n integers, each of them is either a zero or one. If the i-th number equals 1, then vertex i of the tree is black, otherwise vertex i is red. Next n - 1 lines contain the tree edges. The j-th line contains integers uj vj wj (1 β€ uj, vj β€ n; uj β vj; 1 β€ wj β€ 109) which means that the tree has an edge of weight wj between vertices vj and uj.Assume that the tree vertices are numbered from 1 to n.
|
Print a single integer β the minimum number of required swap operations.If it is impossible to get a beautiful tree at any number of operations, print -1.
|
Input: 3 21 0 01 2 22 3 2 | Output: 1
|
Master
| 3 | 698 | 493 | 154 | 3 |
|
704 |
C
|
704C
|
C. Black Widow
| 2,900 |
dp; graphs; implementation; math
|
Natalia Romanova is trying to test something on the new gun S.H.I.E.L.D gave her. In order to determine the result of the test, she needs to find the number of answers to a certain equation. The equation is of form:Where represents logical OR and represents logical exclusive OR (XOR), and vi, j are some boolean variables or their negations. Natalia calls the left side of the equation a XNF formula. Each statement in brackets is called a clause, and vi, j are called literals.In the equation Natalia has, the left side is actually a 2-XNF-2 containing variables x1, x2, ..., xm and their negations. An XNF formula is 2-XNF-2 if: For each 1 β€ i β€ n, ki β€ 2, i.e. the size of each clause doesn't exceed two. Each variable occurs in the formula at most two times (with negation and without negation in total). Please note that it's possible that a variable occurs twice but its negation doesn't occur in any clause (or vice versa). Natalia is given a formula of m variables, consisting of n clauses. Please, make sure to check the samples in order to properly understand how the formula looks like.Natalia is more into fight than theory, so she asked you to tell her the number of answers to this equation. More precisely, you need to find the number of ways to set x1, ..., xm with true and false (out of total of 2m ways) so that the equation is satisfied. Since this number can be extremely large, you need to print the answer modulo 109 + 7.Please, note that some variable may appear twice in one clause, or not appear in the equation at all (but still, setting it to false or true gives different ways to set variables).
|
The first line of input contains two integers n and m (1 β€ n, m β€ 100 000) β the number of clauses and the number of variables respectively.The next n lines contain the formula. The i-th of them starts with an integer ki β the number of literals in the i-th clause. It is followed by ki non-zero integers ai, 1, ..., ai, ki. If ai, j > 0 then vi, j is xai, j otherwise it's negation of x - ai, j (1 β€ ki β€ 2, - m β€ ai, j β€ m, ai, j β 0).
|
Print the answer modulo 1 000 000 007 (109 + 7) in one line.
|
The equation in the first sample is:The equation in the second sample is:The equation in the third sample is:
|
Input: 6 72 4 -22 6 32 -7 12 -5 12 3 62 -2 -5 | Output: 48
|
Master
| 4 | 1,625 | 437 | 60 | 7 |
903 |
A
|
903A
|
A. Hungry Student Problem
| 900 |
greedy; implementation
|
Ivan's classes at the university have just finished, and now he wants to go to the local CFK cafe and eat some fried chicken.CFK sells chicken chunks in small and large portions. A small portion contains 3 chunks; a large one β 7 chunks. Ivan wants to eat exactly x chunks. Now he wonders whether he can buy exactly this amount of chicken.Formally, Ivan wants to know if he can choose two non-negative integers a and b in such a way that a small portions and b large ones contain exactly x chunks.Help Ivan to answer this question for several values of x!
|
The first line contains one integer n (1 β€ n β€ 100) β the number of testcases.The i-th of the following n lines contains one integer xi (1 β€ xi β€ 100) β the number of chicken chunks Ivan wants to eat.
|
Print n lines, in i-th line output YES if Ivan can buy exactly xi chunks. Otherwise, print NO.
|
In the first example Ivan can buy two small portions.In the second example Ivan cannot buy exactly 5 chunks, since one small portion is not enough, but two small portions or one large is too much.
|
Input: 265 | Output: YESNO
|
Beginner
| 2 | 555 | 200 | 94 | 9 |
1,194 |
D
|
1194D
|
D. 1-2-K Game
| 1,700 |
games; math
|
Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one).Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the chip is currently in the cell i, the player can move it into cell i - 1, i - 2 or i - k). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it k cells to the left if the current cell has number i < k. The player who can't make a move loses the game.Who wins if both participants play optimally?Alice and Bob would like to play several games, so you should determine the winner in each game.
|
The first line contains the single integer T (1 β€ T β€ 100) β the number of games. Next T lines contain one game per line. All games are independent.Each of the next T lines contains two integers n and k (0 β€ n β€ 109, 3 β€ k β€ 109) β the length of the strip and the constant denoting the third move, respectively.
|
For each game, print Alice if Alice wins this game and Bob otherwise.
|
Input: 40 33 33 44 4 | Output: BobAliceBobAlice
|
Medium
| 2 | 753 | 311 | 69 | 11 |
|
193 |
E
|
193E
|
E. Fibonacci Number
| 2,900 |
brute force; math; matrices
|
John Doe has a list of all Fibonacci numbers modulo 1013. This list is infinite, it starts with numbers 0 and 1. Each number in the list, apart from the first two, is a sum of previous two modulo 1013. That is, John's list is made from the Fibonacci numbers' list by replacing each number there by the remainder when divided by 1013. John got interested in number f (0 β€ f < 1013) and now wants to find its first occurrence in the list given above. Help John and find the number of the first occurence of number f in the list or otherwise state that number f does not occur in the list. The numeration in John's list starts from zero. There, the 0-th position is the number 0, the 1-st position is the number 1, the 2-nd position is the number 1, the 3-rd position is the number 2, the 4-th position is the number 3 and so on. Thus, the beginning of the list looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
|
The first line contains the single integer f (0 β€ f < 1013) β the number, which position in the list we should find.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Print a single number β the number of the first occurrence of the given number in John's list. If this number doesn't occur in John's list, print -1.
|
Input: 13 | Output: 7
|
Master
| 3 | 908 | 263 | 149 | 1 |
|
1,647 |
F
|
1647F
|
F. Madoka and Laziness
| 3,100 |
dp; greedy
|
Madoka has become too lazy to write a legend, so let's go straight to the formal description of the problem.An array of integers \(a_1, a_2, \ldots, a_n\) is called a hill if it is not empty and there is an index \(i\) in it, for which the following is true: \(a_1 < a_2 < \ldots < a_i > a_{i + 1} > a_{i + 2} > \ldots > a_n\).A sequence \(x\) is a subsequence of a sequence \(y\) if \(x\) can be obtained from \(y\) by deletion of several (possibly, zero or all) elements keeping the order of the other elements. For example, for an array \([69, 1000, 228, -7]\) the array \([1000, -7]\) is a subsequence, while \([1]\) and \([-7, 1000]\) are not.Splitting an array into two subsequences is called good if each element belongs to exactly one subsequence, and also each of these subsequences is a hill.You are given an array of distinct positive integers \(a_1, a_2, \ldots a_n\). It is required to find the number of different pairs of maxima of the first and second subsequences among all good splits. Two pairs that only differ in the order of elements are considered same.
|
The first line of input contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)) β array size.The second line of input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array. It is guaranteed that all \(a_i\) are pairwise distinct.
|
In a single line, print exactly one number β the number of different pairs of maxima of the first and second subsequences among all good splits.
|
In the first test case there are 3 possible pairs: \((3, 4)\), \((2, 4)\), \((1, 4)\). And they are achieved with the following partitions: \([1, 2, 3], [4]\); \([4, 3], [1, 2]\); \([1], [2, 4, 3]\)
|
Input: 4 1 2 4 3 | Output: 3
|
Master
| 2 | 1,076 | 284 | 144 | 16 |
1,822 |
A
|
1822A
|
A. TubeTube Feed
| 800 |
brute force; implementation
|
Mushroom Filippov cooked himself a meal and while having his lunch, he decided to watch a video on TubeTube. He can not spend more than \(t\) seconds for lunch, so he asks you for help with the selection of video.The TubeTube feed is a list of \(n\) videos, indexed from \(1\) to \(n\). The \(i\)-th video lasts \(a_i\) seconds and has an entertainment value \(b_i\). Initially, the feed is opened on the first video, and Mushroom can skip to the next video in \(1\) second (if the next video exists). Mushroom can skip videos any number of times (including zero).Help Mushroom choose one video that he can open and watch in \(t\) seconds. If there are several of them, he wants to choose the most entertaining one. Print the index of any appropriate video, or \(-1\) if there is no such.
|
The first line of the input data contains a single integer \(q\) (\(1 \le q \le 1000\)) β the number of test cases in the test.The description of the test cases follows.The first line of a test case contains two integers \(n\) and \(t\) (\(1 \le n \le 50\), \(1 \le t \le 200\)) β the number of videos in the feed and seconds for lunch, respectively.The second line of a test case contains \(n\) integers \(a_1, a_2, a_3, \dots, a_n\) (\(1 \le a_i \le 100\)) β durations of videos. The third line of a test case contains \(n\) integers \(b_1, b_2, b_3, \dots, b_n\) (\(1 \le b_i \le 100\)) β entertainment values of videos.
|
Output \(q\) integers, each of which is the answer to the corresponding test case. As an answer, output the index of the most entertaining video that Mushroom will have time to watch. If there are several answers, you are allowed to output any of them. Output \(-1\), if there is no video he can watch during his lunch break.
|
Input: 55 91 5 7 6 63 4 7 1 94 44 3 3 21 2 3 45 75 5 5 5 52 1 3 9 74 3354 71 69 9642 24 99 12 17955 6677 88 | Output: 3 2 3 -1 2
|
Beginner
| 2 | 788 | 623 | 325 | 18 |
|
1,488 |
G
|
1488G
|
G. Painting Numbers
| 2,500 |
*special; data structures; greedy; number theory
|
You are given \(n\) integers, each integer is from \(1\) to \(n\), all of them are pairwise distinct. You have to paint them red and blue (each integer should have exactly one color).The cost of painting is the number of pairs \((x, y)\) such that \(y \bmod x = 0\), \(y\) is red and \(x\) is blue.For each \(k \in [1, n]\), calculate the maximum cost of painting if exactly \(k\) integers should have a red color.
|
The first line contains one integer \(n\) (\(2 \le n \le 10^5\)).
|
For each \(k \in [1,n]\) print one integer β the maximum cost of painting, if exactly \(k\) integers should be red.
|
Input: 6 | Output: 3 5 6 6 5 0
|
Expert
| 4 | 414 | 65 | 115 | 14 |
|
2,120 |
A
|
2120A
|
A. Square of Rectangles
| 800 |
geometry; math
|
Aryan is an ardent lover of squares but a hater of rectangles (Yes, he knows all squares are rectangles). But Harshith likes to mess with Aryan. Harshith gives Aryan three rectangles of sizes \(l_1\times b_1\), \(l_2\times b_2\), and \(l_3\times b_3\) such that \(l_3\leq l_2\leq l_1\) and \(b_3\leq b_2\leq b_1\). Aryan, in order to defeat Harshith, decides to arrange these three rectangles to form a square such that no two rectangles overlap and the rectangles are aligned along edges. Rotating rectangles is not allowed. Help Aryan determine if he can defeat Harshith.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. Each test case contains a single line with \(6\) space-separated integers \(l_1, b_1, l_2, b_2, l_3\), and \(b_3\) (\(1 \leq l_3\le l_2\le l_1\le 100\), \(1\le b_3\le b_2\le b_1 \leq 100\)) β the dimensions of the three rectangles.
|
For each testcase, print ""YES"" if the rectangles can be arranged to form a square; otherwise, ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the second test case, the three rectangles \(5\times 3\), \(5\times 1\), and \(5\times 1\) can be arranged as follows to form a square. In the fourth test case, it can be proven that the rectangles can't be arranged to form a square with the given constraints.
|
Input: 5100 100 10 10 1 15 3 5 1 5 12 3 1 2 1 18 5 3 5 3 33 3 3 3 2 1 | Output: NO YES YES NO NO
|
Beginner
| 2 | 573 | 392 | 264 | 21 |
1,809 |
D
|
1809D
|
D. Binary String Sorting
| 1,800 |
constructive algorithms; greedy
|
You are given a binary string \(s\) consisting of only characters 0 and/or 1.You can perform several operations on this string (possibly zero). There are two types of operations: choose two consecutive elements and swap them. In order to perform this operation, you pay \(10^{12}\) coins; choose any element from the string and remove it. In order to perform this operation, you pay \(10^{12}+1\) coins. Your task is to calculate the minimum number of coins required to sort the string \(s\) in non-decreasing order (i. e. transform \(s\) so that \(s_1 \le s_2 \le \dots \le s_m\), where \(m\) is the length of the string after applying all operations). An empty string is also considered sorted in non-decreasing order.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each test case contains the string \(s\) (\(1 \le |s| \le 3 \cdot 10^5\)), consisting of only characters 0 and/or 1.The sum of lengths of all given strings doesn't exceed \(3 \cdot 10^5\).
|
For each test case, print a single integer β the minimum number of coins required to sort the string \(s\) in non-decreasing order.
|
In the first example, you have to remove the \(1\)-st element, so the string becomes equal to 00.In the second example, the string is already sorted.In the third example, you have to swap the \(2\)-nd and the \(3\)-rd elements, so the string becomes equal to 0011.In the fourth example, you have to swap the \(3\)-rd and the \(4\)-th elements, so the string becomes equal to 00011101, and then remove the \(7\)-th element, so the string becomes equal to 0001111.In the fifth example, you have to remove the \(1\)-st element, so the string becomes equal to 001101, and then remove the \(5\)-th element, so the string becomes equal to 00111.In the sixth example, the string is already sorted.
|
Input: 61000010100101101100110111111 | Output: 1000000000001 0 1000000000000 2000000000001 2000000000002 0
|
Medium
| 2 | 720 | 302 | 131 | 18 |
1,344 |
C
|
1344C
|
C. Quantifier Question
| 2,600 |
dfs and similar; dp; graphs; math
|
Logical quantifiers are very useful tools for expressing claims about a set. For this problem, let's focus on the set of real numbers specifically. The set of real numbers includes zero and negatives. There are two kinds of quantifiers: universal (\(\forall\)) and existential (\(\exists\)). You can read more about them here.The universal quantifier is used to make a claim that a statement holds for all real numbers. For example: \(\forall x,x<100\) is read as: for all real numbers \(x\), \(x\) is less than \(100\). This statement is false. \(\forall x,x>x-1\) is read as: for all real numbers \(x\), \(x\) is greater than \(x-1\). This statement is true. The existential quantifier is used to make a claim that there exists some real number for which the statement holds. For example: \(\exists x,x<100\) is read as: there exists a real number \(x\) such that \(x\) is less than \(100\). This statement is true. \(\exists x,x>x-1\) is read as: there exists a real number \(x\) such that \(x\) is greater than \(x-1\). This statement is true. Moreover, these quantifiers can be nested. For example: \(\forall x,\exists y,x<y\) is read as: for all real numbers \(x\), there exists a real number \(y\) such that \(x\) is less than \(y\). This statement is true since for every \(x\), there exists \(y=x+1\). \(\exists y,\forall x,x<y\) is read as: there exists a real number \(y\) such that for all real numbers \(x\), \(x\) is less than \(y\). This statement is false because it claims that there is a maximum real number: a number \(y\) larger than every \(x\). Note that the order of variables and quantifiers is important for the meaning and veracity of a statement.There are \(n\) variables \(x_1,x_2,\ldots,x_n\), and you are given some formula of the form $$$\( f(x_1,\dots,x_n):=(x_{j_1}<x_{k_1})\land (x_{j_2}<x_{k_2})\land \cdots\land (x_{j_m}<x_{k_m}), \)\(where \)\land\( denotes logical AND. That is, \)f(x_1,\ldots, x_n)\( is true if every inequality \)x_{j_i}<x_{k_i}\( holds. Otherwise, if at least one inequality does not hold, then \)f(x_1,\ldots,x_n)\( is false.Your task is to assign quantifiers \)Q_1,\ldots,Q_n\( to either universal (\)\forall\() or existential (\)\exists\() so that the statement \)\( Q_1 x_1, Q_2 x_2, \ldots, Q_n x_n, f(x_1,\ldots, x_n) \)\(is true, and the number of universal quantifiers is maximized, or determine that the statement is false for every possible assignment of quantifiers.Note that the order the variables appear in the statement is fixed. For example, if \)f(x_1,x_2):=(x_1<x_2)\( then you are not allowed to make \)x_2\( appear first and use the statement \)\forall x_2,\exists x_1, x_1<x_2\(. If you assign \)Q_1=\exists\( and \)Q_2=\forall\(, it will only be interpreted as \)\exists x_1,\forall x_2,x_1<x_2$$$.
|
The first line contains two integers \(n\) and \(m\) (\(2\le n\le 2\cdot 10^5\); \(1\le m\le 2\cdot 10^5\)) β the number of variables and the number of inequalities in the formula, respectively.The next \(m\) lines describe the formula. The \(i\)-th of these lines contains two integers \(j_i\),\(k_i\) (\(1\le j_i,k_i\le n\), \(j_i\ne k_i\)).
|
If there is no assignment of quantifiers for which the statement is true, output a single integer \(-1\).Otherwise, on the first line output an integer, the maximum possible number of universal quantifiers.On the next line, output a string of length \(n\), where the \(i\)-th character is ""A"" if \(Q_i\) should be a universal quantifier (\(\forall\)), or ""E"" if \(Q_i\) should be an existential quantifier (\(\exists\)). All letters should be upper-case. If there are multiple solutions where the number of universal quantifiers is maximum, print any.
|
For the first test, the statement \(\forall x_1, \exists x_2, x_1<x_2\) is true. Answers of ""EA"" and ""AA"" give false statements. The answer ""EE"" gives a true statement, but the number of universal quantifiers in this string is less than in our answer.For the second test, we can show that no assignment of quantifiers, for which the statement is true exists.For the third test, the statement \(\forall x_1, \forall x_2, \exists x_3, (x_1<x_3)\land (x_2<x_3)\) is true: We can set \(x_3=\max\{x_1,x_2\}+1\).
|
Input: 2 1 1 2 | Output: 1 AE
|
Expert
| 4 | 2,778 | 343 | 555 | 13 |
1,175 |
A
|
1175A
|
A. From Hero to Zero
| 900 |
implementation; math
|
You are given an integer \(n\) and an integer \(k\).In one step you can do one of the following moves: decrease \(n\) by \(1\); divide \(n\) by \(k\) if \(n\) is divisible by \(k\). For example, if \(n = 27\) and \(k = 3\) you can do the following steps: \(27 \rightarrow 26 \rightarrow 25 \rightarrow 24 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0\).You are asked to calculate the minimum number of steps to reach \(0\) from \(n\).
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of queries.The only line of each query contains two integers \(n\) and \(k\) (\(1 \le n \le 10^{18}\), \(2 \le k \le 10^{18}\)).
|
For each query print the minimum number of steps to reach \(0\) from \(n\) in single line.
|
Steps for the first test case are: \(59 \rightarrow 58 \rightarrow 57 \rightarrow 19 \rightarrow 18 \rightarrow 6 \rightarrow 2 \rightarrow 1 \rightarrow 0\).In the second test case you have to divide \(n\) by \(k\) \(18\) times and then decrease \(n\) by \(1\).
|
Input: 2 59 3 1000000000000000000 10 | Output: 8 19
|
Beginner
| 2 | 472 | 205 | 90 | 11 |
924 |
B
|
924B
|
B. Three-level Laser
| 1,600 |
binary search; greedy; two pointers
|
An atom of element X can exist in n distinct states with energies E1 < E2 < ... < En. Arkady wants to build a laser on this element, using a three-level scheme. Here is a simplified description of the scheme. Three distinct states i, j and k are selected, where i < j < k. After that the following process happens: initially the atom is in the state i, we spend Ek - Ei energy to put the atom in the state k, the atom emits a photon with useful energy Ek - Ej and changes its state to the state j, the atom spontaneously changes its state to the state i, losing energy Ej - Ei, the process repeats from step 1. Let's define the energy conversion efficiency as , i. e. the ration between the useful energy of the photon and spent energy.Due to some limitations, Arkady can only choose such three states that Ek - Ei β€ U.Help Arkady to find such the maximum possible energy conversion efficiency within the above constraints.
|
The first line contains two integers n and U (3 β€ n β€ 105, 1 β€ U β€ 109) β the number of states and the maximum possible difference between Ek and Ei.The second line contains a sequence of integers E1, E2, ..., En (1 β€ E1 < E2... < En β€ 109). It is guaranteed that all Ei are given in increasing order.
|
If it is not possible to choose three states that satisfy all constraints, print -1.Otherwise, print one real number Ξ· β the maximum possible energy conversion efficiency. Your answer is considered correct its absolute or relative error does not exceed 10 - 9.Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
|
In the first example choose states 1, 2 and 3, so that the energy conversion efficiency becomes equal to .In the second example choose states 4, 5 and 9, so that the energy conversion efficiency becomes equal to .
|
Input: 4 41 3 5 7 | Output: 0.5
|
Medium
| 3 | 923 | 301 | 358 | 9 |
875 |
C
|
875C
|
C. National Property
| 2,100 |
2-sat; dfs and similar; graphs; implementation
|
You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library.Some long and uninteresting story was removed...The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large version of a letter x is denoted by x'. BSCII encoding, which is used everywhere in Bookland, is made in that way so that large letters are presented in the order of the numbers they are denoted by, and small letters are presented in the order of the numbers they are denoted by, but all large letters are before all small letters. For example, the following conditions hold: 2 < 3, 2' < 3', 3' < 2.A word x1, x2, ..., xa is not lexicographically greater than y1, y2, ..., yb if one of the two following conditions holds: a β€ b and x1 = y1, ..., xa = ya, i.e. the first word is the prefix of the second word; there is a position 1 β€ j β€ min(a, b), such that x1 = y1, ..., xj - 1 = yj - 1 and xj < yj, i.e. at the first position where the words differ the first word has a smaller letter than the second word has. For example, the word ""3' 7 5"" is before the word ""2 4' 6"" in lexicographical order. It is said that sequence of words is in lexicographical order if each word is not lexicographically greater than the next word in the sequence.Denis has a sequence of words consisting of small letters only. He wants to change some letters to large (let's call this process a capitalization) in such a way that the sequence of words is in lexicographical order. However, he soon realized that for some reason he can't change a single letter in a single word. He only can choose a letter and change all of its occurrences in all words to large letters. He can perform this operation any number of times with arbitrary letters of Bookland's alphabet.Help Denis to choose which letters he needs to capitalize (make large) in order to make the sequence of words lexicographically ordered, or determine that it is impossible.Note that some words can be equal.
|
The first line contains two integers n and m (2 β€ n β€ 100 000, 1 β€ m β€ 100 000) β the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to m.Each of the next n lines contains a description of one word in format li, si, 1, si, 2, ..., si, li (1 β€ li β€ 100 000, 1 β€ si, j β€ m), where li is the length of the word, and si, j is the sequence of letters in the word. The words are given in the order Denis has them in the sequence.It is guaranteed that the total length of all words is not greater than 100 000.
|
In the first line print ""Yes"" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print ""No"" (without quotes).If the required is possible, in the second line print k β the number of letters Denis has to capitalize (make large), and in the third line print k distinct integers β these letters. Note that you don't need to minimize the value k.You can print the letters in any order. If there are multiple answers, print any of them.
|
In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following: 2' 1 1 3' 2' 1 1 The condition 2' < 1 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the first letters of the third and the fourth words are the same, and 3' < 1, then the third word is not lexicographically larger than the fourth word.In the second example the words are in lexicographical order from the beginning, so Denis can do nothing.In the third example there is no set of letters such that if Denis capitalizes them, the sequence becomes lexicographically ordered.
|
Input: 4 31 21 13 1 3 22 1 1 | Output: Yes22 3
|
Hard
| 4 | 2,069 | 602 | 540 | 8 |
1,401 |
E
|
1401E
|
E. Divide Square
| 2,400 |
data structures; geometry; implementation; sortings
|
There is a square of size \(10^6 \times 10^6\) on the coordinate plane with four points \((0, 0)\), \((0, 10^6)\), \((10^6, 0)\), and \((10^6, 10^6)\) as its vertices.You are going to draw segments on the plane. All segments are either horizontal or vertical and intersect with at least one side of the square.Now you are wondering how many pieces this square divides into after drawing all segments. Write a program calculating the number of pieces of the square.
|
The first line contains two integers \(n\) and \(m\) (\(0 \le n, m \le 10^5\)) β the number of horizontal segments and the number of vertical segments.The next \(n\) lines contain descriptions of the horizontal segments. The \(i\)-th line contains three integers \(y_i\), \(lx_i\) and \(rx_i\) (\(0 < y_i < 10^6\); \(0 \le lx_i < rx_i \le 10^6\)), which means the segment connects \((lx_i, y_i)\) and \((rx_i, y_i)\).The next \(m\) lines contain descriptions of the vertical segments. The \(i\)-th line contains three integers \(x_i\), \(ly_i\) and \(ry_i\) (\(0 < x_i < 10^6\); \(0 \le ly_i < ry_i \le 10^6\)), which means the segment connects \((x_i, ly_i)\) and \((x_i, ry_i)\).It's guaranteed that there are no two segments on the same line, and each segment intersects with at least one of square's sides.
|
Print the number of pieces the square is divided into after drawing all the segments.
|
The sample is like this:
|
Input: 3 3 2 3 1000000 4 0 4 3 0 1000000 4 0 1 2 0 5 3 1 1000000 | Output: 7
|
Expert
| 4 | 464 | 810 | 85 | 14 |
1,616 |
H
|
1616H
|
H. Keep XOR Low
| 3,000 |
bitmasks; combinatorics; data structures; divide and conquer; dp; math
|
You are given an array \(a_1, a_2, \ldots, a_n\) and an integer \(x\).Find the number of non-empty subsets of indices of this array \(1 \leq b_1 < b_2 < \ldots < b_k \leq n\), such that for all pairs \((i, j)\) where \(1 \leq i < j \leq k\), the inequality \(a_{b_i} \oplus a_{b_j} \leq x\) is held. Here, \(\oplus\) denotes the bitwise XOR operation. As the answer may be very large, output it modulo \(998\,244\,353\).
|
The first line of the input contains two integers \(n\) and \(x\) (\(1 \leq n \leq 150\,000\), \(0 \leq x < 2^{30}\)). Here, \(n\) is the size of the array.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < 2^{30}\)): the array itself.
|
Print one integer: the number of non-empty subsets such that the bitwise XOR of every pair of elements is at most \(x\), modulo \(998\,244\,353\).
|
Input: 4 2 0 1 2 3 | Output: 8
|
Master
| 6 | 420 | 264 | 146 | 16 |
|
733 |
F
|
733F
|
F. Drivers Dissatisfaction
| 2,200 |
data structures; dsu; graphs; trees
|
In one kingdom there are n cities and m two-way roads. Each road connects a pair of cities, and for each road we know the level of drivers dissatisfaction β the value wi.For each road we know the value ci β how many lamziks we should spend to reduce the level of dissatisfaction with this road by one. Thus, to reduce the dissatisfaction with the i-th road by k, we should spend kΒ·ci lamziks. And it is allowed for the dissatisfaction to become zero or even negative.In accordance with the king's order, we need to choose n - 1 roads and make them the main roads. An important condition must hold: it should be possible to travel from any city to any other by the main roads.The road ministry has a budget of S lamziks for the reform. The ministry is going to spend this budget for repair of some roads (to reduce the dissatisfaction with them), and then to choose the n - 1 main roads.Help to spend the budget in such a way and then to choose the main roads so that the total dissatisfaction with the main roads will be as small as possible. The dissatisfaction with some roads can become negative. It is not necessary to spend whole budget S.It is guaranteed that it is possible to travel from any city to any other using existing roads. Each road in the kingdom is a two-way road.
|
The first line contains two integers n and m (2 β€ n β€ 2Β·105, n - 1 β€ m β€ 2Β·105) β the number of cities and the number of roads in the kingdom, respectively.The second line contains m integers w1, w2, ..., wm (1 β€ wi β€ 109), where wi is the drivers dissatisfaction with the i-th road.The third line contains m integers c1, c2, ..., cm (1 β€ ci β€ 109), where ci is the cost (in lamziks) of reducing the dissatisfaction with the i-th road by one.The next m lines contain the description of the roads. The i-th of this lines contain a pair of integers ai and bi (1 β€ ai, bi β€ n, ai β bi) which mean that the i-th road connects cities ai and bi. All roads are two-way oriented so it is possible to move by the i-th road from ai to bi, and vice versa. It is allowed that a pair of cities is connected by more than one road. The last line contains one integer S (0 β€ S β€ 109) β the number of lamziks which we can spend for reforms.
|
In the first line print K β the minimum possible total dissatisfaction with main roads.In each of the next n - 1 lines print two integers x, vx, which mean that the road x is among main roads and the road x, after the reform, has the level of dissatisfaction vx.Consider that roads are numbered from 1 to m in the order as they are given in the input data. The edges can be printed in arbitrary order. If there are several answers, print any of them.
|
Input: 6 91 3 1 1 3 1 2 2 24 1 4 2 2 5 3 1 61 21 32 32 42 53 53 64 55 67 | Output: 01 13 16 17 28 -5
|
Hard
| 4 | 1,283 | 923 | 450 | 7 |
|
633 |
E
|
633E
|
E. Startup Funding
| 2,400 |
binary search; constructive algorithms; data structures; probabilities; two pointers
|
An e-commerce startup pitches to the investors to get funding. They have been functional for n weeks now and also have a website!For each week they know the number of unique visitors during this week vi and the revenue ci. To evaluate the potential of the startup at some range of weeks from l to r inclusive investors use the minimum among the maximum number of visitors multiplied by 100 and the minimum revenue during this period, that is: The truth is that investors have no idea how to efficiently evaluate the startup, so they are going to pick some k random distinct weeks li and give them to managers of the startup. For each li they should pick some ri β₯ li and report maximum number of visitors and minimum revenue during this period.Then, investors will calculate the potential of the startup for each of these ranges and take minimum value of p(li, ri) as the total evaluation grade of the startup. Assuming that managers of the startup always report the optimal values of ri for some particular li, i.e., the value such that the resulting grade of the startup is maximized, what is the expected resulting grade of the startup?
|
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 1 000 000).The second line contains n integers vi (1 β€ vi β€ 107) β the number of unique visitors during each week.The third line contains n integers ci (1 β€ ci β€ 107) βthe revenue for each week.
|
Print a single real value β the expected grade of the startup. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
|
Consider the first sample.If the investors ask for li = 1 onwards, startup will choose ri = 1, such that max number of visitors is 3 and minimum revenue is 300. Thus, potential in this case is min(3Β·100, 300) = 300.If the investors ask for li = 2 onwards, startup will choose ri = 3, such that max number of visitors is 2 and minimum revenue is 200. Thus, potential in this case is min(2Β·100, 200) = 200.If the investors ask for li = 3 onwards, startup will choose ri = 3, such that max number of visitors is 1 and minimum revenue is 300. Thus, potential in this case is min(1Β·100, 300) = 100.We have to choose a set of size 2 equi-probably and take minimum of each. The possible sets here are : {200, 300},{100, 300},{100, 200}, effectively the set of possible values as perceived by investors equi-probably: {200, 100, 100}. Thus, the expected value is (100 + 200 + 100) / 3 = 133.(3).
|
Input: 3 23 2 1300 200 300 | Output: 133.3333333
|
Expert
| 5 | 1,139 | 265 | 296 | 6 |
946 |
A
|
946A
|
A. Partition
| 800 |
greedy
|
You are given a sequence a consisting of n integers. You may partition this sequence into two sequences b and c in such a way that every element belongs exactly to one of these sequences. Let B be the sum of elements belonging to b, and C be the sum of elements belonging to c (if some of these sequences is empty, then its sum is 0). What is the maximum possible value of B - C?
|
The first line contains one integer n (1 β€ n β€ 100) β the number of elements in a.The second line contains n integers a1, a2, ..., an ( - 100 β€ ai β€ 100) β the elements of sequence a.
|
Print the maximum possible value of B - C, where B is the sum of elements of sequence b, and C is the sum of elements of sequence c.
|
In the first example we may choose b = {1, 0}, c = { - 2}. Then B = 1, C = - 2, B - C = 3.In the second example we choose b = {16, 23, 16, 15, 42, 8}, c = {} (an empty sequence). Then B = 120, C = 0, B - C = 120.
|
Input: 31 -2 0 | Output: 3
|
Beginner
| 1 | 379 | 183 | 132 | 9 |
799 |
G
|
799G
|
G. Cut the pie
| 3,500 |
binary search; data structures; geometry
|
Arkady reached the n-th level in Township game, so Masha decided to bake a pie for him! Of course, the pie has a shape of convex n-gon, i.e. a polygon with n vertices.Arkady decided to cut the pie in two equal in area parts by cutting it by a straight line, so that he can eat one of them and give the other to Masha. There is a difficulty because Arkady has already put a knife at some point of the pie, so he now has to cut the pie by a straight line passing trough this point.Help Arkady: find a line that passes through the point Arkady has put a knife into and cuts the pie into two parts of equal area, or determine that it's impossible. Your program has to quickly answer many queries with the same pie, but different points in which Arkady puts a knife.
|
The first line contains two integers n and q (3 β€ n β€ 104, 1 β€ q β€ 105) β the number of vertices in the pie and the number of queries.n line follow describing the polygon vertices in clockwise order. The i-th of these line contains two integers xi and yi ( - 106 β€ xi, yi β€ 106) β the coordinates of the i-th vertex. It is guaranteed that the polygon is strictly convex, in particular, no three vertices line on the same line.An empty line follows.q lines follow describing the query points. The i-th of these lines contain two integers xi and yi ( - 106 β€ xi, yi β€ 106) β the coordinates of the point in which Arkady puts the knife in the i-th query. In is guaranteed that in each query the given point is strictly inside the polygon, in particular, is not on its edges.
|
For each query print single integer β the polar angle of the line that is the answer for the corresponding query, in radians. The angle should be in the segment [0;Ο], the angles are measured from the direction of OX axis in counter-clockwise order. For example, the polar angle of the OY axis is . If there is no answer in that query, print -1.If there are several answers, print any of them. Your answer is considered correct if the difference between the areas of the parts divided by the total area of the polygon doesn't exceed 10 - 4 by absolute value. In other words, if a and b are the areas of the parts after the cut, then your answer is correct if and only of .
|
Input: 3 10 00 33 01 1 | Output: 2.67794504460098710000
|
Master
| 3 | 761 | 771 | 672 | 7 |
|
1,036 |
D
|
1036D
|
D. Vasya and Arrays
| 1,600 |
greedy; two pointers
|
Vasya has two arrays \(A\) and \(B\) of lengths \(n\) and \(m\), respectively.He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array \([1, 10, 100, 1000, 10000]\) Vasya can obtain array \([1, 1110, 10000]\), and from array \([1, 2, 3]\) Vasya can obtain array \([6]\).Two arrays \(A\) and \(B\) are considered equal if and only if they have the same length and for each valid \(i\) \(A_i = B_i\).Vasya wants to perform some of these operations on array \(A\), some on array \(B\), in such a way that arrays \(A\) and \(B\) become equal. Moreover, the lengths of the resulting arrays should be maximal possible.Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays \(A\) and \(B\) equal.
|
The first line contains a single integer \(n~(1 \le n \le 3 \cdot 10^5)\) β the length of the first array.The second line contains \(n\) integers \(a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9)\) β elements of the array \(A\).The third line contains a single integer \(m~(1 \le m \le 3 \cdot 10^5)\) β the length of the second array.The fourth line contains \(m\) integers \(b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9)\) - elements of the array \(B\).
|
Print a single integer β the maximum length of the resulting arrays after some operations were performed on arrays \(A\) and \(B\) in such a way that they became equal.If there is no way to make array equal, print ""-1"".
|
Input: 511 2 3 5 7411 7 3 7 | Output: 3
|
Medium
| 2 | 953 | 447 | 221 | 10 |
|
1,942 |
F
|
1942F
|
F. Farmer John's Favorite Function
| 2,700 |
brute force; data structures; implementation; math
|
ΩΩPARTS - Camelliaβ Farmer John has an array \(a\) of length \(n\). He also has a function \(f\) with the following recurrence: \(f(1) = \sqrt{a_1}\); For all \(i > 1\), \(f(i) = \sqrt{f(i-1)+a_i}\). Note that \(f(i)\) is not necessarily an integer.He plans to do \(q\) updates to the array. Each update, he gives you two integers \(k\) and \(x\) and he wants you to set \(a_k = x\). After each update, he wants to know \(\lfloor f(n) \rfloor\), where \(\lfloor t \rfloor\) denotes the value of \(t\) rounded down to the nearest integer.
|
The first line contains \(n\) and \(q\) (\(1 \leq n, q \leq 2 \cdot 10^5\)), the length of \(a\) and the number of updates he will perform.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^{18}\)).The next \(q\) lines each contain two integers \(k\) and \(x\) (\(1 \leq k \leq n\), \(0 \leq x \leq 10^{18}\)), the index of the update and the element he will replace \(a_k\) with.
|
For each update, output an integer, \(\lfloor f(n) \rfloor\), on a new line.
|
In the first test case, the array after the first update is \([4, 14, 0, 7, 6]\). The values of \(f\) are: \(f(1)=2\); \(f(2)=4\); \(f(3)=2\); \(f(4)=3\); \(f(5)=3\). Since \(\lfloor f(5) \rfloor = 3\), we output \(3\).The array after the second update is \([3, 14, 0, 7, 6]\). The values of \(f\), rounded to \(6\) decimal places, are: \(f(1)\approx 1.732051\); \(f(2)\approx 3.966365\); \(f(3)\approx 1.991573\); \(f(4)\approx 2.998595\); \(f(5)\approx 2.999766\). Since \(\lfloor f(5) \rfloor = 2\), we output \(2\).
|
Input: 5 60 14 0 7 61 41 32 154 15 25 8 | Output: 3 2 3 2 1 3
|
Master
| 4 | 536 | 417 | 76 | 19 |
177 |
A2
|
177A2
|
A2. Good Matrix Elements
| 800 |
implementation
|
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the ""middle"" row β the row which has exactly rows above it and the same number of rows below it. Elements of the ""middle"" column β the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 Γ 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
|
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.The input limitations for getting 30 points are: 1 β€ n β€ 5 The input limitations for getting 100 points are: 1 β€ n β€ 101
|
Print a single integer β the sum of good matrix elements.
|
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
|
Input: 31 2 34 5 67 8 9 | Output: 45
|
Beginner
| 1 | 650 | 309 | 57 | 1 |
1,446 |
D1
|
1446D1
|
D1. Frequency Problem (Easy Version)
| 2,600 |
data structures; greedy
|
This is the easy version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved.You are given an array \([a_1, a_2, \dots, a_n]\). Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs \(f\) times in this subarray, then at least \(2\) different values should occur exactly \(f\) times.An array \(c\) is a subarray of an array \(d\) if \(c\) can be obtained from \(d\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
The first line contains a single integer \(n\) (\(1 \le n \le 200\,000\)) β the length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le min(n, 100)\)) β elements of the array.
|
You should output exactly one integer β the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output \(0\).
|
In the first sample, the subarray \([1, 1, 2, 2, 3, 3]\) is good, but \([1, 1, 2, 2, 3, 3, 3]\) isn't: in the latter there are \(3\) occurrences of number \(3\), and no other element appears \(3\) times.
|
Input: 7 1 1 2 2 3 3 3 | Output: 6
|
Expert
| 2 | 771 | 222 | 172 | 14 |
832 |
B
|
832B
|
B. Petya and Exam
| 1,600 |
implementation; strings
|
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..There is a glob pattern in the statements (a string consisting of lowercase English letters, characters ""?"" and ""*""). It is known that character ""*"" occurs no more than once in the pattern.Also, n query strings are given, it is required to determine for each of them if the pattern matches it or not.Everything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning.A pattern matches a string if it is possible to replace each character ""?"" with one good lowercase English letter, and the character ""*"" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string.The good letters are given to Petya. All the others are bad.
|
The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad.The second line contains the pattern β a string s of lowercase English letters, characters ""?"" and ""*"" (1 β€ |s| β€ 105). It is guaranteed that character ""*"" occurs in s no more than once.The third line contains integer n (1 β€ n β€ 105) β the number of query strings.n lines follow, each of them contains single non-empty string consisting of lowercase English letters β a query string.It is guaranteed that the total length of all query strings is not greater than 105.
|
Print n lines: in the i-th of them print ""YES"" if the pattern matches the i-th query string, and ""NO"" otherwise.You can choose the case (lower or upper) for each letter arbitrary.
|
In the first example we can replace ""?"" with good letters ""a"" and ""b"", so we can see that the answer for the first query is ""YES"", and the answer for the second query is ""NO"", because we can't match the third letter.Explanation of the second example. The first query: ""NO"", because character ""*"" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string ""ba"", in which both letters are good. The second query: ""YES"", because characters ""?"" can be replaced with corresponding good letters, and character ""*"" can be replaced with empty string, and the strings will coincide. The third query: ""NO"", because characters ""?"" can't be replaced with bad letters. The fourth query: ""YES"", because characters ""?"" can be replaced with good letters ""a"", and character ""*"" can be replaced with a string of bad letters ""x"".
|
Input: aba?a2aaaaab | Output: YESNO
|
Medium
| 2 | 969 | 636 | 183 | 8 |
1,010 |
A
|
1010A
|
A. Fly
| 1,500 |
binary search; math
|
Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on \(n - 2\) intermediate planets. Formally: we number all the planets from \(1\) to \(n\). \(1\) is Earth, \(n\) is Mars. Natasha will make exactly \(n\) flights: \(1 \to 2 \to \ldots n \to 1\).Flight from \(x\) to \(y\) consists of two phases: take-off from planet \(x\) and landing to planet \(y\). This way, the overall itinerary of the trip will be: the \(1\)-st planet \(\to\) take-off from the \(1\)-st planet \(\to\) landing to the \(2\)-nd planet \(\to\) \(2\)-nd planet \(\to\) take-off from the \(2\)-nd planet \(\to\) \(\ldots\) \(\to\) landing to the \(n\)-th planet \(\to\) the \(n\)-th planet \(\to\) take-off from the \(n\)-th planet \(\to\) landing to the \(1\)-st planet \(\to\) the \(1\)-st planet.The mass of the rocket together with all the useful cargo (but without fuel) is \(m\) tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that \(1\) ton of fuel can lift off \(a_i\) tons of rocket from the \(i\)-th planet or to land \(b_i\) tons of rocket onto the \(i\)-th planet. For example, if the weight of rocket is \(9\) tons, weight of fuel is \(3\) tons and take-off coefficient is \(8\) (\(a_i = 8\)), then \(1.5\) tons of fuel will be burnt (since \(1.5 \cdot 8 = 9 + 3\)). The new weight of fuel after take-off will be \(1.5\) tons. Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well.Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly.
|
The first line contains a single integer \(n\) (\(2 \le n \le 1000\)) β number of planets.The second line contains the only integer \(m\) (\(1 \le m \le 1000\)) β weight of the payload.The third line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 1000\)), where \(a_i\) is the number of tons, which can be lifted off by one ton of fuel.The fourth line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 1000\)), where \(b_i\) is the number of tons, which can be landed by one ton of fuel. It is guaranteed, that if Natasha can make a flight, then it takes no more than \(10^9\) tons of fuel.
|
If Natasha can fly to Mars through \((n - 2)\) planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number \(-1\).It is guaranteed, that if Natasha can make a flight, then it takes no more than \(10^9\) tons of fuel.The answer will be considered correct if its absolute or relative error doesn't exceed \(10^{-6}\). Formally, let your answer be \(p\), and the jury's answer be \(q\). Your answer is considered correct if \(\frac{|p - q|}{\max{(1, |q|)}} \le 10^{-6}\).
|
Let's consider the first example.Initially, the mass of a rocket with fuel is \(22\) tons. At take-off from Earth one ton of fuel can lift off \(11\) tons of cargo, so to lift off \(22\) tons you need to burn \(2\) tons of fuel. Remaining weight of the rocket with fuel is \(20\) tons. During landing on Mars, one ton of fuel can land \(5\) tons of cargo, so for landing \(20\) tons you will need to burn \(4\) tons of fuel. There will be \(16\) tons of the rocket with fuel remaining. While taking off from Mars, one ton of fuel can raise \(8\) tons of cargo, so to lift off \(16\) tons you will need to burn \(2\) tons of fuel. There will be \(14\) tons of rocket with fuel after that. During landing on Earth, one ton of fuel can land \(7\) tons of cargo, so for landing \(14\) tons you will need to burn \(2\) tons of fuel. Remaining weight is \(12\) tons, that is, a rocket without any fuel.In the second case, the rocket will not be able even to take off from Earth.
|
Input: 21211 87 5 | Output: 10.0000000000
|
Medium
| 2 | 2,065 | 627 | 537 | 10 |
1,505 |
G
|
1505G
|
G. Encoded message
| 2,600 |
*special; implementation
|
The first line of the input contains a single integer \(N\) (\(1 \le N \le 24\)). The next \(N\) lines contain \(5\) space-separated integers each. The first three integers will be between 0 and 2, inclusive. The last two integers will be between 0 and 3, inclusive. The sum of the first three integers will be equal to the sum of the last two integers.
|
Output the result β a string of lowercase English letters.
|
Input: 1 1 0 0 1 0 | Output: a
|
Expert
| 2 | 0 | 353 | 58 | 15 |
||
1,360 |
E
|
1360E
|
E. Polygon
| 1,300 |
dp; graphs; implementation; shortest paths
|
Polygon is not only the best platform for developing problems but also a square matrix with side \(n\), initially filled with the character 0.On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly \(2n\) cannons were placed. Initial polygon for \(n=4\). Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding.More formally: if a cannon stands in the row \(i\), to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell (\(i, 1\)) and ends in some cell (\(i, j\)); if a cannon stands in the column \(j\), above the first row, and shoots with a 1, then the 1 starts its flight from the cell (\(1, j\)) and ends in some cell (\(i, j\)). For example, consider the following sequence of shots: 1. Shoot the cannon in the row \(2\). 2. Shoot the cannon in the row \(2\). 3. Shoot the cannon in column \(3\). You have a report from the military training on your desk. This report is a square matrix with side length \(n\) consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix?Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0.
|
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then \(t\) test cases follow.Each test case starts with a line containing an integer \(n\) (\(1 \le n \le 50\)) β the size of the polygon.This is followed by \(n\) lines of length \(n\), consisting of 0 and 1 β the polygon matrix after the training.The total area of the matrices in all test cases in one test does not exceed \(10^5\).
|
For each test case print: YES if there is a sequence of shots leading to a given matrix; NO if such a sequence does not exist. The letters in the words YES and NO can be printed in any case.
|
The first test case was explained in the statement.The answer to the second test case is NO, since a 1 in a cell (\(1, 1\)) flying out of any cannon would continue its flight further.
|
Input: 5 4 0010 0011 0000 0000 2 10 01 2 00 00 4 0101 1111 0101 0111 4 0100 1110 0101 0111 | Output: YES NO YES YES NO
|
Easy
| 4 | 1,661 | 427 | 190 | 13 |
999 |
B
|
999B
|
B. Reversing Encryption
| 900 |
implementation
|
A string \(s\) of length \(n\) can be encrypted by the following algorithm: iterate over all divisors of \(n\) in decreasing order (i.e. from \(n\) to \(1\)), for each divisor \(d\), reverse the substring \(s[1 \dots d]\) (i.e. the substring which starts at position \(1\) and ends at position \(d\)). For example, the above algorithm applied to the string \(s\)=""codeforces"" leads to the following changes: ""codeforces"" \(\to\) ""secrofedoc"" \(\to\) ""orcesfedoc"" \(\to\) ""rocesfedoc"" \(\to\) ""rocesfedoc"" (obviously, the last reverse operation doesn't change the string because \(d=1\)).You are given the encrypted string \(t\). Your task is to decrypt this string, i.e., to find a string \(s\) such that the above algorithm results in string \(t\). It can be proven that this string \(s\) always exists and is unique.
|
The first line of input consists of a single integer \(n\) (\(1 \le n \le 100\)) β the length of the string \(t\). The second line of input consists of the string \(t\). The length of \(t\) is \(n\), and it consists only of lowercase Latin letters.
|
Print a string \(s\) such that the above algorithm results in \(t\).
|
The first example is described in the problem statement.
|
Input: 10rocesfedoc | Output: codeforces
|
Beginner
| 1 | 830 | 248 | 68 | 9 |
1,986 |
C
|
1986C
|
C. Update Queries
| 1,100 |
data structures; greedy; sortings
|
Let's consider the following simple problem. You are given a string \(s\) of length \(n\), consisting of lowercase Latin letters, as well as an array of indices \(ind\) of length \(m\) (\(1 \leq ind_i \leq n\)) and a string \(c\) of length \(m\), consisting of lowercase Latin letters. Then, in order, you perform the update operations, namely, during the \(i\)-th operation, you set \(s_{ind_i} = c_i\). Note that you perform all \(m\) operations from the first to the last.Of course, if you change the order of indices in the array \(ind\) and/or the order of letters in the string \(c\), you can get different results. Find the lexicographically smallest string \(s\) that can be obtained after \(m\) update operations, if you can rearrange the indices in the array \(ind\) and the letters in the string \(c\) as you like.A string \(a\) is lexicographically less than a string \(b\) if and only if one of the following conditions is met: \(a\) is a prefix of \(b\), but \(a \neq b\); in the first position where \(a\) and \(b\) differ, the symbol in string \(a\) is earlier in the alphabet than the corresponding symbol in string \(b\).
|
Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of sets of input data. Then follows their description.The first line of each set of input data contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^5\)) β the length of the string \(s\) and the number of updates.The second line of each set of input data contains a string \(s\) of length \(n\), consisting of lowercase Latin letters.The third line of each set of input data contains \(m\) integers \(ind_1, ind_2, \ldots, ind_m\) (\(1 \leq ind_i \leq n\)) β the array of indices \(ind\).The fourth line of each set of input data contains a string \(c\) of length \(m\), consisting of lowercase Latin letters.It is guaranteed that the sum of \(n\) over all sets of input data does not exceed \(2 \cdot 10^5\). Similarly, the sum of \(m\) over all sets of input data does not exceed \(2 \cdot 10^5\).
|
For each set of input data, output the lexicographically smallest string \(s\) that can be obtained by rearranging the indices in the array \(ind\) and the letters in the string \(c\) as you like.
|
In the first set of input data, you can leave the array \(ind\) and the string \(c\) unchanged and simply perform all operations in that order.In the second set of input data, you can set the array \(ind = [1, 1, 4, 2]\) and \(c =\) ""zczw"". Then the string \(s\) will change as follows: \(meow \rightarrow zeow \rightarrow ceow \rightarrow ceoz \rightarrow cwoz\).In the third set of input data, you can leave the array \(ind\) unchanged and set \(c = \) ""admn"". Then the string \(s\) will change as follows: \(abacaba \rightarrow abacaba \rightarrow abdcaba \rightarrow abdcmba \rightarrow abdcmbn\).
|
Input: 41 2a1 1cb4 4meow1 2 1 4zcwz7 4abacaba1 3 5 7damn7 10traktor7 6 5 4 3 2 1 6 4 2codeforces | Output: b cwoz abdcmbn ccdeefo
|
Easy
| 3 | 1,139 | 941 | 196 | 19 |
2,023 |
E
|
2023E
|
E. Tree of Life
| 3,300 |
dp; greedy; trees
|
In the heart of an ancient kingdom grows the legendary Tree of Life β the only one of its kind and the source of magical power for the entire world. The tree consists of \(n\) nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are \(n-1\) channels in the tree, with the \(i\)-th channel connecting nodes \(v_i\) and \(u_i\). Moreover, there exists a unique simple path through the channels between any two nodes in the tree.However, the magical energy flowing through these channels must be balanced; otherwise, the power of the Tree of Life may disrupt the natural order and cause catastrophic consequences. The sages of the kingdom discovered that when two magical channels converge at a single node, a dangerous ""magical resonance vibration"" occurs between them. To protect the Tree of Life and maintain its balance, it is necessary to select several paths and perform special rituals along them. A path is a sequence of distinct nodes \(v_1, v_2, \ldots, v_k\), where each pair of adjacent nodes \(v_i\) and \(v_{i+1}\) is connected by a channel. When the sages perform a ritual along such a path, the resonance vibration between the channels \((v_i, v_{i+1})\) and \((v_{i+1}, v_{i+2})\) is blocked for each \(1 \leq i \leq k - 2\).The sages' task is to select the minimum number of paths and perform rituals along them to block all resonance vibrations. This means that for every pair of channels emanating from a single node, there must exist at least one selected path that contains both of these channels.Help the sages find the minimum number of such paths so that the magical balance of the Tree of Life is preserved, and its power continues to nourish the entire world!
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 4 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 5 \cdot 10^5\)) β the number of nodes in the Tree of Life.The \(i\)-th of the following \(n - 1\) lines of each test case contains two integers \(v_i\) and \(u_i\) (\(1 \leq v_i < u_i \leq n\)) β the channel connecting nodes \(v_i\) and \(u_i\).It is guaranteed that there exists a unique simple path through the channels between any two nodes.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of paths that the sages need to select to prevent a catastrophe.
|
In the first test case, there are two pairs of channels emanating from a single node: \((1, 2)\) and \((2, 3)\), \((2, 3)\) and \((3, 4)\). It is sufficient to perform the ritual along the path \(1-2-3-4\). Thus, the answer is \(1\).In the second test case, there are no pairs of channels emanating from a single node, so the answer is \(0\).In the third test case, rituals can be performed along the paths \(2-1-3\), \(2-1-4\), and \(3-1-4\).
|
Input: 541 22 33 421 241 21 31 483 72 41 22 53 61 33 862 31 23 61 51 4 | Output: 1 0 3 7 3
|
Master
| 3 | 1,765 | 711 | 129 | 20 |
750 |
F
|
750F
|
F. New Year and Finding Roots
| 2,800 |
constructive algorithms; implementation; interactive; trees
|
This is an interactive problem. In the interaction section below you will find the information about flushing the output.The New Year tree of height h is a perfect binary tree with vertices numbered 1 through 2h - 1 in some order. In this problem we assume that h is at least 2. The drawing below shows one example New Year tree of height 3: Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that h β₯ 2). It won't be easy because Limak is a little bear and he doesn't even see the whole tree. Can you help him?There are t testcases. In each testcase, you should first read h from the input. Then you can ask at most 16 questions of format ""? x"" (without quotes), where x is an integer between 1 and 2h - 1, inclusive. As a reply you will get the list of neighbours of vertex x (more details in the ""Interaction"" section below). For example, for a tree on the drawing above after asking ""? 1"" you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root y and print it in the format ""! y"". You will be able to read h for a next testcase only after printing the answer in a previous testcase and flushing the output.Each tree is fixed from the beginning and it doesn't change during your questions.
|
The first line of the input contains a single integer t (1 β€ t β€ 500) β the number of testcases.At the beginning of each testcase you should read from the input a single integer h (2 β€ h β€ 7) β the height of the tree. You can't read the value of h in a next testcase until you answer a previous testcase.
|
In the first sample, a tree corresponds to the drawing from the statement.In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below.
|
Input: 1334 5 721 212 | Output: ? 1? 5? 6! 5
|
Master
| 4 | 1,368 | 304 | 0 | 7 |
|
1,735 |
E
|
1735E
|
E. House Planning
| 2,400 |
constructive algorithms; data structures; graph matchings; greedy
|
There are \(n\) houses in your city arranged on an axis at points \(h_1, h_2, \ldots, h_n\). You want to build a new house for yourself and consider two options where to place it: points \(p_1\) and \(p_2\).As you like visiting friends, you have calculated in advance the distances from both options to all existing houses. More formally, you have calculated two arrays \(d_1\), \(d_2\): \(d_{i, j} = \left|p_i - h_j\right|\), where \(|x|\) defines the absolute value of \(x\).After a long time of inactivity you have forgotten the locations of the houses \(h\) and the options \(p_1\), \(p_2\). But your diary still keeps two arrays β \(d_1\), \(d_2\), whose authenticity you doubt. Also, the values inside each array could be shuffled, so values at the same positions of \(d_1\) and \(d_2\) may correspond to different houses. Pay attention, that values from one array could not get to another, in other words, all values in the array \(d_1\) correspond the distances from \(p_1\) to the houses, and in the array \(d_2\) β from \(p_2\) to the houses.Also pay attention, that the locations of the houses \(h_i\) and the considered options \(p_j\) could match. For example, the next locations are correct: \(h = \{1, 0, 3, 3\}\), \(p = \{1, 1\}\), that could correspond to already shuffled \(d_1 = \{0, 2, 1, 2\}\), \(d_2 = \{2, 2, 1, 0\}\).Check whether there are locations of houses \(h\) and considered points \(p_1\), \(p_2\), for which the founded arrays of distances would be correct. If it is possible, find appropriate locations of houses and considered options.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases. The description of test cases follows.The first line of each test case contains one integer \(n\) (\(1 \le n \le 10^3\)) β the length of arrays \(d_1\), \(d_2\).The next two lines contain \(n\) integers each: arrays \(d_1\) and \(d_2\) (\(0 \le d_{i, j} \le 10^9\)) respectively.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^3\).
|
For each test case, output a single line ""NO"" if there is no answer.Otherwise output three lines. The first line must contain ""YES"". In the second line, print \(n\) integers \(h_1, h_2, \ldots, h_n\). In the third line print two integers \(p_1\), \(p_2\).It must be satisfied that \(0 \le h_i, p_1, p_2 \le 2 \cdot 10^9\). We can show that if there is an answer, then there is one satisfying these constraints.If there are several answers, output any of them.
|
In the image below you can see the sample solutions. Planned houses are shown in bright colours: pink and purple. Existing houses are dim. In test case \(1\), the first planned house is located at point \(0\), the second at point \(10\). The existing house is located at point \(5\) and is at a distance of \(5\) from both planned houses. It can be shown that there is no answer for test case \(2\). In test case \(3\), the planned houses are located at points \(33\) and \(69\). Note that in test case \(4\), both plans are located at point \(1\), where one of the existing houses is located at the same time. It is a correct placement.
|
Input: 4155210 125 20210 3326 6940 2 1 22 2 1 0 | Output: YES 5 0 10 NO YES 0 43 33 69 YES 1 0 3 3 1 1
|
Expert
| 4 | 1,570 | 481 | 463 | 17 |
1,453 |
A
|
1453A
|
A. Cancel the Trains
| 800 |
implementation
|
Gildong's town has a train system that has \(100\) trains that travel from the bottom end to the top end and \(100\) trains that travel from the left end to the right end. The trains starting from each side are numbered from \(1\) to \(100\), respectively, and all trains have the same speed. Let's take a look at the picture below. The train system can be represented as coordinates on a 2D plane. The \(i\)-th train starting at the bottom end is initially at \((i,0)\) and will be at \((i,T)\) after \(T\) minutes, and the \(i\)-th train starting at the left end is initially at \((0,i)\) and will be at \((T,i)\) after \(T\) minutes. All trains arrive at their destinations after \(101\) minutes.However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, \(n\) trains are scheduled to depart from the bottom end and \(m\) trains are scheduled to depart from the left end. If two trains are both at \((x,y)\) at the same time for some \(x\) and \(y\), they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes.
|
Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)).Each test case contains three lines. The first line of each test case consists of two integers \(n\) and \(m\) (\(1 \le n, m \le 100\)) β the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively.The second line of each test case contains \(n\) integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between \(1\) and \(100\), inclusive.The third line of each test case contains \(m\) integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between \(1\) and \(100\), inclusive.
|
For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes.
|
In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero.In the second case, at \(T=4\), there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one.
|
Input: 3 1 2 1 3 4 3 2 1 3 4 2 4 9 14 2 7 16 28 33 57 59 86 99 3 9 14 19 25 26 28 35 41 59 85 87 99 100 | Output: 0 1 3
|
Beginner
| 1 | 1,176 | 864 | 129 | 14 |
77 |
B
|
77B
|
B. Falling Anvils
| 1,800 |
math; probabilities
|
For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all.Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who throws anvils from the sky? From what height? We are sure that such questions have never troubled you!It turns out that throwing an anvil properly is not an easy task at all. Let's describe one of the most popular anvil throwing models.Let the height p of the potential victim vary in the range [0;a] and the direction of the wind q vary in the range [ - b;b]. p and q could be any real (floating) numbers. Then we can assume that the anvil will fit the toon's head perfectly only if the following equation has at least one real root: Determine the probability with which an aim can be successfully hit by an anvil.You can assume that the p and q coefficients are chosen equiprobably and independently in their ranges.
|
The first line contains integer t (1 β€ t β€ 10000) β amount of testcases.Each of the following t lines contain two space-separated integers a and b (0 β€ a, b β€ 106).Pretests contain all the tests with 0 < a < 10, 0 β€ b < 10.
|
Print t lines β the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10 - 6.
|
Input: 24 21 2 | Output: 0.62500000000.5312500000
|
Medium
| 2 | 1,036 | 223 | 147 | 0 |
|
1,766 |
C
|
1766C
|
C. Hamiltonian Wall
| 1,300 |
dp; implementation
|
Sir Monocarp Hamilton is planning to paint his wall. The wall can be represented as a grid, consisting of \(2\) rows and \(m\) columns. Initially, the wall is completely white.Monocarp wants to paint a black picture on the wall. In particular, he wants cell \((i, j)\) (the \(j\)-th cell in the \(i\)-th row) to be colored black, if \(c_{i, j} =\) 'B', and to be left white, if \(c_{i, j} =\) 'W'. Additionally, he wants each column to have at least one black cell, so, for each \(j\), the following constraint is satisfied: \(c_{1, j}\), \(c_{2, j}\) or both of them will be equal to 'B'.In order for the picture to turn out smooth, Monocarp wants to place down a paint brush in some cell \((x_1, y_1)\) and move it along the path \((x_1, y_1), (x_2, y_2), \dots, (x_k, y_k)\) so that: for each \(i\), \((x_i, y_i)\) and \((x_{i+1}, y_{i+1})\) share a common side; all black cells appear in the path exactly once; white cells don't appear in the path. Determine if Monocarp can paint the wall.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(m\) (\(1 \le m \le 2 \cdot 10^5\)) β the number of columns in the wall.The \(i\)-th of the next two lines contains a string \(c_i\), consisting of \(m\) characters, where each character is either 'B' or 'W'. \(c_{i, j}\) is 'B', if the cell \((i, j)\) should be colored black, and 'W', if the cell \((i, j)\) should be left white.Additionally, for each \(j\), the following constraint is satisfied: \(c_{1, j}\), \(c_{2, j}\) or both of them are equal to 'B'.The sum of \(m\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print ""YES"" if Monocarp can paint a wall. Otherwise, print ""NO"".
|
In the first testcase, Monocarp can follow a path \((2, 1)\), \((2, 2)\), \((1, 2)\), \((1, 3)\) with his brush. All black cells appear in the path exactly once, no white cells appear in the path.In the second testcase, Monocarp can follow a path \((1, 1)\), \((2, 1)\).In the third testcase: the path \((1, 1)\), \((2, 1)\), \((2, 2)\), \((2, 3)\), \((1, 3)\), \((2, 4)\), \((2, 5)\), \((1, 5)\) doesn't suffice because a pair of cells \((1, 3)\) and \((2, 4)\) doesn't share a common side; the path \((1, 1)\), \((2, 1)\), \((2, 2)\), \((2, 3)\), \((1, 3)\), \((2, 3)\), \((2, 4)\), \((2, 5)\), \((1, 5)\) doesn't suffice because cell \((2, 3)\) is visited twice; the path \((1, 1)\), \((2, 1)\), \((2, 2)\), \((2, 3)\), \((2, 4)\), \((2, 5)\), \((1, 5)\) doesn't suffice because a black cell \((1, 3)\) doesn't appear in the path; the path \((1, 1)\), \((2, 1)\), \((2, 2)\), \((2, 3)\), \((2, 4)\), \((2, 5)\), \((1, 5)\), \((1, 4)\), \((1, 3)\) doesn't suffice because a white cell \((1, 4)\) appears in the path.
|
Input: 63WBBBBW1BB5BWBWBBBBBB2BWWB5BBBBWBWBBB6BWBBWBBBBBBB | Output: YES YES NO NO NO YES
|
Easy
| 2 | 994 | 683 | 87 | 17 |
750 |
A
|
750A
|
A. New Year and Hurry
| 800 |
binary search; brute force; implementation; math
|
Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will be n problems, sorted by difficulty, i.e. problem 1 is the easiest and problem n is the hardest. Limak knows it will take him 5Β·i minutes to solve the i-th problem.Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first.How many problems can Limak solve if he wants to make it to the party?
|
The only line of the input contains two integers n and k (1 β€ n β€ 10, 1 β€ k β€ 240) β the number of the problems in the contest and the number of minutes Limak needs to get to the party from his house.
|
Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier.
|
In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes respectively. Limak can spend 5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve 2 problems. He doesn't have enough time to solve 3 problems so the answer is 2.In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight.In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems.
|
Input: 3 222 | Output: 2
|
Beginner
| 4 | 594 | 200 | 141 | 7 |
1,116 |
D5
|
1116D5
|
D5. Creeper
| 0 |
*special
|
Implement a unitary operation on 3 qubits which is represented by a square matrix of size 8 with the following pattern:XX....XXXX....XX...XX......XX.....X..X....X..X..XX....XXXX....XXHere X denotes a ""non-zero"" element of the matrix (a complex number which has the square of the absolute value greater than or equal to \(10^{-5}\)), and . denotes a ""zero"" element of the matrix (a complex number which has the square of the absolute value less than \(10^{-5}\)).The row and column indices of the matrix follow little endian format: the least significant bit of the index is stored first in the qubit array. Thus, the first column of the matrix gives you the coefficients of the basis states you'll get if you apply the unitary to the \(|00..0\rangle\) basis state, the second column - to the \(|10..0\rangle\) basis state etc. You can use the DumpUnitary tool to get the coefficients of the matrix your unitary implements (up to relative phases between columns) and the corresponding pattern of Xs and .s.You have to implement an operation which takes an array of 3 qubits as an input and applies the unitary transformation with the matrix of the described shape to it. If there are multiple unitaries which satisfy the requirements, you can implement any of them. The ""output"" of your operation is the pattern of the matrix coefficients implemented by it; you can see the testing harness in the UnitaryPatterns kata.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[]) : Unit { // your code here }}You are not allowed to use measurements in your operation.
|
Beginner
| 1 | 1,672 | 0 | 0 | 11 |
||||
820 |
A
|
820A
|
A. Mister B and Book Reading
| 900 |
implementation
|
Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages.At first day Mister B read v0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day he read v0 pages, at second β v0 + a pages, at third β v0 + 2a pages, and so on). But Mister B is just a human, so he physically wasn't able to read more than v1 pages per day.Also, to refresh his memory, every day, starting from the second, Mister B had to reread last l pages he read on the previous day. Mister B finished the book when he read the last page for the first time.Help Mister B to calculate how many days he needed to finish the book.
|
First and only line contains five space-separated integers: c, v0, v1, a and l (1 β€ c β€ 1000, 0 β€ l < v0 β€ v1 β€ 1000, 0 β€ a β€ 1000) β the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading.
|
Print one integer β the number of days Mister B needed to finish the book.
|
In the first sample test the book contains 5 pages, so Mister B read it right at the first day.In the second sample test at first day Mister B read pages number 1 - 4, at second day β 4 - 11, at third day β 11 - 12 and finished the book.In third sample test every day Mister B read 1 page of the book, so he finished in 15 days.
|
Input: 5 5 10 5 4 | Output: 1
|
Beginner
| 1 | 739 | 293 | 74 | 8 |
1,290 |
A
|
1290A
|
A. Mind Control
| 1,600 |
brute force; data structures; implementation
|
You and your \(n - 1\) friends have found an array of integers \(a_1, a_2, \dots, a_n\). You have decided to share it in the following way: All \(n\) of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.You are standing in the \(m\)-th position in the line. Before the process starts, you may choose up to \(k\) different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.Suppose that you're doing your choices optimally. What is the greatest integer \(x\) such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to \(x\)?Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains three space-separated integers \(n\), \(m\) and \(k\) (\(1 \le m \le n \le 3500\), \(0 \le k \le n - 1\)) β the number of elements in the array, your position in line and the number of people whose choices you can fix.The second line of each test case contains \(n\) positive integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β elements of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3500\).
|
For each test case, print the largest integer \(x\) such that you can guarantee to obtain at least \(x\).
|
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element (\(5\)) because he or she was forced by you to take the last element. After this turn the remaining array will be \([2, 9, 2, 3, 8]\); the second person will take the first element (\(2\)) because he or she was forced by you to take the first element. After this turn the remaining array will be \([9, 2, 3, 8]\); if the third person will choose to take the first element (\(9\)), at your turn the remaining array will be \([2, 3, 8]\) and you will take \(8\) (the last element); if the third person will choose to take the last element (\(8\)), at your turn the remaining array will be \([9, 2, 3]\) and you will take \(9\) (the first element). Thus, this strategy guarantees to end up with at least \(8\). We can prove that there is no strategy that guarantees to end up with at least \(9\). Hence, the answer is \(8\).In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with \(4\).
|
Input: 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 | Output: 8 4 1 1
|
Medium
| 3 | 1,300 | 668 | 105 | 12 |
1,789 |
E
|
1789E
|
E. Serval and Music Game
| 2,500 |
brute force; dp; implementation; math; number theory
|
Serval loves playing music games. He meets a problem when playing music games, and he leaves it for you to solve.You are given \(n\) positive integers \(s_1 < s_2 < \ldots < s_n\). \(f(x)\) is defined as the number of \(i\) (\(1\leq i\leq n\)) that exist non-negative integers \(p_i, q_i\) such that: $$$\(s_i=p_i\left\lfloor{s_n\over x}\right\rfloor + q_i\left\lceil{s_n\over x}\right\rceil\)\(Find out \)\sum_{x=1}^{s_n} x\cdot f(x)\( modulo \)998\,244\,353\(.As a reminder, \)\lfloor x\rfloor\( denotes the maximal integer that is no greater than \)x\(, and \)\lceil x\rceil\( denotes the minimal integer that is no less than \)x$$$.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 10^4\)). The description of the test cases follows.The first line of each test cases contains a single integer \(n\) (\(1\leq n\leq 10^6\)).The second line of each test case contains \(n\) positive integers \(s_1,s_2,\ldots,s_n\) (\(1\leq s_1 < s_2 < \ldots < s_n \leq 10^7\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\), and the sum of \(s_n\) does not exceed \(10^7\).
|
For each test case, print a single integer in a single line β the sum of \(x\cdot f(x)\) over all possible \(x\) modulo \(998\,244\,353\).
|
For the first test case, \(s_n=4\), \(f(x)\) are calculated as followings: \(f(1)=1\) \(\left\lfloor s_n\over 1\right\rfloor=4\), \(\left\lceil s_n\over 1\right\rceil=4\). It can be shown that such \(p_1,p_2\) and \(q_1,q_2\) that satisfy the conditions don't exist. Let \(p_3=1\) and \(q_3=0\), then \(s_3 = p_3\cdot\left\lfloor s_n\over 1\right\rfloor + q_3\cdot\left\lceil s_n\over 1\right\rceil = 1\cdot 4 + 0\cdot 4 = 4\). \(f(2)=2\) \(\left\lfloor s_n\over 2\right\rfloor=2\), \(\left\lceil s_n\over 2\right\rceil=2\). It can be shown that such \(p_1\) and \(q_1\) that satisfy the conditions don't exist. Let \(p_2=1\) and \(q_2=0\), then \(s_2 = p_2\cdot\left\lfloor s_n\over 2\right\rfloor + q_2\cdot\left\lceil s_n\over 2\right\rceil = 1\cdot 2 + 0\cdot 2 = 2\). Let \(p_3=0\) and \(q_3=2\), then \(s_3 = p_3\cdot\left\lfloor s_n\over 2\right\rfloor + q_3\cdot\left\lceil s_n\over 2\right\rceil = 0\cdot 2 + 2\cdot 2 = 4\). \(f(3)=3\) \(\left\lfloor s_n\over 3\right\rfloor=1\), \(\left\lceil s_n\over 3\right\rceil=2\). Let \(p_1=1\) and \(q_1=0\), then \(s_1 = p_1\cdot\left\lfloor s_n\over 3\right\rfloor + q_1\cdot\left\lceil s_n\over 3\right\rceil = 1\cdot 1 + 0\cdot 2 = 1\). Let \(p_2=0\) and \(q_2=1\), then \(s_2 = p_2\cdot\left\lfloor s_n\over 3\right\rfloor + q_2\cdot\left\lceil s_n\over 3\right\rceil = 0\cdot 1 + 1\cdot 2 = 2\). Let \(p_3=0\) and \(q_3=2\), then \(s_3 = p_3\cdot\left\lfloor s_n\over 3\right\rfloor + q_3\cdot\left\lceil s_n\over 3\right\rceil = 0\cdot 1 + 2\cdot 2 = 4\). \(f(4)=3\) \(\left\lfloor s_n\over 4\right\rfloor=1\), \(\left\lceil s_n\over 4\right\rceil=1\). Let \(p_1=1\) and \(q_1=0\), then \(s_1 = p_1\cdot\left\lfloor s_n\over 4\right\rfloor + q_1\cdot\left\lceil s_n\over 4\right\rceil = 1\cdot 1 + 0\cdot 1 = 1\). Let \(p_2=1\) and \(q_2=1\), then \(s_2 = p_2\cdot\left\lfloor s_n\over 4\right\rfloor + q_2\cdot\left\lceil s_n\over 4\right\rceil = 1\cdot 1 + 1\cdot 1 = 2\). Let \(p_3=2\) and \(q_3=2\), then \(s_3 = p_3\cdot\left\lfloor s_n\over 4\right\rfloor + q_3\cdot\left\lceil s_n\over 4\right\rceil = 2\cdot 1 + 2\cdot 1 = 4\). Therefore, the answer is \(\sum_{x=1}^4 x\cdot f(x) = 1\cdot 1 + 2\cdot 2 + 3\cdot 3 + 4\cdot 3 = 26\).For the second test case: \(f(1)=f(2)=f(3)=1\) \(f(4)=3\) \(f(5)=f(6)=f(7)=f(8)=f(9)=4\) For example, when \(x=3\) we have \(f(3)=1\) because there exist \(p_4\) and \(q_4\):$$$\(9 = 1 \cdot\left\lfloor{9\over 3}\right\rfloor + 2 \cdot\left\lceil{9\over 3}\right\rceil\)\(It can be shown that it is impossible to find \)p_1,p_2,p_3\( and \)q_1,q_2,q_3\( that satisfy the conditions.When \)x=5\( we have \)f(5)=4\( because there exist \)p_i\( and \)q_i\( as followings:\)\(1 = 1 \cdot\left\lfloor{9\over 5}\right\rfloor + 0 \cdot\left\lceil{9\over 5}\right\rceil\)\( \)\(2 = 0 \cdot\left\lfloor{9\over 5}\right\rfloor + 1 \cdot\left\lceil{9\over 5}\right\rceil\)\( \)\(7 = 3 \cdot\left\lfloor{9\over 5}\right\rfloor + 2 \cdot\left\lceil{9\over 5}\right\rceil\)\( \)\(9 = 3 \cdot\left\lfloor{9\over 5}\right\rfloor + 3 \cdot\left\lceil{9\over 5}\right\rceil\)\(Therefore, the answer is \)\sum_{x=1}^9 x\cdot f(x) = 158$$$.
|
Input: 431 2 441 2 7 94344208 591000 4779956 540342951633 1661 1741 2134 2221 | Output: 26 158 758737625 12334970
|
Expert
| 5 | 636 | 520 | 138 | 17 |
1,409 |
E
|
1409E
|
E. Two Platforms
| 1,800 |
binary search; dp; sortings; two pointers
|
There are \(n\) points on a plane. The \(i\)-th point has coordinates \((x_i, y_i)\). You have two horizontal platforms, both of length \(k\). Each platform can be placed anywhere on a plane but it should be placed horizontally (on the same \(y\)-coordinate) and have integer borders. If the left border of the platform is \((x, y)\) then the right border is \((x + k, y)\) and all points between borders (including borders) belong to the platform.Note that platforms can share common points (overlap) and it is not necessary to place both platforms on the same \(y\)-coordinate.When you place both platforms on a plane, all points start falling down decreasing their \(y\)-coordinate. If a point collides with some platform at some moment, the point stops and is saved. Points which never collide with any platform are lost.Your task is to find the maximum number of points you can save if you place both platforms optimally.You have to answer \(t\) independent test cases.For better understanding, please read the Note section below to see a picture for the first test case.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le k \le 10^9\)) β the number of points and the length of each platform, respectively. The second line of the test case contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(1 \le x_i \le 10^9\)), where \(x_i\) is \(x\)-coordinate of the \(i\)-th point. The third line of the input contains \(n\) integers \(y_1, y_2, \dots, y_n\) (\(1 \le y_i \le 10^9\)), where \(y_i\) is \(y\)-coordinate of the \(i\)-th point. All points are distinct (there is no pair \(1 \le i < j \le n\) such that \(x_i = x_j\) and \(y_i = y_j\)).It is guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
For each test case, print the answer: the maximum number of points you can save if you place both platforms optimally.
|
The picture corresponding to the first test case of the example:Blue dots represent the points, red segments represent the platforms. One of the possible ways is to place the first platform between points \((1, -1)\) and \((2, -1)\) and the second one between points \((4, 3)\) and \((5, 3)\). Vectors represent how the points will fall down. As you can see, the only point we can't save is the point \((3, 7)\) so it falls down infinitely and will be lost. It can be proven that we can't achieve better answer here. Also note that the point \((5, 3)\) doesn't fall at all because it is already on the platform.
|
Input: 4 7 1 1 5 2 3 1 5 4 1 3 6 7 2 5 4 1 1 1000000000 1000000000 5 10 10 7 5 15 8 20 199 192 219 1904 10 10 15 19 8 17 20 10 9 2 10 19 12 13 6 17 1 14 7 9 19 3 | Output: 6 1 5 10
|
Medium
| 4 | 1,076 | 870 | 118 | 14 |
2,091 |
D
|
2091D
|
D. Place of the Olympiad
| 1,200 |
binary search; greedy; math
|
For the final of the first Olympiad by IT Campus ""NEIMARK"", a rectangular venue was prepared. You may assume that the venue is divided into \(n\) rows, each containing \(m\) spots for participants' desks. A total of \(k\) participants have registered for the final, and each participant will sit at an individual desk. Now, the organizing committee must choose the locations for the desks in the venue.Each desk occupies one of the \(m\) spots in a row. Moreover, if several desks occupy consecutive spots in the same row, we call such a group of desks a bench, and the number of desks in the group is the bench's length. For example, seating \(7\) participants on a \(3 \times 4\) venue (with \(n = 3, m = 4\)) can be arranged as follows: In the figure above, the first row has one bench of length \(3\), the second row has one bench of length \(2\), and the third row has two benches of length \(1\).The organizing committee wants to choose the locations so that the length of the longest bench is as small as possible. In particular, the same \(7\) desks can be arranged in a more optimal way, so that the lengths of all benches do not exceed \(2\): Given the integers \(n\), \(m\), and \(k\), determine the minimum possible length of the longest bench.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). The description of the test cases follows.A single line of each test case contains three positive integers β \(n\), \(m\), \(k\) (\(1 \leq n, m, k \leq 10^9\), \(k \leq n\cdot m\)).
|
For each test case, output a single number β the minimum possible length of the longest bench.
|
Input: 53 4 75 5 51 13 22 4 71 5 4 | Output: 2 1 1 4 2
|
Easy
| 3 | 1,258 | 302 | 94 | 20 |
|
2,009 |
A
|
2009A
|
A. Minimize!
| 800 |
brute force; math
|
You are given two integers \(a\) and \(b\) (\(a \leq b\)). Over all possible integer values of \(c\) (\(a \leq c \leq b\)), find the minimum value of \((c - a) + (b - c)\).
|
The first line contains \(t\) (\(1 \leq t \leq 55\)) β the number of test cases. Each test case contains two integers \(a\) and \(b\) (\(1 \leq a \leq b \leq 10\)).
|
For each test case, output the minimum possible value of \((c - a) + (b - c)\) on a new line.
|
In the first test case, you can choose \(c = 1\) and obtain an answer of \((1 - 1) + (2 - 1) = 1\). It can be shown this is the minimum value possible.In the second test case, you can choose \(c = 6\) and obtain an answer of \((6 - 3) + (10 - 6) = 7\). It can be shown this is the minimum value possible.
|
Input: 31 23 105 5 | Output: 1 7 0
|
Beginner
| 2 | 172 | 164 | 93 | 20 |
1,294 |
F
|
1294F
|
F. Three Paths on a Tree
| 2,000 |
dfs and similar; dp; greedy; trees
|
You are given an unweighted tree with \(n\) vertices. Recall that a tree is a connected undirected graph without cycles.Your task is to choose three distinct vertices \(a, b, c\) on this tree such that the number of edges which belong to at least one of the simple paths between \(a\) and \(b\), \(b\) and \(c\), or \(a\) and \(c\) is the maximum possible. See the notes section for a better understanding.The simple path is the path that visits each vertex at most once.
|
The first line contains one integer number \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree. Next \(n - 1\) lines describe the edges of the tree in form \(a_i, b_i\) (\(1 \le a_i\), \(b_i \le n\), \(a_i \ne b_i\)). It is guaranteed that given graph is a tree.
|
In the first line print one integer \(res\) β the maximum number of edges which belong to at least one of the simple paths between \(a\) and \(b\), \(b\) and \(c\), or \(a\) and \(c\).In the second line print three integers \(a, b, c\) such that \(1 \le a, b, c \le n\) and \(a \ne, b \ne c, a \ne c\).If there are several answers, you can print any.
|
The picture corresponding to the first example (and another one correct answer):If you choose vertices \(1, 5, 6\) then the path between \(1\) and \(5\) consists of edges \((1, 2), (2, 3), (3, 4), (4, 5)\), the path between \(1\) and \(6\) consists of edges \((1, 2), (2, 3), (3, 4), (4, 6)\) and the path between \(5\) and \(6\) consists of edges \((4, 5), (4, 6)\). The union of these paths is \((1, 2), (2, 3), (3, 4), (4, 5), (4, 6)\) so the answer is \(5\). It can be shown that there is no better answer.
|
Input: 8 1 2 2 3 3 4 4 5 4 6 3 7 3 8 | Output: 5 1 8 6
|
Hard
| 4 | 471 | 284 | 350 | 12 |
1,038 |
B
|
1038B
|
B. Non-Coprime Partition
| 1,100 |
constructive algorithms; math
|
Find out if it is possible to partition the first \(n\) positive integers into two non-empty disjoint sets \(S_1\) and \(S_2\) such that:\(\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1\) Here \(\mathrm{sum}(S)\) denotes the sum of all elements present in set \(S\) and \(\mathrm{gcd}\) means thegreatest common divisor.Every integer number from \(1\) to \(n\) should be present in exactly one of \(S_1\) or \(S_2\).
|
The only line of the input contains a single integer \(n\) (\(1 \le n \le 45\,000\))
|
If such partition doesn't exist, print ""No"" (quotes for clarity).Otherwise, print ""Yes"" (quotes for clarity), followed by two lines, describing \(S_1\) and \(S_2\) respectively.Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty.If there are multiple possible partitions β print any of them.
|
In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is ""No"".In the second example, the sums of the sets are \(2\) and \(4\) respectively. The \(\mathrm{gcd}(2, 4) = 2 > 1\), hence that is one of the possible answers.
|
Input: 1 | Output: No
|
Easy
| 2 | 424 | 84 | 367 | 10 |
1,389 |
B
|
1389B
|
B. Array Walk
| 1,600 |
brute force; dp; greedy
|
You are given an array \(a_1, a_2, \dots, a_n\), consisting of \(n\) positive integers. Initially you are standing at index \(1\) and have a score equal to \(a_1\). You can perform two kinds of moves: move right β go from your current index \(x\) to \(x+1\) and add \(a_{x+1}\) to your score. This move can only be performed if \(x<n\). move left β go from your current index \(x\) to \(x-1\) and add \(a_{x-1}\) to your score. This move can only be performed if \(x>1\). Also, you can't perform two or more moves to the left in a row. You want to perform exactly \(k\) moves. Also, there should be no more than \(z\) moves to the left among them.What is the maximum score you can achieve?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains three integers \(n, k\) and \(z\) (\(2 \le n \le 10^5\), \(1 \le k \le n - 1\), \(0 \le z \le min(5, k)\)) β the number of elements in the array, the total number of moves you should perform and the maximum number of moves to the left you can perform.The second line of each testcase contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^4\)) β the given array.The sum of \(n\) over all testcases does not exceed \(3 \cdot 10^5\).
|
Print \(t\) integers β for each testcase output the maximum score you can achieve if you make exactly \(k\) moves in total, no more than \(z\) of them are to the left and there are no two or more moves to the left in a row.
|
In the first testcase you are not allowed to move left at all. So you make four moves to the right and obtain the score \(a_1 + a_2 + a_3 + a_4 + a_5\).In the second example you can move one time to the left. So we can follow these moves: right, right, left, right. The score will be \(a_1 + a_2 + a_3 + a_2 + a_3\).In the third example you can move four times to the left but it's not optimal anyway, you can just move four times to the right and obtain the score \(a_1 + a_2 + a_3 + a_4 + a_5\).
|
Input: 4 5 4 0 1 5 4 3 2 5 4 1 1 5 4 3 2 5 4 4 10 20 30 40 50 10 7 3 4 6 8 2 9 9 7 4 10 9 | Output: 15 19 150 56
|
Medium
| 3 | 689 | 582 | 223 | 13 |
1,183 |
G
|
1183G
|
G. Candy Box (hard version)
| 2,000 |
greedy; implementation; sortings
|
This problem is a version of problem D from the same contest with some additional constraints and tasks.There are \(n\) candies in a candy box. The type of the \(i\)-th candy is \(a_i\) (\(1 \le a_i \le n\)). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i. e. for example, a gift having two candies of type \(1\) and two candies of type \(2\) is bad).It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift.You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number \(f_i\) is given, which is equal to \(0\) if you really want to keep \(i\)-th candy for yourself, or \(1\) if you don't mind including it into your gift. It is possible that two candies of the same type have different values of \(f_i\).You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having \(f_i = 1\) in your gift.You have to answer \(q\) independent queries.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of queries.The first line of each query contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of candies.Then \(n\) lines follow, each containing two integers \(a_i\) and \(f_i\) (\(1 \le a_i \le n\), \(0 \le f_i \le 1\)), where \(a_i\) is the type of the \(i\)-th candy, and \(f_i\) denotes whether you want to keep the \(i\)-th candy for yourself (\(0\) if you want to keep it, \(1\) if you don't mind giving it away).It is guaranteed that the sum of \(n\) over all queries does not exceed \(2 \cdot 10^5\).
|
For each query print two integers: the maximum number of candies in a gift you can compose, according to the constraints in the statement; the maximum number of candies having \(f_i = 1\) in a gift you can compose that contains the maximum possible number of candies.
|
In the first query, you can include two candies of type \(4\) and one candy of type \(5\). All of them have \(f_i = 1\) and you don't mind giving them away as part of the gift.
|
Input: 3 8 1 0 4 1 2 0 4 1 5 1 6 1 3 0 2 0 4 1 1 1 1 2 1 2 1 9 2 0 2 0 4 1 4 1 4 1 7 0 7 1 7 0 7 1 | Output: 3 3 3 3 9 5
|
Hard
| 3 | 1,522 | 625 | 267 | 11 |
161 |
B
|
161B
|
B. Discounts
| 1,700 |
constructive algorithms; greedy; sortings
|
One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheaper). If there are several items with the same minimum price, the discount is available for only one of them!Polycarpus has k carts, and he wants to buy up all stools and pencils from the supermarket. Help him distribute the stools and the pencils among the shopping carts, so that the items' total price (including the discounts) is the least possible.Polycarpus must use all k carts to purchase the items, no shopping cart can remain empty. Each shopping cart can contain an arbitrary number of stools and/or pencils.
|
The first input line contains two integers n and k (1 β€ k β€ n β€ 103) β the number of items in the supermarket and the number of carts, correspondingly. Next n lines describe the items as ""ci ti"" (without the quotes), where ci (1 β€ ci β€ 109) is an integer denoting the price of the i-th item, ti (1 β€ ti β€ 2) is an integer representing the type of item i (1 for a stool and 2 for a pencil). The numbers in the lines are separated by single spaces.
|
In the first line print a single real number with exactly one decimal place β the minimum total price of the items, including the discounts.In the following k lines print the descriptions of the items in the carts. In the i-th line print the description of the i-th cart as ""t b1 b2 ... bt"" (without the quotes), where t is the number of items in the i-th cart, and the sequence b1, b2, ..., bt (1 β€ bj β€ n) gives the indices of items to put in this cart in the optimal distribution. All indices of items in all carts should be pairwise different, each item must belong to exactly one cart. You can print the items in carts and the carts themselves in any order. The items are numbered from 1 to n in the order in which they are specified in the input.If there are multiple optimal distributions, you are allowed to print any of them.
|
In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2Β·0.5 + (3 + 3Β·0.5) = 1 + 4.5 = 5.5.
|
Input: 3 22 13 23 1 | Output: 5.52 1 21 3
|
Medium
| 3 | 837 | 448 | 836 | 1 |
2,117 |
B
|
2117B
|
B. Shrink
| 800 |
constructive algorithms
|
A shrink operation on an array \(a\) of size \(m\) is defined as follows: Choose an index \(i\) (\(2 \le i \le m - 1\)) such that \(a_i \gt a_{i - 1}\) and \(a_i \gt a_{i + 1}\). Remove \(a_i\) from the array. Define the score of a permutation\(^{\text{β}}\) \(p\) as the maximum number of times that you can perform the shrink operation on \(p\).Yousef has given you a single integer \(n\). Construct a permutation \(p\) of length \(n\) with the maximum possible score. If there are multiple answers, you can output any of them.\(^{\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 of the input contains an integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.Each test case contains an integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the size 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, output any permutation \(p_1, p_2, \dots, p_n\) that maximizes the number of shrink operations.
|
In the first test case: We choose \(p = [1, 3, 2]\). Choose index \(2\), and remove \(p_2\) from the array. The array becomes \(p = [1, 2]\). It can be shown that the maximum number of operations we can perform is \(1\). Another valid answer is \(p = [2, 3, 1]\).In the second test case: We choose \(p = [2, 3, 6, 4, 5, 1]\). Choose index \(5\), and remove \(p_5\) from the array. The array becomes \(p = [2, 3, 6, 4, 1]\). Choose index \(3\), and remove \(p_3\) from the array. The array becomes \(p = [2, 3, 4, 1]\). Choose index \(3\), and remove \(p_3\) from the array. The array becomes \(p = [2, 3, 1]\). Choose index \(2\), and remove \(p_2\) from the array. The array becomes \(p = [2, 1]\). The maximum number of operations we can perform is \(4\). Any permutation with a score of \(4\) is valid.
|
Input: 236 | Output: 1 3 2 2 3 6 4 5 1
|
Beginner
| 1 | 868 | 298 | 115 | 21 |
276 |
C
|
276C
|
C. Little Girl and Maximum Sum
| 1,500 |
data structures; greedy; implementation; sortings
|
The little girl loves the problems on array queries very much.One day she came across a rather well-known problem: you've got an array of \(n\) elements (the elements of the array are indexed starting from 1); also, there are \(q\) queries, each one is defined by a pair of integers \(l_i\), \(r_i\) \((1 \le l_i \le r_i \le n)\). You need to find for each query the sum of elements of the array with indexes from \(l_i\) to \(r_i\), inclusive.The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum.
|
The first line contains two space-separated integers \(n\) (\(1 \le n \le 2\cdot10^5\)) and \(q\) (\(1 \le q \le 2\cdot10^5\)) β the number of elements in the array and the number of queries, correspondingly.The next line contains \(n\) space-separated integers \(a_i\) (\(1 \le a_i \le 2\cdot10^5\)) β the array elements.Each of the following \(q\) lines contains two space-separated integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le n\)) β the \(i\)-th query.
|
In a single line print, a single integer β the maximum sum of query replies after the array elements are reordered.Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Input: 3 35 3 21 22 31 3 | Output: 25
|
Medium
| 4 | 680 | 465 | 262 | 2 |
|
875 |
F
|
875F
|
F. Royal Questions
| 2,500 |
dsu; graphs; greedy
|
In a medieval kingdom, the economic crisis is raging. Milk drops fall, Economic indicators are deteriorating every day, money from the treasury disappear. To remedy the situation, King Charles Sunnyface decided make his n sons-princes marry the brides with as big dowry as possible.In search of candidates, the king asked neighboring kingdoms, and after a while several delegations arrived with m unmarried princesses. Receiving guests, Karl learned that the dowry of the i th princess is wi of golden coins. Although the action takes place in the Middle Ages, progressive ideas are widespread in society, according to which no one can force a princess to marry a prince whom she does not like. Therefore, each princess has an opportunity to choose two princes, for each of which she is ready to become a wife. The princes were less fortunate, they will obey the will of their father in the matter of choosing a bride.Knowing the value of the dowry and the preferences of each princess, Charles wants to play weddings in such a way that the total dowry of the brides of all his sons would be as great as possible. At the same time to marry all the princes or princesses is not necessary. Each prince can marry no more than one princess, and vice versa, each princess can marry no more than one prince.Help the king to organize the marriage of his sons in the most profitable way for the treasury.
|
The first line contains two integers n, m (2 β€ n β€ 200 000, 1 β€ m β€ 200 000) β number of princes and princesses respectively.Each of following m lines contains three integers ai, bi, wi (1 β€ ai, bi β€ n, ai β bi, 1 β€ wi β€ 10 000) β number of princes, which i-th princess is ready to marry and the value of her dowry.
|
Print the only integer β the maximum number of gold coins that a king can get by playing the right weddings.
|
Input: 2 31 2 51 2 12 1 10 | Output: 15
|
Expert
| 3 | 1,396 | 315 | 108 | 8 |
|
1,538 |
B
|
1538B
|
B. Friends and Candies
| 800 |
greedy; math
|
Polycarp has \(n\) friends, the \(i\)-th of his friends has \(a_i\) candies. Polycarp's friends do not like when they have different numbers of candies. In other words they want all \(a_i\) to be the same. To solve this, Polycarp performs the following set of actions exactly once: Polycarp chooses \(k\) (\(0 \le k \le n\)) arbitrary friends (let's say he chooses friends with indices \(i_1, i_2, \ldots, i_k\)); Polycarp distributes their \(a_{i_1} + a_{i_2} + \ldots + a_{i_k}\) candies among all \(n\) friends. During distribution for each of \(a_{i_1} + a_{i_2} + \ldots + a_{i_k}\) candies he chooses new owner. That can be any of \(n\) friends. Note, that any candy can be given to the person, who has owned that candy before the distribution process. Note that the number \(k\) is not fixed in advance and can be arbitrary. Your task is to find the minimum value of \(k\).For example, if \(n=4\) and \(a=[4, 5, 2, 5]\), then Polycarp could make the following distribution of the candies: Polycarp chooses \(k=2\) friends with indices \(i=[2, 4]\) and distributes \(a_2 + a_4 = 10\) candies to make \(a=[4, 4, 4, 4]\) (two candies go to person \(3\)). Note that in this example Polycarp cannot choose \(k=1\) friend so that he can redistribute candies so that in the end all \(a_i\) are equal.For the data \(n\) and \(a\), determine the minimum value \(k\). With this value \(k\), Polycarp should be able to select \(k\) friends and redistribute their candies so that everyone will end up with the same number of candies.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^4\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output: the minimum value of \(k\), such that Polycarp can choose exactly \(k\) friends so that he can redistribute the candies in the desired way; ""-1"" if no such value \(k\) exists.
|
Input: 5 4 4 5 2 5 2 0 4 5 10 8 5 1 4 1 10000 7 1 1 1 1 1 1 1 | Output: 2 1 -1 0 0
|
Beginner
| 2 | 1,528 | 369 | 204 | 15 |
|
929 |
D
|
929D
|
D. ΠΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΠ΅ Π²ΡΠ°ΡΠ°
| 2,400 |
ΠΠ΅ΡΠΎΠΉ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π½Π° ΡΠ·ΠΊΠΎΠΉ ΠΏΠΎΠ»ΠΎΡΠΊΠ΅ Π·Π΅ΠΌΠ»ΠΈ, ΡΠ°Π·Π΄Π΅Π»Π΅Π½Π½ΠΎΠΉ Π½Π° n Π·ΠΎΠ½, ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Π½ΡΡ
ΠΎΡ 1 Π΄ΠΎ n. ΠΠ· i-ΠΉ Π·ΠΎΠ½Ρ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠΎΠΉΡΠΈ Π»ΠΈΡΡ Π² (i - 1)-Ρ Π·ΠΎΠ½Ρ ΠΈ Π² (i + 1)-Ρ Π·ΠΎΠ½Ρ, Π΅ΡΠ»ΠΈ ΠΎΠ½ΠΈ ΡΡΡΠ΅ΡΡΠ²ΡΡΡ. ΠΡΠΈ ΡΡΠΎΠΌ ΠΌΠ΅ΠΆΠ΄Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΏΠ°ΡΠΎΠΉ ΡΠΎΡΠ΅Π΄Π½ΠΈΡ
Π·ΠΎΠ½ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ ΠΏΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΠ΅ Π²ΡΠ°ΡΠ°, ΠΊΠΎΡΠΎΡΡΠ΅ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ ΡΠ°Π·Π½ΡΡ
ΡΠ²Π΅ΡΠΎΠ², ΡΠ²Π΅Ρ Π²ΡΠ°Ρ ΠΌΠ΅ΠΆΠ΄Ρ i-ΠΉ ΠΈ (i + 1)-ΠΉ Π·ΠΎΠ½ΠΎΠΉ ΡΠ°Π²Π΅Π½ gi.ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΎΠΉΡΠΈ ΠΏΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΠ΅ Π²ΡΠ°ΡΠ° Π½Π΅ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ°, ΡΠΎΠ»ΡΠΊΠΎ Π΅ΡΠ»ΠΈ ΠΎΠ½ ΠΏΠ΅ΡΠ΅Π΄ ΡΡΠΈΠΌ ΠΏΠΎΠ±ΡΠ²Π°Π» Π² ΠΎΠ΄Π½ΠΎΠΌ ΠΈΠ· ΡΠ°ΡΡΠΎΠ² Ρ
ΡΠ°Π½ΠΈΡΠ΅Π»Π΅ΠΉ ΠΊΠ»ΡΡΠ΅ΠΉ ΡΡΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ° ΠΈ Π²Π·ΡΠ» ΠΊΠ»ΡΡ. Π ΠΊΠ°ΠΆΠ΄ΠΎΠΉ Π·ΠΎΠ½Π΅ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ ΡΠΎΠ²Π½ΠΎ ΠΎΠ΄ΠΈΠ½ ΡΠ°ΡΠ΅Ρ Ρ
ΡΠ°Π½ΠΈΡΠ΅Π»Ρ ΠΊΠ»ΡΡΠ΅ΠΉ Π½Π΅ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ°, ΡΠ²Π΅Ρ ΡΠ°ΡΡΠ° Π² i-ΠΉ Π·ΠΎΠ½Π΅ ΡΠ°Π²Π΅Π½ ki. ΠΠΎΡΠ»Π΅ ΠΏΠΎΡΠ΅ΡΠ΅Π½ΠΈΡ ΡΠ°ΡΡΠ° ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ° ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ Π½Π΅ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½Π½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΡΠ°Π· ΠΏΡΠΎΡ
ΠΎΠ΄ΠΈΡΡ ΡΠ΅ΡΠ΅Π· Π»ΡΠ±ΡΠ΅ Π²ΡΠ°ΡΠ° ΡΡΠΎΠ³ΠΎ ΡΠ²Π΅ΡΠ°.ΠΠ° ΠΏΡΠΎΡ
ΠΎΠ΄ ΡΠ΅ΡΠ΅Π· ΠΎΠ΄Π½ΠΈ Π²ΡΠ°ΡΠ° ΠΡΠΊΠ°Π΄ΠΈΠΉ ΡΡΠ°ΡΠΈΡ ΠΎΠ΄ΠΈΠ½ Ρ
ΠΎΠ΄, Π½Π° ΠΏΠΎΡΠ΅ΡΠ΅Π½ΠΈΠ΅ ΡΠ°ΡΡΠ° ΠΈ Π΄ΡΡΠ³ΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ΅Π½ΠΈΡ Ρ
ΠΎΠ΄Ρ Π½Π΅ ΡΡΠ΅Π±ΡΡΡΡΡ. ΠΠ° ΠΊΠ°ΠΊΠΎΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ Ρ
ΠΎΠ΄ΠΎΠ² ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΈΠ· Π·ΠΎΠ½Ρ a Π² Π·ΠΎΠ½Ρ b, Π΅ΡΠ»ΠΈ ΠΈΠ·Π½Π°ΡΠ°Π»ΡΠ½ΠΎ Ρ Π½Π΅Π³ΠΎ Π½Π΅Ρ Π½ΠΈΠΊΠ°ΠΊΠΈΡ
ΠΊΠ»ΡΡΠ΅ΠΉ?
|
ΠΠ΅ΡΠ²Π°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΡΡΠΈ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° n, a, b (2 β€ n β€ 100 000, 1 β€ a, b β€ n, a β b) β ΡΠΈΡΠ»ΠΎ Π·ΠΎΠ½, Π½ΠΎΠΌΠ΅Ρ Π½Π°ΡΠ°Π»ΡΠ½ΠΎΠΉ Π·ΠΎΠ½Ρ ΠΈ Π½ΠΎΠΌΠ΅Ρ ΠΊΠΎΠ½Π΅ΡΠ½ΠΎΠΉ Π·ΠΎΠ½Ρ, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.ΠΡΠΎΡΠ°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ n - 1 ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ g1, g2, ..., gn - 1 (1 β€ gi β€ 100 000), Π³Π΄Π΅ gi ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ ΡΠ²Π΅Ρ ΠΏΠΎΠ³ΡΠ°Π½ΠΈΡΠ½ΡΡ
Π²ΡΠ°Ρ ΠΌΠ΅ΠΆΠ΄Ρ Π·ΠΎΠ½Π°ΠΌΠΈ i ΠΈ i + 1.Π’ΡΠ΅ΡΡΡ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ n ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» k1, k2, ..., kn (1 β€ ki β€ 100 000), Π³Π΄Π΅ ki ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ ΡΠ²Π΅Ρ ΡΠ°ΡΡΠ° Ρ
ΡΠ°Π½ΠΈΡΠ΅Π»Ρ ΠΊΠ»ΡΡΠ΅ΠΉ Π² i-ΠΉ Π·ΠΎΠ½Π΅.
|
ΠΡΠ»ΠΈ ΠΡΠΊΠ°Π΄ΠΈΠΉ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΈΠ· Π·ΠΎΠ½Ρ a Π² Π·ΠΎΠ½Ρ b, Π½Π΅ ΠΈΠΌΠ΅Ρ ΠΈΠ·Π½Π°ΡΠ°Π»ΡΠ½ΠΎ ΠΊΠ»ΡΡΠ΅ΠΉ, Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ -1.ΠΠ½Π°ΡΠ΅ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Ρ
ΠΎΠ΄ΠΎΠ², ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΏΠΎΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΠΡΠΊΠ°Π΄ΠΈΡ.
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅, ΡΡΠΎΠ±Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΈΠ· Π·ΠΎΠ½Ρ 4 Π² Π·ΠΎΠ½Ρ 1, ΠΡΠΊΠ°Π΄ΠΈΡ Π½ΡΠΆΠ½ΠΎ ΡΠ½Π°ΡΠ°Π»Π° Π²Π·ΡΡΡ ΠΊΠ»ΡΡ ΡΠ²Π΅ΡΠ° 1, ΠΏΡΠΎΠΉΡΠΈ Π² Π·ΠΎΠ½Ρ 3, ΡΠ°ΠΌ Π²Π·ΡΡΡ ΠΊΠ»ΡΡ ΡΠ²Π΅ΡΠ° 2 ΠΈ ΠΏΡΠΎΠΉΡΠΈ ΠΎΠ±ΡΠ°ΡΠ½ΠΎ Π² Π·ΠΎΠ½Ρ 4 ΠΈ Π·Π°ΡΠ΅ΠΌ Π² Π·ΠΎΠ½Ρ 5, Π²Π·ΡΡΡ ΡΠ°ΠΌ ΠΊΠ»ΡΡ ΡΠ²Π΅ΡΠ° 3 ΠΈ Π΄ΠΎΠΉΡΠΈ Π΄ΠΎ Π·ΠΎΠ½Ρ 1 Π·Π° ΡΠ΅ΡΡΡΠ΅ Ρ
ΠΎΠ΄Π°.ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΡΠΊΠ°Π΄ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ Π΄ΠΎΠΉΡΠΈ Π»ΠΈΡΡ Π΄ΠΎ ΡΠ΅ΡΠ²Π΅ΡΡΠΎΠΉ Π·ΠΎΠ½Ρ, ΡΠ°ΠΊ ΠΊΠ°ΠΊ ΡΠ°ΡΡΠΎΠ² Ρ
ΡΠ°Π½ΠΈΡΠ΅Π»Π΅ΠΉ ΠΊΠ»ΡΡΠ΅ΠΉ ΡΠ²Π΅ΡΠ° 1 Π½Π΅Ρ ΡΠΎΠ²ΡΠ΅ΠΌ.
|
Input: 5 4 13 1 1 27 1 2 1 3 | Output: 7
|
Expert
| 0 | 945 | 428 | 162 | 9 |
|
1,904 |
B
|
1904B
|
B. Collecting Game
| 1,100 |
binary search; dp; greedy; sortings; two pointers
|
You are given an array \(a\) of \(n\) positive integers and a score. If your score is greater than or equal to \(a_i\), then you can increase your score by \(a_i\) and remove \(a_i\) from the array. For each index \(i\), output the maximum number of additional array elements that you can remove if you remove \(a_i\) and then set your score to \(a_i\). Note that the removal of \(a_i\) should not be counted in the answer.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 5000\)) β 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 10^5\)) β the length of the array.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.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output \(n\) integers, the \(i\)-th of which denotes the maximum number of additional array elements that you can remove if you remove \(a_i\) from the array and then set your score to \(a_i\).
|
In the first test case, the answers are as follows:If we start with \(i=4\), our initial score is \(a_4=4\) and \(a=[20,5,1,2]\). We can remove \(3\) additional elements in the following order: Since \(4 \ge 1\), we can remove \(1\) and our score becomes \(5\). After this, \(a=[20,5,2]\). Since \(5 \ge 5\), we can remove \(5\) and our score becomes \(10\). After this, \(a=[20,2]\). Since \(10 \ge 2\), we can remove \(2\) and our score becomes \(12\). After this, \(a=[20]\). If we start with \(i=1\) we can remove all remaining elements in the array, so the answer is \(4\).If we start with \(i=2\), we can remove \(3\) additional elements in the following order: \(1\), \(4\), \(2\).If we start with \(i=3\), we can remove no additional elements.If we start with \(i=5\), we can remove \(1\) additional element: \(1\).
|
Input: 4520 5 1 4 231434 7 1442115999999999 999999999 999999999 1000000000 1000000000 | Output: 4 3 0 3 1 1 0 2 0 4 4 4 4 4
|
Easy
| 5 | 423 | 511 | 213 | 19 |
1,379 |
C
|
1379C
|
C. Choosing flowers
| 2,000 |
binary search; brute force; data structures; dfs and similar; dp; greedy; sortings; two pointers
|
Vladimir would like to prepare a present for his wife: they have an anniversary! He decided to buy her exactly \(n\) flowers.Vladimir went to a flower shop, and he was amazed to see that there are \(m\) types of flowers being sold there, and there is unlimited supply of flowers of each type. Vladimir wants to choose flowers to maximize the happiness of his wife. He knows that after receiving the first flower of the \(i\)-th type happiness of his wife increases by \(a_i\) and after receiving each consecutive flower of this type her happiness increases by \(b_i\). That is, if among the chosen flowers there are \(x_i > 0\) flowers of type \(i\), his wife gets \(a_i + (x_i - 1) \cdot b_i\) additional happiness (and if there are no flowers of type \(i\), she gets nothing for this particular type).Please help Vladimir to choose exactly \(n\) flowers to maximize the total happiness of his wife.
|
The first line contains the only integer \(t\) (\(1 \leq t \leq 10\,000\)), the number of test cases. It is followed by \(t\) descriptions of the test cases.Each test case description starts with two integers \(n\) and \(m\) (\(1 \le n \le 10^9\), \(1 \le m \le 100\,000\)), the number of flowers Vladimir needs to choose and the number of types of available flowers.The following \(m\) lines describe the types of flowers: each line contains integers \(a_i\) and \(b_i\) (\(0 \le a_i, b_i \le 10^9\)) for \(i\)-th available type of flowers.The test cases are separated by a blank line. It is guaranteed that the sum of values \(m\) among all test cases does not exceed \(100\,000\).
|
For each test case output a single integer: the maximum total happiness of Vladimir's wife after choosing exactly \(n\) flowers optimally.
|
In the first example case Vladimir can pick 1 flower of the first type and 3 flowers of the second type, in this case the total happiness equals \(5 + (1 + 2 \cdot 4) = 14\).In the second example Vladimir can pick 2 flowers of the first type, 2 flowers of the second type, and 1 flower of the third type, in this case the total happiness equals \((5 + 1 \cdot 2) + (4 + 1 \cdot 2) + 3 = 16\).
|
Input: 2 4 3 5 0 1 4 2 2 5 3 5 2 4 2 3 1 | Output: 14 16
|
Hard
| 8 | 900 | 683 | 138 | 13 |
1,550 |
B
|
1550B
|
B. Maximum Cost Deletion
| 1,000 |
greedy; math
|
You are given a string \(s\) of length \(n\) consisting only of the characters 0 and 1.You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 111110, you will get the string 110. When you delete a substring of length \(l\), you get \(a \cdot l + b\) points.Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty.
|
The first line contains a single integer \(t\) (\(1 \le t \le 2000\)) β the number of testcases.The first line of each testcase contains three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 100; -100 \le a, b \le 100\)) β the length of the string \(s\) and the parameters \(a\) and \(b\).The second line contains the string \(s\). The string \(s\) consists only of the characters 0 and 1.
|
For each testcase, print a single integer β the maximum number of points that you can score.
|
In the first example, it is enough to delete the entire string, then we will get \(2 \cdot 3 + 0 = 6\) points.In the second example, if we delete characters one by one, then for each deleted character we will get \((-2) \cdot 1 + 5 = 3\) points, i. e. \(15\) points in total.In the third example, we can delete the substring 00 from the string 100111, we get \(1 \cdot 2 + (-4) = -2\) points, and the string will be equal to 1111, removing it entirely we get \(1 \cdot 4 + (-4) = 0\) points. In total, we got \(-2\) points for \(2\) operations.
|
Input: 3 3 2 0 000 5 -2 5 11001 6 1 -4 100111 | Output: 6 15 -2
|
Beginner
| 2 | 625 | 388 | 92 | 15 |
1,694 |
B
|
1694B
|
B. Paranoid String
| 1,200 |
constructive algorithms; greedy
|
Let's call a binary string \(T\) of length \(m\) indexed from \(1\) to \(m\) paranoid if we can obtain a string of length \(1\) by performing the following two kinds of operations \(m-1\) times in any order : Select any substring of \(T\) that is equal to 01, and then replace it with 1. Select any substring of \(T\) that is equal to 10, and then replace it with 0.For example, if \(T = \) 001, we can select the substring \([T_2T_3]\) and perform the first operation. So we obtain \(T = \) 01.You are given a binary string \(S\) of length \(n\) indexed from \(1\) to \(n\). Find the number of pairs of integers \((l, r)\) \(1 \le l \le r \le n\) such that \(S[l \ldots r]\) (the substring of \(S\) from \(l\) to \(r\)) is a paranoid string.
|
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the size of \(S\).The second line of each test case contains a binary string \(S\) of \(n\) characters \(S_1S_2 \ldots S_n\). (\(S_i = \) 0 or \(S_i = \) 1 for each \(1 \le i \le n\))It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, output the number of pairs of integers \((l, r)\) \(1 \le l \le r \le n\) such that \(S[l \ldots r]\) (the substring of \(S\) from \(l\) to \(r\)) is a paranoid string.
|
In the first sample, \(S\) already has length \(1\) and doesn't need any operations.In the second sample, all substrings of \(S\) are paranoid. For the entire string, it's enough to perform the first operation.In the third sample, all substrings of \(S\) are paranoid except \([S_2S_3]\), because we can't perform any operations on it, and \([S_1S_2S_3]\) (the entire string).
|
Input: 511201310041001511111 | Output: 1 3 4 8 5
|
Easy
| 2 | 742 | 502 | 188 | 16 |
601 |
A
|
601A
|
A. The Two Routes
| 1,600 |
graphs; shortest paths
|
In Absurdistan, there are n towns (numbered 1 through n) and m bidirectional railways. There is also an absurdly simple road network β for each pair of different towns x and y, there is a bidirectional road between towns x and y if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour.A train and a bus leave town 1 at the same time. They both have the same destination, town n, and don't make any stops on the way (but they can wait in town n). The train can move only along railways and the bus can move only along roads.You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safety β in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town n) simultaneously.Under these constraints, what is the minimum number of hours needed for both vehicles to reach town n (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town n at the same moment of time, but are allowed to do so.
|
The first line of the input contains two integers n and m (2 β€ n β€ 400, 0 β€ m β€ n(n - 1) / 2) β the number of towns and the number of railways respectively.Each of the next m lines contains two integers u and v, denoting a railway between towns u and v (1 β€ u, v β€ n, u β v).You may assume that there is at most one railway connecting any two towns.
|
Output one integer β the smallest possible time of the later vehicle's arrival in town n. If it's impossible for at least one of the vehicles to reach town n, output - 1.
|
In the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town 4 at the same time.In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.
|
Input: 4 21 33 4 | Output: 2
|
Medium
| 2 | 1,186 | 349 | 170 | 6 |
818 |
B
|
818B
|
B. Permutation Game
| 1,600 |
implementation
|
n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a1, a2, ..., an of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it.The game consists of m steps. On each step the current leader with index i counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader becomes the new leader.You are given numbers l1, l2, ..., lm β indices of leaders in the beginning of each step. Child with number l1 is the first leader in the game. Write a program which will restore a possible permutation a1, a2, ..., an. If there are multiple solutions then print any of them. If there is no solution then print -1.
|
The first line contains two integer numbers n, m (1 β€ n, m β€ 100).The second line contains m integer numbers l1, l2, ..., lm (1 β€ li β€ n) β indices of leaders in the beginning of each step.
|
Print such permutation of n numbers a1, a2, ..., an that leaders in the game will be exactly l1, l2, ..., lm if all the rules are followed. If there are multiple solutions print any of them. If there is no permutation which satisfies all described conditions print -1.
|
Let's follow leadership in the first example: Child 2 starts. Leadership goes from 2 to 2 + a2 = 3. Leadership goes from 3 to 3 + a3 = 5. As it's greater than 4, it's going in a circle to 1. Leadership goes from 1 to 1 + a1 = 4. Leadership goes from 4 to 4 + a4 = 8. Thus in circle it still remains at 4.
|
Input: 4 52 3 1 4 4 | Output: 3 1 2 4
|
Medium
| 1 | 757 | 189 | 268 | 8 |
666 |
D
|
666D
|
D. Chain Reaction
| 3,000 |
brute force; geometry
|
Group of Berland scientists, with whom you have a close business relationship, makes a research in the area of peaceful nuclear energy. In particular, they found that a group of four nanobots, placed on a surface of a plate, can run a powerful chain reaction under certain conditions. To be precise, researchers introduced a rectangular Cartesian coordinate system on a flat plate and selected four distinct points with integer coordinates where bots will be placed initially. Next each bot will be assigned with one of the four directions (up, down, left or right) parallel to the coordinate axes. After that, each bot is shifted by an integer distance (which may be different for different bots) along its direction. The chain reaction starts, if the bots are in the corners of a square with positive area with sides parallel to the coordinate axes. Each corner of the square must contain one nanobot. This reaction will be stronger, if bots spend less time to move. We can assume that bots move with unit speed. In other words, the lesser is the maximum length traveled by bot, the stronger is reaction.Scientists have prepared a set of plates and selected starting position for the bots for each plate. Now they ask you to assign the direction for each bot to move after landing such that the maximum length traveled by bot is as small as possible.
|
The first line contains an integer number t (1 β€ t β€ 50) β the number of plates.t descriptions of plates follow. A description of each plate consists of four lines. Each line consists of a pair of integers numbers xi, yi ( - 108 β€ xi, yi β€ 108) β coordinates of the next bot. All bots are in different locations.Note, though, the problem can include several records in one test, you can hack other people's submissions only with the test of one plate, i.e. parameter t in a hack test should be equal to 1.
|
Print answers for all plates separately. First goes a single integer number in a separate line. If scientists have made an unfortunate mistake and nanobots are not able to form the desired square, print -1. Otherwise, print the minimum possible length of the longest bot's path.If a solution exists, in the next four lines print two integer numbers β positions of each bot after moving. Print bots' positions in the order they are specified in the input data.If there are multiple solution, you can print any of them.
|
Input: 21 11 -1-1 1-1 -11 12 24 46 6 | Output: 01 11 -1-1 1-1 -1-1
|
Master
| 2 | 1,352 | 505 | 517 | 6 |
|
1,759 |
G
|
1759G
|
G. Restore the Permutation
| 1,900 |
binary search; constructive algorithms; data structures; greedy; math
|
A sequence of \(n\) numbers is called permutation if it contains all numbers from \(1\) to \(n\) exactly once. For example, the sequences [\(3, 1, 4, 2\)], [\(1\)] and [\(2,1\)] are permutations, but [\(1,2,1\)], [\(0,1\)] and [\(1,3,4\)] β are not.For a permutation \(p\) of even length \(n\) you can make an array \(b\) of length \(\frac{n}{2}\) such that: \(b_i = \max(p_{2i - 1}, p_{2i})\) for \(1 \le i \le \frac{n}{2}\) For example, if \(p\) = [\(2, 4, 3, 1, 5, 6\)], then: \(b_1 = \max(p_1, p_2) = \max(2, 4) = 4\) \(b_2 = \max(p_3, p_4) = \max(3,1)=3\) \(b_3 = \max(p_5, p_6) = \max(5,6) = 6\) As a result, we made \(b\) = \([4, 3, 6]\).For a given array \(b\), find the lexicographically minimal permutation \(p\) such that you can make the given array \(b\) from it.If \(b\) = [\(4,3,6\)], then the lexicographically minimal permutation from which it can be made is \(p\) = [\(1,4,2,3,5,6\)], since: \(b_1 = \max(p_1, p_2) = \max(1, 4) = 4\) \(b_2 = \max(p_3, p_4) = \max(2, 3) = 3\) \(b_3 = \max(p_5, p_6) = \max(5, 6) = 6\) A permutation \(x_1, x_2, \dots, x_n\) is lexicographically smaller than a permutation \(y_1, y_2 \dots, y_n\) if and only if there exists such \(i\) (\(1 \le i \le n\)) that \(x_1=y_1, x_2=y_2, \dots, x_{i-1}=y_{i-1}\) and \(x_i<y_i\).
|
The first line of input data 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 one even integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line of each test case contains exactly \(\frac{n}{2}\) integers \(b_i\) (\(1 \le b_i \le n\)) β elements of array \(b\).It is guaranteed that the sum of \(n\) values over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print on a separate line: lexicographically minimal permutation \(p\) such that you can make an array \(b\) from it; or a number -1 if the permutation you are looking for does not exist.
|
The first test case is parsed in the problem statement.
|
Input: 664 3 642 488 7 2 366 4 244 488 7 4 5 | Output: 1 4 2 3 5 6 1 2 3 4 -1 5 6 3 4 1 2 -1 1 8 6 7 2 4 3 5
|
Hard
| 5 | 1,272 | 480 | 206 | 17 |
1,606 |
A
|
1606A
|
A. AB Balance
| 900 |
strings
|
You are given a string \(s\) of length \(n\) consisting of characters a and/or b.Let \(\operatorname{AB}(s)\) be the number of occurrences of string ab in \(s\) as a substring. Analogically, \(\operatorname{BA}(s)\) is the number of occurrences of ba in \(s\) as a substring.In one step, you can choose any index \(i\) and replace \(s_i\) with character a or b.What is the minimum number of steps you need to make to achieve \(\operatorname{AB}(s) = \operatorname{BA}(s)\)?Reminder:The number of occurrences of string \(d\) in \(s\) as substring is the number of indices \(i\) (\(1 \le i \le |s| - |d| + 1\)) such that substring \(s_i s_{i + 1} \dots s_{i + |d| - 1}\) is equal to \(d\). For example, \(\operatorname{AB}(\)aabbbabaa\() = 2\) since there are two indices \(i\): \(i = 2\) where aabbbabaa and \(i = 6\) where aabbbabaa.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The first and only line of each test case contains a single string \(s\) (\(1 \le |s| \le 100\), where \(|s|\) is the length of the string \(s\)), consisting only of characters a and/or b.
|
For each test case, print the resulting string \(s\) with \(\operatorname{AB}(s) = \operatorname{BA}(s)\) you'll get making the minimum number of steps.If there are multiple answers, print any of them.
|
In the first test case, both \(\operatorname{AB}(s) = 0\) and \(\operatorname{BA}(s) = 0\) (there are no occurrences of ab (ba) in b), so can leave \(s\) untouched.In the second test case, \(\operatorname{AB}(s) = 2\) and \(\operatorname{BA}(s) = 2\), so you can leave \(s\) untouched. In the third test case, \(\operatorname{AB}(s) = 1\) and \(\operatorname{BA}(s) = 0\). For example, we can change \(s_1\) to b and make both values zero.In the fourth test case, \(\operatorname{AB}(s) = 2\) and \(\operatorname{BA}(s) = 1\). For example, we can change \(s_6\) to a and make both values equal to \(1\).
|
Input: 4 b aabbbabaa abbb abbaab | Output: b aabbbabaa bbbb abbaaa
|
Beginner
| 1 | 833 | 345 | 201 | 16 |
483 |
B
|
483B
|
B. Friends and Presents
| 1,800 |
binary search; math
|
You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to the first friend and cnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
|
The only line contains four positive integers cnt1, cnt2, x, y (1 β€ cnt1, cnt2 < 109; cnt1 + cnt2 β€ 109; 2 β€ x < y β€ 3Β·104) β the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
|
Print a single integer β the answer to the problem.
|
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend. In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
|
Input: 3 1 2 3 | Output: 5
|
Medium
| 2 | 865 | 220 | 51 | 4 |
1,312 |
F
|
1312F
|
F. Attack on Red Kingdom
| 2,500 |
games; two pointers
|
The Red Kingdom is attacked by the White King and the Black King!The Kingdom is guarded by \(n\) castles, the \(i\)-th castle is defended by \(a_i\) soldiers. To conquer the Red Kingdom, the Kings have to eliminate all the defenders. Each day the White King launches an attack on one of the castles. Then, at night, the forces of the Black King attack a castle (possibly the same one). Then the White King attacks a castle, then the Black King, and so on. The first attack is performed by the White King.Each attack must target a castle with at least one alive defender in it. There are three types of attacks: a mixed attack decreases the number of defenders in the targeted castle by \(x\) (or sets it to \(0\) if there are already less than \(x\) defenders); an infantry attack decreases the number of defenders in the targeted castle by \(y\) (or sets it to \(0\) if there are already less than \(y\) defenders); a cavalry attack decreases the number of defenders in the targeted castle by \(z\) (or sets it to \(0\) if there are already less than \(z\) defenders). The mixed attack can be launched at any valid target (at any castle with at least one soldier). However, the infantry attack cannot be launched if the previous attack on the targeted castle had the same type, no matter when and by whom it was launched. The same applies to the cavalry attack. A castle that was not attacked at all can be targeted by any type of attack.The King who launches the last attack will be glorified as the conqueror of the Red Kingdom, so both Kings want to launch the last attack (and they are wise enough to find a strategy that allows them to do it no matter what are the actions of their opponent, if such strategy exists). The White King is leading his first attack, and you are responsible for planning it. Can you calculate the number of possible options for the first attack that allow the White King to launch the last attack? Each option for the first attack is represented by the targeted castle and the type of attack, and two options are different if the targeted castles or the types of attack are different.
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Then, the test cases follow. Each test case is represented by two lines. The first line contains four integers \(n\), \(x\), \(y\) and \(z\) (\(1 \le n \le 3 \cdot 10^5\), \(1 \le x, y, z \le 5\)). The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le 10^{18}\)).It is guaranteed that the sum of values of \(n\) over all test cases in the input does not exceed \(3 \cdot 10^5\).
|
For each test case, print the answer to it: the number of possible options for the first attack of the White King (or \(0\), if the Black King can launch the last attack no matter how the White King acts).
|
Input: 3 2 1 3 4 7 6 1 1 2 3 1 1 1 2 2 3 | Output: 2 3 0
|
Expert
| 2 | 2,118 | 504 | 205 | 13 |
|
1,628 |
E
|
1628E
|
E. Groceries in Meteor Town
| 3,100 |
binary search; data structures; dsu; trees
|
Mihai lives in a town where meteor storms are a common problem. It's annoying, because Mihai has to buy groceries sometimes, and getting hit by meteors isn't fun. Therefore, we ask you to find the most dangerous way to buy groceries so that we can trick him to go there.The town has \(n\) buildings numbered from \(1\) to \(n\). Some buildings have roads between them, and there is exactly \(1\) simple path from any building to any other building. Each road has a certain meteor danger level. The buildings all have grocery stores, but Mihai only cares about the open ones, of course. Initially, all the grocery stores are closed.You are given \(q\) queries of three types: Given the integers \(l\) and \(r\), the buildings numbered from \(l\) to \(r\) open their grocery stores (nothing happens to buildings in the range that already have an open grocery store). Given the integers \(l\) and \(r\), the buildings numbered from \(l\) to \(r\) close their grocery stores (nothing happens to buildings in the range that didn't have an open grocery store). Given the integer \(x\), find the maximum meteor danger level on the simple path from \(x\) to any open grocery store, or \(-1\) if there is no edge on any simple path to an open store.
|
The first line contains the two integers \(n\) and \(q\) (\(2 \le n, q \le 3\cdot 10^5\)).Then follows \(n - 1\) lines, the \(i\)-th of which containing the integers \(u_i\), \(v_i\), and \(w_i\) (\(1 \le u_i, v_i \le n, \enspace 1 \le w_i \le 10^9\)) meaning there is two way road between building \(u_i\) and \(v_i\) with meteor danger level \(w_i\).It is guaranteed that the given edges form a tree.Then follows \(q\) lines, the \(j\)-th of which begin with the integer \(t_j\) (\(1 \le t_j \le 3\)), meaning the \(j\)-th query is of the \(t_j\)-th type.If \(t_j\) is \(1\) or \(2\) the rest of the line contains the integers \(l_j\) and \(r_j\) (\(1 \le l_j \le r_j \le n\)).If \(t_j\) is \(3\) the rest of the line contains the integer \(x_j\) (\(1 \le x_j \le n\)).
|
For each query of the \(3\)rd type (\(t_j = 3\)), output the maximum meteor danger level that is on some edge on the simple path from \(x_j\) to some open store, or \(-1\) if there is no such edge.
|
This is an illustration of the town given in the sample input.In the first query, there are no open stores, so obviously there are no edges on the simple path from \(1\) to any open store, so the answer is \(-1\).After the second and third queries, the set of open stores is \(\{1\}\). The simple path from \(1\) to \(1\) has no edges, so the answer for the \(3\)rd query is \(-1\).After the fourth query, there are no open stores.After the fifth and sixth queries, the set of open stores is \(\{5, 6\}\). In the sixth query, there are two paths from \(x_j = 4\) to some open grocery store: \(4\) to \(5\) and \(4\) to \(6\). The biggest meteor danger is found on the edge from \(4\) to \(6\), so the answer for the \(6\)th query is \(4\). This path is marked with red in the illustration.After the rest of the queries, the set of open stores is \(\{5\}\). In the eighth query, the only path from \(x_j = 4\) to an open store is from \(4\) to \(5\), and the maximum weight on that path is \(3\). This path is marked with green in the illustration.In the ninth query, the only path from \(x_j = 1\) to an open store is from \(1\) to \(5\), and the maximum weight on that path is \(5\). This path is marked with blue in the illustration.
|
Input: 6 9 1 3 1 2 3 2 4 5 3 4 6 4 3 4 5 3 1 1 1 1 3 1 2 1 1 1 5 6 3 4 2 6 6 3 4 3 1 | Output: -1 -1 4 3 5
|
Master
| 4 | 1,240 | 771 | 197 | 16 |
2,074 |
F
|
2074F
|
F. Counting Necessary Nodes
| 2,000 |
bitmasks; divide and conquer; greedy; implementation; math
|
A quadtree is a tree data structure in which each node has at most four children and accounts for a square-shaped region.Formally, for all tuples of nonnegative integers \(k,a,b \ge 0\), there exists exactly one node accounting for the following region\(^{\text{β}}\).$$$\([a \cdot 2^k,(a+1) \cdot 2^k] \times [b \cdot 2^k,(b+1) \cdot 2^k]\)\(All nodes whose region is larger than \)1 \times 1\( contain four children corresponding to the regions divided equally into four, and the nodes whose region is \)1 \times 1\( correspond to the leaf nodes of the tree. A small subset of the regions accounted for by the nodes is shown. The relatively darker regions are closer to leaf nodes. The Frontman hates the widespread misconception, such that the quadtree can perform range queries in \)\mathcal{O}(\log n)\( time when there are \)n\( leaf nodes inside the region. In fact, sometimes it is necessary to query much more than \)\mathcal{O}(\log n)\( regions for this, and the time complexity is \)\mathcal{O}(n)\( in some extreme cases. Thus, the Frontman came up with this task to educate you about this worst case of the data structure.The pink soldiers have given you a finite region \)[l_1,r_1] \times [l_2,r_2]\(, where \)l_i\( and \)r_i\( (\)l_i < r_i\() are nonnegative integers. Please find the minimum number of nodes that you must choose in order to make the union of regions accounted for by the chosen nodes exactly the same as the given region. Here, two sets of points are considered different if there exists a point included in one but not in the other.\)^{\text{β}}\(Regions are sets of points with real coordinates, where the point \)(x,y)\( is included in the region \)[p,q] \times [r,s]\( if and only if \)p \le x \le q\( and \)r \le y \le s\(. Here, \)\times$$$ formally refers to Cartesian product of sets.
|
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 four integers \(l_1\), \(r_1\), \(l_2\), \(r_2\) β the boundaries of the region in each axis (\(0 \le l_i < r_i \le 10^6\)).
|
For each test case, output the minimum number of nodes necessary to satisfy the condition on a separate line.
|
On the first test case, the given region is \([0,1] \times [1,2]\). There is one node accounting for \([0,1] \times [1,2]\). Choosing this node, the answer is \(1\).On the second test case, the given region is \([0,2] \times [0,2]\). There is one node accounting for \([0,2] \times [0,2]\). Choosing this node, the answer is \(1\).On the third test case, the given region is \([1,3] \times [1,3]\). There is no node that accounts for \([1,3] \times [1,3]\). Instead, you can make the union of regions exactly the same as \([1,3] \times [1,3]\) by choosing the following \(4\) nodes: A leaf node accounting for \([1,2] \times [1,2]\); A leaf node accounting for \([1,2] \times [2,3]\); A leaf node accounting for \([2,3] \times [1,2]\); A leaf node accounting for \([2,3] \times [2,3]\). It can be shown that it is impossible to make the union of regions exactly the same as \([1,3] \times [1,3]\) with less than \(4\) nodes. Therefore, the answer is \(4\).On the fourth test case, the given region is \([0,2] \times [1,5]\). You can make the union of regions exactly the same as \([0,2] \times [1,5]\) by choosing the following \(5\) nodes: A leaf node accounting for \([0,1] \times [1,2]\); A leaf node accounting for \([1,2] \times [1,2]\); A non-leaf node accounting for \([0,2] \times [2,4]\); A leaf node accounting for \([0,1] \times [4,5]\); A leaf node accounting for \([1,2] \times [4,5]\). It can be shown that it is impossible to make the union of regions exactly the same as \([0,2] \times [1,5]\) with less than \(5\) nodes. Therefore, the answer is \(5\).
|
Input: 50 1 1 20 2 0 21 3 1 30 2 1 59 98 244 353 | Output: 1 1 4 5 374
|
Hard
| 5 | 1,826 | 327 | 109 | 20 |
1,615 |
B
|
1615B
|
B. And It's Non-Zero
| 1,300 |
bitmasks; greedy; math
|
You are given an array consisting of all integers from \([l, r]\) inclusive. For example, if \(l = 2\) and \(r = 5\), the array would be \([2, 3, 4, 5]\). What's the minimum number of elements you can delete to make the bitwise AND of the array non-zero?A bitwise AND is a binary operation that takes two equal-length binary representations and performs the AND operation on each pair of the corresponding bits.
|
The first line contains one integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then \(t\) cases follow.The first line of each test case contains two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq 2 \cdot 10^5\)) β the description of the array.
|
For each test case, output a single integer β the answer to the problem.
|
In the first test case, the array is \([1, 2]\). Currently, the bitwise AND is \(0\), as \(1\ \& \ 2 = 0\). However, after deleting \(1\) (or \(2\)), the array becomes \([2]\) (or \([1]\)), and the bitwise AND becomes \(2\) (or \(1\)). This can be proven to be the optimal, so the answer is \(1\).In the second test case, the array is \([2, 3, 4, 5, 6, 7, 8]\). Currently, the bitwise AND is \(0\). However, after deleting \(4\), \(5\), and \(8\), the array becomes \([2, 3, 6, 7]\), and the bitwise AND becomes \(2\). This can be proven to be the optimal, so the answer is \(3\). Note that there may be other ways to delete \(3\) elements.
|
Input: 5 1 2 2 8 4 5 1 5 100000 200000 | Output: 1 3 0 2 31072
|
Easy
| 3 | 411 | 261 | 72 | 16 |
1,862 |
G
|
1862G
|
G. The Great Equalizer
| 2,000 |
binary search; data structures; math; sortings
|
Tema bought an old device with a small screen and a worn-out inscription ""The Great Equalizer"" on the side.The seller said that the device needs to be given an array \(a\) of integers as input, after which ""The Great Equalizer"" will work as follows: Sort the current array in non-decreasing order and remove duplicate elements leaving only one occurrence of each element. If the current length of the array is equal to \(1\), the device stops working and outputs the single number in the array β output value of the device. Add an arithmetic progression {\(n,\ n - 1,\ n - 2,\ \ldots,\ 1\)} to the current array, where \(n\) is the length of the current array. In other words, \(n - i\) is added to the \(i\)-th element of the array, when indexed from zero. Go to the first step. To test the operation of the device, Tema came up with a certain array of integers \(a\), and then wanted to perform \(q\) operations on the array \(a\) of the following type: Assign the value \(x\) (\(1 \le x \le 10^9\)) to the element \(a_i\) (\(1 \le i \le n\)). Give the array \(a\) as input to the device and find out the result of the device's operation, while the array \(a\) remains unchanged during the operation of the device. Help Tema find out the output values of the device after each operation.
|
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 description of each test case.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the size of the array \(a\) that Tema initially came up with.The second line of each test case contains \(n\) integers \(a_1, a_2, a_3, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array \(a\).The third line of a set contains a single integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of operations.Each of the next \(q\) lines of a test case contains two integers \(i\) (\(1 \le i \le n\)) and \(x\) (\(1 \le x \le 10^9\)) - the descriptions of the operations.It is guaranteed that the sum of the values of \(n\) and the sum of the values of \(q\) for all test cases do not exceed \(2 \cdot 10^5\).
|
For each test case, output \(q\) integers β the output values of the device after each operation.
|
Let's consider the first example of the input.Initially, the array of numbers given as input to the device will be \([6, 4, 8]\). It will change as follows: $$$\([6, 4, 8] \rightarrow [4, 6, 8] \rightarrow [7, 8, 9] \rightarrow [10, 10, 10] \rightarrow [10]\)\(Then, the array of numbers given as input to the device will be \)[6, 10, 8]\(. It will change as follows: \)\([6, 10, 8] \rightarrow [6, 8, 10] \rightarrow [9, 10, 11] \rightarrow [12, 12, 12] \rightarrow [12]\)\(The last array of numbers given as input to the device will be \)[6, 10, 1]\(. It will change as follows: \)\([6, 10, 1] \rightarrow [1, 6, 10] \rightarrow [4, 8, 11] \rightarrow [7, 10, 12] \rightarrow [10, 12, 13] \rightarrow [13, 14, 14] \rightarrow [13, 14] \rightarrow [15, 15] \rightarrow [15]\)$$$
|
Input: 432 4 831 62 103 151 2 2 2 215 325 671 21 71 72 51 22 72 252 5 1 10 6101 74 82 51 42 83 41 93 73 43 1 | Output: 10 12 15 4 10 8 8 9 8 12 2 14 12 12 11 11 10 11 10 11 14
|
Hard
| 4 | 1,293 | 878 | 97 | 18 |
1,580 |
C
|
1580C
|
C. Train Maintenance
| 2,200 |
brute force; data structures; implementation
|
Kawasiro Nitori is excellent in engineering. Thus she has been appointed to help maintain trains.There are \(n\) models of trains, and Nitori's department will only have at most one train of each model at any moment. In the beginning, there are no trains, at each of the following \(m\) days, one train will be added, or one train will be removed. When a train of model \(i\) is added at day \(t\), it works for \(x_i\) days (day \(t\) inclusive), then it is in maintenance for \(y_i\) days, then in work for \(x_i\) days again, and so on until it is removed.In order to make management easier, Nitori wants you to help her calculate how many trains are in maintenance in each day.On a day a train is removed, it is not counted as in maintenance.
|
The first line contains two integers \(n\), \(m\) (\(1 \le n,m \le 2 \cdot 10^5\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i,y_i\) (\(1 \le x_i,y_i \le 10^9\)).Each of the next \(m\) lines contains two integers \(op\), \(k\) (\(1 \le k \le n\), \(op = 1\) or \(op = 2\)). If \(op=1\), it means this day's a train of model \(k\) is added, otherwise the train of model \(k\) is removed. It is guaranteed that when a train of model \(x\) is added, there is no train of the same model in the department, and when a train of model \(x\) is removed, there is such a train in the department.
|
Print \(m\) lines, The \(i\)-th of these lines contains one integers, denoting the number of trains in maintenance in the \(i\)-th day.
|
Consider the first example:The first day: Nitori adds a train of model \(3\). Only a train of model \(3\) is running and no train is in maintenance.The second day: Nitori adds a train of model \(1\). A train of model \(1\) is running and a train of model \(3\) is in maintenance.The third day: Nitori removes a train of model \(1\). The situation is the same as the first day.The fourth day: Nitori removes a train of model \(3\). There are no trains at all.
|
Input: 3 4 10 15 12 10 1 1 1 3 1 1 2 1 2 3 | Output: 0 1 0 0
|
Hard
| 3 | 746 | 607 | 135 | 15 |
278 |
B
|
278B
|
B. New Problem
| 1,500 |
brute force; strings
|
Coming up with a new problem isn't as easy as many people think. Sometimes it is hard enough to name it. We'll consider a title original if it doesn't occur as a substring in any titles of recent Codeforces problems. You've got the titles of n last problems β the strings, consisting of lowercase English letters. Your task is to find the shortest original title for the new problem. If there are multiple such titles, choose the lexicographically minimum one. Note, that title of the problem can't be an empty string.A substring s[l... r] (1 β€ l β€ r β€ |s|) of string s = s1s2... s|s| (where |s| is the length of string s) is string slsl + 1... sr.String x = x1x2... xp is lexicographically smaller than string y = y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or there exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1. The string characters are compared by their ASCII codes.
|
The first line contains integer n (1 β€ n β€ 30) β the number of titles you've got to consider. Then follow n problem titles, one per line. Each title only consists of lowercase English letters (specifically, it doesn't contain any spaces) and has the length from 1 to 20, inclusive.
|
Print a string, consisting of lowercase English letters β the lexicographically minimum shortest original title.
|
In the first sample the first 9 letters of the English alphabet (a, b, c, d, e, f, g, h, i) occur in the problem titles, so the answer is letter j.In the second sample the titles contain 26 English letters, so the shortest original title cannot have length 1. Title aa occurs as a substring in the first title.
|
Input: 5threehorsesgoodsubstringssecretprimematrixbeautifulyear | Output: j
|
Medium
| 2 | 940 | 281 | 112 | 2 |
1,438 |
A
|
1438A
|
A. Specific Tastes of Andre
| 800 |
constructive algorithms; implementation
|
Andre has very specific tastes. Recently he started falling in love with arrays.Andre calls an nonempty array \(b\) good, if sum of its elements is divisible by the length of this array. For example, array \([2, 3, 1]\) is good, as sum of its elements β \(6\) β is divisible by \(3\), but array \([1, 1, 2, 3]\) isn't good, as \(7\) isn't divisible by \(4\). Andre calls an array \(a\) of length \(n\) perfect if the following conditions hold: Every nonempty subarray of this array is good. For every \(i\) (\(1 \le i \le n\)), \(1 \leq a_i \leq 100\). Given a positive integer \(n\), output any perfect array of length \(n\). We can show that for the given constraints such an array always exists.An array \(c\) is a subarray of an array \(d\) if \(c\) can be obtained from \(d\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first and only line of every test case contains a single integer \(n\) (\(1 \le n \le 100\)).
|
For every test, output any perfect array of length \(n\) on a separate line.
|
Array \([19, 33]\) is perfect as all \(3\) its subarrays: \([19]\), \([33]\), \([19, 33]\), have sums divisible by their lengths, and therefore are good.
|
Input: 3 1 2 4 | Output: 24 19 33 7 37 79 49
|
Beginner
| 2 | 914 | 253 | 76 | 14 |
401 |
B
|
401B
|
B. Sereja and Contests
| 1,200 |
greedy; implementation; math
|
Sereja is a coder and he likes to take part in Codesorfes rounds. However, Uzhland doesn't have good internet connection, so Sereja sometimes skips rounds.Codesorfes has rounds of two types: Div1 (for advanced coders) and Div2 (for beginner coders). Two rounds, Div1 and Div2, can go simultaneously, (Div1 round cannot be held without Div2) in all other cases the rounds don't overlap in time. Each round has a unique identifier β a positive integer. The rounds are sequentially (without gaps) numbered with identifiers by the starting time of the round. The identifiers of rounds that are run simultaneously are different by one, also the identifier of the Div1 round is always greater.Sereja is a beginner coder, so he can take part only in rounds of Div2 type. At the moment he is taking part in a Div2 round, its identifier equals to x. Sereja remembers very well that he has taken part in exactly k rounds before this round. Also, he remembers all identifiers of the rounds he has taken part in and all identifiers of the rounds that went simultaneously with them. Sereja doesn't remember anything about the rounds he missed.Sereja is wondering: what minimum and what maximum number of Div2 rounds could he have missed? Help him find these two numbers.
|
The first line contains two integers: x (1 β€ x β€ 4000) β the round Sereja is taking part in today, and k (0 β€ k < 4000) β the number of rounds he took part in.Next k lines contain the descriptions of the rounds that Sereja took part in before. If Sereja took part in one of two simultaneous rounds, the corresponding line looks like: ""1 num2 num1"" (where num2 is the identifier of this Div2 round, num1 is the identifier of the Div1 round). It is guaranteed that num1 - num2 = 1. If Sereja took part in a usual Div2 round, then the corresponding line looks like: ""2 num"" (where num is the identifier of this Div2 round). It is guaranteed that the identifiers of all given rounds are less than x.
|
Print in a single line two integers β the minimum and the maximum number of rounds that Sereja could have missed.
|
In the second sample we have unused identifiers of rounds 1, 6, 7. The minimum number of rounds Sereja could have missed equals to 2. In this case, the round with the identifier 1 will be a usual Div2 round and the round with identifier 6 will be synchronous with the Div1 round. The maximum number of rounds equals 3. In this case all unused identifiers belong to usual Div2 rounds.
|
Input: 3 22 12 2 | Output: 0 0
|
Easy
| 3 | 1,257 | 699 | 113 | 4 |
1,851 |
F
|
1851F
|
F. Lisa and the Martians
| 1,800 |
bitmasks; greedy; math; strings; trees
|
Lisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let's call integer martian if it is a non-negative integer and strictly less than \(2^k\), for example, when \(k = 12\), the numbers \(51\), \(1960\), \(0\) are martian, and the numbers \(\pi\), \(-1\), \(\frac{21}{8}\), \(4096\) are not.The aliens will give Lisa \(n\) martian numbers \(a_1, a_2, \ldots, a_n\). Then they will ask her to name any martian number \(x\). After that, Lisa will select a pair of numbers \(a_i, a_j\) (\(i \neq j\)) in the given sequence and count \((a_i \oplus x) \& (a_j \oplus x)\). The operation \(\oplus\) means Bitwise exclusive OR, the operation \(\&\) means Bitwise And. For example, \((5 \oplus 17) \& (23 \oplus 17) = (00101_2 \oplus 10001_2) \& (10111_2 \oplus 10001_2) = 10100_2 \& 00110_2 = 00100_2 = 4\).Lisa is sure that the higher the calculated value, the higher her chances of returning home. Help the girl choose such \(i, j, x\) that maximize the calculated value.
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β number of testcases.Each testcase is described by two lines.The first line contains integers \(n, k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le k \le 30\)) β the length of the sequence of martian numbers and the value of \(k\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < 2^k\)) β a sequence of martian numbers.It is guaranteed that the sum of \(n\) over all testcases does not exceed \(2 \cdot 10^5\).
|
For each testcase, print three integers \(i, j, x\) (\(1 \le i, j \le n\), \(i \neq j\), \(0 \le x < 2^k\)). The value of \((a_i \oplus x) \& (a_j \oplus x)\) should be the maximum possible.If there are several solutions, you can print any one.
|
First testcase: \((3 \oplus 14) \& (1 \oplus 14) = (0011_2 \oplus 1110_2) \& (0001_2 \oplus 1110_2) = 1101_2 = 1101_2 \& 1111_2 = 1101_2 = 13\).Second testcase: \((1 \oplus 0) \& (1 \oplus 0) = 1\).Third testcase: \((9 \oplus 4082) \& (13 \oplus 4082) = 4091\).Fourth testcase: \((3 \oplus 7) \& (0 \oplus 7) = 4\).
|
Input: 105 43 9 1 4 133 11 0 16 12144 1580 1024 100 9 134 37 3 0 43 20 0 12 412 29 46 14 9 4 4 4 5 10 22 11 02 411 49 42 11 10 1 6 9 11 0 5 | Output: 1 3 14 1 3 0 5 6 4082 2 3 7 1 2 3 1 2 15 4 5 11 1 2 0 1 2 0 2 7 4
|
Medium
| 5 | 1,040 | 501 | 244 | 18 |
1,344 |
B
|
1344B
|
B. Monopole Magnets
| 2,000 |
constructive algorithms; dfs and similar; dsu; graphs
|
A monopole magnet is a magnet that only has one pole, either north or south. They don't actually exist since real magnets have two poles, but this is a programming contest problem, so we don't care.There is an \(n\times m\) grid. Initially, you may place some north magnets and some south magnets into the cells. You are allowed to place as many magnets as you like, even multiple in the same cell.An operation is performed as follows. Choose a north magnet and a south magnet to activate. If they are in the same row or the same column and they occupy different cells, then the north magnet moves one unit closer to the south magnet. Otherwise, if they occupy the same cell or do not share a row or column, then nothing changes. Note that the south magnets are immovable.Each cell of the grid is colored black or white. Let's consider ways to place magnets in the cells so that the following conditions are met. There is at least one south magnet in every row and every column. If a cell is colored black, then it is possible for a north magnet to occupy this cell after some sequence of operations from the initial placement. If a cell is colored white, then it is impossible for a north magnet to occupy this cell after some sequence of operations from the initial placement. Determine if it is possible to place magnets such that these conditions are met. If it is possible, find the minimum number of north magnets required (there are no requirements on the number of south magnets).
|
The first line contains two integers \(n\) and \(m\) (\(1\le n,m\le 1000\)) β the number of rows and the number of columns, respectively.The next \(n\) lines describe the coloring. The \(i\)-th of these lines contains a string of length \(m\), where the \(j\)-th character denotes the color of the cell in row \(i\) and column \(j\). The characters ""#"" and ""."" represent black and white, respectively. It is guaranteed, that the string will not contain any other characters.
|
Output a single integer, the minimum possible number of north magnets required.If there is no placement of magnets that satisfies all conditions, print a single integer \(-1\).
|
In the first test, here is an example placement of magnets: In the second test, we can show that no required placement of magnets exists. Here are three example placements that fail to meet the requirements. The first example violates rule \(3\) since we can move the north magnet down onto a white square. The second example violates rule \(2\) since we cannot move the north magnet to the bottom-left black square by any sequence of operations. The third example violates rule \(1\) since there is no south magnet in the first column. In the third test, here is an example placement of magnets. We can show that there is no required placement of magnets with fewer north magnets. In the fourth test, we can show that no required placement of magnets exists. Here are two example placements that fail to meet the requirements. The first example violates rule \(1\) since there is no south magnet in the first row. The second example violates rules \(1\) and \(3\) since there is no south magnet in the second row and we can move the north magnet up one unit onto a white square. In the fifth test, we can put the south magnet in each cell and no north magnets. Because there are no black cells, it will be a correct placement.
|
Input: 3 3 .#. ### ##. | Output: 1
|
Hard
| 4 | 1,488 | 478 | 176 | 13 |
1,179 |
D
|
1179D
|
D. Fedor Runs for President
| 2,700 |
data structures; dp; trees
|
Fedor runs for president of Byteland! In the debates, he will be asked how to solve Byteland's transport problem. It's a really hard problem because of Byteland's transport system is now a tree (connected graph without cycles). Fedor's team has found out in the ministry of transport of Byteland that there is money in the budget only for one additional road. In the debates, he is going to say that he will build this road as a way to maximize the number of distinct simple paths in the country. A simple path is a path which goes through every vertex no more than once. Two simple paths are named distinct if sets of their edges are distinct. But Byteland's science is deteriorated, so Fedor's team hasn't succeeded to find any scientists to answer how many distinct simple paths they can achieve after adding exactly one edge on the transport system?Help Fedor to solve it.An edge can be added between vertices that are already connected, but it can't be a loop.In this problem, we consider only simple paths of length at least two.
|
The first line contains one integer \(n\) (\(2 \leq n \leq 500\ 000\)) β number of vertices in Byteland's transport system.Each of the following \(n - 1\) lines contains two integers \(v_i\) and \(u_i\) (\(1 \leq v_i, u_i \leq n\)). It's guaranteed that the graph is tree.
|
Print exactly one integer β a maximal number of simple paths that can be achieved after adding one edge.
|
Input: 2 1 2 | Output: 2
|
Master
| 3 | 1,035 | 272 | 104 | 11 |
|
566 |
G
|
566G
|
G. Max and Min
| 2,500 |
geometry
|
Two kittens, Max and Min, play with a pair of non-negative integers x and y. As you can guess from their names, kitten Max loves to maximize and kitten Min loves to minimize. As part of this game Min wants to make sure that both numbers, x and y became negative at the same time, and kitten Max tries to prevent him from doing so.Each kitten has a set of pairs of integers available to it. Kitten Max has n pairs of non-negative integers (ai, bi) (1 β€ i β€ n), and kitten Min has m pairs of non-negative integers (cj, dj) (1 β€ j β€ m). As kitten Max makes a move, it can take any available pair (ai, bi) and add ai to x and bi to y, and kitten Min can take any available pair (cj, dj) and subtract cj from x and dj from y. Each kitten can use each pair multiple times during distinct moves.Max moves first. Kitten Min is winning if at some moment both numbers a, b are negative simultaneously. Otherwise, the winner of the game is kitten Max. Determine which kitten wins if both of them play optimally.
|
The first line contains two integers, n and m (1 β€ n, m β€ 100 000) β the number of pairs of numbers available to Max and Min, correspondingly.The second line contains two integers x, y (1 β€ x, y β€ 109) β the initial values of numbers with which the kittens are playing.Next n lines contain the pairs of numbers ai, bi (1 β€ ai, bi β€ 109) β the pairs available to Max.The last m lines contain pairs of numbers cj, dj (1 β€ cj, dj β€ 109) β the pairs available to Min.
|
Print Β«MaxΒ» (without the quotes), if kitten Max wins, or ""Min"" (without the quotes), if kitten Min wins.
|
In the first test from the statement Min can respond to move (2, 3) by move (3, 10), and to move (3, 2) by move (10, 3). Thus, for each pair of Max and Min's moves the values of both numbers x and y will strictly decrease, ergo, Min will win sooner or later.In the second sample test after each pair of Max and Min's moves both numbers x and y only increase, thus none of them will become negative.
|
Input: 2 242 432 33 23 1010 3 | Output: Min
|
Expert
| 1 | 1,000 | 463 | 106 | 5 |
1,151 |
D
|
1151D
|
D. Stas and the Queue at the Buffet
| 1,600 |
greedy; math; sortings
|
During a break in the buffet of the scientific lyceum of the Kingdom of Kremland, there was formed a queue of \(n\) high school students numbered from \(1\) to \(n\). Initially, each student \(i\) is on position \(i\). Each student \(i\) is characterized by two numbers β \(a_i\) and \(b_i\). Dissatisfaction of the person \(i\) equals the product of \(a_i\) by the number of people standing to the left of his position, add the product \(b_i\) by the number of people standing to the right of his position. Formally, the dissatisfaction of the student \(i\), which is on the position \(j\), equals \(a_i \cdot (j-1) + b_i \cdot (n-j)\).The director entrusted Stas with the task: rearrange the people in the queue so that minimize the total dissatisfaction.Although Stas is able to solve such problems, this was not given to him. He turned for help to you.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of people in the queue.Each of the following \(n\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq 10^8\)) β the characteristic of the student \(i\), initially on the position \(i\).
|
Output one integer β minimum total dissatisfaction which can be achieved by rearranging people in the queue.
|
In the first example it is optimal to put people in this order: (\(3, 1, 2\)). The first person is in the position of \(2\), then his dissatisfaction will be equal to \(4 \cdot 1+2 \cdot 1=6\). The second person is in the position of \(3\), his dissatisfaction will be equal to \(2 \cdot 2+3 \cdot 0=4\). The third person is in the position of \(1\), his dissatisfaction will be equal to \(6 \cdot 0+1 \cdot 2=2\). The total dissatisfaction will be \(12\).In the second example, you need to put people in this order: (\(3, 2, 4, 1\)). The total dissatisfaction will be \(25\).
|
Input: 3 4 2 2 3 6 1 | Output: 12
|
Medium
| 3 | 856 | 291 | 108 | 11 |
1,284 |
A
|
1284A
|
A. New Year and Naming
| 800 |
implementation; strings
|
Happy new year! The year 2020 is also known as Year Gyeongja (κ²½μλ
, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years.There are two sequences of \(n\) strings \(s_1, s_2, s_3, \ldots, s_{n}\) and \(m\) strings \(t_1, t_2, t_3, \ldots, t_{m}\). These strings contain only lowercase letters. There might be duplicates among these strings.Let's call a concatenation of strings \(x\) and \(y\) as the string that is obtained by writing down strings \(x\) and \(y\) one right after another without changing the order. For example, the concatenation of the strings ""code"" and ""forces"" is the string ""codeforces"".The year 1 has a name which is the concatenation of the two strings \(s_1\) and \(t_1\). When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence.For example, if \(n = 3, m = 4, s = \){""a"", ""b"", ""c""}, \(t =\) {""d"", ""e"", ""f"", ""g""}, the following table denotes the resulting year names. Note that the names of the years may repeat. You are given two sequences of strings of size \(n\) and \(m\) and also \(q\) queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system?
|
The first line contains two integers \(n, m\) (\(1 \le n, m \le 20\)).The next line contains \(n\) strings \(s_1, s_2, \ldots, s_{n}\). Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least \(1\) and at most \(10\).The next line contains \(m\) strings \(t_1, t_2, \ldots, t_{m}\). Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least \(1\) and at most \(10\).Among the given \(n + m\) strings may be duplicates (that is, they are not necessarily all different).The next line contains a single integer \(q\) (\(1 \le q \le 2\,020\)).In the next \(q\) lines, an integer \(y\) (\(1 \le y \le 10^9\)) is given, denoting the year we want to know the name for.
|
Print \(q\) lines. For each line, print the name of the year as per the rule described above.
|
The first example denotes the actual names used in the Gapja system. These strings usually are either a number or the name of some animal.
|
Input: 10 12 sin im gye gap eul byeong jeong mu gi gyeong yu sul hae ja chuk in myo jin sa o mi sin 14 1 2 3 4 10 11 12 13 73 2016 2017 2018 2019 2020 | Output: sinyu imsul gyehae gapja gyeongo sinmi imsin gyeyu gyeyu byeongsin jeongyu musul gihae gyeongja
|
Beginner
| 2 | 1,463 | 781 | 93 | 12 |
1,215 |
A
|
1215A
|
A. Yellow Cards
| 1,000 |
greedy; implementation; math
|
The final match of the Berland Football Cup has been held recently. The referee has shown \(n\) yellow cards throughout the match. At the beginning of the match there were \(a_1\) players in the first team and \(a_2\) players in the second team.The rules of sending players off the game are a bit different in Berland football. If a player from the first team receives \(k_1\) yellow cards throughout the match, he can no longer participate in the match β he's sent off. And if a player from the second team receives \(k_2\) yellow cards, he's sent off. After a player leaves the match, he can no longer receive any yellow cards. Each of \(n\) yellow cards was shown to exactly one player. Even if all players from one team (or even from both teams) leave the match, the game still continues.The referee has lost his records on who has received each yellow card. Help him to determine the minimum and the maximum number of players that could have been thrown out of the game.
|
The first line contains one integer \(a_1\) \((1 \le a_1 \le 1\,000)\) β the number of players in the first team.The second line contains one integer \(a_2\) \((1 \le a_2 \le 1\,000)\) β the number of players in the second team.The third line contains one integer \(k_1\) \((1 \le k_1 \le 1\,000)\) β the maximum number of yellow cards a player from the first team can receive (after receiving that many yellow cards, he leaves the game).The fourth line contains one integer \(k_2\) \((1 \le k_2 \le 1\,000)\) β the maximum number of yellow cards a player from the second team can receive (after receiving that many yellow cards, he leaves the game).The fifth line contains one integer \(n\) \((1 \le n \le a_1 \cdot k_1 + a_2 \cdot k_2)\) β the number of yellow cards that have been shown during the match.
|
Print two integers β the minimum and the maximum number of players that could have been thrown out of the game.
|
In the first example it could be possible that no player left the game, so the first number in the output is \(0\). The maximum possible number of players that could have been forced to leave the game is \(4\) β one player from the first team, and three players from the second.In the second example the maximum possible number of yellow cards has been shown \((3 \cdot 6 + 1 \cdot 7 = 25)\), so in any case all players were sent off.
|
Input: 2 3 5 1 8 | Output: 0 4
|
Beginner
| 3 | 975 | 807 | 111 | 12 |
2,073 |
M
|
2073M
| 3,300 |
Master
| 0 | 0 | 0 | 0 | 20 |
|||||||
1,697 |
F
|
1697F
|
F. Too Many Constraints
| 2,800 |
2-sat; constructive algorithms; graphs; implementation
|
You are asked to build an array \(a\), consisting of \(n\) integers, each element should be from \(1\) to \(k\).The array should be non-decreasing (\(a_i \le a_{i+1}\) for all \(i\) from \(1\) to \(n-1\)). You are also given additional constraints on it. Each constraint is of one of three following types: \(1~i~x\): \(a_i\) should not be equal to \(x\); \(2~i~j~x\): \(a_i + a_j\) should be less than or equal to \(x\); \(3~i~j~x\): \(a_i + a_j\) should be greater than or equal to \(x\). Build any non-decreasing array that satisfies all constraints or report that no such array exists.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains three integers \(n, m\) and \(k\) (\(2 \le n \le 2 \cdot 10^4\); \(0 \le m \le 2 \cdot 10^4\); \(2 \le k \le 10\)).The \(i\)-th of the next \(m\) lines contains a description of a constraint. Each constraint is of one of three following types: \(1~i~x\) (\(1 \le i \le n\); \(1 \le x \le k\)): \(a_i\) should not be equal to \(x\); \(2~i~j~x\) (\(1 \le i < j \le n\); \(2 \le x \le 2 \cdot k\)): \(a_i + a_j\) should be less than or equal to \(x\); \(3~i~j~x\) (\(1 \le i < j \le n\); \(2 \le x \le 2 \cdot k\)): \(a_i + a_j\) should be greater than or equal to \(x\). The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^4\). The sum of \(m\) over all testcases doesn't exceed \(2 \cdot 10^4\).
|
For each testcase, determine if there exists a non-decreasing array that satisfies all conditions. If there is no such array, then print -1. Otherwise, print any valid array β \(n\) integers from \(1\) to \(k\).
|
Input: 44 0 42 2 33 1 2 31 2 23 3 21 1 12 2 3 23 2 3 25 5 53 2 5 72 4 5 103 4 5 63 3 4 72 1 5 7 | Output: 1 2 3 4 1 3 -1 1 2 2 5 5
|
Master
| 4 | 589 | 843 | 211 | 16 |
|
1,605 |
D
|
1605D
|
D. Treelabeling
| 2,100 |
bitmasks; constructive algorithms; dfs and similar; games; greedy; implementation; trees
|
Eikooc and Sushi play a game.The game is played on a tree having \(n\) nodes numbered \(1\) to \(n\). Recall that a tree having \(n\) nodes is an undirected, connected graph with \(n-1\) edges.They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choice. Sushi makes the next move, followed by Eikooc, followed by Sushi, and so on. In each turn after the first, a player must move the token to a node \(u\) such that \(u\) is adjacent to the node \(v\) the token is currently on \(u\) has not been visited before \(u \oplus v \leq min(u, v)\) Here \(x \oplus y\) denotes the bitwise XOR operation on integers \(x\) and \(y\).Both the players play optimally. The player who is unable to make a move loses.The following are examples which demonstrate the rules of the game. Suppose Eikooc starts the game by placing the token at node \(4\). Sushi then moves the token to node \(6\), which is unvisited and adjacent to \(4\). It also holds that \(6 \oplus 4 = 2 \leq min(6, 4)\). In the next turn, Eikooc moves the token to node \(5\), which is unvisited and adjacent to \(6\). It holds that \(5 \oplus 6 = 3 \leq min(5, 6)\). Sushi has no more moves to play, so she loses. Suppose Eikooc starts the game by placing the token at node \(3\). Sushi moves the token to node \(2\), which is unvisited and adjacent to \(3\). It also holds that \(3 \oplus 2 = 1 \leq min(3, 2)\). Eikooc cannot move the token to node \(6\) since \(6 \oplus 2 = 4 \nleq min(6, 2)\). Since Eikooc has no moves to play, she loses. Before the game begins, Eikooc decides to sneakily relabel the nodes of the tree in her favour. Formally, a relabeling is a permutation \(p\) of length \(n\) (sequence of \(n\) integers wherein each integer from \(1\) to \(n\) occurs exactly once) where \(p_i\) denotes the new numbering of node \(i\).She wants to maximize the number of nodes she can choose in the first turn which will guarantee her a win. Help Eikooc find any relabeling which will help her do so.
|
The first line contains a single integer \(t~(1 \le t \le 10^5)\) β the number of test cases. The description of each test case is as follows.The first line of each test case contains an integer \(n~(1 \le n \le 2 \cdot 10^5)\) β the number of nodes in the tree.The next \(n-1\) lines contain two integers \(u\) and \(v\) \((1 \le u, v \le n; u \neq v)\) β denoting an edge between nodes \(u\) and \(v\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case print any suitable relabeling β a permutation of length \(n\) which maximizes the number of nodes that can be chosen in the first turn that guarantee a win for Eikooc. If there are multiple such relabelings, you may print any of them.
|
In the first test case, Eikooc has only one choice. Sushi will have no moves to play after Eikooc chooses this node and Eikooc will win.In the second test case, \(1 \oplus 2 = 3 \nleq min(1, 2)\). Hence, after Eikooc picks either of the nodes, Sushi will have no moves to play and Eikooc will win. Both \(\{1, 2\}\) and \(\{2, 1\}\) are optimal relabelings.
|
Input: 3 1 2 1 2 3 1 2 1 3 | Output: 1 2 1 1 2 3
|
Hard
| 7 | 2,037 | 496 | 253 | 16 |
1,519 |
A
|
1519A
|
A. Red and Blue Beans
| 800 |
math
|
You have \(r\) red and \(b\) blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: has at least one red bean (or the number of red beans \(r_i \ge 1\)); has at least one blue bean (or the number of blue beans \(b_i \ge 1\)); the number of red and blue beans should differ in no more than \(d\) (or \(|r_i - b_i| \le d\)) Can you distribute all beans?
|
The first line contains the single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first and only line of each test case contains three integers \(r\), \(b\), and \(d\) (\(1 \le r, b \le 10^9\); \(0 \le d \le 10^9\)) β the number of red and blue beans and the maximum absolute difference in each packet.
|
For each test case, if you can distribute all beans, print YES. Otherwise, print NO.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
|
In the first test case, you can form one packet with \(1\) red and \(1\) blue bean. The absolute difference \(|1 - 1| = 0 \le d\).In the second test case, you can form two packets: \(1\) red and \(4\) blue beans in the first packet and \(1\) red and \(3\) blue beans in the second one.In the third test case, since \(b = 1\), you can form only one packet with \(6\) red and \(1\) blue beans. The absolute difference \(|6 - 1| = 5 > d\).In the fourth test case, since \(d = 0\) so each packet should contain the same number of red and blue beans, but \(r \neq b\).
|
Input: 4 1 1 0 2 7 3 6 1 4 5 4 0 | Output: YES YES NO NO
|
Beginner
| 1 | 405 | 323 | 223 | 15 |
1,988 |
C
|
1988C
|
C. Increasing Sequence with Fixed OR
| 1,300 |
bitmasks; constructive algorithms; greedy
|
You are given a positive integer \(n\). Find the longest sequence of positive integers \(a=[a_1,a_2,\ldots,a_k]\) that satisfies the following conditions, and print the sequence: \(a_i\le n\) for all \(1\le i\le k\). \(a\) is strictly increasing. That is, \(a_i>a_{i-1}\) for all \(2\le i\le k\). \(a_i\,|\,a_{i-1}=n\) for all \(2\le i\le k\), where \(|\) denotes the bitwise OR operation.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The only line of each test case contains one integer \(n\) (\(1\le n\le 10^{18}\)).It's guaranteed that the sum of lengths of the longest valid sequences does not exceed \(5\cdot 10^5\).
|
For each testcase, print two lines. In the first line, print the length of your constructed sequence, \(k\). In the second line, print \(k\) positive integers, denoting the sequence. If there are multiple longest sequences, you can print any of them.
|
Input: 4131423 | Output: 1 1 3 1 2 3 4 4 10 12 14 5 7 18 21 22 23
|
Easy
| 3 | 389 | 343 | 250 | 19 |
|
1,709 |
A
|
1709A
|
A. Three Doors
| 800 |
brute force; greedy; implementation; math
|
There are three doors in front of you, numbered from \(1\) to \(3\) from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.There are three keys β one for each door. Two of them are hidden behind the doors, so that there is no more than one key behind each door. So two doors have one key behind them, one door doesn't have a key behind it. To obtain a key hidden behind a door, you should first unlock that door. The remaining key is in your hands.Can you open all the doors?
|
The first line contains a single integer \(t\) (\(1 \le t \le 18\)) β the number of testcases.The first line of each testcase contains a single integer \(x\) (\(1 \le x \le 3\)) β the number on the key in your hands.The second line contains three integers \(a, b\) and \(c\) (\(0 \le a, b, c \le 3\)) β the number on the key behind each of the doors. If there is no key behind the door, the number is equal to \(0\).Values \(1, 2\) and \(3\) appear exactly once among \(x, a, b\) and \(c\).
|
For each testcase, print ""YES"" if you can open all the doors. Otherwise, print ""NO"".
|
Input: 430 1 210 3 223 1 021 3 0 | Output: YES NO YES NO
|
Beginner
| 4 | 557 | 490 | 88 | 17 |
|
1,404 |
E
|
1404E
|
E. Bricks
| 2,800 |
flows; graph matchings; graphs
|
A brick is defined as a rectangle with integer side lengths with either width \(1\) or height \(1\) (or both).There is an \(n\times m\) grid, and each cell is colored either black or white. A tiling is a way to place bricks onto the grid such that each black cell is covered by exactly one brick, and each white cell is not covered by any brick. In other words, bricks are placed on black cells only, cover all black cells, and no two bricks overlap. An example tiling of the first test case using \(5\) bricks. It is possible to do better, using only \(4\) bricks. What is the minimum number of bricks required to make a valid tiling?
|
The first line contains two integers \(n\), \(m\) (\(1\le n, m\le 200\)) β the number of rows and columns, respectively.The next \(n\) lines describe the grid. The \(i\)-th of these lines contains a string of length \(m\), where the \(j\)-th character denotes the color of the cell in row \(i\), column \(j\). A black cell is given by ""#"", and a white cell is given by ""."".It is guaranteed that there is at least one black cell.
|
Output a single integer, the minimum number of bricks required.
|
The first test case can be tiled with \(4\) bricks placed vertically.The third test case can be tiled with \(18\) bricks like this:
|
Input: 3 4 #.## #### ##.. | Output: 4
|
Master
| 3 | 635 | 432 | 63 | 14 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.