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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
383 |
E
|
383E
|
E. Vowels
| 2,700 |
combinatorics; divide and conquer; dp
|
Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of n 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (a to x). She decided that some of the letters are vowels, and all the others are consonants. The whole language is based on a simple rule: any word that contains at least one vowel is correct.Iahubina forgot which letters are the vowels, and wants to find some possible correct sets of vowels. She asks Iahub questions. In each question, she will give Iahub a set of letters considered vowels (in this question). For each question she wants to know how many words of the dictionary are correct, considering the given set of vowels.Iahubina wants to know the xor of the squared answers to all the possible questions. There are 224 different questions, they are all subsets of the set of the first 24 letters of the English alphabet. Help Iahub find that number.
|
The first line contains one integer, n (1 β€ n β€ 104). Each of the next n lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words.
|
Print one number, the xor of the squared answers to the queries.
|
Input: 5abcaaaadabcddef | Output: 0
|
Master
| 3 | 1,019 | 171 | 64 | 3 |
|
153 |
A
|
153A
|
A. A + B
| 1,600 |
*special
|
You are given two integers A and B. Calculate their sum and output it without leading zeros.
|
Two lines of input data contain integers A and B (1 β€ A, B β€ 105).
|
Output A + B without leading zeros.
|
The code provided in the post about the round doesn't solve the task.
|
Input: 123 | Output: 15
|
Medium
| 1 | 92 | 66 | 35 | 1 |
1,357 |
A4
|
1357A4
|
A4. Distinguish Rz from R1
| 0 |
*special
|
You are given an operation that implements a single-qubit unitary transformation: either the Rz gate or the R1 gate. The operation will have Adjoint and Controlled variants defined.Your task is to perform necessary operations and measurements to figure out which unitary it was and to return 0 if it was the Rz gate or 1 if it was the R1 gate. You are allowed to apply the given operation and its adjoint/controlled variants exactly once (you can pass to it any rotation angle that you choose).You have to implement an operation which takes a single-qubit operation unitary as an input and returns an integer. The operation unitary will accept two parameters (similarly to Rz and R1 intrinsic gates): the first parameter is the rotation angle and the second parameter is the qubit to which the rotation is applied. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Intrinsic; operation Solve (unitary : ((Double, Qubit) => Unit is Adj+Ctl)) : Int { // your code here }}
|
Beginner
| 1 | 1,009 | 0 | 0 | 13 |
||||
1,000 |
F
|
1000F
|
F. One Occurrence
| 2,400 |
data structures; divide and conquer
|
You are given an array \(a\) consisting of \(n\) integers, and \(q\) queries to it. \(i\)-th query is denoted by two integers \(l_i\) and \(r_i\). For each query, you have to find any integer that occurs exactly once in the subarray of \(a\) from index \(l_i\) to index \(r_i\) (a subarray is a contiguous subsegment of an array). For example, if \(a = [1, 1, 2, 3, 2, 4]\), then for query \((l_i = 2, r_i = 6)\) the subarray we are interested in is \([1, 2, 3, 2, 4]\), and possible answers are \(1\), \(3\) and \(4\); for query \((l_i = 1, r_i = 2)\) the subarray we are interested in is \([1, 1]\), and there is no such element that occurs exactly once.Can you answer all of the queries?
|
The first line contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 5 \cdot 10^5\)).The third line contains one integer \(q\) (\(1 \le q \le 5 \cdot 10^5\)).Then \(q\) lines follow, \(i\)-th line containing two integers \(l_i\) and \(r_i\) representing \(i\)-th query (\(1 \le l_i \le r_i \le n\)).
|
Answer the queries as follows:If there is no integer such that it occurs in the subarray from index \(l_i\) to index \(r_i\) exactly once, print \(0\). Otherwise print any such integer.
|
Input: 61 1 2 3 2 422 61 2 | Output: 40
|
Expert
| 2 | 690 | 385 | 185 | 10 |
|
1,375 |
G
|
1375G
|
G. Tree Modification
| 2,800 |
brute force; constructive algorithms; dfs and similar; graph matchings; graphs; trees
|
You are given a tree with \(n\) vertices. You are allowed to modify the structure of the tree through the following multi-step operation: Choose three vertices \(a\), \(b\), and \(c\) such that \(b\) is adjacent to both \(a\) and \(c\). For every vertex \(d\) other than \(b\) that is adjacent to \(a\), remove the edge connecting \(d\) and \(a\) and add the edge connecting \(d\) and \(c\). Delete the edge connecting \(a\) and \(b\) and add the edge connecting \(a\) and \(c\). As an example, consider the following tree: The following diagram illustrates the sequence of steps that happen when we apply an operation to vertices \(2\), \(4\), and \(5\): It can be proven that after each operation, the resulting graph is still a tree.Find the minimum number of operations that must be performed to transform the tree into a star. A star is a tree with one vertex of degree \(n - 1\), called its center, and \(n - 1\) vertices of degree \(1\).
|
The first line contains an integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.The \(i\)-th of the following \(n - 1\) lines contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \neq v_i\)) denoting that there exists an edge connecting vertices \(u_i\) and \(v_i\). It is guaranteed that the given edges form a tree.
|
Print a single integer β the minimum number of operations needed to transform the tree into a star.It can be proven that under the given constraints, it is always possible to transform the tree into a star using at most \(10^{18}\) operations.
|
The first test case corresponds to the tree shown in the statement. As we have seen before, we can transform the tree into a star with center at vertex \(5\) by applying a single operation to vertices \(2\), \(4\), and \(5\).In the second test case, the given tree is already a star with the center at vertex \(4\), so no operations have to be performed.
|
Input: 6 4 5 2 6 3 2 1 2 2 4 | Output: 1
|
Master
| 6 | 944 | 368 | 243 | 13 |
1,355 |
C
|
1355C
|
C. Count Triangles
| 1,800 |
binary search; implementation; math; two pointers
|
Like any unknown mathematician, Yuri has favourite numbers: \(A\), \(B\), \(C\), and \(D\), where \(A \leq B \leq C \leq D\). Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides \(x\), \(y\), and \(z\) exist, such that \(A \leq x \leq B \leq y \leq C \leq z \leq D\) holds?Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property.The triangle is called non-degenerate if and only if its vertices are not collinear.
|
The first line contains four integers: \(A\), \(B\), \(C\) and \(D\) (\(1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5\)) β Yuri's favourite numbers.
|
Print the number of non-degenerate triangles with integer sides \(x\), \(y\), and \(z\) such that the inequality \(A \leq x \leq B \leq y \leq C \leq z \leq D\) holds.
|
In the first example Yuri can make up triangles with sides \((1, 3, 3)\), \((2, 2, 3)\), \((2, 3, 3)\) and \((2, 3, 4)\).In the second example Yuri can make up triangles with sides \((1, 2, 2)\), \((2, 2, 2)\) and \((2, 2, 3)\).In the third example Yuri can make up only one equilateral triangle with sides equal to \(5 \cdot 10^5\).
|
Input: 1 2 3 4 | Output: 4
|
Medium
| 4 | 560 | 150 | 167 | 13 |
1,864 |
A
|
1864A
|
A. Increasing and Decreasing
| 800 |
constructive algorithms; greedy; implementation; math
|
You are given three integers \(x\), \(y\), and \(n\).Your task is to construct an array \(a\) consisting of \(n\) integers which satisfies the following conditions: \(a_1=x\), \(a_n=y\); \(a\) is strictly increasing (i.e. \(a_1 < a_2 < \ldots < a_n\)); if we denote \(b_i=a_{i+1}-a_{i}\) for \(1 \leq i \leq n-1\), then \(b\) is strictly decreasing (i.e. \(b_1 > b_2 > \ldots > b_{n-1}\)). If there is no such array \(a\), print a single integer \(-1\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The only line of each test case contains three integers \(x\), \(y\), \(n\) (\(1 \le x < y \le 1000,3 \le n \le 1000\)).
|
For each test case, output \(n\) integers \(a_1,a_2,\ldots,a_n\). If there are multiple solutions, print any of them.If there is no solution, print a single integer \(-1\).
|
In the first test case, \(a=[1,3,4]\), which is strictly increasing. Next, \(b_1=a_2-a_1=3-1=2\), \(b_2=a_3-a_2=4-3=1\), thus \(b=[2,1]\), which is strictly decreasing.In the second test case, there is no array \(a\) that satisfies all the conditions above.
|
Input: 31 4 31 3 3100 200 4 | Output: 1 3 4 -1 100 150 180 200
|
Beginner
| 4 | 453 | 281 | 172 | 18 |
1,837 |
E
|
1837E
|
E. Playoff Fixing
| 2,200 |
combinatorics; trees
|
\(2^k\) teams participate in a playoff tournament. The teams are numbered from \(1\) to \(2^k\), in order of decreasing strength. So, team \(1\) is the strongest one, team \(2^k\) is the weakest one. A team with a smaller number always defeats a team with a larger number.First of all, the teams are arranged in some order during a procedure called seeding. Each team is assigned another unique value from \(1\) to \(2^k\), called a seed, that represents its starting position in the playoff.The tournament consists of \(2^k - 1\) games. They are held as follows: the teams are split into pairs: team with seed \(1\) plays against team with seed \(2\), team with seed \(3\) plays against team with seed \(4\) (exactly in this order), and so on (so, \(2^{k-1}\) games are played in that phase). When a team loses a game, it is eliminated.After that, only \(2^{k-1}\) teams remain. If only one team remains, it is declared the champion; otherwise, \(2^{k-2}\) games are played: in the first one of them, the winner of the game ""seed \(1\) vs seed \(2\)"" plays against the winner of the game ""seed \(3\) vs seed \(4\)"", then the winner of the game ""seed \(5\) vs seed \(6\)"" plays against the winner of the game ""seed \(7\) vs seed \(8\)"", and so on. This process repeats until only one team remains.After the tournament ends, the teams are assigned places according to the tournament phase when they were eliminated. In particular: the winner of the tournament gets place \(1\); the team eliminated in the finals gets place \(2\); both teams eliminated in the semifinals get place \(3\); all teams eliminated in the quarterfinals get place \(5\); all teams eliminated in the 1/8 finals get place \(9\), and so on. Now that we established the rules, we do a little rigging. In particular, we want: team \(1\) (not team with seed \(1\)) to take place \(1\); team \(2\) to take place \(2\); teams \(3\) and \(4\) to take place \(3\); teams from \(5\) to \(8\) to take place \(5\), and so on. For example, this picture describes one of the possible ways the tournament can go with \(k = 3\), and the resulting places of the teams: Some seeds are already reserved for some teams (we are not the only ones rigging the tournament, apparently). We have to fill the rest of the seeds with the remaining teams to achieve the desired placements. How many ways are there to do that? Since that value might be large, print it modulo \(998\,244\,353\).
|
The first line contains a single integer \(k\) (\(0 \le k \le 19\)) β there are \(2^k\) teams.The second line contains \(2^k\) integers \(a_1, a_2, \dots, a_{2^k}\) (\(a_i = -1\) or \(1 \le a_i \le 2^k\)). If \(a_i \ne -1\), then team \(a_i\) has seed \(i\). Otherwise, the seed \(i\) is not reserved for any team.All values, that are not \(-1\), are distinct.
|
Print a single integer β the number of ways to fill the non-reserved seeds so that the tournament goes as planned, modulo \(998\,244\,353\).
|
Input: 2 1 2 3 4 | Output: 0
|
Hard
| 2 | 2,444 | 360 | 140 | 18 |
|
165 |
C
|
165C
|
C. Another Problem on Strings
| 1,600 |
binary search; brute force; dp; math; strings; two pointers
|
A string is binary, if it consists only of characters ""0"" and ""1"".String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string ""010"" has six substrings: ""0"", ""1"", ""0"", ""01"", ""10"", ""010"". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs.You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters ""1"".
|
The first line contains the single integer k (0 β€ k β€ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters.
|
Print the single number β the number of substrings of the given string, containing exactly k characters ""1"".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 the sought substrings are: ""1"", ""1"", ""10"", ""01"", ""10"", ""010"".In the second sample the sought substrings are: ""101"", ""0101"", ""1010"", ""01010"".
|
Input: 11010 | Output: 6
|
Medium
| 6 | 589 | 161 | 256 | 1 |
232 |
B
|
232B
|
B. Table
| 1,900 |
bitmasks; combinatorics; dp; math
|
John Doe has an n Γ m table. John Doe can paint points in some table cells, not more than one point in one table cell. John Doe wants to use such operations to make each square subtable of size n Γ n have exactly k points.John Doe wondered, how many distinct ways to fill the table with points are there, provided that the condition must hold. As this number can be rather large, John Doe asks to find its remainder after dividing by 1000000007 (109 + 7).You should assume that John always paints a point exactly in the center of some cell. Two ways to fill a table are considered distinct, if there exists a table cell, that has a point in one way and doesn't have it in the other.
|
A single line contains space-separated integers n, m, k (1 β€ n β€ 100; n β€ m β€ 1018; 0 β€ k β€ n2) β the number of rows of the table, the number of columns of the table and the number of points each square must contain.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 a single line print a single integer β the remainder from dividing the described number of ways by 1000000007 (109 + 7).
|
Let's consider the first test case: The gray area belongs to both 5 Γ 5 squares. So, if it has one point, then there shouldn't be points in any other place. If one of the white areas has a point, then the other one also must have a point. Thus, there are about 20 variants, where the point lies in the gray area and 25 variants, where each of the white areas contains a point. Overall there are 45 variants.
|
Input: 5 6 1 | Output: 45
|
Hard
| 4 | 682 | 363 | 123 | 2 |
1,926 |
D
|
1926D
|
D. Vlad and Division
| 1,300 |
bitmasks; greedy
|
Vladislav has \(n\) non-negative integers, and he wants to divide all of them into several groups so that in any group, any pair of numbers does not have matching bit values among bits from \(1\)-st to \(31\)-st bit (i.e., considering the \(31\) least significant bits of the binary representation).For an integer \(k\), let \(k_2(i)\) denote the \(i\)-th bit in its binary representation (from right to left, indexing from 1). For example, if \(k=43\), since \(43=101011_2\), then \(43_2(1)=1\), \(43_2(2)=1\), \(43_2(3)=0\), \(43_2(4)=1\), \(43_2(5)=0\), \(43_2(6)=1\), \(43_2(7)=0\), \(43_2(8)=0, \dots, 43_2(31)=0\).Formally, for any two numbers \(x\) and \(y\) in the same group, the condition \(x_2(i) \neq y_2(i)\) must hold for all \(1 \leq i < 32\).What is the minimum number of groups Vlad needs to achieve his goal? Each number must fall into exactly one group.
|
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 2 \cdot 10^5\)) β the total number of integers.The second line of each test case contains \(n\) given integers \(a_1, \ldots, a_n\) (\(0 \leq a_j < 2^{31}\)).The sum of \(n\) over all test cases in a test does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of groups required to satisfy the condition.
|
In the first test case, any two numbers have the same last \(31\) bits, so we need to place each number in its own group.In the second test case, \(a_1=0000000000000000000000000000000_2\), \(a_2=1111111111111111111111111111111_2\) so they can be placed in the same group because \(a_1(i) \ne a_2(i)\) for each \(i\) between \(1\) and \(31\), inclusive.
|
Input: 941 4 3 420 21474836475476319172 261956880 2136179468 1671164475 188552676731335890506 811593141 11282233624688873446 627404104 1520079543 1458610201461545621 2085938026 1269342732 143025857540 0 2147483647 214748364730 0 214748364781858058912 289424735 1858058912 2024818580 1858058912 289424735 122665067 289424735 | Output: 4 1 3 2 2 3 2 2 4
|
Easy
| 2 | 872 | 419 | 109 | 19 |
1,738 |
C
|
1738C
|
C. Even Number Addicts
| 1,500 |
dp; games; greedy; math
|
Alice and Bob are playing a game on a sequence \(a_1, a_2, \dots, a_n\) of length \(n\). They move in turns and Alice moves first.In the turn of each player, he or she should select an integer and remove it from the sequence. The game ends when there is no integer left in the sequence. Alice wins if the sum of her selected integers is even; otherwise, Bob wins. Your task is to determine who will win the game, if both players play optimally.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. The following lines contain the description of each test case.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 100\)), indicating the length of the sequence.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \leq a_i \leq 10^9\)), indicating the elements of the sequence.
|
For each test case, output ""Alice"" (without quotes) if Alice wins and ""Bob"" (without quotes) otherwise.
|
In the first and second test cases, Alice always selects two odd numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins.In the third test case, Bob has a winning strategy that he always selects a number with the same parity as Alice selects in her last turn. Therefore, Bob always wins.In the fourth test case, Alice always selects two even numbers, so the sum of her selected numbers is always even. Therefore, Alice always wins.
|
Input: 431 3 541 3 5 741 2 3 4410 20 30 40 | Output: Alice Alice Bob Alice
|
Medium
| 4 | 444 | 472 | 107 | 17 |
2 |
C
|
2C
|
C. Commentator problem
| 2,600 |
geometry
|
The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered. Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other.
|
The input data consists of three lines, each of them describes the position of one stadium. The lines have the format x, y, r, where (x, y) are the coordinates of the stadium's center ( - 103 β€ x, y β€ 103), and r (1 β€ r β€ 103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line.
|
Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank.
|
Input: 0 0 1060 0 1030 30 10 | Output: 30.00000 0.00000
|
Expert
| 1 | 894 | 368 | 207 | 0 |
|
1,244 |
B
|
1244B
|
B. Rooms and Staircases
| 1,000 |
brute force; implementation
|
Nikolay lives in a two-storied house. There are \(n\) rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between \(1\) and \(n\)). If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers \(i\) and \(i+1\) on each floor are neighbouring, for all \(1 \leq i \leq n - 1\). There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room \(x\) on the first floor and the room \(x\) on the second floor, then Nikolay can use it to move from one room to another. The picture illustrates a house with \(n = 4\). There is a staircase between the room \(2\) on the first floor and the room \(2\) on the second floor, and another staircase between the room \(4\) on the first floor and the room \(4\) on the second floor. The arrows denote possible directions in which Nikolay can move. The picture corresponds to the string ""0101"" in the input. Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been). Calculate the maximum number of rooms Nikolay can visit during his tour, if: he can start in any room on any floor of his choice, and he won't visit the same room twice.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the input. Then test cases follow. Each test case consists of two lines.The first line contains one integer \(n\) \((1 \le n \le 1\,000)\) β the number of rooms on each floor.The second line contains one string consisting of \(n\) characters, each character is either a '0' or a '1'. If the \(i\)-th character is a '1', then there is a staircase between the room \(i\) on the first floor and the room \(i\) on the second floor. If the \(i\)-th character is a '0', then there is no staircase between the room \(i\) on the first floor and the room \(i\) on the second floor.In hacks it is allowed to use only one test case in the input, so \(t = 1\) should be satisfied.
|
For each test case print one integer β the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.
|
In the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then β to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then β to the fifth room on the second floor. So, Nikolay visits \(6\) rooms.There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).In the third test case it is possible to visit all rooms: first floor, first room \(\rightarrow\) second floor, first room \(\rightarrow\) second floor, second room \(\rightarrow\) first floor, second room \(\rightarrow\) first floor, third room \(\rightarrow\) second floor, third room \(\rightarrow\) second floor, fourth room \(\rightarrow\) first floor, fourth room \(\rightarrow\) first floor, fifth room \(\rightarrow\) second floor, fifth room.In the fourth test case it is also possible to visit all rooms: second floor, third room \(\rightarrow\) second floor, second room \(\rightarrow\) second floor, first room \(\rightarrow\) first floor, first room \(\rightarrow\) first floor, second room \(\rightarrow\) first floor, third room.
|
Input: 4 5 00100 8 00000000 5 11111 3 110 | Output: 6 8 10 6
|
Beginner
| 2 | 1,582 | 775 | 183 | 12 |
257 |
D
|
257D
|
D. Sum
| 1,900 |
greedy; math
|
Vasya has found a piece of paper with an array written on it. The array consists of n integers a1, a2, ..., an. Vasya noticed that the following condition holds for the array ai β€ ai + 1 β€ 2Β·ai for any positive integer i (i < n).Vasya wants to add either a ""+"" or a ""-"" before each number of array. Thus, Vasya will get an expression consisting of n summands. The value of the resulting expression is the sum of all its elements. The task is to add signs ""+"" and ""-"" before each number so that the value of expression s meets the limits 0 β€ s β€ a1. Print a sequence of signs ""+"" and ""-"", satisfying the given limits. It is guaranteed that the solution for the problem exists.
|
The first line contains integer n (1 β€ n β€ 105) β the size of the array. The second line contains space-separated integers a1, a2, ..., an (0 β€ ai β€ 109) β the original array. It is guaranteed that the condition ai β€ ai + 1 β€ 2Β·ai fulfills for any positive integer i (i < n).
|
In a single line print the sequence of n characters ""+"" and ""-"", where the i-th character is the sign that is placed in front of number ai. The value of the resulting expression s must fit into the limits 0 β€ s β€ a1. If there are multiple solutions, you are allowed to print any of them.
|
Input: 41 2 3 5 | Output: +++-
|
Hard
| 2 | 687 | 275 | 291 | 2 |
|
238 |
A
|
238A
|
A. Not Wool Sequences
| 1,300 |
constructive algorithms; math
|
A sequence of non-negative integers a1, a2, ..., an of length n is called a wool sequence if and only if there exists two integers l and r (1 β€ l β€ r β€ n) such that . In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0.The expression means applying the operation of a bitwise xor to numbers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is marked as ""^"", in Pascal β as ""xor"".In this problem you are asked to compute the number of sequences made of n integers from 0 to 2m - 1 that are not a wool sequence. You should print this number modulo 1000000009 (109 + 9).
|
The only line of input contains two space-separated integers n and m (1 β€ n, m β€ 105).
|
Print the required number of sequences modulo 1000000009 (109 + 9) on the only line of output.
|
Sequences of length 3 made of integers 0, 1, 2 and 3 that are not a wool sequence are (1, 3, 1), (1, 2, 1), (2, 1, 2), (2, 3, 2), (3, 1, 3) and (3, 2, 3).
|
Input: 3 2 | Output: 6
|
Easy
| 2 | 686 | 86 | 94 | 2 |
798 |
C
|
798C
|
C. Mike and gcd problem
| 1,700 |
dp; greedy; number theory
|
Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 β€ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so. is the biggest non-negative number d such that d divides bi for every i (1 β€ i β€ n).
|
The first line contains a single integer n (2 β€ n β€ 100 000) β length of sequence A.The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 109) β elements of sequence A.
|
Output on the first line ""YES"" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and ""NO"" (without quotes) otherwise.If the answer was ""YES"", output the minimal number of moves needed to make sequence A beautiful.
|
In the first example you can simply make one move to obtain sequence [0, 2] with .In the second example the gcd of the sequence is already greater than 1.
|
Input: 21 1 | Output: YES1
|
Medium
| 3 | 651 | 192 | 275 | 7 |
955 |
A
|
955A
|
A. Feed the cat
| 1,100 |
greedy; math
|
After waking up at hh:mm, Andrew realised that he had forgotten to feed his only cat for yet another time (guess why there's only one cat). The cat's current hunger level is H points, moreover each minute without food increases his hunger by D points.At any time Andrew can visit the store where tasty buns are sold (you can assume that is doesn't take time to get to the store and back). One such bun costs C roubles and decreases hunger by N points. Since the demand for bakery drops heavily in the evening, there is a special 20% discount for buns starting from 20:00 (note that the cost might become rational). Of course, buns cannot be sold by parts.Determine the minimum amount of money Andrew has to spend in order to feed his cat. The cat is considered fed if its hunger level is less than or equal to zero.
|
The first line contains two integers hh and mm (00 β€ hh β€ 23, 00 β€ mm β€ 59) β the time of Andrew's awakening.The second line contains four integers H, D, C and N (1 β€ H β€ 105, 1 β€ D, C, N β€ 102).
|
Output the minimum amount of money to within three decimal digits. You answer is considered correct, if its absolute or relative error does not exceed 10 - 4.Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
|
In the first sample Andrew can visit the store at exactly 20:00. The cat's hunger will be equal to 315, hence it will be necessary to purchase 315 buns. The discount makes the final answer 25200 roubles.In the second sample it's optimal to visit the store right after he wakes up. Then he'll have to buy 91 bins per 15 roubles each and spend a total of 1365 roubles.
|
Input: 19 00255 1 100 1 | Output: 25200.0000
|
Easy
| 2 | 815 | 195 | 256 | 9 |
2,096 |
B
|
2096B
|
B. Wonderful Gloves
| 1,100 |
greedy; math; sortings
|
You are the proud owner of many colorful gloves, and you keep them in a drawer. Each glove is in one of \(n\) colors numbered \(1\) to \(n\). Specifically, for each \(i\) from \(1\) to \(n\), you have \(l_i\) left gloves and \(r_i\) right gloves with color \(i\).Unfortunately, it's late at night, so you can't see any of your gloves. In other words, you will only know the color and the type (left or right) of a glove after you take it out of the drawer.A matching pair of gloves with color \(i\) consists of exactly one left glove and one right glove with color \(i\). Find the minimum number of gloves you need to take out of the drawer to guarantee that you have at least \(k\) matching pairs of gloves with different colors.Formally, find the smallest positive integer \(x\) such that: For any set of \(x\) gloves you take out of the drawer, there will always be at least \(k\) matching pairs of gloves with different colors.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(k\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\)) β the number of different colors, and the minimum number of required matching pairs of gloves.The second line of each test case contains \(n\) integers \(l_1, l_2, \ldots, l_n\) (\(1 \leq l_i \leq 10^9\)) β the number of left gloves with color \(i\) for each \(i\) from \(1\) to \(n\).The third line of each test case contains \(n\) integers \(r_1, r_2, \ldots, r_n\) (\(1 \leq r_i \leq 10^9\)) β the number of right gloves with color \(i\) for each \(i\) from \(1\) to \(n\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of gloves you need to take out of the drawer.
|
In the first test case, you must take out all of the gloves, so the answer is \(6\).In the second test case, the answer is \(101\). If you take out \(100\) gloves or fewer, then it is possible that all of them are left gloves, which means you won't have a matching pair of gloves.In the third test case, the answer is \(303\). If you only take out \(302\) gloves, then one possible scenario is as follows: Color \(1\): \(100\) left gloves, \(200\) right gloves Color \(2\): \(1\) left glove, \(0\) right gloves Color \(3\): \(0\) left gloves, \(1\) right glove You only have multiple matching pairs of gloves with color \(1\). So you won't have at least \(2\) matching pairs of gloves with different colors.
|
Input: 53 31 1 11 1 11 110013 2100 1 1200 1 15 297 59 50 87 3695 77 33 13 7410 697 59 50 87 36 95 77 33 13 7491 14 84 33 54 89 68 34 14 15 | Output: 6 101 303 481 1010
|
Easy
| 3 | 931 | 838 | 110 | 20 |
96 |
B
|
96B
|
B. Lucky Numbers (easy)
| 1,300 |
binary search; bitmasks; brute force
|
Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.One day Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.
|
The only line contains a positive integer n (1 β€ n β€ 109). This number doesn't have leading zeroes.
|
Output the least super lucky number that is more than or equal to n.Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
|
Input: 4500 | Output: 4747
|
Easy
| 3 | 513 | 99 | 221 | 0 |
|
302 |
A
|
302A
|
A. Eugeny and Array
| 800 |
implementation
|
Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to 1. Also, he has m queries: Query number i is given as a pair of integers li, ri (1 β€ li β€ ri β€ n). The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum ali + ali + 1 + ... + ari = 0, otherwise the response to the query will be integer 0. Help Eugeny, answer all his queries.
|
The first line contains integers n and m (1 β€ n, m β€ 2Β·105). The second line contains n integers a1, a2, ..., an (ai = -1, 1). Next m lines contain Eugene's queries. The i-th line contains integers li, ri (1 β€ li β€ ri β€ n).
|
Print m integers β the responses to Eugene's queries in the order they occur in the input.
|
Input: 2 31 -11 11 22 2 | Output: 010
|
Beginner
| 1 | 425 | 223 | 90 | 3 |
|
2,047 |
B
|
2047B
|
B. Replace Character
| 900 |
brute force; combinatorics; greedy; strings
|
You're given a string \(s\) of length \(n\), consisting of only lowercase English letters. You must do the following operation exactly once: Choose any two indices \(i\) and \(j\) (\(1 \le i, j \le n\)). You can choose \(i = j\). Set \(s_i := s_j\). You need to minimize the number of distinct permutations\(^\dagger\) of \(s\). Output any string with the smallest number of distinct permutations after performing exactly one operation.\(^\dagger\) A permutation of the string is an arrangement of its characters into any order. For example, ""bac"" is a permutation of ""abc"" but ""bcc"" is not.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The first line of each test case contains \(n\) (\(1 \le n \le 10\)) β the length of string \(s\).The second line of each test case contains \(s\) of length \(n\). The string contains only lowercase English letters.
|
For each test case, output the required \(s\) after applying exactly one operation. If there are multiple solutions, print any of them.
|
In the first test case, we can obtain the following strings in one operation: ""abc"", ""bbc"", ""cbc"", ""aac"", ""acc"", ""aba"", and ""abb"".The string ""abc"" has \(6\) distinct permutations: ""abc"", ""acb"", ""bac"", ""bca"", ""cab"", and ""cba"".The string ""cbc"" has \(3\) distinct permutations: ""bcc"", ""cbc"", and ""ccb"", which is the lowest of all the obtainable strings. In fact, all obtainable strings except ""abc"" have \(3\) permutations, so any of them would be accepted.
|
Input: 63abc4xyyx8alphabet1k10aabbccddee6ttbddq | Output: cbc yyyx alphaaet k eabbccddee tttddq
|
Beginner
| 4 | 597 | 375 | 135 | 20 |
2,085 |
E
|
2085E
|
E. Serval and Modulo
| 2,200 |
constructive algorithms; math; number theory
|
There is an array \(a\) consisting of \(n\) non-negative integers and a magic number \(k\) (\(k\ge 1\), \(k\) is an integer). Serval has constructed another array \(b\) of length \(n\), where \(b_i = a_i \bmod k\) holds\(^{\text{β}}\) for all \(1\leq i\leq n\). Then, he shuffled \(b\).You are given the two arrays \(a\) and \(b\). Find a possible magic number \(k\). However, there is a small possibility that Serval fooled you, and such an integer does not exist. In this case, output \(-1\).It can be shown that, under the constraints of the problem, if such an integer \(k\) exists, then there exists a valid answer no more than \(10^9\). And you need to guarantee that \(k\le 10^9\) in your output.\(^{\text{β}}\)\(a_i \bmod k\) denotes the remainder from dividing \(a_i\) by \(k\).
|
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 an integer \(n\) (\(1\leq n\leq 10^4\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0\leq a_i\leq 10^6\)) β the elements of the array \(a\).The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(0\leq b_i\leq 10^6\)) β the elements of the array \(b\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^4\).
|
For each test case, output a single integer \(k\) (\(1\leq k\leq 10^9\)) β the magic number you found. Print \(-1\) if it is impossible to find such an integer.If there are multiple answers, you may print any of them.
|
In the first test case, if \(k\ge 3\), then \(2=a_3\bmod k\) should be in the array \(b\), which leads to a contradiction. For \(k = 1\), \([a_1\bmod k, a_2\bmod k, a_3\bmod k, a_4 \bmod k] = [0,0,0,0]\), which cannot be shuffled to \(b\). For \(k = 2\), \([a_1\bmod k, a_2\bmod k, a_3\bmod k, a_4 \bmod k] = [1,1,0,1]\), which can be shuffled to \(b\). Thus, the only possible answer is \(k=2\).In the second test case, note that \(b\) can be obtained by shuffling \(a\). Thus, all integers between \(6\) and \(10^9\) are possible answers.In the third test case, it can be shown that such \(k\) does not exist. Got fooled by Serval!
|
Input: 543 5 2 70 1 1 153 1 5 2 41 2 3 4 562 3 4 7 8 91 2 3 6 7 8521 22 25 28 200 1 2 1 061 1 2 3 5 80 0 1 1 0 0 | Output: 2 31415926 -1 4 -1
|
Hard
| 3 | 787 | 609 | 217 | 20 |
25 |
D
|
25D
|
D. Roads not only in Berland
| 1,900 |
dsu; graphs; trees
|
Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in total and exactly n - 1 two-way roads. Because of the recent financial crisis, the Berland Government is strongly pressed for money, so to build a new road it has to close some of the existing ones. Every day it is possible to close one existing road and immediately build a new one. Your task is to determine how many days would be needed to rebuild roads so that from each city it became possible to reach all the others, and to draw a plan of closure of old roads and building of new ones.
|
The first line contains integer n (2 β€ n β€ 1000) β amount of cities in Berland and neighboring countries. Next n - 1 lines contain the description of roads. Each road is described by two space-separated integers ai, bi (1 β€ ai, bi β€ n, ai β bi) β pair of cities, which the road connects. It can't be more than one road between a pair of cities. No road connects the city with itself.
|
Output the answer, number t β what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then output t lines β the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v β it means that road between cities i and j became closed and a new road between cities u and v is built. Cities are numbered from 1. If the answer is not unique, output any.
|
Input: 21 2 | Output: 0
|
Hard
| 3 | 779 | 383 | 465 | 0 |
|
732 |
B
|
732B
|
B. Cormen β The Best Friend Of a Man
| 1,000 |
dp; greedy
|
Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. Write a program that will find the minumum number of additional walks and the appropriate schedule β the sequence of integers b1, b2, ..., bn (bi β₯ ai), where bi means the total number of walks with the dog on the i-th day.
|
The first line contains two integers n and k (1 β€ n, k β€ 500) β the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers a1, a2, ..., an (0 β€ ai β€ 500) β the number of walks with Cormen on the i-th day which Polycarp has already planned.
|
In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days. In the second line print n integers b1, b2, ..., bn, where bi β the total number of walks on the i-th day according to the found solutions (ai β€ bi for all i from 1 to n). If there are multiple solutions, print any of them.
|
Input: 3 52 0 1 | Output: 42 3 2
|
Beginner
| 2 | 1,189 | 307 | 381 | 7 |
|
271 |
B
|
271B
|
B. Prime Matrix
| 1,300 |
binary search; brute force; math; number theory
|
You've got an n Γ m matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times.You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not. A matrix is prime if at least one of the two following conditions fulfills: the matrix has a row with prime numbers only; the matrix has a column with prime numbers only; Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
|
The first line contains two integers n, m (1 β€ n, m β€ 500) β the number of rows and columns in the matrix, correspondingly.Each of the following n lines contains m integers β the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105.The numbers in the lines are separated by single spaces.
|
Print a single integer β the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.
|
In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3.In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2.In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.
|
Input: 3 31 2 35 6 14 4 1 | Output: 1
|
Easy
| 4 | 776 | 346 | 145 | 2 |
1,009 |
G
|
1009G
|
G. Allowed Letters
| 2,400 |
bitmasks; flows; graph matchings; graphs; greedy
|
Polycarp has just launched his new startup idea. The niche is pretty free and the key vector of development sounds really promising, so he easily found himself some investors ready to sponsor the company. However, he is yet to name the startup!Actually, Polycarp has already came up with the name but some improvement to it will never hurt. So now he wants to swap letters at some positions in it to obtain the better name. It isn't necessary for letters to be adjacent.In addition, each of the investors has chosen some index in the name and selected a set of letters that can go there. Indices chosen by different investors are pairwise distinct. If some indices aren't chosen by any investor then any letter can go there.Finally, Polycarp is sure that the smallest lexicographically name is the best. (Like why do you think Google decided to become Alphabet?)More formally, you are given a string consisting of lowercase Latin letters from ""a"" to ""f"". You can swap letters at any positions arbitrary number of times (zero swaps is also possible).What is the smallest lexicographically name you can obtain such that the letter at every position is among the allowed letters?If Polycarp can't produce any valid name then print ""Impossible"".
|
The first line is the string \(s\) (\(1 \le |s| \le 10^5\)) β the name Polycarp has came up with. The string consists only of lowercase Latin letters from ""a"" to ""f"".The second line contains a single integer \(m\) (\(0 \le m \le |s|\)) β the number of investors.The \(i\)-th of the next \(m\) lines contain an integer number \(pos_i\) and a non-empty string of allowed characters for \(pos_i\) (\(1 \le pos_i \le |s|\)). Each string contains pairwise distinct letters from ""a"" to ""f"". \(pos_1, pos_2, \dots, pos_m\) are pairwise distinct. If any position of the string doesn't appear in the investors demands then any letter can go in this position.
|
If Polycarp can't produce any valid name then print ""Impossible"".Otherwise print the smallest lexicographically name Polycarp can obtain by swapping letters in string \(s\) such that the letter at every position is among the allowed ones.
|
Input: bedefead52 e1 dc5 b7 ef6 ef | Output: deadbeef
|
Expert
| 5 | 1,247 | 657 | 240 | 10 |
|
1,787 |
C
|
1787C
|
C. Remove the Bracket
| 1,600 |
dp; greedy; math
|
RSJ has a sequence \(a\) of \(n\) integers \(a_1,a_2, \ldots, a_n\) and an integer \(s\). For each of \(a_2,a_3, \ldots, a_{n-1}\), he chose a pair of non-negative integers \(x_i\) and \(y_i\) such that \(x_i+y_i=a_i\) and \((x_i-s) \cdot (y_i-s) \geq 0\).Now he is interested in the value $$$\(F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + \ldots + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.\)\(Please help him find the minimum possible value \)F\( he can get by choosing \)x_i\( and \)y_i$$$ optimally. It can be shown that there is always at least one valid way to choose them.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\), \(s\) (\(3 \le n \le 2 \cdot 10^5\); \(0 \le s \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(0 \le a_i \le 2 \cdot 10^5\)).It is guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\).
|
For each test case, print the minimum possible value of \(F\).
|
In the first test case, \(2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0\).In the second test case, \(5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18\).
|
Input: 105 02 0 1 3 45 15 3 4 3 57 27 6 5 4 3 2 15 11 2 3 4 55 21 2 3 4 54 00 1 1 15 54 3 5 6 44 10 2 1 03 99999200000 200000 2000006 81397976 129785 12984 78561 173685 15480 | Output: 0 18 32 11 14 0 16 0 40000000000 2700826806
|
Medium
| 3 | 582 | 428 | 62 | 17 |
542 |
E
|
542E
|
E. Playing on Graph
| 2,600 |
graphs; shortest paths
|
Vova and Marina love offering puzzles to each other. Today Marina offered Vova to cope with the following task.Vova has a non-directed graph consisting of n vertices and m edges without loops and multiple edges. Let's define the operation of contraction two vertices a and b that are not connected by an edge. As a result of this operation vertices a and b are deleted and instead of them a new vertex x is added into the graph, and also edges are drawn from it to all vertices that were connected with a or with b (specifically, if the vertex was connected with both a and b, then also exactly one edge is added from x to it). Thus, as a result of contraction again a non-directed graph is formed, it contains no loops nor multiple edges, and it contains (n - 1) vertices.Vova must perform the contraction an arbitrary number of times to transform the given graph into a chain of the maximum length. A chain of length k (k β₯ 0) is a connected graph whose vertices can be numbered with integers from 1 to k + 1 so that the edges of the graph connect all pairs of vertices (i, i + 1) (1 β€ i β€ k) and only them. Specifically, the graph that consists of one vertex is a chain of length 0. The vertices that are formed as a result of the contraction are allowed to be used in the following operations of contraction. The picture illustrates the contraction of two vertices marked by red. Help Vova cope with his girlfriend's task. Find the maximum length of the chain that can be obtained from the resulting graph or else determine that it is impossible to obtain the chain.
|
The first line contains two integers n, m (1 β€ n β€ 1000, 0 β€ m β€ 100 000) β the number of vertices and the number of edges in the original graph.Next m lines contain the descriptions of edges in the format ai, bi (1 β€ ai, bi β€ n, ai β bi), which means that there is an edge between vertices ai and bi. It is guaranteed that there is at most one edge between each pair of vertexes.
|
If it is impossible to obtain a chain from the given graph, print - 1. Otherwise, print the maximum possible number of edges in the resulting chain.
|
In the first sample test you can contract vertices 4 and 5 and obtain a chain of length 3.In the second sample test it is initially impossible to contract any pair of vertexes, so it is impossible to achieve the desired result.In the third sample test you can contract vertices 1 and 2 and obtain a chain of length 2.
|
Input: 5 41 22 33 43 5 | Output: 3
|
Expert
| 2 | 1,570 | 380 | 148 | 5 |
1,749 |
D
|
1749D
|
D. Counting Arrays
| 1,900 |
combinatorics; dp; math; number theory
|
Consider an array \(a\) of length \(n\) with elements numbered from \(1\) to \(n\). It is possible to remove the \(i\)-th element of \(a\) if \(gcd(a_i, i) = 1\), where \(gcd\) denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position.An array \(b\) with \(n\) integers such that \(1 \le b_i \le n - i + 1\) is a removal sequence for the array \(a\) if it is possible to remove all elements of \(a\), if you remove the \(b_1\)-th element, then the \(b_2\)-th, ..., then the \(b_n\)-th element. For example, let \(a = [42, 314]\): \([1, 1]\) is a removal sequence: when you remove the \(1\)-st element of the array, the condition \(gcd(42, 1) = 1\) holds, and the array becomes \([314]\); when you remove the \(1\)-st element again, the condition \(gcd(314, 1) = 1\) holds, and the array becomes empty. \([2, 1]\) is not a removal sequence: when you try to remove the \(2\)-nd element, the condition \(gcd(314, 2) = 1\) is false. An array is ambiguous if it has at least two removal sequences. For example, the array \([1, 2, 5]\) is ambiguous: it has removal sequences \([3, 1, 1]\) and \([1, 2, 1]\). The array \([42, 314]\) is not ambiguous: the only removal sequence it has is \([1, 1]\).You are given two integers \(n\) and \(m\). You have to calculate the number of ambiguous arrays \(a\) such that the length of \(a\) is from \(1\) to \(n\) and each \(a_i\) is an integer from \(1\) to \(m\).
|
The only line of the input contains two integers \(n\) and \(m\) (\(2 \le n \le 3 \cdot 10^5\); \(1 \le m \le 10^{12}\)).
|
Print one integer β the number of ambiguous arrays \(a\) such that the length of \(a\) is from \(1\) to \(n\) and each \(a_i\) is an integer from \(1\) to \(m\). Since the answer can be very large, print it modulo \(998244353\).
|
Input: 2 3 | Output: 6
|
Hard
| 4 | 1,472 | 121 | 228 | 17 |
|
1,202 |
E
|
1202E
|
E. You Are Given Some Strings...
| 2,400 |
brute force; string suffix structures; strings
|
You are given a string \(t\) and \(n\) strings \(s_1, s_2, \dots, s_n\). All strings consist of lowercase Latin letters.Let \(f(t, s)\) be the number of occurences of string \(s\) in string \(t\). For example, \(f('\text{aaabacaa}', '\text{aa}') = 3\), and \(f('\text{ababa}', '\text{aba}') = 2\).Calculate the value of \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} f(t, s_i + s_j)\), where \(s + t\) is the concatenation of strings \(s\) and \(t\). Note that if there are two pairs \(i_1\), \(j_1\) and \(i_2\), \(j_2\) such that \(s_{i_1} + s_{j_1} = s_{i_2} + s_{j_2}\), you should include both \(f(t, s_{i_1} + s_{j_1})\) and \(f(t, s_{i_2} + s_{j_2})\) in answer.
|
The first line contains string \(t\) (\(1 \le |t| \le 2 \cdot 10^5\)).The second line contains integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).Each of next \(n\) lines contains string \(s_i\) (\(1 \le |s_i| \le 2 \cdot 10^5\)).It is guaranteed that \(\sum\limits_{i=1}^{n} |s_i| \le 2 \cdot 10^5\). All strings consist of lowercase English letters.
|
Print one integer β the value of \(\sum\limits_{i=1}^{n} \sum\limits_{j=1}^{n} f(t, s_i + s_j)\).
|
Input: aaabacaa 2 a aa | Output: 5
|
Expert
| 3 | 668 | 345 | 97 | 12 |
|
2,062 |
F
|
2062F
|
F. Traveling Salescat
| 2,900 |
constructive algorithms; dp; geometry; graphs; greedy; math; sortings
|
You are a cat selling fun algorithm problems. Today, you want to recommend your fun algorithm problems to \(k\) cities.There are a total of \(n\) cities, each with two parameters \(a_i\) and \(b_i\). Between any two cities \(i,j\) (\(i\ne j\)), there is a bidirectional road with a length of \(\max(a_i + b_j , b_i + a_j)\). The cost of a path is defined as the total length of roads between every two adjacent cities along the path.For \(k=2,3,\ldots,n\), find the minimum cost among all simple paths containing exactly \(k\) distinct cities.
|
The first line of input contains a single integer \(t\) (\(1 \leq t \leq 1500\)) β the number of test cases.For each test case, the first line contains a single integer \(n\) (\(2 \leq n \leq 3\cdot 10^3\)) β the number of cities.Then \(n\) lines follow, the \(i\)-th line contains two integers \(a_i,b_i\) (\(0 \leq a_i,b_i \leq 10^9\)) β the parameters of city \(i\).It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(9\cdot 10^6\).
|
For each test case, print \(n-1\) integers in one line. The \(i\)-th integer represents the minimum cost when \(k=i+1\).
|
In the first test case: For \(k=2\), the optimal path is \(1\to 2\) with a cost of \(\max(0+1,2+2)=4\). For \(k=3\), the optimal path is \(2\to 1\to 3\) with a cost of \(\max(0+1,2+2)+\max(0+3,3+2)=4+5=9\). In the second test case: For \(k=2\), the optimal path is \(1\to 4\). For \(k=3\), the optimal path is \(2\to 3\to 5\). For \(k=4\), the optimal path is \(4\to 1\to 3\to 5\). For \(k=5\), the optimal path is \(5\to 2\to 3\to 1\to 4\).
|
Input: 330 22 13 352 77 56 31 87 58899167687 609615846851467150 45726720931502759 23784096918190644 196992738142090421 475722765409556751 726971942513558832 998277529294328304 434714258 | Output: 4 9 10 22 34 46 770051069 1655330585 2931719265 3918741472 5033924854 6425541981 7934325514
|
Master
| 7 | 543 | 462 | 120 | 20 |
1,750 |
A
|
1750A
|
A. Indirect Sort
| 800 |
constructive algorithms; implementation; math
|
You are given a permutation \(a_1, a_2, \ldots, a_n\) of size \(n\), where each integer from \(1\) to \(n\) appears exactly once.You can do the following operation any number of times (possibly, zero): Choose any three indices \(i, j, k\) (\(1 \le i < j < k \le n\)). If \(a_i > a_k\), replace \(a_i\) with \(a_i + a_j\). Otherwise, swap \(a_j\) and \(a_k\). Determine whether you can make the array \(a\) sorted in non-descending order.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 10\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1,a_2,\dots,a_n\) (\(1 \le a_i \le n\), \(a_i \neq a_j\) if \(i \neq j\)) β the elements of the array \(a\).
|
For each test case, output ""Yes"" (without quotes) if the array can be sorted in non-descending order, 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, \([1,2,3]\) is already sorted in non-descending order.In the second test case, we can choose \(i = 1,j = 2,k = 3\). Since \(a_1 \le a_3\), swap \(a_2\) and \(a_3\), the array then becomes \([1,2,3]\), which is sorted in non-descending order.In the seventh test case, we can do the following operations successively: Choose \(i = 5,j = 6,k = 7\). Since \(a_5 \le a_7\), swap \(a_6\) and \(a_7\), the array then becomes \([1,2,6,7,4,5,3]\). Choose \(i = 5,j = 6,k = 7\). Since \(a_5 > a_7\), replace \(a_5\) with \(a_5+a_6=9\), the array then becomes \([1,2,6,7,9,5,3]\). Choose \(i = 2,j = 5,k = 7\). Since \(a_2 \le a_7\), swap \(a_5\) and \(a_7\), the array then becomes \([1,2,6,7,3,5,9]\). Choose \(i = 2,j = 4,k = 6\). Since \(a_2 \le a_6\), swap \(a_4\) and \(a_6\), the array then becomes \([1,2,6,5,3,7,9]\). Choose \(i = 1,j = 3,k = 5\). Since \(a_1 \le a_5\), swap \(a_3\) and \(a_5\), the array then becomes \([1,2,3,5,6,7,9]\), which is sorted in non-descending order. In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order.
|
Input: 731 2 331 3 275 3 4 7 6 2 177 6 5 4 3 2 152 1 4 5 352 1 3 4 571 2 6 7 4 3 5 | Output: Yes Yes No No No No Yes
|
Beginner
| 3 | 437 | 449 | 282 | 17 |
1,663 |
F
|
1663F
|
F. In Every Generation...
| 0 |
*special; strings
|
In every generation there is a Chosen One. She alone will stand against the vampires, the demons, and the forces of darkness. She is the Slayer. β Joss Whedon
|
A string \(s\) (\(3 \le |s| \le 7\)) consisting of lowercase English letters.
|
A single string. If there is no answer, print ""none"" (without the quotes).
|
Input: tourist | Output: ooggqjx
|
Beginner
| 2 | 158 | 77 | 76 | 16 |
|
2,029 |
I
|
2029I
|
I. Variance Challenge
| 3,400 |
flows; graphs; greedy
|
Kevin has recently learned the definition of variance. For an array \(a\) of length \(n\), the variance of \(a\) is defined as follows: Let \(x=\dfrac{1}{n}\displaystyle\sum_{i=1}^n a_i\), i.e., \(x\) is the mean of the array \(a\); Then, the variance of \(a\) is $$$\( V(a)=\frac{1}{n}\sum_{i=1}^n(a_i-x)^2. \)\( Now, Kevin gives you an array \)a\( consisting of \)n\( integers, as well as an integer \)k\(. You can perform the following operation on \)a\(: Select an interval \)[l,r]\( (\)1\le l\le r\le n\(), then for each \)l\le i\le r\(, increase \)a_i\( by \)k\(. For each \)1\le p\le m\(, you have to find the minimum possible variance of \)a\( after exactly \)p\( operations are performed, independently for each \)p\(.For simplicity, you only need to output the answers multiplied by \)n^2$$$. It can be proven that the results are always integers.
|
Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1\le t\le 100\)) β the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1\le n,m\le 5000\), \(\color{red}{n\cdot m\le 2\cdot 10^4}\), \(1\le k\le 10^5\)) β the length of the array \(a\), the maximum number of operations, and the number you add to \(a_i\) each time, respectively.The second line contains \(n\) integers \(a_1,a_2,\ldots, a_n\) (\(1\le a_i\le 10^5\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\cdot m\) over all tests does not exceed \(2\cdot 10^4\).
|
For each test case, output \(m\) integers in a single line, the \(p\)-th integer denoting the minimum possible variance of \(a\) when exactly \(p\) operations are performed, multiplied by \(n^2\).
|
In the first test case: For \(p = 1\), you can perform the operation on \([1, 1]\), changing \(a\) from \([1, 2, 2]\) to \([2, 2, 2]\). Since all of the elements are equal, the variance is equal to \(0\). For \(p = 2\), you can perform the operation on \([1, 3]\) and then \([1, 1]\), changing \(a\) from \([1, 2, 2]\) to \([2, 3, 3]\) to \([3, 3, 3]\). Since all of the elements are equal, the variance is equal to \(0\). In the second test case, some possible optimal choices are: \(p=1\): \([\underline{1,}\,2,2]\to [3,2,2]\); \(p=2\): \([1,\underline{2,2}] \to [\underline{1,}\,4,4] \to [3,4,4]\). In the third test case, some possible optimal choices are: \(p=1\): \([10,\underline{1,1,1,1,10,1,1,1,1}]\to[10,2,2,2,2,11,2,2,2,2]\); \(p=2\): \([10,1,1,1,1,10,\underline{1,1,1,1}] \to [10,\underline{1,1,1,1},10,2,2,2,2] \to [10,2,2,2,2,10,2,2,2,2]\). In the eighth test case, the optimal choice for all \(p\) is to perform the operation on the whole array \(p\) times.
|
Input: 93 2 11 2 23 2 21 2 210 2 110 1 1 1 1 10 1 1 1 16 8 21 1 4 5 1 38 8 720 43 24 2 4 3 20 438 8 320 43 24 2 4 3 20 4310 12 15 3 3 5 4 1 8 1 1 113 10 1000001 2 3 4 5 6 7 8 9 10 11 5 410 5 100002308 9982 4435 3310 100000 9 7 8100 1919 100000 | Output: 0 0 2 2 1161 1024 53 21 21 5 5 5 5 5 10608 6912 4448 3104 1991 1312 535 304 13248 11184 9375 7815 6447 5319 4383 3687 385 316 269 224 181 156 124 101 80 56 41 29 1486 1486 1486 1486 1486 1486 1486 1486 1486 1486 134618047140 119919447140 107020847140 93922247140 82623647140
|
Master
| 3 | 857 | 691 | 196 | 20 |
530 |
C
|
530C
|
C. Diophantine equation
| 1,500 |
*special
|
You are given an equation A * X + B * Y = C, A, B, C are positive integer coefficients, X and Y are variables which can have positive integer values only. Output the number of solutions of this equation and the solutions themselves.
|
The only line of input contains integers A, B and C (1 β€ A, B, C β€ 1000), separated with spaces.
|
In the first line of the output print the number of the solutions N. In the next N lines print the solutions, formatted as ""XY"", sorted in ascending order of X, one solution per line.
|
Input: 3 5 35 | Output: 25 410 1
|
Medium
| 1 | 232 | 96 | 185 | 5 |
|
411 |
A
|
411A
|
A. Password Check
| 800 |
*special; implementation
|
You have probably registered on Internet sites many times. And each time you should enter your invented password. Usually the registration form automatically checks the password's crypt resistance. If the user's password isn't complex enough, a message is displayed. Today your task is to implement such an automatic check.Web-developers of the company Q assume that a password is complex enough, if it meets all of the following conditions: the password length is at least 5 characters; the password contains at least one large English letter; the password contains at least one small English letter; the password contains at least one digit. You are given a password. Please implement the automatic check of its complexity for company Q.
|
The first line contains a non-empty sequence of characters (at most 100 characters). Each character is either a large English letter, or a small English letter, or a digit, or one of characters: ""!"", ""?"", ""."", "","", ""_"".
|
If the password is complex enough, print message ""Correct"" (without the quotes), otherwise print message ""Too weak"" (without the quotes).
|
Input: abacaba | Output: Too weak
|
Beginner
| 2 | 739 | 229 | 141 | 4 |
|
493 |
C
|
493C
|
C. Vasya and Basketball
| 1,600 |
binary search; brute force; data structures; implementation; sortings; two pointers
|
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of d meters, and a throw is worth 3 points if the distance is larger than d meters, where d is some non-negative integer.Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of d. Help him to do that.
|
The first line contains integer n (1 β€ n β€ 2Β·105) β the number of throws of the first team. Then follow n integer numbers β the distances of throws ai (1 β€ ai β€ 2Β·109). Then follows number m (1 β€ m β€ 2Β·105) β the number of the throws of the second team. Then follow m integer numbers β the distances of throws of bi (1 β€ bi β€ 2Β·109).
|
Print two numbers in the format a:b β the score that is possible considering the problem conditions where the result of subtraction a - b is maximum. If there are several such scores, find the one in which number a is maximum.
|
Input: 31 2 325 6 | Output: 9:6
|
Medium
| 6 | 589 | 333 | 226 | 4 |
|
1,152 |
E
|
1152E
|
E. Neko and Flashback
| 2,400 |
constructive algorithms; dfs and similar; graphs
|
A permutation of length \(k\) is a sequence of \(k\) integers from \(1\) to \(k\) containing each integer exactly once. For example, the sequence \([3, 1, 2]\) is a permutation of length \(3\).When Neko was five, he thought of an array \(a\) of \(n\) positive integers and a permutation \(p\) of length \(n - 1\). Then, he performed the following: Constructed an array \(b\) of length \(n-1\), where \(b_i = \min(a_i, a_{i+1})\). Constructed an array \(c\) of length \(n-1\), where \(c_i = \max(a_i, a_{i+1})\). Constructed an array \(b'\) of length \(n-1\), where \(b'_i = b_{p_i}\). Constructed an array \(c'\) of length \(n-1\), where \(c'_i = c_{p_i}\). For example, if the array \(a\) was \([3, 4, 6, 5, 7]\) and permutation \(p\) was \([2, 4, 1, 3]\), then Neko would have constructed the following arrays: \(b = [3, 4, 5, 5]\) \(c = [4, 6, 6, 7]\) \(b' = [4, 5, 3, 5]\) \(c' = [6, 7, 4, 6]\) Then, he wrote two arrays \(b'\) and \(c'\) on a piece of paper and forgot about it. 14 years later, when he was cleaning up his room, he discovered this old piece of paper with two arrays \(b'\) and \(c'\) written on it. However he can't remember the array \(a\) and permutation \(p\) he used.In case Neko made a mistake and there is no array \(a\) and permutation \(p\) resulting in such \(b'\) and \(c'\), print -1. Otherwise, help him recover any possible array \(a\).
|
The first line contains an integer \(n\) (\(2 \leq n \leq 10^5\)) β the number of elements in array \(a\).The second line contains \(n-1\) integers \(b'_1, b'_2, \ldots, b'_{n-1}\) (\(1 \leq b'_i \leq 10^9\)).The third line contains \(n-1\) integers \(c'_1, c'_2, \ldots, c'_{n-1}\) (\(1 \leq c'_i \leq 10^9\)).
|
If Neko made a mistake and there is no array \(a\) and a permutation \(p\) leading to the \(b'\) and \(c'\), print -1. Otherwise, print \(n\) positive integers \(a_i\) (\(1 \le a_i \le 10^9\)), denoting the elements of the array \(a\).If there are multiple possible solutions, print any of them.
|
The first example is explained is the problem statement.In the third example, for \(a = [3, 4, 5, 2, 1, 4, 3, 2]\), a possible permutation \(p\) is \([7, 1, 5, 4, 3, 2, 6]\). In that case, Neko would have constructed the following arrays: \(b = [3, 4, 2, 1, 1, 3, 2]\) \(c = [4, 5, 5, 2, 4, 4, 3]\) \(b' = [2, 3, 1, 1, 2, 4, 3]\) \(c' = [3, 4, 4, 2, 5, 5, 4]\)
|
Input: 5 4 5 3 5 6 7 4 6 | Output: 3 4 6 5 7
|
Expert
| 3 | 1,371 | 311 | 295 | 11 |
1,978 |
E
|
1978E
|
E. Computing Machine
| 2,000 |
brute force; data structures; dp; greedy; implementation
|
Sasha has two binary strings \(s\) and \(t\) of the same length \(n\), consisting of the characters 0 and 1.There is also a computing machine that can perform two types of operations on binary strings \(a\) and \(b\) of the same length \(k\): If \(a_{i} = a_{i + 2} =\) 0, then you can assign \(b_{i + 1} :=\) 1 (\(1 \le i \le k - 2\)). If \(b_{i} = b_{i + 2} =\) 1, then you can assign \(a_{i + 1} :=\) 1 (\(1 \le i \le k - 2\)). Sasha became interested in the following: if we consider the string \(a=s_ls_{l+1}\ldots s_r\) and the string \(b=t_lt_{l+1}\ldots t_r\), what is the maximum number of 1 characters in the string \(a\) that can be obtained using the computing machine. Since Sasha is very curious but lazy, it is up to you to answer this question for several pairs \((l_i, r_i)\) that interest him.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^{4}\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the strings \(s\) and \(t\).The second line of each test case contains a binary string \(s\) of length \(n\), consisting of the characters 0 and 1.The third line of each test case contains a binary string \(t\) of length \(n\), consisting of the characters 0 and 1.The fourth line of each test case contains a single integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of queries.The \(i\)-th of the following lines contains two integers \(l_{i}\) and \(r_{i}\) (\(1 \le l_{i} \le r_{i} \le n\)) β the boundaries of the \(i\)-th pair of substrings that interest Sasha.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\) and the sum of \(q\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(q\) integers β the answers to all queries.
|
In the first test case: In the first query, \(a =\) 11, so the maximum number of 1 characters is \(2\). In the second query, \(a =\) 111, so the maximum number of 1 characters is \(3\). In the second test case: In the first query, \(a =\) 101 and \(b =\) 110. No operations can be performed, so the maximum number of 1 characters is \(2\). In the second query, \(a =\) 1010 and \(b =\) 1101. Since \(a_2 = a_4 =\) 0, we can assign \(b_3 :=\) 1. Now \(b_1 = b_3 =\) 1, so we can assign \(a_2 :=\) 1. The string \(a\) becomes 1110, so the maximum number of 1 characters is \(3\).
|
Input: 341111000021 22 441010110121 31 4601010101101052 31 62 54 43 6 | Output: 2 3 2 3 1 4 3 1 2
|
Hard
| 5 | 811 | 1,037 | 71 | 19 |
908 |
B
|
908B
|
B. New Year and Buggy Bot
| 1,200 |
brute force; implementation
|
Bob programmed a robot to navigate through a 2d maze.The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it.The robot can only move up, left, right, or down.When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits.The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions.Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit.
|
The first line of input will contain two integers n and m (2 β€ n, m β€ 50), denoting the dimensions of the maze.The next n lines will contain exactly m characters each, denoting the maze.Each character of the maze will be '.', '#', 'S', or 'E'.There will be exactly one 'S' and exactly one 'E' in the maze.The last line will contain a single string s (1 β€ |s| β€ 100) β the instructions given to the robot. Each character of s is a digit from 0 to 3.
|
Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit.
|
For the first sample, the only valid mapping is , where D is down, L is left, U is up, R is right.
|
Input: 5 6.....#S....#.#.....#.......E..333300012 | Output: 1
|
Easy
| 2 | 1,405 | 448 | 108 | 9 |
1,176 |
E
|
1176E
|
E. Cover it!
| 1,700 |
dfs and similar; dsu; graphs; shortest paths; trees
|
You are given an undirected unweighted connected graph consisting of \(n\) vertices and \(m\) edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.Your task is to choose at most \(\lfloor\frac{n}{2}\rfloor\) vertices in this graph so each unchosen vertex is adjacent (in other words, connected by an edge) to at least one of chosen vertices.It is guaranteed that the answer exists. If there are multiple answers, you can print any.You will be given multiple independent queries to answer.
|
The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β the number of queries.Then \(t\) queries follow.The first line of each query contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(n - 1 \le m \le min(2 \cdot 10^5, \frac{n(n-1)}{2})\)) β the number of vertices and the number of edges, respectively.The following \(m\) lines denote edges: edge \(i\) is represented by a pair of integers \(v_i\), \(u_i\) (\(1 \le v_i, u_i \le n\), \(u_i \ne v_i\)), which are the indices of vertices connected by the edge.There are no self-loops or multiple edges in the given graph, i. e. for each pair (\(v_i, u_i\)) there are no other pairs (\(v_i, u_i\)) or (\(u_i, v_i\)) in the list of edges, and for each pair (\(v_i, u_i\)) the condition \(v_i \ne u_i\) is satisfied. It is guaranteed that the given graph is connected.It is guaranteed that \(\sum m \le 2 \cdot 10^5\) over all queries.
|
For each query print two lines.In the first line print \(k\) (\(1 \le \lfloor\frac{n}{2}\rfloor\)) β the number of chosen vertices.In the second line print \(k\) distinct integers \(c_1, c_2, \dots, c_k\) in any order, where \(c_i\) is the index of the \(i\)-th chosen vertex.It is guaranteed that the answer exists. If there are multiple answers, you can print any.
|
In the first query any vertex or any pair of vertices will suffice. Note that you don't have to minimize the number of chosen vertices. In the second query two vertices can be enough (vertices \(2\) and \(4\)) but three is also ok.
|
Input: 2 4 6 1 2 1 3 1 4 2 3 2 4 3 4 6 8 2 5 5 4 4 3 4 1 1 3 2 3 2 6 5 6 | Output: 2 1 3 3 4 3 6
|
Medium
| 5 | 525 | 924 | 366 | 11 |
274 |
C
|
274C
|
C. The Last Hole!
| 2,600 |
brute force; geometry
|
Luyi has n circles on the plane. The i-th circle is centered at (xi, yi). At the time zero circles start to grow simultaneously. In other words, the radius of each circle at time t (t > 0) is equal to t. The circles are drawn as black discs on an infinite white plane. So at each moment the plane consists of several black and white regions. Note that the circles may overlap while growing. We define a hole as a closed, connected white region. For instance, the figure contains two holes shown by red border. During growing some holes may be created and it is easy to see that each created hole will disappear eventually. Luyi asks you to find moment of time such that the last hole disappears. In other words, you should find the first moment such that no hole can be seen after that.
|
The first line of the input contains integer n (1 β€ n β€ 100). Each of the next n lines contains two integers xi and yi ( - 104 β€ xi, yi β€ 104), indicating the location of i-th circle.It's guaranteed that no two circles are centered at the same point.
|
Print the moment where the last hole disappears. If there exists no moment in which we can find holes print -1.The answer will be considered correct if the absolute or relative error does not exceed 10 - 4.
|
Input: 30 01 12 2 | Output: -1
|
Expert
| 2 | 786 | 250 | 206 | 2 |
|
690 |
F2
|
690F2
|
F2. Tree of Life (medium)
| 2,700 |
constructive algorithms; hashing; trees
|
Heidi got tired of deciphering the prophecy hidden in the Tree of Life and decided to go back to her headquarters, rest a little and try there. Of course, she cannot uproot the Tree and take it with her, so she made a drawing of the Tree on a piece of paper. On second thought, she made more identical drawings so as to have n in total (where n is the number of vertices of the Tree of Life) β who knows what might happen?Indeed, on her way back Heidi was ambushed by a group of zombies. While she managed to fend them off, they have damaged her drawings in a peculiar way: from the i-th copy, the vertex numbered i was removed, along with all adjacent edges. In each picture, the zombies have also erased all the vertex numbers and relabeled the remaining n - 1 vertices arbitrarily using numbers 1 to n (fortunately, each vertex still has a distinct number). What's more, the drawings have been arbitrarily shuffled/reordered.Now Heidi wants to recover the Tree of Life from her descriptions of all the drawings (as lists of edges).
|
The first line of the input contains Z β€ 20 β the number of test cases. Z descriptions of single test cases follow.In each test case, the first line of input contains numbers n (2 β€ n β€ 100) and k (where k is the number of drawings; we have k = n). In the following lines, the descriptions of the k drawings are given. The description of the i-th drawing is a line containing mi β the number of edges in this drawing, followed by mi lines describing edges, each of which contains two space-separated integers β- the numbers of the two vertices connected by the edge.
|
If Heidi's drawings cannot possibly come from a single tree, you should output the word NO. Otherwise, output one line containing the word YES and n - 1 lines describing any tree that Heidi's drawings could have come from. For every edge you should output the numbers of the vertices that it connects, separated with a single space. If there are many solutions, print any of them.
|
Input: 15 524 12 113 134 14 32 133 13 24 132 13 24 2 | Output: YES2 54 23 25 1
|
Master
| 3 | 1,034 | 566 | 380 | 6 |
|
1,357 |
A6
|
1357A6
|
A6. Distinguish four Pauli gates
| 0 |
*special
|
You are given an operation that implements a single-qubit unitary transformation: either the identity (I gate) or one of the Pauli gates (X, Y or Z gate). The operation will have Adjoint and Controlled variants defined.Your task is to perform necessary operations and measurements to figure out which unitary it was and to return 0 if it was the I gate, 1 if it was the X gate, 2 if it was the Y gate, 3 if it was the Z gate. You are allowed to apply the given operation and its adjoint/controlled variants exactly once.You have to implement an operation which takes a single-qubit operation as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Intrinsic; operation Solve (unitary : (Qubit => Unit is Adj+Ctl)) : Int { // your code here }}
|
Beginner
| 1 | 812 | 0 | 0 | 13 |
||||
1,610 |
D
|
1610D
|
D. Not Quite Lee
| 2,000 |
combinatorics; dp; math; number theory
|
Lee couldn't sleep lately, because he had nightmares. In one of his nightmares (which was about an unbalanced global round), he decided to fight back and propose a problem below (which you should solve) to balance the round, hopefully setting him free from the nightmares.A non-empty array \(b_1, b_2, \ldots, b_m\) is called good, if there exist \(m\) integer sequences which satisfy the following properties: The \(i\)-th sequence consists of \(b_i\) consecutive integers (for example if \(b_i = 3\) then the \(i\)-th sequence can be \((-1, 0, 1)\) or \((-5, -4, -3)\) but not \((0, -1, 1)\) or \((1, 2, 3, 4)\)). Assuming the sum of integers in the \(i\)-th sequence is \(sum_i\), we want \(sum_1 + sum_2 + \ldots + sum_m\) to be equal to \(0\). You are given an array \(a_1, a_2, \ldots, a_n\). It has \(2^n - 1\) nonempty subsequences. Find how many of them are good.As this number can be very large, output it modulo \(10^9 + 7\).An array \(c\) is a subsequence of an array \(d\) if \(c\) can be obtained from \(d\) by deletion of several (possibly, zero or all) elements.
|
The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the size of array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β elements of the array.
|
Print a single integer β the number of nonempty good subsequences of \(a\), modulo \(10^9 + 7\).
|
For the first test, two examples of good subsequences are \([2, 7]\) and \([2, 2, 4, 7]\):For \(b = [2, 7]\) we can use \((-3, -4)\) as the first sequence and \((-2, -1, \ldots, 4)\) as the second. Note that subsequence \([2, 7]\) appears twice in \([2, 2, 4, 7]\), so we have to count it twice. Green circles denote \((-3, -4)\) and orange squares denote \((-2, -1, \ldots, 4)\). For \(b = [2, 2, 4, 7]\) the following sequences would satisfy the properties: \((-1, 0)\), \((-3, -2)\), \((0, 1, 2, 3)\) and \((-3, -2, \ldots, 3)\)
|
Input: 4 2 2 4 7 | Output: 10
|
Hard
| 4 | 1,078 | 219 | 96 | 16 |
545 |
D
|
545D
|
D. Queue
| 1,300 |
greedy; implementation; sortings
|
Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed. Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.
|
The first line contains integer n (1 β€ n β€ 105).The next line contains n integers ti (1 β€ ti β€ 109), separated by spaces.
|
Print a single number β the maximum number of not disappointed people in the queue.
|
Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.
|
Input: 515 2 1 5 3 | Output: 4
|
Easy
| 3 | 629 | 121 | 83 | 5 |
39 |
C
|
39C
|
C. Moon Craters
| 2,100 |
dp; sortings
|
There are lots of theories concerning the origin of moon craters. Most scientists stick to the meteorite theory, which says that the craters were formed as a result of celestial bodies colliding with the Moon. The other version is that the craters were parts of volcanoes.An extraterrestrial intelligence research specialist professor Okulov (the namesake of the Okulov, the author of famous textbooks on programming) put forward an alternate hypothesis. Guess what kind of a hypothesis it was ββ sure, the one including extraterrestrial mind involvement. Now the professor is looking for proofs of his hypothesis.Professor has data from the moon robot that moves linearly in one direction along the Moon surface. The moon craters are circular in form with integer-valued radii. The moon robot records only the craters whose centers lay on his path and sends to the Earth the information on the distance from the centers of the craters to the initial point of its path and on the radii of the craters.According to the theory of professor Okulov two craters made by an extraterrestrial intelligence for the aims yet unknown either are fully enclosed one in the other or do not intersect at all. Internal or external tangency is acceptable. However the experimental data from the moon robot do not confirm this theory! Nevertheless, professor Okulov is hopeful. He perfectly understands that to create any logical theory one has to ignore some data that are wrong due to faulty measuring (or skillful disguise by the extraterrestrial intelligence that will be sooner or later found by professor Okulov!) Thatβs why Okulov wants to choose among the available crater descriptions the largest set that would satisfy his theory.
|
The first line has an integer n (1 β€ n β€ 2000) β the number of discovered craters. The next n lines contain crater descriptions in the ""ci ri"" format, where ci is the coordinate of the center of the crater on the moon robotβs path, ri is the radius of the crater. All the numbers ci and ri are positive integers not exceeding 109. No two craters coincide.
|
In the first line output the number of craters in the required largest set. In the next line output space-separated numbers of craters that this set consists of. The craters are numbered from 1 to n in the order in which they were given in the input data. The numbers may be output in any order. If the result is not unique, output any.
|
Input: 41 12 24 15 1 | Output: 31 2 4
|
Hard
| 2 | 1,722 | 357 | 336 | 0 |
|
1,392 |
A
|
1392A
|
A. Omkar and Password
| 800 |
greedy; math
|
Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!A password is an array \(a\) of \(n\) positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index \(i\) such that \(1 \leq i < n\) and \(a_{i} \neq a_{i+1}\), delete both \(a_i\) and \(a_{i+1}\) from the array and put \(a_{i}+a_{i+1}\) in their place. For example, for array \([7, 4, 3, 7]\) you can choose \(i = 2\) and the array will become \([7, 4+3, 7] = [7, 7, 7]\). Note that in this array you can't apply this operation anymore.Notice that one operation will decrease the size of the password by \(1\). What is the shortest possible length of the password after some number (possibly \(0\)) of operations?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of the password.The second line of each test case contains \(n\) integers \(a_{1},a_{2},\dots,a_{n}\) (\(1 \leq a_{i} \leq 10^9\)) β the initial contents of your password.The sum of \(n\) over all test cases will not exceed \(2 \cdot 10^5\).
|
For each password, print one integer: the shortest possible length of the password after some number of operations.
|
In the first test case, you can do the following to achieve a length of \(1\):Pick \(i=2\) to get \([2, 4, 1]\)Pick \(i=1\) to get \([6, 1]\)Pick \(i=1\) to get \([7]\)In the second test case, you can't perform any operations because there is no valid \(i\) that satisfies the requirements mentioned above.
|
Input: 2 4 2 1 3 1 2 420 420 | Output: 1 2
|
Beginner
| 2 | 882 | 502 | 115 | 13 |
1,157 |
F
|
1157F
|
F. Maximum Balanced Circle
| 2,000 |
constructive algorithms; dp; greedy; two pointers
|
There are \(n\) people in a row. The height of the \(i\)-th person is \(a_i\). You can choose any subset of these people and try to arrange them into a balanced circle.A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than \(1\). For example, let heights of chosen people be \([a_{i_1}, a_{i_2}, \dots, a_{i_k}]\), where \(k\) is the number of people you choose. Then the condition \(|a_{i_j} - a_{i_{j + 1}}| \le 1\) should be satisfied for all \(j\) from \(1\) to \(k-1\) and the condition \(|a_{i_1} - a_{i_k}| \le 1\) should be also satisfied. \(|x|\) means the absolute value of \(x\). It is obvious that the circle consisting of one person is balanced.Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of people.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the height of the \(i\)-th person.
|
In the first line of the output print \(k\) β the number of people in the maximum balanced circle.In the second line print \(k\) integers \(res_1, res_2, \dots, res_k\), where \(res_j\) is the height of the \(j\)-th person in the maximum balanced circle. The condition \(|res_{j} - res_{j + 1}| \le 1\) should be satisfied for all \(j\) from \(1\) to \(k-1\) and the condition \(|res_{1} - res_{k}| \le 1\) should be also satisfied.
|
Input: 7 4 3 5 1 2 2 1 | Output: 5 2 1 1 2 3
|
Hard
| 4 | 935 | 272 | 432 | 11 |
|
2,077 |
G
|
2077G
|
G. RGB Walking
| 3,500 |
bitmasks; chinese remainder theorem; dfs and similar; graphs; number theory
|
Red and Blue and Green - fn and Silentroomβ You are given a connected graph with \(n\) vertices and \(m\) bidirectional edges with weight not exceeding \(x\). The \(i\)-th edge connects vertices \(u_i\) and \(v_i\), has weight \(w_i\), and is assigned a color \(c_i\) (\(1 \leq i \leq m\), \(1 \leq u_i, v_i \leq n\)). The color \(c_i\) is either red, green, or blue. It is guaranteed that there is at least one edge of each color.For a walk where vertices and edges may be repeated, let \(s_r, s_g, s_b\) denote the sum of the weights of the red, green, and blue edges that the walk passes through, respectively. If an edge is traversed multiple times, each traversal is counted separately.Find the minimum value of \(\max(s_r, s_g, s_b) - \min(s_r, s_g, s_b)\) over all possible walks from vertex \(1\) to vertex \(n\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains three integers \(n\), \(m\), and \(x\) (\(4 \leq n \leq 2 \cdot 10^5\), \(n-1 \leq m \leq 2 \cdot 10^5\), \(1 \leq x \leq 2 \cdot 10^5\)) β the number of vertices, the number of edges in the graph, and the upper bound on the weight of the edges, respectively.The next \(m\) lines each contain three integers \(u_i, v_i, w_i\) and a letter \(c_i\) (\(1 \leq u_i, v_i \leq n\), \(1 \leq w_i \leq x\)), representing a bidirectional edge between vertices \(u_i\) and \(v_i\) with weight \(w_i\) and color \(c_i\). The color \(c_i\) is either 'r', 'g', or 'b', denoting red, green, and blue, respectively.It is guaranteed that the graph is connected and contains at least one edge of each color. The graph may also contain multiple edges and self-loops.Additionally, it is guaranteed that the total sum of all values of \(n\), the total sum of all values of \(m\), and the total sum of all values of \(x\) across all test cases do not exceed \(2 \cdot 10^5\) individually.
|
For each test case, output a single integer β the minimum value of \(\max(s_r, s_g, s_b) - \min(s_r, s_g, s_b)\) over all walks from vertex \(1\) to vertex \(n\).
|
In the first test case, the optimal path is \(1 \to 2 \to 3 \to 4\). The edges used are: \(1 \to 2\) (red, weight \(2\)) \(2 \to 3\) (green, weight \(3\)) \(3 \to 4\) (blue, weight \(2\)) We have \(s_r = 2\), \(s_g = 3\), and \(s_b = 2\). Thus, the answer is \(1\).In the second test case, one of the optimal paths is \(1 \to 1 \to 2 \to 1 \to 2 \to 3 \to 4\). The edges used are: \(1 \to 1\) (red, weight \(1\)) \(1 \to 2\) (red, weight \(1\)) \(2 \to 1\) (red, weight \(1\)) \(1 \to 2\) (red, weight \(1\)) \(2 \to 3\) (green, weight \(4\)) \(3 \to 4\) (blue, weight \(4\)) We have \(s_r = s_g = s_b = 4\). Thus, the answer is \(0\).
|
Input: 34 3 31 2 2 r2 3 3 g3 4 2 b4 5 41 2 1 r1 1 1 r2 1 1 r2 3 4 g3 4 4 b4 6 41 2 2 r1 2 2 r2 3 3 b1 3 4 r1 4 1 g3 4 4 g | Output: 1 0 0
|
Master
| 5 | 820 | 1,171 | 162 | 20 |
1,601 |
D
|
1601D
|
D. Difficult Mountain
| 2,700 |
data structures; dp; greedy; sortings
|
A group of \(n\) alpinists has just reached the foot of the mountain. The initial difficulty of climbing this mountain can be described as an integer \(d\).Each alpinist can be described by two integers \(s\) and \(a\), where \(s\) is his skill of climbing mountains and \(a\) is his neatness.An alpinist of skill level \(s\) is able to climb a mountain of difficulty \(p\) only if \(p \leq s\). As an alpinist climbs a mountain, they affect the path and thus may change mountain difficulty. Specifically, if an alpinist of neatness \(a\) climbs a mountain of difficulty \(p\) the difficulty of this mountain becomes \(\max(p, a)\). Alpinists will climb the mountain one by one. And before the start, they wonder, what is the maximum number of alpinists who will be able to climb the mountain if they choose the right order. As you are the only person in the group who does programming, you are to answer the question.Note that after the order is chosen, each alpinist who can climb the mountain, must climb the mountain at that time.
|
The first line contains two integers \(n\) and \(d\) (\(1 \leq n \leq 500\,000\); \(0 \leq d \leq 10^9\)) β the number of alpinists and the initial difficulty of the mountain.Each of the next \(n\) lines contains two integers \(s_i\) and \(a_i\) (\(0 \leq s_i, a_i \leq 10^9\)) that define the skill of climbing and the neatness of the \(i\)-th alpinist.
|
Print one integer equal to the maximum number of alpinists who can climb the mountain if they choose the right order to do so.
|
In the first example, alpinists \(2\) and \(3\) can climb the mountain if they go in this order. There is no other way to achieve the answer of \(2\).In the second example, alpinist \(1\) is not able to climb because of the initial difficulty of the mountain, while alpinists \(2\) and \(3\) can go up in any order.In the third example, the mountain can be climbed by alpinists \(5\), \(3\) and \(4\) in this particular order. There is no other way to achieve optimal answer.
|
Input: 3 2 2 6 3 5 5 7 | Output: 2
|
Master
| 4 | 1,034 | 354 | 126 | 16 |
1,949 |
B
|
1949B
|
B. Charming Meals
| 1,500 |
binary search; brute force; greedy; sortings
|
The Czech cuisine features \(n\) appetizers and \(n\) main dishes. The \(i\)-th appetizer has spiciness \(a_i\), and the \(i\)-th main dish has spiciness \(b_i\).A typical Czech meal consists of exactly one appetizer and one main dish. You want to pair up the \(n\) appetizers and \(n\) main dishes into \(n\) meals with each appetizer and each main dish being included in exactly one meal.Your meals shall surprise the diners, so you want the spiciness levels of the two parts of the same meal to be as different as possible. The charm of a meal is the difference (in absolute value) between the spiciness of the appetizer and the spiciness of the main dish. So, a meal consisting of an appetizer with spiciness \(x\) and a main dish with spiciness \(y\) has charm equal to \(|x-y|\).You want to maximize the minimum charm of the resulting \(n\) meals. What is the largest possible value of the minimum charm that you can achieve?
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 1\,000\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5\,000\)) βthe number of appetizers and main dishes.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^{9}\)) β the spicinesses of the \(n\) appetizers.The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(0 \leq b_i \leq 10^{9}\)) β the spicinesses of the \(n\) main dishes.It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(25\cdot 10^6\).
|
For each test case, print the largest possible value of the minimum charm you can achieve.
|
In the first test case, no matter how you pair up the appetizers with the main dishes, each meal will have an appetizer with spiciness \(0\) and a main dish with spiciness \(1000000000\), so the charm of each meal will be \(1000000000\).In the second test case, one optimal way to pair up appetizers and main dishes is: \((1, 5)\), \((2, 4)\), \((3, 1)\), \((4, 2)\), \((5, 3)\). The corresponding meals have charms: \(4\), \(2\), \(2\), \(2\), \(2\). The resulting minimum charm is \(2\).In the third test case, one way to maximize the minimum charm is to pair up the three appetizers with spiciness \(0\) with the three main dishes with spiciness \(100\), and the three appetizers with spiciness \(100\) with the three main dishes with spiciness \(0\). Doing so, the charm of each meal will be exactly \(100\).
|
Input: 430 0 01000000000 1000000000 100000000051 2 3 4 51 2 3 4 560 0 0 100 100 100100 100 100 0 0 0714 25 62 74 86 95 1251 62 71 72 92 20 84 | Output: 1000000000 2 100 30
|
Medium
| 4 | 931 | 718 | 90 | 19 |
1,856 |
E2
|
1856E2
|
E2. PermuTree (hard version)
| 2,700 |
bitmasks; dfs and similar; dp; fft; greedy; implementation; math; trees
|
This is the hard version of the problem. The differences between the two versions are the constraint on \(n\) and the time limit. You can make hacks only if both versions of the problem are solved.You are given a tree with \(n\) vertices rooted at vertex \(1\).For some permutation\(^\dagger\) \(a\) of length \(n\), let \(f(a)\) be the number of pairs of vertices \((u, v)\) such that \(a_u < a_{\operatorname{lca}(u, v)} < a_v\). Here, \(\operatorname{lca}(u,v)\) denotes the lowest common ancestor of vertices \(u\) and \(v\).Find the maximum possible value of \(f(a)\) over all permutations \(a\) of length \(n\).\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
The first line contains a single integer \(n\) (\(2 \le n \le 10^6\)).The second line contains \(n - 1\) integers \(p_2,p_3,\ldots,p_n\) (\(1 \le p_i < i\)) indicating that there is an edge between vertices \(i\) and \(p_i\).
|
Output the maximum value of \(f(a)\).
|
The tree in the first test: One possible optimal permutation \(a\) is \([2, 1, 4, 5, 3]\) with \(4\) suitable pairs of vertices: \((2, 3)\), since \(\operatorname{lca}(2, 3) = 1\) and \(1 < 2 < 4\), \((2, 4)\), since \(\operatorname{lca}(2, 4) = 1\) and \(1 < 2 < 5\), \((2, 5)\), since \(\operatorname{lca}(2, 5) = 1\) and \(1 < 2 < 3\), \((5, 4)\), since \(\operatorname{lca}(5, 4) = 3\) and \(3 < 4 < 5\). The tree in the third test: The tree in the fourth test:
|
Input: 5 1 1 3 3 | Output: 4
|
Master
| 8 | 954 | 225 | 37 | 18 |
667 |
A
|
667A
|
A. Pouring Rain
| 1,100 |
geometry; math
|
A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition β when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do. Today everything had changed quietly. You went on the street with a cup contained water, your favorite drink. In a moment when you were drinking a water you noticed that the process became quite long: the cup still contained water because of rain. You decided to make a formal model of what was happening and to find if it was possible to drink all water in that situation. Thus, your cup is a cylinder with diameter equals d centimeters. Initial level of water in cup equals h centimeters from the bottom. You drink a water with a speed equals v milliliters per second. But rain goes with such speed that if you do not drink a water from the cup, the level of water increases on e centimeters per second. The process of drinking water from the cup and the addition of rain to the cup goes evenly and continuously. Find the time needed to make the cup empty or find that it will never happen. It is guaranteed that if it is possible to drink all water, it will happen not later than after 104 seconds.Note one milliliter equals to one cubic centimeter.
|
The only line of the input contains four integer numbers d, h, v, e (1 β€ d, h, v, e β€ 104), where: d β the diameter of your cylindrical cup, h β the initial level of water in the cup, v β the speed of drinking process from the cup in milliliters per second, e β the growth of water because of rain if you do not drink from the cup.
|
If it is impossible to make the cup empty, print ""NO"" (without quotes).Otherwise print ""YES"" (without quotes) in the first line. In the second line print a real number β time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4. It is guaranteed that if the answer exists, it doesn't exceed 104.
|
In the first example the water fills the cup faster than you can drink from it.In the second example area of the cup's bottom equals to , thus we can conclude that you decrease the level of water by centimeters per second. At the same time water level increases by 1 centimeter per second due to rain. Thus, cup will be empty in seconds.
|
Input: 1 2 3 100 | Output: NO
|
Easy
| 2 | 1,353 | 331 | 381 | 6 |
342 |
C
|
342C
|
C. Cupboard and Balloons
| 1,900 |
geometry
|
A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is made of a semicircle with radius r (the cupboard's top) and two walls of height h (the cupboard's sides). The cupboard's depth is r, that is, it looks like a rectangle with base r and height h + r from the sides. The figure below shows what the cupboard looks like (the front view is on the left, the side view is on the right). Xenia got lots of balloons for her birthday. The girl hates the mess, so she wants to store the balloons in the cupboard. Luckily, each balloon is a sphere with radius . Help Xenia calculate the maximum number of balloons she can put in her cupboard. You can say that a balloon is in the cupboard if you can't see any part of the balloon on the left or right view. The balloons in the cupboard can touch each other. It is not allowed to squeeze the balloons or deform them in any way. You can assume that the cupboard's walls are negligibly thin.
|
The single line contains two integers r, h (1 β€ r, h β€ 107).
|
Print a single integer β the maximum number of balloons Xenia can put in the cupboard.
|
Input: 1 1 | Output: 3
|
Hard
| 1 | 955 | 60 | 86 | 3 |
|
1,814 |
C
|
1814C
|
C. Search in Parallel
| 1,500 |
constructive algorithms; greedy; sortings
|
Suppose you have \(n\) boxes. The \(i\)-th box contains infinitely many balls of color \(i\). Sometimes you need to get a ball with some specific color; but you're too lazy to do it yourself.You have bought two robots to retrieve the balls for you. Now you have to program them. In order to program the robots, you have to construct two lists \([a_1, a_2, \dots, a_k]\) and \([b_1, b_2, \dots, b_{n-k}]\), where the list \(a\) represents the boxes assigned to the first robot, and the list \(b\) represents the boxes assigned to the second robot. Every integer from \(1\) to \(n\) must be present in exactly one of these lists.When you request a ball with color \(x\), the robots work as follows. Each robot looks through the boxes that were assigned to that robot, in the order they appear in the list. The first robot spends \(s_1\) seconds analyzing the contents of a box; the second robot spends \(s_2\). As soon as one of the robots finds the box with balls of color \(x\) (and analyzes its contents), the search ends. The search time is the number of seconds from the beginning of the search until one of the robots finishes analyzing the contents of the \(x\)-th box. If a robot analyzes the contents of all boxes assigned to it, it stops searching.For example, suppose \(s_1 = 2\), \(s_2 = 3\), \(a = [4, 1, 5, 3, 7]\), \(b = [2, 6]\). If you request a ball with color \(3\), the following happens: initially, the first robot starts analyzing the box \(4\), and the second robot starts analyzing the box \(2\); at the end of the \(2\)-nd second, the first robot finishes analyzing the box \(4\). It is not the box you need, so the robot continues with the box \(1\); at the end of the \(3\)-rd second, the second robot finishes analyzing the box \(2\). It is not the box you need, so the robot continues with the box \(6\); at the end of the \(4\)-th second, the first robot finishes analyzing the box \(1\). It is not the box you need, so the robot continues with the box \(5\); at the end of the \(6\)-th second, the first robot finishes analyzing the box \(5\). It is not the box you need, so the robot continues with the box \(3\). At the same time, the second robot finishes analyzing the box \(6\). It is not the box you need, and the second robot has analyzed all the boxes in its list, so that robot stops searching; at the end of the \(8\)-th second, the first robot finishes analyzing the box \(3\). It is the box you need, so the search ends; so, the search time is \(8\) seconds. You know that you are going to request a ball of color \(1\) \(r_1\) times, a ball of color \(2\) \(r_2\) times, and so on. You want to construct the lists \(a\) and \(b\) for the robots in such a way that the total search time over all requests is the minimum possible.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines: the first line contains three integers \(n\), \(s_1\), \(s_2\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le s_1, s_2 \le 10\)); the second line contains \(n\) integers \(r_1, r_2, \dots, r_n\) (\(1 \le r_i \le 10^6\)). 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 two lines. The first line should contain the list \(a\), the second line β the list \(b\). Each list has to be printed as follows: first, print the number of elements in it, and then the elements themselves.If there are multiple answers, you may print any of them.
|
Input: 37 3 18 6 4 4 4 1 75 1 101 1 1 1 18 1 14 5 6 8 1 7 3 2 | Output: 2 5 6 5 1 7 2 4 3 5 4 3 5 2 1 0 4 4 2 7 5 4 6 3 1 8
|
Medium
| 3 | 2,770 | 449 | 290 | 18 |
|
1,066 |
B
|
1066B
|
B. Heaters
| 1,500 |
greedy; two pointers
|
Vova's house is an array consisting of \(n\) elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The \(i\)-th element of the array is \(1\) if there is a heater in the position \(i\), otherwise the \(i\)-th element of the array is \(0\).Each heater has a value \(r\) (\(r\) is the same for all heaters). This value means that the heater at the position \(pos\) can warm up all the elements in range \([pos - r + 1; pos + r - 1]\).Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater. Vova's target is to warm up the whole house (all the elements of the array), i.e. if \(n = 6\), \(r = 2\) and heaters are at positions \(2\) and \(5\), then Vova can warm up the whole house if he switches all the heaters in the house on (then the first \(3\) elements will be warmed up by the first heater and the last \(3\) elements will be warmed up by the second heater).Initially, all the heaters are off.But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.Your task is to find this number of heaters or say that it is impossible to warm up the whole house.
|
The first line of the input contains two integers \(n\) and \(r\) (\(1 \le n, r \le 1000\)) β the number of elements in the array and the value of heaters.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 1\)) β the Vova's house description.
|
Print one integer β the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.
|
In the first example the heater at the position \(2\) warms up elements \([1; 3]\), the heater at the position \(3\) warms up elements \([2, 4]\) and the heater at the position \(6\) warms up elements \([5; 6]\) so the answer is \(3\).In the second example the heater at the position \(1\) warms up elements \([1; 3]\) and the heater at the position \(5\) warms up elements \([3; 5]\) so the answer is \(2\).In the third example there are no heaters so the answer is -1.In the fourth example the heater at the position \(3\) warms up elements \([1; 5]\), the heater at the position \(6\) warms up elements \([4; 8]\) and the heater at the position \(10\) warms up elements \([8; 10]\) so the answer is \(3\).
|
Input: 6 20 1 1 0 0 1 | Output: 3
|
Medium
| 2 | 1,462 | 273 | 119 | 10 |
750 |
E
|
750E
|
E. New Year and Old Subsequence
| 2,600 |
data structures; divide and conquer; dp; matrices
|
A string t is called nice if a string ""2017"" occurs in t as a subsequence but a string ""2016"" doesn't occur in t as a subsequence. For example, strings ""203434107"" and ""9220617"" are nice, while strings ""20016"", ""1234"" and ""20167"" aren't nice.The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it's impossible to make a string nice by removing characters, its ugliness is - 1.Limak has a string s of length n, with characters indexed 1 through n. He asks you q queries. In the i-th query you should compute and print the ugliness of a substring (continuous subsequence) of s starting at the index ai and ending at the index bi (inclusive).
|
The first line of the input contains two integers n and q (4 β€ n β€ 200 000, 1 β€ q β€ 200 000) β the length of the string s and the number of queries respectively.The second line contains a string s of length n. Every character is one of digits '0'β'9'.The i-th of next q lines contains two integers ai and bi (1 β€ ai β€ bi β€ n), describing a substring in the i-th query.
|
For each query print the ugliness of the given substring.
|
In the first sample: In the first query, ugliness(""20166766"") = 4 because all four sixes must be removed. In the second query, ugliness(""2016676"") = 3 because all three sixes must be removed. In the third query, ugliness(""0166766"") = - 1 because it's impossible to remove some digits to get a nice string. In the second sample: In the second query, ugliness(""01201666209167"") = 2. It's optimal to remove the first digit '2' and the last digit '6', what gives a string ""010166620917"", which is nice. In the third query, ugliness(""016662091670"") = 1. It's optimal to remove the last digit '6', what gives a nice string ""01666209170"".
|
Input: 8 3201667661 81 72 8 | Output: 43-1
|
Expert
| 4 | 719 | 368 | 57 | 7 |
1,286 |
C2
|
1286C2
|
C2. Madhouse (Hard version)
| 2,800 |
brute force; constructive algorithms; hashing; interactive; math
|
This problem is different with easy version only by constraints on total answers lengthIt is an interactive problemVenya joined a tour to the madhouse, in which orderlies play with patients the following game. Orderlies pick a string \(s\) of length \(n\), consisting only of lowercase English letters. The player can ask two types of queries: ? l r β ask to list all substrings of \(s[l..r]\). Substrings will be returned in random order, and in every substring, all characters will be randomly shuffled. ! s β guess the string picked by the orderlies. This query can be asked exactly once, after that the game will finish. If the string is guessed correctly, the player wins, otherwise he loses. The player can ask no more than \(3\) queries of the first type.To make it easier for the orderlies, there is an additional limitation: the total number of returned substrings in all queries of the first type must not exceed \(\left\lceil 0.777(n+1)^2 \right\rceil\) (\(\lceil x \rceil\) is \(x\) rounded up).Venya asked you to write a program, which will guess the string by interacting with the orderlies' program and acting by the game's rules.Your program should immediately terminate after guessing the string using a query of the second type. In case your program guessed the string incorrectly, or it violated the game rules, it will receive verdict Wrong answer.Note that in every test case the string is fixed beforehand and will not change during the game, which means that the interactor is not adaptive.
|
First line contains number \(n\) (\(1 \le n \le 100\)) β the length of the picked string.
|
Input: 4 a aa a cb b c c | Output: ? 1 2 ? 3 4 ? 4 4 ! aabc
|
Master
| 5 | 1,513 | 89 | 0 | 12 |
||
1,420 |
E
|
1420E
|
E. Battle Lemmings
| 2,500 |
dp; greedy
|
A lighthouse keeper Peter commands an army of \(n\) battle lemmings. He ordered his army to stand in a line and numbered the lemmings from \(1\) to \(n\) from left to right. Some of the lemmings hold shields. Each lemming cannot hold more than one shield.The more protected Peter's army is, the better. To calculate the protection of the army, he finds the number of protected pairs of lemmings, that is such pairs that both lemmings in the pair don't hold a shield, but there is a lemming with a shield between them.Now it's time to prepare for defence and increase the protection of the army. To do this, Peter can give orders. He chooses a lemming with a shield and gives him one of the two orders: give the shield to the left neighbor if it exists and doesn't have a shield; give the shield to the right neighbor if it exists and doesn't have a shield. In one second Peter can give exactly one order.It's not clear how much time Peter has before the defence. So he decided to determine the maximal value of army protection for each \(k\) from \(0\) to \(\frac{n(n-1)}2\), if he gives no more that \(k\) orders. Help Peter to calculate it!
|
First line contains a single integer \(n\) (\(1 \le n \le 80\)), the number of lemmings in Peter's army.Second line contains \(n\) integers \(a_i\) (\(0 \le a_i \le 1\)). If \(a_i = 1\), then the \(i\)-th lemming has a shield, otherwise \(a_i = 0\).
|
Print \(\frac{n(n-1)}2 + 1\) numbers, the greatest possible protection after no more than \(0, 1, \dots, \frac{n(n-1)}2\) orders.
|
Consider the first example.The protection is initially equal to zero, because for each pair of lemmings without shields there is no lemmings with shield.In one second Peter can order the first lemming give his shield to the right neighbor. In this case, the protection is two, as there are two protected pairs of lemmings, \((1, 3)\) and \((1, 4)\).In two seconds Peter can act in the following way. First, he orders the fifth lemming to give a shield to the left neighbor. Then, he orders the first lemming to give a shield to the right neighbor. In this case Peter has three protected pairs of lemmings β \((1, 3)\), \((1, 5)\) and \((3, 5)\).You can make sure that it's impossible to give orders in such a way that the protection becomes greater than three.
|
Input: 5 1 0 0 0 1 | Output: 0 2 3 3 3 3 3 3 3 3 3
|
Expert
| 2 | 1,142 | 249 | 129 | 14 |
1,905 |
C
|
1905C
|
C. Largest Subsequence
| 1,400 |
greedy; strings
|
Given is a string \(s\) of length \(n\). In one operation you can select the lexicographically largest\(^\dagger\) subsequence of string \(s\) and cyclic shift it to the right\(^\ddagger\). Your task is to calculate the minimum number of operations it would take for \(s\) to become sorted, or report that it never reaches a sorted state.\(^\dagger\)A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); In the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).\(^\ddagger\)By cyclic shifting the string \(t_1t_2\ldots t_m\) to the right, we get the string \(t_mt_1\ldots t_{m-1}\).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the string \(s\).The second line of each test case contains a single string \(s\) of length \(n\), consisting of lowercase English letters.It is guaranteed that sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of operations required to make \(s\) sorted, or \(-1\) if it's impossible.
|
In the first test case, the string \(s\) is already sorted, so we need no operations.In the second test case, doing one operation, we will select cb and cyclic shift it. The string \(s\) is now abc which is sorted.In the third test case, \(s\) cannot be sorted.In the fourth test case we will perform the following operations: The lexicographically largest subsequence is zca. Then \(s\) becomes abzc. The lexicographically largest subsequence is zc. Then \(s\) becomes abcz. The string becomes sorted.Thus, we need \(2\) operations.
|
Input: 65aaabc3acb3bac4zbca15czddeneeeemigec13cdefmopqsvxzz | Output: 0 1 -1 2 6 0
|
Easy
| 2 | 781 | 522 | 139 | 19 |
1,334 |
A
|
1334A
|
A. Level Statistics
| 1,200 |
implementation; math
|
Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level.All levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the number of plays increases by \(1\). If he manages to finish the level successfully then the number of clears increases by \(1\) as well. Note that both of the statistics update at the same time (so if the player finishes the level successfully then the number of plays will increase at the same time as the number of clears).Polycarp is very excited about his level, so he keeps peeking at the stats to know how hard his level turns out to be.So he peeked at the stats \(n\) times and wrote down \(n\) pairs of integers β \((p_1, c_1), (p_2, c_2), \dots, (p_n, c_n)\), where \(p_i\) is the number of plays at the \(i\)-th moment of time and \(c_i\) is the number of clears at the same moment of time. The stats are given in chronological order (i.e. the order of given pairs is exactly the same as Polycarp has written down).Between two consecutive moments of time Polycarp peeked at the stats many players (but possibly zero) could attempt the level.Finally, Polycarp wonders if he hasn't messed up any records and all the pairs are correct. If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then he considers his records correct.Help him to check the correctness of his records.For your convenience you have to answer multiple independent test cases.
|
The first line contains a single integer \(T\) \((1 \le T \le 500)\) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of moments of time Polycarp peeked at the stats.Each of the next \(n\) lines contains two integers \(p_i\) and \(c_i\) (\(0 \le p_i, c_i \le 1000\)) β the number of plays and the number of clears of the level at the \(i\)-th moment of time.Note that the stats are given in chronological order.
|
For each test case print a single line.If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then print ""YES"".Otherwise, print ""NO"".You can print each letter in any case (upper or lower).
|
In the first test case at the third moment of time the number of clears increased but the number of plays did not, that couldn't have happened.The second test case is a nice example of a Super Expert level.In the third test case the number of plays decreased, which is impossible.The fourth test case is probably an auto level with a single jump over the spike.In the fifth test case the number of clears decreased, which is also impossible.Nobody wanted to play the sixth test case; Polycarp's mom attempted it to make him feel better, however, she couldn't clear it.
|
Input: 6 3 0 0 1 1 1 2 2 1 0 1000 3 4 10 1 15 2 10 2 15 2 1 765 432 2 4 4 4 3 5 0 0 1 0 1 0 1 0 1 0 | Output: NO YES NO YES NO YES
|
Easy
| 2 | 1,626 | 489 | 268 | 13 |
1,965 |
A
|
1965A
|
A. Everything Nim
| 1,400 |
games; greedy; math; sortings
|
Alice and Bob are playing a game on \(n\) piles of stones. On each player's turn, they select a positive integer \(k\) that is at most the size of the smallest nonempty pile and remove \(k\) stones from each nonempty pile at once. The first player who is unable to make a move (because all piles are empty) loses.Given that Alice goes first, who will win the game if both players play optimally?
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2\cdot 10^5\)) β the number of piles in the game.The next line of each test case contains \(n\) integers \(a_1, a_2, \ldots a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the initial number of stones in the \(i\)-th pile.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 line with the name of the winner, assuming both players play optimally. If Alice wins, print ""Alice"", otherwise print ""Bob"" (without quotes).
|
In the first test case, Alice can win by choosing \(k=3\) on her first turn, which will empty all of the piles at once.In the second test case, Alice must choose \(k=1\) on her first turn since there is a pile of size \(1\), so Bob can win on the next turn by choosing \(k=6\).
|
Input: 753 3 3 3 321 771 3 9 7 4 2 10031 2 362 1 3 4 2 485 7 2 9 6 3 3 211000000000 | Output: Alice Bob Alice Alice Bob Alice Alice
|
Easy
| 4 | 395 | 547 | 180 | 19 |
464 |
E
|
464E
|
E. The Classic Problem
| 3,000 |
data structures; graphs; shortest paths
|
You are given a weighted undirected graph on n vertices and m edges. Find the shortest path from vertex s to vertex t or else state that such path doesn't exist.
|
The first line of the input contains two space-separated integers β n and m (1 β€ n β€ 105; 0 β€ m β€ 105).Next m lines contain the description of the graph edges. The i-th line contains three space-separated integers β ui, vi, xi (1 β€ ui, vi β€ n; 0 β€ xi β€ 105). That means that vertices with numbers ui and vi are connected by edge of length 2xi (2 to the power of xi).The last line contains two space-separated integers β the numbers of vertices s and t.The vertices are numbered from 1 to n. The graph contains no multiple edges and self-loops.
|
In the first line print the remainder after dividing the length of the shortest path by 1000000007 (109 + 7) if the path exists, and -1 if the path doesn't exist.If the path exists print in the second line integer k β the number of vertices in the shortest path from vertex s to vertex t; in the third line print k space-separated integers β the vertices of the shortest path in the visiting order. The first vertex should be vertex s, the last vertex should be vertex t. If there are multiple shortest paths, print any of them.
|
A path from vertex s to vertex t is a sequence v0, ..., vk, such that v0 = s, vk = t, and for any i from 0 to k - 1 vertices vi and vi + 1 are connected by an edge. The length of the path is the sum of weights of edges between vi and vi + 1 for all i from 0 to k - 1. The shortest path from s to t is the path which length is minimum among all possible paths from s to t.
|
Input: 4 41 4 21 2 02 3 03 4 01 4 | Output: 341 2 3 4
|
Master
| 3 | 161 | 543 | 528 | 4 |
901 |
C
|
901C
|
C. Bipartite Segments
| 2,300 |
binary search; data structures; dfs and similar; dsu; graphs; two pointers
|
You are given an undirected graph with n vertices. There are no edge-simple cycles with the even length in it. In other words, there are no cycles of even length that pass each edge at most once. Let's enumerate vertices from 1 to n. You have to answer q queries. Each query is described by a segment of vertices [l; r], and you have to count the number of its subsegments [x; y] (l β€ x β€ y β€ r), such that if we delete all vertices except the segment of vertices [x; y] (including x and y) and edges between them, the resulting graph is bipartite.
|
The first line contains two integers n and m (1 β€ n β€ 3Β·105, 1 β€ m β€ 3Β·105) β the number of vertices and the number of edges in the graph.The next m lines describe edges in the graph. The i-th of these lines contains two integers ai and bi (1 β€ ai, bi β€ n; ai β bi), denoting an edge between vertices ai and bi. It is guaranteed that this graph does not contain edge-simple cycles of even length.The next line contains a single integer q (1 β€ q β€ 3Β·105) β the number of queries.The next q lines contain queries. The i-th of these lines contains two integers li and ri (1 β€ li β€ ri β€ n) β the query parameters.
|
Print q numbers, each in new line: the i-th of them should be the number of subsegments [x; y] (li β€ x β€ y β€ ri), such that the graph that only includes vertices from segment [x; y] and edges between them is bipartite.
|
The first example is shown on the picture below:For the first query, all subsegments of [1; 3], except this segment itself, are suitable.For the first query, all subsegments of [4; 6], except this segment itself, are suitable.For the third query, all subsegments of [1; 6] are suitable, except [1; 3], [1; 4], [1; 5], [1; 6], [2; 6], [3; 6], [4; 6].The second example is shown on the picture below:
|
Input: 6 61 22 33 14 55 66 431 34 61 6 | Output: 5514
|
Expert
| 6 | 548 | 609 | 218 | 9 |
1,725 |
L
|
1725L
|
L. Lemper Cooking Competition
| 2,400 |
data structures
|
Pak Chanek is participating in a lemper cooking competition. In the competition, Pak Chanek has to cook lempers with \(N\) stoves that are arranged sequentially from stove \(1\) to stove \(N\). Initially, stove \(i\) has a temperature of \(A_i\) degrees. A stove can have a negative temperature.Pak Chanek realises that, in order for his lempers to be cooked, he needs to keep the temperature of each stove at a non-negative value. To make it happen, Pak Chanek can do zero or more operations. In one operation, Pak Chanek chooses one stove \(i\) with \(2 \leq i \leq N-1\), then: changes the temperature of stove \(i-1\) into \(A_{i-1} := A_{i-1} + A_{i}\), changes the temperature of stove \(i+1\) into \(A_{i+1} := A_{i+1} + A_{i}\), and changes the temperature of stove \(i\) into \(A_i := -A_i\). Pak Chanek wants to know the minimum number of operations he needs to do such that the temperatures of all stoves are at non-negative values. Help Pak Chanek by telling him the minimum number of operations needed or by reporting if it is not possible to do.
|
The first line contains a single integer \(N\) (\(1 \le N \le 10^5\)) β the number of stoves.The second line contains \(N\) integers \(A_1, A_2, \ldots, A_N\) (\(-10^9 \leq A_i \leq 10^9\)) β the initial temperatures of the stoves.
|
Output an integer representing the minimum number of operations needed to make the temperatures of all stoves at non-negative values or output \(-1\) if it is not possible.
|
For the first example, a sequence of operations that can be done is as follows: Pak Chanek does an operation to stove \(3\), \(A = [2, -2, 1, 4, 2, -2, 9]\). Pak Chanek does an operation to stove \(2\), \(A = [0, 2, -1, 4, 2, -2, 9]\). Pak Chanek does an operation to stove \(3\), \(A = [0, 1, 1, 3, 2, -2, 9]\). Pak Chanek does an operation to stove \(6\), \(A = [0, 1, 1, 3, 0, 2, 7]\). There is no other sequence of operations such that the number of operations needed is fewer than \(4\).
|
Input: 7 2 -1 -1 5 2 -2 9 | Output: 4
|
Expert
| 1 | 1,059 | 231 | 172 | 17 |
187 |
D
|
187D
|
D. BRT Contract
| 2,800 |
data structures
|
In the last war of PMP, he defeated all his opponents and advanced to the final round. But after the end of semi-final round evil attacked him from behind and killed him! God bless him. Before his death, PMP signed a contract with the bus rapid transit (BRT) that improves public transportations by optimizing time of travel estimation. You should help PMP finish his last contract.Each BRT line is straight line that passes n intersecting on its ways. At each intersection there is traffic light that periodically cycles between green and red. It starts illuminating green at time zero. During the green phase which lasts for g seconds, traffic is allowed to proceed. After the green phase the light changes to red and remains in this color for r seconds. During the red phase traffic is prohibited from proceeding. If a vehicle reaches the intersection exactly at a time when the light changes to red, it should stop, but the vehicle is clear to proceed if the light has just changed to green. All traffic lights have the same timing and are synchronized. In other words the period of red (and green) phase is the same for all of traffic lights and they all start illuminating green at time zero.The BRT Company has calculated the time that a bus requires to pass each road segment. A road segment is the distance between two consecutive traffic lights or between a traffic light and source (or destination) station. More precisely BRT specialists provide n + 1 positive integers li, the time in seconds that a bus needs to traverse i-th road segment in the path from source to destination. The l1 value denotes the time that a bus needs to pass the distance between source and the first intersection. The ln + 1 value denotes the time between the last intersection and destination.In one day q buses leave the source station. The i-th bus starts from source at time ti (in seconds). Decision makers of BRT Company want to know what time a bus gets to destination?The bus is considered as point. A bus will always move if it can. The buses do not interfere with each other.
|
The first line of input contains three space-separated positive integers n, g, r (1 β€ n β€ 105, 2 β€ g + r β€ 109) β the number of intersections, duration of green phase and duration of red phase. Next line contains n + 1 integers li (1 β€ li β€ 109) β the time to pass the i-th road segment in the path from source to destination. Next line contains a single integer q (1 β€ q β€ 105) β the number of buses in a day. The i-th of next q lines contains a single integer ti (1 β€ ti β€ 109) β the time when i-th bus leaves the source station.
|
In the i-th line of output you should print a single integer β the time that i-th bus gets to destination.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
|
In the first sample, buses #1, #2 and #5 will reach the destination without waiting behind the red light. But buses #3 and #4 should wait.In the second sample, first bus should wait at third, fourth and fifth intersections. Second and third buses should wait only at the fifth intersection.
|
Input: 1 3 25 2512345 | Output: 89121212
|
Master
| 1 | 2,075 | 531 | 248 | 1 |
1,575 |
K
|
1575K
|
K. Knitting Batik
| 2,200 |
implementation; math
|
Mr. Chanek wants to knit a batik, a traditional cloth from Indonesia. The cloth forms a grid \(a\) with size \(n \times m\). There are \(k\) colors, and each cell in the grid can be one of the \(k\) colors.Define a sub-rectangle as an ordered pair of two cells \(((x_1, y_1), (x_2, y_2))\), denoting the top-left cell and bottom-right cell (inclusively) of a sub-rectangle in \(a\). Two sub-rectangles \(((x_1, y_1), (x_2, y_2))\) and \(((x_3, y_3), (x_4, y_4))\) have the same pattern if and only if the following holds: they have the same width (\(x_2 - x_1 = x_4 - x_3\)); they have the same height (\(y_2 - y_1 = y_4 - y_3\)); for every pair \((i, j)\) where \(0 \leq i \leq x_2 - x_1\) and \(0 \leq j \leq y_2 - y_1\), the color of cells \((x_1 + i, y_1 + j)\) and \((x_3 + i, y_3 + j)\) are equal. Count the number of possible batik color combinations, such that the subrectangles \(((a_x, a_y),(a_x + r - 1, a_y + c - 1))\) and \(((b_x, b_y),(b_x + r - 1, b_y + c - 1))\) have the same pattern.Output the answer modulo \(10^9 + 7\).
|
The first line contains five integers \(n\), \(m\), \(k\), \(r\), and \(c\) (\(1 \leq n, m \leq 10^9\), \(1 \leq k \leq 10^9\), \(1 \leq r \leq \min(10^6, n)\), \(1 \leq c \leq \min(10^6, m)\)) β the size of the batik, the number of colors, and size of the sub-rectangle.The second line contains four integers \(a_x\), \(a_y\), \(b_x\), and \(b_y\) (\(1 \leq a_x, b_x \leq n\), \(1 \leq a_y, b_y \leq m\)) β the top-left corners of the first and second sub-rectangle. Both of the sub-rectangles given are inside the grid (\(1 \leq a_x + r - 1\), \(b_x + r - 1 \leq n\), \(1 \leq a_y + c - 1\), \(b_y + c - 1 \leq m\)).
|
Output an integer denoting the number of possible batik color combinations modulo \(10^9 + 7\).
|
The following are all \(32\) possible color combinations in the first example.
|
Input: 3 3 2 2 2 1 1 2 2 | Output: 32
|
Hard
| 2 | 1,039 | 618 | 95 | 15 |
1,879 |
F
|
1879F
|
F. Last Man Standing
| 2,800 |
brute force; data structures; number theory
|
There are \(n\) heroes in a videogame. Each hero has some health value \(h\) and initial armor value \(a\). Let the current value of armor be \(a_{\mathit{cur}}\), initially equal to \(a\).When \(x\) points of damage are inflicted on a hero, the following happens: if \(x < a_{\mathit{cur}}\), then \(x\) gets subtracted from \(a_{\mathit{cur}}\); otherwise, \(1\) gets subtracted from \(h\) and \(a_{\mathit{cur}}\) gets assigned back to \(a\).In the start of the game, you choose the value \(x\) (an integer strictly greater than \(0\), arbitrarily large). Then you keep attacking all heroes in rounds: in one round, you inflict \(x\) points of damage to all alive heroes. A hero dies when his health becomes \(0\). The game ends when all heroes are dead.The last hero to die earns the number of points, equal to the number of rounds he was the only hero alive. The other heroes get \(0\) points. In particular, if the last round ends with multiple heroes dying, then every hero gets \(0\) points.The game is played for every possible \(x\) (from \(1\) to infinity). The points are reset between the games. What's the maximum number of points each hero has had?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of heroes.The second line contains \(n\) integers \(h_1, h_2, \dots, h_n\) (\(1 \le h_i \le 2 \cdot 10^5\)) β the health value of each hero.The third line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)) β the initial armor value of each hero.
|
For each testcase, print \(n\) integers β the maximum number of points each hero has had over the games played for every possible \(x\).
|
In the first testcase, the game for \(x = 1\) is played as follows: before all rounds: the heroes have \(h = [3, 1, 2]\), \(a_{\mathit{cur}} = [3, 11, 5]\); round \(1\): \(1\) damage is inflicted on every hero: \(h\) remains \([3, 1, 2]\), \(a_{\mathit{cur}}\) becomes \([2, 10, 4]\); round \(2\): \(h = [3, 1, 2]\), \(a_{\mathit{cur}} = [1, 9, 3]\); round \(3\): the first hero runs out of armor, so he loses a health point: \(h = [2, 1, 2]\), \(a_{\mathit{cur}} = [3, 8, 2]\); ... round \(9\): the first hero dies, since his health reaches \(0\): \(h = [0, 1, 1]\), \(a_{\mathit{cur}} = [0, 2, 1]\); round \(10\): the third hero dies: \(h = [0, 1, 0]\), \(a_{\mathit{cur}} = [0, 1, 0]\); round \(11\): the second hero dies: \(h = [0, 0, 0]\), \(a_{\mathit{cur}} = [0, 0, 0]\). The second hero was the last hero to die, and he was the only hero alive during one round. Thus, he gets \(1\) point for that game.The game for \(x = 4\) is played as follows: round \(1\): \(h = [2, 1, 2]\), \(a_{\mathit{cur}} = [3, 7, 1]\); round \(2\): \(h = [1, 1, 1]\), \(a_{\mathit{cur}} = [3, 3, 5]\); round \(3\): \(h = [0, 0, 1]\), \(a_{\mathit{cur}} = [0, 0, 1]\); round \(4\): \(h = [0, 0, 0]\), \(a_{\mathit{cur}} = [0, 0, 0]\); The third hero was the last hero to die, and he was the only hero alive during one round.
|
Input: 333 1 23 11 5110020045 9 5 19 2 9 10 | Output: 1 1 1 20000 0 4 0 0
|
Master
| 3 | 1,163 | 478 | 136 | 18 |
2,026 |
A
|
2026A
|
A. Perpendicular Segments
| 900 |
constructive algorithms; geometry; greedy; math
|
You are given a coordinate plane and three integers \(X\), \(Y\), and \(K\). Find two line segments \(AB\) and \(CD\) such that the coordinates of points \(A\), \(B\), \(C\), and \(D\) are integers; \(0 \le A_x, B_x, C_x, D_x \le X\) and \(0 \le A_y, B_y, C_y, D_y \le Y\); the length of segment \(AB\) is at least \(K\); the length of segment \(CD\) is at least \(K\); segments \(AB\) and \(CD\) are perpendicular: if you draw lines that contain \(AB\) and \(CD\), they will cross at a right angle. Note that it's not necessary for segments to intersect. Segments are perpendicular as long as the lines they induce are perpendicular.
|
The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases. Next, \(t\) cases follow.The first and only line of each test case contains three integers \(X\), \(Y\), and \(K\) (\(1 \le X, Y \le 1000\); \(1 \le K \le 1414\)).Additional constraint on the input: the values of \(X\), \(Y\), and \(K\) are chosen in such a way that the answer exists.
|
For each test case, print two lines. The first line should contain \(4\) integers \(A_x\), \(A_y\), \(B_x\), and \(B_y\) β the coordinates of the first segment.The second line should also contain \(4\) integers \(C_x\), \(C_y\), \(D_x\), and \(D_y\) β the coordinates of the second segment.If there are multiple answers, print any of them.
|
The answer for the first test case is shown below: The answer for the second test case: The answer for the third test case: The answer for the fourth test case:
|
Input: 41 1 13 4 14 3 33 4 4 | Output: 0 0 1 0 0 0 0 1 2 4 2 2 0 1 1 1 0 0 1 3 1 2 4 1 0 1 3 4 0 3 3 0
|
Beginner
| 4 | 634 | 383 | 339 | 20 |
207 |
D9
|
207D9
|
D9. The Beaver's Problem - 3
| 1,800 |
The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subject. The subject in this problem is an integer from 1 to 3. Each of these numbers has a physical meaning. For instance, all documents with subject 3 are about trade.You can download the training set of documents at the following link: http://download4.abbyy.com/a2/X2RZ2ZWXBG5VYWAL61H76ZQM/train.zip. The archive contains three directories with names ""1"", ""2"", ""3"". Directory named ""1"" contains documents on the 1-st subject, directory ""2"" contains documents on the 2-nd subject, and directory ""3"" contains documents on the 3-rd subject. Each document corresponds to exactly one file from some directory.All documents have the following format: the first line contains the document identifier, the second line contains the name of the document, all subsequent lines contain the text of the document. The document identifier is used to make installing the problem more convenient and has no useful information for the participants.You need to write a program that should indicate the subject for a given document. It is guaranteed that all documents given as input to your program correspond to one of the three subjects of the training set.
|
The first line contains integer id (0 β€ id β€ 106) β the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided into 10 groups. Documents of groups 1 and 2 are taken from the training set, but their identifiers will not match the identifiers specified in the training set. Groups from the 3-rd to the 10-th are roughly sorted by the author in ascending order of difficulty (these groups contain documents which aren't present in the training set).
|
Print an integer from 1 to 3, inclusive β the number of the subject the given document corresponds to.
|
Medium
| 0 | 1,472 | 653 | 102 | 2 |
|||
599 |
D
|
599D
|
D. Spongebob and Squares
| 1,900 |
brute force; math
|
Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m columns. For example, in a 3 Γ 5 table there are 15 squares with side one, 8 squares with side two and 3 squares with side three. The total number of distinct squares in a 3 Γ 5 table is 15 + 8 + 3 = 26.
|
The first line of the input contains a single integer x (1 β€ x β€ 1018) β the number of squares inside the tables Spongebob is interested in.
|
First print a single integer k β the number of tables with exactly x distinct squares inside.Then print k pairs of integers describing the tables. Print the pairs in the order of increasing n, and in case of equality β in the order of increasing m.
|
In a 1 Γ 2 table there are 2 1 Γ 1 squares. So, 2 distinct squares in total. In a 2 Γ 3 table there are 6 1 Γ 1 squares and 2 2 Γ 2 squares. That is equal to 8 squares in total.
|
Input: 26 | Output: 61 262 93 55 39 226 1
|
Hard
| 2 | 423 | 140 | 248 | 5 |
171 |
C
|
171C
|
C. A Piece of Cake
| 2,000 |
*special; implementation
|
How to make a cake you'll never eat.Ingredients. 2 carrots 0 calories 100 g chocolate spread 1 pack of flour 1 egg Method. Put calories into the mixing bowl. Take carrots from refrigerator. Chop carrots. Take chocolate spread from refrigerator. Put chocolate spread into the mixing bowl. Combine pack of flour into the mixing bowl. Fold chocolate spread into the mixing bowl. Add chocolate spread into the mixing bowl. Put pack of flour into the mixing bowl. Add egg into the mixing bowl. Fold pack of flour into the mixing bowl. Chop carrots until choped. Pour contents of the mixing bowl into the baking dish. Serves 1.
|
The only line of input contains a sequence of integers a0, a1, ... (1 β€ a0 β€ 100, 0 β€ ai β€ 1000 for i β₯ 1).
|
Output a single integer.
|
Input: 4 1 2 3 4 | Output: 30
|
Hard
| 2 | 621 | 107 | 24 | 1 |
|
125 |
E
|
125E
|
E. MST Company
| 2,400 |
binary search; graphs
|
The MST (Meaningless State Team) company won another tender for an important state reform in Berland.There are n cities in Berland, some pairs of the cities are connected by roads. Each road has its price. One can move along any road in any direction. The MST team should carry out the repair works on some set of roads such that one can get from any city to any other one moving only along the repaired roads. Moreover, this set should contain exactly k capital roads (that is, the roads that start or finish in the capital). The number of the capital is 1.As the budget has already been approved, the MST Company will profit by finding the set with minimum lengths of roads.
|
The first input line contains three integers n, m, k (1 β€ n β€ 5000;0 β€ m β€ 105;0 β€ k < 5000), where n is the number of cities in the country, m is the number of roads in the country, k is the number of capital roads in the required set. Then m lines enumerate the roads in question. Each road is specified by three numbers ai, bi, wi (1 β€ ai, bi β€ n; 1 β€ w β€ 105), where ai, bi are the numbers of cities linked by a road and wi is its length. Between each pair of cities no more than one road exists. There are no roads that start and finish in one city. The capital's number is 1.
|
In the first line print the number of roads in the required set. The second line should contain the numbers of roads included in the sought set. If the sought set does not exist, print -1.
|
Input: 4 5 21 2 12 3 13 4 11 3 31 4 2 | Output: 31 5 2
|
Expert
| 2 | 676 | 581 | 188 | 1 |
|
286 |
B
|
286B
|
B. Shifting
| 2,200 |
implementation
|
John Doe has found the beautiful permutation formula.Let's take permutation p = p1, p2, ..., pn. Let's define transformation f of this permutation: where k (k > 1) is an integer, the transformation parameter, r is such maximum integer that rk β€ n. If rk = n, then elements prk + 1, prk + 2 and so on are omitted. In other words, the described transformation of permutation p cyclically shifts to the left each consecutive block of length k and the last block with the length equal to the remainder after dividing n by k. John Doe thinks that permutation f(f( ... f(p = [1, 2, ..., n], 2) ... , n - 1), n) is beautiful. Unfortunately, he cannot quickly find the beautiful permutation he's interested in. That's why he asked you to help him.Your task is to find a beautiful permutation for the given n. For clarifications, see the notes to the third sample.
|
A single line contains integer n (2 β€ n β€ 106).
|
Print n distinct space-separated integers from 1 to n β a beautiful permutation of size n.
|
A note to the third test sample: f([1, 2, 3, 4], 2) = [2, 1, 4, 3] f([2, 1, 4, 3], 3) = [1, 4, 2, 3] f([1, 4, 2, 3], 4) = [4, 2, 3, 1]
|
Input: 2 | Output: 2 1
|
Hard
| 1 | 855 | 47 | 90 | 2 |
1,490 |
D
|
1490D
|
D. Permutation Transformation
| 1,200 |
dfs and similar; divide and conquer; implementation
|
A permutation β is a sequence of length \(n\) integers from \(1\) to \(n\), in which all the numbers occur exactly once. For example, \([1]\), \([3, 5, 2, 1, 4]\), \([1, 3, 2]\) β permutations, and \([2, 3, 2]\), \([4, 3, 1]\), \([0]\) β no.Polycarp was recently gifted a permutation \(a[1 \dots n]\) of length \(n\). Polycarp likes trees more than permutations, so he wants to transform permutation \(a\) into a rooted binary tree. He transforms an array of different integers into a tree as follows: the maximum element of the array becomes the root of the tree; all elements to the left of the maximum β form a left subtree (which is built according to the same rules but applied to the left part of the array), but if there are no elements to the left of the maximum, then the root has no left child; all elements to the right of the maximum β form a right subtree (which is built according to the same rules but applied to the right side of the array), but if there are no elements to the right of the maximum, then the root has no right child. For example, if he builds a tree by permutation \(a=[3, 5, 2, 1, 4]\), then the root will be the element \(a_2=5\), and the left subtree will be the tree that will be built for the subarray \(a[1 \dots 1] = [3]\), and the right one β for the subarray \(a[3 \dots 5] = [2, 1, 4]\). As a result, the following tree will be built: The tree corresponding to the permutation \(a=[3, 5, 2, 1, 4]\). Another example: let the permutation be \(a=[1, 3, 2, 7, 5, 6, 4]\). In this case, the tree looks like this: The tree corresponding to the permutation \(a=[1, 3, 2, 7, 5, 6, 4]\). Let us denote by \(d_v\) the depth of the vertex \(a_v\), that is, the number of edges on the path from the root to the vertex numbered \(a_v\). Note that the root depth is zero. Given the permutation \(a\), for each vertex, find the value of \(d_v\).
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) β the length of the permutation.This is followed by \(n\) numbers \(a_1, a_2, \ldots, a_n\) β permutation \(a\).
|
For each test case, output \(n\) values β \(d_1, d_2, \ldots, d_n\).
|
Input: 3 5 3 5 2 1 4 1 1 4 4 3 1 2 | Output: 1 0 2 3 1 0 0 1 3 2
|
Easy
| 3 | 1,874 | 314 | 68 | 14 |
|
2,096 |
C
|
2096C
|
C. Wonderful City
| 1,700 |
dp; implementation
|
You are the proud leader of a city in Ancient Berland. There are \(n^2\) buildings arranged in a grid of \(n\) rows and \(n\) columns. The height of the building in row \(i\) and column \(j\) is \(h_{i, j}\).The city is beautiful if no two adjacent by side buildings have the same height. In other words, it must satisfy the following: There does not exist a position \((i, j)\) (\(1 \leq i \leq n\), \(1 \leq j \leq n - 1\)) such that \(h_{i, j} = h_{i, j + 1}\). There does not exist a position \((i, j)\) (\(1 \leq i \leq n - 1\), \(1 \leq j \leq n\)) such that \(h_{i, j} = h_{i + 1, j}\). There are \(n\) workers at company A, and \(n\) workers at company B. Each worker can be hired at most once.It costs \(a_i\) coins to hire worker \(i\) at company A. After hiring, worker \(i\) will: Increase the heights of all buildings in row \(i\) by \(1\). In other words, increase \(h_{i, 1}, h_{i, 2}, \ldots, h_{i, n}\) by \(1\). It costs \(b_j\) coins to hire worker \(j\) at company B. After hiring, worker \(j\) will: Increase the heights of all buildings in column \(j\) by \(1\). In other words, increase \(h_{1, j}, h_{2, j}, \ldots, h_{n, j}\) by \(1\). Find the minimum number of coins needed to make the city beautiful, or report that it is impossible.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \le n \le 1000\)) β the size of the grid.The \(i\)-th of the next \(n\) lines of each test case contains \(n\) integers \(h_{i, 1}, h_{i, 2}, \ldots, h_{i, n}\) (\(1 \le h_{i, j} \le 10^9\)) β the heights of the buildings in row \(i\).The next line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the costs of hiring the workers at company A.The next line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_j \le 10^9\)) β the costs of hiring the workers at company B.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1000\).
|
For each test case, output a single integer β the minimum number of coins needed, or \(-1\) if it is impossible.
|
For the first test case, we can see that the city is already beautiful. Thus, the answer is \(0\).For the second test case, we can hire worker \(2\) from company A, worker \(4\) from company A, and worker \(4\) from company B:\(1\)\(2\)\(1\)\(\color{red}2\)\(\implies\)\(1\)\(2\)\(1\)\(\color{red}3\)\(\color{red}3\)\(\color{red}2\)\(\color{red}1\)\(\color{red}2\)\(\color{red}4\)\(\color{red}3\)\(\color{red}2\)\(\color{red}4\)\(1\)\(2\)\(1\)\(\color{red}1\)\(1\)\(2\)\(1\)\(\color{red}2\)\(\color{red}1\)\(\color{red}3\)\(\color{red}1\)\(\color{red}2\)\(\color{red}2\)\(\color{red}4\)\(\color{red}2\)\(\color{red}4\)The cost of hiring the workers is \(2 + 4 + 8 = 14\). This is the minimum possible cost.For the third test case, no matter what we do, it is impossible to make the city beautiful. Thus, the answer is \(-1\).
|
Input: 421 22 1100 100100 10041 2 1 23 2 1 21 2 1 11 3 1 21 2 3 45 6 7 831 2 22 2 12 1 1100 100 100100 100 10068 7 2 8 4 87 7 9 7 1 18 3 1 1 8 56 8 3 1 1 41 4 5 1 9 67 1 1 6 8 211 23 20 79 30 1515 83 73 57 34 63 | Output: 0 14 -1 183
|
Medium
| 2 | 1,261 | 858 | 112 | 20 |
1,535 |
D
|
1535D
|
D. Playoff Tournament
| 1,800 |
data structures; dfs and similar; dp; implementation; trees
|
\(2^k\) teams participate in a playoff tournament. The tournament consists of \(2^k - 1\) games. They are held as follows: first of all, the teams are split into pairs: team \(1\) plays against team \(2\), team \(3\) plays against team \(4\) (exactly in this order), and so on (so, \(2^{k-1}\) games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only \(2^{k-1}\) teams remain. If only one team remains, it is declared the champion; otherwise, \(2^{k-2}\) games are played: in the first one of them, the winner of the game ""\(1\) vs \(2\)"" plays against the winner of the game ""\(3\) vs \(4\)"", then the winner of the game ""\(5\) vs \(6\)"" plays against the winner of the game ""\(7\) vs \(8\)"", and so on. This process repeats until only one team remains.For example, this picture describes the chronological order of games with \(k = 3\): Let the string \(s\) consisting of \(2^k - 1\) characters describe the results of the games in chronological order as follows: if \(s_i\) is 0, then the team with lower index wins the \(i\)-th game; if \(s_i\) is 1, then the team with greater index wins the \(i\)-th game; if \(s_i\) is ?, then the result of the \(i\)-th game is unknown (any team could win this game). Let \(f(s)\) be the number of possible winners of the tournament described by the string \(s\). A team \(i\) is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team \(i\) is the champion.You are given the initial state of the string \(s\). You have to process \(q\) queries of the following form: \(p\) \(c\) β replace \(s_p\) with character \(c\), and print \(f(s)\) as the result of the query.
|
The first line contains one integer \(k\) (\(1 \le k \le 18\)).The second line contains a string consisting of \(2^k - 1\) characters β the initial state of the string \(s\). Each character is either ?, 0, or 1.The third line contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of queries.Then \(q\) lines follow, the \(i\)-th line contains an integer \(p\) and a character \(c\) (\(1 \le p \le 2^k - 1\); \(c\) is either ?, 0, or 1), describing the \(i\)-th query.
|
For each query, print one integer β \(f(s)\).
|
Input: 3 0110?11 6 5 1 6 ? 7 ? 1 ? 5 ? 1 1 | Output: 1 2 3 3 5 4
|
Medium
| 5 | 1,782 | 484 | 45 | 15 |
|
1,182 |
D
|
1182D
|
D. Complete Mirror
| 2,400 |
constructive algorithms; dfs and similar; dp; hashing; implementation; trees
|
You have given tree consist of \(n\) vertices. Select a vertex as root vertex that satisfies the condition below. For all vertices \(v_{1}\) and \(v_{2}\), if \(distance\)(\(root\), \(v_{1}\)) \(= distance\)(\(root\), \(v_{2})\) then \(degree\)(\(v_{1}\)) \(= degree\)(\(v_{2}\)), where \(degree\) means the number of vertices connected to that vertex, and \(distance\) means the number of edges between two vertices. Determine and find if there is such root vertex in the tree. If there are multiple answers, find any of them.
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^{5}\)) β the number of vertices.Each of the next \(n-1\) lines contains two integers \(v_{i}\) and \(u_{i}\) (\(1 \le v_{i} \lt u_{i} \le n\)) β it means there is an edge exist between \(v_{i}\) and \(u_{i}\). It is guaranteed that the graph forms tree.
|
If there is such root vertex exists, print any of them. Otherwise, print \(-1\).
|
This is the picture for the first example. \(1\), \(5\), \(7\) also can be a valid answer. This is the picture for the second example. You can see that it's impossible to find such root vertex.
|
Input: 7 1 2 2 3 3 4 4 5 3 6 6 7 | Output: 3
|
Expert
| 6 | 527 | 318 | 80 | 11 |
1,625 |
E2
|
1625E2
|
E2. Cats on the Upgrade (hard version)
| 2,800 |
binary search; data structures; dfs and similar; graphs; trees
|
This is the hard version of the problem. The only difference between the easy and the hard versions are removal queries, they are present only in the hard version.""Interplanetary Software, Inc."" together with ""Robots of Cydonia, Ltd."" has developed and released robot cats. These electronic pets can meow, catch mice and entertain the owner in various ways.The developers from ""Interplanetary Software, Inc."" have recently decided to release a software update for these robots. After the update, the cats must solve the problems about bracket sequences. One of the problems is described below. First, we need to learn a bit of bracket sequence theory. Consider the strings that contain characters ""("", "")"" and ""."". Call a string regular bracket sequence (RBS), if it can be transformed to an empty string by one or more operations of removing either single ""."" characters, or a continuous substring ""()"". For instance, the string ""(()(.))"" is an RBS, as it can be transformed to an empty string with the following sequence of removals: ""(()(.))"" \(\rightarrow\) ""(()())"" \(\rightarrow\) ""(())"" \(\rightarrow\) ""()"" \(\rightarrow\) """". We got an empty string, so the initial string was an RBS. At the same time, the string "")("" is not an RBS, as it is not possible to apply such removal operations to it.An RBS is simple if this RBS is not empty, doesn't start with ""."", and doesn't end with ""."".Denote the substring of the string \(s\) as its sequential subsegment. In particular, \(s[l\dots r] = s_ls_{l+1}\dots s_r\), where \(s_i\) is the \(i\)-th character of the string \(s\).Now, move on to the problem statement itself. You are given a string \(s\), initially consisting of characters ""("" and "")"". You need to answer the following queries: Given two indices, \(l\) and \(r\) (\(1 \le l < r \le n\)). It's guaranteed that the \(l\)-th character is equal to ""("", the \(r\)-th character is equal to "")"", and the characters between them are equal to ""."". Then the \(l\)-th and the \(r\)-th characters must be set to ""."". Given two indices, \(l\) and \(r\) (\(1 \le l < r \le n\)), and it's guaranteed that the substring \(s[l\dots r]\) is a simple RBS. You need to find the number of substrings in \(s[l\dots r]\) such that they are simple RBS. In other words, find the number of index pairs \(i\), \(j\) such that \(l \le i < j \le r\) and \(s[i\dots j]\) is a simple RBS. You are an employee in ""Interplanetary Software, Inc."" and you were given the task to teach the cats to solve the problem above, after the update.
|
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 3\cdot10^5\), \(1 \le q \le 3\cdot10^5\)), the length of the string, and the number of queries.The second line contains the string \(s\), consisting of \(n\) characters ""("" and "")"".Each of the following \(q\) lines contains three integers \(t\), \(l\) and \(r\) (\(t \in \{1, 2\}\), \(1 \le l < r \le n\)), the queries you need to answer. It is guaranteed that all the queries are valid and correspond to the problem statements.
|
For each query, print a single integer in a separate line, the number of substrings that are simple RBS. The answers must be printed in the same order as the queries are specified in the input.
|
Consider the example test case.The answer to the first query is \(3\), as there are three suitable substrings: \(s[3\dots6]\), \(s[3\dots4]\) and \(s[5\dots6]\).The answer to the second query is \(4\). The substrings are \(s[3\dots6]\), \(s[3\dots4]\), \(s[5\dots6]\) and \(s[2\dots7]\).After the third query, the string becomes "")(..())()"".The answer to the fourth query is \(2\). The substrings are \(s[5\dots6]\) and \(s[2\dots7]\). Note that \(s[3\dots6]\) is not a simple RBS anymore, as it starts with ""."".The answer to the fifth query is \(4\). The substrings are \(s[5\dots6]\), \(s[2\dots7]\), \(s[8\dots9]\) and \(s[2\dots9]\).After the sixth query, the string becomes "")(....)()"".After the seventh query, the string becomes "")......()"".The answer to the eighth query is \(1\). The substring is \(s[8\dots9]\).
|
Input: 9 8 )(()())() 2 3 6 2 2 7 1 3 4 2 2 7 2 2 9 1 5 6 1 2 7 2 8 9 | Output: 3 4 2 4 1
|
Master
| 5 | 2,570 | 500 | 193 | 16 |
93 |
E
|
93E
|
E. Lostborn
| 2,600 |
dp; math; number theory
|
Igor K. very much likes a multiplayer role playing game WineAge II. Who knows, perhaps, that might be the reason for his poor performance at the university. As any person who plays the game, he is interested in equipping his hero with as good weapon and outfit as possible. One day, as he was reading the game's forum yet again, he discovered a very interesting fact. As it turns out, each weapon in the game is characterised with k different numbers: a1, ..., ak. They are called hit indicators and according to the game developers' plan they are pairwise coprime. The damage that is inflicted during a hit depends not only on the weapon's characteristics, but also on the hero's strength parameter. Thus, if the hero's strength equals n, than the inflicted damage will be calculated as the number of numbers on the segment , that aren't divisible by any hit indicator ai.Recently, having fulfilled another quest, Igor K. found a new Lostborn sword. He wants to know how much damage he will inflict upon his enemies if he uses it.
|
The first line contains two integers: n and k (1 β€ n β€ 1013, 1 β€ k β€ 100). They are the indicator of Igor K's hero's strength and the number of hit indicators.The next line contains space-separated k integers ai (1 β€ ai β€ 1000). They are Lostborn sword's hit indicators. The given k numbers are pairwise coprime.
|
Print the single number β the damage that will be inflicted by Igor K.'s hero when he uses his new weapon. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
|
Input: 20 32 3 5 | Output: 6
|
Expert
| 3 | 1,031 | 312 | 260 | 0 |
|
1,443 |
B
|
1443B
|
B. Saving the City
| 1,300 |
dp; greedy; math; sortings
|
Bertown is a city with \(n\) buildings in a straight line.The city's security service discovered that some buildings were mined. A map was compiled, which is a string of length \(n\), where the \(i\)-th character is ""1"" if there is a mine under the building number \(i\) and ""0"" otherwise.Bertown's best sapper knows how to activate mines so that the buildings above them are not damaged. When a mine under the building numbered \(x\) is activated, it explodes and activates two adjacent mines under the buildings numbered \(x-1\) and \(x+1\) (if there were no mines under the building, then nothing happens). Thus, it is enough to activate any one mine on a continuous segment of mines to activate all the mines of this segment. For manual activation of one mine, the sapper takes \(a\) coins. He can repeat this operation as many times as you want.Also, a sapper can place a mine under a building if it wasn't there. For such an operation, he takes \(b\) coins. He can also repeat this operation as many times as you want.The sapper can carry out operations in any order.You want to blow up all the mines in the city to make it safe. Find the minimum number of coins that the sapper will have to pay so that after his actions there are no mines left in the city.
|
The first line contains one positive integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. Then \(t\) test cases follow.Each test case begins with a line containing two integers \(a\) and \(b\) (\(1 \le a, b \le 1000\)) β the cost of activating and placing one mine, respectively.The next line contains a map of mines in the city β a string consisting of zeros and ones.The sum of the string lengths for all test cases does not exceed \(10^5\).
|
For each test case, output one integer β the minimum number of coins that the sapper will have to pay.
|
In the second test case, if we place a mine under the fourth building and then activate it, then all mines on the field are activated. The cost of such operations is six, \(b=1\) coin for placing a mine and \(a=5\) coins for activating.
|
Input: 2 1 1 01000010 5 1 01101110 | Output: 2 6
|
Easy
| 4 | 1,268 | 455 | 102 | 14 |
183 |
D
|
183D
|
D. T-shirt
| 2,700 |
dp; greedy; probabilities
|
You are going to work in Codeforces as an intern in a team of n engineers, numbered 1 through n. You want to give each engineer a souvenir: a T-shirt from your country (T-shirts are highly desirable in Codeforces). Unfortunately you don't know the size of the T-shirt each engineer fits in. There are m different sizes, numbered 1 through m, and each engineer will fit in a T-shirt of exactly one size.You don't know the engineers' exact sizes, so you asked your friend, Gerald. Unfortunately, he wasn't able to obtain the exact sizes either, but he managed to obtain for each engineer i and for all sizes j, the probability that the size of the T-shirt that fits engineer i is j.Since you're planning to give each engineer one T-shirt, you are going to bring with you exactly n T-shirts. For those n T-shirts, you can bring any combination of sizes (you can bring multiple T-shirts with the same size too!). You don't know the sizes of T-shirts for each engineer when deciding what sizes to bring, so you have to pick this combination based only on the probabilities given by your friend, Gerald. Your task is to maximize the expected number of engineers that receive a T-shirt of his size. This is defined more formally as follows. When you finally arrive at the office, you will ask each engineer his T-shirt size. Then, if you still have a T-shirt of that size, you will give him one of them. Otherwise, you don't give him a T-shirt. You will ask the engineers in order starting from engineer 1, then engineer 2, and so on until engineer n.
|
The first line contains two space-separated integers n and m (1 β€ n β€ 3000, 1 β€ m β€ 300), denoting the number of engineers and the number of T-shirt sizes, respectively.Then n lines follow, each line contains m space-separated integers. The j-th integer in the i-th line represents the probability that the i-th engineer fits in a T-shirt of size j. Each probability will be given as an integer between 0 and 1000, inclusive. The actual probability should be calculated as the given number divided by 1000. It is guaranteed that for any engineer, the sum of the probabilities for all m T-shirts is equal to one.
|
Print a single real number denoting the maximum possible expected number of engineers that will receive a T-shirt.For the answer the absolute or relative error of 10 - 9 is acceptable.
|
For the first example, bring one T-shirt of each size. With 0.5 chance, either both engineers fit inside T-shirts of size 1 or both fit inside T-shirts of size 2. With the other 0.5 chance, one engineer fits inside a T-shirt of size 1 and the other inside a T-shirt of size 2. If the first is true, the number of engineers that receive a T-shirt is one. If the second is true, the number of such engineers is two. Hence, the expected number of engineers who receive a T-shirt is 1.5. This is maximum possible expected number of engineers for all sets of T-shirts. For the second example, bring two T-shirts of size 1 and one T-shirt of size 2. This way, each engineer will definitely receive a T-shirt of his size.For the third example, bring one T-shirt of size 4.
|
Input: 2 2500 500500 500 | Output: 1.500000000000
|
Master
| 3 | 1,544 | 611 | 184 | 1 |
1,875 |
A
|
1875A
|
A. Jellyfish and Undertale
| 900 |
brute force; greedy
|
Flowey has planted a bomb in Snowdin!The bomb has a timer that is initially set to \(b\). Every second, the timer will decrease by \(1\). When the timer reaches \(0\), the bomb will explode! To give the residents of Snowdin enough time to evacuate, you will need to delay the bomb from exploding for as long as possible.You have \(n\) tools. Each tool can only be used at most once. If you use the \(i\)-th tool, the timer will increase by \(x_i\). However, if the timer is changed to an integer larger than \(a\), the timer will be set to \(a\) due to a bug.More specifically, the following events will happen every second in the following order: You will choose some (possibly none) of your tools that have not been used before. If you choose the \(i\)-th tool, and the bomb's timer is currently set to \(c\), the timer will be changed to \(\min(c + x_i, a)\). The timer decreases by \(1\). If the timer reaches \(0\), the bomb explodes. Jellyfish now wants to know the maximum time in seconds until the bomb explodes if the tools are used optimally.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 2000\)). The description of the test cases follows.The first line of each test case contains three integers \(a\), \(b\) and \(n\) (\(1 \leq b \leq a \leq 10^9\), \(1 \leq n \leq 100\)) β the maximum value of the bomb's timer, the initial value of the timer of the bomb and the number of tools.The second line of each test contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(1 \leq x_i \leq 10^9\)) β the number the timer can increase by using the \(i\)-th tool.Note that the sum of \(n\) over all test cases is not bounded.
|
For each test case, output a single integer β the maximum time in seconds until the bomb explodes.
|
Let \(c\) denote the value of the bomb's timer. In the first test case: Second \(1\): choose tool \(1\) and \(2\) at this second, then \(c=5\); the timer decreases by \(1\), then \(c=4\). Second \(2\): the timer decreases by \(1\), then \(c=3\). Second \(3\): the timer decreases by \(1\), then \(c=2\). Second \(4\): the timer decreases by \(1\), then \(c=1\). Second \(5\): choose tool \(3\), then \(c=5\); the timer decreases by \(1\), then \(c=4\). Second \(6\): the timer decreases by \(1\), then \(c=3\). Second \(7\): the timer decreases by \(1\), then \(c=2\). Second \(8\): the timer decreases by \(1\), then \(c=1\). Second \(9\): the timer decreases by \(1\), then \(c=0\). The bomb explodes. It can be proved that there is no way to use the tools such that the bomb explodes after more than \(9\) seconds.
|
Input: 25 3 31 1 77 1 51 2 5 6 8 | Output: 9 21
|
Beginner
| 2 | 1,052 | 636 | 98 | 18 |
438 |
E
|
438E
|
E. The Child and Binary Tree
| 3,100 |
combinatorics; divide and conquer; fft; number theory
|
Our child likes computer science very much, especially he likes binary trees.Consider the sequence of n distinct positive integers: c1, c2, ..., cn. The child calls a vertex-weighted rooted binary tree good if and only if for every vertex v, the weight of v is in the set {c1, c2, ..., cn}. Also our child thinks that the weight of a vertex-weighted tree is the sum of all vertices' weights.Given an integer m, can you for all s (1 β€ s β€ m) calculate the number of good vertex-weighted rooted binary trees with weight s? Please, check the samples for better understanding what trees are considered different.We only want to know the answer modulo 998244353 (7 Γ 17 Γ 223 + 1, a prime number).
|
The first line contains two integers n, m (1 β€ n β€ 105; 1 β€ m β€ 105). The second line contains n space-separated pairwise distinct integers c1, c2, ..., cn. (1 β€ ci β€ 105).
|
Print m lines, each line containing a single integer. The i-th line must contain the number of good vertex-weighted rooted binary trees whose weight exactly equal to i. Print the answers modulo 998244353 (7 Γ 17 Γ 223 + 1, a prime number).
|
In the first example, there are 9 good vertex-weighted rooted binary trees whose weight exactly equal to 3:
|
Input: 2 31 2 | Output: 139
|
Master
| 4 | 692 | 172 | 239 | 4 |
925 |
B
|
925B
|
B. Resource Distribution
| 1,700 |
binary search; implementation; sortings
|
One department of some software company has \(n\) servers of different specifications. Servers are indexed with consecutive integers from \(1\) to \(n\). Suppose that the specifications of the \(j\)-th server may be expressed with a single integer number \(c_j\) of artificial resource units.In order for production to work, it is needed to deploy two services \(S_1\) and \(S_2\) to process incoming requests using the servers of the department. Processing of incoming requests of service \(S_i\) takes \(x_i\) resource units.The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service \(S_i\) is deployed using \(k_i\) servers, then the load is divided equally between these servers and each server requires only \(x_i / k_i\) (that may be a fractional number) resource units.Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides.Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services.
|
The first line contains three integers \(n\), \(x_1\), \(x_2\) (\(2 \leq n \leq 300\,000\), \(1 \leq x_1, x_2 \leq 10^9\)) β the number of servers that the department may use, and resource units requirements for each of the services.The second line contains \(n\) space-separated integers \(c_1, c_2, \ldots, c_n\) (\(1 \leq c_i \leq 10^9\)) β the number of resource units provided by each of the servers.
|
If it is impossible to deploy both services using the given servers, print the only word ""No"" (without the quotes).Otherwise print the word ""Yes"" (without the quotes). In the second line print two integers \(k_1\) and \(k_2\) (\(1 \leq k_1, k_2 \leq n\)) β the number of servers used for each of the services.In the third line print \(k_1\) integers, the indices of the servers that will be used for the first service.In the fourth line print \(k_2\) integers, the indices of the servers that will be used for the second service.No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them.
|
In the first sample test each of the servers 1, 2 and 6 will will provide \(8 / 3 = 2.(6)\) resource units and each of the servers 5, 4 will provide \(16 / 2 = 8\) resource units.In the second sample test the first server will provide \(20\) resource units and each of the remaining servers will provide \(32 / 3 = 10.(6)\) resource units.
|
Input: 6 8 163 5 2 9 8 7 | Output: Yes3 21 2 65 4
|
Medium
| 3 | 1,253 | 405 | 684 | 9 |
2,041 |
M
|
2041M
|
M. Selection Sort
| 2,000 |
binary search; data structures; greedy; two pointers
|
Every student enrolled in the algorithms course is required to submit an assignment this week. The task is to implement an \(O(n^2)\)-time algorithm to sort \(n\) given integers in non-decreasing order. Alice has already completed her assignment, and her implementation is shown below. int alice_sort(int *s, int n){ for(int i = 0; i < n; ++i){ for(int j = i + 1; j < n; ++j){ if(s[i] > s[j]){ int swap = s[i]; s[i] = s[j]; s[j] = swap; } } } return 0;}While you have access to Alice's code, you prefer not to simply copy it. Instead, you want to use Alice's sorting function as a building block for your own solution. There are two ways as listed below you can utilize her function, but each of them can be applied at most once. The order in which these two operations are invoked can be arbitrary. Prefix sort: choose a length \(i \in \{1, 2, \ldots, n\}\) and call \(\texttt{alicesort(}s, i\texttt{)}\). This sorts the first \(i\) elements in the array \(s\). Suffix sort: choose a length \(i \in \{1, 2, \ldots, n\}\) and call \(\texttt{alicesort(}s+n-i, i\texttt{)}\). This sorts the last \(i\) elements in the array \(s\). Due to the time complexity of the sorting algorithm, the cost of performing either a prefix or suffix sort is \(i^2\), where \(i\) is the length of the chosen subarray. Your goal is to determine the minimum cost to sort the input array \(s\) of \(n\) integers in non-decreasing order using Alice's function, following the rules mentioned above.For example, Let \(s=[3,2,5,5,4,1]\). We can first perform a suffix sort of length \(4\), and the array becomes \([3,2,1,4,5,5]\). Then, we perform a prefix sort of length \(3\), and the array becomes \([1,2,3,4,5,5]\), which is a sorted array. The cost is \(4^2+3^2=25\). Here is another example, let \(s=[4,3,2,1]\). We can complete the sorting by performing only a prefix sort of length \(4\), and the cost is \(4^2=16\).
|
The first line contains exactly one integer \(n\) which indicates the number of integers in the array \(s\). The second line contains the \(n\) integers in \(s=[s_0, s_1, \ldots, s_{n-1}]\). \(1 \le n \le 10^6\) For all \(i\) (\(0\le i < n\)), \(0\le s_i < 2^{31}-1\).
|
Output an integer on a line, indicating the minimum cost to sort the input array \(s\) of \(n\) integers in non-decreasing order using Alice's function, following the rules mentioned above.
|
Input: 6 3 2 5 5 4 1 | Output: 25
|
Hard
| 4 | 1,897 | 268 | 189 | 20 |
|
169 |
A
|
169A
|
A. Chores
| 800 |
sortings
|
Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do n chores. Each chore is characterized by a single parameter β its complexity. The complexity of the i-th chore equals hi.As Petya is older, he wants to take the chores with complexity larger than some value x (hi > x) to leave to Vasya the chores with complexity less than or equal to x (hi β€ x). The brothers have already decided that Petya will do exactly a chores and Vasya will do exactly b chores (a + b = n).In how many ways can they choose an integer x so that Petya got exactly a chores and Vasya got exactly b chores?
|
The first input line contains three integers n, a and b (2 β€ n β€ 2000; a, b β₯ 1; a + b = n) β the total number of chores, the number of Petya's chores and the number of Vasya's chores.The next line contains a sequence of integers h1, h2, ..., hn (1 β€ hi β€ 109), hi is the complexity of the i-th chore. The numbers in the given sequence are not necessarily different.All numbers on the lines are separated by single spaces.
|
Print the required number of ways to choose an integer value of x. If there are no such ways, print 0.
|
In the first sample the possible values of x are 3, 4 or 5.In the second sample it is impossible to find such x, that Petya got 3 chores and Vasya got 4.
|
Input: 5 2 36 2 3 100 1 | Output: 3
|
Beginner
| 1 | 654 | 422 | 102 | 1 |
433 |
A
|
433A
|
A. Kitahara Haruki's Gift
| 1,100 |
brute force; implementation
|
Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal to the total weight of the apples given to Ogiso Setsuna.But unfortunately Kitahara Haruki doesn't have a knife right now, so he cannot split any apple into some parts. Please, tell him: is it possible to divide all the apples in a fair way between his friends?
|
The first line contains an integer n (1 β€ n β€ 100) β the number of apples. The second line contains n integers w1, w2, ..., wn (wi = 100 or wi = 200), where wi is the weight of the i-th apple.
|
In a single line print ""YES"" (without the quotes) if it is possible to divide all the apples between his friends. Otherwise print ""NO"" (without the quotes).
|
In the first test sample Kitahara Haruki can give the first and the last apple to Ogiso Setsuna and the middle apple to Touma Kazusa.
|
Input: 3100 200 100 | Output: YES
|
Easy
| 2 | 579 | 192 | 160 | 4 |
683 |
G
|
683G
|
G. The Fraction
| 1,900 |
*special
|
Periodic decimal fraction is usually written as: [entire_part.non-periodic_part (period)]. Any simple fraction can be represented as a periodic decimal fraction and vice versa. For example, the decimal fraction 0.2(45) corresponds to a fraction 27 / 110. Your task is to convert the periodic fraction to a simple periodic fraction.
|
The first line contains the periodic decimal fraction x (0 < x < 1) in the format described in the statement. The total number of digits in the period and non-periodic part of the fraction does not exceed 8. Non-periodic part may be absent, the periodic part can't be absent (but it can be equal to any non-negative number).
|
Print the representation of the fraction x as a simple fraction p / q, where p and q are mutually prime integers.
|
Input: 0.2(45) | Output: 27/110
|
Hard
| 1 | 331 | 324 | 113 | 6 |
|
95 |
C
|
95C
|
C. Volleyball
| 1,900 |
shortest paths
|
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths.Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially.At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium.
|
The first line contains two integers n and m (1 β€ n β€ 1000, 0 β€ m β€ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y (1 β€ x, y β€ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 β€ ui, vi β€ n, 1 β€ wi β€ 109) β they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci (1 β€ ti, ci β€ 109), which describe the taxi driver that waits at the i-th junction β the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character.
|
If taxis can't drive Petya to the destination point, print ""-1"" (without the quotes). Otherwise, print the drive's minimum cost.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
|
An optimal way β ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
|
Input: 4 41 31 2 31 4 12 4 12 3 52 77 21 27 7 | Output: 9
|
Hard
| 1 | 1,003 | 969 | 278 | 0 |
346 |
A
|
346A
|
A. Alice and Bob
| 1,600 |
games; math; number theory
|
It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. The rules are as follows. First, they get a set of n distinct integers. And then they take turns to make the following moves. During each move, either Alice or Bob (the player whose turn is the current) can choose two distinct integers x and y from the set, such that the set doesn't contain their absolute difference |x - y|. Then this player adds integer |x - y| to the set (so, the size of the set increases by one).If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first.
|
The first line contains an integer n (2 β€ n β€ 100) β the initial number of elements in the set. The second line contains n distinct space-separated integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the set.
|
Print a single line with the winner's name. If Alice wins print ""Alice"", otherwise print ""Bob"" (without quotes).
|
Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
|
Input: 22 3 | Output: Alice
|
Medium
| 3 | 705 | 214 | 116 | 3 |
60 |
C
|
60C
|
C. Mushroom Strife
| 2,100 |
brute force; dfs and similar
|
Pasha and Akim were making a forest map β the lawns were the graph's vertexes and the roads joining the lawns were its edges. They decided to encode the number of laughy mushrooms on every lawn in the following way: on every edge between two lawns they wrote two numbers, the greatest common divisor (GCD) and the least common multiple (LCM) of the number of mushrooms on these lawns. But one day Pasha and Akim had an argument about the laughy mushrooms and tore the map. Pasha was left with just some part of it, containing only m roads. Your task is to help Pasha β use the map he has to restore the number of mushrooms on every lawn. As the result is not necessarily unique, help Pasha to restore any one or report that such arrangement of mushrooms does not exist. It is guaranteed that the numbers on the roads on the initial map were no less that 1 and did not exceed 106.
|
The first line contains two numbers n and m () which are the numbers of lawns and roads we know about. Each of the following m lines contains four numbers which are the numbers of lawns the road connects, the GCD and the LCM of the numbers of mushrooms on these lawns (1 β€ GCD, LCM β€ 106).It is guaranteed, that no road connects lawn to itself, and no two lawns are connected by more than one road.
|
The answer should contain ""YES"" or ""NO"" on the first line, saying whether it is possible or not to perform the arrangement. If the answer is ""YES"", print on the following line n numbers which are the numbers of mushrooms on the corresponding lawns.
|
Input: 1 0 | Output: YES1
|
Hard
| 2 | 879 | 398 | 254 | 0 |
|
1,574 |
A
|
1574A
|
A. Regular Bracket Sequences
| 800 |
constructive algorithms
|
A bracket sequence is a string containing only characters ""("" and "")"". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example, bracket sequences ""()()"" and ""(())"" are regular (the resulting expressions are: ""(1)+(1)"" and ""((1+1)+1)""), and "")("", ""("" and "")"" are not.You are given an integer \(n\). Your goal is to construct and print exactly \(n\) different regular bracket sequences of length \(2n\).
|
The first line contains one integer \(t\) (\(1 \le t \le 50\)) β the number of test cases.Each test case consists of one line containing one integer \(n\) (\(1 \le n \le 50\)).
|
For each test case, print \(n\) lines, each containing a regular bracket sequence of length exactly \(2n\). All bracket sequences you output for a testcase should be different (though they may repeat in different test cases). If there are multiple answers, print any of them. It can be shown that it's always possible.
|
Input: 3 3 1 3 | Output: ()()() ((())) (()()) () ((())) (())() ()(())
|
Beginner
| 1 | 574 | 176 | 318 | 15 |
|
76 |
D
|
76D
|
D. Plus and xor
| 1,700 |
dp; greedy; math
|
Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then: X xor Y = 6810 = 10001002. Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions: A = X + Y B = X xor Y, where xor is bitwise exclusive or. X is the smallest number among all numbers for which the first two conditions are true.
|
The first line contains integer number A and the second line contains integer number B (0 β€ A, B β€ 264 - 1).
|
The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.
|
Input: 14276 | Output: 33 109
|
Medium
| 3 | 746 | 108 | 125 | 0 |
|
2,038 |
D
|
2038D
|
D. Divide OR Conquer
| 2,400 |
binary search; bitmasks; data structures; dp; implementation
|
You are given an array \([a_1, a_2, \ldots a_n]\) consisting of integers between \(0\) and \(10^9\). You have to split this array into several segments (possibly one) in such a way that each element belongs to exactly one segment.Let the first segment be the array \([a_{l_1}, a_{l_1 + 1}, \ldots, a_{r_1}]\), the second segment be \([a_{l_2}, a_{l_2+ 1}, \ldots, a_{r_2}]\), ..., the last segment be \([a_{l_k}, a_{l_k+ 1}, \ldots, a_{r_k}]\). Since every element should belong to exactly one array, \(l_1 = 1\), \(r_k = n\), and \(r_i + 1 = l_{i+1}\) for each \(i\) from \(1\) to \(k-1\). The split should meet the following condition: \(f([a_{l_1}, a_{l_1 + 1}, \ldots, a_{r_1}]) \le f([a_{l_2}, a_{l_2+ 1}, \ldots, a_{r_2}]) \le \dots \le f([a_{l_k}, a_{l_k+1}, \ldots, a_{r_k}])\), where \(f(a)\) is the bitwise OR of all elements of the array \(a\).Calculate the number of ways to split the array, and print it modulo \(998\,244\,353\). Two ways are considered different if the sequences \([l_1, r_1, l_2, r_2, \ldots, l_k, r_k]\) denoting the splits are different.
|
The first line contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10 ^9\)) β the elements of the given array.
|
Print one integer β the number of ways to split the array, taken modulo \(998\,244\,353\).
|
In the first two examples, every way to split the array is valid.In the third example, there are three valid ways to split the array: \(k = 3\); \(l_1 = 1, r_1 = 1, l_2 = 2, r_2 = 2, l_3 = 3, r_3 = 3\); the resulting arrays are \([3]\), \([4]\), \([6]\), and \(3 \le 4 \le 6\); \(k = 2\); \(l_1 = 1, r_1 = 1, l_2 = 2, r_2 = 3\); the resulting arrays are \([3]\) and \([4, 6]\), and \(3 \le 6\); \(k = 1\); \(l_1 = 1, r_1 = 3\); there will be only one array: \([3, 4, 6]\). If you split the array into two arrays \([3, 4]\) and \([6]\), the bitwise OR of the first array is \(7\), and the bitwise OR of the second array is \(6\); \(7 > 6\), so this way to split the array is invalid.
|
Input: 31 2 3 | Output: 4
|
Expert
| 5 | 1,071 | 230 | 90 | 20 |
472 |
C
|
472C
|
C. Design Tutorial: Make It Nondeterministic
| 1,400 |
greedy
|
A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?More formally, if we denote the handle of the i-th person as hi, then the following condition must hold: .
|
The first line contains an integer n (1 β€ n β€ 105) β the number of people.The next n lines each contains two strings. The i-th line contains strings fi and si (1 β€ |fi|, |si| β€ 50) β the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.The next line contains n distinct integers: p1, p2, ..., pn (1 β€ pi β€ n).
|
If it is possible, output ""YES"", otherwise output ""NO"".
|
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.In example 3, if Copernicus uses ""copernicus"" as his handle, everything will be alright.
|
Input: 3gennady korotkevichpetr mitrichevgaoyuan chen1 2 3 | Output: NO
|
Easy
| 1 | 698 | 406 | 59 | 4 |
1,182 |
B
|
1182B
|
B. Plus from Picture
| 1,300 |
dfs and similar; implementation; strings
|
You have a given picture with size \(w \times h\). Determine if the given picture has a single ""+"" shape or not. A ""+"" shape is described below: A ""+"" shape has one center nonempty cell. There should be some (at least one) consecutive non-empty cells in each direction (left, right, up, down) from the center. In other words, there should be a ray in each direction. All other cells are empty. Find out if the given picture has single ""+"" shape.
|
The first line contains two integers \(h\) and \(w\) (\(1 \le h\), \(w \le 500\)) β the height and width of the picture.The \(i\)-th of the next \(h\) lines contains string \(s_{i}\) of length \(w\) consisting ""."" and ""*"" where ""."" denotes the empty space and ""*"" denotes the non-empty space.
|
If the given picture satisfies all conditions, print ""YES"". Otherwise, print ""NO"".You can output each letter in any case (upper or lower).
|
In the first example, the given picture contains one ""+"".In the second example, two vertical branches are located in a different column.In the third example, there is a dot outside of the shape.In the fourth example, the width of the two vertical branches is \(2\).In the fifth example, there are two shapes.In the sixth example, there is an empty space inside of the shape.
|
Input: 5 6 ...... ..*... .****. ..*... ..*... | Output: YES
|
Easy
| 3 | 453 | 300 | 142 | 11 |
1,674 |
C
|
1674C
|
C. Infinite Replacement
| 1,000 |
combinatorics; implementation; strings
|
You are given a string \(s\), consisting only of Latin letters 'a', and a string \(t\), consisting of lowercase Latin letters.In one move, you can replace any letter 'a' in the string \(s\) with a string \(t\). Note that after the replacement string \(s\) might contain letters other than 'a'.You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large.Two strings are considered different if they have different length, or they differ at some index.
|
The first line contains a single integer \(q\) (\(1 \le q \le 10^4\)) β the number of testcases.The first line of each testcase contains a non-empty string \(s\), consisting only of Latin letters 'a'. The length of \(s\) doesn't exceed \(50\).The second line contains a non-empty string \(t\), consisting of lowercase Latin letters. The length of \(t\) doesn't exceed \(50\).
|
For each testcase, print the number of different strings \(s\) that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number.
|
In the first example, you can replace any letter 'a' with the string ""a"", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one.In the second example, you can replace the second letter 'a' with ""abc"". String \(s\) becomes equal to ""aabc"". Then the second letter 'a' again. String \(s\) becomes equal to ""aabcbc"". And so on, generating infinitely many different strings.In the third example, you can either leave string \(s\) as is, performing zero moves, or replace the only 'a' with ""b"". String \(s\) becomes equal to ""b"", so you can't perform more moves on it.
|
Input: 3aaaaaaaabcab | Output: 1 -1 2
|
Beginner
| 3 | 552 | 375 | 210 | 16 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.