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,133 |
A
|
1133A
|
A. Middle of the Contest
| 1,000 |
implementation
|
Polycarp is going to participate in the contest. It starts at \(h_1:m_1\) and ends at \(h_2:m_2\). It is guaranteed that the contest lasts an even number of minutes (i.e. \(m_1 \% 2 = m_2 \% 2\), where \(x \% y\) is \(x\) modulo \(y\)). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes.Polycarp wants to know the time of the midpoint of the contest. For example, if the contest lasts from \(10:00\) to \(11:00\) then the answer is \(10:30\), if the contest lasts from \(11:10\) to \(11:12\) then the answer is \(11:11\).
|
The first line of the input contains two integers \(h_1\) and \(m_1\) in the format hh:mm.The second line of the input contains two integers \(h_2\) and \(m_2\) in the same format (hh:mm).It is guaranteed that \(0 \le h_1, h_2 \le 23\) and \(0 \le m_1, m_2 \le 59\).It is guaranteed that the contest lasts an even number of minutes (i.e. \(m_1 \% 2 = m_2 \% 2\), where \(x \% y\) is \(x\) modulo \(y\)). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes.
|
Print two integers \(h_3\) and \(m_3\) (\(0 \le h_3 \le 23, 0 \le m_3 \le 59\)) corresponding to the midpoint of the contest in the format hh:mm. Print each number as exactly two digits (prepend a number with leading zero if needed), separate them with ':'.
|
Input: 10:00 11:00 | Output: 10:30
|
Beginner
| 1 | 619 | 552 | 257 | 11 |
|
1,850 |
C
|
1850C
|
C. Word on the Paper
| 800 |
implementation; strings
|
On an \(8 \times 8\) grid of dots, a word consisting of lowercase Latin letters is written vertically in one column, from top to bottom. What is it?
|
The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.Each test case consists of \(8\) lines, each containing \(8\) characters. Each character in the grid is either \(\texttt{.}\) (representing a dot) or a lowercase Latin letter (\(\texttt{a}\)β\(\texttt{z}\)). The word lies entirely in a single column and is continuous from the beginning to the ending (without gaps). See the sample input for better understanding.
|
For each test case, output a single line containing the word made up of lowercase Latin letters (\(\texttt{a}\)β\(\texttt{z}\)) that is written vertically in one column from top to bottom.
|
Input: 5...................................i.....................................l.......o.......s.......t....................................................................t.......h.......e................................................g.......a.......m.......ea.......a.......a.......a.......a.......a.......a.......a....... | Output: i lost the game aaaaaaaa
|
Beginner
| 2 | 148 | 518 | 188 | 18 |
|
1,553 |
I
|
1553I
|
I. Stairs
| 3,400 |
combinatorics; divide and conquer; dp; fft; math
|
For a permutation \(p\) of numbers \(1\) through \(n\), we define a stair array \(a\) as follows: \(a_i\) is length of the longest segment of permutation which contains position \(i\) and is made of consecutive values in sorted order: \([x, x+1, \ldots, y-1, y]\) or \([y, y-1, \ldots, x+1, x]\) for some \(x \leq y\). For example, for permutation \(p = [4, 1, 2, 3, 7, 6, 5]\) we have \(a = [1, 3, 3, 3, 3, 3, 3]\). You are given the stair array \(a\). Your task is to calculate the number of permutations which have stair array equal to \(a\). Since the number can be big, compute it modulo \(998\,244\,353\). Note that this number can be equal to zero.
|
The first line of input contains integer \(n\) (\(1 \le n \le 10^5\)) β the length of a stair array \(a\).The second line of input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)).
|
Print the number of permutations which have stair array equal to \(a\). Since the number can be big, compute it modulo \(998\,244\,353\).
|
Input: 6 3 3 3 1 1 1 | Output: 6
|
Master
| 5 | 655 | 203 | 137 | 15 |
|
600 |
F
|
600F
|
F. Edge coloring of bipartite graph
| 2,800 |
graphs
|
You are given an undirected bipartite graph without multiple edges. You should paint the edges of graph to minimal number of colours, so that no two adjacent edges have the same colour.
|
The first line contains three integers a, b, m (1 β€ a, b β€ 1000, 0 β€ m β€ 105), a is the size of the first part, b is the size of the second part, m is the number of edges in the graph.Each of the next m lines contains two integers x, y (1 β€ x β€ a, 1 β€ y β€ b), where x is the number of the vertex in the first part and y is the number of the vertex in the second part. It is guaranteed that there are no multiple edges.
|
In the first line print integer c β the minimal number of colours. The second line should contain m integers from 1 to c β the colours of the edges (in the order they appear in the input).If there are several solutions, you can print any one of them.
|
Input: 4 3 51 22 23 24 14 3 | Output: 31 2 3 1 2
|
Master
| 1 | 185 | 418 | 250 | 6 |
|
1,979 |
A
|
1979A
|
A. Guess the Maximum
| 800 |
brute force; greedy; implementation
|
Alice and Bob came up with a rather strange game. They have an array of integers \(a_1, a_2,\ldots, a_n\). Alice chooses a certain integer \(k\) and tells it to Bob, then the following happens: Bob chooses two integers \(i\) and \(j\) (\(1 \le i < j \le n\)), and then finds the maximum among the integers \(a_i, a_{i + 1},\ldots, a_j\); If the obtained maximum is strictly greater than \(k\), Alice wins, otherwise Bob wins. Help Alice find the maximum \(k\) at which she is guaranteed to win.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^4\)) β the number of elements in the array.The second line of each test case contains \(n\) integers \(a_1, a_2,\ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^4\).
|
For each test case, output one integer β the maximum integer \(k\) at which Alice is guaranteed to win.
|
In the first test case, all possible subsegments that Bob can choose look as follows: \([2, 4], [2, 4, 1], [2, 4, 1, 7], [4, 1], [4, 1, 7], [1, 7]\). The maximums on the subsegments are respectively equal to \(4, 4, 7, 4, 7, 7\). It can be shown that \(3\) is the largest integer such that any of the maximums will be strictly greater than it.In the third test case, the only segment that Bob can choose is \([1, 1]\). So the answer is \(0\).
|
Input: 642 4 1 751 2 3 4 521 1337 8 16510 10 10 10 9103 12 9 5 2 3 2 9 8 2 | Output: 3 1 0 15 9 2
|
Beginner
| 3 | 494 | 545 | 103 | 19 |
9 |
A
|
9A
|
A. Die Roll
| 800 |
math; probabilities
|
Yakko, Wakko and Dot, world-famous animaniacs, decided to rest from acting in cartoons, and take a leave to travel a bit. Yakko dreamt to go to Pennsylvania, his Motherland and the Motherland of his ancestors. Wakko thought about Tasmania, its beaches, sun and sea. Dot chose Transylvania as the most mysterious and unpredictable place.But to their great regret, the leave turned to be very short, so it will be enough to visit one of the three above named places. That's why Yakko, as the cleverest, came up with a truly genius idea: let each of the three roll an ordinary six-sided die, and the one with the highest amount of points will be the winner, and will take the other two to the place of his/her dreams.Yakko thrown a die and got Y points, Wakko β W points. It was Dot's turn. But she didn't hurry. Dot wanted to know for sure what were her chances to visit Transylvania.It is known that Yakko and Wakko are true gentlemen, that's why if they have the same amount of points with Dot, they will let Dot win.
|
The only line of the input file contains two natural numbers Y and W β the results of Yakko's and Wakko's die rolls.
|
Output the required probability in the form of irreducible fraction in format Β«A/BΒ», where A β the numerator, and B β the denominator. If the required probability equals to zero, output Β«0/1Β». If the required probability equals to 1, output Β«1/1Β».
|
Dot will go to Transylvania, if she is lucky to roll 4, 5 or 6 points.
|
Input: 4 2 | Output: 1/2
|
Beginner
| 2 | 1,017 | 116 | 247 | 0 |
1,662 |
J
|
1662J
|
J. Training Camp
| 0 |
flows; graphs
|
You are organizing a training camp to teach algorithms to young kids. There are \(n^2\) kids, organized in an \(n\) by \(n\) grid. Each kid is between \(1\) and \(n\) years old (inclusive) and any two kids who are in the same row or in the same column have different ages.You want to select exactly \(n\) kids for a programming competition, with exactly one kid from each row and one kid from each column. Moreover, kids who are not selected must be either older than both kids selected in their row and column, or younger than both kids selected in their row and column (otherwise they will complain). Notice that it is always possible to select \(n\) kids satisfying these requirements (for example by selecting \(n\) kids who have the same age).During the training camp, you observed that some kids are good at programming, and the others are not. What is the maximum number of kids good at programming that you can select while satisfying all the requirements?
|
The first line contains \(n\) (\(1 \leq n \leq 128\)) β the size of the grid.The following \(n\) lines describe the ages of the kids. Specifically, the \(i\)-th line contains \(n\) integers \(a_{i,1}, \, a_{i,2}, \, \dots, \, a_{i, n}\) (\(1 \le a_{i,j} \le n\)) β where \(a_{i,j}\) is the age of the kid in the \(i\)-th row and \(j\)-th column. It is guaranteed that two kids on the same row or column have different ages, i.e., \(a_{i,j} \ne a_{i,j'}\) for any \(1\le i\le n\), \(1\le j < j'\le n\), and \(a_{i,j} \ne a_{i',j}\) for any \(1\le i < i'\le n\), \(1\le j\le n\).The following \(n\) lines describe the programming skills of the kids. Specifically, the \(i\)-th line contains \(n\) integers \(c_{i,1}, \, c_{i,2}, \, \dots, \, c_{i, n}\) (\(c_{i,j} \in \{0, \, 1\}\)) β where \(c_{i,j}=1\) if the kid in the \(i\)-th row and \(j\)-th column is good at programming and \(c_{i,j}=0\) otherwise.
|
Print the maximum number of kids good at programming that you can select while satisfying all the requirements.
|
In the first sample, it is not possible to select the two kids good at programming (in row \(1\) and column \(1\), and in row \(2\) and column \(3\)), because then you would have to select the kid in row \(3\) and column \(2\), and in that case two kids would complain (the one in row \(1\) and column \(2\), and the one in row \(3\) and column \(1\)).A valid selection which contains \(1\) kid good at programming is achieved by choosing the \(3\) kids who are \(1\) year old.In the second sample, there are \(10\) valid choices of the \(n\) kids that satisfy the requirements, and each of them selects exactly \(2\) kids good at programming.
|
Input: 3 1 2 3 3 1 2 2 3 1 1 0 0 0 0 1 0 0 0 | Output: 1
|
Beginner
| 2 | 964 | 905 | 111 | 16 |
1,340 |
C
|
1340C
|
C. Nastya and Unexpected Guest
| 2,400 |
dfs and similar; dp; graphs; shortest paths
|
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the girl's house is a road of \(n\) lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up \(g\) seconds green, and then \(r\) seconds red, then again \(g\) seconds green and so on.Formally, the road can be represented as a segment \([0, n]\). Initially, Denis is at point \(0\). His task is to get to point \(n\) in the shortest possible time.He knows many different integers \(d_1, d_2, \ldots, d_m\), where \(0 \leq d_i \leq n\) β are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed: He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by \(\pm 1\) in \(1\) second. While doing so, he must always stay inside the segment \([0, n]\). He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by \(+1\) and he walked on a safety island, then he can change his position by \(\pm 1\). Otherwise, he can change his position only by \(+1\). Similarly, if in the previous second he changed his position by \(-1\), on a safety island he can change position by \(\pm 1\), and at any other point by \(-1\). At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on. Denis has crossed the road as soon as his coordinate becomes equal to \(n\).This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.
|
The first line contains two integers \(n\) and \(m\) \((1 \leq n \leq 10^6, 2 \leq m \leq min(n + 1, 10^4))\) β road width and the number of safety islands.The second line contains \(m\) distinct integers \(d_1, d_2, \ldots, d_m\) \((0 \leq d_i \leq n)\) β the points where the safety islands are located. It is guaranteed that there are \(0\) and \(n\) among them.The third line contains two integers \(g, r\) \((1 \leq g, r \leq 1000)\) β the time that the green light stays on and the time that the red light stays on.
|
Output a single integer β the minimum time for which Denis can cross the road with obeying all the rules.If it is impossible to cross the road output \(-1\).
|
In the first test, the optimal route is: for the first green light, go to \(7\) and return to \(3\). In this case, we will change the direction of movement at the point \(7\), which is allowed, since there is a safety island at this point. In the end, we will be at the point of \(3\), where there is also a safety island. The next \(11\) seconds we have to wait for the red light. for the second green light reaches \(14\). Wait for the red light again. for \(1\) second go to \(15\). As a result, Denis is at the end of the road. In total, \(45\) seconds are obtained.In the second test, it is impossible to cross the road according to all the rules.
|
Input: 15 5 0 3 7 14 15 11 11 | Output: 45
|
Expert
| 4 | 2,543 | 521 | 157 | 13 |
2,104 |
B
|
2104B
|
B. Move to the End
| 1,000 |
brute force; data structures; dp; greedy; implementation
|
You are given an array \(a\) consisting of \(n\) integers.For every integer \(k\) from \(1\) to \(n\), you have to do the following: choose an arbitrary element of \(a\) and move it to the right end of the array (you can choose the last element, then the array won't change); print the sum of \(k\) last elements of \(a\); move the element you've chosen on the first step to its original position (restore the original array \(a\)). For every \(k\), you choose the element which you move so that the value you print is the maximum possible.Calculate the value you print for every \(k\).
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines: the first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)); the second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)). Additional constraint on the input: the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print \(n\) integers. The \(i\)-th of these integers should be equal to the maximum value you can print if \(k=i\).
|
Let's consider the first test case from the statement: when \(k = 1\), you can move the \(6\)-th element to the end, the array becomes \([13, 5, 10, 14, 8, 13, 15]\), and the value you print is \(15\); when \(k = 2\), you can move the \(6\)-th element to the end, the array becomes \([13, 5, 10, 14, 8, 13, 15]\), and the value you print is \(13 + 15 = 28\); when \(k = 3\), you can move the \(4\)-th element to the end, the array becomes \([13, 5, 10, 8, 15, 13, 14]\), and the value you print is \(15 + 13 + 14 = 42\); when \(k = 4\), you can move the \(5\)-th element to the end, the array becomes \([13, 5, 10, 14, 15, 13, 8]\), and the value you print is \(14 + 15 + 13 + 8 = 50\); when \(k = 5\), you can move the \(1\)-st element to the end, the array becomes \([5, 10, 14, 8, 15, 13, 13]\), and the value you print is \(14 + 8 + 15 + 13 + 13 = 63\); when \(k = 6\), you can move the \(1\)-st element to the end, the array becomes \([5, 10, 14, 8, 15, 13, 13]\), and the value you print is \(10 + 14 + 8 + 15 + 13 + 13 = 73\); when \(k = 7\), you can move the \(6\)-th element to the end, the array becomes \([13, 5, 10, 14, 8, 13, 15]\), and the value you print is \(13 + 5 + 10 + 14 + 8 + 13 + 15 = 78\).
|
Input: 4713 5 10 14 8 15 1361000000000 1000000000 1000000000 1000000000 1000000000 100000000014227 5 | Output: 15 28 42 50 63 73 78 1000000000 2000000000 3000000000 4000000000 5000000000 6000000000 42 7 12
|
Beginner
| 5 | 586 | 401 | 135 | 21 |
336 |
D
|
336D
|
D. Vasily the Bear and Beautiful Strings
| 2,100 |
combinatorics; math; number theory
|
Vasily the Bear loves beautiful strings. String s is beautiful if it meets the following criteria: String s only consists of characters 0 and 1, at that character 0 must occur in string s exactly n times, and character 1 must occur exactly m times. We can obtain character g from string s with some (possibly, zero) number of modifications. The character g equals either zero or one. A modification of string with length at least two is the following operation: we replace two last characters from the string by exactly one other character. This character equals one if it replaces two zeros, otherwise it equals zero. For example, one modification transforms string ""01010"" into string ""0100"", two modifications transform it to ""011"". It is forbidden to modify a string with length less than two.Help the Bear, count the number of beautiful strings. As the number of beautiful strings can be rather large, print the remainder after dividing the number by 1000000007 (109 + 7).
|
The first line of the input contains three space-separated integers n, m, g (0 β€ n, m β€ 105, n + m β₯ 1, 0 β€ g β€ 1).
|
Print a single integer β the answer to the problem modulo 1000000007 (109 + 7).
|
In the first sample the beautiful strings are: ""01"", ""10"".In the second sample the beautiful strings are: ""0011"", ""1001"", ""1010"", ""1100"".In the third sample there are no beautiful strings.
|
Input: 1 1 0 | Output: 2
|
Hard
| 3 | 983 | 115 | 79 | 3 |
314 |
A
|
314A
|
A. Sereja and Contest
| 1,600 |
implementation
|
During the last Sereja's Codesecrof round the server crashed many times, so the round was decided to be made unrated for some participants. Let's assume that n people took part in the contest. Let's assume that the participant who got the first place has rating a1, the second place participant has rating a2, ..., the n-th place participant has rating an. Then changing the rating on the Codesecrof site is calculated by the formula .After the round was over, the Codesecrof management published the participants' results table. They decided that if for a participant di < k, then the round can be considered unrated for him. But imagine the management's surprise when they found out that the participants' rating table is dynamic. In other words, when some participant is removed from the rating, he is removed from the results' table and the rating is recalculated according to the new table. And of course, all applications for exclusion from the rating are considered in view of the current table.We know that among all the applications for exclusion from the rating the first application to consider is from the participant with the best rank (the rank with the minimum number), for who di < k. We also know that the applications for exclusion from rating were submitted by all participants.Now Sereja wonders, what is the number of participants to be excluded from the contest rating, and the numbers of the participants in the original table in the order of their exclusion from the rating. Pay attention to the analysis of the first test case for a better understanding of the statement.
|
The first line contains two integers n, k (1 β€ n β€ 2Β·105, - 109 β€ k β€ 0). The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 109) β ratings of the participants in the initial table.
|
Print the numbers of participants in the order in which they were removed from the table. Print the initial numbers of the participants, that is, the numbers that the participants had in the initial table.
|
Consider the first test sample. Initially the sequence of the contest participants' ratings equals [5, 3, 4, 1, 2]. You can use this sequence to calculate the sequence of rating changes: [0, -9, -13, 8, 14]. According to the problem statement, the application of the participant who won the second place will be considered first. As soon as the second place winner is out from the ratings, the participants' rating sequence will equal [5, 4, 1, 2]. By this sequence you can count the new sequence of rating changes: [0, -8, 2, 6]. According to the problem statement, the application of the participant who won the second place will be considered. Initially this participant won third place. The new rating sequence equals [5, 1, 2], the new sequence of rating changes equals [0, -1, 1]. The second place participant's application is taken into consideration, initially this participant won the fourth place. The new rating sequence equals [5, 2], the new sequence of rating changes equals [0, 0]. No more applications will be considered. Thus, you should print 2, 3, 4.
|
Input: 5 05 3 4 1 2 | Output: 234
|
Medium
| 1 | 1,596 | 208 | 205 | 3 |
188 |
H
|
188H
|
H. Stack
| 1,800 |
*special; expression parsing; implementation
|
In this problem we'll use a stack which supports two types of operations: Push a given number on the stack. Pop two numbers from the stack, perform a given operation (addition or multiplication) on them and push the result on the stack. You are given a string which describes the sequence of operations to be performed on the stack. i-th character corresponds to i-th operation: If i-th character is a digit, push the corresponding number on the stack. If i-th character is Β«+Β» or Β«*Β», perform the corresponding operation. Initially the stack is empty. Output the topmost number on the stack after executing all given operations.
|
The only line of input contains a string of operations, consisting of characters Β«+Β», Β«*Β» and digits (0..9). The length of the string will be between 1 and 20 characters, inclusive.The given sequence of operations is guaranteed to be correct, i.e. the stack will have at least two elements before every math operation. The numbers on the stack will never exceed 106.
|
Output a single number β the topmost element of the stack after performing all given operations.
|
In the first case the stack will end up containing a single number β the result of calculating (1+2)*3+6*6.In the second case there are no math operations, so the answer will be the last number pushed on the stack.
|
Input: 12+3*66*+ | Output: 45
|
Medium
| 3 | 629 | 366 | 96 | 1 |
1,883 |
D
|
1883D
|
D. In Love
| 1,500 |
data structures; greedy
|
Initially, you have an empty multiset of segments. You need to process \(q\) operations of two types: \(+\) \(l\) \(r\) β Add the segment \((l, r)\) to the multiset, \(-\) \(l\) \(r\) β Remove exactly one segment \((l, r)\) from the multiset. It is guaranteed that this segment exists in the multiset.After each operation, you need to determine if there exists a pair of segments in the multiset that do not intersect. A pair of segments \((l, r)\) and \((a, b)\) do not intersect if there does not exist a point \(x\) such that \(l \leq x \leq r\) and \(a \leq x \leq b\).
|
The first line of each test case contains an integer \(q\) (\(1 \leq q \leq 10^5\)) β the number of operations.The next \(q\) lines describe two types of operations. If it is an addition operation, it is given in the format \(+\) \(l\) \(r\). If it is a deletion operation, it is given in the format \(-\) \(l\) \(r\) (\(1 \leq l \leq r \leq 10^9\)).
|
After each operation, print ""YES"" if there exists a pair of segments in the multiset that do not intersect, and ""NO"" otherwise.You can print the answer in any case (uppercase or lowercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive answers.
|
In the example, after the second, third, fourth, and fifth operations, there exists a pair of segments \((1, 2)\) and \((3, 4)\) that do not intersect.Then we remove exactly one segment \((3, 4)\), and by that time we had two segments. Therefore, the answer after this operation also exists.
|
Input: 12+ 1 2+ 3 4+ 2 3+ 2 2+ 3 4- 3 4- 3 4- 1 2+ 3 4- 2 2- 2 3- 3 4 | Output: NO YES YES YES YES YES NO NO YES NO NO NO
|
Medium
| 2 | 573 | 350 | 297 | 18 |
2,048 |
C
|
2048C
|
C. Kevin and Binary Strings
| 1,200 |
bitmasks; brute force; greedy; implementation; strings
|
Kevin discovered a binary string \(s\) that starts with 1 in the river at Moonlit River Park and handed it over to you. Your task is to select two non-empty substrings\(^{\text{β}}\) of \(s\) (which can be overlapped) to maximize the XOR value of these two substrings.The XOR of two binary strings \(a\) and \(b\) is defined as the result of the \(\oplus\) operation applied to the two numbers obtained by interpreting \(a\) and \(b\) as binary numbers, with the leftmost bit representing the highest value. Here, \(\oplus\) denotes the bitwise XOR operation.The strings you choose may have leading zeros.\(^{\text{β}}\)A string \(a\) is a substring of a string \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)).The only line of each test case contains a binary string \(s\) that starts with 1 (\(1\le\lvert s\rvert\le 5000\)).It is guaranteed that the sum of \(\lvert s\rvert\) over all test cases doesn't exceed \(5000\).
|
For each test case, output four integers \(l_1, r_1, l_2, r_2\) (\(1 \le l_1 \le r_1 \le |s|\), \(1 \le l_2 \le r_2 \le |s|\)) β in the case the two substrings you selected are \(s_{l_1} s_{l_1 + 1} \ldots s_{r_1}\) and \(s_{l_2} s_{l_2 + 1} \ldots s_{r_2}\).If there are multiple solutions, print any of them.
|
In the first test case, we can choose \( s_2=\texttt{1} \) and \( s_1 s_2 s_3=\texttt{111} \), and \( \texttt{1}\oplus\texttt{111}=\texttt{110} \). It can be proven that it is impossible to obtain a larger result. Additionally, \( l_1=3\), \(r_1=3\), \(l_2=1\), \(r_2=3 \) is also a valid solution.In the second test case, \( s_1 s_2 s_3=\texttt{100} \), \( s_1 s_2 s_3 s_4=\texttt{1000} \), the result is \( \texttt{100}\oplus\texttt{1000}=\texttt{1100} \), which is the maximum.
|
Input: 5111100010111111011100010001101 | Output: 2 2 1 3 1 3 1 4 1 5 1 4 3 4 1 5 1 13 1 11
|
Easy
| 5 | 845 | 329 | 310 | 20 |
1,759 |
A
|
1759A
|
A. Yes-Yes?
| 800 |
implementation; strings
|
You talked to Polycarp and asked him a question. You know that when he wants to answer ""yes"", he repeats Yes many times in a row.Because of the noise, you only heard part of the answer β some substring of it. That is, if he answered YesYes, then you could hear esY, YesYes, sYes, e, but you couldn't Yess, YES or se.Determine if it is true that the given string \(s\) is a substring of YesYesYes... (Yes repeated many times in a row).
|
The first line of input data contains the singular \(t\) (\(1 \le t \le 1000\)) β the number of test cases in the test.Each test case is described by a single string of Latin letters \(s\) (\(1 \le |s| \le 50\)) β the part of Polycarp's answer that you heard, where \(|s|\) β is the length of the string \(s\).
|
Output \(t\) lines, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if the specified string \(s\) is a substring of the string YesYesYes...Yes (the number of words Yes is arbitrary), and ""NO"" 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: 12YESesYescodeforcesesseYesYesYesYesYesYesYesYeseYYesssYoYes | Output: NO YES NO YES NO YES YES NO NO YES NO YES
|
Beginner
| 2 | 436 | 310 | 388 | 17 |
|
935 |
B
|
935B
|
B. Fafa and the Gates
| 900 |
implementation
|
Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin.The world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.e. the line with the equation x = y). Any point below the wall belongs to the first kingdom while any point above the wall belongs to the second kingdom. There is a gate at any integer point on the line (i.e. at points (0, 0), (1, 1), (2, 2), ...). The wall and the gates do not belong to any of the kingdoms. Fafa is at the gate at position (0, 0) and he wants to walk around in the two kingdoms. He knows the sequence S of moves he will do. This sequence is a string where each character represents a move. The two possible moves Fafa will do are 'U' (move one step up, from (x, y) to (x, y + 1)) and 'R' (move one step right, from (x, y) to (x + 1, y)). Fafa wants to know the number of silver coins he needs to pay to walk around the two kingdoms following the sequence S. Note that if Fafa visits a gate without moving from one kingdom to another, he pays no silver coins. Also assume that he doesn't pay at the gate at point (0, 0), i. e. he is initially on the side he needs.
|
The first line of the input contains single integer n (1 β€ n β€ 105) β the number of moves in the walking sequence.The second line contains a string S of length n consisting of the characters 'U' and 'R' describing the required moves. Fafa will follow the sequence S in order from left to right.
|
On a single line, print one integer representing the number of silver coins Fafa needs to pay at the gates to follow the sequence S.
|
The figure below describes the third sample. The red arrows represent the sequence of moves Fafa will follow. The green gates represent the gates at which Fafa have to pay silver coins.
|
Input: 1U | Output: 0
|
Beginner
| 1 | 1,302 | 294 | 132 | 9 |
2,000 |
G
|
2000G
|
G. Call During the Journey
| 2,100 |
binary search; brute force; graphs; greedy; shortest paths
|
You live in a city consisting of \(n\) intersections and \(m\) streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possibly passing through some other intersections.Every minute, you can board a bus at intersection \(u_i\) and travel for \(l_{i1}\) minutes to intersection \(v_i\). Conversely, you can travel from intersection \(v_i\) to intersection \(u_i\) in \(l_{i1}\) minutes. You can only board and exit the bus at intersections. You can only board the bus at an intersection if you are currently there.You can also walk along each street, which takes \(l_{i2} > l_{i1}\) minutes.You can make stops at intersections.You live at intersection number \(1\). Today you woke up at time \(0\), and you have an important event scheduled at intersection number \(n\), which you must reach no later than time \(t_0\). You also have a phone call planned that will last from \(t_1\) to \(t_2\) minutes (\(t_1 < t_2 < t_0\)). During the phone call, you cannot ride the bus, but you can walk along any streets, make stops, or stay at home. You can exit the bus at minute \(t_1\) and board the bus again at minute \(t_2\).Since you want to get enough sleep, you became curious β how late can you leave home to have time to talk on the phone and still not be late for the event?
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The following are the descriptions of the test cases.The first line of each test case contains two integers \(n\), \(m\) (\(2 \le n \le 10^5, 1 \le m \le 10^5\)) β the number of intersections and streets in the city.The second line of each test case contains three integers \(t_0\), \(t_1\), \(t_2\) (\(1 \le t_1 < t_2 < t_0 \le 10^9\)) β the start time of the event, the start time of the phone call, and its end time, respectively.The next \(m\) lines of each test case contain descriptions of the streets.The \(i\)-th line contains four integers \(u_i\), \(v_i\), \(l_{i1}\), \(l_{i2}\) (\(1 \le u_i, v_i \le n\), \(u_i \neq v_i\), \(1 \le l_{i1} < l_{i2} \le 10^9\)) β the numbers of the intersections connected by the \(i\)-th street, as well as the travel time along the street by bus and on foot. It is guaranteed that no two streets connect the same pair of intersections and that it is possible to reach any intersection from any other.It is guaranteed that the sum of the values of \(n\) across all test cases does not exceed \(10^5\). It is also guaranteed that the sum of the values of \(m\) across all test cases does not exceed \(10^5\).
|
For each test case, output a single integer β the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1.
|
Input: 75 5100 20 801 5 30 1001 2 20 502 3 20 503 4 20 504 5 20 502 1100 50 601 2 55 1104 4100 40 601 2 30 1002 4 30 1001 3 20 503 4 20 503 3100 80 901 2 1 102 3 10 501 3 20 213 258 55 572 1 1 32 3 3 42 112 9 102 1 6 105 58 5 62 1 1 82 3 4 84 2 2 45 3 3 44 5 2 6 | Output: 0 -1 60 80 53 3 2
|
Hard
| 5 | 1,468 | 1,243 | 195 | 20 |
|
140 |
B
|
140B
|
B. New Year Cards
| 1,800 |
brute force; greedy; implementation
|
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has n friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to n in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the i-th friend sent to Alexander a card number i.Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: He will never send to a firend a card that this friend has sent to him. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times.Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to n. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card.Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible.Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
|
The first line contains an integer n (2 β€ n β€ 300) β the number of Alexander's friends, equal to the number of cards. Next n lines contain his friends' preference lists. Each list consists of n different integers from 1 to n. The last line contains Alexander's preference list in the same format.
|
Print n space-separated numbers: the i-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the i-th friend. If there are several solutions, print any of them.
|
In the sample, the algorithm of actions Alexander and his friends perform is as follows: Alexander receives card 1 from the first friend. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. Alexander receives card 2 from the second friend, now he has two cards β 1 and 2. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. Alexander receives card 3 from the third friend. Alexander receives card 4 from the fourth friend. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
|
Input: 41 2 3 44 1 3 24 3 1 23 4 2 13 1 2 4 | Output: 2 1 1 4
|
Medium
| 3 | 1,753 | 296 | 220 | 1 |
460 |
C
|
460C
|
C. Present
| 1,700 |
binary search; data structures; greedy
|
Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions. There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?
|
The first line contains space-separated integers n, m and w (1 β€ w β€ n β€ 105; 1 β€ m β€ 105). The second line contains space-separated integers a1, a2, ..., an (1 β€ ai β€ 109).
|
Print a single integer β the maximum final height of the smallest flower.
|
In the first sample beaver can water the last 3 flowers at the first day. On the next day he may not to water flowers at all. In the end he will get the following heights: [2, 2, 2, 3, 2, 2]. The smallest flower has height equal to 2. It's impossible to get height 3 in this test.
|
Input: 6 2 32 2 2 2 1 1 | Output: 2
|
Medium
| 3 | 993 | 173 | 73 | 4 |
1,054 |
F
|
1054F
|
F. Electric Scheme
| 2,700 |
flows; graph matchings
|
Pasha is a young technician, nevertheless, he has already got a huge goal: to assemble a PC. The first task he has to become familiar with is to assemble an electric scheme.The scheme Pasha assembled yesterday consists of several wires. Each wire is a segment that connects two points on a plane with integer coordinates within the segment \([1, 10^9]\).There are wires of two colors in the scheme: red wires: these wires are horizontal segments, i.e. if such a wire connects two points \((x_1, y_1)\) and \((x_2, y_2)\), then \(y_1 = y_2\); blue wires: these wires are vertical segments, i.e. if such a wire connects two points \((x_1, y_1)\) and \((x_2, y_2)\), then \(x_1 = x_2\). Note that if a wire connects a point to itself, it may be blue, and it can be red. Also, in Pasha's scheme no two wires of the same color intersect, i.e. there are no two wires of same color that have common points.The imperfection of Pasha's scheme was that the wires were not isolated, so in the points where two wires of different colors intersect, Pasha saw sparks. Pasha wrote down all the points where he saw sparks and obtained a set of \(n\) distinct points. After that he disassembled the scheme.Next morning Pasha looked at the set of \(n\) points where he had seen sparks and wondered how many wires had he used. Unfortunately, he does not remember that, so he wonders now what is the smallest number of wires he might have used in the scheme. Help him to determine this number and place the wires in such a way that in the resulting scheme the sparks occur in the same places.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 1000\)) β the number of points where Pasha saw sparks.Each of the next \(n\) lines contain two integers \(x\) and \(y\) (\(1 \leq x, y \leq 10^9\)) β the coordinates of a point with sparks. It is guaranteed that all points are distinct.
|
Print the description of the scheme in the following format.In the first line print \(h\) β the number of horizontal red wires (\(0 \leq h\)). In each of the next \(h\) lines print \(4\) integers \(x_1\), \(y_1\), \(x_2\), \(y_2\) β the coordinates of two points \((x_1, y_1)\) and \((x_2, y_2)\) that are connected with this red wire. The segmenst must be horizontal, i.e. \(y_1 = y_2\) must hold. Also, the constraint \(1 \leq x_1, y_1, x_2, y_2 \leq 10^9\) must hold.After that print \(v\) β the number of vertical blue wires (\(0 \leq v\)). In each of the next \(v\) lines print \(4\) integers \(x_1\), \(y_1\), \(x_2\), \(y_2\) β the coordinates of two points \((x_1, y_1)\) and \((x_2, y_2)\) that are connected with this blue wire. The segmenst must be vertical, i.e. \(x_1 = x_2\) shomustuld hold. Also, the constraint \(1 \leq x_1, y_1, x_2, y_2 \leq 10^9\) must hold.No two segments of the same color should have common points. The set of points where sparks appear should be the same as given in the input.The number of segments \((h + v)\) should be minimum possible. It's easy to see that the answer always exists. If there are multiple possible answers, print any.
|
In the first example Pasha could have assembled the following scheme: In this scheme there are \(2\) wires of each color: red ones connecting \((5, 2)\) with \((1, 2)\) and \((1, 4)\) with \((5, 4)\), blue ones connecting \((2, 1)\) with \((2, 5)\) and \((4, 5)\) with \((4, 1)\). Note that the sparks appear in the points that are described in the input, they are shown in yellow on the picture. For example, Pasha will see the spark in the point \((2, 4)\) because the second red wire and the first blue wire intersect there. It is possible to show that we can't have less than \(4\) wires to produce a scheme with same sparks positions.
|
Input: 42 22 44 24 4 | Output: 25 2 1 21 4 5 422 1 2 54 5 4 1
|
Master
| 2 | 1,572 | 300 | 1,178 | 10 |
292 |
C
|
292C
|
C. Beautiful IP Addresses
| 2,000 |
brute force
|
The problem uses a simplified TCP/IP address model, please read the statement carefully.An IP address is a 32-bit integer, represented as a group of four decimal 8-bit integers (without leading zeroes), separated by commas. For example, record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In the given problem an arbitrary group of four 8-bit integers is a correct IP address.Our hero Polycarpus still works as a system administrator in some large corporation. He likes beautiful IP addresses. To check if some IP address is beautiful, he should do the following: write out in a line four 8-bit numbers of the IP address, without the commas; check if the resulting string is a palindrome. Let us remind you that a palindrome is a string that reads the same from right to left and from left to right.For example, IP addresses 12.102.20.121 and 0.3.14.130 are beautiful (as strings ""1210220121"" and ""0314130"" are palindromes), and IP addresses 1.20.20.1 and 100.4.4.1 are not.Polycarpus wants to find all beautiful IP addresses that have the given set of digits. Each digit from the set must occur in the IP address at least once. IP address must not contain any other digits. Help him to cope with this difficult task.
|
The first line contains a single integer n (1 β€ n β€ 10) β the number of digits in the set. The second line contains the set of integers a1, a2, ..., an (0 β€ ai β€ 9). It is guaranteed that all digits in the set are distinct.
|
In the first line print a single integer k β the number of beautiful IP addresses that contain the given set of digits. In the following k lines print the IP addresses, one per line in the arbitrary order.
|
Input: 60 1 2 9 8 7 | Output: 678.190.209.18779.180.208.19787.190.209.17889.170.207.19897.180.208.17998.170.207.189
|
Hard
| 1 | 1,262 | 223 | 205 | 2 |
|
690 |
A1
|
690A1
|
A1. Collective Mindsets (easy)
| 1,100 |
Tonight is brain dinner night and all zombies will gather together to scarf down some delicious brains. The artful Heidi plans to crash the party, incognito, disguised as one of them. Her objective is to get away with at least one brain, so she can analyze the zombies' mindset back home and gain a strategic advantage.They will be N guests tonight: N - 1 real zombies and a fake one, our Heidi. The living-dead love hierarchies as much as they love brains: each one has a unique rank in the range 1 to N - 1, and Heidi, who still appears slightly different from the others, is attributed the highest rank, N. Tonight there will be a chest with brains on display and every attendee sees how many there are. These will then be split among the attendees according to the following procedure:The zombie of the highest rank makes a suggestion on who gets how many brains (every brain is an indivisible entity). A vote follows. If at least half of the attendees accept the offer, the brains are shared in the suggested way and the feast begins. But if majority is not reached, then the highest-ranked zombie is killed, and the next zombie in hierarchy has to make a suggestion. If he is killed too, then the third highest-ranked makes one, etc. (It's enough to have exactly half of the votes β in case of a tie, the vote of the highest-ranked alive zombie counts twice, and he will of course vote in favor of his own suggestion in order to stay alive.)You should know that zombies are very greedy and sly, and they know this too β basically all zombie brains are alike. Consequently, a zombie will never accept an offer which is suboptimal for him. That is, if an offer is not strictly better than a potential later offer, he will vote against it. And make no mistake: while zombies may normally seem rather dull, tonight their intellects are perfect. Each zombie's priorities for tonight are, in descending order: survive the event (they experienced death already once and know it is no fun), get as many brains as possible. Heidi goes first and must make an offer which at least half of the attendees will accept, and which allocates at least one brain for Heidi herself.What is the smallest number of brains that have to be in the chest for this to be possible?
|
The only line of input contains one integer: N, the number of attendees (1 β€ N β€ 109).
|
Output one integer: the smallest number of brains in the chest which allows Heidi to take one brain home.
|
Input: 1 | Output: 1
|
Easy
| 0 | 2,259 | 86 | 105 | 6 |
||
932 |
F
|
932F
|
F. Escape Through Leaf
| 2,700 |
data structures; dp; geometry
|
You are given a tree with n nodes (numbered from 1 to n) rooted at node 1. Also, each node has two values associated with it. The values for i-th node are ai and bi.You can jump from a node to any node in its subtree. The cost of one jump from node x to node y is the product of ax and by. The total cost of a path formed by one or more jumps is sum of costs of individual jumps. For every node, calculate the minimum total cost to reach any leaf from that node. Pay attention, that root can never be leaf, even if it has degree 1.Note that you cannot jump from a node to itself.
|
The first line of input contains an integer n (2 β€ n β€ 105) β the number of nodes in the tree.The second line contains n space-separated integers a1, a2, ..., an( - 105 β€ ai β€ 105).The third line contains n space-separated integers b1, b2, ..., bn( - 105 β€ bi β€ 105).Next n - 1 lines contains two space-separated integers ui and vi (1 β€ ui, vi β€ n) describing edge between nodes ui and vi in the tree.
|
Output n space-separated integers, i-th of which denotes the minimum cost of a path from node i to reach any leaf.
|
In the first example, node 3 is already a leaf, so the cost is 0. For node 2, jump to node 3 with cost a2 Γ b3 = 50. For node 1, jump directly to node 3 with cost a1 Γ b3 = 10.In the second example, node 3 and node 4 are leaves, so the cost is 0. For node 2, jump to node 4 with cost a2 Γ b4 = 100. For node 1, jump to node 2 with cost a1 Γ b2 = - 400 followed by a jump from 2 to 4 with cost a2 Γ b4 = 100.
|
Input: 32 10 -17 -7 52 32 1 | Output: 10 50 0
|
Master
| 3 | 579 | 401 | 114 | 9 |
898 |
C
|
898C
|
C. Phone Numbers
| 1,400 |
implementation; strings
|
Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.Vasya decided to organize information about the phone numbers of friends. You will be given n strings β all entries from Vasya's phone books. Each entry starts with a friend's name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.The task is to print organized information about the phone numbers of Vasya's friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn't print number x. If the number of a friend in the Vasya's phone books is recorded several times in the same format, it is necessary to take it into account exactly once.Read the examples to understand statement and format of the output better.
|
First line contains the integer n (1 β€ n β€ 20) β number of entries in Vasya's phone books. The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya's friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.
|
Print out the ordered information about the phone numbers of Vasya's friends. First output m β number of friends that are found in Vasya's phone books.The following m lines must contain entries in the following format ""name number_of_phone_numbers phone_numbers"". Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.
|
Input: 2ivan 1 00123masha 1 00123 | Output: 2masha 1 00123 ivan 1 00123
|
Easy
| 2 | 1,287 | 579 | 490 | 8 |
|
1,556 |
D
|
1556D
|
D. Take a Guess
| 1,800 |
bitmasks; constructive algorithms; interactive; math
|
This is an interactive taskWilliam has a certain sequence of integers \(a_1, a_2, \dots, a_n\) in his mind, but due to security concerns, he does not want to reveal it to you completely. William is ready to respond to no more than \(2 \cdot n\) of the following questions: What is the result of a bitwise AND of two items with indices \(i\) and \(j\) (\(i \neq j\)) What is the result of a bitwise OR of two items with indices \(i\) and \(j\) (\(i \neq j\)) You can ask William these questions and you need to find the \(k\)-th smallest number of the sequence.Formally the \(k\)-th smallest number is equal to the number at the \(k\)-th place in a 1-indexed array sorted in non-decreasing order. For example in array \([5, 3, 3, 10, 1]\) \(4\)th smallest number is equal to \(5\), and \(2\)nd and \(3\)rd are \(3\).
|
It is guaranteed that for each element in a sequence the condition \(0 \le a_i \le 10^9\) is satisfied.
|
In the example, the hidden sequence is \([1, 6, 4, 2, 3, 5, 4]\).Below is the interaction in the example.Query (contestant's program)Response (interactor)Notesand 2 52\(a_2=6\), \(a_5=3\). Interactor returns bitwise AND of the given numbers.or 5 67\(a_5=3\), \(a_6=5\). Interactor returns bitwise OR of the given numbers.finish 5\(5\) is the correct answer. Note that you must find the value and not the index of the kth smallest number.
|
Input: 7 6 2 7 | Output: and 2 5 or 5 6 finish 5
|
Medium
| 4 | 815 | 103 | 0 | 15 |
|
822 |
E
|
822E
|
E. Liar
| 2,400 |
binary search; dp; hashing; string suffix structures
|
The first semester ended. You know, after the end of the first semester the holidays begin. On holidays Noora decided to return to ViΔkopolis. As a modest souvenir for Leha, she brought a sausage of length m from Pavlopolis. Everyone knows that any sausage can be represented as a string of lowercase English letters, the length of which is equal to the length of the sausage.Leha was very pleased with the gift and immediately ate the sausage. But then he realized that it was a quite tactless act, because the sausage was a souvenir! So the hacker immediately went to the butcher shop. Unfortunately, there was only another sausage of length n in the shop. However Leha was not upset and bought this sausage. After coming home, he decided to cut the purchased sausage into several pieces and number the pieces starting from 1 from left to right. Then he wants to select several pieces and glue them together so that the obtained sausage is equal to the sausage that Noora gave. But the hacker can glue two pieces together only when the number of the left piece is less than the number of the right piece. Besides he knows that if he glues more than x pieces, Noora will notice that he has falsified souvenir sausage and will be very upset. Of course Leha doesnβt want to upset the girl. The hacker asks you to find out whether he is able to cut the sausage he bought, and then glue some of the pieces so that Noora doesn't notice anything.Formally, you are given two strings s and t. The length of the string s is n, the length of the string t is m. It is required to select several pairwise non-intersecting substrings from s, so that their concatenation in the same order as these substrings appear in s, is equal to the string t. Denote by f(s, t) the minimal number of substrings to be chosen so that their concatenation is equal to the string t. If it is impossible to choose such substrings, then f(s, t) = β. Leha really wants to know whether itβs true that f(s, t) β€ x.
|
The first line contains single integer n (1 β€ n β€ 105) β length of sausage bought by Leha, i.e. the length of the string s.The second line contains string s of the length n consisting of lowercase English letters.The third line contains single integer m (1 β€ m β€ n) β length of sausage bought by Noora, i.e. the length of the string t.The fourth line contains string t of the length m consisting of lowercase English letters.The fifth line contains single integer x (1 β€ x β€ 30) β the maximum number of pieces of sausage that Leha can glue so that Noora doesnβt notice anything.
|
In the only line print ""YES"" (without quotes), if Leha is able to succeed in creating new sausage so that Noora doesn't notice anything. Otherwise print ""NO"" (without quotes).
|
Let's consider the first sample.In the optimal answer, Leha should cut the sausage he bought in the following way: hloyaygrt = h + loy + a + y + g + rt. Then he numbers received parts from 1 to 6: h β number 1 loy β number 2 a β number 3 y β number 4 g β number 5 rt β number 6 Hereupon the hacker should glue the parts with numbers 2, 4 and 6 and get sausage loyygrt equal to one that is given by Noora. Thus, he will have to glue three pieces. Since x = 3 you should print ""YES"" (without quotes).In the second sample both sausages coincide with sausages from the first sample. However since x = 2 you should print ""NO"" (without quotes).
|
Input: 9hloyaygrt6loyyrt3 | Output: YES
|
Expert
| 4 | 1,979 | 578 | 179 | 8 |
452 |
F
|
452F
|
F. Permutation
| 2,700 |
data structures; divide and conquer; hashing
|
You are given a permutation of numbers from 1 to n. Determine whether there's a pair of integers a, b (1 β€ a, b β€ n; a β b) such that the element (note, that it is usual division, not integer one) is between a and b in this permutation.
|
First line consists of a single integer n (1 β€ n β€ 300000) β the size of permutation.Second line contains n integers β the permutation itself.
|
Print ""YES"", if such a pair exists, ""NO"" otherwise (in both cases without quotes, the answer is case insensitive).
|
In the second example 2 is between 1 and 3. Additionally 4 is between 3 and 5.
|
Input: 41 3 4 2 | Output: NO
|
Master
| 3 | 236 | 142 | 118 | 4 |
99 |
A
|
99A
|
A. Help Far Away Kingdom
| 800 |
strings
|
In a far away kingdom lived the King, the Prince, the Shoemaker, the Dressmaker and many other citizens. They lived happily until great trouble came into the Kingdom. The ACMers settled there.Most damage those strange creatures inflicted upon the kingdom was that they loved high precision numbers. As a result, the Kingdom healers had already had three appointments with the merchants who were asked to sell, say, exactly 0.273549107 beer barrels. To deal with the problem somehow, the King issued an order obliging rounding up all numbers to the closest integer to simplify calculations. Specifically, the order went like this: If a number's integer part does not end with digit 9 and its fractional part is strictly less than 0.5, then the rounded up number coincides with the numberβs integer part. If a number's integer part does not end with digit 9 and its fractional part is not less than 0.5, the rounded up number is obtained if we add 1 to the last digit of the numberβs integer part. If the numberβs integer part ends with digit 9, to round up the numbers one should go to Vasilisa the Wise. In the whole Kingdom she is the only one who can perform the tricky operation of carrying into the next position. Merchants found the algorithm very sophisticated and they asked you (the ACMers) to help them. Can you write a program that would perform the rounding according to the Kingβs order?
|
The first line contains a single number to round up β the integer part (a non-empty set of decimal digits that do not start with 0 β with the exception of a case when the set consists of a single digit β in this case 0 can go first), then follows character Β«.Β» (a dot), and then follows the fractional part (any non-empty set of decimal digits). The number's length does not exceed 1000 characters, including the dot. There are no other characters in the input data.
|
If the last number of the integer part is not equal to 9, print the rounded-up number without leading zeroes. Otherwise, print the message ""GOTO Vasilisa."" (without the quotes).
|
Input: 0.0 | Output: 0
|
Beginner
| 1 | 1,399 | 466 | 179 | 0 |
|
1,780 |
D
|
1780D
|
D. Bit Guessing Game
| 1,800 |
binary search; bitmasks; constructive algorithms; interactive
|
This is an interactive problem.Kira has a hidden positive integer \(n\), and Hayato needs to guess it.Initially, Kira gives Hayato the value \(\mathrm{cnt}\) β the number of unit bits in the binary notation of \(n\). To guess \(n\), Hayato can only do operations of one kind: choose an integer \(x\) and subtract it from \(n\). Note that after each operation, the number \(n\) changes. Kira doesn't like bad requests, so if Hayato tries to subtract a number \(x\) greater than \(n\), he will lose to Kira. After each operation, Kira gives Hayato the updated value \(\mathrm{cnt}\) β the number of unit bits in the binary notation of the updated value of \(n\).Kira doesn't have much patience, so Hayato must guess the original value of \(n\) after no more than \(30\) operations.Since Hayato is in elementary school, he asks for your help. Write a program that guesses the number \(n\). Kira is an honest person, so he chooses the initial number \(n\) before all operations and does not change it afterward.
|
The input data contains several test cases. The first line contains one integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains the number \(\mathrm{cnt}\) β the initial number of unit bits in the binary notation \(n\).The hidden integer \(n\) satisfies the following constraint: \(1 \le n \le 10^9\).
|
For example, the number of unit bits in number \(6\) is \(2\), because binary notation of \(6\) is \(110\). For \(13\) the number of unit bits is \(3\), because \(13_{10} = 1101_2\).In the first test case, \(n = 1\), so the input is the number \(1\). After subtracting one from \(n\), it becomes zero, so the number of unit bits in it is \(0\).In the third test case, \(n = 3\), which in binary representation looks like \(3_{10} = 11_2\), so the input is the number of ones, that is \(2\). After subtracting \(2\), \(n = 1\), so the number of unit bits is now \(1\). After subtracting one from \(n\), it becomes equal to zero.Note that the blank lines in the input and output examples are shown for clarity and are not present in the actual interaction.
|
Input: 3 1 0 1 1 0 2 1 0 | Output: - 1 ! 1 - 1 - 1 ! 2 - 2 - 1 ! 3
|
Medium
| 4 | 1,007 | 393 | 0 | 17 |
|
32 |
C
|
32C
|
C. Flea
| 1,700 |
math
|
It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n Γ m centimeters (each cell is 1 Γ 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.
|
The first line contains three integers n, m, s (1 β€ n, m, s β€ 106) β length of the board, width of the board and length of the flea's jump.
|
Output the only integer β the number of the required starting positions of the flea.
|
Input: 2 3 1000000 | Output: 6
|
Medium
| 1 | 682 | 139 | 84 | 0 |
|
808 |
D
|
808D
|
D. Array Division
| 1,900 |
binary search; data structures; implementation
|
Vasya has an array a consisting of positive integer numbers. Vasya wants to divide this array into two non-empty consecutive parts (the prefix and the suffix) so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position).Inserting an element in the same position he was erased from is also considered moving.Can Vasya divide the array after choosing the right element to move and its new position?
|
The first line contains single integer n (1 β€ n β€ 100000) β the size of the array.The second line contains n integers a1, a2... an (1 β€ ai β€ 109) β the elements of the array.
|
Print YES if Vasya can divide the array after moving one element. Otherwise print NO.
|
In the first example Vasya can move the second element to the end of the array.In the second example no move can make the division possible.In the third example Vasya can move the fourth element by one position to the left.
|
Input: 31 3 2 | Output: YES
|
Hard
| 3 | 593 | 174 | 85 | 8 |
18 |
D
|
18D
|
D. Seller Bob
| 2,000 |
brute force; dp; greedy
|
Last year Bob earned by selling memory sticks. During each of n days of his work one of the two following events took place: A customer came to Bob and asked to sell him a 2x MB memory stick. If Bob had such a stick, he sold it and got 2x berllars. Bob won some programming competition and got a 2x MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last n days, Bob wants to know, how much money he could have earned, if he had acted optimally.
|
The first input line contains number n (1 β€ n β€ 5000) β amount of Bob's working days. The following n lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2x MB memory stick (0 β€ x β€ 2000). It's guaranteed that for each x there is not more than one line sell x. Line win x stands for a day when Bob won a 2x MB memory stick (0 β€ x β€ 2000).
|
Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time.
|
Input: 7win 10win 5win 3sell 5sell 3win 10sell 10 | Output: 1056
|
Hard
| 3 | 859 | 395 | 203 | 0 |
|
2,111 |
C
|
2111C
|
C. Equal Values
| 1,100 |
brute force; greedy; two pointers
|
You are given an array \(a_1, a_2, \dots, a_n\), consisting of \(n\) integers.In one operation, you are allowed to perform one of the following actions: Choose a position \(i\) (\(1 < i \le n\)) and make all elements to the left of \(i\) equal to \(a_i\). That is, assign the value \(a_i\) to all \(a_j\) (\(1 \le j < i\)). The cost of such an operation is \((i - 1) \cdot a_i\). Choose a position \(i\) (\(1 \le i < n\)) and make all elements to the right of \(i\) equal to \(a_i\). That is, assign the value \(a_i\) to all \(a_j\) (\(i < j \le n\)). The cost of such an operation is \((n - i) \cdot a_i\). Note that the elements affected by an operation may already be equal to \(a_i\), but that doesn't change the cost.You are allowed to perform any number of operations (including zero). What is the minimum total cost to make all elements of the array equal?
|
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 a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).The sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, print a single integer β the minimum total cost of operations to make all elements of the array equal.
|
In the first test case, you can perform the operation twice: choose \(i = 3\) and make all elements to the left of it equal to it, the cost will be \(2 \cdot 1 = 2\); choose \(i = 3\) and make all elements to the right of it equal to it, the cost will be \(1 \cdot 1 = 1\). The total cost is \(2 + 1 = 3\).In the second test case, all elements are already equal, so no operations need to be performed.
|
Input: 342 4 1 331 1 1107 5 5 5 10 9 9 4 6 10 | Output: 3 0 35
|
Easy
| 3 | 863 | 345 | 122 | 21 |
11 |
D
|
11D
|
D. A Simple Task
| 2,200 |
bitmasks; dp; graphs
|
Given a simple graph, output the number of simple cycles in it. A simple cycle is a cycle with no repeated vertices or edges.
|
The first line of input contains two integers n and m (1 β€ n β€ 19, 0 β€ m) β respectively the number of vertices and edges of the graph. Each of the subsequent m lines contains two integers a and b, (1 β€ a, b β€ n, a β b) indicating that vertices a and b are connected by an undirected edge. There is no more than one edge connecting any pair of vertices.
|
Output the number of cycles in the given graph.
|
The example graph is a clique and contains four cycles of length 3 and three cycles of length 4.
|
Input: 4 61 21 31 42 32 43 4 | Output: 7
|
Hard
| 3 | 125 | 353 | 47 | 0 |
14 |
C
|
14C
|
C. Four Segments
| 1,700 |
brute force; constructive algorithms; geometry; implementation; math
|
Several months later Alex finally got his brother Bob's creation by post. And now, in his turn, Alex wants to boast about something to his brother. He thought for a while, and came to the conclusion that he has no ready creations, and decided to write a program for rectangles detection. According to his plan, the program detects if the four given segments form a rectangle of a positive area and with sides parallel to coordinate axes. As Alex does badly at school and can't write this program by himself, he asks you to help him.
|
The input data contain four lines. Each of these lines contains four integers x1, y1, x2, y2 ( - 109 β€ x1, y1, x2, y2 β€ 109) β coordinates of segment's beginning and end positions. The given segments can degenerate into points.
|
Output the word Β«YESΒ», if the given four segments form the required rectangle, otherwise output Β«NOΒ».
|
Input: 1 1 6 11 0 6 06 0 6 11 1 1 0 | Output: YES
|
Medium
| 5 | 532 | 227 | 101 | 0 |
|
893 |
E
|
893E
|
E. Counting Arrays
| 2,000 |
combinatorics; dp; math; number theory
|
You are given two positive integer numbers x and y. An array F is called an y-factorization of x iff the following conditions are met: There are y elements in F, and all of them are integer numbers; . You have to count the number of pairwise distinct arrays that are y-factorizations of x. Two arrays A and B are considered different iff there exists at least one index i (1 β€ i β€ y) such that Ai β Bi. Since the answer can be very large, print it modulo 109 + 7.
|
The first line contains one integer q (1 β€ q β€ 105) β the number of testcases to solve.Then q lines follow, each containing two integers xi and yi (1 β€ xi, yi β€ 106). Each of these lines represents a testcase.
|
Print q integers. i-th integer has to be equal to the number of yi-factorizations of xi modulo 109 + 7.
|
In the second testcase of the example there are six y-factorizations: { - 4, - 1}; { - 2, - 2}; { - 1, - 4}; {1, 4}; {2, 2}; {4, 1}.
|
Input: 26 34 2 | Output: 366
|
Hard
| 4 | 463 | 209 | 103 | 8 |
1,811 |
D
|
1811D
|
D. Umka and a Long Flight
| 1,600 |
constructive algorithms; implementation; math
|
The girl Umka loves to travel and participate in math olympiads. One day she was flying by plane to the next olympiad and out of boredom explored a huge checkered sheet of paper.Denote the \(n\)-th Fibonacci number as \(F_n = \begin{cases} 1, & n = 0; \\ 1, & n = 1; \\ F_{n-2} + F_{n-1}, & n \ge 2. \end{cases}\)A checkered rectangle with a height of \(F_n\) and a width of \(F_{n+1}\) is called a Fibonacci rectangle of order \(n\).Umka has a Fibonacci rectangle of order \(n\). Someone colored a cell in it at the intersection of the row \(x\) and the column \(y\).It is necessary to cut this rectangle exactly into \(n+1\) squares in such way that the painted cell was in a square with a side of \(1\); there was at most one pair of squares with equal sides; the side of each square was equal to a Fibonacci number. Will Umka be able to cut this rectangle in that way?
|
The first line contains an integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β number of test cases.For each test case the integers \(n\), \(x\), \(y\) are given (\(1 \le n \le 44\), \(1 \le x \le F_n\), \(1 \le y \le F_{n+1}\)) β the order of the Fibonacci rectangle and the coordinates of the colored cell.
|
For each test case, print ""YES"" if the answer is positive, and ""NO"" otherwise.You can print ""YES"" and ""NO"" in any case (for example, the strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive answer).
|
The first, third and fourth test cases.
|
Input: 121 1 12 1 23 1 43 3 24 4 64 3 35 6 55 4 125 2 124 2 11 1 244 758465880 1277583853 | Output: YES NO YES YES YES NO YES NO NO YES YES NO
|
Medium
| 3 | 872 | 303 | 223 | 18 |
1,076 |
A
|
1076A
|
A. Minimizing the String
| 1,200 |
greedy; strings
|
You are given a string \(s\) consisting of \(n\) lowercase Latin letters.You have to remove at most one (i.e. zero or one) character of this string in such a way that the string you obtain will be lexicographically smallest among all strings that can be obtained using this operation.String \(s = s_1 s_2 \dots s_n\) is lexicographically smaller than string \(t = t_1 t_2 \dots t_m\) if \(n < m\) and \(s_1 = t_1, s_2 = t_2, \dots, s_n = t_n\) or there exists a number \(p\) such that \(p \le min(n, m)\) and \(s_1 = t_1, s_2 = t_2, \dots, s_{p-1} = t_{p-1}\) and \(s_p < t_p\).For example, ""aaa"" is smaller than ""aaaa"", ""abb"" is smaller than ""abc"", ""pqr"" is smaller than ""z"".
|
The first line of the input contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of \(s\).The second line of the input contains exactly \(n\) lowercase Latin letters β the string \(s\).
|
Print one string β the smallest possible lexicographically string that can be obtained by removing at most one character from the string \(s\).
|
In the first example you can remove any character of \(s\) to obtain the string ""aa"".In the second example ""abca"" < ""abcd"" < ""abcda"" < ""abda"" < ""acda"" < ""bcda"".
|
Input: 3 aaa | Output: aa
|
Easy
| 2 | 688 | 203 | 143 | 10 |
1,183 |
A
|
1183A
|
A. Nearest Interesting Number
| 800 |
implementation
|
Polycarp knows that if the sum of the digits of a number is divisible by \(3\), then the number itself is divisible by \(3\). He assumes that the numbers, the sum of the digits of which is divisible by \(4\), are also somewhat interesting. Thus, he considers a positive integer \(n\) interesting if its sum of digits is divisible by \(4\).Help Polycarp find the nearest larger or equal interesting number for the given number \(a\). That is, find the interesting number \(n\) such that \(n \ge a\) and \(n\) is minimal.
|
The only line in the input contains an integer \(a\) (\(1 \le a \le 1000\)).
|
Print the nearest greater or equal interesting number for the given number \(a\). In other words, print the interesting number \(n\) such that \(n \ge a\) and \(n\) is minimal.
|
Input: 432 | Output: 435
|
Beginner
| 1 | 519 | 76 | 176 | 11 |
|
1,216 |
E2
|
1216E2
|
E2. Numerical Sequence (hard version)
| 2,200 |
binary search; math
|
The only difference between the easy and the hard versions is the maximum value of \(k\).You are given an infinite sequence of form ""112123123412345\(\dots\)"" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from \(1\) to \(1\), the second one β from \(1\) to \(2\), the third one β from \(1\) to \(3\), \(\dots\), the \(i\)-th block consists of all numbers from \(1\) to \(i\). So the first \(56\) elements of the sequence are ""11212312341234512345612345671234567812345678912345678910"". Elements of the sequence are numbered from one. For example, the \(1\)-st element of the sequence is \(1\), the \(3\)-rd element of the sequence is \(2\), the \(20\)-th element of the sequence is \(5\), the \(38\)-th element is \(2\), the \(56\)-th element of the sequence is \(0\).Your task is to answer \(q\) independent queries. In the \(i\)-th query you are given one integer \(k_i\). Calculate the digit at the position \(k_i\) of the sequence.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 500\)) β the number of queries.The \(i\)-th of the following \(q\) lines contains one integer \(k_i\) \((1 \le k_i \le 10^{18})\) β the description of the corresponding query.
|
Print \(q\) lines. In the \(i\)-th line print one digit \(x_i\) \((0 \le x_i \le 9)\) β the answer to the query \(i\), i.e. \(x_i\) should be equal to the element at the position \(k_i\) of the sequence.
|
Answers on queries from the first example are described in the problem statement.
|
Input: 5 1 3 20 38 56 | Output: 1 2 5 2 0
|
Hard
| 2 | 1,020 | 245 | 203 | 12 |
525 |
A
|
525A
|
A. Vitaliy and Pie
| 1,100 |
greedy; hashing; strings
|
After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house with n room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third room and so on β you can go from the (n - 1)-th room to the n-th room. Thus, you can go to room x only from room x - 1.The potato pie is located in the n-th room and Vitaly needs to go there. Each pair of consecutive rooms has a door between them. In order to go to room x from room x - 1, you need to open the door between the rooms with the corresponding key. In total the house has several types of doors (represented by uppercase Latin letters) and several types of keys (represented by lowercase Latin letters). The key of type t can open the door of type T if and only if t and T are the same letter, written in different cases. For example, key f can open door F.Each of the first n - 1 rooms contains exactly one key of some type that Vitaly can use to get to next rooms. Once the door is open with some key, Vitaly won't get the key from the keyhole but he will immediately run into the next room. In other words, each key can open no more than one door.Vitaly realizes that he may end up in some room without the key that opens the door to the next room. Before the start his run for the potato pie Vitaly can buy any number of keys of any type that is guaranteed to get to room n.Given the plan of the house, Vitaly wants to know what is the minimum number of keys he needs to buy to surely get to the room n, which has a delicious potato pie. Write a program that will help Vitaly find out this number.
|
The first line of the input contains a positive integer n (2 β€ n β€ 105) β the number of rooms in the house.The second line of the input contains string s of length 2Β·n - 2. Let's number the elements of the string from left to right, starting from one. The odd positions in the given string s contain lowercase Latin letters β the types of the keys that lie in the corresponding rooms. Thus, each odd position i of the given string s contains a lowercase Latin letter β the type of the key that lies in room number (i + 1) / 2.The even positions in the given string contain uppercase Latin letters β the types of doors between the rooms. Thus, each even position i of the given string s contains an uppercase letter β the type of the door that leads from room i / 2 to room i / 2 + 1.
|
Print the only integer β the minimum number of keys that Vitaly needs to buy to surely get from room one to room n.
|
Input: 3aAbB | Output: 0
|
Easy
| 3 | 1,732 | 783 | 115 | 5 |
|
1,428 |
G1
|
1428G1
|
G1. Lucky Numbers (Easy Version)
| 2,900 |
dp; greedy
|
This is the easy version of the problem. The only difference is that in this version \(q=1\). You can make hacks only if all versions of the problem are solved.Zookeeper has been teaching his \(q\) sheep how to write and how to add. The \(i\)-th sheep has to write exactly \(k\) non-negative integers with the sum \(n_i\).Strangely, sheep have superstitions about digits and believe that the digits \(3\), \(6\), and \(9\) are lucky. To them, the fortune of a number depends on the decimal representation of the number; the fortune of a number is equal to the sum of fortunes of its digits, and the fortune of a digit depends on its value and position and can be described by the following table. For example, the number \(319\) has fortune \(F_{2} + 3F_{0}\). Each sheep wants to maximize the sum of fortune among all its \(k\) written integers. Can you help them?
|
The first line contains a single integer \(k\) (\(1 \leq k \leq 999999\)): the number of numbers each sheep has to write. The next line contains six integers \(F_0\), \(F_1\), \(F_2\), \(F_3\), \(F_4\), \(F_5\) (\(1 \leq F_i \leq 10^9\)): the fortune assigned to each digit. The next line contains a single integer \(q\) (\(q = 1\)): the number of sheep.Each of the next \(q\) lines contains a single integer \(n_i\) (\(1 \leq n_i \leq 999999\)): the sum of numbers that \(i\)-th sheep has to write. In this version, there is only one line.
|
Print \(q\) lines, where the \(i\)-th line contains the maximum sum of fortune of all numbers of the \(i\)-th sheep. In this version, you should print only one line.
|
In the first test case, \(57 = 9 + 9 + 39\). The three \(9\)'s contribute \(1 \cdot 3\) and \(3\) at the tens position contributes \(2 \cdot 1\). Hence the sum of fortune is \(11\).In the second test case, \(63 = 35 + 19 + 9\). The sum of fortune is \(8\).
|
Input: 3 1 2 3 4 5 6 1 57 | Output: 11
|
Master
| 2 | 865 | 540 | 165 | 14 |
2,101 |
E
|
2101E
|
E. Kia Bakes a Cake
| 3,100 |
data structures; dp; greedy; trees
|
You are given a binary string \(s\) of length \(n\) and a tree \(T\) with \(n\) vertices. Let \(k\) be the number of 1s in \(s\). We will construct a complete undirected weighted graph with \(k\) vertices as follows: For each \(1\le i\le n\) with \(s_i = \mathtt{1}\), create a vertex labeled \(i\). For any two vertices labeled \(u\) and \(v\) that are created in the above step, define the edge weight between them \(w(u, v)\) as the distance\(^{\text{β}}\) between vertex \(u\) and vertex \(v\) in the tree \(T\). A simple path\(^{\text{β }}\) that visits vertices labeled \(v_1, v_2, \ldots, v_m\) in this order is nice if for all \(1\le i\le m - 2\), the condition \(2\cdot w(v_i, v_{i + 1})\le w(v_{i + 1}, v_{i + 2})\) holds. In other words, the weight of each edge in the path must be at least twice the weight of the previous edge. Note that \(s_{v_i} = \mathtt{1}\) has to be satisfied for all \(1\le i\le m\), as otherwise, there would be no vertex with the corresponding label.For each vertex labeled \(i\) (\(1\le i\le n\) and \(s_i = \mathtt{1}\)) in the complete undirected weighted graph, determine the maximum number of vertices in any nice simple path starting from the vertex labeled \(i\).\(^{\text{β}}\)The distance between two vertices \(a\) and \(b\) in a tree is equal to the number of edges on the unique simple path between vertex \(a\) and vertex \(b\).\(^{\text{β }}\)A path is a sequence of vertices \(v_1, v_2, \ldots, v_m\) such that there is an edge between \(v_i\) and \(v_{i + 1}\) for all \(1\le i\le m - 1\). A simple path is a path with no repeated vertices, i.e., \(v_i\neq v_j\) for all \(1\le i < j\le m\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1\le n\le 7\cdot10^4\)) β the length of the binary string \(s\) and the number of vertices in the tree \(T\).The second line of each test case contains a binary string with \(n\) characters \(s_1s_2\ldots s_n\) (\(s_i\in \{\mathtt{0}, \mathtt{1}\}\)) β the string representing the vertices to be constructed in the complete undirected weighted graph.Each of the next \(n - 1\) lines contains two integers \(u\) and \(v\) (\(1\le u, v\le n\)) β the endpoints of the edges of the tree \(T\).It is guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(7\cdot10^4\).
|
For each test case, output \(n\) integers, the \(i\)-th integer representing the maximum number of vertices in any nice simple path starting from the vertex labeled \(i\). If there is no vertex labeled \(i\), i.e., \(s_i = \mathtt{0}\), output \(-1\) instead.
|
In the first test case, the tree \(T\) and the constructed graph are as follows: Left side is the tree \(T\) with selected nodes colored yellow. The right side is the constructed complete graph. The nice path shown in the diagram is \(3\rightarrow 4\rightarrow 2\). The path is nice as \(w(4, 2) = 2\) is at least twice of \(w(3, 4) = 1\). Extending the path using \(2\rightarrow 5\) is not possible as \(w(2, 5) = 3\) is less than twice of \(w(4, 2) = 2\).In the second test case, the tree \(T\) is a simple path of length \(17\). An example of a nice path starting from the vertex labeled \(2\) is \(2\rightarrow 3\rightarrow 5\rightarrow 9\rightarrow 17\), which has edge weights of \(1, 2, 4, 8\) doubling each time.
|
Input: 35011111 22 33 44 517011010111101011011 22 33 44 55 66 77 88 99 1010 1111 1212 1313 1414 1515 1616 172011 2 | Output: -1 3 3 3 3 -1 5 4 -1 4 -1 5 5 5 5 -1 4 -1 5 5 -1 3 -1 1
|
Master
| 4 | 1,644 | 859 | 259 | 21 |
1,929 |
B
|
1929B
|
B. Sasha and the Drawing
| 800 |
constructive algorithms; greedy; math
|
Even in kindergarten, Sasha liked a girl. Therefore, he wanted to give her a drawing and attract her attention.As a drawing, he decided to draw a square grid of size \(n \times n\), in which some cells are colored. But coloring the cells is difficult, so he wants to color as few cells as possible. But at the same time, he wants at least \(k\) diagonals to have at least one colored cell. Note that the square grid of size \(n \times n\) has a total of \(4n - 2\) diagonals.Help little Sasha to make the girl fall in love with him and tell him the minimum number of cells he needs to color.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 10^8\), \(1 \leq k \leq 4n - 2\)) β the size of the square grid and the minimum number of diagonals in which there should be at least one colored cell.
|
For each test case, output a single integer β the minimum number of cells that need to be colored.
|
In the pictures below, the colored cells are marked in black, and all diagonals are marked in purple.In the first test case, you can color \(2\) cells so that \(4\) diagonals contain at least one colored cell: In the third test case, you can color \(6\) cells so that all \(10\) diagonals contain at least one colored cell:
|
Input: 73 43 33 103 94 77 112 3 | Output: 2 2 6 5 4 6 2
|
Beginner
| 3 | 591 | 421 | 98 | 19 |
1,292 |
F
|
1292F
|
F. Nora's Toy Boxes
| 3,500 |
bitmasks; combinatorics; dp
|
SIHanatsuka - EMber SIHanatsuka - ATONEMENTBack in time, the seven-year-old Nora used to play lots of games with her creation ROBO_Head-02, both to have fun and enhance his abilities.One day, Nora's adoptive father, Phoenix Wyle, brought Nora \(n\) boxes of toys. Before unpacking, Nora decided to make a fun game for ROBO.She labelled all \(n\) boxes with \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) and asked ROBO to do the following action several (possibly zero) times: Pick three distinct indices \(i\), \(j\) and \(k\), such that \(a_i \mid a_j\) and \(a_i \mid a_k\). In other words, \(a_i\) divides both \(a_j\) and \(a_k\), that is \(a_j \bmod a_i = 0\), \(a_k \bmod a_i = 0\). After choosing, Nora will give the \(k\)-th box to ROBO, and he will place it on top of the box pile at his side. Initially, the pile is empty. After doing so, the box \(k\) becomes unavailable for any further actions. Being amused after nine different tries of the game, Nora asked ROBO to calculate the number of possible different piles having the largest amount of boxes in them. Two piles are considered different if there exists a position where those two piles have different boxes.Since ROBO was still in his infant stages, and Nora was still too young to concentrate for a long time, both fell asleep before finding the final answer. Can you help them?As the number of such piles can be very large, you should print the answer modulo \(10^9 + 7\).
|
The first line contains an integer \(n\) (\(3 \le n \le 60\)), denoting the number of boxes.The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 60\)), where \(a_i\) is the label of the \(i\)-th box.
|
Print the number of distinct piles having the maximum number of boxes that ROBO_Head can have, modulo \(10^9 + 7\).
|
Let's illustrate the box pile as a sequence \(b\), with the pile's bottommost box being at the leftmost position.In the first example, there are \(2\) distinct piles possible: \(b = [6]\) (\([2, \mathbf{6}, 8] \xrightarrow{(1, 3, 2)} [2, 8]\)) \(b = [8]\) (\([2, 6, \mathbf{8}] \xrightarrow{(1, 2, 3)} [2, 6]\)) In the second example, there are \(4\) distinct piles possible: \(b = [9, 12]\) (\([2, 3, 4, \mathbf{9}, 12] \xrightarrow{(2, 5, 4)} [2, 3, 4, \mathbf{12}] \xrightarrow{(1, 3, 4)} [2, 3, 4]\)) \(b = [4, 12]\) (\([2, 3, \mathbf{4}, 9, 12] \xrightarrow{(1, 5, 3)} [2, 3, 9, \mathbf{12}] \xrightarrow{(2, 3, 4)} [2, 3, 9]\)) \(b = [4, 9]\) (\([2, 3, \mathbf{4}, 9, 12] \xrightarrow{(1, 5, 3)} [2, 3, \mathbf{9}, 12] \xrightarrow{(2, 4, 3)} [2, 3, 12]\)) \(b = [9, 4]\) (\([2, 3, 4, \mathbf{9}, 12] \xrightarrow{(2, 5, 4)} [2, 3, \mathbf{4}, 12] \xrightarrow{(1, 4, 3)} [2, 3, 12]\)) In the third sequence, ROBO can do nothing at all. Therefore, there is only \(1\) valid pile, and that pile is empty.
|
Input: 3 2 6 8 | Output: 2
|
Master
| 3 | 1,449 | 238 | 115 | 12 |
651 |
A
|
651A
|
A. Joysticks
| 1,100 |
dp; greedy; implementation; math
|
Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at a1 percent and second one is charged at a2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not connected to a charger) or charges by 1 percent (if connected to a charger).Game continues while both joysticks have a positive charge. Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. If some joystick completely discharges (its charge turns to 0), the game also stops.Determine the maximum number of minutes that game can last. It is prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is allowed for joystick to be charged by more than 100 percent.
|
The first line of the input contains two positive integers a1 and a2 (1 β€ a1, a2 β€ 100), the initial charge level of first and second joystick respectively.
|
Output the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged.
|
In the first sample game lasts for 6 minute by using the following algorithm: at the beginning of the first minute connect first joystick to the charger, by the end of this minute first joystick is at 4%, second is at 3%; continue the game without changing charger, by the end of the second minute the first joystick is at 5%, second is at 1%; at the beginning of the third minute connect second joystick to the charger, after this minute the first joystick is at 3%, the second one is at 2%; continue the game without changing charger, by the end of the fourth minute first joystick is at 1%, second one is at 3%; at the beginning of the fifth minute connect first joystick to the charger, after this minute the first joystick is at 2%, the second one is at 1%; at the beginning of the sixth minute connect second joystick to the charger, after this minute the first joystick is at 0%, the second one is at 2%. After that the first joystick is completely discharged and the game is stopped.
|
Input: 3 5 | Output: 6
|
Easy
| 4 | 895 | 156 | 128 | 6 |
1,766 |
F
|
1766F
|
F. MCF
| 2,800 |
flows
|
You are given a graph consisting of \(n\) vertices and \(m\) directed arcs. The \(i\)-th arc goes from the vertex \(x_i\) to the vertex \(y_i\), has capacity \(c_i\) and weight \(w_i\). No arc goes into the vertex \(1\), and no arc goes from the vertex \(n\). There are no cycles of negative weight in the graph (it is impossible to travel from any vertex to itself in such a way that the total weight of all arcs you go through is negative).You have to assign each arc a flow (an integer between \(0\) and its capacity, inclusive). For every vertex except \(1\) and \(n\), the total flow on the arcs going to this vertex must be equal to the total flow on the arcs going from that vertex. Let the flow on the \(i\)-th arc be \(f_i\), then the cost of the flow is equal to \(\sum \limits_{i = 1}^{m} f_i w_i\). You have to find a flow which minimizes the cost.Sounds classical, right? Well, we have some additional constraints on the flow on every edge: if \(c_i\) is even, \(f_i\) must be even; if \(c_i\) is odd, \(f_i\) must be odd. Can you solve this problem?
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 100\); \(1 \le m \le 200\)).Then \(m\) lines follow. The \(i\)-th of them contains four integers \(x_i\), \(y_i\), \(c_i\), and \(w_i\) (\(1 \le x_i \le n - 1\); \(2 \le y_i \le n\); \(x_i \ne y_i\); \(1 \le c_i \le 100\); \(-100 \le w_i \le 100\)). These integers describe the \(i\)-th arc.Additional constraints on the input: there are no negative cycles in the graph.
|
If a flow satisfying all of the constraints does not exist, print Impossible.Otherwise, print two lines: the first line should contain one word Possible; the second line should contain \(m\) integers \(f_1, f_2, \dots, f_m\). If there are multiple answers, print any of them. Note that the cost of the flow should be minimized.
|
Input: 3 3 1 2 3 -10 1 2 3 -15 2 3 2 0 | Output: Possible 1 1 2
|
Master
| 1 | 1,063 | 438 | 327 | 17 |
|
677 |
E
|
677E
|
E. Vanya and Balloons
| 2,300 |
binary search; brute force; dp; implementation
|
Vanya plays a game of balloons on the field of size n Γ n, where each cell contains a balloon with one of the values 0, 1, 2 or 3. The goal is to destroy a cross, such that the product of all values of balloons in the cross is maximum possible. There are two types of crosses: normal and rotated. For example:**o****o**ooooo**o****o**oro***o*o*o***o***o*o*o***oFormally, the cross is given by three integers r, c and d, such that d β€ r, c β€ n - d + 1. The normal cross consists of balloons located in cells (x, y) (where x stay for the number of the row and y for the number of the column), such that |x - r|Β·|y - c| = 0 and |x - r| + |y - c| < d. Rotated cross consists of balloons located in cells (x, y), such that |x - r| = |y - c| and |x - r| < d.Vanya wants to know the maximum possible product of the values of balls forming one cross. As this value can be large, output it modulo 109 + 7.
|
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of rows and columns in the table with balloons.The each of the following n lines contains n characters '0', '1', '2' or '3' β the description of the values in balloons.
|
Print the maximum possible product modulo 109 + 7. Note, that you are not asked to maximize the remainder modulo 109 + 7, but to find the maximum value and print it this modulo.
|
In the first sample, the maximum product is achieved for a rotated cross with a center in the cell (3, 3) and radius 1: 2Β·2Β·3Β·3Β·3 = 108.
|
Input: 41233021320200303 | Output: 108
|
Expert
| 4 | 896 | 252 | 177 | 6 |
1,327 |
B
|
1327B
|
B. Princesses and Princes
| 1,200 |
brute force; graphs; greedy
|
The King of Berland Polycarp LXXXIV has \(n\) daughters. To establish his power to the neighbouring kingdoms he wants to marry his daughters to the princes of these kingdoms. As a lucky coincidence there are \(n\) other kingdoms as well.So Polycarp LXXXIV has enumerated his daughters from \(1\) to \(n\) and the kingdoms from \(1\) to \(n\). For each daughter he has compiled a list of kingdoms princes of which she wanted to marry.Polycarp LXXXIV is very busy, so he finds a couple for his daughters greedily one after another.For the first daughter he takes the kingdom with the lowest number from her list and marries the daughter to their prince. For the second daughter he takes the kingdom with the lowest number from her list, prince of which hasn't been taken already. If there are no free princes in the list then the daughter marries nobody and Polycarp LXXXIV proceeds to the next daughter. The process ends after the \(n\)-th daughter.For example, let there be \(4\) daughters and kingdoms, the lists daughters have are \([2, 3]\), \([1, 2]\), \([3, 4]\), \([3]\), respectively. In that case daughter \(1\) marries the prince of kingdom \(2\), daughter \(2\) marries the prince of kingdom \(1\), daughter \(3\) marries the prince of kingdom \(3\), leaving daughter \(4\) nobody to marry to.Actually, before starting the marriage process Polycarp LXXXIV has the time to convince one of his daughters that some prince is also worth marrying to. Effectively, that means that he can add exactly one kingdom to exactly one of his daughter's list. Note that this kingdom should not be present in the daughter's list.Polycarp LXXXIV wants to increase the number of married couples.Unfortunately, what he doesn't have the time for is determining what entry to add. If there is no way to increase the total number of married couples then output that the marriages are already optimal. Otherwise, find such an entry that the total number of married couples increases if Polycarp LXXXIV adds it.If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.For your and our convenience you are asked to answer \(t\) independent test cases.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of daughters and the number of kingdoms.Each of the next \(n\) lines contains the description of each daughter's list. The first integer \(k\) (\(0 \le k \le n\)) is the number of entries in the \(i\)-th daughter's list. After that \(k\) distinct integers follow \(g_i[1], g_i[2], \dots, g_i[k]\) (\(1 \le g_i[j] \le n\)) β the indices of the kingdoms in the list in the increasing order (\(g_i[1] < g_i[2] < \dots < g_i[k]\)).It's guaranteed that the total number of daughters over all test cases does not exceed \(10^5\).It's also guaranteed that the total number of kingdoms in lists over all test cases does not exceed \(10^5\).
|
For each test case print the answer to it.Print ""IMPROVE"" in the first line if Polycarp LXXXIV can add some kingdom to some of his daughter's list so that the total number of married couples increases. The second line then should contain two integers β the index of the daughter and the index of the kingdom Polycarp LXXXIV should add to that daughter's list.If there are multiple ways to add an entry so that the total number of married couples increases then print any of them.Otherwise the only line should contain one word ""OPTIMAL"".
|
The first test case is depicted in the statement. Adding the fourth kingdom to the list of the fourth daughter makes her marry the prince of the fourth kingdom.In the second test case any new entry will increase the number of marriages from \(0\) to \(1\).In the third and the fourth test cases there is no way to add an entry.In the fifth test case there is no way to change the marriages by adding any entry.
|
Input: 5 4 2 2 3 2 1 2 2 3 4 1 3 2 0 0 3 3 1 2 3 3 1 2 3 3 1 2 3 1 1 1 4 1 1 1 2 1 3 1 4 | Output: IMPROVE 4 4 IMPROVE 1 1 OPTIMAL OPTIMAL OPTIMAL
|
Easy
| 3 | 2,199 | 859 | 541 | 13 |
1,770 |
B
|
1770B
|
B. Koxia and Permutation
| 1,000 |
constructive algorithms
|
Reve has two integers \(n\) and \(k\).Let \(p\) be a permutation\(^\dagger\) of length \(n\). Let \(c\) be an array of length \(n - k + 1\) such that $$$\(c_i = \max(p_i, \dots, p_{i+k-1}) + \min(p_i, \dots, p_{i+k-1}).\)\( Let the cost of the permutation \)p\( be the maximum element of \)c\(.Koxia wants you to construct a permutation with the minimum possible cost.\)^\dagger\( 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).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 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 \(p_1,p_2,\dots,p_n\), which is a permutation with minimal cost. If there is more than one permutation with minimal cost, you may output any of them.
|
In the first test case, \(c_1 = \max(p_1,p_2,p_3) + \min(p_1,p_2,p_3) = 5 + 1 = 6\). \(c_2 = \max(p_2,p_3,p_4) + \min(p_2,p_3,p_4) = 3 + 1 = 4\). \(c_3 = \max(p_3,p_4,p_5) + \min(p_3,p_4,p_5) = 4 + 2 = 6\). Therefore, the cost is \(\max(6,4,6)=6\). It can be proven that this is the minimal cost.
|
Input: 35 35 16 6 | Output: 5 1 2 3 4 1 2 3 4 5 3 2 4 1 6 5
|
Beginner
| 1 | 706 | 384 | 191 | 17 |
1,144 |
B
|
1144B
|
B. Parity Alternated Deletions
| 900 |
greedy; implementation; sortings
|
Polycarp has an array \(a\) consisting of \(n\) integers.He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains \(n-1\) elements). For each of the next moves he chooses any element with the only restriction: its parity should differ from the parity of the element deleted on the previous move. In other words, he alternates parities (even-odd-even-odd-... or odd-even-odd-even-...) of the removed elements. Polycarp stops if he can't make a move.Formally: If it is the first move, he chooses any element and deletes it; If it is the second or any next move: if the last deleted element was odd, Polycarp chooses any even element and deletes it; if the last deleted element was even, Polycarp chooses any odd element and deletes it. If after some move Polycarp cannot make a move, the game ends. Polycarp's goal is to minimize the sum of non-deleted elements of the array after end of the game. If Polycarp can delete the whole array, then the sum of non-deleted elements is zero.Help Polycarp find this value.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2000\)) β the number of elements of \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^6\)), where \(a_i\) is the \(i\)-th element of \(a\).
|
Print one integer β the minimum possible sum of non-deleted elements of the array after end of the game.
|
Input: 5 1 5 7 8 2 | Output: 0
|
Beginner
| 3 | 1,128 | 263 | 104 | 11 |
|
2,030 |
C
|
2030C
|
C. A TRUE Battle
| 1,100 |
brute force; games; greedy
|
Alice and Bob are playing a game. There is a list of \(n\) booleans, each of which is either true or false, given as a binary string \(^{\text{β}}\) of length \(n\) (where \(\texttt{1}\) represents true, and \(\texttt{0}\) represents false). Initially, there are no operators between the booleans.Alice and Bob will take alternate turns placing and or or between the booleans, with Alice going first. Thus, the game will consist of \(n-1\) turns since there are \(n\) booleans. Alice aims for the final statement to evaluate to true, while Bob aims for it to evaluate to false. Given the list of boolean values, determine whether Alice will win if both players play optimally.To evaluate the final expression, repeatedly perform the following steps until the statement consists of a single true or false: If the statement contains an and operator, choose any one and replace the subexpression surrounding it with its evaluation. Otherwise, the statement contains an or operator. Choose any one and replace the subexpression surrounding the or with its evaluation. For example, the expression true or false and false is evaluated as true or (false and false) \(=\) true or false \(=\) true. It can be shown that the result of any compound statement is unique.\(^{\text{β}}\)A binary string is a string that only consists of characters \(\texttt{0}\) and \(\texttt{1}\)
|
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the length of the string.The second line contains a binary string of length \(n\), consisting of characters \(\texttt{0}\) and \(\texttt{1}\) β the list of boolean values.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each testcase, output ""YES"" (without quotes) if Alice wins, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yES"", ""yes"" and ""Yes"" will be recognized as a positive response).
|
In the first testcase, Alice can place and between the two booleans. The game ends as there are no other places to place operators, and Alice wins because true and true is true.In the second testcase, Alice can place or between the middle true and the left false. Bob can place and between the middle true and the right false. The statement false or true and false is false.Note that these examples may not be the best strategies for either Alice or Bob.
|
Input: 5211301012101111111100100111111011801000010 | Output: YES NO YES YES NO
|
Easy
| 3 | 1,367 | 439 | 244 | 20 |
478 |
E
|
478E
|
E. Wavy numbers
| 2,900 |
brute force; dfs and similar; meet-in-the-middle; sortings
|
A wavy number is such positive integer that for any digit of its decimal representation except for the first one and the last one following condition holds: the digit is either strictly larger than both its adjacent digits or strictly less than both its adjacent digits. For example, numbers 35270, 102, 747, 20 and 3 are wavy and numbers 123, 1000 and 2212 are not.The task is to find the k-th smallest wavy number r that is divisible by n for the given integer values n and k.You are to write a program that will find the value of r if it doesn't exceed 1014.
|
The only line of input contains two integers n and k, separated by a single space (1 β€ n, k β€ 1014).
|
Your task is to output the only integer r β the answer to the given problem. If such number does not exist or it is larger than 1014, then print ""-1"" (minus one without the quotes) instead.
|
The values of the first four wavy numbers that are divisible by n for the first sample are: 492, 615, 738 and 1845.
|
Input: 123 4 | Output: 1845
|
Master
| 4 | 561 | 100 | 191 | 4 |
1,234 |
B2
|
1234B2
|
B2. Social Network (hard version)
| 1,300 |
data structures; implementation
|
The only difference between easy and hard versions are constraints on \(n\) and \(k\).You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most \(k\) most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals \(0\)).Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.You (suddenly!) have the ability to see the future. You know that during the day you will receive \(n\) messages, the \(i\)-th message will be received from the friend with ID \(id_i\) (\(1 \le id_i \le 10^9\)).If you receive a message from \(id_i\) in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.Otherwise (i.e. if there is no conversation with \(id_i\) on the screen): Firstly, if the number of conversations displayed on the screen is \(k\), the last conversation (which has the position \(k\)) is removed from the screen. Now the number of conversations on the screen is guaranteed to be less than \(k\) and the conversation with the friend \(id_i\) is not displayed on the screen. The conversation with the friend \(id_i\) appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all \(n\) messages.
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le n, k \le 2 \cdot 10^5)\) β the number of messages and the number of conversations your smartphone can show.The second line of the input contains \(n\) integers \(id_1, id_2, \dots, id_n\) (\(1 \le id_i \le 10^9\)), where \(id_i\) is the ID of the friend which sends you the \(i\)-th message.
|
In the first line of the output print one integer \(m\) (\(1 \le m \le min(n, k)\)) β the number of conversations shown after receiving all \(n\) messages.In the second line print \(m\) integers \(ids_1, ids_2, \dots, ids_m\), where \(ids_i\) should be equal to the ID of the friend corresponding to the conversation displayed on the position \(i\) after receiving all \(n\) messages.
|
In the first example the list of conversations will change in the following way (in order from the first to last message): \([]\); \([1]\); \([2, 1]\); \([3, 2]\); \([3, 2]\); \([1, 3]\); \([1, 3]\); \([2, 1]\). In the second example the list of conversations will change in the following way: \([]\); \([2]\); \([3, 2]\); \([3, 2]\); \([1, 3, 2]\); and then the list will not change till the end.
|
Input: 7 2 1 2 3 2 1 3 2 | Output: 2 2 1
|
Easy
| 2 | 1,679 | 366 | 384 | 12 |
1,693 |
B
|
1693B
|
B. Fake Plastic Trees
| 1,700 |
dfs and similar; dp; greedy; trees
|
We are given a rooted tree consisting of \(n\) vertices numbered from \(1\) to \(n\). The root of the tree is the vertex \(1\) and the parent of the vertex \(v\) is \(p_v\).There is a number written on each vertex, initially all numbers are equal to \(0\). Let's denote the number written on the vertex \(v\) as \(a_v\).For each \(v\), we want \(a_v\) to be between \(l_v\) and \(r_v\) \((l_v \leq a_v \leq r_v)\).In a single operation we do the following: Choose some vertex \(v\). Let \(b_1, b_2, \ldots, b_k\) be vertices on the path from the vertex \(1\) to vertex \(v\) (meaning \(b_1 = 1\), \(b_k = v\) and \(b_i = p_{b_{i + 1}}\)). Choose a non-decreasing array \(c\) of length \(k\) of nonnegative integers: \(0 \leq c_1 \leq c_2 \leq \ldots \leq c_k\). For each \(i\) \((1 \leq i \leq k)\), increase \(a_{b_i}\) by \(c_i\). What's the minimum number of operations needed to achieve our goal?
|
The first line contains an integer \(t\) \((1\le t\le 1000)\) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) \((2\le n\le 2 \cdot 10^5)\) β the number of the vertices in the tree.The second line of each test case contains \(n - 1\) integers, \(p_2, p_3, \ldots, p_n\) \((1 \leq p_i < i)\), where \(p_i\) denotes the parent of the vertex \(i\).The \(i\)-th of the following \(n\) lines contains two integers \(l_i\) and \(r_i\) \((1 \le l_i \le r_i \le 10^9)\).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case output the minimum number of operations needed.
|
In the first test case, we can achieve the goal with a single operation: choose \(v = 2\) and \(c = [1, 2]\), resulting in \(a_1 = 1, a_2 = 2\).In the second test case, we can achieve the goal with two operations: first, choose \(v = 2\) and \(c = [3, 3]\), resulting in \(a_1 = 3, a_2 = 3, a_3 = 0\). Then, choose \(v = 3, c = [2, 7]\), resulting in \(a_1 = 5, a_2 = 3, a_3 = 7\).
|
Input: 4211 52 931 14 52 46 1041 2 16 95 64 52 451 2 3 45 54 43 32 21 1 | Output: 1 2 2 5
|
Medium
| 4 | 900 | 639 | 66 | 16 |
1,081 |
A
|
1081A
|
A. Definite Game
| 800 |
constructive algorithms; math
|
Chouti was doing a competitive programming competition. However, after having all the problems accepted, he got bored and decided to invent some small games.He came up with the following game. The player has a positive integer \(n\). Initially the value of \(n\) equals to \(v\) and the player is able to do the following operation as many times as the player want (possibly zero): choose a positive integer \(x\) that \(x<n\) and \(x\) is not a divisor of \(n\), then subtract \(x\) from \(n\). The goal of the player is to minimize the value of \(n\) in the end.Soon, Chouti found the game trivial. Can you also beat the game?
|
The input contains only one integer in the first line: \(v\) (\(1 \le v \le 10^9\)), the initial value of \(n\).
|
Output a single integer, the minimum value of \(n\) the player can get.
|
In the first example, the player can choose \(x=3\) in the first turn, then \(n\) becomes \(5\). He can then choose \(x=4\) in the second turn to get \(n=1\) as the result. There are other ways to get this minimum. However, for example, he cannot choose \(x=2\) in the first turn because \(2\) is a divisor of \(8\).In the second example, since \(n=1\) initially, the player can do nothing.
|
Input: 8 | Output: 1
|
Beginner
| 2 | 628 | 112 | 71 | 10 |
1,956 |
D
|
1956D
|
D. Nene and the Mex Operator
| 2,000 |
bitmasks; brute force; constructive algorithms; divide and conquer; dp; greedy; implementation; math
|
Nene gave you an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\).You can perform the following operation no more than \(5\cdot 10^5\) times (possibly zero): Choose two integers \(l\) and \(r\) such that \(1 \le l \le r \le n\), compute \(x\) as \(\operatorname{MEX}(\{a_l, a_{l+1}, \ldots, a_r\})\), and simultaneously set \(a_l:=x, a_{l+1}:=x, \ldots, a_r:=x\). Here, \(\operatorname{MEX}\) of a set of integers \(\{c_1, c_2, \ldots, c_k\}\) is defined as the smallest non-negative integer \(m\) which does not occur in the set \(c\).Your goal is to maximize the sum of the elements of the array \(a\). Find the maximum sum and construct a sequence of operations that achieves this sum. Note that you don't need to minimize the number of operations in this sequence, you only should use no more than \(5\cdot 10^5\) operations in your solution.
|
The first line contains an integer \(n\) (\(1 \le n \le 18\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(0\leq a_i \leq 10^7\)) β the array \(a\).
|
In the first line, output two integers \(s\) and \(m\) (\(0\le m\le 5\cdot 10^5\)) β the maximum sum of elements of the array \(a\) and the number of operations in your solution.In the \(i\)-th of the following \(m\) lines, output two integers \(l\) and \(r\) (\(1 \le l \le r \le n\)), representing the parameters of the \(i\)-th operation.It can be shown that the maximum sum of elements of the array \(a\) can always be obtained in no more than \(5 \cdot 10^5\) operations.
|
In the first example, after the operation with \(l=1\) and \(r=2\) the array \(a\) becomes equal to \([2,2]\). It can be shown that it is impossible to achieve a larger sum of the elements of \(a\), so the answer is \(4\).In the second example, the initial sum of elements is \(13\) which can be shown to be the largest.In the third example, the array \(a\) changes as follows: after the first operation (\(l=3\), \(r=3\)), the array \(a\) becomes equal to \([1,100,0,1]\); after the second operation (\(l=3\), \(r=4\)), the array \(a\) becomes equal to \([1,100,2,2]\). It can be shown that it is impossible to achieve a larger sum of the elements of \(a\), so the answer is \(105\).
|
Input: 20 1 | Output: 4 1 1 2
|
Hard
| 8 | 857 | 201 | 476 | 19 |
952 |
E
|
952E
|
E. Cheese Board
| 2,000 |
Not to be confused with chessboard.
|
The first line of input contains a single integer N (1 β€ N β€ 100) β the number of cheeses you have.The next N lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The type is either ""soft"" or ""hard. All cheese names are distinct.
|
Output a single number.
|
Input: 9brie softcamembert softfeta softgoat softmuenster softasiago hardcheddar hardgouda hardswiss hard | Output: 3
|
Hard
| 0 | 35 | 385 | 23 | 9 |
||
1,478 |
C
|
1478C
|
C. Nezzar and Symmetric Array
| 1,700 |
implementation; math; sortings
|
Long time ago there was a symmetric array \(a_1,a_2,\ldots,a_{2n}\) consisting of \(2n\) distinct integers. Array \(a_1,a_2,\ldots,a_{2n}\) is called symmetric if for each integer \(1 \le i \le 2n\), there exists an integer \(1 \le j \le 2n\) such that \(a_i = -a_j\).For each integer \(1 \le i \le 2n\), Nezzar wrote down an integer \(d_i\) equal to the sum of absolute differences from \(a_i\) to all integers in \(a\), i. e. \(d_i = \sum_{j = 1}^{2n} {|a_i - a_j|}\).Now a million years has passed and Nezzar can barely remember the array \(d\) and totally forget \(a\). Nezzar wonders if there exists any symmetric array \(a\) consisting of \(2n\) distinct integers that generates the array \(d\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(2n\) integers \(d_1, d_2, \ldots, d_{2n}\) (\(0 \le d_i \le 10^{12}\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print ""YES"" in a single line if there exists a possible array \(a\). Otherwise, print ""NO"".You can print letters in any case (upper or lower).
|
In the first test case, \(a=[1,-3,-1,3]\) is one possible symmetric array that generates the array \(d=[8,12,8,12]\).In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array \(d\).
|
Input: 6 2 8 12 8 12 2 7 7 9 11 2 7 11 7 11 1 1 1 4 40 56 48 40 80 56 80 48 6 240 154 210 162 174 154 186 240 174 186 162 210 | Output: YES NO NO NO NO YES
|
Medium
| 3 | 701 | 386 | 166 | 14 |
32 |
A
|
32A
|
A. Reconnaissance
| 800 |
brute force
|
According to the regulations of Berland's army, a reconnaissance unit should consist of exactly two soldiers. Since these two soldiers shouldn't differ much, their heights can differ by at most d centimeters. Captain Bob has n soldiers in his detachment. Their heights are a1, a2, ..., an centimeters. Some soldiers are of the same height. Bob wants to know, how many ways exist to form a reconnaissance unit of two soldiers from his detachment.Ways (1, 2) and (2, 1) should be regarded as different.
|
The first line contains two integers n and d (1 β€ n β€ 1000, 1 β€ d β€ 109) β amount of soldiers in Bob's detachment and the maximum allowed height difference respectively. The second line contains n space-separated integers β heights of all the soldiers in Bob's detachment. These numbers don't exceed 109.
|
Output one number β amount of ways to form a reconnaissance unit of two soldiers, whose height difference doesn't exceed d.
|
Input: 5 1010 20 50 60 65 | Output: 6
|
Beginner
| 1 | 500 | 304 | 123 | 0 |
|
1,832 |
D2
|
1832D2
|
D2. Red-Blue Operations (Hard Version)
| 2,400 |
binary search; constructive algorithms; greedy; implementation; math
|
The only difference between easy and hard versions is the maximum values of \(n\) and \(q\).You are given an array, consisting of \(n\) integers. Initially, all elements are red.You can apply the following operation to the array multiple times. During the \(i\)-th operation, you select an element of the array; then: if the element is red, it increases by \(i\) and becomes blue; if the element is blue, it decreases by \(i\) and becomes red. The operations are numbered from \(1\), i. e. during the first operation some element is changed by \(1\) and so on.You are asked \(q\) queries of the following form: given an integer \(k\), what can the largest minimum in the array be if you apply exactly \(k\) operations to it? Note that the operations don't affect the array between queries, all queries are asked on the initial array \(a\).
|
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β the number of elements in the array and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).The third line contains \(q\) integers \(k_1, k_2, \dots, k_q\) (\(1 \le k_j \le 10^9\)).
|
For each query, print a single integer β the largest minimum that the array can have after you apply exactly \(k\) operations to it.
|
Input: 4 10 5 2 8 4 1 2 3 4 5 6 7 8 9 10 | Output: 3 4 5 6 7 8 8 10 8 12
|
Expert
| 5 | 839 | 330 | 132 | 18 |
|
1,633 |
B
|
1633B
|
B. Minority
| 800 |
greedy
|
You are given a string \(s\), consisting only of characters '0' and '1'.You have to choose a contiguous substring of \(s\) and remove all occurrences of the character, which is a strict minority in it, from the substring.That is, if the amount of '0's in the substring is strictly smaller than the amount of '1's, remove all occurrences of '0' from the substring. If the amount of '1's is strictly smaller than the amount of '0's, remove all occurrences of '1'. If the amounts are the same, do nothing.You have to apply the operation exactly once. What is the maximum amount of characters that can be removed?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The only line of each testcase contains a non-empty string \(s\), consisting only of characters '0' and '1'. The length of \(s\) doesn't exceed \(2 \cdot 10^5\).The total length of strings \(s\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print a single integer β the maximum amount of characters that can be removed after applying the operation exactly once.
|
In the first testcase, you can choose substrings ""0"", ""1"" or ""01"". In ""0"" the amount of '0' is \(1\), the amount of '1' is \(0\). '1' is a strict minority, thus all occurrences of it are removed from the substring. However, since there were \(0\) of them, nothing changes. Same for ""1"". And in ""01"" neither of '0' or '1' is a strict minority. Thus, nothing changes. So there is no way to remove any characters.In the second testcase, you can choose substring ""10101010101"". It contains \(5\) characters '0' and \(6\) characters '1'. '0' is a strict minority. Thus, you can remove all its occurrences. There exist other substrings that produce the same answer.In the third testcase, you can choose substring ""011000100"". It contains \(6\) characters '0' and \(3\) characters '1'. '1' is a strict minority. Thus, you can remove all its occurrences.
|
Input: 4011010101010111001100010001 | Output: 0 5 3 0
|
Beginner
| 1 | 609 | 342 | 139 | 16 |
1,856 |
C
|
1856C
|
C. To Become Max
| 1,600 |
binary search; brute force; data structures; dp
|
You are given an array of integers \(a\) of length \(n\).In one operation you: Choose an index \(i\) such that \(1 \le i \le n - 1\) and \(a_i \le a_{i + 1}\). Increase \(a_i\) by \(1\). Find the maximum possible value of \(\max(a_1, a_2, \ldots a_n)\) that you can get after performing this operation at most \(k\) times.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 1000\), \(1 \le k \le 10^{8}\)) β the length of the array \(a\) and the maximum number of operations that can be performed.The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 10^{8}\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1000\).
|
For each test case output a single integer β the maximum possible maximum of the array after performing at most \(k\) operations.
|
In the first test case, one possible optimal sequence of operations is: \([\color{red}{1}, 3, 3] \rightarrow [2, \color{red}{3}, 3] \rightarrow [\color{red}{2}, 4, 3] \rightarrow [\color{red}{3}, 4, 3] \rightarrow [4, 4, 3]\).In the second test case, one possible optimal sequence of operations is: \([1, \color{red}{3}, 4, 5, 1] \rightarrow [1, \color{red}{4}, 4, 5, 1] \rightarrow [1, 5, \color{red}{4}, 5, 1] \rightarrow [1, 5, \color{red}{5}, 5, 1] \rightarrow [1, \color{red}{5}, 6, 5, 1] \rightarrow [1, \color{red}{6}, 6, 5, 1] \rightarrow [1, 7, 6, 5, 1]\).
|
Input: 63 41 3 35 61 3 4 5 14 131 1 3 1795 34 3 2 2 25 66 5 4 1 52 173 5 | Output: 4 7 179 5 7 6
|
Medium
| 4 | 322 | 623 | 129 | 18 |
625 |
B
|
625B
|
B. War of the Corporations
| 1,200 |
constructive algorithms; greedy; strings
|
A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000.This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol's artificial intelligence.Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol's director decided to replace some characters in AI name with ""#"". As this operation is pretty expensive, you should find the minimum number of characters to replace with ""#"", such that the name of AI doesn't contain the name of the phone as a substring.Substring is a continuous subsequence of a string.
|
The first line of the input contains the name of AI designed by Gogol, its length doesn't exceed 100 000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn't exceed 30. Both string are non-empty and consist of only small English letters.
|
Print the minimum number of characters that must be replaced with ""#"" in order to obtain that the name of the phone doesn't occur in the name of AI as a substring.
|
In the first sample AI's name may be replaced with ""int#llect"".In the second sample Gogol can just keep things as they are.In the third sample one of the new possible names of AI may be ""s#ris#ri"".
|
Input: intellecttell | Output: 1
|
Easy
| 3 | 1,137 | 295 | 165 | 6 |
329 |
D
|
329D
|
D. The Evil Temple and the Moving Rocks
| 2,500 |
constructive algorithms
|
Important: All possible tests are in the pretest, so you shouldn't hack on this problem. So, if you passed pretests, you will also pass the system test.You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak monsters, you arrived at a square room consisting of tiles forming an n Γ n grid, surrounded entirely by walls. At the end of the room lies a door locked with evil magical forces. The following inscriptions are written on the door: The sound of clashing rocks will awaken the door! Being a very senior adventurer, you immediately realize what this means. In the room next door lies an infinite number of magical rocks. There are four types of rocks: '^': this rock moves upwards; '<': this rock moves leftwards; '>': this rock moves rightwards; 'v': this rock moves downwards. To open the door, you first need to place the rocks on some of the tiles (one tile can be occupied by at most one rock). Then, you select a single rock that you have placed and activate it. The activated rock will then move in its direction until it hits another rock or hits the walls of the room (the rock will not move if something already blocks it in its chosen direction). The rock then deactivates. If it hits the walls, or if there have been already 107 events of rock becoming activated, the movements end. Otherwise, the rock that was hit becomes activated and this procedure is repeated.If a rock moves at least one cell before hitting either the wall or another rock, the hit produces a sound. The door will open once the number of produced sounds is at least x. It is okay for the rocks to continue moving after producing x sounds.The following picture illustrates the four possible scenarios of moving rocks. Moves at least one cell, then hits another rock. A sound is produced, the hit rock becomes activated. Moves at least one cell, then hits the wall (i.e., the side of the room). A sound is produced, the movements end. Does not move because a rock is already standing in the path. The blocking rock becomes activated, but no sounds are produced. Does not move because the wall is in the way. No sounds are produced and the movements end. Assume there's an infinite number of rocks of each type in the neighboring room. You know what to do: place the rocks and open the door!
|
The first line will consists of two integers n and x, denoting the size of the room and the number of sounds required to open the door. There will be exactly three test cases for this problem: n = 5, x = 5; n = 3, x = 2; n = 100, x = 105. All of these testcases are in pretest.
|
Output n lines. Each line consists of n characters β the j-th character of the i-th line represents the content of the tile at the i-th row and the j-th column, and should be one of these: '^', '<', '>', or 'v': a rock as described in the problem statement. '.': an empty tile. Then, output two integers r and c (1 β€ r, c β€ n) on the next line β this means that the rock you activate first is located at the r-th row from above and c-th column from the left. There must be a rock in this cell.If there are multiple solutions, you may output any of them.
|
Here's a simulation of the first example, accompanied with the number of sounds produced so far. 0 sound 1 sound 2 sounds 3 sounds 4 sounds still 4 sounds In the picture above, the activated rock switches between the '^' rock and the '<' rock. However, no sound is produced since the '^' rock didn't move even a single tile. So, still 4 sound. 5 sounds At this point, 5 sound are already produced, so this solution is already correct. However, for the sake of example, we will continue simulating what happens. 6 sounds 7 sounds still 7 sounds 8 sounds And the movement stops. In total, it produces 8 sounds. Notice that the last move produced sound.Here's a simulation of the second example: 0 sound 1 sound 2 sounds Now, the activated stone will switch continuously from one to another without producing a sound until it reaches the 107 limit, after which the movement will cease. In total, it produced exactly 2 sounds, so the solution is correct.
|
Input: 5 5 | Output: >...vv.<....^..>......^.<1 1
|
Expert
| 1 | 2,322 | 277 | 553 | 3 |
579 |
A
|
579A
|
A. Raising Bacteria
| 1,000 |
bitmasks
|
You are a lover of bacteria. You want to raise some bacteria in a box. Initially, the box is empty. Each morning, you can put any number of bacteria into the box. And each night, every bacterium in the box will split into two bacteria. You hope to see exactly x bacteria in the box at some moment. What is the minimum number of bacteria you need to put into the box across those days?
|
The only line containing one integer x (1 β€ x β€ 109).
|
The only line containing one integer: the answer.
|
For the first sample, we can add one bacterium in the box in the first day morning and at the third morning there will be 4 bacteria in the box. Now we put one more resulting 5 in the box. We added 2 bacteria in the process so the answer is 2.For the second sample, we can put one in the first morning and in the 4-th morning there will be 8 in the box. So the answer is 1.
|
Input: 5 | Output: 2
|
Beginner
| 1 | 384 | 53 | 49 | 5 |
2,034 |
F1
|
2034F1
|
F1. Khayyam's Royal Decree (Easy Version)
| 2,500 |
combinatorics; dp; math; sortings
|
This is the easy version of the problem. The only differences between the two versions are the constraints on \(k\) and the sum of \(k\).In ancient Persia, Khayyam, a clever merchant and mathematician, is playing a game with his prized treasure chest containing \(n\) red rubies worth \(2\) dinars each and \(m\) blue sapphires worth \(1\) dinar each. He also has a satchel, which starts empty, and \(k\) scrolls with pairs \((r_1, b_1), (r_2, b_2), \ldots, (r_k, b_k)\) that describe special conditions. The game proceeds for \(n + m\) turns as follows: Khayyam draws a gem uniformly at random from the chest. He removes the gem from the chest and places it in his satchel. If there exists a scroll \(i\) (\(1 \leq i \leq k\)) such that the chest contains exactly \(r_i\) red rubies and \(b_i\) blue sapphires, Khayyam receives a royal decree that doubles the value of all the gems in his satchel as a reward for achieving a special configuration. Note that the value of some gems might be affected by multiple decrees, and in that case the gems' value is doubled multiple times.Determine the expected value of Khayyam's satchel at the end of the game, modulo \(998,244,353\).Formally, let \(M = 998,244,353\). It can be shown that the exact answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1 \leq n, m \leq 2 \cdot 10^5\), \(0 \leq k \leq 500\)) β the number of red rubies, the number of blue sapphires, and the number of scrolls describing special conditions, respectively.Each of the next \(k\) lines contains two integers \(r_i\), \(b_i\) (\(0 \leq r_i \leq n\), \(0 \leq b_i \leq m\), \(1 \leq r_i + b_i \leq n+m-1\)). It is guaranteed that the pairs \((r_i, b_i)\) are distinct.It is guaranteed that the sum of \(n\) and the sum of \(m\) over all test cases do not exceed \(2 \cdot 10^5\), and the sum of \(k\) over all test cases does not exceed \(500\).
|
For each test case, print a single integer: the expected value of Khayyam's satchel at the end of the process, modulo \(998,244,353\).
|
In the first test case, at the end of the process, there will always be \(3\) red rubies and \(4\) blue sapphires. None of the special conditions described in the scrolls are met, so the value of Khayyam's satchel remains unchanged. The total value of the satchel at the end is always \(2 \cdot 3 + 1 \cdot 4 = 10\).In the second test case, consider the following two cases: With probability \(1/2\), Khayyam draws a red ruby, and the value of his satchel becomes \(2\). Then with probability \(1\), he draws a blue sapphire, and the value of his satchel becomes \(3\). With probability \(1/2\), Khayyam draws a blue sapphire, and the value of his satchel becomes \(1\). At this point, the chest contains \(r_1 = 1\) red rubies and \(b_1 = 0\) blue sapphires, which match the special condition described in a scroll. As a result, the value of the satchel is doubled to \(2 \cdot 1 = 2\). Then with probability \(1\), he draws a red ruby, and the value of his satchel becomes \(4\). Thus, the expected value at the end is \(\frac{1}{2} \cdot 3 + \frac{1}{2} \cdot 4 = \frac{7}{2}\), which is \(499,122,180\) modulo \(998,244,353\).
|
Input: 53 4 01 1 11 03 3 21 12 23 3 22 11 210 4 51 08 06 40 27 4 | Output: 10 499122180 798595498 149736666 414854846
|
Expert
| 4 | 1,538 | 815 | 134 | 20 |
1,583 |
G
|
1583G
|
G. Omkar and Time Travel
| 3,000 |
data structures; math
|
El Psy Kongroo.Omkar is watching Steins;Gate.In Steins;Gate, Okabe Rintarou needs to complete \(n\) tasks (\(1 \leq n \leq 2 \cdot 10^5\)). Unfortunately, he doesn't know when he needs to complete the tasks.Initially, the time is \(0\). Time travel will now happen according to the following rules:For each \(k = 1, 2, \ldots, n\), Okabe will realize at time \(b_k\) that he was supposed to complete the \(k\)-th task at time \(a_k\) (\(a_k < b_k\)). When he realizes this, if \(k\)-th task was already completed at time \(a_k\), Okabe keeps the usual flow of time. Otherwise, he time travels to time \(a_k\) then immediately completes the task.If Okabe time travels to time \(a_k\), all tasks completed after this time will become incomplete again. That is, for every \(j\), if \(a_j>a_k\), the \(j\)-th task will become incomplete, if it was complete (if it was incomplete, nothing will change).Okabe has bad memory, so he can time travel to time \(a_k\) only immediately after getting to time \(b_k\) and learning that he was supposed to complete the \(k\)-th task at time \(a_k\). That is, even if Okabe already had to perform \(k\)-th task before, he wouldn't remember it before stumbling on the info about this task at time \(b_k\) again.Please refer to the notes for an example of time travelling.There is a certain set \(s\) of tasks such that the first moment that all of the tasks in \(s\) are simultaneously completed (regardless of whether any other tasks are currently completed), a funny scene will take place. Omkar loves this scene and wants to know how many times Okabe will time travel before this scene takes place. Find this number modulo \(10^9 + 7\). It can be proven that eventually all \(n\) tasks will be completed and so the answer always exists.
|
The first line contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of tasks that Okabe needs to complete.\(n\) lines follow. The \(k\)-th of these lines contain two integers \(a_k\) and \(b_k\) (\(1 \leq a_k < b_k \leq 2n\)) β the time at which Okabe needs to complete the \(k\)-th task and the time that he realizes this respectively. All \(2n\) of these times are distinct (so every time from \(1\) to \(2n\) inclusive appears exactly once in the input).The next line contains a single integer \(t\) (\(1 \leq t \leq n\)) β the size of the set \(s\) of tasks that lead to the funny scene.The last line contains \(t\) integers \(s_1, s_2, \ldots, s_t\) β (\(1 \leq s_k \leq n\), the numbers \(s_1, s_2, \ldots, s_t\) are distinct) β the set \(s\) of tasks.
|
Output a single integer β the number of times that Okabe time travels until all tasks in the set \(s\) are simultaneously completed, modulo \(10^9 + 7\).
|
For the first sample, all tasks need to be completed in order for the funny scene to occur.Initially, the time is \(0\). Nothing happens until time \(3\), when Okabe realizes that he should have done the \(2\)-nd task at time \(2\). He then time travels to time \(2\) and completes the task.As the task is done now, he does not time travel again when the time is again \(3\). However, at time \(4\), he travels to time \(1\) to complete the \(1\)-st task.This undoes the \(2\)-nd task. This means that the \(2\)-nd task is not currently completed, meaning that the funny scene will not occur at this point even though the \(1\)-st task is currently completed and Okabe had previously completed the \(2\)-nd task.Once it is again time \(3\) he travels back to time \(2\) once more and does the \(2\)-nd task again.Now all tasks are complete, with Okabe having time travelled \(3\) times.The second sample has the same tasks for Okabe to complete. However, this time the funny scene only needs the first task to be completed in order to occur. From reading the above sample you can see that this occurs once Okabe has time travelled \(2\) times.
|
Input: 2 1 4 2 3 2 1 2 | Output: 3
|
Master
| 2 | 1,772 | 777 | 153 | 15 |
1,528 |
D
|
1528D
|
D. It's a bird! No, it's a plane! No, it's AaParsa!
| 2,500 |
constructive algorithms; graphs; shortest paths
|
There are \(n\) cities in Shaazzzland, numbered from \(0\) to \(n-1\). Ghaazzzland, the immortal enemy of Shaazzzland, is ruled by AaParsa.As the head of the Ghaazzzland's intelligence agency, AaParsa is carrying out the most important spying mission in Ghaazzzland's history on Shaazzzland.AaParsa has planted \(m\) transport cannons in the cities of Shaazzzland. The \(i\)-th cannon is planted in the city \(a_i\) and is initially pointing at city \(b_i\).It is guaranteed that each of the \(n\) cities has at least one transport cannon planted inside it, and that no two cannons from the same city are initially pointing at the same city (that is, all pairs \((a_i, b_i)\) are distinct).AaParsa used very advanced technology to build the cannons, the cannons rotate every second. In other words, if the \(i\)-th cannon is pointing towards the city \(x\) at some second, it will target the city \((x + 1) \mod n\) at the next second.As their name suggests, transport cannons are for transportation, specifically for human transport. If you use the \(i\)-th cannon to launch yourself towards the city that it's currently pointing at, you'll be airborne for \(c_i\) seconds before reaching your target destination.If you still don't get it, using the \(i\)-th cannon at the \(s\)-th second (using which is only possible if you are currently in the city \(a_i\)) will shoot you to the city \((b_i + s) \mod n\) and you'll land in there after \(c_i\) seconds (so you'll be there in the \((s + c_i)\)-th second). Also note the cannon that you initially launched from will rotate every second but you obviously won't change direction while you are airborne. AaParsa wants to use the cannons for travelling between Shaazzzland's cities in his grand plan, and he can start travelling at second \(0\). For him to fully utilize them, he needs to know the minimum number of seconds required to reach city \(u\) from city \(v\) using the cannons for every pair of cities \((u, v)\).Note that AaParsa can stay in a city for as long as he wants.
|
The first line contains two integers \(n\) and \(m\) \((2 \le n \le 600 , n \le m \le n^2)\) β the number of cities and cannons correspondingly.The \(i\)-th line of the following \(m\) lines contains three integers \(a_i\), \(b_i\) and \(c_i\) \(( 0 \le a_i , b_i \le n-1 , 1 \le c_i \le 10^9)\), denoting the cannon in the city \(a_i\), which is initially pointing to \(b_i\) and travelling by which takes \(c_i\) seconds.It is guaranteed that each of the \(n\) cities has at least one transport cannon planted inside it, and that no two cannons from the same city are initially pointing at the same city (that is, all pairs \((a_i, b_i)\) are distinct).
|
Print \(n\) lines, each line should contain \(n\) integers.The \(j\)-th integer in the \(i\)-th line should be equal to the minimum time required to reach city \(j\) from city \(i\).
|
In the first example one possible path for going from \(0\) to \(2\) would be: Stay inside \(0\) and do nothing for \(1\) second. Use the first cannon and land at \(2\) after \(1\) second. Note that: we could have used the second cannon in \(0\)-th second but it would have taken us \(3\) seconds to reach city \(2\) in that case.
|
Input: 3 4 0 1 1 0 2 3 1 0 1 2 0 1 | Output: 0 1 2 1 0 2 1 2 0
|
Expert
| 3 | 2,033 | 655 | 182 | 15 |
510 |
C
|
510C
|
C. Fox And Names
| 1,600 |
dfs and similar; graphs; sortings
|
Fox Ciel is going to publish a paper on FOCS (Foxes Operated Computer Systems, pronounce: ""Fox""). She heard a rumor: the authors list on the paper is always sorted in the lexicographical order. After checking some examples, she found out that sometimes it wasn't true. On some papers authors' names weren't sorted in lexicographical order in normal sense. But it was always true that after some modification of the order of letters in alphabet, the order of authors becomes lexicographical!She wants to know, if there exists an order of letters in Latin alphabet such that the names on the paper she is submitting are following in the lexicographical order. If so, you should find out any such order.Lexicographical order is defined in following way. When we compare s and t, first we find the leftmost position with differing characters: si β ti. If there is no such position (i. e. s is a prefix of t or vice versa) the shortest string is less. Otherwise, we compare characters si and ti according to their order in alphabet.
|
The first line contains an integer n (1 β€ n β€ 100): number of names.Each of the following n lines contain one string namei (1 β€ |namei| β€ 100), the i-th name. Each name contains only lowercase Latin letters. All names are different.
|
If there exists such order of letters that the given names are sorted lexicographically, output any such order as a permutation of characters 'a'β'z' (i. e. first output the first letter of the modified alphabet, then the second, and so on).Otherwise output a single word ""Impossible"" (without quotes).
|
Input: 3rivestshamiradleman | Output: bcdefghijklmnopqrsatuvwxyz
|
Medium
| 3 | 1,029 | 232 | 304 | 5 |
|
383 |
A
|
383A
|
A. Milking cows
| 1,600 |
data structures; greedy
|
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk).Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost.
|
The first line contains an integer n (1 β€ n β€ 200000). The second line contains n integers a1, a2, ..., an, where ai is 0 if the cow number i is facing left, and 1 if it is facing right.
|
Print a single integer, the minimum amount of lost milk.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.
|
In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
|
Input: 40 0 1 0 | Output: 1
|
Medium
| 2 | 943 | 186 | 205 | 3 |
1,139 |
D
|
1139D
|
D. Steps to One
| 2,300 |
dp; math; number theory; probabilities
|
Vivek initially has an empty array \(a\) and some integer constant \(m\).He performs the following algorithm: Select a random integer \(x\) uniformly in range from \(1\) to \(m\) and append it to the end of \(a\). Compute the greatest common divisor of integers in \(a\). In case it equals to \(1\), break Otherwise, return to step \(1\). Find the expected length of \(a\). It can be shown that it can be represented as \(\frac{P}{Q}\) where \(P\) and \(Q\) are coprime integers and \(Q\neq 0 \pmod{10^9+7}\). Print the value of \(P \cdot Q^{-1} \pmod{10^9+7}\).
|
The first and only line contains a single integer \(m\) (\(1 \leq m \leq 100000\)).
|
Print a single integer β the expected length of the array \(a\) written as \(P \cdot Q^{-1} \pmod{10^9+7}\).
|
In the first example, since Vivek can choose only integers from \(1\) to \(1\), he will have \(a=[1]\) after the first append operation, and after that quit the algorithm. Hence the length of \(a\) is always \(1\), so its expected value is \(1\) as well.In the second example, Vivek each time will append either \(1\) or \(2\), so after finishing the algorithm he will end up having some number of \(2\)'s (possibly zero), and a single \(1\) in the end. The expected length of the list is \(1\cdot \frac{1}{2} + 2\cdot \frac{1}{2^2} + 3\cdot \frac{1}{2^3} + \ldots = 2\).
|
Input: 1 | Output: 1
|
Expert
| 4 | 562 | 83 | 108 | 11 |
58 |
C
|
58C
|
C. Trees
| 1,800 |
brute force
|
On Bertown's main street n trees are growing, the tree number i has the height of ai meters (1 β€ i β€ n). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the n-th one) should be equal to each other, the heights of the 2-nd and the (n - 1)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences ""2 3 4 5 5 4 3 2"" and ""1 2 3 2 1"" are beautiful, and '1 3 3 1"" and ""1 2 3 1"" are not. Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can't be less than or equal to zero. Identify the smallest number of changes of the trees' height needed for the sequence of their heights to become beautiful.
|
The first line contains integer n (1 β€ n β€ 105) which is the number of trees. The second line contains integers ai (1 β€ ai β€ 105) which are the heights of the trees.
|
Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful.
|
Input: 32 2 2 | Output: 1
|
Medium
| 1 | 1,365 | 165 | 134 | 0 |
|
1,450 |
C1
|
1450C1
|
C1. Errich-Tac-Toe (Easy Version)
| 2,100 |
constructive algorithms; math
|
The only difference between the easy and hard versions is that tokens of type O do not appear in the input of the easy version.Errichto gave Monogon the following challenge in order to intimidate him from taking his top contributor spot on Codeforces.In a Tic-Tac-Toe grid, there are \(n\) rows and \(n\) columns. Each cell of the grid is either empty or contains a token. There are two types of tokens: X and O. If there exist three tokens of the same type consecutive in a row or column, it is a winning configuration. Otherwise, it is a draw configuration. The patterns in the first row are winning configurations. The patterns in the second row are draw configurations. In an operation, you can change an X to an O, or an O to an X. Let \(k\) denote the total number of tokens in the grid. Your task is to make the grid a draw in at most \(\lfloor \frac{k}{3}\rfloor\) (rounding down) operations.You are not required to minimize the number of operations.
|
The first line contains a single integer \(t\) (\(1\le t\le 100\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1\le n\le 300\)) β the size of the grid.The following \(n\) lines each contain a string of \(n\) characters, denoting the initial grid. The character in the \(i\)-th row and \(j\)-th column is '.' if the cell is empty, or it is the type of token in the cell: 'X' or 'O'.It is guaranteed that not all cells are empty.In the easy version, the character 'O' does not appear in the input.The sum of \(n\) across all test cases does not exceed \(300\).
|
For each test case, print the state of the grid after applying the operations.We have proof that a solution always exists. If there are multiple solutions, print any.
|
In the first test case, there are initially three 'X' consecutive in the second row and the second column. By changing the middle token to 'O' we make the grid a draw, and we only changed \(1\le \lfloor 5/3\rfloor\) token.In the second test case, we change only \(9\le \lfloor 32/3\rfloor\) tokens, and there does not exist any three 'X' or 'O' consecutive in a row or column, so it is a draw.In the third test case, we change only \(3\le \lfloor 12/3\rfloor\) tokens, and the resulting grid is a draw.
|
Input: 3 3 .X. XXX .X. 6 XX.XXX XXXXXX XXX.XX XXXXXX XX.X.X XXXXXX 5 XXX.X .X..X XXX.X ..X.. ..X.. | Output: .X. XOX .X. XX.XXO XOXXOX OXX.XX XOOXXO XX.X.X OXXOXX XOX.X .X..X XXO.O ..X.. ..X..
|
Hard
| 2 | 958 | 609 | 166 | 14 |
920 |
G
|
920G
|
G. List Of Integers
| 2,200 |
binary search; bitmasks; brute force; combinatorics; math; number theory
|
Let's denote as L(x, p) an infinite sequence of integers y such that gcd(p, y) = 1 and y > x (where gcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L(x, p) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of L(7, 22), respectively.You have to process t queries. Each query is denoted by three integers x, p and k, and the answer to this query is k-th element of L(x, p).
|
The first line contains one integer t (1 β€ t β€ 30000) β the number of queries to process.Then t lines follow. i-th line contains three integers x, p and k for i-th query (1 β€ x, p, k β€ 106).
|
Print t integers, where i-th integer is the answer to i-th query.
|
Input: 37 22 17 22 27 22 3 | Output: 91315
|
Hard
| 6 | 466 | 190 | 65 | 9 |
|
2,011 |
I
|
2011I
|
I. Stack and Queue
| 0 |
*special; data structures; divide and conquer
|
There are \(2\) queues of patients at the doors of two doctors. The first doctor sees patients in the usual order of the queue β whoever arrived first will be seen first. The second doctor does the opposite β he sees those who arrived last first. Thus, there is a queue for the first doctor and a stack for the second doctor. A patient can be in both the queue and the stack. Each patient is characterized by the time their visit to the doctor will take (the time is the same for both doctors).When the appointments begin, the doctors will see patients in the order of the queue and stack, respectively. As soon as a doctor finishes with one patient, he will call the next one.But there is one problem: if a patient is in both the queue and the stack, and he is called to one doctor first and then to the other, while he has not yet finished with the first one, confusion will arise. It is allowed for a patient to go to the second doctor at the exact moment he finishes with the first doctor.The current configuration of the queue and stack is called good if the doctors can see all the patients without any confusion arising.Initially, both the queue and the stack are empty. There are three types of queries: add patient \(x\) to the queue; add patient \(x\) to the stack; patient \(x\), who was in the queue, realizes he is in the wrong place and moves to the stack; however, he moves to the position in the stack as if he had entered the stack at the moment of the query when he entered the queue. It is guaranteed that after each query, each patient is no more than once in the queue and no more than once in the stack.After each query, you need to determine if the current configuration is good.
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of requests.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β for each patient, the time their visit to the doctor will take.Each of the following \(n\) lines contains two integers \(t\) and \(x\) (\(1 \le t \le 3\); \(1 \le x \le n\)) β the type of query and the patient's index. It is guaranteed that: if the query is of type \(1\), then patient \(x\) is not in the queue yet; if the query is of type \(2\), then patient \(x\) is not in the stack yet; if the query is of type \(3\), then patient \(x\) is in the queue already and not in the stack yet.
|
After each query, print ""YES"", if the current configuration is good, and ""NO"" otherwise.
|
In the first test, there are the following configurations: queue: \([1]\); stack: \([]\); patient \(1\) is seen by the first doctor for \(10\) minutes, starting at minute \(0\). queue: \([1]\); stack: \([1]\); patient \(1\) is seen by the first doctor during \([0; 10)\) and by the second during \([0; 10)\). Since patient \(1\) must be at both doctors at the same time, the answer is ""NO"". queue: \([1]\); stack: \([1, 2]\); patient \(1\) is seen by the first doctor during \([0; 10)\), and by the second during \([15; 25)\); patient \(2\) is seen by the second doctor during \([0, 15)\). Now patient \(1\) can make it to the second doctor after seeing the first. In the second test, the configuration after query \(4\) is as follows: queue: \([1, 2]\); stack: \([5, 3]\); patient \(1\): first doctor \([0, 2)\), second doctor is not seeing him; patient \(2\): first doctor \([2, 7)\), second doctor is not seeing him; patient \(3\): first doctor is not seeing him, second doctor \([0, 1)\); patient \(5\): first doctor is not seeing him, second doctor \([1, 6)\). After request \(5\), the next configuration is: queue: \([1]\); stack: \([5, 2, 3]\); patient \(1\): first doctor \([0, 2)\), second doctor is not seeing him; patient \(2\): first doctor is not seeing him, second doctor \([1, 6)\); patient \(3\): first doctor is not seeing him, second doctor \([0, 1)\); patient \(5\): first doctor is not seeing him, second doctor \([6, 11)\). After request \(6\), the next configuration is: queue: \([1, 2]\); stack: \([5, 2, 3]\); patient \(1\): first doctor \([0, 2)\), second doctor is not seeing him; patient \(2\): first doctor \([2, 7)\), second doctor \([1, 6)\); patient \(3\): first doctor is not seeing him, second doctor \([0, 1)\); patient \(5\): first doctor is not seeing him, second doctor \([6, 11)\). Patient \(2\) must be at both doctors at the same time.After request \(7\), the next configuration is: queue: \([2]\); stack: \([1, 5, 2, 3]\); patient \(1\): first doctor is not seeing him, second doctor \([11, 13)\); patient \(2\): first doctor \([0, 5)\), second doctor \([1, 6)\); patient \(3\): first doctor is not seeing him, second doctor \([0, 1)\); patient \(5\): first doctor is not seeing him, second doctor \([6, 11)\). Patient \(2\) must be at both doctors at the same time.
|
Input: 310 15 41 12 12 2 | Output: YES NO YES
|
Beginner
| 3 | 1,702 | 678 | 92 | 20 |
604 |
A
|
604A
|
A. Uncowed Forces
| 1,000 |
implementation
|
Kevin Sun has just finished competing in Codeforces Round #334! The round was 120 minutes long and featured five problems with maximum point values of 500, 1000, 1500, 2000, and 2500, respectively. Despite the challenging tasks, Kevin was uncowed and bulldozed through all of them, distinguishing himself from the herd as the best cowmputer scientist in all of Bovinia. Kevin knows his submission time for each problem, the number of wrong submissions that he made on each problem, and his total numbers of successful and unsuccessful hacks. Because Codeforces scoring is complicated, Kevin wants you to write a program to compute his final score.Codeforces scores are computed as follows: If the maximum point value of a problem is x, and Kevin submitted correctly at minute m but made w wrong submissions, then his score on that problem is . His total score is equal to the sum of his scores for each problem. In addition, Kevin's total score gets increased by 100 points for each successful hack, but gets decreased by 50 points for each unsuccessful hack.All arithmetic operations are performed with absolute precision and no rounding. It is guaranteed that Kevin's final score is an integer.
|
The first line of the input contains five space-separated integers m1, m2, m3, m4, m5, where mi (0 β€ mi β€ 119) is the time of Kevin's last submission for problem i. His last submission is always correct and gets accepted.The second line contains five space-separated integers w1, w2, w3, w4, w5, where wi (0 β€ wi β€ 10) is Kevin's number of wrong submissions on problem i.The last line contains two space-separated integers hs and hu (0 β€ hs, hu β€ 20), denoting the Kevin's numbers of successful and unsuccessful hacks, respectively.
|
Print a single integer, the value of Kevin's final score.
|
In the second sample, Kevin takes 119 minutes on all of the problems. Therefore, he gets of the points on each problem. So his score from solving problems is . Adding in 10Β·100 = 1000 points from hacks, his total score becomes 3930 + 1000 = 4930.
|
Input: 20 40 60 80 1000 1 2 3 41 0 | Output: 4900
|
Beginner
| 1 | 1,196 | 532 | 57 | 6 |
567 |
D
|
567D
|
D. One-Dimensional Battle Ships
| 1,700 |
binary search; data structures; greedy; sortings
|
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of n square cells (that is, on a 1 Γ n table).At the beginning of the game Alice puts k ships on the field without telling their positions to Bob. Each ship looks as a 1 Γ a rectangle (that is, it occupies a sequence of a consecutive squares of the field). The ships cannot intersect and even touch each other.After that Bob makes a sequence of ""shots"". He names cells of the field and Alice either says that the cell is empty (""miss""), or that the cell belongs to some ship (""hit"").But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a ""miss"". Help Bob catch Alice cheating β find Bob's first move, such that after it you can be sure that Alice cheated.
|
The first line of the input contains three integers: n, k and a (1 β€ n, k, a β€ 2Β·105) β the size of the field, the number of the ships and the size of each ship. It is guaranteed that the n, k and a are such that you can put k ships of size a on the field, so that no two ships intersect or touch each other.The second line contains integer m (1 β€ m β€ n) β the number of Bob's moves.The third line contains m distinct integers x1, x2, ..., xm, where xi is the number of the cell where Bob made the i-th shot. The cells are numbered from left to right from 1 to n.
|
Print a single integer β the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to m in the order the were made. If the sought move doesn't exist, then print ""-1"".
|
Input: 11 3 354 8 6 1 11 | Output: 3
|
Medium
| 4 | 824 | 563 | 224 | 5 |
|
86 |
A
|
86A
|
A. Reflection
| 1,600 |
math
|
For each positive integer n consider the integer Ο(n) which is obtained from n by replacing every digit a in the decimal notation of n with the digit (9 - a). We say that Ο(n) is the reflection of n. For example, reflection of 192 equals 807. Note that leading zeros (if any) should be omitted. So reflection of 9 equals 0, reflection of 91 equals 8.Let us call the weight of the number the product of the number and its reflection. Thus, the weight of the number 10 is equal to 10Β·89 = 890.Your task is to find the maximum weight of the numbers in the given range [l, r] (boundaries are included).
|
Input contains two space-separated integers l and r (1 β€ l β€ r β€ 109) β bounds of the range.
|
Output should contain single integer number: maximum value of the product nΒ·Ο(n), where l β€ n β€ r.Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).
|
In the third sample weight of 8 equals 8Β·1 = 8, weight of 9 equals 9Β·0 = 0, weight of 10 equals 890.Thus, maximum value of the product is equal to 890.
|
Input: 3 7 | Output: 20
|
Medium
| 1 | 598 | 92 | 229 | 0 |
891 |
B
|
891B
|
B. Gluttony
| 2,000 |
constructive algorithms; greedy
|
You are given an array a with n distinct integers. Construct an array b by permuting a such that for every non-empty subset of indices S = {x1, x2, ..., xk} (1 β€ xi β€ n, 0 < k < n) the sums of elements on that positions in a and b are different, i. e.
|
The first line contains one integer n (1 β€ n β€ 22) β the size of the array.The second line contains n space-separated distinct integers a1, a2, ..., an (0 β€ ai β€ 109) β the elements of the array.
|
If there is no such array b, print -1.Otherwise in the only line print n space-separated integers b1, b2, ..., bn. Note that b must be a permutation of a.If there are multiple answers, print any of them.
|
An array x is a permutation of y, if we can shuffle elements of y such that it will coincide with x.Note that the empty subset and the subset containing all indices are not counted.
|
Input: 21 2 | Output: 2 1
|
Hard
| 2 | 251 | 195 | 203 | 8 |
11 |
A
|
11A
|
A. Increasing Sequence
| 900 |
constructive algorithms; implementation; math
|
A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t.You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. What is the least number of moves required to make the given sequence increasing?
|
The first line of the input contains two integer numbers n and d (2 β€ n β€ 2000, 1 β€ d β€ 106). The second line contains space separated sequence b0, b1, ..., bn - 1 (1 β€ bi β€ 106).
|
Output the minimal number of moves needed to make the sequence increasing.
|
Input: 4 21 3 3 2 | Output: 3
|
Beginner
| 3 | 320 | 179 | 74 | 0 |
|
1,065 |
G
|
1065G
|
G. Fibonacci Suffix
| 2,700 |
strings
|
Let's denote (yet again) the sequence of Fibonacci strings:\(F(0) = \) 0, \(F(1) = \) 1, \(F(i) = F(i - 2) + F(i - 1)\), where the plus sign denotes the concatenation of two strings.Let's denote the lexicographically sorted sequence of suffixes of string \(F(i)\) as \(A(F(i))\). For example, \(F(4)\) is 01101, and \(A(F(4))\) is the following sequence: 01, 01101, 1, 101, 1101. Elements in this sequence are numbered from \(1\).Your task is to print \(m\) first characters of \(k\)-th element of \(A(F(n))\). If there are less than \(m\) characters in this suffix, then output the whole suffix.
|
The only line of the input contains three numbers \(n\), \(k\) and \(m\) (\(1 \le n, m \le 200\), \(1 \le k \le 10^{18}\)) denoting the index of the Fibonacci string you have to consider, the index of the element of \(A(F(n))\) and the number of characters you have to output, respectively.It is guaranteed that \(k\) does not exceed the length of \(F(n)\).
|
Output \(m\) first characters of \(k\)-th element of \(A(F(n))\), or the whole element if its length is less than \(m\).
|
Input: 4 5 3 | Output: 110
|
Master
| 1 | 596 | 357 | 120 | 10 |
|
394 |
D
|
394D
|
D. Physical Education and Buns
| 0 |
brute force; implementation; math
|
The Physical education teacher at SESC is a sort of mathematician too. His most favorite topic in mathematics is progressions. That is why the teacher wants the students lined up in non-decreasing height form an arithmetic progression.To achieve the goal, the gym teacher ordered a lot of magical buns from the dining room. The magic buns come in two types: when a student eats one magic bun of the first type, his height increases by one, when the student eats one magical bun of the second type, his height decreases by one. The physical education teacher, as expected, cares about the health of his students, so he does not want them to eat a lot of buns. More precisely, he wants the maximum number of buns eaten by some student to be minimum.Help the teacher, get the maximum number of buns that some pupils will have to eat to achieve the goal of the teacher. Also, get one of the possible ways for achieving the objective, namely, the height of the lowest student in the end and the step of the resulting progression.
|
The single line contains integer n (2 β€ n β€ 103) β the number of students. The second line contains n space-separated integers β the heights of all students. The height of one student is an integer which absolute value doesn't exceed 104.
|
In the first line print the maximum number of buns eaten by some student to achieve the teacher's aim. In the second line, print two space-separated integers β the height of the lowest student in the end and the step of the progression. Please, pay attention that the step should be non-negative.If there are multiple possible answers, you can print any of them.
|
Lets look at the first sample. We can proceed in the following manner: don't feed the 1-st student, his height will stay equal to -3; give two buns of the first type to the 2-nd student, his height become equal to -2; give two buns of the first type to the 3-rd student, his height become equal to 0; give two buns of the first type to the 4-th student, his height become equal to -1; give two buns of the second type to the 5-th student, his height become equal to 1. To sum it up, when the students line up in non-decreasing height it will be an arithmetic progression: -3, -2, -1, 0, 1. The height of the lowest student is equal to -3, the step of the progression is equal to 1. The maximum number of buns eaten by one student is equal to 2.
|
Input: 5-3 -4 -2 -3 3 | Output: 2-3 1
|
Beginner
| 3 | 1,024 | 238 | 362 | 3 |
335 |
C
|
335C
|
C. More Reclamation
| 2,100 |
games
|
In a far away land, there are two cities near a river. One day, the cities decide that they have too little space and would like to reclaim some of the river area into land.The river area can be represented by a grid with r rows and exactly two columns β each cell represents a rectangular area. The rows are numbered 1 through r from top to bottom, while the columns are numbered 1 and 2.Initially, all of the cells are occupied by the river. The plan is to turn some of those cells into land one by one, with the cities alternately choosing a cell to reclaim, and continuing until no more cells can be reclaimed.However, the river is also used as a major trade route. The cities need to make sure that ships will still be able to sail from one end of the river to the other. More formally, if a cell (r, c) has been reclaimed, it is not allowed to reclaim any of the cells (r - 1, 3 - c), (r, 3 - c), or (r + 1, 3 - c).The cities are not on friendly terms, and each city wants to be the last city to reclaim a cell (they don't care about how many cells they reclaim, just who reclaims a cell last). The cities have already reclaimed n cells. Your job is to determine which city will be the last to reclaim a cell, assuming both choose cells optimally from current moment.
|
The first line consists of two integers r and n (1 β€ r β€ 100, 0 β€ n β€ r). Then n lines follow, describing the cells that were already reclaimed. Each line consists of two integers: ri and ci (1 β€ ri β€ r, 1 β€ ci β€ 2), which represent the cell located at row ri and column ci. All of the lines describing the cells will be distinct, and the reclaimed cells will not violate the constraints above.
|
Output ""WIN"" if the city whose turn it is to choose a cell can guarantee that they will be the last to choose a cell. Otherwise print ""LOSE"".
|
In the first example, there are 3 possible cells for the first city to reclaim: (2, 1), (3, 1), or (3, 2). The first two possibilities both lose, as they leave exactly one cell for the other city. However, reclaiming the cell at (3, 2) leaves no more cells that can be reclaimed, and therefore the first city wins. In the third example, there are no cells that can be reclaimed.
|
Input: 3 11 1 | Output: WIN
|
Hard
| 1 | 1,273 | 394 | 145 | 3 |
135 |
C
|
135C
|
C. Zero-One
| 1,900 |
constructive algorithms; games; greedy
|
Little Petya very much likes playing with little Masha. Recently he has received a game called ""Zero-One"" as a gift from his mother. Petya immediately offered Masha to play the game with him.Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101. The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it.An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement.
|
The first line contains a sequence of characters each of which can either be a ""0"", a ""1"" or a ""?"". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters ""?"" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive.
|
Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample).
|
In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes.In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end.
|
Input: ???? | Output: 00011011
|
Hard
| 3 | 1,589 | 334 | 333 | 1 |
852 |
E
|
852E
|
E. Casinos and travel
| 2,100 |
dp
|
John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N.John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules.To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey.He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey.In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 109 + 7.
|
In the first line, a positive integer N (1 β€ N β€ 100000), the number of cities. In the next N - 1 lines, two numbers a, b (1 β€ a, b β€ N) separated by a single space meaning that cities a and b are connected by a bidirectional road.
|
Output one number, the answer to the problem modulo 109 + 7.
|
Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4.Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options.
|
Input: 21 2 | Output: 4
|
Hard
| 1 | 1,329 | 231 | 60 | 8 |
622 |
F
|
622F
|
F. The Sum of the k-th Powers
| 2,600 |
math
|
There are well-known formulas: , , . Also mathematicians found similar formulas for higher degrees.Find the value of the sum modulo 109 + 7 (so you should find the remainder after dividing the answer by the value 109 + 7).
|
The only line contains two integers n, k (1 β€ n β€ 109, 0 β€ k β€ 106).
|
Print the only integer a β the remainder after dividing the value of the sum by the value 109 + 7.
|
Input: 4 1 | Output: 10
|
Expert
| 1 | 222 | 68 | 98 | 6 |
|
478 |
C
|
478C
|
C. Table Decorations
| 1,800 |
greedy
|
You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?Your task is to write a program that for given values r, g and b will find the maximum number t of tables, that can be decorated in the required manner.
|
The single line contains three integers r, g and b (0 β€ r, g, b β€ 2Β·109) β the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.
|
Print a single integer t β the maximum number of tables that can be decorated in the required manner.
|
In the first sample you can decorate the tables with the following balloon sets: ""rgg"", ""gbb"", ""brr"", ""rrg"", where ""r"", ""g"" and ""b"" represent the red, green and blue balls, respectively.
|
Input: 5 4 3 | Output: 4
|
Medium
| 1 | 435 | 178 | 101 | 4 |
1,761 |
D
|
1761D
|
D. Carry Bit
| 2,100 |
combinatorics; math
|
Let \(f(x,y)\) be the number of carries of \(x+y\) in binary (i. e. \(f(x,y)=g(x)+g(y)-g(x+y)\), where \(g(x)\) is the number of ones in the binary representation of \(x\)).Given two integers \(n\) and \(k\), find the number of ordered pairs \((a,b)\) such that \(0 \leq a,b < 2^n\), and \(f(a,b)\) equals \(k\). Note that for \(a\ne b\), \((a,b)\) and \((b,a)\) are considered as two different pairs. As this number may be large, output it modulo \(10^9+7\).
|
The only line of each test contains two integers \(n\) and \(k\) (\(0\leq k<n\leq 10^6\)).
|
Output a single integer β the answer modulo \(10^9+7\).
|
Here are some examples for understanding carries:$$$\( \begin{aligned} &\begin{array}{r} 1_{\ \ }1_{\ \ }1\\ +\ _{1}1_{\ \ }0_{\ \ }0\\ \hline \ 1_{\ \ }0_{\ \ }1_{\ \ }1 \end{array} &\begin{array}{r} \ 1_{\ \ }0_{\ \ }1\\ +\ _{\ \ }0_{\ \ }0_{1}1\\ \hline \ 0_{\ \ }1_{\ \ }1_{\ \ }0 \end{array} & &\begin{array}{r} \ 1_{\ \ }0_{\ \ }1\\ +\ _{1}0_{1}1_{1}1\\ \hline \ 1_{\ \ }0_{\ \ }0_{\ \ }0 \end{array} \end{aligned} \)\(So \)f(7,4)=1\(, \)f(5,1)=1\( and \)f(5,3)=3\(.In the first test case, all the pairs meeting the constraints are \)(1,1),(1,5),(2,2),(2,3),(3,2),(4,4),(4,5),(4,6),(4,7),(5,1),(5,4),(5,6),(6,4),(6,5),(7,4)$$$.
|
Input: 3 1 | Output: 15
|
Hard
| 2 | 459 | 90 | 55 | 17 |
174 |
C
|
174C
|
C. Range Increments
| 1,800 |
data structures; greedy
|
Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array a for all indexes in the segment [l, r]. In other words, this function does the following: function rangeIncrement(l, r) for i := l .. r do a[i] = a[i] + 1Polycarpus knows the state of the array a after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105.Before calls of function rangeIncrement(l, r) all array elements equal zero.
|
The first input line contains a single integer n (1 β€ n β€ 105) β the length of the array a[1... n]. The second line contains its integer space-separated elements, a[1], a[2], ..., a[n] (0 β€ a[i] β€ 105) after some series of function calls rangeIncrement(l, r). It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r).
|
Print on the first line t β the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105.Then print t lines β the descriptions of function calls, one per line. Each line should contain two integers li, ri (1 β€ li β€ ri β€ n) β the arguments of the i-th call rangeIncrement(l, r). Calls can be applied in any order.If there are multiple solutions, you are allowed to print any of them.
|
The first sample requires a call for the entire array, and four additional calls: one for the segment [2,2] (i.e. the second element of the array), three for the segment [5,5] (i.e. the fifth element of the array).
|
Input: 61 2 1 1 4 1 | Output: 52 25 55 55 51 6
|
Medium
| 2 | 718 | 427 | 493 | 1 |
1,288 |
C
|
1288C
|
C. Two Arrays
| 1,600 |
combinatorics; dp
|
You are given two integers \(n\) and \(m\). Calculate the number of pairs of arrays \((a, b)\) such that: the length of both arrays is equal to \(m\); each element of each array is an integer between \(1\) and \(n\) (inclusive); \(a_i \le b_i\) for any index \(i\) from \(1\) to \(m\); array \(a\) is sorted in non-descending order; array \(b\) is sorted in non-ascending order. As the result can be very large, you should print it modulo \(10^9+7\).
|
The only line contains two integers \(n\) and \(m\) (\(1 \le n \le 1000\), \(1 \le m \le 10\)).
|
Print one integer β the number of arrays \(a\) and \(b\) satisfying the conditions described above modulo \(10^9+7\).
|
In the first test there are \(5\) suitable arrays: \(a = [1, 1], b = [2, 2]\); \(a = [1, 2], b = [2, 2]\); \(a = [2, 2], b = [2, 2]\); \(a = [1, 1], b = [2, 1]\); \(a = [1, 1], b = [1, 1]\).
|
Input: 2 2 | Output: 5
|
Medium
| 2 | 450 | 95 | 117 | 12 |
1,918 |
B
|
1918B
|
B. Minimize Inversions
| 900 |
constructive algorithms; data structures; greedy; implementation; sortings
|
You are given two permutations \(a\) and \(b\) of length \(n\). A permutation is an array of \(n\) elements from \(1\) to \(n\) where all elements are distinct. For example, an array [\(2,1,3\)] is a permutation, but [\(0,1\)] and [\(1,3,1\)] aren't.You can (as many times as you want) choose two indices \(i\) and \(j\), then swap \(a_i\) with \(a_j\) and \(b_i\) with \(b_j\) simultaneously. You hate inversions, so you want to minimize the total number of inversions in both permutations.An inversion in a permutation \(p\) is a pair of indices \((i, j)\) such that \(i < j\) and \(p_i > p_j\). For example, if \(p=[3,1,4,2,5]\) then there are \(3\) inversions in it (the pairs of indices are \((1,2)\), \((1,4)\) and \((3,4)\)).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 20\,000\)) β the number of test cases.Each test case consists of three lines. The first line contains an integer \(n\) (\(1 \leq n \leq 2\cdot10^5\)) β the length of the permutations \(a\) and \(b\). The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)) β permutation \(a\). The third line contains \(b\) in a similar format. It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, output two permutations \(a'\) and \(b'\) (in the same format as in the input) β the permutations after all operations. The total number of inversions in \(a'\) and \(b'\) should be the minimum possible among all pairs of permutations that can be obtained using operations from the statement.If there are multiple solutions, print any of them.
|
In the first test case, the minimum possible number of inversions is \(10\).In the second test case, we can sort both permutations at the same time. For this, the following operations can be done: Swap the elements in the positions \(1\) and \(3\) in both permutations. After the operation, \(a =\) [\(2,1,3\)], \(b =\) [\(2,1,3\)]. Swap the elements in the positions \(1\) and \(2\). After the operations, \(a\) and \(b\) are sorted. In the third test case, the minimum possible number of inversions is \(7\).
|
Input: 351 2 3 4 55 4 3 2 133 1 23 1 262 5 6 1 3 41 5 3 6 2 4 | Output: 3 2 5 1 4 3 4 1 5 2 1 2 3 1 2 3 2 3 4 6 5 1 1 2 4 3 5 6
|
Beginner
| 5 | 732 | 510 | 363 | 19 |
1,863 |
G
|
1863G
|
G. Swaps
| 2,800 |
combinatorics; dp; graphs; math
|
You are given an array of integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)). You can perform the following operation several (possibly, zero) times: pick an arbitrary \(i\) and perform swap\((a_i, a_{a_i})\). How many distinct arrays is it possible to attain? Output the answer modulo \((10^9 + 7)\).
|
The first line contains an integer \(n\) (\(1 \le n \le 10^6\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1\le a_i\le n\)).
|
Output the number of attainable arrays modulo \((10^9 + 7)\).
|
In the first example, the initial array is \([1, 1, 2]\). If we perform the operation with \(i = 3\), we swap \(a_3\) and \(a_2\), obtaining \([1, 2, 1]\). One can show that there are no other attainable arrays.In the second example, the four attainable arrays are \([2, 1, 4, 3]\), \([1, 2, 4, 3]\), \([1, 2, 3, 4]\), \([2, 1, 3, 4]\). One can show that there are no other attainable arrays.
|
Input: 31 1 2 | Output: 2
|
Master
| 4 | 306 | 149 | 61 | 18 |
1,497 |
E1
|
1497E1
|
E1. Square-Free Division (easy version)
| 1,700 |
data structures; dp; greedy; math; number theory; two pointers
|
This is the easy version of the problem. The only difference is that in this version \(k = 0\).There is an array \(a_1, a_2, \ldots, a_n\) of \(n\) positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square.Moreover, it is allowed to do at most \(k\) such operations before the division: choose a number in the array and change its value to any positive integer. But in this version \(k = 0\), so it is not important.What is the minimum number of continuous segments you should use if you will make changes optimally?
|
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 two integers \(n\), \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(k = 0\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^7\)).It's guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case print a single integer β the answer to the problem.
|
In the first test case the division may be as follows: \([18, 6]\) \([2, 4]\) \([1]\)
|
Input: 35 018 6 2 4 15 06 8 1 24 81 01 | Output: 3 2 1
|
Medium
| 6 | 655 | 407 | 70 | 14 |
377 |
B
|
377B
|
B. Preparing for the Contest
| 1,900 |
binary search; data structures; greedy; sortings
|
Soon there will be held the world's largest programming contest, but the testing system still has m bugs. The contest organizer, a well-known university, has no choice but to attract university students to fix all the bugs. The university has n students able to perform such work. The students realize that they are the only hope of the organizers, so they don't want to work for free: the i-th student wants to get ci 'passes' in his subjects (regardless of the volume of his work).Bugs, like students, are not the same: every bug is characterized by complexity aj, and every student has the level of his abilities bi. Student i can fix a bug j only if the level of his abilities is not less than the complexity of the bug: bi β₯ aj, and he does it in one day. Otherwise, the bug will have to be fixed by another student. Of course, no student can work on a few bugs in one day. All bugs are not dependent on each other, so they can be corrected in any order, and different students can work simultaneously.The university wants to fix all the bugs as quickly as possible, but giving the students the total of not more than s passes. Determine which students to use for that and come up with the schedule of work saying which student should fix which bug.
|
The first line contains three space-separated integers: n, m and s (1 β€ n, m β€ 105, 0 β€ s β€ 109) β the number of students, the number of bugs in the system and the maximum number of passes the university is ready to give the students.The next line contains m space-separated integers a1, a2, ..., am (1 β€ ai β€ 109) β the bugs' complexities.The next line contains n space-separated integers b1, b2, ..., bn (1 β€ bi β€ 109) β the levels of the students' abilities.The next line contains n space-separated integers c1, c2, ..., cn (0 β€ ci β€ 109) β the numbers of the passes the students want to get for their help.
|
If the university can't correct all bugs print ""NO"".Otherwise, on the first line print ""YES"", and on the next line print m space-separated integers: the i-th of these numbers should equal the number of the student who corrects the i-th bug in the optimal answer. The bugs should be corrected as quickly as possible (you must spend the minimum number of days), and the total given passes mustn't exceed s. If there are multiple optimal answers, you can output any of them.
|
Consider the first sample.The third student (with level 3) must fix the 2nd and 4th bugs (complexities 3 and 2 correspondingly) and the second student (with level 1) must fix the 1st and 3rd bugs (their complexity also equals 1). Fixing each bug takes one day for each student, so it takes 2 days to fix all bugs (the students can work in parallel).The second student wants 3 passes for his assistance, the third student wants 6 passes. It meets the university's capabilities as it is ready to give at most 9 passes.
|
Input: 3 4 91 3 1 22 1 34 3 6 | Output: YES2 3 2 3
|
Hard
| 4 | 1,254 | 610 | 475 | 3 |
2,064 |
C
|
2064C
|
C. Remove the Ends
| 1,300 |
brute force; constructive algorithms; dp; greedy
|
You have an array \(a\) of length \(n\) consisting of non-zero integers. Initially, you have \(0\) coins, and you will do the following until \(a\) is empty: Let \(m\) be the current size of \(a\). Select an integer \(i\) where \(1 \le i \le m\), gain \(|a_i|\)\(^{\text{β}}\) coins, and then: if \(a_i < 0\), then replace \(a\) with \([a_1,a_2,\ldots,a_{i - 1}]\) (that is, delete the suffix beginning with \(a_i\)); otherwise, replace \(a\) with \([a_{i + 1},a_{i + 2},\ldots,a_m]\) (that is, delete the prefix ending with \(a_i\)). Find the maximum number of coins you can have at the end of the process.\(^{\text{β}}\)Here \(|a_i|\) represents the absolute value of \(a_i\): it equals \(a_i\) when \(a_i > 0\) and \(-a_i\) when \(a_i < 0\).
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of \(a\).The second line of each testcase contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(-10^9 \le a_i \le 10^9\), \(a_i \ne 0\)).The sum of \(n\) across all testcases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the maximum number of coins you can have at the end of the process.
|
An example of how to get \(23\) coins in the first testcase is as follows: \(a = [3, 1, 4, -1, -5, \color{red}{-9}] \xrightarrow{i = 6} a = [3, 1, 4, -1, -5] \), and get \(9\) coins. \(a = [\color{red}{3}, 1, 4, -1, -5] \xrightarrow{i = 1} a = [1, 4, -1, -5] \), and get \(3\) coins. \(a = [\color{red}{1}, 4, -1, -5] \xrightarrow{i = 1} a = [4, -1, -5] \), and get \(1\) coin. \(a = [4, -1, \color{red}{-5}] \xrightarrow{i = 3} a = [4, -1] \), and get \(5\) coins. \(a = [4, \color{red}{-1}] \xrightarrow{i = 2} a = [4] \), and get \(1\) coin. \(a = [\color{red}{4}] \xrightarrow{i = 1} a = [] \), and get \(4\) coins. After all the operations, you have \(23\) coins.An example of how to get \(40\) coins in the second testcase is as follows: \(a = [-10, -3, -17, \color{red}{1}, 19, 20] \xrightarrow{i = 4} a = [19, 20] \), and get \(1\) coin. \(a = [\color{red}{19}, 20] \xrightarrow{i = 1} a = [20] \), and get \(19\) coins. \(a = [\color{red}{20}] \xrightarrow{i = 1} a = [] \), and get \(20\) coins. After all the operations, you have \(40\) coins.
|
Input: 363 1 4 -1 -5 -96-10 -3 -17 1 19 2011 | Output: 23 40 1
|
Easy
| 4 | 744 | 396 | 94 | 20 |
1,106 |
A
|
1106A
|
A. Lunar New Year and Cross Counting
| 800 |
implementation
|
Lunar New Year is approaching, and you bought a matrix with lots of ""crosses"".This matrix \(M\) of size \(n \times n\) contains only 'X' and '.' (without quotes). The element in the \(i\)-th row and the \(j\)-th column \((i, j)\) is defined as \(M(i, j)\), where \(1 \leq i, j \leq n\). We define a cross appearing in the \(i\)-th row and the \(j\)-th column (\(1 < i, j < n\)) if and only if \(M(i, j) = M(i - 1, j - 1) = M(i - 1, j + 1) = M(i + 1, j - 1) = M(i + 1, j + 1) = \) 'X'.The following figure illustrates a cross appearing at position \((2, 2)\) in a \(3 \times 3\) matrix. X.X.X.X.X Your task is to find out the number of crosses in the given matrix \(M\). Two crosses are different if and only if they appear in different rows or columns.
|
The first line contains only one positive integer \(n\) (\(1 \leq n \leq 500\)), denoting the size of the matrix \(M\).The following \(n\) lines illustrate the matrix \(M\). Each line contains exactly \(n\) characters, each of them is 'X' or '.'. The \(j\)-th element in the \(i\)-th line represents \(M(i, j)\), where \(1 \leq i, j \leq n\).
|
Output a single line containing only one integer number \(k\) β the number of crosses in the given matrix \(M\).
|
In the first sample, a cross appears at \((3, 3)\), so the answer is \(1\).In the second sample, no crosses appear since \(n < 3\), so the answer is \(0\).In the third sample, crosses appear at \((3, 2)\), \((3, 4)\), \((4, 3)\), \((4, 5)\), so the answer is \(4\).
|
Input: 5 ..... .XXX. .XXX. .XXX. ..... | Output: 1
|
Beginner
| 1 | 754 | 342 | 112 | 11 |
1,910 |
B
|
1910B
|
B. Security Guard
| 1,600 |
*special; greedy
|
Monocarp is a security guard in Berland State University. Every day, he tracks how many people and at what time enter and leave the university. He writes this information down as follows: when someone enters the university, Monocarp writes a plus sign '+'; when someone leaves the university, Monocarp writes a minus sign '-'. At the beginning of the current day, there are no people at the university, except for Monocarp. During the day, Monocarp wrote out a sequence \(s\). The characters in \(s\) are listed in the order Monocarp wrote them.Suddenly, Monocarp's boss decided to check his work. Unfortunately, Monocarp is a bit careless. So, the sequence \(s\) that he wrote might be impossible. For example, the sequence ""+--"" can't happen, since it represents a scenario when one person enters the university and two leave.Before his boss starts checking the sequence, Monocarp has the time to swap at most one pair of characters in it. Can he do it in such a way that the resulting sequence is plausible? Note that if the given sequence is already plausible, Monocarp doesn't have to swap anything.
|
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 string \(s\) (\(1 \le |s| \le 3 \cdot 10^5\)), consisting only of characters '+' and/or '-'. A plus sign '+' represents a person entering the university. A minus sign '-' represents a person leaving the university.The sum of all \(|s|\) over all test cases doesn't exceed \(3 \cdot 10^5\).
|
For each test case, print an answer.If it's impossible to swap at most one pair of characters so that the resulting sequence is plausible, print -1.Otherwise, print two integers. If you swap one pair of characters, print two distinct integers from \(1\) to \(n\) β the indices of characters to swap. If you don't swap, print one integer from \(1\) to \(n\) twice β swap a character with itself.If there are multiple answers, print any of them.
|
Input: 6-++-+++----+++- | Output: 1 2 1 1 1 1 -1 1 1 -1
|
Medium
| 2 | 1,106 | 436 | 443 | 19 |
|
1,936 |
E
|
1936E
|
E. Yet Yet Another Permutation Problem
| 3,400 |
divide and conquer; fft; math
|
You are given a permutation \(p\) of length \(n\).Please count the number of permutations \(q\) of length \(n\) which satisfy the following: for each \(1 \le i < n\), \(\max(q_1,\ldots,q_i) \neq \max(p_1,\ldots,p_i)\).Since the answer may be large, output the answer modulo \(998\,244\,353\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)). It is guaranteed that \(p\) is a permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the answer modulo \(998\,244\,353\).
|
In the first test case, \(p = [2, 1]\). The only suitable \(q\) is \([1, 2]\). Indeed, we need to satisfy the inequality \(q_1 \neq p_1\). It only holds for \(q = [1, 2]\).In the second test case, \(p = [1, 2, 3]\). So \(q\) has to satisfy two inequalities: \(q_1 \neq p_1\) and \(\max(q_1, q_2) \neq \max(1, 2) = 2\). One can prove that this only holds for the following \(3\) permutations: \(q = [2, 3, 1]\): in this case \(q_1 = 2 \neq 1\) and \(\max(q_1, q_2) = 3 \neq 2\); \(q = [3, 1, 2]\): in this case \(q_1 = 3 \neq 1\) and \(\max(q_1, q_2) = 3 \neq 2\); \(q = [3, 2, 1]\): in this case \(q_1 = 3 \neq 1\) and \(\max(q_1, q_2) = 3 \neq 2\).
|
Input: 622 131 2 333 1 242 4 1 353 5 1 4 2156 13 2 8 7 11 1 3 9 15 4 5 12 10 14 | Output: 1 3 2 4 18 424488915
|
Master
| 3 | 292 | 501 | 81 | 19 |
1,926 |
G
|
1926G
|
G. Vlad and Trouble at MIT
| 1,900 |
dfs and similar; dp; flows; graphs; greedy; implementation; trees
|
Vladislav has a son who really wanted to go to MIT. The college dormitory at MIT (Moldova Institute of Technology) can be represented as a tree with \(n\) vertices, each vertex being a room with exactly one student. A tree is a connected undirected graph with \(n\) vertices and \(n-1\) edges.Tonight, there are three types of students: students who want to party and play music (marked with \(\texttt{P}\)), students who wish to sleep and enjoy silence (marked with \(\texttt{S}\)), and students who don't care (marked with \(\texttt{C}\)). Initially, all the edges are thin walls which allow music to pass through, so when a partying student puts music on, it will be heard in every room. However, we can place some thick walls on any edges β thick walls don't allow music to pass through them.The university wants to install some thick walls so that every partying student can play music, and no sleepy student can hear it.Because the university lost a lot of money in a naming rights lawsuit, they ask you to find the minimum number of thick walls they will need to use.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 10^5\)) β the number of vertices in the tree.The second line of each test case contains \(n-1\) integers \(a_2, \dots , a_n\) (\(1 \leq a_i < i\)) β it means there is an edge between \(i\) and \(a_i\) in the tree.The third line of each test case contains a string \(s\) of length \(n\) consisting of characters \(\texttt{P}\), \(\texttt{S}\), and \(\texttt{C}\), denoting that student \(i\) is of type \(s_i\).The sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output a single integer β the minimum number of thick walls needed.
|
In the first case, we can install one thick wall between rooms \(1\) and \(2\), as shown below. We cannot install \(0\) walls, since then the music from room 3 will reach room 2 where a student wants to sleep, so the answer is \(1\). There are other valid solutions.
|
Input: 331 1CSP41 2 2PCSS41 2 2PPSS | Output: 1 1 2
|
Hard
| 7 | 1,074 | 647 | 87 | 19 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.