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 caught on fire! The second fire started is considered the second \(0\). Every second fire lit up all intact neightbouring trees to every currently burning tree. The tree is neighbouring if it occupies adjacent by side or by corner cell. Luckily, after \(t\) seconds Berland fire department finally reached the location of fire and instantaneously extinguished it all.Now they want to calculate the destructive power of the fire. Let \(val_{x, y}\) be the second the tree in cell \((x, y)\) got caught on fire. The destructive power is the sum of \(val_{x, y}\) over all \((x, y)\) of burnt trees.Clearly, all the workers of fire department are firefighters, not programmers, thus they asked you to help them calculate the destructive power of the fire.The result can be rather big, so print it modulo \(998244353\).
|
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 positions of trees that initially got caught on fire.Obviously, the position of cell \((0, 0)\) on the grid and the directions of axes is irrelevant as the grid is infinite and the answer doesn't depend on them.It is guaranteed that all the given tree positions are pairwise distinct.The grid is infinite so the fire doesn't stop once it reaches \(-10^8\) or \(10^8\). It continues beyond these borders.
|
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 denoted by (i, j). A player starts in the room (1, 1) and wants to reach the room (n, m).Each room has four doors (except for ones at the maze border), one on each of its walls, and two adjacent by the wall rooms shares the same door. Some of the doors are locked, which means it is impossible to pass through the door. For example, if the door connecting (i, j) and (i, j + 1) is locked, then we can't go from (i, j) to (i, j + 1). Also, one can only travel between the rooms downwards (from the room (i, j) to the room (i + 1, j)) or rightwards (from the room (i, j) to the room (i, j + 1)) provided the corresponding door is not locked. This image represents a maze with some doors locked. The colored arrows denotes all the possible paths while a red cross denotes a locked door. ZS the Coder considers a maze to have difficulty x if there is exactly x ways of travelling from the room (1, 1) to the room (n, m). Two ways are considered different if they differ by the sequence of rooms visited while travelling.Your task is to create a maze such that its difficulty is exactly equal to T. In addition, ZS the Coder doesn't like large mazes, so the size of the maze and the number of locked doors are limited. Sounds simple enough, right?
|
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 integers, x1, y1, x2, y2. This means that the door connecting room (x1, y1) and room (x2, y2) is locked. Note that room (x2, y2) should be adjacent either to the right or to the bottom of (x1, y1), i.e. x2 + y2 should be equal to x1 + y1 + 1. There should not be a locked door that appears twice in the list.It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them.
|
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, separated by commas. The same address can be written more than once.Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that.
|
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 them.
|
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 p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
|
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 the length of the chosen log is \(x\), and the lengths of the resulting pieces are \(y\) and \(z\), then \(y\) and \(z\) have to be positive integers, and \(x=y+z\) must hold. For example, you can chop a log of length \(3\) into logs of lengths \(2\) and \(1\), but not into logs of lengths \(3\) and \(0\), \(2\) and \(2\), or \(1.5\) and \(1.5\).The player who is unable to make a chop will be the loser. Assuming that both errorgorn and maomao90 play optimally, who will be the winner?
|
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 case contains \(n\) integers \(a_1,a_2, \ldots, a_n\) (\(1 \leq a_i \leq 50\)) β the lengths of the logs.Note that there is no bound on the sum of \(n\) over all test cases.
|
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 of length \(1\). After \(4\) moves, it will be maomao90's turn and he will not be able to make a move. Therefore errorgorn will be the winner.In the second test case, errorgorn will not be able to make a move on his first turn and will immediately lose, making maomao90 the winner.
|
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. For example, the strings ""anna"", ""aboba"", ""level"" are palindromes, while the strings ""gorilla"", ""banan"", ""off"" are not.A substring \([l \ldots r]\) of string \(s\) is a string \(s_l s_{l+1} \ldots s_{r-1} s_r\). For example, the substring \([4 \ldots 6]\) of the string ""generation"" is the string ""era"".A string is called beautiful if it does not contain a substring of length at least two that is a palindrome. For example, the strings ""fox"", ""abcdef"", and ""yioy"" are beautiful, while the strings ""xyxx"", ""yikjkitrb"" are not.When an integer \(x\) is added to the character \(s_i\), it is replaced \(x\) times with the next character in the alphabet, with ""z"" being replaced by ""a"".When an integer \(x\) is added to the substring \([l, r]\) of string \(s\), it becomes the string \(s_1 s_2 \ldots s_{l-1} (s_l + x) (s_{l+1} + x) \ldots (s_{r-1} + x) (s_r + x) s_{r+1} \ldots s_n\). For example, when the substring \([2, 4]\) of the string ""abazaba"" is added with the number \(6\), the resulting string is ""ahgfaba"".End of the instruction.After reading the instruction, Anya resigned herself to the fact that she has to answer \(m\) queries. The queries can be of two types: Add the number \(x\) to the substring \([l \ldots r]\) of string \(s\). Determine whether the substring \([l \ldots r]\) of string \(s\) is beautiful.
|
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 test case contains the string \(s\) of length \(n\), consisting of lowercase Latin letters.The next \(m\) lines contain the queries: \(1\) \(l\) \(r\) \(x\) (\(1 \le l \le r \le n\), \(1 \le x \le 10^9\)) - description of a query of the first type; \(2\) \(l\) \(r\) (\(1 \le l \le r \le n\)) - description of a query of the second type. It is guaranteed that the sum of \(n\) and the sum of \(m\) over all test cases do not exceed \(2 \cdot 10^5\).
|
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\) terkreaaaasz; terkreaaaasz: the string kreaaaa is not beautiful as it contains the palindrome aaaa; terkreaaaasz: the string asz is beautiful.
|
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 in the following way: form a list of distances \(d_1, d_2, \dots, d_n\) where \(d_i = |a_i - x|\) (distance between \(a_i\) and \(x\)); sort list \(d\) in non-descending order; take \(d_{k + 1}\) as a result. If there are multiple optimal answers you can print any of them.
|
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 and constant \(k\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_1 < a_2 < \dots < a_n \le 10^9\)) β points in ascending order.It's guaranteed that \(\sum{n}\) doesn't exceed \(2 \cdot 10^5\).
|
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 of \(n\) zeros.Determine if for all permutations\(^\dagger\) \(b\) of \(a\), \(f(a) \leq f(b)\) is true. \(^\dagger\) An array \(b\) is a permutation of an array \(a\) if \(b\) consists of the elements of \(a\) in arbitrary order. For example, \([4,2,3,4]\) is a permutation of \([3,2,4,4]\) while \([1,2,2]\) is not a permutation of \([1,2,3]\).
|
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\)) β description of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
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 \(3\) operations.
|
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 \(n\) marbles have been taken), the game ends.Alice's score at the end of the game is calculated as follows: she receives \(1\) point for every color \(x\) such that she has taken at least one marble of that color; additionally, she receives \(1\) point for every color \(x\) such that she has taken all marbles of that color (of course, only colors present in the game are considered). For example, suppose there are \(5\) marbles, their colors are \([1, 3, 1, 3, 4]\), and the game goes as follows: Alice takes the \(1\)-st marble, then Bob takes the \(3\)-rd marble, then Alice takes the \(5\)-th marble, then Bob takes the \(2\)-nd marble, and finally, Alice takes the \(4\)-th marble. Then, Alice receives \(4\) points: \(3\) points for having at least one marble for colors \(1\), \(3\) and \(4\), and \(1\) point for having all marbles of color \(4\). Note that this strategy is not necessarily optimal for both players.Alice wants to maximize her score at the end of the game. Bob wants to minimize it. Both players play optimally (i. e. Alice chooses a strategy which allows her to get as many points as possible, and Bob chooses a strategy which minimizes the amount of points Alice can get).Calculate Alice's score at the end of the game.
|
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 of the marbles. Additional constraint on the input: the sum of \(n\) over all test cases does not exceed \(1000\).
|
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 players act, Alice receives \(1\) point for having at least one (but not all) marble of color \(4\).
|
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 \(1 \leq i_1 < i_2 < \ldots < i_{r - l + 1} \leq n\), such that \(b_j = a_{i_j}\) for all \(1 \leq j \leq r - l + 1\).
|
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 test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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)\) and \((3, 3)\) are suitable.
|
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.After some changes (possibly zero) you want to buy the string. To buy the string you should buy all its characters. To buy the character \(0\) you should pay \(c_0\) coins, to buy the character \(1\) you should pay \(c_1\) coins.Find the minimum number of coins needed to buy the string.
|
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 of the description of each test case contains the binary string \(s\) of length \(n\).
|
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 can buy the string and pay \(5 \cdot 10 = 50\) coins for that. The total number of coins paid will be \(2 + 50 = 52\).
|
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 (different colors represent different rectangles): And if w = {5, 1, 0, 3, 0, 0, 1}, then it looks like that: Find the number of ways to color some borders of the figure's inner squares so that no square had all four borders colored. The borders of the squares lying on the border of the figure should be considered painted. The ways that differ with the figure's rotation should be considered distinct.
|
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 as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedureFor a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph.
|
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 conducts a long, non-dubious, sentimental experiment.She puts \(n\) cannibalistic ants in a line on a long wooden stick. Initially, the ants have the same weight of \(1\). The distance between any two consecutive ants is the same. The distance between the first ant in the line to the left end and the last ant in the line to the right end is also the same as the distance between the ants. Each ant starts moving towards the left-end or the right-end randomly and equiprobably, at the same constant pace throughout the experiment. Two ants will crash if they are standing next to each other in the line and moving in opposite directions, and ants will change direction immediately when they reach the end of the stick. Ela can't determine the moving direction of each ant, but she understands very well their behavior when crashes happen. If a crash happens between two ants of different weights, the heavier one will eat the lighter one, and gain the weight of the lighter one. After that, the heavier and will continue walking in the same direction. In other words, if the heavier one has weight \(x\) and walking to the right, the lighter one has weight \(y\) and walking to the left (\(x > y\)), then after the crash, the lighter one will diminish, and the heavier one will have weight \(x + y\) and continue walking to the right. If a crash happens between two ants with the same weight, the one walking to the left end of the stick will eat the one walking to the right, and then continue walking in the same direction. In other words, if one ant of weight \(x\) walking to the left, crashes with another ant of weight \(x\) walking to the right, the one walking to the right will disappear, and the one walking to the left will have to weight \(2x\) and continue walking to the left. Please, check the example in the ""Note"" section, which will demonstrate the ants' behavior as above.We can prove that after a definite amount of time, there will be only one last ant standing. Initially, each ant can randomly and equiprobably move to the left or the right, which generates \(2^n\) different cases of initial movements for the whole pack. For each position in the line, calculate the probability that the ant begins in that position and survives. Output it modulo \(10^9 + 7\).Formally, let \(M = 10^9 + 7\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
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 all tests will not exceed \(10^6\).
|
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 index \(2\) (walking to the left) will crash with the ant with index \(1\) (walking to the right). The two ants have the same weight, therefore, ant \(2\) will eat ant \(1\) and gain its weight to \(2\). The same thing happens with ant \(5\) and ant \(4\).The ant \(6\) will walk to the end of the stick, therefore changing its direction. After that, the ant with index \(5\) will crash with the ant with index \(3\). Since ant \(5\) is more heavy (weight=\(2\)) than ant \(3\) (weight=\(1\)), ant \(5\) will eat ant \(3\) and gain its weight to \(3\).Ant \(2\) will walk to the end of the stick, therefore changing its direction. After that, the ant with index \(5\) will crash with the ant with index \(2\). Since ant \(5\) is more heavy (weight=\(3\)) than ant \(2\) (weight=\(2\)), ant \(5\) will eat ant \(2\) and gain its weight to \(5\). Lastly, after ant \(5\) walk to the end of the branch and changes its direction, ant \(5\) will eat ant \(6\) and be the last ant standing.
|
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 between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions.
|
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, so we just perform some unnecessary swap, for example, one can swap 5 and 2.
|
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 answer \(q\) independent queries \((x, y)\): after replacing the \(x\) first elements of \(a\) and the \(y\) last elements of \(a\) by \(n+1\) (making them impossible to remove), what would be the weight of \(a\)?
|
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\) and \(y\) (\(x, y \ge 0\) and \(x+y < n\)).
|
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 red): \([\times, \times, \times, 9, \color{red}{5}, 4, 6, 5, 7, 8, 3, 11, \times]\) \([\times, \times, \times, 9, 4, 6, 5, 7, 8, 3, \color{red}{11}, \times]\) \([\times, \times, \times, 9, 4, \color{red}{6}, 5, 7, 8, 3, \times]\) \([\times, \times, \times, 9, 4, 5, 7, \color{red}{8}, 3, \times]\) \([\times, \times, \times, 9, 4, 5, \color{red}{7}, 3, \times]\) \([\times, \times, \times, 9, 4, 5, 3, \times]\) (final state) It is impossible to remove more than \(5\) elements, hence the weight is \(5\).
|
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 the number of stacks always remains \(n\): stacks don't disappear when they have \(0\) blocks.
|
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 guaranteed that the sum of all \(n\) does not exceed \(10^4\).
|
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 from the second to the third, which would make the heights \(3\) \(4\) \(5\).In the fourth test case we can't make a move, but the sequence is not increasing, so the answer is NO.In the fifth test case we can only make one move (from the second to the third stack), which would make the heights \(0\) \(0\) \(1\). Both \(0\) \(1\) \(0\) and \(0\) \(0\) \(1\) are not increasing sequences, so the answer is NO.
|
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 segment. Pick a random permutation of integers from 1 to k, p1, p2, ..., pk. All k! permutation are equiprobable. This permutation is applied to elements of the chosen segment, i.e. permutation a1, a2, ..., al - 1, al, al + 1, ..., ar - 1, ar, ar + 1, ..., an is transformed to a1, a2, ..., al - 1, al - 1 + p1, al - 1 + p2, ..., al - 1 + pk - 1, al - 1 + pk, ar + 1, ..., an. Inversion if a pair of elements (not necessary neighbouring) with the wrong relative order. In other words, the number of inversion is equal to the number of pairs (i, j) such that i < j and ai > aj. Find the expected number of inversions after we apply exactly one operation mentioned above.
|
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 satisfy the inequations: 0 β€ x β€ n; 0 β€ y β€ m; x + y > 0. Choose their subset of maximum size such that it is also a beautiful set of points.
|
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. For example, weight of array \([1, 4, 2]\) is \(1\), weight of array \([5, 4, 3]\) is \(3\).You have to perform \(q\) queries of the following form β change the value at some position of the array. After each operation, output the weight of the array. Updates are not independent (the change stays for the future).
|
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 \le pos \le n\), \(1 \le x \le 10^6, x \ne a_{pos}\)). It means that you have to make \(a_{pos}=x\).It's guaranteed that at every moment of time, all elements of the array are different.
|
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 possible to cut the grid along grid lines into two connected, congruent pieces so that each pair is part of the same piece. Two pieces are congruent if one can be matched with the other by translation, rotation, and reflection, or a combination of these. The picture above represents the first test case. Arrows indicate pairs of squares, and the thick black line represents the cut. You are given an array \(a\) of \(n\) pairs of adjacent squares. Find the size of the largest strong subsequence of \(a\). An array \(p\) is a subsequence of an array \(q\) if \(p\) can be obtained from \(q\) by deletion of several (possibly, zero or all) elements.
|
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\) is even) β the length of \(a\) and the size of the grid, respectively.Then \(n\) lines follow. The \(i\)-th of these lines contains four space-separated integers \(r_{i,1}\), \(c_{i,1}\), \(r_{i,2}\), and \(c_{i,2}\) (\(1 \leq r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} \leq k\)) β the \(i\)-th element of \(a\), represented by the row and column of the first square \((r_{i,1}, c_{i,1})\) and the row and column of the second square \((r_{i,2}, c_{i,2})\). These squares are adjacent.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\), and the sum of \(k\) over all test cases does not exceed \(500\).
|
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 through its center.
|
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 down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given.
|
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 information given.
|
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 \(s\) and \(t\) of length \(n\) consisting of only of lowercase English characters. He wants to make them equal. Since Ujan is lazy, he will perform the following operation exactly once: he takes two positions \(i\) and \(j\) (\(1 \le i,j \le n\), the values \(i\) and \(j\) can be equal or different), and swaps the characters \(s_i\) and \(t_j\). Can he succeed?Note that he has to perform this operation exactly once. He has to perform this operation.
|
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 length exactly \(n\). The strings consist only of lowercase English letters. It is guaranteed that strings are different.
|
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\) vertices is an undirected connected graph with \(n - 1\) edges.Alice and Bob take turns, with Alice going first. Each player starts at some vertex.On their turn, a player must move from the current vertex to a neighboring vertex that has not yet been visited by anyone. The first player who cannot make a move loses.You are given two vertices \(u\) and \(v\). Represent the simple path from vertex \(u\) to \(v\) as an array \(p_1, p_2, p_3, \ldots, p_m\), where \(p_1 = u\), \(p_m = v\), and there is an edge between \(p_i\) and \(p_{i + 1}\) for all \(i\) (\(1 \le i < m\)).You need to determine the winner of the game if Alice starts at vertex \(1\) and Bob starts at vertex \(p_j\) for each \(j\) (where \(1 \le j \le m\)).
|
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 - 1\) lines contains two integers \(a\) and \(b\) (\(1 \le a, b \le n\)), denoting an undirected edge between vertices \(a\) and \(b\). It is guaranteed that these edges form a tree.The last line of each test case contains two integers \(u\) and \(v\) (\(2 \le u, v \le n\)).It is guaranteed that the path from \(u\) to \(v\) does not pass through vertex \(1\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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 operation at most once for each index \(1 \le i \le n - 1\). However, you can do it in any order you want. Find the maximum number of operations that you can carry out.
|
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 contains the string \(s\) (\(s_i=\texttt{A}\) or \(s_i=\texttt{B}\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
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\) to form \(\texttt{ABBA}\), and finally another operation on \(i=1\) to form \(\texttt{BABA}\). Note that even though at the end, \(s_2 = \texttt{A}\) and \(s_3 = \texttt{B}\), we cannot do an operation on \(i=2\) again as we can only do the operation at most once for each index.
|
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 in the following way: for each city i, different from the capital, there is kept number pi β index of the last city on the way from the capital to i.Once the king of Berland Berl XXXIV decided to move the capital from city r1 to city r2. Naturally, after this the old representation of the road map in Berland's chronicles became incorrect. Please, help the king find out a new representation of the road map in the way described above.
|
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 from r1, there is given integer pi β index of the last city on the way from the capital to city i. All the cities are described in order of increasing indexes.
|
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 consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates \((x_i, y_i)\). Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
|
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 follows: Alice begins the game, with moves alternating each turn; in any move, the current player may choose some node (for example, \(i\)) which has depth at least \(k\). Then, the player picks some positive number of presents hanging from that node, let's call it \(m\) \((1 \le m \le a_i)\); the player then places these \(m\) presents on the \(k\)-th ancestor (let's call it \(j\)) of the \(i\)-th node (the \(k\)-th ancestor of vertex \(i\) is a vertex \(j\) such that \(i\) is a descendant of \(j\), and the difference between the depth of \(j\) and the depth of \(i\) is exactly \(k\)). Now, the number of presents of the \(i\)-th node \((a_i)\) is decreased by \(m\), and, correspondingly, \(a_j\) is increased by \(m\); Alice and Bob both play optimally. The player unable to make a move loses the game.For each possible root of the tree, find who among Alice or Bob wins the game.Note: The depth of a node \(i\) in a tree with root \(r\) is defined as the number of edges on the simple path from node \(r\) to node \(i\). The depth of root \(r\) itself is zero.
|
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 next line contains \(n\) space-separated integers denoting the array \(a\) \((0 \le a_i \le 10^9)\).
|
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 three presents from node 2 to node 1. Alice moves three presents from node 3 to node 1. Bob moves three presents from node 4 to node 3. Alice moves three presents from node 3 to node 1. Bob is now unable to make a move and hence loses.Root node 2Bob always wins in this case. One such gameplay is: Alice moves four presents from node 4 to node 3. Bob moves four presents from node 5 to node 2. Alice moves six presents from node 3 to node 1. Bob moves six presents from node 1 to node 2. Alice is now unable to make a move and hence loses.
|
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 decrease or increase the pressure in any (single) wheel by \(1\). He can increase the pressure no more than \(k\) times in total because it is hard to pump up wheels.Help Polycarp and say what is the minimum number of minutes he needs to spend to equalize the pressure of at least \(m\) wheels among the given \(n\) wheels.
|
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\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the pressure in the \(i\)-th wheel.
|
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 less than k, whose remainder is p when divided by (x + k). That is, f(x) = q(x)Β·(x + k) + p, where q(x) is a polynomial (not necessarily with integer coefficients).
|
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. Your output should satisfy 0 β€ ai < k for all 0 β€ i β€ d - 1, and ad - 1 β 0.If there are many possible solutions, print any of them.
|
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 of the \(j\)-th house wants to live on the left side of the street; otherwise, \(a_j = 1\).The road will pass between two houses. The houses to the left of it will be declared the left-side, and the houses to the right will be declared the right-side. More formally, let the road pass between houses \(i\) and \(i+1\). Then the houses at positions between \(1\) and \(i\) will be on the left side of the street, and at positions between \(i+1\) and \(n\) will be on the right side. The road also may pass before the first and after the last house; in this case, the entire village is declared to be either the right or left side, respectively.To make the design fair, it was decided to lay the road so that at least half of the residents on each side of the village are satisfied with the choice. That is, among \(x\) residents on one side, at least \(\lceil\frac{x}{2}\rceil\) should want to live on that side, where \(\lceil x \rceil\) denotes rounding up a real number \(x\). To the left of the road, there will be \(i\) houses, among the corresponding \(a_j\) there must be at least \(\lceil\frac{i}{2}\rceil\) zeros. To the right of the road, there will be \(n-i\) houses, among the corresponding \(a_j\) there must be at least \(\lceil\frac{n-i}{2}\rceil\) ones. Determine after which house \(i\) the road should be laid in order to satisfy the described condition and be as close to the middle of the village as possible. Formally, among all suitable positions \(i\), minimize \(\left|\frac{n}{2} - i\right|\).If there are multiple suitable positions \(i\) with the minimum \(\left|\frac{n}{2} - i\right|\), output the smaller one.
|
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\) of length \(n\), consisting only of \(0\) and \(1\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3\cdot 10^5\).
|
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, which means that the road cannot be laid after house \(1\).If we lay the road after the second house, \(1\) out of \(2\) residents on the left side (with preferences \(a_1 = 1\), \(a_2 = 0\)) and \(1\) out of \(1\) resident on the right side (with preference \(a_3 = 1\)) will be satisfied with the choice. More than half of the residents on each side are satisfied with the choice, which means that the road can be laid after house \(2\). We can show that this is the optimal answer.
|
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-multiple free subset.
|
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 above equation holds.Let's write out all such z in the increasing order: z1, z2, z3, and so on (zi < zi + 1). Your task is: given the number n, find the number zn.
|
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. The bitwise xor of any two consecutive elements in the permutation is an element in \(S\).Since Kuro is really excited about magical permutations, he wants to create the longest magical permutation possible. In other words, he wants to find the largest non-negative integer \(x\) such that there is a magical permutation of integers from \(0\) to \(2^x - 1\). Since he is a newbie in the subject, he wants you to help him find this value of \(x\) and also the magical permutation for that \(x\).
|
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 guess what? The richest person has found an extra pot of gold! More formally, find an \(a_j=max(a_1, a_2, \dots, a_n)\), change \(a_j\) to \(a_j+x\), where \(x\) is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them.A person is unhappy if their wealth is strictly less than half of the average wealth\(^{\text{β}}\). If strictly more than half of the total population \(n\) are unhappy, Robin Hood will appear by popular demand.Determine the minimum value of \(x\) for Robin Hood to appear, or output \(-1\) if it is impossible.\(^{\text{β}}\)The average wealth is defined as the total wealth divided by the total population \(n\), that is, \(\frac{\sum a_i}{n}\), the result is a real number.
|
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\)) β the wealth of each person.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
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}\), and half of this average is \(\frac{25}{8}\), resulting in \(3\) people being unhappy.In the fifth test case, after adding \(16\) gold, the average wealth becomes \(\frac{31}{5}\), resulting in \(3\) people being unhappy.
|
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\) and \(a_{i + 1}\) to be equal to \(x\). Find the minimum number of operations needed such that the equality of the array is less than or equal to \(1\).
|
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 each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β elements of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10 ^ 5\)
|
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 proportion litres (thus, there should be a1 Β·x, ..., an Β·x litres of corresponding ingredients mixed for some non-negative x). In the kitchen Volodya found out that he has b1, ..., bn litres of these ingredients at his disposal correspondingly. In order to correct his algebra mistakes he ought to cook as much soup as possible in a V litres volume pan (which means the amount of soup cooked can be between 0 and V litres). What is the volume of borscht Volodya will cook ultimately?
|
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 when he gets up. If he gets up at time 0, he will be online at any moment of time between c1 and d1, between c2 and d2, ..., between cq and dq (all borders inclusive). But if he gets up at time t, these segments will be shifted by t. They become [ci + t, di + t] (for all i).If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between l and r (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [l, r] suit for that?
|
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 dj < cj + 1 for all valid i and j.
|
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 game with each other team (n(n - 1) games in total). The team, that plays the home game, traditionally plays in its home kit. The team that plays an away game plays in its away kit. However, if two teams has the kits of the same color, they cannot be distinguished. In this case the away team plays in its home kit.Calculate how many games in the described tournament each team plays in its home kit and how many games it plays in its away kit.
|
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 same color (for example, you can move between a ""blue-red"" city and a ""blue-green"" city). This movement costs \(|i-j|\) coins.Your task is to answer \(q\) independent queries: calculate the minimum cost to move from city \(x\) to city \(y\).
|
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 types: BG, BR, BY, GR, GY, or RY; the \(i\)-th of them describes the portals located in the \(i\)-th city; the symbol B indicates that there is a blue portal in the city, G β green, R β red, and Y β yellow.The \(j\)-th of the next \(q\) lines contains two integers \(x_j\) and \(y_j\) (\(1 \le x_j, y_j \le n\)) β the description of the \(j\)-th query.Additional constraints on the input: the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\); the sum of \(q\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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 beauty is rather bizarre. He defines the beauty of the tree as the sum of \(|a_u - a_v|\) over all edges \((u, v)\) of the tree.Since Parsa's tree is too large, Nima can't maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa's tree.
|
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 integers \(l_i\) and \(r_i\) \((1 \le l_i \le r_i \le 10^9)\).Each of the next \(n-1\) lines contains two integers \(u\) and \(v\) \((1 \le u , v \le n, u\neq v)\) meaning that there is an edge between the vertices \(u\) and \(v\) in Parsa's tree.It is guaranteed that the given graph is a tree.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
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 string that is formed by all the letters in the cells the path visits. Thus, the length of each string is \(2n - 1\).Find the lexicographically smallest string that can be associated with a path after changing letters in at most \(k\) cells of the matrix.A string \(a\) is lexicographically smaller than a string \(b\), if the first different letter in \(a\) and \(b\) is smaller in \(a\).
|
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 they would upvote or downvote. Note that each of the \(x+y+z\) people would vote exactly one time.There are three different results: if there are more people upvote than downvote, the result will be ""+""; if there are more people downvote than upvote, the result will be ""-""; otherwise the result will be ""0"".Because of the \(z\) unknown persons, the result may be uncertain (i.e. there are more than one possible results). More formally, the result is uncertain if and only if there exist two different situations of how the \(z\) persons vote, that the results are different in the two situations.Tell Nauuo the result or report that the result is uncertain.
|
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 definitely get one upvote and one downvote, so the only possible result is ""0"".In the fourth example, if the only one person upvoted, the result would be ""+"", otherwise, the result would be ""-"". There are two possible results, so the result is uncertain.
|
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 performing this operation any number of times?
|
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 resulting coins can be transformed into two coins with value \(1\).
|
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 up to (xn, tn). Interesting events are short so we can assume they are immediate. Event i counts visited if at time ti tourist was at point with coordinate xi.Write program tourist that will find maximum number of events tourist if: at the beginning (when time is equal to 0) tourist appears at point 0, tourist can choose initial point for himself. Yes, you should answer on two similar but different questions.
|
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 β€ 2Β·108, all ti will be between 1 and 2Β·106 inclusive. V will be positive and will not exceed 1000. The input may contain events that happen at the same time or in the same place but not in the same place at the same time.
|
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 display one position to the right (the last digit becomes the first one). For example, if the display is currently showing number 579, then if we push the first button, the display will show 680, and if after that we push the second button, the display will show 068.You know that the lock will open if the display is showing the smallest possible number that can be obtained by pushing the buttons in some order. The leading zeros are ignored while comparing numbers. Now your task is to find the desired number.
|
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 magic creature and gives them to the caster.Now to simplify the problem, assume you have n ponies (numbered from 1 to n). Each pony has three attributes: si : amount of mana that the pony has at time 0; mi : maximum mana that the pony can have; ri : mana regeneration per unit time. Lord Tirek will do m instructions, each of them can be described with three integers: ti, li, ri. The instruction means that at time ti, Tirek will use Absorb Mana on ponies with numbers from li to ri (both borders inclusive). We'll give you all the m instructions in order, count how much mana Tirek absorbs for each instruction.
|
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, li, ri (0 β€ ti β€ 109; 1 β€ li β€ ri β€ n), describing an instruction of Lord Tirek. The instructions are given in strictly increasing order of ti (all ti are distinct).
|
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 block falls until it comes in contact with the flat surface or any other block. Let's define that block \(a\) covers block \(b\) if \(l_a \le l_b \le r_b \le r_a\). Consider what happens when a new block \(i\) falls. If the new (upper) block \(i\) comes in contact with any block \(j\) such that block \(i\) does not cover block \(j\), block \(i\) will stick to block \(j\), and no blocks will disappear. Otherwise, all blocks that block \(i\) covers and is in contact with will be vaporized, and block \(i\) will continue falling with the ability to vaporize lower blocks.For example, consider what happens when three blocks \((1,2)\), \((2,3)\) and \((1,3)\) fall, in that order. The first block will stick to the flat surface. Then, the second block will stick to the first block. Finally, the third block will vaporize the second block, keep falling, vaporize the first block, and stick to the flat surface. Here is a graphic for the first example. After each block falls, help Polycarp determine how many blocks will remain!
|
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 block \(1\) and is in contact with it. Block \(4\) will vaporize all the blocks and stick to the flat surface. Block \(5\) will stick to block \(4\) Block \(6\) will stick to block \(5\). Block \(7\) will stick to block \(6\). Note that no blocks are vaporized because although block \(7\) covers block \(4\) and block \(5\), they never come in contact. Block \(8\) vaporizes block \(7\) and sticks to block \(6\).
|
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 more tokens is considered the winner of the game (in case of a tie, the winner is chosen randomly); the winning player takes all of the loser's tokens; The last player with non-zero tokens is the winner of the championship.All random decisions that are made during the championship are made equally probable and independently.For example, if \(n=4\), \(a = [1, 2, 4, 3]\), then one of the options for the game (there could be other options) is: during the first game, the first and fourth players were selected. The fourth player has more tokens, so he takes the first player's tokens. Now \(a = [0, 2, 4, 4]\); during the second game, the fourth and third players were selected. They have the same number of tokens, but in a random way, the third player is the winner. Now \(a = [0, 2, 8, 0]\); during the third game, the second and third players were selected. The third player has more tokens, so he takes the second player's tokens. Now \(a = [0, 0, 10, 0]\); the third player is declared the winner of the championship. Championship winners will receive personalized prizes. Therefore, the judges want to know in advance which players have a chance of winning, i.e have a non-zero probability of winning the championship. You have been asked to find all such players.
|
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\) positive integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the number of tokens the players have.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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 by it. For each problem, \(p_i\) is an integer not less than \(1\).Two corporations specializing in problem-solving robot manufacturing, ""Robo-Coder Inc."" and ""BionicSolver Industries"", are going to register two robots (one for each corporation) for participation as well. Polycarp knows the advantages and flaws of robots produced by these companies, so, for each problem, he knows precisely whether each robot will solve it during the competition. Knowing this, he can try predicting the results β or manipulating them. For some reason (which absolutely cannot involve bribing), Polycarp wants the ""Robo-Coder Inc."" robot to outperform the ""BionicSolver Industries"" robot in the competition. Polycarp wants to set the values of \(p_i\) in such a way that the ""Robo-Coder Inc."" robot gets strictly more points than the ""BionicSolver Industries"" robot. However, if the values of \(p_i\) will be large, it may look very suspicious β so Polycarp wants to minimize the maximum value of \(p_i\) over all problems. Can you help Polycarp to determine the minimum possible upper bound on the number of points given for solving the problems?
|
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 problem.The third line contains \(n\) integers \(b_1\), \(b_2\), ..., \(b_n\) (\(0 \le b_i \le 1\)). \(b_i = 1\) means that the ""BionicSolver Industries"" robot will solve the \(i\)-th problem, \(b_i = 0\) means that it won't solve the \(i\)-th problem.
|
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 the ""BionicSolver Industries"" robot.
|
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 their points are equal.
|
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 that any two consecutive cells are adjacent by the side in the table. The sequence can have arbitrary length and should visit each cell one or more times. For a cell containing the number \(i\), denote the position of the first occurrence of this cell in the sequence as \(t_i\). The sequence solves the puzzle, if \(t_1 < t_2 < \dots < t_{nm}\). In other words, the cell with number \(x\) should be first visited before the cell with number \(x + 1\) for each \(x\).Let's call a puzzle solvable, if there exists at least one suitable sequence.In one move Serega can choose two arbitrary cells in the table (not necessarily adjacent by the side) and swap their numbers. He would like to know the minimum number of moves to make his puzzle solvable, but he is too impatient. Thus, please tell if the minimum number of moves is \(0\), \(1\), or at least \(2\). In the case, where \(1\) move is required, please also find the number of suitable cell pairs to swap.
|
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 table.
|
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), (1, 6), (2, 6)\). The puzzle from the third example requires at least two swaps, so the answer is \(2\).
|
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 the bishop are between \(2\) and \(7\), inclusive.)The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked. An example of a bishop on a chessboard. The squares it attacks are marked in red. Mihai has marked all squares the bishop attacks, but forgot where the bishop was! Help Mihai find the position of the bishop.
|
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 '.', denoting a square under attack and a square not under attack, respectively.
|
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 on the path from x to y (including both x and y). In other words if values attached to the cities on the path from x to y form an array p of length l then the distance between them is , where is bitwise xor operation.Mahmoud and Ehab want to choose two cities and make a journey from one to another. The index of the start city is always less than or equal to the index of the finish city (they may start and finish in the same city and in this case the distance equals the number attached to that city). They can't determine the two cities so they try every city as a start and every city with greater index as a finish. They want to know the total distance between all pairs of 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 (1 β€ u, v β€ n, u β v), denoting that there is an undirected road between cities u and v. It's guaranteed that you can reach any city from any other using these roads.
|
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://en.wikipedia.org/wiki/Bitwise_operation#XOR.In the first sample the available paths are: city 1 to itself with a distance of 1, city 2 to itself with a distance of 2, city 3 to itself with a distance of 3, city 1 to city 2 with a distance of , city 1 to city 3 with a distance of , city 2 to city 3 with a distance of . The total distance between all pairs of cities equals 1 + 2 + 3 + 3 + 0 + 1 = 10.
|
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 \le n\)), and the game master will reply with an array \(d_1, d_2, \ldots, d_n\), where \(d_i\) is the length of the shortest path from node \(r\) to \(i\), for all \(1 \le i \le n\).Additionally, to make the game unfair challenge Little Dormi the game master will allow at most \(\lceil\frac{n}{2}\rceil\) questions, where \(\lceil x \rceil\) denotes the smallest integer greater than or equal to \(x\).Faced with the stomach-churning possibility of not being able to guess the tree, Little Dormi needs your help to devise a winning strategy!Note that the game master creates the tree before the game starts, and does not change it during the game.
|
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 the output stream.You may output the edges in any order and an edge \((a,b)\) is considered the same as an edge \((b,a)\). Answering is not considered as a query.
|
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, here are the answers for querying every single node in example \(2\): \(1\): \([0,4,1,3,2]\) \(2\): \([4,0,3,1,2]\) \(3\): \([1,3,0,2,1]\) \(4\): \([3,1,2,0,1]\) \(5\): \([2,2,1,1,0]\)
|
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 result), he prepends the current word with \(s_3\) (i.e. writes down \(s_3\) to the left of the current result), he appends the current word with \(s_4\) (i.e. writes down \(s_4\) to the right of the current result), he prepends the current word with \(s_5\) (i.e. writes down \(s_5\) to the left of the current result), and so on for each position until the end of \(s\). For example, if \(s\)=""techno"" the process is: ""t"" \(\to\) ""te"" \(\to\) ""cte"" \(\to\) ""cteh"" \(\to\) ""ncteh"" \(\to\) ""ncteho"". So the encrypted \(s\)=""techno"" is ""ncteho"".Given string \(t\) β the result of encryption of some string \(s\). Your task is to decrypt it, i.e. find the string \(s\).
|
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 doesn't matter.
|
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 disks in such a way that: Disks that were tangent to each other remain tangent to each other. No two disks overlap in a region of positive area. The sum of all radii strictly decreases. The new radii are allowed to be arbitrary positive real numbers. The centers of the disks cannot be changed.
|
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 \(i\)-th disk.
|
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 of the disks (right). In the second sample, depicted below, there is no way to change the radii of the disks in the desired manner. Second sample.
|
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 hence the size of the sequence increases by 1) Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!
|
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 will be followed by a single integer ki (|ki| β€ 103). If ti = 3, it will not be followed by anything.It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.
|
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 racing where different teams compete in a country of n cities. Cities are numbered 1 through n. Every two distinct cities in the country are connected with one bidirectional road. Each competing team should introduce one driver and a set of cars.The competition is held in r rounds. In i-th round, drivers will start at city si and finish at city ti. Drivers are allowed to change their cars at most ki times. Changing cars can take place in any city in no time. One car can be used multiple times in one round, but total number of changes should not exceed ki. Drivers can freely choose their path to destination.PMP has prepared m type of purpose-built cars. Beside for PMPβs driving skills, depending on properties of the car and the road, a car traverses each road in each direction in different times. PMP Warriors wants to devise best strategies of choosing car and roads in each round to maximize the chance of winning the cup. For each round they want to find the minimum time required to finish it.
|
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 the time one car requires to traverse different roads. The k-th integer in j-th line of the i-th set is the time that i-th car requires to traverse the road from j-th city to k-th city. These matrices are not necessarily symmetric, but their diagonal is always zero.Next r lines contain description of the rounds. The i-th of these lines contains space-separated integers si, ti, ki (1 β€ si, ti β€ n, si β ti, 0 β€ ki β€ 1000) β the number of starting city, finishing city and the number of possible car changes in i-th round, correspondingly.
|
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 round which only needs two car changes.
|
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\leq i\leq n\) and output a sequence of operations to achieve the minimum cost.A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
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 n\)) β the permutation \(p\). It is guaranteed that \(p_1,p_2,\ldots,p_n\) is a permutation of \(1,2,\ldots,n\).The third line contains \(n\) integers \(q_1,q_2,\ldots,q_n\) (\(1\leq q_i\leq n\)) β the permutation \(q\). It is guaranteed that \(q_1,q_2,\ldots,q_n\) is a permutation of \(1,2,\ldots,n\).It is guaranteed that the sum of \(n^3\) over all test cases does not exceed \(10^6\).
|
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 length greater than \(n^2\).
|
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\) costing \(\min(|2-4|,|1-2|)=1\), resulting in \(p=[3,2,4,1]\). Swap \(p_1,p_3\) costing \(\min(|1-3|,|3-4|)=1\). Then \(p\) equals \(q\) with a cost of \(3\).
|
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 notation \((x, y)\) denotes the point at the intersection of the \(x\)-th horizontal line and \(y\)-th vertical line.Two points \((x_1,y_1)\) and \((x_2,y_2)\) are adjacent if and only if \(|x_1-x_2| + |y_1-y_2| = 1\). The grid formed by \(n=4\) horizontal lines and \(m=5\) vertical lines. Elena calls a sequence of points \(p_1, p_2, \ldots, p_g\) of length \(g\) a walk if and only if all the following conditions hold: The first point \(p_1\) in this sequence is \((1, 1)\). The last point \(p_g\) in this sequence is \((n, m)\). For each \(1 \le i < g\), the points \(p_i\) and \(p_{i+1}\) are adjacent. Note that the walk may contain the same point more than once. In particular, it may contain point \((1, 1)\) or \((n, m)\) multiple times.There are \(n(m-1)+(n-1)m\) segments connecting the adjacent points in Elena's grid. Elena wants to color each of these segments in blue or red color so that there exists a walk \(p_1, p_2, \ldots, p_{k+1}\) of length \(k+1\) such that out of \(k\) segments connecting two consecutive points in this walk, no two consecutive segments have the same color (in other words, for each \(1 \le i < k\), the color of the segment between points \(p_i\) and \(p_{i+1}\) differs from the color of the segment between points \(p_{i+1}\) and \(p_{i+2}\)). Please find any such coloring or report that there is no such coloring.
|
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 the number of segments in the walk Elena is looking for.
|
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 \(n\) lines of coloring description, output \(m-1\) space-separated characters. The \(j\)-th character in the \(i\)-th of these \(n\) lines should denote the color of the segment between points \((i,j)\) and \((i,j+1)\). Here, use 'B' to denote the blue color and 'R' to denote the red color.In each of the next \(n-1\) lines of coloring description, output \(m\) space-separated characters. The \(j\)-th character in the \(i\)-th of these \(n-1\) lines should denote the color of the segment between points \((i,j)\) and \((i+1,j)\). Similarly, use 'B' to denote the blue color and 'R' to denote the red color.You can output each letter in the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses, and both 'R' and 'r' are valid notation of red.
|
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 column (in a classical game the columns are traditionally indexed by letters). Each chess piece takes up exactly one field. To make a move is to move a chess piece, the pieces move by the following rules: A rook moves any number of fields horizontally or vertically. A bishop moves any number of fields diagonally. A king moves one field in any direction β horizontally, vertically or diagonally. The pieces move like that Petya is thinking about the following problem: what minimum number of moves is needed for each of these pieces to move from field (r1, c1) to field (r2, c2)? At that, we assume that there are no more pieces besides this one on the board. Help him solve this problem.
|
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 through 8.
|
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\). It's not guaranteed that all skill levels are distinct.Let's define skill level of a class as the median of skill levels of students of the class.As a principal of the school, you would like to assign each student to one of the \(2\) classes such that each class has odd number of students (not divisible by \(2\)). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.What is the minimum possible absolute difference you can achieve?
|
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 contains \(2n\) integers \(a_1, a_2, \dots, a_{2 n}\) (\(1 \le a_i \le 10^9\)) β skill levels of students.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
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 will be \(4\), and second with \([5, 1, 3]\), so that the skill level of the second class will be \(3\). Absolute difference will be \(|4 - 3| = 1\).Note that you can't assign like \([2, 3]\), \([6, 5, 4, 1]\) or \([]\), \([6, 5, 4, 1, 2, 3]\) because classes have even number of students.\([2]\), \([1, 3, 4]\) is also not possible because students with skills \(5\) and \(6\) aren't assigned to a class.In the third test you can assign the students in the following way: \([3, 4, 13, 13, 20], [2, 5, 8, 16, 17]\) or \([3, 8, 17], [2, 4, 5, 13, 13, 16, 20]\). Both divisions give minimal possible absolute difference.
|
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): User u and user v become friends. It means, u and v can talk with each other. The friendship is bidirectional. del_friend(u, v): Unfriend user u and user v. It means, u and v cannot talk with each other from then. count_online_friends(u): The function returns the number of friends of user u who are online at the moment. Because the messenger is being tested by some users numbered from 1 to n, there is no register method. This means, at the beginning, some users may be online, and some users may have friends.User ainta is going to make these functions, but before making the messenger public, he wants to know whether he is correct. Help ainta verify his code.
|
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 space-separated integers x1, x2, ..., xo (1 β€ xi β€ n) β the ids of the online users. It is guaranteed that these values are distinct.Each of the next m lines contains two space-separated integers ai and bi (1 β€ ai, bi β€ n; ai β bi) β the ids of two users who are friends at the beginning. It is guaranteed there are no multiple friendship given in the input. Note that the friendship is bidirectional.Next q lines describe the q queries in the format: ""O u"" (1 β€ u β€ n) : Call online(u). It is guaranteed that user u was offline just before the function call. ""F u"" (1 β€ u β€ n) : Call offline(u). It is guaranteed that user u was online just before the function call. ""A u v"" (1 β€ u, v β€ n; u β v) : Call add_friend(u, v). It is guaranteed that these two users weren't friends just before the function call. ""D u v"" (1 β€ u, v β€ n; u β v) : Call del_friend(u, v). It is guaranteed that these two users were friends just before the function call. ""C u"" (1 β€ u β€ n) : Call count_online_friends(u) and print the result in a single line.
|
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 color.Help Anya find the longest segment with this property.
|
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). In other words, \(f\) is a function that returns a positive integer for a pair of positive integers.Let's make an array \(a_1, a_2, \ldots, a_n\), where \(a_i = i\) initially.You will perform \(q\) operations, in \(i\)-th of them you will: assign \(t = f(a_{x_i}, a_{y_i})\) (\(t\) is a temporary variable, it is used only for the next two assignments); assign \(a_{x_i} = t\); assign \(a_{y_i} = t\). In other words, you need to simultaneously change \(a_{x_i}\) and \(a_{y_i}\) to \(f(a_{x_i}, a_{y_i})\). Note that during this process \(f(p, q)\) is always the same for a fixed pair of \(p\) and \(q\).In the end, there should be at most two different numbers in the array \(a\).It should be true for any function \(f\).Find any possible list of pairs. The number of pairs should not exceed \(5 \cdot 10^5\).
|
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 any of them.
|
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 have at most two different numbers.
|
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 \(a\) is \([2, 1, 4, 3]\), then the array \(b\) is equal to \(\left[ \left\lfloor \frac{1}{2} \right\rfloor, \left\lfloor \frac{2}{1} \right\rfloor, \left\lfloor \frac{3}{4} \right\rfloor, \left\lfloor \frac{4}{3} \right\rfloor \right] = [0, 2, 0, 1]\).Unfortunately, the Monocarp has lost his permutation, so he wants to restore it. Your task is to find a permutation \(a\) that corresponds to the given array \(b\). If there are multiple possible permutations, then print any of them. The tests are constructed in such a way that least one suitable permutation exists.
|
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 of \(n\) over test cases does not exceed \(5 \cdot 10^5\); there exists at least one permutation \(a\) that would yield this array \(b\).
|
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 you help Gargari?You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem
|
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 and first column of the grid.During her/his move, a player can do one of the following two operations: Move the rook to a different cell on the same row or the same column of the current cell. A player cannot move the rook to a cell that has been visited \(1000\) times before (i.e., the rook can stay in a certain cell at most \(1000\) times during the entire game). Note that the starting cell is considered to be visited once at the beginning of the game. End the game immediately with a score of \(a_r+b_c\), where \((r, c)\) is the current cell (i.e., the rook is on the \(r\)-th row and \(c\)-th column). Bob wants to maximize the score while Alice wants to minimize it. If they both play this game optimally, what is the final score of the game?
|
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 contains the \(m\) integers \(b_1, b_2,\dots, b_n\) (\(1 \leq b_i \leq 5 \cdot 10^8\)).
|
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\).In the second test case, the final score of the game is \(a_3+b_5\).
|
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\). If it is impossible, report so.\(^\dagger\) An adjacent pair of cells with different characters is a pair of cells \((r_1, c_1)\) and \((r_2, c_2)\) (\(1 \le r_1, r_2 \le 2\), \(1 \le c_1, c_2 \le n\)) such that \(|r_1 - r_2| + |c_1 - c_2| = 1\) and the characters in \((r_1, c_1)\) and \((r_2, c_2)\) are different.
|
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 of adjacent pairs of cells with different characters required.The following line contains string \(s\) of \(n\) characters (\(s_i\) is either A or B) β Miriany's top row of the matchstick.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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 of them.
|
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 \cr \hline \end{array}\) The pairs of different characters are shown below in red: \(\begin{array}{|c|c|} \hline \color{red}{A} & A & A & A \cr \hline \color{red}{B} & A & B & B \cr \hline \end{array}\)βββββββββββββββββ\(\begin{array}{|c|c|} \hline A & A & \color{red}{A} & A \cr \hline B & A & \color{red}{B} & B \cr \hline \end{array}\)βββββββββββββββββ\(\begin{array}{|c|c|} \hline A & A & A & \color{red}{A} \cr \hline B & A & B & \color{red}{B} \cr \hline \end{array}\)βββββββββββββββββ\(\begin{array}{|c|c|} \hline A & A & A & A \cr \hline \color{red}{B} & \color{red}{A} & B & B \cr \hline \end{array}\)βββββββββββββββββ\(\begin{array}{|c|c|} \hline A & A & A & A \cr \hline B & \color{red}{A} & \color{red}{B} & B \cr \hline \end{array}\)There are a total of \(5\) pairs, which satisfies \(k\).
|
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 of stickers as she wants, and may specify any string of length n for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form s. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy.
|
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 instead a single line with the number -1.
|
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 rebel spies have provided her with the maps of the solar system. Each map is an N Γ N grid, where each cell is either occupied by a star or empty. To see whether the information is correct, Heidi needs to know whether the two maps are of the same solar system, or if possibly one of the spies is actually an Empire double agent, feeding her false information.Unfortunately, spies may have accidentally rotated a map by 90, 180, or 270 degrees, or flipped it along the vertical or the horizontal axis, before delivering it to Heidi. If Heidi can rotate or flip the maps so that two of them become identical, then those maps are of the same solar system. Otherwise, there are traitors in the Rebel ranks! Help Heidi find out.
|
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\) vertices, numbered from \(1\) to \(n\), with vertex \(1\) as the root, and Chloe can select a non-negative integer \(d\) to create a perfect binary tree\(^{\text{β }}\) of depth \(d\).Since Penchick and Chloe are good friends, Chloe wants her tree to be isomorphic\(^{\text{β‘}}\) to Penchick's tree. To meet this condition, Chloe can perform the following operation on her own tree any number of times: Select an edge \((u,v)\), where \(u\) is the parent of \(v\). Remove vertex \(v\) and all the edges connected to \(v\), then connect all of \(v\)'s previous children directly to \(u\). In particular, doing an operation on an edge \((u, v)\) where \(v\) is a leaf will delete vertex \(v\) without adding any new edges.Since constructing a perfect binary tree can be time-consuming, Chloe wants to choose the minimum \(d\) such that a perfect binary tree of depth \(d\) can be made isomorphic to Penchick's tree using the above operation. Note that she can't change the roots of the trees.\(^{\text{β}}\)A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root. The parent of vertex \(v\) is the first vertex on the simple path from \(v\) to the root. The root has no parent. A child of vertex \(v\) is any vertex \(u\) for which \(v\) is the parent. A leaf is any vertex without children. \(^{\text{β }}\)A full binary tree is rooted tree, in which each node has \(0\) or \(2\) children. A perfect binary tree is a full binary tree in which every leaf is at the same distance from the root. The depth of such a tree is the distance from the root to a leaf.\(^{\text{β‘}}\)Two rooted trees, rooted at \(r_1\) and \(r_2\) respectively, are considered isomorphic if there exists a permutation \(p\) of the vertices such that an edge \((u, v)\) exists in the first tree if and only if the edge \((p_u, p_v)\) exists in the second tree, and \(p_{r_1} = r_2\).
|
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 test case contains \(n-1\) integers \(p_2, p_3, \ldots, p_n\) (\(1 \leq p_i \leq i-1\)) β the parent of vertex \(i\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
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 operations carried out on a binary tree of depth less than \(2\) can lead to a tree isomorphic to the tree given in the input. In the second test case, the tree is already isomorphic to a perfect binary tree of depth \(3\).
|
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 array in no more than 40 queries.A subsegment \(a[l..r]\) is all the elements \(a_l, a_{l + 1}, ..., a_r\). After asking this subsegment you will be given the position of the second maximum from this subsegment in the whole array.
|
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 produce the same interaction, and the answer for them might be different. Example output is given in purpose of understanding the interaction.
|
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 the \(i\)-th box has a width of \(w_{i}\), a length of \(l_{i}\), and a height of \(h_{i}\).For each of the \(m\) boxes, you need to determine whether all the cubes can fit inside that box. The cubes must be placed in the box following these rules: The cubes can only be stacked in the box such that the sides of the cubes are parallel to the sides of the box; Every cube must be placed either on the bottom of the box or on top of other cubes in such a way that all space below the cube is occupied; A larger cube cannot be placed on top of a smaller cube.
|
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 number of cubes and the number of empty boxes.The next \(m\) lines of each test case contain \(3\) integers \(w_{i}\), \(l_{i}\), and \(h_{i}\) (\(1 \le w_{i}, l_{i}, h_{i} \le 150\)) β the dimensions of the \(i\)-th box.Additional constraints on the input: The sum of \(m\) across all test cases does not exceed \(2 \cdot 10^{5}\).
|
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 morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai β how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.
|
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 one cookie.The third line contains the sequence b1, b2, ..., bn (1 β€ bi β€ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
|
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 with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
|
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 resulting rectangle has the maximum possible area. The rectangle can be degenerate, i. e. its area can be \(0\). Each integer can be used as many times as it occurs in the list (or less).
|
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 on the input: the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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_2, y_2, x_3, y_3, x_4, y_4\) β the coordinates of the corners of the rectangle. You can print the corners in any order.
|
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 should be either a prime number, or equal to 1.Vishtasp said that he would share his Eidi 50 / 50 with Goshtasp, if he could solve the task. Eidi is money given to children for Noruz by their parents and/or relatives.Goshtasp needs to solve this problem to get money, you need to solve it to get score!
|
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 ai < bi then a is lexicographically earlier and if bi < ai then b is lexicographically earlier. If m β n we add zeroes at the end of the smaller sequence (only for the moment of comparison) and then perform the comparison.You do not need to minimize the number of elements in sequence (i.e. m). You just need to print the lexicographically latest solution.See samples to find out how to print the sequence.
|
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 \(2\) one-way staircases which lead from \((i,j)\) to \((i+1,j)\) and from \((i,j)\) to \((i+1,j+1)\) respectively.In each room you can either put a torch or leave it empty. Define the brightness of a room \((i, j)\) to be the number of rooms with a torch from which you can reach the room \((i, j)\) through a non-negative number of staircases.For example, when \(n=5\) and torches are placed in the rooms \((1,1)\), \((2,1)\), \((3,2)\), \((4,1)\), \((4,3)\), and \((5,3)\), the pyramid can be illustrated as follows: In the above picture, rooms with torches are colored in yellow, and empty rooms are white. The blue numbers in the bottom-right corner indicate the brightness of the rooms.The room \((4,2)\) (the room with a star) has brightness \(3\). In the picture below, the rooms from where you can reach \((4,2)\) have red border. The brightness is \(3\) since there are three torches among these rooms. The pyramid is called nice if and only if for all floors, all rooms in the floor have the same brightness.Define the brilliance of a nice pyramid to be the sum of brightness over the rooms \((1,1)\), \((2,1)\), \((3,1)\), ..., \((n,1)\).Find an arrangement of torches in the pyramid, such that the resulting pyramid is nice and its brilliance is maximized.We can show that an answer always exists. If there are multiple answers, output any one of them.
|
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 \(n\) over all test cases does not exceed \(500\).
|
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 are multiple answers, output any one of them.
|
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 arrangements with \(n=3\) will have a greater brilliance.
|
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 number of possible pictures that Paul can take.Two pictures are considered to be different if the coordinates of corresponding rectangles are different.
|
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 yi (1 β€ xi β€ r, 1 β€ yi β€ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
|
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 orchestra looks as follows #**##* Paul must take a photograph of the entire section.In the third sample, the orchestra looks the same as in the second sample.
|
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 by 1.You have to find expected value of res at the end of the process. It can be proved that the expected value of res can be represented as an irreducible fraction . You have to find .
|
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 throws them away before going to bed as he doesn't want to wash them.Vasya wonders, what is the maximum number of days when he can dress fashionable and wear different socks, and after that, for how many days he can then wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.Can you help him?
|
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 been wearing on that day.
|
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 equal to the sum of all elements of the subsequence. You are given a final array \(c\). Check if \(c\) can be obtained from the initial array \(a\) by performing some number (possibly 0) of operations on the initial array.\(^{\dagger}\) A sequence \(b\) is a subsequence of a sequence \(a\) if \(b\) can be obtained from \(a\) by the deletion of several (possibly zero, but not all) elements. In other words, select \(k\) (\(1 \leq k \leq |a|\)) distinct indices \(i_1, i_2, \dots, i_k\) and insert anywhere into \(a\) a new element with the value equal to \(a_{i_1} + a_{i_2} + \dots + a_{i_k}\).
|
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 each test case contains \(n\) space-separated integers \(c_i\) (\(1 \leq c_i \leq 5000\)) β the elements of the final array \(c\) that should be obtained from the initial array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5000\).
|
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 is ""NO"".For the third test case, we can perform the following operations in order to obtain the final given array \(c\): Initially, \(a = [1]\). By choosing the subsequence \([1]\), and inserting \(1\) in the array, \(a\) changes to \([1, 1]\). By choosing the subsequence \([1, 1]\), and inserting \(1+1=2\) in the middle of the array, \(a\) changes to \([1, 2, 1]\). By choosing the subsequence \([1, 2]\), and inserting \(1+2=3\) after the first \(1\) of the array, \(a\) changes to \([1, 3, 2, 1]\). By choosing the subsequence \([1, 3, 1]\) and inserting \(1+3+1=5\) at the beginning of the array, \(a\) changes to \([5, 1, 3, 2, 1]\) (which is the array we needed to obtain).
|
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"", ""conservatoire"", ""refrigerator"", ""stopwatch"", ""windowsill"", ""electricity"", ""government"" and so on. Of course, we can continue listing those words ad infinitum. Fortunately, the solution for that problem has been found. To make our speech clear and brief, we should replace the initial words with those that resemble them but are much shorter. This idea hasn't been brought into life yet, that's why you are chosen to improve the situation. Let's consider the following formal model of transforming words: we shall assume that one can use n words in a chat. For each words we shall introduce a notion of its shorter variant. We shall define shorter variant of an arbitrary word s as such word t, that meets the following conditions: it occurs in s as a subsequence, its length ranges from one to four characters. In other words, the word t consists at least of one and at most of four characters that occur in the same order in the word s. Note that those characters do not necessarily follow in s immediately one after another. You are allowed not to shorten the initial word if its length does not exceed four characters.You are given a list of n different words. Your task is to find a set of their shortened variants. The shortened variants of all words from the list should be different.
|
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 difference sequence. Formally, \([a_1,a_2,\ldots,a_n]\) becomes \([a_2-a_1,a_3-a_2,\ldots,a_n-a_{n-1}]\) after the operation. Find the maximum possible sum of elements of \(a\) after all operations.
|
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 1000\)) β the sequence \(a\).
|
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 case, Cirno can choose not to operate, so the answer is \(1001\).
|
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 free (i.e. replace the current coordinate \(x_i\) with \(x_i - 2\) or with \(x_i + 2\)); move the chip \(i\) by \(1\) to the left or \(1\) to the right and pay one coin for this move (i.e. replace the current coordinate \(x_i\) with \(x_i - 1\) or with \(x_i + 1\)). Note that it's allowed to move chips to any integer coordinate, including negative and zero.Your task is to find the minimum total number of coins required to move all \(n\) chips to the same coordinate (i.e. all \(x_i\) should be equal after some sequence of moves).
|
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 the answer is \(2\).
|
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, you can move the chip to the neighboring cell, but: you can move only right or down. In other words, if the current cell is \((x, y)\), you can move either to \((x, y + 1)\) or to \((x + 1, y)\). There are two special cases: if the chip is in the last column (cell \((x, n)\)) and you're moving right, you'll teleport to the first column (to the cell \((x, 1)\)); if the chip is in the last row (cell \((n, y)\)) and you're moving down, you'll teleport to the first row (to the cell \((1, y)\)). you cannot visit the same cell twice. The starting cell is counted visited from the beginning (so you cannot enter it again), and you can't leave the finishing cell once you visit it. Your total score is counted as the sum of numbers in all cells you have visited. What is the maximum possible score you can achieve?
|
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 number written in the cell \((i, j)\).
|
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 \(j\)-th position is \(b_j\). All the special positions are also distinct.In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on 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 second line of each testcase contains \(n\) distinct integers in the increasing order \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_1 < a_2 < \dots < a_n \le 10^9\); \(a_i \neq 0\)) β the initial positions of the boxes.The third line of each testcase contains \(m\) distinct integers in the increasing order \(b_1, b_2, \dots, b_m\) (\(-10^9 \le b_1 < b_2 < \dots < b_m \le 10^9\); \(b_i \neq 0\)) β the special positions.The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\). The sum of \(m\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
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]\), respectively. Among them positions \([-2, 6, 7, 15]\) are special, thus, the answer is \(4\).In the second testcase you can push the box from \(-1\) to \(-10^9\), then the box from \(1\) to \(10^9\) and obtain the answer \(2\).The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer \(3\).In the fifth testcase there are fewer special positions than boxes. You can move either \(8\) or \(9\) to the right to have some box on position \(10\).
|
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 need exactly \(w \times h\) tiles without any scraps. That is, the width of the kitchen is \(w\) tiles, and the height is \(h\) tiles. As each tile can be rotated in one of four ways, he still needs to decide on how exactly he will tile the floor. There is a single aesthetic criterion that he wants to fulfil: two adjacent tiles must not share a colour on the edge β i.e. one of the tiles must have a white colour on the shared border, and the second one must be black. The picture on the left shows one valid tiling of a \(3 \times 2\) kitchen. The picture on the right shows an invalid arrangement, as the bottom two tiles touch with their white parts. Find the number of possible tilings. As this number may be large, output its remainder when divided by \(998244353\) (a prime number).
|
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 will residents of Berland wait till the next lucky year.
|
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 fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins. You have to calculate how many fights will happen and who will win the game, or state that game won't end.
|
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 number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.All card values are different.
|
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.