Problem Description stringlengths 139 7.17k | Rating float64 800 3.5k | Tags sequencelengths 1 7 | math bool 2 classes | greedy bool 2 classes | implementation bool 2 classes | dp bool 2 classes | data structures bool 2 classes | constructive algorithms bool 2 classes | brute force bool 2 classes | binary search bool 2 classes | sortings bool 2 classes | graphs bool 2 classes |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
You are given two even integers $$$n$$$ and $$$m$$$. Your task is to find any binary matrix $$$a$$$ with $$$n$$$ rows and $$$m$$$ columns where every cell $$$(i,j)$$$ has exactly two neighbours with a different value than $$$a_{i,j}$$$. Two cells in the matrix are considered neighbours if and only if they share a side. More formally, the neighbours of cell $$$(x,y)$$$ are: $$$(x-1,y)$$$, $$$(x,y+1)$$$, $$$(x+1,y)$$$ and $$$(x,y-1)$$$. It can be proven that under the given constraints, an answer always exists. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases. The following lines contain the descriptions of the test cases. The only line of each test case contains two even integers $$$n$$$ and $$$m$$$ ($$$2 le n,m le 50$$$)xa0β the height and width of the binary matrix, respectively. Output For each test case, print $$$n$$$ lines, each of which contains $$$m$$$ numbers, equal to $$$0$$$ or $$$1$$$xa0β any binary matrix which satisfies the constraints described in the statement. It can be proven that under the given constraints, an answer always exists. Example Output 1 0 0 1 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 Note White means $$$0$$$, black means $$$1$$$. | 900 | [
"constructive algorithms"
] | false | false | false | false | false | true | false | false | false | false |
Let's call a sequence $$$b_1, b_2, b_3 dots, b_{k - 1}, b_k$$$ almost increasing if $$$$$$min(b_1, b_2) le min(b_2, b_3) le dots le min(b_{k - 1}, b_k).$$$$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers $$$a_1, a_2, dots, a_n$$$. Calculate the length of its longest almost increasing subsequence. You'll be given $$$t$$$ test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of independent test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 5 cdot 10^5$$$)xa0β 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 n$$$)xa0β the sequence itself. It's guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$5 cdot 10^5$$$. Output For each test case, print one integerxa0β the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Note In the first test case, one of the optimal answers is subsequence $$$1, 2, 7, 2, 2, 3$$$. In the second and third test cases, the whole sequence $$$a$$$ is already almost increasing. | 2,200 | [
"data structures",
"dp"
] | false | false | false | true | true | false | false | false | false | false |
Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of _w_ milliliters and 2_n_ tea cups, each cup is for one of Pasha's friends. The _i_-th cup can hold at most _a__i_ milliliters of water. It turned out that among Pasha's friends there are exactly _n_ boys and exactly _n_ girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows: Pasha can boil the teapot exactly once by pouring there at most _w_ milliliters of water; Pasha pours the same amount of water to each girl; Pasha pours the same amount of water to each boy; if each girl gets _x_ milliliters of water, then each boy gets 2_x_ milliliters of water. In the other words, each boy should get two times more water than each girl does. Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. Input The first line of the input contains two integers, _n_ and _w_ (1u2009β€u2009_n_u2009β€u2009105, 1u2009β€u2009_w_u2009β€u2009109)xa0β the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters. The second line of the input contains the sequence of integers _a__i_ (1u2009β€u2009_a__i_u2009β€u2009109, 1u2009β€u2009_i_u2009β€u20092_n_)xa0βxa0the capacities of Pasha's tea cups in milliliters. Output Print a single real number β the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10u2009-u20096. Note Pasha also has candies that he is going to give to girls but that is another task... | 1,500 | [
"sortings"
] | false | false | false | false | false | false | false | false | true | false |
Problem - 573B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags binary search data structures dp math *1600 No tag edit access β Contest materials adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer _n_ (1u2009β€u2009_n_u2009β€u2009105). The second line contains _n_ space-separated integers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009β€u2009_h__i_u2009β€u2009109) β sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation. Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 10:36:03 (j2). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name --- | 1,600 | [
"binary search"
] | false | false | false | false | false | false | false | true | false | false |
Ivan has _n_ different boxes. The first of them contains some balls of _n_ different colors. Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every _i_ (1u2009β€u2009_i_u2009β€u2009_n_) _i_-th box will contain all balls with color _i_. In order to do this, Ivan will make some turns. Each turn he does the following: 1. Ivan chooses any non-empty box and takes all balls from this box; 2. Then Ivan chooses any _k_ empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into _k_ non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either _k_u2009=u20092 or _k_u2009=u20093. The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes. Help Ivan to determine the minimum possible penalty of the game! Input The first line contains one integer number _n_ (1u2009β€u2009_n_u2009β€u2009200000) β the number of boxes and colors. The second line contains _n_ integer numbers _a_1, _a_2, ..., _a__n_ (1u2009β€u2009_a__i_u2009β€u2009109), where _a__i_ is the number of balls with color _i_. Output Print one number β the minimum possible penalty of the game. Note In the first example you take all the balls from the first box, choose _k_u2009=u20093 and sort all colors to corresponding boxes. Penalty is 6. In the second example you make two turns: 1. Take all the balls from the first box, choose _k_u2009=u20093, put balls of color 3 to the third box, of color 4 β to the fourth box and the rest put back into the first box. Penalty is 14; 2. Take all the balls from the first box, choose _k_u2009=u20092, put balls of color 1 to the first box, of color 2 β to the second box. Penalty is 5. Total penalty is 19. | 2,300 | [
"data structures"
] | false | false | false | false | true | false | false | false | false | false |
Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city. Let's represent the city as an area of $$$n imes n$$$ square blocks. Yura needs to move from the block with coordinates $$$(s_x,s_y)$$$ to the block with coordinates $$$(f_x,f_y)$$$. In one minute Yura can move to any neighboring by side block; in other words, he can move in four directions. Also, there are $$$m$$$ instant-movement locations in the city. Their coordinates are known to you and Yura. Yura can move to an instant-movement location in no time if he is located in a block with the same coordinate $$$x$$$ or with the same coordinate $$$y$$$ as the location. Help Yura to find the smallest time needed to get home. Input The first line contains two integers $$$n$$$ and $$$m$$$xa0β the size of the city and the number of instant-movement locations ($$$1 le n le 10^9$$$, $$$0 le m le 10^5$$$). The next line contains four integers $$$s_x$$$ $$$s_y$$$ $$$f_x$$$ $$$f_y$$$xa0β the coordinates of Yura's initial position and the coordinates of his home ($$$ 1 le s_x, s_y, f_x, f_y le n$$$). Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ $$$y_i$$$xa0β coordinates of the $$$i$$$-th instant-movement location ($$$1 le x_i, y_i le n$$$). Output In the only line print the minimum time required to get home. Examples Input 5 3 1 1 5 5 1 2 4 1 3 3 Input 84 5 67 59 41 2 39 56 7 2 15 3 74 18 22 7 Note In the first example Yura needs to reach $$$(5, 5)$$$ from $$$(1, 1)$$$. He can do that in $$$5$$$ minutes by first using the second instant-movement location (because its $$$y$$$ coordinate is equal to Yura's $$$y$$$ coordinate), and then walking $$$(4, 1) o (4, 2) o (4, 3) o (5, 3) o (5, 4) o (5, 5)$$$. | 2,300 | [
"graphs",
"sortings"
] | false | false | false | false | false | false | false | false | true | true |
Sakurako's exams are over, and she did excellently. As a reward, she received a permutation $$$p$$$. Kosuke was not entirely satisfied because he failed one exam and did not receive a gift. He decided to sneak into her room (thanks to the code for her lock) and spoil the permutation so that it becomes simple. A permutation $$$p$$$ is considered simple if for every $$$i$$$ $$$(1le i le n)$$$ one of the following conditions holds: $$$p_i=i$$$ $$$p_{p_i}=i$$$ For example, the permutations $$$[1, 2, 3, 4]$$$, $$$[5, 2, 4, 3, 1]$$$, and $$$[2, 1]$$$ are simple, while $$$[2, 3, 1]$$$ and $$$[5, 2, 1, 4, 3]$$$ are not. In one operation, Kosuke can choose indices $$$i,j$$$ $$$(1le i,jle n)$$$ and swap the elements $$$p_i$$$ and $$$p_j$$$. Sakurako is about to return home. Your task is to calculate the minimum number of operations that Kosuke needs to perform to make the permutation simple. Input The first line contains one integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. Each test case is described by two lines. The first line contains one integer $$$n$$$ ($$$1le n le 10^6$$$)xa0β the length of the permutation $$$p$$$. The second line contains $$$n$$$ integers $$$p_i$$$ ($$$1le p_ile n$$$)xa0β the elements of the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^6$$$. It is guaranteed that $$$p$$$ is a permutation. Note In the first and second examples, the permutations are already simple. In the fourth example, it is sufficient to swap $$$p_2$$$ and $$$p_4$$$. Thus, the permutation will become $$$[2, 1, 4, 3]$$$ in $$$1$$$ operation. | 1,400 | [
"brute force",
"data structures",
"graphs",
"greedy",
"math"
] | true | true | false | false | true | false | true | false | false | true |
Boboniu gives you $$$r$$$ red balls, $$$g$$$ green balls, $$$b$$$ blue balls, $$$w$$$ white balls. He allows you to do the following operation as many times as you want: Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations. Input The first line contains one integer $$$T$$$ ($$$1le Tle 100$$$) denoting the number of test cases. For each of the next $$$T$$$ cases, the first line contains four integers $$$r$$$, $$$g$$$, $$$b$$$ and $$$w$$$ ($$$0le r,g,b,wle 10^9$$$). Output For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No". Example Input 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 Note In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome. In the second test case, after doing one operation, changing $$$(8,1,9,3)$$$ to $$$(7,0,8,6)$$$, one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr". A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome. | 1,000 | [
"brute force"
] | false | false | false | false | false | false | true | false | false | false |
You have been offered a job in a company developing a large social network. Your first task is connected with searching profiles that most probably belong to the same user. The social network contains _n_ registered profiles, numbered from 1 to _n_. Some pairs there are friends (the "friendship" relationship is mutual, that is, if _i_ is friends with _j_, then _j_ is also friends with _i_). Let's say that profiles _i_ and _j_ (_i_u2009β u2009_j_) are doubles, if for any profile _k_ (_k_u2009β u2009_i_, _k_u2009β u2009_j_) one of the two statements is true: either _k_ is friends with _i_ and _j_, or _k_ isn't friends with either of them. Also, _i_ and _j_ can be friends or not be friends. Your task is to count the number of different unordered pairs (_i_,u2009_j_), such that the profiles _i_ and _j_ are doubles. Note that the pairs are unordered, that is, pairs (_a_,u2009_b_) and (_b_,u2009_a_) are considered identical. Input The first line contains two space-separated integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u2009106, 0u2009β€u2009_m_u2009β€u2009106), β the number of profiles and the number of pairs of friends, correspondingly. Next _m_ lines contains descriptions of pairs of friends in the format "_v_ _u_", where _v_ and _u_ (1u2009β€u2009_v_,u2009_u_u2009β€u2009_n_,u2009_v_u2009β u2009_u_) are numbers of profiles that are friends with each other. It is guaranteed that each unordered pair of friends occurs no more than once and no profile is friends with itself. Output Print the single integer β the number of unordered pairs of profiles that are doubles. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the %I64d specificator. Note In the first and second sample any two profiles are doubles. In the third sample the doubles are pairs of profiles (1,u20093) and (2,u20094). | 2,300 | [
"graphs",
"sortings"
] | false | false | false | false | false | false | false | false | true | true |
Little Sofia is in fourth grade. Today in the geometry lesson she learned about segments and squares. On the way home, she decided to draw $$$n$$$ squares in the snow with a side length of $$$1$$$. For simplicity, we assume that Sofia lives on a plane and can draw only segments of length $$$1$$$, parallel to the coordinate axes, with vertices at integer points. In order to draw a segment, Sofia proceeds as follows. If she wants to draw a vertical segment with the coordinates of the ends $$$(x, y)$$$ and $$$(x, y+1)$$$. Then Sofia looks if there is already a drawn segment with the coordinates of the ends $$$(x', y)$$$ and $$$(x', y+1)$$$ for some $$$x'$$$. If such a segment exists, then Sofia quickly draws a new segment, using the old one as a guideline. If there is no such segment, then Sofia has to take a ruler and measure a new segment for a long time. Same thing happens when Sofia wants to draw a horizontal segment, but only now she checks for the existence of a segment with the same coordinates $$$x$$$, $$$x+1$$$ and the differing coordinate $$$y$$$. For example, if Sofia needs to draw one square, she will have to draw two segments using a ruler: After that, she can draw the remaining two segments, using the first two as a guide: If Sofia needs to draw two squares, she will have to draw three segments using a ruler: After that, she can draw the remaining four segments, using the first three as a guide: Sofia is in a hurry, so she wants to minimize the number of segments that she will have to draw with a ruler without a guide. Help her find this minimum number. | 1,100 | [
"binary search",
"constructive algorithms"
] | false | false | false | false | false | true | false | true | false | false |
After winning another Bed Wars game, Masha and Olya wanted to relax and decided to play a new game. Masha gives Olya an array $$$a$$$ of length $$$n$$$ and a number $$$s$$$. Now Olya's task is to find a non-negative number $$$x$$$ such that $$$displaystylesum_{i=1}^{n} a_i oplus x = s$$$. But she is very tired after a tight round, so please help her with this. But this task seemed too simple to them, so they decided to make the numbers larger (up to $$$2^k$$$) and provide you with their binary representation. Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k, n cdot k le 2 cdot 10^6$$$)xa0β the length of the array $$$a$$$ and the length of the binary representation of all numbers. The second line contains a string of length $$$k$$$, consisting of zeros and onesxa0β the binary representation of the number $$$s$$$, starting from the most significant bits. The next $$$n$$$ lines also contain strings of length $$$k$$$, consisting of zeros and ones, the $$$i$$$-th of these strings contains the binary representation of the number $$$a_i$$$, starting from the most significant bits. It is guaranteed that the sum of the values $$$n cdot k$$$ for all test cases does not exceed $$$2 cdot 10^6$$$. Output For each test case, output a string of length $$$k$$$ on a separate line, consisting of zeros or onesxa0β the binary representation of any suitable number $$$x$$$ ($$$x ge 0$$$), starting from the most significant bits, or $$$-1$$$ if such $$$x$$$ does not exist. Example Input 4 4 5 01011 01110 00110 01100 01111 2 8 00101001 10111111 10011110 5 4 0101 0010 0000 0000 0010 0011 6 5 00011 10110 11001 01010 11100 10011 10000 Output 01110 10011010 0010 -1 Note In the first test case, $$$s = 11, a = [14, 6, 12, 15]$$$, if $$$x = 14$$$, then $$$displaystylesum_{i=1}^{n} a_i oplus x = (14 oplus 14) + (6 oplus 14) + (12 oplus 14) + (15 oplus 14) = 0 + 8 + 2 + 1 = 11 = s$$$. In the second test case, $$$s = 41, a = [191, 158]$$$, if $$$x = 154$$$, then $$$displaystylesum_{i=1}^{n} a_i oplus x = (191 oplus 154) + (158 oplus 154) = 37 + 4 = 41 = s$$$. | 2,600 | [
"brute force",
"dp",
"graphs",
"math"
] | true | false | false | true | false | false | true | false | false | true |
Andrew skipped lessons on the subject 'Algorithms and Data Structures' for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment. The teacher gave Andrew an array of _n_ numbers _a_1, ..., _a__n_. After that he asked Andrew for each _k_ from 1 to _n_u2009-u20091 to build a _k_-ary heap on the array and count the number of elements for which the property of the minimum-rooted heap is violated, i.e. the value of an element is less than the value of its parent. Andrew looked up on the Wikipedia that a _k_-ary heap is a rooted tree with vertices in elements of the array. If the elements of the array are indexed from 1 to _n_, then the children of element _v_ are elements with indices _k_(_v_u2009-u20091)u2009+u20092, ..., _kv_u2009+u20091 (if some of these elements lie outside the borders of the array, the corresponding children are absent). In any _k_-ary heap every element except for the first one has exactly one parent; for the element 1 the parent is absent (this element is the root of the heap). Denote _p_(_v_) as the number of the parent of the element with the number _v_. Let's say that for a non-root element _v_ the property of the heap is violated if _a__v_u2009<u2009_a__p_(_v_). Help Andrew cope with the task! Input The first line contains a single integer _n_ (2u2009β€u2009_n_u2009β€u20092Β·105). The second line contains _n_ space-separated integers _a_1, ..., _a__n_ (u2009-u2009109u2009β€u2009_a__i_u2009β€u2009109). Output in a single line print _n_u2009-u20091 integers, separate the consecutive numbers with a single space β the number of elements for which the property of the _k_-ary heap is violated, for _k_u2009=u20091, 2, ..., _n_u2009-u20091. Note Pictures with the heaps for the first sample are given below; elements for which the property of the heap is violated are marked with red. In the second sample all elements are equal, so the property holds for all pairs. | 2,200 | [
"sortings"
] | false | false | false | false | false | false | false | false | true | false |
Zinaida Viktorovna has $$$n$$$ students in her history class. The homework for today included $$$m$$$ topics, but the students had little time to prepare, so $$$i$$$-th student learned only topics from $$$l_i$$$ to $$$r_i$$$ inclusive. At the beginning of the lesson, each student holds their hand at $$$0$$$. The teacher wants to ask some topics. It goes like this: The teacher asks the topic $$$k$$$. If the student has learned topic $$$k$$$, then he raises his hand by $$$1$$$, otherwise he lower it by $$$1$$$. Each topic Zinaida Viktorovna can ask no more than one time. Find the maximum difference of the heights of the highest and the lowest hand that can be in class after the survey. Note that the student's hand can go below $$$0$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 10^5, 1 le m le 10^9$$$)xa0β the number of students and the number of topics, respectively. Each of the next $$$n$$$ lines of each test case contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le m$$$)xa0β the endpoints of the segment of topics that $$$i$$$-th student has learned. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print one integerxa0β the maximum difference of the heights of the highest and the lowest hand that can be in the class after the survey. Example Input 6 4 8 2 6 4 8 2 7 1 5 3 3 1 3 2 3 2 2 3 5 1 5 1 5 1 5 3 5 1 1 3 3 5 5 4 7 1 7 1 3 3 3 4 5 2 4 1 3 2 4 Note In the first test case, Zinaida Viktorovna can ask the topics $$$5, 6, 7, 8$$$. Then the hand of the $$$2$$$-nd student will be at the height of $$$4$$$, and the hand of the $$$4$$$-th will be at the height of $$$-2$$$, that is, the difference will be equal to $$$6$$$. In the second test case, you can ask about the topics $$$1$$$ and $$$3$$$. Then the hand of the $$$1$$$-st student will be at the height of $$$2$$$ and the hand of the $$$3$$$-rd student will be at the height of $$$-2$$$. So the difference will be $$$4$$$. In the third test case, the difference between the highest and lowest hand will be $$$0$$$ for any set of topics asked. In the fifth test case, you can ask all topics. Then the difference between the heights of the $$$1$$$-st and $$$3$$$-rd students' hands will be $$$12$$$. | 1,900 | [
"brute force",
"data structures",
"greedy",
"implementation",
"sortings"
] | false | true | true | false | true | false | true | false | true | false |
Problem - 2004E - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force games math number theory *2100 No tag edit access β Contest materials β Video Tutorial") ") . Alice goes first. Determine who will win. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases. Each test case consists of two lines: the first line 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^7$$$). Additional constraint on the input: the sum of $$$n$$$ across all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output Alice if Alice wins, or Bob if Bob wins. Example Input 3 3 3 2 9 4 3 3 6 1 5 1 2 3 4 5 Output Bob Alice Bob Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 00:40:41 (f1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name --- | 2,100 | [
"brute force",
"math"
] | true | false | false | false | false | false | true | false | false | false |
Consider every tree (connected undirected acyclic graph) with $$$n$$$ vertices ($$$n$$$ is odd, vertices numbered from $$$1$$$ to $$$n$$$), and for each $$$2 le i le n$$$ the $$$i$$$-th vertex is adjacent to exactly one vertex with a smaller index. For each $$$i$$$ ($$$1 le i le n$$$) calculate the number of trees for which the $$$i$$$-th vertex will be the centroid. The answer can be huge, output it modulo $$$998,244,353$$$. A vertex is called a centroid if its removal splits the tree into subtrees with at most $$$(n-1)/2$$$ vertices each. Input The first line contains an odd integer $$$n$$$ ($$$3 le n < 2 cdot 10^5$$$, $$$n$$$ is odd) β the number of the vertices in the tree. Output Print $$$n$$$ integers in a single line, the $$$i$$$-th integer is the answer for the $$$i$$$-th vertex (modulo $$$998,244,353$$$). Examples Output 276 276 132 36 0 0 0 Note Example $$$1$$$: there are two possible trees: with edges $$$(1-2)$$$, and $$$(1-3)$$$ β here the centroid is $$$1$$$; and with edges $$$(1-2)$$$, and $$$(2-3)$$$ β here the centroid is $$$2$$$. So the answer is $$$1, 1, 0$$$. Example $$$2$$$: there are $$$24$$$ possible trees, for example with edges $$$(1-2)$$$, $$$(2-3)$$$, $$$(3-4)$$$, and $$$(4-5)$$$. Here the centroid is $$$3$$$. | 3,000 | [
"dp",
"math"
] | true | false | false | true | false | false | false | false | false | false |
This is an interactive problem. Anya has gathered $$$n$$$ chess experts numbered from $$$1$$$ to $$$n$$$ for which the following properties hold: For any pair of players one of the players wins every game against the other (and no draws ever occur); Transitivity does not necessarily holdxa0β it might happen that $$$A$$$ always beats $$$B$$$, $$$B$$$ always beats $$$C$$$ and $$$C$$$ always beats $$$A$$$. Anya does not know, for each pair, who is the player who beats the other. To organize a tournament, Anya hosts $$$n-1$$$ games. In each game, she chooses two players. One of them wins and stays, while the other one is disqualified. After all the games are hosted only one player will remain. A player is said to be a candidate master if they can win a tournament (notice that the winner of a tournament may depend on the players selected by Anya in the $$$n-1$$$ games). Since Anya is a curious girl, she is interested in finding the candidate masters. Unfortunately, she does not have much time. To speed up the process, she will organize up to $$$2n$$$ simuls (short for "simultaneous exhibition", in which one player plays against many). In one simul, Anya chooses exactly one player who will play against some (at least one) of the other players. The chosen player wins all games they would win in a regular game, and the same holds for losses. After the simul finishes, Anya is only told the total number of games won by the chosen player (but not which ones). Nobody is disqualified during a simul. Can you help Anya host simuls and determine the candidate masters? The winning players in each pair could be changed between the simuls, but only in a way that preserves the results of all previous simuls. These changes may depend on your queries. Interaction Firstly, the jury sends one integer $$$n$$$ ($$$3 leq n leq 250$$$) which should be readxa0β the number of players. After that, your program may ask queries or report an answer. To ask a query, print "? $$$i ; s_1 s_2 ldots s_n$$$" (without quotes), where $$$i$$$ is the index of the player who will play against some of the other players in the simul. $$$s$$$ is a binary string that denotes the players they play against. $$$i$$$ plays against every player $$$j$$$ for which $$$s_j = 1$$$ holds (and $$$s_j = 1$$$ should hold for at least one $$$1 leq j leq n$$$). Please note that $$$s_i = 0$$$ must hold since a player cannot play against themselves, otherwise, the query is considered to be incorrect. After this, you should read an integerxa0β the number of games player $$$i$$$ has won. When you have identified the answer, you must print "! $$$c_1 c_2 ldots c_n$$$" (without quotes) and terminate your program. $$$c$$$ is a binary string which represents the candidate masters. Player $$$i$$$ is a candidate master if $$$c_i=1$$$ holds, otherwise, they are not. If you ask more than $$$2n$$$ queries or if one of the queries is malformed, the interaction terminates immediately and your program receives verdict Wrong Answer. After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. 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 the documentation for other languages. Hacks are disabled in this problem. Examples Output ? 1 010 ? 2 001 ? 3 100 ! 111 Output ? 5 10110 ? 2 10111 ? 1 01111 ! 10000 Note In the first example, the first query describes a simul in which player $$$1$$$ plays against player $$$2$$$ (and no one else). The answer to the query is $$$1$$$, meaning that player $$$1$$$ won the only game they played. We can conclude that $$$1$$$ beats $$$2$$$. Similarly, the second query tells us that $$$2$$$ beats $$$3$$$ and the third query tells us that $$$3$$$ beats $$$1$$$. All players are candidate masters in this case as Player $$$1$$$ can win the tournament if $$$2$$$ and $$$3$$$ play first. $$$3$$$ loses and leaves, while $$$2$$$ stays. $$$1$$$ then plays against $$$2$$$ and wins; Other players can win in the same fashion. In the second example, the third query describes a simul in which player $$$1$$$ plays against every other player. The answer to the query is $$$4$$$, meaning that they won every game they played. It can be concluded that player $$$1$$$ also beats every other player. They can never lose, hence they are the only player who can remain at the end of every possible tournament, and the only possible candidate master. | 2,400 | [
"constructive algorithms",
"graphs",
"greedy",
"sortings"
] | false | true | false | false | false | true | false | false | true | true |
You are given $$$n$$$ points on the plane, the coordinates of the $$$i$$$-th point are $$$(x_i, y_i)$$$. No two points have the same coordinates. The distance between points $$$i$$$ and $$$j$$$ is defined as $$$d(i,j) = x_i - x_j + y_i - y_j$$$. For each point, you have to choose a color, represented by an integer from $$$1$$$ to $$$n$$$. For every ordered triple of different points $$$(a,b,c)$$$, the following constraints should be met: if $$$a$$$, $$$b$$$ and $$$c$$$ have the same color, then $$$d(a,b) = d(a,c) = d(b,c)$$$; if $$$a$$$ and $$$b$$$ have the same color, and the color of $$$c$$$ is different from the color of $$$a$$$, then $$$d(a,b) < d(a,c)$$$ and $$$d(a,b) < d(b,c)$$$. Calculate the number of different ways to choose the colors that meet these constraints. Input The first line contains one integer $$$n$$$ ($$$2 le n le 100$$$)xa0β the number of points. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 le x_i, y_i le 10^8$$$). No two points have the same coordinates (i.u2009e. if $$$i e j$$$, then either $$$x_i e x_j$$$ or $$$y_i e y_j$$$). Output Print one integerxa0β the number of ways to choose the colors for the points. Since it can be large, print it modulo $$$998244353$$$. Examples Input 5 1 2 2 4 3 4 4 4 1 3 Note In the first test, the following ways to choose the colors are suitable: $$$[1, 1, 1]$$$; $$$[2, 2, 2]$$$; $$$[3, 3, 3]$$$; $$$[1, 2, 3]$$$; $$$[1, 3, 2]$$$; $$$[2, 1, 3]$$$; $$$[2, 3, 1]$$$; $$$[3, 1, 2]$$$; $$$[3, 2, 1]$$$. | 2,400 | [
"brute force",
"constructive algorithms",
"dp",
"graphs",
"greedy",
"implementation",
"math"
] | true | true | true | true | false | true | true | false | false | true |
You are the head of a large enterprise. $$$n$$$ people work at you, and $$$n$$$ is odd (i.u2009e. $$$n$$$ is not divisible by $$$2$$$). You have to distribute salaries to your employees. Initially, you have $$$s$$$ dollars for it, and the $$$i$$$-th employee should get a salary from $$$l_i$$$ to $$$r_i$$$ dollars. You have to distribute salaries in such a way that the median salary is maximum possible. To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example: the median of the sequence $$$[5, 1, 10, 17, 6]$$$ is $$$6$$$, the median of the sequence $$$[1, 2, 1]$$$ is $$$1$$$. It is guaranteed that you have enough money to pay the minimum salary, i.e $$$l_1 + l_2 + dots + l_n le s$$$. Note that you don't have to spend all your $$$s$$$ dollars on salaries. You have to answer $$$t$$$ test cases. Input The first line contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$) β the number of test cases. The first line of each query contains two integers $$$n$$$ and $$$s$$$ ($$$1 le n < 2 cdot 10^5$$$, $$$1 le s le 2 cdot 10^{14}$$$) β the number of employees and the amount of money you have. The value $$$n$$$ is not divisible by $$$2$$$. The following $$$n$$$ lines of each query contain the information about employees. The $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le 10^9$$$). It is guaranteed that the sum of all $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$. It is also guaranteed that you have enough money to pay the minimum salary to each employee, i.u2009e. $$$sumlimits_{i=1}^{n} l_i le s$$$. Output For each test case print one integer β the maximum median salary that you can obtain. Example Input 3 3 26 10 12 1 4 10 11 1 1337 1 1000000000 5 26 4 4 2 4 6 8 5 6 2 7 Note In the first test case, you can distribute salaries as follows: $$$sal_1 = 12, sal_2 = 2, sal_3 = 11$$$ ($$$sal_i$$$ is the salary of the $$$i$$$-th employee). Then the median salary is $$$11$$$. In the second test case, you have to pay $$$1337$$$ dollars to the only employee. In the third test case, you can distribute salaries as follows: $$$sal_1 = 4, sal_2 = 3, sal_3 = 6, sal_4 = 6, sal_5 = 7$$$. Then the median salary is $$$6$$$. | 1,900 | [
"binary search",
"sortings"
] | false | false | false | false | false | false | false | true | true | false |
Problem - 1985A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation strings *800 No tag edit access β Contest materials ") Editorial") xa0β the number of test cases. The first and only line of each test case contains two space-separated strings, $$$a$$$ and $$$b$$$, both of length $$$3$$$. The strings only contain lowercase Latin letters. Output For each test case, after the swap, output $$$a$$$ and $$$b$$$, separated by a space. Example Input 6 bit set cat dog hot dog uwu owo cat cat zzz zzz Output sit bet dat cog dot hog owu uwo cat cat zzz zzz Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 00:46:20 (j1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name --- | 800 | [
"implementation"
] | false | false | true | false | false | false | false | false | false | false |
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties... Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $$$e_i$$$xa0β his inexperience. Russell decided that an explorer with inexperience $$$e$$$ can only join the group of $$$e$$$ or more people. Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him. Input The first line contains the number of independent test cases $$$T$$$($$$1 leq T leq 2 cdot 10^5$$$). Next $$$2T$$$ lines contain description of test cases. The first line of description of each test case contains the number of young explorers $$$N$$$ ($$$1 leq N leq 2 cdot 10^5$$$). The second line contains $$$N$$$ integers $$$e_1, e_2, ldots, e_N$$$ ($$$1 leq e_i leq N$$$), where $$$e_i$$$ is the inexperience of the $$$i$$$-th explorer. It's guaranteed that sum of all $$$N$$$ doesn't exceed $$$3 cdot 10^5$$$. Output Print $$$T$$$ numbers, each number on a separate line. In $$$i$$$-th line print the maximum number of groups Russell can form in $$$i$$$-th test case. Example Input 2 3 1 1 1 5 2 3 1 2 2 Note In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $$$1$$$, so it's not less than the size of his group. In the second example we can organize two groups. Explorers with inexperience $$$1$$$, $$$2$$$ and $$$3$$$ will form the first group, and the other two explorers with inexperience equal to $$$2$$$ will form the second group. This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $$$2$$$, and the second group using only one explorer with inexperience equal to $$$1$$$. In this case the young explorer with inexperience equal to $$$3$$$ will not be included in any group. | 1,200 | [
"dp",
"sortings"
] | false | false | false | true | false | false | false | false | true | false |
For an array $$$b$$$ of size $$$m$$$, we define: the maximum prefix position of $$$b$$$ is the smallest index $$$i$$$ that satisfies $$$b_1+ldots+b_i=max_{j=1}^{m}(b_1+ldots+b_j)$$$; the maximum suffix position of $$$b$$$ is the largest index $$$i$$$ that satisfies $$$b_i+ldots+b_m=max_{j=1}^{m}(b_j+ldots+b_m)$$$. You are given three integers $$$n$$$, $$$x$$$, and $$$y$$$ ($$$x > y$$$). Construct an array $$$a$$$ of size $$$n$$$ satisfying: $$$a_i$$$ is either $$$1$$$ or $$$-1$$$ for all $$$1 le i le n$$$; the maximum prefix position of $$$a$$$ is $$$x$$$; the maximum suffix position of $$$a$$$ is $$$y$$$. If there are multiple arrays that meet the conditions, print any. It can be proven that such an array always exists under the given conditions. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β the number of test cases. For each test case: The only line contains three integers $$$n$$$, $$$x$$$, and $$$y$$$ ($$$2 leq n leq 10^5, 1 le y lt x le n)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$10^5$$$. Output For each test case, output $$$n$$$ space-separated integers $$$a_1, a_2, ldots, a_n$$$ in a new line. Example Output 1 1 1 -1 1 1 1 1 -1 1 1 -1 Note In the second test case, $$$i=x=4$$$ is the smallest index that satisfies $$$a_1+ldots +a_i=max_{j=1}^{n}(a_1+ldots+a_j)=2$$$; $$$i=y=3$$$ is the greatest index that satisfies $$$a_i+ldots +a_n=max_{j=1}^{n}(a_j+ldots+a_n)=2$$$. Thus, the array $$$a=[1,-1,1,1]$$$ is considered correct. | 1,200 | [
"constructive algorithms",
"greedy"
] | false | true | false | false | false | true | false | false | false | false |
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly _n_ exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the _i_-th subject on the day number _a__i_. However, Valera has made an arrangement with each teacher and the teacher of the _i_-th subject allowed him to take an exam before the schedule time on day _b__i_ (_b__i_u2009<u2009_a__i_). Thus, Valera can take an exam for the _i_-th subject either on day _a__i_, or on day _b__i_. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number _a__i_. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer _n_ (1u2009β€u2009_n_u2009β€u20095000) β the number of exams Valera will take. Each of the next _n_ lines contains two positive space-separated integers _a__i_ and _b__i_ (1u2009β€u2009_b__i_u2009<u2009_a__i_u2009β€u2009109) β the date of the exam in the schedule and the early date of passing the _i_-th exam, correspondingly. Output Print a single integer β the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject. | 1,400 | [
"sortings"
] | false | false | false | false | false | false | false | false | true | false |
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting. Suppose the graph _G_ is given. Subset _A_ of its vertices is called a vertex cover of this graph, if for each edge _uv_ there is at least one endpoint of it in this set, i.e. or (or both). Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover. They have agreed to give you their graph and you need to find two disjoint subsets of its vertices _A_ and _B_, such that both _A_ and _B_ are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself). Input The first line of the input contains two integers _n_ and _m_ (2u2009β€u2009_n_u2009β€u2009100u2009000, 1u2009β€u2009_m_u2009β€u2009100u2009000)xa0β the number of vertices and the number of edges in the prize graph, respectively. Each of the next _m_ lines contains a pair of integers _u__i_ and _v__i_ (1u2009u2009β€u2009u2009_u__i_,u2009u2009_v__i_u2009u2009β€u2009u2009_n_), denoting an undirected edge between _u__i_ and _v__i_. It's guaranteed the graph won't contain any self-loops or multiple edges. Output If it's impossible to split the graph between Pari and Arya as they expect, print "-1" (without quotes). If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer _k_ denoting the number of vertices in that vertex cover, and the second line contains _k_ integersxa0β the indices of vertices. Note that because of _m_u2009β₯u20091, vertex cover cannot be empty. Note In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish). In the second sample, there is no way to satisfy both Pari and Arya. | 1,500 | [
"graphs"
] | false | false | false | false | false | false | false | false | false | true |
Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 04:13:09 (j1). Supported by []( | 2,800 | [
"constructive algorithms"
] | false | false | false | false | false | true | false | false | false | false |
William has two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ items. For some segments $$$l..r$$$ of these arrays William wants to know if it is possible to equalize the values of items in these segments using a balancing operation. Formally, the values are equalized if for each $$$i$$$ from $$$l$$$ to $$$r$$$ holds $$$a_i = b_i$$$. To perform a balancing operation an even number of indices must be selected, such that $$$l le pos_1 < pos_2 < dots < pos_k le r$$$. Next the items of array a at positions $$$pos_1, pos_3, pos_5, dots$$$ get incremented by one and the items of array b at positions $$$pos_2, pos_4, pos_6, dots$$$ get incremented by one. William wants to find out if it is possible to equalize the values of elements in two arrays for each segment using some number of balancing operations, and what is the minimal number of operations required for that. Note that for each segment the operations are performed independently. Input The first line contains a two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 10^5$$$, $$$1 le q le 10^5$$$), the size of arrays $$$a$$$ and $$$b$$$ and the number of segments. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ $$$(0 le a_i le 10^9)$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ $$$(0 le b_i le 10^9)$$$. Each of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 le l_i < r_i le n)$$$, the edges of segments. Note For the first segment from $$$2$$$ to $$$6$$$ you can do one operation with $$$pos = [2, 3, 5, 6]$$$, after this operation the arrays will be: $$$a = [0, 2, 2, 9, 4, 2, 7, 5]$$$, $$$b = [2, 2, 2, 9, 4, 2, 5, 8]$$$. Arrays are equal on a segment from $$$2$$$ to $$$6$$$ after this operation. For the second segment from $$$1$$$ to $$$7$$$ you can do three following operations: 1. $$$pos = [1, 3, 5, 6]$$$ 2. $$$pos = [1, 7]$$$ 3. $$$pos = [2, 7]$$$ After these operations, the arrays will be: $$$a = [2, 2, 2, 9, 4, 2, 7, 5]$$$, $$$b = [2, 2, 2, 9, 4, 2, 7, 8]$$$. Arrays are equal on a segment from $$$1$$$ to $$$7$$$ after these operations. For the third segment from $$$2$$$ to $$$4$$$ you can do one operation with $$$pos = [2, 3]$$$, after the operation arrays will be: $$$a = [0, 2, 2, 9, 3, 2, 7, 5]$$$, $$$b = [2, 2, 2, 9, 4, 1, 5, 8]$$$. Arrays are equal on a segment from $$$2$$$ to $$$4$$$ after this operation. It is impossible to equalize the fourth and the fifth segment. | 2,200 | [
"data structures",
"dp",
"greedy"
] | false | true | false | true | true | false | false | false | false | false |
One day, Zimpha casually came up with a problem. As a member of "Zimpha fan club", you decided to solve that problem. You are given two strings $$$s$$$ and $$$t$$$ of length $$$n$$$ and $$$m$$$, respectively. Both strings only consist of lowercase English letters, - and *. You need to replace all occurrences of and -, observing the following rules: For each -, you must replace it with any lowercase English letter. For each *, you must replace it with a string of any (possibly, zero) length which only consists of lowercase English letters. Note that you can replace two different instances of - with different characters. You can also replace each two different instances of with different strings. Suppose $$$s$$$ and $$$t$$$ have been transformed into $$$s'$$$ and $$$t'$$$. Now you're wondering if there's a replacement that makes $$$s'=t'$$$. Input The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 2 cdot 10^6$$$)xa0β the length of the strings $$$s$$$ and $$$t$$$, respectively. The second line contains the string $$$s$$$ of length $$$n$$$. It is guaranteed that $$$s$$$ only consists of lowercase English letters, - and *. The third line contains the string $$$t$$$ of length $$$m$$$. It is guaranteed that $$$t$$$ only consists of lowercase English letters, - and *. Output For each test case, output "Yes" if there is a replacement that makes $$$s'=t'$$$, and output "No" otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response). Examples Input 10 10 justmonika j-stsayori Input 13 11 asoulwangziji -soulg*z-y- Input 20 18 bulijiojio-dibuliduo *li*ji-*ox*i*-du*- Note In the second test case, we can transform both strings into ttklwxx. In $$$s$$$, - will be replaced with l. In $$$t$$$, will be replaced by the empty string with the first and second - will be replaced with k and w respectively. In the fifth test case, we can transform both strings into bulijiojioxdibuliduo. | 3,000 | [
"greedy",
"math"
] | true | true | false | false | false | false | false | false | false | false |
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 786 (Div. 3) Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms implementation sortings *1200 No tag edit access β Contest materials Announcement Tutorial PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. A-B-C Sort time limit per test2 seconds memory limit per test256 megabytes You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty. You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples. Can you make array $$$c$$$ sorted in non-decreasing order? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$)xa0β the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β 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^6$$$)xa0β the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 cdot 10^5$$$. Output For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive). Example input 3 4 3 1 5 3 3 3 2 1 1 7331 output YES NO YES Note In the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$: Step $$$1$$$: $$$a$$$ $$$[3, 1, 5, 3]$$$ $$$Rightarrow$$$ $$$[3, 1, 5]$$$ $$$Rightarrow$$$ $$$[3, 1]$$$ $$$Rightarrow$$$ $$$[3]$$$ $$$Rightarrow$$$ $$$[]$$$ $$$b$$$ $$$[]$$$ $$$[underline{3}]$$$ $$$[3, underline{5}]$$$ $$$[3, underline{1}, 5]$$$ $$$[3, underline{3}, 1, 5]$$$ Step $$$2$$$: $$$b$$$ $$$[3, 3, underline{1}, 5]$$$ $$$Rightarrow$$$ $$$[3, underline{3}, 5]$$$ $$$Rightarrow$$$ $$$[underline{3}, 5]$$$ $$$Rightarrow$$$ $$$[underline{5}]$$$ $$$Rightarrow$$$ $$$[]$$$ $$$c$$$ $$$[]$$$ $$$[1]$$$ $$$[1, 3]$$$ $$$[1, 3, 3]$$$ $$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted. Codeforces (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 02:35:22 (h1). Desktop version, switch to mobile version. Privacy Policy Supported by | 1,200 | [
"constructive algorithms",
"implementation",
"sortings"
] | false | false | true | false | false | true | false | false | true | false |
Everybody! Doremy's Perfect Math Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's math class, Doremy is teaching everyone subtraction. Now she gives you a quiz to prove that you are paying attention in class. You are given a set $$$S$$$ containing positive integers. You may perform the following operation some (possibly zero) number of times: choose two integers $$$x$$$ and $$$y$$$ from the set $$$S$$$ such that $$$x > y$$$ and $$$x - y$$$ is not in the set $$$S$$$. add $$$x-y$$$ into the set $$$S$$$. You need to tell Doremy the maximum possible number of integers in $$$S$$$ if the operations are performed optimally. It can be proven that this number is finite. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. The description of the test cases follows. The first line contains an integer $$$n$$$ ($$$2 le nle 10^5$$$)xa0β the size of the set $$$S$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,dots,a_n$$$ ($$$1le a_1 < a_2 < cdots < a_n le 10^9$$$)xa0β the positive integers in $$$S$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, you need to output the maximum possible number of integers in $$$S$$$. It can be proven that this value is finite. Note In the first test case, no such $$$x$$$ and $$$y$$$ exist. The maximum possible number of integers in $$$S$$$ is $$$2$$$. In the second test case, $$$S={5,10,25}$$$ at first. You can choose $$$x=25$$$, $$$y=10$$$, then add $$$x-y=15$$$ to the set. $$$S={5,10,15,25}$$$ now. You can choose $$$x=25$$$, $$$y=5$$$, then add $$$x-y=20$$$ to the set. $$$S={5,10,15,20,25}$$$ now. You can not perform any operation now. After performing all operations, the number of integers in $$$S$$$ is $$$5$$$. It can be proven that no other sequence of operations allows $$$S$$$ to contain more than $$$5$$$ integers. | 900 | [
"math"
] | true | false | false | false | false | false | false | false | false | false |
This is the hard version of the problem. The only differences between the two versions of this problem are the constraints on $$$n$$$ and the time limit. You can make hacks only if all versions of the problem are solved. You are given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. You can perform the following operation some (possibly zero) times: 1. choose $$$l$$$ and $$$r$$$ such that $$$1 leq l leq r leq n$$$. 2. let $$$x=max(a_l,a_{l+1},ldots,a_r)$$$. 3. for all $$$l leq i leq r$$$, set $$$a_i := x$$$. Determine if you can make array $$$a$$$ equal to array $$$b$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β 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 2 cdot 10^5$$$)xa0β the length of the arrays. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β the elements of array $$$a$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$)xa0β the elements of array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" (without quotes) if you can make $$$a$$$ into $$$b$$$ using any number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response). Example Input 5 5 1 2 3 2 4 1 3 3 2 4 5 3 4 2 2 4 3 4 3 4 4 5 3 2 1 1 1 3 3 3 2 2 2 1 1 1 2 3 1 1 2 2 1 2 Note In the first test case, we can achieve array $$$b$$$ by applying a single operation: $$$(l,r)=(2,3)$$$. In the second test case, it can be shown we cannot achieve array $$$b$$$ in any amount of operations. In the third test case, we can achieve array $$$b$$$ by applying two operations: $$$(l,r)=(2,5)$$$. followed by $$$(l,r)=(1,3)$$$. In the fourth and fifth test cases, it can be shown we cannot achieve array $$$b$$$ in any amount of operations. | 1,800 | [
"constructive algorithms",
"data structures",
"greedy",
"implementation",
"sortings"
] | false | true | true | false | true | true | false | false | true | false |
The 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. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. The description of the test cases follows. The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1le nle 2cdot 10^5$$$, $$$2 le m le 2cdot 10^5$$$, $$$2 le k le min(10, m)$$$)xa0β 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$$$ ($$$1le l_ile r_ile n$$$)xa0β 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 $$$2cdot 10^5$$$. Output For each test case, output one integerxa0β the maximum number of dry cities. Example Input 6 2 3 2 1 2 1 2 1 1 5 3 2 1 3 2 4 3 5 10 6 4 1 5 6 10 2 2 3 7 5 8 1 4 100 6 5 1 100 1 100 1 100 1 100 1 100 1 100 1000 2 2 1 1 1 1 20 5 3 9 20 3 3 10 11 11 13 6 18 Note In 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. | 2,600 | [
"data structures",
"dp"
] | false | false | false | true | true | false | false | false | false | false |
You are managing a mobile phone network, and want to offer competitive prices to connect a network. The network has $$$n$$$ nodes. Your competitor has already offered some connections between some nodes, with some fixed prices. These connections are bidirectional. There are initially $$$m$$$ connections the competitor is offering. The $$$i$$$-th connection your competitor is offering will connect nodes $$$fa_i$$$ and $$$fb_i$$$ and costs $$$fw_i$$$. You have a list of $$$k$$$ connections that you want to offer. It is guaranteed that this set of connection does not form any cycle. The $$$j$$$-th of these connections will connect nodes $$$ga_j$$$ and $$$gb_j$$$. These connections are also bidirectional. The cost of these connections have not been decided yet. You can set the prices of these connections to any arbitrary integer value. These prices are set independently for each connection. After setting the prices, the customer will choose such $$$n - 1$$$ connections that all nodes are connected in a single network and the total cost of chosen connections is minimum possible. If there are multiple ways to choose such networks, the customer will choose an arbitrary one that also maximizes the number of your connections in it. You want to set prices in such a way such that all your $$$k$$$ connections are chosen by the customer, and the sum of prices of your connections is maximized. Print the maximum profit you can achieve, or $$$-1$$$ if it is unbounded. Input The first line of input will contain three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 leq n, k, m leq 5 cdot 10^5, k leq n-1$$$), the number of nodes, the number of your connections, and the number of competitor connections, respectively. The next $$$k$$$ lines contain two integers $$$ga_i$$$ and $$$gb_i$$$ ($$$1 leq ga_i, gb_i leq n$$$, $$$ga_i ot= gb_i$$$), representing one of your connections between nodes $$$ga_i$$$ and $$$gb_i$$$. Your set of connections is guaranteed to be acyclic. The next $$$m$$$ lines contain three integers each, $$$fa_i$$$, $$$fb_i$$$ and $$$fw_i$$$ ($$$1 leq fa_i, fb_i leq n$$$, $$$fa_i ot= fb_i$$$, $$$1 leq fw_i leq 10^9$$$), denoting one of your competitor's connections between nodes $$$fa_i$$$ and $$$fb_i$$$ with cost $$$fw_i$$$. None of these connections connects a node to itself, and no pair of these connections connect the same pair of nodes. In addition, these connections are given by non-decreasing order of cost (that is, $$$fw_{i-1} leq fw_i$$$ for all valid $$$i$$$). Note that there may be some connections that appear in both your set and your competitor's set (though no connection will appear twice in one of this sets). It is guaranteed that the union of all of your connections and your competitor's connections form a connected network. Output Print a single integer, the maximum possible profit you can achieve if you set the prices on your connections appropriately. If the profit is unbounded, print $$$-1$$$. Examples Input 4 3 6 1 2 3 4 1 3 2 3 3 3 1 4 1 2 4 4 2 8 4 3 8 4 1 10 Input 4 3 3 1 2 1 3 1 4 4 1 1000000000 4 2 1000000000 4 3 1000000000 Note In the first sample, it's optimal to give connection $$$1-3$$$ cost $$$3$$$, connection $$$1-2$$$ cost $$$3$$$, and connection $$$3-4$$$ cost $$$8$$$. In this case, the cheapest connected network has cost $$$14$$$, and the customer will choose one that chooses all of your connections. In the second sample, as long as your first connection costs $$$30$$$ or less, the customer chooses both your connections no matter what is the cost of the second connection, so you can get unbounded profit in this case. | 2,600 | [
"graphs"
] | false | false | false | false | false | false | false | false | false | true |
Another Codeforces Round has just finished! It has gathered $$$n$$$ participants, and according to the results, the expected rating change of participant $$$i$$$ is $$$a_i$$$. These rating changes are perfectly balancedxa0β their sum is equal to $$$0$$$. Unfortunately, due to minor technical glitches, the round is declared semi-rated. It means that all rating changes must be divided by two. There are two conditions though: For each participant $$$i$$$, their modified rating change $$$b_i$$$ must be integer, and as close to $$$frac{a_i}{2}$$$ as possible. It means that either $$$b_i = lfloor frac{a_i}{2} floor$$$ or $$$b_i = lceil frac{a_i}{2} ceil$$$. In particular, if $$$a_i$$$ is even, $$$b_i = frac{a_i}{2}$$$. Here $$$lfloor x floor$$$ denotes rounding down to the largest integer not greater than $$$x$$$, and $$$lceil x ceil$$$ denotes rounding up to the smallest integer not smaller than $$$x$$$. The modified rating changes must be perfectly balancedxa0β their sum must be equal to $$$0$$$. Can you help with that? Input The first line contains a single integer $$$n$$$ ($$$2 le n le 13,845$$$), denoting the number of participants. Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$-336 le a_i le 1164$$$), denoting the rating change of the $$$i$$$-th participant. The sum of all $$$a_i$$$ is equal to $$$0$$$. Output Output $$$n$$$ integers $$$b_i$$$, each denoting the modified rating change of the $$$i$$$-th participant in order of input. For any $$$i$$$, it must be true that either $$$b_i = lfloor frac{a_i}{2} floor$$$ or $$$b_i = lceil frac{a_i}{2} ceil$$$. The sum of all $$$b_i$$$ must be equal to $$$0$$$. If there are multiple solutions, print any. We can show that a solution exists for any valid input. Examples Input 7 -7 -29 0 3 24 -29 38 Output -3 -15 0 2 12 -15 19 Note In the first example, $$$b_1 = 5$$$, $$$b_2 = -3$$$ and $$$b_3 = -2$$$ is another correct solution. In the second example there are $$$6$$$ possible solutions, one of them is shown in the example output. | 1,000 | [
"implementation"
] | false | false | true | false | false | false | false | false | false | false |
Problem - 1182F - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags binary search data structures number theory *2700 No tag edit access β Contest materials = ext{abs}( ext{sin}(frac{p}{q} pi x))$$$. Find minimum possible integer $$$x$$$ that maximizes $$$f(x)$$$ where $$$a le x le b$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases. The first line of each test case contains four integers $$$a$$$, $$$b$$$, $$$p$$$, and $$$q$$$ ($$$0 le a le b le 10^{9}$$$, $$$1 le p$$$, $$$q le 10^{9}$$$). Output Print the minimum possible integer $$$x$$$ for each test cases, separated by newline. Example Input 2 0 3 1 3 17 86 389 995 Output 1 55 Note In the first test case, $$$f(0) = 0$$$, $$$f(1) = f(2) approx 0.866$$$, $$$f(3) = 0$$$. In the second test case, $$$f(55) approx 0.999969$$$, which is the largest among all possible values. Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 06:47:19 (i2). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name --- | 2,700 | [
"binary search",
"data structures"
] | false | false | false | false | true | false | false | true | false | false |
We call an array $$$a$$$ pure if all elements in it are pairwise distinct. For example, an array $$$[1, 7, 9]$$$ is pure, $$$[1, 3, 3, 7]$$$ isn't, because $$$3$$$ occurs twice in it. A pure array $$$b$$$ is similar to a pure array $$$c$$$ if their lengths $$$n$$$ are the same and for all pairs of indices $$$l$$$, $$$r$$$, such that $$$1 le l le r le n$$$, it's true that $$$$$$operatorname{argmax}([b_l, b_{l + 1}, ldots, b_r]) = operatorname{argmax}([c_l, c_{l + 1}, ldots, c_r]),$$$$$$ where $$$operatorname{argmax}(x)$$$ is defined as the index of the largest element in $$$x$$$ (which is unique for pure arrays). For example, $$$operatorname{argmax}([3, 4, 2]) = 2$$$, $$$operatorname{argmax}([1337, 179, 57]) = 1$$$. Recently, Tonya found out that Burenka really likes a permutation $$$p$$$ of length $$$n$$$. Tonya decided to please her and give her an array $$$a$$$ similar to $$$p$$$. He already fixed some elements of $$$a$$$, but exactly $$$k$$$ elements are missing (in these positions temporarily $$$a_i = 0$$$). It is guaranteed that $$$k ge 2$$$. Also, he has a set $$$S$$$ of $$$k - 1$$$ numbers. Tonya realized that he was missing one number to fill the empty places of $$$a$$$, so he decided to buy it. He has $$$q$$$ options to buy. Tonya thinks that the number $$$d$$$ suits him, if it is possible to replace all zeros in $$$a$$$ with numbers from $$$S$$$ and the number $$$d$$$, so that $$$a$$$ becomes a pure array similar to $$$p$$$. For each option of $$$d$$$, output whether this number is suitable for him or not. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) is the number of test cases. The description of the test cases follows. The first line of each test case contains a couple of integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 3 cdot 10^5$$$). The second line of each input test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$)xa0β the permutation Burenka likes. The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^6$$$)xa0β elements of Tonya's array, where $$$0$$$ denotes a missing element. It is guaranteed that there are two indexes $$$i, j$$$ $$$(1 le i, j le n, i e j)$$$ such that $$$a_i = 0, a_j = 0$$$, which implies that $$$k geq 2$$$. The fourth line of each test case contains $$$k - 1$$$ distinct integers $$$s_1, s_2, ldots, s_{k-1}$$$ ($$$1 le s_i le 10^6$$$)xa0β elements of Tonya's set $$$S$$$. Each of the next $$$q$$$ lines contains a single integer $$$d$$$ ($$$1 le d le 10^6$$$)xa0β the number that Tonya plans to buy. It is guaranteed that for each given $$$d$$$ it's possible to fill in the gaps in $$$a$$$ with numbers from $$$S$$$ and the number $$$d$$$ to get a pure array. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ in all tests does not exceed $$$3 cdot 10^5$$$. Output Output $$$q$$$ lines. For each value $$$d$$$, print "YES" if there is a way to fill the array $$$a$$$ to make it similar to $$$p$$$, and "NO" otherwise. Example Input 4 4 3 1 4 3 2 5 0 7 0 6 9 1 4 5 3 1 2 5 4 3 0 5 10 0 0 3 9 1 8 11 5 2 1 4 3 2 5 0 0 0 0 0 7 9 1 5 6 100 4 2 4 1 3 2 0 5 3 0 2 4 6 Output YES NO NO YES YES NO YES YES NO NO Note In the first test case for $$$d = 9$$$, you can get $$$a = [5, 9, 7, 6]$$$, it can be proved that $$$a$$$ is similar to $$$p$$$, for $$$d=1$$$ and $$$d=4$$$ it can be proved that there is no answer. In the second test case for $$$d = 1$$$, you can get $$$a = [1, 5, 10, 9, 3]$$$, for $$$d = 8$$$, you can get $$$a = [3, 5, 10, 9, 8]$$$, it can be proved that for $$$d = 11$$$ there is no answer. | 3,300 | [
"data structures",
"greedy",
"math"
] | true | true | false | false | true | false | false | false | false | false |
Problem - 1467E - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags data structures dfs and similar dp trees *2500 No tag edit access β Contest materials ") Editorial") β the number of vertices in the tree. The next line contains $$$n$$$ space-separated integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). The following $$$n-1$$$ lines each contain two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 le u$$$, $$$v le n$$$), denoting an edge from $$$u$$$ to $$$v$$$. It is guaranteed that the edges form a tree. Output Print a single integer β the number of distinctive roots in the tree. Examples Input 5 2 5 1 1 4 1 2 1 3 2 4 2 5 Output 3 Input 5 2 1 1 1 4 1 2 1 3 2 4 2 5 Output 0 Note In the first example, $$$1$$$, $$$2$$$ and $$$5$$$ are distinctive roots. Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 03:41:36 (f1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name --- | 2,500 | [
"data structures",
"dp"
] | false | false | false | true | true | false | false | false | false | false |
This is the hard version of the problem. In the three versions, the constraints on $$$n$$$ and the time limit are different. You can make hacks only if all the versions of the problem are solved. This is the statement of Problem D1B: There are $$$n$$$ cities in a row, numbered $$$1, 2, ldots, n$$$ left to right. At time $$$1$$$, you conquer exactly one city, called the starting city. At time $$$2, 3, ldots, n$$$, you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each $$$i$$$, you conquer city $$$i$$$ at a time no later than $$$a_i$$$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win? For each $$$0 leq k leq n$$$, count the number of arrays of positive integers $$$a_1, a_2, ldots, a_n$$$ such that $$$1 leq a_i leq n$$$ for each $$$1 leq i leq n$$$; the answer to Problem D1B is $$$k$$$. The answer can be very large, so you have to calculate it modulo a given prime $$$p$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 3000$$$). The description of the test cases follows. The only line of each test case contains two integers $$$n$$$, $$$p$$$ ($$$1 le n le 3000$$$, $$$10^8 leq p leq 10^9$$$, $$$p$$$ is prime)xa0β the number of cities and the modulo. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. Output For each test case, output $$$n+1$$$ integers: the $$$i$$$-th integer should be the number of arrays that satisfy the conditions for $$$k = i-1$$$. Example Input 11 1 998244353 2 998244353 3 998244353 4 998244353 5 998244353 6 998244353 7 998244353 8 998244353 9 998244353 10 102275857 10 999662017 Output 0 1 1 2 1 14 7 4 2 183 34 19 16 4 2624 209 112 120 48 12 42605 1546 793 992 468 216 36 785910 13327 6556 9190 4672 2880 864 144 16382863 130922 61939 94992 50100 36960 14256 4608 576 382823936 1441729 657784 1086596 583344 488700 216000 96480 23040 2880 20300780 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 944100756 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 Note In the first test case, arrays with $$$1$$$ good starting city: $$$[1]$$$. In the second test case, arrays with $$$0$$$ good starting cities: $$$[1, 1]$$$; arrays with $$$1$$$ good starting city: $$$[1, 2]$$$, $$$[2, 1]$$$; arrays with $$$2$$$ good starting cities: $$$[2, 2]$$$. In the third test case, arrays with $$$0$$$ good starting cities: $$$[1, 1, 1]$$$, $$$[1, 1, 2]$$$, $$$[1, 1, 3]$$$, $$$[1, 2, 1]$$$, $$$[1, 2, 2]$$$, $$$[1, 3, 1]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 1]$$$, $$$[2, 1, 2]$$$, $$$[2, 2, 1]$$$, $$$[2, 2, 2]$$$, $$$[2, 3, 1]$$$, $$$[2, 3, 2]$$$, $$$[3, 1, 1]$$$; arrays with $$$1$$$ good starting city: $$$[1, 2, 3]$$$, $$$[1, 3, 3]$$$, $$$[2, 1, 3]$$$, $$$[3, 1, 2]$$$, $$$[3, 1, 3]$$$, $$$[3, 2, 1]$$$, $$$[3, 3, 1]$$$; arrays with $$$2$$$ good starting cities: $$$[2, 2, 3]$$$, $$$[2, 3, 3]$$$, $$$[3, 2, 2]$$$, $$$[3, 3, 2]$$$; arrays with $$$3$$$ good starting cities: $$$[3, 2, 3]$$$, $$$[3, 3, 3]$$$. | 3,100 | [
"dp",
"greedy",
"math"
] | true | true | false | true | false | false | false | false | false | false |
You are given a rooted tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. Any vertex can be the root of a tree. A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. The tree is specified by an array of parents $$$p$$$ containing $$$n$$$ numbers: $$$p_i$$$ is a parent of the vertex with the index $$$i$$$. The parent of a vertex $$$u$$$ is a vertex that is the next vertex on the shortest path from $$$u$$$ to the root. For example, on the simple path from $$$5$$$ to $$$3$$$ (the root), the next vertex would be $$$1$$$, so the parent of $$$5$$$ is $$$1$$$. The root has no parent, so for it, the value of $$$p_i$$$ is $$$i$$$ (the root is the only vertex for which $$$p_i=i$$$). Find such a set of paths that: each vertex belongs to exactly one path, each path can contain one or more vertices; in each path each next vertexxa0β is a son of the current vertex (that is, paths always lead downxa0β from parent to son); number of paths is minimal. For example, if $$$n=5$$$ and $$$p=[3, 1, 3, 3, 1]$$$, then the tree can be divided into three paths: 1. $$$3 ightarrow 1 ightarrow 5$$$ (path of $$$3$$$ vertices), 2. $$$4$$$ (path of $$$1$$$ vertices). 3. $$$2$$$ (path of $$$1$$$ vertices). Example of splitting a root tree into three paths for $$$n=5$$$, the root of the treexa0β node $$$3$$$. Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases in the test. Each test case consists of two lines. The first of them contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). It is the number of vertices in the tree. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$). It is guaranteed that the $$$p$$$ array encodes some rooted tree. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2 cdot 10^5$$$. Output For each test case on the first line, output an integer $$$m$$$ β the minimum number of non-intersecting leading down paths that can cover all vertices of the tree. Then print $$$m$$$ pairs of lines containing path descriptions. In the first of them print the length of the path, in the second β the sequence of vertices specifying that path in the order from top to bottom. You can output the paths in any order. If there are several answers, output any of them. | 1,300 | [
"graphs",
"implementation"
] | false | false | true | false | false | false | false | false | false | true |
You are given an integer $$$n$$$. The function $$$C(i,k)$$$ represents the number of distinct ways you can select $$$k$$$ distinct numbers from the set {$$$1, 2, ldots, i$$$} and arrange them in a circle$$$^dagger$$$. Find the value of $$$$$$ sumlimits_{i=1}^n sumlimits_{j=1}^i left( C(i,j) bmod j ight). $$$$$$ Here, the operation $$$x bmod y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$. Since this value can be very large, find it modulo $$$10^9+7$$$. $$$^dagger$$$ In a circular arrangement, sequences are considered identical if one can be rotated to match the other. For instance, $$$[1, 2, 3]$$$ and $$$[2, 3, 1]$$$ are equivalent in a circle. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$)xa0β the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$). Output For each test case, output a single integer on a new linexa0β the value of the expression to be calculated modulo $$$10^9+7$$$. Note In the first test case, $$$C(1,1) bmod 1 = 0$$$. In the second test case: $$$C(1,1)=1$$$ (the arrangements are: $$$[1]$$$); $$$C(2,1)=2$$$ (the arrangements are: $$$[1]$$$, $$$[2]$$$); $$$C(2,2)=1$$$ (the arrangements are: $$$[1, 2]$$$); $$$C(3,1)=3$$$ (the arrangements are: $$$[1]$$$, $$$[2]$$$, $$$[3]$$$); $$$C(3,2)=3$$$ (the arrangements are: $$$[1, 2]$$$, $$$[2, 3]$$$, $$$[3, 1]$$$); $$$C(3,3)=2$$$ (the arrangements are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$). In total, $$$left(C(1,1) bmod 1 ight) + left(C(2,1) bmod 1 ight) + left(C(2,2) bmod 2 ight) + left(C(3,1) bmod 1 ight) + left(C(3,2) bmod 2 ight) + left(C(3,3) bmod 3 ight) = 4$$$. | 2,400 | [
"brute force",
"dp",
"math"
] | true | false | false | true | false | false | true | false | false | false |
Problem - 272D - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags combinatorics math sortings *1600 No tag edit access β Contest materials ,u2009(_a_2,u20092),u2009...,u2009(_a__n_,u2009_n_) and sequence (_b_1,u20091),u2009(_b_2,u20092),u2009...,u2009(_b__n_,u2009_n_). Now Dima wants to count the number of distinct sequences of points of length 2Β·_n_ that can be assembled from these sequences, such that the _x_-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (_p_1,u2009_q_1),u2009(_p_2,u2009_q_2),u2009...,u2009(_p_2Β·_n_,u2009_q_2Β·_n_) and (_x_1,u2009_y_1),u2009(_x_2,u2009_y_2),u2009...,u2009(_x_2Β·_n_,u2009_y_2Β·_n_) distinct, if there is such _i_ (1u2009β€u2009_i_u2009β€u20092Β·_n_), that (_p__i_,u2009_q__i_)u2009β u2009(_x__i_,u2009_y__i_). As the answer can be rather large, print the remainder from dividing the answer by number _m_. Input The first line contains integer _n_ (1u2009β€u2009_n_u2009β€u2009105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009109). The third line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009β€u2009_b__i_u2009β€u2009109). The numbers in the lines are separated by spaces. The last line contains integer _m_ (2u2009β€u2009_m_u2009β€u2009109u2009+u20097). Output In the single line print the remainder after dividing the answer to the problem by number _m_. Examples Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 Note In the first sample you can get only one sequence: (1,u20091),u2009(2,u20091). In the second sample you can get such sequences : (1,u20091),u2009(2,u20092),u2009(2,u20091),u2009(3,u20092); (1,u20091),u2009(2,u20091),u2009(2,u20092),u2009(3,u20092). Thus, the answer is 2. Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 11:46:11 (i1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name --- | 1,600 | [
"sortings"
] | false | false | false | false | false | false | false | false | true | false |
Prefix function of string $$$t = t_1 t_2 ldots t_n$$$ and position $$$i$$$ in it is defined as the length $$$k$$$ of the longest proper (not equal to the whole substring) prefix of substring $$$t_1 t_2 ldots t_i$$$ which is also a suffix of the same substring. For example, for string $$$t = $$$ abacaba the values of the prefix function in positions $$$1, 2, ldots, 7$$$ are equal to $$$[0, 0, 1, 0, 1, 2, 3]$$$. Let $$$f(t)$$$ be equal to the maximum value of the prefix function of string $$$t$$$ over all its positions. For example, $$$f($$$abacaba$$$) = 3$$$. You are given a string $$$s$$$. Reorder its characters arbitrarily to get a string $$$t$$$ (the number of occurrences of any character in strings $$$s$$$ and $$$t$$$ must be equal). The value of $$$f(t)$$$ must be minimized. Out of all options to minimize $$$f(t)$$$, choose the one where string $$$t$$$ is the lexicographically smallest. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. The only line of each test case contains string $$$s$$$ ($$$1 le s le 10^5$$$) consisting of lowercase English letters. It is guaranteed that the sum of lengths of $$$s$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print a single string $$$t$$$. The multisets of letters in strings $$$s$$$ and $$$t$$$ must be equal. The value of $$$f(t)$$$, the maximum of prefix functions in string $$$t$$$, must be as small as possible. String $$$t$$$ must be the lexicographically smallest string out of all strings satisfying the previous conditions. Example Input 3 vkcup abababa zzzzzz Output ckpuv aababab zzzzzz Note A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. In the first test case, $$$f(t) = 0$$$ and the values of prefix function are $$$[0, 0, 0, 0, 0]$$$ for any permutation of letters. String ckpuv is the lexicographically smallest permutation of letters of string vkcup. In the second test case, $$$f(t) = 1$$$ and the values of prefix function are $$$[0, 1, 0, 1, 0, 1, 0]$$$. In the third test case, $$$f(t) = 5$$$ and the values of prefix function are $$$[0, 1, 2, 3, 4, 5]$$$. | 2,100 | [
"constructive algorithms"
] | false | false | false | false | false | true | false | false | false | false |
William arrived at a conference dedicated to cryptocurrencies. Networking, meeting new people, and using friends' connections are essential to stay up to date with the latest news from the world of cryptocurrencies. The conference has $$$n$$$ participants, who are initially unfamiliar with each other. William can introduce any two people, $$$a$$$ and $$$b$$$, who were not familiar before, to each other. William has $$$d$$$ conditions, $$$i$$$'th of which requires person $$$x_i$$$ to have a connection to person $$$y_i$$$. Formally, two people $$$x$$$ and $$$y$$$ have a connection if there is such a chain $$$p_1=x, p_2, p_3, dots, p_k=y$$$ for which for all $$$i$$$ from $$$1$$$ to $$$k - 1$$$ it's true that two people with numbers $$$p_i$$$ and $$$p_{i + 1}$$$ know each other. For every $$$i$$$ ($$$1 le i le d$$$) William wants you to calculate the maximal number of acquaintances one person can have, assuming that William satisfied all conditions from $$$1$$$ and up to and including $$$i$$$ and performed exactly $$$i$$$ introductions. The conditions are being checked after William performed $$$i$$$ introductions. The answer for each $$$i$$$ must be calculated independently. It means that when you compute an answer for $$$i$$$, you should assume that no two people have been introduced to each other yet. Input The first line contains two integers $$$n$$$ and $$$d$$$ ($$$2 le n le 10^3, 1 le d le n - 1$$$), the number of people, and number of conditions, respectively. Each of the next $$$d$$$ lines each contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n, x_i eq y_i$$$), the numbers of people which must have a connection according to condition $$$i$$$. Output Output $$$d$$$ integers. $$$i$$$th number must equal the number of acquaintances the person with the maximal possible acquaintances will have, if William performed $$$i$$$ introductions and satisfied the first $$$i$$$ conditions. Note The explanation for the first test case: In this explanation, the circles and the numbers in them denote a person with the corresponding number. The line denotes that William introduced two connected people. The person marked with red has the most acquaintances. These are not the only correct ways to introduce people. | 1,600 | [
"graphs",
"greedy",
"implementation"
] | false | true | true | false | false | false | false | false | false | true |
One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something. The following statement caught his attention: "Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people" Igor just couldn't get why the required minimum is 6 people. "Well, that's the same for five people, too!" β he kept on repeating in his mind. β "Let's take, say, Max, Ilya, Vova β here, they all know each other! And now let's add Dima and Oleg to Vova β none of them is acquainted with each other! Now, that math is just rubbish!" Igor K. took 5 friends of his and wrote down who of them is friends with whom. Now he wants to check whether it is true for the five people that among them there are either three pairwise acquainted or three pairwise not acquainted people. Input The first line contains an integer _m_ (0u2009β€u2009_m_u2009β€u200910), which is the number of relations of acquaintances among the five friends of Igor's. Each of the following _m_ lines contains two integers _a__i_ and _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u20095;_a__i_u2009β u2009_b__i_), where (_a__i_,u2009_b__i_) is a pair of acquainted people. It is guaranteed that each pair of the acquaintances is described exactly once. The acquaintance relation is symmetrical, i.e. if _x_ is acquainted with _y_, then _y_ is also acquainted with _x_. Output Print "FAIL", if among those five people there are no either three pairwise acquainted or three pairwise unacquainted people. Otherwise print "WIN". | 1,300 | [
"graphs"
] | false | false | false | false | false | false | false | false | false | true |
Anu has created her own function $$$f$$$: $$$f(x, y) = (x y) - y$$$ where $$$$$$ denotes the = (116) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $$$[a_1, a_2, dots, a_n]$$$ is defined as $$$f(f(dots f(f(a_1, a_2), a_3), dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$). Elements of the array are not guaranteed to be different. Output Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. Note In the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$. $$$[11, 4, 0, 6]$$$ is also a valid answer. | 1,500 | [
"brute force"
] | false | false | false | false | false | false | true | false | false | false |
After the educational reform Polycarp studies only two subjects at school, Safety Studies and PE (Physical Education). During the long months of the fourth term, he received _n_ marks in them. When teachers wrote a mark in the journal, they didn't write in what subject the mark was for, they just wrote the mark. Now it's time to show the journal to his strict parents. Polycarp knows that recently at the Parent Meeting the parents were told that he received _a_ Safety Studies marks and _b_ PE marks (_a_u2009+u2009_b_u2009=u2009_n_). Now Polycarp wants to write a subject's name in front of each mark so that: there are exactly _a_ Safety Studies marks, there are exactly _b_ PE marks, the total average score in both subjects is maximum. An average subject grade is the sum of all marks in it, divided by the number of them. Of course, the division is performed in real numbers without rounding up or down. Polycarp aims to maximize the _x_1u2009+u2009_x_2, where _x_1 is the average score in the first subject (Safety Studies), and _x_2 is the average score in the second one (Physical Education). Input The first line contains an integer _n_ (2u2009β€u2009_n_u2009β€u2009105), _n_ is the number of marks in Polycarp's Journal. The second line contains two positive integers _a_,u2009_b_ (1u2009β€u2009_a_,u2009_b_u2009β€u2009_n_u2009-u20091,u2009_a_u2009+u2009_b_u2009=u2009_n_). The third line contains a sequence of integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009β€u2009_t__i_u2009β€u20095), they are Polycarp's marks. Output Print the sequence of integers _f_1,u2009_f_2,u2009...,u2009_f__n_, where _f__i_ (1u2009β€u2009_f__i_u2009β€u20092) is the number of a subject to which the _i_-th mark should be attributed. If there are several possible solutions, then print such that the sequence _f_1,u2009_f_2,u2009...,u2009_f__n_ is the smallest lexicographically. The sequence _p_1,u2009_p_2,u2009...,u2009_p__n_ is lexicographically less than _q_1,u2009_q_2,u2009...,u2009_q__n_ if there exists such _j_ (1u2009β€u2009_j_u2009β€u2009_n_) that _p__i_u2009=u2009_q__i_ for all 1u2009β€u2009_i_u2009<u2009_j_, Π°nd _p__j_u2009<u2009_q__j_. Note In the first sample the average score in the first subject is equal to 4, and in the second one β to 4.5. The total average score is 8.5. | 1,700 | [
"sortings"
] | false | false | false | false | false | false | false | false | true | false |
There are $$$n$$$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $$$s$$$, where the $$$i$$$-th candle is lit if and only if $$$s_i=1$$$. Initially, the candle lights are described by a string $$$a$$$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $$$b$$$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. Input The first line contains an integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 10^5$$$)xa0β the number of candles. The second line contains a string $$$a$$$ of length $$$n$$$ consisting of symbols 0 and 1xa0β the initial pattern of lights. The third line contains a string $$$b$$$ of length $$$n$$$ consisting of symbols 0 and 1xa0β the desired pattern of lights. It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^5$$$. Output For each test case, output the minimum number of operations required to transform $$$a$$$ to $$$b$$$, or $$$-1$$$ if it's impossible. Example Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Note In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $$$01$$$ into $$$11$$$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $$$a$$$ into $$$b$$$: 1. Select the $$$7$$$-th candle: $$$100010{color{red}1}11 o 011101{color{red} 1}00$$$. 2. Select the $$$2$$$-nd candle: $$$0{color{red} 1}1101100 o 1{color{red} 1}0010011$$$. 3. Select the $$$1$$$-st candle: $$${color{red}1}10010011 o {color{red}1}01101100$$$. In the fifth test case, we can perform the following operations to transform $$$a$$$ into $$$b$$$: 1. Select the $$$6$$$-th candle: $$$00101{color{red}1}011 o 11010{color{red}1}100$$$ 2. Select the $$$2$$$-nd candle: $$$1{color{red}1}0101100 o 0{color{red}1}1010011$$$ 3. Select the $$$8$$$-th candle: $$$0110100{color{red}1}1 o 1001011{color{red}1}0$$$ 4. Select the $$$7$$$-th candle: $$$100101{color{red}1}10 o 011010{color{red}1}01$$$ | 1,600 | [
"brute force",
"graphs",
"greedy",
"math"
] | true | true | false | false | false | false | true | false | false | true |
So we got bored and decided to take our own guess at how would "Inception" production go if the budget for the film had been terribly low. The first scene we remembered was the one that features the whole city bending onto itself: It feels like it will require high CGI expenses, doesn't it? Luckily, we came up with a similar-looking scene which was a tiny bit cheaper to make. Firstly, forget about 3D, that's hard and expensive! The city is now represented as a number line (infinite to make it easier, of course). Secondly, the city doesn't have to look natural at all. There are $$$n$$$ buildings on the line. Each building is a square $$$1 imes 1$$$. Buildings are numbered from $$$1$$$ to $$$n$$$ in ascending order of their positions. Lower corners of building $$$i$$$ are at integer points $$$a_i$$$ and $$$a_i + 1$$$ of the number line. Also the distance between any two neighbouring buildings $$$i$$$ and $$$i + 1$$$ doesn't exceed $$$d$$$ (really, this condition is here just to make the city look not that sparse). Distance between some neighbouring buildings $$$i$$$ and $$$i + 1$$$ is calculated from the lower right corner of building $$$i$$$ to the lower left corner of building $$$i + 1$$$. Finally, curvature of the bend is also really hard to simulate! Let the bend at some integer coordinate $$$x$$$ be performed with the following algorithm. Take the ray from $$$x$$$ to $$$+infty$$$ and all the buildings which are on this ray and start turning the ray and the buildings counter-clockwise around point $$$x$$$. At some angle some building will touch either another building or a part of the line. You have to stop bending there (implementing buildings crushing is also not worth its money). Let's call the angle between two rays in the final state the terminal angle $$$alpha_x$$$. The only thing left is to decide what integer point $$$x$$$ is the best to start bending around. Fortunately, we've already chosen $$$m$$$ candidates to perform the bending. So, can you please help us to calculate terminal angle $$$alpha_x$$$ for each bend $$$x$$$ from our list of candidates? Input The first line contains two integer numbers $$$n$$$ and $$$d$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le d le 7000$$$) β the number of buildings and the maximum distance between any pair of neighbouring buildings, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$a_1 = 0$$$, $$$0 < a_{i + 1} - a_i le d + 1$$$) β coordinates of left corners of corresponding buildings in ascending order. The third line contains single integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$) β the number of candidates. The fourth line contains $$$m$$$ integers $$$x_1, x_2, dots, x_m$$$ ($$$0 le x_i le a_n + 1$$$, $$$x_i < x_{i + 1}$$$) β the coordinates of bends you need to calculate terminal angles for in ascending order. Output Print $$$m$$$ numbers. For each bend $$$x_i$$$ print terminal angle $$$alpha_{x_i}$$$ (in radians). Your answer is considered correct if its absolute error does not exceed $$$10^{-9}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$a - b le 10^{-9}$$$. Examples Input 3 5 0 5 7 9 0 1 2 3 4 5 6 7 8 Output 1.570796326794897 1.570796326794897 0.785398163397448 0.927295218001612 0.785398163397448 1.570796326794897 1.570796326794897 1.570796326794897 1.570796326794897 Output 1.570796326794897 0.927295218001612 1.570796326794897 Input 5 0 0 1 2 3 4 6 0 1 2 3 4 5 Output 1.570796326794897 3.141592653589793 3.141592653589793 3.141592653589793 3.141592653589793 1.570796326794897 Note Here you can see the picture of the city for the first example and the bend at position $$$2$$$ for it. The angle you need to measure is marked blue. You can see that it's equal to $$$frac pi 4$$$. You can see that no pair of neighbouring buildings have distance more than $$$4$$$ between them. $$$d = 4$$$ would also suffice for that test. | 3,100 | [
"brute force"
] | false | false | false | false | false | false | true | false | false | false |
Polycarp was given an array $$$a$$$ of $$$n$$$ integers. He really likes triples of numbers, so for each $$$j$$$ ($$$1 le j le n - 2$$$) he wrote down a triple of elements $$$[a_j, a_{j + 1}, a_{j + 2}]$$$. Polycarp considers a pair of triples $$$b$$$ and $$$c$$$ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied: $$$b_1 e c_1$$$ and $$$b_2 = c_2$$$ and $$$b_3 = c_3$$$; $$$b_1 = c_1$$$ and $$$b_2 e c_2$$$ and $$$b_3 = c_3$$$; $$$b_1 = c_1$$$ and $$$b_2 = c_2$$$ and $$$b_3 e c_3$$$. Find the number of beautiful pairs of triples among the written triples $$$[a_j, a_{j + 1}, a_{j + 2}]$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$)xa0β the length of the 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^6$$$)xa0β the elements of the array. It is guaranteed that the sum of the values of $$$n$$$ for all test cases in the test does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0β the number of beautiful pairs of triples among the pairs of the form $$$[a_j, a_{j + 1}, a_{j + 2}]$$$. Note that the answer may not fit into 32-bit data types. Example Input 8 5 3 2 2 2 3 5 1 2 1 2 1 8 1 2 3 2 2 3 4 2 4 2 1 1 1 8 2 1 1 2 1 1 1 1 7 2 1 1 1 1 1 1 6 2 1 1 1 1 1 5 2 1 1 1 1 Note In the first example, $$$a = [3, 2, 2, 2, 3]$$$, Polycarp will write the following triples: 1. $$$[3, 2, 2]$$$; 2. $$$[2, 2, 2]$$$; 3. $$$[2, 2, 3]$$$. The beautiful pairs are triple $$$1$$$ with triple $$$2$$$ and triple $$$2$$$ with triple $$$3$$$. In the third example, $$$a = [1, 2, 3, 2, 2, 3, 4, 2]$$$, Polycarp will write the following triples: 1. $$$[1, 2, 3]$$$; 2. $$$[2, 3, 2]$$$; 3. $$$[3, 2, 2]$$$; 4. $$$[2, 2, 3]$$$; 5. $$$[2, 3, 4]$$$; 6. $$$[3, 4, 2]$$$; The beautiful pairs are triple $$$1$$$ with triple $$$4$$$, triple $$$2$$$ with triple $$$5$$$, and triple $$$3$$$ with triple $$$6$$$. | 1,400 | [
"data structures"
] | false | false | false | false | true | false | false | false | false | false |
There is a tree consisting of _n_ vertices. The vertices are numbered from 1 to _n_. Let's define the length of an interval [_l_,u2009_r_] as the value _r_u2009-u2009_l_u2009+u20091. The score of a subtree of this tree is the maximum length of such an interval [_l_,u2009_r_] that, the vertices with numbers _l_,u2009_l_u2009+u20091,u2009...,u2009_r_ belong to the subtree. Considering all subtrees of the tree whose size is at most _k_, return the maximum score of the subtree. Note, that in this problem tree is not rooted, so a subtree β is an arbitrary connected subgraph of the tree. Input There are two integers in the first line, _n_ and _k_ (1u2009β€u2009_k_u2009β€u2009_n_u2009β€u2009105). Each of the next _n_u2009-u20091 lines contains integers _a__i_ and _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_,u2009_a__i_u2009β u2009_b__i_). That means _a__i_ and _b__i_ are connected by a tree edge. It is guaranteed that the input represents a tree. Output Output should contain a single integer β the maximum possible score. Examples Input 10 6 4 10 10 6 2 9 9 6 8 5 7 1 4 7 7 3 1 8 Input 16 7 13 11 12 11 2 14 8 6 9 15 16 11 5 14 6 15 4 3 11 15 15 14 10 1 3 14 14 7 1 7 Note For the first case, there is some subtree whose size is at most 6, including 3 consecutive numbers of vertices. For example, the subtree that consists of {1,u20093,u20094,u20095,u20097,u20098} or of {1,u20094,u20096,u20097,u20098,u200910} includes 3 consecutive numbers of vertices. But there is no subtree whose size is at most 6, which includes 4 or more consecutive numbers of vertices. | 2,600 | [
"binary search"
] | false | false | false | false | false | false | false | true | false | false |
Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples: for the array $$$[0, 0, 1, 0, 2]$$$ MEX equals to $$$3$$$ because numbers $$$0, 1$$$ and $$$2$$$ are presented in the array and $$$3$$$ is the minimum non-negative integer not presented in the array; for the array $$$[1, 2, 3, 4]$$$ MEX equals to $$$0$$$ because $$$0$$$ is the minimum non-negative integer not presented in the array; for the array $$$[0, 1, 4, 3]$$$ MEX equals to $$$2$$$ because $$$2$$$ is the minimum non-negative integer not presented in the array. You are given an empty array $$$a=[]$$$ (in other words, a zero-length array). You are also given a positive integer $$$x$$$. You are also given $$$q$$$ queries. The $$$j$$$-th query consists of one integer $$$y_j$$$ and means that you have to append one element $$$y_j$$$ to the array. The array length increases by $$$1$$$ after a query. In one move, you can choose any index $$$i$$$ and set $$$a_i := a_i + x$$$ or $$$a_i := a_i - x$$$ (i.e. increase or decrease any element of the array by $$$x$$$). The only restriction is that $$$a_i$$$ cannot become negative. Since initially the array is empty, you can perform moves only after the first query. You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element). You have to find the answer after each of $$$q$$$ queries (i.e. the $$$j$$$-th answer corresponds to the array of length $$$j$$$). Operations are discarded before each query. I.e. the array $$$a$$$ after the $$$j$$$-th query equals to $$$[y_1, y_2, dots, y_j]$$$. Input The first line of the input contains two integers $$$q, x$$$ ($$$1 le q, x le 4 cdot 10^5$$$) β the number of queries and the value of $$$x$$$. The next $$$q$$$ lines describe queries. The $$$j$$$-th query consists of one integer $$$y_j$$$ ($$$0 le y_j le 10^9$$$) and means that you have to append one element $$$y_j$$$ to the array. | 1,600 | [
"data structures",
"implementation"
] | false | false | true | false | true | false | false | false | false | false |
The only difference between easy and hard versions is the maximum values of $$$n$$$ and $$$q$$$. You are given an array, consisting of $$$n$$$ integers. Initially, all elements are red. You can apply the following operation to the array multiple times. During the $$$i$$$-th operation, you select an element of the array; then: if the element is red, it increases by $$$i$$$ and becomes blue; if the element is blue, it decreases by $$$i$$$ and becomes red. The operations are numbered from $$$1$$$, i.u2009e. during the first operation some element is changed by $$$1$$$ and so on. You are asked $$$q$$$ queries of the following form: given an integer $$$k$$$, what can the largest minimum in the array be if you apply exactly $$$k$$$ operations to it? Note that the operations don't affect the array between queries, all queries are asked on the initial array $$$a$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 1000$$$)xa0β the number of elements in the array and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). The third line contains $$$q$$$ integers $$$k_1, k_2, dots, k_q$$$ ($$$1 le k_j le 10^9$$$). Output For each query, print a single integerxa0β the largest minimum that the array can have after you apply exactly $$$k$$$ operations to it. Examples Input 4 10 5 2 8 4 1 2 3 4 5 6 7 8 9 10 Output 3 4 5 6 7 8 8 10 8 12 Input 5 10 5 2 8 4 4 1 2 3 4 5 6 7 8 9 10 Output 3 4 5 6 7 8 9 8 11 8 | 2,100 | [
"binary search",
"greedy",
"implementation",
"math"
] | true | true | true | false | false | false | false | true | false | false |
Bosco is studying the behaviour of particles. He decided to investigate on the peculiar behaviour of the so-called "four-one-two" particle. He does the following: There is a line of length $$$n+1$$$, where the topmost point is position $$$0$$$ and bottommost is position $$$n+1$$$. The particle is initially (at time $$$t=0$$$) at position $$$0$$$ and heading downwards. The particle moves at the speed of $$$1$$$ unit per second. There are $$$n$$$ oscillators at positions $$$1,2,ldots,n$$$. Each oscillator can be described by a binary string. The initial state of each oscillator is the first character of its binary string. When the particle hits with an oscillator, the particle reverses its direction if its current state is $$$ exttt{1}$$$ and continues to move at the same direction if its current state is $$$ exttt{0}$$$, and that oscillator moves on to the next state (the next state of the last state is defined as the first state). Additionally, the particle always reverses its direction when it is at position $$$0$$$ or $$$n+1$$$ at time $$$t > 0$$$. Bosco would like to know the cycle length of the movement of particle. The cycle length is defined as the minimum value of $$$c$$$ such that for any time $$$t ge 0$$$, the position of the particle at time $$$t$$$ is same as the position of the particle at time $$$t+c$$$. It can be proved that such value $$$c$$$ always exists. As he realises the answer might be too large, he asks you to output your answer modulo $$$998244353$$$. Input The first line contains an integer $$$n$$$ ($$$1le nle10^6$$$) β the number of oscillators. The $$$i$$$-th of the next $$$n$$$ line contains a binary string $$$s_i$$$ ($$$1lelefts_i ightle10^6$$$) β the binary string, that contains only characters $$$ exttt{0}$$$ and $$$ exttt{1}$$$, describing the oscillator at position $$$i$$$. It is guaranteed that the sum of all $$$s_i$$$ does not exceed $$$10^6$$$. Output Output a single integer integer β the cycle length of the movement of the particle, modulo $$$998244353$$$. Examples Input 4 01010 0001 11 0001 Note In the first sample, the only oscillator at position $$$1$$$ always has state $$$ exttt{0}$$$. At time moments $$$0,1,2,3$$$ positions the particle are $$$0,1,2,1$$$ respectively. Then the same positions will be repeated, so $$$c=4$$$. Animation for the second sample: [here]( or [a smoother animation]( | 3,100 | [
"dp",
"math"
] | true | false | false | true | false | false | false | false | false | false |
Enjoy erasing Tenzing, identified as Accepted! Tenzing has $$$n$$$ balls arranged in a line. The color of the $$$i$$$-th ball from the left is $$$a_i$$$. Tenzing can do the following operation any number of times: select $$$i$$$ and $$$j$$$ such that $$$1leq i < j leq a$$$ and $$$a_i=a_j$$$, remove $$$a_i,a_{i+1},ldots,a_j$$$ from the array (and decrease the indices of all elements to the right of $$$a_j$$$ by $$$j-i+1$$$). Tenzing wants to know the maximum number of balls he can remove. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1leq tleq 10^3$$$) β the number of test cases. The description of test cases follows. The first line contains a single integer $$$n$$$ ($$$1leq nleq 2cdot 10^5$$$)xa0β the number of balls. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1leq a_i leq n$$$)xa0β the color of the balls. It is guaranteed that sum of $$$n$$$ of all test cases will not exceed $$$2cdot 10^5$$$. Output For each test case, output the maximum number of balls Tenzing can remove. Note In the first example, Tenzing will choose $$$i=2$$$ and $$$j=3$$$ in the first operation so that $$$a=[1,3,3]$$$. Then Tenzing will choose $$$i=2$$$ and $$$j=3$$$ again in the second operation so that $$$a=[1]$$$. So Tenzing can remove $$$4$$$ balls in total. In the second example, Tenzing will choose $$$i=1$$$ and $$$j=3$$$ in the first and only operation so that $$$a=[2]$$$. So Tenzing can remove $$$3$$$ balls in total. | 1,500 | [
"dp"
] | false | false | false | true | false | false | false | false | false | false |
Let $$$a$$$, $$$b$$$, and $$$c$$$ be integers. We define function $$$f(a, b, c)$$$ as follows: Order the numbers $$$a$$$, $$$b$$$, $$$c$$$ in such a way that $$$a le b le c$$$. Then return $$$gcd(a, b)$$$, where $$$gcd(a, b)$$$ denotes the $$$ for each $$$i$$$, $$$j$$$, $$$k$$$, such that $$$1 le i < j < k le n$$$. More formally, compute $$$$$$sum_{i = 1}^n sum_{j = i+1}^n sum_{k =j +1}^n f(a_i, a_j, a_k).$$$$$$ Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 8 cdot 10^4$$$)xa0β 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^5$$$)xa0β elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$8 cdot 10^4$$$. Output For each test case, output a single numberxa0β the sum from the problem statement. Example Input 2 5 2 3 6 12 17 8 6 12 8 10 15 12 18 16 Note In the first test case, the values of $$$f$$$ are as follows: $$$i=1$$$, $$$j=2$$$, $$$k=3$$$, $$$f(a_i,a_j,a_k)=f(2,3,6)=gcd(2,3)=1$$$; $$$i=1$$$, $$$j=2$$$, $$$k=4$$$, $$$f(a_i,a_j,a_k)=f(2,3,12)=gcd(2,3)=1$$$; $$$i=1$$$, $$$j=2$$$, $$$k=5$$$, $$$f(a_i,a_j,a_k)=f(2,3,17)=gcd(2,3)=1$$$; $$$i=1$$$, $$$j=3$$$, $$$k=4$$$, $$$f(a_i,a_j,a_k)=f(2,6,12)=gcd(2,6)=2$$$; $$$i=1$$$, $$$j=3$$$, $$$k=5$$$, $$$f(a_i,a_j,a_k)=f(2,6,17)=gcd(2,6)=2$$$; $$$i=1$$$, $$$j=4$$$, $$$k=5$$$, $$$f(a_i,a_j,a_k)=f(2,12,17)=gcd(2,12)=2$$$; $$$i=2$$$, $$$j=3$$$, $$$k=4$$$, $$$f(a_i,a_j,a_k)=f(3,6,12)=gcd(3,6)=3$$$; $$$i=2$$$, $$$j=3$$$, $$$k=5$$$, $$$f(a_i,a_j,a_k)=f(3,6,17)=gcd(3,6)=3$$$; $$$i=2$$$, $$$j=4$$$, $$$k=5$$$, $$$f(a_i,a_j,a_k)=f(3,12,17)=gcd(3,12)=3$$$; $$$i=3$$$, $$$j=4$$$, $$$k=5$$$, $$$f(a_i,a_j,a_k)=f(6,12,17)=gcd(6,12)=6$$$. The sum over all triples is $$$1+1+1+2+2+2+3+3+3+6=24$$$. In the second test case, there are $$$56$$$ ways to choose values of $$$i$$$, $$$j$$$, $$$k$$$. The sum over all $$$f(a_i,a_j,a_k)$$$ is $$$203$$$. | 2,000 | [
"brute force",
"dp",
"math"
] | true | false | false | true | false | false | true | false | false | false |
You have a playlist consisting of $$$n$$$ songs. The $$$i$$$-th song is characterized by two numbers $$$t_i$$$ and $$$b_i$$$ β its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of $$$3$$$ songs having lengths $$$[5, 7, 4]$$$ and beauty values $$$[11, 14, 6]$$$ is equal to $$$(5 + 7 + 4) cdot 6 = 96$$$. You need to choose at most $$$k$$$ songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 3 cdot 10^5$$$) β the number of songs in the playlist and the maximum number of songs you can choose, respectively. Each of the next $$$n$$$ lines contains two integers $$$t_i$$$ and $$$b_i$$$ ($$$1 le t_i, b_i le 10^6$$$) β the length and beauty of $$$i$$$-th song. Output Print one integer β the maximum pleasure you can get. Examples Input 4 3 4 7 15 1 3 6 6 8 Input 5 3 12 31 112 4 100 100 13 55 55 50 Note In the first test case we can choose songs $$${1, 3, 4}$$$, so the total pleasure is $$$(4 + 3 + 6) cdot 6 = 78$$$. In the second test case we can choose song $$$3$$$. The total pleasure will be equal to $$$100 cdot 100 = 10000$$$. | 1,600 | [
"brute force",
"data structures",
"sortings"
] | false | false | false | false | true | false | true | false | true | false |
Guy-Manuel and Thomas have an array $$$a$$$ of $$$n$$$ integers [$$$a_1, a_2, dots, a_n$$$]. In one step they can add $$$1$$$ to any element of the array. Formally, in one step they can choose any integer index $$$i$$$ ($$$1 le i le n$$$) and do $$$a_i := a_i + 1$$$. If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time. What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $$$a_1 + a_2 +$$$ $$$dots$$$ $$$+ a_n e 0$$$ and $$$a_1 cdot a_2 cdot$$$ $$$dots$$$ $$$cdot a_n e 0$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 100$$$)xa0β the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-100 le a_i le 100$$$)xa0β elements of the array . Output For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero. Example Input 4 3 2 -1 -1 4 -1 0 0 1 2 -1 2 3 0 -2 1 Note In the first test case, the sum is $$$0$$$. If we add $$$1$$$ to the first element, the array will be $$$[3,-1,-1]$$$, the sum will be equal to $$$1$$$ and the product will be equal to $$$3$$$. In the second test case, both product and sum are $$$0$$$. If we add $$$1$$$ to the second and the third element, the array will be $$$[-1,1,1,1]$$$, the sum will be equal to $$$2$$$ and the product will be equal to $$$-1$$$. It can be shown that fewer steps can't be enough. In the third test case, both sum and product are non-zero, we don't need to do anything. In the fourth test case, after adding $$$1$$$ twice to the first element the array will be $$$[2,-2,1]$$$, the sum will be $$$1$$$ and the product will be $$$-4$$$. | 800 | [
"implementation"
] | false | false | true | false | false | false | false | false | false | false |
Having overcome all the difficulties and hardships, Sasha finally decided to marry his girlfriend. To do this, he needs to give her an engagement ring. However, his girlfriend does not like such romantic gestures, but she does like binary search trees$$$^{dagger}$$$. So Sasha decided to give her such a tree. After spending a lot of time on wedding websites for programmers, he found the perfect binary search tree with the root at vertex $$$1$$$. In this tree, the value at vertex $$$v$$$ is equal to $$$val_v$$$. But after some time, he forgot the values in some vertices. Trying to remember the found tree, Sasha wondered β how many binary search trees could he have found on the website, if it is known that the values in all vertices are integers in the segment $$$[1, C]$$$. Since this number can be very large, output it modulo $$$998,244,353$$$. $$$^{dagger}$$$A binary search tree is a rooted binary tree in which for any vertex $$$x$$$, the following property holds: the values of all vertices in the left subtree of vertex $$$x$$$ (if it exists) are less than or equal to the value at vertex $$$x$$$, and the values of all vertices in the right subtree of vertex $$$x$$$ (if it exists) are greater than or equal to the value at vertex $$$x$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) β the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$C$$$ ($$$2 leq n leq 5 cdot 10^5$$$, $$$1 leq C leq 10^9$$$) β the number of vertices in the tree and the maximum allowed value at the vertex. The next $$$n$$$ lines describe the vertices of the tree. The $$$i$$$-th line contains three integers $$$L_i, R_i$$$ and $$$val_i$$$ ($$$-1 le L_i, R_i le n$$$, $$$-1 le val_i le C$$$, $$$L_i, R_i, val_i e 0$$$) β the number of the left child, the number of the right child, and the value at the $$$i$$$-th vertex, respectively. If $$$L_i = -1$$$, then the $$$i$$$-th vertex has no left son. If $$$R_i = -1$$$, then the $$$i$$$-th vertex has no right son. If $$$val_i = -1$$$, then the value at the $$$i$$$-th vertex is unknown. It is guaranteed that at least one suitable binary search tree exists. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output a single integer β the number of suitable binary search trees modulo $$$998,244,353$$$. Example Input 3 5 5 2 3 -1 -1 -1 2 4 -1 3 -1 5 -1 -1 -1 -1 3 69 2 3 47 -1 -1 13 -1 -1 69 3 3 2 3 -1 -1 -1 -1 -1 -1 -1 Note In the first test case, the binary search tree has the following form: Then the possible values at the vertices are: $$$[2, 2, 3, 2, 2]$$$, $$$[2, 2, 3, 2, 3]$$$, $$$[2, 2, 3, 3, 3]$$$, and $$$[3, 2, 3, 3, 3]$$$. In the second test case, the values at all vertices are known, so there is only one suitable binary search tree. | 2,300 | [
"brute force",
"data structures",
"math"
] | true | false | false | false | true | false | true | false | false | false |
This is an interactive problem. Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a $$$n imes n$$$ grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die. Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of $$$4$$$. Today Serval got up too late and only have time to make $$$2019$$$ queries. As his best friend, can you help him find the positions of the head and the tail? Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length $$$0$$$, that means it only has adjacent head and tail. Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries. Input The first line contains a single integer $$$n$$$ ($$$2leq n leq 1000$$$)xa0β the size of the grid. Output When you are ready to answer, you should print ! x1 y1 x2 y2, where $$$(x_1, y_1)$$$ represents the position of the head and $$$(x_2,y_2)$$$ represents the position of the tail. You can print head and tail in any order. Interaction To make a query, you should print ? x1 y1 x2 y2 ($$$1 leq x_1 leq x_2 leq n$$$, $$$1leq y_1 leq y_2 leq n$$$), representing a rectangle consisting of all cells $$$(x,y)$$$ such that $$$x_1 leq x leq x_2$$$ and $$$y_1 leq y leq y_2$$$. You will get a single integer as the answer. After printing a query, do not forget to output the end of line and flush the output, otherwise you will get Idleness limit exceeded. 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. Answer $$$-1$$$ instead of a valid answer means that you made an invalid query or exceeded the maximum number of queries. Exit immediately after receiving $$$-1$$$ and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. If your program cannot find out the head and tail of the snake correctly, you will also get a Wrong Answer verdict. Hacks To make a hack, print a single integer $$$n$$$ ($$$2 leq n leq 1000$$$) in the first line, indicating the size of the grid. Then print an integer $$$k$$$ ($$$2 leq k leq n^2$$$) in the second line, indicating the length of the snake. In the next $$$k$$$ lines, print $$$k$$$ pairs of integers $$$x_i, y_i$$$ ($$$1 leq x_i, y_i leq n$$$), each pair in a single line, indicating the $$$i$$$-th cell of snake, such that the adjacent pairs are adjacent, and all $$$k$$$ pairs are distinct. Examples Output ? 1 1 1 1 ? 1 2 1 2 ? 2 2 2 2 ! 1 1 2 1 Output ? 2 2 2 2 ? 2 1 2 3 ! 2 1 2 3 Note The pictures above show our queries and the answers in the first example. We first made a query for $$$(1,1)$$$ and got an answer $$$1$$$, then found that it must be connected to exactly one other cell. Then we made a query for $$$(1,2)$$$ and got an answer of $$$0$$$, then knew that the snake never entered it. So the cell connected to $$$(1,1)$$$ must be $$$(2,1)$$$. Then we made a query for $$$(2,2)$$$ and got an answer $$$0$$$, then knew that it never entered $$$(2,2)$$$ as well. So the snake cannot leave $$$(2,1)$$$, which implies that the answer is $$$(1,1)$$$ and $$$(2,1)$$$. The pictures above show our queries and the answers in the second example. By making query to $$$(2,2)$$$ and receiving $$$2$$$, we found that the snake occupies $$$(2,2)$$$. And by making query to rectangle from $$$(2,1)$$$ to $$$(2,3)$$$ and receiving answer $$$0$$$, we knew that it never goes out of the rectangle from $$$(2,1)$$$ to $$$(2,3)$$$. Since the first answer is $$$2$$$, both $$$(2,1)$$$ and $$$(2,3)$$$ must be occupied but none of others, so the answer is $$$(2,1)$$$ and $$$(2,3)$$$. | 2,200 | [
"binary search",
"brute force"
] | false | false | false | false | false | false | true | true | false | false |
Calin has $$$n$$$ buckets, the $$$i$$$-th of which contains $$$a_i$$$ wooden squares of side length $$$1$$$. Can Calin build a square using all the given squares? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0β the number of buckets. The second line of each test case contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0β the number of squares in each bucket. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" if Calin can build a square using all of the given $$$1 imes 1$$$ squares, 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). Example Input 5 1 9 2 14 2 7 1 2 3 4 5 6 7 6 1 3 5 7 9 11 4 2 2 2 2 Note In the first test case, Calin can build a $$$3 imes 3$$$ square. In the second test case, Calin can build a $$$4 imes 4$$$ square. In the third test case, Calin cannot build a square using all the given squares. | 800 | [
"binary search",
"implementation"
] | false | false | true | false | false | false | false | true | false | false |
In TreeWorld, there is a popular two-player game played on a tree with $$$n$$$ vertices labelled from $$$1$$$ to $$$n$$$. In this game, the tournament leaders first choose a vertex to be the root of the tree and choose another vertex (possibly the same vertex as the root) to place a coin on. Then, each player will take turns moving the coin to any child$$$^dagger$$$ of the vertex that the coin is currently on. The first player who is unable to make a move loses. Alice wants to be a tree LGM, so she spends a lot of time studying the game. She wrote down an $$$n$$$ by $$$n$$$ matrix $$$s$$$, where $$$s_{i,j} = mathtt{1}$$$ if the first player can win with the root of the tree chosen to be vertex $$$i$$$, and the coin was initially placed on vertex $$$j$$$. Otherwise, $$$s_{i, j} = mathtt{0}$$$. Alice is a perfectionist, so she assumes that both players play perfectly in the game. However, she accidentally knocked her head on the way to the tournament and forgot what the tree looked like. Determine whether there exists a tree that satisfies the winning and losing states represented by matrix $$$s$$$, and if it exists, construct a valid tree. $$$^dagger$$$ A vertex $$$c$$$ is a child of vertex $$$u$$$ if there is an edge between $$$c$$$ and $$$u$$$, and $$$c$$$ does not lie on the unique simple path from the root to vertex $$$u$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 5000$$$)xa0β the number of vertices in the tree. Each of the next $$$n$$$ lines contains a string with $$$n$$$ characters, the $$$j$$$-th character of the $$$i$$$-th line representing $$$s_{i, j}$$$ ($$$s_{i, j} in {mathtt{0}, mathtt{1}}$$$)xa0β the winning and losing states of the tree. Output If there is no tree satisfying the winning and losing states represented by matrix $$$s$$$, print a single line containing "NO". Otherwise, if there exists a tree satisfying matrix $$$s$$$, print "YES" on the first line, followed by $$$n - 1$$$ lines each containing two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$) representing that the tree has an edge between vertices $$$u$$$ and $$$v$$$. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. If there are multiple trees satisfying the winning and losing states represented by matrix $$$s$$$, print any of them. Note In the first test case, the line graph $$$1!-!4!-!2!-!3$$$ satisfies the winning and losing states represented by matrix $$$s$$$. For example, $$$s_{3,3} = 1$$$ as the first player can move the coin from $$$3 ightarrow 2$$$, then the second player moves the coin from $$$2 ightarrow 4$$$, and finally, the first player moves the coin from $$$4 ightarrow 1$$$. At this point, $$$1$$$ has no children, so the second player is unable to make a move and loses. On the other hand, $$$s_{1,3} = 0$$$ as if $$$1$$$ is the root, then $$$3$$$ has no children so the first player is unable to make the first move and loses. In the second test case, it is possible to prove that no tree satisfies the winning and losing states represented by matrix $$$s$$$. | 3,500 | [
"constructive algorithms"
] | false | false | false | false | false | true | false | false | false | false |
Polycarp was gifted an array $$$a$$$ of length $$$n$$$. Polycarp considers an array beautiful if there exists a number $$$C$$$, such that each number in the array occurs either zero or $$$C$$$ times. Polycarp wants to remove some elements from the array $$$a$$$ to make it beautiful. For example, if $$$n=6$$$ and $$$a = [1, 3, 2, 1, 4, 2]$$$, then the following options are possible to make the array $$$a$$$ array beautiful: Polycarp removes elements at positions $$$2$$$ and $$$5$$$, array $$$a$$$ becomes equal to $$$[1, 2, 1, 2]$$$; Polycarp removes elements at positions $$$1$$$ and $$$6$$$, array $$$a$$$ becomes equal to $$$[3, 2, 1, 4]$$$; Polycarp removes elements at positions $$$1, 2$$$ and $$$6$$$, array $$$a$$$ becomes equal to $$$[2, 1, 4]$$$; Help Polycarp determine the minimum number of elements to remove from the array $$$a$$$ to make it beautiful. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case consists of one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β 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$$$)xa0β array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one integerxa0β the minimum number of elements that Polycarp has to remove from the array $$$a$$$ to make it beautiful. Example Input 3 6 1 3 2 1 4 2 4 100 100 4 100 8 1 2 3 3 3 2 6 6 | 1,500 | [
"binary search",
"data structures",
"sortings"
] | false | false | false | false | true | false | false | true | true | false |
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $$$n$$$ problems and should choose at most three of them into this contest. The prettiness of the $$$i$$$-th problem is $$$a_i$$$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $$$x, y, z$$$, then $$$x$$$ should be divisible by neither $$$y$$$, nor $$$z$$$, $$$y$$$ should be divisible by neither $$$x$$$, nor $$$z$$$ and $$$z$$$ should be divisible by neither $$$x$$$, nor $$$y$$$. If the prettinesses of chosen problems are $$$x$$$ and $$$y$$$ then neither $$$x$$$ should be divisible by $$$y$$$ nor $$$y$$$ should be divisible by $$$x$$$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $$$q$$$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) β the number of queries. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β the number of problems. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$2 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the prettiness of the $$$i$$$-th problem. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$. | 2,100 | [
"brute force",
"sortings"
] | false | false | false | false | false | false | true | false | true | false |
The hobbits Frodo and Sam are carrying the One Ring to Mordor. In order not to be spotted by orcs, they decided to go through the mountains. The mountain relief can be represented as a polyline with $$$n$$$ points $$$(x_i, y_i)$$$, numbered from $$$1$$$ to $$$n$$$ ($$$x_i < x_{i + 1}$$$ for $$$1 le i le n - 1$$$). Hobbits start their journey at the point $$$(x_1, y_1)$$$ and should reach the point $$$(x_n, y_n)$$$ to complete their mission. The problem is that there is a tower with the Eye of Sauron, which watches them. The tower is located at the point $$$(x_n, y_n)$$$ and has the height $$$H$$$, so the Eye is located at the point $$$(x_n, y_n + H)$$$. In order to complete the mission successfully, the hobbits have to wear cloaks all the time when the Sauron Eye can see them, i.u2009e. when there is a direct line from the Eye to the hobbits which is not intersected by the relief. The hobbits are low, so their height can be considered negligibly small, but still positive, so when a direct line from the Sauron Eye to the hobbits only touches the relief, the Eye can see them. The Sauron Eye can't see hobbits when they are in the left position, but can see them when they are in the right position. The hobbits do not like to wear cloaks, so they wear them only when they can be spotted by the Eye. Your task is to calculate the total distance the hobbits have to walk while wearing cloaks. Input The first line of the input contains two integers $$$n$$$ and $$$H$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le H le 10^4$$$)xa0β the number of vertices in polyline and the tower height. The next $$$n$$$ lines contain two integers $$$x_i, y_i$$$ each ($$$0 le x_i le 4 cdot 10^5$$$; $$$0 le y_i le 10^4$$$)xa0β the coordinates of the polyline vertices. It is guaranteed that $$$x_i < x_{i + 1}$$$ for $$$1 le i le n - 1$$$. Output Print one real number β the total distance the hobbits have to walk while wearing cloaks. 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}$$$. | 2,500 | [
"binary search"
] | false | false | false | false | false | false | false | true | false | false |
Ivar the Boneless is a great leader. He is trying to capture Kattegat from Lagertha. The war has begun and wave after wave Ivar's warriors are falling in battle. Ivar has $$$n$$$ warriors, he places them on a straight line in front of the main gate, in a way that the $$$i$$$-th warrior stands right after $$$(i-1)$$$-th warrior. The first warrior leads the attack. Each attacker can take up to $$$a_i$$$ arrows before he falls to the ground, where $$$a_i$$$ is the $$$i$$$-th warrior's strength. Lagertha orders her warriors to shoot $$$k_i$$$ arrows during the $$$i$$$-th minute, the arrows one by one hit the first still standing warrior. After all Ivar's warriors fall and all the currently flying arrows fly by, Thor smashes his hammer and all Ivar's warriors get their previous strengths back and stand up to fight again. In other words, if all warriors die in minute $$$t$$$, they will all be standing to fight at the end of minute $$$t$$$. The battle will last for $$$q$$$ minutes, after each minute you should tell Ivar what is the number of his standing warriors. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q leq 200,000$$$)xa0β the number of warriors and the number of minutes in the battle. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) that represent the warriors' strengths. The third line contains $$$q$$$ integers $$$k_1, k_2, ldots, k_q$$$ ($$$1 leq k_i leq 10^{14}$$$), the $$$i$$$-th of them represents Lagertha's order at the $$$i$$$-th minute: $$$k_i$$$ arrows will attack the warriors. Output Output $$$q$$$ lines, the $$$i$$$-th of them is the number of standing warriors after the $$$i$$$-th minute. Examples Input 5 5 1 2 1 2 1 3 10 1 1 1 Note In the first example: after the 1-st minute, the 1-st and 2-nd warriors die. after the 2-nd minute all warriors die (and all arrows left over are wasted), then they will be revived thus answer is 5xa0β all warriors are alive. after the 3-rd minute, the 1-st warrior dies. after the 4-th minute, the 2-nd warrior takes a hit and his strength decreases by 1. after the 5-th minute, the 2-nd warrior dies. | 1,400 | [
"binary search"
] | false | false | false | false | false | false | false | true | false | false |
You are given a tournamentxa0β complete directed graph. In one operation you can pick any vertex $$$v$$$ and change the direction of all edges with $$$v$$$ on one of the ends (i.e all edges $$$u o v$$$ change their orientation to $$$v o u$$$ and vice versa). You want to make the tournament strongly connected with the smallest possible number of such operations if it is possible. Also, if it is possible, you need to find the number of ways to make this number of operations to make graph strongly connected (two ways are different if for some $$$i$$$ vertex that we chose on $$$i$$$-th operation in one way is different from vertex that we chose on $$$i$$$-th operation in another way). You only need to find this value modulo $$$998,244,353$$$. Input The first line of input contains one integer $$$n$$$ ($$$3 leq n leq 2000$$$): the number of vertices in the tournament. Following $$$n$$$ lines contain a description of the given tournament, each of them contains a binary string of length $$$n$$$. If $$$j$$$-th character of $$$i$$$-th string is equal to '1', then the graph has an edge $$$i o j$$$. It is guaranteed that there are no edges $$$i o i$$$ and the graph has exactly one edge among $$$i o j$$$ and $$$j o i$$$ for different $$$i$$$ and $$$j$$$. Output If it is not possible to convert tournament to strongly connected with the given operations, output "-1". Otherwise, output two integers: the smallest number of operations that you need to make the given graph strongly connected and the number of ways to do this number of operations to make graph strongly connected, modulo $$$998,244,353$$$. Examples Input 4 0010 1000 0100 1110 Input 6 010000 001000 100000 111001 111100 111010 | 3,200 | [
"brute force",
"graphs"
] | false | false | false | false | false | false | true | false | false | true |
As Kevin is in BigMan's house, suddenly a trap sends him onto a grid with $$$n$$$ rows and $$$m$$$ columns. BigMan's trap is configured by two arrays: an array $$$a_1,a_2,ldots,a_n$$$ and an array $$$b_1,b_2,ldots,b_m$$$. In the $$$i$$$-th row there is a heater which heats the row by $$$a_i$$$ degrees, and in the $$$j$$$-th column there is a heater which heats the column by $$$b_j$$$ degrees, so that the temperature of cell $$$(i,j)$$$ is $$$a_i+b_j$$$. Fortunately, Kevin has a suit with one parameter $$$x$$$ and two modes: heat resistance. In this mode suit can stand all temperatures greater or equal to $$$x$$$, but freezes as soon as reaches a cell with temperature less than $$$x$$$. cold resistance. In this mode suit can stand all temperatures less than $$$x$$$, but will burn as soon as reaches a cell with temperature at least $$$x$$$. Once Kevin lands on a cell the suit automatically turns to cold resistance mode if the cell has temperature less than $$$x$$$, or to heat resistance mode otherwise, and cannot change after that. We say that two cells are adjacent if they share an edge. Let a path be a sequence $$$c_1,c_2,ldots,c_k$$$ of cells such that $$$c_i$$$ and $$$c_{i+1}$$$ are adjacent for $$$1 leq i leq k-1$$$. We say that two cells are connected if there is a path between the two cells consisting only of cells that Kevin can step on. A connected component is a maximal set of pairwise connected cells. We say that a connected component is good if Kevin can escape the grid starting from it xa0β when it contains at least one border cell of the grid, and that it's bad otherwise. To evaluate the situation, Kevin gives a score of $$$1$$$ to each good component and a score of $$$2$$$ for each bad component. The final score will be the difference between the total score of components with temperatures bigger than or equal to $$$x$$$ and the score of components with temperatures smaller than $$$x$$$. There are $$$q$$$ possible values of $$$x$$$ that Kevin can use, and for each of them Kevin wants to know the final score. Help Kevin defeat BigMan! Input The first line contains three integers $$$n$$$,$$$m$$$,$$$q$$$ ($$$1 leq n,m,q leq 10^5$$$) xa0β the number of rows, columns, and the number of possible values for $$$x$$$ respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^5$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 leq b_i leq 10^5$$$). Each of the next $$$q$$$ lines contains one integer $$$x$$$ ($$$1 leq x leq 2 cdot 10^5$$$). | 3,300 | [
"graphs"
] | false | false | false | false | false | false | false | false | false | true |
This is an interactive problem. In this problem, you need to come up with a strategy for a cooperative game. This game is played by two players. Each player receives 5 cards. Each card has a random integer between 1 and 100 on it. It is guaranteed that all numbers on cards are distinct. The goal of the game is to play a card with a minimal number on it out of all 10 cards dealt to the players before any other card. The problem is that each player can only see their own cards and cannot communicate with another player in any way. The game consists of 5 turns. During each turn, players simultaneously make a move. Each player can either play their smallest card or do nothing. If on some turn the smallest card is played, and no other card is played on or before that turn, players win. If two cards are played at the same turn or if after all 5 turns, no card is still played, players lose. Players cannot communicate, so a strategy for the game should only be based on 5 cards that the player has. You can describe a strategy as five numbers $$$0.0 le p_i le 1.0, sum_{i=1}^{5}p_i le 1$$$, where $$$p_i$$$xa0β the probability of playing the player's smallest card in their hand on $$$i$$$-th turn. If you know the cards dealt to the players, and the strategies that players choose, you can compute the probability of winning by a simple formula. You will be given $$$n=1000$$$ randomly generated hands of 5 cards. You need to generate a strategy for each of the hands to maximize the probability of winning. After the judge program receives all $$$n$$$ strategies, it generates all possible valid pairs of those hands (pairs which have the same numbers are discarded), and computes a probability of winning based on two strategies provided by your program. To ensure that answers for different hands are independent, you must output a strategy for a hand and flush the standard output before reading information about the next hand. If the average probability of winning a game is more than 85% over all valid pairs of hands, the test is considered passed. This problem contains the sample test and $$$20$$$ randomly generated tests with $$$n = 1000$$$. Input The first line contains one integer $$$n$$$xa0β the number of hands. It is guaranteed that $$$n = 1000$$$ for all cases except the first sample case. Each of the next $$$n$$$ lines contains 5 numbers $$$a_i$$$ ($$$1 le a_i le 100, a_i < a_{i+1}$$$)xa0β the cards in the hand. It is guaranteed that each possible set of 5 cards has an equal probability of being chosen. | 2,700 | [
"constructive algorithms"
] | false | false | false | false | false | true | false | false | false | false |
Problem - 1421E - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force dp implementation *2700 No tag edit access β Contest materials ") Editorial") $$$ back in the same position. For example if the array contains the elements $$$ Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 03:51:59 (j1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name --- | 2,700 | [
"brute force",
"dp",
"implementation"
] | false | false | true | true | false | false | true | false | false | false |
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); bracket sequences ")(", "(" and ")" are not. There was an RBS. Some brackets have been replaced with question marks. Is it true that there is a unique way to replace question marks with brackets, so that the resulting sequence is an RBS? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 5 cdot 10^4$$$)xa0β the number of testcases. The only line of each testcase contains an RBS with some brackets replaced with question marks. Each character is either '(', ')' or '?'. At least one RBS can be recovered from the given sequence. The total length of the sequences over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print "YES" if the way to replace question marks with brackets, so that the resulting sequence is an RBS, is unique. If there is more than one way, then print "NO". Example Input 5 (?)) ?????? () ?? ?(?)()?) Note In the first testcase, the only possible original RBS is "(())". In the second testcase, there are multiple ways to recover an RBS. In the third and the fourth testcases, the only possible original RBS is "()". In the fifth testcase, the original RBS can be either "((()()))" or "(())()()". | 1,800 | [
"constructive algorithms",
"greedy",
"implementation"
] | false | true | true | false | false | true | false | false | false | false |
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). There are $$$n! = n cdot (n-1) cdot (n - 2) cdot ldots cdot 1$$$ different permutations of length $$$n$$$. Given a permutation $$$p$$$ of $$$n$$$ numbers, we create an array $$$a$$$ consisting of $$$2n$$$ numbers, which is equal to $$$p$$$ concatenated with its reverse. We then define the beauty of $$$p$$$ as the number of inversions in $$$a$$$. The number of inversions in the array $$$a$$$ is the number of pairs of indices $$$i$$$, $$$j$$$ such that $$$i < j$$$ and $$$a_i > a_j$$$. For example, for permutation $$$p = [1, 2]$$$, $$$a$$$ would be $$$[1, 2, 2, 1]$$$. The inversions in $$$a$$$ are $$$(2, 4)$$$ and $$$(3, 4)$$$ (assuming 1-based indexing). Hence, the beauty of $$$p$$$ is $$$2$$$. Your task is to find the sum of beauties of all $$$n!$$$ permutations of size $$$n$$$. Print the remainder we get when dividing this value by $$$1,000,000,007$$$ ($$$10^9 + 7$$$). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. Each test case has only one line β the integer $$$n$$$ ($$$1 leq n leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print one integer β the sum of beauties of all permutations of size $$$n$$$ modulo $$$1,000,000,007$$$ ($$$10^9 + 7$$$). Note For the first test case of the example, $$$p = [1]$$$ is the only permutation. $$$a = [1, 1]$$$ has $$$0$$$ inversions. For the second test case of the example, the permutations are $$$[1, 2]$$$ and $$$[2, 1]$$$. Their respective $$$a$$$ arrays are $$$[1, 2, 2, 1]$$$ and $$$[2, 1, 1, 2]$$$, both of which have $$$2$$$ inversions. | 900 | [
"greedy",
"math"
] | true | true | false | false | false | false | false | false | false | false |
The $$$ ext{$$$gcdSum$$$}$$$ of a positive integer is the $$$gcd$$$ of that integer with its sum of digits. Formally, $$$ ext{$$$gcdSum$$$}(x) = gcd(x, ext{ sum of digits of } x)$$$ for a positive integer $$$x$$$. $$$gcd(a, b)$$$ denotes the greatest common divisor of $$$a$$$ and $$$b$$$ β the largest integer $$$d$$$ such that both integers $$$a$$$ and $$$b$$$ are divisible by $$$d$$$. For example: $$$ ext{$$$gcdSum$$$}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3$$$. Given an integer $$$n$$$, find the smallest integer $$$x ge n$$$ such that $$$ ext{$$$gcdSum$$$}(x) > 1$$$. Input The first line of input contains one integer $$$t$$$ $$$(1 le t le 10^4)$$$ β the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(1 le n le 10^{18})$$$. All test cases in one test are different. Note Let us explain the three test cases in the sample. Test case 1: $$$n = 11$$$: $$$ ext{$$$gcdSum$$$}(11) = gcd(11, 1 + 1) = gcd(11, 2) = 1$$$. $$$ ext{$$$gcdSum$$$}(12) = gcd(12, 1 + 2) = gcd(12, 3) = 3$$$. So the smallest number $$$ge 11$$$ whose $$$gcdSum$$$ $$$> 1$$$ is $$$12$$$. Test case 2: $$$n = 31$$$: $$$ ext{$$$gcdSum$$$}(31) = gcd(31, 3 + 1) = gcd(31, 4) = 1$$$. $$$ ext{$$$gcdSum$$$}(32) = gcd(32, 3 + 2) = gcd(32, 5) = 1$$$. $$$ ext{$$$gcdSum$$$}(33) = gcd(33, 3 + 3) = gcd(33, 6) = 3$$$. So the smallest number $$$ge 31$$$ whose $$$gcdSum$$$ $$$> 1$$$ is $$$33$$$. Test case 3: $$$ n = 75$$$: $$$ ext{$$$gcdSum$$$}(75) = gcd(75, 7 + 5) = gcd(75, 12) = 3$$$. The $$$ ext{$$$gcdSum$$$}$$$ of $$$75$$$ is already $$$> 1$$$. Hence, it is the answer. | 800 | [
"brute force"
] | false | false | false | false | false | false | true | false | false | false |
You are given an undirected graph with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. There is an edge between vertices $$$u$$$ and $$$v$$$ if and only if $$$u oplus v$$$ is a . The description of test cases follows. The only line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the number of vertices in the graph. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output two lines. The first line should contain a single integer $$$k$$$ ($$$1 le k le n$$$)xa0β the minimum number of colors required. The second line should contain $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 le c_i le k$$$)xa0β the color of each vertex. If there are multiple solutions, output any of them. Example Output 1 1 2 1 2 2 1 2 2 3 1 2 2 3 3 1 2 2 3 3 4 1 2 2 3 3 4 Note In the first test case, the minimum number of colors is $$$1$$$, because there is only one vertex. In the second test case, the minimum number of colors is $$$2$$$, because there is an edge connecting $$$1$$$ and $$$2$$$ ($$$1 oplus 2 = 3$$$, which is a prime number). In the third test case, the minimum number of colors is still $$$2$$$, because $$$2$$$ and $$$3$$$ can be colored the same since there is no edge between $$$2$$$ and $$$3$$$ ($$$2 oplus 3 = 1$$$, which is not a prime number). In the fourth test case, it can be shown that the minimum number of colors is $$$3$$$. In the fifth test case, it can be shown that the minimum number of colors is $$$3$$$. In the sixth test case, it can be shown that the minimum number of colors is $$$4$$$. | 1,900 | [
"constructive algorithms",
"graphs",
"greedy",
"math"
] | true | true | false | false | false | true | false | false | false | true |
This is the easy version of a problem. The only difference between an easy and a hard version is the constraints on $$$t$$$ and $$$n$$$. You can make hacks only if both versions of the problem are solved. Arthur is giving a lesson to his famous $$$2 n$$$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $$$2 i - 1$$$ is sitting at the desk with the knight $$$2 i$$$. Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $$$i$$$-th knight as $$$a_i$$$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $$$maxlimits_{1 le i le n} (a_{2 i - 1} + a_{2 i}) - minlimits_{1 le i le n} (a_{2 i - 1} + a_{2 i})$$$. However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $$$a_i := a_{i + n}$$$, $$$a_{i + n} := a_i$$$ for any $$$1 le i le n$$$. Arthur can make any number of such swaps. What is the best result he can achieve? Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. It is followed by descriptions of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2000$$$)xa0β the number of desks. The second line consists of $$$2n$$$ integers $$$a_1, a_2, ldots, a_{2 n}$$$ ($$$1 le a_i le 10^9$$$)xa0β the intelligence values of the knights. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output a single line containing one integerxa0β the minimal difference Arthur can achieve. Example Input 5 2 6 6 4 4 1 10 17 3 1 10 1 10 1 10 3 3 3 4 5 5 4 5 1 2 3 4 5 6 7 8 9 10 Note In the first test case, Arthur can swap the second and the fourth knights. Then the total intelligence at both desks will be $$$10$$$. In the third test case, Arthur can make $$$0$$$ operations, which will result in the total intelligence of $$$11$$$ at each of the desks. In the fourth test case, Arthur can swap knights with indices $$$2$$$ and $$$5$$$ and achieve the difference of $$$2$$$. It can be proven that he cannot improve his result any further. | 2,700 | [
"data structures",
"dp"
] | false | false | false | true | true | false | false | false | false | false |
There is a grid, consisting of $$$2$$$ rows and $$$n$$$ columns. Each cell of the grid is either free or blocked. A free cell $$$y$$$ is reachable from a free cell $$$x$$$ if at least one of these conditions holds: $$$x$$$ and $$$y$$$ share a side; there exists a free cell $$$z$$$ such that $$$z$$$ is reachable from $$$x$$$ and $$$y$$$ is reachable from $$$z$$$. A connected region is a set of free cells of the grid such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule. For example, consider the following layout, where white cells are free, and dark grey cells are blocked: There are $$$3$$$ regions in it, denoted with red, green and blue color respectively: The given grid contains at most $$$1$$$ connected region. Your task is to calculate the number of free cells meeting the following constraint: if this cell is blocked, the number of connected regions becomes exactly $$$3$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β 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$$$)xa0β the number of columns. The $$$i$$$-th of the next two lines contains a description of the $$$i$$$-th row of the gridxa0β the string $$$s_i$$$, consisting of $$$n$$$ characters. Each character is either . (denoting a free cell) or x (denoting a blocked cell). Additional constraint on the input: the given grid contains at most $$$1$$$ connected region; the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0β the number of cells such that the number of connected regions becomes $$$3$$$ if this cell is blocked. Example Input 4 8 .......x .x.xx... 2 .. .. 3 xxx xxx 9 ..x.x.x.x x.......x Note In the first test case, if the cell $$$(1, 3)$$$ is blocked, the number of connected regions becomes $$$3$$$ (as shown in the picture from the statement). | 1,100 | [
"constructive algorithms"
] | false | false | false | false | false | true | false | false | false | false |
There are $$$n$$$ cities in Shaazzzland, numbered from $$$0$$$ to $$$n-1$$$. Ghaazzzland, the immortal enemy of Shaazzzland, is ruled by AaParsa. As the head of the Ghaazzzland's intelligence agency, AaParsa is carrying out the most important spying mission in Ghaazzzland's history on Shaazzzland. AaParsa has planted $$$m$$$ transport cannons in the cities of Shaazzzland. The $$$i$$$-th cannon is planted in the city $$$a_i$$$ and is initially pointing at city $$$b_i$$$. It is guaranteed that each of the $$$n$$$ cities has at least one transport cannon planted inside it, and that no two cannons from the same city are initially pointing at the same city (that is, all pairs $$$(a_i, b_i)$$$ are distinct). AaParsa used very advanced technology to build the cannons, the cannons rotate every second. In other words, if the $$$i$$$-th cannon is pointing towards the city $$$x$$$ at some second, it will target the city $$$(x + 1) mod n$$$ at the next second. As their name suggests, transport cannons are for transportation, specifically for human transport. If you use the $$$i$$$-th cannon to launch yourself towards the city that it's currently pointing at, you'll be airborne for $$$c_i$$$ seconds before reaching your target destination. If you still don't get it, using the $$$i$$$-th cannon at the $$$s$$$-th second (using which is only possible if you are currently in the city $$$a_i$$$) will shoot you to the city $$$(b_i + s) mod n$$$ and you'll land in there after $$$c_i$$$ seconds (so you'll be there in the $$$(s + c_i)$$$-th second). Also note the cannon that you initially launched from will rotate every second but you obviously won't change direction while you are airborne. AaParsa wants to use the cannons for travelling between Shaazzzland's cities in his grand plan, and he can start travelling at second $$$0$$$. For him to fully utilize them, he needs to know the minimum number of seconds required to reach city $$$u$$$ from city $$$v$$$ using the cannons for every pair of cities $$$(u, v)$$$. Note that AaParsa can stay in a city for as long as he wants. Input The first line contains two integers $$$n$$$ and $$$m$$$ $$$(2 le n le 600 , n le m le n^2)$$$ β the number of cities and cannons correspondingly. The $$$i$$$-th line of the following $$$m$$$ lines contains three integers $$$a_i$$$, $$$b_i$$$ and $$$c_i$$$ $$$( 0 le a_i , b_i le n-1 , 1 le c_i le 10^9)$$$, denoting the cannon in the city $$$a_i$$$, which is initially pointing to $$$b_i$$$ and travelling by which takes $$$c_i$$$ seconds. It is guaranteed that each of the $$$n$$$ cities has at least one transport cannon planted inside it, and that no two cannons from the same city are initially pointing at the same city (that is, all pairs $$$(a_i, b_i)$$$ are distinct). Output Print $$$n$$$ lines, each line should contain $$$n$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line should be equal to the minimum time required to reach city $$$j$$$ from city $$$i$$$. | 2,500 | [
"constructive algorithms",
"graphs"
] | false | false | false | false | false | true | false | false | false | true |
Recently, your friend discovered one special operation on an integer array $$$a$$$: 1. Choose two indices $$$i$$$ and $$$j$$$ ($$$i eq j$$$); 2. Set $$$a_i = a_j = a_i - a_j$$$. After playing with this operation for a while, he came to the next conclusion: For every array $$$a$$$ of $$$n$$$ integers, where $$$1 le a_i le 10^9$$$, you can find a pair of indices $$$(i, j)$$$ such that the total sum of $$$a$$$ will decrease after performing the operation. This statement sounds fishy to you, so you want to find a counterexample for a given integer $$$n$$$. Can you find such counterexample and prove him wrong? In other words, find an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) such that for all pairs of indices $$$(i, j)$$$ performing the operation won't decrease the total sum (it will increase or not change the sum). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains a single integer $$$n$$$ ($$$2 le n le 1000$$$)xa0β the length of array $$$a$$$. Output For each test case, if there is no counterexample array $$$a$$$ of size $$$n$$$, print NO. Otherwise, print YES followed by the array $$$a$$$ itself ($$$1 le a_i le 10^9$$$). If there are multiple counterexamples, print any. Example Output YES 1 337 NO YES 31 4 159 Note In the first test case, the only possible pairs of indices are $$$(1, 2)$$$ and $$$(2, 1)$$$. If you perform the operation on indices $$$(1, 2)$$$ (or $$$(2, 1)$$$), you'll get $$$a_1 = a_2 = 1 - 337 = 336$$$, or array $$$[336, 336]$$$. In both cases, the total sum increases, so this array $$$a$$$ is a counterexample. | 800 | [
"constructive algorithms",
"greedy"
] | false | true | false | false | false | true | false | false | false | false |
Pak Chanek is given an array $$$a$$$ of $$$n$$$ integers. For each $$$i$$$ ($$$1 leq i leq n$$$), Pak Chanek will write the one-element set $$${a_i}$$$ on a whiteboard. After that, in one operation, Pak Chanek may do the following: 1. Choose two different sets $$$S$$$ and $$$T$$$ on the whiteboard such that $$$S cap T = varnothing$$$ ($$$S$$$ and $$$T$$$ do not have any common elements). 2. Erase $$$S$$$ and $$$T$$$ from the whiteboard and write $$$S cup T$$$ (the union of $$$S$$$ and $$$T$$$) onto the whiteboard. After performing zero or more operations, Pak Chanek will construct a multiset $$$M$$$ containing the sizes of all sets written on the whiteboard. In other words, each element in $$$M$$$ corresponds to the size of a set after the operations. How many distinct$$$^dagger$$$ multisets $$$M$$$ can be created by this process? Since the answer may be large, output it modulo $$$998,244,353$$$. $$$^dagger$$$ Multisets $$$B$$$ and $$$C$$$ are different if and only if there exists a value $$$k$$$ such that the number of elements with value $$$k$$$ in $$$B$$$ is different than the number of elements with value $$$k$$$ in $$$C$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq n$$$). Output Output the number of distinct multisets $$$M$$$ modulo $$$998,244,353$$$. Note In the first example, the possible multisets $$$M$$$ are $$${1,1,1,1,1,1}$$$, $$${1,1,1,1,2}$$$, $$${1,1,1,3}$$$, $$${1,1,2,2}$$$, $$${1,1,4}$$$, $$${1,2,3}$$$, and $$${2,2,2}$$$. As an example, let's consider a possible sequence of operations. 1. In the beginning, the sets are $$${1}$$$, $$${1}$$$, $$${2}$$$, $$${1}$$$, $$${4}$$$, and $$${3}$$$. 2. Do an operation on sets $$${1}$$$ and $$${3}$$$. Now, the sets are $$${1}$$$, $$${1}$$$, $$${2}$$$, $$${4}$$$, and $$${1,3}$$$. 3. Do an operation on sets $$${2}$$$ and $$${4}$$$. Now, the sets are $$${1}$$$, $$${1}$$$, $$${1,3}$$$, and $$${2,4}$$$. 4. Do an operation on sets $$${1,3}$$$ and $$${2,4}$$$. Now, the sets are $$${1}$$$, $$${1}$$$, and $$${1,2,3,4}$$$. 5. The multiset $$$M$$$ that is constructed is $$${1,1,4}$$$. | 2,600 | [
"dp",
"math"
] | true | false | false | true | false | false | false | false | false | false |
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated. For example, Zookeeper can use two such operations: AABABBA $$$ o$$$ AABBA $$$ o$$$ AAA. Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string? Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 20000)$$$ xa0β the number of test cases. The description of the test cases follows. Each of the next $$$t$$$ lines contains a single test case each, consisting of a non-empty string $$$s$$$: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of $$$s$$$ are either 'A' or 'B'. It is guaranteed that the sum of $$$s$$$ (length of $$$s$$$) among all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integer: the length of the shortest string that Zookeeper can make. Example Input 3 AAA BABA AABBBABBBB Note For the first test case, you can't make any moves, so the answer is $$$3$$$. For the second test case, one optimal sequence of moves is BABA $$$ o$$$ BA. So, the answer is $$$2$$$. For the third test case, one optimal sequence of moves is AABBBABBBB $$$ o$$$ AABBBABB $$$ o$$$ AABBBB $$$ o$$$ ABBB $$$ o$$$ AB $$$ o$$$ (empty string). So, the answer is $$$0$$$. | 1,100 | [
"brute force",
"data structures"
] | false | false | false | false | true | false | true | false | false | false |
This is an easy version of the problem. The difference between the versions is that the string can be longer than in the easy version. You can only do hacks if both versions of the problem are passed. Kazimir Kazimirovich is a Martian gardener. He has a huge orchard of binary balanced apple trees. Recently Casimir decided to get himself three capybaras. The gardener even came up with their names and wrote them down on a piece of paper. The name of each capybara is a non-empty line consisting of letters "a" and "b". Denote the names of the capybaras by the lines $$$a$$$, $$$b$$$, and $$$c$$$. Then Casimir wrote the nonempty lines $$$a$$$, $$$b$$$, and $$$c$$$ in a row without spaces. For example, if the capybara's name was "aba", "ab", and "bb", then the string the gardener wrote down would look like "abaabbb". The gardener remembered an interesting property: either the string $$$b$$$ is lexicographically not smaller than the strings $$$a$$$ and $$$c$$$ at the same time, or the string $$$b$$$ is lexicographically not greater than the strings $$$a$$$ and $$$c$$$ at the same time. In other words, either $$$a le b$$$ and $$$c le b$$$ are satisfied, or $$$b le a$$$ and $$$b le c$$$ are satisfied (or possibly both conditions simultaneously). Here $$$le$$$ denotes the lexicographic "less than or equal to" for strings. Thus, $$$a le b$$$ means that the strings must either be equal, or the string $$$a$$$ must stand earlier in the dictionary than the string $$$b$$$. For a more detailed explanation of this operation, see "Notes" section. Today the gardener looked at his notes and realized that he cannot recover the names because they are written without spaces. He is no longer sure if he can recover the original strings $$$a$$$, $$$b$$$, and $$$c$$$, so he wants to find any triplet of names that satisfy the above property. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The only line of a test case contains the string $$$s$$$ ($$$3 le s le 100$$$)xa0β the names of the capybaras, written together. The string consists of English letters 'a' and 'b' only. It is guaranteed that the sum of string lengths over all test cases does not exceed $$$500$$$. Output For each test case, print three strings $$$a$$$, $$$b$$$ and $$$c$$$ on a single line, separated by spacesxa0β names of capybaras, such that writing them without spaces results in a line $$$s$$$. Either $$$a le b$$$ and $$$c le b$$$, or $$$b le a$$$ and $$$b le c$$$ must be satisfied. If there are several ways to restore the names, print any of them. If the names cannot be recovered, print ":(" (without quotes). Example Output b bb a a b a a a a ab b a a bb b Note A string $$$x$$$ is lexicographically smaller than a string $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x e y$$$; in the first position where $$$x$$$ and $$$y$$$ differ, the string $$$x$$$ has the letter 'a', and the string $$$y$$$ has the letter 'b'. Now let's move on to the examples. In the first test case, one of the possible ways to split the line $$$s$$$ into three linesxa0β "b", "bb", "a". In the third test case, we can see that the split satisfies two conditions at once (i.xa0e., $$$a le b$$$, $$$c le b$$$, $$$b le a$$$, and $$$b le c$$$ are true simultaneously). | 800 | [
"brute force",
"constructive algorithms",
"implementation"
] | false | false | true | false | false | true | true | false | false | false |
Nikita had a word consisting of exactly $$$3$$$ lowercase Latin letters. The letters in the Latin alphabet are numbered from $$$1$$$ to $$$26$$$, where the letter "a" has the index $$$1$$$, and the letter "z" has the index $$$26$$$. He encoded this word as the sum of the positions of all the characters in the alphabet. For example, the word "cat" he would encode as the integer $$$3 + 1 + 20 = 24$$$, because the letter "c" has the index $$$3$$$ in the alphabet, the letter "a" has the index $$$1$$$, and the letter "t" has the index $$$20$$$. However, this encoding turned out to be ambiguous! For example, when encoding the word "ava", the integer $$$1 + 22 + 1 = 24$$$ is also obtained. Determine the lexicographically smallest word of $$$3$$$ letters that could have been encoded. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases in the test. This is followed by the descriptions of the test cases. The first and only line of each test case contains an integer $$$n$$$ ($$$3 le n le 78$$$) β the encoded word. Output For each test case, output the lexicographically smallest three-letter word that could have been encoded on a separate line. Example Output aav rzz aaa czz auz | 800 | [
"brute force"
] | false | false | false | false | false | false | true | false | false | false |
Problem - 501E - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags binary search combinatorics implementation *2500 No tag edit access β Contest materials (1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_n_), that the elements from the _l_-th to the _r_-th one inclusive can be rearranged in such a way that the whole array will be a palindrome. In other words, pair (_l_,u2009_r_) should meet the condition that after some rearranging of numbers on positions from _l_ to _r_, inclusive (it is allowed not to rearrange the numbers at all), for any 1u2009β€u2009_i_u2009β€u2009_n_ following condition holds: _a_ Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 10:48:31 (i1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name --- | 2,500 | [
"binary search"
] | false | false | false | false | false | false | false | true | false | false |
A cooperative game is played by $$$m$$$ people. In the game, there are $$$3m$$$ sheets of paper: $$$m$$$ sheets with letter 'w', $$$m$$$ sheets with letter 'i', and $$$m$$$ sheets with letter 'n'. Initially, each person is given three sheets (possibly with equal letters). The goal of the game is to allow each of the $$$m$$$ people to spell the word "win" using their sheets of paper. In other words, everyone should have one sheet with letter 'w', one sheet with letter 'i', and one sheet with letter 'n'. To achieve the goal, people can make exchanges. Two people participate in each exchange. Both of them choose exactly one sheet of paper from the three sheets they own and exchange it with each other. Find the shortest sequence of exchanges after which everyone has one 'w', one 'i', and one 'n'. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$m$$$xa0($$$2 le m le 10^5$$$)xa0β the number of people. The $$$i$$$-th of the next $$$m$$$ lines contains a string $$$s_i$$$ of length $$$3$$$ consisting of lowercase English letters 'w', 'i', and 'n', denoting the letters person $$$i$$$ has on their sheets of paper at the beginning of the game, in arbitrary order. Each of the letters 'w', 'i', and 'n' appears on the sheets of paper exactly $$$m$$$ times in total. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print a non-negative integer $$$k$$$xa0β the smallest number of exchanges people need to make everyone have one 'w', one 'i', and one 'n'. In each of the next $$$k$$$ lines print four tokens $$$a_1$$$ $$$c_1$$$ $$$a_2$$$ $$$c_2$$$, describing the exchanges in chronological order ($$$1 le a_1, a_2 le m$$$; $$$a_1 e a_2$$$; $$$c_1, c_2$$$ are one of $$${mathtt{w}, mathtt{i}, mathtt{n}}$$$): person $$$a_1$$$ gives letter $$$c_1$$$ to person $$$a_2$$$, while person $$$a_2$$$ gives letter $$$c_2$$$ to person $$$a_1$$$ at the same time. If there are multiple solutions, print any. Example Input 3 2 nwi inw 3 inn nww wii 4 win www iii nnn Output 0 2 2 w 3 i 3 w 1 n 3 2 w 3 i 2 w 4 n 3 i 4 n | 1,900 | [
"constructive algorithms"
] | false | false | false | false | false | true | false | false | false | false |
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$. You can perform the following operation any number of times (possibly zero): select an index $$$i$$$ ($$$1 leq i leq n$$$) and swap $$$a_i$$$ and $$$b_i$$$. Let's define the cost of the array $$$a$$$ as $$$sum_{i=1}^{n} sum_{j=i + 1}^{n} (a_i + a_j)^2$$$. Similarly, the cost of the array $$$b$$$ is $$$sum_{i=1}^{n} sum_{j=i + 1}^{n} (b_i + b_j)^2$$$. Your task is to minimize the total cost of two arrays. Input Each test case consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 40$$$)xa0β the number of test cases. The following is a description of the input data sets. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0β the length of both arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 100$$$)xa0β elements of the first array. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq 100$$$)xa0β elements of the second array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$. Output For each test case, print the minimum possible total cost. Example Input 3 1 3 6 4 3 6 6 6 2 7 4 1 4 6 7 2 4 2 5 3 5 Note In the second test case, in one of the optimal answers after all operations $$$a = [2, 6, 4, 6]$$$, $$$b = [3, 7, 6, 1]$$$. The cost of the array $$$a$$$ equals to $$$(2 + 6)^2 + (2 + 4)^2 + (2 + 6)^2 + (6 + 4)^2 + (6 + 6)^2 + (4 + 6)^2 = 508$$$. The cost of the array $$$b$$$ equals to $$$(3 + 7)^2 + (3 + 6)^2 + (3 + 1)^2 + (7 + 6)^2 + (7 + 1)^2 + (6 + 1)^2 = 479$$$. The total cost of two arrays equals to $$$508 + 479 = 987$$$. | 1,800 | [
"dp",
"greedy",
"math"
] | true | true | false | true | false | false | false | false | false | false |
End of preview. Expand
in Data Studio
README.md exists but content is empty.
- Downloads last month
- 5