problem_statement
stringlengths
147
8.53k
input
stringlengths
1
771
output
stringlengths
1
592
time_limit
stringclasses
32 values
memory_limit
stringclasses
21 values
tags
stringlengths
6
168
F. Nene and the Passing Gametime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNene is training her team as a basketball coach. Nene's team consists of n players, numbered from 1 to n. The i-th player has an arm interval [l_i,r_i]. Two players i and j (i \neq j) can pass the ball to each other if and only if |i-j|\in[l_i+l_j,r_i+r_j] (here, |x| denotes the absolute value of x).Nene wants to test the cooperation ability of these players. In order to do this, she will hold several rounds of assessment. In each round, Nene will select a sequence of players p_1,p_2,\ldots,p_m such that players p_i and p_{i+1} can pass the ball to each other for all 1 \le i < m. The length of the sequence m can be chosen by Nene. Each player can appear in the sequence p_1,p_2,\ldots,p_m multiple times or not appear in it at all. Then, Nene will throw a ball to player p_1, player p_1 will pass the ball to player p_2 and so on... Player p_m will throw a ball away from the basketball court so it can no longer be used. As a coach, Nene wants each of n players to appear in at least one round of assessment. Since Nene has to go on a date after school, Nene wants you to calculate the minimum number of rounds of assessment needed to complete the task.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 2\cdot 10^5). The description of test cases follows.The first line contains a single integer n (1 \le n \le 2\cdot 10^6) — the number of players.The i-th of the next n lines contains two integers l_i and r_i (1\leq l_i\leq r_i\leq n) — the arm interval of the i-th player.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot 10^6.OutputFor each test case, output one integer — the minimum number of rounds of assessment Nene needs to complete her work.ExampleInput 521 11 121 12 231 31 31 351 12 21 52 21 161 25 52 32 32 21 2Output 2 2 2 1 3 NoteIn the first two test cases, Nene can host two rounds of assessment: one with p=[1] and one with p=[2]. It can be shown that hosting one round of assessment is not enough, so the answer is 2.In the third test case, Nene can host two rounds of assessment: one with p=[1,3] and one with p=[2]. Player 1 can pass the ball to player 3 as |3-1|=2 \in [1+1,3+3]. It can be shown that hosting one round of assessment is not enough, so the answer is 2.
521 11 121 12 231 31 31 351 12 21 52 21 161 25 52 32 32 21 2
2 2 2 1 3
4 seconds
256 megabytes
['constructive algorithms', 'data structures', 'dsu', 'graphs', 'sortings', '*3000']
E2. Nene vs. Monsters (Hard Version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. The only difference between the versions is the constraints on a_i. You can make hacks only if both versions of the problem are solved.Nene is fighting with n monsters, located in a circle. These monsters are numbered from 1 to n, and the i-th (1 \le i \le n) monster's current energy level is a_i.Since the monsters are too strong, Nene decided to fight with them using the Attack Your Neighbour spell. When Nene uses this spell, the following actions happen in the following order one by one: The 1-st monster attacks the 2-nd monster; The 2-nd monster attacks the 3-rd monster; \ldots The (n-1)-th monster attacks the n-th monster; The n-th monster attacks the 1-st monster. When the monster with energy level x attacks the monster with the energy level y, the energy level of the defending monster becomes \max(0, y-x) (the energy level of the attacking monster remains equal to x).Nene is going to use this spell 10^{100} times and deal with the monsters that will still have a non-zero energy level herself. She wants you to determine which monsters will have a non-zero energy level once she will use the described spell 10^{100} times.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of test cases follows.The first line contains a single integer n (2 \le n \le 2 \cdot 10^5) — the number of monsters.The second line contains n integers a_1, a_2, \ldots, a_n (0 \le a_i \le 10^9) — the current energy levels of monsters.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, in the first line output an integer m — the number of monsters with non-zero energy level after 10^{100} uses of the spell; in the second line of output m integers i_1,i_2,\ldots,i_m (1 \le i_1 < i_2 < \ldots < i_m \le n) — the indices of these monsters in the increasing order. If m=0, you may either output an empty line or don't output it.ExampleInput 532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0Output 1 1 0 1 1 2 1 3 6 1 3 6 8 10 12 NoteIn the first test case, the following actions happen during the first 3 uses of the spell in this order: Nene uses the Attack Your Neighbour spell for the first time; the 1-st monster attacks the 2-nd monster, after the attack the energy level of the 2-nd monster becomes equal to \max(0, 5-2)=3; the 2-nd monster attacks the 3-rd monster, after the attack the energy level of the 3-rd monster becomes equal to \max(0, 3-3)=0; the 3-rd monster attacks the 1-st monster, after the attack the energy level of the 1-st monster becomes equal to \max(0, 2-0)=2; Nene uses the Attack Your Neighbour spell for the second time; the 1-st monster attacks the 2-nd monster, after the attack the energy level of the 2-nd monster becomes equal to \max(0, 3-2)=1; the 2-nd monster attacks the 3-rd monster, after the attack the energy level of the 3-rd monster becomes equal to \max(0, 0-1)=0; the 3-rd monster attacks the 1-st monster, after the attack the energy level of the 1-st monster becomes equal to \max(0, 2-0)=2; Nene uses the Attack Your Neighbour spell for the third time; the 1-st monster attacks the 2-nd monster, after the attack the energy level of the 2-nd monster becomes equal to \max(0, 1-2)=0; the 2-nd monster attacks the 3-rd monster, after the attack the energy level of the 3-rd monster becomes equal to \max(0, 0-0)=0; the 3-rd monster attacks the 1-st monster, after the attack the energy level of the 1-st monster becomes equal to \max(0, 2-0)=2. After each of the next uses of the spell, energy levels of monsters do not change. Thus, only the 1-st monster has a non-zero energy level in the end.In the second test case, both monsters initially have zero energy level.
532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0
1 1 0 1 1 2 1 3 6 1 3 6 8 10 12
2 seconds
256 megabytes
['brute force', 'greedy', 'implementation', 'math', '*2700']
E1. Nene vs. Monsters (Easy Version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the easy version of the problem. The only difference between the versions is the constraints on a_i. You can make hacks only if both versions of the problem are solved.Nene is fighting with n monsters, located in a circle. These monsters are numbered from 1 to n, and the i-th (1 \le i \le n) monster's current energy level is a_i.Since the monsters are too strong, Nene decided to fight with them using the Attack Your Neighbour spell. When Nene uses this spell, the following actions happen in the following order one by one: The 1-st monster attacks the 2-nd monster; The 2-nd monster attacks the 3-rd monster; \ldots The (n-1)-th monster attacks the n-th monster; The n-th monster attacks the 1-st monster. When the monster with energy level x attacks the monster with the energy level y, the energy level of the defending monster becomes \max(0, y-x) (the energy level of the attacking monster remains equal to x).Nene is going to use this spell 10^{100} times and deal with the monsters that will still have a non-zero energy level herself. She wants you to determine which monsters will have a non-zero energy level once she will use the described spell 10^{100} times.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of test cases follows.The first line contains a single integer n (2 \le n \le 2 \cdot 10^5) — the number of monsters.The second line contains n integers a_1, a_2, \ldots, a_n (0 \le a_i \le 2 \cdot 10^5) — the current energy levels of monsters.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, in the first line output an integer m — the number of monsters with non-zero energy level after 10^{100} uses of the spell; in the second line of output m integers i_1,i_2,\ldots,i_m (1 \le i_1 < i_2 < \ldots < i_m \le n) — the indices of these monsters in the increasing order. If m=0, you may either output an empty line or don't output it.ExampleInput 532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0Output 1 1 0 1 1 2 1 3 6 1 3 6 8 10 12 NoteIn the first test case, the following actions happen during the first 3 uses of the spell in this order: Nene uses the Attack Your Neighbour spell for the first time; the 1-st monster attacks the 2-nd monster, after the attack the energy level of the 2-nd monster becomes equal to \max(0, 5-2)=3; the 2-nd monster attacks the 3-rd monster, after the attack the energy level of the 3-rd monster becomes equal to \max(0, 3-3)=0; the 3-rd monster attacks the 1-st monster, after the attack the energy level of the 1-st monster becomes equal to \max(0, 2-0)=2; Nene uses the Attack Your Neighbour spell for the second time; the 1-st monster attacks the 2-nd monster, after the attack the energy level of the 2-nd monster becomes equal to \max(0, 3-2)=1; the 2-nd monster attacks the 3-rd monster, after the attack the energy level of the 3-rd monster becomes equal to \max(0, 0-1)=0; the 3-rd monster attacks the 1-st monster, after the attack the energy level of the 1-st monster becomes equal to \max(0, 2-0)=2; Nene uses the Attack Your Neighbour spell for the third time; the 1-st monster attacks the 2-nd monster, after the attack the energy level of the 2-nd monster becomes equal to \max(0, 1-2)=0; the 2-nd monster attacks the 3-rd monster, after the attack the energy level of the 3-rd monster becomes equal to \max(0, 0-0)=0; the 3-rd monster attacks the 1-st monster, after the attack the energy level of the 1-st monster becomes equal to \max(0, 2-0)=2. After each of the next uses of the spell, energy levels of monsters do not change. Thus, only the 1-st monster has a non-zero energy level in the end.In the second test case, both monsters initially have zero energy level.
532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0
1 1 0 1 1 2 1 3 6 1 3 6 8 10 12
2 seconds
256 megabytes
['brute force', 'implementation', 'math', '*2500']
D. Nene and the Mex Operatortime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNene gave you an array of integers a_1, a_2, \ldots, a_n of length n.You can perform the following operation no more than 5\cdot 10^5 times (possibly zero): Choose two integers l and r such that 1 \le l \le r \le n, compute x as \operatorname{MEX}(\{a_l, a_{l+1}, \ldots, a_r\}), and simultaneously set a_l:=x, a_{l+1}:=x, \ldots, a_r:=x. Here, \operatorname{MEX} of a set of integers \{c_1, c_2, \ldots, c_k\} is defined as the smallest non-negative integer m which does not occur in the set c.Your goal is to maximize the sum of the elements of the array a. Find the maximum sum and construct a sequence of operations that achieves this sum. Note that you don't need to minimize the number of operations in this sequence, you only should use no more than 5\cdot 10^5 operations in your solution.InputThe first line contains an integer n (1 \le n \le 18) — the length of the array a.The second line contains n integers a_1,a_2,\ldots,a_n (0\leq a_i \leq 10^7) — the array a.OutputIn the first line, output two integers s and m (0\le m\le 5\cdot 10^5) — the maximum sum of elements of the array a and the number of operations in your solution.In the i-th of the following m lines, output two integers l and r (1 \le l \le r \le n), representing the parameters of the i-th operation.It can be shown that the maximum sum of elements of the array a can always be obtained in no more than 5 \cdot 10^5 operations.ExamplesInput 20 1Output 4 1 1 2 Input 31 3 9Output 13 0 Input 41 100 2 1Output 105 2 3 3 3 4 Input 10Output 1 1 1 1 NoteIn the first example, after the operation with l=1 and r=2 the array a becomes equal to [2,2]. It can be shown that it is impossible to achieve a larger sum of the elements of a, so the answer is 4.In the second example, the initial sum of elements is 13 which can be shown to be the largest.In the third example, the array a changes as follows: after the first operation (l=3, r=3), the array a becomes equal to [1,100,0,1]; after the second operation (l=3, r=4), the array a becomes equal to [1,100,2,2]. It can be shown that it is impossible to achieve a larger sum of the elements of a, so the answer is 105.
20 1
4 1 1 2
2 seconds
256 megabytes
['bitmasks', 'brute force', 'constructive algorithms', 'divide and conquer', 'dp', 'greedy', 'implementation', 'math', '*2000']
C. Nene's Magical Matrixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe magical girl Nene has an n\times n matrix a filled with zeroes. The j-th element of the i-th row of matrix a is denoted as a_{i, j}.She can perform operations of the following two types with this matrix: Type 1 operation: choose an integer i between 1 and n and a permutation p_1, p_2, \ldots, p_n of integers from 1 to n. Assign a_{i, j}:=p_j for all 1 \le j \le n simultaneously. Type 2 operation: choose an integer i between 1 and n and a permutation p_1, p_2, \ldots, p_n of integers from 1 to n. Assign a_{j, i}:=p_j for all 1 \le j \le n simultaneously. Nene wants to maximize the sum of all the numbers in the matrix \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}a_{i,j}. She asks you to find the way to perform the operations so that this sum is maximized. As she doesn't want to make too many operations, you should provide a solution with no more than 2n operations.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).InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 500). The description of test cases follows.The only line of each test case contains a single integer n (1 \le n \le 500) — the size of the matrix a.It is guaranteed that the sum of n^2 over all test cases does not exceed 5 \cdot 10^5.OutputFor each test case, in the first line output two integers s and m (0\leq m\leq 2n) — the maximum sum of the numbers in the matrix and the number of operations in your solution.In the k-th of the next m lines output the description of the k-th operation: an integer c (c \in \{1, 2\}) — the type of the k-th operation; an integer i (1 \le i \le n) — the row or the column the k-th operation is applied to; a permutation p_1, p_2, \ldots, p_n of integers from 1 to n — the permutation used in the k-th operation. Note that you don't need to minimize the number of operations used, you only should use no more than 2n operations. It can be shown that the maximum possible sum can always be obtained in no more than 2n operations.ExampleInput 212Output 1 1 1 1 1 7 3 1 1 1 2 1 2 1 2 2 1 1 2 NoteIn the first test case, the maximum sum s=1 can be obtained in 1 operation by setting a_{1, 1}:=1.In the second test case, the maximum sum s=7 can be obtained in 3 operations as follows: It can be shown that it is impossible to make the sum of the numbers in the matrix larger than 7.
212
1 1 1 1 1 7 3 1 1 1 2 1 2 1 2 2 1 1 2
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'math', '*1600']
B. Nene and the Card Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou and Nene are playing a card game. The deck with 2n cards is used to play this game. Each card has an integer from 1 to n on it, and each of integers 1 through n appears exactly on 2 cards. Additionally, there is a table where cards are placed during the game (initially, the table is empty).In the beginning of the game, these 2n cards are distributed between you and Nene so that each player receives n cards. After it, you and Nene alternatively take 2n turns, i.e. each person takes n turns, starting with you. On each turn: The player whose turn is it selects one of the cards in his hand. Let x be the number on it. The player whose turn is it receives 1 point if there is already a card with the integer x on the table (otherwise, he receives no points). After it, he places the selected card with the integer x on the table. Note that turns are made publicly: each player can see all the cards on the table at each moment.Nene is very smart so she always selects cards optimally in order to maximize her score in the end of the game (after 2n rounds). If she has several optimal moves, she selects the move that minimizes your score in the end of the game.More formally, Nene always takes turns optimally in order to maximize her score in the end of the game in the first place and to minimize your score in the end of the game in the second place.Assuming that the cards are already distributed and cards in your hand have integers a_1, a_2, \ldots, a_n written on them, what is the maximum number of points you can get by taking your turns optimally?InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of test cases follows.The first line contains a single integer n (1 \le n \le 2 \cdot 10^5) — the number of cards you and Nene receive in the beginning of the game.The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le n) — the integers on the cards in your hand. It is guaranteed that each integer from 1 through n appears in the sequence a_1, a_2, \ldots, a_n at most 2 times.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output one integer: the maximum number of points you can get.ExampleInput 541 1 2 387 4 1 2 8 8 5 587 1 4 5 3 4 2 631 2 311Output 1 2 1 0 0 NoteIn the first test case, the integers written on your cards are 1, 1, 2 and 3. The integers written on Nene's cards are 2, 3, 4 and 4. The game may proceed as follows: You select one of the cards with an integer 1 written on it and place it on the table. Nene selects one of the cards with an integer 4 written on it and places it on the table. You select the card with an integer 1 written on it, receive 1 point, and place the selected card on the table. Nene selects the card with an integer 4 written on it, receive 1 point, and places the selected card on the table. You select the card with an integer 2 written on it and place it on the table. Nene selects the card with an integer 2 written on it, receive 1 point, and places the selected card on the table. You select the card with an integer 3 written on it and place it on the table. Nene selects the card with an integer 3 written on it, receive 1 point, and places the selected card on the table. At the end of the game, you scored 1 point, and Nene scored 3. It can be shown that you cannot score more than 1 point if Nene plays optimally, so the answer is 1.In the second test case, if both players play optimally, you score 2 points and Nene scores 6 points.
541 1 2 387 4 1 2 8 8 5 587 1 4 5 3 4 2 631 2 311
1 2 1 0 0
1 second
256 megabytes
['games', 'greedy', '*800']
A. Nene's Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNene invented a new game based on an increasing sequence of integers a_1, a_2, \ldots, a_k.In this game, initially n players are lined up in a row. In each of the rounds of this game, the following happens: Nene finds the a_1-th, a_2-th, \ldots, a_k-th players in a row. They are kicked out of the game simultaneously. If the i-th player in a row should be kicked out, but there are fewer than i players in a row, they are skipped. Once no one is kicked out of the game in some round, all the players that are still in the game are declared as winners.For example, consider the game with a=[3, 5] and n=5 players. Let the players be named player A, player B, \ldots, player E in the order they are lined up initially. Then, Before the first round, players are lined up as ABCDE. Nene finds the 3-rd and the 5-th players in a row. These are players C and E. They are kicked out in the first round. Now players are lined up as ABD. Nene finds the 3-rd and the 5-th players in a row. The 3-rd player is player D and there is no 5-th player in a row. Thus, only player D is kicked out in the second round. In the third round, no one is kicked out of the game, so the game ends after this round. Players A and B are declared as the winners. Nene has not yet decided how many people would join the game initially. Nene gave you q integers n_1, n_2, \ldots, n_q and you should answer the following question for each 1 \le i \le q independently: How many people would be declared as winners if there are n_i players in the game initially? InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 250). The description of test cases follows.The first line case contains two integers k and q (1 \le k, q \le 100) — the length of the sequence a and the number of values n_i you should solve this problem for.The second line contains k integers a_1,a_2,\ldots,a_k (1\leq a_1<a_2<\ldots<a_k\leq 100) — the sequence a.The third line contains q integers n_1,n_2,\ldots,n_q (1\leq n_i \leq 100).OutputFor each test case, output q integers: the i-th (1\le i \le q) of them should be the number of players declared as winners if initially n_i players join the game.ExampleInput 62 13 555 32 4 6 7 91 3 55 43 4 5 6 71 2 3 42 369 961 10 1001 1100503 310 20 301 10 100Output 2 1 1 1 1 2 2 2 1 10 68 50 1 9 9 NoteThe first test case was explained in the statement.In the second test case, when n=1, the only player stays in the game in the first round. After that, the game ends and the only player is declared as a winner.
62 13 555 32 4 6 7 91 3 55 43 4 5 6 71 2 3 42 369 961 10 1001 1100503 310 20 301 10 100
2 1 1 1 1 2 2 2 1 10 68 50 1 9 9
1 second
256 megabytes
['binary search', 'brute force', 'data structures', 'games', 'greedy', '*800']
H. The Most Reckless Defensetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are playing a very popular Tower Defense game called "Runnerfield 2". In this game, the player sets up defensive towers that attack enemies moving from a certain starting point to the player's base.You are given a grid of size n \times m, on which k towers are already placed and a path is laid out through which enemies will move. The cell at the intersection of the x-th row and the y-th column is denoted as (x, y).Each second, a tower deals p_i units of damage to all enemies within its range. For example, if an enemy is located at cell (x, y) and a tower is at (x_i, y_i) with a range of r, then the enemy will take damage of p_i if (x - x_i) ^ 2 + (y - y_i) ^ 2 \le r ^ 2.Enemies move from cell (1, 1) to cell (n, m), visiting each cell of the path exactly once. An enemy instantly moves to an adjacent cell horizontally or vertically, but before doing so, it spends one second in the current cell. If its health becomes zero or less during this second, the enemy can no longer move. The player loses if an enemy reaches cell (n, m) and can make one more move.By default, all towers have a zero range, but the player can set a tower's range to an integer r (r > 0), in which case the health of all enemies will increase by 3^r. However, each r can only be used for at most one tower.Suppose an enemy has a base health of h units. If the tower ranges are 2, 4, and 5, then the enemy's health at the start of the path will be h + 3 ^ 2 + 3 ^ 4 + 3 ^ 5 = h + 9 + 81 + 243 = h + 333. The choice of ranges is made once before the appearance of enemies and cannot be changed after the game starts.Find the maximum amount of base health h for which it is possible to set the ranges so that the player does not lose when an enemy with health h passes through (without considering the additions for tower ranges).InputThe first line contains an integer t (1 \le t \le 100) — the number of test cases.The first line of each test case contains three integers n, m, and k (2 \le n, m \le 50, 1 \le k < n \cdot m) — the dimensions of the field and the number of towers on it.The next n lines each contain m characters — the description of each row of the field, where the character "." denotes an empty cell, and the character "#" denotes a path cell that the enemies will pass through.Then follow k lines — the description of the towers. Each line of description contains three integers x_i, y_i, and p_i (1 \le x_i \le n, 1 \le y_i \le m, 1 \le p_i \le 500) — the coordinates of the tower and its attack parameter. All coordinates correspond to empty cells on the game field, and all pairs (x_i, y_i) are pairwise distinct.It is guaranteed that the sum of n \cdot m does not exceed 2500 for all test cases.OutputFor each test case, output the maximum amount of base health h on a separate line, for which it is possible to set the ranges so that the player does not lose when an enemy with health h passes through (without considering the additions for tower ranges).If it is impossible to choose ranges even for an enemy with 1 unit of base health, output "0".ExampleInput 62 2 1#.##1 2 12 2 1#.##1 2 22 2 1#.##1 2 5003 3 2#..##..##1 2 43 1 33 5 2#.####.#.####.#2 2 22 4 25 5 4#....#....#....#....#####3 2 1424 5 92 5 791 3 50Output 0 1 1491 11 8 1797 NoteIn the first example, there is no point in increasing the tower range, as it will not be able to deal enough damage to the monster even with 1 unit of health.In the second example, the tower has a range of 1, and it deals damage to the monster in cells (1, 1) and (2, 2).In the third example, the tower has a range of 2, and it deals damage to the monster in all path cells. If the enemy's base health is 1491, then after the addition for the tower range, its health will be 1491 + 3 ^ 2 = 1500, which exactly equals the damage the tower will deal to it in three seconds.In the fourth example, the tower at (1, 2) has a range of 1, and the tower at (3, 1) has a range of 2.
62 2 1#.##1 2 12 2 1#.##1 2 22 2 1#.##1 2 5003 3 2#..##..##1 2 43 1 33 5 2#.####.#.####.#2 2 22 4 25 5 4#....#....#....#....#####3 2 1424 5 92 5 791 3 50
0 1 1491 11 8 1797
3 seconds
256 megabytes
['bitmasks', 'brute force', 'constructive algorithms', 'dp', 'flows', 'graph matchings', 'shortest paths', '*2300']
G. GCD on a gridtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNot long ago, Egor learned about the Euclidean algorithm for finding the greatest common divisor of two numbers. The greatest common divisor of two numbers a and b is the largest number that divides both a and b without leaving a remainder. With this knowledge, Egor can solve a problem that he once couldn't.Vasily has a grid with n rows and m columns, and the integer {a_i}_j is located at the intersection of the i-th row and the j-th column. Egor wants to go from the top left corner (at the intersection of the first row and the first column) to the bottom right corner (at the intersection of the last row and the last column) and find the greatest common divisor of all the numbers along the path. He is only allowed to move down and to the right. Egor has written down several paths and obtained different GCD values. He became interested in finding the maximum possible GCD.Unfortunately, Egor is tired of calculating GCDs, so he asks for your help in finding the maximum GCD of the integers along the path from the top left corner to the bottom right corner of the grid.InputThe first line contains an integer t (1 \le t \le {10}^{4}) — the number of test cases.The first line of each test case contains two integers n and m (1 \le n, m \le 100) — the number of rows and columns of the grid.Then, there are n lines, where the i-th line contains m integers (1 \le a_{i,j} \le {10}^{6}) — the integers written in the i-th row and the j-th column of the grid.It is guaranteed that the sum of n \cdot m does not exceed 2 \cdot {10}^{5} over all test cases.OutputFor each test case, output the maximum possible GCD along the path from the top left cell to the bottom right cell in a separate line.ExampleInput 32 330 20 3015 25 403 312 4 93 12 28 3 122 42 4 6 81 3 6 9Output 10 3 1
32 330 20 3015 25 403 312 4 93 12 28 3 122 42 4 6 81 3 6 9
10 3 1
3 seconds
256 megabytes
['brute force', 'dfs and similar', 'dp', 'implementation', 'math', 'number theory', '*1900']
F. Unfair Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice and Bob gathered in the evening to play an exciting game on a sequence of n integers, each integer of the sequence doesn't exceed 4. The rules of the game are too complex to describe, so let's just describe the winning condition — Alice wins if the bitwise XOR of all the numbers in the sequence is non-zero; otherwise, Bob wins.The guys invited Eve to act as a judge. Initially, Alice and Bob play with n numbers. After one game, Eve removes one of the numbers from the sequence, then Alice and Bob play with n-1 numbers. Eve removes one number again, after which Alice and Bob play with n - 2 numbers. This continues until the sequence of numbers is empty.Eve seems to think that in such a game, Alice almost always wins, so she wants Bob to win as many times as possible. Determine the maximum number of times Bob can win against Alice if Eve removes the numbers optimally.InputThe first line contains an integer t (1 \le t \le 10^4) — the number of test cases.The first and only line of each test case contains four integers p_i (0 \le p_i \le 200) — the number of ones, twos, threes, and fours in the sequence at the beginning of the game.OutputFor each test case, print the maximum number of times Bob will win in a separate line, if Eve removes the numbers optimally.ExampleInput 51 1 1 01 0 1 22 2 2 03 3 2 00 9 9 9Output 1 1 3 3 12 NoteIn the first example, Bob wins when Eve has not removed any numbers yet.In the second example, Bob wins if Eve removes one one and one three.
51 1 1 01 0 1 22 2 2 03 3 2 00 9 9 9
1 1 3 3 12
2 seconds
256 megabytes
['dp', 'games', 'greedy', 'math', 'schedules', '*1800']
E. Long Inversionstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA binary string s of length n is given. A binary string is a string consisting only of the characters '1' and '0'.You can choose an integer k (1 \le k \le n) and then apply the following operation any number of times: choose k consecutive characters of the string and invert them, i.e., replace all '0' with '1' and vice versa.Using these operations, you need to make all the characters in the string equal to '1'.For example, if n=5, s=00100, you can choose k=3 and proceed as follows: choose the substring from the 1-st to the 3-rd character and obtain s=\color{blue}{110}00; choose the substring from the 3-rd to the 5-th character and obtain s=11\color{blue}{111}; Find the maximum value of k for which it is possible to make all the characters in the string equal to '1' using the described operations. Note that the number of operations required to achieve this is not important.InputThe first line contains an integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains an integer n (1 \le n \le 5000) — the length of the string s.The second line of each test case contains a string s of length n, consisting of the characters '1' and '0'.It is guaranteed that the sum of the values n^2 over all test cases in the test does not exceed 25 \cdot 10^6.OutputFor each test case, output the maximum integer k (1 \le k \le n) for which it is possible to obtain a string s consisting only of the characters '1' using the described operations.ExampleInput 5500100501000710111013000210Output 3 2 4 3 1
5500100501000710111013000210
3 2 4 3 1
3 seconds
256 megabytes
['brute force', 'greedy', 'implementation', 'sortings', '*1700']
D. Inaccurate Subsequence Searchtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMaxim has an array a of n integers and an array b of m integers (m \le n).Maxim considers an array c of length m to be good if the elements of array c can be rearranged in such a way that at least k of them match the elements of array b. For example, if b = [1, 2, 3, 4] and k = 3, then the arrays [4, 1, 2, 3] and [2, 3, 4, 5] are good (they can be reordered as follows: [1, 2, 3, 4] and [5, 2, 3, 4]), while the arrays [3, 4, 5, 6] and [3, 4, 3, 4] are not good.Maxim wants to choose every subsegment of array a of length m as the elements of array c. Help Maxim count how many selected arrays will be good.In other words, find the number of positions 1 \le l \le n - m + 1 such that the elements a_l, a_{l+1}, \dots, a_{l + m - 1} form a good array.InputThe first line contains an integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains three integers n, m, and k (1 \le k \le m \le n \le 2 \cdot 10^5) — the number of elements in arrays a and b, the required number of matching elements.The second line of each test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^6) — the elements of array a. Elements of the array a are not necessarily unique.The third line of each test case contains m integers b_1, b_2, \dots, b_m (1 \le b_i \le 10^6) — the elements of array b. Elements of the array b are not necessarily unique.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5. Similarly, it is guaranteed that the sum of m over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the number of good subsegments of array a on a separate line.ExampleInput 57 4 24 1 2 3 4 5 61 2 3 47 4 34 1 2 3 4 5 61 2 3 47 4 44 1 2 3 4 5 61 2 3 411 5 39 9 2 2 10 9 7 6 3 6 36 9 7 8 104 1 14 1 5 66Output 4 3 2 4 1 NoteIn the first example, all subsegments are good.In the second example, good subsegments start at positions 1, 2, and 3.In the third example, good subsegments start at positions 1 and 2.
57 4 24 1 2 3 4 5 61 2 3 47 4 34 1 2 3 4 5 61 2 3 47 4 44 1 2 3 4 5 61 2 3 411 5 39 9 2 2 10 9 7 6 3 6 36 9 7 8 104 1 14 1 5 66
4 3 2 4 1
2 seconds
256 megabytes
['data structures', 'two pointers', '*1400']
C. Inhabitant of the Deep Seatime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputn ships set out to explore the depths of the ocean. The ships are numbered from 1 to n and follow each other in ascending order; the i-th ship has a durability of a_i.The Kraken attacked the ships k times in a specific order. First, it attacks the first of the ships, then the last, then the first again, and so on.Each attack by the Kraken reduces the durability of the ship by 1. When the durability of the ship drops to 0, it sinks and is no longer subjected to attacks (thus the ship ceases to be the first or last, and the Kraken only attacks the ships that have not yet sunk). If all the ships have sunk, the Kraken has nothing to attack and it swims away.For example, if n=4, k=5, and a=[1, 2, 4, 3], the following will happen: The Kraken attacks the first ship, its durability becomes zero and now a = [2, 4, 3]; The Kraken attacks the last ship, now a = [2, 4, 2]; The Kraken attacks the first ship, now a = [1, 4, 2]; The Kraken attacks the last ship, now a = [1, 4, 1]; The Kraken attacks the first ship, its durability becomes zero and now a = [4, 1]. How many ships were sunk after the Kraken's attack?InputThe first line contains an integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains two integers n and k (1 \le n \le 2 \cdot 10^5, 1 \le k \le 10^{15}) — the number of ships and how many times the Kraken will attack the ships.The second line of each test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9) — the durability of the ships.It is guaranteed that the sum of n for all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the number of ships sunk by the Kraken on a separate line.ExampleInput 64 51 2 4 34 61 2 4 35 202 7 1 8 22 23 22 151 52 75 2Output 2 3 5 0 2 2
64 51 2 4 34 61 2 4 35 202 7 1 8 22 23 22 151 52 75 2
2 3 5 0 2 2
2 seconds
256 megabytes
['greedy', 'implementation', 'math', '*1300']
B. Progressive Squaretime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA progressive square of size n is an n \times n matrix. Maxim chooses three integers a_{1,1}, c, and d and constructs a progressive square according to the following rules:a_{i+1,j} = a_{i,j} + ca_{i,j+1} = a_{i,j} + dFor example, if n = 3, a_{1,1} = 1, c=2, and d=3, then the progressive square looks as follows: \begin{pmatrix} 1 & 4 & 7 \\ 3 & 6 & 9 \\ 5 & 8 & 11 \end{pmatrix} Last month Maxim constructed a progressive square and remembered the values of n, c, and d. Recently, he found an array b of n^2 integers in random order and wants to make sure that these elements are the elements of that specific square.It can be shown that for any values of n, a_{1,1}, c, and d, there exists exactly one progressive square that satisfies all the rules.InputThe first line contains an integer t (1 \le t \le {10} ^ 4) — the number of test cases.The first line of each test case contains three integers n, c, and d (2 \le n \le 500, 1 \le c, d \le 10^6) — the size of the square and the values of c and d as described in the statement.The second line of each test case contains n \cdot n integers b_1, b_2, \dots, b_{n \cdot n} (1 \le b_i \le 10^9) — the elements found by Maxim.It is guaranteed that the sum of n ^ 2 over all test cases does not exceed 25 \cdot {10} ^ 4.OutputFor each test case, output "YES" in a separate line if a progressive square for the given n, c, and d can be constructed from the array elements a, otherwise output "NO".You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.ExampleInput 53 2 33 9 6 5 7 1 10 4 83 2 33 9 6 5 7 1 11 4 82 100 100400 300 400 5003 2 33 9 6 6 5 1 11 4 84 4 415 27 7 19 23 23 11 15 7 3 19 23 11 15 11 15Output NO YES YES NO NO
53 2 33 9 6 5 7 1 10 4 83 2 33 9 6 5 7 1 11 4 82 100 100400 300 400 5003 2 33 9 6 6 5 1 11 4 84 4 415 27 7 19 23 23 11 15 7 3 19 23 11 15 11 15
NO YES YES NO NO
2 seconds
256 megabytes
['constructive algorithms', 'data structures', 'implementation', 'sortings', '*1000']
A. Yogurt Saletime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe price of one yogurt at the "Vosmiorochka" store is a burles, but there is a promotion where you can buy two yogurts for b burles.Maxim needs to buy exactly n yogurts. When buying two yogurts, he can choose to buy them at the regular price or at the promotion price.What is the minimum amount of burles Maxim should spend to buy n yogurts?InputThe first line contains a single integer t (1 \le t \le {10}^{4}) — the number of test cases.The first and only line of each test case contains three integers n, a, and b (1 \le n \le 100, 1 \le a, b \le 30) — the number of yogurts Maxim wants to buy, the price for one yogurt, and the price for two yogurts on promotion.OutputFor each test case, print in a separate line the minimum cost of buying n yogurts at "Vosmiorochka".ExampleInput 42 5 93 5 93 5 114 5 11Output 9 14 15 20 NoteIn the third test case of the example, it is more advantageous to buy three yogurts for 15 burles than two for 11 and one for 5.In the fourth test case of the example, you need to buy four yogurts, each for 5 burles.
42 5 93 5 93 5 114 5 11
9 14 15 20
1 second
256 megabytes
['math', '*800']
F. Unique Stringstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's say that two strings a and b are equal if you can get the string b by cyclically shifting string a. For example, the strings 0100110 and 1100100 are equal, while 1010 and 1100 are not.You are given a binary string s of length n. Its first c characters are 1-s, and its last n - c characters are 0-s.In one operation, you can replace one 0 with 1.Calculate the number of unique strings you can get using no more than k operations. Since the answer may be too large, print it modulo 10^9 + 7.InputThe first and only line contains three integers n, c and k (1 \le n \le 3000; 1 \le c \le n; 0 \le k \le n - c) — the length of string s, the length of prefix of 1-s and the maximum number of operations.OutputPrint the single integer — the number of unique strings you can achieve performing no more than k operations, modulo 10^9 + 7.ExamplesInput 1 1 0Output 1 Input 3 1 2Output 3 Input 5 1 1Output 3 Input 6 2 2Output 7 Input 24 3 11Output 498062 NoteIn the first test case, the only possible string is 1.In the second test case, the possible strings are: 100, 110, and 111. String 101 is equal to 110, so we don't count it.In the third test case, the possible strings are: 10000, 11000, 10100. String 10010 is equal to 10100, and 10001 is equal to 11000.
1 1 0
1
2 seconds
256 megabytes
['combinatorics', 'dp', 'math', '*3100']
E. Chain Reactiontime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are n monsters standing in a row. The i-th monster has a_i health points.Every second, you can choose one alive monster and launch a chain lightning at it. The lightning deals k damage to it, and also spreads to the left (towards decreasing i) and to the right (towards increasing i) to alive monsters, dealing k damage to each. When the lightning reaches a dead monster or the beginning/end of the row, it stops. A monster is considered alive if its health points are strictly greater than 0.For example, consider the following scenario: there are three monsters with health equal to [5, 2, 7], and k = 3. You can kill them all in 4 seconds: launch a chain lightning at the 3-rd monster, then their health values are [2, -1, 4]; launch a chain lightning at the 1-st monster, then their health values are [-1, -1, 4]; launch a chain lightning at the 3-rd monster, then their health values are [-1, -1, 1]; launch a chain lightning at the 3-th monster, then their health values are [-1, -1, -2]. For each k from 1 to \max(a_1, a_2, \dots, a_n), calculate the minimum number of seconds it takes to kill all the monsters.InputThe first line contains a single integer n (1 \le n \le 10^5) — the number of monsters.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^5) — the health points of the i-th monster.OutputFor each k from 1 to \max(a_1, a_2, \dots, a_n), output the minimum number of seconds it takes to kill all the monsters.ExamplesInput 35 2 7Output 10 6 4 3 2 2 1 Input 47 7 7 7Output 7 4 3 2 2 2 1 Input 101 9 7 6 2 4 7 8 1 3Output 17 9 5 4 3 3 3 2 1
35 2 7
10 6 4 3 2 2 1
3 seconds
512 megabytes
['binary search', 'data structures', 'dsu', 'greedy', 'implementation', 'math', 'number theory', '*2200']
D. Colored Ballstime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputThere are balls of n different colors; the number of balls of the i-th color is a_i.The balls can be combined into groups. Each group should contain at most 2 balls, and no more than 1 ball of each color.Consider all 2^n sets of colors. For a set of colors, let's denote its value as the minimum number of groups the balls of those colors can be distributed into. For example, if there are three colors with 3, 1 and 7 balls respectively, they can be combined into 7 groups (and not less than 7), so the value of that set of colors is 7.Your task is to calculate the sum of values over all 2^n possible sets of colors. Since the answer may be too large, print it modulo 998\,244\,353.InputThe first line contains a single integer n (1 \le n \le 5000) — the number of colors.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 5000) — the number of balls of the i-th color.Additional constraint on input: the total number of balls doesn't exceed 5000.OutputPrint a single integer — the sum of values of all 2^n sets of colors, taken modulo 998\,244\,353.ExamplesInput 31 1 2Output 11 Input 15Output 5 Input 41 3 3 7Output 76 NoteConsider the first example. There are 8 sets of colors: for the empty set, its value is 0; for the set \{1\}, its value is 1; for the set \{2\}, its value is 1; for the set \{3\}, its value is 2; for the set \{1,2\}, its value is 1; for the set \{1,3\}, its value is 2; for the set \{2,3\}, its value is 2; for the set \{1,2,3\}, its value is 2. So, the sum of values over all 2^n sets of colors is 11.
31 1 2
11
2 seconds
1024 megabytes
['combinatorics', 'dp', 'math', 'sortings', '*1800']
C. Long Multiplicationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers x and y of the same length, consisting of digits from 1 to 9.You can perform the following operation any number of times (possibly zero): swap the i-th digit in x and the i-th digit in y.For example, if x=73 and y=31, you can swap the 2-nd digits and get x=71 and y=33.Your task is to maximize the product of x and y using the aforementioned operation any number of times. If there are multiple answers, print any of them.InputThe first line contains a single integer t (1 \le t \le 1000) — the number of test cases.The first line of each test case contains a single integer x (1 \le x < 10^{100}).The second line of each test case contains a single integer y (1 \le y < 10^{100}).Additional constraint on input: the integers x and y consist only of digits from 1 to 9.OutputFor each test case, print two lines — the first line should contain the number x after performing the operations; similarly, the second line should contain the number y after performing the operations. If there are multiple answers, print any of them.ExampleInput 373312535163982Output 71 33 5 2 3912 3586
373312535163982
71 33 5 2 3912 3586
2 seconds
256 megabytes
['greedy', 'math', 'number theory', '*1200']
B. Make It Uglytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call an array a beautiful if you can make all its elements the same by using the following operation an arbitrary number of times (possibly, zero): choose an index i (2 \le i \le |a| - 1) such that a_{i - 1} = a_{i + 1}, and replace a_i with a_{i - 1}. You are given a beautiful array a_1, a_2, \dots, a_n. What is the minimum number of elements you have to remove from it in order for it to stop being beautiful? Swapping elements is prohibited. If it is impossible to do so, then output -1.InputThe 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 (1 \le n \le 3 \cdot 10^5).The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le n).Additional constraints on the input: in every test case, the given array a is beautiful; the sum of n over all test cases does not exceed 3 \cdot 10^5. OutputFor each test case, output a single integer — the minimum number of elements you have to remove from the array a in order for it to stop being beautiful. If it is impossible, then output -1.ExampleInput 432 2 251 2 1 2 11173 3 3 5 3 3 3Output -1 1 -1 3 NoteIn the first testcase, it is impossible to modify the array in such a way that it stops being beautiful. An array consisting of identical numbers will remain beautiful no matter how many numbers we remove from it.In the second testcase, you can remove the number at the index 5, for example.The resulting array will be [1, 2, 1, 2]. Let's check if it is beautiful. Two operations are available: Choose i = 2: the array becomes [1, 1, 1, 2]. No more operations can be applied to it, and the numbers are not all the same. Choose i = 3 instead: the array becomes [1, 2, 2, 2]. No more operations can be applied to it either, and the numbers are still not all the same. Thus, the array [1, 2, 1, 2] is not beautiful.In the fourth testcase, you can remove the first three elements, for example. The resulting array [5, 3, 3, 3] is not beautiful.
432 2 251 2 1 2 11173 3 3 5 3 3 3
-1 1 -1 3
2 seconds
256 megabytes
['implementation', 'math', '*1200']
A. Painting the Ribbontime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputAlice and Bob have bought a ribbon consisting of n parts. Now they want to paint it.First, Alice will paint every part of the ribbon into one of m colors. For each part, she can choose its color arbitrarily.Then, Bob will choose at most k parts of the ribbon and repaint them into the same color (he chooses the affected parts and the color arbitrarily).Bob would like all parts to have the same color. However, Alice thinks that this is too dull, so she wants to paint the ribbon in such a way that Bob cannot make all parts have the same color.Is it possible to paint the ribbon in such a way?InputThe first line contains one integer t (1 \le t \le 1000) — the number of test cases.Each test case consists of one line containing three integers n, m and k (1 \le m, k \le n \le 50) — the number of parts, the number of colors and the number of parts Bob can repaint, respectively.OutputFor each test case, print YES if Alice can paint the ribbon so that Bob cannot make all parts have the same color. Otherwise, print NO.You can print every letter in any register. For example, Yes, yes, yEs will all be recognized as positive answer.ExampleInput 51 1 15 1 15 2 15 2 25 5 3Output NO NO YES NO YES NoteIn the first test case, a ribbon consists of 1 part. So all its parts will always have the same color.In the second test case, there is only 1 color.In the third test case, Alice can paint the ribbon as follows: [1, 2, 1, 2, 1]. It's impossible to change the color of at most 1 part so that all parts have the same color.In the fourth test case, no matter how Alice paints the ribbon, Bob will always be able to repaint 2 parts so that all parts have the same color.In the fifth test case, Alice can paint the ribbon as follows: [1, 2, 3, 4, 5]. It's impossible to change the color of at most 3 parts so that all parts have the same color.
51 1 15 1 15 2 15 2 25 5 3
NO NO YES NO YES
2 seconds
512 megabytes
['constructive algorithms', 'greedy', 'math', '*900']
J. Help, what does it mean to be "Based"time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputInputSup, gimme a single integer x (1 \le x \le 4) homie uwu :3OutputIf x = 1, spit out some based code that, like when ya feed it two integers a and b, it hands over their sum.If x = 2, slap some based code that takes an integer a and returns its absolute value like magic, no cap.If x = 3, check this out: blaze some based code that grabs an integer n (1 \le n \le 50) and an array a of n distinct integers, then flexes the maximum value real quick.If x = 4, toss me some based code that snatches an integer n (1 \le n \le 50), an array a of n distinct integers and an integer k (1 \le k \le n), then nails down that k-th largest value in the array. Get it, fam!ExamplesInput 1Output yoink a yoink b *slaps a on top of b* yeet b go touch some grass Input 2Output yoink a bruh b is lowkey just 0 rip this b fell off by a vibe check a ratios b simp for 7 bruh a is lowkey just b yeet a go touch some grass Input 3Output yoink n yoink a bruh m is lowkey just a[0] bruh i is lowkey just 1 vibe check n ratios i simp for 9 yeet m go touch some grass vibe check a[i] ratios m bruh m is lowkey just a[i] *slaps 1 on top of i* simp for 5 Input 4Output NoteSlide into the "My Submissions" page and vibe check ur code by clicking on its ID. You'll see just how based or cringe it is.
1
yoink a yoink b *slaps a on top of b* yeet b go touch some grass
1 second
256 megabytes
['brute force', 'constructive algorithms', 'expression parsing', 'implementation', 'sortings']
I. Dark Mattertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputView the statement as a PDF.ExampleInput 1 + 1Output 3
1 + 1
3
1 second
256 megabytes
['bitmasks', 'geometry']
H. Palindrometime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA palindrome is a string that reads the same in both directions, for example z, uwu, or moom.InputThe first line contains an integer t (1 \leq t \leq 100) — the number of testcases.The following t lines each contain a string of length at most 100 consisting of lowercase English letters. OutputFor each test case, output "YES" or "NO", denoting the answer.ExamplesInput 8actleradarracecarphpatcodercodeforcessteamOutput NO NO YES YES NO NO YES YES Input 2azOutput NO YES
8actleradarracecarphpatcodercodeforcessteam
NO NO YES YES NO NO YES YES
3 seconds
256 megabytes
['implementation', 'strings']
G. Mathematician Takeovertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA mathematician grabbed my computer as I was preparing this problem, deleted the statement, and changed the samples to fit his liking. The model solution and input/output format are still correct, but the samples are wrong. I've partially fixed the problem so that when you submit, it will be tested against the correct version of the problem. However, I can't fix the samples below. As a computer scientist, can you solve the correct problem?InputThe only line of input contains a real number x (1 \leq x \leq 100), given to exactly three decimal places.OutputOutput one real number — the answer. Your answer is considered correct if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a-b|}{\max(1,|b|)} \le 10^{-4}.ExamplesInput 1.234 Output 0.21026Input 4.113 Output 1.41415Input 99.000 Output 4.59512
1.234
0.21026
1 second
256 megabytes
['binary search', 'dfs and similar', 'math']
F. Gridtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputInputThe input contains 21 lines, each containing 21 characters \texttt{0} or \texttt{1}.ExampleInput 111111101011101111111100000100011001000001101110101101001011101101110101100101011101101110101001001011101100000100111101000001111111101010101111111000000000001100000000111100101111110011101000111010101100110101111101101101001000011001001000001000011000111101110000111001011000000001001001111100111111100001101010000100000100010010100111101110100110110011100101110101100000100010101110101010110000100100000101000011001001111111101011111111100Output 12
111111101011101111111100000100011001000001101110101101001011101101110101100101011101101110101001001011101100000100111101000001111111101010101111111000000000001100000000111100101111110011101000111010101100110101111101101101001000011001001000001000011000111101110000111001011000000001001001111100111111100001101010000100000100010010100111101110100110110011100101110101100000100010101110101010110000100100000101000011001001111111101011111111100
12
1 second
256 megabytes
['brute force']
E. Sweep Linetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBe careful not to make a mistake. You might have to start all over again.— Someone, probablyInputThe first line contains one integer n (1 \leq n \leq 10^5) — the length of the array a.The second line contains n integers a_1, a_2, \ldots, a_n (0 \leq a_i \leq 2) — the array a.OutputOutput a single integer — the number of solutions, modulo 20240401.ExamplesInput 71 1 2 1 1 2 0Output 1Input 71 1 2 1 1 1 0Output 2Input 70 1 2 1 1 1 0Output 0NoteIn the first sample, the array looks like this: \color{blue}{1} \color{blue}{1} \color{darkgreen}{2} \color{blue}{1} \color{blue}{1} \color{darkgreen}{2} \color{gray}{0} Obviously, the answer here is 1 \pmod{20240401}.In the second sample, the array looks like this: \color{blue}{1} \color{blue}{1} \color{darkgreen}{2} \color{blue}{1} \color{blue}{1} \color{blue}{1} \color{gray}{0} I do not know why the answer here is 2 \pmod{20240401}, I had to take a guess.In the third sample, the array looks like this: \color{gray}{0} \color{blue}{1} \color{darkgreen}{2} \color{blue}{1} \color{blue}{1} \color{blue}{1} \color{gray}{0} If the answer here is not 0 \pmod{20240401}, I am literally going to explode.
71 1 2 1 1 2 0
1
1 second
256 megabytes
['combinatorics', 'games', 'math']
D. Are You a Procrastinator?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output!Input!Output!Scoring!ExampleInput ? Output ?Note!
?
?
1 second
256 megabytes
['implementation']
C. They Have Fooledtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output InputThe first line contains one integer n (0 \leq n \leq 12).OutputOne integer — the answer.ExamplesInput 0Output 10Input 1Output 10Input 5Output 7Input 9Output 0
0
10
1 second
256 megabytes
['brute force', 'schedules']
B. Is it stated?time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputInputThe first line contains an integer t (1 \leq t \leq 100) — the number of testcases.The following t lines each contain a string of length at most 100 consisting of lowercase English letters. OutputFor each test case, output "YES" or "NO", denoting the answer.ExampleInput 10isitstatedsubmitacacceptedwawronganswertletimelimitexceededOutput NO YES NO YES NO NO NO NO NO YES
10isitstatedsubmitacacceptedwawronganswertletimelimitexceeded
NO YES NO YES NO NO NO NO NO YES
2 seconds
256 megabytes
['strings']
I. Growing Treestime limit per test5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputwowaka ft. Hatsune Miku - Ura-Omote LoversඞYou are given an undirected connected simple graph with n nodes and m edges, where edge i connects node u_i and v_i, with two positive parameters a_i and b_i attached to it. Additionally, you are also given an integer k.A non-negative array x with size m is called a k-spanning-tree generator if it satisfies the following: Consider the undirected multigraph with n nodes where edge i is cloned x_i times (i.e. there are x_i edges connecting u_i and v_i). It is possible to partition the edges of this graph into k spanning trees, where each edge belongs to exactly one spanning tree^\dagger. The cost of such array x is defined as \sum_{i = 1}^m a_i x_i^2 + b_i x_i. Find the minimum cost of a k-spanning-tree generator.^\dagger A spanning tree of a (multi)graph is a subset of the graph's edges that form a tree connecting all vertices of the graph.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 500) — the number of test cases. The description of the test cases follows.The first line of each test case contains three integers n, m, and k (2 \le n \le 50, n - 1 \le m \le \min(50, \frac{n(n - 1)}{2}), 1 \le k \le 10^7) — the number of nodes in the graph, the number of edges in the graph, and the parameter for the k-spanning-tree generator.Each of the next m lines of each test case contains four integers u_i, v_i, a_i, and b_i (1 \le u_i, v_i \le n, u_i \neq v_i, 1 \le a_i, b_i \le 1000) — the endpoints of the edge i and its two parameters. It is guaranteed that the graph is simple and connected.It is guaranteed that the sum of n^2 and the sum of m^2 over all test cases does not exceed 2500.OutputFor each test case, output a single integer: the minimum cost of a k-spanning-tree generator.ExampleInput 45 5 14 3 5 52 1 5 72 4 6 25 3 3 52 5 2 95 5 34 3 5 52 1 5 72 4 6 25 3 3 52 5 2 92 1 100000001 2 1000 100010 15 107 1 7 65 8 6 64 8 2 24 3 10 910 8 3 44 6 6 15 4 1 39 3 4 38 3 9 97 5 10 32 1 3 46 1 6 42 5 7 310 7 2 18 2 6 8Output 38 191 100000010000000000 2722 NoteIn the first test case, a valid 1-spanning-tree generator is x = [1, 1, 1, 1, 0], as indicated by the following figure. The cost of this generator is (1^2 \cdot 5 + 1 \cdot 5) + (1^2 \cdot 5 + 1 \cdot 7) + (1^2 \cdot 6 + 1 \cdot 2) + (1^2 \cdot 3 + 1 \cdot 5) + (0^2 \cdot 4 + 0 \cdot 9) = 38. It can be proven that no other generator has a lower cost. The 1-spanning-tree partition of x = [1, 1, 1, 1, 0] In the second test case, a valid 3-spanning-tree generator is x = [2, 3, 2, 2, 3], as indicated by the following figure. The cost of this generator is (2^2 \cdot 5 + 2 \cdot 5) + (3^2 \cdot 5 + 3 \cdot 7) + (2^2 \cdot 6 + 2 \cdot 2) + (2^2 \cdot 3 + 2 \cdot 5) + (3^2 \cdot 4 + 3 \cdot 9) = 191. It can be proven that no other generator has a lower cost. The 3-spanning-tree partition of x = [2, 3, 2, 2, 3]
45 5 14 3 5 52 1 5 72 4 6 25 3 3 52 5 2 95 5 34 3 5 52 1 5 72 4 6 25 3 3 52 5 2 92 1 100000001 2 1000 100010 15 107 1 7 65 8 6 64 8 2 24 3 10 910 8 3 44 6 6 15 4 1 39 3 4 38 3 9 97 5 10 32 1 3 46 1 6 42 5 7 310 7 2 18 2 6 8
38 191 100000010000000000 2722
5 seconds
512 megabytes
['binary search', 'constructive algorithms', 'flows', 'graphs', 'greedy', '*3200']
H. Thanos Snaptime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputPiotr Rubik - Psalm dla CiebieඞThere is an array a of size 2^k for some positive integer k, which is initially a permutation of values from 1 to 2^k. Alice and Bob play the following game on the array a. First, a value t between 1 and k is shown to both Alice and Bob. Then, for exactly t turns, the following happens: Alice either does nothing, or chooses two distinct elements of the array a and swaps them. Bob chooses either the left half or the right half of the array a and erases it. The score of the game is defined as the maximum value in a after all t turns have been played. Alice wants to maximize this score, while Bob wants to minimize it.You need to output k numbers: the score of the game if both Alice and Bob play optimally for t from 1 to k.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer k (1 \le k \le 20) — the parameter of the size of a.The second line of each test case contains 2^k integers a_1, a_2, \ldots, a_{2^k} (1 \le a_i \le 2^k, a_i's are pairwise distinct) — the given array a.It is guaranteed that the sum of 2^k over all test cases does not exceed 2^{20}.OutputFor each test case, print k numbers, where the i-th number is the score of the game if both Alice and Bob play optimally for t = i.ExampleInput 511 224 3 2 135 1 6 4 7 2 8 3410 15 6 12 1 3 4 9 13 5 7 16 14 11 2 8532 2 5 23 19 17 31 7 29 3 4 16 13 9 30 24 14 1 8 20 6 15 26 18 10 27 22 12 25 21 28 11Output 1 3 1 7 5 1 15 13 9 1 31 28 25 17 1 NoteIn the third test case, for t = 2, the game could have proceeded as follows: Initially, a = [5, 1, 6, 4, 7, 2, 8, 3]. Alice swaps a_6 and a_8, a becomes [5, 1, 6, 4, 7, 3, 8, 2]. Bob erases the right half of the array, a becomes [5, 1, 6, 4]. Alice does nothing, a remains as [5, 1, 6, 4]. Bob erases the right half of the array, a becomes [5, 1]. The game ends with a score of 5.
511 224 3 2 135 1 6 4 7 2 8 3410 15 6 12 1 3 4 9 13 5 7 16 14 11 2 8532 2 5 23 19 17 31 7 29 3 4 16 13 9 30 24 14 1 8 20 6 15 26 18 10 27 22 12 25 21 28 11
1 3 1 7 5 1 15 13 9 1 31 28 25 17 1
3 seconds
512 megabytes
['binary search', 'dp', 'games', 'greedy', 'trees', '*3200']
G. Clacking Ballstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRammstein - AusländerඞThere are m baskets placed along a circle, numbered from 1 to m in clockwise order (basket m is next to basket 1). Furthermore, there are n balls, where ball i is initially placed in basket a_i, and no basket contains more than one ball.Alice is allowed to perform the following operation, which always takes exactly one second whether you move/throw a ball or not: Alice chooses an integer i between 1 and n uniformly at random. If ball i was thrown away before, do nothing. Otherwise, ball i is moved from the basket currently containing it to the next basket (in clockwise order). If the target basket currently contains another ball j, throw ball j away. She repeats this operation until there is exactly one ball left. Calculate the expected time needed (in seconds) for Alice to end the process.It can be proven that the answer can be represented as a rational number \frac{p}{q} with coprime p and q. You need to output p \cdot q^{-1} \bmod 10^9 + 7. It can be proven that 10^9 + 7 \nmid q.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and m (1 \le n \le 3 \cdot 10^5, n \le m \le 10^9) — the number of balls and the number of baskets.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le m, a_i's are pairwise distinct) — the initial position of each ball.It is guaranteed that the sum of n over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, print one integer: the expected amount of time (in seconds) Alice needs to end the process, modulo 10^9 + 7.ExampleInput 53 105 1 42 1515 16 61 2 3 4 5 66 96 5 4 3 2 11 10069Output 600000042 14 35 333333409 0 NoteIn the first test case, Alice could have proceeded as follows (we define a_i = -1 if ball i has been thrown out): Initially, a = [5, 1, 4]. Alice chooses i = 2 with probability \frac{1}{3}, and ball 2 is moved to basket 2. After this, a = [5, 2, 4]. Alice chooses i = 2 with probability \frac{1}{3}, and ball 2 is moved to basket 3. After this, a = [5, 3, 4]. Alice chooses i = 2 with probability \frac{1}{3}, and ball 2 is moved to basket 4. As basket 4 previously contains ball 3, this ball is thrown out. After this, a = [5, 4, -1]. Alice chooses i = 3 with probability \frac{1}{3}. Ball 3 has already been thrown out, so nothing happens. After this, a = [5, 4, -1]. Alice chooses i = 2 with probability \frac{1}{3}, and ball 2 is moved to basket 5, which throws out ball 1. After this, a = [-1, 5, -1], and the process ends. The answer for this test case is \frac{189}{5}.The answer for the second test case is 14 (note that these two balls are next to each other).The answer for the third test case is 35.The answer for the fourth test case is \frac{220}{3}.In the fifth test case, as there is only one ball initially, the answer is 0.
53 105 1 42 1515 16 61 2 3 4 5 66 96 5 4 3 2 11 10069
600000042 14 35 333333409 0
2 seconds
256 megabytes
['combinatorics', 'math', 'probabilities', '*3100']
F. Inversion Compositiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMy Chemical Romance - DisenchantedඞYou are given a permutation p of size n, as well as a non-negative integer k. You need to construct a permutation q of size n such that \operatorname{inv}(q) + \operatorname{inv}(q \cdot p) = k {}^\dagger {}^\ddagger, or determine if it is impossible to do so.{}^\dagger For two permutations p and q of the same size n, the permutation w = q \cdot p is such that w_i = q_{p_i} for all 1 \le i \le n.{}^\ddagger For a permutation p of size n, the function \operatorname{inv}(p) returns the number of inversions of p, i.e. the number of pairs of indices 1 \le i < j \le n such that p_i > p_j.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and k (1 \le n \le 3 \cdot 10^5, 0 \le k \le n(n - 1)) — the size of p and the target number of inversions.The second line of each test case contains n integers p_1, p_2, \ldots, p_n (1 \le p_i \le n, p_i's are pairwise distinct) — the given permutation p.It is guaranteed that the sum of n over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, print on one line "YES" if there exists a permutation q that satisfies the given condition, or "NO" if there is no such permutation.If the answer is "YES", on the second line, print n integers q_1, q_2, \ldots, q_n that represent such a satisfactory permutation q. If there are multiple such q's, print any of them.ExampleInput 53 42 3 15 52 3 5 1 46 115 1 2 3 4 69 513 1 4 2 5 6 7 8 91 01Output YES 3 2 1 NO NO YES 1 5 9 8 7 6 4 3 2 YES 1 NoteIn the first test case, we have q \cdot p = [2, 1, 3], \operatorname{inv}(q) = 3, and \operatorname{inv}(q \cdot p) = 1.In the fourth test case, we have q \cdot p = [9, 1, 8, 5, 7, 6, 4, 3, 2], \operatorname{inv}(q) = 24, and \operatorname{inv}(q \cdot p) = 27.
53 42 3 15 52 3 5 1 46 115 1 2 3 4 69 513 1 4 2 5 6 7 8 91 01
YES 3 2 1 NO NO YES 1 5 9 8 7 6 4 3 2 YES 1
2 seconds
256 megabytes
['constructive algorithms', 'data structures', 'greedy', '*2500']
E. No Palindromestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputChristopher Tin ft. Soweto Gospel Choir - Baba YetuඞYou are given a string s consisting of lowercase Latin characters. You need to partition^\dagger this string into some substrings, such that each substring is not a palindrome^\ddagger.^\dagger A partition of a string s is an ordered sequence of some k strings t_1, t_2, \ldots, t_k, such that t_1 + t_2 + \ldots + t_k = s, where + here represents the concatenation operation.^\ddagger A string s is considered a palindrome if it reads the same backwards as forwards. For example, \mathtt{racecar}, \mathtt{abccba}, and \mathtt{a} are palindromes, but \mathtt{ab}, \mathtt{dokibird}, and \mathtt{kurosanji} are not.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases.Each test case contains a string s consisting of lowercase Latin characters (1 \le |s| \le 10^6).It is guaranteed that the sum of |s| over all test cases does not exceed 10^6.OutputFor each test case, print on one line "YES" if there exists a partition of s whose parts are not palindromes, or "NO" if there is no such partition.If the answer is "YES", on the second line, print an integer k — the number of parts that s needs to be partitioned to such that each part is not a palindrome. On the third line, print k strings t_1, t_2, \ldots, t_k representing such a partition. If there are multiple such partitions, print any of them.ExampleInput 3sinktheyachtllllllllluwuowouwuOutput YES 1 sinktheyacht NO YES 3 uw uow ouwu NoteIn the first test case, since \mathtt{sinktheyacht} is already non-palindrome, the partition [\mathtt{sinktheyacht}] is valid.In the second test case, as any substring of the string s is palindrome, there are no valid partitions.In the third test case, another valid partition is [\mathtt{uw},\mathtt{uo}, \mathtt{wou}, \mathtt{wu}].
3sinktheyachtllllllllluwuowouwu
YES 1 sinktheyacht NO YES 3 uw uow ouwu
2 seconds
256 megabytes
['brute force', 'constructive algorithms', 'divide and conquer', 'greedy', 'hashing', 'implementation', 'math', 'strings', '*2000']
D. Buying Jewelstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputNightwish feat. Jonsu - Erämaan ViimeinenඞAlice has n coins and wants to shop at Bob's jewelry store. Today, although Bob has not set up the store yet, Bob wants to make sure Alice will buy exactly k jewels. To set up the store, Bob can erect at most 60 stalls (each containing an unlimited amount of jewels) and set the price per jewel for each stall to be an integer number of coins between 1 and 10^{18}.Fortunately, Bob knows that Alice buys greedily: and she will go to stall 1, buy as many jewels as possible, then go to stall 2, buy as many jewels as possible, and so on until the last stall. Knowing this, Bob can choose the number of stalls to set up, as well as set the price for each stall so that Alice buys exactly k jewels. Help Bob fulfill the task, or determine if it is impossible to do so.Note that Alice does not need to spend all her coins.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 1000) — the number of test cases. The description of the test cases follows.Each test case contains two positive integers n and k (1 \le n, k \le 10^{18}) — the number of coins Alice has and the number of jewels Bob wants Alice to have bought at the end.OutputFor each test case, print on one line "YES" if Bob can erect at most 60 stalls and set the prices for the stalls such that Alice buys exactly k jewels, or "NO" if it is impossible to do so.If the answer is "YES", on the second line, print an integer s (1 \le s \le 60) — the number of stalls to be set up by Bob. On the third line, print s positive integers p_1, p_2, \ldots, p_s (1 \le p_i \le 10^{18}) that represent such a satisfactory pricing p, where p_i is the price per jewel for stall i. If there are multiple such p's, print any of them.ExampleInput 37 36 4255 8Output YES 10 2 3 4 5 6 7 8 9 10 11 NO YES 8 128 64 32 16 8 4 2 1 NoteIn the first test case, at the first stall, Alice buys 3 jewels and is left with 1 coin. This is not enough to buy any jewels for any of the remaining stalls, so Alice buys exactly 3 jewels at the end.In the third test case, At the first stall, Alice buys 1 jewel and is left with 127 coins. At the second stall, Alice buys 1 jewel and is left with 63 coins. At the third stall, Alice buys 1 jewel and is left with 31 coins. At the fourth stall, Alice buys 1 jewel and is left with 15 coins. At the fifth stall, Alice buys 1 jewel and is left with 7 coins. At the sixth stall, Alice buys 1 jewel and is left with 3 coins. At the seventh stall, Alice buys 1 jewel and is left with 1 coin. At the eighth stall, Alice buys 1 jewel and is left with 0 coins. Therefore, Alice buys exactly 8 jewels in total.
37 36 4255 8
YES 10 2 3 4 5 6 7 8 9 10 11 NO YES 8 128 64 32 16 8 4 2 1
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'math', '*2000']
C. Ticket Hoardingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMaître Gims - Est-ce que tu m'aimes ?ඞAs the CEO of a startup company, you want to reward each of your k employees with a ticket to the upcoming concert. The tickets will be on sale for n days, and by some time travelling, you have predicted that the price per ticket at day i will be a_i. However, to prevent ticket hoarding, the concert organizers have implemented the following measures: A person may purchase no more than m tickets per day. If a person purchases x tickets on day i, all subsequent days (i.e. from day i+1 onwards) will have their prices per ticket increased by x. For example, if a = [1, 3, 8, 4, 5] and you purchase 2 tickets on day 1, they will cost 2 in total, and the prices from day 2 onwards will become [5, 10, 6, 7]. If you then purchase 3 more tickets on day 2, they will cost in total an additional 15, and the prices from day 3 onwards will become [13, 9, 10].Find the minimum spending to purchase k tickets.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains three integers n, m, and k (1 \le n \le 3 \cdot 10^5, 1 \le m \le 10^9, 1 \le k \le \min(nm, 10^9)) — the number of sale days, the maximum amount of ticket purchasable each day, and the number of tickets to be bought at the end.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9) — the price per ticket for each of the upcoming n days.It is guaranteed that the sum of n over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, print one integer: the minimum amount of money needed to purchase exactly k tickets.ExampleInput 44 2 38 6 4 24 2 88 6 4 25 100 110000 1 100 10 10006 3 95 5 5 5 5 5Output 10 64 1 72 NoteIn the first test case, one optimal way to buy 3 tickets is as follows: Buy 0 tickets on the first day. The prices per ticket for the remaining days are [6, 4, 2]. Buy 0 tickets on the second day. The prices per ticket for the remaining days are [4, 2]. Buy 1 ticket on the third day with cost 4. The price per ticket for the remaining day is [3]. Buy 2 tickets on the fourth day with cost 6. In the second test case, there is only one way to buy 8 tickets: Buy 2 tickets on the first day with cost 16. The prices per ticket for the remaining days are [8, 6, 4]. Buy 2 tickets on the second day with cost 16. The prices per ticket for the remaining days are [8, 6]. Buy 2 tickets on the third day with cost 16. The price per ticket for the remaining day is [8]. Buy 2 tickets on the fourth day with cost 16.
44 2 38 6 4 24 2 88 6 4 25 100 110000 1 100 10 10006 3 95 5 5 5 5 5
10 64 1 72
2 seconds
256 megabytes
['greedy', 'math', 'sortings', '*1400']
B. Battle Cowstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe HU - Shireg ShiregඞThere are n cows participating in a coding tournament. Cow i has a Cowdeforces rating of a_i (all distinct), and is initially in position i. The tournament consists of n-1 matches as follows: The first match is between the cow in position 1 and the cow in position 2. Subsequently, each match i is between the cow in position i+1 and the winner of match i-1. In each match, the cow with the higher Cowdeforces rating wins and proceeds to the next match. You are the owner of cow k. For you, winning the tournament is not important; rather, you want your cow to win in as many matches as possible. As an acquaintance of the tournament organizers, you can ask them to swap the position of your cow with another cow only once, or you can choose to do nothing.Find the maximum number of wins your cow can achieve.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and k (2 \le n \le 10^5, 1 \le k \le n) — the number of cows and your cow's index.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9) — the Cowdeforces rating of the cows. It is guaranteed that a_i's are pairwise different.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, print one integer: the maximum number of wins cow k can achieve if you choose to swap (or do nothing) optimally.ExampleInput 36 112 10 14 11 8 36 57 2 727 10 12 132 21000000000 1Output 1 2 0 NoteIn the first test case, it is optimal to do nothing. Let a' be the Cowdeforces rating of the cows in the original order (with your cow's rating bolded), then Initially, a' = [\mathbf{12}, 10, 14, 11, 8, 3]. Your cow plays against the cow with Cowdeforces rating 10 and wins. a' = [\mathbf{12}, 14, 11, 8, 3]. Your cow plays against the cow with Cowdeforces rating 14 and loses. In total, your cow wins 1 match.In the second test case, it is optimal to swap your cow to position 3. Then, let a' be the Cowdeforces rating of the cows in the order after the swap. Initially, a' = [7, 2, \mathbf{12}, 10, 727, 13]. The cow with Cowdeforces rating 7 plays against the cow with Cowdeforces rating 2 and wins. a' = [7, \mathbf{12}, 10, 727, 13]. The cow with Cowdeforces rating 7 plays against your cow, and your cow wins. a' = [\mathbf{12}, 10, 727, 13]. Your cow plays against the cow with Cowdeforces rating 10 and wins. a' = [\mathbf{12}, 727, 13]. Your cow plays against the cow with Cowdeforces rating 727 and loses. In total, your cow wins 2 matches.
36 112 10 14 11 8 36 57 2 727 10 12 132 21000000000 1
1 2 0
1 second
256 megabytes
['binary search', 'data structures', 'greedy', '*1200']
A. Dual Triggertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNgọt - LẦN CUỐI (đi bên em xót xa người ơi)ඞThere are n lamps numbered 1 to n lined up in a row, initially turned off. You can perform the following operation any number of times (possibly zero): Choose two non-adjacent{}^\dagger lamps that are currently turned off, then turn them on. Determine whether you can reach configuration s, where s_i = 1 means the i-th lamp is turned on, and s_i = 0 otherwise.{}^\dagger Only lamp i and i + 1 are adjacent for all 1 \le i < n. Note that lamp n and 1 are not adjacent when n \ne 2.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 1000) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer n (1 \le n \le 50) — the number of lamps.The second line of each test case contains a binary string s of size n — the final desired configuration.OutputFor each test case, print on one line "YES" if we can reach the configuration s via applying the given operation any number of times. Otherwise, print "NO".ExampleInput 510110101011010100100111060000001112111111111111Output YES NO YES NO YES NoteIn the first test case, the sequence of operation could have been as follows (note that initially s is all zero): \mathtt{0000000000} \to \mathtt{\color{red}{1}0000000\color{red}{1}0} \to \mathtt{1\color{red}{1}00000\color{red}{1}10} \to \mathtt{110\color{red}{1}0\color{red}{1}0110}.In the third test case, we don't have to do any operation.In the fourth test case, we cannot do any operation, but we need the first lamp to be on. Therefore, it is impossible to achieve the desired state.
510110101011010100100111060000001112111111111111
YES NO YES NO YES
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', '*900']
G. Shuffling Songstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVladislav has a playlist consisting of n songs, numbered from 1 to n. Song i has genre g_i and writer w_i. He wants to make a playlist in such a way that every pair of adjacent songs either have the same writer or are from the same genre (or both). He calls such a playlist exciting. Both g_i and w_i are strings of length no more than 10^4.It might not always be possible to make an exciting playlist using all the songs, so the shuffling process occurs in two steps. First, some amount (possibly zero) of the songs are removed, and then the remaining songs in the playlist are rearranged to make it exciting.Since Vladislav doesn't like when songs get removed from his playlist, he wants the making playlist to perform as few removals as possible. Help him find the minimum number of removals that need to be performed in order to be able to rearrange the rest of the songs to make the playlist exciting.InputThe first line of the input contains a single integer t (1 \le t \le 1000) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer n (1 \le n \le 16) — the number of songs in the original playlist.Then n lines follow, the i-th of which contains two strings of lowercase letters g_i and w_i (1 \leq |g_i|, |w_i| \leq 10^4) — the genre and the writer of the i-th song. Where |g_i| and |w_i| are lengths of the strings.The sum of 2^n over all test cases does not exceed 2^{16}.The sum of |g_i| + |w_i| over all test cases does not exceed 4 \cdot 10^5.OutputFor each test case, output a single integer — the minimum number of removals necessary so that the resulting playlist can be made exciting.ExampleInput 41pop taylorswift4electronic themotanselectronic carlasdreamspop themotanspop irinarimes7rap eminemrap drdrerap kanyewestpop taylorswiftindierock arcticmonkeysindierock arcticmonkeyspunkrock theoffspring4a bc de fg hOutput 0 0 4 3 NoteIn the first test case, the playlist is already exciting.In the second test case, if you have the songs in the order 4, 3, 1, 2, it is exciting, so you don't need to remove any songs.In the third test case, you can remove songs 4, 5, 6, 7. Then the playlist with songs in the order 1, 2, 3 is exciting.
41pop taylorswift4electronic themotanselectronic carlasdreamspop themotanspop irinarimes7rap eminemrap drdrerap kanyewestpop taylorswiftindierock arcticmonkeysindierock arcticmonkeyspunkrock theoffspring4a bc de fg h
0 0 4 3
3 seconds
256 megabytes
['bitmasks', 'dfs and similar', 'dp', 'graphs', 'hashing', 'implementation', 'strings', '*1900']
F. 0, 1, 2, Tree!time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFind the minimum height of a rooted tree^{\dagger} with a+b+c vertices that satisfies the following conditions: a vertices have exactly 2 children, b vertices have exactly 1 child, and c vertices have exactly 0 children. If no such tree exists, you should report it. The tree above is rooted at the top vertex, and each vertex is labeled with the number of children it has. Here a=2, b=1, c=3, and the height is 2. ^{\dagger} A rooted tree is a connected graph without cycles, with a special vertex called the root. In a rooted tree, among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. The distance between two vertices in a tree is the number of edges in the shortest path between them. The height of a rooted tree is the maximum distance from a vertex to the root.InputThe first line contains an integer t (1 \leq t \leq 10^4) — the number of test cases.The only line of each test case contains three integers a, b, and c (0 \leq a, b, c \leq 10^5; 1 \leq a + b + c).The sum of a + b + c over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, if no such tree exists, output -1. Otherwise, output one integer — the minimum height of a tree satisfying the conditions in the statement.ExampleInput 102 1 30 0 10 1 11 0 21 1 33 1 48 17 924 36 481 0 00 3 1Output 2 0 1 1 -1 3 6 -1 -1 3 NoteThe first test case is pictured in the statement. It can be proven that you can't get a height smaller than 2.In the second test case, you can form a tree with a single vertex and no edges. It has height 0, which is clearly optimal.In the third test case, you can form a tree with two vertices joined by a single edge. It has height 1, which is clearly optimal.
102 1 30 0 10 1 11 0 21 1 33 1 48 17 924 36 481 0 00 3 1
2 0 1 1 -1 3 6 -1 -1 3
2 seconds
256 megabytes
['bitmasks', 'brute force', 'greedy', 'implementation', 'trees', '*1700']
E. Nearly Shortest Repeating Substringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s of length n consisting of lowercase Latin characters. Find the length of the shortest string k such that several (possibly one) copies of k can be concatenated together to form a string with the same length as s and, at most, one different character.More formally, find the length of the shortest string k such that c = \underbrace{k + \cdots + k}_{x\rm\ \text{times}} for some positive integer x, strings s and c has the same length and c_i \neq s_i for at most one i (i.e. there exist 0 or 1 such positions).InputThe first line contains a single integer t (1 \leq t \leq 10^3) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 2\cdot10^5) — the length of string s.The second line of each test case contains the string s, consisting of lowercase Latin characters.The sum of n over all test cases does not exceed 2\cdot10^5.OutputFor each test case, print the length of the shortest string k satisfying the constraints in the statement.ExampleInput 54abaa4abba13slavicgslavic8hshahaha20stormflamestornflameOutput 1 4 13 2 10 NoteIn the first test case, you can select k = \texttt{a} and k+k+k+k = \texttt{aaaa}, which only differs from s in the second position.In the second test case, you cannot select k of length one or two. We can have k = \texttt{abba}, which is equal to s.
54abaa4abba13slavicgslavic8hshahaha20stormflamestornflame
1 4 13 2 10
2 seconds
256 megabytes
['brute force', 'implementation', 'number theory', 'strings', '*1500']
D. Product of Binary Decimalstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call a number a binary decimal if it is a positive integer and all digits in its decimal notation are either 0 or 1. For example, 1\,010\,111 is a binary decimal, while 10\,201 and 787\,788 are not.Given a number n, you are asked whether or not it is possible to represent n as a product of some (not necessarily distinct) binary decimals.InputThe first line contains a single integer t (1 \leq t \leq 5 \cdot 10^4) — the number of test cases.The only line of each test case contains a single integer n (1 \leq n \leq 10^5).OutputFor each test case, output "YES" (without quotes) if n can be represented as a product of binary decimals, and "NO" (without quotes) otherwise.You can output "YES" and "NO" in any case (for example, strings "yES", "yes", and "Yes" will be recognized as a positive response).ExampleInput 111211146411222110110100000991122024124211001Output YES YES YES YES YES YES NO NO NO NO YES NoteThe first five test cases can be represented as a product of binary decimals as follows: 121 = 11 \times 11. 1 = 1 is already a binary decimal. 14\,641 = 11 \times 11 \times 11 \times 11. 12\,221 = 11 \times 11 \times 101. 10\,110 = 10\,110 is already a binary decimal.
111211146411222110110100000991122024124211001
YES YES YES YES YES YES NO NO NO NO YES
3 seconds
256 megabytes
['brute force', 'dp', 'implementation', 'number theory', '*1100']
C. Clock Conversiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven the time in 24-hour format, output the equivalent time in 12-hour format. 24-hour format divides the day into 24 hours from 00 to 23, each of which has 60 minutes from 00 to 59. 12-hour format divides the day into two halves: the first half is \mathrm{AM}, and the second half is \mathrm{PM}. In each half, the hours are numbered in the order 12, 01, 02, 03, \dots, 11. Each hour has 60 minutes numbered from 00 to 59. InputThe first line contains a single integer t (1 \leq t \leq 1440) — the number of test cases.The only line of each test case contains a string s of length 5 with format hh:mm representing a valid time in the 24-hour format. hh represents the hour from 00 to 23, and mm represents the minute from 00 to 59.The input will always be a valid time in 24-hour format.OutputFor each test case, output two strings separated by a space ("hh:mm AM" or "hh:mm PM"), which are the 12-hour equivalent to the time provided in the test case (without quotes).You should output the time exactly as indicated; in particular, you should not remove leading zeroes.ExampleInput 1109:4118:0612:1400:5900:0014:3401:0119:0711:5912:0021:37Output 09:41 AM 06:06 PM 12:14 PM 12:59 AM 12:00 AM 02:34 PM 01:01 AM 07:07 PM 11:59 AM 12:00 PM 09:37 PM
1109:4118:0612:1400:5900:0014:3401:0119:0711:5912:0021:37
09:41 AM 06:06 PM 12:14 PM 12:59 AM 12:00 AM 02:34 PM 01:01 AM 07:07 PM 11:59 AM 12:00 PM 09:37 PM
1 second
256 megabytes
['implementation', 'math', '*800']
B. Upscalingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an integer n. Output a 2n \times 2n checkerboard made of 2 \times 2 squares alternating '\texttt{#}' and '\texttt{.}', with the top-left cell being '\texttt{#}'. The picture above shows the answers for n=1,2,3,4. InputThe first line contains an integer t (1 \leq t \leq 20) — the number of test cases.The only line of each test case contains a single integer n (1 \leq n \leq 20) — it means you need to output a checkerboard of side length 2n.OutputFor each test case, output 2n lines, each containing 2n characters without spaces — the checkerboard, as described in the statement. Do not output empty lines between test cases.ExampleInput 41234Output ## ## ##.. ##.. ..## ..## ##..## ##..## ..##.. ..##.. ##..## ##..## ##..##.. ##..##.. ..##..## ..##..## ##..##.. ##..##.. ..##..## ..##..##
41234
## ## ##.. ##.. ..## ..## ##..## ##..## ..##.. ..##.. ##..## ##..## ##..##.. ##..##.. ..##..## ..##..## ##..##.. ##..##.. ..##..## ..##..##
1 second
256 megabytes
['implementation', '*800']
A. Stair, Peak, or Neither?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three digits a, b, and c. Determine whether they form a stair, a peak, or neither. A stair satisfies the condition a<b<c. A peak satisfies the condition a<b>c. InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The only line of each test case contains three digits a, b, c (0 \leq a, b, c \leq 9).OutputFor each test case, output "STAIR" if the digits form a stair, "PEAK" if the digits form a peak, and "NONE" otherwise (output the strings without quotes).ExampleInput 71 2 33 2 11 5 33 4 10 0 04 1 74 5 7Output STAIR NONE PEAK PEAK NONE NONE STAIR
71 2 33 2 11 5 33 4 10 0 04 1 74 5 7
STAIR NONE PEAK PEAK NONE NONE STAIR
1 second
256 megabytes
['implementation', '*800']
K. Make Triangletime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n positive integers x_1, x_2, \ldots, x_n and three positive integers n_a, n_b, n_c satisfying n_a+n_b+n_c = n. You want to split the n positive integers into three groups, so that: The first group contains n_a numbers, the second group contains n_b numbers, the third group contains n_c numbers. Let s_a be the sum of the numbers in the first group, s_b be the sum in the second group, and s_c be the sum in the third group. Then s_a, s_b, s_c are the sides of a triangle with positive area.Determine if this is possible. If this is possible, find one way to do so.InputEach test contains multiple test cases. The first line contains an integer t (1\le t\le 100\,000) — the number of test cases. The descriptions of the t test cases follow.The first line of each test case contains the integers n, n_a, n_b, n_c (3 \leq n \leq 200\,000, 1\leq n_a,n_b,n_c \leq n-2, n_a+n_b+n_c = n) — the number of integers to split into three groups, and the desired sizes of the three groups.The second line of each test case contains n integers x_1, x_2, \ldots, x_n (1 \leq x_i \leq 10^{9}).It is guaranteed that the sum of n over all test cases does not exceed 200\,000.OutputFor each test case, print \texttt{YES} if it is possible to split the numbers into three groups satisfying all the conditions. Otherwise, print \texttt{NO}.If such a split exists, then describe the three groups as follows.On the next line, print n_a integers a_1, a_2, \ldots, a_{n_a} — the numbers in the first group.On the next line, print n_b integers b_1, b_2, \ldots, b_{n_b} — the numbers in the second group.On the next line, print n_c integers c_1, c_2, \ldots, c_{n_c} — the numbers in the third group.These n_a+n_b+n_c=n integers should be a permutation of x_1, x_2, \ldots, x_n, and they should satisfy the conditions from the statement.If there are multiple solutions, print any of them.ExampleInput 46 2 2 21 1 1 1 1 15 3 1 11 1 1 1 16 2 2 21 1 1 1 1 38 1 2 516 1 1 1 1 1 1 12Output YES 1 1 1 1 1 1 NO NO YES 16 12 1 1 1 1 1 1 NoteIn the first test case, we can put two 1s into each group: the sum in each group would be 2, and there exists a triangle with positive area and sides 2, 2, 2.In the second and third test cases, it can be shown that there is no such way to split numbers into groups.In the fourth test case, we can put number 16 into the first group, with sum 16, numbers 12 and 1 into the second group, with sum 13, and the remaining five 1s into the third group, with sum 5, as there exists a triangle with positive area and sides 16, 13, 5.
46 2 2 21 1 1 1 1 15 3 1 11 1 1 1 16 2 2 21 1 1 1 1 38 1 2 516 1 1 1 1 1 1 12
YES 1 1 1 1 1 1 NO NO YES 16 12 1 1 1 1 1 1
2 seconds
256 megabytes
['constructive algorithms', 'math']
J. Amanda the Amoebatime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis problem has an attachment. You can use it to simulate and visualize the movements of the amoeba.Amoeba Amanda lives inside a rectangular grid of square pixels. Her body occupies some of these pixels. Other pixels may be either free or blocked. Amanda moves across the grid using the so-called amoeboid movement. In each step of such a movement, her body first shrinks by one pixel (one pixel of the body is removed and becomes free), and then grows at a different place (one previously-free pixel is added to the body).To prevent structural damage, Amanda's body always occupies a connected region of pixels, which means that any pair of pixels forming the body can be connected by a sequence of adjacent pixels without ever leaving the body. Two pixels are considered adjacent if they share a common side (each pixel has at most 4 neighbours). The body remains connected even during the movement, including the moment after removing a pixel and before adding another one.Your task is to help Amanda find her way around. Given her initial position and desired final position, suggest a sequence of valid moves leading from the former to the latter. Illustration of sample 1: The filled shape is the initial position, the dotted region is the final position. InputThe first line contains two integers r and c (1\le r,c \le 50) — the size of the rectangular grid in pixels.The next r lines contain c characters each, describing the initial position of Amanda. Each of those characters is either a dot \texttt{.} denoting a free pixel, an asterisk \texttt{*} denoting Amanda's body, or an \texttt{X} denoting a blocked pixel which may never be occupied.The next line is empty.The next r lines describe the desired final position in the same format as the initial position.It is guaranteed that: The number of pixels forming Amanda's body is the same in both positions, and it is at least 2. The body of Amanda is connected in the initial position. The body of Amanda is connected in the final position. The blocked pixels do not change between the descriptions of the initial and final position, their placement is exactly the same in both positions. OutputPrint \texttt{YES} if it is possible for Amanda to go from the initial position to the final one. Otherwise, print \texttt{NO}.If it is possible, on the next line print one integer m (0\le m\le 10\,000) — the number of moves to execute.The following m lines must contain four integer coordinates each: i_1, j_1, i_2, j_2 (1\le i_1,i_2\le r, 1\le j_1,j_2\le c). These four coordinates specify one move, meaning that the pixel at i_1-th row and j_1-th column is first removed from the body. Then, (i_2,j_2) must designate a different location where one pixel is added.The sequence should consist only of valid moves and after the last move, Amanda's body should occupy the desired final position.If there are multiple solutions, print any of them.Under the assumptions of this problem, it can be proven that if it is possible for Amanda to go from the initial position to the desired final one, then it is possible to do it with at most 10\,000 moves.ExamplesInput 5 8.******.**.X**..*******.**.X**...******..******....X****.*******...X****.******.Output YES 5 3 1 3 8 2 1 2 8 4 1 4 8 2 2 4 7 4 2 2 7 Input 2 5*.X..**X....X**..X*.Output NO NoteIn the first sample, Amanda executes 5 moves to reach the final position, as shown in the figure below.
5 8.******.**.X**..*******.**.X**...******..******....X****.*******...X****.******.
YES 5 3 1 3 8 2 1 2 8 4 1 4 8 2 2 4 7 4 2 2 7
2 seconds
256 megabytes
['graphs', 'implementation', 'trees', 'two pointers']
I. Diskstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n disks in the plane. The center of each disk has integer coordinates, and the radius of each disk is a positive integer. No two disks overlap in a region of positive area, but it is possible for disks to be tangent to each other.Your task is to determine whether it is possible to change the radii of the disks in such a way that: Disks that were tangent to each other remain tangent to each other. No two disks overlap in a region of positive area. The sum of all radii strictly decreases. The new radii are allowed to be arbitrary positive real numbers. The centers of the disks cannot be changed.InputThe first line contains an integer n (1\le n \le 1000) — the number of disks.The next n lines contain three integers each. The i-th of such lines contains x_i, y_i (-10^9 \leq x_i, y_i \leq 10^9), and r_i (1 \leq r_i \leq 10^9) — the coordinates of the center, and the radius, of the i-th disk.OutputPrint \texttt{YES} if it is possible to change the radii in the desired manner. Otherwise, print \texttt{NO}.ExamplesInput 50 2 10 0 14 -3 411 0 311 5 2Output YES Input 42 2 27 2 37 7 22 7 3Output NO NoteIn the first sample, one can decrease the radii of the first and third disk by 0.5, and increase the radius of the second disk by 0.5. This way, the sum of all radii decreases by 0.5. The situation before and after changing the radii is depicted below. First sample (left) and a valid way to change the radii of the disks (right). In the second sample, depicted below, there is no way to change the radii of the disks in the desired manner. Second sample.
50 2 10 0 14 -3 411 0 311 5 2
YES
2 seconds
256 megabytes
['dfs and similar', 'geometry', 'graph matchings', 'graphs']
H. Division Avoidancetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA newly discovered organism can be represented as a set of cells on an infinite grid. There is a coordinate system on the grid such that each cell has two integer coordinates x and y. A cell with coordinates x=a and y=b will be denoted as (a, b).Initially, the organism consists of a single cell (0, 0). Then zero or more divisions can happen. In one division, a cell (a, b) is removed and replaced by two cells (a+1, b) and (a, b+1).For example, after the first division, the organism always consists of two cells (1, 0) and (0, 1), and after the second division, it is either the three cells (2, 0), (1, 1) and (0, 1), or the three cells (1, 0), (1, 1) and (0, 2).A division of a cell (a, b) can only happen if the cells (a+1, b) and (a, b+1) are not yet part of the organism. For example, the cell (1, 0) cannot divide if the organism currently consists of the three cells (1, 0), (1, 1) and (0, 2), since the cell (1, 1) that would be one of the results of this division is already part of the organism.You are given a set of forbidden cells {(c_i, d_i)}. Is it possible for the organism to contain none of those cells after zero or more divisions? InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10\,000) — the number of test cases. The descriptions of the t test cases follow.The first line of each test case contains an integer n (1 \le n \le 10^6) — the number of forbidden cells.The next n lines contain two integers each. The i-th of such lines contains c_i and d_i (0 \le c_i, d_i \le 10^9) — the coordinates of the i-th forbidden cell. It is guaranteed that all forbidden cells are distinct.It is guaranteed that the sum of values of n over all test cases does not exceed 10^6.OutputFor each test case, print \texttt{YES} if it is possible for the organism to contain no forbidden cells after zero or more divisions. Otherwise, print \texttt{NO}.ExampleInput 240 01 00 11 1160 00 10 20 31 01 11 21 32 02 12 22 33 03 13 23 3Output YES NO NoteIn the first test case, dividing the following cells in the following order creates an organism without any forbidden cells: (0, 0), (1, 0), (1, 1), (0, 1), (2, 1), (2, 2), (1, 2), (1, 1). The following picture demonstrates how the organism changes during this process:In the second test case, you can see that, surprisingly, any organism always has at least one cell in the 0 \le x, y \le 3 square, no matter how many divisions we do.
240 01 00 11 1160 00 10 20 31 01 11 21 32 02 12 22 33 03 13 23 3
YES NO
2 seconds
256 megabytes
['greedy', 'math']
G. Scootertime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe Czech Technical University campus consists of n buildings, indexed from 1 to n. In each building, there can be a math class scheduled, or a computer science class, or neither (but not both). Additionally, in each building, there is at most one professor, and each professor is either an expert in mathematics or in computer science.As an intern at University Express Inc., your job is to quickly transport the professors to their classes. For this, you have been granted a brand new two-person scooter, able to accommodate yourself, plus at most one passenger.Initially, you are the only person on the scooter. When you arrive at a building, you may drop off or pick up professors to/from that building. However, in order to improve the efficiency of your task, you are allowed to drive to each of the n buildings at most once, in the order of your choice (you can also decide where to start the itinerary). After the end of your itinerary, in each building where a math class is scheduled, there must be a professor expert in math, and in each building where a computer science class is scheduled, there must be a professor expert in computer science.Devise an itinerary that makes it possible to teach all classes.InputThe first line contains an integer n (1\le n \le 2000) — the number of buildings in the campus.The second line contains a string of c of length n consisting of the characters \texttt{-}, \texttt{C}, \texttt{M} — the i-th character denotes the subject of the class scheduled in the i-th building. \texttt{C} stands for computer science, \texttt{M} stands for mathematics, while \texttt{-} means that there is no class scheduled in the i-th building.The third line contains a string p of length n consisting of the characters \texttt{-}, \texttt{C}, \texttt{M} — the i-th character denotes the expertise of the professor in the i-th building (if there is a professor). \texttt{C} stands for computer science, \texttt{M} stands for mathematics, while \texttt{-} means that there is no professor in the i-th building.It is guaranteed that, for all tests given to your program, there exists a valid itinerary.OutputIn the first line print an integer l — the number of operations in your chosen itinerary. The i-th (1 \leq i \leq l) of the next l lines must contain one of three commands: \texttt{DRIVE } x — go to the building with the number x (1 \leq x \leq n); \texttt{PICKUP} — pick up the professor which was initially at the current building; \texttt{DROPOFF} — drop off the passenger professor at the current building. In order for the itinerary to be valid, the following conditions must hold: No two \texttt{DRIVE} instructions should go to the same building; At most one \texttt{DROPOFF} and one \texttt{PICKUP} instruction in this order should be issued at each specific building; For all \texttt{PICKUP} instructions, there must be a professor initially at the building, as well as no one already riding along on the scooter; For all \texttt{DROPOFF} instructions, there must be a professor riding along at the time of the command; After the itinerary, in each building, if there is a class in that building, there must be a professor expert in the class' subject (either initially, or because they were dropped off there). Note that, in particular, you cannot pick up a professor that you just dropped off for an itinerary to be valid.ExamplesInput 3CM--CMOutput 7 DRIVE 3 PICKUP DRIVE 2 DROPOFF PICKUP DRIVE 1 DROPOFF Input 1CCOutput 0 Input 2-MMCOutput 4 DRIVE 1 PICKUP DRIVE 2 DROPOFFNoteIn the first sample, You start by driving to building number 3. You then pick up the mathematics professor. After dropping him off at building number 2, where a mathematics class is being held, you pick up the computer science professor from there, and drop her off at building number 1, finishing your itinerary.
3CM--CM
7 DRIVE 3 PICKUP DRIVE 2 DROPOFF PICKUP DRIVE 1 DROPOFF
2 seconds
512 megabytes
['graphs', 'greedy']
F. Datingtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are the developer of a dating app which ignores gender completely. The app has n users, indexed from 1 to n. Each user's profile features a list of the activities they enjoy doing. There are m possible activities, indexed from 1 to m.A match between two users is good if they share at least one activity and, at the same time, both of them like at least one activity that the other user does not like.Find a good match if it exists.InputThe first line contains two integers n and m (2 \leq n \leq 200\,000, 1 \leq m \leq 10^6) — the number of users and the number of activities.Each of the following n lines contains a number k_i (0 \leq k_i \leq m) — the number of activities that user i likes — followed by k_i distinct integers from 1 to m — the activities user i likes. It is guaranteed that k_1+k_2+\cdots+k_n does not exceed 10^6.OutputPrint \texttt{YES} if a good match exists. Otherwise, print \texttt{NO}.If a good match exists, on the next line print two integers — the indexes of two users that make a match.ExamplesInput 3 53 1 2 45 1 2 3 4 52 1 5Output YES 3 1 Input 3 31 11 23 2 3 1Output NO NoteIn the first sample, users 1 and 3 form a match, because they share activity 1, and, furthermore, user 3 likes activity 5 (which user 1 does not like) and user 1 likes activity 4 (which user 3 does not like). Note that users 1 and 2, as well as users 2 and 3, do not form a match, as there is no activity that users 1 or 3 like, and user 2 doesn't like.
3 53 1 2 45 1 2 3 4 52 1 5
YES 3 1
3 seconds
512 megabytes
['greedy', 'sortings', 'trees']
E. Damage per Secondtime limit per test5 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputYou just created a new character in your favourite role-playing game and now have to decide how to skill him. The two skill attributes to be chosen are: damage per hit and hits per second. Damage per hit is the amount of damage you deal with a single hit, while hits per second is the number of hits you can make in one second. Initially, both skill attributes are set at 0. You have k skill points to distribute as you want; in other words, you can choose the values of the two skills so that they are positive integers with sum at most k. The tutorial of the game (the boring part you want to finish as soon as possible) consists of n monsters to be killed one after the other. The i-th monster has h_i health points, i.e., it dies after you have inflicted at least h_i damage.How can you assign the two skill attributes to minimize the time necessary to kill all the n monsters?InputThe first line contains two integers n and k (1\leq n\leq200\,000, 2\leq k\leq200\,000) — the number of enemies and the number of skill points.The second line contains n integers h_i (1\leq h_i\leq10^{13}) — the health of the ith enemy.OutputPrint two positive integers x and y (1\le x, y and x+y\le k) — the number of skill points you want to invest in damage per hit and hits per second. If there are multiple optimal solutions, print any of them.ExamplesInput 1 714Output 3 4 Input 4 91 2 3 4Output 4 5 Input 5 133 4 5 6 7Output 7 6 NoteIn the first sample, there is only one monster and you have 7 skill points to distribute. If you make 3 damage per hit, you will need 5 hits to kill it. If you do 4 hits per second, you will need 1.25 seconds to beat the monster. There is no way to beat the monster faster than this.In the second sample, you will need one hit for each monster and a total time of 0.8 seconds if you distribute 4 skill points on damage per hit and the remaining 5 points on hits per second.
1 714
3 4
5 seconds
1024 megabytes
['brute force', 'math']
D. Funny or Scary?time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are designing a new video game. It has n scenarios, which the player may play in any order, but each scenario must be played exactly once. When a player switches from a scenario to another scenario, the game shows a specially crafted transition video to make it all feel part of one big story. This video is specific to a pair of scenarios, but not to their order, in other words, the video playing when switching from scenario a to scenario b is the same as the video playing when switching from scenario b to scenario a. Therefore, you need to create \frac{n(n-1)}{2} different transition videos, one for each possible pair of different scenarios.Each transition video can be either funny or scary. It is boring to see too many funny videos or too many scary videos in a row. Therefore, your goal is to create the videos in such a way that no matter in which order does the player approach the scenarios, they will never see more than \lceil \frac{3n}{4} \rceil transition videos of the same type in a row.You have already come up with ideas for at most \lfloor \frac{n}{2} \rfloor of the transition videos, and therefore already know if those will be funny or scary. Now you need to choose funny or scary for all other transition videos in such a way that the above requirement is satisfied.InputThe first line contains a single integer n (2 \le n \le 24) — the number of scenarios in the game.The next n lines describe the partial transition video plan. Each of those lines contains n characters. The j-th character of the i-th line corresponds to the transition video between the i-th and the j-th scenarios. It will be F if the corresponding transition video will be funny, S if the corresponding transition video will be scary, ? if the corresponding transition video is still undecided, or . if i=j.It is guaranteed that the i-th character of the j-th line and the j-th character of the i-th line will be the same for all i and j. It is guaranteed that at most \lfloor \frac{n}{2} \rfloor (n divided by 2, rounded down) transition videos will already be decided, in other words, that at most 2\lfloor \frac{n}{2} \rfloor characters in the input will be F or S.OutputPrint n lines describing the full transition video plan in the same format as the input. Each of those lines must contain n characters. The j-th character of the i-th line must be F if the corresponding transition video is funny, S if the corresponding transition video is scary, or . if i=j.Each ? character from the input must be replaced with either F or S, and all other characters from the input must remain unchanged. It must still hold that the i-th character of the j-th line and the j-th character of the i-th line are the same for all i and j.For each permutation of the n scenarios, it must hold that the transition videos corresponding to playing the scenarios in this order do not have more than \lceil \frac{3n}{4} \rceil (3n divided by 4, rounded up) videos of the same type consecutively.If there are multiple solutions, print any of them. It can be proven that for all inputs satisfying the constraints of this problem a solution always exists.ExamplesInput 5.?F???.???F?.S???S.?????.Output .FFFF F.FFF FF.SF FFS.F FFFF.Input 12.????????????.????????????.????????????.????????????.????????????.????????????.????????????.????????????.????????????.????????????.????????????.Output .SSSFFSSSSFS S.SFFSFSFFFS SS.SFFFSSSFS SFS.FFSSSSFS FFFF.FFFFFSF FSFFF.SFFSFF SFFSFS.SSSFS SSSSFFS.SSFS SFSSFFSS.SFS SFSSFSSSS.FS FFFFSFFFFF.F SSSSFFSSSSF. NoteIn the first sample: We are allowed \lceil \frac{3\cdot 5}{4} \rceil=4 transition videos of the same type in a row, but for any permutation of the 5 scenarios the player will see only 4 transition videos in total, therefore we can choose funny or scary freely. We must still respect the already chosen types.In the second sample: One of the 479001600 possible permutations of scenarios is 1, 7, 4, 12, 9, 8, 2, 6, 10, 3, 11, 5. The player will get the following sequence of transition videos for this permutation: SSSSSSSSSFS. Even though this sequence has 10 scary transition videos in total, it has only 9 scary transition videos in a row, which is the maximum allowed amount (\lceil \frac{3\cdot 12}{4} \rceil=9).
5.?F???.???F?.S???S.?????.
.FFFF F.FFF FF.SF FFS.F FFFF.
2 seconds
256 megabytes
['constructive algorithms']
C. Annual Ants' Gatheringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputDeep within a forest lies an ancient tree, home to n ants living in n tiny houses, indexed from 1 to n, connected by the branches of the tree. Once a year, all the ants need to gather to watch the EUC. For this, all ants move along the n-1 branches of the tree they live on to meet at the home of one ant.However, this year the ants could not agree on where to meet and need your help to gather up. You can tell all the ants currently at house u to move to house v if there is a branch directly connecting those two houses. However, the ants ignore your command if there are fewer ants gathered in house v than in house u, i.e., if it would be easier for the ants from house v to move. This even holds true if no ant at all is currently in house v. You can give this kind of commands as many times as you want.Is it possible for you to gather all the ants in a single house?InputThe first line contains one integer n (1\leq n\leq 200\,000) — the number of ant homes.Each of the following n-1 lines contains two integers u and v (1\leq u, v\leq n) — there is a branch directly connecting the house u and house v. It is guaranteed that every ant can reach the house of any other ant just by following the branches of the tree.OutputPrint \texttt{YES} if it is possible to gather all the ants in a single house. Otherwise, print \texttt{NO}.ExamplesInput 75 13 24 63 67 11 3Output YES Input 51 44 23 25 3Output NO Input 64 55 66 12 63 2Output YES NoteIn the first sample, you can gather all the ants at house 3 as follows: You tell to the ant at house 4 to move to house 6. You tell to the ant at house 2 to move to house 3. You tell to the two ants at house 6 to move to house 3 (which already contains two ants). You tell to the ant at house 5 to move to house 1. You tell to the ant at house 7 to move to house 1 (which already contains two ants). You tell to the three ants at house 1 to move to house 3 (which already contains four ants). In the second sample, it is impossible to gather all the ants in a single house.
75 13 24 63 67 11 3
YES
2 seconds
256 megabytes
['dfs and similar', 'dp', 'greedy', 'trees']
B. Charming Mealstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe Czech cuisine features n appetizers and n main dishes. The i-th appetizer has spiciness a_i, and the i-th main dish has spiciness b_i.A typical Czech meal consists of exactly one appetizer and one main dish. You want to pair up the n appetizers and n main dishes into n meals with each appetizer and each main dish being included in exactly one meal.Your meals shall surprise the diners, so you want the spiciness levels of the two parts of the same meal to be as different as possible. The charm of a meal is the difference (in absolute value) between the spiciness of the appetizer and the spiciness of the main dish. So, a meal consisting of an appetizer with spiciness x and a main dish with spiciness y has charm equal to |x-y|.You want to maximize the minimum charm of the resulting n meals. What is the largest possible value of the minimum charm that you can achieve?InputEach test contains multiple test cases. The first line contains an integer t (1\le t\le 1\,000) — the number of test cases. The descriptions of the t test cases follow.The first line of each test case contains a single integer n (1 \leq n \leq 5\,000) —the number of appetizers and main dishes.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \leq a_i \leq 10^{9}) — the spicinesses of the n appetizers.The third line of each test case contains n integers b_1, b_2, \ldots, b_n (0 \leq b_i \leq 10^{9}) — the spicinesses of the n main dishes.It is guaranteed that the sum of n^2 over all test cases does not exceed 25\cdot 10^6.OutputFor each test case, print the largest possible value of the minimum charm you can achieve.ExampleInput 430 0 01000000000 1000000000 100000000051 2 3 4 51 2 3 4 560 0 0 100 100 100100 100 100 0 0 0714 25 62 74 86 95 1251 62 71 72 92 20 84Output 1000000000 2 100 30 NoteIn the first test case, no matter how you pair up the appetizers with the main dishes, each meal will have an appetizer with spiciness 0 and a main dish with spiciness 1000000000, so the charm of each meal will be 1000000000.In the second test case, one optimal way to pair up appetizers and main dishes is: (1, 5), (2, 4), (3, 1), (4, 2), (5, 3). The corresponding meals have charms: 4, 2, 2, 2, 2. The resulting minimum charm is 2.In the third test case, one way to maximize the minimum charm is to pair up the three appetizers with spiciness 0 with the three main dishes with spiciness 100, and the three appetizers with spiciness 100 with the three main dishes with spiciness 0. Doing so, the charm of each meal will be exactly 100.
430 0 01000000000 1000000000 100000000051 2 3 4 51 2 3 4 560 0 0 100 100 100100 100 100 0 0 0714 25 62 74 86 95 1251 62 71 72 92 20 84
1000000000 2 100 30
2 seconds
256 megabytes
['binary search', 'brute force', 'greedy', 'sortings']
A. Grovetime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou want to plant trees in a square lawn of size n \times n whose corners have Cartesian coordinates (0, 0), (n, 0), (0, n), and (n, n). Trees can only be planted at locations with integer coordinates. Every tree will grow roots within a disk of radius r centered at the location where the tree was planted; such disks must be fully contained in the lawn (possibly touching the boundary of the lawn) and can only intersect each other on their boundaries.Find a configuration that maximizes the number of trees.InputThe first and only line contains an integer n (1 \leq n \leq 20) and a real number r (0 < r \leq n/2) — the length of the sides of the lawn, and the radius of the disks where each tree will grow roots. The real number r is given in decimal notation with at least 1 and at most 3 digits after the decimal point.OutputIn the first line, print the maximum number m of trees that can be planted.In the next m lines, print a configuration that maximizes the number of trees. Specifically, in the (i+1)-th line, print two integers x and y — the coordinates of the location where the i-th tree should be planted. You can print the trees in any order.If there are multiple solutions, print any of them.ExamplesInput 6 1.241Output 2 4 2 2 4 Input 9 2.0Output 4 2 2 7 2 2 6 6 6 NoteFor the first sample, the sample output is shown in the following figure. Note that this is not the only configuration that maximizes the number of trees. For the second sample, the sample output is shown in the following figure. Note that this is not the only configuration that maximizes the number of trees.
6 1.241
2 4 2 2 4
4 seconds
256 megabytes
['brute force', 'dfs and similar', 'dp', 'geometry', 'probabilities']
G. MST with Matchingtime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an undirected connected graph on n vertices. Each edge of this graph has a weight; the weight of the edge connecting vertices i and j is w_{i,j} (or w_{i,j} = 0 if there is no edge between i and j). All weights are positive integers.You are also given a positive integer c.You have to build a spanning tree of this graph; i. e. choose exactly (n-1) edges of this graph in such a way that every vertex can be reached from every other vertex by traversing some of the chosen edges. The cost of the spanning tree is the sum of two values: the sum of weights of all chosen edges; the maximum matching in the spanning tree (i. e. the maximum size of a set of edges such that they all belong to the chosen spanning tree, and no vertex has more than one incident edge in this set), multiplied by the given integer c. Find any spanning tree with the minimum cost. Since the graph is connected, there exists at least one spanning tree.InputThe first line contains two integers n and c (2 \le n \le 20; 1 \le c \le 10^6).Then n lines follow. The i-th of them contains n integers w_{i,1}, w_{i,2}, \dots, w_{i,n} (0 \le w_{i,j} \le 10^6), where w_{i,j} denotes the weight of the edge between i and j (or w_{i,j} = 0 if there is no such edge).Additional constraints on the input: for every i \in [1, n], w_{i,i} = 0; for every pair of integers (i, j) such that i \in [1, n] and j \in [1, n], w_{i,j} = w_{j,i}; the given graph is connected. OutputPrint one integer — the minimum cost of a spanning tree of the given graph.ExamplesInput 4 100 1 8 01 0 1 08 1 0 20 0 2 0Output 21 Input 4 50 1 8 01 0 1 08 1 0 20 0 2 0Output 14 NoteIn the first example, the minimum cost spanning tree consists of edges (1, 3), (2, 3) and (3, 4). The maximum matching for it is 1.In the second example, the minimum cost spanning tree consists of edges (1, 2), (2, 3) and (3, 4). The maximum matching for it is 2.
4 100 1 8 01 0 1 08 1 0 20 0 2 0
21
6 seconds
512 megabytes
['bitmasks', 'brute force', 'dsu', 'graph matchings', 'trees', '*3100']
F. Rare Coinstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n bags numbered from 1 to n, the i-th bag contains a_i golden coins and b_i silver coins.The value of a gold coin is 1. The value of a silver coin is either 0 or 1, determined for each silver coin independently (0 with probability \frac{1}{2}, 1 with probability \frac{1}{2}).You have to answer q independent queries. Each query is the following: l r — calculate the probability that the total value of coins in bags from l to r is strictly greater than the total value in all other bags. InputThe first line contains two integers n and q (1 \le n, q \le 3 \cdot 10^5) — the number of bags and the number of queries, respectively.The second line contains n integers a_1, a_2, \dots, a_n (0 \le a_i \le 10^6) — the number of gold coins in the i-th bag.The third line contains n integers b_1, b_2, \dots, b_n (0 \le b_i \le 10^6) — the number of silver coins in the i-th bag.Next q lines contain queries. The j-th of the next q lines contains two integers l_j and r_j (1 \le l_j \le r_j \le n) — the description of the j-th query.Additional constraints on the input: the sum of the array a doesn't exceed 10^6; the sum of the array b doesn't exceed 10^6. OutputFor each query, print one integer — the probability that the total value of coins in bags from l to r is strictly greater than the total value in all other bags, taken modulo 998244353.Formally, the probability can be expressed as an irreducible fraction \frac{x}{y}. You have to print the value of x \cdot y^{-1} \bmod 998244353, where y^{-1} is an integer such that y \cdot y^{-1} \bmod 998244353 = 1.ExamplesInput 2 21 00 22 21 1Output 748683265 748683265 Input 4 32 3 4 51 0 7 33 32 31 4Output 997756929 273932289 1 NoteIn both queries from the first example, the answer is \frac{1}{4}.
2 21 00 22 21 1
748683265 748683265
2 seconds
256 megabytes
['combinatorics', 'math', 'probabilities', '*2500']
E. Clique Partitiontime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given two integers, n and k. There is a graph on n vertices, numbered from 1 to n, which initially has no edges.You have to assign each vertex an integer; let a_i be the integer on the vertex i. All a_i should be distinct integers from 1 to n.After assigning integers, for every pair of vertices (i, j), you add an edge between them if |i - j| + |a_i - a_j| \le k.Your goal is to create a graph which can be partitioned into the minimum possible (for the given values of n and k) number of cliques. Each vertex of the graph should belong to exactly one clique. Recall that a clique is a set of vertices such that every pair of vertices in it are connected with an edge.Since BledDest hasn't really brushed his programming skills up, he can't solve the problem "given a graph, partition it into the minimum number of cliques". So we also ask you to print the partition itself.InputThe first line contains one integer t (1 \le t \le 1600) — the number of test cases.Each test case consists of one line containing two integers n and k (2 \le n \le 40; 1 \le k \le 2n).OutputFor each test case, print three lines: the first line should contain n distinct integers a_1, a_2, \dots, a_n (1 \le a_i \le n) — the values you assign to the vertices; the second line should contain one integer q (1 \le q \le n) — the number of cliques you partition the graph into; the third line should contain n integers c_1, c_2, \dots, c_n (1 \le c_i \le q) — the partition of the graph into cliques. Where two vertices u and v are in the same clique if c_u = c_v. If there are multiple answers, print any of them.ExampleInput 32 35 48 16Output 2 1 1 1 1 3 1 5 2 4 2 1 1 2 1 2 1 2 3 4 5 6 7 8 1 1 1 1 1 1 1 1 1
32 35 48 16
2 1 1 1 1 3 1 5 2 4 2 1 1 2 1 2 1 2 3 4 5 6 7 8 1 1 1 1 1 1 1 1 1
3 seconds
512 megabytes
['brute force', 'constructive algorithms', 'graphs', 'greedy', 'implementation', '*2100']
D. Tandem Repeats?time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s, consisting of lowercase Latin letters and/or question marks.A tandem repeat is a string of an even length such that its first half is equal to its second half.A string a is a substring of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.Your goal is to replace each question mark with some lowercase Latin letter in such a way that the length of the longest substring that is a tandem repeat is maximum possible.InputThe first line contains a single integer t (1 \le t \le 1000) — the number of testcases.The only line of each testcase contains a string s (1 \le |s| \le 5000), consisting only of lowercase Latin letters and/or question marks.The total length of the strings over all testcases doesn't exceed 5000. OutputFor each testcase, print a single integer — the maximum length of the longest substring that is a tandem repeat after you replace each question mark in the string with some lowercase Latin letter.If it's impossible to make any tandem repeat substrings in the string, print 0.ExampleInput 4zaabaabz?????code?????scodeforcesOutput 6 4 10 0
4zaabaabz?????code?????scodeforces
6 4 10 0
2 seconds
256 megabytes
['brute force', 'strings', 'two pointers', '*1700']
C. Arrow Pathtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a grid, consisting of 2 rows and n columns. The rows are numbered from 1 to 2 from top to bottom. The columns are numbered from 1 to n from left to right. Each cell of the grid contains an arrow pointing either to the left or to the right. No arrow points outside the grid.There is a robot that starts in a cell (1, 1). Every second, the following two actions happen one after another: Firstly, the robot moves left, right, down or up (it can't try to go outside the grid, and can't skip a move); then it moves along the arrow that is placed in the current cell (the cell it ends up after its move). Your task is to determine whether the robot can reach the cell (2, n).InputThe 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 (2 \le n \le 2 \cdot 10^5).The second line contains a string consisting of exactly n characters < and/or > — the first row of the grid.The third line contains a string consisting of exactly n characters < and/or > — the second row of the grid.Additional constraints on the input: n is even; there are no arrows pointing outside the grid; the sum of n over all test cases doesn't exceed 2 \cdot 10^5. OutputFor each test case, print YES if the robot can reach the cell (2, n); otherwise, print NO.You can print each letter in any case. For example, yes, Yes, YeS will all be recognized as positive answer.ExampleInput 44>><<>>><2><><4>>><>><<6>><<><><>>><Output YES YES NO YES NoteIn the first example, one of the possible paths looks as follows: (1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (2, 4).In the second example, one of the possible paths looks as follows: (1, 1) \rightarrow (2, 1) \rightarrow (2, 2).In the third example, there is no way to reach the cell (2, 4).In the fourth example, one of the possible paths looks as follows: (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (2, 5) \rightarrow (2, 6).
44>><<>>><2><><4>>><>><<6>><<><><>>><
YES YES NO YES
2 seconds
256 megabytes
['brute force', 'constructive algorithms', 'dfs and similar', 'dp', 'graphs', 'shortest paths', '*1300']
B. Array Fixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an integer array a of length n.You can perform the following operation any number of times (possibly zero): take any element of the array a, which is at least 10, delete it, and instead insert the digits that element consisted of in the same position, in order they appear in that element.For example: if we apply this operation to the 3-rd element of the array [12, 3, 45, 67], then the array becomes [12, 3, 4, 5, 67]. if we apply this operation to the 2-nd element of the array [2, 10], then the array becomes [2, 1, 0]. Your task is to determine whether it is possible to make a sorted in non-descending order using the aforementioned operation any number of times (possibly zero). In other words, you have to determine if it is possible to transform the array a in such a way that a_1 \le a_2 \le \dots \le a_k, where k is the current length of the array a.InputThe first line contains a single integer t (1 \le t \le 10^3) — the number of test cases.Each test case consists of two lines: the first line contains a single integer n (2 \le n \le 50). the second line contains n integers a_1, a_2, \dots, a_n (0 \le a_i \le 99). OutputFor each test case, print YES if it is possible to make a sorted in non-decreasing order using the aforementioned operation; otherwise, print NO.You can print each letter in any case. For example, yes, Yes, YeS will all be recognized as a positive answer.ExampleInput 3412 3 45 67312 28 520 0Output YES NO YES NoteIn the first example, you can split the first element, then the array becomes [1, 2, 3, 45, 67].In the second example, there is no way to get a sorted array.In the third example, the array is already sorted.
3412 3 45 67312 28 520 0
YES NO YES
2 seconds
256 megabytes
['brute force', 'dp', 'greedy', 'implementation', '*1100']
A. Special Characterstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an integer n.Your task is to build a string of uppercase Latin letters. There must be exactly n special characters in this string. Let's call a character special if it is equal to exactly one of its neighbors.For example, there are 6 special characters in the AAABAACC string (at positions: 1, 3, 5, 6, 7 and 8).Print any suitable string or report that there is no such string.InputThe first line contains a single integer t (1 \le t \le 50) — the number of test cases.The only line of each test case contains a single integer n (1 \le n \le 50).OutputFor each test case, print the answer as follows: if there is no suitable string, print one line containing the string NO; otherwise, print two lines. The first line should contain the string YES; on the second line print a string of length at most 200 — the answer itself (it can be shown that if some answers exist, then there is an answer of length at most 200). If there are several solutions, print any of them. ExampleInput 3612Output YES AAABAACC NO YES MM
3612
YES AAABAACC NO YES MM
2 seconds
256 megabytes
['brute force', 'constructive algorithms', '*800']
F. Nobody is neededtime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputOleg received a permutation a of length n as a birthday present.Oleg's friend Nechipor asks Oleg q questions, each question is characterized by two numbers l and r, in response to the question Oleg must say the number of sets of indices (t_1, t_2, \ldots, t_k) of any length k \ge 1 such that: l \le t_i \le r for each i from 1 to k. t_i < t_{i+1} for each i from 1 to k-1. a_{t_{i+1}} is divisible by a_{t_i} for each i from 1 to k-1. Help Oleg and answer all of Nechipor's questions.InputEach test consists of several sets of input data. The first line contains a single integer t (1 \le t \le 10^4) — the number of sets of input data. The description of the sets of input data follows.The first line of each set of input data contains two integers n and q (1 \le n, q \le 10^6) — the length of the permutation and the number of Nechipor's questions.The second line of each set of input data contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le n) — the permutation a itself.In each of the next q lines of each set of input data, there are two integers l and r (1 \le l \le r \le n) — the next question of Nechipor.It is guaranteed that the sum of the values of n and the sum of the values of q over all test cases does not exceed 10^6.OutputFor each set of input data, output the answers to all of Nechipor's questions.ExampleInput 48 82 1 6 3 5 4 8 71 82 81 71 61 35 84 42 31 111 13 33 2 11 21 32 38 11 2 3 4 5 6 7 81 8Output 20 15 18 12 5 5 1 3 1 2 3 2 27 NoteAll suitable arrays in the first set of input data: (1), (2), (3), (4), (5), (6), (7), (8), (1, 3), (1, 6), (1, 7), (1, 6, 7), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 6, 7), (6, 7).All suitable arrays in the fourth set of input data: (1), (2), (3), (4), (5), (6), (7), (8), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 2, 4), (1, 2, 6), (1, 2, 8), (1, 2, 4, 8), (1, 3, 6), (1, 4, 8), (2, 4), (2, 6), (2, 8), (2, 4, 8), (3, 6), (4, 8).
48 82 1 6 3 5 4 8 71 82 81 71 61 35 84 42 31 111 13 33 2 11 21 32 38 11 2 3 4 5 6 7 81 8
20 15 18 12 5 5 1 3 1 2 3 2 27
6 seconds
512 megabytes
['2-sat', 'data structures', 'dfs and similar', 'dp', '*2500']
E. Girl Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSome permutation of length n is guessed.You are given the indices of its prefix maximums and suffix maximums.Recall that a permutation of length k is an array of size k such that each integer from 1 to k occurs exactly once.Prefix maximums are the elements that are the maximum on the prefix ending at that element. More formally, the element a_i is a prefix maximum if a_i > a_j for every j < i.Similarly, suffix maximums are defined, the element a_i is a suffix maximum if a_i > a_j for every j > i.You need to output the number of different permutations that could have been guessed.As this number can be very large, output the answer modulo 10^9 + 7.InputEach test consists of several test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains three integers n, m_1 and m_2 (1 \le m_1, m_2 \le n \le 2 \cdot 10^5) — the length of the permutation, the number of prefix maximums, and the number of suffix maximums, respectively.The second line of each test case contains m_1 integers p_1 < p_2 < \ldots < p_{m_1} (1 \le p_i \le n) — the indices of the prefix maximums in increasing order.The third line of each test case contains m_2 integers s_1 < s_2 < \ldots < s_{m_2} (1 \le s_i \le n) — the indices of the suffix maximums in increasing order.It is guaranteed that the sum of the values of n for all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer on a separate line — the number of suitable permutations modulo 10^9 + 7.ExampleInput 61 1 1114 2 31 22 3 43 3 11 2 335 3 41 2 32 3 4 520 5 41 2 3 4 1212 13 18 206 2 31 33 4 6Output 1 3 1 0 317580808 10 NoteThe following permutations are suitable for the second set of input data: [1, 4, 3, 2] [2, 4, 3, 1] [3, 4, 2, 1] The following permutations are suitable for the sixth set of input data: [2, 1, 6, 5, 3, 4] [3, 1, 6, 5, 2, 4] [3, 2, 6, 5, 1, 4] [4, 1, 6, 5, 2, 3] [4, 2, 6, 5, 1, 3] [4, 3, 6, 5, 1, 2] [5, 1, 6, 4, 2, 3] [5, 2, 6, 4, 1, 3] [5, 3, 6, 4, 1, 2] [5, 4, 6, 3, 1, 2]
61 1 1114 2 31 22 3 43 3 11 2 335 3 41 2 32 3 4 520 5 41 2 3 4 1212 13 18 206 2 31 33 4 6
1 3 1 0 317580808 10
2 seconds
256 megabytes
['combinatorics', 'dp', 'math', 'number theory', '*2200']
D. Birthday Gifttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYarik's birthday is coming soon, and Mark decided to give him an array a of length n.Mark knows that Yarik loves bitwise operations very much, and he also has a favorite number x, so Mark wants to find the maximum number k such that it is possible to select pairs of numbers [l_1, r_1], [l_2, r_2], \ldots [l_k, r_k], such that: l_1 = 1. r_k = n. l_i \le r_i for all i from 1 to k. r_i + 1 = l_{i + 1} for all i from 1 to k - 1. (a_{l_1} \oplus a_{l_1 + 1} \oplus \ldots \oplus a_{r_1}) | (a_{l_2} \oplus a_{l_2 + 1} \oplus \ldots \oplus a_{r_2}) | \ldots | (a_{l_k} \oplus a_{l_k + 1} \oplus \ldots \oplus a_{r_k}) \le x, where \oplus denotes the operation of bitwise XOR, and | denotes the operation of bitwise OR. If such k does not exist, then output -1.InputEach 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 following lines contain the descriptions of the test cases.The first line of each test case contains two integers n and x (1 \le n \le 10^5, 0 \le x < 2^{30}) — the length of the array a and the number x respectively.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \le a_i < 2^{30}) — the array a itself.It is guaranteed that the sum of the values of n across all test cases does not exceed 10^5.OutputFor each test case, output a single integer on a separate line — the maximum suitable number k, and -1 if such k does not exist.ExampleInput 83 11 2 32 21 12 21 32 30 03 20 0 14 21 3 3 72 22 35 00 1 2 2 1Output 2 2 1 2 3 -1 1 2 NoteIn the first test case, you can take k equal to 2 and choose two segments [1, 1] and [2, 3], (1) | (2 \oplus 3) = 1. It can be shown that 2 is the maximum possible answer.In the second test case, the segments [1, 1] and [2, 2] are suitable, (1) | (1) = 1. It is not possible to make more segments.In the third test case, it is not possible to choose 2 segments, as (1) | (3) = 3 > 2, so the optimal answer is 1.
83 11 2 32 21 12 21 32 30 03 20 0 14 21 3 3 72 22 35 00 1 2 2 1
2 2 1 2 3 -1 1 2
2 seconds
256 megabytes
['bitmasks', 'brute force', 'constructive algorithms', 'greedy', 'implementation', '*1900']
C. Tree Cuttingtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a tree with n vertices.Your task is to find the maximum number x such that it is possible to remove exactly k edges from this tree in such a way that the size of each remaining connected component^{\dagger} is at least x.^{\dagger} Two vertices v and u are in the same connected component if there exists a sequence of numbers t_1, t_2, \ldots, t_k of arbitrary length k, such that t_1 = v, t_k = u, and for each i from 1 to k - 1, vertices t_i and t_{i+1} are connected by an edge.InputEach test consists of several sets of input data. The first line contains a single integer t (1 \le t \le 10^4) — the number of sets of input data. This is followed by a description of the sets of input data.The first line of each set of input data contains two integers n and k (1 \le k < n \le 10^5) — the number of vertices in the tree and the number of edges to be removed.Each of the next n - 1 lines of each set of input data contains two integers v and u (1 \le v, u \le n) — the next edge of the tree.It is guaranteed that the sum of the values of n for all sets of input data does not exceed 10^5.OutputFor each set of input data, output a single line containing the maximum number x such that it is possible to remove exactly k edges from the tree in such a way that the size of each remaining connected component is at least x.ExampleInput 65 11 21 33 43 52 11 26 11 22 33 44 55 63 11 21 38 21 21 32 42 53 63 73 86 21 22 31 44 55 6Output 2 1 3 1 1 2 NoteThe tree in the first set of input data: After removing the edge 1 — 3, the tree will look as follows: The tree has split into two connected components. The first component consists of two vertices: 1 and 2. The second connected component consists of three vertices: 3, 4 and 5. In both connected components, there are at least two vertices. It can be shown that the answer 3 is not achievable, so the answer is 2.
65 11 21 33 43 52 11 26 11 22 33 44 55 63 11 21 38 21 21 32 42 53 63 73 86 21 22 31 44 55 6
2 1 3 1 1 2
3 seconds
512 megabytes
['binary search', 'dp', 'greedy', 'implementation', 'trees', '*1600']
B. Maximum Sumtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have an array a of n integers.You perform exactly k operations on it. In one operation, you select any contiguous subarray of the array a (possibly empty) and insert the sum of this subarray anywhere in the array.Your task is to find the maximum possible sum of the array after k such operations.As this number can be very large, output the answer modulo 10^9 + 7.Reminder: the remainder of a number x modulo p is the smallest non-negative y such that there exists an integer q and x = p \cdot q + y.InputEach test consists of several test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers n and k (1 \le n, k \le 2 \cdot 10^5) — the length of the array a and the number of operations, respectively.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (-10^9 \le a_i \le 10^9) — the array a itself.It is guaranteed that the sum of the values of n and k for all test cases does not exceed 2 \cdot 10^5.OutputFor each test, output a single integer — the maximum sum of the array that can be obtained after k operations modulo 10^9 + 7.ExampleInput 122 2-4 -73 32 2 81 775 14 -2 8 -12 97 48 14 -9 6 0 -1 37 1005 3 -8 12 -5 -9 36 1000-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -10000000002 11000000000 85 40 0 0 0 06 1048973 757292 58277 -38574 27475 9999847 1-1000 1000 -1000 1000 -1000 1000 -100010 10050408293874 -3498597 7374783 295774930 -48574034 26623784 498754833 -294875830 283045804 85938045Output 999999996 96 896 17 351 716455332 42 2 0 897909241 0 416571966 NoteIn the first test case, it is advantageous to take an empty subarray of the array twice and insert the sum of the empty subarray (zero) anywhere, then the sum of the resulting array will be (-4) + (-7) + 0 + 0 = -11, modulo 10^9 + 7 this is 999\,999\,996.In the second test case, it is advantageous to take the sum of the entire array three times and place it anywhere in the array, then one of the possible sequences of actions: [2, 2, 8] \rightarrow [2, 2, 8, 12] \rightarrow [2, 2, 8, 12, 24] \rightarrow [2, 2, 8, 12, 24, 48], the sum of the final array is 2 + 2 + 8 + 12 + 24 + 48 = 96.In the fourth test case, it is advantageous to take a subarray of the array consisting of the first three numbers (i.e. consisting of the numbers 4, -2 and 8) and insert its sum at the beginning of the array, thereby obtaining the array [10, 4, -2, 8, -12, 9], the sum of this array is 17.In the seventh test case, it will always be advantageous for us to take an empty subarray of the array. In this case, the sum of the resulting array will not differ from the sum of the original. The answer will be the sum of the original array, taken modulo — 42, because (-6 \cdot (10^9 + 7) + 42 = -6\,000\,000\,000).
122 2-4 -73 32 2 81 775 14 -2 8 -12 97 48 14 -9 6 0 -1 37 1005 3 -8 12 -5 -9 36 1000-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -10000000002 11000000000 85 40 0 0 0 06 1048973 757292 58277 -38574 27475 9999847 1-1000 1000 -1000 1000 -1000 1000 -100010 10050408293874 -3498597 7374783 295774930 -48574034 26623784 498754833 -294875830 283045804 85938045
999999996 96 896 17 351 716455332 42 2 0 897909241 0 416571966
1 second
256 megabytes
['dp', 'greedy', 'math', '*1100']
A. Median of an Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of n integers.The median of an array q_1, q_2, \ldots, q_k is the number p_{\lceil \frac{k}{2} \rceil}, where p is the array q sorted in non-decreasing order. For example, the median of the array [9, 5, 1, 2, 6] is 5, as in the sorted array [1, 2, 5, 6, 9], the number at index \lceil \frac{5}{2} \rceil = 3 is 5, and the median of the array [9, 2, 8, 3] is 3, as in the sorted array [2, 3, 8, 9], the number at index \lceil \frac{4}{2} \rceil = 2 is 3.You are allowed to choose an integer i (1 \le i \le n) and increase a_i by 1 in one operation.Your task is to find the minimum number of operations required to increase the median of the array.Note that the array a may not necessarily contain distinct numbers.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains a single integer n (1 \le n \le 10^5) — the length of the array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9) — the array a.It is guaranteed that the sum of the values of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the minimum number of operations required to increase the median of the array.ExampleInput 832 2 847 3 3 11100000000055 5 5 4 562 1 2 3 1 421 221 145 5 5 5Output 1 2 1 3 2 1 2 3 NoteIn the first test case, you can apply one operation to the first number and obtain the array [3, 2, 8], the median of this array is 3, as it is the number at index \lceil \frac{3}{2} \rceil = 2 in the non-decreasing sorted array [2, 3, 8]. The median of the original array [2, 2, 8] is 2, as it is the number at index \lceil \frac{3}{2} \rceil = 2 in the non-decreasing sorted array [2, 2, 8]. Thus, the median increased (3 > 2) in just one operation.In the fourth test case, you can apply one operation to each of the numbers at indices 1, 2, 3 and obtain the array [6, 6, 6, 4, 5], the median of this array is 6, as it is the number at index \lceil \frac{5}{2} \rceil = 3 in the non-decreasing sorted array [4, 5, 6, 6, 6]. The median of the original array [5, 5, 5, 4, 5] is 5, as it is the number at index \lceil \frac{5}{2} \rceil = 2 in the non-decreasing sorted array [4, 5, 5, 5, 5]. Thus, the median increased (6 > 5) in three operations. It can be shown that this is the minimum possible number of operations.In the fifth test case, you can apply one operation to each of the numbers at indices 1, 3 and obtain the array [3, 1, 3, 3, 1, 4], the median of this array is 3, as it is the number at index \lceil \frac{6}{2} \rceil = 3 in the non-decreasing sorted array [1, 1, 3, 3, 3, 4]. The median of the original array [2, 1, 2, 3, 1, 4] is 2, as it is the number at index \lceil \frac{6}{2} \rceil = 3 in the non-decreasing sorted array [1, 1, 2, 2, 3, 4]. Thus, the median increased (3 > 2) in two operations. It can be shown that this is the minimum possible number of operations.
832 2 847 3 3 11100000000055 5 5 4 562 1 2 3 1 421 221 145 5 5 5
1 2 1 3 2 1 2 3
1 second
256 megabytes
['greedy', 'implementation', 'sortings', '*800']
H. GCD is Greatertime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputIn the evenings during the hike, Kirill and Anton decided to take out an array of integers a of length n from their backpack and play a game with it. The rules are as follows: Kirill chooses from 2 to (n-2) numbers and encircles them in red. Anton encircles all the remaining numbers in blue. Kirill calculates the greatest common divisor (GCD) of all the red numbers. Anton calculates the bitwise AND of all the blue numbers and adds the number x to the result. If the GCD of all the red numbers is strictly greater than the sum of the bitwise AND of all the blue numbers and the number x, then Kirill wins; otherwise, Anton wins.Help Kirill to beat Anton or tell if it's impossible.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 20\,000) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers n and x (4\le n \le 4\cdot 10^5, 0 \le x \le 4\cdot 10^5) — the number of integers and the number x respectively.The second line contains an array a of length n (1 \le a_i \le 4\cdot 10^5).It is guaranteed that the sum of n for all test cases does not exceed 4\cdot 10^5. It is also guaranteed that the sum of the maximum values of a_i for each test case does not exceed 4\cdot 10^5.OutputFor each test case, output "YES" on the first line if the condition can be met, on the second line, output the number of chosen numbers by Kirill and the numbers themselves in any order separated by a space, and on the third line, output the size of the second set and the numbers in it.Otherwise, output "NO".You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.ExampleInput 84 14 3 1 84 14 5 8 45 01 1 1 1 15 231 63 127 63 314 11 3 3 38 34 3 4 1 2 2 5 34 21 4 3 68 4831 61 37 15 53 26 61 12Output YES 2 4 8 2 3 1 YES 2 4 4 2 5 8 NO YES 2 63 63 3 31 127 31 YES 2 3 3 2 1 3 YES 2 4 4 6 3 1 2 2 5 3 YES 2 3 6 2 1 4 YES 2 61 61 6 31 37 15 53 26 12
84 14 3 1 84 14 5 8 45 01 1 1 1 15 231 63 127 63 314 11 3 3 38 34 3 4 1 2 2 5 34 21 4 3 68 4831 61 37 15 53 26 61 12
YES 2 4 8 2 3 1 YES 2 4 4 2 5 8 NO YES 2 63 63 3 31 127 31 YES 2 3 3 2 1 3 YES 2 4 4 6 3 1 2 2 5 3 YES 2 3 6 2 1 4 YES 2 61 61 6 31 37 15 53 26 12
3 seconds
512 megabytes
['brute force', 'data structures', 'math', 'number theory', '*2600']
G. Cook and Porridgetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFinally, lunchtime!n schoolchildren have lined up in a long queue at the cook's tent for porridge. The cook will be serving porridge for D minutes. The schoolchild standing in the i-th position in the queue has a priority of k_i and eats one portion of porridge in s_i minutes.At the beginning of each minute of the break, the cook serves the first schoolchild in the queue one portion of porridge, after which the schoolchild goes to eat their portion. If the i-th schoolchild is served a portion at the beginning of the x-th minute, then they will return to the queue at the end of the (x + s_i)-th minute.When the i-th schoolchild returns to the queue, the schoolchildren at the end of the queue whose priority is strictly lower than that of the i-th schoolchild must let them pass. Thus, they will stand in the queue behind the last schoolchild whose priority is not lower than their own. That is, behind the last schoolchild j with k_j \ge k_i. If there is no such schoolchild in the queue, the i-th schoolchild will stand at the front of the queue.If several schoolchildren return at the same time, they will return to the queue in ascending order of their s_i.For example, if n = 3, D = 3, k = [2, 3, 2], and s = [2, 1, 3], the serving will occur as follows: At the beginning of minute 1, the students in the queue are [1, 2, 3], and student 1 is served porridge; at the beginning of minute 2, the students in the queue are [2, 3], and student 2 is served porridge; at the beginning of minute 3, the student in the queue is [3], and student 3 is served porridge; at the end of minute 3, student 2 returns to the queue, and the queue becomes [2]; at the end of minute 3, student 1 returns to the queue, and the queue becomes [2, 1], as his priority is lower. Determine the minimum number of minutes after the start of the break that each schoolchild will receive porridge at least once, or report that this will not happen within D minutes.InputEach test consists of several test cases. The first line contains a single integer t (1 \le t \le 1000) — the number of test cases. This is followed by a description of the test cases.The first line of each test case contains two integers n and D (1 \le n \le 2 \cdot 10^5, 1 \le D \le 3\cdot 10^5) — the number of schoolchildren in the queue and the break time, respectively.The next n lines contain two integers k_i and s_i (1 \le k_i, s_i, \le 10^9) — the priority and the time to eat one portion of porridge for the respective schoolchild. The schoolchildren are given in the order they stand in the queue (from the front to the end).It is guaranteed that the sum of the values of n for all input data sets does not exceed 2\cdot 10^5. Similarly, it is guaranteed that the sum of the values of D for all input data sets does not exceed 3\cdot 10^5.OutputFor each test case, output the minimum number of minutes after which each schoolchild will receive porridge at least once. If this does not happen within the break time, output -1.ExampleInput 73 32 23 12 35 1010 37 111 35 16 15 204 27 28 51 53 15 171 38 28 32 21 15 148 24 21 38 36 41 114 55 148 24 21 38 36 4Output 3 -1 12 6 6 1 6
73 32 23 12 35 1010 37 111 35 16 15 204 27 28 51 53 15 171 38 28 32 21 15 148 24 21 38 36 41 114 55 148 24 21 38 36 4
3 -1 12 6 6 1 6
2 seconds
256 megabytes
['binary search', 'constructive algorithms', 'data structures', 'implementation', '*2500']
F. Kirill and Mushroomstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAs soon as everyone in the camp fell asleep, Kirill sneaked out of the tent and went to the Wise Oak to gather mushrooms.It is known that there are n mushrooms growing under the Oak, each of which has magic power v_i. Kirill really wants to make a magical elixir of maximum strength from the mushrooms.The strength of the elixir is equal to the product of the number of mushrooms in it and the minimum magic power among these mushrooms. To prepare the elixir, Kirill will sequentially pick one mushroom growing under the Oak. Kirill can gather mushrooms in any order.However, it's not that simple. The Wise Oak informed Kirill of a permutation of numbers p from 1 to n. If Kirill picks only k mushrooms, then the magic power of all mushrooms with indices p_1, p_2, \dots, p_{k - 1} will become 0. Kirill will not use mushrooms with zero magic power to prepare the elixir.Your task is to help Kirill gather mushrooms in such a way that he can brew the elixir of maximum possible strength. However, Kirill is a little scared to stay near the oak for too long, so out of all the suitable options for gathering mushrooms, he asks you to find the one with the minimum number of mushrooms.A permutation of length n is an array consisting of n different integers from 1 to n in any order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears in the array twice) and [1,3,4] is also not a permutation (n=3, but 4 appears in the array).InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 200\,000) — the number of mushrooms.The second line contains an array v of size n (1\le v_i \le 10^9) — the magic powers of the mushrooms.The third line contains a permutation p of numbers from 1 to n.It is guaranteed that the sum of the values of n over all test cases does not exceed 2\cdot 10^5.OutputFor each test case, output two integers separated by a space — the maximum strength of the elixir that can be brewed and the minimum number of mushrooms that Kirill needs to use for this.ExampleInput 639 8 143 2 151 2 3 4 51 2 3 4 561 2 3 4 5 66 5 4 3 2 151 4 6 10 102 1 4 5 342 2 5 54 2 3 151 2 9 10 101 4 2 3 5Output 16 2 9 3 8 2 20 2 5 1 20 2 NoteIn the first example, you need to take the mushrooms with indices 1 and 2, so the strength of the elixir is equal to 2 \cdot \min(a_1, a_2) = 2 \cdot \min(9, 8) = 2 \cdot 8 = 16. Note that the magic power of the mushroom with index 3 after picking two mushrooms will become 0.
639 8 143 2 151 2 3 4 51 2 3 4 561 2 3 4 5 66 5 4 3 2 151 4 6 10 102 1 4 5 342 2 5 54 2 3 151 2 9 10 101 4 2 3 5
16 2 9 3 8 2 20 2 5 1 20 2
2 seconds
256 megabytes
['data structures', 'sortings', '*1900']
E. Binary Searchtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAnton got bored during the hike and wanted to solve something. He asked Kirill if he had any new problems, and of course, Kirill had one.You are given a permutation p of size n, and a number x that needs to be found. 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).You decided that you are a cool programmer, so you will use an advanced algorithm for the search — binary search. However, you forgot that for binary search, the array must be sorted.You did not give up and decided to apply this algorithm anyway, and in order to get the correct answer, you can perform the following operation no more than 2 times before running the algorithm: choose the indices i, j (1\le i, j \le n) and swap the elements at positions i and j.After that, the binary search is performed. At the beginning of the algorithm, two variables l = 1 and r = n + 1 are declared. Then the following loop is executed: If r - l = 1, end the loop m = \lfloor \frac{r + l}{2} \rfloor If p_m \le x, assign l = m, otherwise r = m. The goal is to rearrange the numbers in the permutation before the algorithm so that after the algorithm is executed, p_l is equal to x. It can be shown that 2 operations are always sufficient.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 2\cdot 10^4) — the number of test cases. Then follow the descriptions of the test cases.The first line of each test case contains two integers n and x (1 \le x \le n \le 2\cdot 10^5) — the length of the permutation and the number to be found.The second line contains the permutation p separated by spaces (1 \le p_i \le n).It is guaranteed that the sum of the values of n for all test cases does not exceed 2\cdot 10^5.OutputFor each test case, output an integer k (0 \le k \le 2) on the first line — the number of operations performed by you. In the next k lines, output 2 integers i, j (1 \le i, j \le n) separated by a space, indicating that you are swapping the elements at positions i and j.Note that you do not need to minimize the number of operations.ExampleInput 56 31 2 3 4 5 66 53 1 6 5 2 45 13 5 4 2 16 34 3 1 5 2 63 23 2 1Output 0 1 3 4 2 2 4 1 5 2 4 5 2 4 1 1 3
56 31 2 3 4 5 66 53 1 6 5 2 45 13 5 4 2 16 34 3 1 5 2 63 23 2 1
0 1 3 4 2 2 4 1 5 2 4 5 2 4 1 1 3
2 seconds
256 megabytes
['binary search', 'constructive algorithms', 'greedy', '*1700']
D. Seraphim the Owltime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe guys lined up in a queue of n people, starting with person number i = 1, to ask Serafim the Owl about the meaning of life. Unfortunately, Kirill was very busy writing the legend for this problem, so he arrived a little later and stood at the end of the line after the n-th person. Kirill is completely dissatisfied with this situation, so he decided to bribe some people ahead of him.For the i-th person in the queue, Kirill knows two values: a_i and b_i. If at the moment Kirill is standing at position i, then he can choose any position j such that j < i and exchange places with the person at position j. In this case, Kirill will have to pay him a_j coins. And for each k such that j < k < i, Kirill will have to pay b_k coins to the person at position k. Kirill can perform this action any number of times.Kirill is thrifty, so he wants to spend as few coins as possible, but he doesn't want to wait too long, so Kirill believes he should be among the first m people in line.Help Kirill determine the minimum number of coins he will have to spend in order to not wait too long.InputEach test consists of several sets of input data. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then follows the description of the test case.The first line of each test case contains two integers n and m (1 \le m \le n \le 200\,000) — the number of people in the queue besides Kirill and the maximum allowable final position of Kirill, respectively.The second line contains n integers a_1, a_2, \dots, a_n separated by spaces (1 \le a_i \le 10^9).The third line contains n integers b_1, b_2, \dots, b_n separated by spaces (1 \le b_i \le 10^9).It is guaranteed that the sum of the values of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the minimum number of coins Kirill needs to spend.ExampleInput 44 27 3 6 94 3 8 56 26 9 7 1 8 35 8 8 1 4 17 77 2 9 2 6 5 99 1 10 7 1 4 92 12 31 1Output 14 22 9 3
44 27 3 6 94 3 8 56 26 9 7 1 8 35 8 8 1 4 17 77 2 9 2 6 5 99 1 10 7 1 4 92 12 31 1
14 22 9 3
2 seconds
256 megabytes
['dp', 'greedy', '*1300']
C. Left and Right Housestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn the village of Letovo, there are n houses. The villagers decided to build a big road that will divide the village into left and right sides. Each resident wants to live on either the right or the left side of the street, which is described as a sequence a_1, a_2, \dots, a_n, where a_j = 0 if the resident of the j-th house wants to live on the left side of the street; otherwise, a_j = 1.The road will pass between two houses. The houses to the left of it will be declared the left-side, and the houses to the right will be declared the right-side. More formally, let the road pass between houses i and i+1. Then the houses at positions between 1 and i will be on the left side of the street, and at positions between i+1 and n will be on the right side. The road also may pass before the first and after the last house; in this case, the entire village is declared to be either the right or left side, respectively.To make the design fair, it was decided to lay the road so that at least half of the residents on each side of the village are satisfied with the choice. That is, among x residents on one side, at least \lceil\frac{x}{2}\rceil should want to live on that side, where \lceil x \rceil denotes rounding up a real number x. To the left of the road, there will be i houses, among the corresponding a_j there must be at least \lceil\frac{i}{2}\rceil zeros. To the right of the road, there will be n-i houses, among the corresponding a_j there must be at least \lceil\frac{n-i}{2}\rceil ones. Determine after which house i the road should be laid in order to satisfy the described condition and be as close to the middle of the village as possible. Formally, among all suitable positions i, minimize \left|\frac{n}{2} - i\right|.If there are multiple suitable positions i with the minimum \left|\frac{n}{2} - i\right|, output the smaller one.InputEach 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 a single integer n (3 \le n \le 3\cdot 10^5). The next line of each test case contains a string a of length n, consisting only of 0 and 1.It is guaranteed that the sum of n over all test cases does not exceed 3\cdot 10^5.OutputFor each test case, output a single number i — the position of the house after which the road should be laid (if it should be laid before the first house, output 0). We can show that the answer always exists.ExampleInput 731016010111601100130003110300141100Output 2 3 2 3 0 1 0 NoteLet's consider the first example of input data.If we lay the road after the first house, there will be one house a_1 = 1 on the left side of the street, the resident of which would like to live on the right side of the street. Then 0 out of 1 residents on the even side will be satisfied with the choice, which means that the road cannot be laid after house 1.If we lay the road after the second house, 1 out of 2 residents on the left side (with preferences a_1 = 1, a_2 = 0) and 1 out of 1 resident on the right side (with preference a_3 = 1) will be satisfied with the choice. More than half of the residents on each side are satisfied with the choice, which means that the road can be laid after house 2. We can show that this is the optimal answer.
731016010111601100130003110300141100
2 3 2 3 0 1 0
2 seconds
256 megabytes
['brute force', '*1200']
B. Fireworkstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOne of the days of the hike coincided with a holiday, so in the evening at the camp, it was decided to arrange a festive fireworks display. For this purpose, the organizers of the hike bought two installations for launching fireworks and a huge number of shells for launching.Both installations are turned on simultaneously. The first installation launches fireworks every a minutes (i.e., after a, 2 \cdot a, 3 \cdot a, \dots minutes after launch). The second installation launches fireworks every b minutes (i.e., after b, 2 \cdot b, 3 \cdot b, \dots minutes after launch).Each firework is visible in the sky for m + 1 minutes after launch, i.e., if a firework was launched after x minutes after the installations were turned on, it will be visible every minute from x to x + m, inclusive. If one firework was launched m minutes after another, both fireworks will be visible for one minute.What is the maximum number of fireworks that could be seen in the sky at the same time?InputEach test consists of several test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then follow the descriptions of the test cases.The first and only line of each test case contains integers a, b, m (1 \le a, b, m \le 10^{18}) — the frequency of launching for the first installation, the second installation, and the time the firework is visible in the sky.OutputFor each set of input data, output a single number — the maximum number of fireworks that can be seen simultaneously.ExampleInput 66 7 43 4 107 8 565 6 781234598961 1 11 1 1000000000000000000Output 2 7 17 28645268630 4 2000000000000000002 NoteIn the first set of input data, the fireworks are visible in the sky for 5 minutes. Since the first installation launches fireworks every 6 minutes, and the second one every 7 minutes, two fireworks launched from the same installation will not be visible in the sky at the same time. At the same time, after 7 minutes from the start of the holiday, one firework from the first and one from the second camp will be visible. Thus, it is possible to see no more than 2 fireworks simultaneously.In the third set of input data, 17 fireworks will be visible after 112 minutes: 9 fireworks launched from the first installation at times [56, 63, 70, 77, 84, 91, 98, 105, 112]; 8 fireworks launched from the second installation at times [56, 64, 72, 80, 88, 96, 104, 112].
66 7 43 4 107 8 565 6 781234598961 1 11 1 1000000000000000000
2 7 17 28645268630 4 2000000000000000002
1 second
256 megabytes
['math', 'number theory', '*900']
A. Setting up Camptime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe organizing committee plans to take the participants of the Olympiad on a hike after the tour. Currently, the number of tents needed to be taken is being calculated. It is known that each tent can accommodate up to 3 people.Among the participants, there are a introverts, b extroverts, and c universals: Each introvert wants to live in a tent alone. Thus, a tent with an introvert must contain exactly one person — only the introvert himself. Each extrovert wants to live in a tent with two others. Thus, the tent with an extrovert must contain exactly three people. Each universal is fine with any option (living alone, with one other person, or with two others). The organizing committee respects the wishes of each participant very much, so they want to fulfill all of them.Tell us the minimum number of tents needed to be taken so that all participants can be accommodated according to their preferences. If it is impossible to accommodate the participants in a way that fulfills all the wishes, output -1.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. This is followed by the descriptions of the test cases.Each test case is described by a single line containing three integers a, b, c (0 \le a, b, c \le 10^9) — the number of introverts, extroverts, and universals, respectively.OutputFor each test case, output a single integer — the minimum number of tents, or -1 if it is impossible to accommodate the participants.ExampleInput 101 2 31 4 11 4 21 1 11 3 219 7 180 0 07 0 00 24 01000000000 1000000000 1000000000Output 3 -1 3 -1 3 28 0 7 8 1666666667 NoteIn the first test case, 1 tent will be given to the introverts, 1 tent will be shared by two extroverts and one universal, and the last tent will be shared by two universals. In total, 3 tents are needed.In the second test case, three extroverts will take 1 tent, and 1 tent will be taken by an introvert. Then, one extrovert and one universal will be left. This extrovert will not be able to live with two others.
101 2 31 4 11 4 21 1 11 3 219 7 180 0 07 0 00 24 01000000000 1000000000 1000000000
3 -1 3 -1 3 28 0 7 8 1666666667
1 second
256 megabytes
['greedy', 'math', '*800']
B. Equal XORtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of length 2n, consisting of each integer from 1 to n exactly twice.You are also given an integer k (1 \leq k \leq \lfloor \frac{n}{2} \rfloor ).You need to find two arrays l and r each of length \mathbf{2k} such that: l is a subset^\dagger of [a_1, a_2, \ldots a_n] r is a subset of [a_{n+1}, a_{n+2}, \ldots a_{2n}] bitwise XOR of elements of l is equal to the bitwise XOR of elements of r; in other words, l_1 \oplus l_2 \oplus \ldots \oplus l_{2k} = r_1 \oplus r_2 \oplus \ldots \oplus r_{2k} It can be proved that at least one pair of l and r always exists. If there are multiple solutions, you may output any one of them.^\dagger A sequence x is a subset of a sequence y if x can be obtained by deleting several (possibly none or all) elements of y and rearranging the elements in any order. For example, [3,1,2,1], [1, 2, 3], [1, 1] and [3, 2] are subsets of [1, 1, 2, 3] but [4] and [2, 2] are not subsets of [1, 1, 2, 3].InputEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 5000) — the number of test cases. The description of the test cases follows.The first line of each test case contains 2 integers n and k (2 \le n \le 5 \cdot 10^4, 1 \leq k \leq \lfloor \frac{n}{2} \rfloor ).The second line contains 2n integers a_1, a_2, \ldots, a_{2n} (1 \le a_i \le n). It is guaranteed that every integer from 1 to n occurs exactly twice in a.It is guaranteed that the sum of n over all test cases does not exceed 5 \cdot 10^4.OutputFor each test case, output two lines. On the first line of output, output 2k integers l_1, l_2, \ldots, l_{2k}.On the second line of output, output 2k integers r_1, r_2, \ldots r_{2k}.If there are multiple solutions, you may output any one of them.ExampleInput 42 11 2 2 16 16 4 2 1 2 3 1 6 3 5 5 44 11 2 3 4 1 2 3 46 25 1 3 3 5 1 2 6 4 6 4 2Output 2 1 2 1 6 4 1 3 1 2 1 2 5 1 3 3 6 4 2 4 NoteIn the first test case, we choose l=[2,1] and r=[2,1]. [2, 1] is a subset of [a_1, a_2] and [2, 1] is a subset of [a_3, a_4], and 2 \oplus 1 = 2 \oplus 1 = 3.In the second test case, 6 \oplus 4 = 1 \oplus 3 = 2.
42 11 2 2 16 16 4 2 1 2 3 1 6 3 5 5 44 11 2 3 4 1 2 3 46 25 1 3 3 5 1 2 6 4 6 4 2
2 1 2 1 6 4 1 3 1 2 1 2 5 1 3 3 6 4 2 4
1 second
256 megabytes
['bitmasks', 'constructive algorithms', '*1100']
A. Destroying Bridgestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n islands, numbered 1, 2, \ldots, n. Initially, every pair of islands is connected by a bridge. Hence, there are a total of \frac{n (n - 1)}{2} bridges. Everule lives on island 1 and enjoys visiting the other islands using bridges. Dominater has the power to destroy at most k bridges to minimize the number of islands that Everule can reach using (possibly multiple) bridges.Find the minimum number of islands (including island 1) that Everule can visit if Dominater destroys bridges optimally.InputEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^3) — the number of test cases. The description of the test cases follows.The first and only line of each test case contains two integers n and k (1 \le n \le 100, 0 \le k \le \frac{n \cdot (n - 1)}{2}).OutputFor each test case, output the minimum number of islands that Everule can visit if Dominater destroys bridges optimally.ExampleInput 62 02 14 15 105 34 4Output 2 1 4 1 5 1 NoteIn the first test case, since no bridges can be destroyed, all the islands will be reachable.In the second test case, you can destroy the bridge between islands 1 and 2. Everule will not be able to visit island 2 but can still visit island 1. Therefore, the total number of islands that Everule can visit is 1.In the third test case, Everule always has a way of reaching all islands despite what Dominater does. For example, if Dominater destroyed the bridge between islands 1 and 2, Everule can still visit island 2 by traveling by 1 \to 3 \to 2 as the bridges between 1 and 3, and between 3 and 2 are not destroyed.In the fourth test case, you can destroy all bridges since k = \frac{n \cdot (n - 1)}{2}. Everule will be only able to visit 1 island (island 1).
62 02 14 15 105 34 4
2 1 4 1 5 1
1 second
256 megabytes
['graphs', 'greedy', 'math', '*800']
F. Minimum Hamming Distancetime limit per test4 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputYou are given a binary string^\dagger s of length n.A binary string p of the same length n is called good if for every i (1 \leq i \leq n), there exist indices l and r such that: 1 \leq l \leq i \leq r \leq n s_i is a mode^\ddagger of the string p_lp_{l+1}\ldots p_r You are given another binary string t of length n. Find the minimum Hamming distance^\S between t and any good string g.^\dagger A binary string is a string that only consists of characters \mathtt{0} and \mathtt{1}.^\ddagger Character c is a mode of string p of length m if the number of occurrences of c in p is at least \lceil \frac{m}{2} \rceil. For example, \mathtt{0} is a mode of \mathtt{010}, \mathtt{1} is not a mode of \mathtt{010}, and both \mathtt{0} and \mathtt{1} are modes of \mathtt{011010}.^\S The Hamming distance of strings a and b of length m is the number of indices i such that 1 \leq i \leq m and a_i \neq b_i.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5). The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 10^4) — the length of the binary string s.The second line of each test case contains a binary string s of length n consisting of characters 0 and 1.The third line of each test case contains a binary string t of length n consisting of characters 0 and 1.It is guaranteed that the sum of n over all test cases does not exceed 10^6, with the additional assurance that the sum of n^2 over all test cases does not exceed 10^8OutputFor each test case, print the minimum Hamming distance between t and any good string g.ExampleInput 330000004000011116111111000100Output 0 2 1 NoteIn the first test case, g=\mathtt{000} is a good string which has Hamming distance 0 from t.In the second test case, g=\mathtt{0011} is a good string which has Hamming distance 2 from t. It can be proven that there are no good strings with Hamming distance less than 2 from t.In the third test case, g=\mathtt{001100} is a good string which has Hamming distance 1 from t.
330000004000011116111111000100
0 2 1
4 seconds
1024 megabytes
['dp', '*3500']
README.md exists but content is empty. Use the Edit dataset card button to edit it.
Downloads last month
22
Edit dataset card