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
D. Game of Stackstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have n stacks r_1,r_2,\ldots,r_n. Each stack contains some positive integers ranging from 1 to n.Define the following functions:function init(pos): stacks := an array that contains n stacks r[1], r[2], ..., r[n] return get(stacks, pos)function get(stacks, pos): if stacks[pos] is empty: return pos else: new_pos := the top element of stacks[pos] pop the top element of stacks[pos] return get(stacks, new_pos)You want to know the values returned by \texttt{init(1)}, \texttt{init(2)}, \ldots, \texttt{init(n)}.Note that, during these calls, the stacks r_1,r_2,\ldots,r_n don't change, so the calls \texttt{init(1)}, \texttt{init(2)}, \ldots, \texttt{init(n)} are independent.InputThe first line of the input contains one integer n (1\le n\le 10^5) — the length of the array r.Each of the following n lines contains several integers. The first integer k_i (0\le k_i\le 10^5) represents the number of elements in the i-th stack, and the following k_i positive integers c_{i,1},c_{i,2},\ldots,c_{i,k_i} (1\le c_{i,j}\le n) represent the elements in the i-th stack. c_{i,1} is the bottom element.In each test, \sum k_i\le 10^6.OutputYou need to output n values, the i-th of which is the value returned by \texttt{init(i)}.ExamplesInput 3 3 1 2 2 3 3 1 2 3 1 2 1 Output 1 2 2 Input 5 5 1 2 4 3 4 6 1 2 5 3 3 4 6 1 1 4 4 4 2 9 3 1 4 2 3 5 5 1 2 4 4 4 1 3 Output 1 1 1 1 1 NoteIn the first example: When you call \texttt{init(1)}, set \texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]}, and then call \texttt{get(stacks, 1)}. \texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[1]}, which makes \texttt{stacks} become [[1,2],[3,1,2],[1,2,1]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1,2],[3,1],[1,2,1]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 1}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1,2],[3],[1,2,1]], and then call \texttt{get(stacks, 1)}. \texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[1]}, which makes \texttt{stacks} become [[1],[3],[1,2,1]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 3}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1],[],[1,2,1]], and then call \texttt{get(stacks, 3)}. \texttt{stacks[3]} is not empty, set \texttt{new_pos := 1}, and pop the top element of \texttt{stacks[3]}, which makes \texttt{stacks} become [[1],[],[1,2]], and then call \texttt{get(stacks, 1)}. \texttt{stacks[1]} is not empty, set \texttt{new_pos := 1}, and pop the top element of \texttt{stacks[1]}, which makes \texttt{stacks} become [[],[],[1,2]], and then call \texttt{get(stacks, 1)}. \texttt{stacks[1]} is empty, return 1. When you call \texttt{init(2)}, set \texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]}, and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1,2,2],[3,1],[1,2,1]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 1}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1,2,2],[3],[1,2,1]], and then call \texttt{get(stacks, 1)}. \texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[1]}, which makes \texttt{stacks} become [[1,2],[3],[1,2,1]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 3}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1,2],[],[1,2,1]], and then call \texttt{get(stacks, 3)}. \texttt{stacks[3]} is not empty, set \texttt{new_pos := 1}, and pop the top element of \texttt{stacks[3]}, which makes \texttt{stacks} become [[1,2],[],[1,2]], and then call \texttt{get(stacks, 1)}. \texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[1]}, which makes \texttt{stacks} become [[1],[],[1,2]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is empty, return 2. When you call \texttt{init(3)}, set \texttt{stacks := [[1,2,2],[3,1,2],[1,2,1]]}, and then call \texttt{get(stacks, 3)}. \texttt{stacks[3]} is not empty, set \texttt{new_pos := 1}, and pop the top element of \texttt{stacks[3]}, which makes \texttt{stacks} become [[1,2,2],[3,1,2],[1,2]], and then call \texttt{get(stacks, 1)}. \texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[1]}, which makes \texttt{stacks} become [[1,2],[3,1,2],[1,2]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1,2],[3,1],[1,2]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 1}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1,2],[3],[1,2]], and then call \texttt{get(stacks, 1)}. \texttt{stacks[1]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[1]}, which makes \texttt{stacks} become [[1],[3],[1,2]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is not empty, set \texttt{new_pos := 3}, and pop the top element of \texttt{stacks[2]}, which makes \texttt{stacks} become [[1],[],[1,2]], and then call \texttt{get(stacks, 3)}. \texttt{stacks[3]} is not empty, set \texttt{new_pos := 2}, and pop the top element of \texttt{stacks[3]}, which makes \texttt{stacks} become [[1],[],[1]], and then call \texttt{get(stacks, 2)}. \texttt{stacks[2]} is empty, return 2.
3 3 1 2 2 3 3 1 2 3 1 2 1
1 2 2
1 second
256 megabytes
['brute force', 'dfs and similar', 'graphs', 'implementation', 'trees', '*3000']
C2. Doremy's Drying Plan (Hard Version)time limit per test4 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputThe only differences between the two versions of this problem are the constraint on k, the time limit and the memory limit. You can make hacks only if all versions of the problem are solved.Doremy lives in a rainy country consisting of n cities numbered from 1 to n.The weather broadcast predicted the distribution of rain in the next m days. In the i-th day, it will rain in the cities in the interval [l_i, r_i]. A city is called dry if it will never rain in that city in the next m days.It turns out that Doremy has a special power. She can choose k days, and during these days it will not rain. Doremy wants to calculate the maximum number of dry cities after using the special power.InputThe input consists of multiple test cases. The first line contains a single integer t (1\le t\le 10^4) — the number of test cases. The description of the test cases follows.The first line contains three integers n, m and k (1\le n\le 2\cdot 10^5, 2 \le m \le 2\cdot 10^5, 2 \le k \le \min(10, m)) — the number of cities, the number of days, and the number of days of rain that Doremy can prevent.Then, m lines follow. The i-th line contains two integers l_i, r_i (1\le l_i\le r_i\le n) — the rain coverage on day i.It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 2\cdot 10^5.OutputFor each test case, output one integer — the maximum number of dry cities.ExampleInput 62 3 21 21 21 15 3 21 32 43 510 6 41 56 102 23 75 81 4100 6 51 1001 1001 1001 1001 1001 1001000 2 21 11 120 5 39 203 310 1111 136 18Output 1 2 6 0 1000 17 NoteIn the first test case, if Doremy prevents rain 1,2, then city 2 will be dry; rain 2,3, then no city will be dry; rain 1,3, then no city will be dry; So there is at most 1 dry city.In the second test case, if Doremy prevents rain 1,2, then city 1,2 will be dry; rain 2,3, then city 4,5 will be dry; rain 1,3, then city 1,5 will be dry. So there are at most 2 dry cities.In the third test case, it is optimal to prevent rain 1,2,4,5.In the forth test case, there is always a day of rain that wets all the cities and cannot be prevented.
62 3 21 21 21 15 3 21 32 43 510 6 41 56 102 23 75 81 4100 6 51 1001 1001 1001 1001 1001 1001000 2 21 11 120 5 39 203 310 1111 136 18
1 2 6 0 1000 17
4 seconds
1024 megabytes
['data structures', 'dp', '*2600']
C1. Doremy's Drying Plan (Easy Version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe only differences between the two versions of this problem are the constraint on k, the time limit and the memory limit. You can make hacks only if all versions of the problem are solved.Doremy lives in a rainy country consisting of n cities numbered from 1 to n.The weather broadcast predicted the distribution of rain in the next m days. In the i-th day, it will rain in the cities in the interval [l_i, r_i]. A city is called dry if it will never rain in that city in the next m days.It turns out that Doremy has a special power. She can choose k days (in the easy version, k = 2), and during these days it will not rain. Doremy wants to calculate the maximum number of dry cities after using the special power.InputThe input consists of multiple test cases. The first line contains a single integer t (1\le t\le 10^4) — the number of test cases. The description of the test cases follows.The first line contains three integers n, m and k (1\le n\le 2\cdot 10^5, 2 \le m \le 2\cdot 10^5, k = 2) — the number of cities, the number of days, and the number of days of rain that Doremy can prevent.Then, m lines follow. The i-th line contains two integers l_i, r_i (1\le l_i\le r_i\le n) — the rain coverage on day i.It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 2\cdot 10^5.OutputFor each test case, output one integer — the maximum number of dry cities.ExampleInput 62 3 21 21 21 15 3 21 32 43 510 6 21 56 102 23 75 81 4100 6 21 1001 1001 1001 1001 1001 1001000 2 21 11 120 5 29 203 310 1111 136 18Output 1 2 3 0 1000 15 NoteIn the first test case, if Doremy prevents rain 1,2, then city 2 will be dry; rain 2,3, then no city will be dry; rain 1,3, then no city will be dry; So there is at most 1 dry city.In the second test case, if Doremy prevents rain 1,2, then city 1,2 will be dry; rain 2,3, then city 4,5 will be dry; rain 1,3, then city 1,5 will be dry. So there are at most 2 dry cities.In the third test case, it is optimal to prevent rain 2,5.In the forth test case, there is always 4 days of rain that wets all the cities and cannot be prevented.
62 3 21 21 21 15 3 21 32 43 510 6 21 56 102 23 75 81 4100 6 21 1001 1001 1001 1001 1001 1001000 2 21 11 120 5 29 203 310 1111 136 18
1 2 3 0 1000 15
1 second
256 megabytes
['brute force', 'data structures', 'dp', 'greedy', 'sortings', '*2000']
B. Doremy's Connecting Plantime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputDoremy lives in a country consisting of n cities numbered from 1 to n, with a_i people living in the i-th city. It can be modeled as an undirected graph with n nodes.Initially, there are no edges in the graph. Now Doremy wants to make the graph connected. To do this, she can add an edge between i and j if \sum_{k \in S} a_k \ge i\cdot j \cdot c, where S is the set of all the nodes that are currently in the same connected component of either i or j, and c is a given constant.Can Doremy make the graph connected?Two nodes (i, j) are in the same connected component if there exists a path from i to j. A graph is connected if all its nodes are in the same connected component.InputThe input consists of multiple test cases. The first line contains a single integer t (1\le t\le 10^4) — the number of test cases. The description of the test cases follows.The first line contains two integers n, c (2\le n\le 2\cdot 10^5, 1 \le c \le 10^6) — the number of nodes and the constant.The second line of each test case contains n integers a_1,a_2,\ldots,a_n (0 \le a_i \le 10^{12}) — the number of people living in the i-th city.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot 10^5.OutputFor each test case, print "YES" (without quotes), if it is possible to make the graph connected, and "NO" (without quotes) otherwise.You can print letters in any case (upper or lower).ExampleInput 74 100 20 15 102 11 15 10 1 0 4 1995 21 1 3 1 15 55 6 1 10 25 10000001000000000000 1000000000000 1000000000000 1000000000000 10000000000003 10 0 2Output Yes Yes Yes No No Yes No NoteIn the first test case, Doremy can add edges in the following order: Add (1,2). This operation is valid because a_1 + a_2 = 20 \ge i\cdot j \cdot c = 20. Add (1,3). This operation is valid because a_1 + a_2 + a_3 = 35 \ge i \cdot j \cdot c = 30. Add (1,4). This operation is valid because a_1 + a_2 + a_3 + a_4 = 45 \ge i \cdot j \cdot c = 40. In the second test case, Doremy can add edge (1,2) because a_1 + a_2 =2 \ge 1 \cdot 2 \cdot 1. After that, the graph is connected.In the third test case, Doremy can add edges in the order (5,4), (5,3), (5,2) and (5,1).In the fourth test case, Doremy cannot add any edge at all.
74 100 20 15 102 11 15 10 1 0 4 1995 21 1 3 1 15 55 6 1 10 25 10000001000000000000 1000000000000 1000000000000 1000000000000 10000000000003 10 0 2
Yes Yes Yes No No Yes No
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', 'sortings', '*1700']
A. Qingshan Loves Strings 2time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputQingshan has a string s which only contains \texttt{0} and \texttt{1}.A string a of length k is good if and only if a_i \ne a_{k-i+1} for all i=1,2,\ldots,k. For Div. 2 contestants, note that this condition is different from the condition in problem B.For example, \texttt{10}, \texttt{1010}, \texttt{111000} are good, while \texttt{11}, \texttt{101}, \texttt{001}, \texttt{001100} are not good.Qingshan wants to make s good. To do this, she can do the following operation at most 300 times (possibly, zero): insert \texttt{01} to any position of s (getting a new s). Please tell Qingshan if it is possible to make s good. If it is possible, print a sequence of operations that makes s good.InputThe input consists of multiple test cases. The first line contains a single integer t (1\le t\le 100) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n\le 100) — the length of string s, respectively.The second line of each test case contains a string s with length n.It is guaranteed that s only consists of \texttt{0} and \texttt{1}.OutputFor each test case, if it impossible to make s good, output -1.Otherwise, output p (0 \le p \le 300) — the number of operations, in the first line.Then, output p integers in the second line. The i-th integer should be an index x_i (0 \le x_i \le n+2i-2) — the position where you want to insert \texttt{01} in the current s. If x_i=0, you insert \texttt{01} at the beginning of s. Otherwise, you insert \texttt{01} immediately after the x_i-th character of s.We can show that under the constraints in this problem, if an answer exists, there is always an answer that requires at most 300 operations.ExampleInput 620130004111160011101001110011003001Output 0 -1 -1 2 6 7 1 10 -1 NoteIn the first test case, you can do zero operations and get s=\texttt{01}, which is good.Another valid solution is to do one operation: (the inserted \texttt{01} is underlined) \texttt{0}\underline{\texttt{01}}\texttt{1} and get s = \texttt{0011}, which is good.In the second and the third test case, it is impossible to make s good.In the fourth test case, you can do two operations: \texttt{001110}\underline{\texttt{01}} \texttt{0011100}\underline{\texttt{01}}\texttt{1} and get s = \texttt{0011100011}, which is good.
620130004111160011101001110011003001
0 -1 -1 2 6 7 1 10 -1
1 second
256 megabytes
['constructive algorithms', 'greedy', 'implementation', '*1300']
F. Minimum Segmentstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou had a sequence a_1, a_2, \ldots, a_n consisting of integers from 1 to n, not necessarily distinct. For some unknown reason, you decided to calculate the following characteristic of the sequence: Let r_i (1 \le i \le n) be the smallest j \ge i such that on the subsegment a_i, a_{i+1}, \ldots, a_j all distinct numbers from the sequence a appear. More formally, for any k \in [1, n], there exists l \in [i, j] such that a_k = a_l. If such j does not exist, r_i is considered to be equal to n+1. The characteristic of the sequence a is defined as the sequence r_1, r_2, \ldots, r_n. Unfortunately, the sequence a got lost, but you still have its characteristic r. You want to reconstruct any sequence a that matches the characteristic, or determine that there is an error in the characteristic and such a sequence does not exist.InputEach test consist of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^5) — the length of the lost sequence a.The second line of each test case contains n integers r_1, r_2, \ldots, r_n (i \le r_i \le n+1) — the characteristic of the lost sequence a. It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the following: If there is no sequence a with the characteristic r, print "No". Otherwise, print "Yes" on the first line, and on the second line, print any sequence of integers a_1, a_2, \ldots, a_n (1 \le a_i \le n) that matches the characteristic r. You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).ExampleInput 532 3 452 3 5 4 61131 3 483 6 6 6 8 9 9 9Output Yes 1 2 1 No Yes 1 No Yes 1 3 5 3 5 1 1 3 NoteIn the first test case, the sequence a = [1, 2, 1] is suitable. The integers 1 and 2 appear on the subsegments [1, 2] and [2, 3].In the second test case, it can be proved that there is no suitable sequence a.
532 3 452 3 5 4 61131 3 483 6 6 6 8 9 9 9
Yes 1 2 1 No Yes 1 No Yes 1 3 5 3 5 1 1 3
1 second
256 megabytes
['constructive algorithms', '*3400']
E. Good Coloringstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice suggested Bob to play a game. Bob didn't like this idea, but he couldn't refuse Alice, so he asked you to write a program that would play instead of him.The game starts with Alice taking out a grid sheet of size n \times n, the cells of which are initially not colored. After that she colors some 2n cells with colors 1,2,\ldots, 2n, respectively, and informs Bob about these cells.In one move, Bob can point to a cell that has not been colored yet and ask Alice to color that cell. Alice colors that cell with one of the 2n colors of her choice, informing Bob of the chosen color. Bob can make no more than 10 moves, after which he needs to find a good set of four cells.A set of four cells is considered good if the following conditions are met: All the cells in the set are colored; No two cells in the set are colored with the same color; The centers of the cells form a rectangle with sides parallel to the grid lines. InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 200) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (3 \le n \le 1000) — the size of the grid.The i-th of the following 2n lines contains two integers x_i and y_i (1 \le x_i, y_i \le n) — the coordinates of the cell colored with the i-th color.It is guaranteed that all coordinates (x_i, y_i) are pairwise distinct for the same test case.After reading the input data for each test case, continue the interaction as described below.InteractionYou can make no more than 10 moves. To make a move, output "? x y" (1 \leq x,y \leq n). In response to this, the jury's program will output a single integer from 1 to 2n — the color of cell (x,y).After all moves (it is not necessary to make all 10 moves and it is not necessary to make at least one move), output a string in the format "! x_1 x_2 y_1 y_2" (1 \leq x_1, x_2, y_1, y_2 \leq n, x_1 \ne x_2, y_1 \ne y_2). If cells (x_1, y_1), (x_1, y_2), (x_2, y_1), (x_2, y_2) are colored with different colors, the jury's program will output "OK". After that, proceed to the next test case or terminate the program if there are no more test cases left.Otherwise, if any of the specified cells are colored with the same color or one of the cells is not colored yet, the jury's program will output "ERROR". In this case, your program should immediately terminate its execution to receive the Wrong Answer verdict. Otherwise, you may receive an arbitrary verdict.After each move or answer, do not forget to output the end of line and flush the output. Otherwise, you will get the Idleness limit exceeded verdict.To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Note that the interactor is adaptive, meaning that the color Alice will use to color the cells during Bob's moves is not fixed in advance.HacksYou cannot use hacks in this problem.ExampleInput 2 3 1 2 1 3 2 1 2 3 3 1 3 2 1 OK 3 1 1 1 2 1 3 2 1 2 2 2 3 OKOutput ? 1 1 ! 1 2 1 3 ! 1 2 1 2 NoteIn the first test case: In the second test case, cells with coordinates (1, 1), (1, 2), (2, 1), (2, 2) are initially colored by Alice in different colors.
2 3 1 2 1 3 2 1 2 3 3 1 3 2 1 OK 3 1 1 1 2 1 3 2 1 2 2 2 3 OK
? 1 1 ! 1 2 1 3 ! 1 2 1 2
2 seconds
256 megabytes
['binary search', 'constructive algorithms', 'graphs', 'interactive', '*3100']
D. Splittime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call an array b_1, b_2, \ldots, b_m (m \ge 2) good if it can be split into two parts such that all elements in the left part are strictly smaller than all elements in the right part. In other words, there must exist an index 1 \le i < m such that every element from b_1, \ldots, b_i is strictly smaller than every element from b_{i+1}, \ldots, b_m.Given an array a_1, a_2, \ldots a_n consisting of distinct integers from 1 to n. There are q queries. Each query consists of two numbers l and r. For each query, determine whether the array a_l, a_{l+1}, \ldots, a_r is good.InputThe first line contains a single integer n (2 \le n \le 3 \cdot 10^5) — the size of the array.The second line contains n distinct integers a_1, a_2, \ldots, a_n (1 \le a_n \le n) — the elements of the array a.The third line contains a single integer q (1 \le q \le 3 \cdot 10^5) — the number of queries.Each of the next q lines contains two integers l_i and r_i (1 \le l_i < r_i \le n) — the description of the i-th query.OutputFor each query, output "Yes" (without quotes) if the array a_l, a_{l+1}, \ldots, a_r is good, and "No" (without quotes) otherwise.You can output "Yes" and "No" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).ExamplesInput 5 3 2 1 4 5 5 1 5 1 3 1 4 1 2 2 5 Output Yes No Yes No Yes Input 6 1 6 2 4 3 5 3 3 5 2 6 4 6 Output Yes No Yes NoteIn the first example: The array [3,2,1,4,5] can be split into two parts: [3,2,1] and [4,5]. The array [3,2,1] cannot be split into two parts such that all elements in the left part are smaller than all elements in the right part. The array [3,2,1,4] can be split into two parts: [3,2,1] and [4]. The array [3,2] cannot be split into two parts such that all elements in the left part are smaller than all elements in the right part. The array [2,1,4,5] can be split into two parts: [2,1] and [4,5].In the second example: The array [2,4,3] can be split into two parts: [2] and [4,3]. The array [6,2,4,3,5] cannot be split into two parts such that all elements in the left part are smaller than all elements in the right part. The array [4,3,5] can be split into two parts: [4,3] and [5].
5 3 2 1 4 5 5 1 5 1 3 1 4 1 2 2 5
Yes No Yes No Yes
3 seconds
256 megabytes
['binary search', 'data structures', 'divide and conquer', 'dsu', 'math', 'trees', 'two pointers', '*2700']
C. Minimum Arraytime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array a of length n consisting of integers. Then the following operation is sequentially applied to it q times: Choose indices l and r (1 \le l \le r \le n) and an integer x; Add x to all elements of the array a in the segment [l, r]. More formally, assign a_i := a_i + x for all l \le i \le r.Let b_j be the array a obtained after applying the first j operations (0 \le j \le q). Note that b_0 is the array a before applying any operations.You need to find the lexicographically minimum^{\dagger} array among all arrays b_j.^{\dagger}An array x is lexicographically smaller than array y if there is an index i such that x_i < y_i, and x_j = y_j for all j < i. In other words, for the first index i where the arrays differ, x_i < y_i.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 5 \cdot 10^5) — 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 5 \cdot 10^5) — the length of array a.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 elements of array a.The third line of each test case contains a single integer q (0 \le q \le 5 \cdot 10^5) — the number of operations on the array.In each of the next q lines, there are three integers l_j, r_j, and x_j (1 \le l_j \le r_j \le n, -10^9 \le x_j \le 10^9) — the description of each operation. The operations are given in the order they are applied.It is guaranteed that the sum of n over all test cases and the sum of q over all test cases do not exceed 5 \cdot 10^5.OutputFor each test case, output the lexicographically minimum array among all arrays b_j.ExampleInput 241 2 3 421 4 01 3 -10052 1 2 5 432 4 32 5 -21 3 1Output -99 -98 -97 4 2 1 2 5 4 NoteIn the first test case: b_0 = [1,2,3,4]; b_1 = [1,2,3,4]; b_2 = [-99,-98,-97,4].Thus, the lexicographically minimum array is b_2.In the second test case, the lexicographically minimum array is b_0.
241 2 3 421 4 01 3 -10052 1 2 5 432 4 32 5 -21 3 1
-99 -98 -97 4 2 1 2 5 4
3 seconds
256 megabytes
['binary search', 'brute force', 'constructive algorithms', 'data structures', 'greedy', 'hashing', 'two pointers', '*2400']
B. Time Traveltime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputBerland is a country with ancient history, where roads were built and destroyed for centuries. It is known that there always were n cities in Berland. You also have records of t key moments in the history of the country, numbered from 1 to t. Each record contains a list of bidirectional roads between some pairs of cities, which could be used for travel in Berland at a specific moment in time.You have discovered a time machine that transports you between key moments. Unfortunately, you cannot choose what point in time to end up at, but you know the order consisting of k moments in time a_{i}, in which the machine will transport you. Since there is little time between the travels, when you find yourself in the next key moment in time (including after the last time travel), you can travel on at most one existing road at that moment, coming out from the city you were in before time travel.Currently, you are in city 1, and the time machine has already transported you to moment a_{1}. You want to reach city n as quickly as possible. Determine the minimum number of time travels, including the first one, that you need to make in order to reach city n.InputThe first line contains two integers n and t (2 \le n \le 2 \cdot 10^5, 1 \le t \le 2 \cdot 10^5) — the number of cities in Berland and the number of records about key moments in history. Then follows the description of each of the t records.The first line of each record description contains a single integer m_{i} (0 \le m_{i} \le \min \left(\frac{n(n-1)}{2}, 2 \cdot 10^5\right)) — the number of roads in the i-th record.Each of the next m_i lines contains two integers v_{j} and u_{j} (1 \le v_{j}, u_{j} \le n, v_{j} \neq u_{j}) — the numbers of cities connected by the j-th road in the i-th key moment in history.The next line contains a single integer k (1 \le k \le 2 \cdot 10^5) — the number of time moments between which movements will occur.The next line contains k integers a_1, a_2, \ldots, a_k (1 \le a_{i} \le t) — the time moments at which you will be after each movement.It is guaranteed that the sum of m_{i} does not exceed 2 \cdot 10^5. It is guaranteed that each unordered pair (u, v) occurs in the road description for one record no more than once.OutputOutput a single integer — the minimum number of time travels required to reach city n from city 1, or -1 if it is impossible.Note that movement to time moment a_{1} is also considered a movement.ExamplesInput 5 241 22 33 44 522 33 562 1 2 1 2 1Output 5 Input 5 231 23 14 322 14 551 2 1 1 1Output -1 NoteIn the first example, you are in city 1 and move to moment a_{1} = 2. Since there are no suitable roads to pass, you do nothing and move to moment a_{2} = 1, after which you travel along the road (1, 2). Moving to moment a_{3} = 2, you travel along the road (2, 3). Moving to moment a_{4} = 1, you stay in city 3 and make the next time travel. At time moment a_{5} = 2, you travel along the road (3, 5) and end up in the final city after 5 time travels.In the second example, it can be shown that it is impossible to reach city 5 with the given time travels.
5 241 22 33 44 522 33 562 1 2 1 2 1
5
2 seconds
512 megabytes
['binary search', 'graphs', 'shortest paths', '*1900']
F. Diamond Thefttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp is the most famous thief in Berland. This time, he decided to steal two diamonds. Unfortunately for Monocarp, there are n cameras monitoring the diamonds. Each camera has two parameters, t_i and s_i. The first parameter determines whether the camera is monitoring the first diamond only (t_i=1), the second diamond only (t_i=2), or both diamonds (t_i=3). The second parameter determines the number of seconds the camera will be disabled after it is hacked.Every second, Monocarp can perform one of the following three actions: do nothing; choose a camera and hack it; if Monocarp hacks the i-th camera, it will be disabled for the next s_i seconds (if the current second is the T-th one, the camera will be disabled from the (T+1)-th to the (T+s_i)-th second, inclusive); steal a diamond if all cameras monitoring it are currently disabled. Monocarp cannot steal the second diamond if he hasn't stolen the first diamond yet. Note that Monocarp can hack a camera multiple times, even if it is currently disabled.Your task is to determine the minimum time it will take Monocarp to steal both diamonds, beginning with the first diamond, or report that it is impossible.InputThe first line contains a single integer n (0 \le n \le 1500) — the number of cameras.Then n lines follow, the i-th of them contains two integers t_i and s_i (1 \le t_i \le 3; 1 \le s_i \le 2n) — the parameters of the i-th camera.OutputPrint a single integer — the minimum time it will take for Monocarp to steal the first diamond first and then the second diamond. If it is impossible, print -1.ExamplesInput 4 2 6 1 2 1 2 2 1 Output 6 Input 4 2 8 3 2 3 2 3 5 Output 9 Input 2 3 2 2 3 Output 4 Input 1 3 1 Output 4 Input 8 2 1 2 2 3 5 3 6 1 2 1 3 1 4 1 5 Output 11
4 2 6 1 2 1 2 2 1
6
2 seconds
256 megabytes
['data structures', 'greedy', '*3300']
E. I Wanna be the Team Leadertime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputMonocarp is a team leader in a massive IT company.There are m projects his team of programmers has to complete, numbered from 1 to m. The i-th project has a difficulty level b_i.There are n programmers in the team, numbered from 1 to n. The j-th programmer has a stress tolerance level a_j.Monocarp wants to assign the programmers to the projects in such a way that: each programmer is assigned to no more than one project; each project has at least one programmer assigned to it; let k programmers be assigned to the i-th project; then all the assigned programmers have to have a stress tolerance level greater than or equal to \frac{b_i}{k}. Help Monocarp to find a valid assignment. If there are multiple answers, print any of them.InputThe first line contains two integers n and m (1 \le n \le 2 \cdot 10^5; 1 \le m \le 20) — the number of programmers and the number of projects.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9) — the stress tolerance level of each programmer.The third line contains m integers b_1, b_2, \dots, b_m (1 \le b_i \le 10^9) — the difficulty level of each project.OutputIf there is no valid assignment, print "NO".Otherwise, in the first line, print "YES". In the i-th of the next m lines, print the list of the programmers assigned to the i-th project: first, the number of programmers, then their indices in an arbitrary order.If there are multiple answers, print any of them.ExamplesInput 5 3 4 6 100 5 1 50 1 12 Output YES 1 3 1 5 3 2 4 1 Input 5 3 3 6 100 5 1 50 1 12 Output NO Input 5 3 2 2 2 2 4 3 5 1 Output YES 1 5 3 1 2 3 1 4 Input 5 1 10 20 30 40 50 4 Output YES 1 4
5 3 4 6 100 5 1 50 1 12
YES 1 3 1 5 3 2 4 1
2 seconds
512 megabytes
['bitmasks', 'constructive algorithms', 'dp', 'greedy', 'math', 'sortings', 'two pointers', '*2400']
D. Monocarp and the Settime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp has n numbers 1, 2, \dots, n and a set (initially empty). He adds his numbers to this set n times in some order. During each step, he adds a new number (which has not been present in the set before). In other words, the sequence of added numbers is a permutation of length n.Every time Monocarp adds an element into the set except for the first time, he writes out a character: if the element Monocarp is trying to insert becomes the maximum element in the set, Monocarp writes out the character >; if the element Monocarp is trying to insert becomes the minimum element in the set, Monocarp writes out the character <; if none of the above, Monocarp writes out the character ?. You are given a string s of n-1 characters, which represents the characters written out by Monocarp (in the order he wrote them out). You have to process m queries to the string. Each query has the following format: i c — replace s_i with the character c. Both before processing the queries and after each query, you have to calculate the number of different ways to order the integers 1, 2, 3, \dots, n such that, if Monocarp inserts the integers into the set in that order, he gets the string s. Since the answers might be large, print them modulo 998244353.InputThe first line contains two integers n and m (2 \le n \le 3 \cdot 10^5; 1 \le m \le 3 \cdot 10^5).The second line contains the string s, consisting of exactly n-1 characters <, > and/or ?.Then m lines follow. Each of them represents a query. Each line contains an integer i and a character c (1 \le i \le n-1; c is either <, >, or ?).OutputBoth before processing the queries and after each query, print one integer — the number of ways to order the integers 1, 2, 3, \dots, n such that, if Monocarp inserts the integers into the set in that order, he gets the string s. Since the answers might be large, print them modulo 998244353.ExamplesInput 6 4 <?>?> 1 ? 4 < 5 < 1 > Output 3 0 0 0 1 Input 2 2 > 1 ? 1 < Output 1 0 1 NoteIn the first example, there are three possible orderings before all queries: 3, 1, 2, 5, 4, 6; 4, 1, 2, 5, 3, 6; 4, 1, 3, 5, 2, 6. After the last query, there is only one possible ordering: 3, 5, 4, 6, 2, 1.
6 4 <?>?> 1 ? 4 < 5 < 1 >
3 0 0 0 1
2 seconds
256 megabytes
['combinatorics', 'data structures', 'math', '*2100']
C. Decreasing Stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecall that string a is lexicographically smaller than string b if a is a prefix of b (and a \ne b), or there exists an index i (1 \le i \le \min(|a|, |b|)) such that a_i < b_i, and for any index j (1 \le j < i) a_j = b_j.Consider a sequence of strings s_1, s_2, \dots, s_n, each consisting of lowercase Latin letters. String s_1 is given explicitly, and all other strings are generated according to the following rule: to obtain the string s_i, a character is removed from string s_{i-1} in such a way that string s_i is lexicographically minimal.For example, if s_1 = \mathrm{dacb}, then string s_2 = \mathrm{acb}, string s_3 = \mathrm{ab}, string s_4 = \mathrm{a}.After that, we obtain the string S = s_1 + s_2 + \dots + s_n (S is the concatenation of all strings s_1, s_2, \dots, s_n).You need to output the character in position pos of the string S (i. e. the character S_{pos}).InputThe first line contains one integer t — the number of test cases (1 \le t \le 10^4).Each test case consists of two lines. The first line contains the string s_1 (1 \le |s_1| \le 10^6), consisting of lowercase Latin letters. The second line contains the integer pos (1 \le pos \le \frac{|s_1| \cdot (|s_1| +1)}{2}). You may assume that n is equal to the length of the given string (n = |s_1|).Additional constraint on the input: the sum of |s_1| over all test cases does not exceed 10^6.OutputFor each test case, print the answer — the character that is at position pos in string S. Note that the answers between different test cases are not separated by spaces or line breaks.ExampleInput 3cab6abcd9x1Output abx
3cab6abcd9x1
abx
2 seconds
256 megabytes
['implementation', 'strings', '*1600']
B. Fear of the Darktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp tries to get home from work. He is currently at the point O = (0, 0) of a two-dimensional plane; his house is at the point P = (P_x, P_y).Unfortunately, it is late in the evening, so it is very dark. Monocarp is afraid of the darkness. He would like to go home along a path illuminated by something.Thankfully, there are two lanterns, located in the points A = (A_x, A_y) and B = (B_x, B_y). You can choose any non-negative number w and set the power of both lanterns to w. If a lantern's power is set to w, it illuminates a circle of radius w centered at the lantern location (including the borders of the circle).You have to choose the minimum non-negative value w for the power of the lanterns in such a way that there is a path from the point O to the point P which is completely illuminated. You may assume that the lanterns don't interfere with Monocarp's movement. The picture for the first two test cases InputThe first line of the input contains one integer t (1 \le t \le 10^4) — the number of test cases.Each test case consists of three lines: the first line contains two integers P_x and P_y (-10^3 \le P_x, P_y \le 10^3) — the location of Monocarp's house; the second line contains two integers A_x and A_y (-10^3 \le A_x, A_y \le 10^3) — the location of the first lantern; the third line contains two integers B_x and B_y (-10^3 \le B_x, B_y \le 10^3) — the location of the second lantern. Additional constraint on the input: in each test case, the points O, P, A and B are different from each other. OutputFor each test case, print the answer on a separate line — one real number equal to the minimum value of w such that there is a completely illuminated path from the point O to the point P.Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6} — formally, if your answer is a, and the jury's answer is b, your answer will be accepted if \dfrac{|a - b|}{\max(1, b)} \le 10^{-6}.ExampleInput 23 31 0-1 63 3-1 -14 3Output 3.6055512755 3.2015621187
23 31 0-1 63 3-1 -14 3
3.6055512755 3.2015621187
2 seconds
256 megabytes
['binary search', 'geometry', 'math', '*1200']
A. Sum of Threetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp has an integer n.He wants to represent his number as a sum of three distinct positive integers x, y, and z. Additionally, Monocarp wants none of the numbers x, y, and z to be divisible by 3.Your task is to help Monocarp to find any valid triplet of distinct positive integers x, y, and z, or report that such a triplet does not exist.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of testcases.The only line of each testcase contains a single integer n (1 \le n \le 10^{9}).OutputFor each testcase, if there is no valid triplet x, y, and z, print NO on the first line.Otherwise, print YES on the first line. On the second line, print any valid triplet of distinct positive integers x, y, and z such that x + y + z = n, and none of the printed numbers are divisible by 3. If there are multiple valid triplets, you can print any of them.ExampleInput 4104159Output YES 4 5 1 NO YES 2 8 5 NO NoteIn the first testcase, one of the valid triplets is x = 4, y = 5, z = 1. None of these numbers are divisible by three, and 4 + 5 + 1 = 10.In the second testcase, there is no valid triplet.In the third testcase, one of the valid triplets is x = 2, y = 8, z = 5. None of these numbers are divisible by three, and 2 + 8 + 5 = 15.In the fourth testcase, there is no valid triplet.
4104159
YES 4 5 1 NO YES 2 8 5 NO
2 seconds
256 megabytes
['brute force', 'constructive algorithms', 'math', '*800']
A. Deterministic Scheduling for Extended Reality over 5G and Beyondtime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputBackground Extended reality (XR) service is a promising application for future communications. In wireless communications, XR data are transmitted over radio between base stations and mobile terminals. A region is usually divided into multiple cells, each of which is equipped with a base station to serve users. One base station usually serves multiple users, and multiple base stations may serve one user at the same time.TaskThe task of this competition is to design a scheduling algorithm for XR service. By properly allocating radio resources, we want to maximize the number of XR data frames that are successfully transmitted. A diagram is provided below for illustration: The transmission of a data frame is failed when it cannot be completely transmitted during the permitted transmission time window. Therefore, the objective of this task can be modeled as: \mathcal{P}: \max \sum_j f_j \tag{1} f_j=\left\{\begin{array}{c} 1, g_j \geq T B S_j \\ 0, g_j< T B S_j \end{array}\right. \tag{2} Here, f_j represents the transmission result of the j-th frame: when the actual transmitted bits g_j (computed via (5)) is not less than the size of the frame, i.e., TBS_j (transport block size), the frame would be successfully transmitted so that f_j=1. Otherwise, f_j=0.To achieve better user experience, scheduling algorithm should be proposed to efficiently assign the limited radio resources: Time domain resource, which is divided into several transmission time intervals (TTIs), each TTI corresponds to a transmission time of 0.5ms. Frequency domain resource, which is divided into several resource block groups (Resource Block Group, RBG), each RBG corresponds to a transmission bandwidth of 5760 kHz. Power domain resource: each cell has a fixed maximum transmission power to serve users. Summarily, two optimization variables are introduced to represent the scheduling result: b_{r n t}^{(k)} \in\{0,1\} \tag{3} p_{ {rnt }}^{(k)} \geq 0 , \quad \sum_r \sum_n p_{ {rnt }}^{(k)} \leq R, \quad \sum_n p_{ {rnt }}^{(k)} \leq 4 \tag{4} Here, b_{rnt}^{(k)} is a Boolean variable denoting whether the r-th RBG of cell k is allocated to user n at TTI t, and p_{rnt}^{(k)} is a nonnegative continuous variable denoting the power allocated to user n in the r-th RBG of cell k at TTI t. For each TTI of each cell, the power range of each RBG is between 0 and 4, and the total power of all RBGs can not be larger than R.When the radio resources are allocated to the users, the XR data transmission can be provided for them. Assume that the j-th frame belongs to the n-th user, the actual transmitted bits for the frame, i.e., g_j can be given by: g_{j}= W\times \sum_{t=t_{0, j}}^{t_{1, j}} \sum_k \sum_r b_{r n t}^{(k)} \times \log _2\left(1+s_{n t}^{(k)}\right). \tag{5} Note that W\times \mathrm{log}_2 (1+s_{nt}^{(k)} ) is the well-known Shannon formula, which represents the transmitted data volume, where s_{nt}^{(k)} represents the transmission SINR (Signal-to-Interference-plus-Noise-Ratio) of user n in cell k at TTI t, and W=192 is the constant number of available frequency resounce elements of one RBG. t_{0,j} and t_{1,j} denote the start TTI and the end TTI of frame j, respectively. The physical meaning of Formula (5) is that the number of bits transmitted within the valid time period, t_{0,j}\sim t_{1,j}, will be counted as valid transmission bits for the j-th frame. Finally, we give the expression of SINR, which may be complicated but corresponds to the actual physical transmission: s_{nt}^{\left( k \right)} = {\left( {\prod\limits_{r,b_{rnt}^{\left( k \right)} = 1} {s_{rnt}^{\left( k \right)}} } \right)^{\frac{1}{{\sum\nolimits_r {b_{rnt}^{\left( k \right)}} }}}} \tag{6} s_{r n t}^{(k)}=\frac{s_{0, r n t}^{(k)} \times p_{r n t}^{(k)} \times \prod_{m \neq n} e^{d^{(k)}_{mrn} \times b_{r m t}^{(k)}}}{1+\sum_{k^{\prime} \neq k, n^{\prime} \neq n} s_{0, r n t}^{(k^{\prime})} \times p_{r n^{\prime} t}^{\left(k^{\prime}\right)} \times e^{-d^{(k^{\prime})}_{n^{\prime}rn}}} \tag{7} Formula (6) shows the computation of user-level effective SINR: the transmission SINR of user n, i.e., s_{nt}^{(k)}, is the geometric mean of the SINRs of scheduled RBGs. Then, formula (7) shows the computation of RBG-level effective SINR. s_{0,rnt}^{(k)} is a given constant denoting the initial SINR on RBG r of cell k at TTI t, which indicates the quality of the channel. Another given constant value d^{(k)}_{mrn} represents the interference factor between user m and user n on RBG r, when user m is scheduled on cell k. Note that d^{(k)}_{mrn}=d^{(k)}_{nrm}\le 0, which reveals that scheduling multiple users on the same RBG-TTI resource will cause a decrease in the SINR of each user.To sum up, participants are required to find an efficient radio resource allocation, so that more XR data frames can be successfully transmitted. InputThe input of a single test has (4 + R \cdot K \cdot T + N \cdot R \cdot K + 1 + J) lines, which contains user number N, cell number K, TTI number T, RBG number R, initial SINRs s_{0, r n t}^{(k)}, interference factors d_{mrn}, frame number J and information about J frames. The details are as follows: Line 1: User number N, integer, 1 \leq N \leq 100. Users are numbered from 0 to N - 1. Line 2: Cell number K, integer, 1 \leq K \leq 10. Cells are numbered from 0 to K - 1. Line 3: TTI number T, integer, 1 \leq T \leq 1000. TTIs are numbered from 0 to T - 1. Line 4: RBG number R, integer, 1 \leq R \leq 10. RBGs are numbered from 0 to R - 1. Line 5 to (4+R \cdot K\cdot T): Initial SINRs s_{0, r n t}^{(k)}, float, 0 < s_{0, r n t}^{(k)} < 10\,000. Each line has N elements, corresponding to N users. s_{0, r n t}^{(k)} is the (n+1)-th element of line (5+r+k \cdot R+t \cdot K \cdot R). Line (5+R \cdot K \cdot T) to (4+R \cdot K \cdot T + N \cdot R \cdot K): Interference factors d^{(k)}_{mrn}, float, -2 \leq d^{(k)}_{mrn} \leq 0. Each line has N elements, corresponding to N users. d^{(k)}_{mrn} is the (n+1)-th element of line (5+R \cdot K \cdot T+m+r \cdot N + k \cdot R \cdot N). Line (5+R \cdot K \cdot T + N \cdot R \cdot K): Frame number J, integer, 1 \leq J \leq 5000. Last J lines: Frame information. Each line contains 5 integers corresponding to a frame, which are, in order: frame ID j\in\{0,\ldots,J-1\} in increasing order, size TBS_j (0 < TBS_j \leq 100\,000), user ID it belongs to, first TTI t_{0,j} \in\{0,\ldots,T-1\}, and number of TTIs t_{d,j} \in \left[ {1,100} \right]. Last TTI for frame j can be found as t_{1,j}=t_{0,j}+t_{d,j}-1; it is guaranteed that t_{1,j} \le T - 1. It is guaranteed that each user has at most one frame at each TTI.OutputOutput for a certain input is the optimization result of p_{r n t}^{(k)} (float), which has R \cdot K \cdot T lines. Each line has N elements, corresponding to N users. p_{r n t}^{(k)} is the (n+1)-th element of line (1+r+k \cdot R+t \cdot K \cdot R). Note that the optimization result of b_{r n t}^{(k)} does not need to be output, because p_{r n t}^{(k)}>0 and p_{r n t}^{(k)} = 0 means b_{r n t}^{(k)}=1 and b_{r n t}^{(k)} = 0, respectively.Please note that if the outputs do not meet the constraint (4), it will be judged as an incorrect answer and get score 0. Besides, transmit on some TTIs out of time window is valid, but usually results in a lower score due to resources waste. ScoringThe goal is to maximize the number of successfully scheduled frames. When these numbers are tied, we will compare who used less power. To achieve that, Score = X - 10^{-6}\times p, where X and p represent the number of successfully scheduled frames and the total power used for transmission, respectively.The total score for a submission is the sum of scores on each test.ExampleInput 2 2 2 1 1.3865 11.3865 1.3865 11.3865 2.3865 2.3865 2.3865 2.3865 0 -2 -2 0 0 -2 -2 0 2 0 250 0 0 2 1 25 1 0 2 Output 0.000000 0.004950 0.000000 0.004950 0.245039 0.000000 0.245039 0.000000 NoteTwo sets of tests are prepared in this problem. For the duration of the competition, each submission is tested on the preliminary set of tests. When the competition is finished, for each contestant:The jury takes the latest submission with non-zero score on preliminary tests;This submission is tested on the final set of tests for the final rank;The two sets of tests are generated from the same pool of data, based on the real word data.
2 2 2 1 1.3865 11.3865 1.3865 11.3865 2.3865 2.3865 2.3865 2.3865 0 -2 -2 0 0 -2 -2 0 2 0 250 0 0 2 1 25 1 0 2
0.000000 0.004950 0.000000 0.004950 0.245039 0.000000 0.245039 0.000000
2 seconds
1024 megabytes
['*special problem']
E. Hard Designtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputConsider an array of integers b_0, b_1, \ldots, b_{n-1}. Your goal is to make all its elements equal. To do so, you can perform the following operation several (possibly, zero) times: Pick a pair of indices 0 \le l \le r \le n-1, then for each l \le i \le r increase b_i by 1 (i. e. replace b_i with b_i + 1). After performing this operation you receive (r - l + 1)^2 coins. The value f(b) is defined as a pair of integers (cnt, cost), where cnt is the smallest number of operations required to make all elements of the array equal, and cost is the largest total number of coins you can receive among all possible ways to make all elements equal within cnt operations. In other words, first, you need to minimize the number of operations, second, you need to maximize the total number of coins you receive.You are given an array of integers a_0, a_1, \ldots, a_{n-1}. Please, find the value of f for all cyclic shifts of a.Formally, for each 0 \le i \le n-1 you need to do the following: Let c_j = a_{(j + i) \pmod{n}} for each 0 \le j \le n-1. Find f(c). Since cost can be very large, output it modulo (10^9 + 7).Please note that under a fixed cnt you need to maximize the total number of coins cost, not its remainder modulo (10^9 + 7).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 (1 \le n \le 10^6).The second line of each test case contains n integers a_0, a_1, \ldots, a_{n-1} (1 \le a_i \le 10^9).It is guaranteed that the sum of n over all test cases does not exceed 10^6.OutputFor each test case, for each 0 \le i \le n-1 output the value of f for the i-th cyclic shift of array a: first, output cnt (the minimum number of operations), then output cost (the maximum number of coins these operations can give) modulo 10^9 + 7.ExampleInput 51131 3 253 2 4 5 186 5 6 4 2 6 2 2410 10 10 10Output 0 0 3 3 2 5 2 5 7 18 7 16 6 22 5 28 5 28 9 27 9 27 9 27 9 27 11 23 9 27 9 27 13 19 0 0 0 0 0 0 0 0 NoteIn the first test case, there is only one cycle shift, which is equal to [1], and all its elements are already equal.In the second test case, you need to find the answer for three arrays: f([1, 3, 2]) = (3, 3). f([3, 2, 1]) = (2, 5). f([2, 1, 3]) = (2, 5). Consider the case of [2, 1, 3]. To make all elements equal, we can pick l = 1 and r = 1 on the first operation, which results in [2, 2, 3]. On the second operation we can pick l = 0 and r = 1, which results in [3, 3, 3]. We have used 2 operations, and the total number of coins received is 1^2 + 2^2 = 5.
51131 3 253 2 4 5 186 5 6 4 2 6 2 2410 10 10 10
0 0 3 3 2 5 2 5 7 18 7 16 6 22 5 28 5 28 9 27 9 27 9 27 9 27 11 23 9 27 9 27 13 19 0 0 0 0 0 0 0 0
3 seconds
256 megabytes
['greedy', 'implementation', 'math', '*2800']
D. Counting Rhymetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of integers a_1, a_2, \ldots, a_n.A pair of integers (i, j), such that 1 \le i < j \le n, is called good, if there does not exist an integer k (1 \le k \le n) such that a_i is divisible by a_k and a_j is divisible by a_k at the same time.Please, find the number of good pairs.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 (1 \le n \le 10^6).The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le n).It is guaranteed that the sum of n over all test cases does not exceed 10^6.OutputFor each test case, output the number of good pairs.ExampleInput 642 4 4 442 3 4 496 8 9 4 6 8 9 4 997 7 4 4 9 9 6 2 91810 18 18 15 14 4 5 6 8 9 10 12 15 16 18 17 13 112112 19 19 18 18 12 2 18 19 12 12 3 12 12 12 18 19 16 18 19 12Output 0 3 26 26 124 82 NoteIn the first test case, there are no good pairs. In the second test case, here are all the good pairs: (1, 2), (2, 3), and (2, 4).
642 4 4 442 3 4 496 8 9 4 6 8 9 4 997 7 4 4 9 9 6 2 91810 18 18 15 14 4 5 6 8 9 10 12 15 16 18 17 13 112112 19 19 18 18 12 2 18 19 12 12 3 12 12 12 18 19 16 18 19 12
0 3 26 26 124 82
2 seconds
256 megabytes
['dp', 'math', 'number theory', '*2100']
C. Medium Designtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe array a_1, a_2, \ldots, a_m is initially filled with zeroes. You are given n pairwise distinct segments 1 \le l_i \le r_i \le m. You have to select an arbitrary subset of these segments (in particular, you may select an empty set). Next, you do the following: For each i = 1, 2, \ldots, n, if the segment (l_i, r_i) has been selected to the subset, then for each index l_i \le j \le r_i you increase a_j by 1 (i. e. a_j is replaced by a_j + 1). If the segment (l_i, r_i) has not been selected, the array does not change. Next (after processing all values of i = 1, 2, \ldots, n), you compute \max(a) as the maximum value among all elements of a. Analogously, compute \min(a) as the minimum value. Finally, the cost of the selected subset of segments is declared as \max(a) - \min(a).Please, find the maximum cost among all subsets of segments.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 the test cases follows.The first line of each test case contains two integers n and m (1 \le n \le 10^5, 1 \le m \le 10^9) — the number of segments and the length of the array.The following n lines of each test case describe the segments. The i-th of these lines contains two integers l_i and r_i (1 \le l_i \le r_i \le m). It is guaranteed that the segments are pairwise distinct.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the maximum cost among all subsets of the given set of segments.ExampleInput 61 32 23 82 43 54 66 31 11 21 32 22 33 37 62 21 61 25 61 54 43 66 276 265 172 320 211 2212 244 10000000002 9999999993 1000000000123456789 9876543219274 123456789Output 1 3 2 3 4 4 NoteIn the first test case, there is only one segment available. If we do not select it, then the array will be a = [0, 0, 0], and the cost of such (empty) subset of segments will be 0. If, however, we select the only segment, the array will be a = [0, 1, 0], and the cost will be 1 - 0 = 1.In the second test case, we can select all the segments: the array will be a = [0, 1, 2, 3, 2, 1, 0, 0] in this case. The cost will be 3 - 0 = 3.
61 32 23 82 43 54 66 31 11 21 32 22 33 37 62 21 61 25 61 54 43 66 276 265 172 320 211 2212 244 10000000002 9999999993 1000000000123456789 9876543219274 123456789
1 3 2 3 4 4
3 seconds
256 megabytes
['brute force', 'data structures', 'dp', 'greedy', 'sortings', '*1700']
B. Haunted Housetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a number in binary representation consisting of exactly n bits, possibly, with leading zeroes. For example, for n = 5 the number 6 will be given as 00110, and for n = 4 the number 9 will be given as 1001.Let's fix some integer i such that 1 \le i \le n. In one operation you can swap any two adjacent bits in the binary representation. Your goal is to find the smallest number of operations you are required to perform to make the number divisible by 2^i, or say that it is impossible.Please note that for each 1 \le i \le n you are solving the problem independently.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 the test cases follows.The first line of each test case contains one integer n (1 \le n \le 10^5) — the length of the binary representation of the number.The second line of each test case contains a string of length n, consisting of 0 and 1, — the binary representation of the number.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, for each 1 \le i \le n output the smallest number of operations required to make the number divisible by 2^i, or output -1 if it is impossible.ExampleInput 61120130105101017000011112001011000110Output -1 1 -1 0 1 -1 1 3 -1 -1 -1 3 6 9 12 -1 -1 -1 0 2 4 6 10 15 20 -1 -1 -1 -1 -1 NoteIn the first test case, we cannot swap any elements, and the number 1 is not divisible by 2.In the second test case, the initial number is 1. It is not divisible by 2, but if we perform the operation, then we obtain the number with binary representation 10, which is equal to 2 in decimal representation, thus, it is divisible by 2. But this number is not divisible by 4 and we cannot obtain any other number using the operations.In the third test case, the initial number is 2. For i = 1 we do not have to perform any operations since the initial number is divisible by 2. For i = 2 we can perform one operation swapping the first two bits (we would obtain 100 in binary representation, which corresponds to number 4). There is no answer for i = 3.
61120130105101017000011112001011000110
-1 1 -1 0 1 -1 1 3 -1 -1 -1 3 6 9 12 -1 -1 -1 0 2 4 6 10 15 20 -1 -1 -1 -1 -1
1 second
256 megabytes
['binary search', 'greedy', 'math', 'two pointers', '*1100']
A. Simple Designtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA positive integer is called k-beautiful, if the digit sum of the decimal representation of this number is divisible by k^{\dagger}. For example, 9272 is 5-beautiful, since the digit sum of 9272 is 9 + 2 + 7 + 2 = 20.You are given two integers x and k. Please find the smallest integer y \ge x which is k-beautiful.^{\dagger} An integer n is divisible by k if there exists an integer m such that n = k \cdot m.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 the test cases follows.The only line of each test case contains two integers x and k (1 \le x \le 10^9, 1 \le k \le 10).OutputFor each test case, output the smallest integer y \ge x which is k-beautiful.ExampleInput 61 510 837 9777 31235 101 10Output 5 17 45 777 1243 19 NoteIn the first test case, numbers from 1 to 4 consist of a single digit, thus the digit sum is equal to the number itself. None of the integers from 1 to 4 are divisible by 5.In the fourth test case, the digit sum of 777 is 7 + 7 + 7 = 21 which is already divisible by 3.
61 510 837 9777 31235 101 10
5 17 45 777 1243 19
1 second
256 megabytes
['brute force', 'greedy', 'math', '*800']
G2. Dances (Hard Version)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. The only difference is that in this version m \leq 10^9.You are given two arrays of integers a_1, a_2, \ldots, a_n and b_1, b_2, \ldots, b_n. Before applying any operations, you can reorder the elements of each array as you wish. Then, in one operation, you will perform both of the following actions, if the arrays are not empty: Choose any element from array a and remove it (all remaining elements are shifted to a new array a), Choose any element from array b and remove it (all remaining elements are shifted to a new array b).Let k be the final size of both arrays. You need to find the minimum number of operations required to satisfy a_i < b_i for all 1 \leq i \leq k.This problem was too easy, so the problem author decided to make it more challenging. You are also given a positive integer m. Now, you need to find the sum of answers to the problem for m pairs of arrays (c[i], b), where 1 \leq i \leq m. Array c[i] is obtained from a as follows: c[i]_1 = i, c[i]_j = a_j, for 2 \leq j \leq n.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) - the number of sets of input data. This is followed by their description.The first line of each test case contains two integers n and m (2 \leq n \leq 10^5, 1 \leq m \leq 10^9) - the size of arrays a and b and the constraints on the value of element a_1.The second line of each test case contains n - 1 integers a_2, \ldots, a_n (1 \leq a_i \leq 10^9).The third line of each test case contains n integers b_1, b_2, \ldots, b_n (1 \leq b_i \leq 10^9).It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, output the total number of minimum operations for all pairs of arrays (c_i, b).ExampleInput 42 413 24 75 1 53 8 3 38 44 3 3 2 2 1 11 1 1 1 3 3 3 39 19 2 8 3 7 4 6 51 2 3 2 1 4 5 6 5Output 2 12 16 4 NoteIn the first test case: For the pair of arrays ([1, 1], [3, 2]), the answer is 0. No operations or reordering of elements are needed. For the pair of arrays ([2, 1], [3, 2]), the answer is 0. The elements of the first array can be rearranged to obtain [1, 2). No operations are needed. For the pair of arrays ([3, 1], [3, 2]), the answer is 1. The element 3 can be removed from the first array and the element 2 can be removed from the second array. For the pair of arrays ([4, 1], [3, 2]), the answer is 1. The element 4 can be removed from the first array and the element 3 can be removed from the second array.
42 413 24 75 1 53 8 3 38 44 3 3 2 2 1 11 1 1 1 3 3 3 39 19 2 8 3 7 4 6 51 2 3 2 1 4 5 6 5
2 12 16 4
3 seconds
256 megabytes
['binary search', 'greedy', 'sortings', 'two pointers', '*1900']
G1. Dances (Easy version)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the easy version of the problem. The only difference is that in this version m = 1.You are given two arrays of integers a_1, a_2, \ldots, a_n and b_1, b_2, \ldots, b_n. Before applying any operations, you can reorder the elements of each array as you wish. Then, in one operation, you will perform both of the following actions, if the arrays are not empty: Choose any element from array a and remove it (all remaining elements are shifted to a new array a), Choose any element from array b and remove it (all remaining elements are shifted to a new array b).Let k be the final size of both arrays. You need to find the minimum number of operations required to satisfy a_i < b_i for all 1 \leq i \leq k.This problem was too easy, so the problem author decided to make it more challenging. You are also given a positive integer m. Now, you need to find the sum of answers to the problem for m pairs of arrays (c[i], b), where 1 \leq i \leq m. Array c[i] is obtained from a as follows: c[i]_1 = i, c[i]_j = a_j, for 2 \leq j \leq n.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) - the number of sets of input data. This is followed by their description.The first line of each test case contains two integers n and m (2 \leq n \leq 10^5, m = 1) - the size of arrays a and b and the constraints on the value of element a_1.The second line of each test case contains n - 1 integers a_2, \ldots, a_n (1 \leq a_i \leq 10^9).The third line of each test case contains n integers b_1, b_2, \ldots, b_n (1 \leq b_i \leq 10^9).It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, output the total number of minimum operations for all pairs of arrays (c_i, b).ExampleInput 42 113 24 15 1 53 8 3 38 14 3 3 2 2 1 11 1 1 1 3 3 3 39 19 2 8 3 7 4 6 51 2 3 2 1 4 5 6 5Output 0 1 4 4 NoteIn the first test case for the pair of arrays ([1, 1], [3, 2]), the answer is 0. No operations or reordering of elements are needed.
42 113 24 15 1 53 8 3 38 14 3 3 2 2 1 11 1 1 1 3 3 3 39 19 2 8 3 7 4 6 51 2 3 2 1 4 5 6 5
0 1 4 4
3 seconds
256 megabytes
['binary search', 'greedy', 'two pointers', '*1400']
F. You Are So Beautifultime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of integers a_1, a_2, \ldots, a_n. Calculate the number of subarrays of this array 1 \leq l \leq r \leq n, such that: The array b = [a_l, a_{l+1}, \ldots, a_r] occurs in the array a as a subsequence exactly once. In other words, there is exactly one way to select a set of indices 1 \leq i_1 < i_2 < \ldots < i_{r - l + 1} \leq n, such that b_j = a_{i_j} for all 1 \leq j \leq r - l + 1.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. This is followed by their description.The first line of each test case contains an integer n (1 \leq n \leq 10^5) — the size of the array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq 10^9).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the number of suitable subarrays.ExampleInput 61121 131 2 142 3 2 154 5 4 5 4101 7 7 2 3 4 3 2 1 100Output 1 1 4 7 4 28 NoteIn the first test case, there is exactly one subarray (1, 1) that suits us.In the second test case, there is exactly one subarray (1, 2) that suits us. Subarrays (1, 1) and (2, 2) do not suit us, as the subsequence [1] occurs twice in the array.In the third test case, all subarrays except (1, 1) and (3, 3) are suitable.
61121 131 2 142 3 2 154 5 4 5 4101 7 7 2 3 4 3 2 1 100
1 1 4 7 4 28
1 second
256 megabytes
['data structures', '*1400']
E. Look Backtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of integers a_1, a_2, \ldots, a_n. You need to make it non-decreasing with the minimum number of operations. In one operation, you do the following: Choose an index 1 \leq i \leq n, Set a_i = a_i \cdot 2.An array b_1, b_2, \ldots, b_n is non-decreasing if b_i \leq b_{i+1} for all 1 \leq i < n.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. This is followed by their description.The first line of each test case contains an integer n (1 \leq n \leq 10^5) — the size of the array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq 10^9).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the minimum number of operations needed to make the array non-decreasing.ExampleInput 91122 133 2 147 1 5 3511 2 15 7 1061 8 2 16 8 162624323799 708290323122 1 1 3 3 11 12 22 45 777 777 15001212 11 10 9 8 7 6 5 4 3 2 1Output 0 1 3 6 10 3 0 2 66 NoteNo operations are needed in the first test case.In the second test case, we need to choose i = 2, after which the array will be [2, 2].In the third test case, we can apply the following operations: Choose i = 3, after which the array will be [3, 2, 2], Choose i = 3, after which the array will be [3, 2, 4], Choose i = 2, after which the array will be [3, 4, 4].
91122 133 2 147 1 5 3511 2 15 7 1061 8 2 16 8 162624323799 708290323122 1 1 3 3 11 12 22 45 777 777 15001212 11 10 9 8 7 6 5 4 3 2 1
0 1 3 6 10 3 0 2 66
1 second
256 megabytes
['bitmasks', 'greedy', '*1700']
D. In Lovetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputInitially, you have an empty multiset of segments. You need to process q operations of two types: + l r — Add the segment (l, r) to the multiset, - l r — Remove exactly one segment (l, r) from the multiset. It is guaranteed that this segment exists in the multiset.After each operation, you need to determine if there exists a pair of segments in the multiset that do not intersect. A pair of segments (l, r) and (a, b) do not intersect if there does not exist a point x such that l \leq x \leq r and a \leq x \leq b.InputThe first line of each test case contains an integer q (1 \leq q \leq 10^5) — the number of operations.The next q lines describe two types of operations. If it is an addition operation, it is given in the format + l r. If it is a deletion operation, it is given in the format - l r (1 \leq l \leq r \leq 10^9).OutputAfter each operation, print "YES" if there exists a pair of segments in the multiset that do not intersect, and "NO" otherwise.You can print the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.ExampleInput 12+ 1 2+ 3 4+ 2 3+ 2 2+ 3 4- 3 4- 3 4- 1 2+ 3 4- 2 2- 2 3- 3 4Output NO YES YES YES YES YES NO NO YES NO NO NO NoteIn the example, after the second, third, fourth, and fifth operations, there exists a pair of segments (1, 2) and (3, 4) that do not intersect.Then we remove exactly one segment (3, 4), and by that time we had two segments. Therefore, the answer after this operation also exists.
12+ 1 2+ 3 4+ 2 3+ 2 2+ 3 4- 3 4- 3 4- 1 2+ 3 4- 2 2- 2 3- 3 4
NO YES YES YES YES YES NO NO YES NO NO NO
2 seconds
256 megabytes
['data structures', 'greedy', '*1500']
C. Raspberriestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of integers a_1, a_2, \ldots, a_n and a number k (2 \leq k \leq 5). In one operation, you can do the following: Choose an index 1 \leq i \leq n, Set a_i = a_i + 1.Find the minimum number of operations needed to make the product of all the numbers in the array a_1 \cdot a_2 \cdot \ldots \cdot a_n divisible by k.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers n and k (2 \leq n \leq 10^5, 2 \leq k \leq 5) — the size of the array a and the number k.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq 10).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the minimum number of operations needed to make the product of all the numbers in the array divisible by k.ExampleInput 152 57 33 37 4 15 29 7 7 3 95 55 4 1 2 37 49 5 1 5 9 5 13 46 3 63 46 1 53 41 5 94 41 4 1 13 43 5 34 58 9 9 32 51 62 510 104 51 6 1 12 57 7Output 2 2 1 0 2 0 1 2 0 1 1 4 0 4 3 NoteIn the first test case, we need to choose the index i = 2 twice. After that, the array will be a = [7, 5]. The product of all the numbers in the array is 35.In the fourth test case, the product of the numbers in the array is 120, which is already divisible by 5, so no operations are needed.In the eighth test case, we can perform two operations by choosing i = 2 and i = 3 in any order. After that, the array will be a = [1, 6, 10]. The product of the numbers in the array is 60.
152 57 33 37 4 15 29 7 7 3 95 55 4 1 2 37 49 5 1 5 9 5 13 46 3 63 46 1 53 41 5 94 41 4 1 13 43 5 34 58 9 9 32 51 62 510 104 51 6 1 12 57 7
2 2 1 0 2 0 1 2 0 1 1 4 0 4 3
2 seconds
256 megabytes
['dp', 'math', '*1000']
B. Chemistrytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s of length n, consisting of lowercase Latin letters, and an integer k.You need to check if it is possible to remove exactly k characters from the string s in such a way that the remaining characters can be rearranged to form a palindrome. Note that you can reorder the remaining characters in any way.A palindrome is a string that reads the same forwards and backwards. For example, the strings "z", "aaa", "aba", "abccba" are palindromes, while the strings "codeforces", "reality", "ab" are not.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) — the number of the test cases. This is followed by their description.The first line of each test case contains two integers n and k (0 \leq k < n \leq 10^5) — the length of the string s and the number of characters to be deleted.The second line of each test case contains a string s of length n, consisting of lowercase Latin letters.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output "YES" if it is possible to remove exactly k characters from the string s in such a way that the remaining characters can be rearranged to form a palindrome, and "NO" otherwise.You can output the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.ExampleInput 141 0a2 0ab2 1ba3 1abb3 2abc6 2bacacd6 2fagbza6 2zwaafa7 2taagaak14 3ttrraakkttoorr5 3debdb5 4ecadc5 3debca5 3abaacOutput YES NO YES YES YES YES NO NO YES YES YES YES NO YES NoteIn the first test case, nothing can be removed, and the string "a" is a palindrome.In the second test case, nothing can be removed, but the strings "ab" and "ba" are not palindromes.In the third test case, any character can be removed, and the resulting string will be a palindrome.In the fourth test case, one occurrence of the character "a" can be removed, resulting in the string "bb", which is a palindrome.In the sixth test case, one occurrence of the characters "b" and "d" can be removed, resulting in the string "acac", which can be rearranged to the string "acca".In the ninth test case, one occurrence of the characters "t" and "k" can be removed, resulting in the string "aagaa", which is a palindrome.
141 0a2 0ab2 1ba3 1abb3 2abc6 2bacacd6 2fagbza6 2zwaafa7 2taagaak14 3ttrraakkttoorr5 3debdb5 4ecadc5 3debca5 3abaac
YES NO YES YES YES YES NO NO YES YES YES YES NO YES
2 seconds
256 megabytes
['strings', '*900']
A. Morningtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a four-digit pin code consisting of digits from 0 to 9 that needs to be entered. Initially, the cursor points to the digit 1. In one second, you can perform exactly one of the following two actions: Press the cursor to display the current digit, Move the cursor to any adjacent digit. The image above shows the device you are using to enter the pin code. For example, for the digit 5, the adjacent digits are 4 and 6, and for the digit 0, there is only one adjacent digit, 9.Determine the minimum number of seconds required to enter the given four-digit pin code.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) - the number of the test cases. This is followed by their description.The single line of each test case describes the pin code as a string of length 4, consisting of digits from 0 to 9.OutputFor each test case, output the minimum number of seconds required to enter the given pin code.ExampleInput 101111123610101920927300007492854302948361Output 4 9 31 27 28 13 25 16 33 24 NoteIn the first test case, the cursor needs to be pressed 4 times.In the second test case, it can be done in 9 seconds as follows: Press the cursor. Move the cursor to the digit 2. Press the cursor. Move the cursor to the digit 3. Press the cursor. Move the cursor to the digit 4. Move the cursor to the digit 5. Move the cursor to the digit 6. Press the cursor.
101111123610101920927300007492854302948361
4 9 31 27 28 13 25 16 33 24
2 seconds
256 megabytes
['math', '*800']
E2. Two Permutations (Hard Version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. The difference between the two versions is that you have to minimize the number of operations in this version. You can make hacks only if both versions of the problem are solved.You have two permutations^{\dagger} p_{1}, p_{2}, \ldots, p_{n} (of integers 1 to n) and q_{1}, q_{2}, \ldots, q_{m} (of integers 1 to m). Initially p_{i}=a_{i} for i=1, 2, \ldots, n, and q_{j} = b_{j} for j = 1, 2, \ldots, m. You can apply the following operation on the permutations several (possibly, zero) times.In one operation, p and q will change according to the following three steps: You choose integers i, j which satisfy 1 \le i \le n and 1 \le j \le m. Permutation p is partitioned into three parts using p_i as a pivot: the left part is formed by elements p_1, p_2, \ldots, p_{i-1} (this part may be empty), the middle part is the single element p_i, and the right part is p_{i+1}, p_{i+2}, \ldots, p_n (this part may be empty). To proceed, swap the left and the right parts of this partition. Formally, after this step, p will become p_{i+1}, p_{i+2}, \ldots, p_{n}, p_{i}, p_{1}, p_{2}, \ldots, p_{i-1}. The elements of the newly formed p will be reindexed starting from 1. Perform the same transformation on q with index j. Formally, after this step, q will become q_{j+1}, q_{j+2}, \ldots, q_{m}, q_{j}, q_{1}, q_{2}, \ldots, q_{j-1}. The elements of the newly formed q will be reindexed starting from 1. Your goal is to simultaneously make p_{i}=i for i=1, 2, \ldots, n, and q_{j} = j for j = 1, 2, \ldots, m.Find any way to achieve the goal using the minimum number of operations possible, or say that none exists. Please note that you have to minimize the number of operations.^{\dagger} A permutation of length k is an array consisting of k distinct integers from 1 to k 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 (k=3 but there is 4 in the array).InputThe first line contains two integers n and m (1 \le n, m \le 2500).The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le n).The third line contains m integers b_1, b_2, \ldots, b_m (1 \le b_i \le m).It is guaranteed that a and b are permutations.OutputIf there is no solution, print a single integer -1.Otherwise, print an integer k — the number of operations to perform, followed by k lines, each containing two integers i and j (1 \le i \le n, 1 \le j \le m) — the integers chosen for the operation.If there are multiple solutions, print any of them.Please note that you have to minimize the number of operations.ExamplesInput 3 5 2 1 3 5 2 1 4 3 Output 2 3 4 2 4 Input 4 4 3 4 2 1 2 4 1 3 Output 3 3 3 1 4 4 2 Input 2 2 1 2 2 1 Output -1 NoteIn the first test case, we can achieve the goal within 2 operations: In the first operation, choose i = 3, j = 4. After this, p becomes [3, 2, 1] and q becomes [3, 4, 5, 2, 1]. In the second operation, choose i = 2, j = 4. After this, p becomes [1, 2, 3] and q becomes [1, 2, 3, 4, 5]. In the third test case, it is impossible to achieve the goal.
3 5 2 1 3 5 2 1 4 3
2 3 4 2 4
2 seconds
256 megabytes
['constructive algorithms', '*3100']
E1. Two Permutations (Easy Version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the easy version of the problem. The difference between the two versions is that you do not have to minimize the number of operations in this version. You can make hacks only if both versions of the problem are solved.You have two permutations^{\dagger} p_{1}, p_{2}, \ldots, p_{n} (of integers 1 to n) and q_{1}, q_{2}, \ldots, q_{m} (of integers 1 to m). Initially p_{i}=a_{i} for i=1, 2, \ldots, n, and q_{j} = b_{j} for j = 1, 2, \ldots, m. You can apply the following operation on the permutations several (possibly, zero) times.In one operation, p and q will change according to the following three steps: You choose integers i, j which satisfy 1 \le i \le n and 1 \le j \le m. Permutation p is partitioned into three parts using p_i as a pivot: the left part is formed by elements p_1, p_2, \ldots, p_{i-1} (this part may be empty), the middle part is the single element p_i, and the right part is p_{i+1}, p_{i+2}, \ldots, p_n (this part may be empty). To proceed, swap the left and the right parts of this partition. Formally, after this step, p will become p_{i+1}, p_{i+2}, \ldots, p_{n}, p_{i}, p_{1}, p_{2}, \ldots, p_{i-1}. The elements of the newly formed p will be reindexed starting from 1. Perform the same transformation on q with index j. Formally, after this step, q will become q_{j+1}, q_{j+2}, \ldots, q_{m}, q_{j}, q_{1}, q_{2}, \ldots, q_{j-1}. The elements of the newly formed q will be reindexed starting from 1. Your goal is to simultaneously make p_{i}=i for i=1, 2, \ldots, n, and q_{j} = j for j = 1, 2, \ldots, m.Find any valid way to achieve the goal using at most 10\,000 operations, or say that none exists. Please note that you do not have to minimize the number of operations.It can be proved that if it is possible to achieve the goal, then there exists a way to do so using at most 10\,000 operations.^{\dagger} A permutation of length k is an array consisting of k distinct integers from 1 to k 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 (k=3 but there is 4 in the array).InputThe first line contains two integers n and m (1 \le n, m \le 2500).The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le n).The third line contains m integers b_1, b_2, \ldots, b_m (1 \le b_i \le m).It is guaranteed that a and b are permutations.OutputIf there is no solution, print a single integer -1.Otherwise, print an integer k (0 \le k \le 10\,000) — the number of operations to perform, followed by k lines, each containing two integers i and j (1 \le i \le n, 1 \le j \le m) — the integers chosen for the operation.If there are multiple solutions, print any of them.Please note that you do not have to minimize the number of operations.ExamplesInput 3 5 2 1 3 5 2 1 4 3 Output 2 3 4 2 4 Input 4 4 3 4 2 1 2 4 1 3 Output 5 4 2 3 3 1 4 3 2 4 1 Input 2 2 1 2 2 1 Output -1 NoteIn the first example, we can achieve the goal within 2 operations: In the first operation, choose i = 3, j = 4. After this, p becomes [3, 2, 1] and q becomes [3, 4, 5, 2, 1]. In the second operation, choose i = 2, j = 4. After this, p becomes [1, 2, 3] and q becomes [1, 2, 3, 4, 5]. In the third example, it is impossible to achieve the goal.
3 5 2 1 3 5 2 1 4 3
2 3 4 2 4
2 seconds
256 megabytes
['brute force', 'constructive algorithms', 'greedy', 'number theory', '*2400']
D. Tree XORtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a tree with n vertices labeled from 1 to n. An integer a_{i} is written on vertex i for i = 1, 2, \ldots, n. You want to make all a_{i} equal by performing some (possibly, zero) spells.Suppose you root the tree at some vertex. On each spell, you can select any vertex v and any non-negative integer c. Then for all vertices i in the subtree^{\dagger} of v, replace a_{i} with a_{i} \oplus c. The cost of this spell is s \cdot c, where s is the number of vertices in the subtree. Here \oplus denotes the bitwise XOR operation.Let m_r be the minimum possible total cost required to make all a_i equal, if vertex r is chosen as the root of the tree. Find m_{1}, m_{2}, \ldots, m_{n}.^{\dagger} Suppose vertex r is chosen as the root of the tree. Then vertex i belongs to the subtree of v if the simple path from i to r contains v.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 the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^{5}).The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \le a_i < 2^{20}).Each of the next n-1 lines contains two integers u and v (1 \le u, v \le n), denoting that there is an edge connecting two vertices u and v.It is guaranteed that the given edges form a tree.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^{5}.OutputFor each test case, print m_1, m_2, \ldots, m_n on a new line.ExampleInput 243 2 1 01 22 32 41100Output 8 6 12 10 0 NoteIn the first test case, to find m_1 we root the tree at vertex 1. In the first spell, choose v=2 and c=1. After performing the spell, a will become [3, 3, 0, 1]. The cost of this spell is 3. In the second spell, choose v=3 and c=3. After performing the spell, a will become [3, 3, 3, 1]. The cost of this spell is 3. In the third spell, choose v=4 and c=2. After performing the spell, a will become [3, 3, 3, 3]. The cost of this spell is 2. Now all the values in array a are equal, and the total cost is 3 + 3 + 2 = 8.The values m_2, m_3, m_4 can be found analogously.In the second test case, the goal is already achieved because there is only one vertex.
243 2 1 01 22 32 41100
8 6 12 10 0
3 seconds
512 megabytes
['bitmasks', 'dfs and similar', 'dp', 'greedy', 'trees', '*1900']
C. Card Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n cards stacked in a deck. Initially, a_{i} is written on the i-th card from the top. The value written on a card does not change.You will play a game. Initially your score is 0. In each turn, you can do one of the following operations: Choose an odd^{\dagger} positive integer i, which is not greater than the number of cards left in the deck. Remove the i-th card from the top of the deck and add the number written on the card to your score. The remaining cards will be reindexed starting from the top. Choose an even^{\ddagger} positive integer i, which is not greater than the number of cards left in the deck. Remove the i-th card from the top of the deck. The remaining cards will be reindexed starting from the top. End the game. You can end the game whenever you want, you do not have to remove all cards from the initial deck. What is the maximum score you can get when the game ends?^{\dagger} An integer i is odd, if there exists an integer k such that i = 2k + 1.^{\ddagger} An integer i is even, if there exists an integer k such that i = 2k.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 the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^{5}).The second line of each test case contains n integers a_1, a_2, \ldots, a_n (-10^{9} \le a_i \le 10^{9}).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^{5}.OutputFor each test case, print a single integer — the maximum score you can get when the game ends.ExampleInput 44-4 1 -3 541 -2 3 -43-1 3 -51-1Output 5 4 2 0 NoteIn the first test case, one can get the score of 5 as follows: In the first turn, choose i=2. Your score remains 0 and the numbers written on the cards from the top will become [-4, -3, 5]. In the second turn, choose i=3. Your score will become 5 and the numbers written on the cards from the top will become [-4, -3]. In the third turn, end the game with the score of 5. In the second test case, one can get the score of 4 as follows: In the first turn, choose i=3. Your score will become 3 and the numbers written on the cards from the top will become [1, -2, -4]. In the second turn, choose i=1. Your score will become 4 and the numbers written on the cards from the top will become [-2, -4]. In the third turn, end the game with the score of 4. In the third test case, one can get the score of 2 as follows: In the first turn, choose i=1. Your score will become -1 and the numbers written on the cards from the top will become [3, -5]. In the second turn, choose i=1. Your score will become 2 and the numbers written on the cards from the top will become [-5]. In the third turn, end the game with the score of 2.
44-4 1 -3 541 -2 3 -43-1 3 -51-1
5 4 2 0
2 seconds
256 megabytes
['brute force', 'greedy', '*1500']
B. Sets and Uniontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have n sets of integers S_{1}, S_{2}, \ldots, S_{n}. We call a set S attainable, if it is possible to choose some (possibly, none) of the sets S_{1}, S_{2}, \ldots, S_{n} so that S is equal to their union^{\dagger}. If you choose none of S_{1}, S_{2}, \ldots, S_{n}, their union is an empty set.Find the maximum number of elements in an attainable S such that S \neq S_{1} \cup S_{2} \cup \ldots \cup S_{n}.^{\dagger} The union of sets A_1, A_2, \ldots, A_k is defined as the set of elements present in at least one of these sets. It is denoted by A_1 \cup A_2 \cup \ldots \cup A_k. For example, \{2, 4, 6\} \cup \{2, 3\} \cup \{3, 6, 7\} = \{2, 3, 4, 6, 7\}.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 100). The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 50).The following n lines describe the sets S_1, S_2, \ldots, S_n. The i-th of these lines contains an integer k_{i} (1 \le k_{i} \le 50) — the number of elements in S_{i}, followed by k_{i} integers s_{i, 1}, s_{i, 2}, \ldots, s_{i, k_{i}} (1 \le s_{i, 1} < s_{i, 2} < \ldots < s_{i, k_{i}} \le 50) — the elements of S_{i}.OutputFor each test case, print a single integer — the maximum number of elements in an attainable S such that S \neq S_{1} \cup S_{2} \cup \ldots \cup S_{n}.ExampleInput 433 1 2 32 4 52 3 444 1 2 3 43 2 5 63 3 5 63 4 5 651 13 3 6 101 92 1 33 5 8 912 4 28Output 4 5 6 0 NoteIn the first test case, S = S_{1} \cup S_{3} = \{1, 2, 3, 4\} is the largest attainable set not equal to S_1 \cup S_2 \cup S_3 = \{1, 2, 3, 4, 5\}.In the second test case, we can pick S = S_{2} \cup S_{3} \cup S_{4} = \{2, 3, 4, 5, 6\}.In the third test case, we can pick S = S_{2} \cup S_{5} = S_{2} \cup S_{3} \cup S_{5} = \{3, 5, 6, 8, 9, 10\}.In the fourth test case, the only attainable set is S = \varnothing.
433 1 2 32 4 52 3 444 1 2 3 43 2 5 63 3 5 63 4 5 651 13 3 6 101 92 1 33 5 8 912 4 28
4 5 6 0
2 seconds
256 megabytes
['bitmasks', 'brute force', 'constructive algorithms', 'greedy', '*1300']
A. Increasing Sequencetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a sequence a_{1}, a_{2}, \ldots, a_{n}. A sequence b_{1}, b_{2}, \ldots, b_{n} is called good, if it satisfies all of the following conditions: b_{i} is a positive integer for i = 1, 2, \ldots, n; b_{i} \neq a_{i} for i = 1, 2, \ldots, n; b_{1} < b_{2} < \ldots < b_{n}. Find the minimum value of b_{n} among all good sequences b_{1}, b_{2}, \ldots, b_{n}.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 100). The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 100).The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^{9}).OutputFor each test case, print a single integer — the minimum value of b_{n} among all good sequences b.ExampleInput 351 3 2 6 742 3 4 511Output 8 4 2 NoteIn the first test case, b = [2, 4, 5, 7, 8] is a good sequence. It can be proved that there is no good b with b_{5} < 8.In the second test case, b = [1, 2, 3, 4] is an optimal good sequence.In the third test case, b = [2] is an optimal good sequence.
351 3 2 6 742 3 4 511
8 4 2
1 second
256 megabytes
['greedy', '*800']
G. Anya and the Mysterious Stringtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAnya received a string s of length n brought from Rome. The string s consists of lowercase Latin letters and at first glance does not raise any suspicions. An instruction was attached to the string.Start of the instruction.A palindrome is a string that reads the same from left to right and right to left. For example, the strings "anna", "aboba", "level" are palindromes, while the strings "gorilla", "banan", "off" are not.A substring [l \ldots r] of string s is a string s_l s_{l+1} \ldots s_{r-1} s_r. For example, the substring [4 \ldots 6] of the string "generation" is the string "era".A string is called beautiful if it does not contain a substring of length at least two that is a palindrome. For example, the strings "fox", "abcdef", and "yioy" are beautiful, while the strings "xyxx", "yikjkitrb" are not.When an integer x is added to the character s_i, it is replaced x times with the next character in the alphabet, with "z" being replaced by "a".When an integer x is added to the substring [l, r] of string s, it becomes the string s_1 s_2 \ldots s_{l-1} (s_l + x) (s_{l+1} + x) \ldots (s_{r-1} + x) (s_r + x) s_{r+1} \ldots s_n. For example, when the substring [2, 4] of the string "abazaba" is added with the number 6, the resulting string is "ahgfaba".End of the instruction.After reading the instruction, Anya resigned herself to the fact that she has to answer m queries. The queries can be of two types: Add the number x to the substring [l \ldots r] of string s. Determine whether the substring [l \ldots r] of string s is beautiful. InputThe first line contains an integer t (1 \le t \le 10^4) - the number of test cases.The descriptions of the test cases follow.The first line of each test case contains two integers n and m (1 \le n, m \le 2 \cdot 10^5) - the length of the string s and the number of queries.The second line of each test case contains the string s of length n, consisting of lowercase Latin letters.The next m lines contain the queries: 1 l r x (1 \le l \le r \le n, 1 \le x \le 10^9) - description of a query of the first type; 2 l r (1 \le l \le r \le n) - description of a query of the second type. It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 2 \cdot 10^5.OutputFor each query of the second type, output "YES" if the substring [l \ldots r] of string s is beautiful, otherwise output "NO".You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers).ExampleInput 512 8tedubcyyxopz2 2 51 4 8 22 1 71 3 5 401 9 11 31 10 10 92 4 102 10 1210 4ubnxwwgzjt2 4 102 10 101 6 10 82 7 711 3hntcxfxyhtu1 4 6 12 4 101 4 10 2113 2yxhlmzfhqctir1 5 9 31 8 13 152 3bp1 1 2 151 1 2 181 2 2 1000000000Output YES NO NO YES NO YES YES YES NoteIn the first test case of the first test, the following happens: tedubcyyxopz: the string edub is beautiful; tedubcyyxopz \to tedwdeaaxopz; tedwdeaaxopz: the string tedwdea is not beautiful as it contains the palindrome edwde; tedwdeaaxopz \to terkreaaxopz; terkreaaxopz \to terkreaaarsz; terkreaaarsz \to terkreaaaasz; terkreaaaasz: the string kreaaaa is not beautiful as it contains the palindrome aaaa; terkreaaaasz: the string asz is beautiful.
512 8tedubcyyxopz2 2 51 4 8 22 1 71 3 5 401 9 11 31 10 10 92 4 102 10 1210 4ubnxwwgzjt2 4 102 10 101 6 10 82 7 711 3hntcxfxyhtu1 4 6 12 4 101 4 10 2113 2yxhlmzfhqctir1 5 9 31 8 13 152 3bp1 1 2 151 1 2 181 2 2 1000000000
YES NO NO YES NO YES YES YES
3 seconds
256 megabytes
['binary search', 'data structures', '*2000']
F. Minimum Maximum Distancetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a tree with n vertices, some of which are marked. A tree is a connected undirected graph without cycles.Let f_i denote the maximum distance from vertex i to any of the marked vertices.Your task is to find the minimum value of f_i among all vertices. For example, in the tree shown in the example, vertices 2, 6, and 7 are marked. Then the array f(i) = [2, 3, 2, 4, 4, 3, 3]. The minimum f_i is for vertices 1 and 3.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 k \le n \le 2 \cdot 10^5) — the number of vertices in the tree and the number of marked vertices, respectively.The second line of each test case contains k integers a_i (1 \le a_i \le n, a_{i-1} < a_i) — the indices of the marked vertices.The next n - 1 lines contain two integers u_i and v_i — the indices of vertices connected by the i-th edge.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the minimum value of f_i among all vertices.ExamplesInput 67 32 6 71 21 32 42 53 63 74 41 2 3 41 22 33 45 111 21 31 41 55 24 51 22 31 44 510 81 2 3 4 5 8 9 102 1010 55 33 11 77 44 98 96 110 91 2 4 5 6 7 8 9 101 33 99 44 1010 66 77 22 55 8Output 2 2 0 1 4 5 Input 36 131 21 33 43 52 65 31 2 51 21 32 43 57 123 22 66 15 67 64 5Output 0 2 0
67 32 6 71 21 32 42 53 63 74 41 2 3 41 22 33 45 111 21 31 41 55 24 51 22 31 44 510 81 2 3 4 5 8 9 102 1010 55 33 11 77 44 98 96 110 91 2 4 5 6 7 8 9 101 33 99 44 1010 66 77 22 55 8
2 2 0 1 4 5
2 seconds
256 megabytes
['dfs and similar', 'dp', 'graphs', 'shortest paths', 'trees', '*1700']
E. Block Sequencetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven a sequence of integers a of length n.A sequence is called beautiful if it has the form of a series of blocks, each starting with its length, i.e., first comes the length of the block, and then its elements. For example, the sequences [\color{red}{3},\ \color{red}{3},\ \color{red}{4},\ \color{red}{5},\ \color{green}{2},\ \color{green}{6},\ \color{green}{1}] and [\color{red}{1},\ \color{red}{8},\ \color{green}{4},\ \color{green}{5},\ \color{green}{2},\ \color{green}{6},\ \color{green}{1}] are beautiful (different blocks are colored differently), while [1], [1,\ 4,\ 3], [3,\ 2,\ 1] are not.In one operation, you can remove any element from the sequence. What is the minimum number of operations required to make the given sequence beautiful?InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then follows the descriptions of the test cases.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^5) — the length of the sequence a.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 the sequence 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 number — the minimum number of deletions required to make the sequence a beautiful.ExampleInput 773 3 4 5 2 6 145 6 3 263 4 1 6 7 731 4 351 2 3 4 551 2 3 1 254 5 5 1 5Output 0 4 1 1 2 1 0 NoteIn the first test case of the example, the given sequence is already beautiful, as shown in the statement.In the second test case of the example, the sequence can only be made beautiful by removing all elements from it.In the fifth test case of the example, the sequence can be made beautiful by removing the first and last elements. Then the sequence will become [2,\ 3,\ 4].
773 3 4 5 2 6 145 6 3 263 4 1 6 7 731 4 351 2 3 4 551 2 3 1 254 5 5 1 5
0 4 1 1 2 1 0
2 seconds
256 megabytes
['dp', '*1500']
D. Divide and Equalizetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a consisting of n positive integers. You can perform the following operation on it: Choose a pair of elements a_i and a_j (1 \le i, j \le n and i \neq j); Choose one of the divisors of the integer a_i, i.e., an integer x such that a_i \bmod x = 0; Replace a_i with \frac{a_i}{x} and a_j with a_j \cdot x. Determine whether it is possible to make all elements in the array the same by applying the operation a certain number of times (possibly zero).For example, let's consider the array a = [100, 2, 50, 10, 1] with 5 elements. Perform two operations on it: Choose a_3 = 50 and a_2 = 2, x = 5. Replace a_3 with \frac{a_3}{x} = \frac{50}{5} = 10, and a_2 with a_2 \cdot x = 2 \cdot 5 = 10. The resulting array is a = [100, 10, 10, 10, 1]; Choose a_1 = 100 and a_5 = 1, x = 10. Replace a_1 with \frac{a_1}{x} = \frac{100}{10} = 10, and a_5 with a_5 \cdot x = 1 \cdot 10 = 10. The resulting array is a = [10, 10, 10, 10, 10]. After performing these operations, all elements in the array a become equal to 10.InputThe first line of the input contains a single integer t (1 \le t \le 2000) — the number of test cases.Then follows the description of each test case.The first line of each test case contains a single integer n (1 \le n \le 10^4) — the number of elements in the array a.The second line of each test case contains exactly n integers a_i (1 \le a_i \le 10^6) — the elements of the array a.It is guaranteed that the sum of n over all test cases does not exceed 10^4.OutputFor each test case, output a single line: "YES" if it is possible to make all elements in the array equal by applying the operation a certain (possibly zero) number of times; "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer).ExampleInput 75100 2 50 10 131 1 148 2 4 2430 50 27 20275 4024 432 3 1Output YES YES NO YES NO YES NO NoteThe first test case is explained in the problem statement.
75100 2 50 10 131 1 148 2 4 2430 50 27 20275 4024 432 3 1
YES YES NO YES NO YES NO
2 seconds
256 megabytes
['math', 'number theory', '*1300']
C. Perfect Squaretime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKristina has a matrix of size n by n, filled with lowercase Latin letters. The value of n is even.She wants to change some characters so that her matrix becomes a perfect square. A matrix is called a perfect square if it remains unchanged when rotated 90^\circ clockwise once.Here is an example of rotating a matrix by 90^\circ: In one operation, Kristina can choose any cell and replace its value with the next character in the alphabet. If the character is equal to "z", its value does not change.Find the minimum number of operations required to make the matrix a perfect square.For example, if the 4 by 4 matrix looks like this:\matrix{ a & b & b & a \cr b & c & \textbf{b} & b \cr b & c & c & b\cr a & b & b & a \cr }then it is enough to apply 1 operation to the letter b, highlighted in bold.InputThe first line of the input contains a single integer t (1 \le t \le 10^2) — the number of test cases.Then follows the description of each test case.The first line of each test case contains a single even integer n (2 \le n \le 10^3) — the number of rows and columns in the matrix.Then follows n lines, each containing exactly n lowercase Latin letters.It is guaranteed that the sum of n over all test cases does not exceed 10^3.OutputFor each test case, output a single number on a separate line: the minimum number of operations required for Kristina to obtain a perfect square.ExampleInput 54abbabcbbbccbabba2abba6codeforcescodeforcescodeforcescodefo4baaaabbabababaab4bbaaabbaaabaabbaOutput 1 2 181 5 9 NoteThe first test case is explained in the problem statement.
54abbabcbbbccbabba2abba6codeforcescodeforcescodeforcescodefo4baaaabbabababaab4bbaaabbaaabaabba
1 2 181 5 9
2 seconds
256 megabytes
['brute force', 'implementation', '*1200']
B. Three Threadletstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputOnce upon a time, bartender Decim found three threadlets and a pair of scissors.In one operation, Decim chooses any threadlet and cuts it into two threadlets, whose lengths are positive integers and their sum is equal to the length of the threadlet being cut.For example, he can cut a threadlet of length 5 into threadlets of lengths 2 and 3, but he cannot cut it into threadlets of lengths 2.5 and 2.5, or lengths 0 and 5, or lengths 3 and 4.Decim can perform at most three operations. He is allowed to cut the threadlets obtained from previous cuts. Will he be able to make all the threadlets of equal length?InputThe first line contains an integer t (1 \le t \le 10^4) — the number of test cases. Then follows the description of each test case.In a single line of each test case, there are three integers a, b, c (1 \le a, b, c \le 10^9) — the lengths of the threadlets.OutputFor each test case, output "YES" if it is possible to make all the threadlets of equal length by performing at most three operations, otherwise output "NO".You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).ExampleInput 151 3 25 5 56 36 127 8 76 3 34 4 1212 6 81000000000 1000000000 10000000003 7 19 9 19 3 62 8 25 3 108 4 82 8 4Output YES YES NO NO YES YES NO YES NO NO YES YES NO YES NO NoteLet's consider some testcases of the first test.In the first testcase, you can apply following operations:1, 3, 2 \to 1, 2, 1, 2 \to 1, 1, 1, 1, 2 \to 1, 1, 1, 1, 1, 1.In the second testcase, you can do nothing, the threadlets are already of equal length.In the third testcase, it isn't possible to make threadlets of equal length.
151 3 25 5 56 36 127 8 76 3 34 4 1212 6 81000000000 1000000000 10000000003 7 19 9 19 3 62 8 25 3 108 4 82 8 4
YES YES NO NO YES YES NO YES NO NO YES YES NO YES NO
2 seconds
256 megabytes
['math', '*900']
A. Don't Try to Counttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven a string x of length n and a string s of length m (n \cdot m \le 25), consisting of lowercase Latin letters, you can apply any number of operations to the string x.In one operation, you append the current value of x to the end of the string x. Note that the value of x will change after this.For example, if x ="aba", then after applying operations, x will change as follows: "aba" \rightarrow "abaaba" \rightarrow "abaabaabaaba".After what minimum number of operations s will appear in x as a substring? A substring of a string is defined as a contiguous segment of it.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains two numbers n and m (1 \le n \cdot m \le 25) — the lengths of strings x and s, respectively.The second line of each test case contains the string x of length n.The third line of each test case contains the string s of length m.OutputFor each test case, output a single number — the minimum number of operations after which s will appear in x as a substring. If this is not possible, output -1.ExampleInput 121 5aaaaaa5 5eforcforce2 5abababa3 5abaababa4 3babbbbb5 1aaaaaa4 2aabbba2 8bkkbkbkbkb12 2fjdgmujlconttf2 2aaaa3 5abbbabba1 19mmmmmmmmmmmmmmmmmmmmOutput 3 1 2 -1 1 0 1 3 1 0 2 5 NoteIn the first test case of the example, after 2 operations, the string will become "aaaa", and after 3 operations, it will become "aaaaaaaa", so the answer is 3.In the second test case of the example, after applying 1 operation, the string will become "\text{e}\color{red}{\text{force}}\text{forc}", where the substring is highlighted in red.In the fourth test case of the example, it can be shown that it is impossible to obtain the desired string as a substring.
121 5aaaaaa5 5eforcforce2 5abababa3 5abaababa4 3babbbbb5 1aaaaaa4 2aabbba2 8bkkbkbkbkb12 2fjdgmujlconttf2 2aaaa3 5abbbabba1 19mmmmmmmmmmmmmmmmmmmm
3 1 2 -1 1 0 1 3 1 0 2 5
2 seconds
256 megabytes
['brute force', 'strings', '*800']
F. Last Man Standingtime limit per test7 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are n heroes in a videogame. Each hero has some health value h and initial armor value a. Let the current value of armor be a_{\mathit{cur}}, initially equal to a.When x points of damage are inflicted on a hero, the following happens: if x < a_{\mathit{cur}}, then x gets subtracted from a_{\mathit{cur}}; otherwise, 1 gets subtracted from h and a_{\mathit{cur}} gets assigned back to a.In the start of the game, you choose the value x (an integer strictly greater than 0, arbitrarily large). Then you keep attacking all heroes in rounds: in one round, you inflict x points of damage to all alive heroes. A hero dies when his health becomes 0. The game ends when all heroes are dead.The last hero to die earns the number of points, equal to the number of rounds he was the only hero alive. The other heroes get 0 points. In particular, if the last round ends with multiple heroes dying, then every hero gets 0 points.The game is played for every possible x (from 1 to infinity). The points are reset between the games. What's the maximum number of points each hero has had?InputThe first line contains a single integer t (1 \le t \le 10) — the number of testcases.The first line of each testcase contains a single integer n (1 \le n \le 2 \cdot 10^5) — the number of heroes.The second line contains n integers h_1, h_2, \dots, h_n (1 \le h_i \le 2 \cdot 10^5) — the health value of each hero.The third line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 2 \cdot 10^5) — the initial armor value of each hero.OutputFor each testcase, print n integers — the maximum number of points each hero has had over the games played for every possible x.ExampleInput 333 1 23 11 5110020045 9 5 19 2 9 10Output 1 1 1 20000 0 4 0 0 NoteIn the first testcase, the game for x = 1 is played as follows: before all rounds: the heroes have h = [3, 1, 2], a_{\mathit{cur}} = [3, 11, 5]; round 1: 1 damage is inflicted on every hero: h remains [3, 1, 2], a_{\mathit{cur}} becomes [2, 10, 4]; round 2: h = [3, 1, 2], a_{\mathit{cur}} = [1, 9, 3]; round 3: the first hero runs out of armor, so he loses a health point: h = [2, 1, 2], a_{\mathit{cur}} = [3, 8, 2]; ... round 9: the first hero dies, since his health reaches 0: h = [0, 1, 1], a_{\mathit{cur}} = [0, 2, 1]; round 10: the third hero dies: h = [0, 1, 0], a_{\mathit{cur}} = [0, 1, 0]; round 11: the second hero dies: h = [0, 0, 0], a_{\mathit{cur}} = [0, 0, 0]. The second hero was the last hero to die, and he was the only hero alive during one round. Thus, he gets 1 point for that game.The game for x = 4 is played as follows: round 1: h = [2, 1, 2], a_{\mathit{cur}} = [3, 7, 1]; round 2: h = [1, 1, 1], a_{\mathit{cur}} = [3, 3, 5]; round 3: h = [0, 0, 1], a_{\mathit{cur}} = [0, 0, 1]; round 4: h = [0, 0, 0], a_{\mathit{cur}} = [0, 0, 0]; The third hero was the last hero to die, and he was the only hero alive during one round.
333 1 23 11 5110020045 9 5 19 2 9 10
1 1 1 20000 0 4 0 0
7 seconds
512 megabytes
['brute force', 'data structures', 'number theory', '*2800']
E. Interactive Game with Coloringtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.You are given a tree on n vertices; vertex 1 is the root of the tree. For every i \in [2, n], the parent of the i-th vertex is p_i, and p_i < i.You have to color all edges of the tree using the minimum possible number of colors such that you can win the game on that tree (every edge should be painted into exactly one color).The game we're going to play will be conducted as follows. After you paint the edges and print their colors, the jury will place a chip into one of the vertices of the tree (except for the root). Your goal is to move this chip to the root in exactly d moves, where d is the distance from the root to that vertex (the distance is equal to the number of edges on the path). If the chip reaches the root in d moves, you win. Otherwise, you lose.The jury won't tell you where the chip is located. You won't even know the value of d in advance. However, at the start of each move, you will be told how many edges of each color are incident to the current vertex (this includes both the edge leading up the tree and the edges leading away from the root). You have to choose one of these colors, and the chip will be moved along the edge of the chosen color (if there are multiple edges with that color incident to the current vertex, the jury gets to choose one of them). After the chip is moved, you will be told the same information about the current vertex again, and the game continues, until you either reach the root, or you make d moves without reaching the root.The interactor for this problem is adaptive. It means that both the starting vertex and the current vertex are not fixed and may change "on the run" depending on the output of your program. However, the state of the game will always be consistent with the information you are given: there will always be at least one starting vertex and at least one path of your chip from that vertex consistent with both the information about the colors you receive and the colors you've chosen during the moves.InputThe first line contains one integer n (3 \le n \le 100) — the number of vertices in the tree.The second line contains n-1 integers p_2, p_3, \dots, p_n (1 \le p_i < i), where p_i is the parent of the i-th vertex in the tree.InteractionFirst, you have to print the coloring of the edges you've chosen as follows: in the first line, print one integer k (1 \le k \le n - 1) — the number of colors you are using; in the second line, print n-1 integers c_2, c_3, \dots, c_n (1 \le c_i \le k), where c_i should be the color of the edge connecting p_i with i. Then, the game begins. At the start of each move, the jury program prints one of the following: one integer 1 on a separate line, indicating that the chip has reached the root and you won; one integer -1 on a separate line, indicating that either you didn't reach the root in d moves and lost, or you have done something incorrect (either the coloring you provided didn't meet the constraints, or your previous move is impossible); or one integer 0 on a separate line, followed by a line containing k integers e_1, e_2, \dots, e_k, where e_i is the number of edges with color i incident to the current vertex. If you receive 1 or -1, your program should terminate immediately, otherwise the verdict for your submission may be undefined. If you receive 0 followed by k integers e_1, e_2, \dots, e_k, you have to print one integer indicating the color you choose during the move (of course, e_i for that color should not be equal to 0).Don't forget to flush the output every time you print something!ExamplesInput 5 1 1 1 1 0 1 1Output 1 1 1 1 1 1 Input 4 1 2 3 0 0 1 0 0 1 1 0 0 1 0 1 1Output 3 3 1 2 2 1 3 Input 3 1 2 0 1 1 1 Output 2 1 2 1NoteIn the first example, every vertex from 2 to n is connected to the root. So, we can paint all edges into the same color 1, and when the game starts, there will be only one edge incident to the current vertex (and it will lead to the root).In the second example, the tree is a path of 4 vertices. We have to paint its edges into different colors, because it can be shown that we don't have a winning strategy with just two colors.
5 1 1 1 1 0 1 1
1 1 1 1 1 1
2 seconds
512 megabytes
['brute force', 'constructive algorithms', 'dfs and similar', 'graphs', 'implementation', 'interactive', 'trees', '*2400']
D. Sum of XOR Functionstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of length n consisting of non-negative integers.You have to calculate the value of \sum_{l=1}^{n} \sum_{r=l}^{n} f(l, r) \cdot (r - l + 1), where f(l, r) is a_l \oplus a_{l+1} \oplus \dots \oplus a_{r-1} \oplus a_r (the character \oplus denotes bitwise XOR).Since the answer can be very large, print it modulo 998244353.InputThe first line contains one integer n (1 \le n \le 3 \cdot 10^5) — the length of the array a.The second line contains n integers a_1, a_2, \dots, a_n (0 \le a_i \le 10^9).OutputPrint the one integer — the value of \sum_{l=1}^{n} \sum_{r=l}^{n} f(l, r) \cdot (r - l + 1), taken modulo 998244353.ExamplesInput 3 1 3 2 Output 12 Input 4 39 68 31 80 Output 1337 Input 7 313539461 779847196 221612534 488613315 633203958 394620685 761188160 Output 257421502 NoteIn the first example, the answer is equal to f(1, 1) + 2 \cdot f(1, 2) + 3 \cdot f(1, 3) + f(2, 2) + 2 \cdot f(2, 3) + f(3, 3) = = 1 + 2 \cdot 2 + 3 \cdot 0 + 3 + 2 \cdot 1 + 2 = 12.
3 1 3 2
12
2 seconds
256 megabytes
['bitmasks', 'combinatorics', 'divide and conquer', 'dp', 'math', '*1700']
C. Make it Alternatingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a binary string s. A binary string is a string consisting of characters 0 and/or 1.You can perform the following operation on s any number of times (even zero): choose an integer i such that 1 \le i \le |s|, then erase the character s_i. You have to make s alternating, i. e. after you perform the operations, every two adjacent characters in s should be different.Your goal is to calculate two values: the minimum number of operations required to make s alternating; the number of different shortest sequences of operations that make s alternating. Two sequences of operations are different if in at least one operation, the chosen integer i is different in these two sequences. InputThe first line contains one integer t (1 \le t \le 10^4) — the number of test cases.Each test case consists of one line containing the string s (1 \le |s| \le 2 \cdot 10^5). The string s consists of characters 0 and/or 1 only.Additional constraint on the input: the total length of strings s over all test cases does not exceed 2 \cdot 10^5. OutputFor each test case, print two integers: the minimum number of operations you have to perform, and the number of different shortest sequences of operations. Since the second number might be large, print its remainder modulo 998244353.ExampleInput 3100101110101Output 1 2 2 6 0 1 NoteIn the first test case of the example, the shortest sequences of operations are: [2] (delete the 2-nd character); [3] (delete the 3-rd character). In the second test case of the example, the shortest sequences of operations are: [2, 1] (delete the 2-nd character, then delete the 1-st character); [2, 2]; [1, 1]; [1, 2]; [3, 1]; [3, 2]. In the third test case of the example, the only shortest sequence of operations is [] (empty sequence).
3100101110101
1 2 2 6 0 1
2 seconds
256 megabytes
['combinatorics', 'dp', 'greedy', '*1300']
B. Chips on the Boardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a board of size n \times n (n rows and n colums) and two arrays of positive integers a and b of size n.Your task is to place the chips on this board so that the following condition is satisfied for every cell (i, j): there exists at least one chip in the same column or in the same row as the cell (i, j). I. e. there exists a cell (x, y) such that there is a chip in that cell, and either x = i or y = j (or both). The cost of putting a chip in the cell (i, j) is equal to a_i + b_j. For example, for n=3, a=[1, 4, 1] and b=[3, 2, 2]. One of the possible chip placements is as follows: White squares are empty The total cost of that placement is (1+3) + (1+2) + (1+2) = 10.Calculate the minimum possible total cost of putting chips according to the rules above.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 10^9).The third line contains n integers b_1, b_2, \dots, b_n (1 \le b_i \le 10^9).The sum of n over all test cases doesn't exceed 3 \cdot 10^5.OutputFor each test case, print a single integer — the minimum possible total cost of putting chips according to the rules.ExampleInput 431 4 13 2 214524 52 355 2 4 5 33 4 2 1 5Output 10 9 13 24 NoteThe first test case of the example is described in the statement.
431 4 13 2 214524 52 355 2 4 5 33 4 2 1 5
10 9 13 24
2 seconds
256 megabytes
['constructive algorithms', 'greedy', '*900']
A. Rigged!time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp organizes a weightlifting competition. There are n athletes participating in the competition, the i-th athlete has strength s_i and endurance e_i. The 1-st athlete is Monocarp's friend Polycarp, and Monocarp really wants Polycarp to win.The competition will be conducted as follows. The jury will choose a positive (greater than zero) integer w, which denotes the weight of the barbell that will be used in the competition. The goal for each athlete is to lift the barbell as many times as possible. The athlete who lifts the barbell the most amount of times will be declared the winner (if there are multiple such athletes — there's no winner).If the barbell's weight w is strictly greater than the strength of the i-th athlete s_i, then the i-th athlete will be unable to lift the barbell even one single time. Otherwise, the i-th athlete will be able to lift the barbell, and the number of times he does it will be equal to his endurance e_i.For example, suppose there are 4 athletes with parameters s_1 = 7, e_1 = 4; s_2 = 9, e_2 = 3; s_3 = 4, e_3 = 6; s_4 = 2, e_4 = 2. If the weight of the barbell is 5, then: the first athlete will be able to lift the barbell 4 times; the second athlete will be able to lift the barbell 3 times; the third athlete will be unable to lift the barbell; the fourth athlete will be unable to lift the barbell. Monocarp wants to choose w in such a way that Polycarp (the 1-st athlete) wins the competition. Help him to choose the value of w, or report that it is impossible.InputThe first line contains one integer t (1 \le t \le 100) — the number of test cases.The first line of each test case contains one integer n (2 \le n \le 100) — the number of athletes. Then n lines follow, the i-th of them contains two integers s_i and e_i (1 \le s_i \le 10^9; 1 \le e_i \le 100) — the strength and the endurance of the i-th athlete.OutputFor each test case, print the answer as follows: if the answer exists, print one integer — the value of w meeting the constraints. The integer you print should satisfy 1 \le w \le 10^9. It can be shown that if the answer exists, at least one such value of w exists as well. If there are multiple answers, you can print any of them; otherwise, print one integer -1. ExampleInput 347 49 34 62 224 6100 10021337 31337 3Output 5 -1 -1 NoteThe first test case of the example is described in the statement.
347 49 34 62 224 6100 10021337 31337 3
5 -1 -1
2 seconds
256 megabytes
['greedy', '*800']
G. wxhtzdy ORO Treetime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter (finally) qualifying for the IOI 2023, wxhtzdy was very happy, so he decided to do what most competitive programmers do: trying to guess the problems that will be on IOI. During this process, he accidentally made a problem, which he thought was really cool.You are given a tree (a connected acyclic graph) with n vertices and n-1 edges. Vertex i (1 \le i \le n) has a value a_i. Lets' define g(u, v) as the bitwise or of the values of all vertices on the shortest path from u to v. For example, let's say that we want to calculate g(3, 4), on the tree from the first test case in the example. On the path from 3 to 4 are vertices 3, 1, 4. Then, g(3, 4) = a_3 \ | \ a_1 \ | \ a_4 (here, | represents the bitwise OR operation).Also, you are given q queries, and each query looks like this:You are given x and y. Let's consider all vertices z such that z is on the shortest path from x to y (inclusive).Lets define the niceness of a vertex z as the sum of the number of non-zero bits in g(x, z) and the number of non-zero bits in g(y, z). You need to find the maximum niceness among all vertices z on the shortest path from x to y.Since his brain is really tired after solving an output only problem on SIO (he had to do it to qualify for the IOI), he wants your help with this problem.InputThe first line of input 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 2 \cdot 10^5) — the number of vertices.The second line of each test case contains n positive integers a_1, a_2, \dots, a_n (1 \le a_v \le 10^9) — the value of each vertex, the i-th integer in this line corresponds to the vertex i.Following n - 1 lines are the description of a tree.Each line contains two integers u and v (1 \le u, v \le n, u \ne v) — indicating that vertices u and v are connected by an edge.The next line contains a single integer q (1 \le q \le 10^5) — number of queries.Following q lines contain 2 integers x, y (1 \le x, y \le n) — the vertices x and y for each query.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.It is guaranteed that the sum of q over all test cases does not exceed 10^5.OutputFor each test case output q integers, each of which is the answer to the corresponding query.ExamplesInput 341 2 3 41 31 21 431 11 31 437 6 33 12 141 11 21 32 31411 1Output 2 4 3 6 6 6 6 2 Input 374 7 7 4 10 8 106 13 12 17 41 54 247 52 34 52 569 5 6 2 4 65 12 11 64 31 346 11 44 33 575 1 3 7 5 1 62 15 42 33 47 66 324 27 7Output 8 6 7 7 6 6 4 7 6 4 Input 176 8 7 2 5 8 72 13 24 34 64 56 741 56 74 51 4Output 7 7 5 7 NoteThe image below shows the tree from the second example, first test case. Tree from the second example, first test case In the first query, we have x=7, y=5. The shortest path from 7 to 5 is 7-4-2-1-5. Let's calculate the niceness of vertex 7 on this path. We have g(7,7)=a_7=10=(1010)_2 and g(5,7)=a_5 \ | \ a_1 \ | \ a_2 \ | \ a_4 \ | \ a_7=10 \ | \ 4 \ | \ 7 \ | \ 4 \ | \ 10=15=(1111)_2, so its niceness is equal to 2 + 4 = 6.Now let's calculate the niceness of vertex 4 on this path. We have g(7,4)=a_7 \ | \ a_4=10 \ | \ 4=14=(1110)_2 and g(5,4)=a_5 \ | \ a_1 \ | \ a_2 \ | \ a_4=10 \ | \ 4 \ | \ 7 \ | \ 4=15=(1111)_2, so its niceness is equal to 3 + 4 = 7.Now let's calculate the niceness of vertex 2 on this path. We have g(7,2)=a_7 \ | \ a_4 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2 and g(5,2)=a_5 \ | \ a_1 \ | \ a_2=10 \ | \ 4 \ | \ 7=15=(1111)_2, so its niceness is equal to 4 + 4 = 8.Now let's calculate the niceness of vertex 1 on this path. We have g(7,1)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1=10 \ | \ 4 \ | \ 7 \ | \ 4=15=(1111)_2 and g(5,1)=a_5 \ | \ a_1=10 \ | \ 4=14=(1110)_2, so its niceness is equal to 4 + 3 = 7.Finally, let's calculate the niceness of vertex 5 on this path. We have g(7,5)=a_7 \ | \ a_4 \ | \ a_2 \ | \ a_1 \ | \ a_5=10 \ | \ 4 \ | \ 7 \ | \ 4 \ | \ 10=15=(1111)_2 and g(5,5)=a_5=10=(1010)_2, so its niceness is equal to 4 + 2 = 6.The maximum niceness on this path is at vertex 2, and it is 8.
341 2 3 41 31 21 431 11 31 437 6 33 12 141 11 21 32 31411 1
2 4 3 6 6 6 6 2
5 seconds
256 megabytes
['binary search', 'bitmasks', 'brute force', 'data structures', 'dfs and similar', 'implementation', 'trees', '*2300']
F. Vasilije Loves Number Theorytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasilije is a smart student and his discrete mathematics teacher Sonja taught him number theory very well.He gave Ognjen a positive integer n.Denote d(n) as the number of positive integer divisors of n, and denote gcd(a, b) as the largest integer g such that a is divisible by g and b is divisible by g.After that, he gave Ognjen q queries, and there are 2 types of queries. 1, x — set n to n \cdot x, and then answer the following question: does there exist a positive integer a such that gcd(a, n) = 1, and d(n \cdot a) = n? 2 — reset n to its initial value (before any queries). Note that n does not get back to its initial value after the type 1 query.Since Ognjen is afraid of number theory, Vasilije promised him that after each query, d(n) \le 10^9, however, even with that constraint, he still needs your help with this problem.InputThe first line contains a positive integer t, (1 \le t \le 100) — the number of test cases.The first line of each test case contains 2 integers, n and q (1 \le n \le 10^{6}, 1\le q \le 1000) — the number n and the number of queries.The following q lines contain an integer k (1 \le k \le 2), if k=1 then there is another integer in this line x (1 \le x \le 10^6) — the description of the queries.It is guaranteed that, for the given input, d(n) does not exceed 10^9 at any point.It is guaranteed that the sum of q over all test cases doesn't exceed 10^3.OutputFor each type 1 query, you should output "YES" if there exist such positive integer a that gcd(a, n) = 1 and d(n \cdot a)=n, and "NO" if he can't.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).ExampleInput 71 51 11 221 81 920 41 321 71 1216 101 61 61 101 91 11 91 71 31 21 109 11 38 11 28 31 51 81 1011 51 81 21 11 31 1Output YES YES YES YES YES NO YES YES NO YES YES YES NO YES NO YES YES NO NO YES NO NO YES NO NO NO NO NoteIn the first test case, we initially have n=1.After the first query: n=1, d(n)=1, so by taking a = 1, d(n \cdot a) = n, and the answer to this query is "YES".After the second query: n=2, d(n)=2, we can, again, take a = 1, d(n \cdot a) = n, and the answer to this query is "YES".After the third query n=1, and this is a type 2 query so we don't answer it.After the fourth query: n=8, and by taking a=3, d(n \cdot a) = d(24) = 8 = n, so the answer is "YES".After the fifth query: n=72, now we can take a=637 to get n \cdot a = 45864, and d(n \cdot a) = 72 = n, so the answer is "YES".In the second test case, we initially have n=20.After the first query: n=60, and the answer is "YES".After the second query: n=20, this is a type 2 query, so we don't answer it.After the third query: n=140, and it can be proven that no matter which positive integer a we take, d(n \cdot a) will never be equal to n, so the answer to this query is "NO".After the fourth query: n=1680. It can be proven that there exists a positive integer a, such that d(n \cdot a) = n, so the answer is "YES".
71 51 11 221 81 920 41 321 71 1216 101 61 61 101 91 11 91 71 31 21 109 11 38 11 28 31 51 81 1011 51 81 21 11 31 1
YES YES YES YES YES NO YES YES NO YES YES YES NO YES NO YES YES NO NO YES NO NO YES NO NO NO NO
2 seconds
256 megabytes
['brute force', 'math', 'number theory', '*1900']
E. Iva & Pavtime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIva and Pav are a famous Serbian competitive programming couple. In Serbia, they call Pav "papuca" and that's why he will make all of Iva's wishes come true.Iva gave Pav an array a of n elements.Let's define f(l, r) = a_l \ \& \ a_{l+1} \ \& \dots \& \ a_r (here \& denotes the bitwise AND operation). Note that f(l, r) is not defined when l>r.Iva also gave Pav q queries.Each query consists of 2 numbers, k and l, and she wants Pav to find the largest index r (l \le r \le n), such that f(l, r) \ge k. Pav wants to solve this problem fast because he doesn't want to upset Iva. He needs your help.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 2 \cdot 10^5) — the length of array a.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 elements of array a.The third line of each test case contains a single integer q (1 \le q \le 10^5) — the number of queries Iva gave Pav.The next q lines of each test case contains two numbers, l and k (1 \le l \le n, 1 \le k \le 10^9) — the left bound for the subsegment, and the integer k described in statement.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5. Also, it is guaranteed that the sum of q over all test cases does not exceed 2 \cdot 10^5.OutputFor each query output maximal index r (l \le r \le n) such that a_l \ \& \ a_{l+1} \ \& \dots \& \ a_r \ \ge \ k.If such r doesn't exist, output -1.ExampleInput 3515 14 17 42 3431 72 154 557 5 3 1 741 75 72 32 2719 20 15 12 21 7 1141 154 47 125 7Output 2 -1 5 1 5 2 2 2 6 -1 5 NoteIn the first test case n=5, and the array a = [15, 14, 17, 42, 34]The first query asks for the biggest index r such that the f(1, r) \ge 7.f(1,1) = 15, \ f(1, 2) = 14, \ f(1,3)=0 \ f(1, 4)=0 \ f(1, 5)=0, so r=2 is the answer.The second query asks for f(2, r) \ge 15. Since such r doesn't exist, the answer is -1.The third query asks for f(4, r) \ge 5. f(4, 4) = 42, \ f(4, 5) = 34, so r=5 is the answer.In the second test case n=5, and the array a= [7, 5, 3, 1, 7].For the first query, f(1, r) \ge 7.f(1, 1)=7, \ f(1, 2)=5, \ f(1, 3) = 1, \ f(1,4) = 1, \ f(1, 5)=1, so the answer to this query is 1.For the second query, f(5, r) \ge 7.f(5, 5) = 7, so the answer is 5.For the third query, f(2, r) \ge 3.f(2, 2) = 5, \ f(2, 3) = 1, \ f(2, 4) = 1, \ f(2, 5) = 1, so the answer is 2.
3515 14 17 42 3431 72 154 557 5 3 1 741 75 72 32 2719 20 15 12 21 7 1141 154 47 125 7
2 -1 5 1 5 2 2 2 6 -1 5
5 seconds
256 megabytes
['binary search', 'bitmasks', 'data structures', 'greedy', '*1400']
D. Reverse Madnesstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s of length n, containing lowercase Latin letters. Next you will be given a positive integer k and two arrays, l and r of length k.It is guaranteed that the following conditions hold for these 2 arrays: l_1 = 1; r_k = n; l_i \le r_i, for each positive integer i such that 1 \le i \le k; l_i = r_{i-1}+1, for each positive integer i such that 2 \le i \le k; Now you will be given a positive integer q which represents the number of modifications you need to do on s.Each modification is defined with one positive integer x: Find an index i such that l_i \le x \le r_i (notice that such i is unique). Let a=\min(x, r_i+l_i-x) and let b=\max(x, r_i+l_i-x). Reverse the substring of s from index a to index b. Reversing the substring [a, b] of a string s means to make s equal to s_1, s_2, \dots, s_{a-1},\ s_b, s_{b-1}, \dots, s_{a+1}, s_a,\ s_{b+1}, s_{b+2}, \dots, s_{n-1}, s_n.Print s after the last modification is finished.InputEach test contains multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. Description of the test cases follows.The first line of each test case contains two integers n and k (1 \le k \le n \le 2\cdot 10^5) — the length of the string s, and the length of arrays l and r.The second line of each test case contains the string s ( |s| = n) containing lowercase Latin letters — the initial string.The third line of each test case contains k positive integers l_1, l_2, \dots, l_k (1 \le l_i \le n) — the array l.The fourth line of each test case contains k positive integers r_1, r_2, \dots, r_k (1 \le r_i \le n) — the array r.The fifth line of each test case contains a positive integer q (1 \le q \le 2 \cdot 10^5 ) — the number of modifications you need to do to s.The sixth line of each test case contains q positive integers x_1, x_2, \dots, x_q (1\le x_i \le n) — the description of the modifications.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot10^5. It is guaranteed that the sum of q over all test cases does not exceed 2\cdot10^5.It is guaranteed that the conditions in the statement hold for the arrays l and r.OutputFor each test case, in a new line, output the string s after the last modification is done.ExampleInput 54 2abcd1 32 421 35 3abcde1 2 31 2 531 2 33 1gaf1322 210 1aghcdegdij11051 2 3 4 21 1a1111Output badc abedc gaf jihgedcdga a NoteIn the first test case:The initial string is "abcd". In the first modification, we have x=1. Since l_1=1\leq x \leq r_1=2, we find the index i = 1. We reverse the substring from index x=1 to l_1+r_1-x=1+2-1=2. After this modification, our string is "bacd". In the second modification (and the last modification), we have x=3. Since l_2=3\leq x \leq r_2=4, we find the index i = 2. We reverse the substring from index x=3 to l_2+r_2-x=3+4-3=4. After this modification, our string is "badc".In the second test case:The initial string is "abcde". In the first modification, we have x=1. Since l_1=1\leq x \leq r_1=1, we find the index i = 1. We reverse the substring from index x=1 to l_1+r_1-x=1+1-1=1. After this modification, our string hasn't changed ("abcde"). In the second modification, we have x=2. Since l_2=2\leq x \leq r_2=2, we find the index i = 2. We reverse the substring from index x=2 to l_2+r_2-x=2+2-2=2. After this modification, our string hasn't changed ("abcde"). In the third modification (and the last modification), we have x=3. Since l_3=3\leq x \leq r_3=5, we find the index i = 3. We reverse the substring from index x=3 to l_3+r_3-x=3+5-3=5. After this modification, our string is "abedc".
54 2abcd1 32 421 35 3abcde1 2 31 2 531 2 33 1gaf1322 210 1aghcdegdij11051 2 3 4 21 1a1111
badc abedc gaf jihgedcdga a
1 second
256 megabytes
['data structures', 'greedy', '*1600']
C. Vasilije in Cacaktime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAca and Milovan, two fellow competitive programmers, decided to give Vasilije a problem to test his skills.Vasilije is given three positive integers: n, k, and x, and he has to determine if he can choose k distinct integers between 1 and n, such that their sum is equal to x.Since Vasilije is now in the weirdest city in Serbia where Aca and Milovan live, Cacak, the problem seems weird to him. So he needs your help with this problem.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The only line of each test case contains three integers n, k and x (1 \le n \le 2 \cdot 10^5, 1 \le k \le n, 1 \le x \le 4 \cdot 10^{10}) — the maximum element he can choose, the number of elements he can choose and the sum he has to reach.Note that the sum of n over all test cases may exceed 2 \cdot 10^5.OutputFor each test case output one line: "YES", if it is possible to choose k distinct integers between 1 and n, such that their sum is equal to x, and "NO", if it isn't. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).ExampleInput 125 3 105 3 310 10 556 5 202 1 26187856 87856 2609202300200000 190000 1900000000028 5 20042 2 20069 6 4047202 32455 613407217185977 145541 15770805980Output YES NO YES YES NO NO YES NO NO NO YES YES NoteIn the first test case n = 5,\ k=3,\ x=10, so we can choose the numbers: 2, 3, 5, whose sum is 10, so the answer is "YES".In the second test case n = 5, \ k=3, \ x=3, there is no three numbers which satisfies the condition, so the answer is "NO". It can be shown that there are no three numbers whose sum is 3.
125 3 105 3 310 10 556 5 202 1 26187856 87856 2609202300200000 190000 1900000000028 5 20042 2 20069 6 4047202 32455 613407217185977 145541 15770805980
YES NO YES YES NO NO YES NO NO NO YES YES
1 second
256 megabytes
['math', '*900']
B. Aleksa and Stacktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter the Serbian Informatics Olympiad, Aleksa was very sad, because he didn't win a medal (he didn't know stack), so Vasilije came to give him an easy problem, just to make his day better.Vasilije gave Aleksa a positive integer n (n \ge 3) and asked him to construct a strictly increasing array of size n of positive integers, such that 3\cdot a_{i+2} is not divisible by a_i+a_{i+1} for each i (1\le i \le n-2). Note that a strictly increasing array a of size n is an array where a_i < a_{i+1} for each i (1 \le i \le n-1).Since Aleksa thinks he is a bad programmer now, he asked you to help him find such an 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 test cases follows.The first line of each test case contains a single integer n (3 \le n \le 2 \cdot 10^5) — the number of elements in array.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output n integers a_1, a_2, a_3, \dots, a_n (1 \le a_i \le 10^9).It can be proved that the solution exists for any n. If there are multiple solutions, output any of them.ExampleInput 3367Output 6 8 12 7 11 14 20 22 100 9 15 18 27 36 90 120NoteIn the first test case, a_1=6, a_2=8, a_3=12, so a_1+a_2=14 and 3 \cdot a_3=36, so 3 \cdot a_3 is not divisible by a_1+a_2.
3367
6 8 12 7 11 14 20 22 100 9 15 18 27 36 90 120
2 seconds
256 megabytes
['constructive algorithms', 'math', '*800']
A. How Much Does Daytona Cost?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputWe define an integer to be the most common on a subsegment, if its number of occurrences on that subsegment is larger than the number of occurrences of any other integer in that subsegment. A subsegment of an array is a consecutive segment of elements in the array a.Given an array a of size n, and an integer k, determine if there exists a non-empty subsegment of a where k is the most common element.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 1000) — the number of test cases. The description of test cases follows.The first line of each test case contains two integers n and k (1 \le n \le 100, 1 \le k \le 100) — the number of elements in array and the element which must be the most common.The second line of each test case contains n integers a_1, a_2, a_3, \dots, a_n (1 \le a_i \le 100) — elements of the array.OutputFor each test case output "YES" if there exists a subsegment in which k is the most common element, and "NO" otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).ExampleInput 75 41 4 3 4 14 12 3 4 45 643 5 60 4 22 51 54 15 3 3 11 335 33 4 1 5 5Output YES NO NO YES YES YES YES NoteIn the first test case we need to check if there is a subsegment where the most common element is 4.On the subsegment [2,5] the elements are 4, \ 3, \ 4, \ 1. 4 appears 2 times; 1 appears 1 time; 3 appears 1 time.This means that 4 is the most common element on the subsegment [2, 5], so there exists a subsegment where 4 is the most common element.
75 41 4 3 4 14 12 3 4 45 643 5 60 4 22 51 54 15 3 3 11 335 33 4 1 5 5
YES NO NO YES YES YES YES
1 second
256 megabytes
['greedy', '*800']
C. Joyboardtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputChaneka, a gamer kid, invented a new gaming controller called joyboard. Interestingly, the joyboard she invented can only be used to play one game.The joyboard has a screen containing n+1 slots numbered from 1 to n+1 from left to right. The n+1 slots are going to be filled with an array of non-negative integers [a_1,a_2,a_3,\ldots,a_{n+1}]. Chaneka, as the player, must assign a_{n+1} with an integer between 0 and m inclusive. Then, for each i from n to 1, the value of a_i will be equal to the remainder of dividing a_{i+1} (the adjacent value to the right) by i. In other words, a_i = a_{i + 1} \bmod i.Chaneka wants it such that after every slot is assigned with an integer, there are exactly k distinct values in the entire screen (among all n+1 slots). How many valid ways are there for assigning a non-negative integer into slot n+1?InputEach test contains multiple test cases. The first line contains an integer t (1 \leq t \leq 2\cdot10^4) — the number of test cases. The following lines contain the description of each test case.The only line of each test case contains three integers n, m, and k (1 \leq n \leq 10^9; 0 \leq m \leq 10^9; 1 \leq k \leq n+1) — there are n+1 slots, the integer assigned in slot n+1 must not be bigger than m, and there should be exactly k distinct values.OutputFor each test case, output a line containing an integer representing the number of valid ways for assigning a non-negative integer into slot n+1.ExampleInput 44 6 32 0 1265 265 2653 10 2Output 2 1 0 5 NoteIn the first test case, one of the 2 possible ways for Chaneka is to choose a_{n+1}=6. If she does that, then: a_4=a_5\bmod 4=6\bmod 4=2 a_3=a_4\bmod 3=2\bmod 3=2 a_2=a_3\bmod 2=2\bmod 2=0 a_1=a_2\bmod 1=0\bmod 1=0 a = [0, 0, 2, 2, 6] There are 3 distinct values. In the second test case, the 1 possible way for Chaneka is to choose a_{n+1}=0. If she does that, then a = [0, 0, 0]. There is only 1 distinct value.In the third test case, there is no possible way for assigning a non-negative integer into slot n+1.
44 6 32 0 1265 265 2653 10 2
2 1 0 5
1 second
256 megabytes
['math', 'number theory', '*1200']
A. Goals of Victorytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n teams in a football tournament. Each pair of teams match up once. After every match, Pak Chanek receives two integers as the result of the match, the number of goals the two teams score during the match. The efficiency of a team is equal to the total number of goals the team scores in each of its matches minus the total number of goals scored by the opponent in each of its matches.After the tournament ends, Pak Dengklek counts the efficiency of every team. Turns out that he forgot about the efficiency of one of the teams. Given the efficiency of n-1 teams a_1,a_2,a_3,\ldots,a_{n-1}. What is the efficiency of the missing team? It can be shown that the efficiency of the missing team can be uniquely determined.InputEach test contains multiple test cases. The first line contains an integer t (1 \leq t \leq 500) — the number of test cases. The following lines contain the description of each test case.The first line contains a single integer n (2 \leq n \leq 100) — the number of teams.The second line contains n-1 integers a_1,a_2,a_3,\ldots,a_{n-1} (-100\leq a_i\leq100) — the efficiency of n-1 teams.OutputFor each test case, output a line containing an integer representing the efficiency of the missing team.ExampleInput 243 -4 511-30 12 -57 7 0 -81 -68 41 -89 0Output -4 265 NoteIn the first test case, below is a possible tournament result: Team 1 vs. Team 2: 1-2 Team 1 vs. Team 3: 3-0 Team 1 vs. Team 4: 3-2 Team 2 vs. Team 3: 1-4 Team 2 vs. Team 4: 1-3 Team 3 vs. Team 4: 5-0 The efficiency of each team is: Team 1: (1+3+3)-(2+0+2)=7-4=3 Team 2: (2+1+1)-(1+4+3)=4-8=-4 Team 3: (0+4+5)-(3+1+0)=9-4=5 Team 4: (2+3+0)-(3+1+5)=5-9=-4 Therefore, the efficiency of the missing team (team 4) is -4.It can be shown that any possible tournament of 4 teams that has the efficiency of 3 teams be 3, -4, and 5 will always have the efficiency of the 4-th team be -4.
243 -4 511-30 12 -57 7 0 -81 -68 41 -89 0
-4 265
1 second
256 megabytes
['math', '*800']
G. Clubsteptime limit per test3 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputThere is an extremely hard video game that is one of Chaneka's favourite video games. One of the hardest levels in the game is called Clubstep. Clubstep consists of n parts, numbered from 1 to n. Chaneka has practised the level a good amount, so currently, her familiarity value with each part i is a_i.After this, Chaneka can do several (possibly zero) attempts on Clubstep. In each attempt, she dies on one of the n parts. If an attempt dies on part p, that means it only successfully passes through every part k for all 1 \leq k \leq p-1 and it does not reach any part k for all p+1 \leq k \leq n. An attempt that dies on part p takes p seconds.It is known that Chaneka improves much more on the part she dies on than anything else. It is also known that during an attempt, Chaneka does not get to practise that much on the parts she does not reach. So, the effect of an attempt that dies on part p is as follows: Chaneka's familiarity value with part p increases by 2. Chaneka's familiarity value with each part k for all 1 \leq k \leq p-1 increases by 1. There will be q questions. For the j-th question, you are given three integers l_j, r_j, and x_j. Then, you are asked to find out the minimum time (in seconds) for Chaneka to make it such that the familiarity value for every part p (l_j \leq p \leq r_j) is at least x_j.Note that each question is independent, so the attempt Chaneka does on a question does not affect the familiarity values of any other questions.InputThe first line contains a single integer n (1 \leq n \leq 3\cdot10^5) — the number of parts in Clubstep.The second line contains n integers a_1,a_2,a_3,\ldots,a_n (1\leq a_i\leq10^9) — Chaneka's familiarity value with each part.The third line contains a single integer q (1\leq q\leq3\cdot10^5) — the number of questions.The j-th of the next q lines contains three integers l_j, r_j, and x_j (1\leq l_j\leq r_j\leq n; 1\leq x_j\leq10^9) — the description of the j-th question.OutputOutput q lines with an integer in each line. The integer in the j-th line represents the minimum time (in seconds) for Chaneka to make it such that the familiarity value for every part p (l_j \leq p \leq r_j) is at least x_j.ExampleInput 5 1 3 2 1 2 3 1 5 5 2 4 5 3 3 1 Output 15 11 0 NoteFor the 1-st question, one possible strategy is to do the following: Do 1 attempt that dies on part 1. This takes 1 second. The familiarity values become [3, 3, 2, 1, 2]. Do 1 attempt that dies on part 4. This takes 4 seconds. The familiarity values become [4, 4, 3, 3, 2]. Do 2 attempts that die on part 5. This takes 10 seconds. The familiarity values become [6, 6, 5, 5, 6]. The total time taken (in seconds) is 1+4+10=15.For the 2-nd question, one possible strategy is to do the following: Do 1 attempt that dies on part 3. This takes 3 seconds. The familiarity values become [2, 4, 4, 1, 2]. Do 2 attempts that die on part 4. This takes 8 seconds. The familiarity values become [4, 6, 6, 5, 2]. The total time taken (in seconds) is 3+8=11.
5 1 3 2 1 2 3 1 5 5 2 4 5 3 3 1
15 11 0
3 seconds
1024 megabytes
['binary search', 'brute force', 'data structures', 'greedy', 'trees', '*3500']
F. Indefinite Clownfishtime limit per test3.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputPak Chanek has just bought an empty fish tank and he has been dreaming to fill it with his favourite kind of fish, clownfish. Pak Chanek likes clownfish because of their ability to change their genders on demand. Because of the size of his fish tank, Pak Chanek wants to buy exactly k clownfish to fill the tank.Pak Chanek goes to the local fish shop. The shop provides n clownfish numbered from 1 to n, with clownfish i having a size of a_i. Initially, every clownfish in the store does not have an assigned gender, but has the ability to be assigned to two possible clownfish genders, female or male.The store has a procedure which Pak Chanek should follow to buy clownfish. The shop owner will point at each clownfish sequentially from 1 to n and for each clownfish, she asks Pak Chanek whether to buy it or not. Each time Pak Chanek is asked, he must declare whether to buy the currently asked clownfish or not, before the shop owner moves on to the next clownfish. If Pak Chanek declares to buy the currently asked clownfish, he must also declare the gender to be assigned to that clownfish immediately. When assigning the gender for the currently asked clownfish, the following conditions must be satisfied: If Pak Chanek assigns it to be female and he has bought a female clownfish before, then the size of the current one must be exactly 1 bigger than the last female one. If Pak Chanek assigns it to be male and he has bought a male clownfish before, then the size of the current one must be exactly 1 smaller than the last male one. Pak Chanek wants to buy exactly k clownfish such that: There is at least one female clownfish and one male clownfish. Among the k clownfish Pak Chanek buys, the mean size of the female clownfish is equal to the mean size of the male clownfish. Let l and r respectively be the minimum and maximum index of a clownfish Pak Chanek buys. What is the minimum possible value of r-l?InputThe first line contains two integers n and k (2 \leq k \leq n \leq 2\cdot10^5) — the number of clownfish in the shop and the number of clownfish Pak Chanek has to buy.The second line contains n integers a_1,a_2,a_3,\ldots,a_n (1\leq a_i\leq n) — the size of each clownfish.OutputAn integer representing the minimum possible value of r-l. If it is impossible to buy exactly k clownfish and satisfy the problem's condition, print -1.ExamplesInput 9 6 2 4 2 3 2 4 1 3 4 Output 6 Input 6 6 2 6 5 2 6 5 Output -1 Input 5 4 1 2 3 4 5 Output -1 NoteIn the first example, Pak Chanek can do the following: Do not buy clownfish 1. Buy clownfish 2 and assign it as male. Buy clownfish 3 and assign it as female. Buy clownfish 4 and assign it as male. Buy clownfish 5 and assign it as male. Do not buy clownfish 6. Buy clownfish 7 and assign it as male. Buy clownfish 8 and assign it as female. Do not buy clownfish 9. Then, we get that: The mean size among the 2 female clownfish Pak Chanek buys is \frac{2+3}{2} = 2.5. The mean size among the 4 male clownfish Pak Chanek buys is \frac{4+3+2+1}{4} = 2.5. l=2, r=8, r-l=6. There are no better strategies.
9 6 2 4 2 3 2 4 1 3 4
6
3.5 seconds
512 megabytes
['binary search', 'graphs', '*3500']
E. Ball-Stackabletime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWith a problem title like that, there is no way this is going to be a graph problem.Chaneka has a graph with n vertices and n-1 edges. Some of the edges are directed and some of the edges are undirected. Edge i connects vertex u_i to vertex v_i. If t_i=0, edge i is undirected. If t_i=1, edge i is directed in the direction from u_i to v_i. It is known that if you make all edges undirected, the graph becomes a tree^\dagger.Chaneka wants to direct all undirected edges and colour each edge (different edges can have the same colour).After doing that, suppose Chaneka starts a walk from an arbitrary vertex x to an arbitrary vertex y (it is possible that x=y) going through one or more edges. She is allowed to go through each edge either following the direction or opposite to the direction of the edge. She is also allowed to visit a vertex or an edge more than once. During the walk, Chaneka maintains a stack of balls that is initially empty before the walk. Each time Chaneka goes through an edge, she does the following: If Chaneka goes through it in the right direction, she puts a new ball with a colour that is the same as the edge's colour to the top of the stack. If Chaneka goes through it in the opposite direction, she removes the ball that is on the top of the stack. A walk is stackable if and only if the stack is not empty before each time Chaneka goes through an edge in the opposite direction.A walk is ball-stackable if and only if it is stackable and each time Chaneka goes through an edge in the opposite direction, the colour of the ball removed from the stack is the same as the colour of the edge traversed.Is it possible to direct all undirected edges and colour each edge such that all stackable walks are also ball-stackable? If it is possible, find a construction example that uses the maximum number of different colours among all valid ways of directing and colouring. If there are multiple such solutions, output any of them.^\dagger A tree is a connected graph with no cycles.InputThe first line contains a single integer n (2\leq n\leq10^5) — the number of vertices in the graph.The i-th of the next n-1 lines contains three integers u_i, v_i, and t_i (1 \leq u_i,v_i \leq n; 0\leq t_i\leq1) — an undirected edge connecting vectices u_i and v_i if t_i=0, or a directed edge from vertex u_i to vertex v_i if t_i=1. If you make all edges undirected, the graph becomes a tree.OutputA single line containing -1 if it is impossible.Otherwise, the output consists of n lines describing your construction. The first line contains an integer z representing the number of different colours used. The i-th of the next n-1 lines contains three integers p, q, and c (1\leq p,q\leq n; 1\leq c\leq z) — the edge connecting vertices p and q in the graph is directed from vertex p to vertex q and is coloured with colour c. If there are multiple such solutions, output any of them.Note that since there should be z different colours in your construction, that means each colour from 1 to z must appear at least once in the graph.ExampleInput 5 2 5 1 1 3 0 5 4 0 1 5 1 Output 3 1 5 2 5 4 1 2 5 2 3 1 3 NoteThe following is the given graph. Chaneka can direct all undirected edges and colour each edge as follows. As an example, consider a stackable walk 3→1→5→2→5→4→5. Let's show that that walk is ball-stackable. Chaneka starts in vertex 3. The stack is []. Chaneka moves to vertex 1. She puts a ball of colour 3. The stack is [3]. Chaneka moves to vertex 5. She puts a ball of colour 2. The stack is [3,2]. Chaneka moves to vertex 2. She removes a ball of colour 2 (same colour as the edge). The stack is [3]. Chaneka moves to vertex 5. She puts a ball of colour 2. The stack is [3,2]. Chaneka moves to vertex 4. She puts a ball of colour 1. The stack is [3,2,1]. Chaneka moves to vertex 5. She removes a ball of colour 1 (same colour as the edge). The stack is [3,2]. Since every time Chaneka removes a ball from the stack, it has the same colour as the edge traversed, then the walk above is ball-stackable. It can be shown that if we direct and colour the edges as shown above, any possible stackable walk is also ball-stackable.
5 2 5 1 1 3 0 5 4 0 1 5 1
3 1 5 2 5 4 1 2 5 2 3 1 3
2 seconds
512 megabytes
['constructive algorithms', 'data structures', 'dp', 'trees', '*3300']
D. Lexichromatographytime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputPak Chanek loves his faculty, the Faculty of Computer Science, University of Indonesia (Fasilkom). He wants to play with the colours of the faculty's logo, blue and red.There is an array a consisting of n elements, element i has a value of a_i. Pak Chanek wants to colour each element in the array blue or red such that these following conditions are satisfied: If all blue elements are formed into a subsequence^\dagger and so are all the red elements, the blue subsequence is strictly less than the red subsequence lexicographically^\ddagger. Array a does not have any subarray that is imbalanced. A subarray is imbalanced if and only if there is a value k such that the absolute difference between the number of blue elements with value k and the number of red elements with value k in this subarray is 2 or more. Note that it is possible to colour every element of the array the same colour. How many different colourings satisfy all those conditions? Since the answer can be very big, print the answer modulo 998\,244\,353. Two colourings are different if and only if there is at least one element that is blue in one colouring, but red in the other.^\dagger A subsequence of an array is a sequence that can be obtained from the array by deleting some elements (possibly none), without changing the order of the remaining elements.^\ddagger Let p and q be two different sequences. Sequence p is said to be lexicographically less than sequence q if and only if p is a prefix of q or there is an index i such that p_j=q_j holds for every 1\leq j<i, and p_i<q_i. In particular, an empty sequence is always lexicographically less than any non-empty sequence.InputThe first line contains a single integer n (1 \leq n \leq 2\cdot10^5) — the size of array a.The second line contains n integers a_1,a_2,a_3,\ldots,a_n (1\leq a_i\leq2\cdot10^5).OutputAn integer representing the number of different colourings that satisfy all of the problem's conditions, modulo 998\,244\,353.ExamplesInput 8 1 3 1 2 3 2 3 3 Output 3 Input 1 265 Output 1 NoteIn the first example, the 3 ways for colouring all elements from index 1 to index 8 are: Blue, red, red, blue, blue, red, red, blue. Blue, red, red, red, blue, blue, red, blue. Red, red, blue, blue, blue, red, red, blue. As an example, if we colour the elements from index 1 to index 8 to be red, red, blue, red, red, blue, blue, blue, it is not valid colouring, because for subarray a[2..6], there are 0 blue elements with value 3 and 2 red elements with value 3, making subarray a[2..6] an imbalanced subarray.
8 1 3 1 2 3 2 3 3
3
2 seconds
512 megabytes
['combinatorics', 'dfs and similar', 'dsu', 'graphs', 'two pointers', '*2500']
C. Autosynthesistime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputChaneka writes down an array a of n positive integer elements. Initially, all elements are not circled. In one operation, Chaneka can circle an element. It is possible to circle the same element more than once.After doing all operations, Chaneka makes a sequence r consisting of all uncircled elements of a following the order of their indices.Chaneka also makes another sequence p such that its length is equal to the number of operations performed and p_i is the index of the element that is circled in the i-th operation.Chaneka wants to do several operations such that sequence r is equal to sequence p. Help her achieve this, or report if it is impossible! Note that if there are multiple solutions, you can print any of them.InputThe first line contains a single integer n (1 \leq n \leq 2\cdot10^5) — the size of array a.The second line contains n integers a_1,a_2,a_3,\ldots,a_n (1\leq a_i\leq n).OutputA single line containing -1 if it is impossible.Otherwise, the output consists of two lines. The first line contains an integer z representing the number of operations performed. The second line contains z integers, the i-th integer represents the index of the element circled in the i-th operation. If there are multiple solutions, you can print any of them.ExamplesInput 5 3 4 2 2 3 Output 3 3 2 3 Input 3 1 2 3 Output -1 NoteIn the first example, doing the operations like the example output gives the following results: Element a_2 gets circled 1 time. Element a_3 gets circled 2 times. The uncircled elements (ordered by their indices) are a_1, a_4, and a_5. So r=[3,2,3]. p=[3,2,3] Therefore, r=p.In the second example, it is impossible.
5 3 4 2 2 3
3 3 2 3
2 seconds
256 megabytes
['constructive algorithms', 'dfs and similar', 'graphs', 'greedy', 'sortings', '*2100']
B. Effects of Anti Pimplestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputChaneka has an array [a_1,a_2,\ldots,a_n]. Initially, all elements are white. Chaneka will choose one or more different indices and colour the elements at those chosen indices black. Then, she will choose all white elements whose indices are multiples of the index of at least one black element and colour those elements green. After that, her score is the maximum value of a_i out of all black and green elements.There are 2^n-1 ways for Chaneka to choose the black indices. Find the sum of scores for all possible ways Chaneka can choose the black indices. Since the answer can be very big, print the answer modulo 998\,244\,353.InputThe first line contains a single integer n (1 \leq n \leq 10^5) — the size of array a.The second line contains n integers a_1,a_2,a_3,\ldots,a_n (0\leq a_i\leq10^5).OutputAn integer representing the sum of scores for all possible ways Chaneka can choose the black indices, modulo 998\,244\,353.ExamplesInput 4 19 14 19 9 Output 265 Input 1 0 Output 0 Input 15 90000 9000 99000 900 90900 9900 99900 90 90090 9090 99090 990 90990 9990 99990 Output 266012571 NoteIn the first example, below are the 15 possible ways to choose the black indices: Index 1 is black. Indices 2, 3, and 4 are green. Maximum value among them is 19. Index 2 is black. Index 4 is green. Maximum value among them is 14. Index 3 is black. Maximum value among them is 19. Index 4 is black. Maximum value among them is 9. Indices 1 and 2 are black. Indices 3 and 4 are green. Maximum value among them is 19. Indices 1 and 3 are black. Indices 2 and 4 are green. Maximum value among them is 19. Indices 1 and 4 are black. Indices 2 and 3 are green. Maximum value among them is 19. Indices 2 and 3 are black. Index 4 is green. Maximum value among them is 19. Indices 2 and 4 are black. Maximum value among them is 14. Indices 3 and 4 are black. Maximum value among them is 19. Indices 1, 2, and 3 are black. Index 4 is green. Maximum value among them is 19. Indices 1, 2, and 4 are black. Index 3 is green. Maximum value among them is 19. Indices 1, 3, and 4 are black. Index 2 is green. Maximum value among them is 19. Indices 2, 3, and 4 are black. Maximum value among them is 19. Indices 1, 2, 3, and 4 are black. Maximum value among them is 19. The total sum is 19+14+19+9+19+19+19+19+14+19+19+19+19+19+19 = 265.
4 19 14 19 9
265
2 seconds
256 megabytes
['combinatorics', 'number theory', 'sortings', '*1500']
A. Helmets in Night Lighttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPak Chanek is the chief of a village named Khuntien. On one night filled with lights, Pak Chanek has a sudden and important announcement that needs to be notified to all of the n residents in Khuntien.First, Pak Chanek shares the announcement directly to one or more residents with a cost of p for each person. After that, the residents can share the announcement to other residents using a magical helmet-shaped device. However, there is a cost for using the helmet-shaped device. For each i, if the i-th resident has got the announcement at least once (either directly from Pak Chanek or from another resident), he/she can share the announcement to at most a_i other residents with a cost of b_i for each share.If Pak Chanek can also control how the residents share the announcement to other residents, what is the minimum cost for Pak Chanek to notify all n residents of Khuntien about the announcement?InputEach test contains multiple test cases. The first line contains an integer t (1 \leq t \leq 10^4) — the number of test cases. The following lines contain the description of each test case.The first line contains two integers n and p (1 \leq n \leq 10^5; 1 \leq p \leq 10^5) — the number of residents and the cost for Pak Chanek to share the announcement directly to one resident.The second line contains n integers a_1,a_2,a_3,\ldots,a_n (1\leq a_i\leq10^5) — the maximum number of residents that each resident can share the announcement to.The third line contains n integers b_1,b_2,b_3,\ldots,b_n (1\leq b_i\leq10^5) — the cost for each resident to share the announcement to one other resident.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, output a line containing an integer representing the minimum cost to notify all n residents of Khuntien about the announcement.ExampleInput 36 32 3 2 1 1 34 3 2 6 3 61 10000010000014 941 4 2 3103 96 86 57Output 16 100000 265 NoteIn the first test case, the following is a possible optimal strategy: Pak Chanek shares the announcement directly to the 3-rd, 5-th, and 6-th resident. This requires a cost of p+p+p=3+3+3=9. The 3-rd resident shares the announcement to the 1-st and 2-nd resident. This requires a cost of b_3+b_3=2+2=4. The 2-nd resident shares the announcement to the 4-th resident. This requires a cost of b_2=3. The total cost is 9+4+3=16. It can be shown that there is no other strategy with a smaller cost.
36 32 3 2 1 1 34 3 2 6 3 61 10000010000014 941 4 2 3103 96 86 57
16 100000 265
1 second
256 megabytes
['greedy', 'sortings', '*1000']
D. Jellyfish and Mextime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of n nonnegative integers a_1, a_2, \dots, a_n. Let m be a variable that is initialized to 0, Jellyfish will perform the following operation n times: select an index i (1 \leq i \leq |a|) and delete a_i from a. add \operatorname{MEX}(a)^{\dagger} to m. Now Jellyfish wants to know the minimum possible final value of m if he performs all the operations optimally.^{\dagger} The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of [2,2,1] is 0, because 0 does not belong to the array. The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not. The MEX of [0,3,1,2] is 4 because 0, 1, 2, and 3 belong to the array, but 4 does not. InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \leq t \leq 5000). The description of the test cases follows.The first line of each test case contains a single integer n (1 \leq n \leq 5000) — the size of the array.The second line of each test case contains n integers a_1, a_2, \dots, a_n (0 \leq a_i \leq 10^9) — the integers in the array.It is guaranteed that the sum of n over all test cases does not exceed 5000.OutputFor each test case, output a single integer — the minimum value of m if the operations are performed optimally.ExampleInput 485 2 1 0 3 0 4 021 251 0 2 114514 080 1 2 0 1 2 0 3Output 3 0 2 7 NoteIn the first test case, we delete elements from a in the following order: [5,2,\color{red}{1},0,3,0,4,0] \to [5,2,0,3,\color{red}{0},4,0] \to [5,2,\color{red}{0},3,4,0] \to [5,2,3,4,\color{red}{0}] \to [5,2,\color{red}{3},4] \to [\color{red}{5},2,4] \to [\color{red}{2},4] \to [\color{red}{4}] \to [~]. The value of m will be 1+1+1+0+0+0+0+0=3.
485 2 1 0 3 0 4 021 251 0 2 114514 080 1 2 0 1 2 0 3
3 0 2 7
1 second
256 megabytes
['dp', '*1600']
C. Jellyfish and Green Appletime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputJellyfish has n green apple pieces. Each green apple piece weighs 1~\text{kg}. Jellyfish wants to divide these green apple pieces equally among m people.Jellyfish has a magic knife. Each time Jellyfish can choose one piece of green apple and divide it into two smaller pieces, with each piece having half the weight of the original piece.Jellyfish wants to know the minimum number of operations needed such that she can divide the green apple pieces such that the total weight of apple pieces received by each person is the same.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \leq t \leq 2 \cdot 10^4). The description of the test cases follows.The first and only line of each test case contains two integers, n and m (1 \leq n, m \leq 10^9) — the number of the green apple pieces initially and the number of the people.OutputFor each test case, output a single integer — the minimum number of operations required to divide all the green apples equally among the m people. If this cannot be achieved using a finite number of operations, output -1 instead.ExampleInput 410 51 510 43 4Output 0 -1 2 5 NoteIn the first test case, we do not need to divide the apple pieces. Each person will receive 2 pieces of 1~\text{kg} and the total weight of apple pieces received by each person is 2~\text{kg}.In the second test case, it is impossible to divide the apple equally using a finite number of operations.In the third test case, we can divide two of the apple pieces of weight 1~\text{kg}, getting 4 apple pieces of weight 0.5~\text{kg}. Each person will receive 1 apple piece of weight 0.5~\text{kg} and 2 apple pieces of weight 1~\text{kg}. The total weight of apple pieces received by each person is 2.5~\text{kg}.In the fourth test case, we first divide all 3 of the apple pieces of weight 1~\text{kg}, getting 6 pieces of weight 0.5~\text{kg}. Then, we further divide two of the apple pieces of weight 0.5~\text{kg}, getting 4 pieces of weight 0.25~\text{kg}. Each person will receive 1 apple piece of weight 0.5~\text{kg} and 1 apple piece of weight 0.25~\text{kg}. The total weight of apples received by each person is 0.75~\text{kg}.
410 51 510 43 4
0 -1 2 5
1 second
256 megabytes
['bitmasks', 'greedy', 'math', 'number theory', '*1400']
A. Jellyfish and Undertaletime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFlowey has planted a bomb in Snowdin!The bomb has a timer that is initially set to b. Every second, the timer will decrease by 1. When the timer reaches 0, the bomb will explode! To give the residents of Snowdin enough time to evacuate, you will need to delay the bomb from exploding for as long as possible.You have n tools. Each tool can only be used at most once. If you use the i-th tool, the timer will increase by x_i. However, if the timer is changed to an integer larger than a, the timer will be set to a due to a bug.More specifically, the following events will happen every second in the following order: You will choose some (possibly none) of your tools that have not been used before. If you choose the i-th tool, and the bomb's timer is currently set to c, the timer will be changed to \min(c + x_i, a). The timer decreases by 1. If the timer reaches 0, the bomb explodes. Jellyfish now wants to know the maximum time in seconds until the bomb explodes if the tools are used optimally.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \leq t \leq 2000). The description of the test cases follows.The first line of each test case contains three integers a, b and n (1 \leq b \leq a \leq 10^9, 1 \leq n \leq 100) — the maximum value of the bomb's timer, the initial value of the timer of the bomb and the number of tools.The second line of each test contains n integers x_1, x_2, \dots, x_n (1 \leq x_i \leq 10^9) — the number the timer can increase by using the i-th tool.Note that the sum of n over all test cases is not bounded.OutputFor each test case, output a single integer — the maximum time in seconds until the bomb explodes.ExampleInput 25 3 31 1 77 1 51 2 5 6 8Output 9 21 NoteLet c denote the value of the bomb's timer. In the first test case: Second 1: choose tool 1 and 2 at this second, then c=5; the timer decreases by 1, then c=4. Second 2: the timer decreases by 1, then c=3. Second 3: the timer decreases by 1, then c=2. Second 4: the timer decreases by 1, then c=1. Second 5: choose tool 3, then c=5; the timer decreases by 1, then c=4. Second 6: the timer decreases by 1, then c=3. Second 7: the timer decreases by 1, then c=2. Second 8: the timer decreases by 1, then c=1. Second 9: the timer decreases by 1, then c=0. The bomb explodes. It can be proved that there is no way to use the tools such that the bomb explodes after more than 9 seconds.
25 3 31 1 77 1 51 2 5 6 8
9 21
1 second
256 megabytes
['brute force', 'greedy', '*900']
G. Jellyfish and Inscryptiontime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputJellyfish loves playing a game called "Inscryption" which is played on a directed acyclic graph with n vertices and m edges. All edges a \to b satisfy a < b.You need to move from vertex 1 to vertex n along the directed edges, and then fight with the final boss.You will collect cards and props in the process.Each card has two attributes: HP and damage. If a card's HP is a and its damage is b, then the power of the card is a \times b.Each prop has only one attribute: power.In addition to vertex 1 and vertex n, there are some vertices that trigger special events. The special events are: You will get a card with a HP, and b damage. If you have at least one card, choose one of your cards and increase its HP by x. If you have at least one card, choose one of your cards and increase its damage by y. You will get a prop with w power. When you get to vertex n, you can choose at most one of your cards and multiply its damage by 10^9.The final boss is very strong, so you want to maximize the sum of the power of all your cards and props. Find the maximum possible sum of power of all your cards and props if you play the game optimally.InputThe first line contains two integers n and m (2 \leq n \leq 200, n - 1\leq m \leq \min(\frac{n(n-1)}2, 2000)) — the number of the vertices and the number of the edges.In the following n lines, the i-th (1 \leq i \leq n) line describes the special event that will trigger on vertex i: 0 — no special events. 1 a b (1 \leq a, b \leq 200) — you will get a card with a HP, and b damage. 2 x (1 \leq x \leq 200) — if you have at least one card, choose one of your cards and increase its HP by x. 3 y (1 \leq y \leq 200) — if you have at least one card, choose one of your cards and increase its damage by y. 4 w (1 \leq w \leq 10^6) — you will get a prop with w power. In the following m lines, each line contains two integers u and v (1 \leq u < v \leq n) representing a directed edge from vertex u to vertex v.It is guaranteed that every edge (u,v) appears at most once.It is guaranteed that there are no special events on vertex 1 and vertex n.It is guaranteed that for all i, there exists a path from vertex 1 to vertex n that passes through vertex i.OutputOutput a single integer — the maximum sum of the power of all your cards and props.ExamplesInput 6 8 0 1 2 10 1 6 6 2 8 3 10 0 1 2 1 3 2 4 2 5 3 4 3 5 4 6 5 6 Output 100000000000Input 4 3 0 4 1000000 4 1000000 0 1 2 2 3 3 4 Output 2000000Input 16 15 0 1 1 10 1 10 1 2 4 3 4 1 20 20 2 30 3 20 1 1 100 2 9 1 1 200 2 9 3 10 1 190 100 2 10 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 Output 20000000005600Input 9 9 0 1 1 200 3 200 3 200 3 200 3 200 1 200 200 3 200 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 1 9 Output 80000000001000Input 9 8 0 1 20 200 3 200 3 200 2 5 1 100 200 3 200 3 200 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 Output 60000000015000Input 16 34 0 0 0 2 6 3 1 4 27 4 40 3 9 0 2 6 1 8 1 0 2 2 1 10 7 2 9 0 2 4 3 10 2 11 2 7 13 15 3 15 2 3 6 14 4 12 10 12 9 10 7 8 4 13 11 12 4 6 11 16 14 15 2 5 5 15 9 13 8 14 1 3 2 15 12 16 7 10 4 5 1 2 4 11 4 9 4 16 3 6 6 8 7 11 15 16 Output 133000000040NoteIn the first example, we will play the game in the following order: move from vertex 1 to vertex 2, get a card with 2 HP, and 10 damage. move from vertex 2 to vertex 4, choose the card we get from vertex 2 and increase its HP by 8. move from vertex 4 to vertex 6, choose the card we get from vertex 2 and multiply its damage by 10^9. In the end, we will have a card with (2+8)=10 HP and 10 \times 10^9=10^{10} damage, It's power is 10 \times 10^{10}=10^{11}. Because we only have the card we get from vertex 2, so the sum of power of all your cards and props is 10^{11}.
6 8 0 1 2 10 1 6 6 2 8 3 10 0 1 2 1 3 2 4 2 5 3 4 3 5 4 6 5 6
100000000000
2 seconds
1024 megabytes
['dp', '*3500']
F. Jellyfish and OEIStime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputJellyfish always uses OEIS to solve math problems, but now she finds a problem that cannot be solved by OEIS:Count the number of permutations p of [1, 2, \dots, n] such that for all (l, r) such that l \leq r \leq m_l, the subarray [p_l, p_{l+1}, \dots, p_r] is not a permutation of [l, l+1, \dots, r].Since the answer may be large, you only need to find the answer modulo 10^9+7.InputThe first line of the input contains a single integer n (1 \leq n \leq 200) — the length of the permutation.The second line of the input contains n integers m_1, m_2, \dots, m_n (0 \leq m_i \leq n).OutputOutput the number of different permutations that satisfy the conditions, modulo 10^9+7.ExamplesInput 3 1 2 3 Output 2Input 5 2 4 3 4 5 Output 38Input 5 5 1 1 1 1 Output 0NoteIn the first example, [2, 3, 1] and [3, 1, 2] satisfies the condition.
3 1 2 3
2
2 seconds
512 megabytes
['dp', '*3500']
E. Jellyfish and Hacktime limit per test8 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputIt is well known that quick sort works by randomly selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. But Jellyfish thinks that choosing a random element is just a waste of time, so she always chooses the first element to be the pivot. The time her code needs to run can be calculated by the following pseudocode:function fun(A) if A.length > 0 let L[1 ... L.length] and R[1 ... R.length] be new arrays L.length = R.length = 0 for i = 2 to A.length if A[i] < A[1] L.length = L.length + 1 L[L.length] = A[i] else R.length = R.length + 1 R[R.length] = A[i] return A.length + fun(L) + fun(R) else return 0Now you want to show her that her code is slow. When the function \mathrm{fun(A)} is greater than or equal to lim, her code will get \text{Time Limit Exceeded}. You want to know how many distinct permutations P of [1, 2, \dots, n] satisfies \mathrm{fun(P)} \geq lim. Because the answer may be large, you will only need to find the answer modulo 10^9+7.InputThe only line of the input contains two integers n and lim (1 \leq n \leq 200, 1 \leq lim \leq 10^9).OutputOutput the number of different permutations that satisfy the condition modulo 10^9+7.ExamplesInput 4 10 Output 8 Input 8 32 Output 1280 NoteIn the first example, P = [1, 4, 2, 3] satisfies the condition, because: \mathrm{fun([1, 4, 2, 3]) = 4 + fun([4, 2, 3]) = 7 + fun([2, 3]) = 9 + fun([3]) = 10}Do remember to output the answer modulo 10^9+7.
4 10
8
8 seconds
512 megabytes
['dp', 'math', '*3000']
D. Jellyfish and Mikutime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are n + 1 cities with numbers from 0 to n, connected by n roads. The i-th (1 \leq i \leq n) road connects city i-1 and city i bi-directionally. After Jellyfish flew back to city 0, she found out that she had left her Miku fufu in city n.Each road has a positive integer level of beauty. Denote the beauty of the i-th road as a_i.Jellyfish is trying to find her fufu. Because of her poor sense of direction, she doesn't know which way to go. Every day, she randomly chooses a road connected to the city she currently is in and traverses it. Let s be the sum of the beauty of the roads connected to the current city. For each road connected to the current city, Jellyfish will traverse the road with a probability of \frac x s, where x is the beauty of the road, reaching the city on the other side of the road.Jellyfish will start at city 0, and she will get only her fufu back when she reaches city n.You want to choose the beauty of the roads such that the expected number of days Jellyfish takes to find her fufu will be the minimum possible. However, due to limited funding, the sum of beauties of all roads must be less than or equal to m. Find the minimum expected number of days Jellyfish needs to get her fufu back if the beauty of the roads is chosen optimally.InputThe first and only line of the input contains two integers n and m (1 \leq n \leq m \leq 3000) — the number of the roads and the maximum sum of beauty of the roads.OutputOutput the minimum expected number of days Jellyfish needs to get her fufu back if the beauty of the roads is chosen optimally.Your answer will be accepted if the absolute or relative error does not exceed 10^{-9}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a-b|}{\max(1,|b|)} \leq 10^{-9}.ExamplesInput 3 8 Output 5.200000000000Input 10 98 Output 37.721155173329NoteIn the first example, the optimal assignment of beauty is a=[1, 2, 5]. The expected number of days Jellyfish needs to get her fufu back is 5.2.
3 8
5.200000000000
2 seconds
512 megabytes
['divide and conquer', 'dp', 'math', 'probabilities', '*2800']
C. Jellyfish and EVAtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputMonsters have invaded the town again! Asuka invites her good friend, Jellyfish, to drive EVA with her.There are n cities in the town. All the monsters are in city n. Jellyfish and Asuka are currently in city 1 and need to move to city n to defeat the monsters.There are m roads. The i-th road allows one to travel from city a_i to city b_i. All the roads are directed. That is, one cannot travel from city b_i to a_i using the i-th road. Interestingly, all roads satisfy a_i<b_i.Driving EVA requires two people to work together. However, Asuka and Jellyfish have not done any training together before.Suppose that EVA is currently in city u. Jellyfish and Asuka will both choose an undestroyed road that starts at city u. Suppose Jellyfish and Asuka choose roads that end at cities v_1 and v_2 respectively. If v_1 = v_2, EVA moves to city v_1 successfully. Otherwise, EVA stays in city u and both roads that they have chosen will be destroyed.It is possible that EVA is currently in city u (u \neq n) and there are no undestroyed roads that start at city u. In that case, the mission will be a failure. Otherwise, if they reach city n in the end, the mission is considered a success.Every time they choose the roads, Jellyfish knows that Asuka will choose a road randomly. Now, Jellyfish wants to know, if she chooses the roads optimally, what is the maximum probability of the mission being successful.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \leq t \leq 2000). The description of the test cases follows.The first line of each test case contains two integers, n and m (2 \leq n \leq 5000, 0 \leq m \leq \min(\frac{n(n-1)}{2}, 2 \cdot 10^5)) — the number of the cities and the number of the roads.In the following m lines of each test case, each line contains two integers a and b (1 \leq a < b \leq n) — representing a road from city a to city b.It is guaranteed that for each test case, each road appears at most once.It is guaranteed that the sum of n over all test cases will not exceed 5000 and that the sum of m over all test cases will not exceed 2 \cdot 10^5.OutputOutput the maximum probability of the mission being successful if Jellyfish chooses the roads optimally.Your answer will be accepted if the absolute or relative error does not exceed 10^{-9}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if \frac{|a-b|}{\max(1,|b|)} \leq 10^{-9}.ExampleInput 33 21 21 37 81 21 31 41 52 63 64 66 710 201 21 31 41 51 62 62 72 82 93 43 73 83 104 64 84 106 107 87 97 10Output 0.500000000000 0.625000000000 0.491666666667 NoteIn the first test case, Jellyfish will choose v_1=3, and Asuka will choose v_2=2 with a possibility of 0.5 and v_2=3 with a possibility of 0.5. The mission is considered a success with a possibility of 0.5.
33 21 21 37 81 21 31 41 52 63 64 66 710 201 21 31 41 51 62 62 72 82 93 43 73 83 104 64 84 106 107 87 97 10
0.500000000000 0.625000000000 0.491666666667
2 seconds
512 megabytes
['dp', 'graphs', 'greedy', 'math', 'probabilities', '*2300']
B. Jellyfish and Mathtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputJellyfish is given the non-negative integers a, b, c, d and m. Initially (x,y)=(a,b). Jellyfish wants to do several operations so that (x,y)=(c,d).For each operation, she can do one of the following: x := x\,\&\,y, x := x\,|\,y, y := x \oplus y, y := y \oplus m. Here \& denotes the bitwise AND operation, | denotes the bitwise OR operation and \oplus denotes the bitwise XOR operation.Now Jellyfish asks you for the minimum number of operations such that (x,y)=(c,d).InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \leq t \leq 10^5). The description of the test cases follows.The only line of each test case contains five integers, a, b, c, d and m (0 \leq a, b, c, d, m < 2^{30}).OutputFor each test case, output a single integer — the minimum number of operations. If this cannot be achieved, output -1 instead.ExampleInput 101 0 1 1 13 3 1 2 11 6 0 7 12 4 4 9 821 4 0 17 2850 50 0 0 3995 33 1 33 110138 202 174 64 10878 340 68 340 461457 291 491 566 766Output 1 -1 2 -1 -1 2 1 4 1 3 NoteIn the first test case, we can do the operation y = x \oplus y.In the second test case, it is not possible to change (x,y)=(1,2) using any sequence of operations.In the third test case, we can do the operation x = x\,\&\,y followed by the operation y = y \oplus m.
101 0 1 1 13 3 1 2 11 6 0 7 12 4 4 9 821 4 0 17 2850 50 0 0 3995 33 1 33 110138 202 174 64 10878 340 68 340 461457 291 491 566 766
1 -1 2 -1 -1 2 1 4 1 3
2 seconds
512 megabytes
['bitmasks', 'brute force', 'dfs and similar', 'dp', 'graphs', 'shortest paths', '*2400']
A. Jellyfish and Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputJellyfish has n green apples with values a_1, a_2, \dots, a_n and Gellyfish has m green apples with values b_1,b_2,\ldots,b_m.They will play a game with k rounds. For i=1,2,\ldots,k in this order, they will perform the following actions: If i is odd, Jellyfish can choose to swap one of her apples with one of Gellyfish's apples or do nothing. If i is even, Gellyfish can choose to swap one of his apples with one of Jellyfish's apples or do nothing. Both players want to maximize the sum of the values of their apples.Since you are one of the smartest people in the world, Jellyfish wants you to tell her the final sum of the value of her apples after all k rounds of the game. Assume that both Jellyfish and Gellyfish play optimally to maximize the sum of values of their apples.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \leq t \leq 2000). The description of the test cases follows.The first line of each test case contains three integers, n, m and k (1 \leq n, m \leq 50, 1 \leq k \leq 10^9) — the number of green apples Jellyfish has, the number of green apples Gellyfish has and the number of rounds of the game respectively.The second line of each test case contains n integers a_1, a_2, \dots, a_n (1 \leq a_i \leq 10^9) — the values of Jellyfish's green apples.The third line of each test case contains m integers b_1, b_2, \dots, b_m (1 \leq b_i \leq 10^9) — the values of Gellyfish's green apples.Do note that the sum of n and m over all test cases are both not bounded.OutputFor each test case, output a single integer — the final sum of the values of Jellyfish's apples.ExampleInput 42 2 11 23 41 1 10000124 5 110371 1 4 51 9 1 9 81 1 121Output 6 1 19 2 NoteIn the first test case, Jellyfish will swap the apple of value 1 and 4.In the second test case, both players will swap the two apples 10,000 times.In the fourth test case, Jellyfish will do nothing.
42 2 11 23 41 1 10000124 5 110371 1 4 51 9 1 9 81 1 121
6 1 19 2
1 second
256 megabytes
['brute force', 'games', 'greedy', 'implementation', '*1200']
H. Mad Citytime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMarcel and Valeriu are in the mad city, which is represented by n buildings with n two-way roads between them. Marcel and Valeriu start at buildings a and b respectively. Marcel wants to catch Valeriu, in other words, be in the same building as him or meet on the same road. During each move, they choose to go to an adjacent building of their current one or stay in the same building. Because Valeriu knows Marcel so well, Valeriu can predict where Marcel will go in the next move. Valeriu can use this information to make his move. They start and end the move at the same time.It is guaranteed that any pair of buildings is connected by some path and there is at most one road between any pair of buildings.Assuming both players play optimally, answer if Valeriu has a strategy to indefinitely escape Marcel.InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The first line of each test case contains three space-separated integers n, a, b (3 \leq n \leq 2 \cdot 10^5; 1 \leq a, b \leq n) — the number of buildings (which equals the number of roads) and the starting buildings of Marcel and Valeriu.The following n lines each contain two integers u_i, v_i (1 \le u_i, v_i \le n, u_i \neq v_i) — there is a road between buildings u_i and v_i. There is at most one road between any unordered pair of buildings.The sum of n over all test cases does not exceed 2 \cdot 10^5.The roads are given that it is possible to get from any building to any other building going along the roads.OutputFor each test case output "YES" if Valeriu can escape Marcel forever and "NO" otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).ExampleInput 63 2 12 13 21 34 1 41 41 21 32 34 1 21 22 32 43 47 1 14 12 15 34 64 27 53 48 5 38 35 12 66 81 24 85 76 710 6 11 24 35 87 810 41 92 48 16 23 1Output YES NO YES NO NO YES NoteIn the first test case the graph looks as follows: Marcel starts at building 2, while Valeriu starts at building 1. Valeriu knows which way Marcel will move around the triangle, and he can simply always move in the same way to avoid Marcel forever.In the second test case the graph looks as follows: Marcel starts at building 1, while Valeriu starts at building 4. Marcel can go to building 4 on his first move and win, since Valeriu must either go to building 1 (then he meets Marcel on the road from 1 to 4) or stay at building 4 (then he meets Marcel at building 4). So there is no strategy for Valeriu to win.
63 2 12 13 21 34 1 41 41 21 32 34 1 21 22 32 43 47 1 14 12 15 34 64 27 53 48 5 38 35 12 66 81 24 85 76 710 6 11 24 35 87 810 41 92 48 16 23 1
YES NO YES NO NO YES
4 seconds
256 megabytes
['dfs and similar', 'dsu', 'games', 'graphs', 'shortest paths', 'trees', '*1700']
G. ABBC or BACBtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s made up of characters \texttt{A} and \texttt{B}. Initially you have no coins. You can perform two types of operations: Pick a substring^\dagger \texttt{AB}, change it to \texttt{BC}, and get a coin. Pick a substring^\dagger \texttt{BA}, change it to \texttt{CB}, and get a coin. What is the most number of coins you can obtain?^\dagger A substring of length 2 is a sequence of two adjacent characters of a string.InputThe input consists of multiple test cases. The first line of the input contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The only line of each test case contains the string s (1 \leq |s| \leq 2 \cdot 10^5). All characters of s are either \texttt{A} or \texttt{B}.The sum of the lengths of s over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the maximum number of coins you can obtain.ExampleInput 8ABBAABABAABAABBAAAAAABBABABAAAOutput 2 1 3 1 6 2 0 0 NoteIn the first test case you can perform the following operations to get 2 coins: \color{red}{\texttt{AB}}\texttt{BA} \to \texttt{BC}\color{red}{\texttt{BA}} \to \texttt{BCCB}In the second test case you can perform the following operation to get 1 coin: \color{red}{\texttt{AB}}\texttt{A} \to \texttt{BCA}In the third test case you can perform the following operations to get 3 coins: \color{red}{\texttt{BA}}\texttt{ABA} \to \texttt{CBA}\color{red}{\texttt{BA}} \to \texttt{C}\color{red}{\texttt{BA}}\texttt{CB} \to \texttt{CCBCB}
8ABBAABABAABAABBAAAAAABBABABAAA
2 1 3 1 6 2 0 0
1 second
256 megabytes
['constructive algorithms', 'greedy', '*1500']
F. Money Treestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLuca is in front of a row of n trees. The i-th tree has a_i fruit and height h_i.He wants to choose a contiguous subarray of the array [h_l, h_{l+1}, \dots, h_r] such that for each i (l \leq i < r), h_i is divisible^{\dagger} by h_{i+1}. He will collect all the fruit from each of the trees in the subarray (that is, he will collect a_l + a_{l+1} + \dots + a_r fruits). However, if he collects more than k fruits in total, he will get caught. What is the maximum length of a subarray Luca can choose so he doesn't get caught?^{\dagger} x is divisible by y if the ratio \frac{x}{y} is an integer.InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The first of each test case line contains two space-separated integers n and k (1 \leq n \leq 2 \cdot 10^5; 1 \leq k \leq 10^9) — the number of trees and the maximum amount of fruits Luca can collect without getting caught.The second line of each test case contains n space-separated integers a_i (1 \leq a_i \leq 10^4) — the number of fruits in the i-th tree.The third line of each test case contains n space-separated integers h_i (1 \leq h_i \leq 10^9) — the height of the i-th tree.The sum of n over all test cases does not exceed 2 \cdot 10^5. OutputFor each test case output a single integer, the length of the maximum length contiguous subarray satisfying the conditions, or 0 if there is no such subarray.ExampleInput 55 123 2 4 1 84 4 2 4 14 85 4 1 26 2 3 13 127 9 102 2 41 101117 102 6 3 1 5 10 672 24 24 12 4 4 2Output 3 2 1 0 3 NoteIn the first test case, Luca can select the subarray with l=1 and r=3.In the second test case, Luca can select the subarray with l=3 and r=4.In the third test case, Luca can select the subarray with l=2 and r=2.
55 123 2 4 1 84 4 2 4 14 85 4 1 26 2 3 13 127 9 102 2 41 101117 102 6 3 1 5 10 672 24 24 12 4 4 2
3 2 1 0 3
2 seconds
256 megabytes
['binary search', 'greedy', 'math', 'two pointers', '*1300']
E. Building an Aquariumtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou love fish, that's why you have decided to build an aquarium. You have a piece of coral made of n columns, the i-th of which is a_i units tall. Afterwards, you will build a tank around the coral as follows: Pick an integer h \geq 1 — the height of the tank. Build walls of height h on either side of the tank. Then, fill the tank up with water so that the height of each column is h, unless the coral is taller than h; then no water should be added to this column. For example, with a=[3,1,2,4,6,2,5] and a height of h=4, you will end up using a total of w=8 units of water, as shown. You can use at most x units of water to fill up the tank, but you want to build the biggest tank possible. What is the largest value of h you can select?InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains two positive integers n and x (1 \leq n \leq 2 \cdot 10^5; 1 \leq x \leq 10^9) — the number of columns of the coral and the maximum amount of water you can use.The second line of each test case contains n space-separated integers a_i (1 \leq a_i \leq 10^9) — the heights of the coral.The sum of n over all test cases doesn't exceed 2 \cdot 10^5.OutputFor each test case, output a single positive integer h (h \geq 1) — the maximum height the tank can have, so you need at most x units of water to fill up the tank.We have a proof that under these constraints, such a value of h always exists.ExampleInput 57 93 1 2 4 6 2 53 101 1 14 11 4 3 46 19842 6 5 9 1 81 10000000001Output 4 4 2 335 1000000001 NoteThe first test case is pictured in the statement. With h=4 we need 8 units of water, but if h is increased to 5 we need 13 units of water, which is more than x=9. So h=4 is optimal.In the second test case, we can pick h=4 and add 3 units to each column, using a total of 9 units of water. It can be shown that this is optimal.In the third test case, we can pick h=2 and use all of our water, so it is optimal.
57 93 1 2 4 6 2 53 101 1 14 11 4 3 46 19842 6 5 9 1 81 10000000001
4 4 2 335 1000000001
2 seconds
256 megabytes
['binary search', 'sortings', '*1100']
D. 1D Erasertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a strip of paper s that is n cells long. Each cell is either black or white. In an operation you can take any k consecutive cells and make them all white.Find the minimum number of operations needed to remove all black cells.InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The first line of each test case contains two integers n and k (1 \leq k \leq n \leq 2 \cdot 10^5) — the length of the paper and the integer used in the operation.The second line of each test case contains a string s of length n consisting of characters \texttt{B} (representing a black cell) or \texttt{W} (representing a white cell).The sum 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 needed to remove all black cells.ExampleInput 86 3WBWWWB7 3WWBWBWW5 4BWBWB5 5BBBBB8 2BWBWBBBB10 2WBBWBBWBBW4 1BBBB3 2WWWOutput 2 1 2 1 4 3 4 0 NoteIn the first test case you can perform the following operations: \color{red}{\texttt{WBW}}\texttt{WWB} \to \texttt{WWW}\color{red}{\texttt{WWB}} \to \texttt{WWWWWW}In the second test case you can perform the following operations: \texttt{WW}\color{red}{\texttt{BWB}}\texttt{WW} \to \texttt{WWWWWWW}In the third test case you can perform the following operations: \texttt{B}\color{red}{\texttt{WBWB}} \to \color{red}{\texttt{BWWW}}\texttt{W} \to \texttt{WWWWW}
86 3WBWWWB7 3WWBWBWW5 4BWBWB5 5BBBBB8 2BWBWBBBB10 2WBBWBBWBBW4 1BBBB3 2WWW
2 1 2 1 4 3 4 0
1 second
256 megabytes
['greedy', 'implementation', 'two pointers', '*800']
C. Target Practicetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA 10 \times 10 target is made out of five "rings" as shown. Each ring has a different point value: the outermost ring — 1 point, the next ring — 2 points, ..., the center ring — 5 points. Vlad fired several arrows at the target. Help him determine how many points he got.InputThe input consists of multiple test cases. The first line of the input contains a single integer t (1 \leq t \leq 1000) — the number of test cases.Each test case consists of 10 lines, each containing 10 characters. Each character in the grid is either \texttt{X} (representing an arrow) or \texttt{.} (representing no arrow).OutputFor each test case, output a single integer — the total number of points of the arrows.ExampleInput 4X..........................X.......X..........X......................X..X..........................X................................................................................................................................................X.......................................................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXOutput 17 0 5 220 NoteIn the first test case, there are three arrows on the outer ring worth 1 point each, two arrows on the ring worth 3 points each, and two arrows on the ring worth 4 points each. The total score is 3 \times 1 + 2 \times 3 + 2 \times 4 = 17. In the second test case, there aren't any arrows, so the score is 0.
4X..........................X.......X..........X......................X..X..........................X................................................................................................................................................X.......................................................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
17 0 5 220
1 second
256 megabytes
['implementation', 'math', '*800']
B. Good Kidtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSlavic is preparing a present for a friend's birthday. He has an array a of n digits and the present will be the product of all these digits. Because Slavic is a good kid who wants to make the biggest product possible, he wants to add 1 to exactly one of his digits. What is the maximum product Slavic can make?InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 9) — the number of digits.The second line of each test case contains n space-separated integers a_i (0 \leq a_i \leq 9) — the digits in the array.OutputFor each test case, output a single integer — the maximum product Slavic can make, by adding 1 to exactly one of his digits.ExampleInput 442 2 1 230 1 254 3 2 3 499 9 9 9 9 9 9 9 9Output 16 2 432 430467210
442 2 1 230 1 254 3 2 3 499 9 9 9 9 9 9 9 9
16 2 432 430467210
1 second
256 megabytes
['brute force', 'greedy', 'math', '*800']
A. Short Sorttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are three cards with letters \texttt{a}, \texttt{b}, \texttt{c} placed in a row in some order. You can do the following operation at most once: Pick two cards, and swap them. Is it possible that the row becomes \texttt{abc} after the operation? Output "YES" if it is possible, and "NO" otherwise.InputThe first line contains a single integer t (1 \leq t \leq 6) — the number of test cases.The only line of each test case contains a single string consisting of each of the three characters \texttt{a}, \texttt{b}, and \texttt{c} exactly once, representing the cards.OutputFor each test case, output "YES" if you can make the row \texttt{abc} with at most one operation, or "NO" otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).ExampleInput 6abcacbbacbcacabcbaOutput YES YES YES NO NO YES NoteIn the first test case, we don't need to do any operations, since the row is already \texttt{abc}.In the second test case, we can swap \texttt{c} and \texttt{b}: \texttt{acb} \to \texttt{abc}.In the third test case, we can swap \texttt{b} and \texttt{a}: \texttt{bac} \to \texttt{abc}.In the fourth test case, it is impossible to make \texttt{abc} using at most one operation.
6abcacbbacbcacabcba
YES YES YES NO NO YES
1 second
256 megabytes
['brute force', 'implementation', '*800']
G. Replace With Producttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array a of n positive integers. You need to perform the following operation exactly once: Choose 2 integers l and r (1 \le l \le r \le n) and replace the subarray a[l \ldots r] with the single element: the product of all elements in the subarray (a_l \cdot \ldots \cdot a_r).For example, if an operation with parameters l = 2, r = 4 is applied to the array [5, 4, 3, 2, 1], the array will turn into [5, 24, 1].Your task is to maximize the sum of the array after applying this operation. Find the optimal subarray to apply this operation.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 description of the test cases.The first line of each test case contains a single number n (1 \le n \le 2 \cdot 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).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 2 integers l and r (1 \le l \le r \le n) — the boundaries of the subarray to be replaced with the product.If there are multiple solutions, output any of them.ExampleInput 941 3 1 341 1 2 351 1 1 1 1510 1 10 1 101122 232 1 242 1 1 362 1 2 1 1 3Output 2 4 3 4 1 1 1 5 1 1 1 2 2 2 4 4 1 6 NoteIn the first test case, after applying the operation with parameters l = 2, r = 4, the array [1, 3, 1, 3] turns into [1, 9], with a sum equal to 10. It is easy to see that by replacing any other segment with a product, the sum will be less than 10.In the second test case, after applying the operation with parameters l = 3, r = 4, the array [1, 1, 2, 3] turns into [1, 1, 6], with a sum equal to 8. It is easy to see that by replacing any other segment with a product, the sum will be less than 8.In the third test case, it will be optimal to choose any operation with l = r, then the sum of the array will remain 5, and when applying any other operation, the sum of the array will decrease.
941 3 1 341 1 2 351 1 1 1 1510 1 10 1 101122 232 1 242 1 1 362 1 2 1 1 3
2 4 3 4 1 1 1 5 1 1 1 2 2 2 4 4 1 6
1 second
256 megabytes
['brute force', 'greedy', 'math', '*2000']
F. Selling a Menagerietime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are the owner of a menagerie consisting of n animals numbered from 1 to n. However, maintaining the menagerie is quite expensive, so you have decided to sell it!It is known that each animal is afraid of exactly one other animal. More precisely, animal i is afraid of animal a_i (a_i \neq i). Also, the cost of each animal is known, for animal i it is equal to c_i.You will sell all your animals in some fixed order. Formally, you will need to choose some permutation^\dagger p_1, p_2, \ldots, p_n, and sell animal p_1 first, then animal p_2, and so on, selling animal p_n last.When you sell animal i, there are two possible outcomes: If animal a_i was sold before animal i, you receive c_i money for selling animal i. If animal a_i was not sold before animal i, you receive 2 \cdot c_i money for selling animal i. (Surprisingly, animals that are currently afraid are more valuable). Your task is to choose the order of selling the animals in order to maximize the total profit. For example, if a = [3, 4, 4, 1, 3], c = [3, 4, 5, 6, 7], and the permutation you choose is [4, 2, 5, 1, 3], then: The first animal to be sold is animal 4. Animal a_4 = 1 was not sold before, so you receive 2 \cdot c_4 = 12 money for selling it. The second animal to be sold is animal 2. Animal a_2 = 4 was sold before, so you receive c_2 = 4 money for selling it. The third animal to be sold is animal 5. Animal a_5 = 3 was not sold before, so you receive 2 \cdot c_5 = 14 money for selling it. The fourth animal to be sold is animal 1. Animal a_1 = 3 was not sold before, so you receive 2 \cdot c_1 = 6 money for selling it. The fifth animal to be sold is animal 3. Animal a_3 = 4 was sold before, so you receive c_3 = 5 money for selling it.Your total profit, with this choice of permutation, is 12 + 4 + 14 + 6 + 5 = 41. Note that 41 is not the maximum possible profit in this example.^\dagger A permutation of length n is an array consisting of n distinct 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 twice in the array) and [1,3,4] is also not a permutation (n=3, but 4 is present in the array).InputThe first line of the input contains an integer t (1 \le t \le 10^4) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case description contains an integer n (2 \le n \le 10^5) — the number of animals.The second line of the test case description contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le n, a_i \neq i) — a_i means the index of the animal that animal i is afraid of.The third line of the test case description contains n integers c_1, c_2, \dots, c_n (1 \le c_i \le 10^9) — the costs of the animals.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputOutput t lines, each containing the answer to the corresponding test case. The answer should be n integers — the permutation p_1, p_2, \ldots, p_n, indicating in which order to sell the animals in order to maximize the profit. If there are multiple possible answers, you can output any of them.ExampleInput 832 3 26 6 182 1 4 3 6 5 8 71 2 1 2 2 1 2 152 1 1 1 19 8 1 1 122 11000000000 99999999972 3 2 6 4 4 31 2 3 4 5 6 753 4 4 1 33 4 5 6 732 1 11 2 242 1 4 11 1 1 1Output 1 2 3 2 4 5 1 6 3 7 8 3 4 5 1 2 1 2 7 5 1 3 2 6 4 5 3 2 4 1 3 2 1 3 4 1 2
832 3 26 6 182 1 4 3 6 5 8 71 2 1 2 2 1 2 152 1 1 1 19 8 1 1 122 11000000000 99999999972 3 2 6 4 4 31 2 3 4 5 6 753 4 4 1 33 4 5 6 732 1 11 2 242 1 4 11 1 1 1
1 2 3 2 4 5 1 6 3 7 8 3 4 5 1 2 1 2 7 5 1 3 2 6 4 5 3 2 4 1 3 2 1 3 4 1 2
2 seconds
256 megabytes
['dfs and similar', 'dsu', 'graphs', 'implementation', 'math', '*1800']
E. Data Structures Fantime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of integers a_1, a_2, \ldots, a_n, as well as a binary string^{\dagger} s consisting of n characters.Augustin is a big fan of data structures. Therefore, he asked you to implement a data structure that can answer q queries. There are two types of queries: "1 l r" (1\le l \le r \le n) — replace each character s_i for l \le i \le r with its opposite. That is, replace all \texttt{0} with \texttt{1} and all \texttt{1} with \texttt{0}. "2 g" (g \in \{0, 1\}) — calculate the value of the bitwise XOR of the numbers a_i for all indices i such that s_i = g. Note that the \operatorname{XOR} of an empty set of numbers is considered to be equal to 0.Please help Augustin to answer all the queries!For example, if n = 4, a = [1, 2, 3, 6], s = \texttt{1001}, consider the following series of queries: "2 0" — we are interested in the indices i for which s_i = \tt{0}, since s = \tt{1001}, these are the indices 2 and 3, so the answer to the query will be a_2 \oplus a_3 = 2 \oplus 3 = 1. "1 1 3" — we need to replace the characters s_1, s_2, s_3 with their opposites, so before the query s = \tt{1001}, and after the query: s = \tt{0111}. "2 1" — we are interested in the indices i for which s_i = \tt{1}, since s = \tt{0111}, these are the indices 2, 3, and 4, so the answer to the query will be a_2 \oplus a_3 \oplus a_4 = 2 \oplus 3 \oplus 6 = 7. "1 2 4" — s = \tt{0111} \to s = \tt{0000}. "2 1" — s = \tt{0000}, there are no indices with s_i = \tt{1}, so since the \operatorname{XOR} of an empty set of numbers is considered to be equal to 0, the answer to this query is 0.^{\dagger} A binary string is a string containing only characters \texttt{0} or \texttt{1}.InputThe first line of the input contains one integer t (1 \le t \le 10^4) — the number of test cases in the test.The descriptions of the test cases follow.The first line of each test case description contains an integer n (1 \le n \le 10^5) — the length of the array.The second line of the test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9).The third line of the test case contains the binary string s of length n.The fourth line of the test case contains one integer q (1 \le q \le 10^5) — the number of queries.The subsequent q lines of the test case describe the queries. The first number of each query, tp \in \{1, 2\}, characterizes the type of the query: if tp = 1, then 2 integers 1 \le l \le r \le n follow, meaning that the operation of type 1 should be performed with parameters l, r, and if tp = 2, then one integer g \in \{0, 1\} follows, meaning that the operation of type 2 should be performed with parameter g.It is guaranteed that the sum of n over all test cases does not exceed 10^5, and also that the sum of q over all test cases does not exceed 10^5.OutputFor each test case, and for each query of type 2 in it, output the answer to the corresponding query.ExampleInput 551 2 3 4 50100072 02 11 2 42 02 11 1 32 1612 12 14 14 5 500100132 11 2 42 147 7 7 777111132 01 2 32 021000000000 9961791791112 151 42 20 47 70001151 3 41 1 11 3 41 2 42 0Output 3 2 6 7 7 11 7 0 0 16430827 47 NoteLet's analyze the first test case: "2 0" — we are interested in the indices i for which s_i = \tt{0}, since s = \tt{01000}, these are the indices 1, 3, 4, and 5, so the answer to the query will be a_1 \oplus a_3 \oplus a_4 \oplus a_5 = 1 \oplus 3 \oplus 4 \oplus 5 = 3. "2 1" — we are interested in the indices i for which s_i = \tt{1}, since s = \tt{01000}, the only suitable index is 2, so the answer to the query will be a_2 = 2. "1 2 4" — we need to replace the characters s_2, s_3, s_4 with their opposites, so before the query s = \tt{01000}, and after the query: s = \tt{00110}. "2 0" — we are interested in the indices i for which s_i = \tt{0}, since s = \tt{00110}, these are the indices 1, 2, and 5, so the answer to the query will be a_1 \oplus a_2 \oplus a_5 = 1 \oplus 2 \oplus 5 = 6. "2 1" — we are interested in the indices i for which s_i = \tt{1}, since s = \tt{00110}, these are the indices 3 and 4, so the answer to the query will be a_3 \oplus a_4 = 3 \oplus 4 = 7. "1 1 3" — s = \tt{00110} \to s = \tt{11010}. "2 1" — we are interested in the indices i for which s_i = \tt{1}, since s = \tt{11010}, these are the indices 1, 2, and 4, so the answer to the query will be a_1 \oplus a_2 \oplus a_4 = 1 \oplus 2 \oplus 4 = 7.
551 2 3 4 50100072 02 11 2 42 02 11 1 32 1612 12 14 14 5 500100132 11 2 42 147 7 7 777111132 01 2 32 021000000000 9961791791112 151 42 20 47 70001151 3 41 1 11 3 41 2 42 0
3 2 6 7 7 11 7 0 0 16430827 47
2 seconds
256 megabytes
['binary search', 'bitmasks', 'data structures', 'dp', '*1500']
D. Plus Minus Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given 3 integers — n, x, y. Let's call the score of a permutation^\dagger p_1, \ldots, p_n the following value:(p_{1 \cdot x} + p_{2 \cdot x} + \ldots + p_{\lfloor \frac{n}{x} \rfloor \cdot x}) - (p_{1 \cdot y} + p_{2 \cdot y} + \ldots + p_{\lfloor \frac{n}{y} \rfloor \cdot y})In other words, the score of a permutation is the sum of p_i for all indices i divisible by x, minus the sum of p_i for all indices i divisible by y.You need to find the maximum possible score among all permutations of length n.For example, if n = 7, x = 2, y = 3, the maximum score is achieved by the permutation [2,\color{red}{\underline{\color{black}{6}}},\color{blue}{\underline{\color{black}{1}}},\color{red}{\underline{\color{black}{7}}},5,\color{blue}{\underline{\color{red}{\underline{\color{black}{4}}}}},3] and is equal to (6 + 7 + 4) - (1 + 4) = 17 - 5 = 12.^\dagger A permutation of length n is an array consisting of n distinct 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 (the number 2 appears twice in the array) and [1,3,4] is also not a permutation (n=3, but the array contains 4).InputThe first line of input contains an integer t (1 \le t \le 10^4) — the number of test cases.Then follows the description of each test case.The only line of each test case description contains 3 integers n, x, y (1 \le n \le 10^9, 1 \le x, y \le n).OutputFor each test case, output a single integer — the maximum score among all permutations of length n.ExampleInput 87 2 312 6 39 1 92 2 2100 20 5024 4 61000000000 5575 254504 4 1Output 12 -3 44 0 393 87 179179179436104 -6 NoteThe first test case is explained in the problem statement above.In the second test case, one of the optimal permutations will be [12,11,\color{blue}{\underline{\color{black}{2}}},4,8,\color{blue}{\underline{\color{red}{\underline{\color{black}{9}}}}},10,6,\color{blue}{\underline{\color{black}{1}}},5,3,\color{blue}{\underline{\color{red}{\underline{\color{black}{7}}}}}]. The score of this permutation is (9 + 7) - (2 + 9 + 1 + 7) = -3. It can be shown that a score greater than -3 can not be achieved. Note that the answer to the problem can be negative.In the third test case, the score of the permutation will be (p_1 + p_2 + \ldots + p_9) - p_9. One of the optimal permutations for this case is [9, 8, 7, 6, 5, 4, 3, 2, 1], and its score is 44. It can be shown that a score greater than 44 can not be achieved.In the fourth test case, x = y, so the score of any permutation will be 0.
87 2 312 6 39 1 92 2 2100 20 5024 4 61000000000 5575 254504 4 1
12 -3 44 0 393 87 179179179436104 -6
1 second
256 megabytes
['math', '*1200']
C. Non-coprime Splittime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers l \le r. You need to find positive integers a and b such that the following conditions are simultaneously satisfied: l \le a + b \le r \gcd(a, b) \neq 1or report that they do not exist.\gcd(a, b) denotes the greatest common divisor of numbers a and b. For example, \gcd(6, 9) = 3, \gcd(8, 9) = 1, \gcd(4, 2) = 2.InputThe first line of the input contains an integer t (1 \le t \le 500) — the number of test cases.Then the descriptions of the test cases follow.The only line of the description of each test case contains 2 integers l, r (1 \le l \le r \le 10^7).OutputFor each test case, output the integers a, b that satisfy all the conditions on a separate line. If there is no answer, instead output a single number -1.If there are multiple answers, you can output any of them.ExampleInput 1111 151 318 1941 43777 7778000000 100000002000 20231791791 17917911 42 39840769 9840769Output 6 9 -1 14 4 36 6 111 666 4000000 5000000 2009 7 -1 2 2 -1 6274 9834495NoteIn the first test case, 11 \le 6 + 9 \le 15, \gcd(6, 9) = 3, and all conditions are satisfied. Note that this is not the only possible answer, for example, \{4, 10\}, \{5, 10\}, \{6, 6\} are also valid answers for this test case.In the second test case, the only pairs \{a, b\} that satisfy the condition 1 \le a + b \le 3 are \{1, 1\}, \{1, 2\}, \{2, 1\}, but in each of these pairs \gcd(a, b) equals 1, so there is no answer.In the third sample test, \gcd(14, 4) = 2.
1111 151 318 1941 43777 7778000000 100000002000 20231791791 17917911 42 39840769 9840769
6 9 -1 14 4 36 6 111 666 4000000 5000000 2009 7 -1 2 2 -1 6274 9834495
1 second
256 megabytes
['math', 'number theory', '*1100']
B. The Corridor or There and Back Againtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are in a corridor that extends infinitely to the right, divided into square rooms. You start in room 1, proceed to room k, and then return to room 1. You can choose the value of k. Moving to an adjacent room takes 1 second.Additionally, there are n traps in the corridor: the i-th trap is located in room d_i and will be activated s_i seconds after you enter the room \boldsymbol{d_i}. Once a trap is activated, you cannot enter or exit a room with that trap. A schematic representation of a possible corridor and your path to room k and back. Determine the maximum value of k that allows you to travel from room 1 to room k and then return to room 1 safely.For instance, if n=1 and d_1=2, s_1=2, you can proceed to room k=2 and return safely (the trap will activate at the moment 1+s_1=1+2=3, it can't prevent you to return back). But if you attempt to reach room k=3, the trap will activate at the moment 1+s_1=1+2=3, preventing your return (you would attempt to enter room 2 on your way back at second 3, but the activated trap would block you). Any larger value for k is also not feasible. Thus, the answer is k=2.InputThe first line of the input contains an integer t (1 \le t \le 1000) — the number of test cases.The descriptions of the test cases follow.The first line of each test case description contains an integer n (1 \le n \le 100) — the number of traps.The following n lines of each test case description present two integers d_i and s_i (1 \le d_i, s_i \le 200) — the parameters of a trap (you must leave room d_i strictly before s_i seconds have passed since entering this room). It's possible for multiple traps to occupy a single room (the values of d_i can be repeated).OutputFor each test case, print the maximum value of k that allows you to travel to room k and return to room 1 without encountering an active trap.ExampleInput 712 232 84 35 21200 20041 205 93 179100 1210 11 1821 11 231 31 11 3Output 2 5 299 9 9 1 1 NoteThe first test case is explained in the problem statement above.In the second test case, the second trap prevents you from achieving k\ge6. If k\ge6, the second trap will activate at the moment 3+s_2=3+3=6 (the time you enter room 4 plus s_2). In the case of k\ge6, you will return to room 4 at time 7 or later. The trap will be active at that time. It can be shown that room k=5 can be reached without encountering an active trap.In the third test case, you can make it to room 299 and then immediately return to room 1.
712 232 84 35 21200 20041 205 93 179100 1210 11 1821 11 231 31 11 3
2 5 299 9 9 1 1
2 seconds
256 megabytes
['greedy', 'implementation', '*900']
A. Two Vesselstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have two vessels with water. The first vessel contains a grams of water, and the second vessel contains b grams of water. Both vessels are very large and can hold any amount of water.You also have an empty cup that can hold up to c grams of water.In one move, you can scoop up to c grams of water from any vessel and pour it into the other vessel. Note that the mass of water poured in one move does not have to be an integer.What is the minimum number of moves required to make the masses of water in the vessels equal? Note that you cannot perform any actions other than the described moves.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 1000). The description of the test cases follows.Each test case consists of a single line containing three integers a, b, and c (1 \le a, b, c \le 100) — the mass of water in the vessels and the capacity of the cup, respectively.OutputFor each test case, output a single number — the minimum number of moves required to make the masses of water in the vessels equal. It can be shown, that it is always possible.ExampleInput 63 7 217 4 317 17 117 21 1001 100 197 4 3Output 1 3 0 1 50 16 NoteIn the first test case, only one move is enough: if we pour 2 grams of water from the second vessel into the first one, both vessels will contain 5 grams of water.In the second example test case, three moves are enough: Pour 3 grams of water from the first vessel into the second one. After this move, the first vessel will contain 17 - 3 = 14 grams of water, and the second vessel will contain 4 + 3 = 7 grams. Pour 2 grams of water from the first vessel into the second one. After this move, the first vessel will contain 14 - 2 = 12 grams of water, and the second vessel will contain 7 + 2 = 9 grams. Finally, pour 1.5 grams of water from the first vessel into the second one. After this move, the first vessel will contain 12 - 1.5 = 10.5 grams of water, and the second vessel will contain 9 + 1.5 = 10.5 grams.Note that this is not the only way to equalize the vessels in 3 moves, but there is no way to do it in 2 moves.In the third example test case, the vessels initially contain the same amount of water, so no moves are needed. The answer is 0.
63 7 217 4 317 17 117 21 1001 100 197 4 3
1 3 0 1 50 16
1 second
256 megabytes
['brute force', 'greedy', 'math', '*800']
H. Standard Graph Problemtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a weighted directed graph with n vertices and m edges. Each vertex in the graph can be either highlighted or normal. Initially, all vertices are normal. The cost of the graph is defined as the minimum sum of edge weights that need to be selected so that from each normal vertex one can reach at least one highlighted vertex using the selected edges only. If it is not possible to select the edges, the cost is -1 instead.Your task is to compute the cost of the graph after each of the q queries. The queries can be of two types: +\;v_i makes vertex v_i highlighted; it is guaranteed that the vertex is normal before the query. -\;v_i makes vertex v_i normal; it is guaranteed that the vertex is highlighted before the query. Output the cost of the graph after each of the q queries.InputThe first line contains three integers n, m, and q (3 \le n \le 2 \cdot 10^5, 1 \le m, q \le 2 \cdot 10^5) — the number of vertices in the graph, the number of edges, and the number of queries.The next m lines contain the edges of the graph, one edge per line. The i-th line contains three integers u_i, v_i, and c_i (1 \leq u_i, v_i \leq n, u_i \ne v_i, 1 \leq c_i \leq 10^6) — the endpoints of the i-th edge (from u_i to v_i) and its weight (c_i).The next q lines contain the queries, one query per line. The i-th line contains +\;v_i, if it is a query of the first type, and -\;v_i, if it is a query of the second type (1 \leq v_i \leq n).OutputOutput q numbers. The i-th number is the cost of the graph after the first i queries.ExamplesInput 4 5 61 2 12 3 53 2 34 1 82 1 4+ 1- 1+ 3+ 1+ 4+ 2Output 15 -1 14 12 4 0 Input 10 14 108 6 42 5 13 5 41 6 31 3 77 2 16 1 34 10 14 6 55 4 15 8 1010 9 19 5 19 7 6+ 7+ 8- 7+ 10+ 2- 10+ 5- 2- 5+ 3Output 28 24 29 19 18 24 18 19 29 20 NoteIn the first test: After the first query, it is most profitable to select the edges with numbers 3, 4, 5, their total cost is 15. After the second query, there are no highlighted vertices, which means that there are no suitable sets of edges, the cost of the graph is -1. After the third query, it is most profitable to select the edges with numbers 1, 2, 5, their total cost is 14. After the fourth query, it is most profitable to select the edges with numbers 4 and 5, their total cost is 12. After the fifth query, it is most profitable to select only edge number 5, its cost is 4. After the sixth query, all vertices are highlighted and there is no need to select any edges, the cost of the graph is 0.
4 5 61 2 12 3 53 2 34 1 82 1 4+ 1- 1+ 3+ 1+ 4+ 2
15 -1 14 12 4 0
2 seconds
512 megabytes
['data structures', 'graphs', 'greedy', 'trees', '*3500']
G. MEXanizationtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputLet's define f(S). Let S be a multiset (i.e., it can contain repeated elements) of non-negative integers. In one operation, you can choose any non-empty subset of S (which can also contain repeated elements), remove this subset (all elements in it) from S, and add the MEX of the removed subset to S. You can perform any number of such operations. After all the operations, S should contain exactly 1 number. f(S) is the largest number that could remain in S after any sequence of operations.You are given an array of non-negative integers a of length n. For each of its n prefixes, calculate f(S) if S is the corresponding prefix (for the i-th prefix, S consists of the first i elements of array a).The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of [2,2,1] is 0, because 0 does not belong to the array. The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not. The MEX of [0,3,1,2] is 4, because 0, 1, 2 and 3 belong to the array, but 4 does not. InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains an integer n (1 \leq n \leq 2 \cdot 10^5) — the size of array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \leq a_i \leq 2 \cdot 10^5) — the array a.It is guaranteed that the sum of all n values across all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output n numbers: f(S) for each of the n prefixes of array a.ExampleInput 48179 57 2 0 2 3 2 31031 0 381 0 1 2 4 3 0 2Output 179 2 3 3 3 4 4 5 1 1 2 2 1 2 2 3 3 5 5 5 NoteConsider the first test case. For a prefix of length 1, the initial multiset is \{179\}. If we do nothing, we get 179.For a prefix of length 2, the initial multiset is \{57, 179\}. Using the following sequence of operations, we can obtain 2. Apply the operation to \{57\}, the multiset becomes \{0, 179\}. Apply the operation to \{179\}, the multiset becomes \{0, 0\}. Apply the operation to \{0\}, the multiset becomes \{0, 1\}. Apply the operation to \{0, 1\}, the multiset becomes \{2\}. This is our answer. Consider the second test case. For a prefix of length 1, the initial multiset is \{0\}. If we apply the operation to \{0\}, the multiset becomes \{1\}. This is the answer.
48179 57 2 0 2 3 2 31031 0 381 0 1 2 4 3 0 2
179 2 3 3 3 4 4 5 1 1 2 2 1 2 2 3 3 5 5 5
2 seconds
512 megabytes
['data structures', '*3300']
F. Lazy Numberstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given positive integers n and k. For each number from 1 to n, we write its representation in the number system with base k (without leading zeros) and then sort the resulting array in lexicographic order as strings. In the sorted array, we number the elements from 1 to n (i.e., indexing starts from 1). Find the number of values i such that the representation of number i is at the i-th position in the sorted array of representations.Examples of representations: 1 in any number system is equal to 1, 7 with k = 3 is written as 21, and 81 with k = 9 is written as 100.InputThe first line contains a single integer t (1 \leq t \leq 10^3) — the number of test cases. This is followed by a description of the test cases.The only line of each test case contains integers n and k (1 \leq n \leq 10^{18}, 2 \leq k \leq 10^{18}).OutputFor each test case, output a single integer — the number of values 1 \leq i \leq n such that the representation of number i in the number system with base k is at the i-th position after sorting.ExampleInput 82 24 26 433 2532 13780011804570805480 3788366364720306464627 4702032149561577293940402103595405 2Output 2 2 1 3 1 3789 1 7 NoteIn the first test case, for numbers 1 and 2, their representations are at positions 1 and 2 respectively.In the second test case, the sorted array is [1_2 = 1, 10_2 = 2, 100_2 = 4, 11_2 = 3], and only the representations of numbers 1 and 2 are at the required positions.In the third test case, the sorted array is [1_4 = 1, 10_4 = 4, 11_4 = 5, 12_4 = 6, 2_4 = 2, 3_4 = 3], and only the number 1 is at its position.
82 24 26 433 2532 13780011804570805480 3788366364720306464627 4702032149561577293940402103595405 2
2 2 1 3 1 3789 1 7
3 seconds
256 megabytes
['binary search', 'math', '*2900']
E. Another MEX Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of integers a of size n. You can choose a set of non-overlapping subarrays of the given array (note that some elements may be not included in any subarray, this is allowed). For each selected subarray, calculate the MEX of its elements, and then calculate the bitwise XOR of all the obtained MEX values. What is the maximum bitwise XOR that can be obtained?The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of [2,2,1] is 0, because 0 does not belong to the array. The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not. The MEX of [0,3,1,2] is 4, because 0, 1, 2 and 3 belong to the array, but 4 does not. InputThe first line contains an integer t (1 \leq t \leq 5000) — the number of test cases. This is followed by the description of the test cases.The first line of each test case contains an integer n (1 \leq n \leq 5000) — the size of the array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \leq a_i \leq n) — the array a.It is guaranteed that the sum of all n values across all test cases does not exceed 5000.OutputFor each test case, output a single number — the maximum possible XOR of the MEX values of the selected subarrays.ExampleInput 421 0101 2 0 7 1 2 0 2 4 3102 1 0 7 1 2 0 2 4 331 2 1Output 2 6 7 0 NoteIn the first test case, the maximum XOR is 2 if we take the entire array, \operatorname{MEX}([1, 0]) = 2.In the second test case, the maximum XOR is 6 if we partition the array into segments [1, 2, 0] and [7, 1, 2, 0, 2, 4, 3]: \operatorname{MEX}([1, 2, 0]) = 3, \operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5, therefore, the XOR is 5 \oplus 3=6.In the third test case, the maximum XOR is 7 if we partition the array into segments [1, 0] and [7, 1, 2, 0, 2, 4, 3]: \operatorname{MEX}([1, 0]) = 2, \operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5, therefore, the XOR is 5 \oplus 2 = 7.
421 0101 2 0 7 1 2 0 2 4 3102 1 0 7 1 2 0 2 4 331 2 1
2 6 7 0
1 second
256 megabytes
['bitmasks', 'brute force', 'dp', 'shortest paths', '*2300']
D. Prefix Purchasetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have an array a of size n, initially filled with zeros (a_1 = a_2 = \ldots = a_n = 0). You also have an array of integers c of size n.Initially, you have k coins. By paying c_i coins, you can add 1 to all elements of the array a from the first to the i-th element (a_j \mathrel{+}= 1 for all 1 \leq j \leq i). You can buy any c_i any number of times. A purchase is only possible if k \geq c_i, meaning that at any moment k \geq 0 must hold true.Find the lexicographically largest array a that can be obtained.An array a is lexicographically smaller than an array b of the same length if and only if in the first position where a and b differ, the element in array a is smaller than the corresponding element in b.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. This is followed by a description of the test cases.The first line of each test case contains a single integer n (1 \leq n \leq 2 \cdot 10^5) — the size of arrays a and c.The second line of each test case contains n integers c_1, c_2, \ldots, c_n (1 \leq c_i \leq 10^9) — the array c.The third line of each test case contains a single integer k (1 \leq k \leq 10^9) — the number of coins you have.It is guaranteed that the sum of all n values across all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output n integers a_1, a_2, \ldots, a_n — the lexicographically largest array a that can be obtained.ExampleInput 431 2 3523 4733 2 12610 6 4 6 3 47Output 5 0 0 2 1 2 2 2 2 2 2 2 2 1 NoteIn the first test case, a_1 cannot be greater than 5, and if we buy c_1 five times, we will run out of money, so a = [5, 0, 0].In the second test case, a_1 cannot be greater than 2, but we can buy c_1 and c_2 once each (buying c_2 twice is not possible), so a = [2, 1].
431 2 3523 4733 2 12610 6 4 6 3 47
5 0 0 2 1 2 2 2 2 2 2 2 2 1
1 second
256 megabytes
['greedy', 'implementation', 'sortings', '*1800']
C. Colorful Tabletime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers n and k. You are also given an array of integers a_1, a_2, \ldots, a_n of size n. It is known that for all 1 \leq i \leq n, 1 \leq a_i \leq k.Define a two-dimensional array b of size n \times n as follows: b_{i, j} = \min(a_i, a_j). Represent array b as a square, where the upper left cell is b_{1, 1}, rows are numbered from top to bottom from 1 to n, and columns are numbered from left to right from 1 to n. Let the color of a cell be the number written in it (for a cell with coordinates (i, j), this is b_{i, j}).For each color from 1 to k, find the smallest rectangle in the array b containing all cells of this color. Output the sum of width and height of this rectangle.InputThe first line contains a single integer t (1 \leq t \leq 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 \leq n, k \leq 10^5) — the size of array a and the number of colors.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq k) — the array a.It is guaranteed that the sum of the values of n and k over all test cases does not exceed 10^5.OutputFor each test case, output k numbers: the sums of width and height of the smallest rectangle containing all cells of a color, for each color from 1 to k.ExampleInput 52 11 12 21 23 53 2 44 21 2 1 25 31 2 3 2 1Output 4 4 2 0 6 6 2 0 8 6 10 6 2 NoteIn the first test case, the entire array b consists of color 1, so the smallest rectangle for color 1 has a size of 2 \times 2, and the sum of its sides is 4.In the second test case, the array b looks like this:1112One of the corner cells has color 2, and the other three cells have color 1. Therefore, the smallest rectangle for color 1 has a size of 2 \times 2, and for color 2 it is 1 \times 1.In the last test case, the array b looks like this:1111112221123211222111111
52 11 12 21 23 53 2 44 21 2 1 25 31 2 3 2 1
4 4 2 0 6 6 2 0 8 6 10 6 2
1 second
256 megabytes
['binary search', 'data structures', 'dp', 'implementation', 'math', 'two pointers', '*1300']
B. Friendly Arraystime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two arrays of integers — a_1, \ldots, a_n of length n, and b_1, \ldots, b_m of length m. You can choose any element b_j from array b (1 \leq j \leq m), and for all 1 \leq i \leq n perform a_i = a_i | b_j. You can perform any number of such operations.After all the operations, the value of x = a_1 \oplus a_2 \oplus \ldots \oplus a_n will be calculated. Find the minimum and maximum values of x that could be obtained after performing any set of operations.Above, | is the bitwise OR operation, and \oplus is the bitwise XOR operation.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. This is followed by the description of the test cases.The first line of each test case contains two integers n and m (1 \leq n, m \leq 2 \cdot 10^5) — the sizes of arrays a and b.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 array a.The third line of each test case contains m integers b_1, b_2, \ldots, b_m (0 \leq b_i \leq 10^9) — the array b.It is guaranteed that the sum of values of n and m across all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output 2 numbers: the minimum and maximum possible values of x after performing any set of operations.ExampleInput 22 30 11 2 33 11 1 21Output 0 1 2 3 NoteIn the first test case, if we apply the operation with element b_1 = 1, the array a will become [1, 1], and x will be 0. If no operations are applied, then x = 1.In the second test case, if no operations are applied, then x = 2. If we apply the operation with b_1 = 1, then a = [1, 1, 3], and x = 3.
22 30 11 2 33 11 1 21
0 1 2 3
2 seconds
256 megabytes
['bitmasks', 'greedy', 'math', '*1200']
A. MEXanized Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three non-negative integers n, k, and x. Find the maximum possible sum of elements in an array consisting of non-negative integers, which has n elements, its MEX is equal to k, and all its elements do not exceed x. If such an array does not exist, output -1.The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of [2,2,1] is 0, because 0 does not belong to the array. The MEX of [3,1,0,1] is 2, because 0 and 1 belong to the array, but 2 does not. The MEX of [0,3,1,2] is 4, because 0, 1, 2 and 3 belong to the array, but 4 does not. InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases. Then follows the description of the test cases.The only line of each test case contains three integers n, k, and x (1 \leq n, k, x \leq 200).OutputFor each test case, output a single number — the maximum sum of elements in a valid array, or -1, if such an array does not exist.ExampleInput 95 3 34 7 54 2 2812 10 657 51 122200 1 2002 2 13 2 14 7 10Output 7 -1 57 -1 2007 39800 1 2 -1 NoteIn the first test case, the maximum sum is 7, and one of the valid arrays is [0, 1, 2, 2, 2].In the second test case, there are no valid arrays of length n.In the third test case, the maximum sum is 57, and one of the valid arrays is [0, 1, 28, 28].
95 3 34 7 54 2 2812 10 657 51 122200 1 2002 2 13 2 14 7 10
7 -1 57 -1 2007 39800 1 2 -1
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', '*800']
B. 2D Travelingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputPiggy lives on an infinite plane with the Cartesian coordinate system on it.There are n cities on the plane, numbered from 1 to n, and the first k cities are defined as major cities. The coordinates of the i-th city are (x_i,y_i).Piggy, as a well-experienced traveller, wants to have a relaxing trip after Zhongkao examination. Currently, he is in city a, and he wants to travel to city b by air. You can fly between any two cities, and you can visit several cities in any order while travelling, but the final destination must be city b.Because of active trade between major cities, it's possible to travel by plane between them for free. Formally, the price of an air ticket f(i,j) between two cities i and j is defined as follows: f(i,j)= \begin{cases} 0, & \text{if cities }i \text{ and }j \text{ are both major cities} \\ |x_i-x_j|+|y_i-y_j|, & \text{otherwise} \end{cases} Piggy doesn't want to save time, but he wants to save money. So you need to tell him the minimum value of the total cost of all air tickets if he can take any number of flights.InputThe first line of input contains a single integer t (1\le t\le 10^4) — the number of test cases. The description of test cases follows.The first line of each test case contains four integers n, k, a and b (2\le n\le 2\cdot 10^5, 0\le k\le n, 1\le a,b\le n, a\ne b) — the number of cities, the number of major cities and the numbers of the starting and the ending cities.Then n lines follow, the i-th line contains two integers x_i and y_i (-10^9\le x_i,y_i\le 10^9) — the coordinates of the i-th city. The first k lines describe major cities. It is guaranteed that all coordinates are pairwise distinct.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot 10^5.OutputFor each test case, print a single integer — the minimum value of the total price of all air tickets.ExampleInput 56 2 3 50 01 -2-2 1-1 32 -2-3 -32 0 1 2-1000000000 -10000000001000000000 10000000007 5 4 2154 147-154 -147123 45620 2343 20998 244353 1003 1 3 10 101 202 304 3 2 40 0-100 100-1 -1-1 0Output 4 4000000000 0 22 1 NoteIn the first test case: The major cities are marked red. The optimal way to choose the flights is: 3\rightarrow 1 \rightarrow 2 \rightarrow 5, which will cost 3+0+1=4. Note that the flight 1\rightarrow 2 costs 0, because both city 1 and 2 are major cities.In the second test case, since there are only 2 cities, the only way is to take a flight from city 1 to 2.In the third test case, since city 2 and 4 are both major cities, Piggy can directly take a flight from city 2 to 4, which costs 0.In the fourth test case, Piggy can choose to take the following flights: 3\rightarrow 2\rightarrow 1, and the cost is 11+11=22.
56 2 3 50 01 -2-2 1-1 32 -2-3 -32 0 1 2-1000000000 -10000000001000000000 10000000007 5 4 2154 147-154 -147123 45620 2343 20998 244353 1003 1 3 10 101 202 304 3 2 40 0-100 100-1 -1-1 0
4 4000000000 0 22 1
1 second
256 megabytes
['geometry', 'math', 'shortest paths', 'sortings', '*1100']
A. Make It Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputDuring Zhongkao examination, Reycloer met an interesting problem, but he cannot come up with a solution immediately. Time is running out! Please help him.Initially, you are given an array a consisting of n \ge 2 integers, and you want to change all elements in it to 0.In one operation, you select two indices l and r (1\le l\le r\le n) and do the following: Let s=a_l\oplus a_{l+1}\oplus \ldots \oplus a_r, where \oplus denotes the bitwise XOR operation; Then, for all l\le i\le r, replace a_i with s. You can use the operation above in any order at most 8 times in total.Find a sequence of operations, such that after performing the operations in order, all elements in a are equal to 0. It can be proven that the solution always exists.InputThe first line of input contains a single integer t (1\le t\le 500) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer n (2\le n\le 100) — the length of the array a.The second line of each test case contains n integers a_1,a_2,\ldots,a_n (0\le a_i\le 100) — the elements of the array a.OutputFor each test case, in the first line output a single integer k (0\le k\le 8) — the number of operations you use.Then print k lines, in the i-th line output two integers l_i and r_i (1\le l_i\le r_i\le n) representing that you select l_i and r_i in the i-th operation. Note that you do not have to minimize k. If there are multiple solutions, you may output any of them.ExampleInput 641 2 3 083 1 4 1 5 9 2 661 5 4 1 4 750 0 0 0 071 1 9 9 0 1 83100 100 0Output 1 1 4 2 4 7 1 8 6 1 2 3 4 5 6 1 3 4 6 1 6 0 4 1 2 6 7 3 4 6 7 1 1 2 NoteIn the first test case, since 1\oplus2\oplus3\oplus0=0, after performing the operation on segment [1,4], all the elements in the array are equal to 0.In the second test case, after the first operation, the array becomes equal to [3,1,4,15,15,15,15,6], after the second operation, the array becomes equal to [0,0,0,0,0,0,0,0].In the third test case: Operationa beforea after1[\underline{1,5},4,1,4,7]\rightarrow[4,4,4,1,4,7]2[4,4,\underline{4,1},4,7]\rightarrow[4,4,5,5,4,7]3[4,4,5,5,\underline{4,7}]\rightarrow[4,4,5,5,3,3]4[\underline{4,4,5},5,3,3]\rightarrow[5,5,5,5,3,3]5[5,5,5,\underline{5,3,3}]\rightarrow[5,5,5,5,5,5]6[\underline{5,5,5,5,5,5}]\rightarrow[0,0,0,0,0,0] In the fourth test case, the initial array contains only 0, so we do not need to perform any operations with it.
641 2 3 083 1 4 1 5 9 2 661 5 4 1 4 750 0 0 0 071 1 9 9 0 1 83100 100 0
1 1 4 2 4 7 1 8 6 1 2 3 4 5 6 1 3 4 6 1 6 0 4 1 2 6 7 3 4 6 7 1 1 2
1 second
256 megabytes
['constructive algorithms', '*900']