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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,624 |
G
|
1624G
|
G. MinOr Tree
| 1,900 |
bitmasks; dfs and similar; dsu; graphs; greedy
|
Recently, Vlad has been carried away by spanning trees, so his friends, without hesitation, gave him a connected weighted undirected graph of \(n\) vertices and \(m\) edges for his birthday.Vlad defined the ority of a spanning tree as the bitwise OR of all its weights, and now he is interested in what is the minimum possible ority that can be achieved by choosing a certain spanning tree. A spanning tree is a connected subgraph of a given graph that does not contain cycles.In other words, you want to keep \(n-1\) edges so that the graph remains connected and the bitwise OR weights of the edges are as small as possible. You have to find the minimum bitwise OR itself.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases in the input.An empty line is written in front of each test case.This is followed by two numbers \(n\) and \(m\) (\(3 \le n \le 2 \cdot 10^5, n - 1 \le m \le 2 \cdot 10^5\)) — the number of vertices and edges of the graph, respectively.The next \(m\) lines contain the description of the edges. Line \(i\) contains three numbers \(v_i\), \(u_i\) and \(w_i\) (\(1 \le v_i, u_i \le n\), \(1 \le w_i \le 10^9\), \(v_i \neq u_i\)) — the vertices that the edge connects and its weight.It is guaranteed that the sum \(m\) and the sum \(n\) over all test cases does not exceed \(2 \cdot 10^5\) and each test case contains a connected graph.
|
Print \(t\) lines, each of which contains the answer to the corresponding set of input data — the minimum possible spanning tree ority.
|
Graph from the first test case. Ority of this tree equals to 2 or 2 = 2 and it's minimal. Without excluding edge with weight \(1\) ority is 1 or 2 = 3.
|
Input: 33 31 2 12 3 21 3 25 74 2 72 5 83 4 23 2 12 4 24 1 21 2 23 41 2 12 3 21 3 33 1 4 | Output: 2 10 3
|
Hard
| 5 | 673 | 737 | 135 | 16 |
1,620 |
C
|
1620C
|
C. BA-String
| 1,800 |
brute force; dp; greedy; implementation; math
|
You are given an integer \(k\) and a string \(s\) that consists only of characters 'a' (a lowercase Latin letter) and '*' (an asterisk).Each asterisk should be replaced with several (from \(0\) to \(k\) inclusive) lowercase Latin letters 'b'. Different asterisk can be replaced with different counts of letter 'b'.The result of the replacement is called a BA-string.Two strings \(a\) and \(b\) are different if they either have different lengths or there exists such a position \(i\) that \(a_i \neq b_i\).A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\). Now consider all different BA-strings and find the \(x\)-th lexicographically smallest of them.
|
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\), \(k\) and \(x\) (\(1 \le n \le 2000\); \(0 \le k \le 2000\); \(1 \le x \le 10^{18}\)). \(n\) is the length of string \(s\).The second line of each testcase is a string \(s\). It consists of \(n\) characters, each of them is either 'a' (a lowercase Latin letter) or '*' (an asterisk).The sum of \(n\) over all testcases doesn't exceed \(2000\). For each testcase \(x\) doesn't exceed the total number of different BA-strings. String \(s\) contains at least one character 'a'.
|
For each testcase, print a single string, consisting only of characters 'b' and 'a' (lowercase Latin letters) — the \(x\)-th lexicographically smallest BA-string.
|
In the first testcase of the example, BA-strings ordered lexicographically are: a ab abb abbb abbbb In the second testcase of the example, BA-strings ordered lexicographically are: aa aba abba Note that string ""aba"" is only counted once, even though there are two ways to replace asterisks with characters 'b' to get it.
|
Input: 3 2 4 3 a* 4 1 3 a**a 6 3 20 **a*** | Output: abb abba babbbbbbbbb
|
Medium
| 5 | 912 | 633 | 162 | 16 |
154 |
D
|
154D
|
D. Flatland Fencing
| 2,400 |
games; math
|
The King of Flatland will organize a knights' tournament! The winner will get half the kingdom and the favor of the princess of legendary beauty and wisdom. The final test of the applicants' courage and strength will be a fencing tournament. The tournament is held by the following rules: the participants fight one on one, the winner (or rather, the survivor) transfers to the next round.Before the battle both participants stand at the specified points on the Ox axis with integer coordinates. Then they make moves in turn. The first participant moves first, naturally. During a move, the first participant can transfer from the point x to any integer point of the interval [x + a; x + b]. The second participant can transfer during a move to any integer point of the interval [x - b; x - a]. That is, the options for the players' moves are symmetric (note that the numbers a and b are not required to be positive, and if a ≤ 0 ≤ b, then staying in one place is a correct move). At any time the participants can be located arbitrarily relative to each other, that is, it is allowed to ""jump"" over the enemy in any direction. A participant wins if he uses his move to transfer to the point where his opponent is.Of course, the princess has already chosen a husband and now she wants to make her sweetheart win the tournament. He has already reached the tournament finals and he is facing the last battle. The princess asks the tournament manager to arrange the tournament finalists in such a way that her sweetheart wins the tournament, considering that both players play optimally. However, the initial location of the participants has already been announced, and we can only pull some strings and determine which participant will be first and which one will be second. But how do we know which participant can secure the victory? Alas, the princess is not learned in the military affairs... Therefore, she asks you to determine how the battle will end considering that both opponents play optimally. Also, if the first player wins, your task is to determine his winning move.
|
The first line contains four space-separated integers — x1, x2, a and b (x1 ≠ x2, a ≤ b, - 109 ≤ x1, x2, a, b ≤ 109) — coordinates of the points where the first and the second participant start, and the numbers that determine the players' moves, correspondingly.
|
On the first line print the outcome of the battle as ""FIRST"" (without the quotes), if both players play optimally and the first player wins. Print ""SECOND"" (without the quotes) if the second player wins and print ""DRAW"" (without the quotes), if nobody is able to secure the victory.If the first player wins, print on the next line the single integer x — the coordinate of the point where the first player should transfer to win. The indicated move should be valid, that is, it should meet the following condition: x1 + a ≤ x ≤ x1 + b. If there are several winning moves, print any of them. If the first participant can't secure the victory, then you do not have to print anything.
|
In the first sample the first player can win in one move.In the second sample the first participant must go to point 1, where the second participant immediately goes and wins. In the third sample changing the position isn't profitable to either participant, so nobody wins.
|
Input: 0 2 0 4 | Output: FIRST2
|
Expert
| 2 | 2,080 | 262 | 686 | 1 |
1,425 |
D
|
1425D
|
D. Danger of Mad Snakes
| 2,300 |
combinatorics; dp; math
|
Mr. Chanek The Ninja is one day tasked with a mission to handle mad snakes that are attacking a site. Now, Mr. Chanek already arrived at the hills where the destination is right below these hills. The mission area can be divided into a grid of size \(1000 \times 1000\) squares. There are \(N\) mad snakes on the site, the i'th mad snake is located on square \((X_i, Y_i)\) and has a danger level \(B_i\).Mr. Chanek is going to use the Shadow Clone Jutsu and Rasengan that he learned from Lord Seventh to complete this mission. His attack strategy is as follows: Mr. Chanek is going to make \(M\) clones. Each clone will choose a mad snake as the attack target. Each clone must pick a different mad snake to attack. All clones jump off the hills and attack their respective chosen target at once with Rasengan of radius \(R\). If the mad snake at square \((X, Y)\) is attacked with a direct Rasengan, it and all mad snakes at squares \((X', Y')\) where \(max(|X' - X|, |Y' - Y|) \le R\) will die. The real Mr. Chanek will calculate the score of this attack. The score is defined as the square of the sum of the danger levels of all the killed snakes. Now Mr. Chanek is curious, what is the sum of scores for every possible attack strategy? Because this number can be huge, Mr. Chanek only needs the output modulo \(10^9 + 7\).
|
The first line contains three integers \(N\) \(M\) \(R\) \((1 \le M \le N \le 2 \cdot 10^3, 0 \le R < 10^3)\), the number of mad snakes, the number of clones, and the radius of the Rasengan.The next \(N\) lines each contains three integers, \(X_i\), \(Y_i\), dan \(B_i\) \((1 \le X_i, Y_i \le 10^3, 1 \le B_i \le 10^6)\). It is guaranteed that no two mad snakes occupy the same square.
|
A line with an integer that denotes the sum of scores for every possible attack strategy.
|
Here is the illustration of all six possible attack strategies. The circles denote the chosen mad snakes, and the blue squares denote the region of the Rasengan: So, the total score of all attacks is: \(3.600 + 3.600 + 4.900 + 3.600 + 10.000 + 8.100 = 33.800\).
|
Input: 4 2 1 1 1 10 2 2 20 2 3 30 5 2 40 | Output: 33800
|
Expert
| 3 | 1,326 | 385 | 89 | 14 |
1,313 |
B
|
1313B
|
B. Different Rules
| 1,700 |
constructive algorithms; greedy; implementation; math
|
Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be \(n\) participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.Suppose in the first round participant A took \(x\)-th place and in the second round — \(y\)-th place. Then the total score of the participant A is sum \(x + y\). The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every \(i\) from \(1\) to \(n\) exactly one participant took \(i\)-th place in first round and exactly one participant took \(i\)-th place in second round.Right after the end of the Olympiad, Nikolay was informed that he got \(x\)-th place in first round and \(y\)-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.
|
The first line contains an integer \(t\) (\(1 \le t \le 100\)) — the number of test cases to solve.Each of the following \(t\) lines contains integers \(n\), \(x\), \(y\) (\(1 \leq n \leq 10^9\), \(1 \le x, y \le n\)) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round.
|
Print two integers — the minimum and maximum possible overall place Nikolay could take.
|
Explanation for the first example:Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the second — third place.
|
Input: 15 1 3 | Output: 1 3
|
Medium
| 4 | 1,465 | 363 | 87 | 13 |
141 |
D
|
141D
|
D. Take-off Ramps
| 2,300 |
graphs; shortest paths
|
Vasya participates in a ski race along the X axis. The start is at point 0, and the finish is at L, that is, at a distance L meters from the start in the positive direction of the axis. Vasya has been training so hard that he can run one meter in exactly one second.Besides, there are n take-off ramps on the track, each ramp is characterized by four numbers: xi represents the ramp's coordinate di represents from how many meters Vasya will land if he goes down this ramp ti represents the flight time in seconds pi is the number, indicating for how many meters Vasya should gather speed to get ready and fly off the ramp. As Vasya gathers speed, he should ski on the snow (that is, he should not be flying), but his speed still equals one meter per second. Vasya is allowed to move in any direction on the X axis, but he is prohibited to cross the start line, that is go to the negative semiaxis. Vasya himself chooses which take-off ramps he will use and in what order, that is, he is not obliged to take off from all the ramps he encounters. Specifically, Vasya can skip the ramp. It is guaranteed that xi + di ≤ L, that is, Vasya cannot cross the finish line in flight.Vasya can jump from the ramp only in the positive direction of X axis. More formally, when using the i-th ramp, Vasya starts gathering speed at point xi - pi, jumps at point xi, and lands at point xi + di. He cannot use the ramp in opposite direction.Your task is to find the minimum time that Vasya will spend to cover the distance.
|
The first line contains two integers n and L (0 ≤ n ≤ 105, 1 ≤ L ≤ 109). Then n lines contain the descriptions of the ramps, each description is on a single line. Each description is a group of four non-negative integers xi, di, ti, pi (0 ≤ xi ≤ L, 1 ≤ di, ti, pi ≤ 109, xi + di ≤ L).
|
Print in the first line the minimum time in seconds Vasya needs to complete the track. Print in the second line k — the number of take-off ramps that Vasya needs to use, and print on the third line of output k numbers the number the take-off ramps Vasya used in the order in which he used them. Print each number exactly once, separate the numbers with a space. The ramps are numbered starting from 1 in the order in which they are given in the input.
|
In the first sample, Vasya cannot use ramp 2, because then he will need to gather speed starting from point -3, which is not permitted by the statement. The optimal option is using ramp 1, the resulting time is: moving to the point of gathering speed + gathering speed until reaching the takeoff ramp + flight time + moving to the finish line = 0 + 5 + 5 + 5 = 15.In the second sample using ramp 1 is not optimal for Vasya as t1 > d1. The optimal option is using ramp 2, the resulting time is: moving to the point of gathering speed + gathering speed until reaching the takeoff ramp + flight time + moving to the finish line = 14 + 1 + 1 + 0 = 16.
|
Input: 2 205 10 5 54 16 1 7 | Output: 1511
|
Expert
| 2 | 1,507 | 284 | 451 | 1 |
1,492 |
C
|
1492C
|
C. Maximum width
| 1,500 |
binary search; data structures; dp; greedy; two pointers
|
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: \(s\) of length \(n\) and \(t\) of length \(m\).A sequence \(p_1, p_2, \ldots, p_m\), where \(1 \leq p_1 < p_2 < \ldots < p_m \leq n\), is called beautiful, if \(s_{p_i} = t_i\) for all \(i\) from \(1\) to \(m\). The width of a sequence is defined as \(\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)\).Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings \(s\) and \(t\) there is at least one beautiful sequence.
|
The first input line contains two integers \(n\) and \(m\) (\(2 \leq m \leq n \leq 2 \cdot 10^5\)) — the lengths of the strings \(s\) and \(t\).The following line contains a single string \(s\) of length \(n\), consisting of lowercase letters of the Latin alphabet.The last line contains a single string \(t\) of length \(m\), consisting of lowercase letters of the Latin alphabet.It is guaranteed that there is at least one beautiful sequence for the given strings.
|
Output one integer — the maximum width of a beautiful sequence.
|
In the first example there are two beautiful sequences of width \(3\): they are \(\{1, 2, 5\}\) and \(\{1, 4, 5\}\).In the second example the beautiful sequence with the maximum width is \(\{1, 5\}\).In the third example there is exactly one beautiful sequence — it is \(\{1, 2, 3, 4, 5\}\).In the fourth example there is exactly one beautiful sequence — it is \(\{1, 2\}\).
|
Input: 5 3 abbbc abc | Output: 3
|
Medium
| 5 | 624 | 466 | 63 | 14 |
1,252 |
D
|
1252D
|
D. Find String in a Grid
| 3,000 |
data structures; dp; strings; trees
|
You have a grid \(G\) containing \(R\) rows (numbered from \(1\) to \(R\), top to bottom) and \(C\) columns (numbered from \(1\) to \(C\), left to right) of uppercase characters. The character in the \(r^{th}\) row and the \(c^{th}\) column is denoted by \(G_{r, c}\). You also have \(Q\) strings containing uppercase characters. For each of the string, you want to find the number of occurrences of the string in the grid.An occurrence of string \(S\) in the grid is counted if \(S\) can be constructed by starting at one of the cells in the grid, going right \(0\) or more times, and then going down \(0\) or more times. Two occurrences are different if the set of cells used to construct the string is different. Formally, for each string \(S\), you would like to count the number of tuples \(\langle r, c, \Delta r, \Delta c \rangle\) such that: \(1 \le r \le R\) and \(r \le r + \Delta r \le R\) \(1 \le c \le C\) and \(c \le c + \Delta c \le C\) \(S = G_{r, c} G_{r, c + 1} \dots G_{r, c + \Delta c} G_{r + 1, c + \Delta c} \dots G_{r + \Delta r, c + \Delta c}\)
|
Input begins with a line containing three integers: \(R\) \(C\) \(Q\) (\(1 \le R, C \le 500\); \(1 \le Q \le 200\,000\)) representing the size of the grid and the number of strings, respectively. The next \(R\) lines each contains \(C\) uppercase characters representing the grid. The \(c^{th}\) character on the \(r^{th}\) line is \(G_{r, c}\). The next \(Q\) lines each contains a string \(S\) containing uppercase characters. The length of this string is a positive integer not more than \(200\,000\). The sum of the length of all \(Q\) strings combined is not more than \(200\,000\).
|
For each query in the same order as input, output in a line an integer representing the number of occurrences of the string in the grid.
|
Explanation for the sample input/output #1 There are \(2\) occurrences of ""ABC"", represented by the tuples \(\langle 1, 1, 1, 1 \rangle\) and \(\langle 1, 1, 0, 2 \rangle\). There are \(3\) occurrences of ""BC"", represented by the tuples \(\langle 1, 2, 0, 1 \rangle\), \(\langle 1, 2, 1, 0 \rangle\), and \(\langle 2, 1, 0, 1 \rangle\). There is \(1\) occurrence of ""BD"", represented by the tuple \(\langle 2, 1, 1, 0 \rangle\). There is no occurrence of ""AC"". There are \(2\) occurrences of ""A"", represented by the tuples \(\langle 1, 1, 0, 0 \rangle\) and \(\langle 3, 2, 0, 0 \rangle\).
|
Input: 3 3 5 ABC BCD DAB ABC BC BD AC A | Output: 2 3 1 0 2
|
Master
| 4 | 1,068 | 587 | 136 | 12 |
1,649 |
B
|
1649B
|
B. Game of Ball Passing
| 1,300 |
greedy; implementation
|
Daniel is watching a football team playing a game during their training session. They want to improve their passing skills during that session.The game involves \(n\) players, making multiple passes towards each other. Unfortunately, since the balls were moving too fast, after the session Daniel is unable to know how many balls were involved during the game. The only thing he knows is the number of passes delivered by each player during all the session.Find the minimum possible amount of balls that were involved in the game.
|
There are several test cases in the input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) — the number of test cases. This is followed by the test cases description.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 10^5\)) — the number of players.The second line of the test case contains a sequence of integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\)), where \(a_i\) is the number of passes delivered by the \(i\)-th player.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case print a single integer — the answer to the problem.
|
In the first test case, with the only ball, the game can go like this:\(2 \rightarrow 1 \rightarrow 3 \rightarrow 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2\).In the second test case, there is no possible way to play the game with only one ball. One possible way to play with two balls:\(2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1\).\(2 \rightarrow 3 \rightarrow 2 \rightarrow 1\)In the third example, there were no passes, so \(0\) balls are possible.
|
Input: 442 3 3 231 5 220 041000000000 1000000000 1000000000 1000000000 | Output: 1 2 0 1
|
Easy
| 2 | 530 | 585 | 70 | 16 |
1,168 |
A
|
1168A
|
A. Increasing by Modulo
| 1,700 |
binary search; greedy
|
Toad Zitz has an array of integers, each integer is between \(0\) and \(m-1\) inclusive. The integers are \(a_1, a_2, \ldots, a_n\).In one operation Zitz can choose an integer \(k\) and \(k\) indices \(i_1, i_2, \ldots, i_k\) such that \(1 \leq i_1 < i_2 < \ldots < i_k \leq n\). He should then change \(a_{i_j}\) to \(((a_{i_j}+1) \bmod m)\) for each chosen integer \(i_j\). The integer \(m\) is fixed for all operations and indices.Here \(x \bmod y\) denotes the remainder of the division of \(x\) by \(y\).Zitz wants to make his array non-decreasing with the minimum number of such operations. Find this minimum number of operations.
|
The first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 300\,000\)) — the number of integers in the array and the parameter \(m\).The next line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < m\)) — the given array.
|
Output one integer: the minimum number of described operations Zitz needs to make his array non-decreasing. If no operations required, print \(0\).It is easy to see that with enough operations Zitz can always make his array non-decreasing.
|
In the first example, the array is already non-decreasing, so the answer is \(0\).In the second example, you can choose \(k=2\), \(i_1 = 2\), \(i_2 = 5\), the array becomes \([0,0,1,3,3]\). It is non-decreasing, so the answer is \(1\).
|
Input: 5 3 0 0 0 1 2 | Output: 0
|
Medium
| 2 | 636 | 266 | 239 | 11 |
1,594 |
E2
|
1594E2
|
E2. Rubik's Cube Coloring (hard version)
| 2,300 |
brute force; dp; implementation; math; trees
|
It is the hard version of the problem. The difference is that in this version, there are nodes with already chosen colors.Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem?You have a perfect binary tree of \(2^k - 1\) nodes — a binary tree where all vertices \(i\) from \(1\) to \(2^{k - 1} - 1\) have exactly two children: vertices \(2i\) and \(2i + 1\). Vertices from \(2^{k - 1}\) to \(2^k - 1\) don't have any children. You want to color its vertices with the \(6\) Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow).Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube. A picture of Rubik's cube and its 2D map. More formally: a white node can not be neighboring with white and yellow nodes; a yellow node can not be neighboring with white and yellow nodes; a green node can not be neighboring with green and blue nodes; a blue node can not be neighboring with green and blue nodes; a red node can not be neighboring with red and orange nodes; an orange node can not be neighboring with red and orange nodes; However, there are \(n\) special nodes in the tree, colors of which are already chosen.You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color.The answer may be too large, so output the answer modulo \(10^9+7\).
|
The first line contains the integers \(k\) (\(1 \le k \le 60\)) — the number of levels in the perfect binary tree you need to color.The second line contains the integer \(n\) (\(1 \le n \le \min(2^k - 1, 2000)\)) — the number of nodes, colors of which are already chosen.The next \(n\) lines contains integer \(v\) (\(1 \le v \le 2^k - 1\)) and string \(s\) — the index of the node and the color of the node (\(s\) is one of the white, yellow, green, blue, red and orange).It is guaranteed that each node \(v\) appears in the input at most once.
|
Print one integer — the number of the different colorings modulo \(10^9+7\).
|
In the picture below, you can see one of the correct colorings of the first test example.
|
Input: 3 2 5 orange 2 white | Output: 1024
|
Expert
| 5 | 1,518 | 545 | 76 | 15 |
1,006 |
E
|
1006E
|
E. Military Problem
| 1,600 |
dfs and similar; graphs; trees
|
In this problem you will have to help Berland army with organizing their command delivery system.There are \(n\) officers in Berland army. The first officer is the commander of the army, and he does not have any superiors. Every other officer has exactly one direct superior. If officer \(a\) is the direct superior of officer \(b\), then we also can say that officer \(b\) is a direct subordinate of officer \(a\).Officer \(x\) is considered to be a subordinate (direct or indirect) of officer \(y\) if one of the following conditions holds: officer \(y\) is the direct superior of officer \(x\); the direct superior of officer \(x\) is a subordinate of officer \(y\). For example, on the picture below the subordinates of the officer \(3\) are: \(5, 6, 7, 8, 9\).The structure of Berland army is organized in such a way that every officer, except for the commander, is a subordinate of the commander of the army.Formally, let's represent Berland army as a tree consisting of \(n\) vertices, in which vertex \(u\) corresponds to officer \(u\). The parent of vertex \(u\) corresponds to the direct superior of officer \(u\). The root (which has index \(1\)) corresponds to the commander of the army.Berland War Ministry has ordered you to give answers on \(q\) queries, the \(i\)-th query is given as \((u_i, k_i)\), where \(u_i\) is some officer, and \(k_i\) is a positive integer.To process the \(i\)-th query imagine how a command from \(u_i\) spreads to the subordinates of \(u_i\). Typical DFS (depth first search) algorithm is used here.Suppose the current officer is \(a\) and he spreads a command. Officer \(a\) chooses \(b\) — one of his direct subordinates (i.e. a child in the tree) who has not received this command yet. If there are many such direct subordinates, then \(a\) chooses the one having minimal index. Officer \(a\) gives a command to officer \(b\). Afterwards, \(b\) uses exactly the same algorithm to spread the command to its subtree. After \(b\) finishes spreading the command, officer \(a\) chooses the next direct subordinate again (using the same strategy). When officer \(a\) cannot choose any direct subordinate who still hasn't received this command, officer \(a\) finishes spreading the command.Let's look at the following example: If officer \(1\) spreads a command, officers receive it in the following order: \([1, 2, 3, 5 ,6, 8, 7, 9, 4]\).If officer \(3\) spreads a command, officers receive it in the following order: \([3, 5, 6, 8, 7, 9]\).If officer \(7\) spreads a command, officers receive it in the following order: \([7, 9]\).If officer \(9\) spreads a command, officers receive it in the following order: \([9]\).To answer the \(i\)-th query \((u_i, k_i)\), construct a sequence which describes the order in which officers will receive the command if the \(u_i\)-th officer spreads it. Return the \(k_i\)-th element of the constructed list or -1 if there are fewer than \(k_i\) elements in it.You should process queries independently. A query doesn't affect the following queries.
|
The first line of the input contains two integers \(n\) and \(q\) (\(2 \le n \le 2 \cdot 10^5, 1 \le q \le 2 \cdot 10^5\)) — the number of officers in Berland army and the number of queries.The second line of the input contains \(n - 1\) integers \(p_2, p_3, \dots, p_n\) (\(1 \le p_i < i\)), where \(p_i\) is the index of the direct superior of the officer having the index \(i\). The commander has index \(1\) and doesn't have any superiors.The next \(q\) lines describe the queries. The \(i\)-th query is given as a pair (\(u_i, k_i\)) (\(1 \le u_i, k_i \le n\)), where \(u_i\) is the index of the officer which starts spreading a command, and \(k_i\) is the index of the required officer in the command spreading sequence.
|
Print \(q\) numbers, where the \(i\)-th number is the officer at the position \(k_i\) in the list which describes the order in which officers will receive the command if it starts spreading from officer \(u_i\). Print ""-1"" if the number of officers which receive the command is less than \(k_i\).You should process queries independently. They do not affect each other.
|
Input: 9 61 1 1 3 5 3 5 73 11 53 47 31 81 9 | Output: 368-194
|
Medium
| 3 | 3,028 | 726 | 370 | 10 |
|
1,684 |
D
|
1684D
|
D. Traps
| 1,700 |
constructive algorithms; greedy; sortings
|
There are \(n\) traps numbered from \(1\) to \(n\). You will go through them one by one in order. The \(i\)-th trap deals \(a_i\) base damage to you.Instead of going through a trap, you can jump it over. You can jump over no more than \(k\) traps. If you jump over a trap, it does not deal any damage to you. But there is an additional rule: if you jump over a trap, all next traps damages increase by \(1\) (this is a bonus damage).Note that if you jump over a trap, you don't get any damage (neither base damage nor bonus damage). Also, the bonus damage stacks so, for example, if you go through a trap \(i\) with base damage \(a_i\), and you have already jumped over \(3\) traps, you get \((a_i + 3)\) damage.You have to find the minimal damage that it is possible to get if you are allowed to jump over no more than \(k\) traps.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le n\)) — the number of traps and the number of jump overs that you are allowed to make.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — base damage values of all traps.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output a single integer — the minimal total damage that it is possible to get if you are allowed to jump over no more than \(k\) traps.
|
In the first test case it is allowed to jump over all traps and take \(0\) damage.In the second test case there are \(5\) ways to jump over some traps: Do not jump over any trap.Total damage: \(5 + 10 + 11 + 5 = 31\). Jump over the \(1\)-st trap.Total damage: \(\underline{0} + (10 + 1) + (11 + 1) + (5 + 1) = 29\). Jump over the \(2\)-nd trap.Total damage: \(5 + \underline{0} + (11 + 1) + (5 + 1) = 23\). Jump over the \(3\)-rd trap.Total damage: \(5 + 10 + \underline{0} + (5 + 1) = 21\). Jump over the \(4\)-th trap.Total damage: \(5 + 10 + 11 + \underline{0} = 26\).To get minimal damage it is needed to jump over the \(3\)-rd trap, so the answer is \(21\).In the third test case it is optimal to jump over the traps \(1\), \(3\), \(4\), \(5\), \(7\):Total damage: \(0 + (2 + 1) + 0 + 0 + 0 + (2 + 4) + 0 = 9\).
|
Input: 54 48 7 1 44 15 10 11 57 58 2 5 15 11 2 86 31 2 3 4 5 61 17 | Output: 0 21 9 6 0
|
Medium
| 3 | 832 | 614 | 154 | 16 |
641 |
F
|
641F
|
F. Little Artem and 2-SAT
| 3,000 |
Little Artem is a very smart programmer. He knows many different difficult algorithms. Recently he has mastered in 2-SAT one.In computer science, 2-satisfiability (abbreviated as 2-SAT) is the special case of the problem of determining whether a conjunction (logical AND) of disjunctions (logical OR) have a solution, in which all disjunctions consist of no more than two arguments (variables). For the purpose of this problem we consider only 2-SAT formulas where each disjunction consists of exactly two arguments.Consider the following 2-SAT problem as an example: . Note that there might be negations in 2-SAT formula (like for x1 and for x4).Artem now tries to solve as many problems with 2-SAT as possible. He found a very interesting one, which he can not solve yet. Of course, he asks you to help him. The problem is: given two 2-SAT formulas f and g, determine whether their sets of possible solutions are the same. Otherwise, find any variables assignment x such that f(x) ≠ g(x).
|
The first line of the input contains three integers n, m1 and m2 (1 ≤ n ≤ 1000, 1 ≤ m1, m2 ≤ n2) — the number of variables, the number of disjunctions in the first formula and the number of disjunctions in the second formula, respectively.Next m1 lines contains the description of 2-SAT formula f. The description consists of exactly m1 pairs of integers xi ( - n ≤ xi ≤ n, xi ≠ 0) each on separate line, where xi > 0 corresponds to the variable without negation, while xi < 0 corresponds to the variable with negation. Each pair gives a single disjunction. Next m2 lines contains formula g in the similar format.
|
If both formulas share the same set of solutions, output a single word ""SIMILAR"" (without quotes). Otherwise output exactly n integers xi () — any set of values x such that f(x) ≠ g(x).
|
First sample has two equal formulas, so they are similar by definition.In second sample if we compute first function with x1 = 0 and x2 = 0 we get the result 0, because . But the second formula is 1, because .
|
Input: 2 1 11 21 2 | Output: SIMILAR
|
Master
| 0 | 990 | 613 | 187 | 6 |
|
1,027 |
B
|
1027B
|
B. Numbers on the Chessboard
| 1,200 |
implementation; math
|
You are given a chessboard of size \(n \times n\). It is filled with numbers from \(1\) to \(n^2\) in the following way: the first \(\lceil \frac{n^2}{2} \rceil\) numbers from \(1\) to \(\lceil \frac{n^2}{2} \rceil\) are written in the cells with even sum of coordinates from left to right from top to bottom. The rest \(n^2 - \lceil \frac{n^2}{2} \rceil\) numbers from \(\lceil \frac{n^2}{2} \rceil + 1\) to \(n^2\) are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation \(\lceil\frac{x}{y}\rceil\) means division \(x\) by \(y\) rounded up.For example, the left board on the following picture is the chessboard which is given for \(n=4\) and the right board is the chessboard which is given for \(n=5\). You are given \(q\) queries. The \(i\)-th query is described as a pair \(x_i, y_i\). The answer to the \(i\)-th query is the number written in the cell \(x_i, y_i\) (\(x_i\) is the row, \(y_i\) is the column). Rows and columns are numbered from \(1\) to \(n\).
|
The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 10^9\), \(1 \le q \le 10^5\)) — the size of the board and the number of queries.The next \(q\) lines contain two integers each. The \(i\)-th line contains two integers \(x_i, y_i\) (\(1 \le x_i, y_i \le n\)) — description of the \(i\)-th query.
|
For each query from \(1\) to \(q\) print the answer to this query. The answer to the \(i\)-th query is the number written in the cell \(x_i, y_i\) (\(x_i\) is the row, \(y_i\) is the column). Rows and columns are numbered from \(1\) to \(n\). Queries are numbered from \(1\) to \(q\) in order of the input.
|
Answers to the queries from examples are on the board in the picture from the problem statement.
|
Input: 4 51 14 44 33 22 4 | Output: 1816134
|
Easy
| 2 | 1,016 | 312 | 306 | 10 |
441 |
C
|
441C
|
C. Valera and Tubes
| 1,500 |
constructive algorithms; dfs and similar; implementation
|
Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that: r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange k tubes on his rectangle table in a fancy manner.
|
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
|
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells).If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
|
Picture for the first sample: Picture for the second sample:
|
Input: 3 3 3 | Output: 3 1 1 1 2 1 33 2 1 2 2 2 33 3 1 3 2 3 3
|
Medium
| 3 | 884 | 179 | 324 | 4 |
1,179 |
A
|
1179A
|
A. Valeriy and Deque
| 1,500 |
data structures; implementation
|
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with \(n\) elements. The \(i\)-th element is \(a_i\) (\(i\) = \(1, 2, \ldots, n\)). He gradually takes the first two leftmost elements from the deque (let's call them \(A\) and \(B\), respectively), and then does the following: if \(A > B\), he writes \(A\) to the beginning and writes \(B\) to the end of the deque, otherwise, he writes to the beginning \(B\), and \(A\) writes to the end of the deque. We call this sequence of actions an operation.For example, if deque was \([2, 3, 4, 5, 1]\), on the operation he will write \(B=3\) to the beginning and \(A=2\) to the end, so he will get \([3, 4, 5, 1, 2]\).The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him \(q\) queries. Each query consists of the singular number \(m_j\) \((j = 1, 2, \ldots, q)\). It is required for each query to answer which two elements he will pull out on the \(m_j\)-th operation.Note that the queries are independent and for each query the numbers \(A\) and \(B\) should be printed in the order in which they will be pulled out of the deque.Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.
|
The first line contains two integers \(n\) and \(q\) (\(2 \leq n \leq 10^5\), \(0 \leq q \leq 3 \cdot 10^5\)) — the number of elements in the deque and the number of queries. The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\), where \(a_i\) \((0 \leq a_i \leq 10^9)\) — the deque element in \(i\)-th position. The next \(q\) lines contain one number each, meaning \(m_j\) (\(1 \leq m_j \leq 10^{18}\)).
|
For each teacher's query, output two numbers \(A\) and \(B\) — the numbers that Valeriy pulls out of the deque for the \(m_j\)-th operation.
|
Consider all 10 steps for the first test in detail: \([1, 2, 3, 4, 5]\) — on the first operation, \(A\) and \(B\) are \(1\) and \(2\), respectively.So, \(2\) we write to the beginning of the deque, and \(1\) — to the end.We get the following status of the deque: \([2, 3, 4, 5, 1]\). \([2, 3, 4, 5, 1] \Rightarrow A = 2, B = 3\). \([3, 4, 5, 1, 2]\) \([4, 5, 1, 2, 3]\) \([5, 1, 2, 3, 4]\) \([5, 2, 3, 4, 1]\) \([5, 3, 4, 1, 2]\) \([5, 4, 1, 2, 3]\) \([5, 1, 2, 3, 4]\) \([5, 2, 3, 4, 1] \Rightarrow A = 5, B = 2\).
|
Input: 5 3 1 2 3 4 5 1 2 10 | Output: 1 2 2 3 5 2
|
Medium
| 2 | 1,347 | 422 | 140 | 11 |
1,821 |
E
|
1821E
|
E. Rearrange Brackets
| 2,100 |
brute force; dp; greedy; sortings; strings
|
A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example: bracket sequences ""()()"" and ""(())"" are regular (the resulting expressions are: ""(1)+(1)"" and ""((1+1)+1)""); bracket sequences "")("", ""("" and "")"" are not. You are given a regular bracket sequence. In one move, you can remove a pair of adjacent brackets such that the left one is an opening bracket and the right one is a closing bracket. Then concatenate the resulting parts without changing the order. The cost of this move is the number of brackets to the right of the right bracket of this pair.The cost of the regular bracket sequence is the smallest total cost of the moves required to make the sequence empty.Actually, you are not removing any brackets. Instead, you are given a regular bracket sequence and an integer \(k\). You can perform the following operation at most \(k\) times: extract some bracket from the sequence and insert it back at any position (between any two brackets, at the start or at the end; possibly, at the same place it was before). After all operations are performed, the bracket sequence has to be regular. What is the smallest possible cost of the resulting regular bracket sequence?
|
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 \(k\) (\(0 \le k \le 5\)) — the maximum number of operations you can perform.The second line contains a non-empty regular bracket sequence, it consists only of characters '(' and ')'.The total length of the regular bracket sequences over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print a single integer — the smallest possible cost of the regular bracket sequence after you perform at most \(k\) operations on it.
|
Input: 70()0(())1(())5()1(()()(()))2((())()(()())((())))3((())()(()())((()))) | Output: 0 1 0 0 1 4 2
|
Hard
| 5 | 1,343 | 438 | 152 | 18 |
|
1,806 |
C
|
1806C
|
C. Sequence Master
| 1,600 |
brute force; constructive algorithms; math
|
For some positive integer \(m\), YunQian considers an array \(q\) of \(2m\) (possibly negative) integers good, if and only if for every possible subsequence of \(q\) that has length \(m\), the product of the \(m\) elements in the subsequence is equal to the sum of the \(m\) elements that are not in the subsequence. Formally, let \(U=\{1,2,\ldots,2m\}\). For all sets \(S \subseteq U\) such that \(|S|=m\), \(\prod\limits_{i \in S} q_i = \sum\limits_{i \in U \setminus S} q_i\).Define the distance between two arrays \(a\) and \(b\) both of length \(k\) to be \(\sum\limits_{i=1}^k|a_i-b_i|\).You are given a positive integer \(n\) and an array \(p\) of \(2n\) integers.Find the minimum distance between \(p\) and \(q\) over all good arrays \(q\) of length \(2n\). It can be shown for all positive integers \(n\), at least one good array exists. Note that you are not required to construct the array \(q\) that achieves this minimum distance.
|
The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) — 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\cdot10^5\)).The second line of each test case contains \(2n\) integers \(p_1, p_2, \ldots, p_{2n}\) (\(|p_i| \le 10^9\)).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 distance between \(p\) and a good \(q\).
|
In the first test case, it is optimal to let \(q=[6,6]\).In the second test case, it is optimal to let \(q=[2,2,2,2]\).
|
Input: 416 921 2 2 12-2 -2 2 24-3 -2 -1 0 1 2 3 4 | Output: 3 2 5 13
|
Medium
| 3 | 943 | 426 | 79 | 18 |
959 |
A
|
959A
|
A. Mahmoud and Ehab and the even-odd game
| 800 |
games; math
|
Mahmoud and Ehab play a game called the even-odd game. Ehab chooses his favorite integer n and then they take turns, starting from Mahmoud. In each player's turn, he has to choose an integer a and subtract it from n such that: 1 ≤ a ≤ n. If it's Mahmoud's turn, a has to be even, but if it's Ehab's turn, a has to be odd. If the current player can't choose any number satisfying the conditions, he loses. Can you determine the winner if they both play optimally?
|
The only line contains an integer n (1 ≤ n ≤ 109), the number at the beginning of the game.
|
Output ""Mahmoud"" (without quotes) if Mahmoud wins and ""Ehab"" (without quotes) otherwise.
|
In the first sample, Mahmoud can't choose any integer a initially because there is no positive even integer less than or equal to 1 so Ehab wins.In the second sample, Mahmoud has to choose a = 2 and subtract it from n. It's Ehab's turn and n = 0. There is no positive odd integer less than or equal to 0 so Mahmoud wins.
|
Input: 1 | Output: Ehab
|
Beginner
| 2 | 462 | 91 | 92 | 9 |
176 |
A
|
176A
|
A. Trading Business
| 1,200 |
greedy; sortings
|
To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following: aij — the cost of buying an item; bij — the cost of selling an item; cij — the number of remaining items.It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.
|
The first line contains three space-separated integers n, m and k (2 ≤ n ≤ 10, 1 ≤ m, k ≤ 100) — the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly.Then follow n blocks describing each planet.The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1 ≤ bij < aij ≤ 1000, 0 ≤ cij ≤ 100) — the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces.It is guaranteed that the names of all planets are different.
|
Print a single number — the maximum profit Qwerty can get.
|
In the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3·6 + 7·8 = 74). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3·9 + 7·9 = 90 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.
|
Input: 3 3 10Venus6 5 37 6 58 6 10Earth10 9 08 6 410 9 3Mars4 3 08 4 127 2 5 | Output: 16
|
Easy
| 2 | 1,168 | 752 | 58 | 1 |
375 |
B
|
375B
|
B. Maximum Submatrix 2
| 1,600 |
data structures; dp; implementation; sortings
|
You are given a matrix consisting of digits zero and one, its size is n × m. You are allowed to rearrange its rows. What is the maximum area of the submatrix that only consists of ones and can be obtained in the given problem by the described operations?Let's assume that the rows of matrix a are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. A matrix cell on the intersection of the i-th row and the j-th column can be represented as (i, j). Formally, a submatrix of matrix a is a group of four integers d, u, l, r (1 ≤ d ≤ u ≤ n; 1 ≤ l ≤ r ≤ m). We will assume that the submatrix contains cells (i, j) (d ≤ i ≤ u; l ≤ j ≤ r). The area of the submatrix is the number of cells it contains.
|
The first line contains two integers n and m (1 ≤ n, m ≤ 5000). Next n lines contain m characters each — matrix a. Matrix a only contains characters: ""0"" and ""1"". Note that the elements of the matrix follow without any spaces in the lines.
|
Print a single integer — the area of the maximum obtained submatrix. If we cannot obtain a matrix of numbers one, print 0.
|
Input: 1 11 | Output: 1
|
Medium
| 4 | 744 | 243 | 122 | 3 |
|
207 |
D5
|
207D5
|
D5. The Beaver's Problem - 3
| 1,600 |
The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subject. The subject in this problem is an integer from 1 to 3. Each of these numbers has a physical meaning. For instance, all documents with subject 3 are about trade.You can download the training set of documents at the following link: http://download4.abbyy.com/a2/X2RZ2ZWXBG5VYWAL61H76ZQM/train.zip. The archive contains three directories with names ""1"", ""2"", ""3"". Directory named ""1"" contains documents on the 1-st subject, directory ""2"" contains documents on the 2-nd subject, and directory ""3"" contains documents on the 3-rd subject. Each document corresponds to exactly one file from some directory.All documents have the following format: the first line contains the document identifier, the second line contains the name of the document, all subsequent lines contain the text of the document. The document identifier is used to make installing the problem more convenient and has no useful information for the participants.You need to write a program that should indicate the subject for a given document. It is guaranteed that all documents given as input to your program correspond to one of the three subjects of the training set.
|
The first line contains integer id (0 ≤ id ≤ 106) — the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided into 10 groups. Documents of groups 1 and 2 are taken from the training set, but their identifiers will not match the identifiers specified in the training set. Groups from the 3-rd to the 10-th are roughly sorted by the author in ascending order of difficulty (these groups contain documents which aren't present in the training set).
|
Print an integer from 1 to 3, inclusive — the number of the subject the given document corresponds to.
|
Medium
| 0 | 1,472 | 653 | 102 | 2 |
|||
2,123 |
F
|
2123F
|
F. Minimize Fixed Points
| 1,700 |
constructive algorithms; number theory
|
Call a permutation\(^{\text{∗}}\) \(p\) of length \(n\) good if \(\gcd(p_i, i)\)\(^{\text{†}}\) \( > 1\) for all \(2 \leq i \leq n\). Find a good permutation with the minimum number of fixed points\(^{\text{‡}}\) across all good permutations of length \(n\). If there are multiple such permutations, print any of them.\(^{\text{∗}}\) A permutation of length \(n\) is an array that contains every integer from \(1\) to \(n\) exactly once, in any order. \(^{\text{†}}\)\(\gcd(x, y)\) denotes the greatest common divisor (GCD) of \(x\) and \(y\).\(^{\text{‡}}\)A fixed point of a permutation \(p\) is an index \(j\) (\(1\leq j\leq n\)) such that \(p_j = j\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The only line of each test case contains an integer \(n\) (\(2 \leq n \leq 10^5\)) — the length of the permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output on a single line an example of a good permutation of length \(n\) with the minimum number of fixed points.
|
In the third sample, we construct the permutation \(i\)\(p_i\)\(\gcd(p_i, i)\)\(1\)\(1\)\(1\)\(2\)\(4\)\(2\)\(3\)\(6\)\(3\)\(4\)\(2\)\(2\)\(5\)\(5\)\(5\)\(6\)\(3\)\(3\) Then we see that \(\gcd(p_i, i) > 1\) for all \(2\leq i\leq 6\). Furthermore, we see that there are only two fixed points, namely, \(1\) and \(5\). It can be shown that it is impossible to build a good permutation of length \(6\) with fewer fixed points.
|
Input: 423613 | Output: 1 2 1 2 3 1 4 6 2 5 3 1 12 9 6 10 8 7 4 3 5 11 2 13
|
Medium
| 2 | 655 | 292 | 133 | 21 |
1,329 |
E
|
1329E
|
E. Dreamoon Loves AA
| 3,300 |
binary search; greedy
|
There is a string of length \(n+1\) of characters 'A' and 'B'. The first character and last character of the string are equal to 'A'.You are given \(m\) indices \(p_1, p_2, \ldots, p_m\) (\(0\)-indexation) denoting the other indices of characters 'A' in the string. Let's denote the minimum distance between two neighboring 'A' as \(l\), and maximum distance between neighboring 'A' as \(r\).For example, \((l,r)\) of string ""ABBAABBBA"" is \((1,4)\).And let's denote the balance degree of a string as the value of \(r-l\).Now Dreamoon wants to change exactly \(k\) characters from 'B' to 'A', and he wants to make the balance degree of the string as small as possible.Please calculate the required minimum possible value of balance degree.
|
The first line contains one integer \(t\) denoting the number of test cases (\(1 \leq t \leq 400\,000\)).For each test case, the first line contains three integers \(n\), \(m\) and \(k\) (\(1 \leq n \leq 10^{15}, 0 \leq m \leq 400\,000, 0 \leq k < n - m\)).The second line contains \(m\) integers \(p_1, p_2, \ldots, p_m\), (\(0 < p_1 < p_2 < \ldots < p_m < n\)).The total sum of \(m\) is at most \(400\,000\).
|
For each test case, print one integer: the smallest possible value of balance degree after \(k\) changes of 'B' to 'A'.
|
Input: 5 80 3 5 11 24 50 81 7 12 4 10 17 26 37 48 61 25 10 14 3 4 7 12 13 15 17 19 21 23 1 0 0 10 2 0 2 4 | Output: 5 2 0 0 4
|
Master
| 2 | 741 | 410 | 119 | 13 |
|
316 |
G3
|
316G3
|
G3. Good Substrings
| 2,400 |
string suffix structures
|
Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or not the game uses rules. Overall there are n rules. Each rule is described by a group of three (p, l, r), where p is a string and l and r (l ≤ r) are integers. We’ll say that string t complies with rule (p, l, r), if the number of occurrences of string t in string p lies between l and r, inclusive. For example, string ""ab"", complies with rules (""ab"", 1, 2) and (""aab"", 0, 1), but does not comply with rules (""cd"", 1, 2) and (""abab"", 0, 1).A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (|s| is a length of s) is string slsl + 1... sr.Consider a number of occurrences of string t in string p as a number of pairs of integers l, r (1 ≤ l ≤ r ≤ |p|) such that p[l... r] = t.We’ll say that string t is good if it complies with all n rules. Smart Beaver asks you to help him to write a program that can calculate the number of distinct good substrings of string s. Two substrings s[x... y] and s[z... w] are cosidered to be distinct iff s[x... y] ≠ s[z... w].
|
The first line contains string s. The second line contains integer n. Next n lines contain the rules, one per line. Each of these lines contains a string and two integers pi, li, ri, separated by single spaces (0 ≤ li ≤ ri ≤ |pi|). It is guaranteed that all the given strings are non-empty and only contain lowercase English letters.The input limits for scoring 30 points are (subproblem G1): 0 ≤ n ≤ 10. The length of string s and the maximum length of string p is ≤ 200. The input limits for scoring 70 points are (subproblems G1+G2): 0 ≤ n ≤ 10. The length of string s and the maximum length of string p is ≤ 2000. The input limits for scoring 100 points are (subproblems G1+G2+G3): 0 ≤ n ≤ 10. The length of string s and the maximum length of string p is ≤ 50000.
|
Print a single integer — the number of good substrings of string s.
|
There are three good substrings in the first sample test: «aab», «ab» and «b».In the second test only substrings «e» and «t» are good.
|
Input: aaab2aa 0 0aab 1 1 | Output: 3
|
Expert
| 1 | 1,167 | 767 | 67 | 3 |
1,419 |
C
|
1419C
|
C. Killjoy
| 1,500 |
greedy; implementation; math
|
A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large).Killjoy's account is already infected and has a rating equal to \(x\). Its rating is constant. There are \(n\) accounts except hers, numbered from \(1\) to \(n\). The \(i\)-th account's initial rating is \(a_i\). Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed.Contests are regularly held on Codeforces. In each contest, any of these \(n\) accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer.Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change.It can be proven that all accounts can be infected in some finite number of contests.
|
The first line contains a single integer \(t\) \((1 \le t \le 100)\) — the number of test cases. The next \(2t\) lines contain the descriptions of all test cases.The first line of each test case contains two integers \(n\) and \(x\) (\(2 \le n \le 10^3\), \(-4000 \le x \le 4000\)) — the number of accounts on Codeforces and the rating of Killjoy's account.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) \((-4000 \le a_i \le 4000)\) — the ratings of other accounts.
|
For each test case output the minimal number of contests needed to infect all accounts.
|
In the first test case it's possible to make all ratings equal to \(69\). First account's rating will increase by \(1\), and second account's rating will decrease by \(1\), so the sum of all changes will be equal to zero.In the second test case all accounts will be instantly infected, because all ratings (including Killjoy's account's rating) are equal to \(4\).
|
Input: 3 2 69 68 70 6 4 4 4 4 4 4 4 9 38 -21 83 50 -59 -77 15 -71 -78 20 | Output: 1 0 2
|
Medium
| 3 | 1,265 | 501 | 87 | 14 |
1,015 |
B
|
1015B
|
B. Obtaining the String
| 1,200 |
implementation
|
You are given two strings \(s\) and \(t\). Both strings have length \(n\) and consist of lowercase Latin letters. The characters in the strings are numbered from \(1\) to \(n\).You can successively perform the following move any number of times (possibly, zero): swap any two adjacent (neighboring) characters of \(s\) (i.e. for any \(i = \{1, 2, \dots, n - 1\}\) you can swap \(s_i\) and \(s_{i + 1})\). You can't apply a move to the string \(t\). The moves are applied to the string \(s\) one after another.Your task is to obtain the string \(t\) from the string \(s\). Find any way to do it with at most \(10^4\) such moves.You do not have to minimize the number of moves, just find any sequence of moves of length \(10^4\) or less to transform \(s\) into \(t\).
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 50\)) — the length of strings \(s\) and \(t\).The second line of the input contains the string \(s\) consisting of \(n\) lowercase Latin letters.The third line of the input contains the string \(t\) consisting of \(n\) lowercase Latin letters.
|
If it is impossible to obtain the string \(t\) using moves, print ""-1"".Otherwise in the first line print one integer \(k\) — the number of moves to transform \(s\) to \(t\). Note that \(k\) must be an integer number between \(0\) and \(10^4\) inclusive.In the second line print \(k\) integers \(c_j\) (\(1 \le c_j < n\)), where \(c_j\) means that on the \(j\)-th move you swap characters \(s_{c_j}\) and \(s_{c_j + 1}\).If you do not need to apply any moves, print a single integer \(0\) in the first line and either leave the second line empty or do not print it at all.
|
In the first example the string \(s\) changes as follows: ""abcdef"" \(\rightarrow\) ""abdcef"" \(\rightarrow\) ""abdcfe"" \(\rightarrow\) ""abdfce"" \(\rightarrow\) ""abdfec"".In the second example there is no way to transform the string \(s\) into the string \(t\) through any allowed moves.
|
Input: 6abcdefabdfec | Output: 43 5 4 5
|
Easy
| 1 | 765 | 313 | 573 | 10 |
1,499 |
D
|
1499D
|
D. The Number of Pairs
| 2,100 |
dp; math; number theory
|
You are given three positive (greater than zero) integers \(c\), \(d\) and \(x\). You have to find the number of pairs of positive integers \((a, b)\) such that equality \(c \cdot lcm(a, b) - d \cdot gcd(a, b) = x\) holds. Where \(lcm(a, b)\) is the least common multiple of \(a\) and \(b\) and \(gcd(a, b)\) is the greatest common divisor of \(a\) and \(b\).
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of one line containing three integer \(c\), \(d\) and \(x\) (\(1 \le c, d, x \le 10^7\)).
|
For each test case, print one integer — the number of pairs (\(a, b\)) such that the above equality holds.
|
In the first example, the correct pairs are: (\(1, 4\)), (\(4,1\)), (\(3, 6\)), (\(6, 3\)).In the second example, the correct pairs are: (\(1, 2\)), (\(2, 1\)), (\(3, 3\)).
|
Input: 4 1 1 3 4 2 6 3 3 7 2 7 25 | Output: 4 3 0 8
|
Hard
| 3 | 359 | 205 | 106 | 14 |
1,001 |
A
|
1001A
|
A. Generate plus state or minus state
| 1,100 |
*special
|
You are given a qubit in state and an integer sign. Your task is to convert the given qubit to state if sign = 1 or if sign = - 1.
|
You have to implement an operation which takes a qubit and an integer as an input and has no output. The ""output"" of your solution is the state in which it left the input qubit.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (q : Qubit, sign : Int) : () { body { // your code here } }}
|
Easy
| 1 | 130 | 386 | 0 | 10 |
|||
2,067 |
C
|
2067C
|
C. Devyatkino
| 1,500 |
brute force; dfs and similar; greedy; math
|
You are given a positive integer \(n\). In one operation, you can add to \(n\) any positive integer whose decimal representation contains only the digit \(9\), possibly repeated several times.What is the minimum number of operations needed to make the number \(n\) contain at least one digit \(7\) in its decimal representation?For example, if \(n = 80\), it is sufficient to perform one operation: you can add \(99\) to \(n\), after the operation \(n = 179\), which contains the digit \(7\).
|
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 an integer \(n\) (\(10 \leq n \leq 10^9\)).
|
For each test case, output the minimum number of operations required for the number \(n\) to contain the digit \(7\).
|
In the first test case, three operations are sufficient: \(51 + 9 + 9 + 9 = 78\), which contains the digit \(7\). It can be shown that it is impossible to achieve the goal in one or two operations.In the second test case, two operations are sufficient: \(60 + 9 + 9 = 78\).In the third test case, one operation is sufficient: \(61 + 9 = 70\).In the fourth test case, \(n\) already contains the digit \(7\), so no operations are required.In the fifth test case, you can add \(99\) to \(n\) to obtain a number containing the digit \(7\).
|
Input: 16516061777123456891000000000200230019779898989868080000196701590 | Output: 3 2 1 0 1 3 5 4 0 7 1 2 7 0 7 3
|
Medium
| 4 | 492 | 246 | 117 | 20 |
1,363 |
C
|
1363C
|
C. Game On Leaves
| 1,600 |
games; trees
|
Ayush and Ashish play a game on an unrooted tree consisting of \(n\) nodes numbered \(1\) to \(n\). Players make the following move in turns: Select any leaf node in the tree and remove it together with any edge which has this node as one of its endpoints. A leaf node is a node with degree less than or equal to \(1\). A tree is a connected undirected graph without cycles.There is a special node numbered \(x\). The player who removes this node wins the game. Ayush moves first. Determine the winner of the game if each player plays optimally.
|
The first line of the input contains a single integer \(t\) \((1 \leq t \leq 10)\) — the number of testcases. The description of the test cases follows.The first line of each testcase contains two integers \(n\) and \(x\) \((1\leq n \leq 1000, 1 \leq x \leq n)\) — the number of nodes in the tree and the special node respectively.Each of the next \(n-1\) lines contain two integers \(u\), \(v\) \((1 \leq u, v \leq n, \text{ } u \ne v)\), meaning that there is an edge between nodes \(u\) and \(v\) in the tree.
|
For every test case, if Ayush wins the game, print ""Ayush"", otherwise print ""Ashish"" (without quotes).
|
For the \(1\)st test case, Ayush can only remove node \(2\) or \(3\), after which node \(1\) becomes a leaf node and Ashish can remove it in his turn.For the \(2\)nd test case, Ayush can remove node \(2\) in the first move itself.
|
Input: 1 3 1 2 1 3 1 | Output: Ashish
|
Medium
| 2 | 545 | 512 | 106 | 13 |
1,996 |
D
|
1996D
|
D. Fun
| 1,500 |
binary search; brute force; combinatorics; math; number theory
|
Counting is Fun!— satyam343Given two integers \(n\) and \(x\), find the number of triplets (\(a,b,c\)) of positive integers such that \(ab + ac + bc \le n\) and \(a + b + c \le x\). Note that order matters (e.g. (\(1, 1, 2\)) and (\(1, 2, 1\)) are treated as different) and \(a\), \(b\), \(c\) must be strictly greater than \(0\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.Each test case contains two integers \(n\) and \(x\) (\(1 \leq n,x \leq 10^6\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\) and that the sum of \(x\) over all test cases does not exceed \(10^6\).
|
Output a single integer — the number of triplets (\(a,b,c\)) of positive integers such that \(ab + ac + bc \le n\) and \(a + b + c \le x\).
|
In the first test case, the triplets are (\(1, 1, 1\)), (\(1, 1, 2\)), (\(1, 2, 1\)), and (\(2, 1, 1\)).In the second test case, the triplets are (\(1, 1, 1\)), (\(1, 1, 2\)), (\(1, 1, 3\)), (\(1, 2, 1\)), (\(1, 2, 2\)), (\(1, 3, 1\)), (\(2, 1, 1\)), (\(2, 1, 2\)), (\(2, 2, 1\)), and (\(3, 1, 1\)).
|
Input: 47 410 57 1000900000 400000 | Output: 4 10 7 1768016938
|
Medium
| 5 | 330 | 334 | 139 | 19 |
544 |
A
|
544A
|
A. Set of Strings
| 1,100 |
implementation; strings
|
You are given a string q. A sequence of k strings s1, s2, ..., sk is called beautiful, if the concatenation of these strings is string q (formally, s1 + s2 + ... + sk = q) and the first characters of these strings are distinct.Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist.
|
The first line contains a positive integer k (1 ≤ k ≤ 26) — the number of strings that should be in a beautiful sequence. The second line contains string q, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive.
|
If such sequence doesn't exist, then print in a single line ""NO"" (without the quotes). Otherwise, print in the first line ""YES"" (without the quotes) and in the next k lines print the beautiful sequence of strings s1, s2, ..., sk.If there are multiple possible answers, print any of them.
|
In the second sample there are two possible answers: {""aaaca"", ""s""} and {""aaa"", ""cas""}.
|
Input: 1abca | Output: YESabca
|
Easy
| 2 | 321 | 262 | 291 | 5 |
424 |
E
|
424E
|
E. Colored Jenga
| 2,500 |
dfs and similar; dp; probabilities
|
Cold winter evenings in Tomsk are very boring — nobody wants be on the streets at such a time. Residents of Tomsk while away the time sitting in warm apartments, inventing a lot of different games. One of such games is 'Colored Jenga'.This game requires wooden blocks of three colors: red, green and blue. A tower of n levels is made from them. Each level consists of three wooden blocks. The blocks in each level can be of arbitrary colors, but they are always located close and parallel to each other. An example of such a tower is shown in the figure. The game is played by exactly one person. Every minute a player throws a special dice which has six sides. Two sides of the dice are green, two are blue, one is red and one is black. The dice shows each side equiprobably.If the dice shows red, green or blue, the player must take any block of this color out of the tower at this minute so that the tower doesn't fall. If this is not possible, the player waits until the end of the minute, without touching the tower. He also has to wait until the end of the minute without touching the tower if the dice shows the black side. It is not allowed to take blocks from the top level of the tower (whether it is completed or not).Once a player got a block out, he must put it on the top of the tower so as to form a new level or finish the upper level consisting of previously placed blocks. The newly constructed levels should have all the same properties as the initial levels. If the upper level is not completed, starting the new level is prohibited.For the tower not to fall, in each of the levels except for the top, there should be at least one block. Moreover, if at some of these levels there is exactly one block left and this block is not the middle block, the tower falls.The game ends at the moment when there is no block in the tower that you can take out so that the tower doesn't fall.Here is a wonderful game invented by the residents of the city of Tomsk. I wonder for how many minutes can the game last if the player acts optimally well? If a player acts optimally well, then at any moment he tries to choose the block he takes out so as to minimize the expected number of the game duration.Your task is to write a program that determines the expected number of the desired amount of minutes.
|
The first line of the input contains the only integer n (2 ≤ n ≤ 6) — the number of levels in the tower.Then n lines follow, describing the levels of the tower from the bottom to the top (the first line is the top of the tower). Each level is described by three characters, the first and the third of them set the border blocks of the level and the second one is the middle block. The character that describes the block has one of the following values 'R' (a red block), 'G' (a green block) and 'B' (a blue block).
|
In the only line of the output print the sought mathematical expectation value. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
|
Input: 6RGBGRGBBBGGRBRGBRB | Output: 17.119213696601992
|
Expert
| 3 | 2,310 | 514 | 174 | 4 |
|
159 |
D
|
159D
|
D. Palindrome pairs
| 1,500 |
*special; brute force; dp; strings
|
You are given a non-empty string s consisting of lowercase letters. Find the number of pairs of non-overlapping palindromic substrings of this string.In a more formal way, you have to find the quantity of tuples (a, b, x, y) such that 1 ≤ a ≤ b < x ≤ y ≤ |s| and substrings s[a... b], s[x... y] are palindromes.A palindrome is a string that can be read the same way from left to right and from right to left. For example, ""abacaba"", ""z"", ""abba"" are palindromes.A substring s[i... j] (1 ≤ i ≤ j ≤ |s|) of string s = s1s2... s|s| is a string sisi + 1... sj. For example, substring s[2...4] of string s = ""abacaba"" equals ""bac"".
|
The first line of input contains a non-empty string s which consists of lowercase letters ('a'...'z'), s contains at most 2000 characters.
|
Output a single number — the quantity of pairs of non-overlapping palindromic substrings of s.Please do not use the %lld format specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d format specifier.
|
Input: aa | Output: 1
|
Medium
| 4 | 635 | 138 | 250 | 1 |
|
569 |
B
|
569B
|
B. Inventory
| 1,200 |
greedy; math
|
Companies always have a lot of equipment, furniture and other things. All of them should be tracked. To do this, there is an inventory number assigned with each item. It is much easier to create a database by using those numbers and keep the track of everything.During an audit, you were surprised to find out that the items are not numbered sequentially, and some items even share the same inventory number! There is an urgent need to fix it. You have chosen to make the numbers of the items sequential, starting with 1. Changing a number is quite a time-consuming process, and you would like to make maximum use of the current numbering.You have been given information on current inventory numbers for n items in the company. Renumber items so that their inventory numbers form a permutation of numbers from 1 to n by changing the number of as few items as possible. Let us remind you that a set of n numbers forms a permutation if all the numbers are in the range from 1 to n, and no two numbers are equal.
|
The first line contains a single integer n — the number of items (1 ≤ n ≤ 105).The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — the initial inventory numbers of the items.
|
Print n numbers — the final inventory numbers of the items in the order they occur in the input. If there are multiple possible answers, you may print any of them.
|
In the first test the numeration is already a permutation, so there is no need to change anything.In the second test there are two pairs of equal numbers, in each pair you need to replace one number.In the third test you need to replace 2 by 1, as the numbering should start from one.
|
Input: 31 3 2 | Output: 1 3 2
|
Easy
| 2 | 1,009 | 190 | 163 | 5 |
1,316 |
C
|
1316C
|
C. Primitive Primes
| 1,800 |
constructive algorithms; math; ternary search
|
It is Professor R's last class of his teaching career. Every time Professor R taught a class, he gave a special problem for the students to solve. You being his favourite student, put your heart into solving it one last time.You are given two polynomials \(f(x) = a_0 + a_1x + \dots + a_{n-1}x^{n-1}\) and \(g(x) = b_0 + b_1x + \dots + b_{m-1}x^{m-1}\), with positive integral coefficients. It is guaranteed that the cumulative GCD of the coefficients is equal to \(1\) for both the given polynomials. In other words, \(gcd(a_0, a_1, \dots, a_{n-1}) = gcd(b_0, b_1, \dots, b_{m-1}) = 1\). Let \(h(x) = f(x)\cdot g(x)\). Suppose that \(h(x) = c_0 + c_1x + \dots + c_{n+m-2}x^{n+m-2}\). You are also given a prime number \(p\). Professor R challenges you to find any \(t\) such that \(c_t\) isn't divisible by \(p\). He guarantees you that under these conditions such \(t\) always exists. If there are several such \(t\), output any of them.As the input is quite large, please use fast input reading methods.
|
The first line of the input contains three integers, \(n\), \(m\) and \(p\) (\(1 \leq n, m \leq 10^6, 2 \leq p \leq 10^9\)), — \(n\) and \(m\) are the number of terms in \(f(x)\) and \(g(x)\) respectively (one more than the degrees of the respective polynomials) and \(p\) is the given prime number.It is guaranteed that \(p\) is prime.The second line contains \(n\) integers \(a_0, a_1, \dots, a_{n-1}\) (\(1 \leq a_{i} \leq 10^{9}\)) — \(a_i\) is the coefficient of \(x^{i}\) in \(f(x)\).The third line contains \(m\) integers \(b_0, b_1, \dots, b_{m-1}\) (\(1 \leq b_{i} \leq 10^{9}\)) — \(b_i\) is the coefficient of \(x^{i}\) in \(g(x)\).
|
Print a single integer \(t\) (\(0\le t \le n+m-2\)) — the appropriate power of \(x\) in \(h(x)\) whose coefficient isn't divisible by the given prime \(p\). If there are multiple powers of \(x\) that satisfy the condition, print any.
|
In the first test case, \(f(x)\) is \(2x^2 + x + 1\) and \(g(x)\) is \(x + 2\), their product \(h(x)\) being \(2x^3 + 5x^2 + 3x + 2\), so the answer can be 1 or 2 as both 3 and 5 aren't divisible by 2.In the second test case, \(f(x)\) is \(x + 2\) and \(g(x)\) is \(x + 3\), their product \(h(x)\) being \(x^2 + 5x + 6\), so the answer can be any of the powers as no coefficient is divisible by the given prime.
|
Input: 3 2 2 1 1 2 2 1 | Output: 1
|
Medium
| 3 | 1,006 | 643 | 233 | 13 |
1,025 |
C
|
1025C
|
C. Plasticine zebra
| 1,600 |
constructive algorithms; implementation
|
Is there anything better than going to the zoo after a tiresome week at work? No wonder Grisha feels the same while spending the entire weekend accompanied by pretty striped zebras. Inspired by this adventure and an accidentally found plasticine pack (represented as a sequence of black and white stripes), Grisha now wants to select several consequent (contiguous) pieces of alternating colors to create a zebra. Let's call the number of selected pieces the length of the zebra.Before assembling the zebra Grisha can make the following operation \(0\) or more times. He splits the sequence in some place into two parts, then reverses each of them and sticks them together again. For example, if Grisha has pieces in the order ""bwbbw"" (here 'b' denotes a black strip, and 'w' denotes a white strip), then he can split the sequence as bw|bbw (here the vertical bar represents the cut), reverse both parts and obtain ""wbwbb"".Determine the maximum possible length of the zebra that Grisha can produce.
|
The only line contains a string \(s\) (\(1 \le |s| \le 10^5\), where \(|s|\) denotes the length of the string \(s\)) comprised of lowercase English letters 'b' and 'w' only, where 'w' denotes a white piece and 'b' denotes a black piece.
|
Print a single integer — the maximum possible zebra length.
|
In the first example one of the possible sequence of operations is bwwwbww|bw \(\to\) w|wbwwwbwb \(\to\) wbwbwwwbw, that gives the answer equal to \(5\).In the second example no operation can increase the answer.
|
Input: bwwwbwwbw | Output: 5
|
Medium
| 2 | 1,002 | 236 | 59 | 10 |
1,108 |
C
|
1108C
|
C. Nice Garland
| 1,300 |
brute force; greedy; math
|
You have a garland consisting of \(n\) lamps. Each lamp is colored red, green or blue. The color of the \(i\)-th lamp is \(s_i\) ('R', 'G' and 'B' — colors of lamps in the garland).You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is \(t\), then for each \(i, j\) such that \(t_i = t_j\) should be satisfied \(|i-j|~ mod~ 3 = 0\). The value \(|x|\) means absolute value of \(x\), the operation \(x~ mod~ y\) means remainder of \(x\) when divided by \(y\).For example, the following garlands are nice: ""RGBRGBRG"", ""GB"", ""R"", ""GRBGRBG"", ""BRGBRGB"". The following garlands are not nice: ""RR"", ""RGBG"".Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the number of lamps.The second line of the input contains the string \(s\) consisting of \(n\) characters 'R', 'G' and 'B' — colors of lamps in the garland.
|
In the first line of the output print one integer \(r\) — the minimum number of recolors needed to obtain a nice garland from the given one.In the second line of the output print one string \(t\) of length \(n\) — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
|
Input: 3 BRB | Output: 1 GRB
|
Easy
| 3 | 1,043 | 244 | 351 | 11 |
|
641 |
E
|
641E
|
E. Little Artem and Time Machine
| 2,000 |
data structures
|
Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types: Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer. Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset. Count the number of instances of the given integer that are stored in the multiset. But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example. First Artem adds integer 5 to the multiset at the 1-st moment of time. Then Artem adds integer 3 to the multiset at the moment 5. Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1. Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0. Then Artem goes back in time again and removes 5 from the multiset at moment 3. Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment 3. Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes.Help Artem implement time travellers multiset.
|
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of Artem's queries.Then follow n lines with queries descriptions. Each of them contains three integers ai, ti and xi (1 ≤ ai ≤ 3, 1 ≤ ti, xi ≤ 109) — type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent.
|
For each ask operation output the number of instances of integer being queried at the given moment of time.
|
Input: 61 1 53 5 51 2 53 6 52 3 53 7 5 | Output: 121
|
Hard
| 1 | 1,964 | 523 | 107 | 6 |
|
1,741 |
A
|
1741A
|
A. Compare T-Shirt Sizes
| 800 |
implementation; implementation; strings
|
Two T-shirt sizes are given: \(a\) and \(b\). The T-shirt size is either a string M or a string consisting of several (possibly zero) characters X and one of the characters S or L.For example, strings M, XXL, S, XXXXXXXS could be the size of some T-shirts. And the strings XM, LL, SX are not sizes.The letter M stands for medium, S for small, L for large. The letter X refers to the degree of size (from eXtra). For example, XXL is extra-extra-large (bigger than XL, and smaller than XXXL).You need to compare two given sizes of T-shirts \(a\) and \(b\).The T-shirts are compared as follows: any small size (no matter how many letters X) is smaller than the medium size and any large size; any large size (regardless of the number of letters X) is larger than the medium size and any small size; the more letters X before S, the smaller the size; the more letters X in front of L, the larger the size. For example: XXXS < XS XXXL > XL XL > M XXL = XXL XXXXXS < M XL > XXXS
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Each test case consists of one line, in which \(a\) and \(b\) T-shirt sizes are written. The lengths of the strings corresponding to the T-shirt sizes do not exceed \(50\). It is guaranteed that all sizes are correct.
|
For each test case, print on a separate line the result of comparing \(a\) and \(b\) T-shirt sizes (lines ""<"", "">"" or ""="" without quotes).
|
Input: 6XXXS XSXXXL XLXL MXXL XXLXXXXXS ML M | Output: < > > = < >
|
Beginner
| 3 | 972 | 327 | 144 | 17 |
|
1,234 |
F
|
1234F
|
F. Yet Another Substring Reverse
| 2,200 |
bitmasks; dp
|
You are given a string \(s\) consisting only of first \(20\) lowercase Latin letters ('a', 'b', ..., 't').Recall that the substring \(s[l; r]\) of the string \(s\) is the string \(s_l s_{l + 1} \dots s_r\). For example, the substrings of ""codeforces"" are ""code"", ""force"", ""f"", ""for"", but not ""coder"" and ""top"".You can perform the following operation no more than once: choose some substring \(s[l; r]\) and reverse it (i.e. the string \(s_l s_{l + 1} \dots s_r\) becomes \(s_r s_{r - 1} \dots s_l\)).Your goal is to maximize the length of the maximum substring of \(s\) consisting of distinct (i.e. unique) characters.The string consists of distinct characters if no character in this string appears more than once. For example, strings ""abcde"", ""arctg"" and ""minecraft"" consist of distinct characters but strings ""codeforces"", ""abacaba"" do not consist of distinct characters.
|
The only line of the input contains one string \(s\) consisting of no more than \(10^6\) characters 'a', 'b', ..., 't' (first \(20\) lowercase Latin letters).
|
Print one integer — the maximum possible length of the maximum substring of \(s\) consisting of distinct characters after reversing no more than one its substring.
|
Input: abacaba | Output: 3
|
Hard
| 2 | 899 | 158 | 163 | 12 |
|
1,791 |
A
|
1791A
|
A. Codeforces Checking
| 800 |
implementation; strings
|
Given a lowercase Latin character (letter), check if it appears in the string \(\texttt{codeforces}\).
|
The first line of the input contains an integer \(t\) (\(1 \leq t \leq 26\)) — the number of test cases.The only line of each test case contains a character \(c\) — a single lowercase Latin character (letter).
|
For each test case, output ""YES"" (without quotes) if \(c\) satisfies the condition, and ""NO"" (without quotes) otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
|
Input: 10abcdefghij | Output: NO NO YES YES YES YES NO NO NO NO
|
Beginner
| 2 | 102 | 209 | 267 | 17 |
|
425 |
D
|
425D
|
D. Sereja and Squares
| 2,300 |
binary search; data structures; hashing
|
Sereja has painted n distinct points on the plane. The coordinates of each point are integers. Now he is wondering: how many squares are there with sides parallel to the coordinate axes and with points painted in all its four vertexes? Help him, calculate this number.
|
The first line contains integer n (1 ≤ n ≤ 105). Each of the next n lines contains two integers xi, yi (0 ≤ xi, yi ≤ 105), the integers represent the coordinates of the i-th point. It is guaranteed that all the given points are distinct.
|
In a single line print the required number of squares.
|
Input: 50 00 22 02 21 1 | Output: 1
|
Expert
| 3 | 268 | 237 | 54 | 4 |
|
34 |
E
|
34E
|
E. Collisions
| 2,000 |
brute force; implementation; math
|
On a number line there are n balls. At time moment 0 for each ball the following data is known: its coordinate xi, speed vi (possibly, negative) and weight mi. The radius of the balls can be ignored.The balls collide elastically, i.e. if two balls weighing m1 and m2 and with speeds v1 and v2 collide, their new speeds will be: .Your task is to find out, where each ball will be t seconds after.
|
The first line contains two integers n and t (1 ≤ n ≤ 10, 0 ≤ t ≤ 100) — amount of balls and duration of the process. Then follow n lines, each containing three integers: xi, vi, mi (1 ≤ |vi|, mi ≤ 100, |xi| ≤ 100) — coordinate, speed and weight of the ball with index i at time moment 0.It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;t]).
|
Output n numbers — coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point.
|
Input: 2 93 4 50 7 8 | Output: 68.53846153844.538461538
|
Hard
| 3 | 395 | 521 | 134 | 0 |
|
2,086 |
B
|
2086B
|
B. Large Array and Segments
| 1,100 |
binary search; brute force; greedy
|
There is an array \(a\) consisting of \(n\) positive integers, and a positive integer \(k\). An array \(b\) is created from array \(a\) according to the following rules: \(b\) contains \(n \cdot k\) numbers; the first \(n\) numbers of array \(b\) are the same as the numbers of array \(a\), that is, \(b_{i} = a_{i}\) for \(i \le n\); for any \(i > n\), it holds that \(b_{i} = b_{i - n}\). For example, if \(a = [2, 3, 1, 4]\) and \(k = 3\), then \(b = [2, 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4]\). Given a number \(x\), it is required to count the number of such positions \(l\) (\(1 \le l \le n \cdot k\)), for which there exists a position \(r \ge l\), such that the sum of the elements of array \(b\) on the segment \([l, r]\) is at least \(x\) (in other words, \(b_{l} + b_{l+1} + \dots + b_{r} \ge x\)).
|
Each test consists of several test cases. The first line contains one integer \(t\) (\(1 \le t \le 10^{4}\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(k\), \(x\) (\(1 \le n, k \le 10^{5}\); \(1 \le x \le 10^{18}\)).The second line of each test case contains \(n\) integers \(a_{i}\) (\(1 \le a_{i} \le 10^{8}\)).Additional constraints on the input: the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^{5}\); the sum of \(k\) across all test cases does not exceed \(2 \cdot 10^{5}\).
|
For each test case, output one integer — the number of suitable positions \(l\) in the array \(b\).
|
In the first test case, the array \(b\) looks like this:$$$\([3, 4, 2, 1, 5, 3, 4, 2, 1, 5, 3, 4, 2, 1, 5]\)\(There are \)12\( positions \)l\( for which there exists a suitable position \)r\(. Here are some (not all) of them: \)l = 1\(, for which there is a position \)r = 6\(, the sum over the segment \)[1, 6]\( equals \)18\(; \)l = 2\(, for which there is a position \)r = 5\(, the sum over the segment \)[2, 5]\( equals \)12\(; \)l = 6\(, for which there is a position \)r = 9\(, the sum over the segment \)[6, 9]\( equals \)10$$$.
|
Input: 75 3 103 4 2 1 515 97623 1300111105 95 108 111 118 101 95 118 97 108 111 114 97 110 1161 100000 123456789101111 1 111 1 122 1 21 12 1 52 1 | Output: 12 1452188 0 1 1 1 0
|
Easy
| 3 | 804 | 592 | 99 | 20 |
1,637 |
A
|
1637A
|
A. Sorting Parts
| 800 |
brute force; sortings
|
You have an array \(a\) of length \(n\). You can exactly once select an integer \(len\) between \(1\) and \(n - 1\) inclusively, and then sort in non-decreasing order the prefix of the array of length \(len\) and the suffix of the array of length \(n - len\) independently.For example, if the array is \(a = [3, 1, 4, 5, 2]\), and you choose \(len = 2\), then after that the array will be equal to \([1, 3, 2, 4, 5]\).Could it be that after performing this operation, the array will not be sorted in non-decreasing order?
|
There are several test cases in the input data. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases. This is followed by the test cases description.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 10^4\)) — the length of the array.The second line of the test case contains a sequence of integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) — the array elements.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^4\).
|
For each test case of input data, output ""YES"" (without quotes), if the array may be not sorted in non-decreasing order, output ""NO"" (without quotes) otherwise. You can output each letter in any case (uppercase or lowercase).
|
In the first test case, it's possible to select \(len = 1\), then after operation, the array will not be sorted in non-decreasing order and will be equal to \([2, 1, 2]\).In the second test case, it's possible to select \(len = 3\), then after operation, the array will not be sorted in non-decreasing order and will be equal to \([1, 2, 3, 1]\).In the third test case, the array will be sorted in non-decreasing order for every possible \(len\).
|
Input: 332 2 143 1 2 151 2 2 4 4 | Output: YES YES NO
|
Beginner
| 2 | 521 | 528 | 229 | 16 |
1,521 |
C
|
1521C
|
C. Nastia and a Hidden Permutation
| 2,000 |
constructive algorithms; interactive
|
This is an interactive problem!Nastia has a hidden permutation \(p\) of length \(n\) consisting of integers from \(1\) to \(n\). You, for some reason, want to figure out the permutation. To do that, you can give her an integer \(t\) (\(1 \le t \le 2\)), two different indices \(i\) and \(j\) (\(1 \le i, j \le n\), \(i \neq j\)), and an integer \(x\) (\(1 \le x \le n - 1\)). Depending on \(t\), she will answer: \(t = 1\): \(\max{(\min{(x, p_i)}, \min{(x + 1, p_j)})}\); \(t = 2\): \(\min{(\max{(x, p_i)}, \max{(x + 1, p_j)})}\). You can ask Nastia at most \(\lfloor \frac {3 \cdot n} { 2} \rfloor + 30\) times. It is guaranteed that she will not change her permutation depending on your queries. Can you guess the permutation?
|
The input consists of several test cases. In the beginning, you receive the integer \(T\) (\(1 \le T \le 10\,000\)) — the number of test cases.At the beginning of each test case, you receive an integer \(n\) (\(3 \le n \le 10^4\)) — the length of the permutation \(p\).It's guaranteed that the permutation is fixed beforehand and that the sum of \(n\) in one test doesn't exceed \(2 \cdot 10^4\).
|
Consider the first test case.The hidden permutation is \([3, 1, 4, 2]\).We print: ""? \(2\) \(4\) \(1\) \(3\)"" and get back \(\min{(\max{(3, p_4}), \max{(4, p_1)})} = 3\).We print: ""? \(1\) \(2\) \(4\) \(2\)"" and get back \(\max{(\min{(2, p_2)}, \min{(3, p_4)})} = 2\).Consider the second test case.The hidden permutation is \([2, 5, 3, 4, 1]\).We print: ""? \(2\) \(3\) \(4\) \(2\)"" and get back \(\min{(\max{(2, p_3}), \max{(3, p_4)})} = 3\).
|
Input: 2 4 3 2 5 3 | Output: ? 2 4 1 3 ? 1 2 4 2 ! 3 1 4 2 ? 2 3 4 2 ! 2 5 3 4 1
|
Hard
| 2 | 728 | 396 | 0 | 15 |
|
2,074 |
A
|
2074A
|
A. Draw a Square
| 800 |
geometry; implementation
|
The pink soldiers have given you \(4\) distinct points on the plane. The \(4\) points' coordinates are \((-l,0)\), \((r,0)\), \((0,-d)\), \((0,u)\) correspondingly, where \(l\), \(r\), \(d\), \(u\) are positive integers. In the diagram, a square is drawn by connecting the four points \(L\), \(R\), \(D\), \(U\). Please determine if it is possible to draw a square\(^{\text{∗}}\) with the given points as its vertices.\(^{\text{∗}}\)A square is defined as a polygon consisting of \(4\) vertices, of which all sides have equal length and all inner angles are equal. No two edges of the polygon may intersect each other.
|
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 four integers \(l\), \(r\), \(d\), \(u\) (\(1 \le l,r,d,u \le 10\)).
|
For each test case, if you can draw a square using the four points, output ""Yes"". Otherwise, output ""No"".You can output the answer in any case. For example, the strings ""yEs"", ""yes"", and ""YES"" will also be recognized as positive responses.
|
On the first test case, the four given points form a square, so the answer is ""Yes"".On the second test case, the four given points do not form a square, so the answer is ""No"".
|
Input: 22 2 2 21 2 3 4 | Output: Yes No
|
Beginner
| 2 | 618 | 272 | 249 | 20 |
933 |
C
|
933C
|
C. A Colourful Prospect
| 2,700 |
geometry; graphs
|
Firecrackers scare Nian the monster, but they're wayyyyy too noisy! Maybe fireworks make a nice complement.Little Tommy is watching a firework show. As circular shapes spread across the sky, a splendid view unfolds on the night of Lunar New Year's eve.A wonder strikes Tommy. How many regions are formed by the circles on the sky? We consider the sky as a flat plane. A region is a connected part of the plane with positive area, whose bound consists of parts of bounds of the circles and is a curve or several curves without self-intersections, and that does not contain any curve other than its boundaries. Note that exactly one of the regions extends infinitely.
|
The first line of input contains one integer n (1 ≤ n ≤ 3), denoting the number of circles.The following n lines each contains three space-separated integers x, y and r ( - 10 ≤ x, y ≤ 10, 1 ≤ r ≤ 10), describing a circle whose center is (x, y) and the radius is r. No two circles have the same x, y and r at the same time.
|
Print a single integer — the number of regions on the plane.
|
For the first example, For the second example, For the third example,
|
Input: 30 0 12 0 14 0 1 | Output: 4
|
Master
| 2 | 665 | 323 | 60 | 9 |
1,238 |
G
|
1238G
|
G. Adilbek and the Watering System
| 2,700 |
data structures; greedy; sortings
|
Adilbek has to water his garden. He is going to do it with the help of a complex watering system: he only has to deliver water to it, and the mechanisms will do all the remaining job.The watering system consumes one liter of water per minute (if there is no water, it is not working). It can hold no more than \(c\) liters. Adilbek has already poured \(c_0\) liters of water into the system. He is going to start watering the garden right now and water it for \(m\) minutes, and the watering system should contain at least one liter of water at the beginning of the \(i\)-th minute (for every \(i\) from \(0\) to \(m - 1\)).Now Adilbek wonders what he will do if the watering system runs out of water. He called \(n\) his friends and asked them if they are going to bring some water. The \(i\)-th friend answered that he can bring no more than \(a_i\) liters of water; he will arrive at the beginning of the \(t_i\)-th minute and pour all the water he has into the system (if the system cannot hold such amount of water, the excess water is poured out); and then he will ask Adilbek to pay \(b_i\) dollars for each liter of water he has brought. You may assume that if a friend arrives at the beginning of the \(t_i\)-th minute and the system runs out of water at the beginning of the same minute, the friend pours his water fast enough so that the system does not stop working.Of course, Adilbek does not want to pay his friends, but he has to water the garden. So he has to tell his friends how much water should they bring. Formally, Adilbek wants to choose \(n\) integers \(k_1\), \(k_2\), ..., \(k_n\) in such a way that: if each friend \(i\) brings exactly \(k_i\) liters of water, then the watering system works during the whole time required to water the garden; the sum \(\sum\limits_{i = 1}^{n} k_i b_i\) is minimum possible. Help Adilbek to determine the minimum amount he has to pay his friends or determine that Adilbek not able to water the garden for \(m\) minutes.You have to answer \(q\) independent queries.
|
The first line contains one integer \(q\) (\(1 \le q \le 5 \cdot 10^5\)) – the number of queries.The first line of each query contains four integers \(n, m, c\) and \(c_0\) (\(0 \le n \le 5 \cdot 10^5, 2 \le m \le 10^9, 1 \le c_0 \le c \le 10^9\)) — the number of friends, the number of minutes of watering, the capacity of the watering system and the number of liters poured by Adilbek.Each of the next \(n\) lines contains three integers \(t_i, a_i, b_i\) (\( 0 < t_i < m, 1 \le a_i \le c, 1 \le b_i \le 10^9\)) — the \(i\)-th friend's arrival time, the maximum amount of water \(i\)-th friend can bring and the cost of \(1\) liter from \(i\)-th friend.It is guaranteed that sum of all \(n\) over all queries does not exceed \(5 \cdot 10^5\).
|
For each query print one integer — the minimum amount Adilbek has to pay his friends, or \(-1\) if Adilbek is not able to water the garden for \(m\) minutes.
|
Input: 4 1 5 4 2 2 4 2 0 4 5 4 2 5 3 1 1 2 4 3 1 3 2 3 5 1 2 1 1 1 4 3 | Output: 6 0 -1 4
|
Master
| 3 | 2,025 | 744 | 157 | 12 |
|
329 |
B
|
329B
|
B. Biridian Forest
| 1,500 |
dfs and similar; shortest paths
|
You're a mikemon breeder currently in the middle of your journey to become a mikemon master. Your current obstacle is go through the infamous Biridian Forest.The forestThe Biridian Forest is a two-dimensional grid consisting of r rows and c columns. Each cell in Biridian Forest may contain a tree, or may be vacant. A vacant cell may be occupied by zero or more mikemon breeders (there may also be breeders other than you in the forest). Mikemon breeders (including you) cannot enter cells with trees. One of the cells is designated as the exit cell.The initial grid, including your initial position, the exit cell, and the initial positions of all other breeders, will be given to you. Here's an example of such grid (from the first example): MovesBreeders (including you) may move in the forest. In a single move, breeders may perform one of the following actions: Do nothing. Move from the current cell to one of the four adjacent cells (two cells are adjacent if they share a side). Note that breeders cannot enter cells with trees. If you are located on the exit cell, you may leave the forest. Only you can perform this move — all other mikemon breeders will never leave the forest by using this type of movement. After each time you make a single move, each of the other breeders simultaneously make a single move (the choice of which move to make may be different for each of the breeders).Mikemon battleIf you and t (t > 0) mikemon breeders are located on the same cell, exactly t mikemon battles will ensue that time (since you will be battling each of those t breeders once). After the battle, all of those t breeders will leave the forest to heal their respective mikemons.Note that the moment you leave the forest, no more mikemon battles can ensue, even if another mikemon breeder move to the exit cell immediately after that. Also note that a battle only happens between you and another breeders — there will be no battle between two other breeders (there may be multiple breeders coexisting in a single cell).Your goalYou would like to leave the forest. In order to do so, you have to make a sequence of moves, ending with a move of the final type. Before you make any move, however, you post this sequence on your personal virtual idol Blog. Then, you will follow this sequence of moves faithfully.Goal of other breedersBecause you post the sequence in your Blog, the other breeders will all know your exact sequence of moves even before you make your first move. All of them will move in such way that will guarantee a mikemon battle with you, if possible. The breeders that couldn't battle you will do nothing.Your taskPrint the minimum number of mikemon battles that you must participate in, assuming that you pick the sequence of moves that minimize this number. Note that you are not required to minimize the number of moves you make.
|
The first line consists of two integers: r and c (1 ≤ r, c ≤ 1000), denoting the number of rows and the number of columns in Biridian Forest. The next r rows will each depict a row of the map, where each character represents the content of a single cell: 'T': A cell occupied by a tree. 'S': An empty cell, and your starting position. There will be exactly one occurence of this in the map. 'E': An empty cell, and where the exit is located. There will be exactly one occurence of this in the map. A digit (0-9): A cell represented by a digit X means that the cell is empty and is occupied by X breeders (in particular, if X is zero, it means that the cell is not occupied by any breeder). It is guaranteed that it will be possible for you to go from your starting position to the exit cell through a sequence of moves.
|
A single line denoted the minimum possible number of mikemon battles that you have to participate in if you pick a strategy that minimize this number.
|
The following picture illustrates the first example. The blue line denotes a possible sequence of moves that you should post in your blog: The three breeders on the left side of the map will be able to battle you — the lone breeder can simply stay in his place until you come while the other two breeders can move to where the lone breeder is and stay there until you come. The three breeders on the right does not have a way to battle you, so they will stay in their place.For the second example, you should post this sequence in your Blog: Here's what happens. First, you move one cell to the right. Then, the two breeders directly to the right of the exit will simultaneously move to the left. The other three breeder cannot battle you so they will do nothing. You end up in the same cell with 2 breeders, so 2 mikemon battles are conducted. After those battles, all of your opponents leave the forest. Finally, you make another move by leaving the forest.
|
Input: 5 7000E0T3T0TT0T0010T0T02T0T0T00T0S000 | Output: 3
|
Medium
| 2 | 2,857 | 819 | 150 | 3 |
1,706 |
E
|
1706E
|
E. Qpwoeirut and Vertices
| 2,300 |
binary search; data structures; dfs and similar; divide and conquer; dsu; greedy; trees
|
You are given a connected undirected graph with \(n\) vertices and \(m\) edges. Vertices of the graph are numbered by integers from \(1\) to \(n\) and edges of the graph are numbered by integers from \(1\) to \(m\).Your task is to answer \(q\) queries, each consisting of two integers \(l\) and \(r\). The answer to each query is the smallest non-negative integer \(k\) such that the following condition holds: For all pairs of integers \((a, b)\) such that \(l\le a\le b\le r\), vertices \(a\) and \(b\) are reachable from one another using only the first \(k\) edges (that is, edges \(1, 2, \ldots, k\)).
|
The first line contains a single integer \(t\) (\(1\le t\le 1000\)) — the number of test cases.The first line of each test case contains three integers \(n\), \(m\), and \(q\) (\(2\le n\le 10^5\), \(1\le m, q\le 2\cdot 10^5\)) — the number of vertices, edges, and queries respectively.Each of the next \(m\) lines contains two integers \(u_i\) and \(v_i\) (\(1\le u_i, v_i\le n\)) — ends of the \(i\)-th edge.It is guaranteed that the graph is connected and there are no multiple edges or self-loops.Each of the next \(q\) lines contains two integers \(l\) and \(r\) (\(1\le l\le r\le n\)) — descriptions of the queries.It is guaranteed that that the sum of \(n\) over all test cases does not exceed \(10^5\), the sum of \(m\) over all test cases does not exceed \(2\cdot 10^5\), and the sum of \(q\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, print \(q\) integers — the answers to the queries.
|
Graph from the first test case. The integer near the edge is its number. In the first test case, the graph contains \(2\) vertices and a single edge connecting vertices \(1\) and \(2\).In the first query, \(l=1\) and \(r=1\). It is possible to reach any vertex from itself, so the answer to this query is \(0\).In the second query, \(l=1\) and \(r=2\). Vertices \(1\) and \(2\) are reachable from one another using only the first edge, through the path \(1 \longleftrightarrow 2\). It is impossible to reach vertex \(2\) from vertex \(1\) using only the first \(0\) edges. So, the answer to this query is \(1\). Graph from the second test case. The integer near the edge is its number. In the second test case, the graph contains \(5\) vertices and \(5\) edges.In the first query, \(l=1\) and \(r=4\). It is enough to use the first \(3\) edges to satisfy the condition from the statement: Vertices \(1\) and \(2\) are reachable from one another through the path \(1 \longleftrightarrow 2\) (edge \(1\)). Vertices \(1\) and \(3\) are reachable from one another through the path \(1 \longleftrightarrow 3\) (edge \(2\)). Vertices \(1\) and \(4\) are reachable from one another through the path \(1 \longleftrightarrow 2 \longleftrightarrow 4\) (edges \(1\) and \(3\)). Vertices \(2\) and \(3\) are reachable from one another through the path \(2 \longleftrightarrow 1 \longleftrightarrow 3\) (edges \(1\) and \(2\)). Vertices \(2\) and \(4\) are reachable from one another through the path \(2 \longleftrightarrow 4\) (edge \(3\)). Vertices \(3\) and \(4\) are reachable from one another through the path \(3 \longleftrightarrow 1 \longleftrightarrow 2 \longleftrightarrow 4\) (edges \(2\), \(1\), and \(3\)). If we use less than \(3\) of the first edges, then the condition won't be satisfied. For example, it is impossible to reach vertex \(4\) from vertex \(1\) using only the first \(2\) edges. So, the answer to this query is \(3\).In the second query, \(l=3\) and \(r=4\). Vertices \(3\) and \(4\) are reachable from one another through the path \(3 \longleftrightarrow 1 \longleftrightarrow 2 \longleftrightarrow 4\) (edges \(2\), \(1\), and \(3\)). If we use any fewer of the first edges, nodes \(3\) and \(4\) will not be reachable from one another.
|
Input: 32 1 21 21 11 25 5 51 21 32 43 43 51 43 42 22 53 53 2 11 32 31 3 | Output: 0 1 3 3 0 5 5 2
|
Expert
| 7 | 606 | 853 | 70 | 17 |
284 |
A
|
284A
|
A. Cows and Primitive Roots
| 1,400 |
implementation; math; number theory
|
The cows have just learned what a primitive root is! Given a prime p, a primitive root is an integer x (1 ≤ x < p) such that none of integers x - 1, x2 - 1, ..., xp - 2 - 1 are divisible by p, but xp - 1 - 1 is. Unfortunately, computing primitive roots can be time consuming, so the cows need your help. Given a prime p, help the cows find the number of primitive roots .
|
The input contains a single line containing an integer p (2 ≤ p < 2000). It is guaranteed that p is a prime.
|
Output on a single line the number of primitive roots .
|
The only primitive root is 2.The primitive roots are 2 and 3.
|
Input: 3 | Output: 1
|
Easy
| 3 | 371 | 108 | 55 | 2 |
28 |
D
|
28D
|
D. Don't fear, DravDe is kind
| 2,400 |
binary search; data structures; dp; hashing
|
A motorcade of n trucks, driving from city «Z» to city «З», has approached a tunnel, known as Tunnel of Horror. Among truck drivers there were rumours about monster DravDe, who hunts for drivers in that tunnel. Some drivers fear to go first, others - to be the last, but let's consider the general case. Each truck is described with four numbers: v — value of the truck, of its passangers and cargo c — amount of passanger on the truck, the driver included l — total amount of people that should go into the tunnel before this truck, so that the driver can overcome his fear («if the monster appears in front of the motorcade, he'll eat them first») r — total amount of people that should follow this truck, so that the driver can overcome his fear («if the monster appears behind the motorcade, he'll eat them first»). Since the road is narrow, it's impossible to escape DravDe, if he appears from one side. Moreover, the motorcade can't be rearranged. The order of the trucks can't be changed, but it's possible to take any truck out of the motorcade, and leave it near the tunnel for an indefinite period. You, as the head of the motorcade, should remove some of the trucks so, that the rest of the motorcade can move into the tunnel and the total amount of the left trucks' values is maximal.
|
The first input line contains integer number n (1 ≤ n ≤ 105) — amount of trucks in the motorcade. The following n lines contain four integers each. Numbers in the i-th line: vi, ci, li, ri (1 ≤ vi ≤ 104, 1 ≤ ci ≤ 105, 0 ≤ li, ri ≤ 105) — describe the i-th truck. The trucks are numbered from 1, counting from the front of the motorcade.
|
In the first line output number k — amount of trucks that will drive into the tunnel. In the second line output k numbers — indexes of these trucks in ascending order. Don't forget please that you are not allowed to change the order of trucks. If the answer is not unique, output any.
|
Input: 51 1 0 31 1 1 21 1 2 11 1 3 02 1 3 0 | Output: 41 2 3 5
|
Expert
| 4 | 1,296 | 336 | 284 | 0 |
|
1,303 |
A
|
1303A
|
A. Erasing Zeroes
| 800 |
implementation; strings
|
You are given a string \(s\). Each character is either 0 or 1.You want all 1's in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all 1's form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.You may erase some (possibly none) 0's from the string. What is the minimum number of 0's that you have to erase?
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) — the number of test cases.Then \(t\) lines follow, each representing a test case. Each line contains one string \(s\) (\(1 \le |s| \le 100\)); each character of \(s\) is either 0 or 1.
|
Print \(t\) integers, where the \(i\)-th integer is the answer to the \(i\)-th testcase (the minimum number of 0's that you have to erase from \(s\)).
|
In the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111).
|
Input: 3 010011 0 1111000 | Output: 2 0 0
|
Beginner
| 2 | 421 | 249 | 150 | 13 |
731 |
B
|
731B
|
B. Coupons and Discounts
| 1,100 |
constructive algorithms; greedy
|
The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day.There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.
|
The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions.The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.
|
If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print ""YES"" (without quotes) in the only line of output. Otherwise, print ""NO"" (without quotes).
|
In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.
|
Input: 41 2 1 2 | Output: YES
|
Easy
| 2 | 1,413 | 237 | 224 | 7 |
6 |
C
|
6C
|
C. Alice, Bob and Chocolate
| 1,200 |
greedy; two pointers
|
Alice and Bob like games. And now they are ready to start a new game. They have placed n chocolate bars in a line. Alice starts to eat chocolate bars one by one from left to right, and Bob — from right to left. For each chocololate bar the time, needed for the player to consume it, is known (Alice and Bob eat them with equal speed). When the player consumes a chocolate bar, he immediately starts with another. It is not allowed to eat two chocolate bars at the same time, to leave the bar unfinished and to make pauses. If both players start to eat the same bar simultaneously, Bob leaves it to Alice as a true gentleman.How many bars each of the players will consume?
|
The first line contains one integer n (1 ≤ n ≤ 105) — the amount of bars on the table. The second line contains a sequence t1, t2, ..., tn (1 ≤ ti ≤ 1000), where ti is the time (in seconds) needed to consume the i-th bar (in the order from left to right).
|
Print two numbers a and b, where a is the amount of bars consumed by Alice, and b is the amount of bars consumed by Bob.
|
Input: 52 9 8 2 7 | Output: 2 3
|
Easy
| 2 | 671 | 255 | 120 | 0 |
|
514 |
A
|
514A
|
A. Chewbaсca and Number
| 1,200 |
greedy; implementation
|
Luke Skywalker gave Chewbacca an integer number x. Chewbacca isn't good at numbers but he loves inverting digits in them. Inverting digit t means replacing it with digit 9 - t. Help Chewbacca to transform the initial number x to the minimum possible positive number by inverting some (possibly, zero) digits. The decimal representation of the final number shouldn't start with a zero.
|
The first line contains a single integer x (1 ≤ x ≤ 1018) — the number that Luke Skywalker gave to Chewbacca.
|
Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes.
|
Input: 27 | Output: 22
|
Easy
| 2 | 384 | 109 | 142 | 5 |
|
899 |
B
|
899B
|
B. Months and Years
| 1,200 |
implementation
|
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.In this problem you are given n (1 ≤ n ≤ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
|
The first line contains single integer n (1 ≤ n ≤ 24) — the number of integers.The second line contains n integers a1, a2, ..., an (28 ≤ ai ≤ 31) — the numbers you are to check.
|
If there are several consecutive months that fit the sequence, print ""YES"" (without quotes). Otherwise, print ""NO"" (without quotes).You can print each letter in arbitrary case (small or large).
|
In the first example the integers can denote months July, August, September and October.In the second example the answer is no, because there are no two consecutive months each having 30 days.In the third example the months are: February (leap year) — March — April – May — June.In the fourth example the number of days in the second month is 28, so this is February. March follows February and has 31 days, but not 30, so the answer is NO.In the fifth example the months are: December — January — February (non-leap year).
|
Input: 431 31 30 31 | Output: Yes
|
Easy
| 1 | 936 | 177 | 197 | 8 |
1,955 |
G
|
1955G
|
G. GCD on a grid
| 1,900 |
brute force; dfs and similar; dp; implementation; math; number theory
|
Not long ago, Egor learned about the Euclidean algorithm for finding the greatest common divisor of two numbers. The greatest common divisor of two numbers \(a\) and \(b\) is the largest number that divides both \(a\) and \(b\) without leaving a remainder. With this knowledge, Egor can solve a problem that he once couldn't.Vasily has a grid with \(n\) rows and \(m\) columns, and the integer \({a_i}_j\) is located at the intersection of the \(i\)-th row and the \(j\)-th column. Egor wants to go from the top left corner (at the intersection of the first row and the first column) to the bottom right corner (at the intersection of the last row and the last column) and find the greatest common divisor of all the numbers along the path. He is only allowed to move down and to the right. Egor has written down several paths and obtained different GCD values. He became interested in finding the maximum possible GCD.Unfortunately, Egor is tired of calculating GCDs, so he asks for your help in finding the maximum GCD of the integers along the path from the top left corner to the bottom right corner of the grid.
|
The first line contains an integer \(t\) (\(1 \le t \le {10}^{4}\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 100\)) — the number of rows and columns of the grid.Then, there are \(n\) lines, where the \(i\)-th line contains \(m\) integers \((1 \le a_{i,j} \le {10}^{6}\)) — the integers written in the \(i\)-th row and the \(j\)-th column of the grid.It is guaranteed that the sum of \(n \cdot m\) does not exceed \(2 \cdot {10}^{5}\) over all test cases.
|
For each test case, output the maximum possible GCD along the path from the top left cell to the bottom right cell in a separate line.
|
Input: 32 330 20 3015 25 403 312 4 93 12 28 3 122 42 4 6 81 3 6 9 | Output: 10 3 1
|
Hard
| 6 | 1,116 | 529 | 134 | 19 |
|
685 |
B
|
685B
|
B. Kay and Snowflake
| 1,900 |
data structures; dfs and similar; dp; trees
|
After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes.Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of n nodes. The root of tree has index 1. Kay is very interested in the structure of this tree.After doing some research he formed q queries he is interested in. The i-th query asks to find a centroid of the subtree of the node vi. Your goal is to answer all queries.Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node v is formed by nodes u, such that node v is present on the path from u to root.Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree).
|
The first line of the input contains two integers n and q (2 ≤ n ≤ 300 000, 1 ≤ q ≤ 300 000) — the size of the initial tree and the number of queries respectively.The second line contains n - 1 integer p2, p3, ..., pn (1 ≤ pi ≤ n) — the indices of the parents of the nodes from 2 to n. Node 1 is a root of the tree. It's guaranteed that pi define a correct tree.Each of the following q lines contain a single integer vi (1 ≤ vi ≤ n) — the index of the node, that define the subtree, for which we want to find a centroid.
|
For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid.
|
The first query asks for a centroid of the whole tree — this is node 3. If we delete node 3 the tree will split in four components, two of size 1 and two of size 2.The subtree of the second node consists of this node only, so the answer is 2.Node 3 is centroid of its own subtree.The centroids of the subtree of the node 5 are nodes 5 and 6 — both answers are considered correct.
|
Input: 7 41 1 3 3 5 31235 | Output: 3236
|
Hard
| 4 | 946 | 520 | 189 | 6 |
1,230 |
A
|
1230A
|
A. Dawid and Bags of Candies
| 800 |
brute force; implementation
|
Dawid has four bags of candies. The \(i\)-th of them contains \(a_i\) candies. Also, Dawid has two friends. He wants to give each bag to one of his two friends. Is it possible to distribute the bags in such a way that each friend receives the same amount of candies in total?Note, that you can't keep bags for yourself or throw them away, each bag should be given to one of the friends.
|
The only line contains four integers \(a_1\), \(a_2\), \(a_3\) and \(a_4\) (\(1 \leq a_i \leq 100\)) — the numbers of candies in each bag.
|
Output YES if it's possible to give the bags to Dawid's friends so that both friends receive the same amount of candies, or NO otherwise. Each character can be printed in any case (either uppercase or lowercase).
|
In the first sample test, Dawid can give the first and the third bag to the first friend, and the second and the fourth bag to the second friend. This way, each friend will receive \(12\) candies.In the second sample test, it's impossible to distribute the bags.
|
Input: 1 7 11 5 | Output: YES
|
Beginner
| 2 | 386 | 138 | 212 | 12 |
1,878 |
C
|
1878C
|
C. Vasilije in Cacak
| 900 |
math
|
Aca and Milovan, two fellow competitive programmers, decided to give Vasilije a problem to test his skills.Vasilije is given three positive integers: \(n\), \(k\), and \(x\), and he has to determine if he can choose \(k\) distinct integers between \(1\) and \(n\), such that their sum is equal to \(x\).Since Vasilije is now in the weirdest city in Serbia where Aca and Milovan live, Cacak, the problem seems weird to him. So he needs your help with this problem.
|
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 three integers \(n\), \(k\) and \(x\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le n\), \(1 \le x \le 4 \cdot 10^{10}\)) — the maximum element he can choose, the number of elements he can choose and the sum he has to reach.Note that the sum of \(n\) over all test cases may exceed \(2 \cdot 10^5\).
|
For each test case output one line: ""YES"", if it is possible to choose \(k\) distinct integers between \(1\) and \(n\), such that their sum is equal to \(x\), and ""NO"", if it isn't. You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
|
In the first test case \(n = 5,\ k=3,\ x=10\), so we can choose the numbers: \(2\), \(3\), \(5\), whose sum is \(10\), so the answer is ""YES"".In the second test case \(n = 5, \ k=3, \ x=3\), there is no three numbers which satisfies the condition, so the answer is ""NO"". It can be shown that there are no three numbers whose sum is \(3\).
|
Input: 125 3 105 3 310 10 556 5 202 1 26187856 87856 2609202300200000 190000 1900000000028 5 20042 2 20069 6 4047202 32455 613407217185977 145541 15770805980 | Output: YES NO YES YES NO NO YES NO NO NO YES YES
|
Beginner
| 1 | 463 | 436 | 330 | 18 |
1,575 |
D
|
1575D
|
D. Divisible by Twenty-Five
| 1,800 |
brute force; dfs and similar; dp
|
Mr. Chanek has an integer represented by a string \(s\). Zero or more digits have been erased and are denoted by the character _. There are also zero or more digits marked by the character X, meaning they're the same digit.Mr. Chanek wants to count the number of possible integer \(s\), where \(s\) is divisible by \(25\). Of course, \(s\) must not contain any leading zero. He can replace the character _ with any digit. He can also replace the character X with any digit, but it must be the same for every character X.As a note, a leading zero is any 0 digit that comes before the first nonzero digit in a number string in positional notation. For example, 0025 has two leading zeroes. An exception is the integer zero, (0 has no leading zero, but 0000 has three leading zeroes).
|
One line containing the string \(s\) (\(1 \leq |s| \leq 8\)). The string \(s\) consists of the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, _, and X.
|
Output an integer denoting the number of possible integer \(s\).
|
In the first example, the only possible \(s\) is \(25\).In the second and third example, \(s \in \{100, 200,300,400,500,600,700,800,900\}\).In the fifth example, all possible \(s\) will have at least one leading zero.
|
Input: 25 | Output: 1
|
Medium
| 3 | 781 | 145 | 64 | 15 |
1,526 |
C1
|
1526C1
|
C1. Potions (Easy Version)
| 1,500 |
brute force; data structures; dp; greedy
|
This is the easy version of the problem. The only difference is that in this version \(n \leq 2000\). You can make hacks only if both versions of the problem are solved.There are \(n\) potions in a line, with potion \(1\) on the far left and potion \(n\) on the far right. Each potion will increase your health by \(a_i\) when drunk. \(a_i\) can be negative, meaning that potion will decrease will health.You start with \(0\) health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.What is the largest number of potions you can drink?
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 2000\)) — the number of potions. The next line contains \(n\) integers \(a_1\), \(a_2\), ... ,\(a_n\) (\(-10^9 \leq a_i \leq 10^9\)) which represent the change in health after drinking that potion.
|
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
|
For the sample, you can drink \(5\) potions by taking potions \(1\), \(3\), \(4\), \(5\) and \(6\). It is not possible to drink all \(6\) potions because your health will go negative at some point
|
Input: 6 4 -4 1 -3 1 -3 | Output: 5
|
Medium
| 4 | 671 | 261 | 107 | 15 |
519 |
E
|
519E
|
E. A and B and Lecture Rooms
| 2,100 |
binary search; data structures; dfs and similar; dp; trees
|
A and B are preparing themselves for programming contests.The University where A and B study is a set of rooms connected by corridors. Overall, the University has n rooms connected by n - 1 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to n.Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them.As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following m days.
|
The first line contains integer n (1 ≤ n ≤ 105) — the number of rooms in the University.The next n - 1 lines describe the corridors. The i-th of these lines (1 ≤ i ≤ n - 1) contains two integers ai and bi (1 ≤ ai, bi ≤ n), showing that the i-th corridor connects rooms ai and bi.The next line contains integer m (1 ≤ m ≤ 105) — the number of queries.Next m lines describe the queries. The j-th of these lines (1 ≤ j ≤ m) contains two integers xj and yj (1 ≤ xj, yj ≤ n) that means that on the j-th day A will write the contest in the room xj, B will write in the room yj.
|
In the i-th (1 ≤ i ≤ m) line print the number of rooms that are equidistant from the rooms where A and B write contest on the i-th day.
|
in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
|
Input: 41 21 32 412 3 | Output: 1
|
Hard
| 5 | 841 | 571 | 135 | 5 |
615 |
A
|
615A
|
A. Bulbs
| 800 |
implementation
|
Vasya wants to turn on Christmas lights consisting of m bulbs. Initially, all bulbs are turned off. There are n buttons, each of them is connected to some set of bulbs. Vasya can press any of these buttons. When the button is pressed, it turns on all the bulbs it's connected to. Can Vasya light up all the bulbs?If Vasya presses the button such that some bulbs connected to it are already turned on, they do not change their state, i.e. remain turned on.
|
The first line of the input contains integers n and m (1 ≤ n, m ≤ 100) — the number of buttons and the number of bulbs respectively. Each of the next n lines contains xi (0 ≤ xi ≤ m) — the number of bulbs that are turned on by the i-th button, and then xi numbers yij (1 ≤ yij ≤ m) — the numbers of these bulbs.
|
If it's possible to turn on all m bulbs print ""YES"", otherwise print ""NO"".
|
In the first sample you can press each button once and turn on all the bulbs. In the 2 sample it is impossible to turn on the 3-rd lamp.
|
Input: 3 42 1 43 1 3 11 2 | Output: YES
|
Beginner
| 1 | 455 | 311 | 78 | 6 |
1,015 |
A
|
1015A
|
A. Points in Segments
| 800 |
implementation
|
You are given a set of \(n\) segments on the axis \(Ox\), each segment has integer endpoints between \(1\) and \(m\) inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le m\)) — coordinates of the left and of the right endpoints. Consider all integer points between \(1\) and \(m\) inclusive. Your task is to print all such points that don't belong to any segment. The point \(x\) belongs to the segment \([l; r]\) if and only if \(l \le x \le r\).
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 100\)) — the number of segments and the upper bound for coordinates.The next \(n\) lines contain two integers each \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le m\)) — the endpoints of the \(i\)-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that \(l_i=r_i\), i.e. a segment can degenerate to a point.
|
In the first line print one integer \(k\) — the number of points that don't belong to any segment.In the second line print exactly \(k\) integers in any order — the points that don't belong to any segment. All points you print should be distinct.If there are no such points at all, print a single integer \(0\) in the first line and either leave the second line empty or do not print it at all.
|
In the first example the point \(1\) belongs to the second segment, the point \(2\) belongs to the first and the second segments and the point \(5\) belongs to the third segment. The points \(3\) and \(4\) do not belong to any segment.In the second example all the points from \(1\) to \(7\) belong to the first segment.
|
Input: 3 52 21 25 5 | Output: 23 4
|
Beginner
| 1 | 562 | 436 | 394 | 10 |
1,370 |
C
|
1370C
|
C. Number Game
| 1,400 |
games; math; number theory
|
Ashishgup and FastestFinger play a game. They start with a number \(n\) and play in turns. In each turn, a player can make any one of the following moves: Divide \(n\) by any of its odd divisors greater than \(1\). Subtract \(1\) from \(n\) if \(n\) is greater than \(1\). Divisors of a number include the number itself.The player who is unable to make a move loses the game.Ashishgup moves first. Determine the winner of the game if both of them play optimally.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) — the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer — \(n\) (\(1 \leq n \leq 10^9\)).
|
For each test case, print ""Ashishgup"" if he wins, and ""FastestFinger"" otherwise (without quotes).
|
In the first test case, \(n = 1\), Ashishgup cannot make a move. He loses.In the second test case, \(n = 2\), Ashishgup subtracts \(1\) on the first move. Now \(n = 1\), FastestFinger cannot make a move, so he loses.In the third test case, \(n = 3\), Ashishgup divides by \(3\) on the first move. Now \(n = 1\), FastestFinger cannot make a move, so he loses.In the last test case, \(n = 12\), Ashishgup divides it by \(3\). Now \(n = 4\), FastestFinger is forced to subtract \(1\), and Ashishgup gets \(3\), so he wins by dividing it by \(3\).
|
Input: 7 1 2 3 4 5 6 12 | Output: FastestFinger Ashishgup Ashishgup FastestFinger Ashishgup FastestFinger Ashishgup
|
Easy
| 3 | 462 | 232 | 101 | 13 |
1,510 |
K
|
1510K
|
K. King's Task
| 1,200 |
brute force; graphs; implementation
|
The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task.There is a permutation \(p_i\) of numbers from 1 to \(2n\). You can make two types of operations. Swap \(p_1\) and \(p_2\), \(p_3\) and \(p_4\), ..., \(p_{2n-1}\) and \(p_{2n}\). Swap \(p_1\) and \(p_{n+1}\), \(p_2\) and \(p_{n+2}\), ..., \(p_{n}\) and \(p_{2n}\). The task is to find the minimal number of operations required to sort the given permutation.The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task.
|
The first line contains the integer \(n\) (\(1\le n\le 1000\)). The second line contains \(2n\) integers \(p_i\) — the permutation of numbers from 1 to \(2n\).
|
Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print \(-1\).
|
In the first example, you can sort the permutation in three operations: Make operation 1: \(3, 6, 5, 2, 1, 4\). Make operation 2: \(2, 1, 4, 3, 6, 5\). Make operation 1: \(1, 2, 3, 4, 5, 6\).
|
Input: 3 6 3 2 5 4 1 | Output: 3
|
Easy
| 3 | 693 | 159 | 168 | 15 |
551 |
D
|
551D
|
D. GukiZ and Binary Operations
| 2,100 |
combinatorics; implementation; math; matrices; number theory
|
We all know that GukiZ often plays with arrays. Now he is thinking about this problem: how many arrays a, of length n, with non-negative elements strictly less then 2l meet the following condition: ? Here operation means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &), operation means bitwise OR (in Pascal it is equivalent to , in C/C++/Java/Python it is equivalent to |). Because the answer can be quite large, calculate it modulo m. This time GukiZ hasn't come up with solution, and needs you to help him!
|
First and the only line of input contains four integers n, k, l, m (2 ≤ n ≤ 1018, 0 ≤ k ≤ 1018, 0 ≤ l ≤ 64, 1 ≤ m ≤ 109 + 7).
|
In the single line print the number of arrays satisfying the condition above modulo m.
|
In the first sample, satisfying arrays are {1, 1}, {3, 1}, {1, 3}.In the second sample, only satisfying array is {1, 1}.In the third sample, satisfying arrays are {0, 3, 3}, {1, 3, 2}, {1, 3, 3}, {2, 3, 1}, {2, 3, 3}, {3, 3, 0}, {3, 3, 1}, {3, 3, 2}, {3, 3, 3}.
|
Input: 2 1 2 10 | Output: 3
|
Hard
| 5 | 553 | 125 | 86 | 5 |
1,066 |
A
|
1066A
|
A. Vova and Train
| 1,100 |
math
|
Vova plans to go to the conference by train. Initially, the train is at the point \(1\) and the destination point of the path is the point \(L\). The speed of the train is \(1\) length unit per minute (i.e. at the first minute the train is at the point \(1\), at the second minute — at the point \(2\) and so on).There are lanterns on the path. They are placed at the points with coordinates divisible by \(v\) (i.e. the first lantern is at the point \(v\), the second is at the point \(2v\) and so on).There is also exactly one standing train which occupies all the points from \(l\) to \(r\) inclusive.Vova can see the lantern at the point \(p\) if \(p\) is divisible by \(v\) and there is no standing train at this position (\(p \not\in [l; r]\)). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can't see this lantern.Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to \(t\) different conferences, so you should answer \(t\) independent queries.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of queries.Then \(t\) lines follow. The \(i\)-th line contains four integers \(L_i, v_i, l_i, r_i\) (\(1 \le L, v \le 10^9\), \(1 \le l \le r \le L\)) — destination point of the \(i\)-th path, the period of the lantern appearance and the segment occupied by the standing train.
|
Print \(t\) lines. The \(i\)-th line should contain one integer — the answer for the \(i\)-th query.
|
For the first example query, the answer is \(3\). There are lanterns at positions \(2\), \(4\), \(6\), \(8\) and \(10\), but Vova didn't see the lanterns at positions \(4\) and \(6\) because of the standing train.For the second example query, the answer is \(0\) because the only lantern is at the point \(51\) and there is also a standing train at this point.For the third example query, the answer is \(1134\) because there are \(1234\) lanterns, but Vova didn't see the lanterns from the position \(100\) to the position \(199\) inclusive.For the fourth example query, the answer is \(0\) because the standing train covers the whole path.
|
Input: 410 2 3 7100 51 51 511234 1 100 1991000000000 1 1 1000000000 | Output: 3011340
|
Easy
| 1 | 1,040 | 368 | 100 | 10 |
1,738 |
B
|
1738B
|
B. Prefix Sum Addicts
| 1,200 |
constructive algorithms; greedy; math; sortings
|
Suppose \(a_1, a_2, \dots, a_n\) is a sorted integer sequence of length \(n\) such that \(a_1 \leq a_2 \leq \dots \leq a_n\). For every \(1 \leq i \leq n\), the prefix sum \(s_i\) of the first \(i\) terms \(a_1, a_2, \dots, a_i\) is defined by $$$\( s_i = \sum_{k=1}^i a_k = a_1 + a_2 + \dots + a_i. \)\(Now you are given the last \)k\( terms of the prefix sums, which are \)s_{n-k+1}, \dots, s_{n-1}, s_{n}\(. Your task is to determine whether this is possible. Formally, given \)k\( integers \)s_{n-k+1}, \dots, s_{n-1}, s_{n}\(, the task is to check whether there is a sequence \)a_1, a_2, \dots, a_n\( such that \)a_1 \leq a_2 \leq \dots \leq a_n\(, and \)s_i = a_1 + a_2 + \dots + a_i\( for all \)n-k+1 \leq i \leq n$$$.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^5\)) — the number of test cases. The following lines contain the description of each test case.The first line of each test case contains two integers \(n\) (\(1 \leq n \leq 10^5\)) and \(k\) (\(1 \leq k \leq n\)), indicating the length of the sequence \(a\) and the number of terms of prefix sums, respectively.The second line of each test case contains \(k\) integers \(s_{n-k+1}, \dots, s_{n-1}, s_{n}\) (\(-10^9 \leq s_i \leq 10^9\) for every \(n-k+1 \leq i \leq n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output ""YES"" (without quotes) if it is possible and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive response).
|
In the first test case, we have the only sequence \(a = [1, 1, 1, 1, 1]\).In the second test case, we can choose, for example, \(a = [-3, -2, -1, 0, 1, 2, 3]\).In the third test case, the prefix sums define the only sequence \(a = [2, 1, 1]\), but it is not sorted. In the fourth test case, it can be shown that there is no sequence with the given prefix sums.
|
Input: 45 51 2 3 4 57 4-6 -5 -3 03 32 3 43 23 4 | Output: Yes Yes No No
|
Easy
| 4 | 725 | 656 | 248 | 17 |
899 |
C
|
899C
|
C. Dividing the numbers
| 1,300 |
constructive algorithms; graphs; math
|
Petya has n integers: 1, 2, 3, ..., n. He wants to split these integers in two non-empty groups in such a way that the absolute difference of sums of integers in each group is as small as possible. Help Petya to split the integers. Each of n integers should be exactly in one group.
|
The first line contains a single integer n (2 ≤ n ≤ 60 000) — the number of integers Petya has.
|
Print the smallest possible absolute difference in the first line.In the second line print the size of the first group, followed by the integers in that group. You can print these integers in arbitrary order. If there are multiple answers, print any of them.
|
In the first example you have to put integers 1 and 4 in the first group, and 2 and 3 in the second. This way the sum in each group is 5, and the absolute difference is 0.In the second example there are only two integers, and since both groups should be non-empty, you have to put one integer in the first group and one in the second. This way the absolute difference of sums of integers in each group is 1.
|
Input: 4 | Output: 02 1 4
|
Easy
| 3 | 282 | 95 | 258 | 8 |
306 |
D
|
306D
|
D. Polygon
| 2,300 |
constructive algorithms; geometry
|
Polycarpus loves convex polygons, especially if all their angles are the same and all their sides are different. Draw for him any such polygon with the given number of vertexes.
|
The input contains a single integer n (3 ≤ n ≤ 100) — the number of the polygon vertexes.
|
Print n lines, containing the coordinates of the vertexes of the n-gon ""xi yi"" in the counter clockwise order. The coordinates of the vertexes shouldn't exceed 106 in their absolute value. The side lengths should fit within limits [1, 1000] (not necessarily integer). Mutual comparing sides and angles of your polygon during the test will go with precision of 10 - 3.If there is no solution, print ""No solution"" (without the quotes).
|
Input: 8 | Output: 1.000 0.0007.000 0.0009.000 2.0009.000 3.0005.000 7.0003.000 7.0000.000 4.0000.000 1.000
|
Expert
| 2 | 177 | 89 | 437 | 3 |
|
13 |
C
|
13C
|
C. Sequence
| 2,200 |
dp; sortings
|
Little Petya likes to play very much. And most of all he likes to play the following game:He is given a sequence of N integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.The sequence a is called non-decreasing if a1 ≤ a2 ≤ ... ≤ aN holds, where N is the length of the sequence.
|
The first line of the input contains single integer N (1 ≤ N ≤ 5000) — the length of the initial sequence. The following N lines contain one integer each — elements of the sequence. These numbers do not exceed 109 by absolute value.
|
Output one integer — minimum number of steps required to achieve the goal.
|
Input: 53 2 -1 2 11 | Output: 4
|
Hard
| 2 | 481 | 232 | 74 | 0 |
|
1,091 |
G
|
1091G
|
G. New Year and the Factorisation Collaboration
| 3,200 |
interactive; math; number theory
|
Integer factorisation is hard. The RSA Factoring Challenge offered $\(100\,000\) for factoring RSA-\(1024\), a \(1024\)-bit long product of two prime numbers. To this date, nobody was able to claim the prize. We want you to factorise a \(1024\)-bit number.Since your programming language of choice might not offer facilities for handling large integers, we will provide you with a very simple calculator. To use this calculator, you can print queries on the standard output and retrieve the results from the standard input. The operations are as follows: + x y where \(x\) and \(y\) are integers between \(0\) and \(n-1\). Returns \((x+y) \bmod n\). - x y where \(x\) and \(y\) are integers between \(0\) and \(n-1\). Returns \((x-y) \bmod n\). * x y where \(x\) and \(y\) are integers between \(0\) and \(n-1\). Returns \((x \cdot y) \bmod n\). / x y where \(x\) and \(y\) are integers between \(0\) and \(n-1\) and \(y\) is coprime with \(n\). Returns \((x \cdot y^{-1}) \bmod n\) where \(y^{-1}\) is multiplicative inverse of \(y\) modulo \(n\). If \(y\) is not coprime with \(n\), then \(-1\) is returned instead. sqrt x where \(x\) is integer between \(0\) and \(n-1\) coprime with \(n\). Returns \(y\) such that \(y^2 \bmod n = x\). If there are multiple such integers, only one of them is returned. If there are none, \(-1\) is returned instead. ^ x y where \(x\) and \(y\) are integers between \(0\) and \(n-1\). Returns \({x^y \bmod n}\). Find the factorisation of \(n\) that is a product of between \(2\) and \(10\) distinct prime numbers, all of form \(4x + 3\) for some integer \(x\).Because of technical issues, we restrict number of requests to \(100\).
|
The only line contains a single integer \(n\) (\(21 \leq n \leq 2^{1024}\)). It is guaranteed that \(n\) is a product of between \(2\) and \(10\) distinct prime numbers, all of form \(4x + 3\) for some integer \(x\).
|
You can print as many queries as you wish, adhering to the time limit (see the Interaction section for more details). When you think you know the answer, output a single line of form ! k p_1 p_2 ... p_k, where \(k\) is the number of prime factors of \(n\), and \(p_i\) are the distinct prime factors. You may print the factors in any order.Hacks inputFor hacks, use the following format:. The first should contain \(k\) (\(2 \leq k \leq 10\)) — the number of prime factors of \(n\). The second should contain \(k\) space separated integers \(p_1, p_2, \dots, p_k\) (\(21 \leq n \leq 2^{1024}\)) — the prime factors of \(n\). All prime factors have to be of form \(4x + 3\) for some integer \(x\). They all have to be distinct.
|
We start by reading the first line containing the integer \(n = 21\). Then, we ask for: \((12 + 16) \bmod 21 = 28 \bmod 21 = 7\). \((6 - 10) \bmod 21 = -4 \bmod 21 = 17\). \((8 \cdot 15) \bmod 21 = 120 \bmod 21 = 15\). \((5 \cdot 4^{-1}) \bmod 21 = (5 \cdot 16) \bmod 21 = 80 \bmod 21 = 17\). Square root of \(16\). The answer is \(11\), as \((11 \cdot 11) \bmod 21 = 121 \bmod 21 = 16\). Note that the answer may as well be \(10\). Square root of \(5\). There is no \(x\) such that \(x^2 \bmod 21 = 5\), so the output is \(-1\). \((6^{12}) \bmod 21 = 2176782336 \bmod 21 = 15\). We conclude that our calculator is working, stop fooling around and realise that \(21 = 3 \cdot 7\).
|
Input: 21717151711-115 | Output: + 12 16- 6 10* 8 15/ 5 4sqrt 16sqrt 5^ 6 12! 2 3 7
|
Master
| 3 | 1,667 | 216 | 726 | 10 |
45 |
E
|
45E
|
E. Director
| 2,000 |
constructive algorithms; greedy
|
Vasya is a born Berland film director, he is currently working on a new blockbuster, ""The Unexpected"". Vasya knows from his own experience how important it is to choose the main characters' names and surnames wisely. He made up a list of n names and n surnames that he wants to use. Vasya haven't decided yet how to call characters, so he is free to match any name to any surname. Now he has to make the list of all the main characters in the following format: ""Name1 Surname1, Name2 Surname2, ..., Namen Surnamen"", i.e. all the name-surname pairs should be separated by exactly one comma and exactly one space, and the name should be separated from the surname by exactly one space. First of all Vasya wants to maximize the number of the pairs, in which the name and the surname start from one letter. If there are several such variants, Vasya wants to get the lexicographically minimal one. Help him.An answer will be verified a line in the format as is shown above, including the needed commas and spaces. It's the lexicographical minimality of such a line that needs to be ensured. The output line shouldn't end with a space or with a comma.
|
The first input line contains number n (1 ≤ n ≤ 100) — the number of names and surnames. Then follow n lines — the list of names. Then follow n lines — the list of surnames. No two from those 2n strings match. Every name and surname is a non-empty string consisting of no more than 10 Latin letters. It is guaranteed that the first letter is uppercase and the rest are lowercase.
|
The output data consist of a single line — the needed list. Note that one should follow closely the output data format!
|
Input: 4AnnAnnaSabrinaJohnPetrovIvanovaStoltzAbacaba | Output: Ann Abacaba, Anna Ivanova, John Petrov, Sabrina Stoltz
|
Hard
| 2 | 1,149 | 379 | 119 | 0 |
|
2,065 |
C2
|
2065C2
|
C2. Skibidus and Fanum Tax (hard version)
| 1,300 |
binary search; greedy
|
This is the hard version of the problem. In this version, \(m \leq 2\cdot 10^5\).Skibidus has obtained two arrays \(a\) and \(b\), containing \(n\) and \(m\) elements respectively. For each integer \(i\) from \(1\) to \(n\), he is allowed to perform the operation at most once: Choose an integer \(j\) such that \(1 \leq j \leq m\). Set \(a_i := b_j - a_i\). Note that \(a_i\) may become non-positive as a result of this operation. Skibidus needs your help determining whether he can sort \(a\) in non-decreasing order\(^{\text{∗}}\) by performing the above operation some number of times.\(^{\text{∗}}\)\(a\) is sorted in non-decreasing order if \(a_1 \leq a_2 \leq \ldots \leq a_n\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq m \leq 2\cdot 10^5\)).The following line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)).The following line of each test case contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(1 \leq b_i \leq 10^9\)).It is guaranteed that the sum of \(n\) and the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, if it is possible to sort \(a\) in non-decreasing order, print ""YES"" on a new line. Otherwise, print ""NO"" on a new line.You can output the answer in any case. For example, the strings ""yEs"", ""yes"", and ""Yes"" will also be recognized as positive responses.
|
In the first test case, \([5]\) is already sorted.In the second test case, it can be shown that it is impossible.In the third test case, we can set \(a_2:=b_1-a_2=6-4=2\) and \(a_3:=b_3-a_3=8-6=2\). The sequence \([2,2,2,5]\) is in nondecreasing order.In the last case, we can apply operations on each index. The sequence becomes \([-1,0,1]\), which is in nondecreasing order.
|
Input: 51 359 1 10000000003 21 4 33 44 32 4 6 56 1 85 26 4 5 4 54 10003 19 8 78 | Output: YES NO YES NO YES
|
Easy
| 2 | 685 | 569 | 284 | 20 |
1,969 |
F
|
1969F
|
F. Card Pairing
| 3,000 |
dp; greedy; hashing; implementation
|
There is a deck of \(n\) cards, each card has one of \(k\) types. You are given the sequence \(a_1, a_2, \dots, a_n\) denoting the types of cards in the deck from top to bottom. Both \(n\) and \(k\) are even numbers.You play a game with these cards. First, you draw \(k\) topmost cards from the deck. Then, the following happens each turn of the game: you choose exactly two cards from your hand and play them. If these cards have the same type, you earn a coin; then, if the deck is not empty, you draw exactly two top cards from it; then, if both your hand and your deck are empty, the game ends. Otherwise, the new turn begins. You have to calculate the maximum number of coins you can earn during the game.
|
The first line of the input contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 1000\), both \(n\) and \(k\) are even).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le k\)).
|
Print one integer — the maximum number of coins you can earn.
|
Input: 4 21 2 1 2 | Output: 0
|
Master
| 4 | 710 | 226 | 61 | 19 |
|
718 |
A
|
718A
|
A. Efim and Strange Grade
| 1,700 |
dp; implementation; math
|
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all.In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.
|
The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 109) — the length of Efim's grade and the number of seconds till the end of the break respectively.The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0.
|
Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.
|
In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect.In the third sample the optimal strategy is to not perform any rounding at all.
|
Input: 6 110.245 | Output: 10.25
|
Medium
| 3 | 1,395 | 381 | 85 | 7 |
1,095 |
A
|
1095A
|
A. Repeating Cipher
| 800 |
implementation
|
Polycarp loves ciphers. He has invented his own cipher called repeating.Repeating cipher is used for strings. To encrypt the string \(s=s_{1}s_{2} \dots s_{m}\) (\(1 \le m \le 10\)), Polycarp uses the following algorithm: he writes down \(s_1\) ones, he writes down \(s_2\) twice, he writes down \(s_3\) three times, ... he writes down \(s_m\) \(m\) times. For example, if \(s\)=""bab"" the process is: ""b"" \(\to\) ""baa"" \(\to\) ""baabbb"". So the encrypted \(s\)=""bab"" is ""baabbb"".Given string \(t\) — the result of encryption of some string \(s\). Your task is to decrypt it, i. e. find the string \(s\).
|
The first line contains integer \(n\) (\(1 \le n \le 55\)) — the length of the encrypted string. The second line of the input contains \(t\) — the result of encryption of some string \(s\). It contains only lowercase Latin letters. The length of \(t\) is exactly \(n\).It is guaranteed that the answer to the test exists.
|
Print such string \(s\) that after encryption it equals \(t\).
|
Input: 6 baabbb | Output: bab
|
Beginner
| 1 | 614 | 321 | 62 | 10 |
|
1,401 |
C
|
1401C
|
C. Mere Array
| 1,300 |
constructive algorithms; math; number theory; sortings
|
You are given an array \(a_1, a_2, \dots, a_n\) where all \(a_i\) are integers and greater than \(0\). In one operation, you can choose two different indices \(i\) and \(j\) (\(1 \le i, j \le n\)). If \(gcd(a_i, a_j)\) is equal to the minimum element of the whole array \(a\), you can swap \(a_i\) and \(a_j\). \(gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\). Now you'd like to make \(a\) non-decreasing using the operation any number of times (possibly zero). Determine if you can do this. An array \(a\) is non-decreasing if and only if \(a_1 \le a_2 \le \ldots \le a_n\).
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The first line of each test case contains one integer \(n\) (\(1 \le n \le 10^5\)) — the length of array \(a\). The second line of each test case contains \(n\) positive integers \(a_1, a_2, \ldots a_n\) (\(1 \le a_i \le 10^9\)) — the array itself. It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case, output ""YES"" if it is possible to make the array \(a\) non-decreasing using the described operation, or ""NO"" if it is impossible to do so.
|
In the first and third sample, the array is already non-decreasing. In the second sample, we can swap \(a_1\) and \(a_3\) first, and swap \(a_1\) and \(a_5\) second to make the array non-decreasing. In the forth sample, we cannot the array non-decreasing using the operation.
|
Input: 4 1 8 6 4 3 6 6 2 9 4 4 5 6 7 5 7 5 2 2 4 | Output: YES YES YES NO
|
Easy
| 4 | 610 | 425 | 162 | 14 |
1,152 |
D
|
1152D
|
D. Neko and Aki's Prank
| 2,100 |
dp; greedy; trees
|
Neko is playing with his toys on the backyard of Aki's house. Aki decided to play a prank on him, by secretly putting catnip into Neko's toys. Unfortunately, he went overboard and put an entire bag of catnip into the toys...It took Neko an entire day to turn back to normal. Neko reported to Aki that he saw a lot of weird things, including a trie of all correct bracket sequences of length \(2n\).The definition of correct bracket sequence is as follows: The empty sequence is a correct bracket sequence, If \(s\) is a correct bracket sequence, then \((\,s\,)\) is a correct bracket sequence, If \(s\) and \(t\) are a correct bracket sequence, then \(st\) is also a correct bracket sequence. For example, the strings ""(())"", ""()()"" form a correct bracket sequence, while "")("" and ""(("" not.Aki then came up with an interesting problem: What is the size of the maximum matching (the largest set of edges such that there are no two edges with a common vertex) in this trie? Since the answer can be quite large, print it modulo \(10^9 + 7\).
|
The only line contains a single integer \(n\) (\(1 \le n \le 1000\)).
|
Print exactly one integer — the size of the maximum matching in the trie. Since the answer can be quite large, print it modulo \(10^9 + 7\).
|
The pictures below illustrate tries in the first two examples (for clarity, the round brackets are replaced with angle brackets). The maximum matching is highlighted with blue.
|
Input: 1 | Output: 1
|
Hard
| 3 | 1,046 | 69 | 140 | 11 |
274 |
E
|
274E
|
E. Mirror Room
| 3,000 |
data structures; implementation
|
Imagine an n × m grid with some blocked cells. The top left cell in the grid has coordinates (1, 1) and the bottom right cell has coordinates (n, m). There are k blocked cells in the grid and others are empty. You flash a laser beam from the center of an empty cell (xs, ys) in one of the diagonal directions (i.e. north-east, north-west, south-east or south-west). If the beam hits a blocked cell or the border of the grid it will reflect. The behavior of the beam reflection in different situations is depicted in the figure below. After a while the beam enters an infinite cycle. Count the number of empty cells that the beam goes through at least once. We consider that the beam goes through cell if it goes through its center.
|
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 105, 0 ≤ k ≤ 105). Each of the next k lines contains two integers xi and yi (1 ≤ xi ≤ n, 1 ≤ yi ≤ m) indicating the position of the i-th blocked cell. The last line contains xs, ys (1 ≤ xs ≤ n, 1 ≤ ys ≤ m) and the flash direction which is equal to ""NE"", ""NW"", ""SE"" or ""SW"". These strings denote directions ( - 1, 1), ( - 1, - 1), (1, 1), (1, - 1).It's guaranteed that no two blocked cells have the same coordinates.
|
In the only line of the output print the number of empty cells that the beam goes through at least once.Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Input: 3 3 01 2 SW | Output: 6
|
Master
| 2 | 731 | 498 | 253 | 2 |
|
989 |
C
|
989C
|
C. A Mist of Florescence
| 1,800 |
constructive algorithms; graphs
|
As the boat drifts down the river, a wood full of blossoms shows up on the riverfront.""I've been here once,"" Mino exclaims with delight, ""it's breathtakingly amazing.""""What is it like?""""Look, Kanno, you've got your paintbrush, and I've got my words. Have a try, shall we?"" There are four kinds of flowers in the wood, Amaranths, Begonias, Centaureas and Dianthuses.The wood can be represented by a rectangular grid of \(n\) rows and \(m\) columns. In each cell of the grid, there is exactly one type of flowers.According to Mino, the numbers of connected components formed by each kind of flowers are \(a\), \(b\), \(c\) and \(d\) respectively. Two cells are considered in the same connected component if and only if a path exists between them that moves between cells sharing common edges and passes only through cells containing the same flowers.You are to help Kanno depict such a grid of flowers, with \(n\) and \(m\) arbitrarily chosen under the constraints given below. It can be shown that at least one solution exists under the constraints of this problem.Note that you can choose arbitrary \(n\) and \(m\) under the constraints below, they are not given in the input.
|
The first and only line of input contains four space-separated integers \(a\), \(b\), \(c\) and \(d\) (\(1 \leq a, b, c, d \leq 100\)) — the required number of connected components of Amaranths, Begonias, Centaureas and Dianthuses, respectively.
|
In the first line, output two space-separated integers \(n\) and \(m\) (\(1 \leq n, m \leq 50\)) — the number of rows and the number of columns in the grid respectively.Then output \(n\) lines each consisting of \(m\) consecutive English letters, representing one row of the grid. Each letter should be among 'A', 'B', 'C' and 'D', representing Amaranths, Begonias, Centaureas and Dianthuses, respectively.In case there are multiple solutions, print any. You can output each letter in either case (upper or lower).
|
In the first example, each cell of Amaranths, Begonias and Centaureas forms a connected component, while all the Dianthuses form one.
|
Input: 5 3 2 1 | Output: 4 7DDDDDDDDABACADDBABACDDDDDDDD
|
Medium
| 2 | 1,184 | 245 | 514 | 9 |
1,713 |
C
|
1713C
|
C. Build Permutation
| 1,200 |
constructive algorithms; dp; math
|
A \(\mathbf{0}\)-indexed array \(a\) of size \(n\) is called good if for all valid indices \(i\) (\(0 \le i \le n-1\)), \(a_i + i\) is a perfect square\(^\dagger\).Given an integer \(n\). Find a permutation\(^\ddagger\) \(p\) of \([0,1,2,\ldots,n-1]\) that is good or determine that no such permutation exists.\(^\dagger\) An integer \(x\) is said to be a perfect square if there exists an integer \(y\) such that \(x = y^2\).\(^\ddagger\) An array \(b\) is a permutation of an array \(a\) if \(b\) consists of the elements of \(a\) in arbitrary order. For example, \([4,2,3,4]\) is a permutation of \([3,2,4,4]\) while \([1,2,2]\) is not a permutation of \([1,2,3]\).
|
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 a single integer \(n\) (\(1 \le n \le 10^5\)) — the length of the permutation \(p\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output \(n\) distinct integers \(p_0, p_1, \dots, p_{n-1}\) (\(0 \le p_i \le n-1\)) — the permutation \(p\) — if the answer exists, and \(-1\) otherwise.
|
In the first test case, we have \(n=3\). The array \(p = [1, 0, 2]\) is good since \(1 + 0 = 1^2\), \(0 + 1 = 1^2\), and \(2 + 2 = 2^2\)In the second test case, we have \(n=4\). The array \(p = [0, 3, 2, 1]\) is good since \(0 + 0 = 0^2\), \(3 + 1 = 2^2\), \(2+2 = 2^2\), and \(1+3 = 2^2\).
|
Input: 3347 | Output: 1 0 2 0 3 2 1 1 0 2 6 5 4 3
|
Easy
| 3 | 668 | 306 | 173 | 17 |
523 |
D
|
523D
|
D. Statistics of Recompressing Videos
| 1,600 |
*special; data structures; implementation
|
A social network for dogs called DH (DogHouse) has k special servers to recompress uploaded videos of cute cats. After each video is uploaded, it should be recompressed on one (any) of the servers, and only after that it can be saved in the social network.We know that each server takes one second to recompress a one minute fragment. Thus, any server takes m seconds to recompress a m minute video.We know the time when each of the n videos were uploaded to the network (in seconds starting from the moment all servers started working). All videos appear at different moments of time and they are recompressed in the order they appear. If some video appeared at time s, then its recompressing can start at that very moment, immediately. Some videos can await recompressing when all the servers are busy. In this case, as soon as a server is available, it immediately starts recompressing another video. The videos that await recompressing go in a queue. If by the moment the videos started being recompressed some servers are available, then any of them starts recompressing the video.For each video find the moment it stops being recompressed.
|
The first line of the input contains integers n and k (1 ≤ n, k ≤ 5·105) — the number of videos and servers, respectively.Next n lines contain the descriptions of the videos as pairs of integers si, mi (1 ≤ si, mi ≤ 109), where si is the time in seconds when the i-th video appeared and mi is its duration in minutes. It is guaranteed that all the si's are distinct and the videos are given in the chronological order of upload, that is in the order of increasing si.
|
Print n numbers e1, e2, ..., en, where ei is the time in seconds after the servers start working, when the i-th video will be recompressed.
|
Input: 3 21 52 53 5 | Output: 6711
|
Medium
| 3 | 1,145 | 467 | 139 | 5 |
|
798 |
B
|
798B
|
B. Mike and strings
| 1,300 |
brute force; dp; strings
|
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string ""coolmike"", in one move he can transform it into the string ""oolmikec"".Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?
|
The first line contains integer n (1 ≤ n ≤ 50) — the number of strings.This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.
|
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
|
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into ""zwoxz"".
|
Input: 4xzzwozwoxzzzwoxxzzwo | Output: 5
|
Easy
| 3 | 409 | 255 | 121 | 7 |
2,040 |
C
|
2040C
|
C. Ordered Permutations
| 1,600 |
bitmasks; combinatorics; constructive algorithms; greedy; math; two pointers
|
Consider a permutation\(^{\text{∗}}\) \(p_1, p_2, \ldots, p_n\) of integers from \(1\) to \(n\). We can introduce the following sum for it\(^{\text{†}}\):$$$\(S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r)\)\(Let us consider all permutations of length \)n\( with the maximum possible value of \)S(p)\(. Output the \)k\(-th of them in lexicographical\)^{\text{‡}}\(order, or report that there are less than \)k\( 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). \)^{\text{†}}\(For example: For the permutation \)[1, 2, 3]\( the value of \)S(p)\( is equal to \)\min(1) + \min(1, 2) + \min(1, 2, 3) + \min(2) + \min(2, 3) + \min(3) =\( \)1 + 1 + 1 + 2 + 2 + 3 = 10\( For the permutation \)[2, 4, 1, 3]\( the value of \)S(p)\( is equal to \)\min(2) + \min(2, 4) + \min(2, 4, 1) + \min(2, 4, 1, 3) \ +\( \) \min(4) + \min(4, 1) + \min(4, 1, 3) \ +\( \)\min(1) + \min(1, 3) \ +\( \)\min(3) =\( \)2 + 2 + 1 + 1 + 4 + 1 + 1 + 1 + 1 + 3 = 17\(. \)^{\text{‡}}\(An array \)a\( is lexicographically smaller than an array \)b\( if and only if one of the following holds: \)a\( is a prefix of \)b\(, but \)a \ne b\(; or in the first position where \)a\( and \)b\( differ, the array \)a\( has a smaller element than the corresponding element in \)b$$$.
|
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 two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le k \le 10^{12}\)) — the length of the permutation and the index number of the desired permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10 ^ 5\).
|
For each test case, if there are less than \(k\) suitable permutations, print \(-1\).Otherwise, print the \(k\)-th suitable permutation.
|
Let us calculate the required sum for all permutations of length \(3\) (ordered lexicographically): PermutationValue of \(S(p)\)\([1, 2, 3]\)\(10\)\([1, 3, 2]\)\(10\)\([2, 1, 3]\)\(9\)\([2, 3, 1]\)\(10\)\([3, 1, 2]\)\(9\)\([3, 2, 1]\)\(10\) In the first test case, you have to print the second suitable permutation of length \(3\). Looking at the table, we see that it is the permutation \([1, 3, 2]\).In the second test case, you have to print the third suitable permutation of length \(3\). Looking at the table, we see that it is the permutation \([2, 3, 1]\).
|
Input: 63 23 34 114 66 397 34 | Output: 1 3 2 2 3 1 -1 2 4 3 1 -1 2 3 4 5 7 6 1
|
Medium
| 6 | 1,557 | 461 | 136 | 20 |
171 |
G
|
171G
|
G. Mysterious numbers - 2
| 1,600 |
*special
|
The only line of input contains three integers a1, a2, a3 (1 ≤ a1, a2, a3 ≤ 20), separated by spaces.
|
Output a single integer.
|
Input: 2 3 2 | Output: 5
|
Medium
| 1 | 0 | 101 | 24 | 1 |
||
1,493 |
E
|
1493E
|
E. Enormous XOR
| 2,600 |
bitmasks; constructive algorithms; greedy; math; strings; two pointers
|
You are given two integers \(l\) and \(r\) in binary representation. Let \(g(x, y)\) be equal to the bitwise XOR of all integers from \(x\) to \(y\) inclusive (that is \(x \oplus (x+1) \oplus \dots \oplus (y-1) \oplus y\)). Let's define \(f(l, r)\) as the maximum of all values of \(g(x, y)\) satisfying \(l \le x \le y \le r\).Output \(f(l, r)\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^6\)) — the length of the binary representation of \(r\).The second line contains the binary representation of \(l\) — a string of length \(n\) consisting of digits \(0\) and \(1\) (\(0 \le l < 2^n\)).The third line contains the binary representation of \(r\) — a string of length \(n\) consisting of digits \(0\) and \(1\) (\(0 \le r < 2^n\)).It is guaranteed that \(l \le r\). The binary representation of \(r\) does not contain any extra leading zeros (if \(r=0\), the binary representation of it consists of a single zero). The binary representation of \(l\) is preceded with leading zeros so that its length is equal to \(n\).
|
In a single line output the value of \(f(l, r)\) for the given pair of \(l\) and \(r\) in binary representation without extra leading zeros.
|
In sample test case \(l=19\), \(r=122\). \(f(x,y)\) is maximal and is equal to \(127\), with \(x=27\), \(y=100\), for example.
|
Input: 7 0010011 1111010 | Output: 1111111
|
Expert
| 6 | 347 | 695 | 140 | 14 |
977 |
F
|
977F
|
F. Consecutive Subsequence
| 1,700 |
dp
|
You are given an integer array of length \(n\).You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other words the required sequence should be equal to \([x, x + 1, \dots, x + k - 1]\) for some value \(x\) and length \(k\).Subsequence of an array can be obtained by erasing some (possibly zero) elements from the array. You can erase any elements, not necessarily going successively. The remaining elements preserve their order. For example, for the array \([5, 3, 1, 2, 4]\) the following arrays are subsequences: \([3]\), \([5, 3, 1, 2, 4]\), \([5, 1, 4]\), but the array \([1, 3]\) is not.
|
The first line of the input containing integer number \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the array. The second line of the input containing \(n\) integer numbers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) — the array itself.
|
On the first line print \(k\) — the maximum length of the subsequence of the given array that forms an increasing sequence of consecutive integers.On the second line print the sequence of the indices of the any maximum length subsequence of the given array that forms an increasing sequence of consecutive integers.
|
All valid answers for the first example (as sequences of indices): \([1, 3, 5, 6]\) \([2, 3, 5, 6]\) All valid answers for the second example: \([1, 4]\) \([2, 5]\) \([3, 6]\) All valid answers for the third example: \([1]\) \([2]\) \([3]\) \([4]\) All valid answers for the fourth example: \([1, 2, 3, 7, 8, 9]\)
|
Input: 73 3 4 7 5 6 8 | Output: 42 3 5 6
|
Medium
| 1 | 691 | 249 | 315 | 9 |
730 |
A
|
730A
|
A. Toda 2
| 1,800 |
greedy; implementation
|
A group of n friends enjoys playing popular video game Toda 2. There is a rating system describing skill level of each player, initially the rating of the i-th friend is ri.The friends decided to take part in the championship as a team. But they should have equal ratings to be allowed to compose a single team consisting of all n friends. So the friends are faced with the problem: how to make all their ratings equal.One way to change ratings is to willingly lose in some matches. Friends can form a party consisting of two to five (but not more than n) friends and play a match in the game. When the party loses, the rating of each of its members decreases by 1. A rating can't become negative, so ri = 0 doesn't change after losing.The friends can take part in multiple matches, each time making a party from any subset of friends (but remember about constraints on party size: from 2 to 5 members).The friends want to make their ratings equal but as high as possible.Help the friends develop a strategy of losing the matches so that all their ratings become equal and the resulting rating is maximum possible.
|
The first line contains a single integer n (2 ≤ n ≤ 100) — the number of friends.The second line contains n non-negative integers r1, r2, ..., rn (0 ≤ ri ≤ 100), where ri is the initial rating of the i-th friend.
|
In the first line, print a single integer R — the final rating of each of the friends.In the second line, print integer t — the number of matches the friends have to play. Each of the following t lines should contain n characters '0' or '1', where the j-th character of the i-th line is equal to: '0', if friend j should not play in match i, '1', if friend j should play in match i. Each line should contain between two and five characters '1', inclusive.The value t should not exceed 104, it is guaranteed that such solution exists. Remember that you shouldn't minimize the value t, but you should maximize R. If there are multiple solutions, print any of them.
|
Input: 54 5 1 7 4 | Output: 180101000011010101001000011110000001111000
|
Medium
| 2 | 1,114 | 212 | 662 | 7 |
|
2,078 |
B
|
2078B
|
B. Vicious Labyrinth
| 1,100 |
constructive algorithms; graphs; greedy; implementation; math
|
Axium Crisis - ak+q There are \(n\) cells in a labyrinth, and cell \(i\) (\(1 \leq i \leq n\)) is \(n - i\) kilometers away from the exit. In particular, cell \(n\) is the exit. Note also that each cell is connected to the exit but is not accessible from any other cell in any way.In each cell, there is initially exactly one person stuck in it. You want to help everyone get as close to the exit as possible by installing a teleporter in each cell \(i\) (\(1 \leq i \leq n\)), which translocates the person in that cell to another cell \(a_i\).The labyrinth owner caught you in the act. Amused, she let you continue, but under some conditions: Everyone must use the teleporter exactly \(k\) times. No teleporter in any cell can lead to the same cell it is in. Formally, \(i \neq a_i\) for all \(1 \leq i \leq n\).You must find a teleporter configuration that minimizes the sum of distances of all individuals from the exit after using the teleporter exactly \(k\) times while still satisfying the restrictions of the labyrinth owner.If there are many possible configurations, you can output any of them.
|
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 and only line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 2 \cdot 10^5\), \(1 \leq k \leq 10^9\)) — the number of cells in the labyrinth and the value \(k\).It is guaranteed that the total sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(n\) integers — the destinations of the teleporters \(a_1, a_2, \ldots, a_n\) in order, satisfying the given conditions (\(1 \leq a_i \leq n\), \(a_i \neq i\)).
|
In the first test case, the position of each person is as follows. Before teleporting: \([1, 2]\). First teleportation: \([2, 1]\).The distance sum is \((2-2) + (2-1) = 1\), which is the minimum possible.In the second test case, the position of each person is as follows. Before teleporting: \([1, 2, 3]\). First teleportation: \([2, 3, 2]\). Second teleportation: \([3, 2, 3]\).The distance sum is \((3-3) + (3-2) + (3-3) = 1\), which is the minimum possible.
|
Input: 22 13 2 | Output: 2 1 2 3 2
|
Easy
| 5 | 1,104 | 458 | 188 | 20 |
87 |
E
|
87E
|
E. Mogohu-Rea Idol
| 2,600 |
geometry
|
A long time ago somewhere in the depths of America existed a powerful tribe governed by the great leader Pinnie-the-Wooh. Once the tribe conquered three Maya cities. Pinnie-the-Wooh grew concerned: there had to be some control over the conquered territories. That's why he appealed to the priests of the supreme god Mogohu-Rea for help.The priests conveyed the god's will to him: to control these three cities he should put an idol to Mogohu-Rea — that will create a religious field over the cities. However, the idol is so powerful that it can easily drive the people around it mad unless it is balanced by exactly three sacrifice altars, placed one in each city. To balance the idol the altars should be placed so that the center of mass of the system of these three points coincided with the idol. When counting the center of mass consider that all the altars have the same mass.Now Pinnie-the-Wooh is thinking where to put the idol. He has a list of hills, that are suitable to put an idol there. Help him to identify on which of them you can put an idol without risking to fry off the brains of the cities' population with the religious field.Each city has a shape of a convex polygon such that no three vertexes lie on a straight line. The cities can intersect. Each altar should be attached to the city through a special ceremony, besides, it must be situated on the city's territory (possibly at the border). Thus, there may be several altars on a city's territory, but exactly one of them will be attached to the city. The altars, the idol and the hills are points on the plane, some of them may coincide.The hills are taken into consideration independently from each other, the altars' location for different hills may also be different.
|
First follow descriptions of the three cities, divided by empty lines. The descriptions are in the following format:The first line contains an integer n, which represent the number of the polygon's vertexes (3 ≤ n ≤ 5·104). Next n lines contain two integers xi, yi each, they are the coordinates of the polygon's i-th vertex in the counterclockwise order.After the cities' description follows the integer m (1 ≤ m ≤ 105), which represents the number of hills. Next m lines each contain two integers xj, yj, they are the coordinates of the j-th hill.All the coordinates in the input data do not exceed 5·108 in the absolute value.
|
For each hill print on a single line ""YES"" (without the quotes) or ""NO"" (without the quotes), depending on whether the three sacrifice altars can be put to balance the idol or not.
|
For the hill at (2, 1) the altars can be placed at the points (1, 0), (7, 5), ( - 2, - 2), for the hill at (1, 1) — at the points (0, 0), (6, 4), ( - 3, - 1). Many other groups of three points can do the trick. There are no suitable points for other hills.
|
Input: 30 01 01 148 85 56 48 43-1 -1-3 -1-2 -250 02 17 11 15 3 | Output: NOYESNOYESNO
|
Expert
| 1 | 1,747 | 629 | 184 | 0 |
928 |
A
|
928A
|
A. Login Verification
| 1,200 |
*special; strings
|
When registering in a social network, users are allowed to create their own convenient login to make it easier to share contacts, print it on business cards, etc.Login is an arbitrary sequence of lower and uppercase latin letters, digits and underline symbols («_»). However, in order to decrease the number of frauds and user-inattention related issues, it is prohibited to register a login if it is similar with an already existing login. More precisely, two logins s and t are considered similar if we can transform s to t via a sequence of operations of the following types: transform lowercase letters to uppercase and vice versa; change letter «O» (uppercase latin letter) to digit «0» and vice versa; change digit «1» (one) to any letter among «l» (lowercase latin «L»), «I» (uppercase latin «i») and vice versa, or change one of these letters to other. For example, logins «Codeforces» and «codef0rces» as well as «OO0OOO00O0OOO0O00OOO0OO_lol» and «OO0OOO0O00OOO0O00OO0OOO_1oI» are considered similar whereas «Codeforces» and «Code_forces» are not.You're given a list of existing logins with no two similar amonst and a newly created user login. Check whether this new login is similar with any of the existing ones.
|
The first line contains a non-empty string s consisting of lower and uppercase latin letters, digits and underline symbols («_») with length not exceeding 50 — the login itself.The second line contains a single integer n (1 ≤ n ≤ 1 000) — the number of existing logins.The next n lines describe the existing logins, following the same constraints as the user login (refer to the first line of the input). It's guaranteed that no two existing logins are similar.
|
Print «Yes» (without quotes), if user can register via this login, i.e. none of the existing logins is similar with it.Otherwise print «No» (without quotes).
|
In the second sample case the user wants to create a login consisting of three zeros. It's impossible due to collision with the third among the existing.In the third sample case the new login is similar with the second one.
|
Input: 1_wat22_watwat_1 | Output: Yes
|
Easy
| 2 | 1,224 | 461 | 157 | 9 |
1,844 |
B
|
1844B
|
B. Permutations & Primes
| 1,000 |
constructive algorithms; math
|
You are given a positive integer \(n\).In this problem, the \(\operatorname{MEX}\) of a collection of integers \(c_1,c_2,\dots,c_k\) is defined as the smallest positive integer \(x\) which does not occur in the collection \(c\). The primality of an array \(a_1,\dots,a_n\) is defined as the number of pairs \((l,r)\) such that \(1 \le l \le r \le n\) and \(\operatorname{MEX}(a_l,\dots,a_r)\) is a prime number. Find any permutation of \(1,2,\dots,n\) with the maximum possible primality among all permutations of \(1,2,\dots,n\). Note: A prime number is a number greater than or equal to \(2\) that is not divisible by any positive integer except \(1\) and itself. For example, \(2,5,13\) are prime numbers, but \(1\) and \(6\) are not prime numbers. A permutation of \(1,2,\dots,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).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(n\) integers: a permutation of \(1,2,\dots,n\) that achieves the maximum possible primality.If there are multiple solutions, print any of them.
|
In the first test case, there are \(3\) pairs \((l,r)\) with \(1 \le l \le r \le 2\), out of which \(2\) have a prime \(\operatorname{MEX}(a_l,\dots,a_r)\): \((l,r) = (1,1)\): \(\operatorname{MEX}(2) = 1\), which is not prime. \((l,r) = (1,2)\): \(\operatorname{MEX}(2,1) = 3\), which is prime. \((l,r) = (2,2)\): \(\operatorname{MEX}(1) = 2\), which is prime. Therefore, the primality is \(2\).In the second test case, \(\operatorname{MEX}(1) = 2\) is prime, so the primality is \(1\).In the third test case, the maximum possible primality is \(8\).
|
Input: 3215 | Output: 2 1 1 5 2 1 4 3
|
Beginner
| 2 | 1,079 | 348 | 172 | 18 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.