contest_id int32 1 2.13k | index stringclasses 62
values | problem_id stringlengths 2 6 | title stringlengths 0 67 | rating int32 0 3.5k | tags stringlengths 0 139 | statement stringlengths 0 6.96k | input_spec stringlengths 0 2.32k | output_spec stringlengths 0 1.52k | note stringlengths 0 5.06k | sample_tests stringlengths 0 1.02k | difficulty_category stringclasses 6
values | tag_count int8 0 11 | statement_length int32 0 6.96k | input_spec_length int16 0 2.32k | output_spec_length int16 0 1.52k | contest_year int16 0 21 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,086 | F | 1086F | F. Forest Fires | 3,500 | math | Berland forest was planted several decades ago in a formation of an infinite grid with a single tree in every cell. Now the trees are grown up and they form a pretty dense structure.So dense, actually, that the fire became a real danger for the forest. This season had been abnormally hot in Berland and some trees got c... | The first line contains two integers \(n\) and \(t\) (\(1 \le n \le 50\), \(0 \le t \le 10^8\)) β the number of trees that initially got caught on fire and the time fire department extinguished the fire, respectively.Each of the next \(n\) lines contains two integers \(x\) and \(y\) (\(-10^8 \le x, y \le 10^8\)) β the ... | Print a single integer β the sum of \(val_{x, y}\) over all \((x, y)\) of burnt trees modulo \(998244353\). | Here are the first three examples. The grey cells have \(val = 0\), the orange cells have \(val = 1\) and the red cells have \(val = 2\). | Input: 1 2 10 11 | Output: 40 | Master | 1 | 1,134 | 723 | 107 | 10 |
715 | D | 715D | D. Create a Maze | 3,100 | constructive algorithms | ZS the Coder loves mazes. Your job is to create one so that he can play with it. A maze consists of n Γ m rooms, and the rooms are arranged in n rows (numbered from the top to the bottom starting from 1) and m columns (numbered from the left to the right starting from 1). The room in the i-th row and j-th column is den... | The first and only line of the input contains a single integer T (1 β€ T β€ 1018), the difficulty of the required maze. | The first line should contain two integers n and m (1 β€ n, m β€ 50) β the number of rows and columns of the maze respectively.The next line should contain a single integer k (0 β€ k β€ 300) β the number of locked doors in the maze.Then, k lines describing locked doors should follow. Each of them should contain four intege... | Here are how the sample input and output looks like. The colored arrows denotes all the possible paths while a red cross denotes a locked door.In the first sample case: In the second sample case: | Input: 3 | Output: 3 20 | Master | 1 | 1,559 | 117 | 725 | 7 |
31 | B | 31B | B. Sysadmin Bob | 1,500 | greedy; implementation; strings | Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters. Bob is a system administrator in Β«BersoftΒ» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, ... | The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters Β«@Β». | If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of the... | Input: a@aa@a | Output: a@a,a@a | Medium | 3 | 807 | 194 | 322 | 0 | |
588 | A | 588A | A. Duff and Meat | 900 | greedy | Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat. There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., an and... | The first line of input contains integer n (1 β€ n β€ 105), the number of days.In the next n lines, i-th line contains two integers ai and pi (1 β€ ai, pi β€ 100), the amount of meat Duff needs and the cost of meat in that day. | Print the minimum money needed to keep Duff happy for n days, in one line. | In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day. | Input: 31 32 23 1 | Output: 10 | Beginner | 1 | 580 | 223 | 74 | 5 |
1,672 | A | 1672A | A. Log Chopping | 800 | games; implementation; math | There are \(n\) logs, the \(i\)-th log has a length of \(a_i\) meters. Since chopping logs is tiring work, errorgorn and maomao90 have decided to play a game.errorgorn and maomao90 will take turns chopping the logs with errorgorn chopping first. On his turn, the player will pick a log and chop it into \(2\) pieces. If ... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 50\)) β the number of logs.The second line of each test c... | For each test case, print ""errorgorn"" if errorgorn wins or ""maomao90"" if maomao90 wins. (Output without quotes). | In the first test case, errorgorn will be the winner. An optimal move is to chop the log of length \(4\) into \(2\) logs of length \(2\). After this there will only be \(4\) logs of length \(2\) and \(1\) log of length \(1\).After this, the only move any player can do is to chop any log of length \(2\) into \(2\) logs ... | Input: 242 4 2 111 | Output: errorgorn maomao90 | Beginner | 3 | 808 | 492 | 116 | 16 |
1,881 | G | 1881G | G. Anya and the Mysterious String | 2,000 | binary search; data structures | Anya received a string \(s\) of length \(n\) brought from Rome. The string \(s\) consists of lowercase Latin letters and at first glance does not raise any suspicions. An instruction was attached to the string.Start of the instruction.A palindrome is a string that reads the same from left to right and right to left. Fo... | The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) - the number of test cases.The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) - the length of the string \(s\) and the number of queries.The second line of each... | For each query of the second type, output ""YES"" if the substring \([l \ldots r]\) of string \(s\) is beautiful, otherwise output ""NO"".You can output ""YES"" and ""NO"" in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive answers). | In the first test case of the first test, the following happens: tedubcyyxopz: the string edub is beautiful; tedubcyyxopz \(\to\) tedwdeaaxopz; tedwdeaaxopz: the string tedwdea is not beautiful as it contains the palindrome edwde; tedwdeaaxopz \(\to\) terkreaaxopz; terkreaaxopz \(\to\) terkreaaarsz; terkreaaarsz \(\to\... | Input: 512 8tedubcyyxopz2 2 51 4 8 22 1 71 3 5 401 9 11 31 10 10 92 4 102 10 1210 4ubnxwwgzjt2 4 102 10 101 6 10 82 7 711 3hntcxfxyhtu1 4 6 12 4 101 4 10 2113 2yxhlmzfhqctir1 5 9 31 8 13 152 3bp1 1 2 151 1 2 181 2 2 1000000000 | Output: YES NO NO YES NO YES YES YES | Hard | 2 | 1,676 | 770 | 289 | 18 |
1,175 | C | 1175C | C. Electrification | 1,600 | binary search; brute force; greedy | At first, there was a legend related to the name of the problem, but now it's just a formal statement.You are given \(n\) points \(a_1, a_2, \dots, a_n\) on the \(OX\) axis. Now you are asked to find such an integer point \(x\) on \(OX\) axis that \(f_k(x)\) is minimal possible.The function \(f_k(x)\) can be described ... | The first line contains single integer \(T\) (\( 1 \le T \le 2 \cdot 10^5\)) β number of queries. Next \(2 \cdot T\) lines contain descriptions of queries. All queries are independent. The first line of each query contains two integers \(n\), \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le k < n\)) β the number of points ... | Print \(T\) integers β corresponding points \(x\) which have minimal possible value of \(f_k(x)\). If there are multiple answers you can print any of them. | Input: 3 3 2 1 2 5 2 1 1 1000000000 1 0 4 | Output: 3 500000000 4 | Medium | 3 | 593 | 542 | 155 | 11 | |
1,713 | B | 1713B | B. Optimal Reduction | 1,000 | constructive algorithms; sortings | Consider an array \(a\) of \(n\) positive integers.You may perform the following operation: select two indices \(l\) and \(r\) (\(1 \leq l \leq r \leq n\)), then decrease all elements \(a_l, a_{l + 1}, \dots, a_r\) by \(1\). Let's call \(f(a)\) the minimum number of operations needed to change array \(a\) into an array... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β descrip... | For each test case, print ""YES"" (without quotes) if for all permutations \(b\) of \(a\), \(f(a) \leq f(b)\) is true, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive response). | In the first test case, we can change all elements to \(0\) in \(5\) operations. It can be shown that no permutation of \([2, 3, 5, 4]\) requires less than \(5\) operations to change all elements to \(0\).In the third test case, we need \(5\) operations to change all elements to \(0\), while \([2, 3, 3, 1]\) only needs... | Input: 342 3 5 431 2 343 1 3 2 | Output: YES YES NO | Beginner | 2 | 667 | 428 | 297 | 17 |
2,042 | B | 2042B | B. Game with Colored Marbles | 900 | games; greedy | Alice and Bob play a game. There are \(n\) marbles, the \(i\)-th of them has color \(c_i\). The players take turns; Alice goes first, then Bob, then Alice again, then Bob again, and so on.During their turn, a player must take one of the remaining marbles and remove it from the game. If there are no marbles left (all \(... | The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case consists of two lines: the first line contains one integer \(n\) (\(1 \le n \le 1000\)) β the number of marbles; the second line contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le n\)) β the colors ... | For each test case, print one integer β Alice's score at the end of the game, assuming that both players play optimally. | In the second test case of the example, the colors of all marbles are distinct, so, no matter how the players act, Alice receives \(4\) points for having all marbles of two colors, and no marbles of the third color.In the third test case of the example, the colors of all marbles are the same, so, no matter how the play... | Input: 351 3 1 3 431 2 344 4 4 4 | Output: 4 4 1 | Beginner | 2 | 1,567 | 434 | 120 | 20 |
1,883 | F | 1883F | F. You Are So Beautiful | 1,400 | data structures | You are given an array of integers \(a_1, a_2, \ldots, a_n\). Calculate the number of subarrays of this array \(1 \leq l \leq r \leq n\), such that: The array \(b = [a_l, a_{l+1}, \ldots, a_r]\) occurs in the array \(a\) as a subsequence exactly once. In other words, there is exactly one way to select a set of indices ... | Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. This is followed by their description.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)) β the size of the array \(a\).The second line of each t... | For each test case, output the number of suitable subarrays. | In the first test case, there is exactly one subarray \((1, 1)\) that suits us.In the second test case, there is exactly one subarray \((1, 2)\) that suits us. Subarrays \((1, 1)\) and \((2, 2)\) do not suit us, as the subsequence \([1]\) occurs twice in the array.In the third test case, all subarrays except \((1, 1)\)... | Input: 61121 131 2 142 3 2 154 5 4 5 4101 7 7 2 3 4 3 2 1 100 | Output: 1 1 4 7 4 28 | Easy | 1 | 438 | 498 | 60 | 18 |
409 | E | 409E | E. Dome | 1,800 | *special | The input contains a single floating-point number x with exactly 6 decimal places (0 < x < 5). | Output two integers separated by a single space. Each integer should be between 1 and 10, inclusive. If several solutions exist, output any of them. Solution will exist for all tests. | Input: 1.200000 | Output: 3 2 | Medium | 1 | 0 | 94 | 183 | 4 | ||
1,440 | A | 1440A | A. Buy the String | 800 | implementation; math | You are given four integers \(n\), \(c_0\), \(c_1\) and \(h\) and a binary string \(s\) of length \(n\).A binary string is a string consisting of characters \(0\) and \(1\).You can change any character of the string \(s\) (the string should be still binary after the change). You should pay \(h\) coins for each change.A... | The first line contains a single integer \(t\) (\(1 \leq t \leq 10\)) β the number of test cases. Next \(2t\) lines contain descriptions of test cases.The first line of the description of each test case contains four integers \(n\), \(c_{0}\), \(c_{1}\), \(h\) (\(1 \leq n, c_{0}, c_{1}, h \leq 1000\)).The second line o... | For each test case print a single integer β the minimum number of coins needed to buy the string. | In the first test case, you can buy all characters and pay \(3\) coins, because both characters \(0\) and \(1\) costs \(1\) coin.In the second test case, you can firstly change \(2\)-nd and \(4\)-th symbols of the string from \(1\) to \(0\) and pay \(2\) coins for that. Your string will be \(00000\). After that, you ca... | Input: 6 3 1 1 1 100 5 10 100 1 01010 5 10 1 1 11111 5 1 10 1 11111 12 2 1 10 101110110101 2 100 1 10 00 | Output: 3 52 5 10 16 22 | Beginner | 2 | 606 | 405 | 97 | 14 |
498 | E | 498E | E. Stairs and Lines | 2,700 | dp; matrices | You are given a figure on a grid representing stairs consisting of 7 steps. The width of the stair on height i is wi squares. Formally, the figure is created by consecutively joining rectangles of size wi Γ i so that the wi sides lie on one straight line. Thus, for example, if all wi = 1, the figure will look like that... | The single line of the input contains 7 numbers w1, w2, ..., w7 (0 β€ wi β€ 105). It is guaranteed that at least one of the wi's isn't equal to zero. | In the single line of the output display a single number β the answer to the problem modulo 109 + 7. | All the possible ways of painting the third sample are given below: | Input: 0 1 0 0 0 0 0 | Output: 1 | Master | 2 | 724 | 147 | 100 | 4 |
340 | D | 340D | D. Bubble Sort Graph | 1,500 | binary search; data structures; dp | Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear a... | The first line of the input contains an integer n (2 β€ n β€ 105). The next line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ n). | Output a single integer β the answer to the problem. | Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2]. | Input: 33 1 2 | Output: 2 | Medium | 3 | 1,118 | 137 | 52 | 3 |
1,737 | E | 1737E | E. Ela Goes Hiking | 2,500 | combinatorics; dp; math; probabilities | Ela likes to go hiking a lot. She loves nature and exploring the various creatures it offers. One day, she saw a strange type of ant, with a cannibalistic feature. More specifically, an ant would eat any ants that it sees which is smaller than it.Curious about this feature from a new creature, Ela ain't furious. She co... | 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 only line of each test contains an integer \(n\) (\(1 \le n \le 10^6\)) β the number of ants in the experiment.It is guaranteed that the sum of \(n\) in al... | For each test, print \(n\) lines. \(i\)-th line contains a single number that denotes the survival probability of the \(i\)-th ant in the line modulo \(10^9 + 7\). | Here is the example of \(6\) ants moving on the branch. An ant's movement will be denoted by either a character \(L\) or \(R\). Initially, the pack of ants on the branch will move as \(RLRRLR\). Here's how the behavior of the pack demonstrated: Initially, the ants are positioned as above. After a while, the ant with in... | Input: 3452 | Output: 0 250000002 250000002 500000004 0 250000002 250000002 250000002 250000002 0 1 | Expert | 4 | 2,957 | 353 | 163 | 17 |
676 | A | 676A | A. Nicholas and Permutation | 800 | constructive algorithms; implementation | Nicholas has an array a that contains n distinct integers from 1 to n. In other words, Nicholas has a permutation of size n.Nicholas want the minimum element (integer 1) and the maximum element (integer n) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance b... | The first line of the input contains a single integer n (2 β€ n β€ 100) β the size of the permutation.The second line of the input contains n distinct integers a1, a2, ..., an (1 β€ ai β€ n), where ai is equal to the element at the i-th position. | Print a single integer β the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap. | In the first sample, one may obtain the optimal answer by swapping elements 1 and 2.In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2.In the third sample, the distance between the minimum and the maximum elements is already maximum possible, ... | Input: 54 5 1 3 2 | Output: 3 | Beginner | 2 | 476 | 242 | 152 | 6 |
1,404 | C | 1404C | C. Fixed Point Removal | 2,300 | binary search; constructive algorithms; data structures; greedy; two pointers | Let \(a_1, \ldots, a_n\) be an array of \(n\) positive integers. In one operation, you can choose an index \(i\) such that \(a_i = i\), and remove \(a_i\) from the array (after the removal, the remaining parts are concatenated).The weight of \(a\) is defined as the maximum number of elements you can remove.You must ans... | The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 3 \cdot 10^5\)) β the length of the array and the number of queries.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \leq a_i \leq n\)) β elements of the array.The \(i\)-th of the next \(q\) lines contains two integers \(x... | Print \(q\) lines, \(i\)-th line should contain a single integer β the answer to the \(i\)-th query. | Explanation of the first query:After making first \(x = 3\) and last \(y = 1\) elements impossible to remove, \(a\) becomes \([\times, \times, \times, 9, 5, 4, 6, 5, 7, 8, 3, 11, \times]\) (we represent \(14\) as \(\times\) for clarity).Here is a strategy that removes \(5\) elements (the element removed is colored in r... | Input: 13 5 2 2 3 9 5 4 6 5 7 8 3 11 13 3 1 0 0 2 4 5 0 0 12 | Output: 5 11 6 1 0 | Expert | 5 | 530 | 366 | 100 | 14 |
1,486 | A | 1486A | A. Shifting Stacks | 900 | greedy; implementation | You have \(n\) stacks of blocks. The \(i\)-th stack contains \(h_i\) blocks and it's height is the number of blocks in it. In one move you can take a block from the \(i\)-th stack (if there is at least one block) and put it to the \(i + 1\)-th stack. Can you make the sequence of heights strictly increasing?Note that th... | First line contains a single integer \(t\) \((1 \leq t \leq 10^4)\) β the number of test cases.The first line of each test case contains a single integer \(n\) \((1 \leq n \leq 100)\). The second line of each test case contains \(n\) integers \(h_i\) \((0 \leq h_i \leq 10^9)\) β starting heights of the stacks.It's guar... | For each test case output YES if you can make the sequence of heights strictly increasing and NO otherwise.You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). | In the first test case there is no need to make any moves, the sequence of heights is already increasing.In the second test case we need to move one block from the first stack to the second. Then the heights become \(0\) \(1\).In the third test case we could move one block from the first stack to the second and then fr... | Input: 6 2 1 2 2 1 0 3 4 4 4 2 0 0 3 0 1 0 4 1000000000 1000000000 1000000000 1000000000 | Output: YES YES YES NO NO YES | Beginner | 2 | 412 | 378 | 221 | 14 |
749 | E | 749E | E. Inversions After Shuffle | 2,400 | data structures; probabilities | You are given a permutation of integers from 1 to n. Exactly once you apply the following operation to this permutation: pick a random segment and shuffle its elements. Formally: Pick a random segment (continuous subsequence) from l to r. All segments are equiprobable. Let k = r - l + 1, i.e. the length of the chosen s... | The first line contains a single integer n (1 β€ n β€ 100 000) β the length of the permutation.The second line contains n distinct integers from 1 to n β elements of the permutation. | Print one real value β the expected number of inversions. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 9. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if . | Input: 32 3 1 | Output: 1.916666666666666666666666666667 | Expert | 2 | 988 | 180 | 291 | 7 | |
268 | C | 268C | C. Beautiful Sets of Points | 1,500 | constructive algorithms; implementation | Manao has invented a new mathematical term β a beautiful set of points. He calls a set of points on a plane beautiful if it meets the following conditions: The coordinates of each point in the set are integers. For any two points from the set, the distance between them is a non-integer. Consider all points (x, y) which... | The single line contains two space-separated integers n and m (1 β€ n, m β€ 100). | In the first line print a single integer β the size k of the found beautiful set. In each of the next k lines print a pair of space-separated integers β the x- and y- coordinates, respectively, of a point from the set.If there are several optimal solutions, you may print any of them. | Consider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) β , between (1, 2) and (2, 0) β . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution. | Input: 2 2 | Output: 30 11 22 0 | Medium | 2 | 462 | 79 | 284 | 2 |
1,270 | H | 1270H | H. Number of Components | 3,300 | data structures | Suppose that we have an array of \(n\) distinct numbers \(a_1, a_2, \dots, a_n\). Let's build a graph on \(n\) vertices as follows: for every pair of vertices \(i < j\) let's connect \(i\) and \(j\) with an edge, if \(a_i < a_j\). Let's define weight of the array to be the number of connected components in this graph. ... | The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 5 \cdot 10^5\)) β the size of the array and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)) β the initial array.Each of the next \(q\) lines contains two integers \(pos\) and \(x\) (\(1... | After each query, output the weight of the array. | After the first query array looks like \([25, 40, 30, 20, 10]\), the weight is equal to \(3\).After the second query array looks like \([25, 40, 45, 20, 10]\), the weight is still equal to \(3\).After the third query array looks like \([48, 40, 45, 20, 10]\), the weight is equal to \(4\). | Input: 5 3 50 40 30 20 10 1 25 3 45 1 48 | Output: 3 3 4 | Master | 1 | 634 | 507 | 49 | 12 |
1,627 | F | 1627F | F. Not Splitting | 2,700 | geometry; graphs; greedy; implementation; shortest paths | There is a \(k \times k\) grid, where \(k\) is even. The square in row \(r\) and column \(c\) is denoted by \((r,c)\). Two squares \((r_1, c_1)\) and \((r_2, c_2)\) are considered adjacent if \(\lvert r_1 - r_2 \rvert + \lvert c_1 - c_2 \rvert = 1\).An array of adjacent pairs of squares is called strong if it is possib... | The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two space-separated integers \(n\) and \(k\) (\(1 \leq n \leq 10^5\); \(2 \leq k \leq 500\), \(k\) i... | For each test case, output a single integer β the size of the largest strong subsequence of \(a\). | In the first test case, the array \(a\) is not good, but if we take the subsequence \([a_1, a_2, a_3, a_4, a_5, a_6, a_8]\), then the square can be split as shown in the statement.In the second test case, we can take the subsequence consisting of the last four elements of \(a\) and cut the square with a horizontal line... | Input: 38 41 2 1 32 2 2 33 2 3 34 2 4 31 4 2 42 1 3 12 2 3 24 1 4 27 21 1 1 22 1 2 21 1 1 21 1 2 11 2 2 21 1 2 11 2 2 21 63 3 3 4 | Output: 7 4 1 | Master | 5 | 963 | 946 | 98 | 16 |
618 | B | 618B | B. Guess the Permutation | 1,100 | constructive algorithms | Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integer i from 1 to n.Bob gave you all the values of ai, j that he wrote... | The first line of the input will contain a single integer n (2 β€ n β€ 50).The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the informa... | Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them. | In the first case, the answer can be {1, 2} or {2, 1}.In the second case, another possible answer is {2, 4, 5, 1, 3}. | Input: 20 11 0 | Output: 2 1 | Easy | 1 | 542 | 331 | 165 | 6 |
1,243 | B1 | 1243B1 | B1. Character Swap (Easy Version) | 1,000 | strings | This problem is different from the hard version. In this version Ujan makes exactly one exchange. You can hack this problem only if you solve both problems.After struggling and failing many times, Ujan decided to try to clean up his house again. He decided to get his strings in order first.Ujan has two distinct strings... | The first line contains a single integer \(k\) (\(1 \leq k \leq 10\)), the number of test cases.For each of the test cases, the first line contains a single integer \(n\) (\(2 \leq n \leq 10^4\)), the length of the strings \(s\) and \(t\). Each of the next two lines contains the strings \(s\) and \(t\), each having len... | For each test case, output ""Yes"" if Ujan can make the two strings equal and ""No"" otherwise.You can print each letter in any case (upper or lower). | In the first test case, Ujan can swap characters \(s_1\) and \(t_4\), obtaining the word ""house"".In the second test case, it is not possible to make the strings equal using exactly one swap of \(s_i\) and \(t_j\). | Input: 4 5 souse houhe 3 cat dog 2 aa az 3 abc bca | Output: Yes No No No | Beginner | 1 | 775 | 438 | 150 | 12 |
2,013 | F2 | 2013F2 | F2. Game in Tree (Hard Version) | 3,500 | binary search; data structures; trees | This is the hard version of the problem. In this version, it is not guaranteed that \(u = v\). You can make hacks only if both versions of the problem are solved.Alice and Bob are playing a fun game on a tree. This game is played on a tree with \(n\) vertices, numbered from \(1\) to \(n\). Recall that a tree with \(n\)... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.Each of the following \(n ... | For each test case, output \(m\) lines.In the \(i\)-th line, print the winner of the game if Alice starts at vertex \(1\) and Bob starts at vertex \(p_i\). Print ""Alice"" (without quotes) if Alice wins, or ""Bob"" (without quotes) otherwise. | Tree from the first example. In the first test case, the path will be (\(2,3\)). If Bob starts at vertex \(2\), Alice will not be able to move anywhere on her first turn and will lose.However, if Bob starts at vertex \(3\), Alice will move to vertex \(2\), and Bob will have no remaining vertices to visit and will lose. | Input: 331 22 32 361 21 32 42 51 64 541 21 32 42 4 | Output: Bob Alice Alice Bob Alice Bob Alice | Master | 3 | 1,048 | 773 | 242 | 20 |
1,896 | B | 1896B | B. AB Flipping | 900 | greedy; strings; two pointers | You are given a string \(s\) of length \(n\) consisting of characters \(\texttt{A}\) and \(\texttt{B}\). You are allowed to do the following operation: Choose an index \(1 \le i \le n - 1\) such that \(s_i = \texttt{A}\) and \(s_{i + 1} = \texttt{B}\). Then, swap \(s_i\) and \(s_{i+1}\). You are only allowed to do the ... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2\cdot 10^5\)) β the length of string \(s\).The second line of each test case conta... | For each test case, print a single integer containing the maximum number of operations that you can carry out. | In the first test case, we can do the operation exactly once for \(i=1\) as \(s_1=\texttt{A}\) and \(s_2=\texttt{B}\).In the second test case, it can be proven that it is not possible to do an operation.In the third test case, we can do an operation on \(i=2\) to form \(\texttt{ABAB}\), then another operation on \(i=3\... | Input: 32AB4BBBA4AABB | Output: 1 0 3 | Beginner | 3 | 488 | 475 | 110 | 18 |
34 | D | 34D | D. Road Map | 1,600 | dfs and similar; graphs | There are n cities in Berland. Each city has its index β an integer number from 1 to n. The capital has index r1. All the roads in Berland are two-way. The road system is such that there is exactly one path from the capital to each city, i.e. the road map looks like a tree. In Berland's chronicles the road map is kept ... | The first line contains three space-separated integers n, r1, r2 (2 β€ n β€ 5Β·104, 1 β€ r1 β r2 β€ n) β amount of cities in Berland, index of the old capital and index of the new one, correspondingly.The following line contains n - 1 space-separated integers β the old representation of the road map. For each city, apart fr... | Output n - 1 numbers β new representation of the road map in the same format. | Input: 3 2 32 2 | Output: 2 3 | Medium | 2 | 756 | 477 | 77 | 0 | |
1,163 | C1 | 1163C1 | C1. Power Transmission (Easy Edition) | 1,900 | brute force; geometry | This problem is same as the next one, but has smaller constraints.It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consis... | The first line contains a single integer \(n\) (\(2 \le n \le 50\)) β the number of electric poles.Each of the following \(n\) lines contains two integers \(x_i\), \(y_i\) (\(-10^4 \le x_i, y_i \le 10^4\)) β the coordinates of the poles.It is guaranteed that all of these \(n\) points are distinct. | Print a single integer β the number of pairs of wires that are intersecting. | In the first example: In the second example: Note that the three poles \((0, 0)\), \((0, 2)\) and \((0, 4)\) are connected by a single wire.In the third example: | Input: 4 0 0 1 1 0 3 1 2 | Output: 14 | Hard | 2 | 1,198 | 298 | 76 | 11 |
1,498 | F | 1498F | F. Christmas Game | 2,500 | bitmasks; data structures; dfs and similar; dp; games; math; trees | Alice and Bob are going to celebrate Christmas by playing a game with a tree of presents. The tree has \(n\) nodes (numbered \(1\) to \(n\), with some node \(r\) as its root). There are \(a_i\) presents are hanging from the \(i\)-th node.Before beginning the game, a special integer \(k\) is chosen. The game proceeds as... | The first line contains two space-separated integers \(n\) and \(k\) \((3 \le n \le 10^5, 1 \le k \le 20)\).The next \(n-1\) lines each contain two integers \(x\) and \(y\) \((1 \le x, y \le n, x \neq y)\), denoting an undirected edge between the two nodes \(x\) and \(y\). These edges form a tree of \(n\) nodes.The nex... | Output \(n\) integers, where the \(i\)-th integer is \(1\) if Alice wins the game when the tree is rooted at node \(i\), or \(0\) otherwise. | Let us calculate the answer for sample input with root node as 1 and as 2.Root node 1Alice always wins in this case. One possible gameplay between Alice and Bob is: Alice moves one present from node 4 to node 3. Bob moves four presents from node 5 to node 2. Alice moves four presents from node 2 to node 1. Bob moves th... | Input: 5 1 1 2 1 3 5 2 4 3 0 3 2 4 4 | Output: 1 0 0 1 1 | Expert | 7 | 1,392 | 417 | 140 | 14 |
1,170 | F | 1170F | F. Wheels | 0 | *special; binary search; greedy | Polycarp has \(n\) wheels and a car with \(m\) slots for wheels. The initial pressure in the \(i\)-th wheel is \(a_i\).Polycarp's goal is to take exactly \(m\) wheels among the given \(n\) wheels and equalize the pressure in them (then he can put these wheels in a car and use it for driving). In one minute he can decre... | The first line of the input contains three integers \(n, m\) and \(k\) (\(1 \le m \le n \le 2 \cdot 10^5, 0 \le k \le 10^9\)) β the number of wheels, the number of slots for wheels in a car and the number of times Polycarp can increase by \(1\) the pressure in a wheel.The second line of the input contains \(n\) integer... | Print one integer β the minimum number of minutes Polycarp needs to spend to equalize the pressure in at least \(m\) wheels among the given \(n\) wheels. | Input: 6 6 7 6 15 16 20 1 5 | Output: 39 | Beginner | 3 | 638 | 425 | 153 | 11 | |
933 | B | 933B | B. A Determined Cleanup | 2,000 | math | In order to put away old things and welcome a fresh new year, a thorough cleaning of the house is a must.Little Tommy finds an old polynomial and cleaned it up by taking it modulo another. But now he regrets doing this...Given two integers p and k, find a polynomial f(x) with non-negative integer coefficients strictly ... | The only line of input contains two space-separated integers p and k (1 β€ p β€ 1018, 2 β€ k β€ 2 000). | If the polynomial does not exist, print a single integer -1, or output two lines otherwise.In the first line print a non-negative integer d β the number of coefficients in the polynomial.In the second line print d space-separated integers a0, a1, ..., ad - 1, describing a polynomial fulfilling the given requirements. Y... | In the first example, f(x) = x6 + x5 + x4 + x = (x5 - x4 + 3x3 - 6x2 + 12x - 23)Β·(x + 2) + 46.In the second example, f(x) = x2 + 205x + 92 = (x - 9)Β·(x + 214) + 2018. | Input: 46 2 | Output: 70 1 0 0 1 1 1 | Hard | 1 | 484 | 99 | 451 | 9 |
1,945 | C | 1945C | C. Left and Right Houses | 1,200 | brute force | In the village of Letovo, there are \(n\) houses. The villagers decided to build a big road that will divide the village into left and right sides. Each resident wants to live on either the right or the left side of the street, which is described as a sequence \(a_1, a_2, \dots, a_n\), where \(a_j = 0\) if the resident... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 3\cdot 10^5\)). The next line of each test case contains a string \(a\) ... | For each test case, output a single number \(i\) β the position of the house after which the road should be laid (if it should be laid before the first house, output \(0\)). We can show that the answer always exists. | Let's consider the first example of input data.If we lay the road after the first house, there will be one house \(a_1 = 1\) on the left side of the street, the resident of which would like to live on the right side of the street. Then \(0\) out of \(1\) residents on the even side will be satisfied with the choice, whi... | Input: 731016010111601100130003110300141100 | Output: 2 3 2 3 0 1 0 | Easy | 1 | 1,959 | 463 | 216 | 19 |
274 | A | 274A | A. k-Multiple Free Set | 1,500 | binary search; greedy; sortings | A k-multiple free set is a set of integers where there is no pair of integers where one is equal to another integer multiplied by k. That is, there are no two integers x and y (x < y) from the set, such that y = xΒ·k.You're given a set of n distinct positive integers. Your task is to find the size of it's largest k-mult... | The first line of the input contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109). The next line contains a list of n distinct positive integers a1, a2, ..., an (1 β€ ai β€ 109).All the numbers in the lines are separated by single spaces. | On the only line of the output print the size of the largest k-multiple free subset of {a1, a2, ..., an}. | In the sample input one of the possible maximum 2-multiple free subsets is {4, 5, 6}. | Input: 6 22 3 6 5 4 10 | Output: 3 | Medium | 3 | 337 | 239 | 105 | 2 |
225 | E | 225E | E. Unsolvable | 2,100 | math; number theory | Consider the following equation: where sign [a] represents the integer part of number a.Let's find all integer z (z > 0), for which this equation is unsolvable in positive integers. The phrase ""unsolvable in positive integers"" means that there are no such positive integers x and y (x, y > 0), for which the given abov... | The first line contains a single integer n (1 β€ n β€ 40). | Print a single integer β the number zn modulo 1000000007 (109 + 7). It is guaranteed that the answer exists. | Input: 1 | Output: 1 | Hard | 2 | 479 | 56 | 108 | 2 | |
1,163 | E | 1163E | E. Magical Permutation | 2,400 | bitmasks; brute force; constructive algorithms; data structures; graphs; math | Kuro has just learned about permutations and he is really excited to create a new permutation type. He has chosen \(n\) distinct positive integers and put all of them in a set \(S\). Now he defines a magical permutation to be: A permutation of integers from \(0\) to \(2^x - 1\), where \(x\) is a non-negative integer. T... | The first line contains the integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of elements in the set \(S\).The next line contains \(n\) distinct integers \(S_1, S_2, \ldots, S_n\) (\(1 \leq S_i \leq 2 \cdot 10^5\)) β the elements in the set \(S\). | In the first line print the largest non-negative integer \(x\), such that there is a magical permutation of integers from \(0\) to \(2^x - 1\).Then print \(2^x\) integers describing a magical permutation of integers from \(0\) to \(2^x - 1\). If there are multiple such magical permutations, print any of them. | In the first example, \(0, 1, 3, 2\) is a magical permutation since: \(0 \oplus 1 = 1 \in S\) \(1 \oplus 3 = 2 \in S\) \(3 \oplus 2 = 1 \in S\)Where \(\oplus\) denotes bitwise xor operation. | Input: 3 1 2 3 | Output: 2 0 1 3 2 | Expert | 6 | 814 | 257 | 310 | 11 |
2,014 | C | 2014C | C. Robin Hood in Town | 1,100 | binary search; greedy; math | In Sherwood, we judge a man not by his wealth, but by his merit.Look around, the rich are getting richer, and the poor are getting poorer. We need to take from the rich and give to the poor. We need Robin Hood!There are \(n\) people living in the town. Just now, the wealth of the \(i\)-th person was \(a_i\) gold. But g... | The first line of input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2\cdot10^5\)) β the total population.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^6\))... | For each test case, output one integer β the minimum number of gold that the richest person must find for Robin Hood to appear. If it is impossible, output \(-1\) instead. | In the first test case, it is impossible for a single person to be unhappy.In the second test case, there is always \(1\) happy person (the richest).In the third test case, no additional gold are required, so the answer is \(0\).In the fourth test case, after adding \(15\) gold, the average wealth becomes \(\frac{25}{4... | Input: 61222 1931 3 2041 2 3 451 2 3 4 561 2 1 1 1 25 | Output: -1 -1 0 15 16 0 | Easy | 3 | 1,073 | 443 | 171 | 20 |
1,672 | C | 1672C | C. Unequal Array | 1,100 | constructive algorithms; greedy; implementation | You are given an array \(a\) of length \(n\). We define the equality of the array as the number of indices \(1 \le i \le n - 1\) such that \(a_i = a_{i + 1}\). We are allowed to do the following operation: Select two integers \(i\) and \(x\) such that \(1 \le i \le n - 1\) and \(1 \le x \le 10^9\). Then, set \(a_i\) an... | Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2 \le n \le 2 \cdot 10 ^ 5\)) β the length of array \(a\).The second line of... | For each test case, print the minimum number of operations needed. | In the first test case, we can select \(i=2\) and \(x=2\) to form \([1, 2, 2, 1, 1]\). Then, we can select \(i=3\) and \(x=3\) to form \([1, 2, 3, 3, 1]\).In the second test case, we can select \(i=3\) and \(x=100\) to form \([2, 1, 100, 100, 2]\). | Input: 451 1 1 1 152 1 1 1 261 1 2 3 3 461 2 1 4 5 4 | Output: 2 1 2 0 | Easy | 3 | 470 | 528 | 66 | 16 |
42 | A | 42A | A. Guilty β to the kitchen! | 1,400 | greedy; implementation | It's a very unfortunate day for Volodya today. He got bad mark in algebra and was therefore forced to do some work in the kitchen, namely to cook borscht (traditional Russian soup). This should also improve his algebra skills.According to the borscht recipe it consists of n ingredients that have to be mixed in proporti... | The first line of the input contains two space-separated integers n and V (1 β€ n β€ 20, 1 β€ V β€ 10000). The next line contains n space-separated integers ai (1 β€ ai β€ 100). Finally, the last line contains n space-separated integers bi (0 β€ bi β€ 100). | Your program should output just one real number β the volume of soup that Volodya will cook. Your answer must have a relative or absolute error less than 10 - 4. | Input: 1 100140 | Output: 40.0 | Easy | 2 | 795 | 249 | 161 | 0 | |
469 | B | 469B | B. Chat Online | 1,300 | implementation | Little X and Little Z are good friends. They always chat online. But both of them have schedules.Little Z has fixed schedule. He always online at any moment of time between a1 and b1, between a2 and b2, ..., between ap and bp (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time... | The first line contains four space-separated integers p, q, l, r (1 β€ p, q β€ 50; 0 β€ l β€ r β€ 1000).Each of the next p lines contains two space-separated integers ai, bi (0 β€ ai < bi β€ 1000). Each of the next q lines contains two space-separated integers cj, dj (0 β€ cj < dj β€ 1000).It's guaranteed that bi < ai + 1 and d... | Output a single integer β the number of moments of time from the segment [l, r] which suit for online conversation. | Input: 1 1 0 42 30 1 | Output: 3 | Easy | 1 | 1,056 | 353 | 115 | 4 | |
1,090 | G | 1090G | 2,500 | games; implementation | Expert | 2 | 0 | 0 | 0 | 10 | ||||||
1,926 | A | 1926A | A. Vlad and the Best of Five | 800 | implementation | Vladislav has a string of length \(5\), whose characters are each either \(\texttt{A}\) or \(\texttt{B}\).Which letter appears most frequently: \(\texttt{A}\) or \(\texttt{B}\)? | The first line of the input contains an integer \(t\) (\(1 \leq t \leq 32\)) β the number of test cases.The only line of each test case contains a string of length \(5\) consisting of letters \(\texttt{A}\) and \(\texttt{B}\).All \(t\) strings in a test are different (distinct). | For each test case, output one letter (\(\texttt{A}\) or \(\texttt{B}\)) denoting the character that appears most frequently in the string. | Input: 8ABABBABABABBBABAAAAABBBBBBABAAAAAABBAAAA | Output: B A B A B A A A | Beginner | 1 | 177 | 279 | 139 | 19 | |
432 | B | 432B | B. Football Kit | 1,200 | brute force; greedy; implementation | Consider a football tournament where n teams participate. Each team has two football kits: for home games, and for away games. The kit for home games of the i-th team has color xi and the kit for away games of this team has color yi (xi β yi).In the tournament, each team plays exactly one home game and exactly one away... | The first line contains a single integer n (2 β€ n β€ 105) β the number of teams. Next n lines contain the description of the teams. The i-th line contains two space-separated numbers xi, yi (1 β€ xi, yi β€ 105; xi β yi) β the color numbers for the home and away kits of the i-th team. | For each team, print on a single line two space-separated integers β the number of games this team is going to play in home and away kits, correspondingly. Print the answers for the teams in the order they appeared in the input. | Input: 21 22 1 | Output: 2 02 0 | Easy | 3 | 765 | 281 | 228 | 4 | |
2,004 | D | 2004D | D. Colored Portals | 1,600 | binary search; brute force; data structures; graphs; greedy; implementation; shortest paths | There are \(n\) cities located on a straight line. The cities are numbered from \(1\) to \(n\).Portals are used to move between cities. There are \(4\) colors of portals: blue, green, red, and yellow. Each city has portals of two different colors. You can move from city \(i\) to city \(j\) if they have portals of the s... | 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 two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β the number of cities and the number of queries, respectively.The second line contains \(n\) strings of the following... | For each query, print a single integer β the minimum cost to move from city \(x\) to city \(y\) (or \(-1\) if it is impossible). | Input: 24 5BR BR GY GR1 23 14 41 44 22 1BG RY1 2 | Output: 1 4 0 3 2 -1 | Medium | 7 | 564 | 851 | 128 | 20 | |
1,528 | A | 1528A | A. Parsa's Humongous Tree | 1,600 | dfs and similar; divide and conquer; dp; greedy; trees | Parsa has a humongous tree on \(n\) vertices.On each vertex \(v\) he has written two integers \(l_v\) and \(r_v\).To make Parsa's tree look even more majestic, Nima wants to assign a number \(a_v\) (\(l_v \le a_v \le r_v\)) to each vertex \(v\) such that the beauty of Parsa's tree is maximized.Nima's sense of the beaut... | The first line contains an integer \(t\) \((1\le t\le 250)\) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) \((2\le n\le 10^5)\) β the number of vertices in Parsa's tree.The \(i\)-th of the following \(n\) lines contains two integer... | For each test case print the maximum possible beauty for Parsa's tree. | The trees in the example: In the first test case, one possible assignment is \(a = \{1, 8\}\) which results in \(|1 - 8| = 7\).In the second test case, one of the possible assignments is \(a = \{1, 5, 9\}\) which results in a beauty of \(|1 - 5| + |5 - 9| = 8\) | Input: 3 2 1 6 3 8 1 2 3 1 3 4 6 7 9 1 2 2 3 6 3 14 12 20 12 19 2 12 10 17 3 17 3 2 6 5 1 5 2 6 4 6 | Output: 7 8 62 | Medium | 5 | 585 | 699 | 70 | 15 |
1,031 | D | 1031D | D. Minimum path | 1,900 | greedy | You are given a matrix of size \(n \times n\) filled with lowercase English letters. You can change no more than \(k\) letters in this matrix.Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the strin... | The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2000\), \(0 \le k \le n^2\)) β the size of the matrix and the number of letters you can change.Each of the next \(n\) lines contains a string of \(n\) lowercase English letters denoting one row of the matrix. | Output the lexicographically smallest string that can be associated with some valid path after changing no more than \(k\) letters in the matrix. | In the first sample test case it is possible to change letters 'b' in cells \((2, 1)\) and \((3, 1)\) to 'a', then the minimum path contains cells \((1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)\). The first coordinate corresponds to the row and the second coordinate corresponds to the column. | Input: 4 2abcdbcdebcadbcde | Output: aaabcde | Hard | 1 | 704 | 276 | 145 | 10 |
1,173 | A | 1173A | A. Nauuo and Votes | 800 | greedy | Nauuo is a girl who loves writing comments.One day, she posted a comment on Codeforces, wondering whether she would get upvotes or downvotes.It's known that there were \(x\) persons who would upvote, \(y\) persons who would downvote, and there were also another \(z\) persons who would vote, but you don't know whether t... | The only line contains three integers \(x\), \(y\), \(z\) (\(0\le x,y,z\le100\)), corresponding to the number of persons who would upvote, downvote or unknown. | If there is only one possible result, print the result : ""+"", ""-"" or ""0"".Otherwise, print ""?"" to report that the result is uncertain. | In the first example, Nauuo would definitely get three upvotes and seven downvotes, so the only possible result is ""-"".In the second example, no matter the person unknown downvotes or upvotes, Nauuo would get more upvotes than downvotes. So the only possible result is ""+"".In the third example, Nauuo would definitel... | Input: 3 7 0 | Output: - | Beginner | 1 | 984 | 159 | 141 | 11 |
2,043 | A | 2043A | A. Coin Transformation | 800 | brute force; math | Initially, you have a coin with value \(n\). You can perform the following operation any number of times (possibly zero): transform one coin with value \(x\), where \(x\) is greater than \(3\) (\(x>3\)), into two coins with value \(\lfloor \frac{x}{4} \rfloor\). What is the maximum number of coins you can have after pe... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of one line containing one integer \(n\) (\(1 \le n \le 10^{18}\)). | For each test case, print one integer β the maximum number of coins you can have after performing the operation any number of times. | In the first example, you have a coin of value \(1\), and you can't do anything with it. So, the answer is \(1\).In the second example, you can transform a coin of value \(5\) into two coins with value \(1\).In the third example, you can transform a coin of value \(16\) into two coins with value \(4\). Each of the resu... | Input: 415161000000000000000000 | Output: 1 2 4 536870912 | Beginner | 2 | 364 | 183 | 132 | 20 |
76 | F | 76F | F. Tourist | 2,300 | binary search; data structures; dp | Tourist walks along the X axis. He can choose either of two directions and any speed not exceeding V. He can also stand without moving anywhere. He knows from newspapers that at time t1 in the point with coordinate x1 an interesting event will occur, at time t2 in the point with coordinate x2 β another one, and so on u... | The first line of input contains single integer number N (1 β€ N β€ 100000) β number of interesting events. The following N lines contain two integers xi and ti β coordinate and time of the i-th event. The last line of the input contains integer V β maximum speed of the tourist. All xi will be within range - 2Β·108 β€ xi β€... | The only line of the output should contain two space-sepatated integers β maximum number of events tourist can visit in he starts moving from point 0 at time 0, and maximum number of events tourist can visit if he chooses the initial point for himself. | Input: 3-1 142 740 82 | Output: 1 2 | Expert | 3 | 731 | 541 | 252 | 0 | |
496 | B | 496B | B. Secret Combination | 1,500 | brute force; constructive algorithms; implementation | You got a box with a combination lock. The lock has a display showing n digits. There are two buttons on the box, each button changes digits on the display. You have quickly discovered that the first button adds 1 to all the digits (all digits 9 become digits 0), and the second button shifts all the digits on the displ... | The first line contains a single integer n (1 β€ n β€ 1000) β the number of digits on the display.The second line contains n digits β the initial state of the display. | Print a single line containing n digits β the desired state of the display containing the smallest possible number. | Input: 3579 | Output: 024 | Medium | 3 | 828 | 165 | 115 | 4 | |
453 | E | 453E | E. Little Pony and Lord Tirek | 3,100 | data structures | Lord Tirek is a centaur and the main antagonist in the season four finale episodes in the series ""My Little Pony: Friendship Is Magic"". In ""Twilight's Kingdom"" (Part 1), Tirek escapes from Tartarus and drains magic from ponies to grow stronger. The core skill of Tirek is called Absorb Mana. It takes all mana from a... | The first line contains an integer n (1 β€ n β€ 105) β the number of ponies. Each of the next n lines contains three integers si, mi, ri (0 β€ si β€ mi β€ 105; 0 β€ ri β€ 105), describing a pony. The next line contains an integer m (1 β€ m β€ 105) β the number of instructions. Each of the next m lines contains three integers ti... | For each instruction, output a single line which contains a single integer, the total mana absorbed in this instruction. | Every pony starts with zero mana. For the first instruction, each pony has 5 mana, so you get 25 mana in total and each pony has 0 mana after the first instruction.For the second instruction, pony 3 has 14 mana and other ponies have mana equal to their mi. | Input: 50 10 10 12 10 20 10 12 10 10 125 1 519 1 5 | Output: 2558 | Master | 1 | 934 | 487 | 120 | 4 |
1,297 | I | 1297I | I. Falling Blocks | 0 | *special; data structures | Recently, Polycarp has invented a new mobile game with falling blocks.In the game, \(n\) blocks are falling down, one at a time, towards a flat surface with length \(d\) units. Each block can be represented as a rectangle with coordinates from \(l_i\) to \(r_i\) and unit height, dropped downwards from very high up. A b... | The first line contains two integers \(n\) and \(d\) (\(1 \le n, d \le 10^5\)) β the number of falling blocks and the length of the flat surface.The \(i\)-th of the following \(n\) lines contains integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le d\)) β the coordinates of the \(i\)-th block. | Output \(n\) integers. The \(i\)-th integer should be the number of blocks that will be left after the \(i\)-th block falls. | The first example is explained above.In the second example, this is what happens after each block falls: Block \(1\) will stick to the flat surface. Block \(2\) will stick to the flat surface. Block \(3\) will stick to blocks \(1\) and \(2\). Note that block \(3\) will not vaporize block \(2\) because it does not cover... | Input: 3 3 1 2 2 3 1 3 | Output: 1 2 1 | Beginner | 2 | 1,348 | 295 | 124 | 12 |
1,490 | E | 1490E | E. Accidental Victory | 1,400 | binary search; data structures; greedy | A championship is held in Berland, in which \(n\) players participate. The player with the number \(i\) has \(a_i\) (\(a_i \ge 1\)) tokens.The championship consists of \(n-1\) games, which are played according to the following rules: in each game, two random players with non-zero tokens are selected; the player with mo... | The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case consists of one positive integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of players in the championship.The second line of each test case contains \(n\) po... | For each test case, print the number of players who have a nonzero probability of winning the championship. On the next line print the numbers of these players in increasing order. Players are numbered starting from one in the order in which they appear in the input. | Input: 2 4 1 2 4 3 5 1 1 1 1 1 | Output: 3 2 3 4 5 1 2 3 4 5 | Easy | 3 | 1,590 | 519 | 267 | 14 | |
678 | D | 678D | D. Iterated Linear Function | 1,700 | math; number theory | Consider a linear function f(x) = Ax + B. Let's define g(0)(x) = x and g(n)(x) = f(g(n - 1)(x)) for n > 0. For the given integer values A, B, n and x find the value of g(n)(x) modulo 109 + 7. | The only line contains four integers A, B, n and x (1 β€ A, B, x β€ 109, 1 β€ n β€ 1018) β the parameters from the problem statement.Note that the given value n can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. | Print the only integer s β the value g(n)(x) modulo 109 + 7. | Input: 3 4 1 1 | Output: 7 | Medium | 2 | 191 | 314 | 60 | 6 | |
1,321 | A | 1321A | A. Contest for Robots | 900 | greedy | Polycarp is preparing the first programming contest for robots. There are \(n\) problems in it, and a lot of robots are going to participate in it. Each robot solving the problem \(i\) gets \(p_i\) points, and the score of each robot in the competition is calculated as the sum of \(p_i\) over all problems \(i\) solved ... | The first line contains one integer \(n\) (\(1 \le n \le 100\)) β the number of problems.The second line contains \(n\) integers \(r_1\), \(r_2\), ..., \(r_n\) (\(0 \le r_i \le 1\)). \(r_i = 1\) means that the ""Robo-Coder Inc."" robot will solve the \(i\)-th problem, \(r_i = 0\) means that it won't solve the \(i\)-th ... | If ""Robo-Coder Inc."" robot cannot outperform the ""BionicSolver Industries"" robot by any means, print one integer \(-1\).Otherwise, print the minimum possible value of \(\max \limits_{i = 1}^{n} p_i\), if all values of \(p_i\) are set in such a way that the ""Robo-Coder Inc."" robot gets strictly more points than th... | In the first example, one of the valid score assignments is \(p = [3, 1, 3, 1, 1]\). Then the ""Robo-Coder"" gets \(7\) points, the ""BionicSolver"" β \(6\) points.In the second example, both robots get \(0\) points, and the score distribution does not matter.In the third example, both robots solve all problems, so the... | Input: 5 1 1 1 0 0 0 1 1 1 1 | Output: 3 | Beginner | 1 | 1,465 | 574 | 356 | 13 |
1,700 | E | 1700E | E. Serega the Pirate | 2,600 | brute force; constructive algorithms | Little pirate Serega robbed a ship with puzzles of different kinds. Among all kinds, he liked only one, the hardest.A puzzle is a table of \(n\) rows and \(m\) columns, whose cells contain each number from \(1\) to \(n \cdot m\) exactly once.To solve a puzzle, you have to find a sequence of cells in the table, such tha... | In the first line there are two whole positive numbers \(n, m\) (\(1 \leq n\cdot m \leq 400\,000\)) β table dimensions.In the next \(n\) lines there are \(m\) integer numbers \(a_{i1}, a_{i2}, \dots, a_{im}\) (\(1 \le a_{ij} \le nm\)). It is guaranteed that every number from \(1\) to \(nm\) occurs exactly once in the t... | Let \(a\) be the minimum number of moves to make the puzzle solvable.If \(a = 0\), print \(0\).If \(a = 1\), print \(1\) and the number of valid swaps.If \(a \ge 2\), print \(2\). | In the first example the sequence \((1, 2), (1, 1), (1, 2), (1, 3), (2, 3), (3, 3)\), \((2, 3), (1, 3), (1, 2), (1, 1), (2, 1), (2, 2), (3, 2), (3, 1)\) solves the puzzle, so the answer is \(0\).The puzzle in the second example can't be solved, but it's solvable after any of three swaps of cells with values \((1, 5), (... | Input: 3 3 2 1 3 6 7 4 9 8 5 | Output: 0 | Expert | 2 | 1,278 | 325 | 179 | 17 |
1,692 | C | 1692C | C. Where's the Bishop? | 800 | implementation | Mihai has an \(8 \times 8\) chessboard whose rows are numbered from \(1\) to \(8\) from top to bottom and whose columns are numbered from \(1\) to \(8\) from left to right.Mihai has placed exactly one bishop on the chessboard. The bishop is not placed on the edges of the board. (In other words, the row and column of th... | The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 36\)) β the number of test cases. The description of test cases follows. There is an empty line before each test case.Each test case consists of \(8\) lines, each containing \(8\) characters. Each of these characters is either '#' or '.', deno... | For each test case, output two integers \(r\) and \(c\) (\(2 \leq r, c \leq 7\)) β the row and column of the bishop. The input is generated in such a way that there is always exactly one possible location of the bishop that is not on the edge of the board. | The first test case is pictured in the statement. Since the bishop lies in the intersection row \(4\) and column \(3\), the correct output is 4 3. | Input: 3.....#..#...#....#.#......#......#.#....#...#........#........#.#.#......#......#.#........#........#........#........#........#.#.....#..#...#....#.#......#......#.#....#...#..#.....##....... | Output: 4 3 2 2 4 5 | Beginner | 1 | 770 | 391 | 256 | 16 |
766 | E | 766E | E. Mahmoud and a xor trip | 2,100 | bitmasks; constructive algorithms; data structures; dfs and similar; dp; math; trees | Mahmoud and Ehab live in a country with n cities numbered from 1 to n and connected by n - 1 undirected roads. It's guaranteed that you can reach any city from any other using these roads. Each city has a number ai attached to it.We define the distance from city x to city y as the xor of numbers attached to the cities ... | The first line contains integer n (1 β€ n β€ 105) β the number of cities in Mahmoud and Ehab's country.Then the second line contains n integers a1, a2, ..., an (0 β€ ai β€ 106) which represent the numbers attached to the cities. Integer ai is attached to the city i.Each of the next n - 1 lines contains two integers u and v... | Output one number denoting the total distance between all pairs of cities. | A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://... | Input: 31 2 31 22 3 | Output: 10 | Hard | 7 | 1,007 | 487 | 74 | 7 |
598 | A | 598A | A. Tricky Sum | 900 | math | In this problem you are to calculate the sum of all integers from 1 to n, but you should take all powers of two with minus in the sum.For example, for n = 4 the sum is equal to - 1 - 2 + 3 - 4 = - 4, because 1, 2 and 4 are 20, 21 and 22 respectively.Calculate the answer for t values of n. | The first line of the input contains a single integer t (1 β€ t β€ 100) β the number of values of n to be processed.Each of next t lines contains a single integer n (1 β€ n β€ 109). | Print the requested sum for each of t integers n given in the input. | The answer for the first sample is explained in the statement. | Input: 241000000000 | Output: -4499999998352516354 | Beginner | 1 | 289 | 177 | 68 | 5 |
1,534 | D | 1534D | D. Lost Tree | 1,800 | constructive algorithms; interactive; trees | This is an interactive problem.Little Dormi was faced with an awkward problem at the carnival: he has to guess the edges of an unweighted tree of \(n\) nodes! The nodes of the tree are numbered from \(1\) to \(n\).The game master only allows him to ask one type of question: Little Dormi picks a node \(r\) (\(1 \le r \l... | The first line of input contains the integer \(n\) (\(2 \le n \le 2\,000\)), the number of nodes in the tree.You will then begin interaction. | When your program has found the tree, first output a line consisting of a single ""!"" followed by \(n-1\) lines each with two space separated integers \(a\) and \(b\), denoting an edge connecting nodes \(a\) and \(b\) (\(1 \le a, b \le n\)). Once you are done, terminate your program normally immediately after flushing... | Here is the tree from the first example. Notice that the edges can be output in any order.Additionally, here are the answers for querying every single node in example \(1\): \(1\): \([0,1,2,2]\) \(2\): \([1,0,1,1]\) \(3\): \([2,1,0,2]\) \(4\): \([2,1,2,0]\)Below is the tree from the second example interaction. Lastly, ... | Input: 4 0 1 2 2 1 0 1 1 | Output: ? 1 ? 2 ! 4 2 1 2 2 3 | Medium | 3 | 967 | 141 | 483 | 15 |
1,085 | A | 1085A | A. Right-Left Cipher | 800 | implementation; strings | Polycarp loves ciphers. He has invented his own cipher called Right-Left.Right-Left cipher is used for strings. To encrypt the string \(s=s_{1}s_{2} \dots s_{n}\) Polycarp uses the following algorithm: he writes down \(s_1\), he appends the current word with \(s_2\) (i.e. writes down \(s_2\) to the right of the current... | The only line of the input contains \(t\) β the result of encryption of some string \(s\). It contains only lowercase Latin letters. The length of \(t\) is between \(1\) and \(50\), inclusive. | Print such string \(s\) that after encryption it equals \(t\). | Input: ncteho | Output: techno | Beginner | 2 | 1,006 | 192 | 62 | 10 | |
251 | A | 251A | A. Points on Line | 1,300 | binary search; combinatorics; two pointers | Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two farthest of them doesn't exceed d.Note that the order of the points inside the group of three chosen points ... | The first line contains two integers: n and d (1 β€ n β€ 105; 1 β€ d β€ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 β the x-coordinates of the points that Petya has got.It is guaranteed that the coordinates of the points in the input strictly increase. | Print a single integer β the number of groups of three points, where the distance between two farthest points doesn't exceed d.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | In the first sample any group of three points meets our conditions.In the seconds sample only 2 groups of three points meet our conditions: {-3, -2, -1} and {-2, -1, 0}.In the third sample only one group does: {1, 10, 20}. | Input: 4 31 2 3 4 | Output: 4 | Easy | 3 | 335 | 301 | 273 | 2 |
1,949 | I | 1949I | I. Disks | 1,800 | dfs and similar; geometry; graph matchings; graphs | You are given \(n\) disks in the plane. The center of each disk has integer coordinates, and the radius of each disk is a positive integer. No two disks overlap in a region of positive area, but it is possible for disks to be tangent to each other.Your task is to determine whether it is possible to change the radii of ... | The first line contains an integer \(n\) (\(1\le n \le 1000\)) β the number of disks.The next \(n\) lines contain three integers each. The \(i\)-th of such lines contains \(x_i\), \(y_i\) (\(-10^9 \leq x_i, y_i \leq 10^9\)), and \(r_i\) (\(1 \leq r_i \leq 10^9\)) β the coordinates of the center, and the radius, of the ... | Print \(\texttt{YES}\) if it is possible to change the radii in the desired manner. Otherwise, print \(\texttt{NO}\). | In the first sample, one can decrease the radii of the first and third disk by \(0.5\), and increase the radius of the second disk by \(0.5\). This way, the sum of all radii decreases by \(0.5\). The situation before and after changing the radii is depicted below. First sample (left) and a valid way to change the radii... | Input: 50 2 10 0 14 -3 411 0 311 5 2 | Output: YES | Medium | 4 | 618 | 334 | 117 | 19 |
283 | A | 283A | A. Cows and Sequence | 1,600 | constructive algorithms; data structures; implementation | Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: Add the integer xi to the first ai elements of the sequence. Append an integer ki to the end of the sequence. (And henc... | The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 β€ ti β€ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| β€ 103; 1 β€ ai). If ti = 2, it... | Output n lines each containing the average of the numbers in the sequence after the corresponding operation.The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6. | In the second sample, the sequence becomes | Input: 52 132 32 13 | Output: 0.5000000.0000001.5000001.3333331.500000 | Medium | 3 | 654 | 573 | 202 | 2 |
130 | A | 130A | A. Hexagonal numbers | 900 | *special; implementation | Hexagonal numbers are figurate numbers which can be calculated using the formula hn = 2n2 - n. You are given n; calculate n-th hexagonal number. | The only line of input contains an integer n (1 β€ n β€ 100). | Output the n-th hexagonal number. | Input: 2 | Output: 6 | Beginner | 2 | 144 | 59 | 33 | 1 | |
187 | B | 187B | B. AlgoRace | 1,800 | dp; shortest paths | PMP is getting a warrior. He is practicing a lot, but the results are not acceptable yet. This time instead of programming contests, he decided to compete in a car racing to increase the spirit of victory. He decides to choose a competition that also exhibits algorithmic features.AlgoRace is a special league of car rac... | The first line contains three space-separated integers n, m, r (2 β€ n β€ 60, 1 β€ m β€ 60, 1 β€ r β€ 105) β the number of cities, the number of different types of cars and the number of rounds in the competition, correspondingly.Next m sets of n Γ n matrices of integers between 0 to 106 (inclusive) will follow β describing ... | For each round you should print the minimum required time to complete the round in a single line. | In the first sample, in all rounds PMP goes from city #1 to city #2, then city #3 and finally city #4. But the sequences of types of the cars he uses are (1, 2, 1) in the first round and (1, 2, 2) in the second round. In the third round, although he can change his car three times, he uses the same strategy as the first... | Input: 4 2 30 1 5 62 0 3 61 3 0 16 6 7 00 3 5 62 0 1 61 3 0 26 6 7 01 4 21 4 11 4 3 | Output: 343 | Medium | 2 | 1,324 | 860 | 97 | 1 |
2,062 | G | 2062G | G. Permutation Factory | 3,500 | flows; geometry; graph matchings; graphs | You are given two permutations \(p_1,p_2,\ldots,p_n\) and \(q_1,q_2,\ldots,q_n\) of length \(n\). In one operation, you can select two integers \(1\leq i,j\leq n,i\neq j\) and swap \(p_i\) and \(p_j\). The cost of the operation is \(\min (|i-j|,|p_i-p_j|)\).Find the minimum cost to make \(p_i = q_i\) hold for all \(1\l... | The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of input test cases.The first line of each test case contains one integer \(n\) (\(2 \le n \le 100\)) β the length of permutations \(p\) and \(q\).The second line contains \(n\) integers \(p_1,p_2,\ldots,p_n\) (\(1\leq p_i\leq... | For each test case, output the total number of operations \(k\) (\(0\le k\le n^2\)) on the first line. Then output \(k\) lines, each containing two integers \(i,j\) (\(1\le i,j\le n\), \(i\neq j\)) representing an operation to swap \(p_i\) and \(p_j\) in order.It can be shown that no optimal operation sequence has a le... | In the second test case, you can swap \(p_1,p_3\) costing \(\min(|1-3|,|1-3|)=2\). Then \(p\) equals \(q\) with a cost of \(2\).In the third test case, you can perform the following operations:Initially, \(p=[2,1,4,3]\). Swap \(p_1,p_4\) costing \(\min(|1-4|,|2-3|)=1\), resulting in \(p=[3,1,4,2]\). Swap \(p_2,p_4\) co... | Input: 422 12 131 2 33 2 142 1 4 34 2 3 151 4 3 2 55 2 3 4 1 | Output: 0 1 1 3 3 1 4 2 4 1 3 4 1 2 4 5 2 5 1 4 | Master | 4 | 721 | 710 | 346 | 20 |
1,898 | C | 1898C | C. Colorful Grid | 1,700 | constructive algorithms | Elena has a grid formed by \(n\) horizontal lines and \(m\) vertical lines. The horizontal lines are numbered by integers from \(1\) to \(n\) from top to bottom. The vertical lines are numbered by integers from \(1\) to \(m\) from left to right. For each \(x\) and \(y\) (\(1 \leq x \leq n\), \(1 \leq y \leq m\)), the n... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 32\)). The description of test cases follows.The only line of each test case contains three integers \(n\), \(m\), and \(k\) (\(3 \leq n,m \leq 16\), \(1 \leq k \leq 10^9\)) β the dimensions of the grid and t... | For each test case, output ""NO"" if it is not possible to color each of the \(n(m-1)+(n-1)m\) segments in blue or red color, so that there exists a walk of length \(k+1\) satisfying the condition from the statement.Otherwise, output in the first line ""YES"", and then provide the required coloring.In each of the first... | In the first test case, one of the correct answers is shown in the picture below. The color-alternating walk of length \(12\) is highlighted. In the second and the third test cases, it can be shown that there is no coloring satisfying the condition from the statement. | Input: 54 5 113 3 23 4 10000000003 3 125884 4 8 | Output: YES R R B B R R R R B B B R R R B B R B B R B R B B B B B B R R R NO NO YES R B B B B R B B R R B B YES B B R R B R B R R R R B B R R B B B B B B R R R | Medium | 1 | 1,681 | 375 | 1,156 | 18 |
370 | A | 370A | A. Rook, Bishop and King | 1,100 | graphs; math; shortest paths | Little Petya is learning to play chess. He has already learned how to move a king, a rook and a bishop. Let us remind you the rules of moving chess pieces. A chessboard is 64 square fields organized into an 8 Γ 8 table. A field is represented by a pair of integers (r, c) β the number of the row and the number of the co... | The input contains four integers r1, c1, r2, c2 (1 β€ r1, c1, r2, c2 β€ 8) β the coordinates of the starting and the final field. The starting field doesn't coincide with the final one.You can assume that the chessboard rows are numbered from top to bottom 1 through 8, and the columns are numbered from left to right 1 th... | Print three space-separated integers: the minimum number of moves the rook, the bishop and the king (in this order) is needed to move from field (r1, c1) to field (r2, c2). If a piece cannot make such a move, print a 0 instead of the corresponding number. | Input: 4 3 1 6 | Output: 2 1 3 | Easy | 3 | 1,007 | 328 | 255 | 3 | |
1,300 | B | 1300B | B. Assigning to Classes | 1,000 | greedy; implementation; sortings | Reminder: the median of the array \([a_1, a_2, \dots, a_{2k+1}]\) of odd number of elements is defined as follows: let \([b_1, b_2, \dots, b_{2k+1}]\) be the elements of the array in the sorted order. Then median of this array is equal to \(b_{k+1}\).There are \(2n\) students, the \(i\)-th student has skill level \(a_i... | Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of students halved.The second line of each test case conta... | For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes. | In the first test, there is only one way to partition students β one in each class. The absolute difference of the skill levels will be \(|1 - 1| = 0\).In the second test, one of the possible partitions is to make the first class of students with skill levels \([6, 4, 2]\), so that the skill level of the first class wi... | Input: 3 1 1 1 3 6 5 4 1 2 3 5 13 4 20 13 2 5 8 3 17 16 | Output: 0 1 5 | Beginner | 3 | 969 | 505 | 135 | 13 |
398 | D | 398D | D. Instant Messanger | 0 | data structures | User ainta decided to make a new instant messenger called ""aintalk"". With aintalk, each user can chat with other people. User ainta made the prototype of some functions to implement this thing. login(u): User u logins into aintalk and becomes online. logout(u): User u logouts and becomes offline. add_friend(u, v): Us... | The first line contains three space-separated integers n, m and q (1 β€ n β€ 50000; 1 β€ m β€ 150000; 1 β€ q β€ 250000) β the number of users, the number of pairs of friends, and the number of queries.The second line contains an integer o (1 β€ o β€ n) β the number of online users at the beginning. The third line contains o sp... | For each count_online_friends(u) query, print the required answer in a single line. | Input: 5 2 9141 33 4C 3A 2 5O 1D 1 3A 1 2A 4 2C 2F 4C 2 | Output: 121 | Beginner | 1 | 983 | 1,360 | 83 | 3 | |
1,090 | M | 1090M | M. The Pleasant Walk | 1,000 | implementation | There are \(n\) houses along the road where Anya lives, each one is painted in one of \(k\) possible colors.Anya likes walking along this road, but she doesn't like when two adjacent houses at the road have the same color. She wants to select a long segment of the road such that no two adjacent houses have the same col... | The first line contains two integers \(n\) and \(k\) β the number of houses and the number of colors (\(1 \le n \le 100\,000\), \(1 \le k \le 100\,000\)).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) β the colors of the houses along the road (\(1 \le a_i \le k\)). | Output a single integer β the maximum number of houses on the road segment having no two adjacent houses of the same color. | In the example, the longest segment without neighboring houses of the same color is from the house 4 to the house 7. The colors of the houses are \([3, 2, 1, 2]\) and its length is 4 houses. | Input: 8 3 1 2 3 3 2 1 2 2 | Output: 4 | Beginner | 1 | 377 | 282 | 123 | 10 |
1,408 | F | 1408F | F. Two Different | 2,300 | constructive algorithms; divide and conquer | You are given an integer \(n\).You should find a list of pairs \((x_1, y_1)\), \((x_2, y_2)\), ..., \((x_q, y_q)\) (\(1 \leq x_i, y_i \leq n\)) satisfying the following condition.Let's consider some function \(f: \mathbb{N} \times \mathbb{N} \to \mathbb{N}\) (we define \(\mathbb{N}\) as the set of positive integers). I... | The single line contains a single integer \(n\) (\(1 \leq n \leq 15\,000\)). | In the first line print \(q\) (\(0 \leq q \leq 5 \cdot 10^5\)) β the number of pairs.In each of the next \(q\) lines print two integers. In the \(i\)-th line print \(x_i\), \(y_i\) (\(1 \leq x_i, y_i \leq n\)).The condition described in the statement should be satisfied.If there exists multiple answers you can print an... | In the first example, after performing the only operation the array \(a\) will be \([f(a_1, a_2), f(a_1, a_2), a_3]\). It will always have at most two different numbers.In the second example, after performing two operations the array \(a\) will be \([f(a_1, a_2), f(a_1, a_2), f(a_3, a_4), f(a_3, a_4)]\). It will always... | Input: 3 | Output: 1 1 2 | Expert | 2 | 1,130 | 76 | 330 | 14 |
1,701 | D | 1701D | D. Permutation Restoration | 1,900 | binary search; data structures; greedy; math; sortings; two pointers | Monocarp had a permutation \(a\) of \(n\) integers \(1\), \(2\), ..., \(n\) (a permutation is an array where each element from \(1\) to \(n\) occurs exactly once).Then Monocarp calculated an array of integers \(b\) of size \(n\), where \(b_i = \left\lfloor \frac{i}{a_i} \right\rfloor\). For example, if the permutation ... | The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(0 \le b_i \le n\)).Additional constrains on the input: the sum ... | For each test case, print \(n\) integers β a permutation \(a\) that corresponds to the given array \(b\). If there are multiple possible permutations, then print any of them. | Input: 440 2 0 121 150 0 1 4 130 1 3 | Output: 2 1 4 3 1 2 3 4 2 1 5 3 2 1 | Hard | 6 | 890 | 457 | 174 | 17 | |
914 | A | 914A | A. Perfect Squares | 900 | brute force; implementation; math | Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square.A number x is said to be a perfect square if there exists an integer y such that x = y2. | The first line contains a single integer n (1 β€ n β€ 1000) β the number of elements in the array.The second line contains n integers a1, a2, ..., an ( - 106 β€ ai β€ 106) β the elements of the array.It is guaranteed that at least one element of the array is not a perfect square. | Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists. | In the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2. | Input: 24 2 | Output: 2 | Beginner | 3 | 200 | 276 | 115 | 9 |
463 | D | 463D | D. Gargari and Permutations | 1,900 | dfs and similar; dp; graphs; implementation | Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can... | The first line contains two integers n and k (1 β€ n β€ 1000; 2 β€ k β€ 5). Each of the next k lines contains integers 1, 2, ..., n in some order β description of the current permutation. | Print the length of the longest common subsequence. | The answer for the first test sample is subsequence [1, 2, 3]. | Input: 4 31 4 2 34 1 2 31 2 4 3 | Output: 3 | Hard | 4 | 455 | 183 | 51 | 4 |
1,710 | E | 1710E | E. Two Arrays | 2,400 | binary search; games; graph matchings | You are given two arrays of integers \(a_1,a_2,\dots,a_n\) and \(b_1,b_2,\dots,b_m\). Alice and Bob are going to play a game. Alice moves first and they take turns making a move.They play on a grid of size \(n \times m\) (a grid with \(n\) rows and \(m\) columns). Initially, there is a rook positioned on the first row ... | The first line contains two integers \(n\) and \(m\) (\(1 \leq n,m \leq 2 \cdot 10^5\)) β the length of the arrays \(a\) and \(b\) (which coincide with the number of rows and columns of the grid).The second line contains the \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 5 \cdot 10^8\)).The third line conta... | Print a single line containing the final score of the game. | In the first test case, Alice moves the rook to \((2, 1)\) and Bob moves the rook to \((1, 1)\). This process will repeat for \(999\) times until finally, after Alice moves the rook, Bob cannot move it back to \((1, 1)\) because it has been visited \(1000\) times before. So the final score of the game is \(a_2+b_1=4\).... | Input: 2 1 3 2 2 | Output: 4 | Expert | 3 | 1,072 | 402 | 59 | 17 |
470 | G | 470G | G. Hamming Distance | 2,300 | *special | Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. You are given two strings; calculate the distance between them. | The input consists of two lines. Each line contains a string of characters 'A'-'Z' between 1 and 100 characters, inclusive. The strings have equal length. | Output Hamming distance between the strings. | Input: CODECHEFTOPCODER | Output: 6 | Expert | 1 | 193 | 154 | 44 | 4 | |
1,852 | D | 1852D | D. Miriany and Matchstick | 2,800 | constructive algorithms; dp; greedy | Miriany's matchstick is a \(2 \times n\) grid that needs to be filled with characters A or B. He has already filled in the first row of the grid and would like you to fill in the second row. You must do so in a way such that the number of adjacent pairs of cells with different characters\(^\dagger\) is equal to \(k\). ... | The first line consists of an integer \(t\), the number of test cases (\(1 \leq t \leq 1000\)). The description of the test cases follows.The first line of each test case has two integers, \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5, 0 \leq k \leq 3 \cdot n\)) β the number of columns of the matchstick, and the number... | For each test case, if there is no way to fill the second row with the number of adjacent pairs of cells with different characters equals \(k\), output ""NO"". Otherwise, output ""YES"". Then, print \(n\) characters that a valid bottom row for Miriany's matchstick consists of. If there are several answers, output any o... | In the first test case, it can be proved that there exists no possible way to fill in row \(2\) of the grid such that \(k = 1\). For the second test case, BABB is one possible answer.The grid below is the result of filling in BABB as the second row. \(\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & A & B & B \... | Input: 410 1ABBAAABBAA4 5AAAA9 17BAAABBAAB4 9ABAB | Output: NO YES BABB YES ABABAABAB NO | Master | 3 | 636 | 601 | 327 | 18 |
335 | A | 335A | A. Banana | 1,400 | binary search; constructive algorithms; greedy | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly n stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length n. Piegirl wants to create a string s using stickers. She may buy as many sheets ... | The first line contains string s (1 β€ |s| β€ 1000), consisting of lowercase English characters only. The second line contains an integer n (1 β€ n β€ 1000). | On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of n lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string s, print ... | In the second example, Piegirl can order 3 sheets of stickers with the characters ""nab"". She can take characters ""nab"" from the first sheet, ""na"" from the second, and ""a"" from the third, and arrange them to from ""banana"". | Input: banana4 | Output: 2baan | Easy | 3 | 747 | 153 | 361 | 3 |
958 | A1 | 958A1 | A1. Death Stars (easy) | 1,400 | implementation | The stardate is 1977 and the science and art of detecting Death Stars is in its infancy. Princess Heidi has received information about the stars in the nearby solar system from the Rebel spies and now, to help her identify the exact location of the Death Star, she needs to know whether this information is correct. Two ... | The first line of the input contains one number N (1 β€ N β€ 10) β the dimension of each map. Next N lines each contain N characters, depicting the first map: 'X' indicates a star, while 'O' indicates an empty quadrant of space. Next N lines each contain N characters, depicting the second map in the same format. | The only line of output should contain the word Yes if the maps are identical, or No if it is impossible to match them by performing rotations and translations. | In the first test, you can match the first map to the second map by first flipping the first map along the vertical axis, and then by rotating it 90 degrees clockwise. | Input: 4XOOOXXOOOOOOXXXXXOOOXOOOXOXOXOXX | Output: Yes | Easy | 1 | 1,043 | 311 | 160 | 9 |
2,031 | E | 2031E | E. Penchick and Chloe's Trees | 2,100 | data structures; dfs and similar; dp; greedy; implementation; math; sortings; trees | With just a few hours left until Penchick and Chloe leave for Singapore, they could hardly wait to see the towering trees at the Singapore Botanic Gardens! Attempting to contain their excitement, Penchick crafted a rooted tree to keep Chloe and himself busy.Penchick has a rooted tree\(^{\text{β}}\) consisting of \(n\) ... | 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 a single integer \(n\) (\(2 \le n \le 10^6\)) β the number of vertices in Penchick's tree.The second line of each tes... | For each test case, output a single integer on each line: the minimum depth of Chloe's perfect binary tree. | For the first test case, create a perfect binary tree with depth \(2\).Consider carrying out the operation on edge \(AC\). Then the edges \(AC\), \(CF\), and \(CG\) are removed, and edges \(AF\) and \(AG\) are added. The resulting tree is isomorphic to the tree given in the input. It can be proven that no sequence of o... | Input: 561 2 2 1 1151 1 2 2 3 3 4 4 5 5 6 6 7 751 2 2 271 1 2 1 1 2101 1 1 2 2 2 4 3 3 | Output: 2 3 3 3 3 | Hard | 8 | 2,232 | 518 | 107 | 20 |
1,486 | C1 | 1486C1 | C1. Guessing the Greatest (easy version) | 1,600 | binary search; interactive | The only difference between the easy and the hard version is the limit to the number of queries.This is an interactive problem.There is an array \(a\) of \(n\) different numbers. In one query you can ask the position of the second maximum element in a subsegment \(a[l..r]\). Find the position of the maximum element in ... | The first line contains a single integer \(n\) \((2 \leq n \leq 10^5)\) β the number of elements in the array. | In the sample suppose \(a\) is \([5, 1, 4, 2, 3]\). So after asking the \([1..5]\) subsegment \(4\) is second to max value, and it's position is \(3\). After asking the \([4..5]\) subsegment \(2\) is second to max value and it's position in the whole array is \(4\).Note that there are other arrays \(a\) that would prod... | Input: 5 3 4 | Output: ? 1 5 ? 4 5 ! 1 | Medium | 2 | 554 | 110 | 0 | 14 | |
2,111 | B | 2111B | B. Fibonacci Cubes | 1,100 | brute force; dp; implementation; math | There are \(n\) Fibonacci cubes, where the side of the \(i\)-th cube is equal to \(f_{i}\), where \(f_{i}\) is the \(i\)-th Fibonacci number.In this problem, the Fibonacci numbers are defined as follows: \(f_{1} = 1\) \(f_{2} = 2\) \(f_{i} = f_{i - 1} + f_{i - 2}\) for \(i > 2\) There are also \(m\) empty boxes, where ... | Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^{3}\)) β the number of test cases. The description of the test cases follows.In the first line of each test case, there are two integers \(n\) and \(m\) (\(2 \le n \le 10, 1 \le m \le 2 \cdot 10^{5}\)) β the numbe... | For each test case, output a string of length \(m\), where the \(i\)-th character is equal to ""1"" if all \(n\) cubes can fit into the \(i\)-th box; otherwise, the \(i\)-th character is equal to ""0"". | In the first test case, only one box is suitable. The cubes can be placed in it as follows: | Input: 25 43 1 210 10 109 8 1314 7 202 63 3 31 2 12 1 23 2 22 3 13 2 4 | Output: 0010 100101 | Easy | 4 | 877 | 647 | 202 | 21 |
670 | D1 | 670D1 | D1. Magic Powder - 1 | 1,400 | binary search; brute force; implementation | This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.Waking up in the mo... | The first line of the input contains two positive integers n and k (1 β€ n, k β€ 1000) β the number of ingredients and the number of grams of the magic powder.The second line contains the sequence a1, a2, ..., an (1 β€ ai β€ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake... | Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder. | In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient... | Input: 3 12 1 411 3 16 | Output: 4 | Easy | 3 | 941 | 502 | 132 | 6 |
2,094 | A | 2094A | A. Trippi Troppi | 800 | strings | Trippi Troppi resides in a strange world. The ancient name of each country consists of three strings. The first letter of each string is concatenated to form the country's modern name. Given the country's ancient name, please output the modern name. | The first line contains an integer \(t\) β the number of independent test cases (\(1 \leq t \leq 100\)).The following \(t\) lines each contain three space-separated strings. Each string has a length of no more than \(10\), and contains only lowercase Latin characters. | For each test case, output the string formed by concatenating the first letter of each word. | Input: 7united states americaoh my godi cant liebinary indexed treebelieve in yourselfskibidi slay sigmagod bless america | Output: usa omg icl bit biy sss gba | Beginner | 1 | 249 | 268 | 92 | 20 | |
2,038 | C | 2038C | C. DIY | 1,400 | data structures; geometry; greedy; sortings | You are given a list of \(n\) integers \(a_1, a_2, \dots, a_n\). You need to pick \(8\) elements from the list and use them as coordinates of four points. These four points should be corners of a rectangle which has its sides parallel to the coordinate axes. Your task is to pick coordinates in such a way that the resul... | The first line contains one integer \(t\) (\(1 \le t \le 25\,000\)) β the number of test cases.The first line of each test case contains one integer \(n\) (\(8 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)).Additional constraint ... | For each test case, print the answer as follows: if it is impossible to construct a rectangle which meets the constraints from the statement, print a single line containing the word NO (case-insensitive); otherwise, in the first line, print YES (case-insensitive). In the second line, print \(8\) integers \(x_1, y_1, x_... | Input: 316-5 1 1 2 2 3 3 4 4 5 5 6 6 7 7 1080 0 -1 2 2 1 1 380 0 0 0 0 5 0 5 | Output: YES 1 2 1 7 6 2 6 7 NO YES 0 0 0 5 0 0 0 5 | Easy | 4 | 502 | 404 | 439 | 20 | |
72 | A | 72A | A. Goshtasp, Vishtasp and Eidi | 1,800 | *special; greedy; math | Goshtasp was known to be a good programmer in his school. One day Vishtasp, Goshtasp's friend, asked him to solve this task:Given a positive integer n, you should determine whether n is rich.The positive integer x is rich, if there exists some set of distinct numbers a1, a2, ..., am such that . In addition: every ai sh... | Input contains a single positive integer n (1 β€ n β€ 10000). | If the number is not rich print 0. Otherwise print the numbers a1, ..., am. If several solutions exist print the lexicographically latest solution. Answers are compared as sequences of numbers, not as strings.For comparing two sequences a1, ..., am and b1, ..., bn we first find the first index i such that ai β bi, if a... | Input: 11 | Output: 11=11 | Medium | 3 | 620 | 59 | 725 | 0 | |
1,734 | B | 1734B | B. Bright, Nice, Brilliant | 800 | constructive algorithms | There is a pyramid which consists of \(n\) floors. The floors are numbered from top to bottom in increasing order. In the pyramid, the \(i\)-th floor consists of \(i\) rooms.Denote the \(j\)-th room on the \(i\)-th floor as \((i,j)\). For all positive integers \(i\) and \(j\) such that \(1 \le j \le i < n\), there are ... | The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains a single positive integer \(n\) (\(1 \le n \le 500\)) β the number of floors in the pyramid.It is guaranteed that the sum of \(... | For each test case, output \(n\) lines, the arrangement of torches in the pyramid.The \(i\)-th line should contain \(i\) integers, each separated with a space. The \(j\)-th integer on the \(i\)-th line should be \(1\) if room \((i,j)\) has a torch, and \(0\) otherwise.We can show that an answer always exists. If there ... | In the third test case, torches are placed in \((1,1)\), \((2,1)\), \((2,2)\), \((3,1)\), and \((3,3)\). The pyramid is nice as rooms on each floor have the same brightness. For example, all rooms on the third floor have brightness \(3\).The brilliance of the pyramid is \(1+2+3 = 6\). It can be shown that no arrangemen... | Input: 3123 | Output: 1 1 1 1 1 1 1 1 0 1 | Beginner | 1 | 1,685 | 368 | 365 | 17 |
635 | A | 635A | A. Orchestra | 1,100 | brute force; implementation | Paul is at the orchestra. The string section is arranged in an r Γ c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count ... | The first line of input contains four space-separated integers r, c, n, k (1 β€ r, c, n β€ 10, 1 β€ k β€ n) β the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.The next n lines each contain two integers xi and y... | Print a single integer β the number of photographs Paul can take which include at least k violas. | We will use '*' to denote violinists and '#' to denote violists.In the first sample, the orchestra looks as follows *#** Paul can take a photograph of just the viola, the 1 Γ 2 column containing the viola, the 2 Γ 1 row containing the viola, or the entire string section, for 4 pictures total.In the second sample, the o... | Input: 2 2 1 11 2 | Output: 4 | Easy | 2 | 476 | 450 | 97 | 6 |
891 | E | 891E | E. Lust | 3,000 | combinatorics; math; matrices | A false witness that speaketh lies!You are given a sequence containing n integers. There is a variable res that is equal to 0 initially. The following process repeats k times.Choose an index from 1 to n uniformly at random. Name it x. Add to res the multiply of all ai's such that 1 β€ i β€ n, but i β x. Then, subtract ax... | The first line contains two integers n and k (1 β€ n β€ 5000, 1 β€ k β€ 109) β the number of elements and parameter k that is specified in the statement.The second line contains n space separated integers a1, a2, ..., an (0 β€ ai β€ 109). | Output a single integer β the value . | Input: 2 15 5 | Output: 5 | Master | 3 | 506 | 232 | 37 | 8 | |
581 | A | 581A | A. Vasya the Hipster | 800 | implementation; math | One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red socks and b blue socks.According to the latest fashion, hipsters should wear the socks of different colors: a red one on the left foot, a blue one on the right foot.Every day Vasya puts on new socks in the morning and thro... | The single line of the input contains two positive integers a and b (1 β€ a, b β€ 100) β the number of red and blue socks that Vasya's got. | Print two space-separated integers β the maximum number of days when Vasya can wear different socks and the number of days when he can wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.Keep in mind that at the end of the day Vasya throws away the socks that he's ... | In the first sample Vasya can first put on one pair of different socks, after that he has two red socks left to wear on the second day. | Input: 3 1 | Output: 1 1 | Beginner | 2 | 661 | 137 | 345 | 5 |
1,807 | G1 | 1807G1 | G1. Subsequence Addition (Easy Version) | 1,100 | brute force; data structures; dp; greedy; implementation; sortings | The only difference between the two versions is that in this version, the constraints are lower.Initially, array \(a\) contains just the number \(1\). You can perform several operations in order to change the array. In an operation, you can select some subsequence\(^{\dagger}\) of \(a\) and add into \(a\) an element eq... | The first line of the input contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5000\)) β the number of elements the final array \(c\) should have.The second line of eac... | For each test case, output ""YES"" (without quotes) if such a sequence of operations exists, and ""NO"" (without quotes) 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). | For the first test case, the initial array \(a\) is already equal to \([1]\), so the answer is ""YES"".For the second test case, performing any amount of operations will change \(a\) to an array of size at least two which doesn't only have the element \(2\), thus obtaining the array \([2]\) is impossible and the answer... | Input: 6111255 1 3 2 157 1 5 2 131 1 151 1 4 2 1 | Output: YES NO YES NO YES YES | Easy | 6 | 915 | 584 | 274 | 18 |
120 | H | 120H | H. Brevity is Soul of Wit | 1,800 | graph matchings | As we communicate, we learn much new information. However, the process of communication takes too much time. It becomes clear if we look at the words we use in our everyday speech.We can list many simple words consisting of many letters: ""information"", ""technologies"", ""university"", ""construction"", ""conservatoi... | The first line of the input file contains the only integer n (1 β€ n β€ 200). Then n lines contain a set of different non-empty words that consist of lowercase Latin letters. The length of each word does not exceed 10 characters. | If the solution exists, print in the output file exactly n lines, where the i-th line represents the shortened variant of the i-th word from the initial set. If there are several variants to solve the problem, print any of them. If there is no solution, print -1. | Input: 6privetspasibocodeforcesjavamarmeladnormalno | Output: pretspscdfsjavamamanorm | Medium | 1 | 1,610 | 227 | 263 | 1 | |
2,062 | C | 2062C | C. Cirno and Operations | 1,200 | brute force; math | Cirno has a sequence \(a\) of length \(n\). She can perform either of the following two operations for any (possibly, zero) times unless the current length of \(a\) is \(1\): Reverse the sequence. Formally, \([a_1,a_2,\ldots,a_n]\) becomes \([a_n,a_{n-1},\ldots,a_1]\) after the operation. Replace the sequence with its ... | The first line of input contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of input test cases.The first line of each test case contains a single integer \(n\) (\(1\le n\le 50\)) β the length of sequence \(a\).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(|a_i|\le... | For each test case, print an integer representing the maximum possible sum. | In the first test case, Cirno can not perform any operation, so the answer is \(-1000\).In the second test case, Cirno firstly reverses the sequence, then replaces the sequence with its difference sequence: \([5,-3]\to[-3,5]\to[8]\). It can be proven that this maximizes the sum, so the answer is \(8\).In the third test... | Input: 51-100025 -321000 199 7 9 -9 9 -8 7 -8 911678 201 340 444 453 922 128 987 127 752 0 | Output: -1000 8 1001 2056 269891 | Easy | 2 | 518 | 350 | 75 | 20 |
1,213 | A | 1213A | A. Chips Moving | 900 | math | You are given \(n\) chips on a number line. The \(i\)-th chip is placed at the integer coordinate \(x_i\). Some chips can have equal coordinates.You can perform each of the two following types of moves any (possibly, zero) number of times on any chip: Move the chip \(i\) by \(2\) to the left or \(2\) to the right for f... | The first line of the input contains one integer \(n\) (\(1 \le n \le 100\)) β the number of chips.The second line of the input contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(1 \le x_i \le 10^9\)), where \(x_i\) is the coordinate of the \(i\)-th chip. | Print one integer β the minimum total number of coins required to move all \(n\) chips to the same coordinate. | In the first example you need to move the first chip by \(2\) to the right and the second chip by \(1\) to the right or move the third chip by \(2\) to the left and the second chip by \(1\) to the left so the answer is \(1\).In the second example you need to move two chips with coordinate \(3\) by \(1\) to the left so ... | Input: 3 1 2 3 | Output: 1 | Beginner | 1 | 853 | 256 | 110 | 12 |
1,765 | K | 1765K | K. Torus Path | 1,500 | greedy; math | You are given a square grid with \(n\) rows and \(n\) columns, where each cell has a non-negative integer written in it. There is a chip initially placed at the top left cell (the cell with coordinates \((1, 1)\)). You need to move the chip to the bottom right cell (the cell with coordinates \((n, n)\)).In one step, yo... | The first line contains the single integer \(n\) (\(2 \le n \le 200\)) β the number of rows and columns in the grid.Next \(n\) lines contains the description of each row of the grid. The \(i\)-th line contains \(n\) integers \(a_{i, 1}, a_{i, 2}, \dots, a_{i, n}\) (\(0 \le a_{i, j} \le 10^9\)) where \(a_{i, j}\) is the... | Print one integer β the maximum possible score you can achieve. | Input: 2 1 2 3 4 | Output: 8 | Medium | 2 | 1,130 | 359 | 63 | 17 | |
1,494 | C | 1494C | C. 1D Sokoban | 1,900 | binary search; dp; greedy; implementation; two pointers | You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.You start on a position \(0\). There are \(n\) boxes, the \(i\)-th box is on a position \(a_i\). All positions of the boxes are distinct. There are also \(m\) special positions,... | The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of testcases.Then descriptions of \(t\) testcases follow.The first line of each testcase contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of boxes and the number of special positions, respectively.The ... | For each testcase print a single integer β the maximum number of boxes that can be placed on special positions. | In the first testcase you can go \(5\) to the right: the box on position \(1\) gets pushed to position \(6\) and the box on position \(5\) gets pushed to position \(7\). Then you can go \(6\) to the left to end up on position \(-1\) and push a box to \(-2\). At the end, the boxes are on positions \([-2, 6, 7, 11, 15]\)... | Input: 5 5 6 -1 1 5 11 15 -4 -3 -2 6 7 15 2 2 -1 1 -1000000000 1000000000 2 2 -1000000000 1000000000 -1 1 3 5 -1 1 2 -2 -1 1 2 5 2 1 1 2 10 | Output: 4 2 0 3 1 | Hard | 5 | 953 | 873 | 111 | 14 |
1,178 | C | 1178C | C. Tiles | 1,300 | combinatorics; greedy; math | Bob is decorating his kitchen, more precisely, the floor. He has found a prime candidate for the tiles he will use. They come in a simple form factor β a square tile that is diagonally split into white and black part as depicted in the figure below. The dimension of this tile is perfect for this kitchen, as he will nee... | The only line contains two space separated integers \(w\), \(h\) (\(1 \leq w,h \leq 1\,000\)) β the width and height of the kitchen, measured in tiles. | Output a single integer \(n\) β the remainder of the number of tilings when divided by \(998244353\). | Input: 2 2 | Output: 16 | Easy | 3 | 1,107 | 151 | 101 | 11 | |
808 | A | 808A | A. Lucky Year | 900 | implementation | Apart from having lots of holidays throughout the year, residents of Berland also have whole lucky years. Year is considered lucky if it has no more than 1 non-zero digit in its number. So years 100, 40000, 5 are lucky and 12, 3001 and 12345 are not.You are given current year in Berland. Your task is to find how long w... | The first line contains integer number n (1 β€ n β€ 109) β current year in Berland. | Output amount of years from the current year to the next lucky one. | In the first example next lucky year is 5. In the second one β 300. In the third β 5000. | Input: 4 | Output: 1 | Beginner | 1 | 375 | 81 | 67 | 8 |
546 | C | 546C | C. Soldier and Cards | 1,400 | brute force; dfs and similar; games | Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a ""war""-like card game. The rules are following. On each turn a fi... | First line contains a single integer n (2 β€ n β€ 10), the number of cards.Second line contains integer k1 (1 β€ k1 β€ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.Third line contains integer k2 (k1 + k2 = n), the ... | If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.If the game won't end and will continue forever output - 1. | First sample: Second sample: | Input: 42 1 32 4 2 | Output: 6 2 | Easy | 3 | 862 | 495 | 236 | 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.