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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
238 |
C
|
238C
|
C. World Eater Brothers
| 2,100 |
dfs and similar; dp; greedy; trees
|
You must have heard of the two brothers dreaming of ruling the world. With all their previous plans failed, this time they decided to cooperate with each other in order to rule the world. As you know there are n countries in the world. These countries are connected by n - 1 directed roads. If you don't consider direction of the roads there is a unique path between every pair of countries in the world, passing through each road at most once. Each of the brothers wants to establish his reign in some country, then it's possible for him to control the countries that can be reached from his country using directed roads. The brothers can rule the world if there exists at most two countries for brothers to choose (and establish their reign in these countries) so that any other country is under control of at least one of them. In order to make this possible they want to change the direction of minimum number of roads. Your task is to calculate this minimum number of roads.
|
The first line of input contains an integer n (1 β€ n β€ 3000). Each of the next n - 1 lines contains two space-separated integers ai and bi (1 β€ ai, bi β€ n; ai β bi) saying there is a road from country ai to country bi.Consider that countries are numbered from 1 to n. It's guaranteed that if you don't consider direction of the roads there is a unique path between every pair of countries in the world, passing through each road at most once.
|
In the only line of output print the minimum number of roads that their direction should be changed so that the brothers will be able to rule the world.
|
Input: 42 13 14 1 | Output: 1
|
Hard
| 4 | 979 | 442 | 152 | 2 |
|
1,868 |
D
|
1868D
|
D. Flower-like Pseudotree
| 3,000 |
constructive algorithms; graphs; greedy; implementation; trees
|
A pseudotree is a connected graph which has exactly one cycle and no self-loops. Note that a pseudotree may contain multiple-edges. It can be shown that a pseudotree with \(n\) vertices always contains \(n\) edges.After deleting all edges on the cycle in the pseudotree, a forest\(^{\dagger}\) will be formed. It can be shown that each tree in the forest will contain exactly one vertex which is on cycle before removing the edges. If all trees in the forest have the same depth\(^{\ddagger}\) when picking the vertex on cycle as root, we call the original pseudotree flower-like.Our friend sszcdjr, had a flower-like pseudotree with \(n\) vertices and \(n\) edges. However, he forgot all the edges in the pseudotree. Fortunately, he still remembers the degrees of vertices. Specifically, the degree of the \(i\)-th vertex is \(d_i\).You have to help sszcdjr construct a possible flower-like pseudotree with \(n\) vertices, where the degree of the \(i\)-th vertex is exactly \(d_i\), or tell him that it is impossible.\(^{\dagger}\) A forest is a graph in which all connectivity components are trees. A connected graph without cycles and self-loops is called a tree.\(^{\ddagger}\) The depth of a tree with a root is the maximum distance from the root to the vertex of this tree.
|
The first line of the input contains a single integer \(t\) (\(1\leq t\leq 10^5\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2\leq n\leq 10^6\)) β the number of vertices.The second line of each test case contains \(n\) integers \(d_1,d_2,\ldots,d_n\) (\(1\leq d_i\leq n\)) β the degree of each vertex.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, if there exist a possible flower-like pseudotree: Print ""Yes"" (without quotes) in the first line. Then, output \(n\) lines, in each line print two integers \(u_i\) and \(v_i\) β the two vertices that the \(i\)-th edge connects. If there are multiple answers, you may output any of them.Otherwise, print ""No"" (without quotes) in the only line of output.You can output the first line of each test case in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, the only possible flower-like psuedotree is: After deleting all edges on the cycle in the pseudotree, each tree has depth \(0\).In the second test case, it can be proven that there's no such flower-like psuedotree.In the third test case, one of the possible flower-like psuedotrees is:
|
Input: 632 2 241 2 3 474 3 3 1 1 1 161 1 2 2 3 3101 1 5 2 1 1 1 1 1 691 1 3 1 1 4 1 1 5 | Output: Yes 1 2 2 3 3 1 No Yes 1 2 2 3 3 1 1 4 1 5 2 6 3 7 Yes 5 6 6 5 1 3 2 4 3 5 4 6 No Yes 3 6 6 9 9 3 1 3 2 6 4 6 5 9 7 9 8 9
|
Master
| 5 | 1,279 | 477 | 559 | 18 |
963 |
C
|
963C
|
C. Cutting Rectangle
| 2,600 |
brute force; math; number theory
|
A rectangle with sides \(A\) and \(B\) is cut into rectangles with cuts parallel to its sides. For example, if \(p\) horizontal and \(q\) vertical cuts were made, \((p + 1) \cdot (q + 1)\) rectangles were left after the cutting. After the cutting, rectangles were of \(n\) different types. Two rectangles are different if at least one side of one rectangle isn't equal to the corresponding side of the other. Note that the rectangle can't be rotated, this means that rectangles \(a \times b\) and \(b \times a\) are considered different if \(a \neq b\).For each type of rectangles, lengths of the sides of rectangles are given along with the amount of the rectangles of this type that were left after cutting the initial rectangle.Calculate the amount of pairs \((A; B)\) such as the given rectangles could be created by cutting the rectangle with sides of lengths \(A\) and \(B\). Note that pairs \((A; B)\) and \((B; A)\) are considered different when \(A \neq B\).
|
The first line consists of a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^{5}\)) β amount of different types of rectangles left after cutting the initial rectangle.The next \(n\) lines each consist of three integers \(w_{i}, h_{i}, c_{i}\) \((1 \leq w_{i}, h_{i}, c_{i} \leq 10^{12})\) β the lengths of the sides of the rectangles of this type and the amount of the rectangles of this type.It is guaranteed that the rectangles of the different types are different.
|
Output one integer β the answer to the problem.
|
In the first sample there are three suitable pairs: \((1; 9)\), \((3; 3)\) and \((9; 1)\).In the second sample case there are 6 suitable pairs: \((2; 220)\), \((4; 110)\), \((8; 55)\), \((10; 44)\), \((20; 22)\) and \((40; 11)\).Here the sample of cut for \((20; 22)\). The third sample has no suitable pairs.
|
Input: 11 1 9 | Output: 3
|
Expert
| 3 | 967 | 468 | 47 | 9 |
414 |
D
|
414D
|
D. Mashmokh and Water Tanks
| 2,300 |
binary search; data structures; greedy; trees; two pointers
|
Mashmokh is playing a new game. In the beginning he has k liters of water and p coins. Additionally he has a rooted tree (an undirected connected acyclic graph) that consists of m vertices. Each vertex of the tree contains a water tank that is empty in the beginning.The game begins with the fact that Mashmokh chooses some (no more than k) of these tanks (except the root) and pours into each of them exactly 1 liter of water. Then the following process is performed until there is no water remained in tanks. The process consists of several steps. At the beginning of each step Mashmokh opens doors of all tanks. Then Mashmokh closes doors of some tanks (he is not allowed to close door of tank in the root) for the duration of this move. Let's denote the number of liters in some tank with closed door as w, Mashmokh pays w coins for the closing of that tank during this move. Let's denote by x1, x2, ..., xm as the list of vertices of the tree sorted (nondecreasing) by their depth. The vertices from this list should be considered one by one in the order. Firstly vertex x1 (which is the root itself) is emptied. Then for each vertex xi (i > 1), if its door is closed then skip the vertex else move all the water from the tank of vertex xi to the tank of its father (even if the tank of the father is closed). Suppose l moves were made until the tree became empty. Let's denote the amount of water inside the tank of the root after the i-th move by wi then Mashmokh will win max(w1, w2, ..., wl) dollars. Mashmokh wanted to know what is the maximum amount of dollars he can win by playing the above game. He asked you to find this value for him.
|
The first line of the input contains three space-separated integers m, k, p (2 β€ m β€ 105; 0 β€ k, p β€ 109). Each of the following m - 1 lines contains two space-separated integers ai, bi (1 β€ ai, bi β€ m; ai β bi) β the edges of the tree.Consider that the vertices of the tree are numbered from 1 to m. The root of the tree has number 1.
|
Output a single integer, the number Mashmokh asked you to find.
|
The tree in the first sample is shown on the picture below. The black, red, blue colors correspond to vertices with 0, 1, 2 liters of water.One way to achieve the maximum amount of money is to put 1 liter of water in each of vertices 3 and 4. The beginning state is shown on the picture below.Then in the first move Mashmokh will pay one token to close the door of the third vertex tank. The tree after the first move is shown on the picture below.After the second move there are 2 liters of water in the root as shown on the picture below.
|
Input: 10 2 11 21 33 43 52 66 86 79 88 10 | Output: 2
|
Expert
| 5 | 1,650 | 335 | 63 | 4 |
1,725 |
E
|
1725E
|
E. Electrical Efficiency
| 2,500 |
combinatorics; data structures; dp; math; number theory; trees
|
In the country of Dengkleknesia, there are \(N\) factories numbered from \(1\) to \(N\). Factory \(i\) has an electrical coefficient of \(A_i\). There are also \(N-1\) power lines with the \(j\)-th power line connecting factory \(U_j\) and factory \(V_j\). It can be guaranteed that each factory in Dengkleknesia is connected to all other factories in Dengkleknesia through one or more power lines. In other words, the collection of factories forms a tree. Each pair of different factories in Dengkleknesia can use one or more existing power lines to transfer electricity to each other. However, each power line needs to be turned on first so that electricity can pass through it.Define \(f(x, y, z)\) as the minimum number of power lines that need to be turned on so that factory \(x\) can make electrical transfers to factory \(y\) and factory \(z\). Also define \(g(x, y, z)\) as the number of distinct prime factors of \(\text{GCD}(A_x, A_y, A_z)\).To measure the electrical efficiency, you must find the sum of \(f(x, y, z) \times g(x, y, z)\) for all combinations of \((x, y, z)\) such that \(1 \leq x < y < z \leq N\). Because the answer can be very large, you just need to output the answer modulo \(998\,244\,353\).Note: \(\text{GCD}(k_1, k_2, k_3)\) is the greatest common divisor of \(k_1\), \(k_2\), and \(k_3\), which is the biggest integer that simultaneously divides \(k_1\), \(k_2\), and \(k_3\).
|
The first line contains a single integer \(N\) (\(1 \le N \le 2 \cdot 10^5\)) β the number of factories in Dengkleknesia.The second line contains \(N\) integers \(A_1, A_2, \dots, A_N\) (\(1 \leq A_i \leq 2 \cdot 10^5\)) β the electrical coefficients of the factories.The \(j\)-th of the next \(N-1\) lines contains two integers \(U_j\) and \(V_j\) (\(1 \le U_j, V_j \le N\)) β a power line that connects cities \(U_j\) and \(V_j\). The collection of factories forms a tree.
|
An integer representing the sum of \(f(x, y, z) \times g(x, y, z)\) for all combinations of \((x, y, z)\) such that \(1 \leq x < y < z \leq N\), modulo \(998\,244\,353\)
|
In the first example, the only \((x, y, z)\) possible is \((1, 2, 3)\). Because \(\text{GCD}(A_1, A_2, A_3) = \text{GCD}(1, 2, 3) = 1\) has \(0\) distinct prime factors, therefore \(f(x, y, z) \times g(x, y, z) = 2 \times 0 = 0\).In the second example, all triples \((x, y, z)\) that satisfy the condition are as follows: \((1, 2, 3) \rightarrow f(1, 2, 3) \times g(1, 2, 3) = 2 \times 1 = 2\) \((1, 2, 4) \rightarrow f(1, 2, 4) \times g(1, 2, 4) = 2 \times 1 = 2\) \((1, 3, 4) \rightarrow f(1, 3, 4) \times g(1, 3, 4) = 3 \times 2 = 6\) \((2, 3, 4) \rightarrow f(2, 3, 4) \times g(2, 3, 4) = 2 \times 1 = 2\) So the electrical efficiency is \(2 + 2 + 6 + 2 = 12\).
|
Input: 3 1 2 3 1 2 2 3 | Output: 0
|
Expert
| 6 | 1,412 | 474 | 169 | 17 |
25 |
E
|
25E
|
E. Test
| 2,200 |
hashing; strings
|
Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings β input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal length of test, which couldn't be passed by all three Bob's solutions?
|
There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.
|
Output one number β what is minimal length of the string, containing s1, s2 and s3 as substrings.
|
Input: abbccd | Output: 4
|
Hard
| 2 | 585 | 192 | 97 | 0 |
|
990 |
D
|
990D
|
D. Graph And Its Complement
| 1,700 |
constructive algorithms; graphs; implementation
|
Given three numbers \(n, a, b\). You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to \(a\), and the number of components in its complement is \(b\). The matrix must be symmetric, and all digits on the main diagonal must be zeroes.In an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.The adjacency matrix of an undirected graph is a square matrix of size \(n\) consisting only of ""0"" and ""1"", where \(n\) is the number of vertices of the graph and the \(i\)-th row and the \(i\)-th column correspond to the \(i\)-th vertex of the graph. The cell \((i,j)\) of the adjacency matrix contains \(1\) if and only if the \(i\)-th and \(j\)-th vertices in the graph are connected by an edge.A connected component is a set of vertices \(X\) such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to \(X\) violates this rule.The complement or inverse of a graph \(G\) is a graph \(H\) on the same vertices such that two distinct vertices of \(H\) are adjacent if and only if they are not adjacent in \(G\).
|
In a single line, three numbers are given \(n, a, b \,(1 \le n \le 1000, 1 \le a, b \le n)\): is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement.
|
If there is no graph that satisfies these constraints on a single line, print ""NO"" (without quotes).Otherwise, on the first line, print ""YES""(without quotes). In each of the next \(n\) lines, output \(n\) digits such that \(j\)-th digit of \(i\)-th line must be \(1\) if and only if there is an edge between vertices \(i\) and \(j\) in \(G\) (and \(0\) otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes. If there are several matrices that satisfy the conditions β output any of them.
|
Input: 3 1 2 | Output: YES001001110
|
Medium
| 3 | 1,237 | 261 | 540 | 9 |
|
1,140 |
G
|
1140G
|
G. Double Tree
| 2,700 |
data structures; divide and conquer; shortest paths; trees
|
You are given a special undirected graph. It consists of \(2n\) vertices numbered from \(1\) to \(2n\). The following properties hold for the graph: there are exactly \(3n-2\) edges in the graph: \(n\) edges connect vertices having odd numbers with vertices having even numbers, \(n - 1\) edges connect vertices having odd numbers with each other, and \(n - 1\) edges connect vertices having even numbers with each other; for each edge \((u, v)\) between a pair of vertices with odd numbers, there exists an edge \((u + 1, v + 1)\), and vice versa; for each odd number \(u \in [1, 2n - 1]\), there exists an edge \((u, u + 1)\); the graph is connected; moreover, if we delete all vertices with even numbers from it, and all edges incident to them, the graph will become a tree (the same applies to deleting odd vertices).So, the graph can be represented as two trees having the same structure, and \(n\) edges connecting each vertex of the first tree to the corresponding vertex of the second tree.Edges of the graph are weighted. The length of some simple path in the graph is the sum of weights of traversed edges.You are given \(q\) queries to this graph; in each query, you are asked to compute the length of the shortest path between some pair of vertices in this graph. Can you answer all of the queries?
|
The first line of the input contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)).The second line contains \(n\) integers \(w_{1, 2}\), \(w_{3,4}\), ..., \(w_{2n - 1, 2n}\) (\(1 \le w_{i, i + 1} \le 10^{12}\)). These integers describe the weights of the edges connecting odd vertices with even ones.Then \(n-1\) lines follow. \(i\)-th line contains four integers \(x_i\), \(y_i\), \(w_{i, 1}\) and \(w_{i, 2}\) (\(1 \le x_i, y_i \le n\), \(x_i \ne y_i\), \(1 \le w_{i, j} \le 10^{12}\)); it describes two edges: one connecting \(2x_i - 1\) with \(2y_i - 1\) and having weight \(w_{i, 1}\); another connecting \(2x_i\) with \(2y_i\) and having weight \(w_{i, 2}\).The next line contains one integer \(q\) (\(1 \le q \le 6 \cdot 10^5\)) β the number of queries.Then \(q\) lines follow, \(i\)-th line contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le 2n\), \(u_i \ne v_i\)), describing a query ""compute the length of the shortest path between vertices \(u_i\) and \(v_i\)"".
|
Print \(q\) integers, \(i\)-th integer should be equal to the answer to the \(i\)-th query.
|
The graph in the first test looks like that:
|
Input: 5 3 6 15 4 8 1 2 5 4 2 3 5 7 1 4 1 5 1 5 2 1 3 1 2 5 6 1 10 | Output: 3 15 4
|
Master
| 4 | 1,310 | 990 | 91 | 11 |
699 |
A
|
699A
|
A. Launch of Collider
| 1,000 |
implementation
|
There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.You know the direction of each particle movement β it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.
|
The first line contains the positive integer n (1 β€ n β€ 200 000) β the number of particles. The second line contains n symbols ""L"" and ""R"". If the i-th symbol equals ""L"", then the i-th particle will move to the left, otherwise the i-th symbol equals ""R"" and the i-th particle will move to the right.The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 β€ xi β€ 109) β the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.
|
In the first line print the only integer β the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen.
|
In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.
|
Input: 4RLRL2 4 6 10 | Output: 1
|
Beginner
| 1 | 1,152 | 567 | 223 | 6 |
1,213 |
E
|
1213E
|
E. Two Small Strings
| 1,900 |
brute force; constructive algorithms
|
You are given two strings \(s\) and \(t\) both of length \(2\) and both consisting only of characters 'a', 'b' and 'c'.Possible examples of strings \(s\) and \(t\): ""ab"", ""ca"", ""bb"".You have to find a string \(res\) consisting of \(3n\) characters, \(n\) characters should be 'a', \(n\) characters should be 'b' and \(n\) characters should be 'c' and \(s\) and \(t\) should not occur in \(res\) as substrings.A substring of a string is a contiguous subsequence of that string. So, the strings ""ab"", ""ac"" and ""cc"" are substrings of the string ""abacc"", but the strings ""bc"", ""aa"" and ""cb"" are not substrings of the string ""abacc"".If there are multiple answers, you can print any of them.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 10^5\)) β the number of characters 'a', 'b' and 'c' in the resulting string.The second line of the input contains one string \(s\) of length \(2\) consisting of characters 'a', 'b' and 'c'.The third line of the input contains one string \(t\) of length \(2\) consisting of characters 'a', 'b' and 'c'.
|
If it is impossible to find the suitable string, print ""NO"" on the first line. Otherwise print ""YES"" on the first line and string \(res\) on the second line. \(res\) should consist of \(3n\) characters, \(n\) characters should be 'a', \(n\) characters should be 'b' and \(n\) characters should be 'c' and \(s\) and \(t\) should not occur in \(res\) as substrings.If there are multiple answers, you can print any of them.
|
Input: 2 ab bc | Output: YES acbbac
|
Hard
| 2 | 707 | 371 | 424 | 12 |
|
1,928 |
D
|
1928D
|
D. Lonely Mountain Dungeons
| 1,900 |
brute force; data structures; greedy; math; ternary search
|
Once, the people, elves, dwarves, and other inhabitants of Middle-earth gathered to reclaim the treasures stolen from them by Smaug. In the name of this great goal, they rallied around the powerful elf Timothy and began to plan the overthrow of the ruler of the Lonely Mountain.The army of Middle-earth inhabitants will consist of several squads. It is known that each pair of creatures of the same race, which are in different squads, adds \(b\) units to the total strength of the army. But since it will be difficult for Timothy to lead an army consisting of a large number of squads, the total strength of an army consisting of \(k\) squads is reduced by \((k - 1) \cdot x\) units. Note that the army always consists of at least one squad.It is known that there are \(n\) races in Middle-earth, and the number of creatures of the \(i\)-th race is equal to \(c_i\). Help the inhabitants of Middle-earth determine the maximum strength of the army they can assemble.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(b\), and \(x\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le b \le 10^6, 0 \le x \le 10^9\)) β the number of races and the constants \(b\) and \(x\) described above.The second line of each test case contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le 2 \cdot 10^5\)) β the number of creatures of each of the \(n\) races.It is guaranteed that the sum of the values \(c_1 + c_2 + \ldots + c_n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the maximum strength of the army that the inhabitants of Middle-earth can assemble.
|
In the first test case, the inhabitants of Middle-earth can form \(3\) squads. Since \(x = 0\), the army's strength will not decrease due to the number of squads. The inhabitants can be distributed among the squads as follows: The single representative of the first species can be sent to the first squad. The first representative of the second species can be sent to the first squad, the second representative of the second species can be sent to the second squad. Then the total strength of the army will increase by \(b = 1\). The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by \(3 \cdot b = 3\), as they form three pairs in different squads.Thus, the total strength of the army is \(4\).In the second test case, the inhabitants of Middle-earth can form \(3\) squads. Since \(x = 10\), the army's strength will decrease by \(20\). The inhabitants can be distributed among the squads as follows: The first representative of the first species can be sent to the first squad, the second representative of the first species can be sent to the second squad. Then the total strength of the army will increase by \(b = 5\). The first and second representatives of the second species can be sent to the first squad, the third and fourth representatives of the second species can be sent to the second squad, the fifth representative of the second species can be sent to the third squad. Then the total strength of the army will increase by \(8 \cdot b = 40\). The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by \(3 \cdot b = 15\), as they form three pairs in different squads.Thus, the total strength of the army is \(5 + 40 + 15 - 20 = 40\).
|
Input: 53 1 01 2 33 5 102 5 34 3 33 2 1 24 1 04 1 4 24 1 104 1 4 2 | Output: 4 40 9 13 0
|
Hard
| 5 | 966 | 711 | 129 | 19 |
1,608 |
G
|
1608G
|
G. Alphabetic Tree
| 3,500 |
binary search; data structures; dfs and similar; hashing; string suffix structures; strings; trees
|
You are given \(m\) strings and a tree on \(n\) nodes. Each edge has some letter written on it.You have to answer \(q\) queries. Each query is described by \(4\) integers \(u\), \(v\), \(l\) and \(r\). The answer to the query is the total number of occurrences of \(str(u,v)\) in strings with indices from \(l\) to \(r\). \(str(u,v)\) is defined as the string that is made by concatenating letters written on the edges on the shortest path from \(u\) to \(v\) (in order that they are traversed).
|
The first line of the input contains three integers \(n\), \(m\) and \(q\) (\(2 \le n \le 10^5\), \(1 \le m,q \le 10^5\)).The \(i\)-th of the following \(n-1\) lines contains two integers \(u_i, v_i\) and a lowercase Latin letter \(c_i\) (\(1 \le u_i, v_i \le n\), \(u_i \neq v_i\)), denoting the edge between nodes \(u_i, v_i\) with a character \(c_i\) on it. It's guaranteed that these edges form a tree.The following \(m\) lines contain the strings consisting of lowercase Latin letters. The total length of those strings does not exceed \(10^5\).Then \(q\) lines follow, each containing four integers \(u\), \(v\), \(l\) and \(r\) (\(1 \le u,v \le n\), \(u \neq v\), \(1 \le l \le r \le m\)), denoting the queries.
|
For each query print a single integer β the answer to the query.
|
Input: 2 5 3 1 2 a aab abab aaa b a 2 1 1 5 1 2 1 3 2 1 3 5 | Output: 8 7 4
|
Master
| 7 | 495 | 718 | 64 | 16 |
|
2,110 |
D
|
2110D
|
D. Fewer Batteries
| 1,700 |
binary search; dfs and similar; dp; graphs; greedy; hashing
|
In 2077, when robots took over the world, they decided to compete in the following game.There are \(n\) checkpoints, and the \(i\)-th checkpoint contains \(b_i\) batteries. Initially, the Robot starts at the \(1\)-st checkpoint with no batteries and must reach the \(n\)-th checkpoint.There are a total of \(m\) one-way passages between the checkpoints. The \(i\)-th passage allows movement from point \(s_i\) to point \(t_i\) (\(s_i < t_i\)), but not the other way. Additionally, the \(i\)-th passage can only be used if the robot has at least \(w_i\) charged batteries; otherwise, it will run out of power on the way.When the robot arrives at point \(v\), it can additionally take any number of batteries from \(0\) to \(b_v\), inclusive. Moreover, it always carries all previously collected batteries, and at each checkpoint, it recharges all previously collected batteries.Find the minimum number of batteries that the robot can have at the end of the journey, or report that it is impossible to reach from the first checkpoint to the last.
|
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, m\) (\(2 \leq n \leq 2 \cdot 10^5, 0 \leq m \leq 3 \cdot 10^5\)) β the number of checkpoints and the number of passages, respectively.The second line contains \(n\) numbers \(b_i\) (\(0 \leq b_i \leq 10^9\)) β the number of batteries at the \(i\)-th checkpoint.The next \(m\) lines contain three integers \(s_i, t_i, w_i\) (\(1 \leq s_i < t_i \leq n, 1 \leq w_i \leq 10^9\)) β the endpoints of the passage and the minimum number of batteries required to pass through it.It is guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\).It is guaranteed that the sum of \(m\) does not exceed \(3 \cdot 10^5\).
|
For each test case, output the minimum number of batteries that you can have at the end of the journey, or \(-1\) if it is impossible to reach point \(n\).
|
In the first test case, you need to take \(1\) battery at the starting point, then move to point \(2\), and then to point \(3\).In the second test case, you need to take \(2\) batteries at the starting point, then move to point \(2\), take another \(2\) batteries, move to point \(4\), and then to point \(5\).In the third test case, there is no path from point \(1\) to point \(n\).In the fourth test case, you need to take \(1\) battery at the starting point, then move to point \(2\), take another \(9\) batteries, move to point \(3\), and then to point \(4\).
|
Input: 43 32 0 01 2 12 3 11 3 25 62 2 5 0 11 2 21 3 11 4 33 5 52 4 44 5 32 01 14 43 10 0 01 2 11 3 32 3 103 4 5 | Output: 1 4 -1 10
|
Medium
| 6 | 1,044 | 836 | 155 | 21 |
1,210 |
G
|
1210G
|
G. Mateusz and Escape Room
| 3,500 |
dp
|
Mateusz likes to travel! However, on his \(42\)nd visit to Saint Computersburg there is not much left to sightsee. That's why he decided to go to an escape room with his friends!The team has solved all riddles flawlessly. There is only one riddle remaining β a huge circular table! There are \(n\) weighing scales lying on top of the table, distributed along the circle. Each scale is adjacent to exactly two other scales: for each \(i \in \{1, 2, \dots, n-1\}\), the \(i\)-th and the \((i+1)\)-th scales are adjacent to each other, as well as the first and the \(n\)-th scale.The \(i\)-th scale initially contains \(a_i\) heavy coins. Mateusz can perform moves β each move consists of fetching a single coin from one scale and putting it on any adjacent scale.It turns out that the riddle will be solved when there is a specific amount of coins on each of the scales. Specifically, each scale has parameters \(l_i\) and \(r_i\). If each coin lies on a single scale and for each \(i\), the \(i\)-th scale contains at least \(l_i\) and at most \(r_i\) coins, the riddle will be solved and Mateusz's team will win!Mateusz is aiming for the best possible time. Therefore, he wants to solved the riddle as quickly as possible. What is the minimum possible number of moves required to fulfill all the conditions?
|
The first line contains an integer \(n\) (\(3 \le n \le 35\,000\)) β the number of weighing scales in the circle.The following \(n\) lines describe the scales. The \(i\)-th of these lines describes the \(i\)-th scale and consists of three integers \(a_i, l_i, r_i\) (\(0 \le a_i \le 35\,000\), \(0 \le l_i \le r_i \le 35\,000\)).It's guaranteed that the riddle is solvable, that is, \(\sum_{i=1}^n l_i \le \sum_{i=1}^n a_i \le \sum_{i=1}^n r_i\).
|
Output one integer β the minimum number of operations required to solve the riddle.
|
Input: 5 0 2 3 1 2 3 4 3 3 4 3 3 4 3 3 | Output: 4
|
Master
| 1 | 1,307 | 446 | 83 | 12 |
|
468 |
A
|
468A
|
A. 24 Game
| 1,500 |
constructive algorithms; greedy; math
|
Little X used to play a card game called ""24 Game"", but recently he has found it too easy. So he invented a new game.Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote them a and b, erase them from the sequence, and append to the sequence either a + b, or a - b, or a Γ b.After n - 1 steps there is only one number left. Can you make this number equal to 24?
|
The first line contains a single integer n (1 β€ n β€ 105).
|
If it's possible, print ""YES"" in the first line. Otherwise, print ""NO"" (without the quotes).If there is a way to obtain 24 as the result number, in the following n - 1 lines print the required operations an operation per line. Each operation should be in form: ""a op b = c"". Where a and b are the numbers you've picked at this operation; op is either ""+"", or ""-"", or ""*""; c is the result of corresponding operation. Note, that the absolute value of c mustn't be greater than 1018. The result of the last operation must be equal to 24. Separate operator sign and equality sign from numbers with spaces.If there are multiple valid answers, you may print any of them.
|
Input: 1 | Output: NO
|
Medium
| 3 | 425 | 57 | 676 | 4 |
|
23 |
D
|
23D
|
D. Tetragon
| 2,600 |
geometry; math
|
You're given the centers of three equal sides of a strictly convex tetragon. Your task is to restore the initial tetragon.
|
The first input line contains one number T β amount of tests (1 β€ T β€ 5Β·104). Each of the following T lines contains numbers x1, y1, x2, y2, x3, y3 β coordinates of different points that are the centers of three equal sides (non-negative integer numbers, not exceeding 10).
|
For each test output two lines. If the required tetragon exists, output in the first line YES, in the second line β four pairs of numbers β coordinates of the polygon's vertices in clockwise or counter-clockwise order. Don't forget, please, that the tetragon should be strictly convex, i.e. no 3 of its points lie on one line. Output numbers with 9 characters after a decimal point.If the required tetragon doen't exist, output NO in the first line, and leave the second line empty.
|
Input: 31 1 2 2 3 30 1 1 0 2 29 3 7 9 9 8 | Output: NOYES3.5 1.5 0.5 2.5 -0.5 -0.5 2.5 0.5NO
|
Expert
| 2 | 122 | 273 | 482 | 0 |
|
557 |
E
|
557E
|
E. Ann and Half-Palindrome
| 2,300 |
data structures; dp; graphs; string suffix structures; strings; trees
|
Tomorrow Ann takes the hardest exam of programming where she should get an excellent mark. On the last theoretical class the teacher introduced the notion of a half-palindrome. String t is a half-palindrome, if for all the odd positions i () the following condition is held: ti = t|t| - i + 1, where |t| is the length of string t if positions are indexed from 1. For example, strings ""abaa"", ""a"", ""bb"", ""abbbaa"" are half-palindromes and strings ""ab"", ""bba"" and ""aaabaa"" are not.Ann knows that on the exam she will get string s, consisting only of letters a and b, and number k. To get an excellent mark she has to find the k-th in the lexicographical order string among all substrings of s that are half-palyndromes. Note that each substring in this order is considered as many times as many times it occurs in s.The teachers guarantees that the given number k doesn't exceed the number of substrings of the given string that are half-palindromes.Can you cope with this problem?
|
The first line of the input contains string s (1 β€ |s| β€ 5000), consisting only of characters 'a' and 'b', where |s| is the length of string s.The second line contains a positive integer k β the lexicographical number of the requested string among all the half-palindrome substrings of the given string s. The strings are numbered starting from one. It is guaranteed that number k doesn't exceed the number of substrings of the given string that are half-palindromes.
|
Print a substring of the given string that is the k-th in the lexicographical order of all substrings of the given string that are half-palindromes.
|
By definition, string a = a1a2... an is lexicographically less than string b = b1b2... bm, if either a is a prefix of b and doesn't coincide with b, or there exists such i, that a1 = b1, a2 = b2, ... ai - 1 = bi - 1, ai < bi.In the first sample half-palindrome substrings are the following strings β a, a, a, a, aa, aba, abaa, abba, abbabaa, b, b, b, b, baab, bab, bb, bbab, bbabaab (the list is given in the lexicographical order).
|
Input: abbabaab7 | Output: abaa
|
Expert
| 6 | 992 | 467 | 148 | 5 |
761 |
E
|
761E
|
E. Dasha and Puzzle
| 2,000 |
constructive algorithms; dfs and similar; graphs; greedy; trees
|
Dasha decided to have a rest after solving the problem. She had been ready to start her favourite activity β origami, but remembered the puzzle that she could not solve. The tree is a non-oriented connected graph without cycles. In particular, there always are n - 1 edges in a tree with n vertices.The puzzle is to position the vertices at the points of the Cartesian plane with integral coordinates, so that the segments between the vertices connected by edges are parallel to the coordinate axes. Also, the intersection of segments is allowed only at their ends. Distinct vertices should be placed at different points. Help Dasha to find any suitable way to position the tree vertices on the plane.It is guaranteed that if it is possible to position the tree vertices on the plane without violating the condition which is given above, then you can do it by using points with integral coordinates which don't exceed 1018 in absolute value.
|
The first line contains single integer n (1 β€ n β€ 30) β the number of vertices in the tree. Each of next n - 1 lines contains two integers ui, vi (1 β€ ui, vi β€ n) that mean that the i-th edge of the tree connects vertices ui and vi.It is guaranteed that the described graph is a tree.
|
If the puzzle doesn't have a solution then in the only line print ""NO"".Otherwise, the first line should contain ""YES"". The next n lines should contain the pair of integers xi, yi (|xi|, |yi| β€ 1018) β the coordinates of the point which corresponds to the i-th vertex of the tree.If there are several solutions, print any of them.
|
In the first sample one of the possible positions of tree is:
|
Input: 71 21 32 42 53 63 7 | Output: YES0 01 00 12 01 -1-1 10 2
|
Hard
| 5 | 941 | 284 | 333 | 7 |
546 |
E
|
546E
|
E. Soldier and Traveling
| 2,100 |
flows; graphs; math
|
In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th city consists of ai soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.Check if is it possible that after roaming there will be exactly bi soldiers in the i-th city.
|
First line of input consists of two integers n and m (1 β€ n β€ 100, 0 β€ m β€ 200).Next line contains n integers a1, a2, ..., an (0 β€ ai β€ 100).Next line contains n integers b1, b2, ..., bn (0 β€ bi β€ 100).Then m lines follow, each of them consists of two integers p and q (1 β€ p, q β€ n, p β q) denoting that there is an undirected road between cities p and q. It is guaranteed that there is at most one road between each pair of cities.
|
If the conditions can not be met output single word ""NO"".Otherwise output word ""YES"" and then n lines, each of them consisting of n integers. Number in the i-th line in the j-th column should denote how many soldiers should road from city i to city j (if i β j) or how many soldiers should stay in city i (if i = j).If there are several possible answers you may output any of them.
|
Input: 4 41 2 6 33 5 3 11 22 33 44 2 | Output: YES1 0 0 0 2 0 0 0 0 5 1 0 0 0 2 1
|
Hard
| 3 | 393 | 433 | 385 | 5 |
|
54 |
B
|
54B
|
B. Cutting Jigsaw Puzzle
| 1,800 |
hashing; implementation
|
The Hedgehog recently remembered one of his favorite childhood activities, β solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one.Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces.All puzzle pieces turn out to be of the same size X Γ Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A Γ B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size.
|
The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20.Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters.
|
In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly β by the length X.
|
The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
|
Input: 2 4ABDCABDC | Output: 32 1
|
Medium
| 2 | 1,463 | 251 | 473 | 0 |
1,136 |
E
|
1136E
|
E. Nastya Hasn't Written a Legend
| 2,200 |
binary search; data structures
|
In this task, Nastya asked us to write a formal statement.An array \(a\) of length \(n\) and an array \(k\) of length \(n-1\) are given. Two types of queries should be processed: increase \(a_i\) by \(x\). Then if \(a_{i+1} < a_i + k_i\), \(a_{i+1}\) becomes exactly \(a_i + k_i\); again, if \(a_{i+2} < a_{i+1} + k_{i+1}\), \(a_{i+2}\) becomes exactly \(a_{i+1} + k_{i+1}\), and so far for \(a_{i+3}\), ..., \(a_n\); print the sum of the contiguous subarray from the \(l\)-th element to the \(r\)-th element of the array \(a\). It's guaranteed that initially \(a_i + k_i \leq a_{i+1}\) for all \(1 \leq i \leq n-1\).
|
The first line contains a single integer \(n\) (\(2 \leq n \leq 10^{5}\)) β the number of elements in the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^{9} \leq a_i \leq 10^{9}\)) β the elements of the array \(a\).The third line contains \(n-1\) integers \(k_1, k_2, \ldots, k_{n-1}\) (\(-10^{6} \leq k_i \leq 10^{6}\)) β the elements of the array \(k\).The fourth line contains a single integer \(q\) (\(1 \leq q \leq 10^{5}\)) β the number of queries.Each of the following \(q\) lines contains a query of one of two types: if the query has the first type, the corresponding line contains the character '+' (without quotes), and then there are two integers \(i\) and \(x\) (\(1 \leq i \leq n\), \(0 \leq x \leq 10^{6}\)), it means that integer \(x\) is added to the \(i\)-th element of the array \(a\) as described in the statement. if the query has the second type, the corresponding line contains the character 's' (without quotes) and then there are two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq n\)).
|
For each query of the second type print a single integer in a new line β the sum of the corresponding subarray.
|
In the first example: after the first change \(a = [3, 4, 3]\); after the second change \(a = [3, 4, 4]\). In the second example: after the first change \(a = [6, 9, 10]\); after the second change \(a = [6, 13, 14]\).
|
Input: 3 1 2 3 1 -1 5 s 2 3 + 1 2 s 1 2 + 3 1 s 2 3 | Output: 5 7 8
|
Hard
| 2 | 617 | 1,051 | 111 | 11 |
1,442 |
D
|
1442D
|
D. Sum
| 2,800 |
data structures; divide and conquer; dp; greedy
|
You are given \(n\) non-decreasing arrays of non-negative numbers. Vasya repeats the following operation \(k\) times: Selects a non-empty array. Puts the first element of the selected array in his pocket. Removes the first element from the selected array. Vasya wants to maximize the sum of the elements in his pocket.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le n, k \le 3\,000\)): the number of arrays and operations.Each of the next \(n\) lines contain an array. The first integer in each line is \(t_i\) (\(1 \le t_i \le 10^6\)): the size of the \(i\)-th array. The following \(t_i\) integers \(a_{i, j}\) (\(0 \le a_{i, 1} \le \ldots \le a_{i, t_i} \le 10^8\)) are the elements of the \(i\)-th array.It is guaranteed that \(k \le \sum\limits_{i=1}^n t_i \le 10^6\).
|
Print one integer: the maximum possible sum of all elements in Vasya's pocket after \(k\) operations.
|
Input: 3 3 2 5 10 3 1 2 3 2 1 20 | Output: 26
|
Master
| 4 | 318 | 469 | 101 | 14 |
|
1,172 |
E
|
1172E
|
E. Nauuo and ODT
| 3,300 |
data structures
|
Nauuo is a girl who loves traveling.One day she went to a tree, Old Driver Tree, literally, a tree with an old driver on it.The tree is a connected graph consisting of \(n\) nodes and \(n-1\) edges. Each node has a color, and Nauuo will visit the ODT through a simple path on the tree in the old driver's car.Nauuo wants to visit see more different colors in her journey, but she doesn't know which simple path she will be traveling on. So, she wants to calculate the sum of the numbers of different colors on all different paths. Can you help her?What's more, the ODT is being redecorated, so there will be \(m\) modifications, each modification will change a single node's color. Nauuo wants to know the answer after each modification too.Note that in this problem, we consider the simple path from \(u\) to \(v\) and the simple path from \(v\) to \(u\) as two different simple paths if and only if \(u\ne v\).
|
The first line contains two integers \(n\) and \(m\) (\(2\le n\le 4\cdot 10^5\), \(1\le m\le 4\cdot 10^5\)) β the number of nodes and the number of modifications.The second line contains \(n\) integers \(c_1,c_2,\ldots,c_n\) (\(1\le c_i\le n\)), where \(c_i\) is the initial color of node \(i\).Each of the next \(n-1\) lines contains two integers \(u\) and \(v\) (\(1\le u,v\le n\)), denoting there is an edge between \(u\) and \(v\). It is guaranteed that the given edges form a tree.Each of the next \(m\) lines contains two integers \(u\) and \(x\) (\(1\le u,x\le n\)), which means a modification that changes the color of node \(u\) into \(x\).
|
The output contains \(m+1\) integers β the first integer is the answer at the beginning, the rest integers are the answers after every modification in the given order.
|
Example 1The number of colors on each simple path at the beginning:
|
Input: 5 3 1 2 1 2 3 1 2 1 3 3 4 3 5 3 3 4 1 4 3 | Output: 47 51 49 45
|
Master
| 1 | 912 | 649 | 167 | 11 |
1,941 |
A
|
1941A
|
A. Rudolf and the Ticket
| 800 |
brute force; math
|
Rudolf is going to visit Bernard, and he decided to take the metro to get to him. The ticket can be purchased at a machine that accepts exactly two coins, the sum of which does not exceed \(k\).Rudolf has two pockets with coins. In the left pocket, there are \(n\) coins with denominations \(b_1, b_2, \dots, b_n\). In the right pocket, there are \(m\) coins with denominations \(c_1, c_2, \dots, c_m\). He wants to choose exactly one coin from the left pocket and exactly one coin from the right pocket (two coins in total).Help Rudolf determine how many ways there are to select indices \(f\) and \(s\) such that \(b_f + c_s \le k\).
|
The first line contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then follows the description of each test case.The first line of each test case contains three natural numbers \(n\), \(m\), and \(k\) (\(1 \le n, m \le 100, 1 \le k \le 2000\)) β the number of coins in the left and right pockets, and the maximum sum of two coins for the ticket payment at the counter, respectively.The second line of each test case contains \(n\) integers \(b_i\) (\(1 \le b_i \le 1000\)) β the denominations of coins in the left pocket.The third line of each test case contains \(m\) integers \(c_i\) (\(1 \le c_i \le 1000\)) β the denominations of coins in the right pocket.
|
For each testcase, output a single integer β the number of ways Rudolf can select two coins, taking one from each pocket, so that the sum of the coins does not exceed \(k\).
|
Note that the pairs indicate the indices of the coins in the array, not their denominations.In the first test case, Rudolf can choose the following pairs of coins: \([1, 1], [1, 2], [1, 4], [2, 1], [2, 2], [2, 4]\).In the second test case, Rudolf cannot choose one coin from each pocket in any way, as the sum of any two elements from the first and second arrays will exceed the value of \(k=4\).In the third test case, Rudolf can choose: \([1, 1], [2, 1], [3, 1], [4, 1]\).In the fourth test case, Rudolf can choose any coin from the left pocket and any coin from the right pocket.
|
Input: 44 4 81 5 10 142 1 8 12 3 44 81 2 34 2 71 1 1 12 73 4 20001 1 11 1 1 1 | Output: 6 0 4 12
|
Beginner
| 2 | 635 | 685 | 173 | 19 |
1,508 |
A
|
1508A
|
A. Binary Literature
| 1,900 |
constructive algorithms; greedy; implementation; strings; two pointers
|
A bitstring is a string that contains only the characters 0 and 1.Koyomi Kanou is working hard towards her dream of becoming a writer. To practice, she decided to participate in the Binary Novel Writing Contest. The writing prompt for the contest consists of three bitstrings of length \(2n\). A valid novel for the contest is a bitstring of length at most \(3n\) that contains at least two of the three given strings as subsequences.Koyomi has just received the three prompt strings from the contest organizers. Help her write a valid novel for the contest.A string \(a\) is a subsequence of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero) characters.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).Each of the following three lines contains a bitstring of length \(2n\). It is guaranteed that these three strings are pairwise distinct.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(10^5\).
|
For each test case, print a single line containing a bitstring of length at most \(3n\) that has at least two of the given bitstrings as subsequences.It can be proven that under the constraints of the problem, such a bitstring always exists.If there are multiple possible answers, you may output any of them.
|
In the first test case, the bitstrings 00 and 01 are subsequences of the output string: 010 and 010. Note that 11 is not a subsequence of the output string, but this is not required.In the second test case all three input strings are subsequences of the output string: 011001010, 011001010 and 011001010.
|
Input: 2 1 00 11 01 3 011001 111010 010001 | Output: 010 011001010
|
Hard
| 5 | 695 | 408 | 308 | 15 |
1,517 |
B
|
1517B
|
B. Morning Jogging
| 1,200 |
constructive algorithms; greedy; sortings
|
The 2050 volunteers are organizing the ""Run! Chase the Rising Sun"" activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.There are \(n+1\) checkpoints on the trail. They are numbered by \(0\), \(1\), ..., \(n\). A runner must start at checkpoint \(0\) and finish at checkpoint \(n\). No checkpoint is skippable β he must run from checkpoint \(0\) to checkpoint \(1\), then from checkpoint \(1\) to checkpoint \(2\) and so on. Look at the picture in notes section for clarification.Between any two adjacent checkpoints, there are \(m\) different paths to choose. For any \(1\le i\le n\), to run from checkpoint \(i-1\) to checkpoint \(i\), a runner can choose exactly one from the \(m\) possible paths. The length of the \(j\)-th path between checkpoint \(i-1\) and \(i\) is \(b_{i,j}\) for any \(1\le j\le m\) and \(1\le i\le n\).To test the trail, we have \(m\) runners. Each runner must run from the checkpoint \(0\) to the checkpoint \(n\) once, visiting all the checkpoints. Every path between every pair of adjacent checkpoints needs to be ran by exactly one runner. If a runner chooses the path of length \(l_i\) between checkpoint \(i-1\) and \(i\) (\(1\le i\le n\)), his tiredness is $$$\(\min_{i=1}^n l_i,\)\( i. e. the minimum length of the paths he takes.Please arrange the paths of the \)m$$$ runners to minimize the sum of tiredness of them.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n,m \leq 100\)).The \(i\)-th of the next \(n\) lines contains \(m\) integers \(b_{i,1}\), \(b_{i,2}\), ..., \(b_{i,m}\) (\(1 \le b_{i,j} \le 10^9\)).It is guaranteed that the sum of \(n\cdot m\) over all test cases does not exceed \(10^4\).
|
For each test case, output \(n\) lines. The \(j\)-th number in the \(i\)-th line should contain the length of the path that runner \(j\) chooses to run from checkpoint \(i-1\) to checkpoint \(i\). There should be exactly \(m\) integers in the \(i\)-th line and these integers should form a permuatation of \(b_{i, 1}\), ..., \(b_{i, m}\) for all \(1\le i\le n\).If there are multiple answers, print any.
|
In the first case, the sum of tiredness is \(\min(2,5) + \min(3,3) + \min(4,1) = 6\). In the second case, the sum of tiredness is \(\min(2,4,3) + \min(3,1,5) = 3\).
|
Input: 2 2 3 2 3 4 1 3 5 3 2 2 3 4 1 3 5 | Output: 2 3 4 5 3 1 2 3 4 1 3 5
|
Easy
| 3 | 1,401 | 481 | 403 | 15 |
643 |
A
|
643A
|
A. Bear and Colors
| 1,500 |
implementation
|
Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color ti.For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.There are non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant.
|
The first line of the input contains a single integer n (1 β€ n β€ 5000) β the number of balls.The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ n) where ti is the color of the i-th ball.
|
Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color.
|
In the first sample, color 2 is dominant in three intervals: An interval [2, 2] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4, 4] contains one ball, with color 2 again. An interval [2, 4] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and color 1 is dominant in all of them.
|
Input: 41 2 1 2 | Output: 7 3 0 0
|
Medium
| 1 | 588 | 197 | 106 | 6 |
143 |
A
|
143A
|
A. Help Vasilisa the Wise 2
| 1,000 |
brute force; math
|
Vasilisa the Wise from the Kingdom of Far Far Away got a magic box with a secret as a present from her friend Hellawisa the Wise from the Kingdom of A Little Closer. However, Vasilisa the Wise does not know what the box's secret is, since she cannot open it again. She hopes that you will help her one more time with that.The box's lock looks as follows: it contains 4 identical deepenings for gems as a 2 Γ 2 square, and some integer numbers are written at the lock's edge near the deepenings. The example of a lock is given on the picture below. The box is accompanied with 9 gems. Their shapes match the deepenings' shapes and each gem contains one number from 1 to 9 (each number is written on exactly one gem). The box will only open after it is decorated with gems correctly: that is, each deepening in the lock should be filled with exactly one gem. Also, the sums of numbers in the square's rows, columns and two diagonals of the square should match the numbers written at the lock's edge. For example, the above lock will open if we fill the deepenings with gems with numbers as is shown on the picture below. Now Vasilisa the Wise wants to define, given the numbers on the box's lock, which gems she should put in the deepenings to open the box. Help Vasilisa to solve this challenging task.
|
The input contains numbers written on the edges of the lock of the box. The first line contains space-separated integers r1 and r2 that define the required sums of numbers in the rows of the square. The second line contains space-separated integers c1 and c2 that define the required sums of numbers in the columns of the square. The third line contains space-separated integers d1 and d2 that define the required sums of numbers on the main and on the side diagonals of the square (1 β€ r1, r2, c1, c2, d1, d2 β€ 20). Correspondence between the above 6 variables and places where they are written is shown on the picture below. For more clarifications please look at the second sample test that demonstrates the example given in the problem statement.
|
Print the scheme of decorating the box with stones: two lines containing two space-separated integers from 1 to 9. The numbers should be pairwise different. If there is no solution for the given lock, then print the single number ""-1"" (without the quotes).If there are several solutions, output any.
|
Pay attention to the last test from the statement: it is impossible to open the box because for that Vasilisa the Wise would need 4 identical gems containing number ""5"". However, Vasilisa only has one gem with each number from 1 to 9.
|
Input: 3 74 65 5 | Output: 1 23 4
|
Beginner
| 2 | 1,301 | 750 | 301 | 1 |
163 |
D
|
163D
|
D. Large Refrigerator
| 2,900 |
brute force
|
Vasya wants to buy a new refrigerator. He believes that a refrigerator should be a rectangular parallelepiped with integer edge lengths. Vasya calculated that for daily use he will need a refrigerator with volume of at least V. Moreover, Vasya is a minimalist by nature, so the volume should be no more than V, either β why take up extra space in the apartment? Having made up his mind about the volume of the refrigerator, Vasya faced a new challenge β for a fixed volume of V the refrigerator must have the minimum surface area so that it is easier to clean.The volume and the surface area of a refrigerator with edges a, b, c are equal to V = abc and S = 2(ab + bc + ca), correspondingly.Given the volume V, help Vasya find the integer lengths for the refrigerator's edges a, b, c so that the refrigerator's volume equals V and its surface area S is minimized.
|
The first line contains a single integer t (1 β€ t β€ 500) β the number of data sets.The description of t data sets follows. Each set consists of a single integer V (2 β€ V β€ 1018), given by its factorization as follows.Let V = p1a1p2a2... pkak, where pi are different prime numbers and ai are positive integer powers. Then the first line describing a data set contains a single positive integer k β the number of different prime divisors of V. Next k lines contain prime numbers pi and their powers ai, separated by spaces. All pi are different, all ai > 0.
|
Print t lines, on the i-th line print the answer to the i-th data set as four space-separated integers: the minimum possible surface area S and the corresponding edge lengths a, b, c. If there are multiple variants of the lengths of edges that give the minimum area, you are allowed to print any of them. You can print the lengths of the fridge's edges in any order.
|
In the first data set of the sample the fridge's volume V = 23 = 8, and the minimum surface area will be produced by the edges of equal length.In the second data set the volume V = 17, and it can be produced by only one set of integer lengths.
|
Input: 312 3117 133 12 35 1 | Output: 24 2 2 270 1 1 17148 4 6 5
|
Master
| 1 | 863 | 555 | 366 | 1 |
2,000 |
F
|
2000F
|
F. Color Rows and Columns
| 1,900 |
dp; greedy; implementation; math
|
You have \(n\) rectangles, the \(i\)-th of which has a width of \(a_i\) and a height of \(b_i\).You can perform the following operation an unlimited number of times: choose a rectangle and a cell in it, and then color it.Each time you completely color any row or column, you earn \(1\) point. Your task is to score at least \(k\) points with as few operations as possible.Suppose you have a rectangle with a width of \(6\) and a height of \(3\). You can score \(4\) points by coloring all the cells in any \(4\) columns, thus performing \(12\) operations.
|
The first line contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The following are the descriptions of the test cases.The first line of each test case description contains two integers \(n\) and \(k\) (\(1 \le n \le 1000, 1 \le k \le 100\)) β the number of rectangles in the case and the required number of points.The next \(n\) lines contain the descriptions of the rectangles. The \(i\)-th line contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 100\)) β the width and height of the \(i\)-th rectangle.It is guaranteed that the sum of the values of \(n\) across all test cases does not exceed \(1000\).
|
For each test case, output a single integer β the minimum number of operations required to score at least \(k\) points. If it is impossible to score at least \(k\) points, output -1.
|
Input: 71 46 31 54 45 101 11 11 11 11 12 1001 25 63 112 23 34 43 259 24 38 104 185 48 58 36 2 | Output: 12 14 5 -1 17 80 35
|
Hard
| 4 | 555 | 643 | 182 | 20 |
|
1,490 |
A
|
1490A
|
A. Dense Array
| 800 |
greedy; math
|
Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any \(i\) (\(1 \le i \le n-1\)), this condition must be satisfied: $$$\(\frac{\max(a[i], a[i+1])}{\min(a[i], a[i+1])} \le 2\)\(For example, the arrays \)[1, 2, 3, 4, 3]\(, \)[1, 1, 1]\( and \)[5, 10]\( are dense. And the arrays \)[5, 11]\(, \)[1, 4, 2]\(, \)[6, 6, 1]\( are not dense.You are given an array \)a\( of \)n\( integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added.For example, if \)a=[4,2,10,1]\(, then the answer is \)5\(, and the array itself after inserting elements into it may look like this: \)a=[4,2,\underline{\textbf{3}},\underline{\textbf{5}},10,\underline{\textbf{6}},\underline{\textbf{4}},\underline{\textbf{2}},1]\( (there are other ways to build such \)a$$$).
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(2 \le n \le 50\)) β the length of the array \(a\).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 50\)).
|
For each test case, output one integer β the minimum number of numbers that must be added to the array to make it dense.
|
The first test case is explained in the statements.In the second test case, you can insert one element, \(a=[1,\underline{\textbf{2}},3]\).In the third test case, you can insert two elements, \(a=[6,\underline{\textbf{4}},\underline{\textbf{2}},1]\).In the fourth test case, you can insert one element, \(a=[1,\underline{\textbf{2}},4,2]\).In the fifth test case, the array \(a\) is already dense.
|
Input: 6 4 4 2 10 1 2 1 3 2 6 1 3 1 4 2 5 1 2 3 4 3 12 4 31 25 50 30 20 34 46 42 16 15 16 | Output: 5 1 2 1 0 3
|
Beginner
| 2 | 972 | 295 | 120 | 14 |
1,843 |
F2
|
1843F2
|
F2. Omsk Metro (hard version)
| 2,300 |
data structures; dfs and similar; divide and conquer; dp; math; trees
|
This is the hard version of the problem. The only difference between the simple and hard versions is that in this version \(u\) can take any possible value.As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and between any two stations there is exactly one path that passes through each of the tunnels no more than once. In other words, the metro is a tree.To develop the metro and attract residents, the following system is used in Omsk. Each station has its own weight \(x \in \{-1, 1\}\). If the station has a weight of \(-1\), then when the station is visited by an Omsk resident, a fee of \(1\) burle is charged. If the weight of the station is \(1\), then the Omsk resident is rewarded with \(1\) burle.Omsk Metro currently has only one station with number \(1\) and weight \(x = 1\). Every day, one of the following events occurs: A new station with weight \(x\) is added to the station with number \(v_i\), and it is assigned a number that is one greater than the number of existing stations. Alex, who lives in Omsk, wonders: is there a subsegment\(\dagger\) (possibly empty) of the path between vertices \(u\) and \(v\) such that, by traveling along it, exactly \(k\) burles can be earned (if \(k < 0\), this means that \(k\) burles will have to be spent on travel). In other words, Alex is interested in whether there is such a subsegment of the path that the sum of the weights of the vertices in it is equal to \(k\). Note that the subsegment can be empty, and then the sum is equal to \(0\). You are a friend of Alex, so your task is to answer Alex's questions.\(\dagger\)Subsegment β continuous sequence of elements.
|
The first line contains a single number \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains the number \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of events.Then there are \(n\) lines describing the events. In the \(i\)-th line, one of the following options is possible: First comes the symbol ""+"" (without quotes), then two numbers \(v_i\) and \(x_i\) (\(x_i \in \{-1, 1\}\), it is also guaranteed that the vertex with number \(v_i\) exists). In this case, a new station with weight \(x_i\) is added to the station with number \(v_i\). First comes the symbol ""?"" (without quotes), and then three numbers \(u_i\), \(v_i\), and \(k_i\) (\(-n \le k_i \le n\)). It is guaranteed that the vertices with numbers \(u_i\) and \(v_i\) exist. In this case, it is necessary to determine whether there is a subsegment (possibly empty) of the path between stations \(u_i\) and \(v_i\) with a sum of weights exactly equal to \(k_i\). It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each of Alex's questions, output ""Yes"" (without quotes) if the subsegment described in the condition exists, otherwise output ""No"" (without quotes).You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
|
Explanation of the first sample.The answer to the second question is ""Yes"", because there is a path \(1\).In the fourth question, we can choose the \(1\) path again.In the fifth query, the answer is ""Yes"", since there is a path \(1-3\).In the sixth query, we can choose an empty path because the sum of the weights on it is \(0\).It is not difficult to show that there are no paths satisfying the first and third queries.
|
Input: 18+ 1 -1? 1 1 2? 1 2 1+ 1 1? 1 3 -1? 1 1 1? 1 3 2? 1 1 0 | Output: NO YES NO YES YES YES
|
Expert
| 6 | 1,761 | 1,073 | 299 | 18 |
254 |
C
|
254C
|
C. Anagram
| 1,800 |
greedy; strings
|
String x is an anagram of string y, if we can rearrange the letters in string x and get exact string y. For example, strings ""DOG"" and ""GOD"" are anagrams, so are strings ""BABA"" and ""AABB"", but strings ""ABBAC"" and ""CAABA"" are not.You are given two strings s and t of the same length, consisting of uppercase English letters. You need to get the anagram of string t from string s. You are permitted to perform the replacing operation: every operation is replacing some character from the string s by any other character. Get the anagram of string t in the least number of replacing operations. If you can get multiple anagrams of string t in the least number of operations, get the lexicographically minimal one.The lexicographic order of strings is the familiar to us ""dictionary"" order. Formally, the string p of length n is lexicographically smaller than string q of the same length, if p1 = q1, p2 = q2, ..., pk - 1 = qk - 1, pk < qk for some k (1 β€ k β€ n). Here characters in the strings are numbered from 1. The characters of the strings are compared in the alphabetic order.
|
The input consists of two lines. The first line contains string s, the second line contains string t. The strings have the same length (from 1 to 105 characters) and consist of uppercase English letters.
|
In the first line print z β the minimum number of replacement operations, needed to get an anagram of string t from string s. In the second line print the lexicographically minimum anagram that could be obtained in z operations.
|
The second sample has eight anagrams of string t, that can be obtained from string s by replacing exactly two letters: ""ADBADC"", ""ADDABC"", ""CDAABD"", ""CDBAAD"", ""CDBADA"", ""CDDABA"", ""DDAABC"", ""DDBAAC"". These anagrams are listed in the lexicographical order. The lexicographically minimum anagram is ""ADBADC"".
|
Input: ABACBA | Output: 1ABC
|
Medium
| 2 | 1,093 | 203 | 228 | 2 |
1,764 |
E
|
1764E
|
E. Doremy's Number Line
| 2,400 |
dp; greedy; sortings
|
Doremy has two arrays \(a\) and \(b\) of \(n\) integers each, and an integer \(k\).Initially, she has a number line where no integers are colored. She chooses a permutation \(p\) of \([1,2,\ldots,n]\) then performs \(n\) moves. On the \(i\)-th move she does the following: Pick an uncolored integer \(x\) on the number line such that either: \(x \leq a_{p_i}\); or there exists a colored integer \(y\) such that \(y \leq a_{p_i}\) and \(x \leq y+b_{p_i}\). Color integer \(x\) with color \(p_i\). Determine if the integer \(k\) can be colored with color \(1\).
|
The input 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 contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(1 \le k \le 10^9\)).Each of the following \(n\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i,b_i \le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output ""YES"" (without quotes) if the point \(k\) can be colored with color \(1\). Otherwise, output ""NO"" (without quotes).You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive response).
|
For the first test case, it is impossible to color point \(16\) with color \(1\).For the second test case, \(p=[2,1,3,4]\) is one possible choice, the detail is shown below. On the first move, pick \(x=8\) and color it with color \(2\) since \(x=8\) is uncolored and \(x \le a_2\). On the second move, pick \(x=16\) and color it with color \(1\) since there exists a colored point \(y=8\) such that \(y\le a_1\) and \(x \le y + b_1\). On the third move, pick \(x=0\) and color it with color \(3\) since \(x=0\) is uncolored and \(x \le a_3\). On the forth move, pick \(x=-2\) and color it with color \(4\) since \(x=-2\) is uncolored and \(x \le a_4\). In the end, point \(-2,0,8,16\) are colored with color \(4,3,2,1\), respectively. For the third test case, \(p=[2,1,4,3]\) is one possible choice.For the fourth test case, \(p=[2,3,4,1]\) is one possible choice.
|
Input: 64 165 38 1210 715 14 168 1210 715 15 34 1610 715 15 38 124 1615 15 38 1210 71 1000000000500000000 5000000002 10000000001 9999999991 1 | Output: NO YES YES YES NO YES
|
Expert
| 3 | 560 | 468 | 286 | 17 |
1,423 |
I
|
1423I
|
I. Lookup Tables
| 3,000 |
bitmasks
|
John has \(Q\) closed intervals of consecutive \(2K\)-bit numbers \([l_i, r_i]\) and one 16-bit value \(v_i\) for each interval. (\(0 \leq i < Q\))John wants to implement a function F that maps \(2K\)-bit numbers to 16-bit numbers in such a way that inputs from each interval are mapped to that interval's value. In other words: $$$\(F(x) = v_i, \; \textrm{for every } 0 \leq i < Q \; \textrm{, and every } x \in [l_i, r_i]\)\( The output of F for other inputs is unimportant.John wants to make his implementation of F fast so he has decided to use lookup tables. A single \)2K\(-bit lookup table would be too large to fit in memory, so instead John plans to use two K-bit lookup tables, LSBTable and MSBTable. His implementation will look like this: \)\( F(x) = \textrm{LSBTable}[\textrm{lowKBits}(x)] \; \& \; \textrm{MSBTable}[\textrm{highKBits}(x)]\)\( In other words it returns the ""bitwise and"" of results of looking up the K least significant bits in LSBTable and the K most significant bits in MSBTable.John needs your help. Given \)K\(, \)Q\( and \)Q\( intervals \)[l_i, r_i]\( and values \)v_i$$$, find any two lookup tables which can implement F or report that such tables don't exist.
|
The first line contains two integers \(K\) and \(Q\) (\( 1 <= K <= 16\), \(1 <= Q <= 2\cdot 10^5\)).Each of the next \(Q\) lines contains three integers \(l_i\), \(r_i\) and \(v_i\). ( \(0 \leq l_i \leq r_i < 2^{2K}\), \(0 \leq v_i < 2^{16}\)).
|
On the first line output ""possible"" (without quotes) if two tables satisfying the conditions exist, or ""impossible"" (without quotes) if they don't exist.If a solution exists, in the next \(2 \cdot 2^K\) lines your program should output all values of the two lookup tables (LSBTable and MSBTable) it found. When there are multiple pairs of tables satisfying the conditions, your program may output any such pair. On lines \(1 + i\) output \(\textrm{LSBTable}[i]\). (\(0 \leq i < 2^K\), \(0 \leq \textrm{LSBTable}[i] < 2^{16}\)).On lines \(1 + 2^K + i\) output \(\textrm{MSBTable}[i]\). (\(0 \leq i < 2^K\), \(0 \leq \textrm{MSBTable}[i] < 2^{16}\)).
|
A closed interval \([a, b]\) includes both a and b.In the first sample, tables \(\textrm{LSBTable} = [1,3]\) and \(\textrm{MSBTable} = [1,3]\) satisfy the conditions: \(F[0] = \textrm{LSBTable}[0] \& \textrm{MSBTable}[0] = 1 \& 1 = 1\), \(F[1] = \textrm{LSBTable}[1] \& \textrm{MSBTable}[0] = 3 \& 1 = 1\), \(F[2] = \textrm{LSBTable}[0] \& \textrm{MSBTable}[1] = 1 \& 3 = 1\), \(F[3] = \textrm{LSBTable}[1] \& \textrm{MSBTable}[1] = 3 \& 3 = 3\).In the second sample, tables \(\textrm{LSBTable} = [3,3,2,2]\) and \(\textrm{MSBTable} = [0,3,0,1]\) satisfy all the conditions.In the third sample there are no two lookup tables which can satisfy the conditions.
|
Input: 1 2 0 2 1 3 3 3 | Output: possible 1 3 1 3
|
Master
| 1 | 1,198 | 244 | 652 | 14 |
1,251 |
A
|
1251A
|
A. Broken Keyboard
| 1,000 |
brute force; strings; two pointers
|
Recently Polycarp noticed that some of the buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains \(26\) buttons (one for each letter of the Latin alphabet). Each button is either working fine or malfunctioning. To check which buttons need replacement, Polycarp pressed some buttons in sequence, and a string \(s\) appeared on the screen. When Polycarp presses a button with character \(c\), one of the following events happened: if the button was working correctly, a character \(c\) appeared at the end of the string Polycarp was typing; if the button was malfunctioning, two characters \(c\) appeared at the end of the string. For example, suppose the buttons corresponding to characters a and c are working correctly, and the button corresponding to b is malfunctioning. If Polycarp presses the buttons in the order a, b, a, c, a, b, a, then the string he is typing changes as follows: a \(\rightarrow\) abb \(\rightarrow\) abba \(\rightarrow\) abbac \(\rightarrow\) abbaca \(\rightarrow\) abbacabb \(\rightarrow\) abbacabba.You are given a string \(s\) which appeared on the screen after Polycarp pressed some buttons. Help Polycarp to determine which buttons are working correctly for sure (that is, this string could not appear on the screen if any of these buttons was malfunctioning).You may assume that the buttons don't start malfunctioning when Polycarp types the string: each button either works correctly throughout the whole process, or malfunctions throughout the whole process.
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the input.Then the test cases follow. Each test case is represented by one line containing a string \(s\) consisting of no less than \(1\) and no more than \(500\) lowercase Latin letters.
|
For each test case, print one line containing a string \(res\). The string \(res\) should contain all characters which correspond to buttons that work correctly in alphabetical order, without any separators or repetitions. If all buttons may malfunction, \(res\) should be empty.
|
Input: 4 a zzaaz ccff cbddbb | Output: a z bc
|
Beginner
| 3 | 1,543 | 282 | 279 | 12 |
|
225 |
A
|
225A
|
A. Dice Tower
| 1,100 |
constructive algorithms; greedy
|
A dice is a cube, its faces contain distinct integers from 1 to 6 as black points. The sum of numbers at the opposite dice faces always equals 7. Please note that there are only two dice (these dices are mirror of each other) that satisfy the given constraints (both of them are shown on the picture on the left). Alice and Bob play dice. Alice has built a tower from n dice. We know that in this tower the adjacent dice contact with faces with distinct numbers. Bob wants to uniquely identify the numbers written on the faces of all dice, from which the tower is built. Unfortunately, Bob is looking at the tower from the face, and so he does not see all the numbers on the faces. Bob sees the number on the top of the tower and the numbers on the two adjacent sides (on the right side of the picture shown what Bob sees).Help Bob, tell whether it is possible to uniquely identify the numbers on the faces of all the dice in the tower, or not.
|
The first line contains a single integer n (1 β€ n β€ 100) β the number of dice in the tower.The second line contains an integer x (1 β€ x β€ 6) β the number Bob sees at the top of the tower. Next n lines contain two space-separated integers each: the i-th line contains numbers ai, bi (1 β€ ai, bi β€ 6; ai β bi) β the numbers Bob sees on the two sidelong faces of the i-th dice in the tower.Consider the dice in the tower indexed from top to bottom from 1 to n. That is, the topmost dice has index 1 (the dice whose top face Bob can see). It is guaranteed that it is possible to make a dice tower that will look as described in the input.
|
Print ""YES"" (without the quotes), if it is possible to to uniquely identify the numbers on the faces of all the dice in the tower. If it is impossible, print ""NO"" (without the quotes).
|
Input: 363 25 42 4 | Output: YES
|
Easy
| 2 | 944 | 634 | 188 | 2 |
|
1,158 |
F
|
1158F
|
F. Density of subarrays
| 3,500 |
dp; math
|
Let \(c\) be some positive integer. Let's call an array \(a_1, a_2, \ldots, a_n\) of positive integers \(c\)-array, if for all \(i\) condition \(1 \leq a_i \leq c\) is satisfied. Let's call \(c\)-array \(b_1, b_2, \ldots, b_k\) a subarray of \(c\)-array \(a_1, a_2, \ldots, a_n\), if there exists such set of \(k\) indices \(1 \leq i_1 < i_2 < \ldots < i_k \leq n\) that \(b_j = a_{i_j}\) for all \(1 \leq j \leq k\). Let's define density of \(c\)-array \(a_1, a_2, \ldots, a_n\) as maximal non-negative integer \(p\), such that any \(c\)-array, that contains \(p\) numbers is a subarray of \(a_1, a_2, \ldots, a_n\).You are given a number \(c\) and some \(c\)-array \(a_1, a_2, \ldots, a_n\). For all \(0 \leq p \leq n\) find the number of sequences of indices \(1 \leq i_1 < i_2 < \ldots < i_k \leq n\) for all \(1 \leq k \leq n\), such that density of array \(a_{i_1}, a_{i_2}, \ldots, a_{i_k}\) is equal to \(p\). Find these numbers by modulo \(998\,244\,353\), because they can be too large.
|
The first line contains two integers \(n\) and \(c\), separated by spaces (\(1 \leq n, c \leq 3\,000\)). The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\), separated by spaces (\(1 \leq a_i \leq c\)).
|
Print \(n + 1\) numbers \(s_0, s_1, \ldots, s_n\). \(s_p\) should be equal to the number of sequences of indices \(1 \leq i_1 < i_2 < \ldots < i_k \leq n\) for all \(1 \leq k \leq n\) by modulo \(998\,244\,353\), such that the density of array \(a_{i_1}, a_{i_2}, \ldots, a_{i_k}\) is equal to \(p\).
|
In the first example, it's easy to see that the density of array will always be equal to its length. There exists \(4\) sequences with one index, \(6\) with two indices, \(4\) with three and \(1\) with four.In the second example, the only sequence of indices, such that the array will have non-zero density is all indices because in other cases there won't be at least one number from \(1\) to \(3\) in the array, so it won't satisfy the condition of density for \(p \geq 1\).
|
Input: 4 1 1 1 1 1 | Output: 0 4 6 4 1
|
Master
| 2 | 996 | 216 | 300 | 11 |
167 |
C
|
167C
|
C. Wizards and Numbers
| 2,300 |
games; math
|
In some country live wizards. They love playing with numbers. The blackboard has two numbers written on it β a and b. The order of the numbers is not important. Let's consider a β€ b for the sake of definiteness. The players can cast one of the two spells in turns: Replace b with b - ak. Number k can be chosen by the player, considering the limitations that k > 0 and b - ak β₯ 0. Number k is chosen independently each time an active player casts a spell. Replace b with b mod a. If a > b, similar moves are possible.If at least one of the numbers equals zero, a player can't make a move, because taking a remainder modulo zero is considered somewhat uncivilized, and it is far too boring to subtract a zero. The player who cannot make a move, loses.To perform well in the magic totalizator, you need to learn to quickly determine which player wins, if both wizards play optimally: the one that moves first or the one that moves second.
|
The first line contains a single integer t β the number of input data sets (1 β€ t β€ 104). Each of the next t lines contains two integers a, b (0 β€ a, b β€ 1018). The numbers are separated by a space.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
|
For any of the t input sets print ""First"" (without the quotes) if the player who moves first wins. Print ""Second"" (without the quotes) if the player who moves second wins. Print the answers to different data sets on different lines in the order in which they are given in the input.
|
In the first sample, the first player should go to (11,10). Then, after a single move of the second player to (1,10), he will take 10 modulo 1 and win.In the second sample the first player has two moves to (1,10) and (21,10). After both moves the second player can win.In the third sample, the first player has no moves.In the fourth sample, the first player wins in one move, taking 30 modulo 10.
|
Input: 410 2131 100 110 30 | Output: FirstSecondSecondFirst
|
Expert
| 2 | 936 | 350 | 286 | 1 |
1,203 |
B
|
1203B
|
B. Equal Rectangles
| 1,200 |
greedy; math
|
You are given \(4n\) sticks, the length of the \(i\)-th stick is \(a_i\).You have to create \(n\) rectangles, each rectangle will consist of exactly \(4\) sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.You want to all rectangles to have equal area. The area of the rectangle with sides \(a\) and \(b\) is \(a \cdot b\).Your task is to say if it is possible to create exactly \(n\) rectangles of equal area or not.You have to answer \(q\) independent queries.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 500\)) β the number of queries. Then \(q\) queries follow.The first line of the query contains one integer \(n\) (\(1 \le n \le 100\)) β the number of rectangles.The second line of the query contains \(4n\) integers \(a_1, a_2, \dots, a_{4n}\) (\(1 \le a_i \le 10^4\)), where \(a_i\) is the length of the \(i\)-th stick.
|
For each query print the answer to it. If it is impossible to create exactly \(n\) rectangles of equal area using given sticks, print ""NO"". Otherwise print ""YES"".
|
Input: 5 1 1 1 10 10 2 10 5 2 10 1 1 2 5 2 10 5 1 10 5 1 1 1 2 1 1 1 1 1 1 1 1 1 10000 10000 10000 10000 | Output: YES YES NO YES YES
|
Easy
| 2 | 707 | 390 | 166 | 12 |
|
683 |
E
|
683E
|
E. Hammer throwing
| 1,800 |
*special
|
n athletes take part in the hammer throw. Each of them has his own unique identifier β the integer from 1 to n (all athletes have distinct identifiers). After the draw, the order in which the athletes will throw the hammer has been determined (they will do it one by one).Unfortunately, a not very attentive judge lost the list with the order of athletes, but each of the athletes has remembered how many competitors with identifiers larger than his own will throw the hammer before him.Your task is to help the organizers as quickly as possible to restore the order of the athletes.
|
The first line contains the positive integer n (1 β€ n β€ 1000) β the number of athletes.The next line contains the sequence of integers a1, a2, ..., an (0 β€ ai < n), where ai is equal to the number of the athletes with identifiers larger than i, who should throw the hammer before the athlete with identifier i.
|
Print n distinct numbers β the sequence of athletes' identifiers in the order in which they will throw the hammer. If there are several answers it is allowed to print any of them.
|
Input: 42 0 1 0 | Output: 2 4 1 3
|
Medium
| 1 | 583 | 310 | 179 | 6 |
|
1,081 |
H
|
1081H
|
H. Palindromic Magic
| 3,500 |
data structures; hashing; strings
|
After learning some fancy algorithms about palindromes, Chouti found palindromes very interesting, so he wants to challenge you with this problem.Chouti has got two strings \(A\) and \(B\). Since he likes palindromes, he would like to pick \(a\) as some non-empty palindromic substring of \(A\) and \(b\) as some non-empty palindromic substring of \(B\). Concatenating them, he will get string \(ab\).Chouti thinks strings he could get this way are interesting, so he wants to know how many different strings he can get.
|
The first line contains a single string \(A\) (\(1 \le |A| \le 2 \cdot 10^5\)).The second line contains a single string \(B\) (\(1 \le |B| \le 2 \cdot 10^5\)).Strings \(A\) and \(B\) contain only lowercase English letters.
|
The first and only line should contain a single integer β the number of possible strings.
|
In the first example, attainable strings are ""a"" + ""a"" = ""aa"", ""aa"" + ""a"" = ""aaa"", ""aa"" + ""aba"" = ""aaaba"", ""aa"" + ""b"" = ""aab"", ""a"" + ""aba"" = ""aaba"", ""a"" + ""b"" = ""ab"". In the second example, attainable strings are ""aa"", ""aaa"", ""aaaa"", ""aaaba"", ""aab"", ""aaba"", ""ab"", ""abaa"", ""abaaa"", ""abaaba"", ""abab"", ""ba"", ""baa"", ""baba"", ""bb"".Notice that though ""a""+""aa""=""aa""+""a""=""aaa"", ""aaa"" will only be counted once.
|
Input: aaaba | Output: 6
|
Master
| 3 | 520 | 222 | 89 | 10 |
433 |
D
|
433D
|
D. Nanami's Digital Board
| 2,000 |
dsu; implementation
|
Nanami is an expert at playing games. This day, Nanami's good friend Hajime invited her to watch a game of baseball. Unwilling as she was, she followed him to the stadium. But Nanami had no interest in the game, so she looked around to see if there was something that might interest her. That's when she saw the digital board at one end of the stadium.The digital board is n pixels in height and m pixels in width, every pixel is either light or dark. The pixels are described by its coordinate. The j-th pixel of the i-th line is pixel (i, j). The board displays messages by switching a combination of pixels to light, and the rest to dark. Nanami notices that the state of the pixels on the board changes from time to time. At certain times, certain pixels on the board may switch from light to dark, or from dark to light.Nanami wonders, what is the area of the biggest light block such that a specific pixel is on its side. A light block is a sub-rectangle of the board, in which all pixels are light. Pixel (i, j) belongs to a side of sub-rectangle with (x1, y1) and (x2, y2) as its upper-left and lower-right vertex if and only if it satisfies the logical condition: ((i = x1 or i = x2) and (y1 β€ j β€ y2)) or ((j = y1 or j = y2) and (x1 β€ i β€ x2)).Nanami has all the history of changing pixels, also she has some questions of the described type, can you answer them?
|
The first line contains three space-separated integers n, m and q (1 β€ n, m, q β€ 1000) β the height and width of the digital board, and the number of operations.Then follow n lines, each line containing m space-separated integers. The j-th integer of the i-th line is ai, j β the initial state of pixel (i, j). If ai, j = 0, pixel (i, j) is initially dark. If ai, j = 1, pixel (i, j) is initially light. Then follow q lines, each line containing three space-separated integers op, x, and y (1 β€ op β€ 2; 1 β€ x β€ n; 1 β€ y β€ m), describing an operation. If op = 1, the pixel at (x, y) changes its state (from light to dark or from dark to light). If op = 2, Nanami queries the biggest light block with pixel (x, y) on its side.
|
For each query, print a single line containing one integer β the answer to Nanami's query.
|
Consider the first sample.The first query specifies pixel (2, 2), which is dark itself, so there are no valid light blocks, thus the answer is 0.The second query specifies pixel (1, 2). The biggest light block is the block with (1, 2) as its upper-left vertex and (1, 3) as its lower-right vertex.The last query specifies pixel (2, 2), which became light in the third operation. The biggest light block is the block with (1, 2) as its upper-left vertex and (3, 3) as its lower-right vertex.
|
Input: 3 4 50 1 1 01 0 0 10 1 1 02 2 22 1 21 2 21 2 32 2 2 | Output: 026
|
Hard
| 2 | 1,372 | 724 | 90 | 4 |
2,131 |
C
|
2131C
|
C. Make it Equal
| 0 |
math; number theory
|
Given two multisets \(S\) and \(T\) of size \(n\) and a positive integer \(k\), you may perform the following operations any number (including zero) of times on \(S\): Select an element \(x\) in \(S\), and remove one occurrence of \(x\) in \(S\). Then, either insert \(x+k\) into \(S\), or insert \(|x-k|\) into \(S\). Determine if it is possible to make \(S\) equal to \(T\). Two multisets \(S\) and \(T\) are equal if every element appears the same number of times in \(S\) and \(T\).
|
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 description of the test cases follows. The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \( 1 \le k \le 10^9\)) β the size of \(S\) and the constant, respectively.The second line contains \(n\) integers \(S_1,S_2,\ldots,S_n\) (\(0 \le S_i \le 10^9\)) β the elements in \(S\).The third line contains \(n\) integers \(T_1,T_2,\ldots,T_n\) (\(0 \le T_i \le 10^9\)) β the elements in \(T\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output ""YES"" if it is possible to make \(S\) equal to \(T\), and ""NO"" otherwise. You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, we can remove one occurrence of \(1\) from \(S\) and insert \(|1-k|=|1-3|=2\) into \(S\), making \(S\) equal to \(T\).In the second test case, we can remove one occurrence of \(4\) from \(S\) and insert \(4+k=4+8=12\) into \(S\), making \(S\) equal to \(T\).In the last test case, we can show that it is impossible to make \(S\) equal to \(T\).
|
Input: 51 3121 84123 56 2 98 4 112 72 82 93 20 1 01 0 1 | Output: YES YES YES NO NO
|
Beginner
| 2 | 486 | 648 | 266 | 21 |
1,085 |
C
|
1085C
|
C. Connect Three
| 1,600 |
implementation; math
|
The Squareland national forest is divided into equal \(1 \times 1\) square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates \((x, y)\) of its south-west corner.Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of land \(A, B, C\) in the forest. Initially, all plots in the forest (including the plots \(A, B, C\)) are covered by trees. The friends want to visit each other, so they want to clean some of the plots from trees. After cleaning, one should be able to reach any of the plots \(A, B, C\) from any other one of those by moving through adjacent cleared plots. Two plots are adjacent if they share a side. For example, \(A=(0,0)\), \(B=(1,1)\), \(C=(2,2)\). The minimal number of plots to be cleared is \(5\). One of the ways to do it is shown with the gray color. Of course, the friends don't want to strain too much. Help them find out the smallest number of plots they need to clean from trees.
|
The first line contains two integers \(x_A\) and \(y_A\) β coordinates of the plot \(A\) (\(0 \leq x_A, y_A \leq 1000\)). The following two lines describe coordinates \((x_B, y_B)\) and \((x_C, y_C)\) of plots \(B\) and \(C\) respectively in the same format (\(0 \leq x_B, y_B, x_C, y_C \leq 1000\)). It is guaranteed that all three plots are distinct.
|
On the first line print a single integer \(k\) β the smallest number of plots needed to be cleaned from trees. The following \(k\) lines should contain coordinates of all plots needed to be cleaned. All \(k\) plots should be distinct. You can output the plots in any order.If there are multiple solutions, print any of them.
|
The first example is shown on the picture in the legend.The second example is illustrated with the following image:
|
Input: 0 0 1 1 2 2 | Output: 5 0 0 1 0 1 1 1 2 2 2
|
Medium
| 2 | 1,009 | 352 | 324 | 10 |
1,310 |
E
|
1310E
|
E. Strange Function
| 2,900 |
dp
|
Let's define the function \(f\) of multiset \(a\) as the multiset of number of occurences of every number, that is present in \(a\).E.g., \(f(\{5, 5, 1, 2, 5, 2, 3, 3, 9, 5\}) = \{1, 1, 2, 2, 4\}\).Let's define \(f^k(a)\), as applying \(f\) to array \(a\) \(k\) times: \(f^k(a) = f(f^{k-1}(a)), f^0(a) = a\). E.g., \(f^2(\{5, 5, 1, 2, 5, 2, 3, 3, 9, 5\}) = \{1, 2, 2\}\).You are given integers \(n, k\) and you are asked how many different values the function \(f^k(a)\) can have, where \(a\) is arbitrary non-empty array with numbers of size no more than \(n\). Print the answer modulo \(998\,244\,353\).
|
The first and only line of input consists of two integers \(n, k\) (\(1 \le n, k \le 2020\)).
|
Print one number β the number of different values of function \(f^k(a)\) on all possible non-empty arrays with no more than \(n\) elements modulo \(998\,244\,353\).
|
Input: 3 1 | Output: 6
|
Master
| 1 | 605 | 93 | 164 | 13 |
|
1,107 |
C
|
1107C
|
C. Brutality
| 1,300 |
greedy; sortings; two pointers
|
You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.You are playing the game on the new generation console so your gamepad have \(26\) buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.You are given a sequence of hits, the \(i\)-th hit deals \(a_i\) units of damage to the opponent's character. To perform the \(i\)-th hit you have to press the button \(s_i\) on your gamepad. Hits are numbered from \(1\) to \(n\).You know that if you press some button more than \(k\) times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of \(a_i\) over all \(i\) for the hits which weren't skipped.Note that if you skip the hit then the counter of consecutive presses the button won't reset.Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons.
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of hits and the maximum number of times you can push the same button in a row.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the damage of the \(i\)-th hit.The third line of the input contains the string \(s\) consisting of exactly \(n\) lowercase Latin letters β the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).
|
Print one integer \(dmg\) β the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons.
|
In the first example you can choose hits with numbers \([1, 3, 4, 5, 6, 7]\) with the total damage \(1 + 16 + 18 + 7 + 2 + 10 = 54\).In the second example you can choose all hits so the total damage is \(2 + 4 + 1 + 3 + 1000 = 1010\).In the third example you can choose all hits expect the third one so the total damage is \(2 + 4 + 3 + 1000 = 1009\).In the fourth example you can choose hits with numbers \([2, 3, 6, 8]\). Only this way you can reach the maximum total damage \(15 + 2 + 8 + 16 = 41\).In the fifth example you can choose only hits with numbers \([2, 4, 6]\) with the total damage \(18 + 19 + 15 = 52\).In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage \(10\).
|
Input: 7 3 1 5 16 18 7 2 10 baaaaca | Output: 54
|
Easy
| 3 | 1,237 | 571 | 135 | 11 |
24 |
B
|
24B
|
B. F1 Champions
| 1,500 |
implementation
|
Formula One championship consists of series of races called Grand Prix. After every race drivers receive points according to their final position. Only the top 10 drivers receive points in the following order 25, 18, 15, 12, 10, 8, 6, 4, 2, 1. At the conclusion of the championship the driver with most points is the champion. If there is a tie, champion is the one with most wins (i.e. first places). If a tie still exists, it is chosen the one with most second places, and so on, until there are no more place to use for compare. Last year another scoring system was proposed but rejected. In it the champion is the one with most wins. If there is tie, champion is the one with most points. If a tie still exists it is proceeded the same way as in the original scoring system, that is comparing number of second, third, forth, and so on, places.You are given the result of all races during the season and you are to determine the champion according to both scoring systems. It is guaranteed, that both systems will produce unique champion.
|
The first line contain integer t (1 β€ t β€ 20), where t is the number of races. After that all races are described one by one. Every race description start with an integer n (1 β€ n β€ 50) on a line of itself, where n is the number of clasified drivers in the given race. After that n lines follow with the classification for the race, each containing the name of a driver. The names of drivers are given in order from the first to the last place. The name of the driver consists of lowercase and uppercase English letters and has length at most 50 characters. Comparing of names should be case-sensetive.
|
Your output should contain exactly two line. On the first line is the name of the champion according to the original rule, and on the second line the name of the champion according to the alternative rule.
|
It is not guaranteed that the same drivers participate in all races. For the championship consider every driver that has participated in at least one race. The total number of drivers during the whole season is not more then 50.
|
Input: 33HamiltonVettelWebber2WebberVettel2HamiltonVettel | Output: VettelHamilton
|
Medium
| 1 | 1,041 | 602 | 205 | 0 |
1,158 |
D
|
1158D
|
D. Winding polygonal line
| 2,600 |
constructive algorithms; geometry; greedy; math
|
Vasya has \(n\) different points \(A_1, A_2, \ldots A_n\) on the plane. No three of them lie on the same line He wants to place them in some order \(A_{p_1}, A_{p_2}, \ldots, A_{p_n}\), where \(p_1, p_2, \ldots, p_n\) β some permutation of integers from \(1\) to \(n\).After doing so, he will draw oriented polygonal line on these points, drawing oriented segments from each point to the next in the chosen order. So, for all \(1 \leq i \leq n-1\) he will draw oriented segment from point \(A_{p_i}\) to point \(A_{p_{i+1}}\). He wants to make this polygonal line satisfying \(2\) conditions: it will be non-self-intersecting, so any \(2\) segments which are not neighbors don't have common points. it will be winding. Vasya has a string \(s\), consisting of \((n-2)\) symbols ""L"" or ""R"". Let's call an oriented polygonal line winding, if its \(i\)-th turn left, if \(s_i = \) ""L"" and right, if \(s_i = \) ""R"". More formally: \(i\)-th turn will be in point \(A_{p_{i+1}}\), where oriented segment from point \(A_{p_i}\) to point \(A_{p_{i+1}}\) changes to oriented segment from point \(A_{p_{i+1}}\) to point \(A_{p_{i+2}}\). Let's define vectors \(\overrightarrow{v_1} = \overrightarrow{A_{p_i} A_{p_{i+1}}}\) and \(\overrightarrow{v_2} = \overrightarrow{A_{p_{i+1}} A_{p_{i+2}}}\). Then if in order to rotate the vector \(\overrightarrow{v_1}\) by the smallest possible angle, so that its direction coincides with the direction of the vector \(\overrightarrow{v_2}\) we need to make a turn counterclockwise, then we say that \(i\)-th turn is to the left, and otherwise to the right. For better understanding look at this pictures with some examples of turns: There are left turns on this picture There are right turns on this picture You are given coordinates of the points \(A_1, A_2, \ldots A_n\) on the plane and string \(s\). Find a permutation \(p_1, p_2, \ldots, p_n\) of the integers from \(1\) to \(n\), such that the polygonal line, drawn by Vasya satisfy two necessary conditions.
|
The first line contains one integer \(n\) β the number of points (\(3 \leq n \leq 2000\)). Next \(n\) lines contains two integers \(x_i\) and \(y_i\), divided by space β coordinates of the point \(A_i\) on the plane (\(-10^9 \leq x_i, y_i \leq 10^9\)). The last line contains a string \(s\) consisting of symbols ""L"" and ""R"" with length \((n-2)\). It is guaranteed that all points are different and no three points lie at the same line.
|
If the satisfying permutation doesn't exists, print \(-1\). In the other case, print \(n\) numbers \(p_1, p_2, \ldots, p_n\) β the permutation which was found (\(1 \leq p_i \leq n\) and all \(p_1, p_2, \ldots, p_n\) are different). If there exists more than one solution, you can find any.
|
This is the picture with the polygonal line from the \(1\) test: As we see, this polygonal line is non-self-intersecting and winding, because the turn in point \(2\) is left.This is the picture with the polygonal line from the \(2\) test:
|
Input: 3 1 1 3 1 1 3 L | Output: 1 2 3
|
Expert
| 4 | 2,000 | 440 | 289 | 11 |
1,769 |
C2
|
1769C2
|
C2. ΠΠΎΠ΄ΠΊΡΡΡΠΊΠ° II
| 1,300 |
*special; dp
|
Π ΡΡΠΎΠΉ Π²Π΅ΡΡΠΈΠΈ Π·Π°Π΄Π°ΡΠΈ \(n \le 2 \cdot 10^5\) ΠΈ \(a_i \le 10^6\) (Π° ΡΠ°ΠΊΠΆΠ΅ Π΅ΡΡΡ ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½ΠΈΠ΅ Π½Π° ΡΡΠΌΠΌΡ \(n\) ΠΏΠΎ Π½Π°Π±ΠΎΡΠ°ΠΌ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
Π²Π½ΡΡΡΠΈ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠ΅ΡΡΠ°).ΠΠΈΠΊΠ° Π·Π° Π²ΡΠ΅ΠΌΡ ΡΠ°Π±ΠΎΡΡ Π² ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ VK ΡΠΆΠ΅ ΡΠ΄Π΅Π»Π°Π»Π° \(n\) ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ² Π² ΡΠΈΡΡΠ΅ΠΌΠ΅ ΠΊΠΎΠ½ΡΡΠΎΠ»Ρ Π²Π΅ΡΡΠΈΠΉ. \(i\)-ΠΉ ΠΊΠΎΠΌΠΌΠΈΡ Π±ΡΠ» ΡΠ΄Π΅Π»Π°Π½ Π² \(a_i\)-ΠΉ Π΄Π΅Π½Ρ ΡΠ°Π±ΠΎΡΡ ΠΠΈΠΊΠΈ Π² ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ. Π Π½Π΅ΠΊΠΎΡΠΎΡΡΠ΅ Π΄Π½ΠΈ ΠΠΈΠΊΠ° ΠΌΠΎΠ³Π»Π° ΡΠ΄Π΅Π»Π°ΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ², Π° Π² Π΄ΡΡΠ³ΠΈΠ΅ β Π½Π΅ ΡΠ΄Π΅Π»Π°ΡΡ Π½ΠΈ ΠΎΠ΄Π½ΠΎΠ³ΠΎ.ΠΠΈΠΊΡ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΡΡΡ ΡΠ°ΠΊΠΈΠ΅ ΠΎΡΡΠ΅Π·ΠΊΠΈ ΠΏΠΎΠ΄ΡΡΠ΄ ΠΈΠ΄ΡΡΠΈΡ
Π΄Π½Π΅ΠΉ, ΡΡΠΎ Π² ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· ΡΡΠΈΡ
Π΄Π½Π΅ΠΉ Ρ Π½Π΅Ρ Π΅ΡΡΡ Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄ΠΈΠ½ ΠΊΠΎΠΌΠΌΠΈΡ. Π§Π΅ΠΌ Π΄Π»ΠΈΠ½Π½Π΅Π΅ Π±ΡΠ΄Π΅Ρ ΡΠ°ΠΌΡΠΉ Π΄Π»ΠΈΠ½Π½ΡΠΉ ΡΠ°ΠΊΠΎΠΉ ΠΎΡΡΠ΅Π·ΠΎΠΊ, ΡΠ΅ΠΌ Π±ΠΎΠ»Π΅Π΅ ΠΏΡΠΎΠ΄ΡΠΊΡΠΈΠ²Π½ΡΠΌ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΎΠΌ ΠΎΠ½Π° Π±ΡΠ΄Π΅Ρ ΡΠ΅Π±Ρ ΠΎΡΡΡΠ°ΡΡ.ΠΠ΅Π΄Π°Π²Π½ΠΎ ΠΠΈΠΊΠ° Π½Π°ΡΠ»Π° ΡΠΏΠΎΡΠΎΠ± ΠΏΠΎΠ΄ΠΊΡΡΡΠΈΡΡ Π²ΡΠ΅ΠΌΡ Π»ΡΠ±ΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠ° Π²ΠΏΠ΅ΡΡΠ΄, Π½ΠΎ Π½Π΅ Π±ΠΎΠ»Π΅Π΅ ΡΠ΅ΠΌ Π½Π° ΡΡΡΠΊΠΈ. Π’Π°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, \(i\)-ΠΉ ΠΊΠΎΠΌΠΌΠΈΡ ΡΠ΅ΠΏΠ΅ΡΡ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Β«ΡΠ΄Π΅Π»Π°Π½Β» Π»ΠΈΠ±ΠΎ Π² \(a_i\)-ΠΉ, Π»ΠΈΠ±ΠΎ Π² \((a_i + 1)\)-ΠΉ Π΄Π΅Π½Ρ. ΠΡΠ΅ΠΌΡ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠ° ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ΄ΠΊΡΡΡΠΈΡΡ Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΠΎ ΠΎΡ Π΄ΡΡΠ³ΠΈΡ
β Π² ΡΠ°ΡΡΠ½ΠΎΡΡΠΈ, ΠΌΠΎΠΆΠ½ΠΎ ΠΊΠ°ΠΊ ΠΎΡΡΠ°Π²ΠΈΡΡ Π²ΡΠ΅ΠΌ ΠΊΠΎΠΌΠΌΠΈΡΠ°ΠΌ ΠΈΡΡ
ΠΎΠ΄Π½ΠΎΠ΅ Π²ΡΠ΅ΠΌΡ, ΡΠ°ΠΊ ΠΈ ΠΏΠ΅ΡΠ΅Π½Π΅ΡΡΠΈ Π²ΡΠ΅ ΠΊΠΎΠΌΠΌΠΈΡΡ ΡΠΎΠ²Π½ΠΎ Π½Π° Π΄Π΅Π½Ρ Π²ΠΏΠ΅ΡΡΠ΄.ΠΠ°ΠΉΠ΄ΠΈΡΠ΅ Π΄Π»ΠΈΠ½Ρ ΡΠ°ΠΌΠΎΠ³ΠΎ Π΄Π»ΠΈΠ½Π½ΠΎΠ³ΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ³ΠΎ ΠΎΡΡΠ΅Π·ΠΊΠ° ΠΏΠΎΠ΄ΡΡΠ΄ ΠΈΠ΄ΡΡΠΈΡ
Π΄Π½Π΅ΠΉ, Π² ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· ΠΊΠΎΡΠΎΡΡΡ
Ρ ΠΠΈΠΊΠΈ Π² ΠΏΡΠΎΡΠΈΠ»Π΅ Π±ΡΠ΄Π΅Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°ΡΡΡΡ Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄ΠΈΠ½ ΠΊΠΎΠΌΠΌΠΈΡ, ΠΏΠΎΡΠ»Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠΉ ΠΏΠΎΠ΄ΠΊΡΡΡΠΊΠΈ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ Π½Π΅ΠΊΠΎΡΠΎΡΡΡ
ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ².
|
ΠΠ°ΠΆΠ΄ΡΠΉ ΡΠ΅ΡΡ ΡΠΎΡΡΠΎΠΈΡ ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ
Π½Π°Π±ΠΎΡΠΎΠ² Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
. Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ \(t\) (\(1 \le t \le 100\)) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π½Π°Π±ΠΎΡΠΎΠ² Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
. ΠΠ°Π»Π΅Π΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ Π½Π°Π±ΠΎΡΠΎΠ² Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
.ΠΠ΅ΡΠ²Π°Ρ ΡΡΡΠΎΠΊΠ° ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ Π½Π°Π±ΠΎΡΠ° Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β ΡΠΈΡΠ»ΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ².ΠΡΠΎΡΠ°Ρ ΡΡΡΠΎΠΊΠ° ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ \(n\) ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» \(a_1, a_2, \ldots, a_n\) Π² Π½Π΅ΡΠ±ΡΠ²Π°ΡΡΠ΅ΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅ (\(1 \le a_1 \le a_2 \le \ldots \le a_n \le 10^6\)) β Π½ΠΎΠΌΠ΅ΡΠ° Π΄Π½Π΅ΠΉ, Π² ΠΊΠΎΡΠΎΡΡΠ΅ Π±ΡΠ»ΠΈ ΡΠ΄Π΅Π»Π°Π½Ρ ΠΊΠΎΠΌΠΌΠΈΡΡ.ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ ΡΡΠΌΠΌΠ° \(n\) ΠΏΠΎ Π²ΡΠ΅ΠΌ Π½Π°Π±ΠΎΡΠ°ΠΌ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
Π½Π΅ ΠΏΡΠ΅Π²ΠΎΡΡ
ΠΎΠ΄ΠΈΡ \(2 \cdot 10^5\).
|
ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ Π½Π°Π±ΠΎΡΠ° Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ β ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΡΡ Π΄Π»ΠΈΠ½Ρ ΠΎΡΡΠ΅Π·ΠΊΠ° Π΄Π½Π΅ΠΉ, Π² ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· ΠΊΠΎΡΠΎΡΡΡ
Ρ ΠΠΈΠΊΠΈ Π² ΠΏΡΠΎΡΠΈΠ»Π΅ Π±ΡΠ΄Π΅Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°ΡΡΡΡ Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄ΠΈΠ½ ΠΊΠΎΠΌΠΌΠΈΡ, ΠΏΠΎΡΠ»Π΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠΉ ΠΏΠΎΠ΄ΠΊΡΡΡΠΊΠΈ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ Π½Π΅ΠΊΠΎΡΠΎΡΡΡ
ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ² Π²ΠΏΠ΅ΡΡΠ΄ Π½Π΅ Π±ΠΎΠ»Π΅Π΅ ΡΠ΅ΠΌ Π½Π° ΡΡΡΠΊΠΈ.
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΌ Π½Π°Π±ΠΎΡΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠΌΠ΅Π½ΡΡΡ Π΄Π°ΡΡ ΠΊΠΎΠΌΠΌΠΈΡΠ° Π² Π΄Π΅Π½Ρ \(3\) Π½Π° Π΄Π΅Π½Ρ \(4\), Π΄Π°ΡΡ ΠΊΠΎΠΌΠΌΠΈΡΠ° Π² Π΄Π΅Π½Ρ \(4\) β Π½Π° Π΄Π΅Π½Ρ \(5\), Π° Π΄Π°ΡΡ Π»ΡΠ±ΠΎΠ³ΠΎ ΠΈΠ· ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ² Π² Π΄Π΅Π½Ρ \(6\) β Π½Π° Π΄Π΅Π½Ρ \(7\). Π’ΠΎΠ³Π΄Π° Π² ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· Π΄Π½Π΅ΠΉ \(4\), \(5\), \(6\), \(7\) ΠΈ \(8\) Π² ΠΏΡΠΎΡΠΈΠ»Π΅ ΠΠΈΠΊΠΈ Π±ΡΠ΄Π΅Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°ΡΡΡΡ Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄ΠΈΠ½ ΠΊΠΎΠΌΠΌΠΈΡ, ΠΈ Π½Π°ΠΈΠ±ΠΎΠ»ΡΡΠΈΠΉ ΠΎΡΡΠ΅Π·ΠΎΠΊ ΠΈΠ· ΠΏΠΎΠ΄ΡΡΠ΄ ΠΈΠ΄ΡΡΠΈΡ
Π΄Π½Π΅ΠΉ Ρ ΠΊΠΎΠΌΠΌΠΈΡΠ°ΠΌΠΈ β \([4; 8]\) β Π±ΡΠ΄Π΅Ρ ΠΈΠΌΠ΅ΡΡ Π΄Π»ΠΈΠ½Ρ \(5\).ΠΠΎ Π²ΡΠΎΡΠΎΠΌ Π½Π°Π±ΠΎΡΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΠΌΠΎΠΆΠ½ΠΎ Π»ΠΈΠ±ΠΎ ΠΎΡΡΠ°Π²ΠΈΡΡ Π²ΡΠ΅ ΠΊΠΎΠΌΠΌΠΈΡΡ ΠΊΠ°ΠΊ Π΅ΡΡΡ, Π»ΠΈΠ±ΠΎ ΠΏΠ΅ΡΠ΅Π½Π΅ΡΡΠΈ ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΊΠΎΠΌΠΌΠΈΡ Π½Π° Π΄Π΅Π½Ρ Π²ΠΏΠ΅ΡΡΠ΄. Π Π»ΡΠ±ΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ Π΄Π»ΠΈΠ½Π° ΠΎΡΡΠ΅Π·ΠΊΠ° Π΄Π½Π΅ΠΉ ΡΠΎΡΡΠ°Π²ΠΈΡ \(6\).Π ΡΡΠ΅ΡΡΠ΅ΠΌ Π½Π°Π±ΠΎΡΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΠΠΈΠΊΠ° ΡΠ΄Π΅Π»Π°Π»Π° ΠΌΠ½ΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ², Π½ΠΎ Π²ΡΠ΅ Π² ΠΎΠ΄ΠΈΠ½ ΠΈ ΡΠΎΡ ΠΆΠ΅ Π΄Π΅Π½Ρ Ρ Π½ΠΎΠΌΠ΅ΡΠΎΠΌ \(10\). Π Π»ΡΡΡΠ΅ΠΌ ΡΠ»ΡΡΠ°Π΅ ΠΎΡΡΠ΅Π·ΠΎΠΊ Π΄Π½Π΅ΠΉ Π΄ΠΎΡΡΠΈΠ³Π½Π΅Ρ Π΄Π»ΠΈΠ½Ρ \(2\) β Π΅ΡΠ»ΠΈ ΠΊΠ°ΠΊΠΈΠ΅-ΡΠΎ ΠΊΠΎΠΌΠΌΠΈΡΡ ΠΎΡΡΠ°Π²ΠΈΡΡ Π½Π° Π΄Π΅Π½Ρ \(10\), Π° Π΄ΡΡΠ³ΠΈΠ΅ ΠΏΠ΅ΡΠ΅Π½Π΅ΡΡΠΈ Π½Π° Π΄Π΅Π½Ρ \(11\).
|
Input: 391 1 3 4 6 6 6 8 1061 2 3 4 5 6510 10 10 10 10 | Output: 5 6 2
|
Easy
| 2 | 1,177 | 617 | 264 | 17 |
1,583 |
F
|
1583F
|
F. Defender of Childhood Dreams
| 2,500 |
bitmasks; constructive algorithms; divide and conquer
|
Even if you just leave them be, they will fall to pieces all by themselves. So, someone has to protect them, right?You find yourself playing with Teucer again in the city of Liyue. As you take the eccentric little kid around, you notice something interesting about the structure of the city.Liyue can be represented as a directed graph containing \(n\) nodes. Nodes are labeled from \(1\) to \(n\). There is a directed edge from node \(a\) to node \(b\) if and only if \(a < b\).A path between nodes \(a\) and \(b\) is defined as a sequence of edges such that you can start at \(a\), travel along all of these edges in the corresponding direction, and end at \(b\). The length of a path is defined by the number of edges. A rainbow path of length \(x\) is defined as a path in the graph such that there exists at least 2 distinct colors among the set of \(x\) edges.Teucer's favorite number is \(k\). You are curious about the following scenario: If you were to label each edge with a color, what is the minimum number of colors needed to ensure that all paths of length \(k\) or longer are rainbow paths?Teucer wants to surprise his older brother with a map of Liyue. He also wants to know a valid coloring of edges that uses the minimum number of colors. Please help him with this task!
|
The only line of input contains two integers \(n\) and \(k\) (\(2 \leq k < n \leq 1000\)).
|
On the first line, output \(c\), the minimum colors you need to satisfy the above requirements.On the second line, print a valid edge coloring as an array of \(\frac{n(n-1)}{2}\) integers ranging from \(1\) to \(c\). Exactly \(c\) distinct colors should exist in the construction. Print the edges in increasing order by the start node first, then by the second node.For example, if \(n=4\), the edge colors will correspond to this order of edges: (\(1\), \(2\)), (\(1\), \(3\)), (\(1\), \(4\)), (\(2\), \(3\)), (\(2\), \(4\)), (\(3\), \(4\))
|
The corresponding construction for the first test case looks like this: It is impossible to satisfy the constraints with less than \(2\) colors.The corresponding construction for the second test case looks like this: One can show there exists no construction using less than \(3\) colors.
|
Input: 5 3 | Output: 2 1 2 2 2 2 2 2 1 1 1
|
Expert
| 3 | 1,288 | 90 | 541 | 15 |
708 |
A
|
708A
|
A. Letters Cyclic Shift
| 1,200 |
constructive algorithms; greedy; implementation; strings
|
You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly one non-empty substring of s and shift all its letters 'z' 'y' 'x' 'b' 'a' 'z'. In other words, each character is replaced with the previous character of English alphabet and 'a' is replaced with 'z'.What is the lexicographically minimum string that can be obtained from s by performing this shift exactly once?
|
The only line of the input contains the string s (1 β€ |s| β€ 100 000) consisting of lowercase English letters.
|
Print the lexicographically minimum string that can be obtained from s by shifting letters of exactly one non-empty substring.
|
String s is lexicographically smaller than some other string t of the same length if there exists some 1 β€ i β€ |s|, such that s1 = t1, s2 = t2, ..., si - 1 = ti - 1, and si < ti.
|
Input: codeforces | Output: bncdenqbdr
|
Easy
| 4 | 412 | 109 | 126 | 7 |
453 |
A
|
453A
|
A. Little Pony and Expected Maximum
| 1,600 |
probabilities
|
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability . Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
|
A single line contains two integers m and n (1 β€ m, n β€ 105).
|
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
|
Consider the third test example. If you've made two tosses: You can get 1 in the first toss, and 2 in the second. Maximum equals to 2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2. The probability of each outcome is 0.25, that is expectation equals to: You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
|
Input: 6 1 | Output: 3.500000000000
|
Medium
| 1 | 613 | 61 | 161 | 4 |
1,930 |
E
|
1930E
|
E. 2..3...4.... Wonderful! Wonderful!
| 2,400 |
combinatorics; dp; math
|
Stack has an array \(a\) of length \(n\) such that \(a_i = i\) for all \(i\) (\(1 \leq i \leq n\)). He will select a positive integer \(k\) (\(1 \leq k \leq \lfloor \frac{n-1}{2} \rfloor\)) and do the following operation on \(a\) any number (possibly \(0\)) of times. Select a subsequence\(^\dagger\) \(s\) of length \(2 \cdot k + 1\) from \(a\). Now, he will delete the first \(k\) elements of \(s\) from \(a\). To keep things perfectly balanced (as all things should be), he will also delete the last \(k\) elements of \(s\) from \(a\). Stack wonders how many arrays \(a\) can he end up with for each \(k\) (\(1 \leq k \leq \lfloor \frac{n-1}{2} \rfloor\)). As Stack is weak at counting problems, he needs your help. Since the number of arrays might be too large, please print it modulo \(998\,244\,353\).\(^\dagger\) A sequence \(x\) is a subsequence of a sequence \(y\) if \(x\) can be obtained from \(y\) by deleting several (possibly, zero or all) elements. For example, \([1, 3]\), \([1, 2, 3]\) and \([2, 3]\) are subsequences of \([1, 2, 3]\). On the other hand, \([3, 1]\) and \([2, 1, 3]\) are not subsequences of \([1, 2, 3]\).
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2 \cdot 10^3\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^6\)) β the length of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test, on a new line, print \(\lfloor \frac{n-1}{2} \rfloor\) space-separated integers β the \(i\)-th integer representing the number of arrays modulo \(998\,244\,353\) that Stack can get if he selects \(k=i\).
|
In the first test case, two \(a\) are possible for \(k=1\): \([1,2,3]\); \([2]\). In the second test case, four \(a\) are possible for \(k=1\): \([1,2,3,4]\); \([1,3]\); \([2,3]\); \([2,4]\). In the third test case, two \(a\) are possible for \(k=2\): \([1,2,3,4,5]\); \([3]\).
|
Input: 434510 | Output: 2 4 10 2 487 162 85 10
|
Expert
| 3 | 1,139 | 396 | 218 | 19 |
1,800 |
G
|
1800G
|
G. Symmetree
| 2,200 |
dfs and similar; hashing; implementation; trees
|
Kid was gifted a tree of \(n\) vertices with the root in the vertex \(1\). Since he really like symmetrical objects, Kid wants to find out if this tree is symmetrical. For example, the trees in the picture above are symmetrical. And the trees in this picture are not symmetrical. Formally, a tree is symmetrical if there exists an order of children such that: The subtree of the leftmost child of the root is a mirror image of the subtree of the rightmost child; the subtree of the second-left child of the root is a mirror image of the subtree of the second-right child of the root; ... if the number of children of the root is odd, then the subtree of the middle child should be symmetrical.
|
The first line of input data contains single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The first line of each case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.The following \(n-1\) lines contain two integers each \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β indices of vertices connected by an edge.It is guaranteed that the sum of \(n\) over all cases does not exceed \(2 \cdot 10^5\).
|
Output \(t\) strings, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if this tree is symmetrical, and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
|
Input: 661 51 61 22 32 471 51 33 61 44 74 291 22 42 33 51 77 67 88 9102 99 102 36 74 31 23 82 56 5103 28 109 74 28 22 14 56 55 71 | Output: YES NO YES NO NO YES
|
Hard
| 4 | 693 | 480 | 304 | 18 |
|
1,506 |
A
|
1506A
|
A. Strange Table
| 800 |
math
|
Polycarp found a rectangular table consisting of \(n\) rows and \(m\) columns. He noticed that each cell of the table has its number, obtained by the following algorithm ""by columns"": cells are numbered starting from one; cells are numbered from left to right by columns, and inside each column from top to bottom; number of each cell is an integer one greater than in the previous cell. For example, if \(n = 3\) and \(m = 5\), the table will be numbered as follows:$$$\( \begin{matrix} 1 & 4 & 7 & 10 & 13 \\ 2 & 5 & 8 & 11 & 14 \\ 3 & 6 & 9 & 12 & 15 \\ \end{matrix} \)\(However, Polycarp considers such numbering inconvenient. He likes the numbering ""by rows"": cells are numbered starting from one; cells are numbered from top to bottom by rows, and inside each row from left to right; number of each cell is an integer one greater than the number of the previous cell. For example, if \)n = 3\( and \)m = 5\(, then Polycarp likes the following table numbering: \)\( \begin{matrix} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & 10 \\ 11 & 12 & 13 & 14 & 15 \\ \end{matrix} \)\(Polycarp doesn't have much time, so he asks you to find out what would be the cell number in the numbering ""by rows"", if in the numbering ""by columns"" the cell has the number \)x$$$?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.Each test case consists of a single line containing three integers \(n\), \(m\), \(x\) (\(1 \le n, m \le 10^6\), \(1 \le x \le n \cdot m\)), where \(n\) and \(m\) are the number of rows and columns in the table, and \(x\) is the cell number.Note that the numbers in some test cases do not fit into the \(32\)-bit integer type, so you must use at least the \(64\)-bit integer type of your programming language.
|
For each test case, output the cell number in the numbering ""by rows"".
|
Input: 5 1 1 1 2 2 3 3 5 11 100 100 7312 1000000 1000000 1000000000000 | Output: 1 2 9 1174 1000000000000
|
Beginner
| 1 | 1,262 | 509 | 72 | 15 |
|
1,181 |
D
|
1181D
|
D. Irrigation
| 2,200 |
binary search; data structures; implementation; sortings; trees; two pointers
|
Misha was interested in water delivery from childhood. That's why his mother sent him to the annual Innovative Olympiad in Irrigation (IOI). Pupils from all Berland compete there demonstrating their skills in watering. It is extremely expensive to host such an olympiad, so after the first \(n\) olympiads the organizers introduced the following rule of the host city selection.The host cities of the olympiads are selected in the following way. There are \(m\) cities in Berland wishing to host the olympiad, they are numbered from \(1\) to \(m\). The host city of each next olympiad is determined as the city that hosted the olympiad the smallest number of times before. If there are several such cities, the city with the smallest index is selected among them.Misha's mother is interested where the olympiad will be held in some specific years. The only information she knows is the above selection rule and the host cities of the first \(n\) olympiads. Help her and if you succeed, she will ask Misha to avoid flooding your house.
|
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \leq n, m, q \leq 500\,000\)) β the number of olympiads before the rule was introduced, the number of cities in Berland wishing to host the olympiad, and the number of years Misha's mother is interested in, respectively.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq m\)), where \(a_i\) denotes the city which hosted the olympiad in the \(i\)-th year. Note that before the rule was introduced the host city was chosen arbitrarily.Each of the next \(q\) lines contains an integer \(k_i\) (\(n + 1 \leq k_i \leq 10^{18}\)) β the year number Misha's mother is interested in host city in.
|
Print \(q\) integers. The \(i\)-th of them should be the city the olympiad will be hosted in the year \(k_i\).
|
In the first example Misha's mother is interested in the first \(10\) years after the rule was introduced. The host cities these years are 4, 3, 4, 2, 3, 4, 1, 2, 3, 4.In the second example the host cities after the new city is introduced are 2, 3, 1, 2, 3, 5, 1, 2, 3, 4, 5, 1.
|
Input: 6 4 10 3 1 1 1 2 2 7 8 9 10 11 12 13 14 15 16 | Output: 4 3 4 2 3 4 1 2 3 4
|
Hard
| 6 | 1,034 | 687 | 110 | 11 |
1,006 |
C
|
1006C
|
C. Three Parts of the Array
| 1,200 |
binary search; data structures; two pointers
|
You are given an array \(d_1, d_2, \dots, d_n\) consisting of \(n\) integer numbers.Your task is to split this array into three parts (some of which may be empty) in such a way that each element of the array belongs to exactly one of the three parts, and each of the parts forms a consecutive contiguous subsegment (possibly, empty) of the original array. Let the sum of elements of the first part be \(sum_1\), the sum of elements of the second part be \(sum_2\) and the sum of elements of the third part be \(sum_3\). Among all possible ways to split the array you have to choose a way such that \(sum_1 = sum_3\) and \(sum_1\) is maximum possible.More formally, if the first part of the array contains \(a\) elements, the second part of the array contains \(b\) elements and the third part contains \(c\) elements, then:$$$\(sum_1 = \sum\limits_{1 \le i \le a}d_i,\)\( \)\(sum_2 = \sum\limits_{a + 1 \le i \le a + b}d_i,\)\( \)\(sum_3 = \sum\limits_{a + b + 1 \le i \le a + b + c}d_i.\)\(The sum of an empty array is \)0\(.Your task is to find a way to split the array such that \)sum_1 = sum_3\( and \)sum_1$$$ is maximum possible.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in the array \(d\).The second line of the input contains \(n\) integers \(d_1, d_2, \dots, d_n\) (\(1 \le d_i \le 10^9\)) β the elements of the array \(d\).
|
Print a single integer β the maximum possible value of \(sum_1\), considering that the condition \(sum_1 = sum_3\) must be met.Obviously, at least one valid way to split the array exists (use \(a=c=0\) and \(b=n\)).
|
In the first example there is only one possible splitting which maximizes \(sum_1\): \([1, 3, 1], [~], [1, 4]\).In the second example the only way to have \(sum_1=4\) is: \([1, 3], [2, 1], [4]\).In the third example there is only one way to split the array: \([~], [4, 1, 2], [~]\).
|
Input: 51 3 1 1 4 | Output: 5
|
Easy
| 3 | 1,135 | 267 | 215 | 10 |
602 |
B
|
602B
|
B. Approximating a Constant Range
| 1,400 |
dp; implementation; two pointers
|
When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points that seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging β but why not make a similar programming contest problem while we're at it?You're given a sequence of n data points a1, ..., an. There aren't any big jumps between consecutive data points β for each 1 β€ i < n, it's guaranteed that |ai + 1 - ai| β€ 1.A range [l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be the maximum and m the minimum value of ai for l β€ i β€ r; the range [l, r] is almost constant if M - m β€ 1.Find the length of the longest almost constant range.
|
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of data points.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 100 000).
|
Print a single number β the maximum length of an almost constant range of the given sequence.
|
In the first sample, the longest almost constant range is [2, 5]; its length (the number of data points in it) is 4.In the second sample, there are three almost constant ranges of length 4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].
|
Input: 51 2 3 3 2 | Output: 4
|
Easy
| 3 | 967 | 173 | 93 | 6 |
177 |
E1
|
177E1
|
E1. Space Voyage
| 1,700 |
binary search
|
The Smart Beaver from ABBYY plans a space travel on an ultramodern spaceship. During the voyage he plans to visit n planets. For planet i ai is the maximum number of suitcases that an alien tourist is allowed to bring to the planet, and bi is the number of citizens on the planet.The Smart Beaver is going to bring some presents from ABBYY to the planets he will be visiting. The presents are packed in suitcases, x presents in each. The Beaver will take to the ship exactly a1 + ... + an suitcases.As the Beaver lands on the i-th planet, he takes ai suitcases and goes out. On the first day on the planet the Beaver takes a walk and gets to know the citizens. On the second and all subsequent days the Beaver gives presents to the citizens β each of the bi citizens gets one present per day. The Beaver leaves the planet in the evening of the day when the number of presents left is strictly less than the number of citizens (i.e. as soon as he won't be able to give away the proper number of presents the next day). He leaves the remaining presents at the hotel.The Beaver is going to spend exactly c days traveling. The time spent on flights between the planets is considered to be zero. In how many ways can one choose the positive integer x so that the planned voyage will take exactly c days?
|
The first input line contains space-separated integers n and c β the number of planets that the Beaver is going to visit and the number of days he is going to spend traveling, correspondingly.The next n lines contain pairs of space-separated integers ai, bi (1 β€ i β€ n) β the number of suitcases he can bring to the i-th planet and the number of citizens of the i-th planet, correspondingly.The input limitations for getting 30 points are: 1 β€ n β€ 100 1 β€ ai β€ 100 1 β€ bi β€ 100 1 β€ c β€ 100 The input limitations for getting 100 points are: 1 β€ n β€ 104 0 β€ ai β€ 109 1 β€ bi β€ 109 1 β€ c β€ 109 Due to possible overflow, it is recommended to use the 64-bit arithmetic. In some solutions even the 64-bit arithmetic can overflow. So be careful in calculations!
|
Print a single number k β the number of ways to choose x so as to travel for exactly c days. If there are infinitely many possible values of x, print -1.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
|
In the first example there is only one suitable value x = 5. Then the Beaver takes 1 suitcase with 5 presents to the first planet. Here he spends 2 days: he hangs around on the first day, and he gives away five presents on the second day. He takes 2 suitcases with 10 presents to the second planet. Here he spends 3 days β he gives away 4 presents on the second and the third days and leaves the remaining 2 presents at the hotel. In total, the Beaver spends 5 days traveling.For x = 4 or less the Beaver won't have enough presents for the second day on the first planet, so the voyage will end too soon. For x = 6 and more the Beaver will spend at least one more day on the second planet, and the voyage will take too long.
|
Input: 2 51 52 4 | Output: 1
|
Medium
| 1 | 1,298 | 753 | 295 | 1 |
50 |
B
|
50B
|
B. Choosing Symbol Pairs
| 1,500 |
strings
|
There is a given string S consisting of N symbols. Your task is to find the number of ordered pairs of integers i and j such that1. 1 β€ i, j β€ N2. S[i] = S[j], that is the i-th symbol of string S is equal to the j-th.
|
The single input line contains S, consisting of lowercase Latin letters and digits. It is guaranteed that string S in not empty and its length does not exceed 105.
|
Print a single number which represents the number of pairs i and j with the needed property. Pairs (x, y) and (y, x) should be considered different, i.e. the ordered pairs count.
|
Input: great10 | Output: 7
|
Medium
| 1 | 217 | 163 | 178 | 0 |
|
1,615 |
E
|
1615E
|
E. Purple Crayon
| 2,400 |
data structures; dfs and similar; games; graphs; greedy; math; sortings; trees
|
Two players, Red and Blue, are at it again, and this time they're playing with crayons! The mischievous duo is now vandalizing a rooted tree, by coloring the nodes while playing their favorite game.The game works as follows: there is a tree of size \(n\), rooted at node \(1\), where each node is initially white. Red and Blue get one turn each. Red goes first. In Red's turn, he can do the following operation any number of times: Pick any subtree of the rooted tree, and color every node in the subtree red. However, to make the game fair, Red is only allowed to color \(k\) nodes of the tree. In other words, after Red's turn, at most \(k\) of the nodes can be colored red.Then, it's Blue's turn. Blue can do the following operation any number of times: Pick any subtree of the rooted tree, and color every node in the subtree blue. However, he's not allowed to choose a subtree that contains a node already colored red, as that would make the node purple and no one likes purple crayon. Note: there's no restriction on the number of nodes Blue can color, as long as he doesn't color a node that Red has already colored.After the two turns, the score of the game is determined as follows: let \(w\) be the number of white nodes, \(r\) be the number of red nodes, and \(b\) be the number of blue nodes. The score of the game is \(w \cdot (r - b)\).Red wants to maximize this score, and Blue wants to minimize it. If both players play optimally, what will the final score of the game be?
|
The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le k \le n\)) β the number of vertices in the tree and the maximum number of red nodes.Next \(n - 1\) lines contains description of edges. The \(i\)-th line contains two space separated integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \neq v_i\)) β the \(i\)-th edge of the tree.It's guaranteed that given edges form a tree.
|
Print one integer β the resulting score if both Red and Blue play optimally.
|
In the first test case, the optimal strategy is as follows: Red chooses to color the subtrees of nodes \(2\) and \(3\). Blue chooses to color the subtree of node \(4\). At the end of this process, nodes \(2\) and \(3\) are red, node \(4\) is blue, and node \(1\) is white. The score of the game is \(1 \cdot (2 - 1) = 1\).In the second test case, the optimal strategy is as follows: Red chooses to color the subtree of node \(4\). This colors both nodes \(4\) and \(5\). Blue does not have any options, so nothing is colored blue. At the end of this process, nodes \(4\) and \(5\) are red, and nodes \(1\), \(2\) and \(3\) are white. The score of the game is \(3 \cdot (2 - 0) = 6\).For the third test case: The score of the game is \(4 \cdot (2 - 1) = 4\).
|
Input: 4 2 1 2 1 3 1 4 | Output: 1
|
Expert
| 8 | 1,488 | 426 | 76 | 16 |
83 |
A
|
83A
|
A. Magical Array
| 1,300 |
math
|
Valery is very interested in magic. Magic attracts him so much that he sees it everywhere. He explains any strange and weird phenomenon through intervention of supernatural forces. But who would have thought that even in a regular array of numbers Valera manages to see something beautiful and magical.Valera absolutely accidentally got a piece of ancient parchment on which an array of numbers was written. He immediately thought that the numbers in this array were not random. As a result of extensive research Valera worked out a wonderful property that a magical array should have: an array is defined as magic if its minimum and maximum coincide.He decided to share this outstanding discovery with you, but he asks you for help in return. Despite the tremendous intelligence and wit, Valera counts very badly and so you will have to complete his work. All you have to do is count the number of magical subarrays of the original array of numbers, written on the parchment. Subarray is defined as non-empty sequence of consecutive elements.
|
The first line of the input data contains an integer n (1 β€ n β€ 105). The second line contains an array of original integers a1, a2, ..., an ( - 109 β€ ai β€ 109).
|
Print on the single line the answer to the problem: the amount of subarrays, which are magical.Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator).
|
Notes to sample tests:Magical subarrays are shown with pairs of indices [a;b] of the beginning and the end.In the first sample: [1;1], [2;2], [3;3], [4;4], [2;3].In the second sample: [1;1], [2;2], [3;3], [4;4], [5;5], [1;2], [2;3], [1;3].
|
Input: 42 1 1 4 | Output: 5
|
Easy
| 1 | 1,043 | 161 | 260 | 0 |
1,705 |
E
|
1705E
|
E. Mark and Professor Koro
| 2,300 |
binary search; bitmasks; brute force; combinatorics; data structures; greedy
|
After watching a certain anime before going to sleep, Mark dreams of standing in an old classroom with a blackboard that has a sequence of \(n\) positive integers \(a_1, a_2,\dots,a_n\) on it.Then, professor Koro comes in. He can perform the following operation: select an integer \(x\) that appears at least \(2\) times on the board, erase those \(2\) appearances, and write \(x+1\) on the board. Professor Koro then asks Mark the question, ""what is the maximum possible number that could appear on the board after some operations?""Mark quickly solves this question, but he is still slower than professor Koro. Thus, professor Koro decides to give Mark additional challenges. He will update the initial sequence of integers \(q\) times. Each time, he will choose positive integers \(k\) and \(l\), then change \(a_k\) to \(l\). After each update, he will ask Mark the same question again.Help Mark answer these questions faster than Professor Koro!Note that the updates are persistent. Changes made to the sequence \(a\) will apply when processing future updates.
|
The first line of the input contains two integers \(n\) and \(q\) (\(2\leq n\leq 2\cdot 10^5\), \(1\leq q\leq 2\cdot 10^5\)) β the length of the sequence \(a\) and the number of updates, respectively.The second line contains \(n\) integers \(a_1,a_2,\dots,a_n\) (\(1\leq a_i\leq 2\cdot 10^5\))Then, \(q\) lines follow, each consisting of two integers \(k\) and \(l\) (\(1\leq k\leq n\), \(1\leq l\leq 2\cdot 10^5\)), telling to update \(a_k\) to \(l\).
|
Print \(q\) lines. The \(i\)-th line should consist of a single integer β the answer after the \(i\)-th update.
|
In the first example test, the program must proceed through \(4\) updates.The sequence after the first update is \([2,3,2,4,5]\). One sequence of operations that achieves the number \(6\) the following. Initially, the blackboard has numbers \([2,3,2,4,5]\). Erase two copies of \(2\) and write \(3\), yielding \([3,4,5,\color{red}{3}]\). Erase two copies of \(3\) and write \(4\), yielding \([4,5,\color{red}{4}]\). Erase two copies of \(4\) and write \(5\), yielding \([5,\color{red}{5}]\). Erase two copies of \(5\) and write \(6\), yielding \([\color{red}{6}]\). Then, in the second update, the array is changed to \([2,3,2,4,3]\). This time, Mark cannot achieve \(6\). However, one sequence that Mark can use to achieve \(5\) is shown below. Initially, the blackboard has \([2,3,2,4,3]\). Erase two copies of \(2\) and write \(3\), yielding \([3,4,3,\color{red}{3}]\). Erase two copies of \(3\) and write \(4\), yielding \([3,4,\color{red}{4}]\). Erase two copies of \(4\) and write \(5\), yielding \([3,\color{red}{5}]\). In the third update, the array is changed to \([2,3,2,1,3]\). One way to achieve \(4\) is shown below. Initially, the blackboard has \([2,3,2,1,3]\). Erase two copies of \(3\) and write \(4\), yielding \([2,2,1,\color{red}{4}]\).
|
Input: 5 4 2 2 2 4 5 2 3 5 3 4 1 1 4 | Output: 6 5 4 5
|
Expert
| 6 | 1,066 | 452 | 111 | 17 |
414 |
A
|
414A
|
A. Mashmokh and Numbers
| 1,500 |
constructive algorithms; number theory
|
It's holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh. In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points.Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn't know how choose the initial sequence in the right way. Please, help him. Find n distinct integers a1, a2, ..., an such that his boss will score exactly k points. Also Mashmokh can't memorize too huge numbers. Therefore each of these integers must be at most 109.
|
The first line of input contains two space-separated integers n, k (1 β€ n β€ 105; 0 β€ k β€ 108).
|
If such sequence doesn't exist output -1 otherwise output n distinct space-separated integers a1, a2, ..., an (1 β€ ai β€ 109).
|
gcd(x, y) is greatest common divisor of x and y.
|
Input: 5 2 | Output: 1 2 3 4 5
|
Medium
| 2 | 984 | 94 | 125 | 4 |
41 |
B
|
41B
|
B. Martian Dollar
| 1,400 |
brute force
|
One day Vasya got hold of information on the Martian dollar course in bourles for the next n days. The buying prices and the selling prices for one dollar on day i are the same and are equal to ai. Vasya has b bourles. He can buy a certain number of dollars and then sell it no more than once in n days. According to Martian laws, one can buy only an integer number of dollars. Which maximal sum of money in bourles can Vasya get by the end of day n?
|
The first line contains two integers n and b (1 β€ n, b β€ 2000) β the number of days and the initial number of money in bourles. The next line contains n integers ai (1 β€ ai β€ 2000) β the prices of Martian dollars.
|
Print the single number β which maximal sum of money in bourles can Vasya get by the end of day n.
|
Input: 2 43 7 | Output: 8
|
Easy
| 1 | 450 | 213 | 98 | 0 |
|
1,657 |
A
|
1657A
|
A. Integer Moves
| 800 |
brute force; math
|
There's a chip in the point \((0, 0)\) of the coordinate plane. In one operation, you can move the chip from some point \((x_1, y_1)\) to some point \((x_2, y_2)\) if the Euclidean distance between these two points is an integer (i.e. \(\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}\) is integer).Your task is to determine the minimum number of operations required to move the chip from the point \((0, 0)\) to the point \((x, y)\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 3000\)) β number of test cases.The single line of each test case contains two integers \(x\) and \(y\) (\(0 \le x, y \le 50\)) β the coordinates of the destination point.
|
For each test case, print one integer β the minimum number of operations required to move the chip from the point \((0, 0)\) to the point \((x, y)\).
|
In the first example, one operation \((0, 0) \rightarrow (8, 6)\) is enough. \(\sqrt{(0-8)^2+(0-6)^2}=\sqrt{64+36}=\sqrt{100}=10\) is an integer.In the second example, the chip is already at the destination point.In the third example, the chip can be moved as follows: \((0, 0) \rightarrow (5, 12) \rightarrow (9, 15)\). \(\sqrt{(0-5)^2+(0-12)^2}=\sqrt{25+144}=\sqrt{169}=13\) and \(\sqrt{(5-9)^2+(12-15)^2}=\sqrt{16+9}=\sqrt{25}=5\) are integers.
|
Input: 38 60 09 15 | Output: 1 0 2
|
Beginner
| 2 | 417 | 232 | 149 | 16 |
1,183 |
E
|
1183E
|
E. Subsequences (easy version)
| 2,000 |
dp; graphs; implementation; shortest paths
|
The only difference between the easy and the hard versions is constraints.A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string ""abaca"" the following strings are subsequences: ""abaca"", ""aba"", ""aaa"", ""a"" and """" (empty string). But the following strings are not subsequences: ""aabaca"", ""cb"" and ""bcaa"".You are given a string \(s\) consisting of \(n\) lowercase Latin letters.In one move you can take any subsequence \(t\) of the given string and add it to the set \(S\). The set \(S\) can't contain duplicates. This move costs \(n - |t|\), where \(|t|\) is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).Your task is to find out the minimum possible total cost to obtain a set \(S\) of size \(k\) or report that it is impossible to do so.
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le n, k \le 100\)) β the length of the string and the size of the set, correspondingly.The second line of the input contains a string \(s\) consisting of \(n\) lowercase Latin letters.
|
Print one integer β if it is impossible to obtain the set \(S\) of size \(k\), print -1. Otherwise, print the minimum possible total cost to do it.
|
In the first example we can generate \(S\) = { ""asdf"", ""asd"", ""adf"", ""asf"", ""sdf"" }. The cost of the first element in \(S\) is \(0\) and the cost of the others is \(1\). So the total cost of \(S\) is \(4\).
|
Input: 4 5 asdf | Output: 4
|
Hard
| 4 | 1,029 | 256 | 147 | 11 |
264 |
C
|
264C
|
C. Choosing Balls
| 2,000 |
dp
|
There are n balls. They are arranged in a row. Each ball has a color (for convenience an integer) and an integer value. The color of the i-th ball is ci and the value of the i-th ball is vi.Squirrel Liss chooses some balls and makes a new sequence without changing the relative order of the balls. She wants to maximize the value of this sequence.The value of the sequence is defined as the sum of following values for each ball (where a and b are given constants): If the ball is not in the beginning of the sequence and the color of the ball is same as previous ball's color, add (the value of the ball) Γ a. Otherwise, add (the value of the ball) Γ b. You are given q queries. Each query contains two integers ai and bi. For each query find the maximal value of the sequence she can make when a = ai and b = bi.Note that the new sequence can be empty, and the value of an empty sequence is defined as zero.
|
The first line contains two integers n and q (1 β€ n β€ 105; 1 β€ q β€ 500). The second line contains n integers: v1, v2, ..., vn (|vi| β€ 105). The third line contains n integers: c1, c2, ..., cn (1 β€ ci β€ n).The following q lines contain the values of the constants a and b for queries. The i-th of these lines contains two integers ai and bi (|ai|, |bi| β€ 105).In each line integers are separated by single spaces.
|
For each query, output a line containing an integer β the answer to the query. The i-th line contains the answer to the i-th query in the input order.Please, do not write 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 example, to achieve the maximal value: In the first query, you should select 1st, 3rd, and 4th ball. In the second query, you should select 3rd, 4th, 5th and 6th ball. In the third query, you should select 2nd and 4th ball. Note that there may be other ways to achieve the maximal value.
|
Input: 6 31 -2 3 4 0 -11 2 1 2 1 15 1-2 11 0 | Output: 2094
|
Hard
| 1 | 909 | 412 | 299 | 2 |
1,993 |
A
|
1993A
|
A. Question Marks
| 800 |
greedy; implementation
|
Tim is doing a test consisting of \(4n\) questions; each question has \(4\) options: 'A', 'B', 'C', and 'D'. For each option, there are exactly \(n\) correct answers corresponding to that option β meaning there are \(n\) questions with the answer 'A', \(n\) questions with the answer 'B', \(n\) questions with the answer 'C', and \(n\) questions with the answer 'D'.For each question, Tim wrote his answer on the answer sheet. If he could not figure out the answer, he would leave a question mark '?' for that question.You are given his answer sheet of \(4n\) characters. What is the maximum number of correct answers Tim can get?
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)).The second line of each test case contains a string \(s\) of \(4n\) characters (\(s_i \in \{\texttt{A}, \texttt{B}, \texttt{C}, \texttt{D}, \texttt{?}\}\)) β Tim's answers for the questions.
|
For each test case, print a single integer β the maximum score that Tim can achieve.
|
In the first test case, there is exactly one question with each answer 'A', 'B', 'C', and 'D'; so it's possible that Tim gets all his answers correct.In the second test case, there are only two correct answers 'A' which makes him get exactly \(2\) points in any case.In the third test case, Tim can get at most \(2\) correct answers with option 'A' and \(2\) correct answers with option 'B'. For example, he would get \(4\) points if the answers were 'AACCBBDD'.In the fourth test case, he refuses to answer any question at all, which makes him get \(0\) points.
|
Input: 61ABCD2AAAAAAAA2AAAABBBB2????????3ABCABCABCABC5ACADC??ACAC?DCAABC?C | Output: 4 2 4 0 9 13
|
Beginner
| 2 | 630 | 368 | 84 | 19 |
449 |
D
|
449D
|
D. Jzzhu and Numbers
| 2,400 |
bitmasks; combinatorics; dp
|
Jzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence of indexes i1, i2, ..., ik (1 β€ i1 < i2 < ... < ik β€ n) a group of size k. Jzzhu wonders, how many groups exists such that ai1 & ai2 & ... & aik = 0 (1 β€ k β€ n)? Help him and print this number modulo 1000000007 (109 + 7). Operation x & y denotes bitwise AND operation of two numbers.
|
The first line contains a single integer n (1 β€ n β€ 106). The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 106).
|
Output a single integer representing the number of required groups modulo 1000000007 (109 + 7).
|
Input: 32 3 3 | Output: 0
|
Expert
| 3 | 359 | 125 | 95 | 4 |
|
1,374 |
E1
|
1374E1
|
E1. Reading Books (easy version)
| 1,600 |
data structures; greedy; sortings
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster.There are \(n\) books in the family library. The \(i\)-th book is described by three integers: \(t_i\) β the amount of time Alice and Bob need to spend to read it, \(a_i\) (equals \(1\) if Alice likes the \(i\)-th book and \(0\) if not), and \(b_i\) (equals \(1\) if Bob likes the \(i\)-th book and \(0\) if not).So they need to choose some books from the given \(n\) books in such a way that: Alice likes at least \(k\) books from the chosen set and Bob likes at least \(k\) books from the chosen set; the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of \(t_i\) over all books that are in the chosen set.Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)).The next \(n\) lines contain descriptions of books, one description per line: the \(i\)-th line contains three integers \(t_i\), \(a_i\) and \(b_i\) (\(1 \le t_i \le 10^4\), \(0 \le a_i, b_i \le 1\)), where: \(t_i\) β the amount of time required for reading the \(i\)-th book; \(a_i\) equals \(1\) if Alice likes the \(i\)-th book and \(0\) otherwise; \(b_i\) equals \(1\) if Bob likes the \(i\)-th book and \(0\) otherwise.
|
If there is no solution, print only one integer -1. Otherwise print one integer \(T\) β the minimum total reading time of the suitable set of books.
|
Input: 8 47 1 12 1 14 0 18 1 11 0 11 1 11 0 13 0 0 | Output: 18
|
Medium
| 3 | 1,309 | 527 | 148 | 13 |
|
1,932 |
D
|
1932D
|
D. Card Game
| 1,400 |
greedy; implementation
|
Two players are playing an online card game. The game is played using a 32-card deck. Each card has a suit and a rank. There are four suits: clubs, diamonds, hearts, and spades. We will encode them with characters 'C', 'D', 'H', and 'S', respectively. And there are 8 ranks, in increasing order: '2', '3', '4', '5', '6', '7', '8', '9'.Each card is denoted by two letters: its rank and its suit. For example, the 8 of Hearts is denoted as 8H.At the beginning of the game, one suit is chosen as the trump suit.In each round, players make moves like this: the first player places one of his cards on the table, and the second player must beat this card with one of their cards. After that, both cards are moved to the discard pile.A card can beat another card if both cards have the same suit and the first card has a higher rank than the second. For example, 8S can beat 4S. Additionally, a trump card can beat any non-trump card, regardless of the rank of the cards, for example, if the trump suit is clubs ('C'), then 3C can beat 9D. Note that trump cards can be beaten only by the trump cards of higher rank.There were \(n\) rounds played in the game, so the discard pile now contains \(2n\) cards. You want to reconstruct the rounds played in the game, but the cards in the discard pile are shuffled. Find any possible sequence of \(n\) rounds that might have been played in the game.
|
The first line contains integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.The first line of a test case contains the integer number \(n\) (\(1\le n\le 16\)).The second line of a test case contains one character, the trump suit. It is one of ""CDHS"".The third line of a test case contains the description of \(2n\) cards. Each card is described by a two-character string, the first character is the rank of the card, which is one of ""23456789"", and the second one is the suit of the card, which is one of ""CDHS"". All cards are different.
|
For each test case print the answer to it: Print \(n\) lines. In each line, print the description of two cards, in the same format as in the input: the first card that was played by the first player, and then the card that was used by the second player to beat it. If there is no solution, print a single line ""IMPOSSIBLE"".If there are multiple solutions, print any of them.
|
Input: 83S3C 9S 4C 6D 3S 7S2C3S 5D 9S 6H1H6C 5D1S7S 3S1H9S 9H1S9S 9H1C9D 8H2C9C 9S 6H 8C | Output: 3C 4C 6D 9S 3S 7S IMPOSSIBLE IMPOSSIBLE 3S 7S 9S 9H 9H 9S IMPOSSIBLE 6H 9C 9S 8C
|
Easy
| 2 | 1,386 | 584 | 376 | 19 |
|
706 |
A
|
706A
|
A. Beru-taxi
| 900 |
brute force; geometry; implementation
|
Vasiliy lives at point (a, b) of the coordinate plane. He is hurrying up to work so he wants to get out of his house as soon as possible. New app suggested n available Beru-taxi nearby. The i-th taxi is located at point (xi, yi) and moves with a speed vi. Consider that each of n drivers will move directly to Vasiliy and with a maximum possible speed. Compute the minimum time when Vasiliy will get in any of Beru-taxi cars.
|
The first line of the input contains two integers a and b ( - 100 β€ a, b β€ 100) β coordinates of Vasiliy's home.The second line contains a single integer n (1 β€ n β€ 1000) β the number of available Beru-taxi cars nearby. The i-th of the following n lines contains three integers xi, yi and vi ( - 100 β€ xi, yi β€ 100, 1 β€ vi β€ 100) β the coordinates of the i-th car and its speed.It's allowed that several cars are located at the same point. Also, cars may be located at exactly the same point where Vasiliy lives.
|
Print a single real value β the minimum time Vasiliy needs to get in any of the Beru-taxi cars. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
|
In the first sample, first taxi will get to Vasiliy in time 2, and second will do this in time 1, therefore 1 is the answer.In the second sample, cars 2 and 3 will arrive simultaneously.
|
Input: 0 022 0 10 2 2 | Output: 1.00000000000000000000
|
Beginner
| 3 | 425 | 512 | 328 | 7 |
1,942 |
C2
|
1942C2
|
C2. Bessie's Birthday Cake (Hard Version)
| 1,700 |
geometry; greedy; math
|
Proof Geometric Construction Can Solve All Love Affairs - manbo-pβ This is the hard version of the problem. The only difference between the two versions is the constraint on \(y\). In this version \(0 \leq y \leq n - x\). You can make hacks only if both versions are solved.Bessie has received a birthday cake from her best friend Elsie, and it came in the form of a regular polygon with \(n\) sides. The vertices of the cake are numbered from \(1\) to \(n\) clockwise. You and Bessie are going to choose some of those vertices to cut non-intersecting diagonals into the cake. In other words, the endpoints of the diagonals must be part of the chosen vertices.Bessie would only like to give out pieces of cake which result in a triangle to keep consistency. The size of the pieces doesn't matter, and the whole cake does not have to be separated into all triangles (other shapes are allowed in the cake, but those will not be counted). Bessie has already chosen \(x\) of those vertices that can be used to form diagonals. She wants you to choose no more than \(y\) other vertices such that the number of triangular pieces of cake she can give out is maximized.What is the maximum number of triangular pieces of cake Bessie can give out?
|
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 consists of three integers, \(n\), \(x\), and \(y\) (\(4 \leq n \leq 10^9\), \(2 \leq x \leq \min(n, 2 \cdot 10^5)\), \(0 \leq y \leq n - x\)) β the number of sides of the polygon, number of vertices Bessie has chosen, and the maximum number of other vertices you can choose.The second line consists of \(x\) distinct integers from \(1\) to \(n\), representing the vertices Bessie has chosen.It is guaranteed the sum of \(x\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer: the maximum number of non-intersecting triangular pieces of cake she can give out.
|
In test cases \(1\), \(2\) and \(3\), you can get \(6\), \(5\) and \(2\) non-intersecting triangular pieces of cake, respectively. A possible construction is shown in the following pictures:The green dots represent vertices that Bessie chose, the yellow dots represent vertices that you chose, the blue lines represent diagonals that are drawn, and the red numbers represent triangles that are counted.
|
Input: 38 4 21 6 2 57 3 16 4 34 2 21 3 | Output: 6 5 2
|
Medium
| 3 | 1,235 | 611 | 127 | 19 |
178 |
B2
|
178B2
|
B2. Greedy Merchants
| 1,600 |
In ABBYY a wonderful Smart Beaver lives. This time, he began to study history. When he read about the Roman Empire, he became interested in the life of merchants.The Roman Empire consisted of n cities numbered from 1 to n. It also had m bidirectional roads numbered from 1 to m. Each road connected two different cities. Any two cities were connected by no more than one road.We say that there is a path between cities c1 and c2 if there exists a finite sequence of cities t1, t2, ..., tp (p β₯ 1) such that: t1 = c1 tp = c2 for any i (1 β€ i < p), cities ti and ti + 1 are connected by a road We know that there existed a path between any two cities in the Roman Empire.In the Empire k merchants lived numbered from 1 to k. For each merchant we know a pair of numbers si and li, where si is the number of the city where this merchant's warehouse is, and li is the number of the city where his shop is. The shop and the warehouse could be located in different cities, so the merchants had to deliver goods from the warehouse to the shop.Let's call a road important for the merchant if its destruction threatens to ruin the merchant, that is, without this road there is no path from the merchant's warehouse to his shop. Merchants in the Roman Empire are very greedy, so each merchant pays a tax (1 dinar) only for those roads which are important for him. In other words, each merchant pays di dinars of tax, where di (di β₯ 0) is the number of roads important for the i-th merchant.The tax collection day came in the Empire. The Smart Beaver from ABBYY is very curious by nature, so he decided to count how many dinars each merchant had paid that day. And now he needs your help.
|
The first input line contains two integers n and m, separated by a space, n is the number of cities, and m is the number of roads in the empire.The following m lines contain pairs of integers ai, bi (1 β€ ai, bi β€ n, ai β bi), separated by a space β the numbers of cities connected by the i-th road. It is guaranteed that any two cities are connected by no more than one road and that there exists a path between any two cities in the Roman Empire.The next line contains a single integer k β the number of merchants in the empire.The following k lines contain pairs of integers si, li (1 β€ si, li β€ n), separated by a space, β si is the number of the city in which the warehouse of the i-th merchant is located, and li is the number of the city in which the shop of the i-th merchant is located.The input limitations for getting 20 points are: 1 β€ n β€ 200 1 β€ m β€ 200 1 β€ k β€ 200 The input limitations for getting 50 points are: 1 β€ n β€ 2000 1 β€ m β€ 2000 1 β€ k β€ 2000 The input limitations for getting 100 points are: 1 β€ n β€ 105 1 β€ m β€ 105 1 β€ k β€ 105
|
Print exactly k lines, the i-th line should contain a single integer di β the number of dinars that the i-th merchant paid.
|
The given sample is illustrated in the figure below. Let's describe the result for the first merchant. The merchant's warehouse is located in city 1 and his shop is in city 5. Let us note that if either road, (1, 2) or (2, 3) is destroyed, there won't be any path between cities 1 and 5 anymore. If any other road is destroyed, the path will be preserved. That's why for the given merchant the answer is 2.
|
Input: 7 81 22 33 44 55 65 73 54 741 52 42 64 7 | Output: 2120
|
Medium
| 0 | 1,676 | 1,052 | 123 | 1 |
|
1,037 |
F
|
1037F
|
F. Maximum Reduction
| 2,500 |
combinatorics; data structures; math
|
Given an array \(a\) of \(n\) integers and an integer \(k\) (\(2 \le k \le n\)), where each element of the array is denoted by \(a_i\) (\(0 \le i < n\)). Perform the operation \(z\) given below on \(a\) and print the value of \(z(a,k)\) modulo \(10^{9}+7\).function z(array a, integer k): if length(a) < k: return 0 else: b = empty array ans = 0 for i = 0 .. (length(a) - k): temp = a[i] for j = i .. (i + k - 1): temp = max(temp, a[j]) append temp to the end of b ans = ans + temp return ans + z(b, k)
|
The first line of input contains two integers \(n\) and \(k\) (\(2 \le k \le n \le 10^6\)) β the length of the initial array \(a\) and the parameter \(k\).The second line of input contains \(n\) integers \(a_0, a_1, \ldots, a_{n - 1}\) (\(1 \le a_{i} \le 10^9\)) β the elements of the array \(a\).
|
Output the only integer, the value of \(z(a,k)\) modulo \(10^9+7\).
|
In the first example: for \(a=(9,1,10)\), \(ans=19\) and \(b=(9,10)\), for \(a=(9,10)\), \(ans=10\) and \(b=(10)\), for \(a=(10)\), \(ans=0\). So the returned value is \(19+10+0=29\).In the second example: for \(a=(5,8,7,1,9)\), \(ans=25\) and \(b=(8,8,9)\), for \(a=(8,8,9)\), \(ans=9\) and \(b=(9)\), for \(a=(9)\), \(ans=0\). So the returned value is \(25+9+0=34\).
|
Input: 3 29 1 10 | Output: 29
|
Expert
| 3 | 502 | 297 | 67 | 10 |
1,656 |
B
|
1656B
|
B. Subtract Operation
| 1,100 |
data structures; greedy; math; two pointers
|
You are given a list of \(n\) integers. You can perform the following operation: you choose an element \(x\) from the list, erase \(x\) from the list, and subtract the value of \(x\) from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly \(1\).Given an integer \(k\) (\(k>0\)), find if there is some sequence of \(n-1\) operations such that, after applying the operations, the only remaining element of the list is equal to \(k\).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 2\cdot 10^5\), \(1 \leq k \leq 10^9\)), the number of integers in the list, and the target value, respectively.The second line of each test case contains the \(n\) integers of the list \(a_1, a_2, \ldots, a_n\) (\(-10^9 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) over all test cases is not greater that \(2 \cdot 10^5\).
|
For each test case, print YES if you can achieve \(k\) with a sequence of \(n-1\) operations. Otherwise, print NO.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as a positive answer).
|
In the first example we have the list \(\{4, 2, 2, 7\}\), and we have the target \(k = 5\). One way to achieve it is the following: first we choose the third element, obtaining the list \(\{2, 0, 5\}\). Next we choose the first element, obtaining the list \(\{-2, 3\}\). Finally, we choose the first element, obtaining the list \(\{5\}\).
|
Input: 44 54 2 2 75 41 9 1 3 42 1717 02 1718 18 | Output: YES NO YES NO
|
Easy
| 4 | 479 | 607 | 246 | 16 |
1,009 |
F
|
1009F
|
F. Dominant Indices
| 2,300 |
data structures; dsu; trees
|
You are given a rooted undirected tree consisting of \(n\) vertices. Vertex \(1\) is the root.Let's denote a depth array of vertex \(x\) as an infinite sequence \([d_{x, 0}, d_{x, 1}, d_{x, 2}, \dots]\), where \(d_{x, i}\) is the number of vertices \(y\) such that both conditions hold: \(x\) is an ancestor of \(y\); the simple path from \(x\) to \(y\) traverses exactly \(i\) edges. The dominant index of a depth array of vertex \(x\) (or, shortly, the dominant index of vertex \(x\)) is an index \(j\) such that: for every \(k < j\), \(d_{x, k} < d_{x, j}\); for every \(k > j\), \(d_{x, k} \le d_{x, j}\). For every vertex in the tree calculate its dominant index.
|
The first line contains one integer \(n\) (\(1 \le n \le 10^6\)) β the number of vertices in a tree.Then \(n - 1\) lines follow, each containing two integers \(x\) and \(y\) (\(1 \le x, y \le n\), \(x \ne y\)). This line denotes an edge of the tree.It is guaranteed that these edges form a tree.
|
Output \(n\) numbers. \(i\)-th number should be equal to the dominant index of vertex \(i\).
|
Input: 41 22 33 4 | Output: 0000
|
Expert
| 3 | 668 | 295 | 92 | 10 |
|
1,606 |
E
|
1606E
|
E. Arena
| 2,100 |
combinatorics; dp; math
|
There are \(n\) heroes fighting in the arena. Initially, the \(i\)-th hero has \(a_i\) health points.The fight in the arena takes place in several rounds. At the beginning of each round, each alive hero deals \(1\) damage to all other heroes. Hits of all heroes occur simultaneously. Heroes whose health is less than \(1\) at the end of the round are considered killed.If exactly \(1\) hero remains alive after a certain round, then he is declared the winner. Otherwise, there is no winner.Your task is to calculate the number of ways to choose the initial health points for each hero \(a_i\), where \(1 \le a_i \le x\), so that there is no winner of the fight. The number of ways can be very large, so print it modulo \(998244353\). Two ways are considered different if at least one hero has a different amount of health. For example, \([1, 2, 1]\) and \([2, 1, 1]\) are different.
|
The only line contains two integers \(n\) and \(x\) (\(2 \le n \le 500; 1 \le x \le 500\)).
|
Print one integer β the number of ways to choose the initial health points for each hero \(a_i\), where \(1 \le a_i \le x\), so that there is no winner of the fight, taken modulo \(998244353\).
|
Input: 2 5 | Output: 5
|
Hard
| 3 | 882 | 91 | 193 | 16 |
|
2,004 |
F
|
2004F
|
F. Make a Palindrome
| 2,600 |
binary search; brute force; data structures; greedy; math
|
You are given an array \(a\) consisting of \(n\) integers.Let the function \(f(b)\) return the minimum number of operations needed to make an array \(b\) a palindrome. The operations you can make are: choose two adjacent elements \(b_i\) and \(b_{i+1}\), remove them, and replace them with a single element equal to \((b_i + b_{i + 1})\); or choose an element \(b_i > 1\), remove it, and replace it with two positive integers \(x\) and \(y\) (\(x > 0\) and \(y > 0\)) such that \(x + y = b_i\). For example, from an array \(b=[2, 1, 3]\), you can obtain the following arrays in one operation: \([1, 1, 1, 3]\), \([2, 1, 1, 2]\), \([3, 3]\), \([2, 4]\), or \([2, 1, 2, 1]\).Calculate \(\displaystyle \left(\sum_{1 \le l \le r \le n}{f(a[l..r])}\right)\), where \(a[l..r]\) is the subarray of \(a\) from index \(l\) to index \(r\), inclusive. In other words, find the sum of the values of the function \(f\) for all subarrays of the array \(a\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)).Additional constraint on the input: the sum of \(n\) over all test cases does not exceed \(2000\).
|
For each test case, print a single integer β the sum of the values of the function \(f\) for all subarrays of the array \(a\).
|
Input: 432 1 341 1 1 154 2 3 1 541 2 1 2 | Output: 3 0 14 5
|
Expert
| 5 | 943 | 373 | 126 | 20 |
|
1,219 |
G
|
1219G
|
G. Harvester
| 2,000 |
implementation
|
It is Bubble Cup finals season and farmer Johnny Bubbles must harvest his bubbles. The bubbles are in a rectangular bubblefield formed of \(N\) x \(M\) square parcels divided into \(N\) rows and \(M\) columns. The parcel in \(i^{th}\) row and \(j^{th}\) column yields \(A_{i,j}\) bubbles.Johnny Bubbles has available a very special self-driving bubble harvester that, once manually positioned at the beginning of a row or column, automatically harvests all the bubbles in that row or column. Once the harvester reaches the end of the row or column it stops and must be repositioned. The harvester can pass through any parcel any number of times, but it can collect bubbles from the parcel only once.Johnny is very busy farmer, so he is available to manually position the harvester at most four times per day. Johnny is also impatient, so he wants to harvest as many bubbles as possible on the first day.Please help Johnny to calculate what is the maximum number of bubbles he can collect on the first day.
|
The first line contains two integers \(N\) and \(M\) (\(1\) \(\leq\) \(N\), \(M\) \(\leq\) \(N\) * \(M\) \(\leq\) \(10^{5}\)) - the bubblefield size.Each of the next \(N\) lines contains \(M\) integers. The \(j^{th}\) element in the \(i^{th}\) line is \(A_{i,j}\) (\(0\) \(\leq\) \(a_{i,j}\) \(\leq\) \(10^{9}\)) β the yield of the parcel located in the \(i^{th}\) row and the \(j^{th}\) column.
|
Output contains one integer number - maximum number of the bubbles Johnny can harvest on the first day.
|
In the first example, farmer Johnny can harvest all the bubbles by positioning the harvester on the first and the second row.In the second example, one way Johnny can harvest maximum number of bubbles is to position the harvester in the second row, the fourth row, the second column and the fourth column.
|
Input: 2 2 1 2 3 4 | Output: 10
|
Hard
| 1 | 1,005 | 395 | 103 | 12 |
661 |
B
|
661B
|
B. Seasons
| 1,900 |
*special
|
You are given a name of a month. Output the season of the year to which it belongs (based on Northern hemisphere).
|
The input consists of a single string containing the name of one of the twelve months (January, February, March, April, May, June, July, August, September, October, November or December). The string is capitalized as given here.
|
Output a single string β the season of the year to which the given month belongs (winter, spring, summer or autumn). The name of the season should be in lowercase.
|
Assume that winter is December through February, spring is March through May, summer is June through August and autumn is September through November.
|
Input: April | Output: spring
|
Hard
| 1 | 114 | 228 | 163 | 6 |
1,141 |
G
|
1141G
|
G. Privatization of Roads in Treeland
| 1,900 |
binary search; constructive algorithms; dfs and similar; graphs; greedy; trees
|
Treeland consists of \(n\) cities and \(n-1\) roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right β the country's topology is an undirected tree.There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed \(k\) and the number of companies taking part in the privatization is minimal.Choose the number of companies \(r\) such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most \(k\). In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal \(r\) that there is such assignment to companies from \(1\) to \(r\) that the number of cities which are not good doesn't exceed \(k\). The picture illustrates the first example (\(n=6, k=2\)). The answer contains \(r=2\) companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number \(3\)) is not good. The number of such vertices (just one) doesn't exceed \(k=2\). It is impossible to have at most \(k=2\) not good cities in case of one company.
|
The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 200000, 0 \le k \le n - 1\)) β the number of cities and the maximal number of cities which can have two or more roads belonging to one company.The following \(n-1\) lines contain roads, one road per line. Each line contains a pair of integers \(x_i\), \(y_i\) (\(1 \le x_i, y_i \le n\)), where \(x_i\), \(y_i\) are cities connected with the \(i\)-th road.
|
In the first line print the required \(r\) (\(1 \le r \le n - 1\)). In the second line print \(n-1\) numbers \(c_1, c_2, \dots, c_{n-1}\) (\(1 \le c_i \le r\)), where \(c_i\) is the company to own the \(i\)-th road. If there are multiple answers, print any of them.
|
Input: 6 2 1 4 4 3 3 5 3 6 5 2 | Output: 2 1 2 1 1 2
|
Hard
| 6 | 1,657 | 423 | 265 | 11 |
|
988 |
C
|
988C
|
C. Equal Sums
| 1,400 |
implementation; sortings
|
You are given \(k\) sequences of integers. The length of the \(i\)-th sequence equals to \(n_i\).You have to choose exactly two sequences \(i\) and \(j\) (\(i \ne j\)) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence \(i\) (its length will be equal to \(n_i - 1\)) equals to the sum of the changed sequence \(j\) (its length will be equal to \(n_j - 1\)).Note that it's required to remove exactly one element in each of the two chosen sequences.Assume that the sum of the empty (of the length equals \(0\)) sequence is \(0\).
|
The first line contains an integer \(k\) (\(2 \le k \le 2 \cdot 10^5\)) β the number of sequences.Then \(k\) pairs of lines follow, each pair containing a sequence.The first line in the \(i\)-th pair contains one integer \(n_i\) (\(1 \le n_i < 2 \cdot 10^5\)) β the length of the \(i\)-th sequence. The second line of the \(i\)-th pair contains a sequence of \(n_i\) integers \(a_{i, 1}, a_{i, 2}, \dots, a_{i, n_i}\).The elements of sequences are integer numbers from \(-10^4\) to \(10^4\).The sum of lengths of all given sequences don't exceed \(2 \cdot 10^5\), i.e. \(n_1 + n_2 + \dots + n_k \le 2 \cdot 10^5\).
|
If it is impossible to choose two sequences such that they satisfy given conditions, print ""NO"" (without quotes). Otherwise in the first line print ""YES"" (without quotes), in the second line β two integers \(i\), \(x\) (\(1 \le i \le k, 1 \le x \le n_i\)), in the third line β two integers \(j\), \(y\) (\(1 \le j \le k, 1 \le y \le n_j\)). It means that the sum of the elements of the \(i\)-th sequence without the element with index \(x\) equals to the sum of the elements of the \(j\)-th sequence without the element with index \(y\).Two chosen sequences must be distinct, i.e. \(i \ne j\). You can print them in any order.If there are multiple possible answers, print any of them.
|
In the first example there are two sequences \([2, 3, 1, 3, 2]\) and \([1, 1, 2, 2, 2, 1]\). You can remove the second element from the first sequence to get \([2, 1, 3, 2]\) and you can remove the sixth element from the second sequence to get \([1, 1, 2, 2, 2]\). The sums of the both resulting sequences equal to \(8\), i.e. the sums are equal.
|
Input: 252 3 1 3 261 1 2 2 2 1 | Output: YES2 61 2
|
Easy
| 2 | 588 | 614 | 688 | 9 |
1,550 |
A
|
1550A
|
A. Find The Array
| 800 |
greedy; math
|
Let's call an array \(a\) consisting of \(n\) positive (greater than \(0\)) integers beautiful if the following condition is held for every \(i\) from \(1\) to \(n\): either \(a_i = 1\), or at least one of the numbers \(a_i - 1\) and \(a_i - 2\) exists in the array as well.For example: the array \([5, 3, 1]\) is beautiful: for \(a_1\), the number \(a_1 - 2 = 3\) exists in the array; for \(a_2\), the number \(a_2 - 2 = 1\) exists in the array; for \(a_3\), the condition \(a_3 = 1\) holds; the array \([1, 2, 2, 2, 2]\) is beautiful: for \(a_1\), the condition \(a_1 = 1\) holds; for every other number \(a_i\), the number \(a_i - 1 = 1\) exists in the array; the array \([1, 4]\) is not beautiful: for \(a_2\), neither \(a_2 - 2 = 2\) nor \(a_2 - 1 = 3\) exists in the array, and \(a_2 \ne 1\); the array \([2]\) is not beautiful: for \(a_1\), neither \(a_1 - 1 = 1\) nor \(a_1 - 2 = 0\) exists in the array, and \(a_1 \ne 1\); the array \([2, 1, 3]\) is beautiful: for \(a_1\), the number \(a_1 - 1 = 1\) exists in the array; for \(a_2\), the condition \(a_2 = 1\) holds; for \(a_3\), the number \(a_3 - 2 = 1\) exists in the array. You are given a positive integer \(s\). Find the minimum possible size of a beautiful array with the sum of elements equal to \(s\).
|
The first line contains one integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases.Then \(t\) lines follow, the \(i\)-th line contains one integer \(s\) (\(1 \le s \le 5000\)) for the \(i\)-th test case.
|
Print \(t\) integers, the \(i\)-th integer should be the answer for the \(i\)-th testcase: the minimum possible size of a beautiful array with the sum of elements equal to \(s\).
|
Consider the example test: in the first test case, the array \([1]\) meets all conditions; in the second test case, the array \([3, 4, 1]\) meets all conditions; in the third test case, the array \([1, 2, 4]\) meets all conditions; in the fourth test case, the array \([1, 4, 6, 8, 10, 2, 11]\) meets all conditions.
|
Input: 4 1 8 7 42 | Output: 1 3 3 7
|
Beginner
| 2 | 1,270 | 212 | 178 | 15 |
317 |
D
|
317D
|
D. Game with Powers
| 2,300 |
dp; games
|
Vasya and Petya wrote down all integers from 1 to n to play the ""powers"" game (n can be quite large; however, Vasya and Petya are not confused by this fact).Players choose numbers in turn (Vasya chooses first). If some number x is chosen at the current turn, it is forbidden to choose x or all of its other positive integer powers (that is, x2, x3, ...) at the next turns. For instance, if the number 9 is chosen at the first turn, one cannot choose 9 or 81 later, while it is still allowed to choose 3 or 27. The one who cannot make a move loses.Who wins if both Vasya and Petya play optimally?
|
Input contains single integer n (1 β€ n β€ 109).
|
Print the name of the winner β ""Vasya"" or ""Petya"" (without quotes).
|
In the first sample Vasya will choose 1 and win immediately.In the second sample no matter which number Vasya chooses during his first turn, Petya can choose the remaining number and win.
|
Input: 1 | Output: Vasya
|
Expert
| 2 | 597 | 46 | 71 | 3 |
670 |
D2
|
670D2
|
D2. Magic Powder - 2
| 1,500 |
binary search; implementation
|
The term of this problem is the same as the previous one, the only exception β increased restrictions.
|
The first line contains two positive integers n and k (1 β€ n β€ 100 000, 1 β€ k β€ 109) β the number of ingredients and the number of grams of the magic powder.The second line contains the sequence a1, a2, ..., an (1 β€ ai β€ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.The third line contains the sequence b1, b2, ..., bn (1 β€ bi β€ 109), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
|
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
|
Input: 1 100000000011000000000 | Output: 2000000000
|
Medium
| 2 | 102 | 500 | 132 | 6 |
|
346 |
D
|
346D
|
D. Robot Control
| 2,600 |
dp; graphs; shortest paths
|
The boss of the Company of Robot is a cruel man. His motto is ""Move forward Or Die!"". And that is exactly what his company's product do. Look at the behavior of the company's robot when it is walking in the directed graph. This behavior has been called ""Three Laws of Robotics"": Law 1. The Robot will destroy itself when it visits a vertex of the graph which it has already visited. Law 2. The Robot will destroy itself when it has no way to go (that is when it reaches a vertex whose out-degree is zero). Law 3. The Robot will move randomly when it has multiple ways to move (that is when it reach a vertex whose out-degree is more than one). Of course, the robot can move only along the directed edges of the graph. Can you imagine a robot behaving like that? That's why they are sold at a very low price, just for those who are short of money, including mzry1992, of course. mzry1992 has such a robot, and she wants to move it from vertex s to vertex t in a directed graph safely without self-destruction. Luckily, she can send her robot special orders at each vertex. A special order shows the robot which way to move, if it has multiple ways to move (to prevent random moving of the robot according to Law 3). When the robot reaches vertex t, mzry1992 takes it off the graph immediately. So you can see that, as long as there exists a path from s to t, she can always find a way to reach the goal (whatever the vertex t has the outdegree of zero or not). Sample 2 However, sending orders is expensive, so your task is to find the minimum number of orders mzry1992 needs to send in the worst case. Please note that mzry1992 can give orders to the robot while it is walking on the graph. Look at the first sample to clarify that part of the problem.
|
The first line contains two integers n (1 β€ n β€ 106) β the number of vertices of the graph, and m (1 β€ m β€ 106) β the number of edges. Then m lines follow, each with two integers ui and vi (1 β€ ui, vi β€ n; vi β ui), these integers denote that there is a directed edge from vertex ui to vertex vi. The last line contains two integers s and t (1 β€ s, t β€ n).It is guaranteed that there are no multiple edges and self-loops.
|
If there is a way to reach a goal, print the required minimum number of orders in the worst case. Otherwise, print -1.
|
Consider the first test sample. Initially the robot is on vertex 1. So, on the first step the robot can go to vertex 2 or 3. No matter what vertex the robot chooses, mzry1992 must give an order to the robot. This order is to go to vertex 4. If mzry1992 doesn't give an order to the robot at vertex 2 or 3, the robot can choose the ""bad"" outgoing edge (return to vertex 1) according Law 3. So, the answer is one.
|
Input: 4 61 22 11 33 12 43 41 4 | Output: 1
|
Expert
| 3 | 1,756 | 421 | 118 | 3 |
425 |
A
|
425A
|
A. Sereja and Swaps
| 1,500 |
brute force; sortings
|
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation:A swap operation is the following sequence of actions: choose two indexes i, j (i β j); perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Sereja get if he is allowed to perform at most k swap operations?
|
The first line contains two integers n and k (1 β€ n β€ 200; 1 β€ k β€ 10). The next line contains n integers a[1], a[2], ..., a[n] ( - 1000 β€ a[i] β€ 1000).
|
In a single line print the maximum value of m(a) that Sereja can get if he is allowed to perform at most k swap operations.
|
Input: 10 210 -1 2 2 2 2 2 2 -1 10 | Output: 32
|
Medium
| 2 | 355 | 152 | 123 | 4 |
|
1,749 |
E
|
1749E
|
E. Cactus Wall
| 2,400 |
constructive algorithms; dfs and similar; graphs; shortest paths
|
Monocarp is playing Minecraft and wants to build a wall of cacti. He wants to build it on a field of sand of the size of \(n \times m\) cells. Initially, there are cacti in some cells of the field. Note that, in Minecraft, cacti cannot grow on cells adjacent to each other by side β and the initial field meets this restriction. Monocarp can plant new cacti (they must also fulfil the aforementioned condition). He can't chop down any of the cacti that are already growing on the field β he doesn't have an axe, and the cacti are too prickly for his hands.Monocarp believes that the wall is complete if there is no path from the top row of the field to the bottom row, such that: each two consecutive cells in the path are adjacent by side; no cell belonging to the path contains a cactus. Your task is to plant the minimum number of cacti to build a wall (or to report that this is impossible).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n, m \le 2 \cdot 10^5\); \(n \times m \le 4 \cdot 10^5\)) β the number of rows and columns, respectively.Then \(n\) rows follow, \(i\)-th row contains a string \(s_i\) of length \(m\), where \(s_{i, j}\) is '#', if a cactus grows at the intersection of the \(i\)-th row and the \(j\)-th column. Otherwise, \(s_{i, j}\) is '.'.The sum of \(n \times m\) over all test cases does not exceed \(4 \cdot 10^5\).
|
For each test case, print NO in the first line if it is impossible to build a cactus wall without breaking the rules. Otherwise, print YES in the first line, then print \(n\) lines of \(m\) characters each β the field itself, where the \(j\)-th character of the \(i\)-th line is equal to '#', if there is a cactus on the intersection of the \(i\)-th row and the \(j\)-th column, otherwise it is '.'. If there are multiple optimal answers, print any of them.
|
Input: 42 4.#....#.3 3#.#....#.5 5.........................4 3#...#.#.#... | Output: YES .#.# #.#. NO YES ....# ...#. ..#.. .#... #.... YES #.. .#. #.# ...
|
Expert
| 4 | 895 | 578 | 457 | 17 |
|
1,451 |
C
|
1451C
|
C. String Equality
| 1,400 |
dp; greedy; hashing; implementation; strings
|
Ashish has two strings \(a\) and \(b\), each of length \(n\), and an integer \(k\). The strings only contain lowercase English letters.He wants to convert string \(a\) into string \(b\) by performing some (possibly zero) operations on \(a\).In one move, he can either choose an index \(i\) (\(1 \leq i\leq n-1\)) and swap \(a_i\) and \(a_{i+1}\), or choose an index \(i\) (\(1 \leq i \leq n-k+1\)) and if \(a_i, a_{i+1}, \ldots, a_{i+k-1}\) are all equal to some character \(c\) (\(c \neq\) 'z'), replace each one with the next character \((c+1)\), that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string \(a\). Help Ashish determine if it is possible to convert string \(a\) into \(b\) after performing some (possibly zero) operations on it.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of each test case is as follows.The first line of each test case contains two integers \(n\) (\(2 \leq n \leq 10^6\)) and \(k\) (\(1 \leq k \leq n\)).The second line of each test case contains the string \(a\) of length \(n\) consisting of lowercase English letters.The third line of each test case contains the string \(b\) of length \(n\) consisting of lowercase English letters.It is guaranteed that the sum of values \(n\) among all test cases does not exceed \(10^6\).
|
For each test case, print ""Yes"" if Ashish can convert \(a\) into \(b\) after some moves, else print ""No"".You may print the letters of the answer in any case (upper or lower).
|
In the first test case it can be shown that it is impossible to convert \(a\) into \(b\).In the second test case,""abba"" \(\xrightarrow{\text{inc}}\) ""acca"" \(\xrightarrow{\text{inc}}\) \(\ldots\) \(\xrightarrow{\text{inc}}\) ""azza"".Here ""swap"" denotes an operation of the first type, and ""inc"" denotes an operation of the second type.In the fourth test case,""aaabba"" \(\xrightarrow{\text{swap}}\) ""aaabab"" \(\xrightarrow{\text{swap}}\) ""aaaabb"" \(\xrightarrow{\text{inc}}\) \(\ldots\) \(\xrightarrow{\text{inc}}\) ""ddaabb"" \(\xrightarrow{\text{inc}}\) \(\ldots\) \(\xrightarrow{\text{inc}}\) ""ddddbb"" \(\xrightarrow{\text{inc}}\) \(\ldots\) \(\xrightarrow{\text{inc}}\) ""ddddcc"".
|
Input: 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc | Output: No Yes No Yes
|
Easy
| 5 | 855 | 589 | 178 | 14 |
1,533 |
G
|
1533G
|
G. Biome Map
| 0 |
*special; constructive algorithms; dfs and similar; graphs
|
Polycarp decided to generate a biome map for his game. A map is a matrix divided into cells \(1 \times 1\). Each cell of the map must contain one of the available biomes.Each biome is defined by two parameters: temperature (an integer from \(1\) to \(n\)) and humidity (an integer from \(1\) to \(m\)). But not for every temperature/humidity combination, a corresponding biome is available.The biome map should be generated according to the following rules: each cell of the map belongs to exactly one biome; each available biome has at least one cell on the map; if two cells of the map are adjacent by the side and they belong to biomes with parameters (\(t_1, h_1\)) and (\(t_2, h_2\)), respectively, then the equality \(|t_1-t_2| + |h_1-h_2| = 1\) holds; let the number of available biomes be equal to \(k\), then the number of rows and columns of the map (separately) should not exceed \(k\). Help Polycarp generate a biome map that meets all the conditions described above (or report that this is impossible).
|
The first line contains a single integer \(t\) (\(1 \le t \le 20\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10\)) β maximum temperature and humidity parameters.The following \(n\) lines contain \(m\) integers each \(a_{i,1}, a_{i, 2}, \dots, a_{i, m}\) (\(0 \le a_{i, j} \le 100\)), where \(a_{i, j}\) β the biome identifier with the parameters \((i, j)\), if \(a_{i, j} \neq 0\), otherwise the biome with such parameters is not available.All biome identifiers are different, and there are at least two biomes available.
|
For each test case, print the answer in the following format: print \(-1\) in a single line if there is no map that meets all the conditions; otherwise, in the first line, print two integers \(h\) and \(w\) β the number of rows and columns of the map, respectively. In the following \(h\) lines, print \(w\) integers β the identifiers of the biomes in the corresponding cells of the map.
|
Input: 4 2 3 0 2 5 0 1 0 3 5 0 3 4 9 11 1 5 0 10 12 0 6 7 0 0 2 2 2 0 0 5 1 2 13 37 | Output: 1 3 5 2 1 2 8 11 9 4 3 5 1 5 6 12 10 9 4 3 5 6 7 -1 1 2 13 37
|
Beginner
| 4 | 1,015 | 596 | 387 | 15 |
|
1,499 |
G
|
1499G
|
G. Graph Coloring
| 3,100 |
data structures; graphs; interactive
|
You are given a bipartite graph consisting of \(n_1\) vertices in the first part, \(n_2\) vertices in the second part, and \(m\) edges, numbered from \(1\) to \(m\). You have to color each edge into one of two colors, red and blue. You have to minimize the following value: \(\sum \limits_{v \in V} |r(v) - b(v)|\), where \(V\) is the set of vertices of the graph, \(r(v)\) is the number of red edges incident to \(v\), and \(b(v)\) is the number of blue edges incident to \(v\).Sounds classical and easy, right? Well, you have to process \(q\) queries of the following format: \(1\) \(v_1\) \(v_2\) β add a new edge connecting the vertex \(v_1\) of the first part with the vertex \(v_2\) of the second part. This edge gets a new index as follows: the first added edge gets the index \(m + 1\), the second β \(m + 2\), and so on. After adding the edge, you have to print the hash of the current optimal coloring (if there are multiple optimal colorings, print the hash of any of them). Actually, this hash won't be verified, you may print any number as the answer to this query, but you may be asked to produce the coloring having this hash; \(2\) β print the optimal coloring of the graph with the same hash you printed while processing the previous query. The query of this type will only be asked after a query of type \(1\), and there will be at most \(10\) queries of this type. If there are multiple optimal colorings corresponding to this hash, print any of them. Note that if an edge was red or blue in some coloring, it may change its color in next colorings.The hash of the coloring is calculated as follows: let \(R\) be the set of indices of red edges, then the hash is \((\sum \limits_{i \in R} 2^i) \bmod 998244353\).Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program.
|
The first line contains three integers \(n_1\), \(n_2\) and \(m\) (\(1 \le n_1, n_2, m \le 2 \cdot 10^5\)).Then \(m\) lines follow, the \(i\)-th of them contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i \le n_1\); \(1 \le y_i \le n_2\)) meaning that the \(i\)-th edge connects the vertex \(x_i\) from the first part and the vertex \(y_i\) from the second part.The next line contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of queries you have to process.The next \(q\) lines contain the queries in the format introduced in the statement.Additional constraints on the input: at any moment, the graph won't contain any multiple edges; the queries of type \(2\) are only asked if the previous query had type \(1\); there are at most \(10\) queries of type \(2\).
|
To answer a query of type \(1\), print one integer β the hash of the optimal coloring.To answer a query of type \(2\), print one line. It should begin with the integer \(k\) β the number of red edges. Then, \(k\) distinct integer should follow β the indices of red edges in your coloring, in any order. Each index should correspond to an existing edge, and the hash of the coloring you produce should be equal to the hash you printed as the answer to the previous query.If there are multiple answers to a query, you may print any of them.
|
Input: 3 4 2 1 2 3 4 10 1 1 3 1 2 3 2 1 3 3 2 1 2 4 2 1 2 1 1 1 1 2 | Output: 8 8 1 3 40 2 3 5 104 3 5 6 3 104 360 4 5 6 3 8
|
Master
| 3 | 2,020 | 786 | 538 | 14 |
|
1,811 |
A
|
1811A
|
A. Insert Digit
| 800 |
greedy; math; strings
|
You have a positive number of length \(n\) and one additional digit.You can insert this digit anywhere in the number, including at the beginning or at the end.Your task is to make the result as large as possible.For example, you have the number \(76543\), and the additional digit is \(4\). Then the maximum number you can get is \(765443\), and it can be obtained in two ways β by inserting a digit after the \(3\)th or after the \(4\)th digit of the number.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The descriptions of the test cases follow.The first line of the description of each test case contains two integers \(n\) and \(d\) (\(1 \le n \le 2 \cdot 10^5\); \(0 \le d \le 9\)) β the length of the number and an additional digit, respectively.The second line of the description of each test case contains a string consisting of \(n\) digits β the number that you have initially. It is guaranteed that the number does not contain leading zeros.It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a string consisting of \(n + 1\) digits β the maximum possible number that can be obtained.
|
Input: 115 4765431 012 5443 66665 6135795 89753119 498765432101234567895 7737378 1200000007 0705895912 1828127127732 | Output: 765443 10 544 6666 613579 987531 98765443210123456789 773737 210000000 70589590 8281271277321
|
Beginner
| 3 | 459 | 635 | 118 | 18 |
|
145 |
C
|
145C
|
C. Lucky Subsequence
| 2,100 |
combinatorics; dp; math
|
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has sequence a consisting of n integers.The subsequence of the sequence a is such subsequence that can be obtained from a by removing zero or more of its elements.Two sequences are considered different if index sets of numbers included in them are different. That is, the values βof the elements βdo not matter in the comparison of subsequences. In particular, any sequence of length n has exactly 2n different subsequences (including an empty subsequence).A subsequence is considered lucky if it has a length exactly k and does not contain two identical lucky numbers (unlucky numbers can be repeated any number of times).Help Petya find the number of different lucky subsequences of the sequence a. As Petya's parents don't let him play with large numbers, you should print the result modulo prime number 1000000007 (109 + 7).
|
The first line contains two integers n and k (1 β€ k β€ n β€ 105). The next line contains n integers ai (1 β€ ai β€ 109) β the sequence a.
|
On the single line print the single number β the answer to the problem modulo prime number 1000000007 (109 + 7).
|
In the first sample all 3 subsequences of the needed length are considered lucky.In the second sample there are 4 lucky subsequences. For them the sets of indexes equal (the indexation starts from 1): {1, 3}, {1, 4}, {2, 3} and {2, 4}.
|
Input: 3 210 10 10 | Output: 3
|
Hard
| 3 | 1,054 | 133 | 112 | 1 |
1,924 |
F
|
1924F
|
F. Anti-Proxy Attendance
| 3,500 |
constructive algorithms; dp; interactive; ternary search
|
This is an interactive problem!Mr. 1048576 is one of those faculty who hates wasting his time in taking class attendance. Instead of taking attendance the old-fashioned way, he decided to try out something new today.There are \(n\) students in his class, having roll numbers \(1\) to \(n\). He knows that exactly \(1\) student is absent today. In order to determine who is absent, he can ask some queries to the class. In each query, he can provide two integers \(l\) and \(r\) (\(1\leq l\leq r\leq n\)) and all students whose roll numbers are between \(l\) and \(r\) (inclusive) will raise their hands. He then counts them to determine if the roll number of the absent student lies between these values.Things seemed fine until his teaching assistant noticed something β the students are dishonest! Some students whose roll numbers lie in the given range may not raise their hands, while some other students whose roll number does not lie in the given range may raise their hands. But the students don't want to raise much suspicion. So, only the following \(4\) cases are possible for a particular query \((l,r)\) β True Positive: \(r-l+1\) students are present and \(r-l+1\) students raised their hands. True Negative: \(r-l\) students are present and \(r-l\) students raised their hands. False Positive: \(r-l\) students are present but \(r-l+1\) students raised their hands. False Negative: \(r-l+1\) students are present but \(r-l\) students raised their hands. In the first two cases, the students are said to be answering honestly, while in the last two cases, the students are said to be answering dishonestly. The students can mutually decide upon their strategy, not known to Mr. 1048576. Also, the students do not want to raise any suspicion and at the same time, want to create a lot of confusion. So, their strategy always meets the following two conditions β The students will never answer honestly \(3\) times in a row. The students will never answer dishonestly \(3\) times in a row. Mr. 1048576 is frustrated by this act of students. So, he is willing to mark at most \(2\) students as absent (though he knows that only one is). The attendance is said to be successful if the student who is actually absent is among those two. Also, due to limited class time, he can only ask up to \(\lceil\log_{1.116}{n}\rceil-1\) queries (weird numbers but okay). Help him complete a successful attendance.
|
For the first test case, the student with roll number \(2\) is absent and the truth sequence (see section for hacks) is TFFTFTTF. During execution of your solution, this test case will use a non-adaptive grader.For the second test case, the student with roll number \(4\) is absent, and the truth sequence is FFTFTTFT. During the execution of your solution, in this test case your program will interact with an adaptive grader. So, the actual answer might be different depending on your queries but will always remain consistent with the responses to the previous queries.
|
Input: 2 5 3 2 1 2 0 1 0 2 0 1 6 6 2 2 0 1 1 0 0 0 1 | Output: ? 1 4 ? 3 5 ? 2 2 ? 1 3 ? 3 3 ? 3 3 ! 3 ? 2 4 ? 4 4 ! 2 # ? 1 6 ? 1 3 ? 4 6 ? 1 1 ? 3 3 ? 5 5 ! 3 ? 2 2 ? 4 4 ! 4 #
|
Master
| 4 | 2,410 | 0 | 0 | 19 |
||
15 |
A
|
15A
|
A. Cottage Village
| 1,200 |
implementation; sortings
|
A new cottage village called Β«FlatvilleΒ» is being built in Flatland. By now they have already built in Β«FlatvilleΒ» n square houses with the centres on the Πx-axis. The houses' sides are parallel to the coordinate axes. It's known that no two houses overlap, but they can touch each other.The architect bureau, where Peter works, was commissioned to build a new house in Β«FlatvilleΒ». The customer wants his future house to be on the Πx-axis, to be square in shape, have a side t, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the Ox-axis and it shouldn't overlap any of the houses in the village.Peter was given a list of all the houses in Β«FlatvilleΒ». Would you help him find the amount of possible positions of the new house?
|
The first line of the input data contains numbers n and t (1 β€ n, t β€ 1000). Then there follow n lines, each of them contains two space-separated integer numbers: xi ai, where xi β x-coordinate of the centre of the i-th house, and ai β length of its side ( - 1000 β€ xi β€ 1000, 1 β€ ai β€ 1000).
|
Output the amount of possible positions of the new house.
|
It is possible for the x-coordinate of the new house to have non-integer value.
|
Input: 2 20 46 2 | Output: 4
|
Easy
| 2 | 819 | 292 | 57 | 0 |
1,578 |
I
|
1578I
|
I. Interactive Rays
| 3,300 |
geometry; interactive
|
This is an interactive problem.Your goal is to find a circle on a plane by shooting rays and getting the distance to the circle as a result. Interactor has three hidden integer parameters that are determined in advance for each test, but which you don't know β \(x_c\), \(y_c\), and \(r_c\). \((x_c, y_c)\) are coordinates of the circle's center and \(r_c\) is its radius. The absolute values of \(x_c\), \(y_c\), and \(r_c\) do not exceed \(10^5\), and \(1 \leq r_c \leq \sqrt{x_c^2 + y_c^2} - 1\).You can shoot rays that extend from the origin \((0,0)\) and go via a point \((x_q,y_q)\) with the integer coordinates you specify. For each ray, you get a distance from the ray to the circle or \(0\) if the ray intersects the circle.
|
Illustration of the queries from the example interaction.
|
Input: ? 0 -10 ? 10 -10 ? 10 0 ? 10 10 ? 10 20 ? 10 30 ! 20 10 10 | Output: 12.360679775 11.2132034356 0.0 0.0 3.416407865 5.8113883008
|
Master
| 2 | 733 | 0 | 0 | 15 |
||
1,656 |
G
|
1656G
|
G. Cycle Palindrome
| 3,200 |
constructive algorithms; graphs; math
|
We say that a sequence of \(n\) integers \(a_1, a_2, \ldots, a_n\) is a palindrome if for all \(1 \leq i \leq n\), \(a_i = a_{n-i+1}\). You are given a sequence of \(n\) integers \(a_1, a_2, \ldots, a_n\) and you have to find, if it exists, a cycle permutation \(\sigma\) so that the sequence \(a_{\sigma(1)}, a_{\sigma(2)}, \ldots, a_{\sigma(n)}\) is a palindrome. A permutation of \(1, 2, \ldots, n\) is a bijective function from \(\{1, 2, \ldots, n\}\) to \(\{1, 2, \ldots, n\}\). We say that a permutation \(\sigma\) is a cycle permutation if \(1, \sigma(1), \sigma^2(1), \ldots, \sigma^{n-1}(1)\) are pairwise different numbers. Here \(\sigma^m(1)\) denotes \(\underbrace{\sigma(\sigma(\ldots \sigma}_{m \text{ times}}(1) \ldots))\).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 3 \cdot 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the size of the sequence.The second line of each test case contains \(n\) integers \(a_1, \ldots, a_n\) (\(1 \leq a_i \leq n\)).The sum of \(n\) for all test cases is at most \(2 \cdot 10^5\).
|
For each test case, output one line with YES if a cycle permutation exists, otherwise output one line with NO.If the answer is YES, output one additional line with \(n\) integers \(\sigma(1), \sigma(2), \ldots, \sigma(n)\), the permutation. If there is more than one permutation, you may print any.
|
Input: 3 4 1 2 2 1 3 1 2 1 7 1 3 3 3 1 2 2 | Output: YES 3 1 4 2 NO YES 5 3 7 2 6 4 1
|
Master
| 3 | 738 | 475 | 298 | 16 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.