Problem ID stringlengths 2 6 | Problem Description stringlengths 0 7.52k | Rating float64 800 3.5k | 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 | __index_level_0__ int64 3 9.98k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1995B2 | This is the hard version of the problem. The only difference is that in this version, instead of listing the number of petals for each flower, the number of petals and the quantity of flowers in the store is set for all types of flowers. A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $$$n$$$ different types of flowers in the store, each of which is characterized by the number of petals and the quantity of this type of flower. A flower with $$$k$$$ petals costs $$$k$$$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use to decorate her cake should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $$$m$$$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet? Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0β the number of test cases. This is followed by descriptions of the test cases. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 le n le 2 cdot 10^5, 1 le m le 10^{18}$$$)xa0β the number of types of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $$$n$$$ different integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the number of petals of the $$$i$$$-th flower type in the store (for different indexes $$$i eq j$$$, it must be $$$a_i eq a_j$$$). The third line of each test case contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 le c_i le 10^9$$$), where $$$c_i$$$ is the quantity of the $$$i$$$-th flower type in the store. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot {10}^5$$$. Output For each test case, print one integerxa0β the maximum possible number of petals in a bouquet that a girl can collect, observing all the conditions listed above. Example Input 7 3 10 1 2 3 2 2 1 3 1033 206 207 1000 3 4 1 6 20 4 2 7 5 6 1 1 2 1 3 1 7 8 100000 239 30 610 122 24 40 8 2 12 13123 112 1456 124 100 123 10982 6 13 2 4 11 1 3 5 2 2 1 2 2 1 8 10330 206 210 200 201 198 199 222 1000 9 10 11 12 13 14 15 16 2 10000000000 11 12 87312315 753297050 Output 7 1033 19 99990 13 10000 9999999999 Note In the first test case, some valid bouquets are $$$(1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2)$$$. The maximum over all valid bouquets not greater than $$$10$$$ is $$$7$$$ for $$$(2, 2, 3)$$$. In the second test case, you can assemble a valid bouquet with $$$(206, 206, 207, 207, 207)$$$ with a sum of $$$1033$$$, which is the maximum number of petals the girl can buy. In the third test case, you can assemble a valid bouquet with $$$(5, 5, 5, 4)$$$ with a sum of $$$19$$$. It can be seen that no valid bouquet can have $$$20$$$ petals. | 1,700 | true | true | false | false | true | false | false | true | true | false | 295 |
1712E1 | We are sum for we are many Some Number This version of the problem differs from the next one only in the constraint on $$$t$$$. You can make hacks only if both versions of the problem are solved. You are given two positive integers $$$l$$$ and $$$r$$$. Count the number of distinct triplets of integers $$$(i, j, k)$$$ such that $$$l le i < j < k le r$$$ and $$$operatorname{lcm}(i,j,k) ge i + j + k$$$. Here $$$operatorname{lcm}(i, j, k)$$$ denotes the . Description of the test cases follows. The only line for each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le 2 cdot 10^5$$$, $$$l + 2 le r$$$). Output For each test case print one integerxa0β the number of suitable triplets. Example Input 5 1 4 3 5 8 86 68 86 6 86868 Output 3 1 78975 969 109229059713337 Note In the first test case, there are $$$3$$$ suitable triplets: $$$(1,2,3)$$$, $$$(1,3,4)$$$, $$$(2,3,4)$$$. In the second test case, there is $$$1$$$ suitable triplet: $$$(3,4,5)$$$. | 2,300 | true | false | false | false | false | false | true | true | false | false | 2,009 |
1942A | Let's call an array $$$a$$$ sorted if $$$a_1 leq a_2 leq ldots leq a_{n - 1} leq a_{n}$$$. You are given two of Farmer John's favorite integers, $$$n$$$ and $$$k$$$. He challenges you to find any array $$$a_1, a_2, ldots, a_{n}$$$ satisfying the following requirements: $$$1 leq a_i leq 10^9$$$ for each $$$1 leq i leq n$$$; Out of the $$$n$$$ total cyclic shifts of $$$a$$$, exactly $$$k$$$ of them are sorted.$$$^dagger$$$ If there is no such array $$$a$$$, output $$$-1$$$. $$$^dagger$$$The $$$x$$$-th ($$$1 leq x leq n$$$) cyclic shift of the array $$$a$$$ is $$$a_x, a_{x+1} ldots a_n, a_1, a_2 ldots a_{x - 1}$$$. If $$$c_{x, i}$$$ denotes the $$$i$$$'th element of the $$$x$$$'th cyclic shift of $$$a$$$, exactly $$$k$$$ such $$$x$$$ should satisfy $$$c_{x,1} leq c_{x,2} leq ldots leq c_{x, n - 1} leq c_{x, n}$$$. For example, the cyclic shifts for $$$a = [1, 2, 3, 3]$$$ are the following: $$$x = 1$$$: $$$[1, 2, 3, 3]$$$ (sorted); $$$x = 2$$$: $$$[2, 3, 3, 1]$$$ (not sorted); $$$x = 3$$$: $$$[3, 3, 1, 2]$$$ (not sorted); $$$x = 4$$$: $$$[3, 1, 2, 3]$$$ (not sorted). Input The first line contains $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0β the number of test cases. Each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq k leq n leq 10^3$$$)xa0β the length of $$$a$$$ and the number of sorted cyclic shifts $$$a$$$ must have. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$. Output For each test case, print a single line: if there is a valid array $$$a$$$, output $$$n$$$ integers, representing $$$a_1, a_2, ldots, a_{n}$$$; otherwise, output $$$-1$$$. If there are multiple solutions, print any of them. Note In the first testcase, $$$a = [1, 1]$$$ satisfies $$$n = 2, k = 2$$$: The two cyclic shifts of $$$a$$$ are $$$[a_1, a_2]$$$ and $$$[a_2, a_1]$$$, which are both $$$[1, 1]$$$ and are sorted. In the second testcase, $$$a = [69,420, 69, 420]$$$ satisfies $$$n = 3, k = 1$$$: The three cyclic shifts of $$$a$$$ are $$$[a_1, a_2, a_3]$$$, $$$[a_2, a_3, a_1]$$$, $$$[a_3, a_1, a_2]$$$, which are $$$[69,420, 69, 420]$$$, $$$[69, 420, 69,420]$$$, and $$$[420, 69,420, 69]$$$, respectively. Only $$$[69, 420, 69,420]$$$ is sorted. | 800 | true | false | false | false | false | true | false | false | false | false | 632 |
451A | After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of _n_ horizontal and _m_ vertical sticks. An intersection point is any point on the grid which is formed by the intersection of one horizontal stick and one vertical stick. In the grid shown below, _n_u2009=u20093 and _m_u2009=u20093. There are _n_u2009+u2009_m_u2009=u20096 sticks in total (horizontal sticks are shown in red and vertical sticks are shown in green). There are _n_Β·_m_u2009=u20099 intersection points, numbered from 1 to 9. The rules of the game are very simple. The players move in turns. Akshat won gold, so he makes the first move. During his/her move, a player must choose any remaining intersection point and remove from the grid all sticks which pass through this point. A player will lose the game if he/she cannot make a move (i.e. there are no intersection points remaining on the grid at his/her move). Assume that both players play optimally. Who will win the game? Input The first line of input contains two space-separated integers, _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009100). Output Print a single line containing "Akshat" or "Malvika" (without the quotes), depending on the winner of the game. Note Explanation of the first sample: The grid has four intersection points, numbered from 1 to 4. If Akshat chooses intersection point 1, then he will remove two sticks (1u2009-u20092 and 1u2009-u20093). The resulting grid will look like this. Now there is only one remaining intersection point (i.e. 4). Malvika must choose it and remove both remaining sticks. After her move the grid will be empty. In the empty grid, Akshat cannot make any move, hence he will lose. Since all 4 intersection points of the grid are equivalent, Akshat will lose no matter which one he picks. | 900 | false | false | true | false | false | false | false | false | false | false | 8,037 |
1301D | Bashar was practicing for the national programming contest. Because of sitting too much in front of the computer without doing physical movements and eating a lot Bashar became much fatter. Bashar is going to quit programming after the national contest and he is going to become an actor (just like his father), so he should lose weight. In order to lose weight, Bashar is going to run for $$$k$$$ kilometers. Bashar is going to run in a place that looks like a grid of $$$n$$$ rows and $$$m$$$ columns. In this grid there are two one-way roads of one-kilometer length between each pair of adjacent by side cells, one road is going from the first cell to the second one, and the other road is going from the second cell to the first one. So, there are exactly $$$(4 n m - 2n - 2m)$$$ roads. Let's take, for example, $$$n = 3$$$ and $$$m = 4$$$. In this case, there are $$$34$$$ roads. It is the picture of this case (arrows describe roads): Bashar wants to run by these rules: He starts at the top-left cell in the grid; In one move Bashar may go up (the symbol 'U'), down (the symbol 'D'), left (the symbol 'L') or right (the symbol 'R'). More formally, if he stands in the cell in the row $$$i$$$ and in the column $$$j$$$, i.e. in the cell $$$(i, j)$$$ he will move to: in the case 'U' to the cell $$$(i-1, j)$$$; in the case 'D' to the cell $$$(i+1, j)$$$; in the case 'L' to the cell $$$(i, j-1)$$$; in the case 'R' to the cell $$$(i, j+1)$$$; He wants to run exactly $$$k$$$ kilometers, so he wants to make exactly $$$k$$$ moves; Bashar can finish in any cell of the grid; He can't go out of the grid so at any moment of the time he should be on some cell; Bashar doesn't want to get bored while running so he must not visit the same road twice. But he can visit the same cell any number of times. Bashar asks you if it is possible to run by such rules. If it is possible, you should tell him how should he run. You should give him $$$a$$$ steps to do and since Bashar can't remember too many steps, $$$a$$$ should not exceed $$$3000$$$. In every step, you should give him an integer $$$f$$$ and a string of moves $$$s$$$ of length at most $$$4$$$ which means that he should repeat the moves in the string $$$s$$$ for $$$f$$$ times. He will perform the steps in the order you print them. For example, if the steps are $$$2$$$ RUD, $$$3$$$ UUL then the moves he is going to move are RUD $$$+$$$ RUD $$$+$$$ UUL $$$+$$$ UUL $$$+$$$ UUL $$$=$$$ RUDRUDUULUULUUL. Can you help him and give him a correct sequence of moves such that the total distance he will run is equal to $$$k$$$ kilometers or say, that it is impossible? Input The only line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 leq n, m leq 500$$$, $$$1 leq k leq 10 ^{9}$$$), which are the number of rows and the number of columns in the grid and the total distance Bashar wants to run. Output If there is no possible way to run $$$k$$$ kilometers, print "NO" (without quotes), otherwise print "YES" (without quotes) in the first line. If the answer is "YES", on the second line print an integer $$$a$$$ ($$$1 leq a leq 3000$$$)xa0β the number of steps, then print $$$a$$$ lines describing the steps. To describe a step, print an integer $$$f$$$ ($$$1 leq f leq 10^{9}$$$) and a string of moves $$$s$$$ of length at most $$$4$$$. Every character in $$$s$$$ should be 'U', 'D', 'L' or 'R'. Bashar will start from the top-left cell. Make sure to move exactly $$$k$$$ moves without visiting the same road twice and without going outside the grid. He can finish at any cell. We can show that if it is possible to run exactly $$$k$$$ kilometers, then it is possible to describe the path under such output constraints. Note The moves Bashar is going to move in the first example are: "RRLL". It is not possible to run $$$1000000000$$$ kilometers in the second example because the total length of the roads is smaller and Bashar can't run the same road twice. The moves Bashar is going to move in the third example are: "RRDDLLRR". The moves Bashar is going to move in the fifth example are: "RRRLLLDRRRDULLLD". It is the picture of his run (the roads on this way are marked with red and numbered in the order of his running): | 2,000 | false | false | true | false | false | true | false | false | false | true | 4,191 |
895E | Vasya and Petya were tired of studying so they decided to play a game. Before the game begins Vasya looks at array _a_ consisting of _n_ integers. As soon as he remembers all elements of _a_ the game begins. Vasya closes his eyes and Petya does _q_ actions of one of two types: 1) Petya says 4 integers _l_1,u2009_r_1,u2009_l_2,u2009_r_2xa0β boundaries of two non-intersecting segments. After that he swaps one random element from the [_l_1,u2009_r_1] segment with another random element from the [_l_2,u2009_r_2] segment. 2) Petya asks Vasya the sum of the elements of _a_ in the [_l_,u2009_r_] segment. Vasya is a mathematician so he answers Petya the mathematical expectation of the sum of the elements in the segment. Your task is to write a program which will answer the second type questions as Vasya would do it. In other words your program should print the mathematical expectation of the sum of the elements of _a_ in the [_l_,u2009_r_] segment for every second type query. Input The first line contains two integers _n_,u2009_q_ (2u2009β€u2009_n_u2009β€u2009105,u20091u2009β€u2009_q_u2009β€u2009105) xa0β the number of elements in the array and the number of queries you need to handle. The second line contains _n_ integers _a__i_ (1u2009β€u2009_a__i_u2009β€u2009109) xa0β elements of the array. The next _q_ lines contain Petya's actions of type 1 or 2. If it is a type 1 action then the line contains 5 integers 1,u2009_l_1,u2009_r_1,u2009_l_2,u2009_r_2 (1u2009β€u2009_l_1u2009β€u2009_r_1u2009β€u2009_n_,u20091u2009β€u2009_l_2u2009β€u2009_r_2u2009β€u2009_n_). If it is a type 2 query then the line contains 3 integers 2,u2009_l_,u2009_r_ (1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_n_). It is guaranteed that there is at least one type 2 query and segments [_l_1,u2009_r_1],u2009[_l_2,u2009_r_2] don't have common elements. Output For each type 2 query print one real numberxa0β the mathematical expectation of the sum of elements in the segment. Your answer will be considered correct if its absolute or relative error doesn't exceed 10u2009-u20094 xa0β formally, the answer is correct if where _x_ is jury's answer and _y_ is yours. Examples Input 4 4 1 1 2 2 1 2 2 3 3 2 1 2 1 1 2 3 4 2 1 2 Input 10 5 1 1 1 1 1 2 2 2 2 2 1 1 5 6 10 2 1 5 1 1 5 6 10 1 1 5 6 10 2 6 10 Input 10 10 1 2 3 4 5 6 7 8 9 10 1 1 5 6 10 1 1 5 6 10 2 1 5 1 1 3 6 9 2 1 3 1 5 7 8 10 1 1 1 10 10 2 1 5 2 7 10 2 1 10 Output 23.0000000 14.0000000 28.0133333 21.5733333 55.0000000 | 2,300 | false | false | false | false | true | false | false | false | false | false | 6,166 |
863C | Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is "Bob", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, "Alice". So now Ilya wants to compare his robots' performance in a simple game called "1-2-3". This game is similar to the "Rock-Paper-Scissors" game: both robots secretly choose a number from the set {1,u20092,u20093} and say it at the same moment. If both robots choose the same number, then it's a draw and noone gets any points. But if chosen numbers are different, then one of the robots gets a point: 3 beats 2, 2 beats 1 and 1 beats 3. Both robots' programs make them choose their numbers in such a way that their choice in (_i_u2009+u20091)-th game depends only on the numbers chosen by them in _i_-th game. Ilya knows that the robots will play _k_ games, Alice will choose number _a_ in the first game, and Bob will choose _b_ in the first game. He also knows both robots' programs and can tell what each robot will choose depending on their choices in previous game. Ilya doesn't want to wait until robots play all _k_ games, so he asks you to predict the number of points they will have after the final game. Input The first line contains three numbers _k_, _a_, _b_ (1u2009β€u2009_k_u2009β€u20091018, 1u2009β€u2009_a_,u2009_b_u2009β€u20093). Then 3 lines follow, _i_-th of them containing 3 numbers _A__i_,u20091, _A__i_,u20092, _A__i_,u20093, where _A__i_,u2009_j_ represents Alice's choice in the game if Alice chose _i_ in previous game and Bob chose _j_ (1u2009β€u2009_A__i_,u2009_j_u2009β€u20093). Then 3 lines follow, _i_-th of them containing 3 numbers _B__i_,u20091, _B__i_,u20092, _B__i_,u20093, where _B__i_,u2009_j_ represents Bob's choice in the game if Alice chose _i_ in previous game and Bob chose _j_ (1u2009β€u2009_B__i_,u2009_j_u2009β€u20093). Output Print two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after _k_ games. Examples Input 10 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 Input 8 1 1 2 2 1 3 3 1 3 1 3 1 1 1 2 1 1 1 2 3 Input 5 1 1 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 Note In the second example game goes like this: The fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice. | 1,800 | false | false | true | false | false | false | false | false | false | true | 6,292 |
1367C | Polycarp and his friends want to visit a new restaurant. The restaurant has $$$n$$$ tables arranged along a straight line. People are already sitting at some tables. The tables are numbered from $$$1$$$ to $$$n$$$ in the order from left to right. The state of the restaurant is described by a string of length $$$n$$$ which contains characters "1" (the table is occupied) and "0" (the table is empty). Restaurant rules prohibit people to sit at a distance of $$$k$$$ or less from each other. That is, if a person sits at the table number $$$i$$$, then all tables with numbers from $$$i-k$$$ to $$$i+k$$$ (except for the $$$i$$$-th) should be free. In other words, the absolute difference of the numbers of any two occupied tables must be strictly greater than $$$k$$$. For example, if $$$n=8$$$ and $$$k=2$$$, then: strings "10010001", "10000010", "00000000", "00100000" satisfy the rules of the restaurant; strings "10100100", "10011001", "11111111" do not satisfy to the rules of the restaurant, since each of them has a pair of "1" with a distance less than or equal to $$$k=2$$$. In particular, if the state of the restaurant is described by a string without "1" or a string with one "1", then the requirement of the restaurant is satisfied. You are given a binary string $$$s$$$ that describes the current state of the restaurant. It is guaranteed that the rules of the restaurant are satisfied for the string $$$s$$$. Find the maximum number of free tables that you can occupy so as not to violate the rules of the restaurant. Formally, what is the maximum number of "0" that can be replaced by "1" such that the requirement will still be satisfied? For example, if $$$n=6$$$, $$$k=1$$$, $$$s=$$$xa0"100010", then the answer to the problem will be $$$1$$$, since only the table at position $$$3$$$ can be occupied such that the rules are still satisfied. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2cdot 10^5$$$)xa0β the number of tables in the restaurant and the minimum allowed distance between two people. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of "0" and "1"xa0β a description of the free and occupied tables in the restaurant. The given string satisfy to the rules of the restaurantxa0β the difference between indices of any two "1" is more than $$$k$$$. The sum of $$$n$$$ for all test cases in one test does not exceed $$$2cdot 10^5$$$. Output For each test case output one integerxa0β the number of tables that you can occupy so as not to violate the rules of the restaurant. If additional tables cannot be taken, then, obviously, you need to output $$$0$$$. Note The first test case is explained in the statement. In the second test case, the answer is $$$2$$$, since you can choose the first and the sixth table. In the third test case, you cannot take any free table without violating the rules of the restaurant. | 1,300 | true | true | false | false | false | true | false | false | false | false | 3,841 |
1900F | For an array $$$b_1, b_2, ldots, b_m$$$, for some $$$i$$$ ($$$1 < i < m$$$), element $$$b_i$$$ is said to be a local minimum if $$$b_i < b_{i-1}$$$ and $$$b_i < b_{i+1}$$$. Element $$$b_1$$$ is said to be a local minimum if $$$b_1 < b_2$$$. Element $$$b_m$$$ is said to be a local minimum if $$$b_m < b_{m-1}$$$. For an array $$$b_1, b_2, ldots, b_m$$$, for some $$$i$$$ ($$$1 < i < m$$$), element $$$b_i$$$ is said to be a local maximum if $$$b_i > b_{i-1}$$$ and $$$b_i > b_{i+1}$$$. Element $$$b_1$$$ is said to be a local maximum if $$$b_1 > b_2$$$. Element $$$b_m$$$ is said to be a local maximum if $$$b_m > b_{m-1}$$$. Let $$$x$$$ be an array of distinct elements. We define two operations on it: $$$1$$$xa0β delete all elements from $$$x$$$ that are not local minima. $$$2$$$xa0β delete all elements from $$$x$$$ that are not local maxima. Define $$$f(x)$$$ as follows. Repeat operations $$$1, 2, 1, 2, ldots$$$ in that order until you get only one element left in the array. Return that element. For example, take an array $$$[1,3,2]$$$. We will first do type $$$1$$$ operation and get $$$[1, 2]$$$. Then we will perform type $$$2$$$ operation and get $$$[2]$$$. Therefore, $$$f([1,3,2]) = 2$$$. You are given a permutation$$$^dagger$$$ $$$a$$$ of size $$$n$$$ and $$$q$$$ queries. Each query consists of two integers $$$l$$$ and $$$r$$$ such that $$$1 le l le r le n$$$. The query asks you to compute $$$f([a_l, a_{l+1}, ldots, a_r])$$$. $$$^dagger$$$ A permutation of length $$$n$$$ is an array 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). Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 10^5$$$)xa0β the length of the permutation $$$a$$$ and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β the elements of permutation $$$a$$$. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$)xa0β the description of $$$i$$$-th query. Output For each query, output a single integerxa0β the answer to that query. Examples Input 7 5 1 4 3 6 2 7 5 1 1 1 2 2 3 1 4 1 7 Input 10 1 1 2 3 4 5 6 7 8 9 10 1 10 Note In the first query of the first example, the only number in the subarray is $$$1$$$, therefore it is the answer. In the second query of the first example, our subarray initially looks like $$$[1, 4]$$$. After performing type $$$1$$$ operation we get $$$[1]$$$. In the third query of the first example, our subarray initially looks like $$$[4, 3]$$$. After performing type $$$1$$$ operation we get $$$[3]$$$. In the fourth query of the first example, our subarray initially looks like $$$[1, 4, 3, 6]$$$. After performing type $$$1$$$ operation we get $$$[1, 3]$$$. Then we perform type $$$2$$$ operation and we get $$$[3]$$$. In the fifth query of the first example, our subarray initially looks like $$$[1, 4, 3, 6, 2, 7, 5]$$$. After performing type $$$1$$$ operation we get $$$[1,3,2,5]$$$. After performing type $$$2$$$ operation we get $$$[3,5]$$$. Then we perform type $$$1$$$ operation and we get $$$[3]$$$. In the first and only query of the second example our subarray initially looks like $$$[1,2,3,4,5,6,7,8,9,10]$$$. Here $$$1$$$ is the only local minimum, so only it is left after performing type $$$1$$$ operation. | 2,800 | false | false | true | false | true | false | false | true | false | false | 910 |
1152E | A permutation of length $$$k$$$ is a sequence of $$$k$$$ integers from $$$1$$$ to $$$k$$$ containing each integer exactly once. For example, the sequence $$$[3, 1, 2]$$$ is a permutation of length $$$3$$$. When Neko was five, he thought of an array $$$a$$$ of $$$n$$$ positive integers and a permutation $$$p$$$ of length $$$n - 1$$$. Then, he performed the following: Constructed an array $$$b$$$ of length $$$n-1$$$, where $$$b_i = min(a_i, a_{i+1})$$$. Constructed an array $$$c$$$ of length $$$n-1$$$, where $$$c_i = max(a_i, a_{i+1})$$$. Constructed an array $$$b'$$$ of length $$$n-1$$$, where $$$b'_i = b_{p_i}$$$. Constructed an array $$$c'$$$ of length $$$n-1$$$, where $$$c'_i = c_{p_i}$$$. For example, if the array $$$a$$$ was $$$[3, 4, 6, 5, 7]$$$ and permutation $$$p$$$ was $$$[2, 4, 1, 3]$$$, then Neko would have constructed the following arrays: $$$b = [3, 4, 5, 5]$$$ $$$c = [4, 6, 6, 7]$$$ $$$b' = [4, 5, 3, 5]$$$ $$$c' = [6, 7, 4, 6]$$$ Then, he wrote two arrays $$$b'$$$ and $$$c'$$$ on a piece of paper and forgot about it. 14 years later, when he was cleaning up his room, he discovered this old piece of paper with two arrays $$$b'$$$ and $$$c'$$$ written on it. However he can't remember the array $$$a$$$ and permutation $$$p$$$ he used. In case Neko made a mistake and there is no array $$$a$$$ and permutation $$$p$$$ resulting in such $$$b'$$$ and $$$c'$$$, print -1. Otherwise, help him recover any possible array $$$a$$$. Input The first line contains an integer $$$n$$$ ($$$2 leq n leq 10^5$$$)xa0β the number of elements in array $$$a$$$. The second line contains $$$n-1$$$ integers $$$b'_1, b'_2, ldots, b'_{n-1}$$$ ($$$1 leq b'_i leq 10^9$$$). The third line contains $$$n-1$$$ integers $$$c'_1, c'_2, ldots, c'_{n-1}$$$ ($$$1 leq c'_i leq 10^9$$$). Output If Neko made a mistake and there is no array $$$a$$$ and a permutation $$$p$$$ leading to the $$$b'$$$ and $$$c'$$$, print -1. Otherwise, print $$$n$$$ positive integers $$$a_i$$$ ($$$1 le a_i le 10^9$$$), denoting the elements of the array $$$a$$$. If there are multiple possible solutions, print any of them. Examples Input 8 2 3 1 1 2 4 3 3 4 4 2 5 5 4 Note The first example is explained is the problem statement. In the third example, for $$$a = [3, 4, 5, 2, 1, 4, 3, 2]$$$, a possible permutation $$$p$$$ is $$$[7, 1, 5, 4, 3, 2, 6]$$$. In that case, Neko would have constructed the following arrays: $$$b = [3, 4, 2, 1, 1, 3, 2]$$$ $$$c = [4, 5, 5, 2, 4, 4, 3]$$$ $$$b' = [2, 3, 1, 1, 2, 4, 3]$$$ $$$c' = [3, 4, 4, 2, 5, 5, 4]$$$ | 2,400 | false | false | false | false | false | true | false | false | false | true | 4,952 |
780E | The evil Bumbershoot corporation produces clones for gruesome experiments in a vast underground lab. On one occasion, the corp cloned a boy Andryusha who was smarter than his comrades. Immediately Andryusha understood that something fishy was going on there. He rallied fellow clones to go on a feud against the evil corp, and they set out to find an exit from the lab. The corp had to reduce to destroy the lab complex. The lab can be pictured as a connected graph with _n_ vertices and _m_ edges. _k_ clones of Andryusha start looking for an exit in some of the vertices. Each clone can traverse any edge once per second. Any number of clones are allowed to be at any vertex simultaneously. Each clone is allowed to stop looking at any time moment, but he must look at his starting vertex at least. The exit can be located at any vertex of the lab, hence each vertex must be visited by at least one clone. Each clone can visit at most vertices before the lab explodes. Your task is to choose starting vertices and searching routes for the clones. Each route can have at most vertices. Input The first line contains three integers _n_, _m_, and _k_ (1u2009β€u2009_n_u2009β€u20092Β·105, _n_u2009-u20091u2009β€u2009_m_u2009β€u20092Β·105, 1u2009β€u2009_k_u2009β€u2009_n_)xa0β the number of vertices and edges in the lab, and the number of clones. Each of the next _m_ lines contains two integers _x__i_ and _y__i_ (1u2009β€u2009_x__i_,u2009_y__i_u2009β€u2009_n_)xa0β indices of vertices connected by the respective edge. The graph is allowed to have self-loops and multiple edges. The graph is guaranteed to be connected. Output You should print _k_ lines. _i_-th of these lines must start with an integer _c__i_ ()xa0β the number of vertices visited by _i_-th clone, followed by _c__i_ integersxa0β indices of vertices visited by this clone in the order of visiting. You have to print each vertex every time it is visited, regardless if it was visited earlier or not. It is guaranteed that a valid answer exists. Note In the first sample case there is only one clone who may visit vertices in order (2, 1, 3), which fits the constraint of 6 vertices per clone. In the second sample case the two clones can visited vertices in order (2, 1, 3) and (4, 1, 5), which fits the constraint of 5 vertices per clone. | 2,100 | false | false | false | false | false | true | false | false | false | true | 6,644 |
2021B | You are given an array $$$a$$$ of $$$n$$$ positive integers and an integer $$$x$$$. You can do the following two-step operation any (possibly zero) number of times: 1. Choose an index $$$i$$$ ($$$1 leq i leq n$$$). 2. Increase $$$a_i$$$ by $$$x$$$, in other words $$$a_i := a_i + x$$$. Find the maximum value of the $$$operatorname{MEX}$$$ of $$$a$$$ if you perform the operations optimally. The $$$operatorname{MEX}$$$ (minimum excluded value) of an array is the smallest non-negative integer that is not in the array. For example: The $$$operatorname{MEX}$$$ of $$$[2,2,1]$$$ is $$$0$$$ because $$$0$$$ is not in the array. The $$$operatorname{MEX}$$$ of $$$[3,1,0,1]$$$ is $$$2$$$ because $$$0$$$ and $$$1$$$ are in the array but $$$2$$$ is not. The $$$operatorname{MEX}$$$ of $$$[0,3,1,2]$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ are in the array but $$$4$$$ is not. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 5000$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le x le 10^9$$$)xa0β the length of the array and the integer to be used in the operation. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0β the given array. 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 a single integer: the maximum $$$operatorname{MEX}$$$ of $$$a$$$ if you perform the operations optimally. Example Input 3 6 3 0 3 2 1 5 2 6 2 1 3 4 1 0 2 4 5 2 5 10 3 Note In the first test case, the $$$operatorname{MEX}$$$ of $$$a$$$ is $$$4$$$ without performing any operations, which is the maximum. In the second test case, the $$$operatorname{MEX}$$$ of $$$a$$$ is $$$5$$$ without performing any operations. If we perform two operations both with $$$i=1$$$, we will have the array $$$a=[5,3,4,1,0,2]$$$. Then, the $$$operatorname{MEX}$$$ of $$$a$$$ will become $$$6$$$, which is the maximum. In the third test case, the $$$operatorname{MEX}$$$ of $$$a$$$ is $$$0$$$ without performing any operations, which is the maximum. | 1,200 | true | true | false | false | false | false | true | false | false | false | 145 |
1575I | Chanek Jones is back, helping his long-lost relative Indiana Jones, to find a secret treasure in a maze buried below a desert full of illusions. The map of the labyrinth forms a tree with $$$n$$$ rooms numbered from $$$1$$$ to $$$n$$$ and $$$n - 1$$$ tunnels connecting them such that it is possible to travel between each pair of rooms through several tunnels. The $$$i$$$-th room ($$$1 leq i leq n$$$) has $$$a_i$$$ illusion rate. To go from the $$$x$$$-th room to the $$$y$$$-th room, there must exist a tunnel between $$$x$$$ and $$$y$$$, and it takes $$$max(a_x + a_y, a_x - a_y)$$$ energy. $$$z$$$ denotes the absolute value of $$$z$$$. To prevent grave robbers, the maze can change the illusion rate of any room in it. Chanek and Indiana would ask $$$q$$$ queries. There are two types of queries to be done: $$$1 u c$$$ β The illusion rate of the $$$x$$$-th room is changed to $$$c$$$ ($$$1 leq u leq n$$$, $$$0 leq c leq 10^9$$$). $$$2 u v$$$ β Chanek and Indiana ask you the minimum sum of energy needed to take the secret treasure at room $$$v$$$ if they are initially at room $$$u$$$ ($$$1 leq u, v leq n$$$). Help them, so you can get a portion of the treasure! Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 leq n leq 10^5$$$, $$$1 leq q leq 10^5$$$) β the number of rooms in the maze and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$) β inital illusion rate of each room. The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$s_i$$$ and $$$t_i$$$ ($$$1 leq s_i, t_i leq n$$$), meaning there is a tunnel connecting $$$s_i$$$-th room and $$$t_i$$$-th room. The given edges form a tree. The next $$$q$$$ lines contain the query as described. The given queries are valid. Output For each type $$$2$$$ query, output a line containing an integer β the minimum sum of energy needed for Chanek and Indiana to take the secret treasure. Example Input 6 4 10 -9 2 -1 4 -6 1 5 5 4 5 6 6 2 6 3 2 1 2 1 1 -3 2 1 2 2 3 3 Note In the first query, their movement from the $$$1$$$-st to the $$$2$$$-nd room is as follows. $$$1 ightarrow 5$$$ β takes $$$max(10 + 4, 10 - 4) = 14$$$ energy. $$$5 ightarrow 6$$$ β takes $$$max(4 + (-6), 4 - (-6)) = 10$$$ energy. $$$6 ightarrow 2$$$ β takes $$$max(-6 + (-9), -6 - (-9)) = 15$$$ energy. In total, it takes $$$39$$$ energy. In the second query, the illusion rate of the $$$1$$$-st room changes from $$$10$$$ to $$$-3$$$. In the third query, their movement from the $$$1$$$-st to the $$$2$$$-nd room is as follows. $$$1 ightarrow 5$$$ β takes $$$max(-3 + 4, -3 - 4) = 7$$$ energy. $$$5 ightarrow 6$$$ β takes $$$max(4 + (-6), 4 - (-6)) = 10$$$ energy. $$$6 ightarrow 2$$$ β takes $$$max(-6 + (-9), -6 - (-9)) = 15$$$ energy. Now, it takes $$$32$$$ energy. | 2,300 | false | false | false | false | true | false | false | false | false | false | 2,760 |
1800G | Kid was gifted a tree of $$$n$$$ vertices with the root in the vertex $$$1$$$. Since he really like symmetrical objects, Kid wants to find out if this tree is symmetrical. For example, the trees in the picture above are symmetrical. And the trees in this picture are not symmetrical. Formally, a tree is symmetrical if there exists an order of children such that: The subtree of the leftmost child of the root is a mirror image of the subtree of the rightmost child; the subtree of the second-left child of the root is a mirror image of the subtree of the second-right child of the root; ... if the number of children of the root is odd, then the subtree of the middle child should be symmetrical. Input The first line of input data contains single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases in the test. The first line of each case contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the number of vertices in the tree. The following $$$n-1$$$ lines contain two integers each $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) β indices of vertices connected by an edge. It is guaranteed that the sum of $$$n$$$ over all cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ strings, each of which is the answer to the corresponding test case. As an answer, output "YES" if this tree is symmetrical, 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 6 6 1 5 1 6 1 2 2 3 2 4 7 1 5 1 3 3 6 1 4 4 7 4 2 9 1 2 2 4 2 3 3 5 1 7 7 6 7 8 8 9 10 2 9 9 10 2 3 6 7 4 3 1 2 3 8 2 5 6 5 10 3 2 8 10 9 7 4 2 8 2 2 1 4 5 6 5 5 7 1 Output YES NO YES NO NO YES | 2,200 | false | false | true | false | false | false | false | false | false | false | 1,467 |
1355F | This is an interactive problem. We have hidden an integer $$$1 le X le 10^{9}$$$. You don't have to guess this number. You have to find the number of divisors of this number, and you don't even have to find the exact number: your answer will be considered correct if its absolute error is not greater than 7 or its relative error is not greater than $$$0.5$$$. More formally, let your answer be $$$ans$$$ and the number of divisors of $$$X$$$ be $$$d$$$, then your answer will be considered correct if at least one of the two following conditions is true: $$$ ans - d le 7$$$; $$$frac{1}{2} le frac{ans}{d} le 2$$$. You can make at most $$$22$$$ queries. One query consists of one integer $$$1 le Q le 10^{18}$$$. In response, you will get $$$gcd(X, Q)$$$xa0β the greatest common divisor of $$$X$$$ and $$$Q$$$. The number $$$X$$$ is fixed before all queries. In other words, interactor is not adaptive. Let's call the process of guessing the number of divisors of number $$$X$$$ a game. In one test you will have to play $$$T$$$ independent games, that is, guess the number of divisors $$$T$$$ times for $$$T$$$ independent values of $$$X$$$. Input The first line of input contains one integer $$$T$$$ ($$$1 le T le 100$$$)xa0β the number of games. Interaction To make a query print a line "? Q" ($$$1 le Q le 10^{18}$$$). After that read one integer $$$gcd(X, Q)$$$. You can make no more than $$$22$$$ such queries during one game. If you are confident that you have figured out the number of divisors of $$$X$$$ with enough precision, you can print your answer in "! ans" format. $$$ans$$$ have to be an integer. If this was the last game, you have to terminate the program, otherwise you have to start the next game immediately. Note that the interactor doesn't print anything in response to you printing answer. After printing a query do not forget to output end of line and flush the output. 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. Hacks To hack, use the following format: The first line contains one integer $$$T$$$ ($$$1 le T le 100$$$)xa0β the number of games. Each of the next $$$T$$$ lines contains one integer $$$X$$$ ($$$1 le X le 10^{9}$$$)xa0β the hidden number. So the example has the form 2 998244353 4194304 Example Input 2 1 1 1 1024 1048576 4194304 Output ? 982306799268821872 ? 230856864650023977 ? 134690134760714371 ! 5 ? 1024 ? 1048576 ? 1073741824 ! 42 Note Why the limitation for number of queries is 22 exactly? Maybe the problem author is a Taylor Swift fan. Let's look at the example. In the first game $$$X = 998,244,353$$$ is hidden. Would be hard to guess this, right? This number is prime, so the number of its divisors is 2. The solution has made several random queries, and all the responses turned out to be 1 (strange things, not even one of three random numbers is divisible by $$$998,244,353$$$). It's fare to assume that the hidden number doesn't have many divisors, so the solution has answered 5. Why not. This answer will be considered correct since $$$ 5 - 2 = 3 le 7$$$. In the second game $$$X = 4,194,304 = 2^{22}$$$ is hidden, it has 23 divisors. The solution has made queries $$$1024 = 2^{10}$$$, $$$1,048,576 =2^{20}$$$, $$$1,073,741,824 = 2^{30}$$$ and got responses $$$1024 = 2^{10}$$$, $$$1,048,576 =2^{20}$$$, $$$4,194,304 = 2^{22}$$$, respectively. Then the solution got completely confused and answered the answer to The Ultimate Question of Life, the Universe, and Everything. This answer will be considered correct since $$$frac{1}{2} le frac{42}{23} le 2$$$. | 2,600 | false | false | false | false | false | true | false | false | false | false | 3,926 |
337E | Problem - 337E - 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 number theory trees *2200 No tag edit access β Contest materials . The second line contains _n_ distinct space-separated integers _a__i_ (2u2009β€u2009_a__i_u2009β€u20091012). Output Print a single integer β the minimum number of vertices in the divisor tree that contains each of the numbers _a__i_. Examples Input 2 6 10 Output 7 Input 4 6 72 8 4 Output 12 Input 1 7 Output 1 Note Sample 1. The smallest divisor tree looks this way: Sample 2. In this case you can build the following divisor tree: Sample 3. Note that the tree can consist of a single vertex. | 2,200 | false | false | false | false | false | false | true | false | false | false | 8,483 |
23E | Problem - 23E - Codeforces =============== xa0 : he deletes any (possibly, zero) amount of edges of the tree, and counts the product of sizes of the connected components left after the deletion. Your task is to find out the maximum number that Bob can get in his new game for a given tree. Input The first input line contains integer number _n_ (1u2009β€u2009_n_u2009β€u2009700) β amount of vertices in the tree. The following _n_u2009-u20091 lines contain the description of the edges. Each line contains the pair of vertices' indexes, joined by an edge, _a__i_, _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_). It's guaranteed that the graph described in the input is a tree. Output Output the only number β the maximum product of sizes of the connected components, that Bob can get after deleting some of the tree's edges. Examples Input 5 1 2 2 3 3 4 4 5 Output 6 Input 8 1 2 1 3 2 4 2 5 3 6 3 7 6 8 Output 18 Input 3 1 2 1 3 Output 3 | 2,500 | false | false | false | true | false | false | false | false | false | false | 9,879 |
1114F | You are given an array $$$a_1, a_2, ldots, a_n$$$. You need to perform $$$q$$$ queries of the following two types: 1. "MULTIPLY l r x"xa0β for every $$$i$$$ ($$$l le i le r$$$) multiply $$$a_i$$$ by $$$x$$$. 2. "TOTIENT l r"xa0β print $$$varphi(prod limits_{i=l}^{r} a_i)$$$ taken modulo $$$10^9+7$$$, where $$$varphi$$$ denotes Euler's totient function. The $$$) is the number of integers $$$x$$$ ($$$1 le x le n$$$) such that $$$gcd(n,x) = 1$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n le 4 cdot 10^5$$$, $$$1 le q le 2 cdot 10^5$$$)xa0β the number of elements in array $$$a$$$ and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 300$$$)xa0β the elements of array $$$a$$$. Then $$$q$$$ lines follow, describing queries in the format given in the statement. 1. "MULTIPLY l r x" ($$$1 le l le r le n$$$, $$$1 le x le 300$$$)xa0β denotes a multiplication query. 2. "TOTIENT l r" ($$$1 le l le r le n$$$)xa0β denotes a query on the value of Euler's totient function. It is guaranteed that there is at least one "TOTIENT" query. Output For each "TOTIENT" query, print the answer to it. Example Input 4 4 5 9 1 2 TOTIENT 3 3 TOTIENT 3 4 MULTIPLY 4 4 3 TOTIENT 4 4 Note In the first example, $$$varphi(1) = 1$$$ for the first query, $$$varphi(2) = 1$$$ for the second query and $$$varphi(6) = 2$$$ for the third one. | 2,400 | true | false | false | false | true | false | false | false | false | false | 5,129 |
1328C | A number is ternary if it contains only digits $$$0$$$, $$$1$$$ and $$$2$$$. For example, the following numbers are ternary: $$$1022$$$, $$$11$$$, $$$21$$$, $$$2002$$$. You are given a long ternary number $$$x$$$. The first (leftmost) digit of $$$x$$$ is guaranteed to be $$$2$$$, the other digits of $$$x$$$ can be $$$0$$$, $$$1$$$ or $$$2$$$. Let's define the ternary XOR operation $$$odot$$$ of two ternary numbers $$$a$$$ and $$$b$$$ (both of length $$$n$$$) as a number $$$c = a odot b$$$ of length $$$n$$$, where $$$c_i = (a_i + b_i) % 3$$$ (where $$$%$$$ is modulo operation). In other words, add the corresponding digits and take the remainders of the sums when divided by $$$3$$$. For example, $$$10222 odot 11021 = 21210$$$. Your task is to find such ternary numbers $$$a$$$ and $$$b$$$ both of length $$$n$$$ and both without leading zeros that $$$a odot b = x$$$ and $$$max(a, b)$$$ is the minimum possible. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 5 cdot 10^4$$$) β the length of $$$x$$$. The second line of the test case contains ternary number $$$x$$$ consisting of $$$n$$$ digits $$$0, 1$$$ or $$$2$$$. It is guaranteed that the first digit of $$$x$$$ is $$$2$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^4$$$ ($$$sum n le 5 cdot 10^4$$$). Output For each test case, print the answer β two ternary integers $$$a$$$ and $$$b$$$ both of length $$$n$$$ and both without leading zeros such that $$$a odot b = x$$$ and $$$max(a, b)$$$ is the minimum possible. If there are several answers, you can print any. Example Input 4 5 22222 5 21211 1 2 9 220222021 Output 11111 11111 11000 10211 1 1 110111011 110111010 | 1,200 | false | true | true | false | false | false | false | false | false | false | 4,069 |
1845A | You are given an integer $$$n$$$, which you want to obtain. You have an unlimited supply of every integer from $$$1$$$ to $$$k$$$, except integer $$$x$$$ (there are no integer $$$x$$$ at all). You are allowed to take an arbitrary amount of each of these integers (possibly, zero). Can you make the sum of taken integers equal to $$$n$$$? If there are multiple answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of testcases. The only line of each testcase contains three integers $$$n, k$$$ and $$$x$$$ ($$$1 le x le k le n le 100$$$). Output For each test case, in the first line, print "YES" or "NO"xa0β whether you can take an arbitrary amount of each integer from $$$1$$$ to $$$k$$$, except integer $$$x$$$, so that their sum is equal to $$$n$$$. If you can, the second line should contain a single integer $$$m$$$xa0β the total amount of taken integers. The third line should contain $$$m$$$ integersxa0β each of them from $$$1$$$ to $$$k$$$, not equal to $$$x$$$, and their sum is $$$n$$$. If there are multiple answers, print any of them. Example Input 5 10 3 2 5 2 1 4 2 1 7 7 3 6 1 1 Output YES 6 3 1 1 1 1 3 NO YES 2 2 2 YES 1 7 NO Note Another possible answer for the first testcase is $$$[3, 3, 3, 1]$$$. Note that you don't have to minimize the amount of taken integers. There also exist other answers. In the second testcase, you only have an unlimited supply of integer $$$2$$$. There is no way to get sum $$$5$$$ using only them. In the fifth testcase, there are no integers available at all, so you can't get any positive sum. | 800 | true | false | true | false | false | true | false | false | false | false | 1,214 |
1646D | You are given a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. A tree is a connected undirected graph without cycles. For each $$$i=1,2, ldots, n$$$, let $$$w_i$$$ be the weight of the $$$i$$$-th vertex. A vertex is called good if its weight is equal to the sum of the weights of all its neighbors. Initially, the weights of all nodes are unassigned. Assign positive integer weights to each vertex of the tree, such that the number of good vertices in the tree is maximized. If there are multiple ways to do it, you have to find one that minimizes the sum of weights of all vertices in the tree. Input The first line contains one integer $$$n$$$ ($$$2le nle 2cdot 10^5$$$) β the number of vertices in the tree. Then, $$$nβ1$$$ lines follow. Each of them contains two integers $$$u$$$ and $$$v$$$ ($$$1le u,vle n$$$) denoting an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the edges form a tree. Output In the first line print two integers xa0β the maximum number of good vertices and the minimum possible sum of weights for that maximum. In the second line print $$$n$$$ integers $$$w_1, w_2, ldots, w_n$$$ ($$$1le w_ile 10^9$$$) xa0β the corresponding weight assigned to each vertex. It can be proven that there exists an optimal solution satisfying these constraints. If there are multiple optimal solutions, you may print any. Examples Input 9 3 4 7 6 2 1 8 3 5 6 1 8 8 6 9 6 Output 6 11 1 1 1 1 1 1 1 3 1 Note This is the tree for the first test case: In this case, if you assign a weight of $$$1$$$ to each vertex, then the good vertices (which are painted black) are $$$1$$$, $$$3$$$ and $$$4$$$. It impossible to assign weights so that all vertices are good vertices. The minimum sum of weights in this case is $$$1+1+1+1=4$$$, and it is impossible to have a lower sum because the weights have to be positive integers. This is the tree for the second test case: In this case, if you assign a weight of $$$1$$$ to each vertex, then the good vertices (which are painted black) are $$$2$$$ and $$$3$$$. It can be proven that this is an optimal assignment. | 2,000 | false | false | true | true | false | true | false | false | false | false | 2,392 |
712A | There are _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ written in a row. For all _i_ from 1 to _n_, values _a__i_ are defined by the crows performing the following procedure: The crow sets _a__i_ initially 0. The crow then adds _b__i_ to _a__i_, subtracts _b__i_u2009+u20091, adds the _b__i_u2009+u20092 number, and so on until the _n_'th number. Thus, _a__i_u2009=u2009_b__i_u2009-u2009_b__i_u2009+u20091u2009+u2009_b__i_u2009+u20092u2009-u2009_b__i_u2009+u20093.... Memory gives you the values _a_1,u2009_a_2,u2009...,u2009_a__n_, and he now wants you to find the initial numbers _b_1,u2009_b_2,u2009...,u2009_b__n_ written in the row? Can you do it? Input The first line of the input contains a single integer _n_ (2u2009β€u2009_n_u2009β€u2009100u2009000)xa0β the number of integers written in the row. The next line contains _n_, the _i_'th of which is _a__i_ (u2009-u2009109u2009β€u2009_a__i_u2009β€u2009109)xa0β the value of the _i_'th number. Output Print _n_ integers corresponding to the sequence _b_1,u2009_b_2,u2009...,u2009_b__n_. It's guaranteed that the answer is unique and fits in 32-bit integer type. Note In the first sample test, the crows report the numbers 6,u2009-u20094, 8,u2009-u20092, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6u2009=u20092u2009-u20094u2009+u20096u2009-u20091u2009+u20093, and u2009-u20094u2009=u20094u2009-u20096u2009+u20091u2009-u20093. In the second sample test, the sequence 1, u2009-u20093, 4, 11, 6 satisfies the reports. For example, 5u2009=u200911u2009-u20096 and 6u2009=u20096. | 800 | true | false | true | false | false | false | false | false | false | false | 6,971 |
1733C | You are given an array $$$a$$$ with $$$n$$$ non-negative integers. You can apply the following operation on it. Choose two indices $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$). If $$$a_l + a_r$$$ is odd, do $$$a_r := a_l$$$. If $$$a_l + a_r$$$ is even, do $$$a_l := a_r$$$. Find any sequence of at most $$$n$$$ operations that makes $$$a$$$ non-decreasing. It can be proven that it is always possible. Note that you do not have to minimize the number of operations. An array $$$a_1, a_2, ldots, a_n$$$ is non-decreasing if and only if $$$a_1 le a_2 le ldots le a_n$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0β the number of test cases. Each test case consists of two lines. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$) xa0β the array itself. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, print one integer $$$m$$$ ($$$0 le m le n$$$), the number of operations, in the first line. Then print $$$m$$$ lines. Each line must contain two integers $$$l_i, r_i$$$, which are the indices you chose in the $$$i$$$-th operation ($$$1 le l_i < r_i le n$$$). If there are multiple solutions, print any of them. Example Input 3 2 7 8 5 1 1000000000 3 0 5 1 0 Note In the second test case, $$$a$$$ changes like this: Select indices $$$3$$$ and $$$4$$$. $$$a_3 + a_4 = 3$$$ is odd, so do $$$a_4 := a_3$$$. $$$a = [1, 1000000000, 3, 3, 5]$$$ now. Select indices $$$1$$$ and $$$2$$$. $$$a_1 + a_2 = 1000000001$$$ is odd, so do $$$a_2 := a_1$$$. $$$a = [1, 1, 3, 3, 5]$$$ now, and it is non-decreasing. In the first and third test cases, $$$a$$$ is already non-decreasing. | 1,300 | false | false | false | false | false | true | false | false | true | false | 1,890 |
1295B | You are given string $$$s$$$ of length $$$n$$$ consisting of 0-s and 1-s. You build an infinite string $$$t$$$ as a concatenation of an infinite number of strings $$$s$$$, or $$$t = ssss dots$$$ For example, if $$$s =$$$ 10010, then $$$t =$$$ 100101001010010... Calculate the number of prefixes of $$$t$$$ with balance equal to $$$x$$$. The balance of some string $$$q$$$ is equal to $$$cnt_{0, q} - cnt_{1, q}$$$, where $$$cnt_{0, q}$$$ is the number of occurrences of 0 in $$$q$$$, and $$$cnt_{1, q}$$$ is the number of occurrences of 1 in $$$q$$$. The number of such prefixes can be infinite; if it is so, you must say that. A prefix is a string consisting of several first letters of a given string, without any reorders. An empty prefix is also a valid prefix. For example, the string "abcd" has 5 prefixes: empty string, "a", "ab", "abc" and "abcd". Input The first line contains the single integer $$$T$$$ ($$$1 le T le 100$$$) β the number of test cases. Next $$$2T$$$ lines contain descriptions of test cases β two lines per test case. The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 10^5$$$, $$$-10^9 le x le 10^9$$$) β the length of string $$$s$$$ and the desired balance, respectively. The second line contains the binary string $$$s$$$ ($$$s = n$$$, $$$s_i in { ext{0}, ext{1}}$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$10^5$$$. Output Print $$$T$$$ integers β one per test case. For each test case print the number of prefixes or $$$-1$$$ if there is an infinite number of such prefixes. Example Input 4 6 10 010010 5 3 10101 1 0 0 2 0 01 Note In the first test case, there are 3 good prefixes of $$$t$$$: with length $$$28$$$, $$$30$$$ and $$$32$$$. | 1,700 | true | false | false | false | false | false | false | false | false | false | 4,222 |
706C | Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help. Vasiliy is given _n_ strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on). To reverse the _i_-th string Vasiliy has to spent _c__i_ units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order. String _A_ is lexicographically smaller than string _B_ if it is shorter than _B_ (_A_u2009<u2009_B_) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in _A_ is smaller than the character in _B_. For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically. Input The first line of the input contains a single integer _n_ (2u2009β€u2009_n_u2009β€u2009100u2009000)xa0β the number of strings. The second line contains _n_ integers _c__i_ (0u2009β€u2009_c__i_u2009β€u2009109), the _i_-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the _i_-th string. Then follow _n_ lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100u2009000. Output If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print u2009-u20091. Otherwise, print the minimum total amount of energy Vasiliy has to spent. Note In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller. In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is u2009-u20091. In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is u2009-u20091. | 1,600 | false | false | false | true | false | false | false | false | false | false | 6,997 |
1141G | Treeland consists of $$$n$$$ cities and $$$n-1$$$ roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right β the country's topology is an undirected tree. There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads. The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed $$$k$$$ and the number of companies taking part in the privatization is minimal. Choose the number of companies $$$r$$$ such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most $$$k$$$. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal $$$r$$$ that there is such assignment to companies from $$$1$$$ to $$$r$$$ that the number of cities which are not good doesn't exceed $$$k$$$. The picture illustrates the first example ($$$n=6, k=2$$$). The answer contains $$$r=2$$$ companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number $$$3$$$) is not good. The number of such vertices (just one) doesn't exceed $$$k=2$$$. It is impossible to have at most $$$k=2$$$ not good cities in case of one company. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 200000, 0 le k le n - 1$$$) β the number of cities and the maximal number of cities which can have two or more roads belonging to one company. The following $$$n-1$$$ lines contain roads, one road per line. Each line contains a pair of integers $$$x_i$$$, $$$y_i$$$ ($$$1 le x_i, y_i le n$$$), where $$$x_i$$$, $$$y_i$$$ are cities connected with the $$$i$$$-th road. Output In the first line print the required $$$r$$$ ($$$1 le r le n - 1$$$). In the second line print $$$n-1$$$ numbers $$$c_1, c_2, dots, c_{n-1}$$$ ($$$1 le c_i le r$$$), where $$$c_i$$$ is the company to own the $$$i$$$-th road. If there are multiple answers, print any of them. Examples Input 6 2 1 4 4 3 3 5 3 6 5 2 Input 10 2 10 3 1 2 1 3 1 4 2 5 2 6 2 7 3 8 3 9 Output 3 1 1 2 3 2 3 1 3 1 | 1,900 | false | true | false | false | false | true | false | true | false | true | 5,014 |
1970C3 | This is the hard version of the problem. The only difference in this version is the constraint on $$$t$$$. Ron and Hermione are playing a game on a tree of $$$n$$$ nodes that are initially inactive. The game consists of $$$t$$$ rounds, each of which starts with a stone on exactly one node, which is considered as activated. A move consists of picking an inactive neighbor of the node with a stone on it and moving the stone there (thus activating this neighbor). Ron makes the first move, after which he alternates with Hermione until no valid move is available. The player that cannot make a move loses the round. If both players play optimally, who wins each round of this game? Note that all the rounds are played with the same tree; only the starting node changes. Moreover, after each round, all active nodes are considered inactive again. Input The first line contains integers $$$n$$$ ($$$2 leq n leq 2 imes 10^5$$$), $$$t$$$ ($$$1 leq t leq n$$$), the number of nodes in the tree and the number of rounds, respectively. The next $$$n-1$$$ lines contain two integers $$$1 leq u, v leq n$$$ each, corresponding to an edge of the tree. The next line contains $$$t$$$ integers $$$1 leq u_1 , dots , u_t leq n$$$, corresponding to the node where the stone is initially put. Output The output consists of $$$t$$$ lines, each line being either "Ron" or "Hermione". Examples Input 6 3 1 2 2 3 1 4 4 5 4 6 1 4 6 Output Hermione Ron Hermione | 1,900 | false | false | false | true | false | false | false | false | false | false | 478 |
91C | A ski base is planned to be built in Walrusland. Recently, however, the project is still in the constructing phase. A large land lot was chosen for the construction. It contains _n_ ski junctions, numbered from 1 to _n_. Initially the junctions aren't connected in any way. In the constructing process _m_ bidirectional ski roads will be built. The roads are built one after another: first the road number 1 will be built, then the road number 2, and so on. The _i_-th road connects the junctions with numbers _a__i_ and _b__i_. Track is the route with the following properties: The route is closed, that is, it begins and ends in one and the same junction. The route contains at least one road. The route doesn't go on one road more than once, however it can visit any junction any number of times. Let's consider the ski base as a non-empty set of roads that can be divided into one or more tracks so that exactly one track went along each road of the chosen set. Besides, each track can consist only of roads from the chosen set. Ski base doesn't have to be connected. Two ski bases are considered different if they consist of different road sets. After building each new road the Walrusland government wants to know the number of variants of choosing a ski base based on some subset of the already built roads. The government asks you to help them solve the given problem. Input The first line contains two integers _n_ and _m_ (2u2009β€u2009_n_u2009β€u2009105,u20091u2009β€u2009_m_u2009β€u2009105). They represent the number of junctions and the number of roads correspondingly. Then on _m_ lines follows the description of the roads in the order in which they were built. Each road is described by a pair of integers _a__i_ and _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_,u2009_a__i_u2009β u2009_b__i_) β the numbers of the connected junctions. There could be more than one road between a pair of junctions. Output Print _m_ lines: the _i_-th line should represent the number of ways to build a ski base after the end of construction of the road number _i_. The numbers should be printed modulo 1000000009 (109u2009+u20099). Note Let us have 3 junctions and 4 roads between the junctions have already been built (as after building all the roads in the sample): 1 and 3, 2 and 3, 2 roads between junctions 1 and 2. The land lot for the construction will look like this: The land lot for the construction will look in the following way: We can choose a subset of roads in three ways: In the first and the second ways you can choose one path, for example, 1 - 2 - 3 - 1. In the first case you can choose one path 1 - 2 - 1. | 2,300 | false | false | false | false | false | false | false | false | false | true | 9,520 |
1876F | Pak Chanek has just bought an empty fish tank and he has been dreaming to fill it with his favourite kind of fish, clownfish. Pak Chanek likes clownfish because of their ability to change their genders on demand. Because of the size of his fish tank, Pak Chanek wants to buy exactly $$$k$$$ clownfish to fill the tank. Pak Chanek goes to the local fish shop. The shop provides $$$n$$$ clownfish numbered from $$$1$$$ to $$$n$$$, with clownfish $$$i$$$ having a size of $$$a_i$$$. Initially, every clownfish in the store does not have an assigned gender, but has the ability to be assigned to two possible clownfish genders, female or male. The store has a procedure which Pak Chanek should follow to buy clownfish. The shop owner will point at each clownfish sequentially from $$$1$$$ to $$$n$$$ and for each clownfish, she asks Pak Chanek whether to buy it or not. Each time Pak Chanek is asked, he must declare whether to buy the currently asked clownfish or not, before the shop owner moves on to the next clownfish. If Pak Chanek declares to buy the currently asked clownfish, he must also declare the gender to be assigned to that clownfish immediately. When assigning the gender for the currently asked clownfish, the following conditions must be satisfied: If Pak Chanek assigns it to be female and he has bought a female clownfish before, then the size of the current one must be exactly $$$1$$$ bigger than the last female one. If Pak Chanek assigns it to be male and he has bought a male clownfish before, then the size of the current one must be exactly $$$1$$$ smaller than the last male one. Pak Chanek wants to buy exactly $$$k$$$ clownfish such that: There is at least one female clownfish and one male clownfish. Among the $$$k$$$ clownfish Pak Chanek buys, the mean size of the female clownfish is equal to the mean size of the male clownfish. Let $$$l$$$ and $$$r$$$ respectively be the minimum and maximum index of a clownfish Pak Chanek buys. What is the minimum possible value of $$$r-l$$$? Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq k leq n leq 2cdot10^5$$$) β the number of clownfish in the shop and the number of clownfish Pak Chanek has to buy. The second line contains $$$n$$$ integers $$$a_1,a_2,a_3,ldots,a_n$$$ ($$$1leq a_ileq n$$$) β the size of each clownfish. Note In the first example, Pak Chanek can do the following: 1. Do not buy clownfish $$$1$$$. 2. Buy clownfish $$$2$$$ and assign it as male. 3. Buy clownfish $$$3$$$ and assign it as female. 4. Buy clownfish $$$4$$$ and assign it as male. 5. Buy clownfish $$$5$$$ and assign it as male. 6. Do not buy clownfish $$$6$$$. 7. Buy clownfish $$$7$$$ and assign it as male. 8. Buy clownfish $$$8$$$ and assign it as female. 9. Do not buy clownfish $$$9$$$. Then, we get that: The mean size among the $$$2$$$ female clownfish Pak Chanek buys is $$$frac{2+3}{2} = 2.5$$$. The mean size among the $$$4$$$ male clownfish Pak Chanek buys is $$$frac{4+3+2+1}{4} = 2.5$$$. $$$l=2$$$, $$$r=8$$$, $$$r-l=6$$$. There are no better strategies. | 3,500 | false | false | false | false | false | false | false | true | false | true | 1,021 |
1352A | A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from $$$1$$$ to $$$9$$$ (inclusive) are round. For example, the following numbers are round: $$$4000$$$, $$$1$$$, $$$9$$$, $$$800$$$, $$$90$$$. The following numbers are not round: $$$110$$$, $$$707$$$, $$$222$$$, $$$1001$$$. You are given a positive integer $$$n$$$ ($$$1 le n le 10^4$$$). Represent the number $$$n$$$ as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number $$$n$$$ as a sum of the least number of terms, each of which is a round number. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing an integer $$$n$$$ ($$$1 le n le 10^4$$$). Output Print $$$t$$$ answers to the test cases. Each answer must begin with an integer $$$k$$$ β the minimum number of summands. Next, $$$k$$$ terms must follow, each of which is a round number, and their sum is $$$n$$$. The terms can be printed in any order. If there are several answers, print any of them. Example Input 5 5009 7 9876 10000 10 Output 2 5000 9 1 7 4 800 70 6 9000 1 10000 1 10 | 800 | true | false | true | false | false | false | false | false | false | false | 3,952 |
1808B | Galaxy Luck, a well-known casino in the entire solar system, introduces a new card game. In this game, there is a deck that consists of $$$n$$$ cards. Each card has $$$m$$$ numbers written on it. Each of the $$$n$$$ players receives exactly one card from the deck. Then all players play with each other in pairs, and each pair of players plays exactly once. Thus, if there are, for example, four players in total, then six games are played: the first against the second, the first against the third, the first against the fourth, the second against the third, the second against the fourth and the third against the fourth. Each of these games determines the winner in some way, but the rules are quite complicated, so we will not describe them here. All that matters is how many chips are paid out to the winner. Let the first player's card have the numbers $$$a_1, a_2, dots, a_m$$$, and the second player's cardxa0β $$$b_1, b_2, dots, b_m$$$. Then the winner of the game gets $$$a_1 - b_1 + a_2 - b_2 + dots + a_m - b_m$$$ chips from the total pot, where $$$x$$$ denotes the absolute value of $$$x$$$. To determine the size of the total pot, it is necessary to calculate the winners' total winnings for all games. Since there can be many cards in a deck and many players, you have been assigned to write a program that does all the necessary calculations. Input Each test consists of several test cases. The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n cdot m le 3cdot 10^5$$$)xa0β the number of cards in the deck and the count of numbers on the one card. Each of the following $$$n$$$ lines of the test case set contains $$$m$$$ integers $$$c_{i,j}$$$ ($$$1 le c_{i,j} le 10^6$$$)xa0β a description of the $$$i$$$-th card. It is guaranteed that the total $$$n cdot m$$$ in all tests does not exceed $$$3 cdot 10^5$$$. Note Consider the first test case. In the game between the first and second player, the winner receives $$$1-7 + 4-9 + 2-2 + 8-1 + 5-4 = 19$$$ chips. In the game between the first and third player, the winner receives $$$1-3 + 4-8 + 2-5 + 8-3 + 5-1 = 18$$$ in chips. In the game between the second and third player, the winner receives $$$7-3 + 9-8 + 2-5 + 1-3 + 4-1 = 13$$$ chips. The total is $$$19 + 18 + 13 = 50$$$ chips. | 1,200 | true | false | false | false | false | false | false | false | true | false | 1,426 |
1146E | You are given an array of $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$. You will perform $$$q$$$ operations. In the $$$i$$$-th operation, you have a symbol $$$s_i$$$ which is either "<" or ">" and a number $$$x_i$$$. You make a new array $$$b$$$ such that $$$b_j = -a_j$$$ if $$$a_j s_i x_i$$$ and $$$b_j = a_j$$$ otherwise (i.e. if $$$s_i$$$ is '>', then all $$$a_j > x_i$$$ will be flipped). After doing all these replacements, $$$a$$$ is set to be $$$b$$$. You want to know what your final array looks like after all operations. Input The first line contains two integers $$$n,q$$$ ($$$1 leq n,q leq 10^5$$$)xa0β the number of integers and the number of queries. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^5 leq a_i leq 10^5$$$)xa0β the numbers. Each of the next $$$q$$$ lines contains a character and an integer $$$s_i, x_i$$$. ($$$s_i in {<, >}, -10^5 leq x_i leq 10^5$$$)xa0β the queries. Output Print $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ representing the array after all operations. Examples Input 11 3 -5 -4 -3 -2 -1 0 1 2 3 4 5 > 2 > -4 < 5 Output 5 4 -3 -2 -1 0 1 2 -3 4 5 Input 5 5 0 1 -2 -1 2 < -2 < -1 < 0 < 1 < 2 Note In the first example, the array goes through the following changes: Initial: $$$[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]$$$ $$$> 2$$$: $$$[-5, -4, -3, -2, -1, 0, 1, 2, -3, -4, -5]$$$ $$$> -4$$$: $$$[-5, -4, 3, 2, 1, 0, -1, -2, 3, -4, -5]$$$ $$$< 5$$$: $$$[5, 4, -3, -2, -1, 0, 1, 2, -3, 4, 5]$$$ | 2,400 | false | false | true | false | true | false | false | false | false | false | 4,987 |
288C | Problem - 288C - 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 math *1700 No tag edit access β Contest materials . Output In the first line print integer _m_ the maximum possible beauty. In the second line print any permutation of integers from 0 to _n_ with the beauty equal to _m_. If there are several suitable permutations, you are allowed to print any of them. Examples Input 4 Output 20 0 2 1 4 3 | 1,700 | true | false | true | false | false | false | false | false | false | false | 8,682 |
1766B | You want to type the string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters, using your favorite text editor Notepad#. Notepad# supports two kinds of operations: append any letter to the end of the string; copy a continuous substring of an already typed string and paste this substring to the end of the string. Can you type string $$$s$$$ in strictly less than $$$n$$$ operations? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the length of the string $$$s$$$. The second line contains a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters. The sum of $$$n$$$ doesn't exceed $$$2 cdot 10^5$$$ over all testcases. Output For each testcase, print "YES" if you can type string $$$s$$$ in strictly less than $$$n$$$ operations. Otherwise, print "NO". Example Input 6 10 codeforces 8 labacaba 5 uohhh 16 isthissuffixtree 1 x 4 momo Output NO YES NO YES NO YES Note In the first testcase, you can start with typing "codef" ($$$5$$$ operations), then copy "o" ($$$1$$$ operation) from an already typed part, then finish with typing "rces" ($$$4$$$ operations). That will be $$$10$$$ operations, which is not strictly less than $$$n$$$. There exist other ways to type "codeforces". However, no matter what you do, you can't do less than $$$n$$$ operations. In the second testcase, you can type "labac" ($$$5$$$ operations), then copy "aba" ($$$1$$$ operation), finishing the string in $$$6$$$ operations. | 1,000 | false | false | true | false | false | false | false | false | false | false | 1,702 |
1650C | On the number line there are $$$m$$$ points, $$$i$$$-th of which has integer coordinate $$$x_i$$$ and integer weight $$$w_i$$$. The coordinates of all points are different, and the points are numbered from $$$1$$$ to $$$m$$$. A sequence of $$$n$$$ segments $$$[l_1, r_1], [l_2, r_2], dots, [l_n, r_n]$$$ is called system of nested segments if for each pair $$$i, j$$$ ($$$1 le i < j le n$$$) the condition $$$l_i < l_j < r_j < r_i$$$ is satisfied. In other words, the second segment is strictly inside the first one, the third segment is strictly inside the second one, and so on. For a given number $$$n$$$, find a system of nested segments such that: both ends of each segment are one of $$$m$$$ given points; the sum of the weights $$$2cdot n$$$ of the points used as ends of the segments is minimal. For example, let $$$m = 8$$$. The given points are marked in the picture, their weights are marked in red, their coordinates are marked in blue. Make a system of three nested segments: weight of the first segment: $$$1 + 1 = 2$$$ weight of the second segment: $$$10 + (-1) = 9$$$ weight of the third segment: $$$3 + (-2) = 1$$$ sum of the weights of all the segments in the system: $$$2 + 9 + 1 = 12$$$ System of three nested segments Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0βthe number of input test cases. An empty line is written before each test case. The first line of each test case contains two positive integers $$$n$$$ ($$$1 le n le 10^5$$$) and $$$m$$$ ($$$2 cdot n le m le 2 cdot 10^5$$$). The next $$$m$$$ lines contain pairs of integers $$$x_i$$$ ($$$-10^9 le x_i le 10^9$$$) and $$$w_i$$$ ($$$-10^4 le w_i le 10^4$$$) β coordinate and weight of point number $$$i$$$ ($$$1 le i le m$$$) respectively. All $$$x_i$$$ are different. It is guaranteed that the sum of $$$m$$$ values over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$n + 1$$$ lines: in the first of them, output the weight of the composed system, and in the next $$$n$$$ lines output exactly two numbers xa0β the indices of the points which are the endpoints of the $$$i$$$-th segment ($$$1 le i le n$$$). The order in which you output the endpoints of a segment is not important β you can output the index of the left endpoint first and then the number of the right endpoint, or the other way around. If there are several ways to make a system of nested segments with minimal weight, output any of them. Example Input 3 3 8 0 10 -2 1 4 10 11 20 7 -1 9 1 2 3 5 -2 3 6 -1 2 1 3 3 -1 2 4 4 0 8 2 2 5 5 -1 3 -2 1 0 -2 0 -5 -3 Output 12 2 6 5 1 7 8 10 1 6 5 2 3 4 -6 5 1 4 2 Note The first test case coincides with the example from the condition. It can be shown that the weight of the composed system is minimal. The second test case has only $$$6$$$ points, so you need to use each of them to compose $$$3$$$ segments. | 1,200 | false | true | true | false | false | false | false | false | true | false | 2,373 |
1931D | Polycarp has two favorite integers $$$x$$$ and $$$y$$$ (they can be equal), and he has found an array $$$a$$$ of length $$$n$$$. Polycarp considers a pair of indices $$$langle i, j angle$$$ ($$$1 le i < j le n$$$) beautiful if: $$$a_i + a_j$$$ is divisible by $$$x$$$; $$$a_i - a_j$$$ is divisible by $$$y$$$. For example, if $$$x=5$$$, $$$y=2$$$, $$$n=6$$$, $$$a=$$$[$$$1, 2, 7, 4, 9, 6$$$], then the only beautiful pairs are: $$$langle 1, 5 angle$$$: $$$a_1 + a_5 = 1 + 9 = 10$$$ ($$$10$$$ is divisible by $$$5$$$) and $$$a_1 - a_5 = 1 - 9 = -8$$$ ($$$-8$$$ is divisible by $$$2$$$); $$$langle 4, 6 angle$$$: $$$a_4 + a_6 = 4 + 6 = 10$$$ ($$$10$$$ is divisible by $$$5$$$) and $$$a_4 - a_6 = 4 - 6 = -2$$$ ($$$-2$$$ is divisible by $$$2$$$). Find the number of beautiful pairs in the array $$$a$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains three integers $$$n$$$, $$$x$$$, and $$$y$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le x, y le 10^9$$$)xa0β the size of the array and Polycarp's favorite integers. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β the elements of the array. 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 a single integerxa0β the number of beautiful pairs in the array $$$a$$$. Example Input 7 6 5 2 1 2 7 4 9 6 7 9 5 1 10 15 3 8 12 15 9 4 10 14 10 2 2 11 11 13 5 6 9 5 6 10 7 6 7 9 7 7 10 10 9 6 2 4 9 7 1 2 2 13 3 15 9 2 3 14 6 1 15 12 15 8 2 15 10 5 7 13 3 3 2 12 11 3 7 13 14 | 1,300 | true | false | false | false | false | false | false | false | false | false | 698 |
38D | Once Vasya played bricks. All the bricks in the set had regular cubical shape. Vasya vas a talented architect, however the tower he built kept falling apart. Let us consider the building process. Vasya takes a brick and puts it on top of the already built tower so that the sides of the brick are parallel to the sides of the bricks he has already used. Let's introduce a Cartesian coordinate system on the horizontal plane, where Vasya puts the first brick. Then the projection of brick number _i_ on the plane is a square with sides parallel to the axes of coordinates with opposite corners in points (_x__i_,u20091,u2009_y__i_,u20091) and (_x__i_,u20092,u2009_y__i_,u20092). The bricks are cast from homogeneous plastic and the weight of a brick _a_u2009Γu2009_a_u2009Γu2009_a_ is _a_3 grams. It is guaranteed that Vasya puts any brick except the first one on the previous one, that is the area of intersection of the upper side of the previous brick and the lower side of the next brick is always positive. We (Vasya included) live in a normal world where the laws of physical statics work. And that is why, perhaps, if we put yet another brick, the tower will collapse under its own weight. Vasya puts the cubes consecutively one on top of the other until at least one cube loses the balance and falls down. If it happens, Vasya gets upset and stops the construction. Print the number of bricks in the maximal stable tower, that is the maximal number _m_ satisfying the condition that all the towers consisting of bricks 1, 2, ..., _k_ for every integer _k_ from 1 to _m_ remain stable. Input The first input file contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009100) which is the number of bricks. Each of the next _n_ lines contains four numbers _x__i_,u20091,u2009_y__i_,u20091,u2009_x__i_,u20092,u2009_y__i_,u20092 (_x__i_,u20091u2009β u2009_x__i_,u20092,u2009_x__i_,u20091u2009-u2009_x__i_,u20092u2009=u2009_y__i_,u20091u2009-u2009_y__i_,u20092) which are the coordinates of the opposite angles of the base of the brick number _i_. The coordinates are integers and their absolute value does not exceed 50. The cubes are given in the order Vasya puts them. It is guaranteed that the area of intersection of the upper side of the brick number _i_u2009-u20091 and the lower side of the brick number _i_ is strictly strictly greater than zero for all _i_u2009β₯u20092. | 1,900 | false | false | true | false | false | false | false | false | false | false | 9,805 |
1325B | Ehab has an array $$$a$$$ of length $$$n$$$. He has just enough free time to make a new array consisting of $$$n$$$ copies of the old array, written back-to-back. What will be the length of the new array's longest increasing subsequence? A sequence $$$a$$$ is a subsequence of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements. The longest increasing subsequence of an array is the longest subsequence such that its elements are ordered in strictly increasing order. Input The first line contains an integer $$$t$$$xa0β the number of test cases you need to solve. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$ldots$$$, $$$a_{n}$$$ ($$$1 le a_i le 10^9$$$)xa0β the elements of the array $$$a$$$. The sum of $$$n$$$ across the test cases doesn't exceed $$$10^5$$$. Output For each testcase, output the length of the longest increasing subsequence of $$$a$$$ if you concatenate it to itself $$$n$$$ times. Example Input 2 3 3 2 1 6 3 1 4 1 5 9 Note In the first sample, the new array is $$$[3,2, extbf{1},3, extbf{2},1, extbf{3},2,1]$$$. The longest increasing subsequence is marked in bold. In the second sample, the longest increasing subsequence will be $$$[1,3,4,5,9]$$$. | 800 | false | true | true | false | false | false | false | false | false | false | 4,092 |
126B | Problem - 126B - 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 dp hashing string suffix structures strings *1700 No tag edit access β Contest materials . When Asterix read the substring _t_ aloud, the temple doors opened. You know the string _s_. Find the substring _t_ or determine that such substring does not exist and all that's been written above is just a nice legend. Input You are given the string _s_ whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters. Output Print the string _t_. If a suitable _t_ string does not exist, then print "Just a legend" without the quotes. Examples Input fixprefixsuffix Output fix Input abcdabc Output Just a legend | 1,700 | false | false | false | true | false | false | false | true | false | false | 9,376 |
685C | When the river brought Gerda to the house of the Old Lady who Knew Magic, this lady decided to make Gerda her daughter. She wants Gerda to forget about Kay, so she puts all the roses from the garden underground. Mole, who lives in this garden, now can watch the roses without going up to the surface. Typical mole is blind, but this mole was granted as special vision by the Old Lady. He can watch any underground objects on any distance, even through the obstacles and other objects. However, the quality of the picture depends on the Manhattan distance to object being observed. Mole wants to find an optimal point to watch roses, that is such point with integer coordinates that the maximum Manhattan distance to the rose is minimum possible. As usual, he asks you to help. Manhattan distance between points (_x_1,u2009xa0_y_1,u2009xa0_z_1) and (_x_2,u2009xa0_y_2,u2009xa0_z_2) is defined as _x_1u2009-u2009_x_2u2009+u2009_y_1u2009-u2009_y_2u2009+u2009_z_1u2009-u2009_z_2. Input The first line of the input contains an integer _t_ _t_ (1u2009β€u2009_t_u2009β€u2009100u2009000)xa0β the number of test cases. Then follow exactly _t_ blocks, each containing the description of exactly one test. The first line of each block contains an integer _n__i_ (1u2009β€u2009_n__i_u2009β€u2009100u2009000)xa0β the number of roses in the test. Then follow _n__i_ lines, containing three integers eachxa0β the coordinates of the corresponding rose. Note that two or more roses may share the same position. It's guaranteed that the sum of all _n__i_ doesn't exceed 100u2009000 and all coordinates are not greater than 1018 by their absolute value. Output For each of _t_ test cases print three integersxa0β the coordinates of the optimal point to watch roses. If there are many optimal answers, print any of them. The coordinates of the optimal point may coincide with the coordinates of any rose. Note In the first sample, the maximum Manhattan distance from the point to the rose is equal to 4. In the second sample, the maximum possible distance is 0. Note that the positions of the roses may coincide with each other and with the position of the optimal point. | 2,900 | true | false | false | false | false | false | false | true | false | false | 7,081 |
217A | Problem - 217A - 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 dfs and similar dsu graphs *1200 No tag edit access β Contest materials β the number of snow drifts. Each of the following _n_ lines contains two integers _x__i_ and _y__i_ (1u2009β€u2009_x__i_,u2009_y__i_u2009β€u20091000) β the coordinates of the _i_-th snow drift. Note that the north direction coinΡides with the direction of _Oy_ axis, so the east direction coinΡides with the direction of the _Ox_ axis. All snow drift's locations are distinct. Output Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one. Examples Input 2 2 1 1 2 Output 1 Input 2 2 1 4 1 Output 0 | 1,200 | false | false | false | false | false | false | true | false | false | true | 8,971 |
586B | A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sausage and cheese sandwiches, but first, he needs to buy a sausage and some cheese. The town where Laurenty lives in is not large. The houses in it are located in two rows, _n_ houses in each row. Laurenty lives in the very last house of the second row. The only shop in town is placed in the first house of the first row. The first and second rows are separated with the main avenue of the city. The adjacent houses of one row are separated by streets. Each crosswalk of a street or an avenue has some traffic lights. In order to cross the street, you need to press a button on the traffic light, wait for a while for the green light and cross the street. Different traffic lights can have different waiting time. The traffic light on the crosswalk from the _j_-th house of the _i_-th row to the (_j_u2009+u20091)-th house of the same row has waiting time equal to _a__ij_ (1u2009β€u2009_i_u2009β€u20092,u20091u2009β€u2009_j_u2009β€u2009_n_u2009-u20091). For the traffic light on the crossing from the _j_-th house of one row to the _j_-th house of another row the waiting time equals _b__j_ (1u2009β€u2009_j_u2009β€u2009_n_). The city doesn't have any other crossings. The boy wants to get to the store, buy the products and go back. The main avenue of the city is wide enough, so the boy wants to cross it exactly once on the way to the store and exactly once on the way back home. The boy would get bored if he had to walk the same way again, so he wants the way home to be different from the way to the store in at least one crossing. Figure to the first sample. Help Laurenty determine the minimum total time he needs to wait at the crossroads. Input The first line of the input contains integer _n_ (2u2009β€u2009_n_u2009β€u200950) β the number of houses in each row. Each of the next two lines contains _n_u2009-u20091 space-separated integer β values _a__ij_ (1u2009β€u2009_a__ij_u2009β€u2009100). The last line contains _n_ space-separated integers _b__j_ (1u2009β€u2009_b__j_u2009β€u2009100). Note The first sample is shown on the figure above. In the second sample, Laurenty's path can look as follows: Laurenty crosses the avenue, the waiting time is 3; Laurenty uses the second crossing in the first row, the waiting time is 2; Laurenty uses the first crossing in the first row, the waiting time is 1; Laurenty uses the first crossing in the first row, the waiting time is 1; Laurenty crosses the avenue, the waiting time is 1; Laurenty uses the second crossing in the second row, the waiting time is 3. In total we get that the answer equals 11. In the last sample Laurenty visits all the crossings, so the answer is 4. | 1,300 | false | false | true | false | false | false | false | false | false | false | 7,498 |
1702G2 | This is a hard version of the problem. The only difference between an easy and a hard version is in the number of queries. Polycarp grew a tree from $$$n$$$ vertices. We remind you that a tree of $$$n$$$ vertices is an undirected connected graph of $$$n$$$ vertices and $$$n-1$$$ edges that does not contain cycles. He calls a set of vertices passable if there is such a path in the tree that passes through each vertex of this set without passing through any edge twice. The path can visit other vertices (not from this set). In other words, a set of vertices is called passable if there is a simple path that passes through all the vertices of this set (and possibly some other). For example, for a tree below sets $$${3, 2, 5}$$$, $$${1, 5, 4}$$$, $$${1, 4}$$$ are passable, and $$${1, 3, 5}$$$, $$${1, 2, 3, 4, 5}$$$ are not. Polycarp asks you to answer $$$q$$$ queries. Each query is a set of vertices. For each query, you need to determine whether the corresponding set of vertices is passable. Input The first line of input contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β number of vertices. Following $$$n - 1$$$ lines a description of the tree.. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$) β indices of vertices connected by an edge. Following line contains single integer $$$q$$$ ($$$1 le q le 10^5$$$)xa0β number of queries. The following $$$2 cdot q$$$ lines contain descriptions of sets. The first line of the description contains an integer $$$k$$$ ($$$1 le k le n$$$) β the size of the set. The second line of the description contains $$$k$$$ of distinct integers $$$p_1, p_2, dots, p_k$$$ ($$$1 le p_i le n$$$) β indices of the vertices of the set. It is guaranteed that the sum of $$$k$$$ values for all queries does not exceed $$$2 cdot 10^5$$$. Output Output $$$q$$$ lines, each of which contains the answer to the corresponding query. As an answer, output "YES" if the set is passable, 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). Examples Input 5 1 2 2 3 2 4 4 5 5 3 3 2 5 5 1 2 3 4 5 2 1 4 3 1 3 5 3 1 5 4 Input 5 1 2 3 2 2 4 5 2 4 2 3 1 3 3 4 5 3 2 3 5 1 1 | 2,000 | false | false | false | false | true | false | false | false | false | false | 2,063 |
1856E2 | This is the hard version of the problem. The differences between the two versions are the constraint on $$$n$$$ and the time limit. You can make hacks only if both versions of the problem are solved. You are given a tree with $$$n$$$ vertices rooted at vertex $$$1$$$. For some permutation$$$^dagger$$$ $$$a$$$ of length $$$n$$$, let $$$f(a)$$$ be the number of pairs of vertices $$$(u, v)$$$ such that $$$a_u < a_{operatorname{lca}(u, v)} < a_v$$$. Here, $$$operatorname{lca}(u,v)$$$ denotes the $$$ over all permutations $$$a$$$ of length $$$n$$$. $$$^dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^6$$$). The second line contains $$$n - 1$$$ integers $$$p_2,p_3,ldots,p_n$$$ ($$$1 le p_i < i$$$) indicating that there is an edge between vertices $$$i$$$ and $$$p_i$$$. Note The tree in the first test: One possible optimal permutation $$$a$$$ is $$$[2, 1, 4, 5, 3]$$$ with $$$4$$$ suitable pairs of vertices: $$$(2, 3)$$$, since $$$operatorname{lca}(2, 3) = 1$$$ and $$$1 < 2 < 4$$$, $$$(2, 4)$$$, since $$$operatorname{lca}(2, 4) = 1$$$ and $$$1 < 2 < 5$$$, $$$(2, 5)$$$, since $$$operatorname{lca}(2, 5) = 1$$$ and $$$1 < 2 < 3$$$, $$$(5, 4)$$$, since $$$operatorname{lca}(5, 4) = 3$$$ and $$$3 < 4 < 5$$$. The tree in the third test: The tree in the fourth test: | 2,700 | true | true | true | true | false | false | false | false | false | false | 1,145 |
2018E1 | This is the easy version of the problem. In this version, the constraints on $$$n$$$ and the time limit are lower. You can make hacks only if both versions of the problem are solved. A set of (closed) segments is complex if it can be partitioned into some subsets such that all the subsets have the same size; and a pair of segments intersects if and only if the two segments are in the same subset. You are given $$$n$$$ segments $$$[l_1, r_1], [l_2, r_2], ldots, [l_n, r_n]$$$. Find the maximum size of a complex subset of these segments. 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 a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^4$$$)xa0β the number of segments. The second line of each test case contains $$$n$$$ integers $$$l_1, l_2, ldots, l_n$$$ ($$$1 le l_i le 2n$$$)xa0β the left endpoints of the segments. The third line of each test case contains $$$n$$$ integers $$$r_1, r_2, ldots, r_n$$$ ($$$l_i leq r_i le 2n$$$)xa0β the right endpoints of the segments. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^4$$$. Output For each test case, output a single integer: the maximum size of a complex subset of the given segments. Example Input 3 3 1 2 3 5 4 6 5 1 2 3 6 8 5 4 7 9 10 5 3 1 4 1 5 7 2 6 5 10 Note In the first test case, all pairs of segments intersect, therefore it is optimal to form a single group containing all of the three segments. In the second test case, there is no valid partition for all of the five segments. A valid partition with four segments is the following: $$${{ [1, 5], [2, 4] }, { [6, 9], [8, 10] }}$$$. In the third test case, it is optimal to make a single group containing all the segments except the second. | 3,300 | true | true | false | false | true | false | false | true | true | false | 159 |
1634D | This is an interactive problem. We picked an array of whole numbers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$) and concealed exactly one zero in it! Your goal is to find the location of this zero, that is, to find $$$i$$$ such that $$$a_i = 0$$$. You are allowed to make several queries to guess the answer. For each query, you can think up three distinct indices $$$i, j, k$$$, and we will tell you the value of $$$max(a_i, a_j, a_k) - min(a_i, a_j, a_k)$$$. In other words, we will tell you the difference between the maximum and the minimum number among $$$a_i$$$, $$$a_j$$$ and $$$a_k$$$. You are allowed to make no more than $$$2 cdot n - 2$$$ queries, and after that you have two tries to guess where the zero is. That is, you have to tell us two numbers $$$i$$$ and $$$j$$$ and you win if $$$a_i = 0$$$ or $$$a_j = 0$$$. Can you guess where we hid the zero? Note that the array in each test case is fixed beforehand and will not change during the game. In other words, the interactor is not adaptive. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). Description of the test cases follows. The first and only line of each test case contains an integer $$$n$$$ ($$$4 le n le 1000$$$)xa0β the length of the array that we picked. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. Interaction For each test case, the interaction starts with reading $$$n$$$. To make a query, print "? $$$i$$$ $$$j$$$ $$$k$$$" (without quotes, $$$1 le i, j, k le n$$$, indices must be distinct). Then you should read our response from standard input, that is, $$$max(a_i, a_j, a_k) - min(a_i, a_j, a_k)$$$. If the response is $$$-1$$$, it means your program has made an invalid query or has run out of tries. Your program must terminate immediately after reading $$$-1$$$, and you will get a verdict Wrong answer. Otherwise you may get any verdict, because the program will continue reading from the closed stream. Note that if the query is correct, the answer will never be $$$-1$$$ because $$$max(a_i, a_j, a_k) - min(a_i, a_j, a_k) geq 0$$$. To give the final answer, print "! $$$i$$$ $$$j$$$" (without the quotes). Printing the same number twice (that is, $$$i = j$$$) is allowed. Note that giving this answer is not counted towards the limit of $$$2 cdot n - 2$$$ queries. After that, your program must continue to solve the remaining test cases, or exit if all test cases have been solved. After printing a query, don't forget to output line feed and flush the output buffer. Otherwise you will get the verdict Idleness limit exceeded. To flush the buffer, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; Read documentation for other languages. Hacks The first line must contain an integer $$$t$$$ ($$$1 le t le 500$$$)xa0β the count of test cases. The first line of each test case must contain an integer $$$n$$$ ($$$4 le n le 1000$$$)xa0β the length of the hidden array. The second line of each test case must contain $$$n$$$ integers separated by spacesxa0β $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$). There must also be exactly one zero in this array. The sum of $$$n$$$ over all test cases must not exceed $$$3000$$$. | 2,000 | true | false | false | false | false | true | false | false | false | false | 2,443 |
729E | There are _n_ workers in a company, each of them has a unique id from 1 to _n_. Exaclty one of them is a chief, his id is _s_. Each worker except the chief has exactly one immediate superior. There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself. Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake. Input The first line contains two positive integers _n_ and _s_ (1u2009β€u2009_n_u2009β€u20092Β·105, 1u2009β€u2009_s_u2009β€u2009_n_)xa0β the number of workers and the id of the chief. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009β€u2009_n_u2009-u20091), where _a__i_ is the number of superiors (not only immediate) the worker with id _i_ reported about. Output Print the minimum number of workers that could make a mistake. Note In the first example it is possible that only the first worker made a mistake. Then: the immediate superior of the first worker is the second worker, the immediate superior of the third worker is the first worker, the second worker is the chief. | 1,900 | false | true | false | false | true | true | false | false | true | true | 6,890 |
1155C | Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the $$$i$$$-th of them will start during the $$$x_i$$$-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes $$$x_1, x_2, dots, x_n$$$, so he will be awake during each of these minutes (note that it does not matter if his alarm clock will ring during any other minute). Ivan can choose two properties for the alarm clock β the first minute it will ring (let's denote it as $$$y$$$) and the interval between two consecutive signals (let's denote it by $$$p$$$). After the clock is set, it will ring during minutes $$$y, y + p, y + 2p, y + 3p$$$ and so on. Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of $$$p$$$. He has to pick it among the given values $$$p_1, p_2, dots, p_m$$$ (his phone does not support any other options for this setting). So Ivan has to choose the first minute $$$y$$$ when the alarm clock should start ringing and the interval between two consecutive signals $$$p_j$$$ in such a way that it will ring during all given minutes $$$x_1, x_2, dots, x_n$$$ (and it does not matter if his alarm clock will ring in any other minutes). Your task is to tell the first minute $$$y$$$ and the index $$$j$$$ such that if Ivan sets his alarm clock with properties $$$y$$$ and $$$p_j$$$ it will ring during all given minutes $$$x_1, x_2, dots, x_n$$$ or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 3 cdot 10^5, 1 le m le 3 cdot 10^5$$$) β the number of events and the number of possible settings for the interval between signals. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 le x_i le 10^{18}$$$), where $$$x_i$$$ is the minute when $$$i$$$-th event starts. It is guaranteed that all $$$x_i$$$ are given in increasing order (i.u2009e. the condition $$$x_1 < x_2 < dots < x_n$$$ holds). The third line of the input contains $$$m$$$ integers $$$p_1, p_2, dots, p_m$$$ ($$$1 le p_j le 10^{18}$$$), where $$$p_j$$$ is the $$$j$$$-th option for the interval between two consecutive signals. Output If it's impossible to choose such values $$$y$$$ and $$$j$$$ so all constraints are satisfied, print "NO" in the first line. Otherwise print "YES" in the first line. Then print two integers $$$y$$$ ($$$1 le y le 10^{18}$$$) and $$$j$$$ ($$$1 le j le m$$$) in the second line, where $$$y$$$ is the first minute Ivan's alarm clock should start ringing and $$$j$$$ is the index of the option for the interval between two consecutive signals (options are numbered from $$$1$$$ to $$$m$$$ in the order they are given input). These values should be chosen in such a way that the alarm clock will ring during all given minutes $$$x_1, x_2, dots, x_n$$$. If there are multiple answers, you can print any. | 1,300 | true | false | false | false | false | false | false | false | false | false | 4,934 |
1852C | To prepare her "Takodachi" dumbo octopuses for world domination, Ninomae Ina'nis, a.k.a. Ina of the Mountain, orders Hoshimachi Suisei to throw boulders at them. Ina asks you, Kiryu Coco, to help choose where the boulders are thrown. There are $$$n$$$ octopuses on a single-file trail on Ina's mountain, numbered $$$1, 2, ldots, n$$$. The $$$i$$$-th octopus has a certain initial health value $$$a_i$$$, where $$$1 leq a_i leq k$$$. Each boulder crushes consecutive octopuses with indexes $$$l, l+1, ldots, r$$$, where $$$1 leq l leq r leq n$$$. You can choose the numbers $$$l$$$ and $$$r$$$ arbitrarily for each boulder. For each boulder, the health value of each octopus the boulder crushes is reduced by $$$1$$$. However, as octopuses are immortal, once they reach a health value of $$$0$$$, they will immediately regenerate to a health value of $$$k$$$. Given the octopuses' initial health values, find the minimum number of boulders that need to be thrown to make the health of all octopuses equal to $$$k$$$. 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. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le k le 10^9$$$)xa0β the number of octopuses, and the upper bound of a octopus's health value. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le k$$$)xa0β the initial health values of the octopuses. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, the minimum number of boulders thrown is $$$2$$$: Throw the first boulder between $$$[l,r] = [1,3]$$$. Then, the octopuses' health values become $$$[3, 1, 3, 3]$$$. Throw the second boulder between $$$[l,r] = [2,2]$$$. Then, the octopuses' health values become $$$[3, 3, 3, 3]$$$. In the second test case, the minimum number of boulders thrown is $$$4$$$. The $$$[l,r]$$$ ranges are $$$[1,7], [2, 6], [3, 5], [4, 4]$$$. | 2,400 | true | true | false | true | true | false | false | false | false | false | 1,165 |
1508A | A bitstring is a string that contains only the characters 0 and 1. Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length $$$2n$$$. A valid novel for the contest is a bitstring of length at most $$$3n$$$ that contains at least two of the three given strings as subsequences. Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest. A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) characters. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). Each of the following three lines contains a bitstring of length $$$2n$$$. It is guaranteed that these three strings are pairwise distinct. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$. Output For each test case, print a single line containing a bitstring of length at most $$$3n$$$ that has at least two of the given bitstrings as subsequences. It can be proven that under the constraints of the problem, such a bitstring always exists. If there are multiple possible answers, you may output any of them. Example Input 2 1 00 11 01 3 011001 111010 010001 Note In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required. In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010. | 1,900 | false | true | true | false | false | true | false | false | false | false | 3,136 |
1918D | You are given an array of numbers $$$a_1, a_2, ldots, a_n$$$. Your task is to block some elements of the array in order to minimize its cost. Suppose you block the elements with indices $$$1 leq b_1 < b_2 < ldots < b_m leq n$$$. Then the cost of the array is calculated as the maximum of: the sum of the blocked elements, i.e., $$$a_{b_1} + a_{b_2} + ldots + a_{b_m}$$$. the maximum sum of the segments into which the array is divided when the blocked elements are removed. That is, the maximum sum of the following ($$$m + 1$$$) subarrays: [$$$1, b_1 β 1$$$], [$$$b_1 + 1, b_2 β 1$$$], [$$$ldots$$$], [$$$b_{mβ1} + 1, b_m - 1$$$], [$$$b_m + 1, n$$$] (the sum of numbers in a subarray of the form [$$$x,x β 1$$$] is considered to be $$$0$$$). For example, if $$$n = 6$$$, the original array is [$$$1, 4, 5, 3, 3, 2$$$], and you block the elements at positions $$$2$$$ and $$$5$$$, then the cost of the array will be the maximum of the sum of the blocked elements ($$$4 + 3 = 7$$$) and the sums of the subarrays ($$$1$$$, $$$5 + 3 = 8$$$, $$$2$$$), which is $$$max(7,1,8,2) = 8$$$. You need to output the minimum cost of the array after blocking. Input The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 30,000$$$)xa0β the number of queries. Each test case consists of two lines. The first line contains an integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0β the length of the array $$$a$$$. The second line contains $$$n$$$ elements $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0β the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single numberxa0β the minimum cost of blocking the array. Example Input 3 6 1 4 5 3 3 2 5 1 2 3 4 5 6 4 1 6 3 10 7 Note The first test case matches with the array from the statement. To obtain a cost of $$$7$$$, you need to block the elements at positions $$$2$$$ and $$$4$$$. In this case, the cost of the array is calculated as the maximum of: the sum of the blocked elements, which is $$$a_2 + a_4 = 7$$$. the maximum sum of the segments into which the array is divided when the blocked elements are removed, i.e., the maximum of $$$a_1$$$, $$$a_3$$$, $$$a_5 + a_6 = max(1,5,5) = 5$$$. So the cost is $$$max(7,5) = 7$$$. In the second test case, you can block the elements at positions $$$1$$$ and $$$4$$$. In the third test case, to obtain the answer $$$11$$$, you can block the elements at positions $$$2$$$ and $$$5$$$. There are other ways to get this answer, for example, blocking positions $$$4$$$ and $$$6$$$. | 1,900 | false | false | true | true | true | false | false | true | false | false | 785 |
1197A | Let's denote a $$$k$$$-step ladder as the following structure: exactly $$$k + 2$$$ wooden planks, of which two planks of length at least $$$k+1$$$ β the base of the ladder; $$$k$$$ planks of length at least $$$1$$$ β the steps of the ladder; Note that neither the base planks, nor the steps planks are required to be equal. For example, ladders $$$1$$$ and $$$3$$$ are correct $$$2$$$-step ladders and ladder $$$2$$$ is a correct $$$1$$$-step ladder. On the first picture the lengths of planks are $$$[3, 3]$$$ for the base and $$$[1]$$$ for the step. On the second picture lengths are $$$[3, 3]$$$ for the base and $$$[2]$$$ for the step. On the third picture lengths are $$$[3, 4]$$$ for the base and $$$[2, 3]$$$ for the steps. You have $$$n$$$ planks. The length of the $$$i$$$-th planks is $$$a_i$$$. You don't have a saw, so you can't cut the planks you have. Though you have a hammer and nails, so you can assemble the improvised "ladder" from the planks. The question is: what is the maximum number $$$k$$$ such that you can choose some subset of the given planks and assemble a $$$k$$$-step ladder using them? Input The first line contains a single integer $$$T$$$ ($$$1 le T le 100$$$) β the number of queries. The queries are independent. Each query consists of two lines. The first line contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$) β the number of planks you have. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^5$$$) β the lengths of the corresponding planks. It's guaranteed that the total number of planks from all queries doesn't exceed $$$10^5$$$. Output Print $$$T$$$ integers β one per query. The $$$i$$$-th integer is the maximum number $$$k$$$, such that you can choose some subset of the planks given in the $$$i$$$-th query and assemble a $$$k$$$-step ladder using them. Print $$$0$$$ if you can't make even $$$1$$$-step ladder from the given set of planks. Example Input 4 4 1 3 1 3 3 3 3 2 5 2 3 3 4 2 3 1 1 2 Note Examples for the queries $$$1-3$$$ are shown at the image in the legend section. The Russian meme to express the quality of the ladders: | 900 | true | true | false | false | false | false | false | false | true | false | 4,715 |
1881B | Once upon a time, bartender Decim found three threadlets and a pair of scissors. In one operation, Decim chooses any threadlet and cuts it into two threadlets, whose lengths are positive integers and their sum is equal to the length of the threadlet being cut. For example, he can cut a threadlet of length $$$5$$$ into threadlets of lengths $$$2$$$ and $$$3$$$, but he cannot cut it into threadlets of lengths $$$2.5$$$ and $$$2.5$$$, or lengths $$$0$$$ and $$$5$$$, or lengths $$$3$$$ and $$$4$$$. Decim can perform at most three operations. He is allowed to cut the threadlets obtained from previous cuts. Will he be able to make all the threadlets of equal length? Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then follows the description of each test case. In a single line of each test case, there are three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 le a, b, c le 10^9$$$)xa0β the lengths of the threadlets. Output For each test case, output "YES" if it is possible to make all the threadlets of equal length by performing at most three operations, otherwise output "NO". You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 15 1 3 2 5 5 5 6 36 12 7 8 7 6 3 3 4 4 12 12 6 8 1000000000 1000000000 1000000000 3 7 1 9 9 1 9 3 6 2 8 2 5 3 10 8 4 8 2 8 4 Output YES YES NO NO YES YES NO YES NO NO YES YES NO YES NO Note Let's consider some testcases of the first test. In the first testcase, you can apply following operations: $$$1, 3, 2 o 1, 2, 1, 2 o 1, 1, 1, 1, 2 o 1, 1, 1, 1, 1, 1$$$. In the second testcase, you can do nothing, the threadlets are already of equal length. In the third testcase, it isn't possible to make threadlets of equal length. | 900 | true | false | false | false | false | false | false | false | false | false | 1,003 |
1513B | A sequence of $$$n$$$ non-negative integers ($$$n ge 2$$$) $$$a_1, a_2, dots, a_n$$$ is called good if for all $$$i$$$ from $$$1$$$ to $$$n-1$$$ the following condition holds true: $$$$$$a_1 : & : a_2 : & : dots : & : a_i = a_{i+1} : & : a_{i+2} : & : dots : & : a_n,$$$$$$ where $$$&$$$ denotes the . Find the number of permutations $$$p$$$ of numbers ranging from $$$1$$$ to $$$n$$$, for which the sequence $$$a_{p_1}$$$, $$$a_{p_2}$$$, ... ,$$$a_{p_n}$$$ is good. Since this number can be large, output it modulo $$$10^9+7$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$), denoting the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$) β the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Note In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of $$$6$$$ permutations possible with numbers from $$$1$$$ to $$$3$$$: $$$[1,2,3]$$$, $$$[1,3,2]$$$, $$$[2,1,3]$$$, $$$[2,3,1]$$$, $$$[3,1,2]$$$, $$$[3,2,1]$$$. In the second test case, it can be proved that no permutation exists for which the sequence is good. In the third test case, there are a total of $$$36$$$ permutations for which the sequence is good. One of them is the permutation $$$[1,5,4,2,3]$$$ which results in the sequence $$$s=[0,0,3,2,0]$$$. This is a good sequence because $$$ s_1 = s_2 : & : s_3 : & : s_4 : & : s_5 = 0$$$, $$$ s_1 : & : s_2 = s_3 : & : s_4 : & : s_5 = 0$$$, $$$ s_1 : & : s_2 : & : s_3 = s_4 : & : s_5 = 0$$$, $$$ s_1 : & : s_2 : & : s_3 : & : s_4 = s_5 = 0$$$. | 1,400 | true | false | false | false | false | true | false | false | false | false | 3,101 |
716A | ZS the Coder is coding on a crazy computer. If you don't type in a word for a _c_ consecutive seconds, everything you typed disappear! More formally, if you typed a word at second _a_ and then the next word at second _b_, then if _b_u2009-u2009_a_u2009β€u2009_c_, just the new word is appended to other words on the screen. If _b_u2009-u2009_a_u2009>u2009_c_, then everything on the screen disappears and after that the word you have typed appears on the screen. For example, if _c_u2009=u20095 and you typed words at seconds 1,u20093,u20098,u200914,u200919,u200920 then at the second 8 there will be 3 words on the screen. After that, everything disappears at the second 13 because nothing was typed. At the seconds 14 and 19 another two words are typed, and finally, at the second 20, one more word is typed, and a total of 3 words remain on the screen. You're given the times when ZS the Coder typed the words. Determine how many words remain on the screen after he finished typing everything. Input The first line contains two integers _n_ and _c_ (1u2009β€u2009_n_u2009β€u2009100u2009000,u20091u2009β€u2009_c_u2009β€u2009109)xa0β the number of words ZS the Coder typed and the crazy computer delay respectively. The next line contains _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009β€u2009_t_1u2009<u2009_t_2u2009<u2009...u2009<u2009_t__n_u2009β€u2009109), where _t__i_ denotes the second when ZS the Coder typed the _i_-th word. Output Print a single positive integer, the number of words that remain on the screen after all _n_ words was typed, in other words, at the second _t__n_. Note The first sample is already explained in the problem statement. For the second sample, after typing the first word at the second 1, it disappears because the next word is typed at the second 3 and 3u2009-u20091u2009>u20091. Similarly, only 1 word will remain at the second 9. Then, a word is typed at the second 10, so there will be two words on the screen, as the old word won't disappear because 10u2009-u20099u2009β€u20091. | 800 | false | false | true | false | false | false | false | false | false | false | 6,954 |
1889A | Qingshan has a string $$$s$$$ which only contains $$$ exttt{0}$$$ and $$$ exttt{1}$$$. A string $$$a$$$ of length $$$k$$$ is good if and only if $$$a_i e a_{k-i+1}$$$ for all $$$i=1,2,ldots,k$$$. For Div. 2 contestants, note that this condition is different from the condition in problem B. For example, $$$ exttt{10}$$$, $$$ exttt{1010}$$$, $$$ exttt{111000}$$$ are good, while $$$ exttt{11}$$$, $$$ exttt{101}$$$, $$$ exttt{001}$$$, $$$ exttt{001100}$$$ are not good. Qingshan wants to make $$$s$$$ good. To do this, she can do the following operation at most $$$300$$$ times (possibly, zero): insert $$$ exttt{01}$$$ to any position of $$$s$$$ (getting a new $$$s$$$). Please tell Qingshan if it is possible to make $$$s$$$ good. If it is possible, print a sequence of operations that makes $$$s$$$ good. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 100$$$)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 nle 100$$$)xa0β the length of string $$$s$$$, respectively. The second line of each test case contains a string $$$s$$$ with length $$$n$$$. It is guaranteed that $$$s$$$ only consists of $$$ exttt{0}$$$ and $$$ exttt{1}$$$. Output For each test case, if it impossible to make $$$s$$$ good, output $$$-1$$$. Otherwise, output $$$p$$$ ($$$0 le p le 300$$$)xa0β the number of operations, in the first line. Then, output $$$p$$$ integers in the second line. The $$$i$$$-th integer should be an index $$$x_i$$$ ($$$0 le x_i le n+2i-2$$$)xa0β the position where you want to insert $$$ exttt{01}$$$ in the current $$$s$$$. If $$$x_i=0$$$, you insert $$$ exttt{01}$$$ at the beginning of $$$s$$$. Otherwise, you insert $$$ exttt{01}$$$ immediately after the $$$x_i$$$-th character of $$$s$$$. We can show that under the constraints in this problem, if an answer exists, there is always an answer that requires at most $$$300$$$ operations. Example Input 6 2 01 3 000 4 1111 6 001110 10 0111001100 3 001 Output 0 -1 -1 2 6 7 1 10 -1 Note In the first test case, you can do zero operations and get $$$s= exttt{01}$$$, which is good. Another valid solution is to do one operation: (the inserted $$$ exttt{01}$$$ is underlined) 1. $$$ exttt{0}underline{ exttt{01}} exttt{1}$$$ and get $$$s = exttt{0011}$$$, which is good. In the second and the third test case, it is impossible to make $$$s$$$ good. In the fourth test case, you can do two operations: 1. $$$ exttt{001110}underline{ exttt{01}}$$$ 2. $$$ exttt{0011100}underline{ exttt{01}} exttt{1}$$$ and get $$$s = exttt{0011100011}$$$, which is good. | 1,300 | false | true | true | false | false | true | false | false | false | false | 966 |
1444E | This is an interactive problem. You are given a treexa0β connected undirected graph without cycles. One vertex of the tree is special, and you have to find which one. You can ask questions in the following form: given an edge of the tree, which endpoint is closer to the special vertex, meaning which endpoint's shortest path to the special vertex contains fewer edges. You have to find the special vertex by asking the minimum number of questions in the worst case for a given tree. Please note that the special vertex might not be fixed by the interactor in advance: it might change the vertex to any other one, with the requirement of being consistent with the previously given answers. Input You are given an integer $$$n$$$ ($$$2 le n le 100$$$)xa0β the number of vertices in a tree. The folloiwing $$$n-1$$$ lines contain two integers each, $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$), that denote an edge in the tree connecting $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree. Interaction After reading the input data, one can start making queries. There are two possible queries: 1. "?xa0$$$u$$$ $$$v$$$"xa0β to ask for an edge $$$(u, v)$$$ ($$$1 le u, v le n$$$) which of the endpoints is closer to the special vertex. The answer to this query is one of the endpoints. Note that, $$$u$$$ and $$$v$$$ must be connected by an edge, and hence they can not have the same distance to the special vertex. 2. "!xa0$$$u$$$"xa0β to indicate that you found the special vertex. After the program does that, it must immediately terminate. Do not forget to output the end of line and flush the output. Otherwise you will get Idleness limit exceeded verdict. To flush the output, you can use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; sys.stdout.flush() in Python; see documentation for other languages. In case you ask more queries than needed in the worst case for a given tree, you will get verdict Wrong answer. Examples Input 5 1 2 2 3 3 4 4 5 3 2 1 Output ? 3 4 ? 2 3 ? 1 2 ! 1 Input 5 2 1 3 1 4 1 5 1 1 1 4 Output ? 1 2 ? 1 3 ? 1 4 ! 4 Note Hacks are forbidden in this task. | 3,500 | false | false | false | true | false | false | true | false | false | false | 3,450 |
1628B | Mihai plans to watch a movie. He only likes palindromic movies, so he wants to skip some (possibly zero) scenes to make the remaining parts of the movie palindromic. You are given a list $$$s$$$ of $$$n$$$ non-empty strings of length at most $$$3$$$, representing the scenes of Mihai's movie. A subsequence of $$$s$$$ is called awesome if it is non-empty and the concatenation of the strings in the subsequence, in order, is a palindrome. Can you help Mihai check if there is at least one awesome subsequence of $$$s$$$? A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not. A sequence $$$a$$$ is a non-empty subsequence of a non-empty sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly zero, but not all) elements. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) β the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) β the number of scenes in the movie. Then follows $$$n$$$ lines, the $$$i$$$-th of which containing a single non-empty string $$$s_i$$$ of length at most $$$3$$$, consisting of lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print "YES" if there is an awesome subsequence of $$$s$$$, or "NO" otherwise (case insensitive). Example Input 6 5 zx ab cc zx ba 2 ab bad 4 co def orc es 3 a b c 3 ab cd cba 2 ab ab Output YES NO NO YES YES NO Note In the first test case, an awesome subsequence of $$$s$$$ is $$$[ab, cc, ba]$$$ | 1,700 | false | true | false | false | false | false | false | false | false | false | 2,474 |
444A | DZY loves Physics, and he enjoys calculating density. Almost everything has density, even a graph. We define the density of a non-directed graph (nodes and edges of the graph have some values) as follows: where _v_ is the sum of the values of the nodes, _e_ is the sum of the values of the edges. Once DZY got a graph _G_, now he wants to find a connected induced subgraph _G_' of the graph, such that the density of _G_' is as large as possible. An induced subgraph _G_'(_V_',u2009_E_') of a graph _G_(_V_,u2009_E_) is a graph that satisfies: Help DZY to find the induced subgraph with maximum density. Note that the induced subgraph you choose must be connected. Input The first line contains two space-separated integers _n_xa0(1u2009β€u2009_n_u2009β€u2009500), . Integer _n_ represents the number of nodes of the graph _G_, _m_ represents the number of edges. The second line contains _n_ space-separated integers _x__i_xa0(1u2009β€u2009_x__i_u2009β€u2009106), where _x__i_ represents the value of the _i_-th node. Consider the graph nodes are numbered from 1 to _n_. Each of the next _m_ lines contains three space-separated integers _a__i_,u2009_b__i_,u2009_c__i_xa0(1u2009β€u2009_a__i_u2009<u2009_b__i_u2009β€u2009_n_;xa01u2009β€u2009_c__i_u2009β€u2009103), denoting an edge between node _a__i_ and _b__i_ with value _c__i_. The graph won't contain multiple edges. Output Output a real number denoting the answer, with an absolute or relative error of at most 10u2009-u20099. Examples Input 5 6 13 56 73 98 17 1 2 56 1 3 29 1 4 42 2 3 95 2 4 88 3 4 63 Note In the first sample, you can only choose an empty subgraph, or the subgraph containing only node 1. In the second sample, choosing the whole graph is optimal. | 1,600 | true | true | false | false | false | false | false | false | false | false | 8,063 |
765G | Problem - 765G - 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 math meet-in-the-middle number theory *3200 No tag edit access β Contest materials ") . In the next line of input there is an integer _n_ (1u2009β€u2009_n_u2009β€u20095Β·105). Each of the next _n_ lines contains two space-separated integers _p__i_, Ξ±_i_ (1u2009β€u2009_p__i_,u2009Ξ±_i_u2009β€u2009109, _p__i_ is prime). All _p__i_ are distinct. Output A single integerxa0β the answer to the problem. Examples Input 1 2 2 1 3 1 Output 2 Input 01 2 3 2 5 1 Output 15 Input 1011 1 3 1000000000 Output 411979884 | 3,200 | true | false | false | true | false | false | true | false | false | false | 6,724 |
220C | Problem - 220C - 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 *2100 No tag edit access β Contest materials element of the permutation _a_ as _a__i_, the _j_-th (1u2009β€u2009_j_u2009β€u2009_n_) element of the permutation _b_ β as _b__j_. The distance between permutations _a_ and _b_ is the minimum absolute value of the difference between the positions of the occurrences of some number in _a_ and in _b_. More formally, it's such minimum _i_u2009-u2009_j_, that _a__i_u2009=u2009_b__j_. A cyclic shift number _i_ (1u2009β€u2009_i_u2009β€u2009_n_) of permutation _b_ consisting from _n_ elements is a permutation _b__i__b__i_u2009+u20091... _b__n__b_1_b_2... _b__i_u2009-u20091. Overall a permutation has _n_ cyclic shifts. The Little Elephant wonders, for all cyclic shifts of permutation _b_, what is the distance between the cyclic shift and permutation _a_? Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the size of the permutations. The second line contains permutation _a_ as _n_ distinct numbers from 1 to _n_, inclusive. The numbers are separated with single spaces. The third line contains permutation _b_ in the same format. Output In _n_ lines print _n_ integers β the answers for cyclic shifts. Print the answers to the shifts in the order of the shifts' numeration in permutation _b_, that is, first for the 1-st cyclic shift, then for the 2-nd, and so on. Examples Input 2 1 2 2 1 Output 1 0 Input 4 2 1 3 4 3 4 2 1 Output 2 1 0 1 | 2,100 | false | false | false | false | true | false | false | false | false | false | 8,957 |
1051C | Vasya has a multiset $$$s$$$ consisting of $$$n$$$ integer numbers. Vasya calls some number $$$x$$$ nice if it appears in the multiset exactly once. For example, multiset $$${1, 1, 2, 3, 3, 3, 4}$$$ contains nice numbers $$$2$$$ and $$$4$$$. Vasya wants to split multiset $$$s$$$ into two multisets $$$a$$$ and $$$b$$$ (one of which may be empty) in such a way that the quantity of nice numbers in multiset $$$a$$$ would be the same as the quantity of nice numbers in multiset $$$b$$$ (the quantity of numbers to appear exactly once in multiset $$$a$$$ and the quantity of numbers to appear exactly once in multiset $$$b$$$). Input The first line contains a single integer $$$n~(2 le n le 100)$$$. The second line contains $$$n$$$ integers $$$s_1, s_2, dots s_n~(1 le s_i le 100)$$$ β the multiset $$$s$$$. Output If there exists no split of $$$s$$$ to satisfy the given requirements, then print "NO" in the first line. Otherwise print "YES" in the first line. The second line should contain a string, consisting of $$$n$$$ characters. $$$i$$$-th character should be equal to 'A' if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$a$$$ and 'B' if if the $$$i$$$-th element of multiset $$$s$$$ goes to multiset $$$b$$$. Elements are numbered from $$$1$$$ to $$$n$$$ in the order they are given in the input. If there exist multiple solutions, then print any of them. | 1,500 | true | true | true | true | false | false | true | false | false | false | 5,453 |
1250M | Berland Gardeners United Inc. hired you for the project called "SmartGarden". The main feature of this project is automatic garden watering. Formally the garden can be represented as a square of $$$n imes n$$$ cells with rows numbered $$$1$$$ to $$$n$$$ from top to bottom and columns numbered $$$1$$$ to $$$n$$$ from left to right. Each cell of the garden contains either a plant or a slab. It's known that slabs are located on the main diagonal of the matrix representing the garden, and in the cells that are below the main diagonal and share a side with at least one cell of the main diagonal. All the remaining cells of the garden are filled with plants. Example of the garden for $$$n=5$$$. During implementation of the project you created a smart robot that takes a list of commands as an input, which are processed one by one. Each command contains: a list of horizontal lines (rows in the matrix representing the garden); a list of vertical lines (columns in the matrix representing the garden). While executing each command robot waters only cells in the intersection of specified rows and specified columns. So, if you specify $$$r$$$ rows and $$$c$$$ columns, then exactly $$$r cdot c$$$ cells will be watered. In the demo for the customer you have tuned robot in such a way that it waters all the garden. To do that you prepared a single command containing all $$$n$$$ rows and all $$$n$$$ columns. Unfortunately, 5 hours before the demo for your customer it turned out that the CEO of Berland Gardeners United Inc. was going to take part in it. Moreover, most probably he will be standing on a garden slab during the demo! Now you need to create a list of commands for the robot so that it waters all the plants and doesn't water any cell containing a slab. Since it's only a beta version of "SmartGarden", the total number of commands shouldn't exceed $$$50$$$. Create a program that, for a given size of the garden, will find a list of no more than $$$50$$$ commands that allow the robot to water all the plants in the garden without watering the slabs. It is allowed to water a plant several times. Output In the first line print the total number of commands for the robot $$$k$$$ ($$$1 le k le 50$$$). In the next $$$2 cdot k$$$ lines print all the commands. Each command should be specified by $$$2$$$ lines. The first line of each command should describe rows in the command and the second line should describe columns in the command. Each of these $$$2$$$ lines should have the following format: the first number of the line should specify the total number of items $$$x$$$ in the appropriate list; then $$$x$$$ distinct numbers follow, where each number is in the range $$$1 dots n$$$ and describes a chosen row for the first line and a chosen column for the second line. If there are multiple ways to water the garden, print any of them. | 2,500 | false | false | false | false | false | true | false | false | false | false | 4,443 |
165A | One day Vasya painted a Cartesian coordinate system on a piece of paper and marked some set of points (_x_1,u2009_y_1),u2009(_x_2,u2009_y_2),u2009...,u2009(_x__n_,u2009_y__n_). Let's define neighbors for some fixed point from the given set (_x_,u2009_y_): point (_x_',u2009_y_') is (_x_,u2009_y_)'s right neighbor, if _x_'u2009>u2009_x_ and _y_'u2009=u2009_y_ point (_x_',u2009_y_') is (_x_,u2009_y_)'s left neighbor, if _x_'u2009<u2009_x_ and _y_'u2009=u2009_y_ point (_x_',u2009_y_') is (_x_,u2009_y_)'s lower neighbor, if _x_'u2009=u2009_x_ and _y_'u2009<u2009_y_ point (_x_',u2009_y_') is (_x_,u2009_y_)'s upper neighbor, if _x_'u2009=u2009_x_ and _y_'u2009>u2009_y_ We'll consider point (_x_,u2009_y_) from the given set supercentral, if it has at least one upper, at least one lower, at least one left and at least one right neighbor among this set's points. Vasya marked quite many points on the paper. Analyzing the picture manually is rather a challenge, so Vasya asked you to help him. Your task is to find the number of supercentral points in the given set. Input The first input line contains the only integer _n_ (1u2009β€u2009_n_u2009β€u2009200) β the number of points in the given set. Next _n_ lines contain the coordinates of the points written as "_x_ _y_" (without the quotes) (_x_,u2009_y_u2009β€u20091000), all coordinates are integers. The numbers in the line are separated by exactly one space. It is guaranteed that all points are different. Output Print the only number β the number of supercentral points of the given set. Examples Input 8 1 1 4 2 3 1 1 2 0 2 0 1 1 0 1 3 Note In the first sample the supercentral points are only points (1,u20091) and (1,u20092). In the second sample there is one supercental point β point (0,u20090). | 1,000 | false | false | true | false | false | false | false | false | false | false | 9,205 |
1743G | Problem - 1743G - 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 bitmasks combinatorics constructive algorithms data structures dp hashing math *3000 No tag edit access β Contest materials . So, for example, $$$f_2$$$ is 10, $$$f_3$$$ is 101, $$$f_4$$$ is 10110. For a given string $$$s$$$, let's define $$$g(s)$$$ as the number of ways to cut it into several (any number, possibly even just one) strings such that none of these strings are Fibonacci strings. For example, if $$$s$$$ is 10110101, $$$g(s) = 3$$$ since there are three ways to cut it: 101101 $$$+$$$ 01; 1011 $$$+$$$ 0101; 1011 $$$+$$$ 01 $$$+$$$ 01. You are given a sequence of strings $$$s_1, s_2, dots, s_n$$$. Calculate $$$g(s_1), g(s_1 + s_2), dots, g(s_1 + s_2 + ldots + s_n)$$$. Since these values can be huge, print them modulo $$$998244353$$$. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^3$$$). Then, $$$n$$$ lines follow. The $$$i$$$-th line contains the string $$$s_i$$$ ($$$1 le s_i le 10^3$$$), consisting of characters 0 and/or 1. Output Print $$$n$$$ integers, where the $$$i$$$-th integer is $$$g(s_1 + s_2 + ldots + s_i) bmod 998244353$$$. Examples Input 1 10110101 Output 3 Input 3 1111 1 0 Output 2 3 3 Input 6 10110101 100100001110 0000001100010001 1111 1001010100101010101001 000100000010101111 Output 3 561 1466229 9887505 972227653 52128355 | 3,000 | true | false | false | true | true | true | false | false | false | false | 1,818 |
1747B | You are given an integer $$$n$$$. Let's define $$$s(n)$$$ as the string "BAN" concatenated $$$n$$$ times. For example, $$$s(1)$$$ = "BAN", $$$s(3)$$$ = "BANBANBAN". Note that the length of the string $$$s(n)$$$ is equal to $$$3n$$$. Consider $$$s(n)$$$. You can perform the following operation on $$$s(n)$$$ any number of times (possibly zero): Select any two distinct indices $$$i$$$ and $$$j$$$ $$$(1 leq i, j leq 3n, i e j)$$$. Then, swap $$$s(n)_i$$$ and $$$s(n)_j$$$. You want the string "BAN" to not appear in $$$s(n)$$$ as a subsequence. What's the smallest number of operations you have to do to achieve this? Also, find one such shortest sequence of operations. A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 100)$$$ xa0β the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ $$$(1 leq n leq 100)$$$. Output For each test case, in the first line output $$$m$$$ ($$$0 le m le 10^5$$$)xa0β the minimum number of operations required. It's guaranteed that the objective is always achievable in at most $$$10^5$$$ operations under the constraints of the problem. Then, output $$$m$$$ lines. The $$$k$$$-th of these lines should contain two integers $$$i_k$$$, $$$j_k$$$ $$$(1leq i_k, j_k leq 3n, i_k e j_k)$$$ denoting that you want to swap characters at indices $$$i_k$$$ and $$$j_k$$$ at the $$$k$$$-th operation. After all $$$m$$$ operations, "BAN" must not appear in $$$s(n)$$$ as a subsequence. If there are multiple possible answers, output any. Note In the first testcase, $$$s(1) = $$$ "BAN", we can swap $$$s(1)_1$$$ and $$$s(1)_2$$$, converting $$$s(1)$$$ to "ABN", which does not contain "BAN" as a subsequence. In the second testcase, $$$s(2) = $$$ "BANBAN", we can swap $$$s(2)_2$$$ and $$$s(2)_6$$$, converting $$$s(2)$$$ to "BNNBAA", which does not contain "BAN" as a subsequence. | 900 | false | false | false | false | false | true | false | false | false | false | 1,801 |
60E | Once upon a time in the thicket of the mushroom forest lived mushroom gnomes. They were famous among their neighbors for their magic mushrooms. Their magic nature made it possible that between every two neighboring mushrooms every minute grew another mushroom with the weight equal to the sum of weights of two neighboring ones. The mushroom gnomes loved it when everything was in order, that's why they always planted the mushrooms in one line in the order of their weights' increasing. Well... The gnomes planted the mushrooms and went to eat. After _x_ minutes they returned and saw that new mushrooms had grown up, so that the increasing order had been violated. The gnomes replanted all the mushrooms in the correct order, that is, they sorted the mushrooms in the order of the weights' increasing. And went to eat again (those gnomes were quite big eaters). What total weights modulo _p_ will the mushrooms have in another _y_ minutes? Input The first line contains four integers _n_, _x_, _y_, _p_ (1u2009β€u2009_n_u2009β€u2009106,u20090u2009β€u2009_x_,u2009_y_u2009β€u20091018,u2009_x_u2009+u2009_y_u2009>u20090,u20092u2009β€u2009_p_u2009β€u2009109) which represent the number of mushrooms, the number of minutes after the first replanting, the number of minutes after the second replanting and the module. The next line contains _n_ integers _a__i_ which represent the mushrooms' weight in the non-decreasing order (0u2009β€u2009_a__i_u2009β€u2009109). Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). Output The answer should contain a single number which is the total weights of the mushrooms modulo _p_ in the end after _x_u2009+u2009_y_ minutes. | 2,600 | true | false | false | false | false | false | false | false | false | false | 9,671 |
89B | Vasya writes his own library for building graphical user interface. Vasya called his creation VTK (VasyaToolKit). One of the interesting aspects of this library is that widgets are packed in each other. A widget is some element of graphical interface. Each widget has width and height, and occupies some rectangle on the screen. Any widget in Vasya's library is of type Widget. For simplicity we will identify the widget and its type. Types HBox and VBox are derivatives of type Widget, so they also are types Widget. Widgets HBox and VBox are special. They can store other widgets. Both those widgets can use the pack() method to pack directly in itself some other widget. Widgets of types HBox and VBox can store several other widgets, even several equal widgets β they will simply appear several times. As a result of using the method pack() only the link to the packed widget is saved, that is when the packed widget is changed, its image in the widget, into which it is packed, will also change. We shall assume that the widget _a_ is packed in the widget _b_ if there exists a chain of widgets _a_u2009=u2009_c_1,u2009_c_2,u2009...,u2009_c__k_u2009=u2009_b_, _k_u2009β₯u20092, for which _c__i_ is packed directly to _c__i_u2009+u20091 for any 1u2009β€u2009_i_u2009<u2009_k_. In Vasya's library the situation when the widget _a_ is packed in the widget _a_ (that is, in itself) is not allowed. If you try to pack the widgets into each other in this manner immediately results in an error. Also, the widgets HBox and VBox have parameters border and spacing, which are determined by the methods set_border() and set_spacing() respectively. By default both of these options equal 0. The picture above shows how the widgets are packed into HBox and VBox. At that HBox and VBox automatically change their size depending on the size of packed widgets. As for HBox and VBox, they only differ in that in HBox the widgets are packed horizontally and in VBox β vertically. The parameter spacing sets the distance between adjacent widgets, and border β a frame around all packed widgets of the desired width. Packed widgets are placed exactly in the order in which the pack() method was called for them. If within HBox or VBox there are no packed widgets, their sizes are equal to 0u2009Γu20090, regardless of the options border and spacing. The construction of all the widgets is performed using a scripting language VasyaScript. The description of the language can be found in the input data. For the final verification of the code Vasya asks you to write a program that calculates the sizes of all the widgets on the source code in the language of VasyaScript. Input The first line contains an integer _n_ β the number of instructions (1u2009β€u2009_n_u2009β€u2009100). Next _n_ lines contain instructions in the language VasyaScript β one instruction per line. There is a list of possible instructions below. "Widget " β create a new widget [name] of the type Widget possessing the width of [x] units and the height of [y] units. "HBox [name]" β create a new widget [name] of the type HBox. "VBox [name]" β create a new widget [name] of the type VBox. "[name1].pack([name2])" β pack the widget [name2] in the widget [name1]. At that, the widget [name1] must be of type HBox or VBox. "[name].set_border([x])" β set for a widget [name] the border parameter to [x] units. The widget [name] must be of type HBox or VBox. "[name].set_spacing([x])" β set for a widget [name] the spacing parameter to [x] units. The widget [name] must be of type HBox or VBox. All instructions are written without spaces at the beginning and at the end of the string. The words inside the instruction are separated by exactly one space. There are no spaces directly before the numbers and directly after them. The case matters, for example, "wiDget x" is not a correct instruction. The case of the letters is correct in the input data. All names of the widgets consist of lowercase Latin letters and has the length from 1 to 10 characters inclusive. The names of all widgets are pairwise different. All numbers in the script are integers from 0 to 100 inclusive It is guaranteed that the above-given script is correct, that is that all the operations with the widgets take place after the widgets are created and no widget is packed in itself. It is guaranteed that the script creates at least one widget. Output For each widget print on a single line its name, width and height, separated by spaces. The lines must be ordered lexicographically by a widget's name. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d specificator) | 2,300 | false | false | true | true | false | false | false | false | false | true | 9,528 |
8B | The whole world got obsessed with robots,and to keep pace with the progress, great Berland's programmer Draude decided to build his own robot. He was working hard at the robot. He taught it to walk the shortest path from one point to another, to record all its movements, but like in many Draude's programs, there was a bug β the robot didn't always walk the shortest path. Fortunately, the robot recorded its own movements correctly. Now Draude wants to find out when his robot functions wrong. Heh, if Draude only remembered the map of the field, where he tested the robot, he would easily say if the robot walked in the right direction or not. But the field map was lost never to be found, that's why he asks you to find out if there exist at least one map, where the path recorded by the robot is the shortest. The map is an infinite checkered field, where each square is either empty, or contains an obstruction. It is also known that the robot never tries to run into the obstruction. By the recorded robot's movements find out if there exist at least one such map, that it is possible to choose for the robot a starting square (the starting square should be empty) such that when the robot moves from this square its movements coincide with the recorded ones (the robot doesn't run into anything, moving along empty squares only), and the path from the starting square to the end one is the shortest. In one movement the robot can move into the square (providing there are no obstrutions in this square) that has common sides with the square the robot is currently in. Input The first line of the input file contains the recording of the robot's movements. This recording is a non-empty string, consisting of uppercase Latin letters L, R, U and D, standing for movements left, right, up and down respectively. The length of the string does not exceed 100. | 1,400 | false | false | true | false | false | true | false | false | false | true | 9,954 |
21D | Problem - 21D - 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 bitmasks graph matchings graphs *2400 No tag edit access β Contest materials ") . Input The first line of the input contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u200915,u20090u2009β€u2009_m_u2009β€u20092000), _n_ is the amount of vertices, and _m_ is the amount of edges. Following _m_ lines contain edges as a triples _x_,u2009_y_,u2009_w_ (1u2009β€u2009_x_,u2009_y_u2009β€u2009_n_,u20091u2009β€u2009_w_u2009β€u200910000), _x_,u2009_y_ are edge endpoints, and _w_ is the edge length. Output Output minimal cycle length or -1 if it doesn't exists. Examples Input 3 3 1 2 1 2 3 1 3 1 1 Output 3 Input 3 2 1 2 3 2 3 4 Output 14 | 2,400 | false | false | false | false | false | false | false | false | false | true | 9,889 |
1325A | Problem - 1325A - 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 constructive algorithms greedy number theory *800 No tag edit access β Contest materials +LCM(a,b)=x$$$. As a reminder, $$$GCD(a,b)$$$ is the greatest integer that divides both $$$a$$$ and $$$b$$$. Similarly, $$$LCM(a,b)$$$ is the smallest integer such that both $$$a$$$ and $$$b$$$ divide it. It's guaranteed that the solution always exists. If there are several such pairs $$$(a, b)$$$, you can output any of them. Input The first line contains a single integer $$$t$$$ $$$(1 le t le 100)$$$ xa0β the number of testcases. Each testcase consists of one line containing a single integer, $$$x$$$ $$$(2 le x le 10^9)$$$. Output For each testcase, output a pair of positive integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^9)$$$ such that $$$GCD(a,b)+LCM(a,b)=x$$$. It's guaranteed that the solution always exists. If there are several such pairs $$$(a, b)$$$, you can output any of them. Example Input 2 2 14 Output 1 1 6 4 Note In the first testcase of the sample, $$$GCD(1,1)+LCM(1,1)=1+1=2$$$. In the second testcase of the sample, $$$GCD(6,4)+LCM(6,4)=2+12=14$$$. | 800 | false | true | false | false | false | true | false | false | false | false | 4,093 |
1422D | 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 | false | false | false | false | false | false | false | false | true | true | 3,566 |
97C | One university has just found out about a sport programming contest called ACM ICPC v2.0. This contest doesn't differ much from the well-known ACM ICPC, for example, the participants are not allowed to take part in the finals more than two times. However, there is one notable difference: the teams in the contest should consist of exactly _n_ participants. Having taken part in several ACM ICPC v2.0 finals and having not won any medals, the students and the university governors realized that it's high time they changed something about the preparation process. Specifically, as the first innovation it was decided to change the teams' formation process. Having spent considerable amount of time on studying the statistics of other universities' performance, they managed to receive some interesting information: the dependence between the probability of winning a medal and the number of team members that participated in the finals in the past. More formally, we know _n_u2009+u20091 real numbers _p_0u2009β€u2009_p_1u2009β€u2009...u2009β€u2009_p__n_, where _p__i_ is the probability of getting a medal on the finals if the team has _i_ participants of previous finals, and other _n_u2009-u2009_i_ participants arrived to the finals for the first time. Despite such useful data, the university governors are unable to determine such team forming tactics that would provide the maximum probability of winning a medal at ACM ICPC v2.0 finals on average (we are supposed to want to provide such result to the far future and we are also supposed to have an endless supply of students). And how about you, can you offer such optimal tactic? At the first stage the university governors want to know the value of maximum average probability. More formally, suppose that the university sends a team to the _k_-th world finals. The team has _a__k_ participants of previous finals (0u2009β€u2009_a__k_u2009β€u2009_n_). Since each person can participate in the finals no more than twice, the following condition must be true: . Your task is to choose sequence so that the limit Ξ¨ exists and it's value is maximal: As is an infinite sequence, you should only print the maximum value of the Ξ¨ limit. Input The first line contains an integer _n_ (3u2009β€u2009_n_u2009β€u2009100), _n_ is the number of team participants. The second line contains _n_u2009+u20091 real numbers with no more than 6 digits after decimal point _p__i_ (0u2009β€u2009_i_u2009β€u2009_n_,u20090u2009β€u2009_p__i_u2009β€u20091) β the probability of that the team will win a medal if it contains _i_ participants who has already been on the finals. Also the condition _p__i_u2009β€u2009_p__i_u2009+u20091 should be fulfilled for all 0u2009β€u2009_i_u2009β€u2009_n_u2009-u20091. | 2,400 | true | false | false | false | false | false | false | true | false | true | 9,499 |
1870A | You are given three non-negative integers $$$n$$$, $$$k$$$, and $$$x$$$. Find the maximum possible sum of elements in an array consisting of non-negative integers, which has $$$n$$$ elements, its MEX is equal to $$$k$$$, and all its elements do not exceed $$$x$$$. If such an array does not exist, output $$$-1$$$. The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of $$$[2,2,1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. The MEX of $$$[3,1,0,1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. The MEX of $$$[0,3,1,2]$$$ is $$$4$$$, because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the array, but $$$4$$$ does not. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) β the number of test cases. Then follows the description of the test cases. The only line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$x$$$ ($$$1 leq n, k, x leq 200$$$). Output For each test case, output a single number β the maximum sum of elements in a valid array, or $$$-1$$$, if such an array does not exist. Example Input 9 5 3 3 4 7 5 4 2 28 12 10 6 57 51 122 200 1 200 2 2 1 3 2 1 4 7 10 Output 7 -1 57 -1 2007 39800 1 2 -1 Note In the first test case, the maximum sum is $$$7$$$, and one of the valid arrays is $$$[0, 1, 2, 2, 2]$$$. In the second test case, there are no valid arrays of length $$$n$$$. In the third test case, the maximum sum is $$$57$$$, and one of the valid arrays is $$$[0, 1, 28, 28]$$$. | 800 | true | true | false | false | false | true | false | false | false | false | 1,059 |
1728A | The title is a reference to the very first Educational Round from our writers team, Educational Round 18. There is a bag, containing colored balls. There are $$$n$$$ different colors of balls, numbered from $$$1$$$ to $$$n$$$. There are $$$mathit{cnt}_i$$$ balls of color $$$i$$$ in the bag. The total amount of balls in the bag is odd (e.u2009g. $$$mathit{cnt}_1 + mathit{cnt}_2 + dots + mathit{cnt}_n$$$ is odd). In one move, you can choose two balls with different colors and take them out of the bag. At some point, all the remaining balls in the bag will have the same color. That's when you can't make moves anymore. Find any possible color of the remaining balls. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 20$$$)xa0β the number of colors. The second line contains $$$n$$$ integers $$$mathit{cnt}_1, mathit{cnt}_2, dots, mathit{cnt}_n$$$ ($$$1 le mathit{cnt}_i le 100$$$)xa0β the amount of balls of each color in the bag. The total amount of balls in the bag is odd (e.u2009g. $$$mathit{cnt}_1 + mathit{cnt}_2 + dots + mathit{cnt}_n$$$ is odd). Output For each testcase, print a single integerxa0β any possible color of the remaining balls, after you made some moves and can't make moves anymore. Note In the first testcase, your first and only move can be one of the following: take balls with colors $$$1$$$ and $$$2$$$; take balls with colors $$$1$$$ and $$$3$$$; take balls with colors $$$2$$$ and $$$3$$$. After the move, exactly one ball will remain. Its color can be $$$3, 2$$$ or $$$1$$$ depending on the move. In the second testcase, you can't make moves at allxa0β there is only color of balls already. This color is $$$1$$$. In the third testcase, you can keep removing one ball of color $$$1$$$ and one ball of color $$$2$$$ until there are no more balls of color $$$1$$$. At the end, three balls of color $$$2$$$ remain. | 800 | false | true | true | false | false | false | true | false | true | false | 1,925 |
1156E | Problem - 1156E - 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 divide and conquer dsu two pointers *2200 No tag edit access β Contest materials . Let's call some subsegment $$$p | 2,200 | false | false | false | false | true | false | false | false | false | false | 4,926 |
1943A | Alice and Bob play yet another game on an array $$$a$$$ of size $$$n$$$. Alice starts with an empty array $$$c$$$. Both players take turns playing, with Alice starting first. On Alice's turn, she picks one element from $$$a$$$, appends that element to $$$c$$$, and then deletes it from $$$a$$$. On Bob's turn, he picks one element from $$$a$$$, and then deletes it from $$$a$$$. The game ends when the array $$$a$$$ is empty. Game's score is defined to be the MEX$$$^dagger$$$ of $$$c$$$. Alice wants to maximize the score while Bob wants to minimize it. Find game's final score if both players play optimally. $$$^dagger$$$ The $$$operatorname{MEX}$$$ (minimum excludant) of an array of integers is defined as the smallest non-negative integer which does not occur in the array. For example: The MEX of $$$[2,2,1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. The MEX of $$$[3,1,0,1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. The MEX of $$$[0,3,1,2]$$$ is $$$4$$$, because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the array, but $$$4$$$ does not. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2 cdot 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$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i < n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, find game's score if both players play optimally. Example Input 3 4 0 0 1 1 4 0 1 2 3 2 1 1 Note In the first test case, a possible game with a score of $$$2$$$ is as follows: 1. Alice chooses the element $$$1$$$. After this move, $$$a=[0,0,1]$$$ and $$$c=[1]$$$. 2. Bob chooses the element $$$0$$$. After this move, $$$a=[0,1]$$$ and $$$c=[1]$$$. 3. Alice chooses the element $$$0$$$. After this move, $$$a=[1]$$$ and $$$c=[1,0]$$$. 4. Bob chooses the element $$$1$$$. After this move, $$$a=[,]$$$ and $$$c=[1,0]$$$. At the end, $$$c=[1,0]$$$, which has a MEX of $$$2$$$. Note that this is an example game and does not necessarily represent the optimal strategy for both players. | 1,300 | false | true | false | false | false | false | false | false | false | false | 623 |
47D | Problem - 47D - 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 *2200 No tag edit access β Contest materials ") which represent the number of numbers in the code and the number of attempts made by Vasya. Then follow _m_ lines, each containing space-separated _s__i_ and _c__i_ which correspondingly indicate Vasyaβs attempt (a line containing _n_ numbers which are 0 or 1) and the systemβs response (an integer from 0 to 5 inclusively). Output Print the single number which indicates how many possible code variants that do not contradict the _m_ system responses are left. Examples Input 6 2 000000 2 010100 4 Output 6 Input 6 3 000000 2 010100 4 111100 0 Output 0 Input 6 3 000000 2 010100 4 111100 2 Output 1 | 2,200 | false | false | false | false | false | false | true | false | false | false | 9,739 |
1040B | Long story short, shashlik is Miroslav's favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over. This time Miroslav laid out $$$n$$$ skewers parallel to each other, and enumerated them with consecutive integers from $$$1$$$ to $$$n$$$ in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number $$$i$$$, it leads to turning $$$k$$$ closest skewers from each side of the skewer $$$i$$$, that is, skewers number $$$i - k$$$, $$$i - k + 1$$$, ..., $$$i - 1$$$, $$$i + 1$$$, ..., $$$i + k - 1$$$, $$$i + k$$$ (if they exist). For example, let $$$n = 6$$$ and $$$k = 1$$$. When Miroslav turns skewer number $$$3$$$, then skewers with numbers $$$2$$$, $$$3$$$, and $$$4$$$ will come up turned over. If after that he turns skewer number $$$1$$$, then skewers number $$$1$$$, $$$3$$$, and $$$4$$$ will be turned over, while skewer number $$$2$$$ will be in the initial position (because it is turned again). As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all $$$n$$$ skewers with the minimal possible number of actions. For example, for the above example $$$n = 6$$$ and $$$k = 1$$$, two turnings are sufficient: he can turn over skewers number $$$2$$$ and $$$5$$$. Help Miroslav turn over all $$$n$$$ skewers. Note In the first example the first operation turns over skewers $$$1$$$, $$$2$$$ and $$$3$$$, the second operation turns over skewers $$$4$$$, $$$5$$$, $$$6$$$ and $$$7$$$. In the second example it is also correct to turn over skewers $$$2$$$ and $$$5$$$, but turning skewers $$$2$$$ and $$$4$$$, or $$$1$$$ and $$$5$$$ are incorrect solutions because the skewer $$$3$$$ is in the initial state after these operations. | 1,300 | true | true | false | true | false | false | false | false | false | false | 5,496 |
727F | Polycarp is an experienced participant in Codehorses programming contests. Now he wants to become a problemsetter. He sent to the coordinator a set of _n_ problems. Each problem has it's quality, the quality of the _i_-th problem is _a__i_ (_a__i_ can be positive, negative or equal to zero). The problems are ordered by expected difficulty, but the difficulty is not related to the quality in any way. The easiest problem has index 1, the hardest problem has index _n_. The coordinator's mood is equal to _q_ now. After reading a problem, the mood changes by it's quality. It means that after the coordinator reads a problem with quality _b_, the value _b_ is added to his mood. The coordinator always reads problems one by one from the easiest to the hardest, it's impossible to change the order of the problems. If after reading some problem the coordinator's mood becomes negative, he immediately stops reading and rejects the problemset. Polycarp wants to remove the minimum number of problems from his problemset to make the coordinator's mood non-negative at any moment of time. Polycarp is not sure about the current coordinator's mood, but he has _m_ guesses "the current coordinator's mood _q_u2009=u2009_b__i_". For each of _m_ guesses, find the minimum number of problems Polycarp needs to remove so that the coordinator's mood will always be greater or equal to 0 while he reads problems from the easiest of the remaining problems to the hardest. Input The first line of input contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u2009750, 1u2009β€u2009_m_u2009β€u2009200u2009000)xa0β the number of problems in the problemset and the number of guesses about the current coordinator's mood. The second line of input contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009β€u2009_a__i_u2009β€u2009109)xa0β the qualities of the problems in order of increasing difficulty. The third line of input contains _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_ (0u2009β€u2009_b__i_u2009β€u20091015)xa0β the guesses of the current coordinator's mood _q_. | 2,300 | false | true | false | true | false | false | false | true | false | false | 6,895 |
487B | Problem - 487B - 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 two pointers *2000 No tag edit access β Contest materials . For each piece of strip, it must satisfy: Each piece should contain at least _l_ numbers. The difference between the maximal and the minimal number on the piece should be at most _s_. Please help Alexandra to find the minimal number of pieces meeting the condition above. Input The first line contains three space-separated integers _n_,u2009_s_,u2009_l_ (1u2009β€u2009_n_u2009β€u2009105,u20090u2009β€u2009_s_u2009β€u2009109,u20091u2009β€u2009_l_u2009β€u2009105). The second line contains _n_ integers _a__i_ separated by spaces (u2009-u2009109u2009β€u2009_a__i_u2009β€u2009109). Output Output the minimal number of strip pieces. If there are no ways to split the strip, output -1. Examples Input 7 2 2 1 3 1 2 4 1 2 Output 3 Input 7 2 2 1 100 1 100 1 100 1 Output -1 Note For the first sample, we can split the strip into 3 pieces: | 2,000 | false | false | false | true | true | false | false | true | false | false | 7,889 |
96A | Problem - 96A - 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 *900 No tag edit access β Contest materials Question A - D") | 900 | false | false | true | false | false | false | false | false | false | false | 9,503 |
9A | Problem - 9A - 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 math probabilities *800 No tag edit access β Contest materials | 800 | true | false | false | false | false | false | false | false | false | false | 9,950 |
935B | Two neighboring kingdoms decided to build a wall between them with some gates to enable the citizens to go from one kingdom to another. Each time a citizen passes through a gate, he has to pay one silver coin. The world can be represented by the first quadrant of a plane and the wall is built along the identity line (i.e. the line with the equation _x_u2009=u2009_y_). Any point below the wall belongs to the first kingdom while any point above the wall belongs to the second kingdom. There is a gate at any integer point on the line (i.e. at points (0,u20090), (1,u20091), (2,u20092), ...). The wall and the gates do not belong to any of the kingdoms. Fafa is at the gate at position (0,u20090) and he wants to walk around in the two kingdoms. He knows the sequence _S_ of moves he will do. This sequence is a string where each character represents a move. The two possible moves Fafa will do are 'U' (move one step up, from (_x_,u2009_y_) to (_x_,u2009_y_u2009+u20091)) and 'R' (move one step right, from (_x_,u2009_y_) to (_x_u2009+u20091,u2009_y_)). Fafa wants to know the number of silver coins he needs to pay to walk around the two kingdoms following the sequence _S_. Note that if Fafa visits a gate without moving from one kingdom to another, he pays no silver coins. Also assume that he doesn't pay at the gate at point (0,u20090), i.xa0e. he is initially on the side he needs. Input The first line of the input contains single integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of moves in the walking sequence. The second line contains a string _S_ of length _n_ consisting of the characters 'U' and 'R' describing the required moves. Fafa will follow the sequence _S_ in order from left to right. | 900 | false | false | true | false | false | false | false | false | false | false | 5,963 |
733C | There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city. Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monster being eaten, and they stand in the queue next to each other. Monsters eat each other instantly. There are no monsters which are being eaten at the same moment. After the monster _A_ eats the monster _B_, the weight of the monster _A_ increases by the weight of the eaten monster _B_. In result of such eating the length of the queue decreases by one, all monsters after the eaten one step forward so that there is no empty places in the queue again. A monster can eat several monsters one after another. Initially there were _n_ monsters in the queue, the _i_-th of which had weight _a__i_. For example, if weights are [1,u20092,u20092,u20092,u20091,u20092] (in order of queue, monsters are numbered from 1 to 6 from left to right) then some of the options are: 1. the first monster can't eat the second monster because _a_1u2009=u20091 is not greater than _a_2u2009=u20092; 2. the second monster can't eat the third monster because _a_2u2009=u20092 is not greater than _a_3u2009=u20092; 3. the second monster can't eat the fifth monster because they are not neighbors; 4. the second monster can eat the first monster, the queue will be transformed to [3,u20092,u20092,u20091,u20092]. After some time, someone said a good joke and all monsters recovered. At that moment there were _k_ (_k_u2009β€u2009_n_) monsters in the queue, the _j_-th of which had weight _b__j_. Both sequences (_a_ and _b_) contain the weights of the monsters in the order from the first to the last. You are required to provide one of the possible orders of eating monsters which led to the current queue, or to determine that this could not happen. Assume that the doctor didn't make any appointments while monsters were eating each other. Input The first line contains single integer _n_ (1u2009β€u2009_n_u2009β€u2009500)xa0β the number of monsters in the initial queue. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009106)xa0β the initial weights of the monsters. The third line contains single integer _k_ (1u2009β€u2009_k_u2009β€u2009_n_)xa0β the number of monsters in the queue after the joke. The fourth line contains _k_ integers _b_1,u2009_b_2,u2009...,u2009_b__k_ (1u2009β€u2009_b__j_u2009β€u20095Β·108)xa0β the weights of the monsters after the joke. Monsters are listed in the order from the beginning of the queue to the end. Output In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line. Otherwise print "YES" (without quotes) in the first line. In the next _n_u2009-u2009_k_ lines print actions in the chronological order. In each line print _x_xa0β the index number of the monster in the current queue which eats and, separated by space, the symbol 'L' if the monster which stays the _x_-th in the queue eats the monster in front of him, or 'R' if the monster which stays the _x_-th in the queue eats the monster behind him. After each eating the queue is enumerated again. When one monster eats another the queue decreases. If there are several answers, print any of them. Note In the first example, initially there were _n_u2009=u20096 monsters, their weights are [1,u20092,u20092,u20092,u20091,u20092] (in order of queue from the first monster to the last monster). The final queue should be [5,u20095]. The following sequence of eatings leads to the final queue: the second monster eats the monster to the left (i.e. the first monster), queue becomes [3,u20092,u20092,u20091,u20092]; the first monster (note, it was the second on the previous step) eats the monster to the right (i.e. the second monster), queue becomes [5,u20092,u20091,u20092]; the fourth monster eats the mosnter to the left (i.e. the third monster), queue becomes [5,u20092,u20093]; the finally, the third monster eats the monster to the left (i.e. the second monster), queue becomes [5,u20095]. Note that for each step the output contains numbers of the monsters in their current order in the queue. | 1,800 | false | true | false | true | false | true | false | false | false | false | 6,862 |
560B | Problem - 560B - 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 constructive algorithms implementation *1200 No tag edit access β Contest materials , and if they cannot, print "NO" (without the quotes). Examples Input 3 2 1 3 2 1 Output YES Input 5 5 3 3 3 3 Output NO Input 4 2 2 3 1 2 Output YES Note That's how we can place the pictures in the first test: And that's how we can do it in the third one. | 1,200 | false | false | true | false | false | true | false | false | false | false | 7,592 |
926C | A camera you have accidentally left in a desert has taken an interesting photo. The photo has a resolution of _n_ pixels width, and each column of this photo is all white or all black. Thus, we can represent the photo as a sequence of _n_ zeros and ones, where 0 means that the corresponding column is all white, and 1 means that the corresponding column is black. You think that this photo can contain a zebra. In this case the whole photo should consist of several (possibly, only one) alternating black and white stripes of equal width. For example, the photo [0,u20090,u20090,u20091,u20091,u20091,u20090,u20090,u20090] can be a photo of zebra, while the photo [0,u20090,u20090,u20091,u20091,u20091,u20091] can not, because the width of the black stripe is 3, while the width of the white stripe is 4. Can the given photo be a photo of zebra or not? Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009100u2009000) β the width of the photo. The second line contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009β€u20091) β the description of the photo. If _a__i_ is zero, the _i_-th column is all black. If _a__i_ is one, then the _i_-th column is all white. Output If the photo can be a photo of zebra, print "YES" (without quotes). Otherwise, print "NO". You can print each letter in any case (upper or lower). Note The first two examples are described in the statements. In the third example all pixels are white, so the photo can be a photo of zebra. In the fourth example the width of the first stripe is equal to three (white color), the width of the second stripe is equal to three (black), and the width of the third stripe is equal to two (white). Thus, not all stripes have equal length, so this photo is not a photo of zebra. | 1,700 | false | false | true | false | false | false | false | false | false | false | 6,003 |
177E2 | The Smart Beaver from ABBYY plans a space travel on an ultramodern spaceship. During the voyage he plans to visit _n_ planets. For planet _i_ _a__i_ is the maximum number of suitcases that an alien tourist is allowed to bring to the planet, and _b__i_ is the number of citizens on the planet. The Smart Beaver is going to bring some presents from ABBYY to the planets he will be visiting. The presents are packed in suitcases, _x_ presents in each. The Beaver will take to the ship exactly _a_1u2009+u2009...u2009+u2009_a__n_ suitcases. As the Beaver lands on the _i_-th planet, he takes _a__i_ suitcases and goes out. On the first day on the planet the Beaver takes a walk and gets to know the citizens. On the second and all subsequent days the Beaver gives presents to the citizens β each of the _b__i_ citizens gets one present per day. The Beaver leaves the planet in the evening of the day when the number of presents left is strictly less than the number of citizens (i.e. as soon as he won't be able to give away the proper number of presents the next day). He leaves the remaining presents at the hotel. The Beaver is going to spend exactly _c_ days traveling. The time spent on flights between the planets is considered to be zero. In how many ways can one choose the positive integer _x_ so that the planned voyage will take exactly _c_ days? Input The first input line contains space-separated integers _n_ and _c_ β the number of planets that the Beaver is going to visit and the number of days he is going to spend traveling, correspondingly. The next _n_ lines contain pairs of space-separated integers _a__i_,u2009_b__i_ (1u2009β€u2009_i_u2009β€u2009_n_) β the number of suitcases he can bring to the _i_-th planet and the number of citizens of the _i_-th planet, correspondingly. The input limitations for getting 30 points are: 1u2009β€u2009_n_u2009β€u2009100 1u2009β€u2009_a__i_u2009β€u2009100 1u2009β€u2009_b__i_u2009β€u2009100 1u2009β€u2009_c_u2009β€u2009100 The input limitations for getting 100 points are: 1u2009β€u2009_n_u2009β€u2009104 0u2009β€u2009_a__i_u2009β€u2009109 1u2009β€u2009_b__i_u2009β€u2009109 1u2009β€u2009_c_u2009β€u2009109 Due to possible overflow, it is recommended to use the 64-bit arithmetic. In some solutions even the 64-bit arithmetic can overflow. So be careful in calculations! Output Print a single number _k_ β the number of ways to choose _x_ so as to travel for exactly _c_ days. If there are infinitely many possible values of _x_, print -1. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. Note In the first example there is only one suitable value _x_u2009=u20095. Then the Beaver takes 1 suitcase with 5 presents to the first planet. Here he spends 2 days: he hangs around on the first day, and he gives away five presents on the second day. He takes 2 suitcases with 10 presents to the second planet. Here he spends 3 days β he gives away 4 presents on the second and the third days and leaves the remaining 2 presents at the hotel. In total, the Beaver spends 5 days traveling. For _x_u2009=u20094 or less the Beaver won't have enough presents for the second day on the first planet, so the voyage will end too soon. For _x_u2009=u20096 and more the Beaver will spend at least one more day on the second planet, and the voyage will take too long. | 1,900 | false | false | false | false | false | false | false | true | false | false | 9,145 |
918B | As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin's task was to add comments to nginx configuration for school's website. The school has _n_ servers. Each server has a name and an ip (names aren't necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we'll assume that an nginx command is of form "command ip;" where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers. Each ip is of form "a.b.c.d" where _a_, _b_, _c_ and _d_ are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has _m_ commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is "command ip;" Dustin has to replace it with "command ip; #name" where name is the name of the server with ip equal to ip. Dustin doesn't know anything about nginx, so he panicked again and his friends asked you to do his task for him. Input The first line of input contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u20091000). The next _n_ lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1u2009β€u2009_name_u2009β€u200910, _name_ only consists of English lowercase letters). It is guaranteed that all ip are distinct. The next _m_ lines contain the commands in the configuration file. Each line is of form "command ip;" (1u2009β€u2009_command_u2009β€u200910, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the _n_ school servers. Output Print _m_ lines, the commands in the configuration file after Dustin did his task. Examples Input 2 2 main 192.168.0.2 replica 192.168.0.1 block 192.168.0.1; proxy 192.168.0.2; Output block 192.168.0.1; #replica proxy 192.168.0.2; #main Input 3 5 google 8.8.8.8 codeforces 212.193.33.27 server 138.197.64.57 redirect 138.197.64.57; block 8.8.8.8; cf 212.193.33.27; unblock 8.8.8.8; check 138.197.64.57; Output redirect 138.197.64.57; #server block 8.8.8.8; #google cf 212.193.33.27; #codeforces unblock 8.8.8.8; #google check 138.197.64.57; #server | 900 | false | false | true | false | false | false | false | false | false | false | 6,057 |
1379A | Acacius is studying strings theory. Today he came with the following problem. You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once? Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc". Occurrence of a string $$$t$$$ of length $$$m$$$ in the string $$$s$$$ of length $$$n$$$ as a substring is a index $$$i$$$ ($$$1 leq i leq n - m + 1$$$) such that string $$$s[i..i+m-1]$$$ consisting of $$$m$$$ consecutive symbols of $$$s$$$ starting from $$$i$$$-th equals to string $$$t$$$. For example string "ababa" has two occurrences of a string "aba" as a substring with $$$i = 1$$$ and $$$i = 3$$$, but there are no occurrences of a string "aba" in the string "acba" as a substring. Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once. Input First line of input contains an integer $$$T$$$ ($$$1 leq T leq 5000$$$), number of test cases. $$$T$$$ pairs of lines with test case descriptions follow. The first line of a test case description contains a single integer $$$n$$$ ($$$7 leq n leq 50$$$), length of a string $$$s$$$. The second line of a test case description contains string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters and question marks. Output For each test case output an answer for it. In case if there is no way to replace question marks in string $$$s$$$ with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No". Otherwise output "Yes" and in the next line output a resulting string consisting of $$$n$$$ lowercase English letters. If there are multiple possible strings, output any. You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer). Example Input 6 7 abacaba 7 ??????? 11 aba?abacaba 11 abacaba?aba 15 asdf???f???qwer 11 abacabacaba Output Yes abacaba Yes abacaba Yes abadabacaba Yes abacabadaba No No Note In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring. In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular. In sixth example there are two occurrences of a string "abacaba" as a substring. | 1,500 | false | false | true | false | false | false | true | false | false | false | 3,778 |
1798E | Let's call an array $$$b_1, b_2, ldots, b_m$$$ a test if $$$b_1 = m - 1$$$. Let's call an array $$$b_1, b_2, ldots, b_m$$$ a multitest if the array $$$b_2, b_3, ldots, b_m$$$ can be split into $$$b_1$$$ non-empty subarrays so that each of these subarrays is a test. Note that each element of the array must be included in exactly one subarray, and the subarrays must consist of consecutive elements. Let's define the function $$$f$$$ from the array $$$b_1, b_2, ldots, b_m$$$ as the minimum number of operations of the form "Replace any $$$b_i$$$ with any non-negative integer $$$x$$$", which needs to be done so that the array $$$b_1, b_2, ldots, b_m$$$ becomes a multitest. You are given an array of positive integers $$$a_1, a_2, ldots, a_n$$$. For each $$$i$$$ from $$$1$$$ to $$$n - 1$$$, find $$$f([a_i, a_{i+1}, ldots, a_n])$$$. Below are some examples of tests and multitests. Tests: $$$[underline{1}, 5]$$$, $$$[underline{2}, 2, 2]$$$, $$$[underline{3}, 4, 1, 1]$$$, $$$[underline{5}, 0, 0, 0, 0, 0]$$$, $$$[underline{7}, 1, 2, 3, 4, 5, 6, 7]$$$, $$$[underline{0}]$$$. These arrays are tests since their first element (underlined) is equal to the length of the array minus one. Multitests: $$$[1, underline{underline{1}, 1}]$$$, $$$[2, underline{underline{3}, 0, 0, 1}, underline{underline{1}, 12}]$$$, $$$[3, underline{underline{2}, 2, 7}, underline{underline{1}, 1}, underline{underline{3}, 4, 4, 4}]$$$, $$$[4, underline{underline{0}}, underline{underline{3}, 1, 7, 9}, underline{underline{4}, 2, 0, 0, 9}, underline{underline{1}, 777}]$$$. Underlined are the subarrays after the split, and double underlined are the first elements of each subarray. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 300,000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 300,000$$$)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 300,000$$$)xa0β elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$300,000$$$. Output For each test case print $$$n - 1$$$ numbersxa0β $$$f([a_i, a_{i+1}, ldots, a_n])$$$ for each $$$i$$$ from $$$1$$$ to $$$n - 1$$$. Examples Input 3 4 1 2 1 7 7 3 1 3 1 2 1 1 4 2 7 1 1 Output 0 1 1 0 1 1 0 1 1 1 1 1 Input 1 19 3 4 1 2 1 7 7 3 1 3 1 2 1 1 4 2 7 1 1 Output 0 0 1 1 1 1 1 1 1 0 1 0 1 0 2 1 1 1 Note In the first test case of the first test the array $$$[1, 2, 1, 7]$$$ is a multitest since the array $$$[2, 1, 7]$$$ is a test. The array $$$[2, 1, 7]$$$ is not a multitest, but after replacing the first number with $$$1$$$, an array $$$[1, 1, 7]$$$ is obtained, which is a multitest. The array $$$[1, 7]$$$ is also not a multitest, but the array $$$[1, 0]$$$ is, so $$$f([1, 7]) = 1$$$. In the second test case of first test, for $$$i = 2$$$, $$$f([a_i, a_{i+1}, ldots, a_n]) = f([1, 3, 1, 2, 1, 1]) = 1$$$, since the array itself is not a multitest, but after replacing the second element with $$$4$$$ you get multitest. In the third test case of first test, for $$$i = 1$$$, $$$f([a_i, a_{i+1}, ldots, a_n]) = f([2, 7, 1, 1]) = 1$$$, since the array itself is not a multitest, but after replacing the second element with $$$0$$$ you get multitest. The second test is an array composed of all the numbers of the first test. Therefore $$$f([a_1, a_2, ldots, a_n])$$$ naturally equals to $$$0$$$. | 2,300 | false | false | false | true | false | false | true | false | false | false | 1,486 |
1997E | Monocarp is playing a computer game. He starts the game being level $$$1$$$. He is about to fight $$$n$$$ monsters, in order from $$$1$$$ to $$$n$$$. The level of the $$$i$$$-th monster is $$$a_i$$$. For each monster in the given order, Monocarp's encounter goes as follows: if Monocarp's level is strictly higher than the monster's level, the monster flees (runs away); otherwise, Monocarp fights the monster. After every $$$k$$$-th fight with a monster (fleeing monsters do not count), Monocarp's level increases by $$$1$$$. So, his level becomes $$$2$$$ after $$$k$$$ monsters he fights, $$$3$$$ after $$$2k$$$ monsters, $$$4$$$ after $$$3k$$$ monsters, and so on. You need to process $$$q$$$ queries of the following form: $$$i~x$$$: will Monocarp fight the $$$i$$$-th monster (or will this monster flee) if the parameter $$$k$$$ is equal to $$$x$$$? Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$)xa0β the number of monsters and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$)xa0β the levels of the monsters. In the $$$j$$$-th of the following $$$q$$$ lines, two integers $$$i$$$ and $$$x$$$ ($$$1 le i, x le n$$$)xa0β the index of the monster and the number of fights required for a level up in the $$$j$$$-th query. Output For each query, output "YES", if Monocarp will fight the $$$i$$$-th monster in this query, and "NO", if the $$$i$$$-th monster flees. Examples Input 4 16 2 1 2 1 1 1 2 1 3 1 4 1 1 2 2 2 3 2 4 2 1 3 2 3 3 3 4 3 1 4 2 4 3 4 4 4 Output YES NO YES NO YES YES YES NO YES YES YES NO YES YES YES YES Input 7 15 1 1 2 1 1 1 1 5 3 2 2 2 2 1 6 5 1 5 5 7 7 3 5 7 4 4 3 2 5 1 2 5 6 4 1 6 1 Output NO YES YES YES NO YES YES YES NO NO YES YES YES NO NO | 2,200 | false | false | true | false | true | false | true | true | false | false | 279 |
1380D | There are $$$n$$$ warriors in a row. The power of the $$$i$$$-th warrior is $$$a_i$$$. All powers are pairwise distinct. You have two types of spells which you may cast: 1. Fireball: you spend $$$x$$$ mana and destroy exactly $$$k$$$ consecutive warriors; 2. Berserk: you spend $$$y$$$ mana, choose two consecutive warriors, and the warrior with greater power destroys the warrior with smaller power. For example, let the powers of warriors be $$$[2, 3, 7, 8, 11, 5, 4]$$$, and $$$k = 3$$$. If you cast Berserk on warriors with powers $$$8$$$ and $$$11$$$, the resulting sequence of powers becomes $$$[2, 3, 7, 11, 5, 4]$$$. Then, for example, if you cast Fireball on consecutive warriors with powers $$$[7, 11, 5]$$$, the resulting sequence of powers becomes $$$[2, 3, 4]$$$. You want to turn the current sequence of warriors powers $$$a_1, a_2, dots, a_n$$$ into $$$b_1, b_2, dots, b_m$$$. Calculate the minimum amount of mana you need to spend on it. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$)xa0β the length of sequence $$$a$$$ and the length of sequence $$$b$$$ respectively. The second line contains three integers $$$x, k, y$$$ ($$$1 le x, y, le 10^9; 1 le k le n$$$)xa0β the cost of fireball, the range of fireball and the cost of berserk respectively. The third line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). It is guaranteed that all integers $$$a_i$$$ are pairwise distinct. The fourth line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 le b_i le n$$$). It is guaranteed that all integers $$$b_i$$$ are pairwise distinct. Output Print the minimum amount of mana for turning the sequnce $$$a_1, a_2, dots, a_n$$$ into $$$b_1, b_2, dots, b_m$$$, or $$$-1$$$ if it is impossible. | 2,000 | true | true | true | false | false | true | false | false | false | false | 3,768 |
448B | Bizon the Champion isn't just a bison. He also is a favorite of the "Bizons" team. At a competition the "Bizons" got the following problem: "You are given two distinct words (strings of English letters), _s_ and _t_. You need to transform word _s_ into word _t_". The task looked simple to the guys because they know the suffix data structures well. Bizon Senior loves suffix automaton. By applying it once to a string, he can remove from this string any single character. Bizon Middle knows suffix array well. By applying it once to a string, he can swap any two characters of this string. The guys do not know anything about the suffix tree, but it can help them do much more. Bizon the Champion wonders whether the "Bizons" can solve the problem. Perhaps, the solution do not require both data structures. Find out whether the guys can solve the problem and if they can, how do they do it? Can they solve it either only with use of suffix automaton or only with use of suffix array or they need both structures? Note that any structure may be used an unlimited number of times, the structures may be used in any order. Input The first line contains a non-empty word _s_. The second line contains a non-empty word _t_. Words _s_ and _t_ are different. Each word consists only of lowercase English letters. Each word contains at most 100 letters. Output In the single line print the answer to the problem. Print "need tree" (without the quotes) if word _s_ cannot be transformed into word _t_ even with use of both suffix array and suffix automaton. Print "automaton" (without the quotes) if you need only the suffix automaton to solve the problem. Print "array" (without the quotes) if you need only the suffix array to solve the problem. Print "both" (without the quotes), if you need both data structures to solve the problem. It's guaranteed that if you can solve the problem only with use of suffix array, then it is impossible to solve it only with use of suffix automaton. This is also true for suffix automaton. Note In the third sample you can act like that: first transform "both" into "oth" by removing the first character using the suffix automaton and then make two swaps of the string using the suffix array and get "hot". | 1,400 | false | false | true | false | false | false | false | false | false | false | 8,048 |
1481D | Your friend Salem is Warawreh's brother and only loves math and geometry problems. He has solved plenty of such problems, but according to Warawreh, in order to graduate from university he has to solve more graph problems. Since Salem is not good with graphs he asked your help with the following problem. You are given a complete directed graph with $$$n$$$ vertices without self-loops. In other words, you have $$$n$$$ vertices and each pair of vertices $$$u$$$ and $$$v$$$ ($$$u eq v$$$) has both directed edges $$$(u, v)$$$ and $$$(v, u)$$$. Every directed edge of the graph is labeled with a single character: either 'a' or 'b' (edges $$$(u, v)$$$ and $$$(v, u)$$$ may have different labels). You are also given an integer $$$m > 0$$$. You should find a path of length $$$m$$$ such that the string obtained by writing out edges' labels when going along the path is a palindrome. The length of the path is the number of edges in it. You can visit the same vertex and the same directed edge any number of times. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 1000$$$; $$$1 leq m leq 10^{5}$$$)xa0β the number of vertices in the graph and desirable length of the palindrome. Each of the next $$$n$$$ lines contains $$$n$$$ characters. The $$$j$$$-th character of the $$$i$$$-th line describes the character on the edge that is going from node $$$i$$$ to node $$$j$$$. Every character is either 'a' or 'b' if $$$i eq j$$$, or '*' if $$$i = j$$$, since the graph doesn't contain self-loops. It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$1000$$$ and the sum of $$$m$$$ doesn't exceed $$$10^5$$$. Output For each test case, if it is possible to find such path, print "YES" and the path itself as a sequence of $$$m + 1$$$ integers: indices of vertices in the path in the appropriate order. If there are several valid paths, print any of them. Otherwise, (if there is no answer) print "NO". Example Input 5 3 1 *ba b*b ab* 3 3 *ba b*b ab* 3 4 *ba b*b ab* 4 6 *aaa b*ba ab*a bba* 2 6 *a b* Output YES 1 2 YES 2 1 3 2 YES 1 3 1 3 1 YES 1 2 1 3 4 1 4 NO Note The graph from the first three test cases is shown below: In the first test case, the answer sequence is $$$[1,2]$$$ which means that the path is: $$$$$$1 xrightarrow{ ext{b}} 2$$$$$$ So the string that is obtained by the given path is b. In the second test case, the answer sequence is $$$[2,1,3,2]$$$ which means that the path is: $$$$$$2 xrightarrow{ ext{b}} 1 xrightarrow{ ext{a}} 3 xrightarrow{ ext{b}} 2$$$$$$ So the string that is obtained by the given path is bab. In the third test case, the answer sequence is $$$[1,3,1,3,1]$$$ which means that the path is: $$$$$$1 xrightarrow{ ext{a}} 3 xrightarrow{ ext{a}} 1 xrightarrow{ ext{a}} 3 xrightarrow{ ext{a}} 1$$$$$$ So the string that is obtained by the given path is aaaa. The string obtained in the fourth test case is abaaba. | 2,000 | false | true | true | false | false | true | true | false | false | true | 3,270 |
1055A | Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home. In the city in which Alice and Bob live, the first metro line is being built. This metro line contains $$$n$$$ stations numbered from $$$1$$$ to $$$n$$$. Bob lives near the station with number $$$1$$$, while Alice lives near the station with number $$$s$$$. The metro line has two tracks. Trains on the first track go from the station $$$1$$$ to the station $$$n$$$ and trains on the second track go in reverse direction. Just after the train arrives to the end of its track, it goes to the depot immediately, so it is impossible to travel on it after that. Some stations are not yet open at all and some are only partially openxa0β for each station and for each track it is known whether the station is closed for that track or not. If a station is closed for some track, all trains going in this track's direction pass the station without stopping on it. When the Bob got the information on opened and closed stations, he found that traveling by metro may be unexpectedly complicated. Help Bob determine whether he can travel to the Alice's home by metro or he should search for some other transport. Input The first line contains two integers $$$n$$$ and $$$s$$$ ($$$2 le s le n le 1000$$$)xa0β the number of stations in the metro and the number of the station where Alice's home is located. Bob lives at station $$$1$$$. Next lines describe information about closed and open stations. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$). If $$$a_i = 1$$$, then the $$$i$$$-th station is open on the first track (that is, in the direction of increasing station numbers). Otherwise the station is closed on the first track. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$b_i = 0$$$ or $$$b_i = 1$$$). If $$$b_i = 1$$$, then the $$$i$$$-th station is open on the second track (that is, in the direction of decreasing station numbers). Otherwise the station is closed on the second track. Output Print "YES" (quotes for clarity) if Bob will be able to commute to the Alice's home by metro and "NO" (quotes for clarity) otherwise. You can print each letter in any case (upper or lower). Examples Input 5 3 1 1 1 1 1 1 1 1 1 1 Input 5 4 1 0 0 0 1 0 1 1 1 1 Input 5 2 0 1 1 1 1 1 1 1 1 1 Note In the first example, all stations are opened, so Bob can simply travel to the station with number $$$3$$$. In the second example, Bob should travel to the station $$$5$$$ first, switch to the second track and travel to the station $$$4$$$ then. In the third example, Bob simply can't enter the train going in the direction of Alice's home. | 900 | false | false | false | false | false | false | false | false | false | true | 5,439 |
41A | Problem - 41A - 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 ") reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc. However, making a mistake during the "translation" is easy. Vasya translated the word _s_ from Berlandish into Birlandish as _t_. Help him: find out if he translated the word correctly. Input The first line contains word _s_, the second line contains word _t_. The words consist of lowercase Latin letters. The input data do not contain unnecessary spaces. The words are not empty and their lengths do not exceed 100 symbols. Output If the word _t_ is a word _s_, written reversely, print YES, otherwise print NO. Examples Input code edoc Output YES Input abb aba Output NO Input code code Output NO | 800 | false | false | true | false | false | false | false | false | false | false | 9,784 |
1313D | Being Santa Claus is very difficult. Sometimes you have to deal with difficult situations. Today Santa Claus came to the holiday and there were $$$m$$$ children lined up in front of him. Let's number them from $$$1$$$ to $$$m$$$. Grandfather Frost knows $$$n$$$ spells. The $$$i$$$-th spell gives a candy to every child whose place is in the $$$[L_i, R_i]$$$ range. Each spell can be used at most once. It is also known that if all spells are used, each child will receive at most $$$k$$$ candies. It is not good for children to eat a lot of sweets, so each child can eat no more than one candy, while the remaining candies will be equally divided between his (or her) Mom and Dad. So it turns out that if a child would be given an even amount of candies (possibly zero), then he (or she) will be unable to eat any candies and will go sad. However, the rest of the children (who received an odd number of candies) will be happy. Help Santa Claus to know the maximum number of children he can make happy by casting some of his spells. Input The first line contains three integers of $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 leq n leq 100,000, 1 leq m leq 10^9, 1 leq k leq 8$$$)xa0β the number of spells, the number of children and the upper limit on the number of candy a child can get if all spells are used, respectively. This is followed by $$$n$$$ lines, each containing integers $$$L_i$$$ and $$$R_i$$$ ($$$1 leq L_i leq R_i leq m$$$)xa0β the parameters of the $$$i$$$ spell. Output Print a single integerxa0β the maximum number of children that Santa can make happy. Note In the first example, Santa should apply the first and third spell. In this case all children will be happy except the third. | 2,500 | false | false | true | true | false | false | false | false | false | false | 4,126 |
212C | A very tense moment: _n_ cowboys stand in a circle and each one points his colt at a neighbor. Each cowboy can point the colt to the person who follows or precedes him in clockwise direction. Human life is worthless, just like in any real western. The picture changes each second! Every second the cowboys analyse the situation and, if a pair of cowboys realize that they aim at each other, they turn around. In a second all such pairs of neighboring cowboys aiming at each other turn around. All actions happen instantaneously and simultaneously in a second. We'll use character "A" to denote a cowboy who aims at his neighbour in the clockwise direction, and character "B" for a cowboy who aims at his neighbour in the counter clockwise direction. Then a string of letters "A" and "B" will denote the circle of cowboys, the record is made from the first of them in a clockwise direction. For example, a circle that looks like "ABBBABBBA" after a second transforms into "BABBBABBA" and a circle that looks like "BABBA" transforms into "ABABB". This picture illustrates how the circle "BABBA" transforms into "ABABB" A second passed and now the cowboys' position is described by string _s_. Your task is to determine the number of possible states that lead to _s_ in a second. Two states are considered distinct if there is a cowboy who aims at his clockwise neighbor in one state and at his counter clockwise neighbor in the other state. Input The input data consists of a single string _s_. Its length is from 3 to 100 characters, inclusive. Line _s_ consists of letters "A" and "B". Output Print the sought number of states. Note In the first sample the possible initial states are "ABBBABBAB" and "ABBBABBBA". In the second sample the possible initial states are "AABBB" and "BABBA". | 2,100 | true | false | false | true | false | false | false | false | false | false | 8,991 |
1617A | You are given strings $$$S$$$ and $$$T$$$, consisting of lowercase English letters. It is guaranteed that $$$T$$$ is a permutation of the string abc. Find string $$$S'$$$, the lexicographically smallest permutation of $$$S$$$ such that $$$T$$$ is not a subsequence of $$$S'$$$. String $$$a$$$ is a permutation of string $$$b$$$ if the number of occurrences of each distinct character is the same in both strings. A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements. 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 Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. Description of the test cases follows. The first line of each test case contains a string $$$S$$$ ($$$1 le S le 100$$$), consisting of lowercase English letters. The second line of each test case contains a string $$$T$$$ that is a permutation of the string abc. (Hence, $$$T = 3$$$). Note that there is no limit on the sum of $$$S$$$ across all test cases. Output For each test case, output a single string $$$S'$$$, the lexicographically smallest permutation of $$$S$$$ such that $$$T$$$ is not a subsequence of $$$S'$$$. Example Input 7 abacaba abc cccba acb dbsic bac abracadabra abc dddddddddddd cba bbc abc ac abc Output aaaacbb abccc bcdis aaaaacbbdrr dddddddddddd bbc ac Note In the first test case, both aaaabbc and aaaabcb are lexicographically smaller than aaaacbb, but they contain abc as a subsequence. In the second test case, abccc is the smallest permutation of cccba and does not contain acb as a subsequence. In the third test case, bcdis is the smallest permutation of dbsic and does not contain bac as a subsequence. | 800 | false | true | false | false | false | true | false | false | true | false | 2,548 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.