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,635 |
A
|
1635A
|
A. Min Or Sum
| 800 |
bitmasks; greedy
|
You are given an array \(a\) of size \(n\).You can perform the following operation on the array: Choose two different integers \(i, j\) \((1 \leq i < j \leq n\)), replace \(a_i\) with \(x\) and \(a_j\) with \(y\). In order not to break the array, \(a_i | a_j = x | y\) must be held, where \(|\) denotes the bitwise OR operation. Notice that \(x\) and \(y\) are non-negative integers. Please output the minimum sum of the array you can get after using the operation above any number of times.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) \((1 \leq t \leq 1000)\). Description of the test cases follows.The first line of each test case contains an integer \(n\) \((2 \leq n \leq 100)\) β the size of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots ,a_n\) \((0 \leq a_i < 2^{30})\).
|
For each test case, print one number in a line β the minimum possible sum of the array.
|
In the first example, you can perform the following operations to obtain the array \([1, 0, 2]\):1. choose \(i = 1, j = 2\), change \(a_1 = 1\) and \(a_2 = 2\), it's valid since \(1 | 3 = 1 | 2\). The array becomes \([1, 2, 2]\).2. choose \(i = 2, j = 3\), change \(a_2 = 0\) and \(a_3 = 2\), it's valid since \(2 | 2 = 0 | 2\). The array becomes \([1, 0, 2]\).We can prove that the minimum sum is \(1 + 0 + 2 = 3\)In the second example, We don't need any operations.
|
Input: 431 3 251 2 4 8 1626 633 5 6 | Output: 3 31 6 7
|
Beginner
| 2 | 491 | 378 | 87 | 16 |
575 |
B
|
575B
|
B. Bribes
| 2,200 |
dfs and similar; graphs; trees
|
Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way β however, we know that all towns are reachable, though illegally!Fortunately for us, the police tend to be very corrupt and they will allow a lorry driver to break the rules and drive in the wrong direction provided they receive βa small giftβ. There is one patrol car for every road and they will request 1000 Ruritanian dinars when a driver drives in the wrong direction. However, being greedy, every time a patrol car notices the same driver breaking the rule, they will charge double the amount of money they requested the previous time on that particular road.Borna is a lorry driver that managed to figure out this bribing pattern. As part of his job, he has to make K stops in some towns all over Ruritania and he has to make these stops in a certain order. There are N towns (enumerated from 1 to N) in Ruritania and Bornaβs initial location is the capital city i.e. town 1. He happens to know which ones out of the N - 1 roads in Ruritania are currently unidirectional, but he is unable to compute the least amount of money he needs to prepare for bribing the police. Help Borna by providing him with an answer and you will be richly rewarded.
|
The first line contains N, the number of towns in Ruritania. The following N - 1 lines contain information regarding individual roads between towns. A road is represented by a tuple of integers (a,b,x), which are separated with a single whitespace character. The numbers a and b represent the cities connected by this particular road, and x is either 0 or 1: 0 means that the road is bidirectional, 1 means that only the a β b direction is legal. The next line contains K, the number of stops Borna has to make. The final line of input contains K positive integers s1, β¦, sK: the towns Borna has to visit. 1 β€ N β€ 105 1 β€ K β€ 106 1 β€ a, b β€ N for all roads for all roads 1 β€ si β€ N for all 1 β€ i β€ K
|
The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo 109 + 7.
|
Borna first takes the route 1 β 5 and has to pay 1000 dinars. After that, he takes the route 5 β 1 β 2 β 3 β 4 and pays nothing this time. However, when he has to return via 4 β 3 β 2 β 1 β 5, he needs to prepare 3000 (1000+2000) dinars. Afterwards, getting to 2 via 5 β 1 β 2 will cost him nothing. Finally, he doesn't even have to leave town 2 to get to 2, so there is no need to prepare any additional bribe money. Hence he has to prepare 4000 dinars in total.
|
Input: 51 2 02 3 05 1 13 4 155 4 5 2 2 | Output: 4
|
Hard
| 3 | 1,456 | 699 | 143 | 5 |
303 |
D
|
303D
|
D. Rotatable Number
| 2,600 |
math; number theory
|
Bike is a smart boy who loves math very much. He invented a number called ""Rotatable Number"" inspired by 142857. As you can see, 142857 is a magic number because any of its rotatings can be got by multiplying that number by 1, 2, ..., 6 (numbers from one to number's length). Rotating a number means putting its last several digit into first. For example, by rotating number 12345 you can obtain any numbers: 12345, 51234, 45123, 34512, 23451. It's worth mentioning that leading-zeroes are allowed. So both 4500123 and 0123450 can be obtained by rotating 0012345. You can see why 142857 satisfies the condition. All of the 6 equations are under base 10. 142857Β·1 = 142857; 142857Β·2 = 285714; 142857Β·3 = 428571; 142857Β·4 = 571428; 142857Β·5 = 714285; 142857Β·6 = 857142. Now, Bike has a problem. He extends ""Rotatable Number"" under any base b. As is mentioned above, 142857 is a ""Rotatable Number"" under base 10. Another example is 0011 under base 2. All of the 4 equations are under base 2. 0011Β·1 = 0011; 0011Β·10 = 0110; 0011Β·11 = 1001; 0011Β·100 = 1100. So, he wants to find the largest b (1 < b < x) so that there is a positive ""Rotatable Number"" (leading-zeroes allowed) of length n under base b.Note that any time you multiply a rotatable number by numbers from 1 to its length you should get a rotating of that number.
|
The only line contains two space-separated integers n, x (1 β€ n β€ 5Β·106, 2 β€ x β€ 109).
|
Print a single integer β the largest b you found. If no such b exists, print -1 instead.
|
Input: 6 11 | Output: 10
|
Expert
| 2 | 1,329 | 86 | 88 | 3 |
|
979 |
D
|
979D
|
D. Kuro and GCD and XOR and SUM
| 2,200 |
binary search; bitmasks; brute force; data structures; dp; dsu; greedy; math; number theory; strings; trees
|
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common divisor (GCD), the XOR value, and the sum of two numbers. Kuro loves the game so much that he solves levels by levels day by day.Sadly, he's going on a vacation for a day, and he isn't able to continue his solving streak on his own. As Katie is a reliable person, Kuro kindly asked her to come to his house on this day to play the game for him.Initally, there is an empty array \(a\). The game consists of \(q\) tasks of two types. The first type asks Katie to add a number \(u_i\) to \(a\). The second type asks Katie to find a number \(v\) existing in \(a\) such that \(k_i \mid GCD(x_i, v)\), \(x_i + v \leq s_i\), and \(x_i \oplus v\) is maximized, where \(\oplus\) denotes the bitwise XOR operation, \(GCD(c, d)\) denotes the greatest common divisor of integers \(c\) and \(d\), and \(y \mid x\) means \(x\) is divisible by \(y\), or report -1 if no such numbers are found.Since you are a programmer, Katie needs you to automatically and accurately perform the tasks in the game to satisfy her dear friend Kuro. Let's help her!
|
The first line contains one integer \(q\) (\(2 \leq q \leq 10^{5}\)) β the number of tasks the game wants you to perform.\(q\) lines follow, each line begins with an integer \(t_i\) β the type of the task: If \(t_i = 1\), an integer \(u_i\) follow (\(1 \leq u_i \leq 10^{5}\)) β you have to add \(u_i\) to the array \(a\). If \(t_i = 2\), three integers \(x_i\), \(k_i\), and \(s_i\) follow (\(1 \leq x_i, k_i, s_i \leq 10^{5}\)) β you must find a number \(v\) existing in the array \(a\) such that \(k_i \mid GCD(x_i, v)\), \(x_i + v \leq s_i\), and \(x_i \oplus v\) is maximized, where \(\oplus\) denotes the XOR operation, or report -1 if no such numbers are found. It is guaranteed that the type of the first task is type \(1\), and there exists at least one task of type \(2\).
|
For each task of type \(2\), output on one line the desired number \(v\), or -1 if no such numbers are found.
|
In the first example, there are 5 tasks: The first task requires you to add \(1\) into \(a\). \(a\) is now \(\left\{1\right\}\). The second task requires you to add \(2\) into \(a\). \(a\) is now \(\left\{1, 2\right\}\). The third task asks you a question with \(x = 1\), \(k = 1\) and \(s = 3\). Taking both \(1\) and \(2\) as \(v\) satisfies \(1 \mid GCD(1, v)\) and \(1 + v \leq 3\). Because \(2 \oplus 1 = 3 > 1 \oplus 1 = 0\), \(2\) is the answer to this task. The fourth task asks you a question with \(x = 1\), \(k = 1\) and \(s = 2\). Only \(v = 1\) satisfies \(1 \mid GCD(1, v)\) and \(1 + v \leq 2\), so \(1\) is the answer to this task. The fifth task asks you a question with \(x = 1\), \(k = 1\) and \(s = 1\). There are no elements in \(a\) that satisfy the conditions, so we report -1 as the answer to this task.
|
Input: 51 11 22 1 1 32 1 1 22 1 1 1 | Output: 21-1
|
Hard
| 11 | 1,133 | 782 | 109 | 9 |
1,623 |
C
|
1623C
|
C. Balanced Stone Heaps
| 1,600 |
binary search; greedy
|
There are \(n\) heaps of stone. The \(i\)-th heap has \(h_i\) stones. You want to change the number of stones in the heap by performing the following process once: You go through the heaps from the \(3\)-rd heap to the \(n\)-th heap, in this order. Let \(i\) be the number of the current heap. You can choose a number \(d\) (\(0 \le 3 \cdot d \le h_i\)), move \(d\) stones from the \(i\)-th heap to the \((i - 1)\)-th heap, and \(2 \cdot d\) stones from the \(i\)-th heap to the \((i - 2)\)-th heap. So after that \(h_i\) is decreased by \(3 \cdot d\), \(h_{i - 1}\) is increased by \(d\), and \(h_{i - 2}\) is increased by \(2 \cdot d\). You can choose different or same \(d\) for different operations. Some heaps may become empty, but they still count as heaps. What is the maximum number of stones in the smallest heap after the process?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^5\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)).The second lines of each test case contains \(n\) integers \(h_1, h_2, h_3, \ldots, h_n\) (\(1 \le h_i \le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print the maximum number of stones that the smallest heap can contain.
|
In the first test case, the initial heap sizes are \([1, 2, 10, 100]\). We can move the stones as follows. move \(3\) stones and \(6\) from the \(3\)-rd heap to the \(2\)-nd and \(1\) heap respectively. The heap sizes will be \([7, 5, 1, 100]\); move \(6\) stones and \(12\) stones from the last heap to the \(3\)-rd and \(2\)-nd heap respectively. The heap sizes will be \([7, 17, 7, 82]\). In the second test case, the last heap is \(1\), and we can not increase its size.In the third test case, it is better not to move any stones.In the last test case, the final achievable configuration of the heaps can be \([3, 5, 3, 4, 3, 3]\).
|
Input: 441 2 10 1004100 100 100 155 1 1 1 861 2 3 4 5 6 | Output: 7 1 1 3
|
Medium
| 2 | 840 | 467 | 90 | 16 |
1,372 |
D
|
1372D
|
D. Omkar and Circle
| 2,100 |
brute force; dp; games; greedy
|
Danny, the local Math Maniac, is fascinated by circles, Omkar's most recent creation. Help him solve this circle problem!You are given \(n\) nonnegative integers \(a_1, a_2, \dots, a_n\) arranged in a circle, where \(n\) must be odd (ie. \(n-1\) is divisible by \(2\)). Formally, for all \(i\) such that \(2 \leq i \leq n\), the elements \(a_{i - 1}\) and \(a_i\) are considered to be adjacent, and \(a_n\) and \(a_1\) are also considered to be adjacent. In one operation, you pick a number on the circle, replace it with the sum of the two elements adjacent to it, and then delete the two adjacent elements from the circle. This is repeated until only one number remains in the circle, which we call the circular value.Help Danny find the maximum possible circular value after some sequences of operations.
|
The first line contains one odd integer \(n\) (\(1 \leq n < 2 \cdot 10^5\), \(n\) is odd) β the initial size of the circle.The second line contains \(n\) integers \(a_{1},a_{2},\dots,a_{n}\) (\(0 \leq a_{i} \leq 10^9\)) β the initial numbers in the circle.
|
Output the maximum possible circular value after applying some sequence of operations to the given circle.
|
For the first test case, here's how a circular value of \(17\) is obtained:Pick the number at index \(3\). The sum of adjacent elements equals \(17\). Delete \(7\) and \(10\) from the circle and replace \(2\) with \(17\).Note that the answer may not fit in a \(32\)-bit integer.
|
Input: 3 7 10 2 | Output: 17
|
Hard
| 4 | 807 | 256 | 106 | 13 |
743 |
E
|
743E
|
E. Vladik and cards
| 2,200 |
binary search; bitmasks; brute force; dp
|
Vladik was bored on his way home and decided to play the following game. He took n cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions: the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are ck cards with number k on them in the subsequence, than for all pairs of integers the condition |ci - cj| β€ 1 must hold. if there is at least one card with number x on it in the subsequence, then all cards with number x in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1, 1, 2, 2] satisfies this condition while the subsequence [1, 2, 2, 1] doesn't. Note that [1, 1, 2, 2] doesn't satisfy the first condition. Please help Vladik to find the length of the longest subsequence that satisfies both conditions.
|
The first line contains single integer n (1 β€ n β€ 1000) β the number of cards in Vladik's sequence.The second line contains the sequence of n positive integers not exceeding 8 β the description of Vladik's sequence.
|
Print single integer β the length of the longest subsequence of Vladik's sequence that satisfies both conditions.
|
In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.
|
Input: 31 1 1 | Output: 1
|
Hard
| 4 | 1,088 | 215 | 113 | 7 |
2,063 |
B
|
2063B
|
B. Subsequence Update
| 1,100 |
constructive algorithms; data structures; greedy; sortings
|
After Little John borrowed expansion screws from auntie a few hundred times, eventually she decided to come and take back the unused ones.But as they are a crucial part of home design, Little John decides to hide some in the most unreachable places β under the eco-friendly wood veneers. You are given an integer sequence \(a_1, a_2, \ldots, a_n\), and a segment \([l,r]\) (\(1 \le l \le r \le n\)).You must perform the following operation on the sequence exactly once. Choose any subsequence\(^{\text{β}}\) of the sequence \(a\), and reverse it. Note that the subsequence does not have to be contiguous. Formally, choose any number of indices \(i_1,i_2,\ldots,i_k\) such that \(1 \le i_1 < i_2 < \ldots < i_k \le n\). Then, change the \(i_x\)-th element to the original value of the \(i_{k-x+1}\)-th element simultaneously for all \(1 \le x \le k\).Find the minimum value of \(a_l+a_{l+1}+\ldots+a_{r-1}+a_r\) after performing the operation.\(^{\text{β}}\)A sequence \(b\) is a subsequence of a sequence \(a\) if \(b\) can be obtained from \(a\) by the deletion of several (possibly, zero or all) element from arbitrary positions.
|
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 three integers \(n\), \(l\), \(r\) (\(1 \le l \le r \le n \le 10^5\)) β the length of \(a\), and the segment \([l,r]\).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_{i} \le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output the minimum value of \(a_l+a_{l+1}+\ldots+a_{r-1}+a_r\) on a separate line.
|
On the second test case, the array is \(a=[1,2,3]\) and the segment is \([2,3]\).After choosing the subsequence \(a_1,a_3\) and reversing it, the sequence becomes \([3,2,1]\). Then, the sum \(a_2+a_3\) becomes \(3\). It can be shown that the minimum possible value of the sum is \(3\).
|
Input: 62 1 12 13 2 31 2 33 1 33 1 24 2 31 2 2 25 2 53 3 2 3 56 1 33 6 6 4 3 2 | Output: 1 3 6 3 11 8
|
Easy
| 4 | 1,131 | 515 | 102 | 20 |
1,083 |
B
|
1083B
|
B. The Fair Nut and Strings
| 2,000 |
greedy; strings
|
Recently, the Fair Nut has written \(k\) strings of length \(n\), consisting of letters ""a"" and ""b"". He calculated \(c\) β the number of strings that are prefixes of at least one of the written strings. Every string was counted only one time.Then, he lost his sheet with strings. He remembers that all written strings were lexicographically not smaller than string \(s\) and not bigger than string \(t\). He is interested: what is the maximum value of \(c\) that he could get.A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).
|
The first line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 5 \cdot 10^5\), \(1 \leq k \leq 10^9\)).The second line contains a string \(s\) (\(|s| = n\)) β the string consisting of letters ""a"" and ""b.The third line contains a string \(t\) (\(|t| = n\)) β the string consisting of letters ""a"" and ""b.It is guaranteed that string \(s\) is lexicographically not bigger than \(t\).
|
Print one number β maximal value of \(c\).
|
In the first example, Nut could write strings ""aa"", ""ab"", ""ba"", ""bb"". These \(4\) strings are prefixes of at least one of the written strings, as well as ""a"" and ""b"". Totally, \(6\) strings.In the second example, Nut could write strings ""aba"", ""baa"", ""bba"".In the third example, there are only two different strings that Nut could write. If both of them are written, \(c=8\).
|
Input: 2 4 aa bb | Output: 6
|
Hard
| 2 | 790 | 393 | 42 | 10 |
1,866 |
F
|
1866F
|
F. Freak Joker Process
| 3,100 |
binary search; data structures; sortings
|
After the success of the basketball teams formed and trained by Pak Chanek last year (Basketball Together), Pak Chanek wants to measure the performance of each player that is considered as a superstar.There are \(N\) superstar players that have been trained by Pak Chanek. At the end of the season, some calculations will be made on the performance of the \(N\) players using an international method. Each player has two values \(A_i\) and \(B_i\) where each represents the offensive and defensive value of that player.Define \(\text{RankA}(i)\) as the offensive ranking of the \(i\)-th player, whose value is \(c+1\) with \(c\) here representing the number of \(j\) (\(1 \leq j \leq N\)) such that \(A_j>A_i\). Define \(\text{RankB}(i)\) as the defensive ranking of the \(i\)-th player, whose value is \(c+1\) with \(c\) here representing the number of \(j\) (\(1 \leq j \leq N\)) such that \(B_j>B_i\).Define \(\text{RankOverall}(i)\) as the overall ranking of the \(i\)-th player, whose value is \(c+1\) with \(c\) here representing the number of \(j\) (\(1 \leq j \leq N\)) such that \(\text{RankA}(j)+\text{RankB}(j)<\text{RankA}(i)+\text{RankB}(i)\).During the next \(Q\) days, exactly one event will happen on each day. Each event is one of the three following possibilities: 1 k c β If \(c\) is +, then \(A_k\) increases by \(1\). If \(c\) is -, then \(A_k\) decreases by \(1\). (\(1\leq k\leq N\); \(c\) is + or -) 2 k c β If \(c\) is +, then \(B_k\) increases by \(1\). If \(c\) is -, then \(B_k\) decreases by \(1\). (\(1\leq k\leq N\); \(c\) is + or -) 3 k β Pak Chanek wants to know the value of \(\text{RankOverall}(k)\) at that moment. (\(1\leq k\leq N\))
|
The first line contains a single integer \(N\) (\(1\leq N\leq10^5\)) β the number of superstar players.The second line contains \(N\) integers \(A_1, A_2, A_3 \ldots, A_N\) (\(1 \leq A_i \leq 10^5\)) β the offensive value of each player.The third line contains \(N\) integers \(B_1, B_2, B_3 \ldots, B_N\) (\(1 \leq B_i \leq 10^5\)) β the defensive value of each player.The fourth line contains a single integer \(Q\) (\(1\leq Q\leq10^5\)) β the number of events.The \(j\)-th of the next \(Q\) lines contains the \(j\)-th event as described. At any moment, each value of \(A_i\) and \(B_i\) is always between \(1\) and \(10^5\) inclusive. There is at least one event of type \(3\).
|
For each event of type \(3\), output a line containing an integer representing the value of \(\text{RankOverall}(k)\) at that moment.
|
At the \(8\)-th event, \(A=[3,2,1,3,2]\) and \(B=[3,6,1,4,1]\). It can be obtained that the values of \(\text{RankA}\) and \(\text{RankB}\) for each player are as follows: \(\text{RankA}(1)=1\), \(\text{RankB}(1)=3\) \(\text{RankA}(2)=3\), \(\text{RankB}(2)=1\) \(\text{RankA}(3)=5\), \(\text{RankB}(3)=4\) \(\text{RankA}(4)=1\), \(\text{RankB}(4)=2\) \(\text{RankA}(5)=3\), \(\text{RankB}(5)=4\) So it can be obtained that \(\text{RankOverall}(1)=2\).
|
Input: 5 3 3 1 3 2 3 7 1 3 1 8 3 1 3 2 2 4 + 1 2 - 3 2 3 3 2 2 - 3 1 | Output: 2 1 2 5 2
|
Master
| 3 | 1,670 | 681 | 133 | 18 |
1,009 |
A
|
1009A
|
A. Game Shopping
| 800 |
implementation
|
Maxim wants to buy some games at the local game shop. There are \(n\) games in the shop, the \(i\)-th game costs \(c_i\).Maxim has a wallet which can be represented as an array of integers. His wallet contains \(m\) bills, the \(j\)-th bill has value \(a_j\).Games in the shop are ordered from left to right, Maxim tries to buy every game in that order.When Maxim stands at the position \(i\) in the shop, he takes the first bill from his wallet (if his wallet is empty then he proceeds to the next position immediately) and tries to buy the \(i\)-th game using this bill. After Maxim tried to buy the \(n\)-th game, he leaves the shop.Maxim buys the \(i\)-th game if and only if the value of the first bill (which he takes) from his wallet is greater or equal to the cost of the \(i\)-th game. If he successfully buys the \(i\)-th game, the first bill from his wallet disappears and the next bill becomes first. Otherwise Maxim leaves the first bill in his wallet (this bill still remains the first one) and proceeds to the next game.For example, for array \(c = [2, 4, 5, 2, 4]\) and array \(a = [5, 3, 4, 6]\) the following process takes place: Maxim buys the first game using the first bill (its value is \(5\)), the bill disappears, after that the second bill (with value \(3\)) becomes the first one in Maxim's wallet, then Maxim doesn't buy the second game because \(c_2 > a_2\), the same with the third game, then he buys the fourth game using the bill of value \(a_2\) (the third bill becomes the first one in Maxim's wallet) and buys the fifth game using the bill of value \(a_3\).Your task is to get the number of games Maxim will buy.
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) β the number of games and the number of bills in Maxim's wallet.The second line of the input contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le 1000\)), where \(c_i\) is the cost of the \(i\)-th game.The third line of the input contains \(m\) integers \(a_1, a_2, \dots, a_m\) (\(1 \le a_j \le 1000\)), where \(a_j\) is the value of the \(j\)-th bill from the Maxim's wallet.
|
Print a single integer β the number of games Maxim will buy.
|
The first example is described in the problem statement.In the second example Maxim cannot buy any game because the value of the first bill in his wallet is smaller than the cost of any game in the shop.In the third example the values of the bills in Maxim's wallet are large enough to buy any game he encounter until he runs out of bills in his wallet.
|
Input: 5 42 4 5 2 45 3 4 6 | Output: 3
|
Beginner
| 1 | 1,646 | 482 | 60 | 10 |
1,843 |
F1
|
1843F1
|
F1. Omsk Metro (simple version)
| 1,800 |
data structures; dfs and similar; dp; graphs; greedy; math; trees
|
This is the simple version of the problem. The only difference between the simple and hard versions is that in this version \(u = 1\).As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and between any two stations there is exactly one path that passes through each of the tunnels no more than once. In other words, the metro is a tree.To develop the metro and attract residents, the following system is used in Omsk. Each station has its own weight \(x \in \{-1, 1\}\). If the station has a weight of \(-1\), then when the station is visited by an Omsk resident, a fee of \(1\) burle is charged. If the weight of the station is \(1\), then the Omsk resident is rewarded with \(1\) burle.Omsk Metro currently has only one station with number \(1\) and weight \(x = 1\). Every day, one of the following events occurs: A new station with weight \(x\) is added to the station with number \(v_i\), and it is assigned a number that is one greater than the number of existing stations. Alex, who lives in Omsk, wonders: is there a subsegment\(\dagger\) (possibly empty) of the path between vertices \(u\) and \(v\) such that, by traveling along it, exactly \(k\) burles can be earned (if \(k < 0\), this means that \(k\) burles will have to be spent on travel). In other words, Alex is interested in whether there is such a subsegment of the path that the sum of the weights of the vertices in it is equal to \(k\). Note that the subsegment can be empty, and then the sum is equal to \(0\). You are a friend of Alex, so your task is to answer Alex's questions.\(\dagger\)Subsegment β continuous sequence of elements.
|
The first line contains a single number \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains the number \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of events.Then there are \(n\) lines describing the events. In the \(i\)-th line, one of the following options is possible: First comes the symbol ""+"" (without quotes), then two numbers \(v_i\) and \(x_i\) (\(x_i \in \{-1, 1\}\), it is also guaranteed that the vertex with number \(v_i\) exists). In this case, a new station with weight \(x_i\) is added to the station with number \(v_i\). First comes the symbol ""?"" (without quotes), and then three numbers \(u_i\), \(v_i\), and \(k_i\) (\(-n \le k_i \le n\)). It is guaranteed that the vertices with numbers \(u_i\) and \(v_i\) exist. In this case, it is necessary to determine whether there is a subsegment (possibly empty) of the path between stations \(u_i\) and \(v_i\) with a sum of weights exactly equal to \(k_i\). In this version of the task, it is guaranteed that \(u_i = 1\). It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each of Alex's questions, output ""Yes"" (without quotes) if the subsegment described in the condition exists, otherwise output ""No"" (without quotes).You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
|
Explanation of the first sample.The answer to the second question is ""Yes"", because there is a path \(1\).In the fourth question, we can choose the \(1\) path again.In the fifth query, the answer is ""Yes"", since there is a path \(1-3\).In the sixth query, we can choose an empty path because the sum of the weights on it is \(0\).It is not difficult to show that there are no paths satisfying the first and third queries.
|
Input: 18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0 | Output: NO YES NO YES YES YES
|
Medium
| 7 | 1,739 | 1,137 | 299 | 18 |
625 |
A
|
625A
|
A. Guest From the Past
| 1,700 |
implementation; math
|
Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated.Kolya is hungry, so he went to the nearest milk shop. In 2084 you may buy kefir in a plastic liter bottle, that costs a rubles, or in glass liter bottle, that costs b rubles. Also, you may return empty glass bottle and get c (c < b) rubles back, but you cannot return plastic bottles.Kolya has n rubles and he is really hungry, so he wants to drink as much kefir as possible. There were no plastic bottles in his 1984, so Kolya doesn't know how to act optimally and asks for your help.
|
First line of the input contains a single integer n (1 β€ n β€ 1018) β the number of rubles Kolya has at the beginning.Then follow three lines containing integers a, b and c (1 β€ a β€ 1018, 1 β€ c < b β€ 1018) β the cost of one plastic liter bottle, the cost of one glass liter bottle and the money one can get back by returning an empty glass bottle, respectively.
|
Print the only integer β maximum number of liters of kefir, that Kolya can drink.
|
In the first sample, Kolya can buy one glass bottle, then return it and buy one more glass bottle. Thus he will drink 2 liters of kefir.In the second sample, Kolya can buy two plastic bottle and get two liters of kefir, or he can buy one liter glass bottle, then return it and buy one plastic bottle. In both cases he will drink two liters of kefir.
|
Input: 101198 | Output: 2
|
Medium
| 2 | 714 | 360 | 81 | 6 |
337 |
D
|
337D
|
D. Book of Evil
| 2,000 |
dfs and similar; divide and conquer; dp; trees
|
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n settlements numbered from 1 to n. Moving through the swamp is very difficult, so people tramped exactly n - 1 paths. Each of these paths connects some pair of settlements and is bidirectional. Moreover, it is possible to reach any settlement from any other one by traversing one or several paths.The distance between two settlements is the minimum number of paths that have to be crossed to get from one settlement to the other one. Manao knows that the Book of Evil has got a damage range d. This means that if the Book of Evil is located in some settlement, its damage (for example, emergence of ghosts and werewolves) affects other settlements at distance d or less from the settlement where the Book resides.Manao has heard of m settlements affected by the Book of Evil. Their numbers are p1, p2, ..., pm. Note that the Book may be affecting other settlements as well, but this has not been detected yet. Manao wants to determine which settlements may contain the Book. Help him with this difficult task.
|
The first line contains three space-separated integers n, m and d (1 β€ m β€ n β€ 100000; 0 β€ d β€ n - 1). The second line contains m distinct space-separated integers p1, p2, ..., pm (1 β€ pi β€ n). Then n - 1 lines follow, each line describes a path made in the area. A path is described by a pair of space-separated integers ai and bi representing the ends of this path.
|
Print a single number β the number of settlements that may contain the Book of Evil. It is possible that Manao received some controversial information and there is no settlement that may contain the Book. In such case, print 0.
|
Sample 1. The damage range of the Book of Evil equals 3 and its effects have been noticed in settlements 1 and 2. Thus, it can be in settlements 3, 4 or 5.
|
Input: 6 2 31 21 52 33 44 55 6 | Output: 3
|
Hard
| 4 | 1,106 | 367 | 227 | 3 |
443 |
A
|
443A
|
A. Anton and Letters
| 800 |
constructive algorithms; implementation
|
Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line. Unfortunately, from time to time Anton would forget writing some letter and write it again. He asks you to count the total number of distinct letters in his set.
|
The first and the single line contains the set of letters. The length of the line doesn't exceed 1000. It is guaranteed that the line starts from an opening curved bracket and ends with a closing curved bracket. Between them, small English letters are listed, separated by a comma. Each comma is followed by a space.
|
Print a single number β the number of distinct letters in Anton's set.
|
Input: {a, b, c} | Output: 3
|
Beginner
| 2 | 449 | 316 | 70 | 4 |
|
234 |
F
|
234F
|
F. Fence
| 1,800 |
dp
|
Vasya should paint a fence in front of his own cottage. The fence is a sequence of n wooden boards arranged in a single row. Each board is a 1 centimeter wide rectangle. Let's number the board fence using numbers 1, 2, ..., n from left to right. The height of the i-th board is hi centimeters.Vasya has a 1 centimeter wide brush and the paint of two colors, red and green. Of course, the amount of the paint is limited. Vasya counted the area he can paint each of the colors. It turned out that he can not paint over a square centimeters of the fence red, and he can not paint over b square centimeters green. Each board of the fence should be painted exactly one of the two colors. Perhaps Vasya won't need one of the colors.In addition, Vasya wants his fence to look smart. To do this, he should paint the fence so as to minimize the value that Vasya called the fence unattractiveness value. Vasya believes that two consecutive fence boards, painted different colors, look unattractive. The unattractiveness value of a fence is the total length of contact between the neighboring boards of various colors. To make the fence look nice, you need to minimize the value as low as possible. Your task is to find what is the minimum unattractiveness Vasya can get, if he paints his fence completely. The picture shows the fence, where the heights of boards (from left to right) are 2,3,2,4,3,1. The first and the fifth boards are painted red, the others are painted green. The first and the second boards have contact length 2, the fourth and fifth boards have contact length 3, the fifth and the sixth have contact length 1. Therefore, the unattractiveness of the given painted fence is 2+3+1=6.
|
The first line contains a single integer n (1 β€ n β€ 200) β the number of boards in Vasya's fence.The second line contains two integers a and b (0 β€ a, b β€ 4Β·104) β the area that can be painted red and the area that can be painted green, correspondingly.The third line contains a sequence of n integers h1, h2, ..., hn (1 β€ hi β€ 200) β the heights of the fence boards.All numbers in the lines are separated by single spaces.
|
Print a single number β the minimum unattractiveness value Vasya can get if he paints his fence completely. If it is impossible to do, print - 1.
|
Input: 45 73 3 4 1 | Output: 3
|
Medium
| 1 | 1,692 | 423 | 145 | 2 |
|
23 |
E
|
23E
|
E. Tree
| 2,500 |
dp
|
Recently Bob invented a new game with a tree (we should remind you, that a tree is a connected graph without cycles): he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree.
|
The first input line contains integer number n (1 β€ n β€ 700) β amount of vertices in the tree. The following n - 1 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, ai, bi (1 β€ ai, bi β€ n). It's guaranteed that the graph described in the input is a tree.
|
Output the only number β the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges.
|
Input: 51 22 33 44 5 | Output: 6
|
Expert
| 1 | 358 | 317 | 140 | 0 |
|
594 |
A
|
594A
|
A. Warrior and Archer
| 2,300 |
games
|
In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.Vova and Lesha are friends. They often meet at Vova's place and compete against each other in a computer game named The Ancient Papyri: Swordsink. Vova always chooses a warrior as his fighter and Leshac chooses an archer. After that they should choose initial positions for their characters and start the fight. A warrior is good at melee combat, so Vova will try to make the distance between fighters as small as possible. An archer prefers to keep the enemy at a distance, so Lesha will try to make the initial distance as large as possible.There are n (n is always even) possible starting positions for characters marked along the Ox axis. The positions are given by their distinct coordinates x1, x2, ..., xn, two characters cannot end up at the same position.Vova and Lesha take turns banning available positions, Vova moves first. During each turn one of the guys bans exactly one of the remaining positions. Banned positions cannot be used by both Vova and Lesha. They continue to make moves until there are only two possible positions remaining (thus, the total number of moves will be n - 2). After that Vova's character takes the position with the lesser coordinate and Lesha's character takes the position with the bigger coordinate and the guys start fighting.Vova and Lesha are already tired by the game of choosing positions, as they need to play it before every fight, so they asked you (the developer of the The Ancient Papyri: Swordsink) to write a module that would automatically determine the distance at which the warrior and the archer will start fighting if both Vova and Lesha play optimally.
|
The first line on the input contains a single integer n (2 β€ n β€ 200 000, n is even) β the number of positions available initially. The second line contains n distinct integers x1, x2, ..., xn (0 β€ xi β€ 109), giving the coordinates of the corresponding positions.
|
Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.
|
In the first sample one of the optimum behavior of the players looks like that: Vova bans the position at coordinate 15; Lesha bans the position at coordinate 3; Vova bans the position at coordinate 31; Lesha bans the position at coordinate 1. After these actions only positions 0 and 7 will remain, and the distance between them is equal to 7.In the second sample there are only two possible positions, so there will be no bans.
|
Input: 60 1 3 7 15 31 | Output: 7
|
Expert
| 1 | 1,932 | 263 | 134 | 5 |
1,353 |
B
|
1353B
|
B. Two Arrays And Swaps
| 800 |
greedy; sortings
|
You are given two arrays \(a\) and \(b\) both consisting of \(n\) positive (greater than zero) integers. You are also given an integer \(k\).In one move, you can choose two indices \(i\) and \(j\) (\(1 \le i, j \le n\)) and swap \(a_i\) and \(b_j\) (i.e. \(a_i\) becomes \(b_j\) and vice versa). Note that \(i\) and \(j\) can be equal or different (in particular, swap \(a_2\) with \(b_2\) or swap \(a_3\) and \(b_9\) both are acceptable moves).Your task is to find the maximum possible sum you can obtain in the array \(a\) if you can do no more than (i.e. at most) \(k\) such moves (swaps).You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 200\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(k\) (\(1 \le n \le 30; 0 \le k \le n\)) β the number of elements in \(a\) and \(b\) and the maximum number of moves you can do. The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 30\)), where \(a_i\) is the \(i\)-th element of \(a\). The third line of the test case contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 30\)), where \(b_i\) is the \(i\)-th element of \(b\).
|
For each test case, print the answer β the maximum possible sum you can obtain in the array \(a\) if you can do no more than (i.e. at most) \(k\) swaps.
|
In the first test case of the example, you can swap \(a_1 = 1\) and \(b_2 = 4\), so \(a=[4, 2]\) and \(b=[3, 1]\).In the second test case of the example, you don't need to swap anything.In the third test case of the example, you can swap \(a_1 = 1\) and \(b_1 = 10\), \(a_3 = 3\) and \(b_3 = 10\) and \(a_2 = 2\) and \(b_4 = 10\), so \(a=[10, 10, 10, 4, 5]\) and \(b=[1, 9, 3, 2, 9]\).In the fourth test case of the example, you cannot swap anything.In the fifth test case of the example, you can swap arrays \(a\) and \(b\), so \(a=[4, 4, 5, 4]\) and \(b=[1, 2, 2, 1]\).
|
Input: 5 2 1 1 2 3 4 5 5 5 5 6 6 5 1 2 5 4 3 5 3 1 2 3 4 5 10 9 10 10 9 4 0 2 2 4 3 2 4 2 3 4 4 1 2 2 1 4 4 5 4 | Output: 6 27 39 11 17
|
Beginner
| 2 | 640 | 634 | 152 | 13 |
1,028 |
C
|
1028C
|
C. Rectangles
| 1,600 |
geometry; implementation; sortings
|
You are given \(n\) rectangles on a plane with coordinates of their bottom left and upper right points. Some \((n-1)\) of the given \(n\) rectangles have some common point. A point belongs to a rectangle if this point is strictly inside the rectangle or belongs to its boundary.Find any point with integer coordinates that belongs to at least \((n-1)\) given rectangles.
|
The first line contains a single integer \(n\) (\(2 \le n \le 132\,674\)) β the number of given rectangles.Each the next \(n\) lines contains four integers \(x_1\), \(y_1\), \(x_2\) and \(y_2\) (\(-10^9 \le x_1 < x_2 \le 10^9\), \(-10^9 \le y_1 < y_2 \le 10^9\)) β the coordinates of the bottom left and upper right corners of a rectangle.
|
Print two integers \(x\) and \(y\) β the coordinates of any point that belongs to at least \((n-1)\) given rectangles.
|
The picture below shows the rectangles in the first and second samples. The possible answers are highlighted. The picture below shows the rectangles in the third and fourth samples.
|
Input: 30 0 1 11 1 2 23 0 4 1 | Output: 1 1
|
Medium
| 3 | 370 | 339 | 118 | 10 |
277 |
E
|
277E
|
E. Binary Tree on Plane
| 2,400 |
flows; trees
|
A root tree is a directed acyclic graph that contains one node (root), from which there is exactly one path to any other node.A root tree is binary if each node has at most two outgoing arcs.When a binary tree is painted on the plane, all arcs should be directed from top to bottom. That is, each arc going from u to v must meet the condition yu > yv.You've been given the coordinates of all tree nodes. Your task is to connect these nodes by arcs so as to get the binary root tree and make the total length of the arcs minimum. All arcs of the built tree must be directed from top to bottom.
|
The first line contains a single integer n (2 β€ n β€ 400) β the number of nodes in the tree. Then follow n lines, two integers per line: xi, yi (|xi|, |yi| β€ 103) β coordinates of the nodes. It is guaranteed that all points are distinct.
|
If it is impossible to build a binary root tree on the given points, print ""-1"". Otherwise, print a single real number β the total length of the arcs in the minimum binary tree. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.
|
Input: 30 01 02 1 | Output: 3.650281539872885
|
Expert
| 2 | 592 | 236 | 274 | 2 |
|
1,299 |
A
|
1299A
|
A. Anu Has a Function
| 1,500 |
brute force; greedy; math
|
Anu has created her own function \(f\): \(f(x, y) = (x | y) - y\) where \(|\) denotes the bitwise OR operation. For example, \(f(11, 6) = (11|6) - 6 = 15 - 6 = 9\). It can be proved that for any nonnegative numbers \(x\) and \(y\) value of \(f(x, y)\) is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems.A value of an array \([a_1, a_2, \dots, a_n]\) is defined as \(f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)\) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)). Elements of the array are not guaranteed to be different.
|
Output \(n\) integers, the reordering of the array with maximum value. If there are multiple answers, print any.
|
In the first testcase, value of the array \([11, 6, 4, 0]\) is \(f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9\).\([11, 4, 0, 6]\) is also a valid answer.
|
Input: 4 4 0 11 6 | Output: 11 6 4 0
|
Medium
| 3 | 743 | 219 | 112 | 12 |
2,043 |
F
|
2043F
|
F. Nim
| 2,700 |
bitmasks; brute force; combinatorics; dp; games; greedy; implementation; shortest paths
|
Recall the rules of the game ""Nim"". There are \(n\) piles of stones, where the \(i\)-th pile initially contains some number of stones. Two players take turns choosing a non-empty pile and removing any positive (strictly greater than \(0\)) number of stones from it. The player unable to make a move loses the game.You are given an array \(a\), consisting of \(n\) integers. Artem and Ruslan decided to play Nim on segments of this array. Each of the \(q\) rounds is defined by a segment \((l_i, r_i)\), where the elements \(a_{l_i}, a_{l_i+1}, \dots, a_{r_i}\) represent the sizes of the piles of stones.Before the game starts, Ruslan can remove any number of piles from the chosen segment. However, at least one pile must remain, so in a single round he can remove at most \((r_i - l_i)\) piles. He is allowed to remove \(0\) piles. After the removal, the game is played on the remaining piles within the segment.All rounds are independent: the changes made in one round do not affect the original array or any other rounds.Ruslan wants to remove as many piles as possible so that Artem, who always makes the first move, loses.For each round, determine: the maximum number of piles Ruslan can remove; the number of ways to choose the maximum number of piles for removal. Two ways are considered different if there exists an index \(i\) such that the pile at index \(i\) is removed in one way but not in the other. Since the number of ways can be large, output it modulo \(998\,244\,353\).If Ruslan cannot ensure Artem's loss in a particular round, output -1 for that round.
|
The first line of input contains two integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\)) β the size of the array and the number of segments for which the answers need to be calculated.The second line of input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 50\)) β the elements of the initial array.The \(i\)-th of the next \(q\) lines contains two integers \(l_i, r_i\) (\(1 \le l_i \le r_i \le n\)) β the bounds of the segment on which the boys want to play the game during the \(i\)-th round.
|
For each round: if Ruslan can win, print two integers β the maximum number of piles that can be removed, and the number of ways to remove the maximum number of piles, taken modulo \(998\,244\,353\); otherwise print -1.
|
Input: 9 50 1 2 1 3 4 5 6 01 52 53 54 51 9 | Output: 4 1 2 1 0 1 -1 8 2
|
Master
| 8 | 1,576 | 512 | 218 | 20 |
|
1,829 |
E
|
1829E
|
E. The Lakes
| 1,100 |
dfs and similar; dsu; graphs; implementation
|
You are given an \(n \times m\) grid \(a\) of non-negative integers. The value \(a_{i,j}\) represents the depth of water at the \(i\)-th row and \(j\)-th column. A lake is a set of cells such that: each cell in the set has \(a_{i,j} > 0\), and there exists a path between any pair of cells in the lake by going up, down, left, or right a number of times and without stepping on a cell with \(a_{i,j} = 0\). The volume of a lake is the sum of depths of all the cells in the lake.Find the largest volume of a lake in the grid.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n, m\) (\(1 \leq n, m \leq 1000\)) β the number of rows and columns of the grid, respectively.Then \(n\) lines follow each with \(m\) integers \(a_{i,j}\) (\(0 \leq a_{i,j} \leq 1000\)) β the depth of the water at each cell.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(10^6\).
|
For each test case, output a single integer β the largest volume of a lake in the grid.
|
Input: 53 31 2 03 4 00 0 51 103 30 1 11 0 11 1 15 51 1 1 1 11 0 0 0 11 0 5 0 11 0 0 0 11 1 1 1 15 51 1 1 1 11 0 0 0 11 1 4 0 11 0 0 0 11 1 1 1 1 | Output: 10 0 7 16 21
|
Easy
| 4 | 524 | 472 | 87 | 18 |
|
331 |
C3
|
331C3
|
C3. The Great Julya Calendar
| 2,500 |
dp
|
Yet another Armageddon is coming! This time the culprit is the Julya tribe calendar. The beavers in this tribe knew math very well. Smart Beaver, an archaeologist, got a sacred plate with a magic integer on it. The translation from Old Beaverish is as follows: ""May the Great Beaver bless you! May your chacres open and may your third eye never turn blind from beholding the Truth! Take the magic number, subtract a digit from it (the digit must occur in the number) and get a new magic number. Repeat this operation until a magic number equals zero. The Earth will stand on Three Beavers for the time, equal to the number of subtractions you perform!""Distinct subtraction sequences can obviously get you different number of operations. But the Smart Beaver is ready to face the worst and is asking you to count the minimum number of operations he needs to reduce the magic number to zero.
|
The single line contains the magic integer n, 0 β€ n. to get 20 points, you need to solve the problem with constraints: n β€ 106 (subproblem C1); to get 40 points, you need to solve the problem with constraints: n β€ 1012 (subproblems C1+C2); to get 100 points, you need to solve the problem with constraints: n β€ 1018 (subproblems C1+C2+C3).
|
Print a single integer β the minimum number of subtractions that turns the magic number to a zero.
|
In the first test sample the minimum number of operations can be reached by the following sequence of subtractions: 24 β 20 β 18 β 10 β 9 β 0
|
Input: 24 | Output: 5
|
Expert
| 1 | 891 | 339 | 98 | 3 |
1,415 |
B
|
1415B
|
B. Repainting Street
| 1,100 |
brute force; brute force; greedy
|
There is a street with \(n\) houses in a line, numbered from \(1\) to \(n\). The house \(i\) is initially painted in color \(c_i\). The street is considered beautiful if all houses are painted in the same color. Tom, the painter, is in charge of making the street beautiful. Tom's painting capacity is defined by an integer, let's call it \(k\).On one day, Tom can do the following repainting process that consists of two steps: He chooses two integers \(l\) and \(r\) such that \( 1 \le l \le r \le n \) and \( r - l + 1 = k \). For each house \(i\) such that \(l \le i \le r\), he can either repaint it with any color he wants, or ignore it and let it keep its current color. Note that in the same day Tom can use different colors to repaint different houses.Tom wants to know the minimum number of days needed to repaint the street so that it becomes beautiful.
|
The first line of input contains a single integer \(t\) (\( 1 \le t \le 10^4\)), the number of test cases. Description of test cases follows.In the first line of a test case description there are two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)).The second line contains \(n\) space-separated integers. The \(i\)-th of these integers represents \(c_i\) (\(1 \le c_i \le 100\)), the color which house \(i\) is initially painted in.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
Print \(t\) lines, each with one integer: the minimum number of days Tom needs to make the street beautiful for each test case.
|
In the first test case Tom should paint houses 1 and 2 in the first day in color 2, houses 5 and 6 in the second day in color 2, and the last house in color 2 on the third day.In the second test case Tom can, for example, spend 6 days to paint houses 1, 2, 4, 5, 6, 7 in color 3.In the third test case Tom can paint the first house in the first day and houses 6, 7, and 8 in the second day in color 3.
|
Input: 3 10 2 1 1 2 2 1 1 2 2 2 1 7 1 1 2 3 4 5 6 7 10 3 1 3 3 3 3 1 2 1 3 3 | Output: 3 6 2
|
Easy
| 3 | 864 | 522 | 127 | 14 |
370 |
C
|
370C
|
C. Mittens
| 1,800 |
constructive algorithms; greedy; sortings
|
A Christmas party in city S. had n children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to m, and the children are numbered from 1 to n. Then the i-th child has both mittens of color ci.The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children.The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten.
|
The first line contains two integers n and m β the number of the children and the number of possible mitten colors (1 β€ n β€ 5000, 1 β€ m β€ 100). The second line contains n integers c1, c2, ... cn, where ci is the color of the mittens of the i-th child (1 β€ ci β€ m).
|
In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next n lines print the way the mittens can be distributed in this case. On the i-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the i-th child will get. If there are multiple solutions, you can print any of them.
|
Input: 6 31 3 2 2 1 1 | Output: 62 11 22 11 31 23 1
|
Medium
| 3 | 1,410 | 264 | 394 | 3 |
|
1,519 |
F
|
1519F
|
F. Chests and Keys
| 3,200 |
bitmasks; brute force; dfs and similar; dp; flows
|
Alice and Bob play a game. Alice has got \(n\) treasure chests (the \(i\)-th of which contains \(a_i\) coins) and \(m\) keys (the \(j\)-th of which she can sell Bob for \(b_j\) coins).Firstly, Alice puts some locks on the chests. There are \(m\) types of locks, the locks of the \(j\)-th type can only be opened with the \(j\)-th key. To put a lock of type \(j\) on the \(i\)-th chest, Alice has to pay \(c_{i,j}\) dollars. Alice can put any number of different types of locks on each chest (possibly, zero).Then, Bob buys some of the keys from Alice (possibly none, possibly all of them) and opens each chest he can (he can open a chest if he has the keys for all of the locks on this chest). Bob's profit is the difference between the total number of coins in the opened chests and the total number of coins he spends buying keys from Alice. If Bob's profit is strictly positive (greater than zero), he wins the game. Otherwise, Alice wins the game.Alice wants to put some locks on some chests so no matter which keys Bob buys, she always wins (Bob cannot get positive profit). Of course, she wants to spend the minimum possible number of dollars on buying the locks. Help her to determine whether she can win the game at all, and if she can, how many dollars she has to spend on the locks.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 6\)) β the number of chests and the number of keys, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 4\)), where \(a_i\) is the number of coins in the \(i\)-th chest.The third line contains \(m\) integers \(b_1, b_2, \dots, b_m\) (\(1 \le b_j \le 4\)), where \(b_j\) is the number of coins Bob has to spend to buy the \(j\)-th key from Alice.Then \(n\) lines follow. The \(i\)-th of them contains \(m\) integers \(c_{i,1}, c_{i,2}, \dots, c_{i,m}\) (\(1 \le c_{i,j} \le 10^7\)), where \(c_{i,j}\) is the number of dollars Alice has to spend to put a lock of the \(j\)-th type on the \(i\)-th chest.
|
If Alice cannot ensure her victory (no matter which locks she puts on which chests, Bob always has a way to gain positive profit), print \(-1\).Otherwise, print one integer β the minimum number of dollars Alice has to spend to win the game regardless of Bob's actions.
|
In the first example, Alice should put locks of types \(1\) and \(3\) on the first chest, and locks of type \(2\) and \(3\) on the second chest.In the second example, Alice should put locks of types \(1\) and \(2\) on the first chest, and a lock of type \(3\) on the second chest.
|
Input: 2 3 3 3 1 1 4 10 20 100 20 15 80 | Output: 205
|
Master
| 5 | 1,292 | 715 | 268 | 15 |
818 |
E
|
818E
|
E. Card Game Again
| 1,900 |
binary search; data structures; number theory; two pointers
|
Vova again tries to play some computer card game.The rules of deck creation in this game are simple. Vova is given an existing deck of n cards and a magic number k. The order of the cards in the deck is fixed. Each card has a number written on it; number ai is written on the i-th card in the deck.After receiving the deck and the magic number, Vova removes x (possibly x = 0) cards from the top of the deck, y (possibly y = 0) cards from the bottom of the deck, and the rest of the deck is his new deck (Vova has to leave at least one card in the deck after removing cards). So Vova's new deck actually contains cards x + 1, x + 2, ... n - y - 1, n - y from the original deck.Vova's new deck is considered valid iff the product of all numbers written on the cards in his new deck is divisible by k. So Vova received a deck (possibly not a valid one) and a number k, and now he wonders, how many ways are there to choose x and y so the deck he will get after removing x cards from the top and y cards from the bottom is valid?
|
The first line contains two integers n and k (1 β€ n β€ 100 000, 1 β€ k β€ 109).The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the numbers written on the cards.
|
Print the number of ways to choose x and y so the resulting deck is valid.
|
In the first example the possible values of x and y are: x = 0, y = 0; x = 1, y = 0; x = 2, y = 0; x = 0, y = 1.
|
Input: 3 46 2 8 | Output: 4
|
Hard
| 4 | 1,026 | 178 | 74 | 8 |
1,006 |
B
|
1006B
|
B. Polycarp's Practice
| 1,200 |
greedy; implementation; sortings
|
Polycarp is practicing his problem solving skill. He has a list of \(n\) problems with difficulties \(a_1, a_2, \dots, a_n\), respectively. His plan is to practice for exactly \(k\) days. Each day he has to solve at least one problem from his list. Polycarp solves the problems in the order they are given in his list, he cannot skip any problem from his list. He has to solve all \(n\) problems in exactly \(k\) days.Thus, each day Polycarp solves a contiguous sequence of (consecutive) problems from the start of the list. He can't skip problems or solve them multiple times. As a result, in \(k\) days he will solve all the \(n\) problems.The profit of the \(j\)-th day of Polycarp's practice is the maximum among all the difficulties of problems Polycarp solves during the \(j\)-th day (i.e. if he solves problems with indices from \(l\) to \(r\) during a day, then the profit of the day is \(\max\limits_{l \le i \le r}a_i\)). The total profit of his practice is the sum of the profits over all \(k\) days of his practice.You want to help Polycarp to get the maximum possible total profit over all valid ways to solve problems. Your task is to distribute all \(n\) problems between \(k\) days satisfying the conditions above in such a way, that the total profit is maximum.For example, if \(n = 8, k = 3\) and \(a = [5, 4, 2, 6, 5, 1, 9, 2]\), one of the possible distributions with maximum total profit is: \([5, 4, 2], [6, 5], [1, 9, 2]\). Here the total profit equals \(5 + 6 + 9 = 20\).
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2000\)) β the number of problems and the number of days, respectively.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2000\)) β difficulties of problems in Polycarp's list, in the order they are placed in the list (i.e. in the order Polycarp will solve them).
|
In the first line of the output print the maximum possible total profit.In the second line print exactly \(k\) positive integers \(t_1, t_2, \dots, t_k\) (\(t_1 + t_2 + \dots + t_k\) must equal \(n\)), where \(t_j\) means the number of problems Polycarp will solve during the \(j\)-th day in order to achieve the maximum possible total profit of his practice.If there are many possible answers, you may print any of them.
|
The first example is described in the problem statement.In the second example there is only one possible distribution.In the third example the best answer is to distribute problems in the following way: \([1, 2000], [2000, 2]\). The total profit of this distribution is \(2000 + 2000 = 4000\).
|
Input: 8 35 4 2 6 5 1 9 2 | Output: 203 2 3
|
Easy
| 3 | 1,495 | 393 | 421 | 10 |
158 |
A
|
158A
|
A. Next Round
| 800 |
*special; implementation
|
""Contestant who earns a score equal to or greater than the k-th place finisher's score will advance to the next round, as long as the contestant earns a positive score..."" β an excerpt from contest rules.A total of n participants took part in the contest (n β₯ k), and you already know their scores. Calculate how many participants will advance to the next round.
|
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 50) separated by a single space.The second line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 100), where ai is the score earned by the participant who got the i-th place. The given sequence is non-increasing (that is, for all i from 1 to n - 1 the following condition is fulfilled: ai β₯ ai + 1).
|
Output the number of participants who advance to the next round.
|
In the first example the participant on the 5th place earned 7 points. As the participant on the 6th place also earned 7 points, there are 6 advancers.In the second example nobody got a positive score.
|
Input: 8 510 9 8 7 7 7 5 5 | Output: 6
|
Beginner
| 2 | 364 | 383 | 64 | 1 |
2,087 |
H
|
2087H
|
H. Nim with Special Numbers
| 0 |
*special; *special
|
Let's recall the rules of the game ""Nim"". The game is played by two players who take turns. They have a certain number of piles of stones (the piles can be of the same size or different sizes; the size is defined as the number of stones in a pile). On their turn, a player must choose any non-empty pile of stones and remove any number of stones from it (at least one). The player who cannot make a move loses.In this problem, a modified version of the game will be used. There is a special set of numbers \(S\) that prohibits certain moves. For each number \(s_i\) in this set, if the current size of the pile of stones is strictly greater than \(s_i\), it cannot be made strictly less than \(s_i\) in one move. In other words, if the current size of the pile is \(x\), and the player tries to make a move that results in a size of \(y\), and \(x > s_i > y\) for some number \(s_i \in S\), such a move is not allowed.Your task is as follows. Initially, the set \(S\) is empty. You need to process queries of three types: add a certain number \(s_i\) to the set; remove a certain number \(s_i\) from the set; determine who will win if this modified version of Nim is played with piles of stones of sizes \(a_1, a_2, \dots, a_k\). Assume that both players play optimally.
|
The first line contains a single integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.Each query is given in one line in one of the following formats: \(1\) \(s_i\) (\(1 \le s_i \le 3 \cdot 10^5\)) β if the number \(s_i\) is in the set \(S\), remove it; otherwise, add it to \(S\); \(2\) \(k_i\) \(a_{i,1}\) \(a_{i,2}\) ... \(a_{i,k_i}\) (\(1 \le k_i \le 3\); \(1 \le a_{i,j} \le 3 \cdot 10^5\)) β determine who will win the game if it uses \(k_i\) piles of stones with sizes \(a_{i,1}, a_{i,2}, \dots, a_{i,k_i}\).
|
For each query of type \(2\), output First if the player making the first move will win, or Second if they will lose.
|
Input: 112 2 1 41 22 2 1 42 3 3 1 41 32 3 3 1 42 2 1 41 42 2 1 51 22 2 1 5 | Output: First Second Second Second Second First Second
|
Beginner
| 2 | 1,272 | 531 | 117 | 20 |
|
2,089 |
B2
|
2089B2
|
B2. Canteen (Hard Version)
| 2,300 |
binary search; data structures; dp; flows; greedy; two pointers
|
This is the hard version of the problem. The difference between the versions is that in this version, there are no additional limits on \(k\). You can hack only if you solved all versions of this problem. Ecrade has two sequences \(a_0, a_1, \ldots, a_{n - 1}\) and \(b_0, b_1, \ldots, b_{n - 1}\) consisting of integers. It is guaranteed that the sum of all elements in \(a\) does not exceed the sum of all elements in \(b\).Initially, Ecrade can make exactly \(k\) changes to the sequence \(a\). It is guaranteed that \(k\) does not exceed the sum of \(a\). In each change: Choose an integer \(i\) (\(0 \le i < n\)) such that \(a_i > 0\), and perform \(a_i := a_i - 1\). Then Ecrade will perform the following three operations sequentially on \(a\) and \(b\), which constitutes one round of operations: For each \(0 \le i < n\): \(t := \min(a_i, b_i), a_i := a_i - t, b_i := b_i - t\); For each \(0 \le i < n\): \(c_i := a_{(i - 1) \bmod n}\); For each \(0 \le i < n\): \(a_i := c_i\); Ecrade wants to know the minimum number of rounds required for all elements in \(a\) to become equal to \(0\) after exactly \(k\) changes to \(a\).However, this seems a bit complicated, so please help him!
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(k\) (\(1\le n\le 2\cdot 10^5\), \(0\le k\le 2\cdot 10^{14}\)).The second line of each test case contains \(n\) integers \(a_0, a_1, \ldots, a_{n - 1}\) (\(1 \le a_i \le 10^9\)).The third line of each test case contains \(n\) integers \(b_0, b_1, \ldots, b_{n - 1}\) (\(1 \le b_i \le 10^9\)).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2\cdot 10^5\). It is also guaranteed that in each test case the sum of \(a\) does not exceed the sum of \(b\), and that \(k\) does not exceed the sum of \(a\).
|
For each test case, output the minimum number of rounds required for all elements in \(a\) to become equal to \(0\) after exactly \(k\) changes to \(a\).
|
In the fifth test case, all elements in \(a\) become \(0\) after exactly \(6\) changes.In the sixth test case, Ecrade can do exactly one change to \(a_3\), then \(a\) will become \([1,2,2,4]\). After the first round, \(a=[3,0,0,0],b=[3,1,0,0]\); After the second round, \(a=[0,0,0,0],b=[0,1,0,0]\). In the seventh test case, Ecrade can do exactly one change to \(a_4\), then \(a\) will become \([2,1,1,1]\). After the first round, \(a=[0,1,0,0],b=[0,1,1,0]\); After the second round, \(a=[0,0,0,0],b=[0,0,1,0]\).
|
Input: 83 01 1 45 1 44 01 2 3 44 3 2 14 02 1 1 21 2 2 18 01 2 3 4 5 6 7 88 7 6 5 4 3 2 13 61 1 45 1 44 11 2 3 44 3 2 14 12 1 1 21 2 2 14 22 1 1 21 2 2 1 | Output: 1 4 4 8 0 2 2 1
|
Expert
| 6 | 1,193 | 762 | 153 | 20 |
155 |
A
|
155A
|
A. I_love_%username%
| 800 |
brute force
|
Vasya adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him.One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress of his coolness. For each contest where this coder participated, he wrote out a single non-negative number β the number of points his favorite coder earned in the contest. Vasya wrote out the points for the contest in the order, in which the contests run (naturally, no two contests ran simultaneously).Vasya considers a coder's performance in a contest amazing in two situations: he can break either his best or his worst performance record. First, it is amazing if during the contest the coder earns strictly more points that he earned on each past contest. Second, it is amazing if during the contest the coder earns strictly less points that he earned on each past contest. A coder's first contest isn't considered amazing. Now he wants to count the number of amazing performances the coder had throughout his whole history of participating in contests. But the list of earned points turned out long and Vasya can't code... That's why he asks you to help him.
|
The first line contains the single integer n (1 β€ n β€ 1000) β the number of contests where the coder participated.The next line contains n space-separated non-negative integer numbers β they are the points which the coder has earned. The points are given in the chronological order. All points do not exceed 10000.
|
Print the single number β the number of amazing performances the coder has had during his whole history of participating in the contests.
|
In the first sample the performances number 2 and 3 are amazing.In the second sample the performances number 2, 4, 9 and 10 are amazing.
|
Input: 5100 50 200 150 200 | Output: 2
|
Beginner
| 1 | 1,259 | 314 | 137 | 1 |
1,437 |
G
|
1437G
|
G. Death DBMS
| 2,600 |
data structures; string suffix structures; strings; trees
|
For the simplicity, let's say that the ""Death Note"" is a notebook that kills a person when their name is written in it.It's easy to kill with it, but it's pretty hard to keep track of people you haven't killed and still plan to. You decided to make a ""Death Database Management System"" β a computer program that provides the easy access to the database of possible victims. Let me describe its specifications to you.Let's define a victim entity: a victim has a name (not necessarily unique) that consists only of lowercase Latin letters and an integer suspicion value.At the start of the program the user enters a list of \(n\) victim names into a database, each suspicion value is set to \(0\).Then the user makes queries of two types: \(1~i~x\) β set the suspicion value of the \(i\)-th victim to \(x\); \(2~q\) β given a string \(q\) find the maximum suspicion value of a victim whose name is a contiguous substring of \(q\). Just to remind you, this program doesn't kill people, it only helps to search for the names to write down in an actual notebook. Thus, the list of the victims in the database doesn't change throughout the queries.What are you waiting for? Write that program now!
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\)) β the number of victims and the number of queries, respectively.Each of the next \(n\) lines contains a single string \(s_i\) β the name of the \(i\)-th victim. Each name consists only of lowercase Latin letters.Each of the next \(m\) lines contains a query of one of two types: \(1~i~x\) (\(1 \le i \le n\), \(0 \le x \le 10^9\)) β change the suspicion value of the \(i\)-th victim to \(x\); \(2~q\) β given a string \(q\) consisting only of lowercase Latin letters find the maximum suspicion value of a victim whose name is a contiguous substring of \(q\). There is at least one query of the second type. The total length of the strings \(s_i\) doesn't exceed \(3 \cdot 10^5\). The total length of the strings \(q\) doesn't exceed \(3 \cdot 10^5\).
|
For each query of the second type print an integer value. If there is no victim name that is a contiguous substring of \(q\), then print \(-1\). Otherwise, print the maximum suspicion value of a victim whose name is a contiguous substring of \(q\).
|
Input: 5 8 kurou takuo takeshi naomi shingo 2 nakiraomi 2 abanaomicaba 1 3 943 2 takuotakeshishingo 1 5 135832 2 shingotakeshi 1 5 0 2 shingotakeshi | Output: -1 0 943 135832 943
|
Expert
| 4 | 1,195 | 837 | 248 | 14 |
|
538 |
G
|
538G
|
G. Berserk Robot
| 3,100 |
constructive algorithms; math; sortings
|
Help! A robot escaped our lab and we need help finding it. The lab is at the point (0, 0) of the coordinate plane, at time 0 the robot was there. The robot's movements are defined by a program β a string of length l, consisting of characters U, L, D, R. Each second the robot executes the next command in his program: if the current coordinates of the robot are (x, y), then commands U, L, D, R move it to cells (x, y + 1), (x - 1, y), (x, y - 1), (x + 1, y) respectively. The execution of the program started at time 0. The program is looped, i.e. each l seconds of executing the program start again from the first character. Unfortunately, we don't know what program was loaded into the robot when he left the lab.Our radars managed to find out the position of the robot at n moments of time: we know that at the moment of time ti the robot is at the point (xi, yi). Given this data, either help to determine what program could be loaded into the robot, or determine that no possible program meets the data and the robot must have broken down.
|
The first line of the input contains two space-separated integers n and l (1 β€ n β€ 2Β·105, 1 β€ l β€ 2Β·106).Next n lines contain three space-separated integers β ti, xi, yi (1 β€ ti β€ 1018, - 1018 β€ xi, yi β€ 1018). The radar data is given chronologically, i.e. ti < ti + 1 for all i from 1 to n - 1.
|
Print any of the possible programs that meet the data. If no program meets the data, print a single word 'NO' (without the quotes).
|
Input: 3 31 1 02 1 -13 0 -1 | Output: RDL
|
Master
| 3 | 1,045 | 295 | 131 | 5 |
|
1,747 |
D
|
1747D
|
D. Yet Another Problem
| 1,900 |
binary search; bitmasks; constructive algorithms; data structures
|
You are given an array \(a\) of \(n\) integers \(a_1, a_2, a_3, \ldots, a_n\).You have to answer \(q\) independent queries, each consisting of two integers \(l\) and \(r\). Consider the subarray \(a[l:r]\) \(=\) \([a_l, a_{l+1}, \ldots, a_r]\). You can apply the following operation to the subarray any number of times (possibly zero)- Choose two integers \(L\), \(R\) such that \(l \le L \le R \le r\) and \(R - L + 1\) is odd. Replace each element in the subarray from \(L\) to \(R\) with the XOR of the elements in the subarray \([L, R]\). The answer to the query is the minimum number of operations required to make all elements of the subarray \(a[l:r]\) equal to \(0\) or \(-1\) if it is impossible to make all of them equal to \(0\). You can find more details about XOR operation here.
|
The first line contains two integers \(n\) and \(q\) \((1 \le n, q \le 2 \cdot 10^5)\) β the length of the array \(a\) and the number of queries.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((0 \le a_i \lt 2^{30})\) β the elements of the array \(a\).The \(i\)-th of the next \(q\) lines contains two integers \(l_i\) and \(r_i\) \((1 \le l_i \le r_i \le n)\) β the description of the \(i\)-th query.
|
For each query, output a single integer β the answer to that query.
|
In the first query, \(l = 3, r = 4\), subarray = \([3, 3]\). We can apply operation only to the subarrays of length \(1\), which won't change the array; hence it is impossible to make all elements equal to \(0\).In the second query, \(l = 4, r = 6\), subarray = \([3, 1, 2]\). We can choose the whole subarray \((L = 4, R = 6)\) and replace all elements by their XOR \((3 \oplus 1 \oplus 2) = 0\), making the subarray \([0, 0, 0]\).In the fifth query, \(l = 1, r = 6\), subarray = \([3, 0, 3, 3, 1, 2]\). We can make the operations as follows: Choose \(L = 4, R = 6\), making the subarray \([3, 0, 3, 0, 0, 0]\). Choose \(L = 1, R = 5\), making the subarray \([0, 0, 0, 0, 0, 0]\).
|
Input: 7 6 3 0 3 3 1 2 3 3 4 4 6 3 7 5 6 1 6 2 2 | Output: -1 1 1 -1 2 0
|
Hard
| 4 | 792 | 419 | 67 | 17 |
1,990 |
A
|
1990A
|
A. Submission Bait
| 900 |
brute force; games; greedy; sortings
|
Alice and Bob are playing a game in an array \(a\) of size \(n\).They take turns to do operations, with Alice starting first. The player who can not operate will lose. At first, a variable \(mx\) is set to \(0\).In one operation, a player can do: Choose an index \(i\) (\(1 \le i \le n\)) such that \(a_{i} \geq mx\) and set \(mx\) to \(a_{i}\). Then, set \(a_{i}\) to \(0\).Determine whether Alice has a winning strategy.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases.For each test case: The first line contains an integer \(n\) (\(2 \leq n \leq 50\)) β the size of the array. The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)) β the elements of the array.
|
For each test case, if Alice has a winning strategy, output ""YES"". Otherwise, output ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, Alice can choose \(i=1\) since \(a_1=2 \ge mx=0\).After Alice's operation, \(a=[0,1]\) and \(mx=2\). Bob can not do any operation. Alice wins.In the second test case, Alice doesn't have a winning strategy.For example, if Alice chooses \(i=1\), after Alice's operation: \(a=[0,1]\) and \(mx=1\). Then, Bob can choose \(i=2\) since \(a_2=1 \ge mx=1\). After Bob's operation: \(a=[0,0]\) and \(mx=1\). Alice can not do any operation. Bob wins.
|
Input: 522 121 133 3 343 3 4 441 2 2 2 | Output: YES NO YES NO YES
|
Beginner
| 4 | 422 | 320 | 255 | 19 |
1,114 |
B
|
1114B
|
B. Yet Another Array Partitioning Task
| 1,500 |
constructive algorithms; greedy; sortings
|
An array \(b\) is called to be a subarray of \(a\) if it forms a continuous subsequence of \(a\), that is, if it is equal to \(a_l\), \(a_{l + 1}\), \(\ldots\), \(a_r\) for some \(l, r\).Suppose \(m\) is some known constant. For any array, having \(m\) or more elements, let's define it's beauty as the sum of \(m\) largest elements of that array. For example: For array \(x = [4, 3, 1, 5, 2]\) and \(m = 3\), the \(3\) largest elements of \(x\) are \(5\), \(4\) and \(3\), so the beauty of \(x\) is \(5 + 4 + 3 = 12\). For array \(x = [10, 10, 10]\) and \(m = 2\), the beauty of \(x\) is \(10 + 10 = 20\).You are given an array \(a_1, a_2, \ldots, a_n\), the value of the said constant \(m\) and an integer \(k\). Your need to split the array \(a\) into exactly \(k\) subarrays such that: Each element from \(a\) belongs to exactly one subarray. Each subarray has at least \(m\) elements. The sum of all beauties of \(k\) subarrays is maximum possible.
|
The first line contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m\), \(2 \le k\), \(m \cdot k \le n\)) β the number of elements in \(a\), the constant \(m\) in the definition of beauty and the number of subarrays to split to.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)).
|
In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition.In the second line, print \(k-1\) integers \(p_1, p_2, \ldots, p_{k-1}\) (\(1 \le p_1 < p_2 < \ldots < p_{k-1} < n\)) representing the partition of the array, in which: All elements with indices from \(1\) to \(p_1\) belong to the first subarray. All elements with indices from \(p_1 + 1\) to \(p_2\) belong to the second subarray. \(\ldots\). All elements with indices from \(p_{k-1} + 1\) to \(n\) belong to the last, \(k\)-th subarray.If there are several optimal partitions, print any of them.
|
In the first example, one of the optimal partitions is \([5, 2, 5]\), \([2, 4]\), \([1, 1, 3, 2]\). The beauty of the subarray \([5, 2, 5]\) is \(5 + 5 = 10\). The beauty of the subarray \([2, 4]\) is \(2 + 4 = 6\). The beauty of the subarray \([1, 1, 3, 2]\) is \(3 + 2 = 5\). The sum of their beauties is \(10 + 6 + 5 = 21\).In the second example, one optimal partition is \([4]\), \([1, 3]\), \([2, 2]\), \([3]\).
|
Input: 9 2 3 5 2 5 2 4 1 1 3 2 | Output: 21 3 5
|
Medium
| 3 | 953 | 357 | 605 | 11 |
142 |
E
|
142E
|
E. Help Greg the Dwarf 2
| 3,000 |
geometry
|
Greg the Dwarf has been really busy recently with excavations by the Neverland Mountain. However for the well-known reasons (as you probably remember he is a very unusual dwarf and he cannot stand sunlight) Greg can only excavate at night. And in the morning he should be in his crypt before the first sun ray strikes. That's why he wants to find the shortest route from the excavation point to his crypt. Greg has recollected how the Codeforces participants successfully solved the problem of transporting his coffin to a crypt. So, in some miraculous way Greg appeared in your bedroom and asks you to help him in a highly persuasive manner. As usual, you didn't feel like turning him down.After some thought, you formalized the task as follows: as the Neverland mountain has a regular shape and ends with a rather sharp peak, it can be represented as a cone whose base radius equals r and whose height equals h. The graveyard where Greg is busy excavating and his crypt can be represented by two points on the cone's surface. All you've got to do is find the distance between points on the cone's surface. The task is complicated by the fact that the mountain's base on the ground level and even everything below the mountain has been dug through by gnome (one may wonder whether they've been looking for the same stuff as Greg...). So, one can consider the shortest way to pass not only along the side surface, but also along the cone's base (and in a specific case both points can lie on the cone's base β see the first sample test)Greg will be satisfied with the problem solution represented as the length of the shortest path between two points β he can find his way pretty well on his own. He gave you two hours to solve the problem and the time is ticking!
|
The first input line contains space-separated integers r and h (1 β€ r, h β€ 1000) β the base radius and the cone height correspondingly. The second and third lines contain coordinates of two points on the cone surface, groups of three space-separated real numbers. The coordinates of the points are given in the systems of coordinates where the origin of coordinates is located in the centre of the cone's base and its rotation axis matches the OZ axis. In this coordinate system the vertex of the cone is located at the point (0, 0, h), the base of the cone is a circle whose center is at the point (0, 0, 0), lying on the XOY plane, and all points on the cone surface have a non-negative coordinate z. It is guaranteed that the distances from the points to the cone surface do not exceed 10 - 12. All real numbers in the input have no more than 16 digits after decimal point.
|
Print the length of the shortest path between the points given in the input, with absolute or relative error not exceeding 10 - 6.
|
Input: 2 21.0 0.0 0.0-1.0 0.0 0.0 | Output: 2.000000000
|
Master
| 1 | 1,764 | 876 | 130 | 1 |
|
1,552 |
F
|
1552F
|
F. Telepanting
| 2,200 |
binary search; data structures; dp; sortings
|
An ant moves on the real line with constant speed of \(1\) unit per second. It starts at \(0\) and always moves to the right (so its position increases by \(1\) each second).There are \(n\) portals, the \(i\)-th of which is located at position \(x_i\) and teleports to position \(y_i < x_i\). Each portal can be either active or inactive. The initial state of the \(i\)-th portal is determined by \(s_i\): if \(s_i=0\) then the \(i\)-th portal is initially inactive, if \(s_i=1\) then the \(i\)-th portal is initially active. When the ant travels through a portal (i.e., when its position coincides with the position of a portal): if the portal is inactive, it becomes active (in this case the path of the ant is not affected); if the portal is active, it becomes inactive and the ant is instantly teleported to the position \(y_i\), where it keeps on moving as normal. How long (from the instant it starts moving) does it take for the ant to reach the position \(x_n + 1\)? It can be shown that this happens in a finite amount of time. Since the answer may be very large, compute it modulo \(998\,244\,353\).
|
The first line contains the integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β the number of portals.The \(i\)-th of the next \(n\) lines contains three integers \(x_i\), \(y_i\) and \(s_i\) (\(1\le y_i < x_i\le 10^9\), \(s_i\in\{0,1\}\)) β the position of the \(i\)-th portal, the position where the ant is teleported when it travels through the \(i\)-th portal (if it is active), and the initial state of the \(i\)-th portal.The positions of the portals are strictly increasing, that is \(x_1<x_2<\cdots<x_n\). It is guaranteed that the \(2n\) integers \(x_1, \, x_2, \, \dots, \, x_n, \, y_1, \, y_2, \, \dots, \, y_n\) are all distinct.
|
Output the amount of time elapsed, in seconds, from the instant the ant starts moving to the instant it reaches the position \(x_n+1\). Since the answer may be very large, output it modulo \(998\,244\,353\).
|
Explanation of the first sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed \(1\) and the time spent during the movement is written above the arrow). $$$\( 0 \stackrel{6}{\longrightarrow} 6 \leadsto 5 \stackrel{3}{\longrightarrow} 8 \leadsto 1 \stackrel{2}{\longrightarrow} 3 \leadsto 2 \stackrel{4}{\longrightarrow} 6 \leadsto 5 \stackrel{2}{\longrightarrow} 7 \leadsto 4 \stackrel{2}{\longrightarrow} 6 \leadsto 5 \stackrel{4}{\longrightarrow} 9 \)\( Notice that the total time is \)6+3+2+4+2+2+4=23\(.Explanation of the second sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed \)1\( and the time spent during the movement is written above the arrow). \)\( 0 \stackrel{454971987}{\longrightarrow} 454971987 \leadsto 406874902 \stackrel{48097086}{\longrightarrow} 454971988 \)\( Notice that the total time is \)454971987+48097086=503069073\(.Explanation of the third sample: Since all portals are initially off, the ant will not be teleported and will go straight from \)0\( to \)x_n+1=899754846+1=899754847$$$.
|
Input: 4 3 2 0 6 5 1 7 4 0 8 1 1 | Output: 23
|
Hard
| 4 | 1,109 | 633 | 207 | 15 |
1,671 |
B
|
1671B
|
B. Consecutive Points Segment
| 1,000 |
brute force; math; sortings
|
You are given \(n\) points with integer coordinates on a coordinate axis \(OX\). The coordinate of the \(i\)-th point is \(x_i\). All points' coordinates are distinct and given in strictly increasing order.For each point \(i\), you can do the following operation no more than once: take this point and move it by \(1\) to the left or to the right (i..e., you can change its coordinate \(x_i\) to \(x_i - 1\) or to \(x_i + 1\)). In other words, for each point, you choose (separately) its new coordinate. For the \(i\)-th point, it can be either \(x_i - 1\), \(x_i\) or \(x_i + 1\).Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i. e. for some integer \(l\) the coordinates of points should be equal to \(l, l + 1, \ldots, l + n - 1\).Note that the resulting points should have distinct coordinates.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of points in the set \(x\).The second line of the test case contains \(n\) integers \(x_1 < x_2 < \ldots < x_n\) (\(1 \le x_i \le 10^6\)), where \(x_i\) is the coordinate of the \(i\)-th point.It is guaranteed that the points are given in strictly increasing order (this also means that all coordinates are distinct). It is also guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
For each test case, print the answer β if the set of points from the test case can be moved to form a consecutive segment of integers, print YES, otherwise print NO.
|
Input: 521 431 2 341 2 3 71100000032 5 6 | Output: YES YES NO YES YES
|
Beginner
| 3 | 954 | 671 | 165 | 16 |
|
643 |
F
|
643F
|
F. Bears and Juice
| 2,900 |
dp; math; meet-in-the-middle
|
There are n bears in the inn and p places to sleep. Bears will party together for some number of nights (and days).Bears love drinking juice. They don't like wine but they can't distinguish it from juice by taste or smell.A bear doesn't sleep unless he drinks wine. A bear must go to sleep a few hours after drinking a wine. He will wake up many days after the party is over.Radewoosh is the owner of the inn. He wants to put some number of barrels in front of bears. One barrel will contain wine and all other ones will contain juice. Radewoosh will challenge bears to find a barrel with wine.Each night, the following happens in this exact order: Each bear must choose a (maybe empty) set of barrels. The same barrel may be chosen by many bears. Each bear drinks a glass from each barrel he chose. All bears who drink wine go to sleep (exactly those bears who chose a barrel with wine). They will wake up many days after the party is over. If there are not enough places to sleep then bears lose immediately. At the end, if it's sure where wine is and there is at least one awake bear then bears win (unless they have lost before because of the number of places to sleep).Radewoosh wants to allow bears to win. He considers q scenarios. In the i-th scenario the party will last for i nights. Then, let Ri denote the maximum number of barrels for which bears surely win if they behave optimally. Let's define . Your task is to find , where denotes the exclusive or (also denoted as XOR).Note that the same barrel may be chosen by many bears and all of them will go to sleep at once.
|
The only line of the input contains three integers n, p and q (1 β€ n β€ 109, 1 β€ p β€ 130, 1 β€ q β€ 2 000 000) β the number of bears, the number of places to sleep and the number of scenarios, respectively.
|
Print one integer, equal to .
|
In the first sample, there are 5 bears and only 1 place to sleep. We have R1 = 6, R2 = 11, R3 = 16 so the answer is . Let's analyze the optimal strategy for scenario with 2 days. There are R2 = 11 barrels and 10 of them contain juice. In the first night, the i-th bear chooses a barrel i only. If one of the first 5 barrels contains wine then one bear goes to sleep. Then, bears win because they know where wine is and there is at least one awake bear. But let's say none of the first 5 barrels contains wine. In the second night, the i-th bear chooses a barrel 5 + i. If one of barrels 6 β 10 contains wine then one bear goes to sleep. And again, bears win in such a situation. If nobody went to sleep then wine is in a barrel 11. In the second sample, there is only one bear. He should choose an empty set of barrels in each night. Otherwise, he would maybe get wine and bears would lose (because there must be at least one awake bear). So, for any number of days we have Ri = 1. The answer is .
|
Input: 5 1 3 | Output: 32
|
Master
| 3 | 1,583 | 203 | 29 | 6 |
316 |
C2
|
316C2
|
C2. Tidying Up
| 2,300 |
flows; graph matchings
|
Smart Beaver is careful about his appearance and pays special attention to shoes so he has a huge number of pairs of shoes from the most famous brands of the forest. He's trying to handle his shoes carefully so that each pair stood side by side. But by the end of the week because of his very active lifestyle in his dressing room becomes a mess.Smart Beaver from ABBYY is not only the brightest beaver in the area, but he also is the most domestically oriented. For example, on Mondays the Smart Beaver cleans everything in his home.It's Monday morning. Smart Beaver does not want to spend the whole day cleaning, besides, there is much in to do and itβs the gym day, so he wants to clean up as soon as possible. Now the floors are washed, the dust is wiped off β itβs time to clean up in the dressing room. But as soon as the Smart Beaver entered the dressing room, all plans for the day were suddenly destroyed: chaos reigned there and it seemed impossible to handle, even in a week. Give our hero some hope: tell him what is the minimum number of shoes need to change the position to make the dressing room neat.The dressing room is rectangular and is divided into n Γ m equal squares, each square contains exactly one shoe. Each pair of shoes has a unique number that is integer from 1 to , more formally, a square with coordinates (i, j) contains an integer number of the pair which is lying on it. The Smart Beaver believes that the dressing room is neat only when each pair of sneakers lies together. We assume that the pair of sneakers in squares (i1, j1) and (i2, j2) lies together if |i1 - i2| + |j1 - j2| = 1.
|
The first line contains two space-separated integers n and m. They correspond to the dressing room size. Next n lines contain m space-separated integers each. Those numbers describe the dressing room. Each number corresponds to a snicker. It is guaranteed that: nΒ·m is even. All numbers, corresponding to the numbers of pairs of shoes in the dressing room, will lie between 1 and . Each number from 1 to will occur exactly twice. The input limits for scoring 30 points are (subproblem C1): 2 β€ n, m β€ 8. The input limits for scoring 100 points are (subproblems C1+C2): 2 β€ n, m β€ 80.
|
Print exactly one integer β the minimum number of the sneakers that need to change their location.
|
The second sample.
|
Input: 2 31 1 22 3 3 | Output: 2
|
Expert
| 2 | 1,621 | 583 | 98 | 3 |
2,025 |
E
|
2025E
|
E. Card Game
| 2,200 |
combinatorics; dp; fft; greedy; math
|
In the most popular card game in Berland, a deck of \(n \times m\) cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from \(1\) to \(n\), and ranks are numbered from \(1\) to \(m\). There is exactly one card in the deck for each combination of suit and rank.A card with suit \(a\) and rank \(b\) can beat a card with suit \(c\) and rank \(d\) in one of two cases: \(a = 1\), \(c \ne 1\) (a card of suit \(1\) can beat a card of any other suit); \(a = c\), \(b > d\) (a card can beat any other card of the same suit but of a lower rank). Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i. e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins.Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo \(998244353\).
|
The only line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 500\)).Additional constraint on the input: \(m\) is even.
|
Print a single integer β the number of ways to distribute the cards so that the first player wins, taken modulo \(998244353\).
|
Input: 1 4 | Output: 2
|
Hard
| 5 | 1,356 | 127 | 126 | 20 |
|
370 |
E
|
370E
|
E. Summer Reading
| 2,500 |
dp; greedy
|
At school Vasya got an impressive list of summer reading books. Unlike other modern schoolchildren, Vasya loves reading, so he read some book each day of the summer.As Vasya was reading books, he was making notes in the Reader's Diary. Each day he wrote the orderal number of the book he was reading. The books in the list are numbered starting from 1 and Vasya was reading them in the order they go in the list. Vasya never reads a new book until he finishes reading the previous one. Unfortunately, Vasya wasn't accurate and some days he forgot to note the number of the book and the notes for those days remained empty.As Vasya knows that the literature teacher will want to check the Reader's Diary, so he needs to restore the lost records. Help him do it and fill all the blanks. Vasya is sure that he spends at least two and at most five days for each book. Vasya finished reading all the books he had started. Assume that the reading list contained many books. So many, in fact, that it is impossible to read all of them in a summer. If there are multiple valid ways to restore the diary records, Vasya prefers the one that shows the maximum number of read books.
|
The first line contains integer n β the number of summer days (2 β€ n β€ 2Β·105). The second line contains n integers a1, a2, ... an β the records in the diary in the order they were written (0 β€ ai β€ 105). If Vasya forgot to write the number of the book on the i-th day, then ai equals 0.
|
If it is impossible to correctly fill the blanks in the diary (the diary may contain mistakes initially), print ""-1"". Otherwise, print in the first line the maximum number of books Vasya could have read in the summer if we stick to the diary. In the second line print n integers β the diary with correctly inserted records. If there are multiple optimal solutions, you can print any of them.
|
Input: 70 1 0 0 0 3 0 | Output: 31 1 2 2 3 3 3
|
Expert
| 2 | 1,170 | 286 | 393 | 3 |
|
40 |
C
|
40C
|
C. Berland Square
| 2,300 |
implementation; math
|
Last year the world's largest square was built in Berland. It is known that the square can be represented as an infinite plane with an introduced Cartesian system of coordinates. On that square two sets of concentric circles were painted. Let's call the set of concentric circles with radii 1, 2, ..., K and the center in the point (z, 0) a (K, z)-set. Thus, on the square were painted a (N, x)-set and a (M, y)-set. You have to find out how many parts those sets divided the square into.
|
The first line contains integers N, x, M, y. (1 β€ N, M β€ 100000, - 100000 β€ x, y β€ 100000, x β y).
|
Print the sought number of parts.
|
Picture for the third sample:
|
Input: 1 0 1 1 | Output: 4
|
Expert
| 2 | 488 | 98 | 33 | 0 |
1,919 |
B
|
1919B
|
B. Plus-Minus Split
| 800 |
greedy
|
You are given a string \(s\) of length \(n\) consisting of characters ""+"" and ""-"". \(s\) represents an array \(a\) of length \(n\) defined by \(a_i=1\) if \(s_i=\) ""+"" and \(a_i=-1\) if \(s_i=\) ""-"".You will do the following process to calculate your penalty: Split \(a\) into non-empty arrays \(b_1,b_2,\ldots,b_k\) such that \(b_1+b_2+\ldots+b_k=a^\dagger\), where \(+\) denotes array concatenation. The penalty of a single array is the absolute value of its sum multiplied by its length. In other words, for some array \(c\) of length \(m\), its penalty is calculated as \(p(c)=|c_1+c_2+\ldots+c_m| \cdot m\). The total penalty that you will receive is \(p(b_1)+p(b_2)+\ldots+p(b_k)\). If you perform the above process optimally, find the minimum possible penalty you will receive.\(^\dagger\) Some valid ways to split \(a=[3,1,4,1,5]\) into \((b_1,b_2,\ldots,b_k)\) are \(([3],[1],[4],[1],[5])\), \(([3,1],[4,1,5])\) and \(([3,1,4,1,5])\) while some invalid ways to split \(a\) are \(([3,1],[1,5])\), \(([3],[\,],[1,4],[1,5])\) and \(([3,4],[5,1,1])\).
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 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\) (\(1 \le n \le 5000\)) β the length of string \(s\).The second line of each test case contains string \(s\) (\(s_i \in \{ \mathtt{+}, \mathtt{-} \}\), \(|s| = n\)).Note that there are no constraints on the sum of \(n\) over all test cases.
|
For each test case, output a single integer representing the minimum possible penalty you will receive.
|
In the first test case, we have \(a=[1]\). We can split array \(a\) into \(([1])\). Then, the sum of penalties of the subarrays is \(p([1]) = 1\).In the second test case, we have \(a=[-1,-1,-1,-1,-1]\). We can split array \(a\) into \(([-1],[-1],[-1],[-1],[-1])\). Then, the sum of penalties of the subarrays is \(p([-1]) + p([-1]) + p([-1]) + p([-1]) + p([-1]) = 1 + 1 + 1 + 1 + 1 = 5\).In the third test case, we have \(a=[1,-1,1,-1,1,-1]\). We can split array \(a\) into \(([1,-1,1,-1],[1,-1])\). Then, the sum of penalties of the subarrays is \(p([1,-1,1,-1]) + p([1,-1]) = 0 + 0 = 0\).
|
Input: 51+5-----6+-+-+-10--+++++++-20+---++++-+++++---++- | Output: 1 5 0 4 4
|
Beginner
| 1 | 1,064 | 486 | 103 | 19 |
1,743 |
E
|
1743E
|
E. FTL
| 2,400 |
binary search; dp
|
Monocarp is playing a video game. In the game, he controls a spaceship and has to destroy an enemy spaceship.Monocarp has two lasers installed on his spaceship. Both lasers \(1\) and \(2\) have two values: \(p_i\) β the power of the laser; \(t_i\) β the reload time of the laser. When a laser is fully charged, Monocarp can either shoot it or wait for the other laser to charge and shoot both of them at the same time.An enemy spaceship has \(h\) durability and \(s\) shield capacity. When Monocarp shoots an enemy spaceship, it receives \((P - s)\) damage (i. e. \((P - s)\) gets subtracted from its durability), where \(P\) is the total power of the lasers that Monocarp shoots (i. e. \(p_i\) if he only shoots laser \(i\) and \(p_1 + p_2\) if he shoots both lasers at the same time). An enemy spaceship is considered destroyed when its durability becomes \(0\) or lower.Initially, both lasers are zero charged.What's the lowest amount of time it can take Monocarp to destroy an enemy spaceship?
|
The first line contains two integers \(p_1\) and \(t_1\) (\(2 \le p_1 \le 5000\); \(1 \le t_1 \le 10^{12}\)) β the power and the reload time of the first laser.The second line contains two integers \(p_2\) and \(t_2\) (\(2 \le p_2 \le 5000\); \(1 \le t_2 \le 10^{12}\)) β the power and the reload time of the second laser.The third line contains two integers \(h\) and \(s\) (\(1 \le h \le 5000\); \(1 \le s < \min(p_1, p_2)\)) β the durability and the shield capacity of an enemy spaceship. Note that the last constraint implies that Monocarp will always be able to destroy an enemy spaceship.
|
Print a single integer β the lowest amount of time it can take Monocarp to destroy an enemy spaceship.
|
In the first example, Monocarp waits for both lasers to charge, then shoots both lasers at \(10\), they deal \((5 + 4 - 1) = 8\) damage. Then he waits again and shoots lasers at \(20\), dealing \(8\) more damage.In the second example, Monocarp doesn't wait for the second laser to charge. He just shoots the first laser \(25\) times, dealing \((10 - 9) = 1\) damage each time.
|
Input: 5 10 4 9 16 1 | Output: 20
|
Expert
| 2 | 997 | 594 | 102 | 17 |
1,690 |
B
|
1690B
|
B. Array Decrements
| 800 |
greedy; implementation
|
Kristina has two arrays \(a\) and \(b\), each containing \(n\) non-negative integers. She can perform the following operation on array \(a\) any number of times: apply a decrement to each non-zero element of the array, that is, replace the value of each element \(a_i\) such that \(a_i > 0\) with the value \(a_i - 1\) (\(1 \le i \le n\)). If \(a_i\) was \(0\), its value does not change. Determine whether Kristina can get an array \(b\) from an array \(a\) in some number of operations (probably zero). In other words, can she make \(a_i = b_i\) after some number of operations for each \(1 \le i \le n\)?For example, let \(n = 4\), \(a = [3, 5, 4, 1]\) and \(b = [1, 3, 2, 0]\). In this case, she can apply the operation twice: after the first application of the operation she gets \(a = [2, 4, 3, 0]\); after the second use of the operation she gets \(a = [1, 3, 2, 0]\). Thus, in two operations, she can get an array \(b\) from an array \(a\).
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) βthe number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^4\)).The second line of each test case contains exactly \(n\) non-negative integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^9\)).The third line of each test case contains exactly \(n\) non-negative integers \(b_1, b_2, \dots, b_n\) (\(0 \le b_i \le 10^9\)).It is guaranteed that the sum of \(n\) values over all test cases in the test does not exceed \(2 \cdot 10^5\).
|
For each test case, output on a separate line: YES, if by doing some number of operations it is possible to get an array \(b\) from an array \(a\); NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
|
The first test case is analyzed in the statement.In the second test case, it is enough to apply the operation to array \(a\) once.In the third test case, it is impossible to get array \(b\) from array \(a\).
|
Input: 643 5 4 11 3 2 031 2 10 1 045 3 7 21 1 1 151 2 3 4 51 2 3 4 6180146 | Output: YES YES NO NO YES NO
|
Beginner
| 2 | 948 | 621 | 287 | 16 |
171 |
A
|
171A
|
A. Mysterious numbers - 1
| 1,200 |
*special; constructive algorithms
|
The input contains two integers a1, a2 (0 β€ ai β€ 109), separated by a single space.
|
Output a single integer.
|
Input: 3 14 | Output: 44
|
Easy
| 2 | 0 | 83 | 24 | 1 |
||
1,462 |
B
|
1462B
|
B. Last Year's Substring
| 800 |
dp; implementation; strings
|
Polycarp has a string \(s[1 \dots n]\) of length \(n\) consisting of decimal digits. Polycarp performs the following operation with the string \(s\) no more than once (i.e. he can perform operation \(0\) or \(1\) time): Polycarp selects two numbers \(i\) and \(j\) (\(1 \leq i \leq j \leq n\)) and removes characters from the \(s\) string at the positions \(i, i+1, i+2, \ldots, j\) (i.e. removes substring \(s[i \dots j]\)). More formally, Polycarp turns the string \(s\) into the string \(s_1 s_2 \ldots s_{i-1} s_{j+1} s_{j+2} \ldots s_{n}\). For example, the string \(s = \)""20192020"" Polycarp can turn into strings: ""2020"" (in this case \((i, j)=(3, 6)\) or \((i, j)=(1, 4)\)); ""2019220"" (in this case \((i, j)=(6, 6)\)); ""020"" (in this case \((i, j)=(1, 5)\)); other operations are also possible, only a few of them are listed above. Polycarp likes the string ""2020"" very much, so he is wondering if it is possible to turn the string \(s\) into a string ""2020"" in no more than one operation? Note that you can perform zero operations.
|
The first line contains a positive integer \(t\) (\(1 \leq t \leq 1000 \)) β number of test cases in the test. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(4 \leq n \leq 200\)) β length of the string \(s\). The next line contains a string \(s\) of length \(n\) consisting of decimal digits. It is allowed that the string \(s\) starts with digit 0.
|
For each test case, output on a separate line: ""YES"" if Polycarp can turn the string \(s\) into a string ""2020"" in no more than one operation (i.e. he can perform \(0\) or \(1\) operation); ""NO"" otherwise. You may print every letter of ""YES"" and ""NO"" in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
|
In the first test case, Polycarp could choose \(i=3\) and \(j=6\).In the second test case, Polycarp could choose \(i=2\) and \(j=5\).In the third test case, Polycarp did not perform any operations with the string.
|
Input: 6 8 20192020 8 22019020 4 2020 5 20002 6 729040 6 200200 | Output: YES YES YES NO NO NO
|
Beginner
| 3 | 1,052 | 393 | 377 | 14 |
1,819 |
B
|
1819B
|
B. The Butcher
| 1,900 |
geometry; greedy; implementation; sortings; two pointers
|
Anton plays his favorite game ""Defense of The Ancients 2"" for his favorite hero β The Butcher. Now he wants to make his own dinner. To do this he will take a rectangle of height \(h\) and width \(w\), then make a vertical or horizontal cut so that both resulting parts have integer sides. After that, he will put one of the parts in the box and cut the other again, and so on.More formally, a rectangle of size \(h \times w\) can be cut into two parts of sizes \(x \times w\) and \((h - x) \times w\), where \(x\) is an integer from \(1\) to \((h - 1)\), or into two parts of sizes \(h \times y\) and \(h \times (w - y)\), where \(y\) is an integer from \(1\) to \((w - 1)\).He will repeat this operation \(n - 1\) times, and then put the remaining rectangle into the box too. Thus, the box will contain \(n\) rectangles, of which \(n - 1\) rectangles were put in the box as a result of the cuts, and the \(n\)-th rectangle is the one that the Butcher has left after all \(n - 1\) cuts.Unfortunately, Butcher forgot the numbers \(h\) and \(w\), but he still has \(n\) rectangles mixed in random order. Note that Butcher didn't rotate the rectangles, but only shuffled them. Now he wants to know all possible pairs \((h, w)\) from which this set of rectangles can be obtained. And you have to help him do it!It is guaranteed that there exists at least one pair \((h, w)\) from which this set of rectangles can be obtained.
|
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 test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of rectangles obtained.The \(i\)-th of the next \(n\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 10^6\)) β the height and width of the \(i\)-th rectangle.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, on the first line output a single integer \(m\) β the number of pairs \((h, w)\) denoting the sizes of rectangles from which the given rectangles can be obtained. Two rectangles are considered different if they have different heights or widths.On each of the following \(m\) lines print output integers \(h_i\) and \(w_i\) β the height and width of the rectangle from which the given rectangles can be obtained. You can output the rectangles in any order.
|
In the first test case, Butcher could only have a rectangle of size \(4 \times 5\). Then the cuts could look like this (first the green cut was made, then the red one): In the second test case, Butcher could have either a rectangle of \(1 \times 3\) or \(3 \times 1\). The cuts would have looked like this (first the green cut was made, then the red cut): In the third test case, Butcher did not make any cuts, so the rectangle is \(10 \times 10\).
|
Input: 431 23 51 331 11 11 1110 1043 25 52 28 7 | Output: 1 4 5 2 1 3 3 1 1 10 10 1 13 7
|
Hard
| 5 | 1,423 | 561 | 475 | 18 |
1,968 |
C
|
1968C
|
C. Assembly via Remainders
| 1,000 |
constructive algorithms; number theory
|
You are given an array \(x_2,x_3,\dots,x_n\). Your task is to find any array \(a_1,\dots,a_n\), where: \(1\le a_i\le 10^9\) for all \(1\le i\le n\). \(x_i=a_i \bmod a_{i-1}\) for all \(2\le i\le n\). Here \(c\bmod d\) denotes the remainder of the division of the integer \(c\) by the integer \(d\). For example \(5 \bmod 2 = 1\), \(72 \bmod 3 = 0\), \(143 \bmod 14 = 3\).Note that if there is more than one \(a\) which satisfies the statement, you are allowed to find any.
|
The first line contains a single 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 500)\) β the number of elements in \(a\).The second line of each test case contains \(n-1\) integers \(x_2,\dots,x_n\) \((1\le x_i\le 500)\) β the elements of \(x\).It is guaranteed that the sum of values \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output any \(a_1,\dots,a_n\) (\(1 \le a_i \le 10^9\)) which satisfies the statement.
|
In the first test case \(a=[3,5,4,9]\) satisfies the conditions, because: \(a_2\bmod a_1=5\bmod 3=2=x_2\); \(a_3\bmod a_2=4\bmod 5=4=x_3\); \(a_4\bmod a_3=9\bmod 4=1=x_4\);
|
Input: 542 4 131 164 2 5 1 2250031 5 | Output: 3 5 4 9 2 5 11 5 14 16 5 11 24 501 500 2 7 5
|
Beginner
| 2 | 472 | 437 | 103 | 19 |
1,236 |
E
|
1236E
|
E. Alice and the Unfair Game
| 2,500 |
binary search; data structures; dp; dsu
|
Alice is playing a game with her good friend, Marisa.There are \(n\) boxes arranged in a line, numbered with integers from \(1\) to \(n\) from left to right. Marisa will hide a doll in one of the boxes. Then Alice will have \(m\) chances to guess where the doll is. If Alice will correctly guess the number of box, where doll is now, she will win the game, otherwise, her friend will win the game.In order to win, Marisa will use some unfair tricks. After each time Alice guesses a box, she can move the doll to the neighboring box or just keep it at its place. Boxes \(i\) and \(i + 1\) are neighboring for all \(1 \leq i \leq n - 1\). She can also use this trick once before the game starts.So, the game happens in this order: the game starts, Marisa makes the trick, Alice makes the first guess, Marisa makes the trick, Alice makes the second guess, Marisa makes the trick, \(\ldots\), Alice makes \(m\)-th guess, Marisa makes the trick, the game ends.Alice has come up with a sequence \(a_1, a_2, \ldots, a_m\). In the \(i\)-th guess, she will ask if the doll is in the box \(a_i\). She wants to know the number of scenarios \((x, y)\) (for all \(1 \leq x, y \leq n\)), such that Marisa can win the game if she will put the doll at the \(x\)-th box at the beginning and at the end of the game, the doll will be at the \(y\)-th box. Help her and calculate this number.
|
The first line contains two integers \(n\) and \(m\), separated by space (\(1 \leq n, m \leq 10^5\)) β the number of boxes and the number of guesses, which Alice will make.The next line contains \(m\) integers \(a_1, a_2, \ldots, a_m\), separated by spaces (\(1 \leq a_i \leq n\)), the number \(a_i\) means the number of the box which Alice will guess in the \(i\)-th guess.
|
Print the number of scenarios in a single line, or the number of pairs of boxes \((x, y)\) (\(1 \leq x, y \leq n\)), such that if Marisa will put the doll into the box with number \(x\), she can make tricks in such way, that at the end of the game the doll will be in the box with number \(y\) and she will win the game.
|
In the first example, the possible scenarios are \((1, 1)\), \((1, 2)\), \((2, 1)\), \((2, 2)\), \((2, 3)\), \((3, 2)\), \((3, 3)\).Let's take \((2, 2)\) as an example. The boxes, in which the doll will be during the game can be \(2 \to 3 \to 3 \to 3 \to 2\)
|
Input: 3 3 2 2 2 | Output: 7
|
Expert
| 4 | 1,371 | 374 | 320 | 12 |
193 |
A
|
193A
|
A. Cutting Figure
| 1,700 |
constructive algorithms; graphs; trees
|
You've gotten an n Γ m sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as A. Set A is connected. Your task is to find the minimum number of squares that we can delete from set A to make it not connected.A set of painted squares is called connected, if for every two squares a and b from this set there is a sequence of squares from the set, beginning in a and ending in b, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition.
|
The first input line contains two space-separated integers n and m (1 β€ n, m β€ 50) β the sizes of the sheet of paper. Each of the next n lines contains m characters β the description of the sheet of paper: the j-th character of the i-th line equals either ""#"", if the corresponding square is painted (belongs to set A), or equals ""."" if the corresponding square is not painted (does not belong to set A). It is guaranteed that the set of all painted squares A is connected and isn't empty.
|
On the first line print the minimum number of squares that need to be deleted to make set A not connected. If it is impossible, print -1.
|
In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore.The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses.
|
Input: 5 4#####..##..##..##### | Output: 2
|
Medium
| 3 | 644 | 493 | 137 | 1 |
683 |
H
|
683H
|
H. Exchange of Books
| 1,900 |
*special
|
n pupils, who love to read books, study at school. It is known that each student has exactly one best friend, and each pupil is the best friend of exactly one other pupil. Each of the pupils has exactly one interesting book.The pupils decided to share books with each other. Every day, all pupils give their own books to their best friends. Thus, every day each of the pupils has exactly one book.Your task is to use the list of the best friends and determine the exchange of books among pupils after k days. For simplicity, all students are numbered from 1 to n in all tests.
|
The first line contains two integers n and k (2 β€ n β€ 100000, 1 β€ k β€ 1016) β the number of pupils and days during which they will exchange books.The second line contains n different integers ai (1 β€ ai β€ n), where ai is equal to the number of the pupil who has the best friend with the number i.It is guaranteed that no pupil is the best friend of himself.
|
In a single line print n different integers, where i-th integer should be equal to the number of the pupil who will have the book, which the pupil with the number i had in the beginning, after k days.
|
The explanation to the first test.There are 4 pupils and 1 day. The list of the best friends equals to {2, 4, 1, 3}. It means that: the pupil with the number 3 β is the best friend of pupil with the number 1, the pupil with the number 1 β is the best friend of pupil with the number 2, the pupil with the number 4 β is the best friend of pupil with the number 3, the pupil with the number 2 β is the best friend of pupil with the number 4. After the first day the exchange of books will be {3, 1, 4, 2}. the pupil with the number 3 will have the book, which the pupil with the number 1 had in the beginning, the pupil with the number 1 will have the book, which the pupil with the number 2 had in the beginning, the pupil with the number 4 will have the book, which the pupil with the number 3 had in the beginning the pupil with the number 2 will have the book, which the pupil with the number 4 had in the beginning. Thus, the answer is 3 1 4 2.
|
Input: 4 12 4 1 3 | Output: 3 1 4 2
|
Hard
| 1 | 576 | 357 | 200 | 6 |
1,176 |
F
|
1176F
|
F. Destroy it!
| 2,100 |
dp; implementation; sortings
|
You are playing a computer card game called Splay the Sire. Currently you are struggling to defeat the final boss of the game.The boss battle consists of \(n\) turns. During each turn, you will get several cards. Each card has two parameters: its cost \(c_i\) and damage \(d_i\). You may play some of your cards during each turn in some sequence (you choose the cards and the exact order they are played), as long as the total cost of the cards you play during the turn does not exceed \(3\). After playing some (possibly zero) cards, you end your turn, and all cards you didn't play are discarded. Note that you can use each card at most once.Your character has also found an artifact that boosts the damage of some of your actions: every \(10\)-th card you play deals double damage.What is the maximum possible damage you can deal during \(n\) turns?
|
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of turns.Then \(n\) blocks of input follow, the \(i\)-th block representing the cards you get during the \(i\)-th turn.Each block begins with a line containing one integer \(k_i\) (\(1 \le k_i \le 2 \cdot 10^5\)) β the number of cards you get during \(i\)-th turn. Then \(k_i\) lines follow, each containing two integers \(c_j\) and \(d_j\) (\(1 \le c_j \le 3\), \(1 \le d_j \le 10^9\)) β the parameters of the corresponding card.It is guaranteed that \(\sum \limits_{i = 1}^{n} k_i \le 2 \cdot 10^5\).
|
Print one integer β the maximum damage you may deal.
|
In the example test the best course of action is as follows:During the first turn, play all three cards in any order and deal \(18\) damage.During the second turn, play both cards and deal \(7\) damage.During the third turn, play the first and the third card and deal \(13\) damage.During the fourth turn, play the first and the third card and deal \(25\) damage.During the fifth turn, play the only card, which will deal double damage (\(200\)).
|
Input: 5 3 1 6 1 7 1 5 2 1 4 1 3 3 1 10 3 5 2 3 3 1 15 2 4 1 10 1 1 100 | Output: 263
|
Hard
| 3 | 852 | 588 | 52 | 11 |
1,678 |
A
|
1678A
|
A. Tokitsukaze and All Zero Sequence
| 800 |
implementation
|
Tokitsukaze has a sequence \(a\) of length \(n\). For each operation, she selects two numbers \(a_i\) and \(a_j\) (\(i \ne j\); \(1 \leq i,j \leq n\)). If \(a_i = a_j\), change one of them to \(0\). Otherwise change both of them to \(\min(a_i, a_j)\). Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to \(0\). It can be proved that the answer always exists.
|
The first line contains a single positive integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.For each test case, the first line contains a single integer \(n\) (\(2 \leq n \leq 100\)) β the length of the sequence \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 100\)) β the sequence \(a\).
|
For each test case, print a single integer β the minimum number of operations to change all numbers in the sequence to \(0\).
|
In the first test case, one of the possible ways to change all numbers in the sequence to \(0\):In the \(1\)-st operation, \(a_1 < a_2\), after the operation, \(a_2 = a_1 = 1\). Now the sequence \(a\) is \([1,1,3]\).In the \(2\)-nd operation, \(a_1 = a_2 = 1\), after the operation, \(a_1 = 0\). Now the sequence \(a\) is \([0,1,3]\).In the \(3\)-rd operation, \(a_1 < a_2\), after the operation, \(a_2 = 0\). Now the sequence \(a\) is \([0,0,3]\).In the \(4\)-th operation, \(a_2 < a_3\), after the operation, \(a_3 = 0\). Now the sequence \(a\) is \([0,0,0]\).So the minimum number of operations is \(4\).
|
Input: 331 2 331 2 231 2 0 | Output: 4 3 2
|
Beginner
| 1 | 406 | 347 | 125 | 16 |
2,030 |
G2
|
2030G2
|
G2. The Destruction of the Universe (Hard Version)
| 3,100 |
combinatorics; math
|
This is the hard version of the problem. In this version, \(n \leq 10^6\). You can only make hacks if both versions of the problem are solved.Orangutans are powerful beingsβso powerful that they only need \(1\) unit of time to destroy every vulnerable planet in the universe!There are \(n\) planets in the universe. Each planet has an interval of vulnerability \([l, r]\), during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by \(1\) unit.Specifically, suppose the expansion is performed on planet \(p\) with interval of vulnerability \([l_p, r_p]\). Then, the resulting interval of vulnerability may be either \([l_p - 1, r_p]\) or \([l_p, r_p + 1]\).Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo \(998\,244\,353\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^6\)) β the number of planets in the universe.The following \(n\) lines contain two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \leq r_i \leq n\)) β the initial interval of vulnerability of the \(i\)-th planet.It is guaranteed that the sum of \(n\) does not exceed \(10^6\) over all test cases.
|
For each test case, output an integer β the sum of scores to destroy all non-empty subsets of the planets in the universe, modulo \(998\,244\,353\).
|
In the first testcase, there are seven non-empty subsets of planets we must consider: For each of the subsets \(\{[1,1]\}, \{[2,3]\}, \{[3,3]\}\), the score is \(0\). For the subset \(\{[2,3], [3,3]\}\), the score is \(0\), because the point \(3\) is already contained in both planets' interval of vulnerability. For the subset \(\{[1,1], [2,3]\}\), the score is \(1\). By using one operation on changing the interval of vulnerability of the second planet to be \([1,3]\), the two planets now both have the point \(1\) in their interval. For the subset \(\{[1,1], [3,3]\}\), the score is \(2\). By using two operations on changing the interval of vulnerability of the first planet to be \([1,3]\), the two planets now both have the point \(3\) in their interval. For the subset \(\{[1,1], [2,3], [3,3]\}\), the score is \(2\). By using one operation on changing the interval of vulnerability of the first planet to be \([1,2]\) and one operation on changing the interval of vulnerability of the third planet to \([2,3]\), all three planets will have the point \(2\) in their interval. The sum of scores of all non-empty subsets of the first testcase is \(0 \cdot 4 + 1 \cdot 1 + 2\cdot2 = 5\).
|
Input: 331 12 33 341 42 32 41 151 22 33 44 51 5 | Output: 5 6 24
|
Master
| 2 | 1,147 | 464 | 148 | 20 |
305 |
B
|
305B
|
B. Continued Fractions
| 1,700 |
brute force; implementation; math
|
A continued fraction of height n is a fraction of form . You are given two rational numbers, one is represented as and the other one is represented as a finite fraction of height n. Check if they are equal.
|
The first line contains two space-separated integers p, q (1 β€ q β€ p β€ 1018) β the numerator and the denominator of the first fraction.The second line contains integer n (1 β€ n β€ 90) β the height of the second fraction. The third line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 1018) β the continued fraction.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Print ""YES"" if these fractions are equal and ""NO"" otherwise.
|
In the first sample .In the second sample .In the third sample .
|
Input: 9 422 4 | Output: YES
|
Medium
| 3 | 206 | 475 | 64 | 3 |
1,061 |
E
|
1061E
|
E. Politics
| 2,600 |
flows; graphs
|
There are \(n\) cities in the country. Two candidates are fighting for the post of the President. The elections are set in the future, and both candidates have already planned how they are going to connect the cities with roads. Both plans will connect all cities using \(n - 1\) roads only. That is, each plan can be viewed as a tree. Both of the candidates had also specified their choice of the capital among \(n\) cities (\(x\) for the first candidate and \(y\) for the second candidate), which may or may not be same.Each city has a potential of building a port (one city can have at most one port). Building a port in \(i\)-th city brings \(a_i\) amount of money. However, each candidate has his specific demands. The demands are of the form: \(k\) \(x\), which means that the candidate wants to build exactly \(x\) ports in the subtree of the \(k\)-th city of his tree (the tree is rooted at the capital of his choice). Find out the maximum revenue that can be gained while fulfilling all demands of both candidates, or print -1 if it is not possible to do.It is additionally guaranteed, that each candidate has specified the port demands for the capital of his choice.
|
The first line contains integers \(n\), \(x\) and \(y\) (\(1 \le n \le 500\), \(1 \le x, y \le n\)) β the number of cities, the capital of the first candidate and the capital of the second candidate respectively.Next line contains integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\,000\)) β the revenue gained if the port is constructed in the corresponding city.Each of the next \(n - 1\) lines contains integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)), denoting edges between cities in the tree of the first candidate.Each of the next \(n - 1\) lines contains integers \(u'_i\) and \(v'_i\) (\(1 \le u'_i, v'_i \le n\), \(u'_i \ne v'_i\)), denoting edges between cities in the tree of the second candidate.Next line contains an integer \(q_1\) (\(1 \le q_1 \le n\)), denoting the number of demands of the first candidate.Each of the next \(q_1\) lines contains two integers \(k\) and \(x\) (\(1 \le k \le n\), \(1 \le x \le n\)) β the city number and the number of ports in its subtree.Next line contains an integer \(q_2\) (\(1 \le q_2 \le n\)), denoting the number of demands of the second candidate.Each of the next \(q_2\) lines contain two integers \(k\) and \(x\) (\(1 \le k \le n\), \(1 \le x \le n\)) β the city number and the number of ports in its subtree.It is guaranteed, that given edges correspond to valid trees, each candidate has given demand about each city at most once and that each candidate has specified the port demands for the capital of his choice. That is, the city \(x\) is always given in demands of the first candidate and city \(y\) is always given in the demands of the second candidate.
|
Print exactly one integer β the maximum possible revenue that can be gained, while satisfying demands of both candidates, or -1 if it is not possible to satisfy all of the demands.
|
In the first example, it is optimal to build ports in cities \(2\), \(3\) and \(4\), which fulfills all demands of both candidates and gives revenue equal to \(2 + 3 + 4 = 9\).In the second example, it is optimal to build ports in cities \(2\) and \(3\), which fulfills all demands of both candidates and gives revenue equal to \(99 + 99 = 198\). In the third example, it is not possible to build ports in such way, that all demands of both candidates are specified, hence the answer is -1.
|
Input: 4 1 21 2 3 41 21 33 41 22 31 421 34 112 3 | Output: 9
|
Expert
| 2 | 1,176 | 1,651 | 180 | 10 |
1,931 |
A
|
1931A
|
A. Recovering a Small String
| 800 |
brute force; strings
|
Nikita had a word consisting of exactly \(3\) lowercase Latin letters. The letters in the Latin alphabet are numbered from \(1\) to \(26\), where the letter ""a"" has the index \(1\), and the letter ""z"" has the index \(26\).He encoded this word as the sum of the positions of all the characters in the alphabet. For example, the word ""cat"" he would encode as the integer \(3 + 1 + 20 = 24\), because the letter ""c"" has the index \(3\) in the alphabet, the letter ""a"" has the index \(1\), and the letter ""t"" has the index \(20\).However, this encoding turned out to be ambiguous! For example, when encoding the word ""ava"", the integer \(1 + 22 + 1 = 24\) is also obtained.Determine the lexicographically smallest word of \(3\) letters that could have been encoded.A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the test.This is followed by the descriptions of the test cases.The first and only line of each test case contains an integer \(n\) (\(3 \le n \le 78\)) β the encoded word.
|
For each test case, output the lexicographically smallest three-letter word that could have been encoded on a separate line.
|
Input: 5247035548 | Output: aav rzz aaa czz auz
|
Beginner
| 2 | 1,085 | 284 | 124 | 19 |
|
2,020 |
E
|
2020E
|
E. Expected Power
| 2,000 |
bitmasks; dp; math; probabilities
|
You are given an array of \(n\) integers \(a_1,a_2,\ldots,a_n\). You are also given an array \(p_1, p_2, \ldots, p_n\).Let \(S\) denote the random multiset (i. e., it may contain equal elements) constructed as follows: Initially, \(S\) is empty. For each \(i\) from \(1\) to \(n\), insert \(a_i\) into \(S\) with probability \(\frac{p_i}{10^4}\). Note that each element is inserted independently. Denote \(f(S)\) as the bitwise XOR of all elements of \(S\). Please calculate the expected value of \((f(S))^2\). Output the answer modulo \(10^9 + 7\).Formally, let \(M = 10^9 + 7\). It can be shown that the 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 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 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 1023\)).The third line of each test case contains \(n\) integers \(p_1,p_2,\ldots,p_n\) (\(1 \le p_i \le 10^4\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the expected value of \((f(S))^2\), modulo \(10^9 + 7\).
|
In the first test case, \(a = [1, 2]\) and each element is inserted into \(S\) with probability \(\frac{1}{2}\), since \(p_1 = p_2 = 5000\) and \(\frac{p_i}{10^4} = \frac{1}{2}\). Thus, there are \(4\) outcomes for \(S\), each happening with the same probability of \(\frac{1}{4}\): \(S = \varnothing\). In this case, \(f(S) = 0\), \((f(S))^2 = 0\). \(S = \{1\}\). In this case, \(f(S) = 1\), \((f(S))^2 = 1\). \(S = \{2\}\). In this case, \(f(S) = 2\), \((f(S))^2 = 4\). \(S = \{1,2\}\). In this case, \(f(S) = 1 \oplus 2 = 3\), \((f(S))^2 = 9\). Hence, the answer is \(0 \cdot \frac{1}{4} + 1 \cdot \frac{1}{4} + 4\cdot \frac{1}{4} + 9 \cdot \frac{1}{4} = \frac{14}{4} = \frac{7}{2} \equiv 500\,000\,007 \pmod{10^9 + 7}\).In the second test case, \(a = [1, 1]\), \(a_1\) is inserted into \(S\) with probability \(0.1\), while \(a_2\) is inserted into \(S\) with probability \(0.2\). There are \(3\) outcomes for \(S\): \(S = \varnothing\). In this case, \(f(S) = 0\), \((f(S))^2 = 0\). This happens with probability \((1-0.1) \cdot (1-0.2) = 0.72\). \(S = \{1\}\). In this case, \(f(S) = 1\), \((f(S))^2 = 1\). This happens with probability \((1-0.1) \cdot 0.2 + 0.1 \cdot (1-0.2) = 0.26\). \(S = \{1, 1\}\). In this case, \(f(S) = 0\), \((f(S))^2 = 0\). This happens with probability \(0.1 \cdot 0.2 = 0.02\). Hence, the answer is \(0 \cdot 0.72 + 1 \cdot 0.26 + 0 \cdot 0.02 = 0.26 = \frac{26}{100} \equiv 820\,000\,006 \pmod{10^9 + 7}\).
|
Input: 421 25000 500021 11000 20006343 624 675 451 902 8206536 5326 7648 2165 9430 54281110000 | Output: 500000007 820000006 280120536 1
|
Hard
| 4 | 901 | 560 | 83 | 20 |
1,789 |
B
|
1789B
|
B. Serval and Inversion Magic
| 800 |
brute force; implementation; strings; two pointers
|
Serval has a string \(s\) that only consists of 0 and 1 of length \(n\). The \(i\)-th character of \(s\) is denoted as \(s_i\), where \(1\leq i\leq n\).Serval can perform the following operation called Inversion Magic on the string \(s\): Choose an segment \([l, r]\) (\(1\leq l\leq r\leq n\)). For \(l\leq i\leq r\), change \(s_i\) into 1 if \(s_i\) is 0, and change \(s_i\) into 0 if \(s_i\) is 1. For example, let \(s\) be 010100 and the segment \([2,5]\) is chosen. The string \(s\) will be 001010 after performing the Inversion Magic.Serval wants to make \(s\) a palindrome after performing Inversion Magic exactly once. Help him to determine whether it is possible.A string is a palindrome iff it reads the same backwards as forwards. For example, 010010 is a palindrome but 10111 is not.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2\leq n\leq 10^5\)) β the length of string \(s\).The second line of each test case contains a binary string \(s\) of length \(n\). Only characters 0 and 1 can appear in \(s\).It's guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, print Yes if \(s\) can be a palindrome after performing Inversion Magic exactly once, and print No if not.You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
|
In the first test case, Serval can perform Inversion Magic on the segment \([1,4]\). The string \(s\) will be 0110 after the magic.In the second test case, Serval can perform Inversion Magic on the segment \([1,3]\). The string \(s\) will be 01110 after the magic.In the third test case, Serval can't make \(s\) a palindrome by performing Inversion Magic exactly once.
|
Input: 34100151001070111011 | Output: Yes Yes No
|
Beginner
| 4 | 794 | 494 | 251 | 17 |
796 |
E
|
796E
|
E. Exam Cheating
| 2,400 |
binary search; dp
|
Zane and Zane's crush have just decided to date! However, the girl is having a problem with her Physics final exam, and needs your help.There are n questions, numbered from 1 to n. Question i comes before question i + 1 (1 β€ i < n). Each of the questions cannot be guessed on, due to the huge penalty for wrong answers. The girl luckily sits in the middle of two geniuses, so she is going to cheat. However, the geniuses have limitations. Each of them may or may not know the answers to some questions. Anyway, it is safe to assume that the answers on their answer sheets are absolutely correct.To make sure she will not get caught by the proctor, the girl will glance at most p times, each time looking at no more than k consecutive questions on one of the two geniuses' answer sheet. When the girl looks at some question on an answer sheet, she copies the answer to that question if it is on that answer sheet, or does nothing otherwise.Help the girl find the maximum number of questions she can get correct.
|
The first line contains three integers n, p, and k (1 β€ n, p β€ 1, 000, 1 β€ k β€ min(n, 50)) β the number of questions, the maximum number of times the girl can glance, and the maximum number of consecutive questions that can be looked at in one time glancing, respectively.The second line starts with one integer r (0 β€ r β€ n), denoting the number of questions the first genius has answered on his answer sheet. Then follow r integers a1, a2, ..., ar (1 β€ ai β€ n) β the answered questions, given in a strictly-increasing order (that is, ai < ai + 1).The third line starts with one integer s (0 β€ s β€ n), denoting the number of questions the second genius has answered on his answer sheet. Then follow s integers b1, b2, ..., bs (1 β€ bi β€ n) β the answered questions, given in a strictly-increasing order (that is, bi < bi + 1).
|
Print one integer β the maximum number of questions the girl can answer correctly.
|
Let (x, l, r) denote the action of looking at all questions i such that l β€ i β€ r on the answer sheet of the x-th genius.In the first sample, the girl could get 4 questions correct by performing sequence of actions (1, 1, 3) and (2, 5, 6).In the second sample, the girl could perform sequence of actions (1, 3, 5), (2, 2, 4), and (2, 6, 8) to get 7 questions correct.
|
Input: 6 2 33 1 3 64 1 2 5 6 | Output: 4
|
Expert
| 2 | 1,010 | 826 | 82 | 7 |
895 |
A
|
895A
|
A. Pizza Separation
| 1,200 |
brute force; implementation
|
Students Vasya and Petya are studying at the BSU (Byteland State University). At one of the breaks they decided to order a pizza. In this problem pizza is a circle of some radius. The pizza was delivered already cut into n pieces. The i-th piece is a sector of angle equal to ai. Vasya and Petya want to divide all pieces of pizza into two continuous sectors in such way that the difference between angles of these sectors is minimal. Sector angle is sum of angles of all pieces in it. Pay attention, that one of sectors can be empty.
|
The first line contains one integer n (1 β€ n β€ 360) β the number of pieces into which the delivered pizza was cut.The second line contains n integers ai (1 β€ ai β€ 360) β the angles of the sectors into which the pizza was cut. The sum of all ai is 360.
|
Print one integer β the minimal difference between angles of sectors that will go to Vasya and Petya.
|
In first sample Vasya can take 1 and 2 pieces, Petya can take 3 and 4 pieces. Then the answer is |(90 + 90) - (90 + 90)| = 0.In third sample there is only one piece of pizza that can be taken by only one from Vasya and Petya. So the answer is |360 - 0| = 360.In fourth sample Vasya can take 1 and 4 pieces, then Petya will take 2 and 3 pieces. So the answer is |(170 + 10) - (30 + 150)| = 0.Picture explaning fourth sample:Both red and green sectors consist of two adjacent pieces of pizza. So Vasya can take green sector, then Petya will take red sector.
|
Input: 490 90 90 90 | Output: 0
|
Easy
| 2 | 534 | 251 | 101 | 8 |
222 |
B
|
222B
|
B. Cosmic Tables
| 1,300 |
data structures; implementation
|
The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement. UCDHP stores some secret information about meteors as an n Γ m table with integers in its cells. The order of meteors in the Universe is changing. That's why the main UCDHP module receives the following queries: The query to swap two table rows; The query to swap two table columns; The query to obtain a secret number in a particular table cell. As the main UCDHP module is critical, writing the functional of working with the table has been commissioned to you.
|
The first line contains three space-separated integers n, m and k (1 β€ n, m β€ 1000, 1 β€ k β€ 500000) β the number of table columns and rows and the number of queries, correspondingly.Next n lines contain m space-separated numbers each β the initial state of the table. Each number p in the table is an integer and satisfies the inequality 0 β€ p β€ 106.Next k lines contain queries in the format ""si xi yi"", where si is one of the characters ""Ρ"", ""r"" or ""g"", and xi, yi are two integers. If si = ""c"", then the current query is the query to swap columns with indexes xi and yi (1 β€ x, y β€ m, x β y); If si = ""r"", then the current query is the query to swap rows with indexes xi and yi (1 β€ x, y β€ n, x β y); If si = ""g"", then the current query is the query to obtain the number that located in the xi-th row and in the yi-th column (1 β€ x β€ n, 1 β€ y β€ m). The table rows are considered to be indexed from top to bottom from 1 to n, and the table columns β from left to right from 1 to m.
|
For each query to obtain a number (si = ""g"") print the required number. Print the answers to the queries in the order of the queries in the input.
|
Let's see how the table changes in the second test case.After the first operation is fulfilled, the table looks like that:2 1 41 3 5After the second operation is fulfilled, the table looks like that:1 3 52 1 4So the answer to the third query (the number located in the first row and in the third column) will be 5.
|
Input: 3 3 51 2 34 5 67 8 9g 3 2r 3 2c 2 3g 2 2g 3 2 | Output: 896
|
Easy
| 2 | 661 | 997 | 148 | 2 |
315 |
A
|
315A
|
A. Sereja and Bottles
| 1,400 |
brute force
|
Sereja and his friends went to a picnic. The guys had n soda bottles just for it. Sereja forgot the bottle opener as usual, so the guys had to come up with another way to open bottles.Sereja knows that the i-th bottle is from brand ai, besides, you can use it to open other bottles of brand bi. You can use one bottle to open multiple other bottles. Sereja can open bottle with opened bottle or closed bottle.Knowing this, Sereja wants to find out the number of bottles they've got that they won't be able to open in any way. Help him and find this number.
|
The first line contains integer n (1 β€ n β€ 100) β the number of bottles. The next n lines contain the bottles' description. The i-th line contains two integers ai, bi (1 β€ ai, bi β€ 1000) β the description of the i-th bottle.
|
In a single line print a single integer β the answer to the problem.
|
Input: 41 12 23 34 4 | Output: 4
|
Easy
| 1 | 556 | 224 | 68 | 3 |
|
1,792 |
E
|
1792E
|
E. Divisors and Table
| 2,400 |
brute force; dfs and similar; dp; number theory
|
You are given an \(n \times n\) multiplication table and a positive integer \(m = m_1 \cdot m_2\). A \(n \times n\) multiplication table is a table with \(n\) rows and \(n\) columns numbered from \(1\) to \(n\), where \(a_{i, j} = i \cdot j\).For each divisor \(d\) of \(m\), check: does \(d\) occur in the table at least once, and if it does, what is the minimum row that contains \(d\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10\)) β the number of test cases.The first and only line of each test case contains three integers \(n\), \(m_1\) and \(m_2\) (\(1 \le n \le 10^9\); \(1 \le m_1, m_2 \le 10^9\)) β the size of the multiplication table and the integer \(m\) represented as \(m_1 \cdot m_2\).
|
For each test case, let \(d_1, d_2, \dots, d_k\) be all divisors of \(m\) sorted in the increasing order. And let \(a_1, a_2, \dots, a_k\) be an array of answers, where \(a_i\) is equal to the minimum row index where divisor \(d_i\) occurs, or \(0\), if there is no such row.Since array \(a\) may be large, first, print an integer \(s\) β the number of divisors of \(m\) that are present in the \(n \times n\) table. Next, print a single value \(X = a_1 \oplus a_2 \oplus \dots \oplus a_k\), where \(\oplus\) denotes the bitwise XOR operation.
|
In the first test case, \(m = 72 \cdot 1 = 72\) and has \(12\) divisors \([1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72]\). The \(3 \times 3\) multiplication table looks like that: 123112322463369 For each divisor of \(m\) that is present in the table, the position with minimum row index is marked. So the array of answers \(a\) is equal to \([1, 1, 1, 2, 2, 0, 3, 0, 0, 0, 0, 0]\). There are only \(6\) non-zero values, and xor of \(a\) is equal to \(2\).In the second test case, \(m = 10 \cdot 15 = 150\) and has \(12\) divisors \([1, 2, 3, 5, 6, 10, 15, 25, 30, 50, 75, 150]\). All divisors except \(75\) and \(150\) are present in the \(10 \times 10\) table. Array \(a\) \(=\) \([1, 1, 1, 1, 1, 1, 3, 5, 3, 5, 0, 0]\). There are \(10\) non-zero values, and xor of \(a\) is equal to \(0\).In the third test case, \(m = 1 \cdot 210 = 210\) and has \(16\) divisors \([1, 2, 3, 5, 6, 7, 10, 14, 15, 21, 30, 35, 42, 70, 105, 210]\). The \(6 \times 6\) table with marked divisors is shown below: 1234561123456224681012336912151844812162024551015202530661218243036 Array \(a\) \(=\) \([1, 1, 1, 1, 1, 0, 2, 0, 3, 0, 5, 0, 0, 0, 0, 0]\). There are \(8\) non-zero values, and xor of \(a\) is equal to \(5\).
|
Input: 33 72 110 10 156 1 210 | Output: 6 2 10 0 8 5
|
Expert
| 4 | 388 | 334 | 543 | 17 |
1,090 |
B
|
1090B
| 1,900 |
Hard
| 0 | 0 | 0 | 0 | 10 |
|||||||
321 |
A
|
321A
|
A. Ciel and Robot
| 1,700 |
binary search; implementation; math
|
Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operation. There are four move operations at all: 'U': go up, (x, y) β (x, y+1); 'D': go down, (x, y) β (x, y-1); 'L': go left, (x, y) β (x-1, y); 'R': go right, (x, y) β (x+1, y). The robot will do the operations in s from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (a, b).
|
The first line contains two integers a and b, ( - 109 β€ a, b β€ 109). The second line contains a string s (1 β€ |s| β€ 100, s only contains characters 'U', 'D', 'L', 'R') β the command.
|
Print ""Yes"" if the robot will be located at (a, b), and ""No"" otherwise.
|
In the first and second test case, command string is ""RU"", so the robot will go right, then go up, then right, and then up and so on.The locations of its moves are (0, 0) β (1, 0) β (1, 1) β (2, 1) β (2, 2) β ...So it can reach (2, 2) but not (1, 2).
|
Input: 2 2RU | Output: Yes
|
Medium
| 3 | 524 | 182 | 75 | 3 |
1,450 |
F
|
1450F
|
F. The Struggling Contestant
| 2,400 |
constructive algorithms; greedy
|
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants.The contest consists of \(n\) problems, where the tag of the \(i\)-th problem is denoted by an integer \(a_i\).You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order.Formally, your solve order can be described by a permutation \(p\) of length \(n\). The cost of a permutation is defined as the number of indices \(i\) (\(1\le i<n\)) where \(|p_{i+1}-p_i|>1\). You have the requirement that \(a_{p_i}\ne a_{p_{i+1}}\) for all \(1\le i< n\).You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it.
|
The first line contains a single integer \(t\) (\(1\leq t\leq 10^4\)) β the number of test cases.The first line of the description of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of problems in the contest.The next line contains \(n\) integers \(a_1,a_2,\ldots a_n\) (\(1 \le a_i \le n\)) β the tags of the problems.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, if there are no permutations that satisfy the required condition, print \(-1\). Otherwise, print the minimum possible cost of a permutation that satisfies the required condition.
|
In the first test case, let \(p=[5, 4, 3, 2, 1, 6]\). The cost is \(1\) because we jump from \(p_5=1\) to \(p_6=6\), and \(|6-1|>1\). This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than \(1\).In the second test case, let \(p=[1,5,2,4,3]\). The cost is \(3\) because \(|p_2-p_1|>1\), \(|p_3-p_2|>1\), and \(|p_4-p_3|>1\). The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than \(3\).In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is \(-1\).
|
Input: 4 6 2 1 2 3 1 1 5 1 1 1 2 2 8 7 7 2 7 7 1 8 7 10 1 2 3 4 1 1 2 3 4 1 | Output: 1 3 -1 2
|
Expert
| 2 | 1,231 | 438 | 198 | 14 |
1,416 |
D
|
1416D
|
D. Graph and Queries
| 2,600 |
data structures; dsu; graphs; implementation; trees
|
You are given an undirected graph consisting of \(n\) vertices and \(m\) edges. Initially there is a single integer written on every vertex: the vertex \(i\) has \(p_i\) written on it. All \(p_i\) are distinct integers from \(1\) to \(n\).You have to process \(q\) queries of two types: \(1\) \(v\) β among all vertices reachable from the vertex \(v\) using the edges of the graph (including the vertex \(v\) itself), find a vertex \(u\) with the largest number \(p_u\) written on it, print \(p_u\) and replace \(p_u\) with \(0\); \(2\) \(i\) β delete the \(i\)-th edge from the graph. Note that, in a query of the first type, it is possible that all vertices reachable from \(v\) have \(0\) written on them. In this case, \(u\) is not explicitly defined, but since the selection of \(u\) does not affect anything, you can choose any vertex reachable from \(v\) and print its value (which is \(0\)).
|
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 3 \cdot 10^5\); \(1 \le q \le 5 \cdot 10^5\)).The second line contains \(n\) distinct integers \(p_1\), \(p_2\), ..., \(p_n\), where \(p_i\) is the number initially written on vertex \(i\) (\(1 \le p_i \le n\)).Then \(m\) lines follow, the \(i\)-th of them contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le n\), \(a_i \ne b_i\)) and means that the \(i\)-th edge connects vertices \(a_i\) and \(b_i\). It is guaranteed that the graph does not contain multi-edges.Then \(q\) lines follow, which describe the queries. Each line is given by one of the following formats: \(1\) \(v\) β denotes a query of the first type with a vertex \(v\) (\(1 \le v \le n\)). \(2\) \(i\) β denotes a query of the second type with an edge \(i\) (\(1 \le i \le m\)). For each query of the second type, it is guaranteed that the corresponding edge is not deleted from the graph yet.
|
For every query of the first type, print the value of \(p_u\) written on the chosen vertex \(u\).
|
Input: 5 4 6 1 2 5 4 3 1 2 2 3 1 3 4 5 1 1 2 1 2 3 1 1 1 2 1 2 | Output: 5 1 2 0
|
Expert
| 5 | 899 | 980 | 97 | 14 |
|
1,137 |
B
|
1137B
|
B. Camp Schedule
| 1,600 |
greedy; hashing; strings
|
The new camp by widely-known over the country Spring Programming Camp is going to start soon. Hence, all the team of friendly curators and teachers started composing the camp's schedule. After some continuous discussion, they came up with a schedule \(s\), which can be represented as a binary string, in which the \(i\)-th symbol is '1' if students will write the contest in the \(i\)-th day and '0' if they will have a day off.At the last moment Gleb said that the camp will be the most productive if it runs with the schedule \(t\) (which can be described in the same format as schedule \(s\)). Since the number of days in the current may be different from number of days in schedule \(t\), Gleb required that the camp's schedule must be altered so that the number of occurrences of \(t\) in it as a substring is maximum possible. At the same time, the number of contest days and days off shouldn't change, only their order may change.Could you rearrange the schedule in the best possible way?
|
The first line contains string \(s\) (\(1 \leqslant |s| \leqslant 500\,000\)), denoting the current project of the camp's schedule.The second line contains string \(t\) (\(1 \leqslant |t| \leqslant 500\,000\)), denoting the optimal schedule according to Gleb.Strings \(s\) and \(t\) contain characters '0' and '1' only.
|
In the only line print the schedule having the largest number of substrings equal to \(t\). Printed schedule should consist of characters '0' and '1' only and the number of zeros should be equal to the number of zeros in \(s\) and the number of ones should be equal to the number of ones in \(s\).In case there multiple optimal schedules, print any of them.
|
In the first example there are two occurrences, one starting from first position and one starting from fourth position.In the second example there is only one occurrence, which starts from third position. Note, that the answer is not unique. For example, if we move the first day (which is a day off) to the last position, the number of occurrences of \(t\) wouldn't change.In the third example it's impossible to make even a single occurrence.
|
Input: 101101 110 | Output: 110110
|
Medium
| 3 | 996 | 319 | 357 | 11 |
628 |
C
|
628C
|
C. Bear and String Distance
| 1,300 |
greedy; strings
|
Limak is a little polar bear. He likes nice strings β strings of length n, consisting of lowercase English letters only.The distance between two letters is defined as the difference between their positions in the alphabet. For example, , and .Also, the distance between two nice strings is defined as the sum of distances of corresponding letters. For example, , and .Limak gives you a nice string s and an integer k. He challenges you to find any nice string s' that . Find any s' satisfying the given conditions, or print ""-1"" if it's impossible to do so.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use gets/scanf/printf instead of getline/cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
|
The first line contains two integers n and k (1 β€ n β€ 105, 0 β€ k β€ 106).The second line contains a string s of length n, consisting of lowercase English letters.
|
If there is no string satisfying the given conditions then print ""-1"" (without the quotes).Otherwise, print any nice string s' that .
|
Input: 4 26bear | Output: roar
|
Easy
| 2 | 807 | 161 | 135 | 6 |
|
1,399 |
E1
|
1399E1
|
E1. Weights Division (easy version)
| 2,000 |
data structures; dfs and similar; greedy; trees
|
Easy and hard versions are actually different problems, so we advise you to read both statements carefully.You are given a weighted rooted tree, vertex \(1\) is the root of this tree.A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex \(v\) is the last different from \(v\) vertex on the path from the root to the vertex \(v\). Children of vertex \(v\) are all vertices for which \(v\) is the parent. A vertex is a leaf if it has no children. The weighted tree is such a tree that each edge of this tree has some weight.The weight of the path is the sum of edges weights on this path. The weight of the path from the vertex to itself is \(0\).You can make a sequence of zero or more moves. On each move, you select an edge and divide its weight by \(2\) rounding down. More formally, during one move, you choose some edge \(i\) and divide its weight by \(2\) rounding down (\(w_i := \left\lfloor\frac{w_i}{2}\right\rfloor\)).Your task is to find the minimum number of moves required to make the sum of weights of paths from the root to each leaf at most \(S\). In other words, if \(w(i, j)\) is the weight of the path from the vertex \(i\) to the vertex \(j\), then you have to make \(\sum\limits_{v \in leaves} w(root, v) \le S\), where \(leaves\) is the list of all leaves.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(S\) (\(2 \le n \le 10^5; 1 \le S \le 10^{16}\)) β the number of vertices in the tree and the maximum possible sum of weights you have to obtain. The next \(n-1\) lines describe edges of the tree. The edge \(i\) is described as three integers \(v_i\), \(u_i\) and \(w_i\) (\(1 \le v_i, u_i \le n; 1 \le w_i \le 10^6\)), where \(v_i\) and \(u_i\) are vertices the edge \(i\) connects and \(w_i\) is the weight of this edge.It is guaranteed that the sum of \(n\) does not exceed \(10^5\) (\(\sum n \le 10^5\)).
|
For each test case, print the answer: the minimum number of moves required to make the sum of weights of paths from the root to each leaf at most \(S\).
|
Input: 3 3 20 2 1 8 3 1 7 5 50 1 3 100 1 5 10 2 3 123 5 4 55 2 100 1 2 409 | Output: 0 8 3
|
Hard
| 4 | 1,388 | 716 | 152 | 13 |
|
1,063 |
E
|
1063E
|
E. Lasers and Mirrors
| 3,000 |
constructive algorithms; math
|
Oleg came to see the maze of mirrors. The maze is a \(n\) by \(n\) room in which each cell is either empty or contains a mirror connecting opposite corners of this cell. Mirrors in this maze reflect light in a perfect way, which causes the interesting visual effects and contributes to the loss of orientation in the maze.Oleg is a person of curious nature, so he decided to install \(n\) lasers facing internal of the maze on the south wall of the maze. On the north wall of the maze, Oleg installed \(n\) receivers, also facing internal of the maze. Let's number lasers and receivers from west to east with distinct integers from \(1\) to \(n\). Each laser sends a beam of some specific kind and receiver with number \(a_i\) should receive the beam sent from laser number \(i\). Since two lasers' beams can't come to the same receiver, these numbers form a permutation β each of the receiver numbers occurs exactly once.You came to the maze together with Oleg. Help him to place the mirrors in the initially empty maze so that the maximum number of lasers' beams will come to the receivers they should. There are no mirrors outside the maze, so if the laser beam leaves the maze, it will not be able to go back.
|
The first line contains a single integer \(n\) (\(1 \le n \le 1000\)) β the size of the maze.The second line contains a permutation of \(n\) integers \(a_i\) (\(1 \le a_i \le n\)), where \(a_i\) defines the number of the receiver, to which the beam from \(i\)-th laser should come.
|
In the first line print the maximum possible number of laser beams, which can come to the receivers they should.In the next \(n\) lines of length \(n\) print the arrangement of mirrors, causing such number of laser beams to come where they should. If the corresponding cell is empty, print ""."", otherwise print ""/"" or ""\"", depending on the orientation of the mirror.In your output north should be above, south should be below, and west and east should be on left and on the right respectively.It is allowed for laser beams to come not to the receivers they correspond to, but they are not counted in the answer.If there are multiple arrangements of mirrors leading to the optimal answer β print any of them.
|
The picture illustrates the arrangements of the mirrors in the first example.
|
Input: 44 1 3 2 | Output: 3.\..\\../../...\
|
Master
| 2 | 1,213 | 281 | 713 | 10 |
845 |
E
|
845E
|
E. Fire in the City
| 2,400 |
binary search; data structures
|
The capital of Berland looks like a rectangle of size n Γ m of the square blocks of same size.Fire!It is known that k + 1 blocks got caught on fire (k + 1 β€ nΒ·m). Those blocks are centers of ignition. Moreover positions of k of these centers are known and one of these stays unknown. All k + 1 positions are distinct.The fire goes the following way: during the zero minute of fire only these k + 1 centers of ignition are burning. Every next minute the fire goes to all neighbouring blocks to the one which is burning. You can consider blocks to burn for so long that this time exceeds the time taken in the problem. The neighbouring blocks are those that touch the current block by a side or by a corner.Berland Fire Deparment wants to estimate the minimal time it takes the fire to lighten up the whole city. Remember that the positions of k blocks (centers of ignition) are known and (k + 1)-th can be positioned in any other block.Help Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city.
|
The first line contains three integers n, m and k (1 β€ n, m β€ 109, 1 β€ k β€ 500).Each of the next k lines contain two integers xi and yi (1 β€ xi β€ n, 1 β€ yi β€ m) β coordinates of the i-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct.
|
Print the minimal time it takes the fire to lighten up the whole city (in minutes).
|
In the first example the last block can have coordinates (4, 4).In the second example the last block can have coordinates (8, 3).
|
Input: 7 7 31 22 15 5 | Output: 3
|
Expert
| 2 | 1,040 | 283 | 83 | 8 |
1,503 |
C
|
1503C
|
C. Travelling Salesman Problem
| 2,200 |
binary search; data structures; dp; greedy; shortest paths; sortings; two pointers
|
There are \(n\) cities numbered from \(1\) to \(n\), and city \(i\) has beauty \(a_i\).A salesman wants to start at city \(1\), visit every city exactly once, and return to city \(1\).For all \(i\ne j\), a flight from city \(i\) to city \(j\) costs \(\max(c_i,a_j-a_i)\) dollars, where \(c_i\) is the price floor enforced by city \(i\). Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip.
|
The first line contains a single integer \(n\) (\(2\le n\le 10^5\)) β the number of cities.The \(i\)-th of the next \(n\) lines contains two integers \(a_i\), \(c_i\) (\(0\le a_i,c_i\le 10^9\)) β the beauty and price floor of the \(i\)-th city.
|
Output a single integer β the minimum total cost.
|
In the first test case, we can travel in order \(1\to 3\to 2\to 1\). The flight \(1\to 3\) costs \(\max(c_1,a_3-a_1)=\max(9,4-1)=9\). The flight \(3\to 2\) costs \(\max(c_3, a_2-a_3)=\max(1,2-4)=1\). The flight \(2\to 1\) costs \(\max(c_2,a_1-a_2)=\max(1,1-2)=1\). The total cost is \(11\), and we cannot do better.
|
Input: 3 1 9 2 1 4 1 | Output: 11
|
Hard
| 7 | 441 | 244 | 49 | 15 |
1,744 |
C
|
1744C
|
C. Traffic Light
| 1,000 |
binary search; implementation; two pointers
|
You find yourself on an unusual crossroad with a weird traffic light. That traffic light has three possible colors: red (r), yellow (y), green (g). It is known that the traffic light repeats its colors every \(n\) seconds and at the \(i\)-th second the color \(s_i\) is on.That way, the order of the colors is described by a string. For example, if \(s=\)""rggry"", then the traffic light works as the following: red-green-green-red-yellow-red-green-green-red-yellow- ... and so on.More formally, you are given a string \(s_1, s_2, \ldots, s_n\) of length \(n\). At the first second the color \(s_1\) is on, at the second β \(s_2\), ..., at the \(n\)-th second the color \(s_n\) is on, at the \(n + 1\)-st second the color \(s_1\) is on and so on.You need to cross the road and that can only be done when the green color is on. You know which color is on the traffic light at the moment, but you don't know the current moment of time. You need to find the minimum amount of time in which you are guaranteed to cross the road.You can assume that you cross the road immediately. For example, with \(s=\)""rggry"" and the current color r there are two options: either the green color will be on after \(1\) second, or after \(3\). That way, the answer is equal to \(3\) β that is the number of seconds that we are guaranteed to cross the road, if the current color is r.
|
The first line contains a single integer \(t\) \((1 \leq t \leq 10^4\)) β the number of test cases.Then the description of the test cases follows.The first line of each test case contains an integer \(n\) and a symbol \(c\) (\(1 \leq n \leq 2 \cdot 10^5\), \(c\) is one of allowed traffic light colors r, y or g)β the length of the string \(s\) and the current color of the traffic light. The second line of each test case contains a string \(s\) of the length \(n\), consisting of the letters r, y and g.It is guaranteed that the symbol g is in the string \(s\) and the symbol \(c\) is in the string \(s\). It is guaranteed, that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case output the minimal number of second in which you are guaranteed to cross the road.
|
The first test case is explained in the statement.In the second test case the green color is on so you can cross the road immediately. In the third test case, if the red color was on at the second second, then we would wait for the green color for one second, and if the red light was on at the first second, then we would wait for the green light for two seconds.In the fourth test case the longest we would wait for the green color is if we wait for it starting from the fifth second.
|
Input: 65 rrggry1 gg3 rrrg5 yyrrgy7 rrgrgyrg9 yrrrgyyygy | Output: 3 0 2 4 1 4
|
Beginner
| 3 | 1,367 | 699 | 101 | 17 |
583 |
B
|
583B
|
B. Robot's Task
| 1,200 |
greedy; implementation
|
Robot Doc is located in the hall, with n computers stand in a line, numbered from left to right from 1 to n. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the i-th of them, the robot needs to collect at least ai any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all n parts of information if initially it is next to computer with number 1.It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information.
|
The first line contains number n (1 β€ n β€ 1000). The second line contains n non-negative integers a1, a2, ..., an (0 β€ ai < n), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information.
|
Print a single number β the minimum number of changes in direction that the robot will have to make in order to collect all n parts of information.
|
In the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7.
|
Input: 30 2 0 | Output: 1
|
Easy
| 2 | 1,000 | 242 | 147 | 5 |
1,973 |
C
|
1973C
|
C. Cat, Fox and Double Maximum
| 1,700 |
constructive algorithms; greedy; implementation; math; sortings
|
Fox loves permutations! She came up with the following problem and asked Cat to solve it:You are given an even positive integer \(n\) and a permutation\(^\dagger\) \(p\) of length \(n\). The score of another permutation \(q\) of length \(n\) is the number of local maximums in the array \(a\) of length \(n\), where \(a_i = p_i + q_i\) for all \(i\) (\(1 \le i \le n\)). In other words, the score of \(q\) is the number of \(i\) such that \(1 < i < n\) (note the strict inequalities), \(a_{i-1} < a_i\), and \(a_i > a_{i+1}\) (once again, note the strict inequalities). Find the permutation \(q\) that achieves the maximum score for given \(n\) and \(p\). If there exist multiple such permutations, you can pick any of them.\(^\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).
|
The first line of input contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases in the input you will have to solve.The first line of each test case contains one even integer \(n\) (\(4 \leq n \leq 10^5\), \(n\) is even) β the length of the permutation \(p\).The second line of each test case contains the \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)). It is guaranteed that \(p\) is a permutation of length \(n\).It is guaranteed that the sum of \(n\) across all test cases doesn't exceed \(10^5\).
|
For each test case, output one line containing any permutation of length \(n\) (the array \(q\)), such that \(q\) maximizes the score under the given constraints.
|
In the first example, \(a = [3, 6, 4, 7]\). The array has just one local maximum (on the second position), so the score of the chosen permutation \(q\) is \(1\). It can be proven that this score is optimal under the constraints.In the last example, the resulting array \(a = [6, 6, 12, 7, 14, 7, 14, 6]\) has \(3\) local maximums β on the third, fifth and seventh positions.
|
Input: 441 2 3 444 3 1 266 5 1 4 2 381 2 4 5 7 6 8 3 | Output: 2 4 1 3 3 1 4 2 2 5 1 4 3 6 5 4 8 2 7 1 6 3
|
Medium
| 5 | 1,061 | 540 | 162 | 19 |
85 |
E
|
85E
|
E. Guard Towers
| 2,600 |
binary search; dsu; geometry; graphs; sortings
|
In a far away kingdom lives a very greedy king. To defend his land, he built n guard towers. Apart from the towers the kingdom has two armies, each headed by a tyrannical and narcissistic general. The generals can't stand each other, specifically, they will never let soldiers of two armies be present in one tower.During defence operations to manage a guard tower a general has to send part of his army to that tower. Each general asks some fee from the king for managing towers. As they live in a really far away kingdom, each general evaluates his fee in the following weird manner: he finds two remotest (the most distant) towers, where the soldiers of his army are situated and asks for the fee equal to the distance. Each tower is represented by a point on the plane with coordinates (x, y), and the distance between two points with coordinates (x1, y1) and (x2, y2) is determined in this kingdom as |x1 - x2| + |y1 - y2|.The greedy king was not exactly satisfied with such a requirement from the generals, that's why he only agreed to pay one fee for two generals, equal to the maximum of two demanded fees. However, the king is still green with greed, and among all the ways to arrange towers between armies, he wants to find the cheapest one. Each tower should be occupied by soldiers of exactly one army.He hired you for that. You should find the minimum amount of money that will be enough to pay the fees. And as the king is also very scrupulous, you should also count the number of arrangements that will cost the same amount of money. As their number can be quite large, it is enough for the king to know it as a remainder from dividing by 109 + 7.Two arrangements are distinct if the sets of towers occupied by soldiers of the first general are distinct.
|
The first line contains an integer n (2 β€ n β€ 5000), n is the number of guard towers. Then follow n lines, each of which contains two integers x, y β the coordinates of the i-th tower (0 β€ x, y β€ 5000). No two towers are present at one point.Pretest 6 is one of the maximal tests for this problem.
|
Print on the first line the smallest possible amount of money that will be enough to pay fees to the generals. Print on the second line the number of arrangements that can be carried out using the smallest possible fee. This number should be calculated modulo 1000000007 (109 + 7).
|
In the first example there are only two towers, the distance between which is equal to 2. If we give both towers to one general, then we well have to pay 2 units of money. If each general receives a tower to manage, to fee will be equal to 0. That is the smallest possible fee. As you can easily see, we can obtain it in two ways.
|
Input: 20 01 1 | Output: 02
|
Expert
| 5 | 1,769 | 297 | 281 | 0 |
3 |
B
|
3B
|
B. Lorry
| 1,900 |
greedy; sortings
|
A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.
|
The first line contains a pair of integer numbers n and v (1 β€ n β€ 105; 1 β€ v β€ 109), where n is the number of waterborne vehicles in the boat depot, and v is the truck body volume of the lorry in cubic metres. The following n lines contain the information about the waterborne vehicles, that is a pair of numbers ti, pi (1 β€ ti β€ 2; 1 β€ pi β€ 104), where ti is the vehicle type (1 β a kayak, 2 β a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.
|
In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.
|
Input: 3 21 22 71 3 | Output: 72
|
Hard
| 2 | 1,005 | 529 | 225 | 0 |
|
1,839 |
E
|
1839E
|
E. Decreasing Game
| 2,400 |
constructive algorithms; dfs and similar; dp; greedy; interactive
|
This is an interactive problem.Consider the following game for two players: Initially, an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\) is written on blackboard. Game consists of rounds. On each round, the following happens: The first player selects any \(i\) such that \(a_i \gt 0\). If there is no such \(i\), the first player loses the game (the second player wins) and game ends. The second player selects any \(j \neq i\) such that \(a_j \gt 0\). If there is no such \(j\), the second player loses the game (the first player wins) and game ends. Let \(d = \min(a_i, a_j)\). The values of \(a_i\) and \(a_j\) are simultaneously decreased by \(d\) and the next round starts. It can be shown that game always ends after the finite number of rounds.You have to select which player you will play for (first or second) and win the game.
|
The first line contains a single integer \(n\) (\(1 \le n \le 300\)) β the length of array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 300\)) β array \(a\).
|
In the first example \(n = 4\) and array \(a\) is \([\, 10, 4, 6, 3 \,]\). The game goes as follows: After reading array \(a\) contestant's program chooses to play as the first player and prints ""First"". First round: the first player chooses \(i = 1\), the second player chooses \(j = 3\). \(d = \min(a_1, a_3) = \min(10, 6) = 6\) is calculated. Elements \(a_1\) and \(a_3\) are decreased by \(6\). Array \(a\) becomes equal to \([\, 4, 4, 0, 3 \,]\). Second round: the first player chooses \(i = 2\), the second player chooses \(j = 1\). \(d = \min(a_2, a_1) = \min(4, 4) = 4\) is calculated. Elements \(a_2\) and \(a_1\) are decreased by \(4\). Array \(a\) becomes equal to \([\, 0, 0, 0, 3 \,]\). Third round: the first player chooses \(i = 4\). There is no \(j \neq 4\) such that \(a_j > 0\), so the second player can't make a correct move and the first player wins. Jury's program prints \(j = 0\). After reading it, contestant's program terminates. In the second example \(n = 6\) and array \(a\) is \([\, 4, 5, 5, 11, 3, 2 \,]\). The game goes as follows: Contestant's program chooses to play as the second player and prints ""Second"". First round: \(i = 2\), \(j = 4\), \(a = [\, 4, 0, 5, 6, 3, 2 \,]\). Second round: \(i = 5\), \(j = 4\), \(a = [\, 4, 0, 5, 3, 0, 2 \,]\). Third round: \(i = 4\), \(j = 3\), \(a = [\, 4, 0, 2, 0, 0, 2 \,]\). Fourth round: \(i = 6\), \(j = 1\), \(a = [\, 2, 0, 2, 0, 0, 0 \,]\). Fifth round: \(i = 1\), \(j = 3\), \(a = [\, 0, 0, 0, 0, 0, 0 \,]\). Sixth round: the first player can't make a correct move and the second player wins. Jury's program prints \(i = 0\). After reading it, contestant's program terminates. Note that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.
|
Input: 4 10 4 6 3 3 1 0 | Output: First 1 2 4
|
Expert
| 5 | 849 | 201 | 0 | 18 |
|
1,914 |
F
|
1914F
|
F. Programming Competition
| 1,900 |
dfs and similar; dp; graph matchings; greedy; trees
|
BerSoft is the biggest IT corporation in Berland. There are \(n\) employees at BerSoft company, numbered from \(1\) to \(n\). The first employee is the head of the company, and he does not have any superiors. Every other employee \(i\) has exactly one direct superior \(p_i\).Employee \(x\) is considered to be a superior (direct or indirect) of employee \(y\) if one of the following conditions holds: employee \(x\) is the direct superior of employee \(y\); employee \(x\) is a superior of the direct superior of employee \(y\). The structure of BerSoft is organized in such a way that the head of the company is superior of every employee.A programming competition is going to be held soon. Two-person teams should be created for this purpose. However, if one employee in a team is the superior of another, they are uncomfortable together. So, teams of two people should be created so that no one is the superior of the other. Note that no employee can participate in more than one team.Your task is to calculate the maximum possible number of teams according to the aforementioned rules.
|
The first line contains a single 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 2 \cdot 10^5\)) β the number of employees.The second line contains \(n-1\) integers \(p_2, p_3, \dots, p_n\) (\(1 \le p_i \le n\)), where \(p_i\) is the index of the direct superior of the \(i\)-th employee.The sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the maximum possible number of teams according to the aforementioned rules.
|
In the first test case, team \((3, 4)\) can be created.In the second test case, no team can be created, because there are only \(2\) employees and one is the superior of another.In the third test case, team \((2, 3)\) can be created.In the fourth test case, teams \((2, 4)\), \((3, 5)\) and \((6, 7)\) can be created.In the fifth test case, teams \((2, 3)\), \((6, 4)\) and \((5, 7)\) can be created.
|
Input: 641 2 12155 5 5 171 2 1 1 3 371 1 3 2 2 471 2 1 1 1 3 | Output: 1 0 1 3 3 3
|
Hard
| 5 | 1,091 | 453 | 120 | 19 |
407 |
E
|
407E
|
E. k-d-sequence
| 3,100 |
data structures
|
We'll call a sequence of integers a good k-d sequence if we can add to it at most k numbers in such a way that after the sorting the sequence will be an arithmetic progression with difference d.You got hold of some sequence a, consisting of n integers. Your task is to find its longest contiguous subsegment, such that it is a good k-d sequence.
|
The first line contains three space-separated integers n, k, d (1 β€ n β€ 2Β·105; 0 β€ k β€ 2Β·105; 0 β€ d β€ 109). The second line contains n space-separated integers: a1, a2, ..., an ( - 109 β€ ai β€ 109) β the actual sequence.
|
Print two space-separated integers l, r (1 β€ l β€ r β€ n) show that sequence al, al + 1, ..., ar is the longest subsegment that is a good k-d sequence.If there are multiple optimal answers, print the one with the minimum value of l.
|
In the first test sample the answer is the subsegment consisting of numbers 2, 8, 6 β after adding number 4 and sorting it becomes sequence 2, 4, 6, 8 β the arithmetic progression with difference 2.
|
Input: 6 1 24 3 2 8 6 2 | Output: 3 5
|
Master
| 1 | 345 | 219 | 230 | 4 |
1,538 |
A
|
1538A
|
A. Stone Game
| 800 |
brute force; dp; greedy
|
Polycarp is playing a new computer game. This game has \(n\) stones in a row. The stone on the position \(i\) has integer power \(a_i\). The powers of all stones are distinct.Each turn Polycarp can destroy either stone on the first position or stone on the last position (in other words, either the leftmost or the rightmost stone). When Polycarp destroys the stone it does not exist any more.Now, Polycarp wants two achievements. He gets them if he destroys the stone with the least power and the stone with the greatest power. Help Polycarp find out what is the minimum number of moves he should make in order to achieve his goal.For example, if \(n = 5\) and \(a = [1, 5, 4, 3, 2]\), then Polycarp could make the following moves: Destroy the leftmost stone. After this move \(a = [5, 4, 3, 2]\); Destroy the rightmost stone. After this move \(a = [5, 4, 3]\); Destroy the leftmost stone. After this move \(a = [4, 3]\). Polycarp destroyed the stones with the greatest and least power, so he can end the game. Please note that in the example above, you can complete the game in two steps. For example: Destroy the leftmost stone. After this move \(a = [5, 4, 3, 2]\); Destroy the leftmost stone. After this move \(a = [4, 3, 2]\). Polycarp destroyed the stones with the greatest and least power, so he can end the game.
|
The first line contains an integer \(t\) (\(1 \le t \le 100\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(2 \le n \le 100\)) β the number of stones.The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the power of the stones.
|
For each test case, output the minimum number of moves required to destroy the stones with the greatest and the lowest power.
|
Input: 5 5 1 5 4 3 2 8 2 1 3 4 5 6 8 7 8 4 2 3 1 8 6 7 5 4 3 4 2 1 4 2 3 1 4 | Output: 2 4 5 3 2
|
Beginner
| 3 | 1,321 | 321 | 125 | 15 |
|
1,343 |
C
|
1343C
|
C. Alternating Subsequence
| 1,200 |
dp; greedy; two pointers
|
Recall that the sequence \(b\) is a a subsequence of the sequence \(a\) if \(b\) can be derived from \(a\) by removing zero or more elements without changing the order of the remaining elements. For example, if \(a=[1, 2, 1, 3, 1, 2, 1]\), then possible subsequences are: \([1, 1, 1, 1]\), \([3]\) and \([1, 2, 1, 3, 1, 2, 1]\), but not \([3, 2, 3]\) and \([1, 1, 1, 1, 2]\).You are given a sequence \(a\) consisting of \(n\) positive and negative elements (there is no zeros in the sequence).Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.In other words, if the maximum length of alternating subsequence is \(k\) then your task is to find the maximum sum of elements of some alternating subsequence of length \(k\).You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\). The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9, a_i \ne 0\)), where \(a_i\) is the \(i\)-th element of \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
For each test case, print the answer β the maximum sum of the maximum by size (length) alternating subsequence of \(a\).
|
In the first test case of the example, one of the possible answers is \([1, 2, \underline{3}, \underline{-1}, -2]\).In the second test case of the example, one of the possible answers is \([-1, -2, \underline{-1}, -3]\).In the third test case of the example, one of the possible answers is \([\underline{-2}, 8, 3, \underline{8}, \underline{-4}, -15, \underline{5}, \underline{-2}, -3, \underline{1}]\).In the fourth test case of the example, one of the possible answers is \([\underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}, \underline{1}, \underline{-1000000000}]\).
|
Input: 4 5 1 2 3 -1 -2 4 -1 -2 -1 -3 10 -2 8 3 8 -4 -15 5 -2 -3 1 6 1 -1000000000 1 -1000000000 1 -1000000000 | Output: 2 -1 6 -2999999997
|
Easy
| 3 | 1,075 | 552 | 120 | 13 |
337 |
A
|
337A
|
A. Puzzles
| 900 |
greedy
|
The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her n students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces).The shop assistant told the teacher that there are m puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of f1 pieces, the second one consists of f2 pieces and so on.Ms. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let A be the number of pieces in the largest puzzle that the teacher buys and B be the number of pieces in the smallest such puzzle. She wants to choose such n puzzles that A - B is minimum possible. Help the teacher and find the least possible value of A - B.
|
The first line contains space-separated integers n and m (2 β€ n β€ m β€ 50). The second line contains m space-separated integers f1, f2, ..., fm (4 β€ fi β€ 1000) β the quantities of pieces in the puzzles sold in the shop.
|
Print a single integer β the least possible difference the teacher can obtain.
|
Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.
|
Input: 4 610 12 10 7 5 22 | Output: 5
|
Beginner
| 1 | 1,014 | 218 | 78 | 3 |
710 |
B
|
710B
|
B. Optimal Point on a Line
| 1,400 |
brute force; sortings
|
You are given n points on a line with their coordinates xi. Find the point x so the sum of distances to the given points is minimal.
|
The first line contains integer n (1 β€ n β€ 3Β·105) β the number of points on the line.The second line contains n integers xi ( - 109 β€ xi β€ 109) β the coordinates of the given n points.
|
Print the only integer x β the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.
|
Input: 41 2 3 4 | Output: 2
|
Easy
| 2 | 132 | 184 | 205 | 7 |
|
2,084 |
E
|
2084E
|
E. Blossom
| 2,400 |
binary search; combinatorics; dp; implementation; math; two pointers
|
You are given a permutation \(a\) of length \(n\)\(^{\text{β}}\) where some elements are missing and represented by \(-1\).Define the value of a permutation as the sum of the MEX\(^{\text{β }}\) of all its non-empty subsegments\(^{\text{β‘}}\).Find the sum of the value of all possible valid permutations that can be formed by filling in the missing elements of \(a\) modulo \(10^9 + 7\).\(^{\text{β}}\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(\bf{0}\) to \(\bf{n - 1}\) in arbitrary order. For example, \([1,2,0,4,3]\) is a permutation, but \([0,1,1]\) is not a permutation (\(1\) appears twice in the array), and \([0,2,3]\) is also not a permutation (\(n=3\) but there is \(3\) in the array). \(^{\text{β }}\)The minimum excluded (MEX) of a collection of integers \(c_1, c_2, \ldots, c_k\) is defined as the smallest non-negative integer \(x\) which does not occur in the collection \(c\). \(^{\text{β‘}}\)A sequence \(a\) is a subsegment of a sequence \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-1 \le a_i < n\)).It is guaranteed that the elements of \(a\) that are not \(-1\) are distinct.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5000\).
|
For each test case, output a single integer β the sum of the value of all possible valid permutations modulo \(10^9 + 7\).
|
In the first test case, the only valid permutation is \([0, 1]\), and the value of \([0, 1]\) is \(3\) since:$$$\(\operatorname{mex}([0]) + \operatorname{mex}([1]) + \operatorname{mex}([0, 1]) = 1 + 0 + 2 = 3\)\(So the answer is \)3\(.In the second test case, there are two valid permutations: \)[0, 1]\( and \)[1, 0]\(. The value of \)[0, 1]\( and the value of \)[1, 0]\( is \)3\(, so the answer is \)3 + 3 = 6\(.In the fourth test case, there are two valid permutations: \)[0, 2, 1]\( and \)[1, 2, 0]\(. The value of \)[0, 2, 1]\( is \)5\( since:\)\(\operatorname{mex}([0]) + \operatorname{mex}([2]) + \operatorname{mex}([1]) + \operatorname{mex}([0, 2]) + \operatorname{mex}([2, 1]) + \operatorname{mex}([0, 2, 1]) = 1 + 0 + 0 + 1 + 0 + 3 = 5\)\(And the value of \)[1, 2, 0]\( is \)5\( since:\)\(\operatorname{mex}([1]) + \operatorname{mex}([2]) + \operatorname{mex}([0]) + \operatorname{mex}([1, 2]) + \operatorname{mex}([2, 0]) + \operatorname{mex}([1, 2, 0]) = 0 + 0 + 1 + 0 + 1 + 3 = 5\)\(So the answer is \)5 + 5 = 10$$$.
|
Input: 520 -12-1 -132 0 13-1 2 -15-1 0 -1 2 -1 | Output: 3 6 7 10 104
|
Expert
| 6 | 1,178 | 498 | 122 | 20 |
1,606 |
C
|
1606C
|
C. Banknotes
| 1,400 |
greedy; number theory
|
In Berland, \(n\) different types of banknotes are used. Banknotes of the \(i\)-th type have denomination \(10^{a_i}\) burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly \(1\).Let's denote \(f(s)\) as the minimum number of banknotes required to represent exactly \(s\) burles. For example, if the denominations of banknotes used in Berland are \(1\), \(10\) and \(100\), then \(f(59) = 14\): \(9\) banknotes with denomination of \(1\) burle and \(5\) banknotes with denomination of \(10\) burles can be used to represent exactly \(9 \cdot 1 + 5 \cdot 10 = 59\) burles, and there's no way to do it with fewer banknotes.For a given integer \(k\), find the minimum positive number of burles \(s\) that cannot be represented with \(k\) or fewer banknotes (that is, \(f(s) > k\)).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10; 1 \le k \le 10^9\)).The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 = a_1 < a_2 < \dots < a_n \le 9\)).
|
For each test case, print one integer β the minimum positive number of burles \(s\) that cannot be represented with \(k\) or fewer banknotes.
|
Input: 4 3 13 0 1 2 2 777 0 4 3 255 0 1 3 10 1000000000 0 1 2 3 4 5 6 7 8 9 | Output: 59 778 148999 999999920999999999
|
Easy
| 2 | 833 | 306 | 141 | 16 |
|
952 |
F
|
952F
|
F. 2 + 2 != 4
| 2,400 |
*special
|
One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression. However, looks like there is a bug in the reference solution...
|
The only line of input data contains the arithmetic expression. The expression will contain between 2 and 10 operands, separated with arithmetic signs plus and/or minus. Each operand will be an integer between 0 and 255, inclusive.
|
Reproduce the output of the reference solution, including the bug.
|
Input: 8-7+6-5+4-3+2-1-0 | Output: 4
|
Expert
| 1 | 265 | 231 | 66 | 9 |
|
1,104 |
B
|
1104B
|
B. Game with string
| 1,200 |
data structures; implementation; math
|
Two people are playing a game with a string \(s\), consisting of lowercase latin letters. On a player's turn, he should choose two consecutive equal letters in the string and delete them. For example, if the string is equal to ""xaax"" than there is only one possible turn: delete ""aa"", so the string will become ""xx"". A player not able to make a turn loses.Your task is to determine which player will win if both play optimally.
|
The only line contains the string \(s\), consisting of lowercase latin letters (\(1 \leq |s| \leq 100\,000\)), where \(|s|\) means the length of a string \(s\).
|
If the first player wins, print ""Yes"". If the second player wins, print ""No"".
|
In the first example the first player is unable to make a turn, so he loses.In the second example first player turns the string into ""q"", then second player is unable to move, so he loses.
|
Input: abacaba | Output: No
|
Easy
| 3 | 433 | 160 | 81 | 11 |
639 |
F
|
639F
|
F. Bear and Chemistry
| 3,300 |
data structures; dfs and similar; graphs; trees
|
Limak is a smart brown bear who loves chemistry, reactions and transforming elements.In Bearland (Limak's home) there are n elements, numbered 1 through n. There are also special machines, that can transform elements. Each machine is described by two integers ai, bi representing two elements, not necessarily distinct. One can use a machine either to transform an element ai to bi or to transform bi to ai. Machines in Bearland aren't very resistant and each of them can be used at most once. It is possible that ai = bi and that many machines have the same pair ai, bi.Radewoosh is Limak's biggest enemy and rival. He wants to test Limak in the chemistry. They will meet tomorrow and both of them will bring all their machines. Limak has m machines but he doesn't know much about his enemy. They agreed Radewoosh will choose two distinct elements, let's denote them as x and y. Limak will be allowed to use both his and Radewoosh's machines. He may use zero or more (maybe even all) machines to achieve the goal, each machine at most once. Limak will start from an element x and his task will be to first get an element y and then to again get an element x β then we say that he succeeds. After that Radewoosh would agree that Limak knows the chemistry (and Radewoosh would go away).Radewoosh likes some particular non-empty set of favorite elements and he will choose x, y from that set. Limak doesn't know exactly which elements are in the set and also he doesn't know what machines Radewoosh has. Limak has heard q gossips (queries) though and each of them consists of Radewoosh's machines and favorite elements. For each gossip Limak wonders if he would be able to succeed tomorrow for every pair x, y chosen from the set of favorite elements. If yes then print ""YES"" (without the quotes). But if there exists a pair (x, y) from the given set that Limak wouldn't be able to succeed then you should print ""NO"" (without the quotes).
|
The first line contains three integers n, m and q (1 β€ n, q β€ 300 000, 0 β€ m β€ 300 000) β the number of elements, the number of Limak's machines and the number of gossips, respectively.Each of the next m lines contains two integers ai and bi (1 β€ ai, bi β€ n) describing one of Limak's machines.Then, the description of q gossips follows.The first line of the description of the i-th gossip contains two integers ni and mi (1 β€ ni β€ 300 000, 0 β€ mi β€ 300 000). The second line contains ni distinct integers xi, 1, xi, 2, ..., xi, ni (1 β€ xi, j β€ n) β Radewoosh's favorite elements in the i-th gossip. Note that ni = 1 is allowed, in this case there are no pairs of distinct elements, so Limak automatically wins (the answer is ""YES""). Then mi lines follow, each containing two integers ai, j, bi, j (1 β€ ai, j, bi, j) describing one of Radewoosh's machines in the i-th gossip.The sum of ni over all gossips won't exceed 300 000. Also, the sum of mi over all gossips won't exceed 300 000.Important: Because we want you to process the gossips online, in order to know the elements in Radewoosh's favorite set and elements that his machines can transform, for on each number that denotes them in the input you should use following function:int rotate(int element){ element=(element+R)%n; if (element==0) { element=n; } return element;}where R is initially equal to 0 and is increased by the number of the query any time the answer is ""YES"". Queries are numbered starting with 1 in the order they appear in the input.
|
You should print q lines. The i-th of them should contain ""YES"" (without quotes) if for the i-th gossip for each pair of elements x and y (in the set xi, 1, xi, 2, ..., xi, ni) Limak is able to succeed. Otherwise you should print ""NO"" (without quotes).
|
Lets look at first sample:In first gossip Radewoosh's favorite set is {4, 2} and he has no machines. Limak can tranform element 4 into 2 (so half of a task is complete) and then 2 into 3, and 3 into 4. Answer is ""YES"", so R is increased by 1.In second gossip set in the input is denoted by {6, 2} and machine by (3, 4), but R is equal to 1, so set is {1, 3} and machine is (4, 5). Answer is ""NO"", so R isn't changed.In third gossip set {6, 4, 3} and machines (2, 5) and (4, 6) are deciphered to be {1, 5, 4}, (3, 6) and (5, 1).Consider Radewoosh's choices: If he chooses elements 1 and 5, then Limak is able to transform 1 into 5, then 6 into 3, 3 into 2 and 2 into 1. If he chooses elements 5 and 4, then Limak is able to transform 5 into 6, 6 into 3, 3 into 4 (half way already behind him), 4 into 2, 2 into 1, 1 into 5. If he chooses elements 1 and 4, then Limak is able to transform 1 into 2, 2 into 4, 4 into 3, 3 into 6, 6 into 5 and 5 into 1. So Limak is able to execute task. Answer is ""YES"" and R is increased by 3 (it's equal to 4 now).In last gossip {1, 2} and (1, 2) are deciphered to be {5, 6} and (5, 6). Now there are 2 machines (5, 6) so Limak is able to execute task again.
|
Input: 6 5 41 22 33 42 45 62 04 22 16 23 43 26 3 42 54 62 11 21 2 | Output: YESNOYESYES
|
Master
| 4 | 1,940 | 1,516 | 256 | 6 |
1,999 |
C
|
1999C
|
C. Showering
| 800 |
greedy; implementation
|
As a computer science student, Alex faces a hard challenge β showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes \(s\) minutes to shower and a day only has \(m\) minutes! He already has \(n\) tasks planned for the day. Task \(i\) is represented as an interval \((l_i\), \(r_i)\), which means that Alex is busy and can not take a shower in that time interval (at any point in time strictly between \(l_i\) and \(r_i\)). No two tasks overlap.Given all \(n\) time intervals, will Alex be able to shower that day? In other words, will Alex have a free time interval of length at least \(s\)? In the first test case, Alex can shower for the first \(3\) minutes of the day and not miss any of the tasks.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(s\), and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\); \(1 \leq s, m \leq 10^9\)) β the number of time intervals Alex already has planned, the amount of time Alex takes to take a shower, and the amount of minutes a day has.Then \(n\) lines follow, the \(i\)-th of which contains two integers \(l_i\) and \(r_i\) (\(0 \leq l_i < r_i \leq m\)) β the time interval of the \(i\)-th task. No two tasks overlap.Additional constraint on the input: \(l_i > r_{i-1}\) for every \(i > 1\).The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output ""YES"" (without quotes) if Alex can take a shower for that given test case, and ""NO"" (also 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).
|
Input: 43 3 103 56 89 103 3 101 23 56 73 3 101 23 56 83 4 101 26 78 9 | Output: YES YES NO YES
|
Beginner
| 2 | 749 | 707 | 287 | 19 |
|
863 |
F
|
863F
|
F. Almost Permutation
| 2,200 |
flows
|
Recently Ivan noticed an array a while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug.Ivan clearly remembers that there were n elements in the array, and each element was not less than 1 and not greater than n. Also he remembers q facts about the array. There are two types of facts that Ivan remembers: 1 li ri vi β for each x such that li β€ x β€ ri ax β₯ vi; 2 li ri vi β for each x such that li β€ x β€ ri ax β€ vi. Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the q facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the cost of array as follows:, where cnt(i) is the number of occurences of i in the array.Help Ivan to determine minimum possible cost of the array that corresponds to the facts!
|
The first line contains two integer numbers n and q (1 β€ n β€ 50, 0 β€ q β€ 100).Then q lines follow, each representing a fact about the array. i-th line contains the numbers ti, li, ri and vi for i-th fact (1 β€ ti β€ 2, 1 β€ li β€ ri β€ n, 1 β€ vi β€ n, ti denotes the type of the fact).
|
If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible cost of the array.
|
Input: 3 0 | Output: 3
|
Hard
| 1 | 959 | 279 | 141 | 8 |
|
1,398 |
F
|
1398F
|
F. Controversial Rounds
| 2,500 |
binary search; data structures; dp; greedy; two pointers
|
Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won \(x\) rounds in a row. For example, if Bob won five rounds in a row and \(x = 2\), then two sets ends.You know that Alice and Bob have already played \(n\) rounds, and you know the results of some rounds. For each \(x\) from \(1\) to \(n\), calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins \(x\) rounds in a row. It is possible that the last set is still not finished β in that case, you should not count it in the answer.
|
The first line contains one integer \(n\) (\(1 \le n \le 10^6\)) β the number of rounds.The second line contains one string \(s\) of length \(n\) β the descriptions of rounds. If the \(i\)-th element of the string is 0, then Alice won the \(i\)-th round; if it is 1, then Bob won the \(i\)-th round, and if it is ?, then you don't know who won the \(i\)-th round.
|
In the only line print \(n\) integers. The \(i\)-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins \(i\) rounds in a row.
|
Let's consider the first test case: if \(x = 1\) and \(s = 110000\) or \(s = 111000\) then there are six finished sets; if \(x = 2\) and \(s = 110000\) then there are three finished sets; if \(x = 3\) and \(s = 111000\) then there are two finished sets; if \(x = 4\) and \(s = 110000\) then there is one finished set; if \(x = 5\) then there are no finished sets; if \(x = 6\) then there are no finished sets.
|
Input: 6 11?000 | Output: 6 3 2 1 0 0
|
Expert
| 5 | 697 | 363 | 218 | 13 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.