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,904 |
A
|
1904A
|
A. Forked!
| 900 |
brute force; implementation
|
Lunchbox is done with playing chess! His queen and king just got forked again!In chess, a fork is when a knight attacks two pieces of higher value, commonly the king and the queen. Lunchbox knows that knights can be tricky, and in the version of chess that he is playing, knights are even trickier: instead of moving \(1\) tile in one direction and \(2\) tiles in the other, knights in Lunchbox's modified game move \(a\) tiles in one direction and \(b\) tiles in the other.Lunchbox is playing chess on an infinite chessboard which contains all cells \((x,y)\) where \(x\) and \(y\) are (possibly negative) integers. Lunchbox's king and queen are placed on cells \((x_K,y_K)\) and \((x_Q,y_Q)\) respectively. Find the number of positions such that if a knight was placed on that cell, it would attack both the king and queen.
|
Each test contains multiple test cases. The first line 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 two integers \(a\) and \(b\) (\(1 \le a, b \le 10^8\)) β describing the possible moves of the knight.The second line of each test case contains two integers \(x_K\) and \(y_K\) (\(0 \le x_K, y_K \le 10^8\)) β the position of Lunchbox's king. The third line in a test case contains \(x_Q\) and \(y_Q\) (\(0 \le x_Q, y_Q \le 10^8\)) β the position of Lunchbox's queen.It is guaranteed that Lunchbox's queen and king will occupy different cells. That is, \((x_K,y_K) \neq (x_Q,y_Q)\).
|
For each test case, output the number of positions on an infinite chessboard such that a knight can attack both the king and the queen.
|
In the first test case, the knight can move 2 squares in one direction and 1 square in the other (it is essentially the same as the knight in standard chess). A knight placed on \((2, 1)\) or \((1, 2)\) would attack both the king and queen. Example of a knight placement that forks the queen and king in the first test case. The squares that the knight attacks are highlighted in red. In the second test case, a knight placed on \((2, 2)\) would attack both the king and queen. Example of a knight placement that does not fork the queen and king in the second test case. The knight attacks the king but not the queen. In the third test case, a knight placed on \((4, 4)\) or \((4, -4)\) would attack both the king and queen. In the fourth test case, there are no positions where the knight can attack both the king and the queen.(Credits to EnDeRBeaT for the nice images)
|
Input: 42 10 03 31 13 11 34 40 08 04 21 43 4 | Output: 2 1 2 0
|
Beginner
| 2 | 825 | 699 | 135 | 19 |
1,663 |
G
|
1663G
|
G. Six Characters
| 0 |
*special; constructive algorithms; strings
|
Aenar should go to the string's home.
|
A string consisting of only \(6\) letters.
|
A string consisting of only \(6\) characters.
|
If many answers are possible, the jury will still only accept one!
|
Beginner
| 3 | 37 | 42 | 45 | 16 |
|
2,060 |
D
|
2060D
|
D. Subtract Min Sort
| 1,100 |
greedy
|
You are given a sequence \(a\) consisting of \(n\) positive integers.You can perform the following operation any number of times. Select an index \(i\) (\(1 \le i < n\)), and subtract \(\min(a_i,a_{i+1})\) from both \(a_i\) and \(a_{i+1}\). Determine if it is possible to make the sequence non-decreasing by using the operation any number of times.
|
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 second line of each test case contains \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
If it is possible to make the sequence non-decreasing, print ""YES"" on a new line. Otherwise, print ""NO"" on a new line.You can output the answer in any case. For example, the strings ""yEs"", ""yes"", and ""Yes"" will also be recognized as positive responses.
|
In the first test case, the array is already sorted.In the second test case, we can show that it is impossible.In the third test case, after performing an operation on \(i=1\), the array becomes \([0,1,2,3]\), which is now in nondecreasing order.
|
Input: 551 2 3 4 544 3 2 144 5 2 384 5 4 5 4 5 4 599 9 8 2 4 4 3 5 3 | Output: YES NO YES YES NO
|
Easy
| 1 | 348 | 440 | 262 | 20 |
1,000 |
B
|
1000B
|
B. Light It Up
| 1,500 |
greedy
|
Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment \(0\) and turn power off at moment \(M\). Moreover, the lamp allows you to set a program of switching its state (states are ""lights on"" and ""lights off""). Unfortunately, some program is already installed into the lamp.The lamp allows only good programs. Good program can be represented as a non-empty array \(a\), where \(0 < a_1 < a_2 < \dots < a_{|a|} < M\). All \(a_i\) must be integers. Of course, preinstalled program is a good program.The lamp follows program \(a\) in next manner: at moment \(0\) turns power and light on. Then at moment \(a_i\) the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment \(1\) and then do nothing, the total time when the lamp is lit will be \(1\). Finally, at moment \(M\) the lamp is turning its power off regardless of its state.Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program \(a\), so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of \(a\), or even at the begining or at the end of \(a\).Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from \(x\) till moment \(y\), then its lit for \(y - x\) units of time. Segments of time when the lamp is lit are summed up.
|
First line contains two space separated integers \(n\) and \(M\) (\(1 \le n \le 10^5\), \(2 \le M \le 10^9\)) β the length of program \(a\) and the moment when power turns off.Second line contains \(n\) space separated integers \(a_1, a_2, \dots, a_n\) (\(0 < a_1 < a_2 < \dots < a_n < M\)) β initially installed program \(a\).
|
Print the only integer β maximum possible total time when the lamp is lit.
|
In the first example, one of possible optimal solutions is to insert value \(x = 3\) before \(a_1\), so program will be \([3, 4, 6, 7]\) and time of lamp being lit equals \((3 - 0) + (6 - 4) + (10 - 7) = 8\). Other possible solution is to insert \(x = 5\) in appropriate place.In the second example, there is only one optimal solution: to insert \(x = 2\) between \(a_1\) and \(a_2\). Program will become \([1, 2, 10]\), and answer will be \((1 - 0) + (10 - 2) = 9\).In the third example, optimal answer is to leave program untouched, so answer will be \((3 - 0) + (7 - 4) = 6\).
|
Input: 3 104 6 7 | Output: 8
|
Medium
| 1 | 1,771 | 327 | 74 | 10 |
835 |
B
|
835B
|
B. The number on the board
| 1,100 |
greedy
|
Some natural number was written on the board. Its sum of digits was not less than k. But you were distracted a bit, and someone changed this number to n, replacing some digits with others. It's known that the length of the number didn't change.You have to find the minimum number of digits in which these two numbers can differ.
|
The first line contains integer k (1 β€ k β€ 109).The second line contains integer n (1 β€ n < 10100000).There are no leading zeros in n. It's guaranteed that this situation is possible.
|
Print the minimum number of digits in which the initial number and n can differ.
|
In the first example, the initial number could be 12.In the second example the sum of the digits of n is not less than k. The initial number could be equal to n.
|
Input: 311 | Output: 1
|
Easy
| 1 | 328 | 183 | 80 | 8 |
1,100 |
D
|
1100D
|
D. Dasha and Chess
| 2,500 |
constructive algorithms; games; interactive
|
This is an interactive task.Dasha and NN like playing chess. While playing a match they decided that normal chess isn't interesting enough for them, so they invented a game described below.There are \(666\) black rooks and \(1\) white king on the chess board of size \(999 \times 999\). The white king wins if he gets checked by rook, or, in other words, if he moves onto the square which shares either a row or column with a black rook.The sides take turns, starting with white. NN plays as a white king and on each of his turns he moves a king to one of the squares that are adjacent to his current position either by side or diagonally, or, formally, if the king was on the square \((x, y)\), it can move to the square \((nx, ny)\) if and only \(\max (|nx - x|, |ny - y|) = 1\) , \(1 \leq nx, ny \leq 999\). NN is also forbidden from moving onto the squares occupied with black rooks, however, he can move onto the same row or column as a black rook.Dasha, however, neglects playing by the chess rules, and instead of moving rooks normally she moves one of her rooks on any space devoid of other chess pieces. It is also possible that the rook would move onto the same square it was before and the position wouldn't change. However, she can't move the rook on the same row or column with the king.Each player makes \(2000\) turns, if the white king wasn't checked by a black rook during those turns, black wins. NN doesn't like losing, but thinks the task is too difficult for him, so he asks you to write a program that will always win playing for the white king. Note that Dasha can see your king and play depending on its position.
|
In the beginning your program will receive \(667\) lines from input. Each line contains two integers \(x\) and \(y\) (\(1 \leq x, y \leq 999\)) β the piece's coordinates. The first line contains the coordinates of the king and the next \(666\) contain the coordinates of the rooks. The first coordinate denotes the number of the row where the piece is located, the second denotes the column. It is guaranteed that initially the king isn't in check and that all pieces occupy different squares.
|
After getting king checked, you program should terminate immediately without printing anything extra.
|
The example is trimmed. The full initial positions of the rooks in the first test are available at https://pastebin.com/qQCTXgKP. It is not guaranteed that they will behave as in the example.
|
Input: 999 9991 11 22 12 21 32 3<...>26 1326 1426 1526 161 700 8002 1 2<...>-1 -1 -1 | Output: 999 998999 997<...>999 26
|
Expert
| 3 | 1,637 | 493 | 101 | 11 |
960 |
C
|
960C
|
C. Subsequence Counting
| 1,700 |
bitmasks; constructive algorithms; greedy; implementation
|
Pikachu had an array with him. He wrote down all the non-empty subsequences of the array on paper. Note that an array of size n has 2n - 1 non-empty subsequences in it. Pikachu being mischievous as he always is, removed all the subsequences in which Maximum_element_of_the_subsequence - Minimum_element_of_subsequence β₯ dPikachu was finally left with X subsequences. However, he lost the initial array he had, and now is in serious trouble. He still remembers the numbers X and d. He now wants you to construct any such array which will satisfy the above conditions. All the numbers in the final array should be positive integers less than 1018. Note the number of elements in the output array should not be more than 104. If no answer is possible, print - 1.
|
The only line of input consists of two space separated integers X and d (1 β€ X, d β€ 109).
|
Output should consist of two lines.First line should contain a single integer n (1 β€ n β€ 10 000)β the number of integers in the final array.Second line should consist of n space separated integers β a1, a2, ... , an (1 β€ ai < 1018).If there is no answer, print a single integer -1. If there are multiple answers, print any of them.
|
In the output of the first example case, the remaining subsequences after removing those with Maximum_element_of_the_subsequence - Minimum_element_of_subsequence β₯ 5 are [5], [5, 7], [5, 6], [5, 7, 6], [50], [7], [7, 6], [15], [6], [100]. There are 10 of them. Hence, the array [5, 50, 7, 15, 6, 100] is valid.Similarly, in the output of the second example case, the remaining sub-sequences after removing those with Maximum_element_of_the_subsequence - Minimum_element_of_subsequence β₯ 2 are [10], [100], [1000], [10000]. There are 4 of them. Hence, the array [10, 100, 1000, 10000] is valid.
|
Input: 10 5 | Output: 65 50 7 15 6 100
|
Medium
| 4 | 759 | 89 | 331 | 9 |
803 |
B
|
803B
|
B. Distances to Zero
| 1,200 |
constructive algorithms
|
You are given the array of integer numbers a0, a1, ..., an - 1. For each element find the distance to the nearest zero (to the element which equals to zero). There is at least one zero element in the given array.
|
The first line contains integer n (1 β€ n β€ 2Β·105) β length of the array a. The second line contains integer elements of the array separated by single spaces ( - 109 β€ ai β€ 109).
|
Print the sequence d0, d1, ..., dn - 1, where di is the difference of indices between i and nearest j such that aj = 0. It is possible that i = j.
|
Input: 92 1 0 3 0 0 3 2 4 | Output: 2 1 0 1 0 0 1 2 3
|
Easy
| 1 | 212 | 177 | 146 | 8 |
|
1,181 |
E1
|
1181E1
|
E1. A Story of One Country (Easy)
| 2,500 |
brute force; divide and conquer; sortings
|
This problem differs from the next problem only in constraints.Petya decided to visit Byteland during the summer holidays. It turned out that the history of this country is quite unusual.Initially, there were \(n\) different countries on the land that is now Berland. Each country had its own territory that was represented as a rectangle on the map. The sides of the rectangle were parallel to the axes, and the corners were located at points with integer coordinates. Territories of no two countries intersected, but it was possible that some territories touched each other. As time passed, sometimes two countries merged into one. It only happened if the union of their territories was also a rectangle. In the end only one country remained β Byteland.Initially, each country had a rectangular castle inside its territory. Its sides were parallel to the axes and its corners had integer coordinates. Some castles might touch the border of the corresponding country and sides or other castles. Miraculously, after all the unions the castles are still intact. Unfortunately, their locations are the only information we have to restore the initial territories of the countries. The possible formation of Byteland. The castles are shown in blue. Petya wonders why no information about the initial countries remained. He suspected that the whole story is a fake. You were recommended to him as a smart person. Please check whether or not there exists a possible set of initial territories that could make the story true.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 1000\)) β the number of countries and castles.Each of the next \(n\) lines contains four integers \(a_i, b_i, c_i, d_i\) (\(0 \leq a_i < c_i \leq 10^9\), \(0 \leq b_i < d_i \leq 10^9\)) β the coordinates of the \(i\)-th castle, where \((a_i, b_i)\) are the coordinates of the lower left corner and \((c_i, d_i)\) are the coordinates of the upper right corner.It is guaranteed, that no two castles intersect, however, they may touch.
|
If there exists a possible set of territories that satisfies the story, print ""YES"", otherwise print ""NO"".You can print each letter in any case (upper or lower).
|
The castles in the first and second examples are shown on the pictures below.
|
Input: 4 0 0 1 2 0 2 1 3 1 0 2 1 1 1 2 3 | Output: YES
|
Expert
| 3 | 1,518 | 496 | 165 | 11 |
1,817 |
C
|
1817C
|
C. Similar Polynomials
| 2,400 |
combinatorics; math
|
A polynomial \(A(x)\) of degree \(d\) is an expression of the form \(A(x) = a_0 + a_1 x + a_2 x^2 + \dots + a_d x^d\), where \(a_i\) are integers, and \(a_d \neq 0\). Two polynomials \(A(x)\) and \(B(x)\) are called similar if there is an integer \(s\) such that for any integer \(x\) it holds that$$$\( B(x) \equiv A(x+s) \pmod{10^9+7}. \)\(For two similar polynomials \)A(x)\( and \)B(x)\( of degree \)d\(, you're given their values in the points \)x=0,1,\dots, d\( modulo \)10^9+7\(.Find a value \)s\( such that \)B(x) \equiv A(x+s) \pmod{10^9+7}\( for all integers \)x$$$.
|
The first line contains a single integer \(d\) (\(1 \le d \le 2\,500\,000\)).The second line contains \(d+1\) integers \(A(0), A(1), \ldots, A(d)\) (\(0 \le A(i) < 10^9+7\)) β the values of the polynomial \(A(x)\).The third line contains \(d+1\) integers \(B(0), B(1), \ldots, B(d)\) (\(0 \le B(i) < 10^9+7\)) β the values of the polynomial \(B(x)\).It is guaranteed that \(A(x)\) and \(B(x)\) are similar and that the leading coefficients (i.e., the coefficients in front of \(x^d\)) of \(A(x)\) and \(B(x)\) are not divisible by \(10^9+7\).
|
Print a single integer \(s\) (\(0 \leq s < 10^9+7\)) such that \(B(x) \equiv A(x+s) \pmod{10^9+7}\) for all integers \(x\).If there are multiple solutions, print any.
|
In the first example, \(A(x) \equiv x-1 \pmod{10^9+7}\) and \(B(x)\equiv x+2 \pmod{10^9+7}\). They're similar because $$$\(B(x) \equiv A(x+3) \pmod{10^9+7}.\)\(In the second example, \)A(x) \equiv (x+1)^2 \pmod{10^9+7}\( and \)B(x) \equiv (x+10)^2 \pmod{10^9+7}\(, hence \)\(B(x) \equiv A(x+9) \pmod{10^9+7}.\)$$$
|
Input: 1 1000000006 0 2 3 | Output: 3
|
Expert
| 2 | 576 | 542 | 166 | 18 |
653 |
F
|
653F
|
F. Paper task
| 2,600 |
data structures; string suffix structures; strings
|
Alex was programming while Valentina (his toddler daughter) got there and started asking many questions about the round brackets (or parenthesis) in the code. He explained her a bit and when she got it he gave her a task in order to finish his code on time.For the purpose of this problem we consider only strings consisting of opening and closing round brackets, that is characters '(' and ')'.The sequence of brackets is called correct if: it's empty; it's a correct sequence of brackets, enclosed in a pair of opening and closing brackets; it's a concatenation of two correct sequences of brackets. For example, the sequences ""()()"" and ""((()))(())"" are correct, while "")(()"", ""((((("" and ""())"" are not.Alex took a piece of paper, wrote a string s consisting of brackets and asked Valentina to count the number of distinct non-empty substrings of s that are correct sequences of brackets. In other words, her task is to count the number of non-empty correct sequences of brackets that occur in a string s as a substring (don't mix up with subsequences).When Valentina finished the task, Alex noticed he doesn't know the answer. Help him don't loose face in front of Valentina and solve the problem!
|
The first line of the input contains an integer n (1 β€ n β€ 500 000) β the length of the string s.The second line contains a string s of length n consisting of only '(' and ')'.
|
Print the number of distinct non-empty correct sequences that occur in s as substring.
|
In the first sample, there are 5 distinct substrings we should count: ""()"", ""()()"", ""()()()"", ""()()()()"" and ""()()()()()"".In the second sample, there are 3 distinct substrings we should count: ""()"", ""(())"" and ""(())()"".
|
Input: 10()()()()() | Output: 5
|
Expert
| 3 | 1,211 | 176 | 86 | 6 |
1,210 |
D
|
1210D
|
D. Konrad and Company Evaluation
| 2,400 |
graphs
|
Konrad is a Human Relations consultant working for VoltModder, a large electrical equipment producer. Today, he has been tasked with evaluating the level of happiness in the company.There are \(n\) people working for VoltModder, numbered from \(1\) to \(n\). Each employee earns a different amount of money in the company β initially, the \(i\)-th person earns \(i\) rubles per day.On each of \(q\) following days, the salaries will be revised. At the end of the \(i\)-th day, employee \(v_i\) will start earning \(n+i\) rubles per day and will become the best-paid person in the company. The employee will keep his new salary until it gets revised again.Some pairs of people don't like each other. This creates a great psychological danger in the company. Formally, if two people \(a\) and \(b\) dislike each other and \(a\) earns more money than \(b\), employee \(a\) will brag about this to \(b\). A dangerous triple is a triple of three employees \(a\), \(b\) and \(c\), such that \(a\) brags to \(b\), who in turn brags to \(c\). If \(a\) dislikes \(b\), then \(b\) dislikes \(a\).At the beginning of each day, Konrad needs to evaluate the number of dangerous triples in the company. Can you help him do it?
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\,000\), \(0 \le m \le 100\,000\)) β the number of employees in the company and the number of pairs of people who don't like each other. Each of the following \(m\) lines contains two integers \(a_i\), \(b_i\) (\(1 \le a_i, b_i \le n\), \(a_i \neq b_i\)) denoting that employees \(a_i\) and \(b_i\) hate each other (that is, \(a_i\) dislikes \(b_i\) and \(b_i\) dislikes \(a_i\)). Each such relationship will be mentioned exactly once.The next line contains an integer \(q\) (\(0 \le q \le 100\,000\)) β the number of salary revisions. The \(i\)-th of the following \(q\) lines contains a single integer \(v_i\) (\(1 \le v_i \le n\)) denoting that at the end of the \(i\)-th day, employee \(v_i\) will earn the most.
|
Output \(q + 1\) integers. The \(i\)-th of them should contain the number of dangerous triples in the company at the beginning of the \(i\)-th day.
|
Consider the first sample test. The \(i\)-th row in the following image shows the structure of the company at the beginning of the \(i\)-th day. A directed edge from \(a\) to \(b\) denotes that employee \(a\) brags to employee \(b\). The dangerous triples are marked by highlighted edges.
|
Input: 4 5 1 2 2 4 1 3 3 4 2 3 2 2 3 | Output: 4 3 2
|
Expert
| 1 | 1,212 | 787 | 147 | 12 |
612 |
A
|
612A
|
A. The Text Splitting
| 1,300 |
brute force; implementation; strings
|
You are given the string s of length n and the numbers p, q. Split the string s to pieces of length p and q.For example, the string ""Hello"" for p = 2, q = 3 can be split to the two strings ""Hel"" and ""lo"" or to the two strings ""He"" and ""llo"".Note it is allowed to split the string s to the strings only of length p or to the strings only of length q (see the second sample test).
|
The first line contains three positive integers n, p, q (1 β€ p, q β€ n β€ 100).The second line contains the string s consists of lowercase and uppercase latin letters and digits.
|
If it's impossible to split the string s to the strings of length p and q print the only number ""-1"".Otherwise in the first line print integer k β the number of strings in partition of s.Each of the next k lines should contain the strings in partition. Each string should be of the length p or q. The string should be in order of their appearing in string s β from left to right.If there are several solutions print any of them.
|
Input: 5 2 3Hello | Output: 2Hello
|
Easy
| 3 | 388 | 176 | 430 | 6 |
|
921 |
05
|
92105
|
05. Labyrinth-5
| 3,200 |
See the problem statement here: http://codeforces.com/contest/921/problem/01.
|
Master
| 0 | 77 | 0 | 0 | 9 |
|||||
1,279 |
C
|
1279C
|
C. Stack of Presents
| 1,400 |
data structures; implementation
|
Santa has to send presents to the kids. He has a large stack of \(n\) presents, numbered from \(1\) to \(n\); the topmost present has number \(a_1\), the next present is \(a_2\), and so on; the bottom present has number \(a_n\). All numbers are distinct.Santa has a list of \(m\) distinct presents he has to send: \(b_1\), \(b_2\), ..., \(b_m\). He will send them in the order they appear in the list.To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are \(k\) presents above the present Santa wants to send, it takes him \(2k + 1\) seconds to do it. Fortunately, Santa can speed the whole process up β when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way).What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way.Your program has to answer \(t\) different test cases.
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Then the test cases follow, each represented by three lines.The first line contains two integers \(n\) and \(m\) (\(1 \le m \le n \le 10^5\)) β the number of presents in the stack and the number of presents Santa wants to send, respectively.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le n\), all \(a_i\) are unique) β the order of presents in the stack.The third line contains \(m\) integers \(b_1\), \(b_2\), ..., \(b_m\) (\(1 \le b_i \le n\), all \(b_i\) are unique) β the ordered list of presents Santa has to send.The sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case print one integer β the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack.
|
Input: 2 3 3 3 1 2 3 2 1 7 2 2 1 7 3 4 5 6 3 1 | Output: 5 8
|
Easy
| 2 | 1,236 | 713 | 191 | 12 |
|
1,651 |
C
|
1651C
|
C. Fault-tolerant Network
| 1,500 |
brute force; data structures; implementation
|
There is a classroom with two rows of computers. There are \(n\) computers in each row and each computer has its own grade. Computers in the first row have grades \(a_1, a_2, \dots, a_n\) and in the second row β \(b_1, b_2, \dots, b_n\).Initially, all pairs of neighboring computers in each row are connected by wire (pairs \((i, i + 1)\) for all \(1 \le i < n\)), so two rows form two independent computer networks.Your task is to combine them into one common network by connecting one or more pairs of computers from different rows. Connecting the \(i\)-th computer from the first row and the \(j\)-th computer from the second row costs \(|a_i - b_j|\).You can connect one computer to several other computers, but you need to provide at least a basic fault tolerance: you need to connect computers in such a way that the network stays connected, despite one of its computers failing. In other words, if one computer is broken (no matter which one), the network won't split into two or more parts.What is the minimum total cost to make a fault-tolerant network?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Next, \(t\) cases follow.The first line of each test case contains the single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of computers in each row.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the grades of computers in the first row.The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 10^9\)) β the grades of computers in the second row.It's guaranteed that the total sum of \(n\) doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the minimum total cost to make a fault-tolerant network.
|
In the first test case, it's optimal to connect four pairs of computers: computer \(1\) from the first row with computer \(2\) from the second row: cost \(|1 - 4| = 3\); computer \(3\) from the first row with computer \(2\) from the second row: cost \(|1 - 4| = 3\); computer \(2\) from the first row with computer \(1\) from the second row: cost \(|10 - 20| = 10\); computer \(2\) from the first row with computer \(3\) from the second row: cost \(|10 - 25| = 15\); In total, \(3 + 3 + 10 + 15 = 31\).In the second test case, it's optimal to connect \(1\) from the first row with \(1\) from the second row, and \(4\) from the first row with \(4\) from the second row.
|
Input: 231 10 120 4 2541 1 1 11000000000 1000000000 1000000000 1000000000 | Output: 31 1999999998
|
Medium
| 3 | 1,062 | 601 | 101 | 16 |
1,698 |
F
|
1698F
|
F. Equal Reversal
| 2,800 |
constructive algorithms; graphs; implementation; math
|
There is an array \(a\) of length \(n\). You may perform the following operation on it: Choose two indices \(l\) and \(r\) where \(1 \le l \le r \le n\) and \(a_l = a_r\). Then, reverse the subsegment from the \(l\)-th to the \(r\)-th element, i. e. set \([a_l, a_{l + 1}, \ldots, a_{r - 1}, a_r]\) to \([a_r, a_{r-1}, \ldots, a_{l+1}, a_l]\). You are also given another array \(b\) of length \(n\) which is a permutation of \(a\). Find a sequence of at most \(n^2\) operations that transforms array \(a\) into \(b\), or report that no such sequence exists.
|
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 an integer \(n\) (\(1 \le n \le 500\)) β the length of array \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β elements of the array \(a\).The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le n\)) β elements of the array \(b\).It is guaranteed that \(b\) is a permutation of \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(500\).
|
For each test case, output ""NO"" (without quotes) if it is impossible to turn \(a\) into \(b\) using at most \(n^2\) operations.Otherwise, output ""YES"" (without quotes). Then output an integer \(k\) (\(0 \leq k \leq n^2\)) denoting the number of operations you will perform. Note that you don't have to minimize the number of operations.Afterwards, output \(k\) lines. The \(i\)-th line should contain two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \leq r_i \leq n\)) β the left and right indices for the \(i\)-th operation.You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive response).If there are multiple possible sequences of operations, you may output any of them.
|
In the first test case, we can perform the following operations: $$$\([1,2,4,3,1,2,1,1] \xrightarrow[l=5,\,r=8]{} [1,2,4,3,1,1,2,1] \xrightarrow[l=1,\,r=6]{} [1,1,3,4,2,1,2,1].\)\(In the second test case, we can perform the following operations: \)\([1,2,3,1,3,2,3] \xrightarrow[l=1,\,r=4]{} [1,3,2,1,3,2,3] \xrightarrow[l=3,\,r=6]{} [1,3,2,3,1,2,3].\)\(It can be proven that it is impossible to turn \)a\( into \)b$$$ in the third and fourth test cases.
|
Input: 581 2 4 3 1 2 1 11 1 3 4 2 1 2 171 2 3 1 3 2 31 3 2 3 1 2 331 1 21 2 121 22 1111 | Output: YES 2 5 8 1 6 YES 2 1 4 3 6 NO NO YES 0
|
Master
| 4 | 557 | 708 | 750 | 16 |
1,418 |
A
|
1418A
|
A. Buying Torches
| 1,000 |
math
|
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft \(k\) torches. One torch can be crafted using one stick and one coal.Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange \(1\) stick for \(x\) sticks (you lose \(1\) stick and gain \(x\) sticks). exchange \(y\) sticks for \(1\) coal (you lose \(y\) sticks and gain \(1\) coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order.Your task is to find the minimum number of trades you need to craft at least \(k\) torches. The answer always exists under the given constraints.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The only line of the test case contains three integers \(x\), \(y\) and \(k\) (\(2 \le x \le 10^9\); \(1 \le y, k \le 10^9\)) β the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively.
|
For each test case, print the answer: the minimum number of trades you need to craft at least \(k\) torches. The answer always exists under the given constraints.
|
Input: 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 | Output: 14 33 25 2000000003 1000000001999999999
|
Beginner
| 1 | 752 | 415 | 162 | 14 |
|
1,949 |
A
|
1949A
|
A. Grove
| 3,300 |
brute force; dfs and similar; dp; geometry; probabilities
|
You want to plant trees in a square lawn of size \(n \times n\) whose corners have Cartesian coordinates \((0, 0)\), \((n, 0)\), \((0, n)\), and \((n, n)\). Trees can only be planted at locations with integer coordinates. Every tree will grow roots within a disk of radius \(r\) centered at the location where the tree was planted; such disks must be fully contained in the lawn (possibly touching the boundary of the lawn) and can only intersect each other on their boundaries.Find a configuration that maximizes the number of trees.
|
The first and only line contains an integer \(n\) (\(1 \leq n \leq 20\)) and a real number \(r\) (\(0 < r \leq n/2\)) β the length of the sides of the lawn, and the radius of the disks where each tree will grow roots. The real number \(r\) is given in decimal notation with at least \(1\) and at most \(3\) digits after the decimal point.
|
In the first line, print the maximum number \(m\) of trees that can be planted.In the next \(m\) lines, print a configuration that maximizes the number of trees. Specifically, in the \((i+1)\)-th line, print two integers \(x\) and \(y\) β the coordinates of the location where the \(i\)-th tree should be planted. You can print the trees in any order.If there are multiple solutions, print any of them.
|
For the first sample, the sample output is shown in the following figure. Note that this is not the only configuration that maximizes the number of trees. For the second sample, the sample output is shown in the following figure. Note that this is not the only configuration that maximizes the number of trees.
|
Input: 6 1.241 | Output: 2 4 2 2 4
|
Master
| 5 | 534 | 338 | 402 | 19 |
624 |
B
|
624B
|
B. Making a String
| 1,100 |
greedy; sortings
|
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied: the i-th letter occurs in the string no more than ai times; the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
|
The first line of the input contains a single integer n (2 β€ n β€ 26) β the number of letters in the alphabet.The next line contains n integers ai (1 β€ ai β€ 109) β i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
|
Print a single integer β the maximum length of the string that meets all the requirements.
|
For convenience let's consider an alphabet consisting of three letters: ""a"", ""b"", ""c"". In the first sample, some of the optimal strings are: ""cccaabbccbb"", ""aabcbcbcbcb"". In the second sample some of the optimal strings are: ""acc"", ""cbc"".
|
Input: 32 5 5 | Output: 11
|
Easy
| 2 | 354 | 272 | 90 | 6 |
1,208 |
C
|
1208C
|
C. Magic Grid
| 1,800 |
constructive algorithms
|
Let us define a magic grid to be a square matrix of integers of size \(n \times n\), satisfying the following conditions. All integers from \(0\) to \((n^2 - 1)\) inclusive appear in the matrix exactly once. Bitwise XOR of all elements in a row or a column must be the same for each row and column. You are given an integer \(n\) which is a multiple of \(4\). Construct a magic grid of size \(n \times n\).
|
The only line of input contains an integer \(n\) (\(4 \leq n \leq 1000\)). It is guaranteed that \(n\) is a multiple of \(4\).
|
Print a magic grid, i.e. \(n\) lines, the \(i\)-th of which contains \(n\) space-separated integers, representing the \(i\)-th row of the grid.If there are multiple answers, print any. We can show that an answer always exists.
|
In the first example, XOR of each row and each column is \(13\).In the second example, XOR of each row and each column is \(60\).
|
Input: 4 | Output: 8 9 1 13 3 12 7 5 0 2 4 11 6 10 15 14
|
Medium
| 1 | 406 | 126 | 226 | 12 |
1,116 |
D4
|
1116D4
|
D4. TIE fighter
| 0 |
*special
|
Implement a unitary operation on \(N\) qubits which is represented by a square matrix of size \(2^N\) which has non-zero elements in the following positions: the central 2x2 sub-matrix, the diagonals of the top right and bottom left square sub-matrices of size \(2^{N-1}-1\) that do not overlap with the central 2x2 sub-matrix, the anti-diagonals of the top left and bottom right square sub-matrices of size \(2^{N-1}-1\) that do not overlap with the central 2x2 sub-matrix.For example, for \(N = 3\) the matrix of the operation should have the following shape:..X..X...X....X.X......X...XX......XX...X......X.X....X...X..X..Here X denotes a ""non-zero"" element of the matrix (a complex number which has the square of the absolute value greater than or equal to \(10^{-5}\)), and . denotes a ""zero"" element of the matrix (a complex number which has the square of the absolute value less than \(10^{-5}\)).The row and column indices of the matrix follow little endian format: the least significant bit of the index is stored first in the qubit array. Thus, the first column of the matrix gives you the coefficients of the basis states you'll get if you apply the unitary to the \(|00..0\rangle\) basis state, the second column - to the \(|10..0\rangle\) basis state etc. You can use the DumpUnitary tool to get the coefficients of the matrix your unitary implements (up to relative phases between columns) and the corresponding pattern of Xs and .s.You have to implement an operation which takes an array of \(N\) (\(2 \le N \le 5\)) qubits as an input and applies the unitary transformation with the matrix of the described shape to it. If there are multiple unitaries which satisfy the requirements, you can implement any of them. The ""output"" of your operation is the pattern of the matrix coefficients implemented by it; you can see the testing harness in the UnitaryPatterns kata.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[]) : Unit { // your code here }}You are not allowed to use measurements in your operation.
|
Beginner
| 1 | 2,138 | 0 | 0 | 11 |
||||
1,266 |
G
|
1266G
|
G. Permutation Concatenation
| 3,300 |
string suffix structures
|
Let \(n\) be an integer. Consider all permutations on integers \(1\) to \(n\) in lexicographic order, and concatenate them into one big sequence \(P\). For example, if \(n = 3\), then \(P = [1, 2, 3, 1, 3, 2, 2, 1, 3, 2, 3, 1, 3, 1, 2, 3, 2, 1]\). The length of this sequence is \(n \cdot n!\).Let \(1 \leq i \leq j \leq n \cdot n!\) be a pair of indices. We call the sequence \((P_i, P_{i+1}, \dots, P_{j-1}, P_j)\) a subarray of \(P\). You are given \(n\). Find the number of distinct subarrays of \(P\). Since this number may be large, output it modulo \(998244353\) (a prime number).
|
The only line contains one integer \(n\) (\(1 \leq n \leq 10^6\)), as described in the problem statement.
|
Output a single integer β the number of distinct subarrays, modulo \(998244353\).
|
In the first example, the sequence \(P = [1, 2, 2, 1]\). It has eight distinct subarrays: \([1]\), \([2]\), \([1, 2]\), \([2, 1]\), \([2, 2]\), \([1, 2, 2]\), \([2, 2, 1]\) and \([1, 2, 2, 1]\).
|
Input: 2 | Output: 8
|
Master
| 1 | 587 | 105 | 81 | 12 |
494 |
E
|
494E
|
E. Sharti
| 3,200 |
data structures; games
|
During the last 24 hours Hamed and Malek spent all their time playing ""Sharti"". Now they are too exhausted to finish the last round. So they asked you for help to determine the winner of this round. ""Sharti"" is played on a n Γ n board with some of cells colored white and others colored black. The rows of the board are numbered from top to bottom using number 1 to n. Also the columns of the board are numbered from left to right using numbers 1 to n. The cell located at the intersection of i-th row and j-th column is denoted by (i, j).The players alternatively take turns. In each turn the player must choose a square with side-length at most k with its lower-right cell painted white. Then the colors of all the cells in this square are inversed (white cells become black and vice-versa). The player who cannot perform a move in his turn loses. You know Hamed and Malek are very clever and they would have played their best moves at each turn. Knowing this and the fact that Hamed takes the first turn, given the initial board as described in the input, you must determine which one of them will be the winner.
|
In this problem the initial board is specified as a set of m rectangles. All cells that lie inside at least one of these rectangles are colored white and the rest are colored black.In the first line of input three space-spereated integers n, m, k (1 β€ k β€ n β€ 109, 1 β€ m β€ 5Β·104) follow, denoting size of the board, number of rectangles and maximum size of the turn square during the game, respectively.In i-th line of the next m lines four space-seperated integers ai, bi, ci, di (1 β€ ai β€ ci β€ n, 1 β€ bi β€ di β€ n) are given meaning that i-th rectangle determining the initial board is a rectangle with upper-left cell at (ai, bi) and lower-right cell at (ci, di).
|
If Hamed wins, print ""Hamed"", otherwise print ""Malek"" (without the quotes).
|
Input: 5 2 11 1 3 32 2 4 4 | Output: Malek
|
Master
| 2 | 1,119 | 665 | 79 | 4 |
|
454 |
A
|
454A
|
A. Little Pony and Crystal Mine
| 800 |
implementation
|
Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n > 1) is an n Γ n matrix with a diamond inscribed into it.You are given an odd integer n. You need to draw a crystal of size n. The diamond cells of the matrix should be represented by character ""D"". All other cells of the matrix should be represented by character ""*"". Look at the examples to understand what you need to draw.
|
The only line contains an integer n (3 β€ n β€ 101; n is odd).
|
Output a crystal of size n.
|
Input: 3 | Output: *D*DDD*D*
|
Beginner
| 1 | 422 | 60 | 27 | 4 |
|
1,973 |
E
|
1973E
|
E. Cat, Fox and Swaps
| 2,500 |
graphs; math; sortings
|
Fox has found an array \(p_1, p_2, \ldots, p_n\), that is a permutation of length \(n^\dagger\) of the numbers \(1, 2, \ldots, n\). She wants to sort the elements in increasing order. Cat wants to help her β he is able to swap any two numbers \(x\) and \(y\) in the array, but only if \(l \leq x + y \leq r\) (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times.They don't know the numbers \(l\), \(r\) yet, they only know that it's true that \(1 \leq l \leq r \leq 2 \cdot n\). You are given the number \(n\) and the array \(p_1, p_2, \ldots, p_n\). Determine how many pairs \((l, r)\) satisfying the conditions are there such that you can sort the permutation if you can only swap two number \((x, y)\) such that \(l \leq x + y \leq r\) (arbitrary number of times, possibly \(0\)).\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
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.Description of each test case consists of two lines. The first line contains one integer \(n\) (\(1 \leq n \leq 10^5\)).The second line contains \(n\) integers: the array \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)). It is guaranteed that this array is a permutation of length \(n\). It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print the number of pairs of integers \((l, r)\) such that \(1 \leq l \leq r \leq 2 \cdot n\), and you can sort the array under the constraints.
|
In the first example, we need to be able to swap \(1\) and \(2\), so we must be able to swap numbers with sum \(3\). There are exactly \(6\) pairs satisfying the condition: \((1, 3), (2, 3), (3, 3), (1, 4), (2, 4)\) and \((3, 4)\), so the answer is \(6\).In the second example, the \(11\) pairs satisfying the condition are \((1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)\) and \((4, 6)\). For example, if we pick the pair \((3, 4)\) we can first swap the numbers \(1\) and \(2\) and then the numbers \(1\) and \(3\), after this, the permutation is sorted.
|
Input: 722 133 1 243 2 1 455 3 1 2 451 2 3 4 563 2 1 5 4 661 3 2 4 5 6 | Output: 6 11 23 29 55 46 58
|
Expert
| 3 | 1,200 | 532 | 164 | 19 |
1,523 |
A
|
1523A
|
A. Game of Life
| 800 |
implementation
|
William really likes the cellular automaton called ""Game of Life"" so he decided to make his own version. For simplicity, William decided to define his cellular automaton on an array containing \(n\) cells, with each cell either being alive or dead.Evolution of the array in William's cellular automaton occurs iteratively in the following way: If the element is dead and it has exactly \(1\) alive neighbor in the current state of the array, then on the next iteration it will become alive. For an element at index \(i\) the neighbors would be elements with indices \(i - 1\) and \(i + 1\). If there is no element at that index, it is considered to be a dead neighbor. William is a humane person so all alive elements stay alive. Check the note section for examples of the evolution.You are given some initial state of all elements and you need to help William find the state of the array after \(m\) iterations of evolution.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n \le 10^3, 1 \le m \le 10^9\)), which are the total number of cells in the array and the number of iterations.The second line of each test case contains a string of length \(n\) made up of characters ""0"" and ""1"" and defines the initial state of the array. ""1"" means a cell is alive and ""0"" means it is dead.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^4\).
|
In each test case output a string of length \(n\), made up of characters ""0"" and ""1"" β the state of the array after \(m\) iterations of evolution.
|
Sequence of iterations of evolution for the first test case 01000000001 β initial state 11100000011 β first iteration of evolution 11110000111 β second iteration of evolution 11111001111 β third iteration of evolution Sequence of iterations of evolution for the second test case 0110100101 β initial state 1110111101 β first iteration of evolution 1110111101 β second iteration of evolution
|
Input: 4 11 3 01000000001 10 2 0110100101 5 2 10101 3 100 000 | Output: 11111001111 1110111101 10101 000
|
Beginner
| 1 | 927 | 637 | 150 | 15 |
379 |
B
|
379B
|
B. New Year Present
| 1,200 |
constructive algorithms; implementation
|
The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception.Vasily knows that the best present is (no, it's not a contest) money. He's put n empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided to put ai coins to the i-th wallet from the left.Vasily is a very busy man, so the money are sorted into the bags by his robot. Initially, the robot stands by the leftmost wallet in the row. The robot can follow instructions of three types: go to the wallet that is to the left of the current one (if such wallet exists), go to the wallet that is to the right of the current one (if such wallet exists), put a coin to the current wallet. Due to some technical malfunctions the robot cannot follow two ""put a coin"" instructions in a row.Vasily doesn't want to wait for long, so he wants to write a program for the robot that contains at most 106 operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him.
|
The first line contains integer n (2 β€ n β€ 300) β the number of wallets. The next line contains n integers a1, a2, ..., an (0 β€ ai β€ 300).It is guaranteed that at least one ai is positive.
|
Print the sequence that consists of k (1 β€ k β€ 106) characters, each of them equals: ""L"", ""R"" or ""P"". Each character of the sequence is an instruction to the robot. Character ""L"" orders to move to the left, character ""R"" orders to move to the right, character ""P"" orders the robot to put a coin in the wallet. The robot is not allowed to go beyond the wallet line. In other words, you cannot give instructions ""L"" if the robot is at wallet 1, or ""R"" at wallet n.As a result of the performed operations, the i-th wallet from the left must contain exactly ai coins. If there are multiple answers, you can print any of them.
|
Input: 21 2 | Output: PRPLRP
|
Easy
| 2 | 1,069 | 188 | 637 | 3 |
|
1,486 |
B
|
1486B
|
B. Eastern Exhibition
| 1,500 |
binary search; geometry; shortest paths; sortings
|
You and your friends live in \(n\) houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points \((x_1, y_1)\) and \((x_2, y_2)\) is \(|x_1 - x_2| + |y_1 - y_2|\), where \(|x|\) is the absolute value of \(x\).
|
First line contains a single integer \(t\) \((1 \leq t \leq 1000)\) β the number of test cases.The first line of each test case contains a single integer \(n\) \((1 \leq n \leq 1000)\). Next \(n\) lines describe the positions of the houses \((x_i, y_i)\) \((0 \leq x_i, y_i \leq 10^9)\).It's guaranteed that the sum of all \(n\) does not exceed \(1000\).
|
For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house.
|
Here are the images for the example test cases. Blue dots stand for the houses, green β possible positions for the exhibition.First test case.Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at \((0, 0)\).
|
Input: 6 3 0 0 2 0 1 2 4 1 0 0 2 2 3 3 1 4 0 0 0 1 1 0 1 1 2 0 0 1 1 2 0 0 2 0 2 0 0 0 0 | Output: 1 4 4 4 3 1
|
Medium
| 4 | 625 | 354 | 159 | 14 |
1,358 |
D
|
1358D
|
D. The Best Vacation
| 1,900 |
binary search; brute force; greedy; implementation; two pointers
|
You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly \(x\) days and that's the exact number of days you will spend visiting your friend. You will spend exactly \(x\) consecutive (successive) days visiting Coronavirus-chan.They use a very unusual calendar in Naha: there are \(n\) months in a year, \(i\)-th month lasts exactly \(d_i\) days. Days in the \(i\)-th month are numbered from \(1\) to \(d_i\). There are no leap years in Naha.The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get \(j\) hugs if you visit Coronavirus-chan on the \(j\)-th day of the month.You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year.
|
The first line of input contains two integers \(n\) and \(x\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of months in the year and the number of days you can spend with your friend.The second line contains \(n\) integers \(d_1, d_2, \ldots, d_n\), \(d_i\) is the number of days in the \(i\)-th month (\(1 \le d_i \le 10^6\)).It is guaranteed that \(1 \le x \le d_1 + d_2 + \ldots + d_n\).
|
Print one integer β the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life.
|
In the first test case, the numbers of the days in a year are (indices of days in a corresponding month) \(\{1,1,2,3,1\}\). Coronavirus-chan will hug you the most if you come on the third day of the year: \(2+3=5\) hugs.In the second test case, the numbers of the days are \(\{1,2,3,1,2,3,1,2,3\}\). You will get the most hugs if you arrive on the third day of the year: \(3+1+2+3+1+2=12\) hugs.In the third test case, the numbers of the days are \(\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}\). You will get the most hugs if you come on the twelfth day of the year: your friend will hug you \(2+3+1+2+3+4=15\) times.
|
Input: 3 2 1 3 1 | Output: 5
|
Hard
| 5 | 1,118 | 389 | 124 | 13 |
581 |
F
|
581F
|
F. Zublicanes and Mumocrates
| 2,400 |
dp; trees; two pointers
|
It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on n main squares of the capital of Berland. Each of the n squares certainly can have demonstrations of only one party, otherwise it could lead to riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties.Some pairs of squares are connected by (n - 1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares.The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even.To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.
|
The first line of the input contains a single integer n (2 β€ n β€ 5000) β the number of squares in the capital of Berland.Next n - 1 lines contain the pairs of integers x, y (1 β€ x, y β€ n, x β y) β the numbers of the squares connected by the road. All squares are numbered with integers from 1 to n. It is guaranteed that the number of dead end squares of the city is even.
|
Print a single number β the minimum number of roads connecting the squares with demonstrations of different parties.
|
Input: 81 42 43 46 57 58 54 5 | Output: 1
|
Expert
| 3 | 1,389 | 372 | 116 | 5 |
|
841 |
B
|
841B
|
B. Godsend
| 1,100 |
games; math
|
Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two players play the game on the array. Players move one by one. The first player can choose for his move a subsegment of non-zero length with an odd sum of numbers and remove it from the array, after that the remaining parts are glued together into one array and the game continues. The second player can choose a subsegment of non-zero length with an even sum and remove it. Loses the one who can not make a move. Who will win if both play optimally?
|
First line of input data contains single integer n (1 β€ n β€ 106) β length of the array.Next line contains n integers a1, a2, ..., an (0 β€ ai β€ 109).
|
Output answer in single line. ""First"", if first player wins, and ""Second"" otherwise (without quotes).
|
In first sample first player remove whole array in one move and win.In second sample first player can't make a move and lose.
|
Input: 41 3 2 3 | Output: First
|
Easy
| 2 | 545 | 148 | 105 | 8 |
1,965 |
D
|
1965D
|
D. Missing Subarray Sum
| 2,900 |
constructive algorithms
|
There is a hidden array \(a\) of \(n\) positive integers. You know that \(a\) is a palindrome, or in other words, for all \(1 \le i \le n\), \(a_i = a_{n + 1 - i}\). You are given the sums of all but one of its distinct subarrays, in arbitrary order. The subarray whose sum is not given can be any of the \(\frac{n(n+1)}{2}\) distinct subarrays of \(a\).Recover any possible palindrome \(a\). The input is chosen such that there is always at least one array \(a\) that satisfies the conditions.An array \(b\) is a subarray of \(a\) if \(b\) can be obtained from \(a\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 200\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 1000\)) β the size of the array \(a\).The next line of each test case contains \(\frac{n(n+1)}{2} - 1\) integers \(s_i\) (\(1\leq s_i \leq 10^9\)) β all but one of the subarray sums of \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1000\).Additional constraint on the input: There is always at least one valid solution.Hacks are disabled for this problem.
|
For each test case, print one line containing \(n\) positive integers \(a_1, a_2, \cdots a_n\) β any valid array \(a\). Note that \(a\) must be a palindrome.If there are multiple solutions, print any.
|
For the first example case, the subarrays of \(a = [1, 2, 1]\) are: \([1]\) with sum \(1\), \([2]\) with sum \(2\), \([1]\) with sum \(1\), \([1, 2]\) with sum \(3\), \([2, 1]\) with sum \(3\), \([1, 2, 1]\) with sum \(4\). So the full list of subarray sums is \(1, 1, 2, 3, 3, 4\), and the sum that is missing from the input list is \(3\).For the second example case, the missing subarray sum is \(4\), for the subarray \([2, 2]\).For the third example case, the missing subarray sum is \(13\), because there are two subarrays with sum \(13\) (\([3, 5, 5]\) and \([5, 5, 3]\)) but \(13\) only occurs once in the input.
|
Input: 731 2 3 4 1418 2 11 9 7 11 7 2 945 10 5 16 3 3 13 8 848 10 4 6 4 20 14 14 651 2 3 4 5 4 3 2 1 1 2 3 2 151 1 2 2 2 3 3 3 3 4 5 5 6 83500000000 1000000000 500000000 500000000 1000000000 | Output: 1 2 1 7 2 2 7 3 5 5 3 6 4 4 6 1 1 1 1 1 2 1 2 1 2 500000000 500000000 500000000
|
Master
| 1 | 705 | 623 | 200 | 19 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.