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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
358 |
E
|
358E
|
E. Dima and Kicks
| 2,300 |
brute force; dsu; graphs; implementation
|
Dima is a good person. In fact, he's great. But all good things come to an end...Seryozha is going to kick Dima just few times.. For this reason he divides the room into unit squares. Now the room is a rectangle n Γ m consisting of unit squares.For the beginning, Seryozha put Dima in a center of some square. Then he started to kick Dima (it is known, that he kicks Dima at least once). Each time when Dima is kicked he flyes up and moves into one of four directions (up, left, right, down). On each move Dima passes k (k > 1) unit of the length in the corresponding direction. Seryozha is really kind, so he kicks Dima in such way that Dima never meets the walls (in other words, Dima never leave the room's space). Seryozha is also dynamic character so Dima never flies above the same segment, connecting a pair of adjacent squares, twice.Seryozha kicks Dima for a long time, but Dima is not vindictive β Dima writes. Dima marked all squares in which he was staying or above which he was flying. Thanks to kicks, Dima does not remember the k value, so he asks you to find all possible values which matches to the Dima's records.
|
The first line contains n and m (1 β€ n, m β€ 103) β size of the room.Next n lines goes, each contains m numbers aij β Dima's notes: aij = 1, if Dima was staying in the square (i, j) or was flying above it. Otherwise aij = 0.At least one aij equals 1.
|
In a single line in accending order print all k (k > 1), which matches the Dima's notes. If there are no such k and Dima invented this story with kicks, print -1.
|
Input: 5 51 1 1 1 11 0 0 0 11 0 0 0 11 0 0 0 11 1 1 1 1 | Output: 2 4
|
Expert
| 4 | 1,131 | 249 | 162 | 3 |
|
1,605 |
F
|
1605F
|
F. PalindORme
| 2,900 |
combinatorics; dp
|
An integer array \(a\) of length \(n\) is said to be a PalindORme if (\(a_{1}\) \(|\) \(a_{2} \) \(|\) \( \ldots \) \(|\) \( a_{i}) = (a_{{n - i + 1}} \) \(|\) \( \ldots \) \(|\) \( a_{{n - 1}} \) \(|\) \( a_{n}) \) for all \( 1 \leq i \leq n\), where \(|\) denotes the bitwise OR operation.An integer array \(a\) of length \(n\) is considered to be good if its elements can be rearranged to form a PalindORme. Formally, array \(a\) is good if there exists a permutation \(p_1, p_2, \ldots p_n\) (an array where each integer from \(1\) to \(n\) appears exactly once) for which \(a_{p_1}, a_{p_2}, \ldots a_{p_n}\) is a PalindORme.Find the number of good arrays of length \(n\), consisting only of integers in the range \([0, 2^{k} - 1]\), and print it modulo some prime \(m\).Two arrays \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\) are considered to be different if there exists any \(i\) \((1 \leq i \leq n)\) such that \(a_i \ne b_i\).
|
The first and only line of the input contains three integers \(n\), \(k\) and \(m\) (\(1 \leq n,k \leq 80\), \(10^8 \lt m \lt 10^9\)). It is guaranteed that \(m\) is prime.
|
Print a single integer β the number of good arrays modulo \(m\).
|
In the first sample, both the possible arrays \([0]\) and \([1]\) are good.In the second sample, some examples of good arrays are: \([2, 1, 2]\) because it is already PalindORme. \([1, 1, 0]\) because it can rearranged to \([1, 0, 1]\) which is PalindORmeNote that \([1, 1, 0]\), \([1, 0, 1]\) and \([0, 1, 1]\) are all good arrays and are considered to be different according to the definition in the statement.In the third sample, an example of a good array is \([1, 0, 1, 4, 2, 5, 4]\). It can be rearranged to an array \(b = [1, 5, 0, 2, 4, 4, 1]\) which is a PalindORme because: \(\mathrm{OR}(1, 1)\) = \(\mathrm{OR}(7, 7)\) = \(1\) \(\mathrm{OR}(1, 2)\) = \(\mathrm{OR}(6, 7)\) = \(5\) \(\mathrm{OR}(1, 3)\) = \(\mathrm{OR}(5, 7)\) = \(5\) \(\mathrm{OR}(1, 4)\) = \(\mathrm{OR}(4, 7)\) = \(7\) \(\mathrm{OR}(1, 5)\) = \(\mathrm{OR}(3, 7)\) = \(7\) \(\mathrm{OR}(1, 6)\) = \(\mathrm{OR}(2, 7)\) = \(7\) \(\mathrm{OR}(1, 7)\) = \(\mathrm{OR}(1, 7)\) = \(7\)Here \(\mathrm{OR}(l, r)\) denotes \(b_{l}\) \(|\) \(b_{l+1} \) \(|\) \( \ldots \) \(|\) \( b_{r}\)
|
Input: 1 1 998244353 | Output: 2
|
Master
| 2 | 948 | 172 | 64 | 16 |
2,030 |
E
|
2030E
|
E. MEXimize the Score
| 2,200 |
combinatorics; data structures; dp; greedy; implementation; math
|
Suppose we partition the elements of an array \(b\) into any number \(k\) of non-empty multisets \(S_1, S_2, \ldots, S_k\), where \(k\) is an arbitrary positive integer. Define the score of \(b\) as the maximum value of \(\operatorname{MEX}(S_1)\)\(^{\text{β}}\)\( + \operatorname{MEX}(S_2) + \ldots + \operatorname{MEX}(S_k)\) over all possible partitions of \(b\) for any integer \(k\).Envy is given an array \(a\) of size \(n\). Since he knows that calculating the score of \(a\) is too easy for you, he instead asks you to calculate the sum of scores of all \(2^n - 1\) non-empty subsequences of \(a\).\(^{\text{β }}\) Since this answer may be large, please output it modulo \(998\,244\,353\).\(^{\text{β}}\)\(\operatorname{MEX}\) of a collection of integers \(c_1, c_2, \ldots, c_k\) is defined as the smallest non-negative integer \(x\) that does not occur in the collection \(c\). For example, \(\operatorname{MEX}([0,1,2,2]) = 3\) and \(\operatorname{MEX}([1,2,2]) = 0\)\(^{\text{β }}\)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.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < n\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the answer, modulo \(998\,244\,353\).
|
In the first testcase, we must consider seven subsequences: \([0]\): The score is \(1\). \([0]\): The score is \(1\). \([1]\): The score is \(0\). \([0,0]\): The score is \(2\). \([0,1]\): The score is \(2\). \([0,1]\): The score is \(2\). \([0,0,1]\): The score is \(3\). The answer for the first testcase is \(1+1+2+2+2+3=11\).In the last testcase, all subsequences have a score of \(0\).
|
Input: 430 0 140 0 1 150 0 1 2 241 1 1 1 | Output: 11 26 53 0
|
Hard
| 6 | 1,135 | 438 | 64 | 20 |
962 |
D
|
962D
|
D. Merge Equals
| 1,600 |
data structures; implementation
|
You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value \(x\) that occurs in the array \(2\) or more times. Take the first two occurrences of \(x\) in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, \(2 \cdot x\)).Determine how the array will look after described operations are performed.For example, consider the given array looks like \([3, 4, 1, 2, 2, 1, 1]\). It will be changed in the following way: \([3, 4, 1, 2, 2, 1, 1]~\rightarrow~[3, 4, 2, 2, 2, 1]~\rightarrow~[3, 4, 4, 2, 1]~\rightarrow~[3, 8, 2, 1]\).If the given array is look like \([1, 1, 3, 1, 1]\) it will be changed in the following way: \([1, 1, 3, 1, 1]~\rightarrow~[2, 3, 1, 1]~\rightarrow~[2, 3, 2]~\rightarrow~[3, 4]\).
|
The first line contains a single integer \(n\) (\(2 \le n \le 150\,000\)) β the number of elements in the array.The second line contains a sequence from \(n\) elements \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{9}\)) β the elements of the array.
|
In the first line print an integer \(k\) β the number of elements in the array after all the performed operations. In the second line print \(k\) integers β the elements of the array after all the performed operations.
|
The first two examples were considered in the statement.In the third example all integers in the given array are distinct, so it will not change.
|
Input: 73 4 1 2 2 1 1 | Output: 43 8 2 1
|
Medium
| 2 | 906 | 248 | 218 | 9 |
2,028 |
E
|
2028E
|
E. Alice's Adventures in the Rabbit Hole
| 2,300 |
combinatorics; dfs and similar; dp; games; greedy; math; probabilities; trees
|
Alice is at the bottom of the rabbit hole! The rabbit hole can be modeled as a tree\(^{\text{β}}\) which has an exit at vertex \(1\), and Alice starts at some vertex \(v\). She wants to get out of the hole, but unfortunately, the Queen of Hearts has ordered her execution. Each minute, a fair coin is flipped. If it lands heads, Alice gets to move to an adjacent vertex of her current location, and otherwise, the Queen of Hearts gets to pull Alice to an adjacent vertex of the Queen's choosing. If Alice ever ends up on any of the non-root leaves\(^{\text{β }}\) of the tree, Alice loses.Assuming both of them move optimally, compute the probability that Alice manages to escape for every single starting vertex \(1\le v\le n\). Since these probabilities can be very small, output them modulo \(998\,244\,353\).Formally, let \(M = 998\,244\,353\). It can be shown that the exact answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\). \(^{\text{β}}\)A tree is a connected simple graph which has \(n\) vertices and \(n-1\) edges.\(^{\text{β }}\)A leaf is a vertex that is connected to exactly one edge.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2\le n\le 2\cdot 10^5\)) β the number of vertices in the tree.The \(i\)-th of the next \(n - 1\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\) and \(x_i \neq y_i\)) β the edges of the tree. It is guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output \(n\) integers on one line β the probabilities of Alice escaping starting from vertex \(1, 2, \ldots, n\). Since these probabilities can be very small, output them modulo \(998\,244\,353\).
|
For the first test case: Alice escapes from the root (vertex \(1\)) by definition with probability \(1\). Alice immediately loses from vertices \(4\) and \(5\) since they are leaves. From the other two vertices, Alice escapes with probability \(\frac 12\) since the Queen will pull her to the leaves.
|
Input: 251 21 32 43 591 22 34 55 67 88 92 45 7 | Output: 1 499122177 499122177 0 0 1 499122177 0 332748118 166374059 0 443664157 720954255 0
|
Expert
| 8 | 1,340 | 588 | 216 | 20 |
729 |
B
|
729B
|
B. Spotlights
| 1,200 |
dp; implementation
|
Theater stage is a rectangular field of size n Γ m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) β left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.A position is good if two conditions hold: there is no actor in the cell the spotlight is placed to; there is at least one actor in the direction the spotlight projects. Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.
|
The first line contains two positive integers n and m (1 β€ n, m β€ 1000) β the number of rows and the number of columns in the plan.The next n lines contain m integers, 0 or 1 each β the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.
|
Print one integer β the number of good positions for placing the spotlight.
|
In the first example the following positions are good: the (1, 1) cell and right direction; the (1, 1) cell and down direction; the (1, 3) cell and left direction; the (1, 3) cell and down direction; the (1, 4) cell and left direction; the (2, 2) cell and left direction; the (2, 2) cell and up direction; the (2, 2) and right direction; the (2, 4) cell and left direction. Therefore, there are 9 good positions in this example.
|
Input: 2 40 1 0 01 0 1 0 | Output: 9
|
Easy
| 2 | 822 | 382 | 75 | 7 |
687 |
A
|
687A
|
A. NP-Hard Problem
| 1,500 |
dfs and similar; graphs
|
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it's impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).
|
The first line of the input contains two integers n and m (2 β€ n β€ 100 000, 1 β€ m β€ 100 000) β the number of vertices and the number of edges in the prize graph, respectively.Each of the next m lines contains a pair of integers ui and vi (1 β€ ui, vi β€ n), denoting an undirected edge between ui and vi. It's guaranteed the graph won't contain any self-loops or multiple edges.
|
If it's impossible to split the graph between Pari and Arya as they expect, print ""-1"" (without quotes).If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers β the indices of vertices. Note that because of m β₯ 1, vertex cover cannot be empty.
|
In the first sample, you can give the vertex number 2 to Arya and vertices numbered 1 and 3 to Pari and keep vertex number 4 for yourself (or give it someone, if you wish).In the second sample, there is no way to satisfy both Pari and Arya.
|
Input: 4 21 22 3 | Output: 12 21 3
|
Medium
| 2 | 776 | 376 | 476 | 6 |
1,196 |
C
|
1196C
|
C. Robot Breakout
| 1,500 |
implementation
|
\(n\) robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the \(i\)-th robot is currently located at the point having coordinates (\(x_i\), \(y_i\)). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers \(X\) and \(Y\), and when each robot receives this command, it starts moving towards the point having coordinates (\(X\), \(Y\)). The robot stops its movement in two cases: either it reaches (\(X\), \(Y\)); or it cannot get any closer to (\(X\), \(Y\)). Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let's denote the current coordinates of the robot as (\(x_c\), \(y_c\)). Then the movement system allows it to move to any of the four adjacent points: the first action allows it to move from (\(x_c\), \(y_c\)) to (\(x_c - 1\), \(y_c\)); the second action allows it to move from (\(x_c\), \(y_c\)) to (\(x_c\), \(y_c + 1\)); the third action allows it to move from (\(x_c\), \(y_c\)) to (\(x_c + 1\), \(y_c\)); the fourth action allows it to move from (\(x_c\), \(y_c\)) to (\(x_c\), \(y_c - 1\)). Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform.You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers \(X\) and \(Y\) so that each robot can reach the point (\(X\), \(Y\)). Is it possible to find such a point?
|
The first line contains one integer \(q\) (\(1 \le q \le 10^5\)) β the number of queries.Then \(q\) queries follow. Each query begins with one line containing one integer \(n\) (\(1 \le n \le 10^5\)) β the number of robots in the query. Then \(n\) lines follow, the \(i\)-th of these lines describes the \(i\)-th robot in the current query: it contains six integer numbers \(x_i\), \(y_i\), \(f_{i, 1}\), \(f_{i, 2}\), \(f_{i, 3}\) and \(f_{i, 4}\) (\(-10^5 \le x_i, y_i \le 10^5\), \(0 \le f_{i, j} \le 1\)). The first two numbers describe the initial location of the \(i\)-th robot, and the following four numbers describe which actions the \(i\)-th robot can use to move (\(f_{i, j} = 1\) if the \(i\)-th robot can use the \(j\)-th action, and \(f_{i, j} = 0\) if it cannot use the \(j\)-th action).It is guaranteed that the total number of robots over all queries does not exceed \(10^5\).
|
You should answer each query independently, in the order these queries appear in the input.To answer a query, you should do one of the following: if it is impossible to find a point that is reachable by all \(n\) robots, print one number \(0\) on a separate line; if it is possible to find a point that is reachable by all \(n\) robots, print three space-separated integers on the same line: \(1\) \(X\) \(Y\), where \(X\) and \(Y\) are the coordinates of the point reachable by all \(n\) robots. Both \(X\) and \(Y\) should not exceed \(10^5\) by absolute value; it is guaranteed that if there exists at least one point reachable by all robots, then at least one of such points has both coordinates not exceeding \(10^5\) by absolute value.
|
Input: 4 2 -1 -2 0 0 0 0 -1 -2 0 0 0 0 3 1 5 1 1 1 1 2 5 0 1 0 1 3 5 1 0 0 0 2 1337 1337 0 1 1 1 1336 1337 1 1 0 1 1 3 5 1 1 1 1 | Output: 1 -1 -2 1 2 5 0 1 -100000 -100000
|
Medium
| 1 | 1,926 | 893 | 741 | 11 |
|
2,005 |
A
|
2005A
|
A. Simple Palindrome
| 900 |
combinatorics; constructive algorithms; greedy; math
|
Narek has to spend 2 hours with some 2-year-old kids at the kindergarten. He wants to teach them competitive programming, and their first lesson is about palindromes.Narek found out that the kids only know the vowels of the English alphabet (the letters \(\mathtt{a}\), \(\mathtt{e}\), \(\mathtt{i}\), \(\mathtt{o}\), and \(\mathtt{u}\)), so Narek needs to make a string that consists of vowels only. After making the string, he'll ask the kids to count the number of subsequences that are palindromes. Narek wants to keep it simple, so he's looking for a string such that the amount of palindrome subsequences is minimal.Help Narek find a string of length \(n\), consisting of lowercase English vowels only (letters \(\mathtt{a}\), \(\mathtt{e}\), \(\mathtt{i}\), \(\mathtt{o}\), and \(\mathtt{u}\)), which minimizes the amount of palindrome\(^{\dagger}\) subsequences\(^{\ddagger}\) in it.\(^{\dagger}\) A string is called a palindrome if it reads the same from left to right and from right to left.\(^{\ddagger}\) String \(t\) is a subsequence of string \(s\) if \(t\) can be obtained from \(s\) by removing several (possibly, zero or all) characters from \(s\) and concatenating the remaining ones, without changing their order. For example, \(\mathtt{odocs}\) is a subsequence of \(\texttt{c}{\color{red}{\texttt{od}}}\texttt{ef}{\color{red}{\texttt{o}}}\texttt{r}{\color{red}{\texttt{c}}}\texttt{e}{\color{red}{\texttt{s}}}\).
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Subsequently, the description of each test case follows.The only line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the size of the string.
|
For each test case, output any string of length \(n\) that satisfies the above conditions.
|
In the first example, \(\texttt{uo}\) has only three palindrome subsequences: \(\texttt{u}\), \(\texttt{o}\), and the empty string. It can be shown that there is no better answer.In the third example, \(\texttt{oeiiua}\) has only eight palindrome subsequences: \(\texttt{o}\), \(\texttt{e}\), \(\texttt{i}\), \(\texttt{i}\), \(\texttt{u}\), \(\texttt{a}\), \(\texttt{ii}\), and the empty string. It can be shown that there is no better answer.
|
Input: 3236 | Output: uo iae oeiiua
|
Beginner
| 4 | 1,432 | 277 | 90 | 20 |
1,958 |
H
|
1958H
|
H. Composite Spells
| 2,600 |
*special; dp
|
Monocarp plays a fantasy RPG. His character is a mage, so he casts spells. There are two types of spells he knows β basic spells and composite spells.There are \(n\) basic spells in Monocarp's spell book, numbered from \(1\) to \(n\). Each basic spell simply changes the health of the target: either decreases it or increases it. The \(i\)-th basic spell changes the target's health value by \(b_i\) (increases by \(b_i\) if \(b_i\) is non-negative, or decreases by \(|b_i|\) if \(b_i\) is negative). If the target's health value goes to \(0\) or below, it dies, and all next spells cast at it do nothing.There are also \(m\) composite spells in the spell book, numbered from \(n+1\) to \(n+m\). Each composite spell is a sequence of other spells, cast in specific order. A composite spell can consist both of basic spells and composite spells; the \(i\)-th spell consists of \(s_i\) other spells, and each of those spells has index strictly less than \(i\) (so there is no situation that composite spells infinitely cast each other). So, actually, each composite spell can be considered a finite sequence of basic spells, although its length might be huge. Note that the same spell can appear in a composite spell multiple times.Monocarp has decided to cast the \((n+m)\)-th spell from his spell book. The target of this spell is a monster with an initial health value of \(hp\). Monocarp wants to know whether the monster will be killed or not, and if it will be killed, which basic spell will kill it.
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case is given as follows: the first line contains two integers \(n\) and \(hp\) (\(1 \le n \le 5000\); \(1 \le hp \le 10^{9}\)) β the number of basic spells and the initial health value of the monster; the second line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(-10^9 \le b_i \le 10^9\)) β the descriptions of basic spells; the third line contains one integer \(m\) (\(1 \le m \le 5000\)) β the number of composite spells; then \(m\) lines follow, the \(i\)-th of these lines describes the \((n+i)\)-th spell: it begins with an integer \(s_{n+i}\) (\(1 \le s_{n+i} \le 5000\)) denoting the length of the spell (the number of spells it consists of); then a sequence of integers from \(1\) to \((n+i-1)\) follows, denoting the sequence of spells. Additional constraints on the input: the sum of \(n\) over all test cases does not exceed \(5000\); the sum of \(m\) over all test cases does not exceed \(5000\); the total length of all composite spells over all test cases does not exceed \(5000\).
|
For each test case, print one integer: if the monster dies, print the index of the basic spell that kills the monster; otherwise, print \(-1\).
|
Input: 44 91 -2 3 -433 1 4 34 1 2 1 26 6 5 6 5 6 54 91 -2 3 -433 1 4 34 1 2 1 27 6 5 6 5 6 6 53 31-10 -20 3016 1 2 3 1 2 36 20-1 -5 -9 -7 -1 -143 6 5 24 3 3 7 66 4 8 4 4 6 73 6 5 7 | Output: 4 4 -1 -1
|
Expert
| 2 | 1,504 | 1,104 | 143 | 19 |
|
1,146 |
C
|
1146C
|
C. Tree Diameter
| 1,700 |
bitmasks; graphs; interactive
|
There is a weighted tree with \(n\) nodes and \(n-1\) edges. The nodes are conveniently labeled from \(1\) to \(n\). The weights are positive integers at most \(100\). Define the distance between two nodes to be the sum of edges on the unique path between the nodes. You would like to find the diameter of the tree. Diameter is the maximum distance between a pair of nodes.Unfortunately, the tree isn't given to you, but you can ask some questions about it. In one question, you can specify two nonempty disjoint sets of nodes \(p\) and \(q\), and the judge will return the maximum distance between a node in \(p\) and a node in \(q\). In the words, maximum distance between \(x\) and \(y\), where \(x \in p\) and \(y \in q\). After asking not more than \(9\) questions, you must report the maximum distance between any pair of nodes.
|
In the first example, the first tree looks as follows: In the first question, we have \(p = {1}\), and \(q = {2, 3, 4, 5}\). The maximum distance between a node in \(p\) and a node in \(q\) is \(9\) (the distance between nodes \(1\) and \(5\)).The second tree is a tree with two nodes with an edge with weight \(99\) between them.
|
Input: 2 5 9 6 10 9 10 2 99 | Output: 1 4 1 2 3 4 5 1 4 2 3 4 5 1 1 4 3 4 5 1 2 1 4 4 5 1 2 3 1 4 5 1 2 3 4 -1 10 1 1 1 2 -1 99
|
Medium
| 3 | 834 | 0 | 0 | 11 |
||
1,091 |
F
|
1091F
|
F. New Year and the Mallard Expedition
| 2,600 |
constructive algorithms; greedy
|
Bob is a duck. He wants to get to Alice's nest, so that those two can duck! Duck is the ultimate animal! (Image courtesy of See Bang) The journey can be represented as a straight line, consisting of \(n\) segments. Bob is located to the left of the first segment, while Alice's nest is on the right of the last segment. Each segment has a length in meters, and also terrain type: grass, water or lava. Bob has three movement types: swimming, walking and flying. He can switch between them or change his direction at any point in time (even when he is located at a non-integer coordinate), and doing so doesn't require any extra time. Bob can swim only on the water, walk only on the grass and fly over any terrain. Flying one meter takes \(1\) second, swimming one meter takes \(3\) seconds, and finally walking one meter takes \(5\) seconds.Bob has a finite amount of energy, called stamina. Swimming and walking is relaxing for him, so he gains \(1\) stamina for every meter he walks or swims. On the other hand, flying is quite tiring, and he spends \(1\) stamina for every meter flown. Staying in place does not influence his stamina at all. Of course, his stamina can never become negative. Initially, his stamina is zero.What is the shortest possible time in which he can reach Alice's nest?
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of segments of terrain. The second line contains \(n\) integers \(l_1, l_2, \dots, l_n\) (\(1 \leq l_i \leq 10^{12}\)). The \(l_i\) represents the length of the \(i\)-th terrain segment in meters.The third line contains a string \(s\) consisting of \(n\) characters ""G"", ""W"", ""L"", representing Grass, Water and Lava, respectively. It is guaranteed that the first segment is not Lava.
|
Output a single integer \(t\) β the minimum time Bob needs to reach Alice.
|
In the first sample, Bob first walks \(5\) meters in \(25\) seconds. Then he flies the remaining \(5\) meters in \(5\) seconds.In the second sample, Bob first swims \(10\) meters in \(30\) seconds. Then he flies over the patch of lava for \(10\) seconds.In the third sample, the water pond is much smaller. Bob first swims over the water pond, taking him \(3\) seconds. However, he cannot fly over the lava just yet, as he only has one stamina while he needs two. So he swims back for half a meter, and then half a meter forward, taking him \(3\) seconds in total. Now he has \(2\) stamina, so he can spend \(2\) seconds flying over the lava.In the fourth sample, he walks for \(50\) seconds, flies for \(10\) seconds, swims for \(15\) seconds, and finally flies for \(5\) seconds.
|
Input: 1 10 G | Output: 30
|
Expert
| 2 | 1,297 | 474 | 74 | 10 |
178 |
E1
|
178E1
|
E1. The Beaver's Problem - 2
| 1,900 |
Offering the ABBYY Cup participants a problem written by the Smart Beaver is becoming a tradition. He proposed the following problem.You are given a monochrome image, that is, an image that is composed of two colors (black and white). The image is given in raster form, that is, as a matrix of pixels' colors, and the matrix's size coincides with the size of the image.The white color on the given image corresponds to the background. Also, the image contains several black geometric shapes. It is known that the image can contain only two types of shapes: squares and circles. Your task is to count the number of circles and the number of squares which the given image contains.The squares on the image can be rotated arbitrarily. In addition, the image can possibly contain some noise arranged as follows: each pixel of the original image can change its color to the opposite with the probability of 20%. An example of an image that has no noise and the sides of the squares are parallel to the coordinate axes (two circles and three squares). An example of an image that has no noise and the squares are rotated arbitrarily (two circles and three squares). An example of an image that has noise and the squares are rotated arbitrarily (one circle and three squares).
|
The first input line contains a single integer n (1000 β€ n β€ 2000), which is the length and the width of the original image. Next n lines describe the matrix of colors of the image pixels. The i-th line contains exactly n integers aij (0 β€ aij β€ 1), separated by spaces. Value of aij = 0 corresponds to a white pixel and aij = 1 corresponds to a black one. It is guaranteed that the lengths of the sides of the squares and the diameters of the circles in the image are at least 15 pixels, and the distance between any two figures is at least 10 pixels. It is also guaranteed that a human can easily calculate the number of circles and squares in the original image. The total number of figures in the image doesn't exceed 50.The input limitations for getting 20 points are: These test cases have no noise and the sides of the squares are parallel to the coordinate axes. The input limitations for getting 50 points are: These test cases have no noise, but the squares are rotated arbitrarily. The input limitations for getting 100 points are: These test cases have noise and the squares are rotated arbitrarily.
|
Print exactly two integers, separated by a single space β the number of circles and the number of squares in the given image, correspondingly.
|
You are given a sample of original data for each difficulty level. The samples are available at http://codeforces.com/static/materials/contests/178/e-samples.zip .
|
Hard
| 0 | 1,269 | 1,111 | 142 | 1 |
||
356 |
D
|
356D
|
D. Bags and Coins
| 2,700 |
bitmasks; constructive algorithms; dp; greedy
|
When you were a child you must have been told a puzzle of bags and coins. Anyway, here's one of its versions: A horse has three bags. The first bag has one coin, the second bag has one coin and the third bag has three coins. In total, the horse has three coins in the bags. How is that possible? The answer is quite simple. The third bag contains a coin and two other bags. This problem is a generalization of the childhood puzzle. You have n bags. You know that the first bag contains a1 coins, the second bag contains a2 coins, ..., the n-th bag contains an coins. In total, there are s coins. Find the way to arrange the bags and coins so that they match the described scenario or else state that it is impossible to do.
|
The first line contains two integers n and s (1 β€ n, s β€ 70000) β the number of bags and the total number of coins. The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 70000), where ai shows the number of coins in the i-th bag.
|
If the answer doesn't exist, print -1. Otherwise, print n lines, on the i-th line print the contents of the i-th bag. The first number in the line, ci (0 β€ ci β€ ai), must represent the number of coins lying directly in the i-th bag (the coins in the bags that are in the i-th bag are not taken into consideration). The second number in the line, ki (0 β€ ki < n) must represent the number of bags that lie directly in the i-th bag (the bags that are inside the bags lying in the i-th bag are not taken into consideration). Next, the line must contain ki integers β the numbers of the bags that are lying directly in the i-th bag.The total number of coins in the solution must equal s. If we count the total number of coins the i-th bag in the solution has, we should get ai. No bag can directly lie in more than one bag. The bags can be nested in more than one level (see the second test case). If there are multiple correct answers, you can print any of them.
|
The pictures below show two possible ways to solve one test case from the statement. The left picture corresponds to the first test case, the right picture corresponds to the second one.
|
Input: 3 31 3 1 | Output: 1 01 2 3 11 0
|
Master
| 4 | 723 | 235 | 959 | 3 |
1,466 |
B
|
1466B
|
B. Last minute enhancements
| 800 |
dp; greedy
|
Athenaeus has just finished creating his latest musical composition and will present it tomorrow to the people of Athens. Unfortunately, the melody is rather dull and highly likely won't be met with a warm reception. His song consists of \(n\) notes, which we will treat as positive integers. The diversity of a song is the number of different notes it contains. As a patron of music, Euterpe watches over composers and guides them throughout the process of creating new melodies. She decided to help Athenaeus by changing his song to make it more diverse.Being a minor goddess, she cannot arbitrarily change the song. Instead, for each of the \(n\) notes in the song, she can either leave it as it is or increase it by \(1\).Given the song as a sequence of integers describing the notes, find out the maximal, achievable diversity.
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases. Then \(t\) test cases follow, each one is described in two lines.In the first line of each test case there is a single integer \(n\) (\(1 \leq n \leq 10^5\)) denoting the length of the song. The next line contains a sequence of \(n\) integers \(x_1, x_2, \ldots, x_n\) \((1 \leq x_1 \leq x_2 \leq \ldots \leq x_n \leq 2 \cdot n)\), describing the song.The sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, you should output a single line containing precisely one integer, the maximal diversity of the song, i.e. the maximal possible number of different elements in the final sequence.
|
In the first test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence \(1, \underline{3}, 2, 2, \underline{6}, \underline{7}\), which has \(5\) different elements (increased elements are underlined).In the second test case, Euterpe can increase the first element to obtain the sequence \(\underline{5}, 4\), which has \(2\) different elements.In the third test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence \(1, \underline{2}, 3, 4, \underline{5}, \underline{6}\), which has \(6\) different elements.
|
Input: 5 6 1 2 2 2 5 6 2 4 4 6 1 1 3 4 4 5 1 1 6 1 1 1 2 2 2 | Output: 5 2 6 1 3
|
Beginner
| 2 | 832 | 554 | 198 | 14 |
490 |
A
|
490A
|
A. Team Olympiad
| 800 |
greedy; implementation; sortings
|
The School β0 of the capital of Berland has n children studying in it. All the children in this school are gifted: some of them are good at programming, some are good at maths, others are good at PE (Physical Education). Hence, for each child we know value ti: ti = 1, if the i-th child is good at programming, ti = 2, if the i-th child is good at maths, ti = 3, if the i-th child is good at PE Each child happens to be good at exactly one of these three subjects.The Team Scientific Decathlon Olympias requires teams of three students. The school teachers decided that the teams will be composed of three children that are good at different subjects. That is, each team must have one mathematician, one programmer and one sportsman. Of course, each child can be a member of no more than one team.What is the maximum number of teams that the school will be able to present at the Olympiad? How should the teams be formed for that?
|
The first line contains integer n (1 β€ n β€ 5000) β the number of children in the school. The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 3), where ti describes the skill of the i-th child.
|
In the first line output integer w β the largest possible number of teams. Then print w lines, containing three numbers in each line. Each triple represents the indexes of the children forming the team. You can print both the teams, and the numbers in the triplets in any order. The children are numbered from 1 to n in the order of their appearance in the input. Each child must participate in no more than one team. If there are several solutions, print any of them.If no teams can be compiled, print the only line with value w equal to 0.
|
Input: 71 3 1 3 2 1 2 | Output: 23 5 26 7 4
|
Beginner
| 3 | 930 | 202 | 541 | 4 |
|
467 |
C
|
467C
|
C. George and Job
| 1,700 |
dp; implementation
|
The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.Given a sequence of n integers p1, p2, ..., pn. You are to choose k pairs of integers: [l1, r1], [l2, r2], ..., [lk, rk] (1 β€ l1 β€ r1 < l2 β€ r2 < ... < lk β€ rk β€ n; ri - li + 1 = m), in such a way that the value of sum is maximal possible. Help George to cope with the task.
|
The first line contains three integers n, m and k (1 β€ (m Γ k) β€ n β€ 5000). The second line contains n integers p1, p2, ..., pn (0 β€ pi β€ 109).
|
Print an integer in a single line β the maximum possible value of sum.
|
Input: 5 2 11 2 3 4 5 | Output: 9
|
Medium
| 2 | 491 | 143 | 70 | 4 |
|
1,581 |
B
|
1581B
|
B. Diameter of Graph
| 1,200 |
constructive algorithms; graphs; greedy; math
|
CQXYM wants to create a connected undirected graph with \(n\) nodes and \(m\) edges, and the diameter of the graph must be strictly less than \(k-1\). Also, CQXYM doesn't want a graph that contains self-loops or multiple edges (i.e. each edge connects two different vertices and between each pair of vertices there is at most one edge).The diameter of a graph is the maximum distance between any two nodes.The distance between two nodes is the minimum number of the edges on the path which endpoints are the two nodes.CQXYM wonders whether it is possible to create such a graph.
|
The input consists of multiple test cases. The first line contains an integer \(t (1 \leq t \leq 10^5)\) β the number of test cases. The description of the test cases follows.Only one line of each test case contains three integers \(n(1 \leq n \leq 10^9)\), \(m\), \(k\) \((0 \leq m,k \leq 10^9)\).
|
For each test case, print YES if it is possible to create the graph, or print NO if it is impossible. You can print each letter in any case (upper or lower).
|
In the first test case, the graph's diameter equal to 0.In the second test case, the graph's diameter can only be 2.In the third test case, the graph's diameter can only be 1.
|
Input: 5 1 0 3 4 5 3 4 6 3 5 4 1 2 1 1 | Output: YES NO YES NO NO
|
Easy
| 4 | 578 | 298 | 157 | 15 |
1,571 |
F
|
1571F
|
F. Kotlinforces
| 2,000 |
*special; constructive algorithms; dp
|
Kotlinforces is a web platfrom that hosts programming competitions.The staff of Kotlinforces is asked to schedule \(n\) programming competitions on the next \(m\) days. Each competition is held in multiple stages; the regulations of the \(i\)-th competition state that this competition should consist of exactly \(k_i\) stages, and each stage, starting from the second one, should be scheduled exactly \(t_i\) days after the previous stage. In other words, if the first stage of the \(i\)-th competition is scheduled on day \(x\), the second stage should be scheduled on day \(x+t_i\), the third stage β on day \(x+2t_i\), ..., the \(k_i\)-th stage (which is the last one) β on day \(x+(k_i-1)t_i\).All \(n\) competitions should be scheduled in such a way that they start and finish during the next \(m\) days, and on any of these \(m\) days, at most one stage of one competition is held (two stages of different competitions should not be scheduled on the same day).Is it possible to schedule all \(n\) competitions to meet these constraints?
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 5000\)) β the number of competitions and the number of days, respectively.Then \(n\) lines follow, each describing a competition which should be scheduled. The \(i\)-th line contains two integers \(k_i\) and \(t_i\) (\(2 \le k_i \le 5000\); \(1 \le t_i \le 2\)) β the parameters of the \(i\)-th competition.
|
If it is impossible to schedule all \(n\) competitions on the next \(m\) days so that there is at most one stage during each day, print -1.Otherwise, print \(n\) integers. The \(i\)-th integer should represent the day when the first stage of the \(i\)-th competition is scheduled; days are numbered from \(1\) to \(m\). If there are multiple answers, print any of them.
|
Input: 3 7 3 2 2 2 2 2 | Output: 2 5 1
|
Hard
| 3 | 1,043 | 378 | 369 | 15 |
|
359 |
A
|
359A
|
A. Table
| 1,000 |
constructive algorithms; greedy; implementation
|
Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns β from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) β€ p β€ max(x1, x2), min(y1, y2) β€ q β€ max(y1, y2).Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.
|
The first line contains exactly two integers n, m (3 β€ n, m β€ 50).Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.
|
Print a single number β the minimum number of operations Simon needs to carry out his idea.
|
In the first sample, the sequence of operations can be like this: For the first time you need to choose cell (2, 2) and corner (1, 1). For the second time you need to choose cell (2, 2) and corner (3, 3). For the third time you need to choose cell (2, 2) and corner (3, 1). For the fourth time you need to choose cell (2, 2) and corner (1, 3). In the second sample the sequence of operations can be like this: For the first time you need to choose cell (3, 1) and corner (4, 3). For the second time you need to choose cell (2, 3) and corner (1, 1).
|
Input: 3 30 0 00 1 00 0 0 | Output: 4
|
Beginner
| 3 | 930 | 378 | 91 | 3 |
1,153 |
E
|
1153E
|
E. Serval and Snake
| 2,200 |
binary search; brute force; interactive
|
This is an interactive problem.Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a \(n \times n\) grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die.Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of \(4\). Today Serval got up too late and only have time to make \(2019\) queries. As his best friend, can you help him find the positions of the head and the tail?Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length \(0\), that means it only has adjacent head and tail.Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries.
|
The first line contains a single integer \(n\) (\(2\leq n \leq 1000\)) β the size of the grid.
|
When you are ready to answer, you should print ! x1 y1 x2 y2, where \((x_1, y_1)\) represents the position of the head and \((x_2,y_2)\) represents the position of the tail. You can print head and tail in any order.
|
The pictures above show our queries and the answers in the first example. We first made a query for \((1,1)\) and got an answer \(1\), then found that it must be connected to exactly one other cell. Then we made a query for \((1,2)\) and got an answer of \(0\), then knew that the snake never entered it. So the cell connected to \((1,1)\) must be \((2,1)\). Then we made a query for \((2,2)\) and got an answer \(0\), then knew that it never entered \((2,2)\) as well. So the snake cannot leave \((2,1)\), which implies that the answer is \((1,1)\) and \((2,1)\). The pictures above show our queries and the answers in the second example. By making query to \((2,2)\) and receiving \(2\), we found that the snake occupies \((2,2)\). And by making query to rectangle from \((2,1)\) to \((2,3)\) and receiving answer \(0\), we knew that it never goes out of the rectangle from \((2,1)\) to \((2,3)\). Since the first answer is \(2\), both \((2,1)\) and \((2,3)\) must be occupied but none of others, so the answer is \((2,1)\) and \((2,3)\).
|
Input: 2 1 0 0 | Output: ? 1 1 1 1 ? 1 2 1 2 ? 2 2 2 2 ! 1 1 2 1
|
Hard
| 3 | 1,326 | 94 | 215 | 11 |
1,131 |
G
|
1131G
|
G. Most Dangerous Shark
| 2,700 |
data structures; dp; two pointers
|
Semyon participates in the most prestigious competition of the world ocean for the title of the most dangerous shark. During this competition sharks compete in different subjects: speed swimming, masking, map navigation and many others. Now Semyon is taking part in Β«destructionΒ» contest.During it, \(m\) dominoes are placed in front of the shark. All dominoes are on the same line, but the height of the dominoes may vary. The distance between adjacent dominoes is \(1\). Moreover, each Domino has its own cost value, expressed as an integer. The goal is to drop all the dominoes. To do this, the shark can push any domino to the left or to the right, and it will begin falling in this direction. If during the fall the domino touches other dominoes, they will also start falling in the same direction in which the original domino is falling, thus beginning a chain reaction, as a result of which many dominoes can fall. A falling domino touches another one, if and only if the distance between them was strictly less than the height of the falling domino, the dominoes do not necessarily have to be adjacent.Of course, any shark can easily drop all the dominoes in this way, so the goal is not to drop all the dominoes, but do it with a minimum cost. The cost of the destruction is the sum of the costs of dominoes that the shark needs to push to make all the dominoes fall.Simon has already won in the previous subjects, but is not smart enough to win in this one. Help Semyon and determine the minimum total cost of the dominoes he will have to push to make all the dominoes fall.
|
In order to reduce input size, the heights and costs of the dominoes are described with blocks.The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 250\,000, 1 \leq m \leq 10^7\)) β the number of the blocks and the total number of the dominoes Semyon must drop.Then descriptions of \(n\) blocks follow. Description of every block consists of three lines.The first line of block's description contains a single integer \(k_i\) (\(1 \leq k_i \leq 250\,000, \sum_{i = 1}^{n}{k_i} \leq 250\,000\)) β the number of dominoes in the block.The second line of block's description contains \(k_i\) integers \(a_j\) (\(1 \leq a_j \leq m\)) β the heights of the dominoes in the blocks.The third line contains \(k_i\) integers \(c_j\) (\(1 \leq c_j \leq 100\,000\)) β the costs of the dominoes in the block.Then the domino sequence is described (from left to right).The first line of this description contains a single integer \(q\) (\(n \leq q \leq 250\,000\)) β the number of blocks in the sequence of domino sequence.Each of the following \(q\) lines contains integers \(id_i, mul_i\) (\(1 \leq id_i \leq n\), \(1 \leq mul_i \leq 100\,000\)), denoting that the next \(k_{id_i}\) dominoes are dominoes of the block \(id_i\), with their cost multiplied by \(mul_i\).It's guaranteed, that \(\sum_{i = 1}^{q}{k_{id_i}} = m\), and that's every block is used in the sequence at least once.
|
Print exactly one integer β the minimum cost to make all the dominoes fall.
|
In the first example, there are \(7\) dominoes in front of the Semyon. Their heights are equal to \([3, 1, 2, 2, 1, 2, 2]\), and their costs are equal to \([4, 3, 6, 3, 1, 2, 1]\). Semyon should drop the domino with index \(7\) to the left, it will fall and drop the domino \(6\) as well. The domino \(6\) during the fall will drop the domino \(5\), however the latter will not drop any more dominoes. Then Semyon should drop domino with number \(1\) to the right and it will drop dominoes \(2\) and \(3\) after falling. And the domino \(3\) will drop the domino \(4\) after falling. Hence all dominoes are fallen this way.In the second example, there is a single domino of cost \(10000000000\).
|
Input: 2 7 3 1 2 2 1 2 1 1 3 2 3 2 2 1 3 1 1 | Output: 5
|
Master
| 3 | 1,584 | 1,390 | 75 | 11 |
616 |
C
|
616C
|
C. The Labyrinth
| 1,600 |
dfs and similar
|
You are given a rectangular field of n Γ m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side.Let's call a connected component any non-extendible set of cells such that any two of them are connected by the path of adjacent cells. It is a typical well-known definition of a connected component.For each impassable cell (x, y) imagine that it is an empty cell (all other cells remain unchanged) and find the size (the number of cells) of the connected component which contains (x, y). You should do it for each impassable cell independently.The answer should be printed as a matrix with n rows and m columns. The j-th symbol of the i-th row should be ""."" if the cell is empty at the start. Otherwise the j-th symbol of the i-th row should contain the only digit β- the answer modulo 10. The matrix should be printed without any spaces.To make your output faster it is recommended to build the output as an array of n strings having length m and print it as a sequence of lines. It will be much faster than writing character-by-character.As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
|
The first line contains two integers n, m (1 β€ n, m β€ 1000) β the number of rows and columns in the field.Each of the next n lines contains m symbols: ""."" for empty cells, ""*"" for impassable cells.
|
Print the answer as a matrix as described above. See the examples to precise the format of the output.
|
In first example, if we imagine that the central cell is empty then it will be included to component of size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner).
|
Input: 3 3*.*.*.*.* | Output: 3.3.5.3.3
|
Medium
| 1 | 1,420 | 201 | 102 | 6 |
37 |
A
|
37A
|
A. Towers
| 1,000 |
sortings
|
Little Vasya has received a young builderβs kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same.Vasya wants to construct the minimal number of towers from the bars. Help Vasya to use the bars in the best way possible.
|
The first line contains an integer N (1 β€ N β€ 1000) β the number of bars at Vasyaβs disposal. The second line contains N space-separated integers li β the lengths of the bars. All the lengths are natural numbers not exceeding 1000.
|
In one line output two numbers β the height of the largest tower and their total number. Remember that Vasya should use all the bars.
|
Input: 31 2 3 | Output: 1 3
|
Beginner
| 1 | 327 | 231 | 133 | 0 |
|
452 |
B
|
452B
|
B. 4-point polyline
| 1,800 |
brute force; constructive algorithms; geometry; trees
|
You are given a rectangular grid of lattice points from (0, 0) to (n, m) inclusive. You have to choose exactly 4 different points to build a polyline possibly with self-intersections and self-touching. This polyline should be as long as possible.A polyline defined by points p1, p2, p3, p4 consists of the line segments p1 p2, p2 p3, p3 p4, and its length is the sum of the lengths of the individual line segments.
|
The only line of the input contains two integers n and m (0 β€ n, m β€ 1000). It is guaranteed that grid contains at least 4 different points.
|
Print 4 lines with two integers per line separated by space β coordinates of points p1, p2, p3, p4 in order which represent the longest possible polyline.Judge program compares your answer and jury's answer with 10 - 6 precision.
|
Input: 1 1 | Output: 1 10 01 00 1
|
Medium
| 4 | 414 | 140 | 229 | 4 |
|
48 |
G
|
48G
|
G. Galaxy Union
| 2,700 |
dp; trees; two pointers
|
In a far away galaxy there are n inhabited planets numbered with numbers from 1 to n. One day the presidents of all the n planets independently from each other came up with an idea of creating the Galaxy Union. Now they need to share this wonderful idea with their galaxymates, thatβs why each president is busy working out a project of negotiating with the other presidents.For negotiations between some pairs of the planets there are bidirectional communication channels, each of which is characterized with ""dial duration"" ti which, as a rule, takes several hours and exceeds the call duration greatly. Overall the galaxy has n communication channels and they unite all the planets into a uniform network. That means that it is possible to phone to any planet v from any planet u, perhaps, using some transitional planets v1, v2, ..., vm via the existing channels between u and v1, v1 and v2, ..., vm - 1 and vm, vm and v. At that the dial duration from u to v will be equal to the sum of dial durations of the used channels.So, every president has to talk one by one to the presidents of all the rest n - 1 planets. At that the negotiations take place strictly consecutively, and until the negotiations with a planet stop, the dial to another one does not begin. As the matter is urgent, from the different ways to call the needed planet every time the quickest one is chosen. Little time is needed to assure another president on the importance of the Galaxy Union, thatβs why the duration of the negotiations with each planet can be considered equal to the dial duration time for those planets. As the presidents know nothing about each otherβs plans, they do not take into consideration the possibility that, for example, the sought president may call himself or already know about the founding of the Galaxy Union from other sources.The governments of all the n planets asked you to work out the negotiation plans. First you are to find out for every president how much time his supposed negotiations will take.
|
The first line contains an integer n (3 β€ n β€ 200000) which represents the number of planets in the Galaxy and the number of communication channels equal to it. The next n lines contain three integers each ai, bi and ti (1 β€ ai, bi β€ n, ai β bi, 1 β€ ti β€ 103) that represent the numbers of planet joined by a communication channel and its ""dial duration"". There can be no more than one communication channel between a pair of planets.
|
In the first line output n integers β the durations of the supposed negotiations for each president. Separate the numbers by spaces.
|
Input: 31 2 32 3 21 3 1 | Output: 4 5 3
|
Master
| 3 | 2,020 | 436 | 132 | 0 |
|
11 |
B
|
11B
|
B. Jumping Jack
| 1,600 |
math
|
Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point x. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach x.
|
The input data consists of only one integer x ( - 109 β€ x β€ 109).
|
Output the minimal number of jumps that Jack requires to reach x.
|
Input: 2 | Output: 3
|
Medium
| 1 | 384 | 65 | 65 | 0 |
|
1,970 |
E2
|
1970E2
|
E2. Trails (Medium)
| 2,000 |
dp; matrices
|
Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are \(m\) cabins, numbered 1 to \(m\). Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin \(i\) is connected with \(s_i\) short trails and \(l_i\) long trails to the lake.Each day, Harry walks a trail from the cabin where he currently is to Lake Geneva, and then from there he walks a trail to any of the \(m\) cabins (including the one he started in). However, as he has to finish the hike in a day, at least one of the two trails has to be short.How many possible combinations of trails can Harry take if he starts in cabin 1 and walks for \(n\) days?Give the answer modulo \(10^9 + 7\).
|
The first line contains the integers \(m\) and \(n\).The second line contains \(m\) integers, \(s_1, \dots, s_m\), where \(s_i\) is the number of short trails between cabin \(i\) and Lake Geneva.The third and last line contains \(m\) integers, \(l_1, \dots, l_m\), where \(l_i\) is the number of long trails between cabin \(i\) and Lake Geneva.We have the following constraints:\(0 \le s_i, l_i \le 10^3\).\(1 \le m \le 10^2\).\(1 \le n \le 10^9\).
|
The number of possible combinations of trails, modulo \(10^9 + 7\).
|
Input: 3 21 0 10 1 1 | Output: 18
|
Hard
| 2 | 754 | 448 | 67 | 19 |
|
5 |
A
|
5A
|
A. Chat Server's Outgoing Traffic
| 1,000 |
implementation
|
Polycarp is working on a new project called ""Polychat"". Following modern tendencies in IT, he decided, that this project should contain chat as well. To achieve this goal, Polycarp has spent several hours in front of his laptop and implemented a chat server that can process three types of commands: Include a person to the chat ('Add' command). Remove a person from the chat ('Remove' command). Send a message from a person to all people, who are currently in the chat, including the one, who sends the message ('Send' command). Now Polycarp wants to find out the amount of outgoing traffic that the server will produce while processing a particular set of commands.Polycarp knows that chat server sends no traffic for 'Add' and 'Remove' commands. When 'Send' command is processed, server sends l bytes to each participant of the chat, where l is the length of the message.As Polycarp has no time, he is asking for your help in solving this problem.
|
Input file will contain not more than 100 commands, each in its own line. No line will exceed 100 characters. Formats of the commands will be the following: +<name> for 'Add' command. -<name> for 'Remove' command. <sender_name>:<message_text> for 'Send' command. <name> and <sender_name> is a non-empty sequence of Latin letters and digits. <message_text> can contain letters, digits and spaces, but can't start or end with a space. <message_text> can be an empty line.It is guaranteed, that input data are correct, i.e. there will be no 'Add' command if person with such a name is already in the chat, there will be no 'Remove' command if there is no person with such a name in the chat etc.All names are case-sensitive.
|
Print a single number β answer to the problem.
|
Input: +MikeMike:hello+Kate+Dmitry-DmitryKate:hi-Kate | Output: 9
|
Beginner
| 1 | 952 | 721 | 46 | 0 |
|
1,065 |
A
|
1065A
|
A. Vasya and Chocolate
| 800 |
implementation; math
|
There is a special offer in Vasya's favourite supermarket: if the customer buys \(a\) chocolate bars, he or she may take \(b\) additional bars for free. This special offer can be used any number of times.Vasya currently has \(s\) roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs \(c\) roubles. Help Vasya to calculate the maximum possible number of chocolate bars he can get!
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of testcases.Each of the next \(t\) lines contains four integers \(s, a, b, c~(1 \le s, a, b, c \le 10^9)\) β the number of roubles Vasya has, the number of chocolate bars you have to buy to use the special offer, the number of bars you get for free, and the cost of one bar, respectively.
|
Print \(t\) lines. \(i\)-th line should contain the maximum possible number of chocolate bars Vasya can get in \(i\)-th test.
|
In the first test of the example Vasya can buy \(9\) bars, get \(3\) for free, buy another bar, and so he will get \(13\) bars.In the second test Vasya buys \(1000000000\) bars and gets \(1000000000000000000\) for free. So he has \(1000000001000000000\) bars.
|
Input: 210 3 1 11000000000 1 1000000000 1 | Output: 131000000001000000000
|
Beginner
| 2 | 413 | 366 | 125 | 10 |
777 |
C
|
777C
|
C. Alyona and Spreadsheet
| 1,600 |
binary search; data structures; dp; greedy; implementation; two pointers
|
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j β€ ai + 1, j for all i from 1 to n - 1.Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j β€ ai + 1, j for all i from l to r - 1 inclusive.Alyona is too small to deal with this task and asks you to help!
|
The first line of the input contains two positive integers n and m (1 β€ nΒ·m β€ 100 000) β the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 β€ ai, j β€ 109).The next line of the input contains an integer k (1 β€ k β€ 100 000) β the number of task that teacher gave to Alyona.The i-th of the next k lines contains two integers li and ri (1 β€ li β€ ri β€ n).
|
Print ""Yes"" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print ""No"".
|
In the sample, the whole table is not sorted in any column. However, rows 1β3 are sorted in column 1, while rows 4β5 are sorted in column 3.
|
Input: 5 41 2 3 53 1 3 24 5 2 35 5 3 24 4 3 461 12 54 53 51 31 5 | Output: YesNoYesYesYesNo
|
Medium
| 6 | 832 | 609 | 183 | 7 |
1,221 |
D
|
1221D
|
D. Make The Fence Great Again
| 1,800 |
dp
|
You have a fence consisting of \(n\) vertical boards. The width of each board is \(1\). The height of the \(i\)-th board is \(a_i\). You think that the fence is great if there is no pair of adjacent boards having the same height. More formally, the fence is great if and only if for all indices from \(2\) to \(n\), the condition \(a_{i-1} \neq a_i\) holds.Unfortunately, it is possible that now your fence is not great. But you can change it! You can increase the length of the \(i\)-th board by \(1\), but you have to pay \(b_i\) rubles for it. The length of each board can be increased any number of times (possibly, zero).Calculate the minimum number of rubles you have to spend to make the fence great again!You have to answer \(q\) independent queries.
|
The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.The first line of each query contains one integers \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of boards in the fence.The following \(n\) lines of each query contain the descriptions of the boards. The \(i\)-th line contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 10^9\)) β the length of the \(i\)-th board and the price for increasing it by \(1\), respectively.It is guaranteed that sum of all \(n\) over all queries not exceed \(3 \cdot 10^5\).It is guaranteed that answer to each query will not exceed \(10^{18}\).
|
For each query print one integer β the minimum number of rubles you have to spend to make the fence great.
|
In the first query you have to increase the length of second board by \(2\). So your total costs if \(2 \cdot b_2 = 2\).In the second query you have to increase the length of first board by \(1\) and the length of third board by \(1\). So your total costs if \(1 \cdot b_1 + 1 \cdot b_3 = 9\).In the third query the fence is great initially, so you don't need to spend rubles.
|
Input: 3 3 2 4 2 1 3 5 3 2 3 2 10 2 6 4 1 7 3 3 2 6 1000000000 2 | Output: 2 9 0
|
Medium
| 1 | 758 | 636 | 106 | 12 |
808 |
C
|
808C
|
C. Tea Party
| 1,400 |
constructive algorithms; greedy; sortings
|
Polycarp invited all his friends to the tea party to celebrate the holiday. He has n cups, one for each of his n friends, with volumes a1, a2, ..., an. His teapot stores w milliliters of tea (w β€ a1 + a2 + ... + an). Polycarp wants to pour tea in cups in such a way that: Every cup will contain tea for at least half of its volume Every cup will contain integer number of milliliters of tea All the tea from the teapot will be poured into cups All friends will be satisfied. Friend with cup i won't be satisfied, if there exists such cup j that cup i contains less tea than cup j but ai > aj.For each cup output how many milliliters of tea should be poured in it. If it's impossible to pour all the tea and satisfy all conditions then output -1.
|
The first line contains two integer numbers n and w (1 β€ n β€ 100, ).The second line contains n numbers a1, a2, ..., an (1 β€ ai β€ 100).
|
Output how many milliliters of tea every cup should contain. If there are multiple answers, print any of them.If it's impossible to pour all the tea and satisfy all conditions then output -1.
|
In the third example you should pour to the first cup at least 5 milliliters, to the second one at least 4, to the third one at least 5. It sums up to 14, which is greater than 10 milliliters available.
|
Input: 2 108 7 | Output: 6 4
|
Easy
| 3 | 745 | 134 | 191 | 8 |
1,787 |
I
|
1787I
|
I. Treasure Hunt
| 3,400 |
data structures; divide and conquer; two pointers
|
Define the beauty value of a sequence \(b_1,b_2,\ldots,b_c\) as the maximum value of \(\sum\limits_{i=1}^{q}b_i + \sum\limits_{i=s}^{t}b_i\), where \(q\), \(s\), \(t\) are all integers and \(s > q\) or \(t\leq q\). Note that \(b_i = 0\) when \(i<1\) or \(i>c\), \(\sum\limits_{i=s}^{t}b_i = 0\) when \(s>t\).For example, when \(b = [-1,-2,-3]\), we may have \(q = 0\), \(s = 3\), \(t = 2\) so the beauty value is \(0 + 0 = 0\). And when \(b = [-1,2,-3]\), we have \(q = s = t = 2\) so the beauty value is \(1 + 2 = 3\).You are given a sequence \(a\) of length \(n\), determine the sum of the beauty value of all non-empty subsegments \(a_l,a_{l+1},\ldots,a_r\) (\(1\leq l\leq r\leq n\)) of the sequence \(a\).Print the answer modulo \(998\,244\,353\).
|
Each test contains multiple test cases. The first line contains an integer \(T\) (\(1 \le T \le 10^4\)) β the number of test cases.The first line contains an integer \(n\) (\(1\le n\le 10^6\)) β the length of \(a\).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(-10^6 \leq a_i \leq 10^6\)) β the given sequence.It's guaranteed that the sum of \(n\) does not exceed \(10^6\).
|
For each test case, print a line containing a single integer β the answer modulo \(998\,244\,353\).
|
In the second test case, for the subsequence \([-26,43,-41,34,13]\), when \(q=5\), \(s=2\), \(t=5\), \(\sum\limits_{i=1}^{q}b_i + \sum\limits_{i=s}^{t}b_i = 23 + 49 = 72\).In the third test case, there is only one non-empty consecutive subsequence \([74]\). When \(q=1\), \(s=1\), \(t=1\), \(\sum\limits_{i=1}^{q}b_i + \sum\limits_{i=s}^{t}b_i = 148\).
|
Input: 4780 59 100 -52 -86 -62 758-48 -14 -26 43 -41 34 13 551742056 -60 62 13 88 -48 64 36 -10 19 94 25 -69 88 87 79 -70 74 -26 59 | Output: 5924 2548 148 98887
|
Master
| 3 | 751 | 393 | 99 | 17 |
1,005 |
E2
|
1005E2
|
E2. Median on Segments (General Case Edition)
| 2,400 |
sortings
|
You are given an integer sequence \(a_1, a_2, \dots, a_n\).Find the number of pairs of indices \((l, r)\) (\(1 \le l \le r \le n\)) such that the value of median of \(a_l, a_{l+1}, \dots, a_r\) is exactly the given number \(m\).The median of a sequence is the value of an element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.For example, if \(a=[4, 2, 7, 5]\) then its median is \(4\) since after sorting the sequence, it will look like \([2, 4, 5, 7]\) and the left of two middle elements is equal to \(4\). The median of \([7, 1, 2, 9, 6]\) equals \(6\) since after sorting, the value \(6\) will be in the middle of the sequence.Write a program to find the number of pairs of indices \((l, r)\) (\(1 \le l \le r \le n\)) such that the value of median of \(a_l, a_{l+1}, \dots, a_r\) is exactly the given number \(m\).
|
The first line contains integers \(n\) and \(m\) (\(1 \le n,m \le 2\cdot10^5\)) β the length of the given sequence and the required value of the median.The second line contains an integer sequence \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2\cdot10^5\)).
|
Print the required number.
|
In the first example, the suitable pairs of indices are: \((1, 3)\), \((1, 4)\), \((1, 5)\), \((2, 2)\), \((2, 3)\), \((2, 5)\), \((4, 5)\) and \((5, 5)\).
|
Input: 5 41 4 5 60 4 | Output: 8
|
Expert
| 1 | 934 | 253 | 26 | 10 |
552 |
A
|
552A
|
A. Vanya and Table
| 1,000 |
implementation; math
|
Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result.
|
The first line contains integer n (1 β€ n β€ 100) β the number of rectangles.Each of the following n lines contains four integers x1, y1, x2, y2 (1 β€ x1 β€ x2 β€ 100, 1 β€ y1 β€ y2 β€ 100), where x1 and y1 are the number of the column and row of the lower left cell and x2 and y2 are the number of the column and row of the upper right cell of a rectangle.
|
In a single line print the sum of all values in the cells of the table.
|
Note to the first sample test:Values of the table in the first three rows and columns will be as follows:121121110So, the sum of values will be equal to 10.Note to the second sample test:Values of the table in the first three rows and columns will be as follows:222222222So, the sum of values will be equal to 18.
|
Input: 21 1 2 32 2 3 3 | Output: 10
|
Beginner
| 2 | 586 | 349 | 71 | 5 |
1,557 |
E
|
1557E
|
E. Assiut Chess
| 2,800 |
brute force; constructive algorithms; interactive
|
This is an interactive problem.ICPC Assiut Community decided to hold a unique chess contest, and you were chosen to control a queen and hunt down the hidden king, while a member of ICPC Assiut Community controls this king.You compete on an \(8\times8\) chessboard, the rows are numerated from top to bottom, and the columns are numerated left to right, and the cell in row \(x\) and column \(y\) is denoted as \((x, y)\).In one turn you can move the queen to any of the squares on the same horizontal line, vertical line, or any of the diagonals. For example, if the queen was on square (\(4\), \(5\)), you can move to (\(q_1\), \(5\)), (\(4\), \(q_1\)), (\(q_1\), \(9-q_1\)), or (\(q_2\), \(q_2+1\)) where (\(1 \le q_1 \le 8\), \(q_1 \ne 4\), \(1 \le q_2 \le 7\), \(q_2 \ne 4\)). Note that the queen cannot stay on its current cell. In one turn, the king can move ""Right"", ""Left"", ""Up"", ""Down"", ""Down-Right"", ""Down-Left"", ""Up-Left"", or ""Up-Right"" such that he doesn't get out of the board. The king cannot move into a cell that is on the same row, column or diagonal with the queen (including the position of the queen itself). For example, if the king was on square (\(4\), \(5\)), he can move to (\(4+k_1\), \(5+k_2\)) where (\(-1 \le k_1,k_2 \le 1\), \((k_1, k_2) \ne (0, 0)\)). At the start of the game, you should place the queen at any location on the board, and this is done once per game. After that the king is secretly placed at any cell different from the queen's location. You do not know the position of the king. Then, the king and the queen take turns with the king moving first. The king moves to one of the possible directions (""Right"", ""Down"", ""Up-Left"", etc.), and you are only given the direction it moves to. After that, you should move your queen by declaring the square to which your queen will move. The game follows like this until you win the game or run out of moves.You win if the king has no valid moves. You lose if after \(130\) moves of the queen the king still has valid moves.
|
The first line contains a single integer \(t\) (\(1 \le t \le 60\)) β the number of test cases.
|
In the example, the hidden king was at \((8, 8)\) at the start. The game follows like this:
|
Input: 1 Left Right Done | Output: 7 5 7 6 7 7
|
Master
| 3 | 2,033 | 95 | 0 | 15 |
|
1,095 |
E
|
1095E
|
E. Almost Regular Bracket Sequence
| 1,900 |
implementation
|
You are given a bracket sequence \(s\) consisting of \(n\) opening '(' and closing ')' brackets.A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences ""()()"", ""(())"" are regular (the resulting expressions are: ""(1)+(1)"", ""((1+1)+1)""), and "")("" and ""("" are not.You can change the type of some bracket \(s_i\). It means that if \(s_i = \) ')' then you can change it to '(' and vice versa.Your task is to calculate the number of positions \(i\) such that if you change the type of the \(i\)-th bracket, then the resulting bracket sequence becomes regular.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 10^6\)) β the length of the bracket sequence.The second line of the input contains the string \(s\) consisting of \(n\) opening '(' and closing ')' brackets.
|
Print one integer β the number of positions \(i\) such that if you change the type of the \(i\)-th bracket, then the resulting bracket sequence becomes regular.
|
Input: 6 (((()) | Output: 3
|
Hard
| 1 | 735 | 227 | 160 | 10 |
|
1,348 |
B
|
1348B
|
B. Phoenix and Beauty
| 1,400 |
constructive algorithms; data structures; greedy; sortings
|
Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length \(k\) have the same sum. A subarray of an array is any sequence of consecutive elements.Phoenix currently has an array \(a\) of length \(n\). He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between \(1\) and \(n\) inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 50\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 100\)).The second line of each test case contains \(n\) space-separated integers (\(1 \le a_i \le n\)) β the array that Phoenix currently has. This array may or may not be already beautiful.
|
For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines.The first line should contain the length of the beautiful array \(m\) (\(n \le m \le 10^4\)). You don't need to minimize \(m\).The second line should contain \(m\) space-separated integers (\(1 \le b_i \le n\)) β a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array \(a\). You may print integers that weren't originally in array \(a\).If there are multiple solutions, print any. It's guaranteed that if we can make array \(a\) beautiful, we can always make it with resulting length no more than \(10^4\).
|
In the first test case, we can make array \(a\) beautiful by inserting the integer \(1\) at index \(3\) (in between the two existing \(2\)s). Now, all subarrays of length \(k=2\) have the same sum \(3\). There exists many other possible solutions, for example: \(2, 1, 2, 1, 2, 1\) \(1, 2, 1, 2, 1, 2\) In the second test case, the array is already beautiful: all subarrays of length \(k=3\) have the same sum \(5\).In the third test case, it can be shown that we cannot insert numbers to make array \(a\) beautiful.In the fourth test case, the array \(b\) shown is beautiful and all subarrays of length \(k=4\) have the same sum \(10\). There exist other solutions also.
|
Input: 4 4 2 1 2 2 1 4 3 1 2 2 1 3 2 1 2 3 4 4 4 3 4 2 | Output: 5 1 2 1 2 1 4 1 2 2 1 -1 7 4 3 2 1 4 3 2
|
Easy
| 4 | 548 | 414 | 660 | 13 |
1,878 |
F
|
1878F
|
F. Vasilije Loves Number Theory
| 1,900 |
brute force; math; number theory
|
Vasilije is a smart student and his discrete mathematics teacher Sonja taught him number theory very well.He gave Ognjen a positive integer \(n\).Denote \(d(n)\) as the number of positive integer divisors of \(n\), and denote \(gcd(a, b)\) as the largest integer \(g\) such that \(a\) is divisible by \(g\) and \(b\) is divisible by \(g\).After that, he gave Ognjen \(q\) queries, and there are \(2\) types of queries. \(1\), \(x\) β set \(n\) to \(n \cdot x\), and then answer the following question: does there exist a positive integer \(a\) such that \(gcd(a, n) = 1\), and \(d(n \cdot a) = n\)? \(2\) β reset \(n\) to its initial value (before any queries). Note that \(n\) does not get back to its initial value after the type 1 query.Since Ognjen is afraid of number theory, Vasilije promised him that after each query, \(d(n) \le 10^9\), however, even with that constraint, he still needs your help with this problem.
|
The first line contains a positive integer \(t\), (\(1 \le t \le 100\)) β the number of test cases.The first line of each test case contains \(2\) integers, \(n\) and \(q\) (\(1 \le n \le 10^{6}\), \(1\le q \le 1000\)) β the number \(n\) and the number of queries.The following \(q\) lines contain an integer \(k\) (\(1 \le k \le 2\)), if \(k=1\) then there is another integer in this line \(x\) (\(1 \le x \le 10^6\)) β the description of the queries.It is guaranteed that, for the given input, \(d(n)\) does not exceed \(10^9\) at any point.It is guaranteed that the sum of \(q\) over all test cases doesn't exceed \(10^3\).
|
For each type 1 query, you should output ""YES"" if there exist such positive integer \(a\) that \(gcd(a, n) = 1\) and \(d(n \cdot a)=n\), and ""NO"" if he can't.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
|
In the first test case, we initially have \(n=1\).After the first query: \(n=1\), \(d(n)=1\), so by taking \(a = 1\), \(d(n \cdot a) = n\), and the answer to this query is ""YES"".After the second query: \(n=2\), \(d(n)=2\), we can, again, take \(a = 1\), \(d(n \cdot a) = n\), and the answer to this query is ""YES"".After the third query \(n=1\), and this is a type \(2\) query so we don't answer it.After the fourth query: \(n=8\), and by taking \(a=3\), \(d(n \cdot a) = d(24) = 8 = n\), so the answer is ""YES"".After the fifth query: \(n=72\), now we can take \(a=637\) to get \(n \cdot a = 45864\), and \(d(n \cdot a) = 72 = n\), so the answer is ""YES"".In the second test case, we initially have \(n=20\).After the first query: \(n=60\), and the answer is ""YES"".After the second query: \(n=20\), this is a type \(2\) query, so we don't answer it.After the third query: \(n=140\), and it can be proven that no matter which positive integer \(a\) we take, \(d(n \cdot a)\) will never be equal to \(n\), so the answer to this query is ""NO"".After the fourth query: \(n=1680\). It can be proven that there exists a positive integer \(a\), such that \(d(n \cdot a) = n\), so the answer is ""YES"".
|
Input: 71 51 11 221 81 920 41 321 71 1216 101 61 61 101 91 11 91 71 31 21 109 11 38 11 28 31 51 81 1011 51 81 21 11 31 1 | Output: YES YES YES YES YES NO YES YES NO YES YES YES NO YES NO YES YES NO NO YES NO NO YES NO NO NO NO
|
Hard
| 3 | 924 | 626 | 306 | 18 |
1,741 |
G
|
1741G
|
G. Kirill and Company
| 2,200 |
bitmasks; brute force; dfs and similar; dp; flows; graphs; shortest paths
|
Kirill lives on a connected undirected graph of \(n\) vertices and \(m\) edges at vertex \(1\). One fine evening he gathered \(f\) friends, the \(i\)-th friend lives at the vertex \(h_i\). So all friends are now in the vertex \(1\), the \(i\)-th friend must get to his home to the vertex \(h_i\).The evening is about to end and it is time to leave. It turned out that \(k\) (\(k \le 6\)) of his friends have no cars, and they would have to walk if no one gives them a ride. One friend with a car can give a ride to any number of friends without cars, but only if he can give them a ride by driving along one of the shortest paths to his house.For example, in the graph below, a friend from vertex \(h_i=5\) can give a ride to friends from the following sets of vertices: \([2, 3]\), \([2, 4]\), \([2]\), \([3]\), \([4]\), but can't give a ride to friend from vertex \(6\) or a set \([3, 4]\). The vertices where friends without cars live are highlighted in green, and with cars β in red. Kirill wants as few friends as possible to have to walk. Help him find the minimum possible number.
|
The first line of input data contains an integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases in the test.The first line of the test case contains two integers \(n\) and \(m\) (\(2 \le n \le 10^4\), \(n-1 \le m \le min (10^4, \)\( \frac{n \cdot (n - 1)}{2} \)\()\)) β the number of vertices and edges, respectively.The next \(m\) lines of the test case contain a description of the edges, two integers each \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) β indexes of vertices connected by an edge. It is guaranteed that there is at most one edge between any pair of vertices (i.e. no multiple edges in the graph).Then follows line containing the number \(f\) (\(1 \le f \le 10^4\)) β the number of Kirill's friends.The next line of the test case contains \(f\) integers: \(h_1, h_2, \dots, h_f\) (\(2 \le h_i \le n\)) β the vertices in which they live. Some vertices may be repeated.The next line of the set contains the number \(k\) (\(1 \le k \le min(6, f)\)) β the number of friends without cars.The last line of each test case contains \(k\) integers: \(p_1, p_2, \dots, p_k\) (\(1 \le p_i \le f\), \(p_i < p_{i+1}\)) β indexes of friends without cars.It is guaranteed that the sum of \(n\) over all cases does not exceed \(10^4\), as well as the sums of \(m\) and \(f\).
|
Output \(t\) lines, each of which contains the answer to the corresponding test case. As an answer, output a single integer β the minimum possible number of friends who will have to walk.
|
The first test case of the first example is explained in the statement.In the second test case of the first example, two friends with cars live at vertex \(5\), one can give a ride to friends from vertices \(2\) and \(3\), and the second from vertex \(4\), only a friend from vertex \(6\) will have to walk.
|
Input: 36 71 22 32 43 54 53 66 552 3 4 5 641 2 3 56 71 22 32 43 54 53 66 562 3 4 5 6 541 2 3 54 41 21 32 33 433 4 221 3 | Output: 2 1 1
|
Hard
| 7 | 1,087 | 1,291 | 187 | 17 |
21 |
B
|
21B
|
B. Intersection
| 2,000 |
implementation; math
|
You are given two set of points. The first set is determined by the equation A1x + B1y + C1 = 0, and the second one is determined by the equation A2x + B2y + C2 = 0.Write the program which finds the number of points in the intersection of two given sets.
|
The first line of the input contains three integer numbers A1, B1, C1 separated by space. The second line contains three integer numbers A2, B2, C2 separated by space. All the numbers are between -100 and 100, inclusive.
|
Print the number of points in the intersection or -1 if there are infinite number of points.
|
Input: 1 1 02 2 0 | Output: -1
|
Hard
| 2 | 254 | 220 | 92 | 0 |
|
1,662 |
E
|
1662E
|
E. Round Table
| 0 |
math
|
There are \(n\) people, numbered from \(1\) to \(n\), sitting at a round table. Person \(i+1\) is sitting to the right of person \(i\) (with person \(1\) sitting to the right of person \(n\)).You have come up with a better seating arrangement, which is given as a permutation \(p_1, p_2, \dots, p_n\). More specifically, you want to change the seats of the people so that at the end person \(p_{i+1}\) is sitting to the right of person \(p_i\) (with person \(p_1\) sitting to the right of person \(p_n\)). Notice that for each seating arrangement there are \(n\) permutations that describe it (which can be obtained by rotations).In order to achieve that, you can swap two people sitting at adjacent places; but there is a catch: for all \(1 \le x \le n-1\) you cannot swap person \(x\) and person \(x+1\) (notice that you can swap person \(n\) and person \(1\)). What is the minimum number of swaps necessary? It can be proven that any arrangement can be achieved.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 10\,000\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 200\,000\)) β the number of people sitting at the table. The second line contains \(n\) distinct integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n\), \(p_i \ne p_j\) for \(i \ne j\)) β the desired final order of the people around the table.The sum of the values of \(n\) over all test cases does not exceed \(200\,000\).
|
For each test case, print the minimum number of swaps necessary to achieve the desired order.
|
In the first test case, we can swap person \(4\) and person \(1\) (who are adjacent) in the initial configuration and get the order \([4, 2, 3, 1]\) which is equivalent to the desired one. Hence in this case a single swap is sufficient.
|
Input: 342 3 1 455 4 3 2 174 1 6 5 3 7 2 | Output: 1 10 22
|
Beginner
| 1 | 965 | 584 | 93 | 16 |
1,850 |
H
|
1850H
|
H. The Third Letter
| 1,700 |
dfs and similar; dsu; graphs; greedy; implementation
|
In order to win his toughest battle, Mircea came up with a great strategy for his army. He has \(n\) soldiers and decided to arrange them in a certain way in camps. Each soldier has to belong to exactly one camp, and there is one camp at each integer point on the \(x\)-axis (at points \(\cdots, -2, -1, 0, 1, 2, \cdots\)).The strategy consists of \(m\) conditions. Condition \(i\) tells that soldier \(a_i\) should belong to a camp that is situated \(d_i\) meters in front of the camp that person \(b_i\) belongs to. (If \(d_i < 0\), then \(a_i\)'s camp should be \(-d_i\) meters behind \(b_i\)'s camp.)Now, Mircea wonders if there exists a partition of soldiers that respects the condition and he asks for your help! Answer ""YES"" if there is a partition of the \(n\) soldiers that satisfies all of the \(m\) conditions and ""NO"" otherwise.Note that two different soldiers may be placed in the same camp.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains two positive integers \(n\) and \(m\) (\(2 \leq n \leq 2 \cdot 10^5\); \(1 \leq m \leq n\)) β the number of soldiers, and the number of conditions respectively.Then \(m\) lines follow, each of them containing \(3\) integers: \(a_i\), \(b_i\), \(d_i\) (\(a_i \neq b_i\); \(1 \leq a_i, b_i \leq n\); \(-10^9 \leq d_i \leq 10^9\)) β denoting the conditions explained in the statement. Note that if \(d_i\) is positive, \(a_i\) should be \(d_i\) meters in front of \(b_i\) and if it is negative, \(a_i\) should be \(-d_i\) meters behind \(b_i\).Note that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, output ""YES"" if there is an arrangement of the \(n\) soldiers that satisfies all of the \(m\) conditions and ""NO"" otherwise.
|
For the first test case, we can partition the soldiers into camps in the following way: soldier: Soldier \(1\) in the camp with the coordinate \(x = 3\). Soldier \(2\) in the camp with the coordinate \(x = 5\). Soldier \(3\) in the camp with the coordinate \(x = 9\). Soldier \(4\) in the camp with the coordinate \(x = 11\). For the second test case, there is no partition that can satisfy all the constraints at the same time.For the third test case, there is no partition that satisfies all the constraints since we get contradictory information about the same pair.For the fourth test case, in order to satisfy the only condition, a possible partition is: Soldier \(1\) in the camp with the coordinate \(x = 10\). Soldier \(2\) in the camp with the coordinate \(x = 13\). Soldier \(3\) in the camp with the coordinate \(x = -2023\). Soldier \(4\) in the camp with the coordinate \(x = -2023\).
|
Input: 45 31 2 22 3 44 2 -66 51 2 22 3 44 2 -65 4 43 5 1002 21 2 51 2 44 11 2 3 | Output: YES NO NO YES
|
Medium
| 5 | 908 | 760 | 148 | 18 |
1,015 |
E1
|
1015E1
|
E1. Stars Drawing (Easy Edition)
| 1,700 |
brute force; dp; greedy
|
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length \(0\) are not allowed).Let's consider empty cells are denoted by '.', then the following figures are stars: The leftmost figure is a star of size \(1\), the middle figure is a star of size \(2\) and the rightmost figure is a star of size \(3\). You are given a rectangular grid of size \(n \times m\) consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from \(1\) to \(n\), columns are numbered from \(1\) to \(m\). Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed \(n \cdot m\). Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most \(n \cdot m\) stars.
|
The first line of the input contains two integers \(n\) and \(m\) (\(3 \le n, m \le 100\)) β the sizes of the given grid.The next \(n\) lines contains \(m\) characters each, the \(i\)-th line describes the \(i\)-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
|
If it is impossible to draw the given grid using stars only, print ""-1"".Otherwise in the first line print one integer \(k\) (\(0 \le k \le n \cdot m\)) β the number of stars needed to draw the given grid. The next \(k\) lines should contain three integers each β \(x_j\), \(y_j\) and \(s_j\), where \(x_j\) is the row index of the central star character, \(y_j\) is the column index of the central star character and \(s_j\) is the size of the star. Each star should be completely inside the grid.
|
In the first example the output 23 4 13 5 2is also correct.
|
Input: 6 8....*......**.....*****....**.......*........... | Output: 33 4 13 5 23 5 1
|
Medium
| 3 | 1,175 | 299 | 499 | 10 |
542 |
B
|
542B
|
B. Duck Hunt
| 3,100 |
data structures
|
A duck hunter is doing his favorite thing, hunting. He lives in a two dimensional world and is located at point (0, 0). As he doesn't like walking for his prey, he prefers to shoot only vertically up (because in this case, the ducks fall straight into his hands). The hunter doesn't reload the gun immediately β r or more seconds must pass between the shots. When the hunter shoots up, the bullet immediately hits all the ducks who are directly above the hunter.In a two dimensional world each duck is a horizontal segment that moves horizontally in the negative direction of the Ox axis at the speed 1 length unit per second. For each duck we know the values hi and ti β the x-coordinates of its head (the left end of the segment) and its tail (the right end of the segment) at time 0. The height where the duck is flying isn't important as the gun shoots vertically up to the infinite height and hits all the ducks on its way. The figure to the first sample. What maximum number of ducks can the hunter shoot? The duck is considered shot by the hunter if at the moment of the shot at least one of its point intersects the Oy axis. After the hunter shoots the duck, it falls and it can't be shot anymore. The hunter cannot make shots before the moment of time 0.
|
The first line of the input contains integers n, r (1 β€ n β€ 200 000, 1 β€ r β€ 109) β the number of ducks and the minimum time in seconds between the shots. Then n lines follow, each of them contains two integers hi, ti ( - 109 β€ hi < ti β€ 109) β the x-coordinate of the head and tail of the i-th duck at the moment 0.
|
Print a single integer β the maximum number of ducks that can be shot by the hunter.
|
In the first sample the hunter must shoot at time 0, this shot kills ducks 1 and 3. Then the hunter needs to reload the gun and shoot again at time 3. His second shot hits the tail of duck 2.In the second sample the hunter can make shots at times 0 and 6 to hit three ducks.
|
Input: 3 3-3 01 3-1 2 | Output: 3
|
Master
| 1 | 1,263 | 316 | 84 | 5 |
2,038 |
K
|
2038K
|
K. Grid Walk
| 2,100 |
brute force; dp; greedy; math; number theory
|
You have an \(n \times n\) grid and two integers \(a\) and \(b\). Both the rows and the columns are numbered from \(1\) to \(n\). Let's denote the cell at the intersection of the \(i\)-th row and the \(j\)-th column as \((i, j)\).You are standing in the cell \((1, 1)\) and want to move into the cell \((n, n)\).Suppose you are in the cell \((i, j)\); in one step, you can move either into the cell \((i, j + 1)\) or into the cell \((i + 1, j)\) if the corresponding cells exist.Let's define the cost of the cell \((i, j)\) as \(c(i, j) = \gcd(i, a) + \gcd(j, b)\) (here, \(\gcd(x,y)\) denotes the greatest common divisor of \(x\) and \(y\)). The cost of the route from \((1, 1)\) to \((n, n)\) is the sum of costs of the visited cells (including the starting cell and the finishing cell). Find the route with minimum possible cost and print its cost.
|
The only line contains three integers \(n\), \(a\), and \(b\) (\(2 \le n \le 10^6\); \(1 \le a, b \le 10^6\)).
|
Print one integer β the cost of the cheapest route from \((1, 1)\) to \((n, n)\).
|
The first example is described in the picture above.
|
Input: 4 2 4 | Output: 21
|
Hard
| 5 | 851 | 110 | 81 | 20 |
253 |
A
|
253A
|
A. Boys and Girls
| 1,100 |
greedy
|
There are n boys and m girls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers from 1 to n + m. Then the number of integers i (1 β€ i < n + m) such that positions with indexes i and i + 1 contain children of different genders (position i has a girl and position i + 1 has a boy or vice versa) must be as large as possible. Help the children and tell them how to form the line.
|
The single line of the input contains two integers n and m (1 β€ n, m β€ 100), separated by a space.
|
Print a line of n + m characters. Print on the i-th position of the line character ""B"", if the i-th position of your arrangement should have a boy and ""G"", if it should have a girl. Of course, the number of characters ""B"" should equal n and the number of characters ""G"" should equal m. If there are multiple optimal solutions, print any of them.
|
In the first sample another possible answer is BGBGBG. In the second sample answer BBGBGB is also optimal.
|
Input: 3 3 | Output: GBGBGB
|
Easy
| 1 | 519 | 98 | 353 | 2 |
1,582 |
F2
|
1582F2
|
F2. Korney Korneevich and XOR (hard version)
| 2,400 |
binary search; brute force; dp; greedy; two pointers
|
This is a harder version of the problem with bigger constraints.Korney Korneevich dag up an array \(a\) of length \(n\). Korney Korneevich has recently read about the operation bitwise XOR, so he wished to experiment with it. For this purpose, he decided to find all integers \(x \ge 0\) such that there exists an increasing subsequence of the array \(a\), in which the bitwise XOR of numbers is equal to \(x\).It didn't take a long time for Korney Korneevich to find all such \(x\), and he wants to check his result. That's why he asked you to solve this problem!A sequence \(s\) is a subsequence of a sequence \(b\) if \(s\) can be obtained from \(b\) by deletion of several (possibly, zero or all) elements.A sequence \(s_1, s_2, \ldots , s_m\) is called increasing if \(s_1 < s_2 < \ldots < s_m\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^6\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 5000\)) β the elements of the array \(a\).
|
In the first line print a single integer \(k\) β the number of found \(x\) values.In the second line print \(k\) integers in increasing order \(x_1, x_2, \ldots x_k\) (\(0 \le x_1 < \ldots < x_k\)) β found \(x\) values.
|
In the first test case: To get value \(x = 0\) it is possible to choose and empty subsequence To get value \(x = 2\) it is possible to choose a subsequence \([2]\) To get value \(x = 4\) it is possible to choose a subsequence \([4]\) To get value \(x = 6\) it is possible to choose a subsequence \([2, 4]\)
|
Input: 4 4 2 2 4 | Output: 4 0 2 4 6
|
Expert
| 5 | 801 | 195 | 219 | 15 |
1,989 |
C
|
1989C
|
C. Two Movies
| 1,400 |
greedy; math
|
A movie company has released \(2\) movies. These \(2\) movies were watched by \(n\) people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie.If a person is asked to leave a review for the movie, then: if that person liked the movie, they will leave a positive review, and the movie's rating will increase by \(1\); if that person disliked the movie, they will leave a negative review, and the movie's rating will decrease by \(1\); otherwise, they will leave a neutral review, and the movie's rating will not change. Every person will review exactly one movie β and for every person, you can choose which movie they will review.The company's rating is the minimum of the ratings of the two movies. Your task is to calculate the maximum possible rating of the company.
|
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 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-1 \le a_i \le 1\)), where \(a_i\) is equal to \(-1\) if the first movie was disliked by the \(i\)-th viewer; equal to \(1\) if the first movie was liked; and \(0\) if the attitude is neutral.The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(-1 \le b_i \le 1\)), where \(b_i\) is equal to \(-1\) if the second movie was disliked by the \(i\)-th viewer; equal to \(1\) if the second movie was liked; and \(0\) if the attitude is neutral.Additional constraint on the input: the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the maximum possible rating of the company, if for each person, choose which movie to leave a review on.
|
Input: 42-1 1-1 -11-1-150 -1 1 0 1-1 1 0 0 14-1 -1 -1 1-1 1 1 1 | Output: 0 -1 1 1
|
Easy
| 2 | 847 | 820 | 149 | 19 |
|
1,168 |
B
|
1168B
|
B. Good Triple
| 1,900 |
brute force; two pointers
|
Toad Rash has a binary string \(s\). A binary string consists only of zeros and ones.Let \(n\) be the length of \(s\).Rash needs to find the number of such pairs of integers \(l\), \(r\) that \(1 \leq l \leq r \leq n\) and there is at least one pair of integers \(x\), \(k\) such that \(1 \leq x, k \leq n\), \(l \leq x < x + 2k \leq r\), and \(s_x = s_{x+k} = s_{x+2k}\).Find this number of pairs for Rash.
|
The first line contains the string \(s\) (\(1 \leq |s| \leq 300\,000\)), consisting of zeros and ones.
|
Output one integer: the number of such pairs of integers \(l\), \(r\) that \(1 \leq l \leq r \leq n\) and there is at least one pair of integers \(x\), \(k\) such that \(1 \leq x, k \leq n\), \(l \leq x < x + 2k \leq r\), and \(s_x = s_{x+k} = s_{x+2k}\).
|
In the first example, there are three \(l\), \(r\) pairs we need to count: \(1\), \(6\); \(2\), \(6\); and \(1\), \(5\).In the second example, there are no values \(x\), \(k\) for the initial string, so the answer is \(0\).
|
Input: 010101 | Output: 3
|
Hard
| 2 | 407 | 102 | 255 | 11 |
1,991 |
A
|
1991A
|
A. Maximize the Last Element
| 800 |
greedy; implementation
|
You are given an array \(a\) of \(n\) integers, where \(n\) is odd.In one operation, you will remove two adjacent elements from the array \(a\), and then concatenate the remaining parts of the array. For example, given the array \([4,7,4,2,9]\), we can obtain the arrays \([4,2,9]\) and \([4,7,9]\) by the operations \([\underline{4,7}, 4,2,9] \to [4,2,9]\) and \([4,7,\underline{4,2},9] \to [4,7,9]\) respectively. However, we cannot obtain the array \([7,2,9]\) as it requires deleting non-adjacent elements \([\underline{4},7,\underline{4},2,9]\).You will repeatedly perform this operation until exactly one element remains in \(a\).Find the maximum possible value of the remaining element in \(a\).
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 99\); \(n\) is odd) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\)) β the elements of the array \(a\).Note that there is no bound on the sum of \(n\) over all test cases.
|
For each test case, output a single integer β the maximum possible value of the remaining element in \(a\).
|
In the first test case, the array \(a\) is \([6]\). Since there is only one element, no operations are needed. The maximum possible value of the remaining element is \(6\).In the second test case, the array \(a\) is \([1, 3, 2]\). We can remove the first two elements \([\underline{1, 3}, 2] \to [2]\), or remove the last two elements \([1, \underline{3, 2}] \to [1]\). Therefore, the maximum possible value of the remaining element is \(2\).In the third test case, the array \(a\) is \([4, 7, 4, 2, 9]\). One way to maximize the remaining element is \([4, \underline{7, 4}, 2, 9] \to [\underline{4, 2}, 9] \to [9]\). Therefore, the maximum possible value of the remaining element is \(9\).In the fourth test case, the array \(a\) is \([3, 1, 4, 1, 5, 9, 2]\). It can be shown that the maximum possible value of the remaining element is \(5\).
|
Input: 41631 3 254 7 4 2 973 1 4 1 5 9 2 | Output: 6 2 9 5
|
Beginner
| 2 | 702 | 518 | 107 | 19 |
1,061 |
B
|
1061B
|
B. Views Matter
| 1,400 |
greedy; implementation; sortings
|
You came to the exhibition and one exhibit has drawn your attention. It consists of \(n\) stacks of blocks, where the \(i\)-th stack consists of \(a_i\) blocks resting on the surface.The height of the exhibit is equal to \(m\). Consequently, the number of blocks in each stack is less than or equal to \(m\).There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks. Find the maximum number of blocks you can remove such that the views for both the cameras would not change.Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\,000\), \(1 \le m \le 10^9\)) β the number of stacks and the height of the exhibit.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le m\)) β the number of blocks in each stack from left to right.
|
Print exactly one integer β the maximum number of blocks that can be removed.
|
The following pictures illustrate the first example and its possible solution.Blue cells indicate removed blocks. There are \(10\) blue cells, so the answer is \(10\).
|
Input: 5 63 3 3 3 3 | Output: 10
|
Easy
| 3 | 873 | 299 | 77 | 10 |
1,119 |
A
|
1119A
|
A. Ilya and a Colorful Walk
| 1,100 |
greedy; implementation
|
Ilya lives in a beautiful city of Chordalsk.There are \(n\) houses on the street Ilya lives, they are numerated from \(1\) to \(n\) from left to right; the distance between every two neighboring houses is equal to \(1\) unit. The neighboring houses are \(1\) and \(2\), \(2\) and \(3\), ..., \(n-1\) and \(n\). The houses \(n\) and \(1\) are not neighboring.The houses are colored in colors \(c_1, c_2, \ldots, c_n\) so that the \(i\)-th house is colored in the color \(c_i\). Everyone knows that Chordalsk is not boring, so there are at least two houses colored in different colors.Ilya wants to select two houses \(i\) and \(j\) so that \(1 \leq i < j \leq n\), and they have different colors: \(c_i \neq c_j\). He will then walk from the house \(i\) to the house \(j\) the distance of \((j-i)\) units.Ilya loves long walks, so he wants to choose the houses so that the distance between them is the maximum possible.Help Ilya, find this maximum possible distance.
|
The first line contains a single integer \(n\) (\(3 \leq n \leq 300\,000\)) β the number of cities on the street.The second line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \leq c_i \leq n\)) β the colors of the houses.It is guaranteed that there is at least one pair of indices \(i\) and \(j\) so that \(1 \leq i < j \leq n\) and \(c_i \neq c_j\).
|
Print a single integer β the maximum possible distance Ilya can walk.
|
In the first example the optimal way is to walk from the first house to the last one, where Ilya can walk the distance of \(5-1 = 4\) units.In the second example the optimal way is to either walk from the first house to the second or from the second to the third. Both these ways have the distance of \(1\) unit.In the third example the optimal way is to walk from the third house to the last one, where Ilya can walk the distance of \(7-3 = 4\) units.
|
Input: 5 1 2 3 2 3 | Output: 4
|
Easy
| 2 | 965 | 359 | 69 | 11 |
1,943 |
E2
|
1943E2
|
E2. MEX Game 2 (Hard Version)
| 3,300 |
binary search; greedy; two pointers
|
This is the hard version of the problem. The only difference between the two versions is the constraint on \(t\), \(m\) and the sum of \(m\). You can make hacks only if both versions of the problem are solved.Alice and Bob play yet another game on an array \(a\) of size \(n\). Alice starts with an empty array \(c\). Both players take turns playing, with Alice starting first.On Alice's turn, she picks one element from \(a\), appends that element to \(c\), and then deletes it from \(a\).On Bob's turn, he picks at most \(k\) elements from \(a\), and then deletes it from \(a\). The game ends when the array \(a\) is empty. Alice's score is defined to be the MEX\(^\dagger\) of \(c\). Alice wants to maximize her score while Bob wants to minimize it. Find Alice's final score if both players play optimally.The array will be given in compressed format. Instead of giving the elements present in the array, we will be giving their frequencies. Formally, you will be given \(m\), the maximum element in the array, and then \(m + 1\) integers \(f_0, f_1, \ldots, f_{m}\), where \(f_i\) represents the number of times \(i\) occurs in the array \(a\).\(^\dagger\) The \(\operatorname{MEX}\) (minimum excludant) of an array of integers is defined as the smallest non-negative integer which does not occur in the array. For example: The MEX of \([2,2,1]\) is \(0\), because \(0\) does not belong to the array. The MEX of \([3,1,0,1]\) is \(2\), because \(0\) and \(1\) belong to the array, but \(2\) does not. The MEX of \([0,3,1,2]\) is \(4\), because \(0\), \(1\), \(2\) and \(3\) belong to the array, but \(4\) does not.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(m\) and \(k\) (\(1 \le m \le 2 \cdot 10^5, 1 \le k \le 10^9\)).The second line contains \(m + 1\) integers \(f_0, f_1, \ldots, f_m\) (\(1 \le f_i \le 10^9\)).It is guaranteed the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, find Alice's score if both players play optimally.
|
In the first test case, the array \(a\) is \([0, 0, 0, 0, 1, 1, 1, 1, 1]\). A possible game with a score of \(2\) is as follows: Alice chooses the element \(0\). After this move, \(a = [0, 0, 0, 1, 1, 1, 1, 1]\) and \(c=[0]\). Bob chooses to remove the \(3\) elements \(0\), \(0\) and \(1\). After this move, \(a = [0, 1, 1, 1, 1]\) and \(c=[0]\). Alice chooses the element \(1\). After this move, \(a = [0,1,1,1]\) and \(c=[0,1]\). Bob removes the \(4\) remaining elements \(0\), \(1\), \(1\) and \(1\). After this move, \(a=[\,]\) and \(c=[0,1]\). At the end, \(c=[0,1]\) which has a MEX of \(2\). Note that this is an example game and does not necessarily represent the optimal strategy for both players.In the second test case, Alice can choose a \(0\) in her first turn, guaranteeing that her score is at least \(1\). While Bob can remove all copies element \(1\) in his first turn, thus guaranteeing that Alice's score cannot exceed \(1\). So Alice's score is \(1\) if both players play optimally.
|
Input: 51 44 52 10000000001000000000 1000000000 10000000003 22 3 100 11 12 23 11 1 1 1 | Output: 2 1 3 2 1
|
Master
| 3 | 1,618 | 484 | 70 | 19 |
1,251 |
B
|
1251B
|
B. Binary Palindromes
| 1,400 |
greedy; strings
|
A palindrome is a string \(t\) which reads the same backward as forward (formally, \(t[i] = t[|t| + 1 - i]\) for all \(i \in [1, |t|]\)). Here \(|t|\) denotes the length of a string \(t\). For example, the strings 010, 1001 and 0 are palindromes.You have \(n\) binary strings \(s_1, s_2, \dots, s_n\) (each \(s_i\) consists of zeroes and/or ones). You can swap any pair of characters any number of times (possibly, zero). Characters can be either from the same string or from different strings β there are no restrictions.Formally, in one move you: choose four integer numbers \(x, a, y, b\) such that \(1 \le x, y \le n\) and \(1 \le a \le |s_x|\) and \(1 \le b \le |s_y|\) (where \(x\) and \(y\) are string indices and \(a\) and \(b\) are positions in strings \(s_x\) and \(s_y\) respectively), swap (exchange) the characters \(s_x[a]\) and \(s_y[b]\). What is the maximum number of strings you can make palindromic simultaneously?
|
The first line contains single integer \(Q\) (\(1 \le Q \le 50\)) β the number of test cases.The first line on each test case contains single integer \(n\) (\(1 \le n \le 50\)) β the number of binary strings you have.Next \(n\) lines contains binary strings \(s_1, s_2, \dots, s_n\) β one per line. It's guaranteed that \(1 \le |s_i| \le 50\) and all strings constist of zeroes and/or ones.
|
Print \(Q\) integers β one per test case. The \(i\)-th integer should be the maximum number of palindromic strings you can achieve simultaneously performing zero or more swaps on strings from the \(i\)-th test case.
|
In the first test case, \(s_1\) is palindrome, so the answer is \(1\).In the second test case you can't make all three strings palindromic at the same time, but you can make any pair of strings palindromic. For example, let's make \(s_1 = \text{0110}\), \(s_2 = \text{111111}\) and \(s_3 = \text{010000}\).In the third test case we can make both strings palindromic. For example, \(s_1 = \text{11011}\) and \(s_2 = \text{100001}\).In the last test case \(s_2\) is palindrome and you can make \(s_1\) palindrome, for example, by swapping \(s_1[2]\) and \(s_1[3]\).
|
Input: 4 1 0 3 1110 100110 010101 2 11111 000001 2 001 11100111 | Output: 1 2 2 2
|
Easy
| 2 | 933 | 390 | 215 | 12 |
65 |
A
|
65A
|
A. Harry Potter and Three Spells
| 1,800 |
implementation; math
|
A long time ago (probably even in the first book), Nicholas Flamel, a great alchemist and the creator of the Philosopher's Stone, taught Harry Potter three useful spells. The first one allows you to convert a grams of sand into b grams of lead, the second one allows you to convert c grams of lead into d grams of gold and third one allows you to convert e grams of gold into f grams of sand. When Harry told his friends about these spells, Ron Weasley was amazed. After all, if they succeed in turning sand into lead, lead into gold, and then turning part of the gold into sand again and so on, then it will be possible to start with a small amount of sand and get huge amounts of gold! Even an infinite amount of gold! Hermione Granger, by contrast, was skeptical about that idea. She argues that according to the law of conservation of matter getting an infinite amount of matter, even using magic, is impossible. On the contrary, the amount of matter may even decrease during transformation, being converted to magical energy. Though Hermione's theory seems convincing, Ron won't believe her. As far as Ron is concerned, Hermione made up her law of conservation of matter to stop Harry and Ron wasting their time with this nonsense, and to make them go and do homework instead. That's why Ron has already collected a certain amount of sand for the experiments. A quarrel between the friends seems unavoidable...Help Harry to determine which one of his friends is right, and avoid the quarrel after all. To do this you have to figure out whether it is possible to get the amount of gold greater than any preassigned number from some finite amount of sand.
|
The first line contains 6 integers a, b, c, d, e, f (0 β€ a, b, c, d, e, f β€ 1000).
|
Print ""Ron"", if it is possible to get an infinitely large amount of gold having a certain finite amount of sand (and not having any gold and lead at all), i.e., Ron is right. Otherwise, print ""Hermione"".
|
Consider the first sample. Let's start with the 500 grams of sand. Apply the first spell 5 times and turn the sand into 1000 grams of lead. Then apply the second spell 4 times to get 600 grams of gold. Letβs take 400 grams from the resulting amount of gold turn them back into sand. We get 500 grams of sand and 200 grams of gold. If we apply the same operations to 500 grams of sand again, we can get extra 200 grams of gold every time. Thus, you can get 200, 400, 600 etc. grams of gold, i.e., starting with a finite amount of sand (500 grams), you can get the amount of gold which is greater than any preassigned number.In the forth sample it is impossible to get sand, or lead, or gold, applying the spells.In the fifth sample an infinitely large amount of gold can be obtained by using only the second spell, which allows you to receive 1 gram of gold out of nothing. Note that if such a second spell is available, then the first and the third one do not affect the answer at all.The seventh sample is more interesting. We can also start with a zero amount of sand there. With the aid of the third spell you can get sand out of nothing. We get 10000 grams of sand in this manner. Let's get 100 grams of lead using the first spell 100 times. Then make 1 gram of gold from them. We managed to receive 1 gram of gold, starting with a zero amount of sand! Clearly, in this manner you can get an infinitely large amount of gold.
|
Input: 100 200 250 150 200 250 | Output: Ron
|
Medium
| 2 | 1,658 | 82 | 207 | 0 |
1,366 |
A
|
1366A
|
A. Shovels and Swords
| 1,100 |
binary search; greedy; math
|
Polycarp plays a well-known computer game (we won't mention its name). In this game, he can craft tools of two types β shovels and swords. To craft a shovel, Polycarp spends two sticks and one diamond; to craft a sword, Polycarp spends two diamonds and one stick.Each tool can be sold for exactly one emerald. How many emeralds can Polycarp earn, if he has \(a\) sticks and \(b\) diamonds?
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains two integers \(a\) and \(b\) (\(0 \le a, b \le 10^9\)) β the number of sticks and the number of diamonds, respectively.
|
For each test case print one integer β the maximum number of emeralds Polycarp can earn.
|
In the first test case Polycarp can earn two emeralds as follows: craft one sword and one shovel.In the second test case Polycarp does not have any diamonds, so he cannot craft anything.
|
Input: 4 4 4 1000000000 0 7 15 8 7 | Output: 2 0 7 5
|
Easy
| 3 | 389 | 252 | 88 | 13 |
977 |
B
|
977B
|
B. Two-gram
| 900 |
implementation; strings
|
Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, ""AZ"", ""AA"", ""ZA"" β three distinct two-grams.You are given a string \(s\) consisting of \(n\) capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string \(s\) = ""BBAABBBA"" the answer is two-gram ""BB"", which contained in \(s\) three times. In other words, find any most frequent two-gram.Note that occurrences of the two-gram can overlap with each other.
|
The first line of the input contains integer number \(n\) (\(2 \le n \le 100\)) β the length of string \(s\). The second line of the input contains the string \(s\) consisting of \(n\) capital Latin letters.
|
Print the only line containing exactly two capital Latin letters β any two-gram contained in the given string \(s\) as a substring (i.e. two consecutive characters of the string) maximal number of times.
|
In the first example ""BA"" is also valid answer.In the second example the only two-gram ""ZZ"" can be printed because it contained in the string ""ZZZAA"" two times.
|
Input: 7ABACABA | Output: AB
|
Beginner
| 2 | 597 | 207 | 203 | 9 |
723 |
C
|
723C
|
C. Polycarp at the Radio
| 1,600 |
greedy
|
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others. We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.Find this maximum possible value of the minimum among the bj (1 β€ j β€ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
|
The first line of the input contains two integers n and m (1 β€ m β€ n β€ 2000).The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109), where ai is the performer of the i-th song.
|
In the first line print two integers: the maximum possible value of the minimum among the bj (1 β€ j β€ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.In the second line print the changed playlist.If there are multiple answers, print any of them.
|
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist. In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
|
Input: 4 21 2 3 2 | Output: 2 11 2 1 2
|
Medium
| 1 | 748 | 188 | 358 | 7 |
2,020 |
D
|
2020D
|
D. Connect the Dots
| 1,800 |
brute force; dp; dsu; graphs; math; trees
|
One fine evening, Alice sat down to play the classic game ""Connect the Dots"", but with a twist.To play the game, Alice draws a straight line and marks \(n\) points on it, indexed from \(1\) to \(n\). Initially, there are no arcs between the points, so they are all disjoint. After that, Alice performs \(m\) operations of the following type: She picks three integers \(a_i\), \(d_i\) (\(1 \le d_i \le 10\)), and \(k_i\). She selects points \(a_i, a_i+d_i, a_i+2d_i, a_i+3d_i, \ldots, a_i+k_i\cdot d_i\) and connects each pair of these points with arcs. After performing all \(m\) operations, she wants to know the number of connected components\(^\dagger\) these points form. Please help her find this number.\(^\dagger\) Two points are said to be in one connected component if there is a path between them via several (possibly zero) arcs and other points.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)).The \(i\)-th of the following \(m\) lines contains three integers \(a_i\), \(d_i\), and \(k_i\) (\(1 \le a_i \le a_i + k_i\cdot d_i \le n\), \(1 \le d_i \le 10\), \(0 \le k_i \le n\)).It is guaranteed that both the sum of \(n\) and the sum of \(m\) over all test cases do not exceed \(2 \cdot 10^5\).
|
For each test case, output the number of connected components.
|
In the first test case, there are \(n = 10\) points. The first operation joins the points \(1\), \(3\), \(5\), \(7\), and \(9\). The second operation joins the points \(2\), \(4\), \(6\), \(8\), and \(10\). There are thus two connected components: \(\{1, 3, 5, 7, 9\}\) and \(\{2, 4, 6, 8, 10\}\).In the second test case, there are \(n = 100\) points. The only operation joins the points \(19\), \(21\), \(23\), \(25\), and \(27\). Now all of them form a single connected component of size \(5\). The other \(95\) points form single-point connected components. Thus, the answer is \(1 + 95 = 96\).In the third test case, there are \(n = 100\) points. After the operations, all odd points from \(1\) to \(79\) will be in one connected component of size \(40\). The other \(60\) points form single-point connected components. Thus, the answer is \(1 + 60 = 61\).
|
Input: 310 21 2 42 2 4100 119 2 4100 31 2 57 2 617 2 31 | Output: 2 96 61
|
Medium
| 6 | 859 | 593 | 62 | 20 |
8 |
D
|
8D
|
D. Two Friends
| 2,600 |
binary search; geometry
|
Two neighbours, Alan and Bob, live in the city, where there are three buildings only: a cinema, a shop and the house, where they live. The rest is a big asphalt square. Once they went to the cinema, and the film impressed them so deeply, that when they left the cinema, they did not want to stop discussing it.Bob wants to get home, but Alan has to go to the shop first, and only then go home. So, they agreed to cover some distance together discussing the film (their common path might pass through the shop, or they might walk circles around the cinema together), and then to part each other's company and go each his own way. After they part, they will start thinking about their daily pursuits; and even if they meet again, they won't be able to go on with the discussion. Thus, Bob's path will be a continuous curve, having the cinema and the house as its ends. Alan's path β a continuous curve, going through the shop, and having the cinema and the house as its ends.The film ended late, that's why the whole distance covered by Alan should not differ from the shortest one by more than t1, and the distance covered by Bob should not differ from the shortest one by more than t2.Find the maximum distance that Alan and Bob will cover together, discussing the film.
|
The first line contains two integers: t1, t2 (0 β€ t1, t2 β€ 100). The second line contains the cinema's coordinates, the third one β the house's, and the last line β the shop's. All the coordinates are given in meters, are integer, and do not exceed 100 in absolute magnitude. No two given places are in the same building.
|
In the only line output one number β the maximum distance that Alan and Bob will cover together, discussing the film. Output the answer accurate to not less than 4 decimal places.
|
Input: 0 20 04 0-3 0 | Output: 1.0000000000
|
Expert
| 2 | 1,270 | 321 | 179 | 0 |
|
345 |
D
|
345D
|
D. Chain Letter
| 2,200 |
*special; dfs and similar; graphs
|
A chain letter is a kind of a message which urges the recipient to forward it to as many contacts as possible, usually with some kind of mystic explanation. Of course, this is only a superstition, and you don't believe in it, but all your friends do. You know that today there will be one of these letters going around, and you want to know how many times you'll receive it β of course, not that you'll be sending it yourself!You are given an array of strings f with n elements which describes the contacts between you and n - 1 of your friends: j-th character of i-th string (f[i][j]) is ""1"" if people i and j will send messages to each other, and ""0"" otherwise. Person 1 starts sending the letter to all his contacts; every person who receives the letter for the first time sends it to all his contacts. You are person n, and you don't forward the letter when you receive it. Calculate the number of copies of this letter you'll receive.
|
The first line of the input contains an integer n (2 β€ n β€ 50) β the number of people involved. Next n following lines contain elements of f, strings of length n. Each character in f is either ""0"" or ""1"". It's guaranteed that two following equations hold: f[i][j] = f[j][i], f[i][i] = 0, for all i, j (1 β€ i, j β€ n).
|
Output a single integer β the number of copies of the letter you will receive eventually.
|
In the first case, everybody sends letters to everyone, so you get copies from all three of your friends.In the second case, you don't know any of these people, so they don't bother you with their superstitious stuff.In the third case, two of your friends send you copies of the letter but the third friend doesn't know them so he is unaffected.
|
Input: 40111101111011110 | Output: 3
|
Hard
| 3 | 943 | 320 | 89 | 3 |
630 |
J
|
630J
|
J. Divisibility
| 1,100 |
math; number theory
|
IT City company developing computer games invented a new way to reward its employees. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is divisible by all numbers from 2 to 10 every developer of this game gets a small bonus.A game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that n people will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it.
|
The only line of the input contains one integer n (1 β€ n β€ 1018) β the prediction on the number of people who will buy the game.
|
Output one integer showing how many numbers from 1 to n are divisible by all numbers from 2 to 10.
|
Input: 3000 | Output: 1
|
Easy
| 2 | 653 | 128 | 98 | 6 |
|
1,901 |
B
|
1901B
|
B. Chip and Ribbon
| 1,100 |
greedy; math
|
There is a ribbon divided into \(n\) cells, numbered from \(1\) to \(n\) from left to right. Initially, an integer \(0\) is written in each cell.Monocarp plays a game with a chip. The game consists of several turns. During the first turn, Monocarp places the chip in the \(1\)-st cell of the ribbon. During each turn except for the first turn, Monocarp does exactly one of the two following actions: move the chip to the next cell (i. e. if the chip is in the cell \(i\), it is moved to the cell \(i+1\)). This action is impossible if the chip is in the last cell; choose any cell \(x\) and teleport the chip into that cell. It is possible to choose the cell where the chip is currently located. At the end of each turn, the integer written in the cell with the chip is increased by \(1\).Monocarp's goal is to make some turns so that the \(1\)-st cell contains the integer \(c_1\), the \(2\)-nd cell contains the integer \(c_2\), ..., the \(n\)-th cell contains the integer \(c_n\). He wants to teleport the chip as few times as possible.Help Monocarp calculate the minimum number of times he has to teleport the chip.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines: the first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)); the second line contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(0 \le c_i \le 10^9\); \(c_1 \ge 1\)). It can be shown that under these constraints, it is always possible to make a finite amount of turns so that the integers in the cells match the sequence \(c_1, c_2, \dots, c_n\).Additional constraint on the input: the sum of values of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print one integer β the minimum number of times Monocarp has to teleport the chip.
|
In the first test case of the example, Monocarp can perform the turns as follows: place the chip in the \(1\)-st cell; the numbers in the cells are \([1, 0, 0, 0]\); move the chip to the next (\(2\)-nd) cell; the numbers in the cells are \([1, 1, 0, 0]\); move the chip to the next (\(3\)-rd) cell; the numbers in the cells are \([1, 1, 1, 0]\); teleport the chip to the \(2\)-nd cell; the numbers in the cells are \([1, 2, 1, 0]\); move the chip to the next (\(3\)-rd) cell; the numbers in the cells are \([1, 2, 2, 0]\); move the chip to the next (\(4\)-th) cell; the numbers in the cells are \([1, 2, 2, 1]\).
|
Input: 441 2 2 151 0 1 0 155 4 3 2 1112 | Output: 1 2 4 11
|
Easy
| 2 | 1,119 | 605 | 102 | 19 |
1,571 |
A
|
1571A
|
A. Sequence of Comparisons
| 800 |
*special
|
Once upon a time, Petya had an array of integers \(a\) of length \(n\). But over time, the array itself was lost, and only \(n-1\) results of comparisons of neighboring array elements remained. In other words, for every \(i\) from \(1\) to \(n-1\), Petya knows exactly one of these three facts: \(a_i < a_{i+1}\); \(a_i = a_{i+1}\); \(a_i > a_{i+1}\). Petya wonders if it is possible to uniquely determine the result of comparing \(a_1\) and \(a_n\).You have to help Petya determine the result of comparing \(a_1\) and \(a_n\) or report that the result cannot be determined unambiguously.
|
The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.The only line of the test case contains the string \(s\) (\(1 \le |s| \le 100\)), where \(s_i\) is: <, if \(a_i < a_{i + 1}\); >, if \(a_i > a_{i + 1}\); =, if \(a_i = a_{i + 1}\).
|
For each test case, print a single string equal to: <, if \(a_1 < a_n\); >, if \(a_1 > a_n\); =, if \(a_1 = a_n\); ?, if it is impossible to uniquely determine the result of the comparison.
|
Consider the test cases of the example: in the first test case, it's easy to see that \(a_1 > a_4\) since \(a_1 > a_2 > a_3 > a_4\); in the second test case, both sequences \([1, 2, 0, 10, 10, 15]\) and \([10, 11, 1, 2, 2, 5]\) meet the constraints; in the first one, \(a_1 < a_6\), and in the second one, \(a_1 > a_6\), so it's impossible to compare \(a_1\) and \(a_6\); in the third test case, we already know that \(a_1 = a_2\); in the fourth test case, it's easy to see that \(a_3 = a_4 = a_5\), and \(a_1 < a_2 < a_3\), so \(a_1 < a_5\).
|
Input: 4 >>> <><=< = <<== | Output: > ? = <
|
Beginner
| 1 | 588 | 276 | 189 | 15 |
1,395 |
A
|
1395A
|
A. Boboniu Likes to Color Balls
| 1,000 |
brute force; math
|
Boboniu gives you \(r\) red balls, \(g\) green balls, \(b\) blue balls, \(w\) white balls. He allows you to do the following operation as many times as you want: Pick a red ball, a green ball, and a blue ball and then change their color to white. You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations.
|
The first line contains one integer \(T\) (\(1\le T\le 100\)) denoting the number of test cases.For each of the next \(T\) cases, the first line contains four integers \(r\), \(g\), \(b\) and \(w\) (\(0\le r,g,b,w\le 10^9\)).
|
For each test case, print ""Yes"" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print ""No"".
|
In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome.In the second test case, after doing one operation, changing \((8,1,9,3)\) to \((7,0,8,6)\), one of those possible palindromes may be ""rrrwwwbbbbrbbbbwwwrrr"".A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, ""rggbwbggr"", ""b"", ""gg"" are palindromes while ""rgbb"", ""gbbgr"" are not. Notice that an empty word, phrase, or sequence is palindrome.
|
Input: 4 0 1 1 1 8 1 9 3 0 0 0 0 1000000000 1000000000 1000000000 1000000000 | Output: No Yes Yes Yes
|
Beginner
| 2 | 386 | 225 | 186 | 13 |
1,458 |
E
|
1458E
|
E. Nim Shortcuts
| 3,100 |
data structures; games
|
After your debut mobile game ""Nim"" blew up, you decided to make a sequel called ""Nim 2"". This game will expand on the trusted Nim game formula, adding the much awaited second heap! In the game, there are two heaps, each containing a non-negative number of stones. Two players make moves in turn. On their turn, a player can take any positive number of stones from either one of the heaps. A player who is unable to move loses the game.To make the game easier to playtest, you've introduced developer shortcuts. There are \(n\) shortcut positions \((x_1, y_1), \ldots, (x_n, y_n)\). These change the game as follows: suppose that before a player's turn the first and second heap contain \(x\) and \(y\) stones respectively. If the pair \((x, y)\) is equal to one of the pairs \((x_i, y_i)\), then the player about to move loses instantly, otherwise they are able to make moves as normal. Note that in the above explanation the two heaps and all pairs are ordered, that is, \(x\) must refer to the size of the first heap, and \(y\) must refer to the size of the second heap.The game release was followed by too much celebration, and next thing you know is developer shortcuts made their way to the next official update of the game! Players now complain that the AI opponent has become unbeatable at certain stages of the game. You now have to write a program to figure out which of the given initial positions can be won by the starting player, assuming both players act optimally.
|
The first line contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^5\)) β the number of shortcut positions, and the number of initial positions that need to be evaluated.The following \(n\) lines describe shortcut positions. The \(i\)-th of these lines contains two integers \(x_i, y_i\) (\(0 \leq x_i, y_i \leq 10^9\)). It is guaranteed that all shortcut positions are distinct.The following \(m\) lines describe initial positions. The \(i\)-th of these lines contains two integers \(a_i, b_i\) (\(0 \leq a_i, b_i \leq 10^9\)) β the number of stones in the first and second heap respectively. It is guaranteed that all initial positions are distinct. However, initial positions are not necessarily distinct from shortcut positions.
|
For each initial position, on a separate line print ""WIN"" if the starting player is able to win from this position, and ""LOSE"" otherwise.
|
Input: 3 5 3 0 0 1 2 2 0 0 1 1 2 2 3 3 5 4 | Output: LOSE WIN LOSE WIN LOSE
|
Master
| 2 | 1,483 | 741 | 141 | 14 |
|
910 |
A
|
910A
|
A. The Way to Home
| 800 |
dfs and similar; dp; greedy; implementation
|
A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The frog can jump to the right at a distance not more than d. So, after she jumped from the point x she can reach the point x + a, where a is an integer from 1 to d.For each point from 1 to n is known if there is a lily flower in it. The frog can jump only in points with a lilies. Guaranteed that there are lilies in the points 1 and n.Determine the minimal number of jumps that the frog needs to reach home which is in the point n from the point 1. Consider that initially the frog is in the point 1. If the frog can not reach home, print -1.
|
The first line contains two integers n and d (2 β€ n β€ 100, 1 β€ d β€ n - 1) β the point, which the frog wants to reach, and the maximal length of the frog jump.The second line contains a string s of length n, consisting of zeros and ones. If a character of the string s equals to zero, then in the corresponding point there is no lily flower. In the other case, in the corresponding point there is a lily flower. Guaranteed that the first and the last characters of the string s equal to one.
|
If the frog can not reach the home, print -1.In the other case, print the minimal number of jumps that the frog needs to reach the home which is in the point n from the point 1.
|
In the first example the from can reach home in two jumps: the first jump from the point 1 to the point 4 (the length of the jump is three), and the second jump from the point 4 to the point 8 (the length of the jump is four).In the second example the frog can not reach home, because to make it she need to jump on a distance three, but the maximum length of her jump equals to two.
|
Input: 8 410010101 | Output: 2
|
Beginner
| 4 | 650 | 490 | 177 | 9 |
1,548 |
D1
|
1548D1
|
D1. Gregor and the Odd Cows (Easy)
| 2,300 |
bitmasks; geometry; math; number theory
|
This is the easy version of the problem. The only difference from the hard version is that in this version all coordinates are even.There are \(n\) fence-posts at distinct coordinates on a plane. It is guaranteed that no three fence posts lie on the same line.There are an infinite number of cows on the plane, one at every point with integer coordinates.Gregor is a member of the Illuminati, and wants to build a triangular fence, connecting \(3\) distinct existing fence posts. A cow strictly inside the fence is said to be enclosed. If there are an odd number of enclosed cows and the area of the fence is an integer, the fence is said to be interesting.Find the number of interesting fences.
|
The first line contains the integer \(n\) (\(3 \le n \le 6000\)), the number of fence posts which Gregor can choose to form the vertices of a fence.Each of the next \(n\) line contains two integers \(x\) and \(y\) (\(0 \le x,y \le 10^7\), \(x\) and \(y\) are even), where \((x,y)\) is the coordinate of a fence post. All fence posts lie at distinct coordinates. No three fence posts are on the same line.
|
Print a single integer, the number of interesting fences. Two fences are considered different if they are constructed with a different set of three fence posts.
|
In the first example, there is only \(1\) fence. That fence is interesting since its area is \(4\) and there is \(1\) enclosed cow, marked in red. In the second example, there are \(3\) interesting fences. \((0,0)\) β \((30,14)\) β \((2,10)\) \((2,16)\) β \((30,14)\) β \((2,10)\) \((30,14)\) β \((4,6)\) β \((2,10)\)
|
Input: 3 0 0 2 0 0 4 | Output: 1
|
Expert
| 4 | 695 | 404 | 160 | 15 |
1,534 |
F1
|
1534F1
|
F1. Falling Sand (Easy Version)
| 2,500 |
dfs and similar; graphs; greedy
|
This is the easy version of the problem. The difference between the versions is the constraints on \(a_i\). You can make hacks only if all versions of the problem are solved.Little Dormi has recently received a puzzle from his friend and needs your help to solve it. The puzzle consists of an upright board with \(n\) rows and \(m\) columns of cells, some empty and some filled with blocks of sand, and \(m\) non-negative integers \(a_1,a_2,\ldots,a_m\) (\(0 \leq a_i \leq n\)). In this version of the problem, \(a_i\) will be equal to the number of blocks of sand in column \(i\).When a cell filled with a block of sand is disturbed, the block of sand will fall from its cell to the sand counter at the bottom of the column (each column has a sand counter). While a block of sand is falling, other blocks of sand that are adjacent at any point to the falling block of sand will also be disturbed and start to fall. Specifically, a block of sand disturbed at a cell \((i,j)\) will pass through all cells below and including the cell \((i,j)\) within the column, disturbing all adjacent cells along the way. Here, the cells adjacent to a cell \((i,j)\) are defined as \((i-1,j)\), \((i,j-1)\), \((i+1,j)\), and \((i,j+1)\) (if they are within the grid). Note that the newly falling blocks can disturb other blocks.In one operation you are able to disturb any piece of sand. The puzzle is solved when there are at least \(a_i\) blocks of sand counted in the \(i\)-th sand counter for each column from \(1\) to \(m\).You are now tasked with finding the minimum amount of operations in order to solve the puzzle. Note that Little Dormi will never give you a puzzle that is impossible to solve.
|
The first line consists of two space-separated positive integers \(n\) and \(m\) (\(1 \leq n \cdot m \leq 400\,000\)).Each of the next \(n\) lines contains \(m\) characters, describing each row of the board. If a character on a line is '.', the corresponding cell is empty. If it is '#', the cell contains a block of sand.The final line contains \(m\) non-negative integers \(a_1,a_2,\ldots,a_m\) (\(0 \leq a_i \leq n\)) β the minimum amount of blocks of sand that needs to fall below the board in each column. In this version of the problem, \(a_i\) will be equal to the number of blocks of sand in column \(i\).
|
Print one non-negative integer, the minimum amount of operations needed to solve the puzzle.
|
For example \(1\), by disturbing both blocks of sand on the first row from the top at the first and sixth columns from the left, and the block of sand on the second row from the top and the fourth column from the left, it is possible to have all the required amounts of sand fall in each column. It can be proved that this is not possible with fewer than \(3\) operations, and as such the answer is \(3\). Here is the puzzle from the first example. For example \(2\), by disturbing the cell on the top row and rightmost column, one can cause all of the blocks of sand in the board to fall into the counters at the bottom. Thus, the answer is \(1\). Here is the puzzle from the second example.
|
Input: 5 7 #....#. .#.#... #....#. #....## #.#.... 4 1 1 1 0 3 1 | Output: 3
|
Expert
| 3 | 1,689 | 613 | 92 | 15 |
128 |
B
|
128B
|
B. String
| 2,100 |
brute force; constructive algorithms; hashing; implementation; string suffix structures; strings
|
One day in the IT lesson Anna and Maria learned about the lexicographic order.String x is lexicographically less than string y, if either x is a prefix of y (and x β y), or there exists such i (1 β€ i β€ min(|x|, |y|)), that xi < yi, and for any j (1 β€ j < i) xj = yj. Here |a| denotes the length of the string a. The lexicographic comparison of strings is implemented by operator < in modern programming languagesββ.The teacher gave Anna and Maria homework. She gave them a string of length n. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string ""aab"": ""a"", ""a"", ""aa"", ""ab"", ""aab"", ""b""). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the k-th string from the list. Help Anna and Maria do the homework.
|
The first line contains a non-empty string that only consists of small Latin letters (""a""-""z""), whose length does not exceed 105. The second line contains the only integer k (1 β€ k β€ 105).
|
Print the string Anna and Maria need β the k-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than k, print a string saying ""No such line."" (without the quotes).
|
In the second sample before string ""bc"" follow strings ""a"", ""ab"", ""abc"", ""b"".
|
Input: aa2 | Output: a
|
Hard
| 6 | 974 | 192 | 221 | 1 |
2,056 |
F1
|
2056F1
|
F1. Xor of Median (Easy Version)
| 2,700 |
bitmasks; brute force; combinatorics; dp; math
|
This is the easy version of the problem. The difference between the versions is that in this version, the constraints on \(t\), \(k\), and \(m\) are lower. You can hack only if you solved all versions of this problem. A sequence \(a\) of \(n\) integers is called good if the following condition holds: Let \(\text{cnt}_x\) be the number of occurrences of \(x\) in sequence \(a\). For all pairs \(0 \le i < j < m\), at least one of the following has to be true: \(\text{cnt}_i = 0\), \(\text{cnt}_j = 0\), or \(\text{cnt}_i \le \text{cnt}_j\). In other words, if both \(i\) and \(j\) are present in sequence \(a\), then the number of occurrences of \(i\) in \(a\) is less than or equal to the number of occurrences of \(j\) in \(a\). You are given integers \(n\) and \(m\). Calculate the value of the bitwise XOR of the median\(^{\text{β}}\) of all good sequences \(a\) of length \(n\) with \(0\le a_i < m\). Note that the value of \(n\) can be very large, so you are given its binary representation instead.\(^{\text{β}}\)The median of a sequence \(a\) of length \(n\) is defined as the \(\left\lfloor\frac{n + 1}{2}\right\rfloor\)-th smallest value in the sequence.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 50\)). The description of the test cases follows. The first line of each test case contains two integers \(k\) and \(m\) (\(1 \le k \le 200\), \(1 \le m \le 500\)) β the number of bits in \(n\) and the upper bound on the elements in sequence \(a\).The second line of each test case contains a binary string of length \(k\) β the binary representation of \(n\) with no leading zeros.It is guaranteed that the sum of \(k\) over all test cases does not exceed \(200\).
|
For each test case, output a single integer representing the bitwise XOR of the median of all good sequences \(a\) of length \(n\) where \(0\le a_i < m\).
|
In the first example, \(n = 10_2 = 2\) and \(m = 3\). All possible sequences with elements less than \(m\) are: \([0, 0]\), \([0, 1]\), \([0, 2]\), \([1, 0]\), \([1, 1]\), \([1, 2]\), \([2, 0]\), \([2, 1]\), \([2, 2]\). All of them are good, so the answer is: \(0 \oplus 0 \oplus 0 \oplus 0 \oplus 1 \oplus 1 \oplus 0 \oplus 1 \oplus 2 = 3\).In the second example, \(n = 11_2 = 3\) and \(m = 3\). Some good sequences are \([2, 2, 2]\), \([1, 0, 1]\), and \([2, 0, 1]\). However, a sequence \([2, 0, 0]\) is not good, because \(\text{cnt}_0 = 2\), \(\text{cnt}_2 = 1\). Therefore, if we set \(i = 0\) and \(j = 2\), \(i < j\) holds, but \(\text{cnt}_i \le \text{cnt}_j\) does not.
|
Input: 62 3102 3115 1111017 9110101117 34110010100010100101 5001 | Output: 3 2 0 8 32 0
|
Master
| 5 | 1,166 | 575 | 154 | 20 |
1,657 |
D
|
1657D
|
D. For Gamers. By Gamers.
| 2,000 |
binary search; brute force; greedy; math; sortings
|
Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has \(C\) coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with \(C\) coins.There are \(n\) types of units. Every unit type has three parameters: \(c_i\) β the cost of recruiting one unit of the \(i\)-th type; \(d_i\) β the damage that one unit of the \(i\)-th type deals in a second; \(h_i\) β the amount of health of one unit of the \(i\)-th type. Monocarp has to face \(m\) monsters. Every monster has two parameters: \(D_j\) β the damage that the \(j\)-th monster deals in a second; \(H_j\) β the amount of health the \(j\)-th monster has. Monocarp has to fight only the \(j\)-th monster during the \(j\)-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than \(C\), then report that it's impossible to kill that monster.
|
The first line contains two integers \(n\) and \(C\) (\(1 \le n \le 3 \cdot 10^5\); \(1 \le C \le 10^6\)) β the number of types of units and the amount of coins Monocarp has before each battle.The \(i\)-th of the next \(n\) lines contains three integers \(c_i, d_i\) and \(h_i\) (\(1 \le c_i \le C\); \(1 \le d_i, h_i \le 10^6\)).The next line contains a single integer \(m\) (\(1 \le m \le 3 \cdot 10^5\)) β the number of monsters that Monocarp has to face.The \(j\)-th of the next \(m\) lines contains two integers \(D_j\) and \(H_j\) (\(1 \le D_j \le 10^6\); \(1 \le H_j \le 10^{12}\)).
|
Print \(m\) integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than \(C\), then print \(-1\).
|
Consider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster \(0.75\) seconds to kill each other. He can recruit two units for the cost of \(6\) coins and kill the monster in \(0.375\) second.Monocarp can recruit one unit of the second type, because he kills the monster in \(0.6\) seconds, and the monster kills him in \(0.625\) seconds. The unit is faster. Thus, \(5\) coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost \(30\) coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type.
|
Input: 3 10 3 4 6 5 5 5 10 3 4 3 8 3 5 4 10 15 | Output: 5 3 -1
|
Hard
| 5 | 1,377 | 589 | 174 | 16 |
1,153 |
A
|
1153A
|
A. Serval and Bus
| 1,000 |
brute force; math
|
It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly.Serval will go to the bus station at time \(t\), and there are \(n\) bus routes which stop at this station. For the \(i\)-th bus route, the first bus arrives at time \(s_i\) minutes, and each bus of this route comes \(d_i\) minutes later than the previous one.As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them.
|
The first line contains two space-separated integers \(n\) and \(t\) (\(1\leq n\leq 100\), \(1\leq t\leq 10^5\)) β the number of bus routes and the time Serval goes to the station. Each of the next \(n\) lines contains two space-separated integers \(s_i\) and \(d_i\) (\(1\leq s_i,d_i\leq 10^5\)) β the time when the first bus of this route arrives and the interval between two buses of this route.
|
Print one number β what bus route Serval will use. If there are several possible answers, you can print any of them.
|
In the first example, the first bus of the first route arrives at time \(6\), and the first bus of the second route arrives at time \(9\), so the first route is the answer.In the second example, a bus of the third route arrives at time \(5\), so it is the answer.In the third example, buses of the first route come at times \(2\), \(4\), \(6\), \(8\), and so fourth, buses of the second route come at times \(2\), \(5\), \(8\), and so fourth and buses of the third route come at times \(2\), \(6\), \(10\), and so on, so \(1\) and \(2\) are both acceptable answers while \(3\) is not.
|
Input: 2 2 6 4 9 5 | Output: 1
|
Beginner
| 2 | 851 | 398 | 116 | 11 |
1,452 |
B
|
1452B
|
B. Toy Blocks
| 1,400 |
binary search; greedy; math; sortings
|
You are asked to watch your nephew who likes to play with toy blocks in a strange way.He has \(n\) boxes and the \(i\)-th box has \(a_i\) blocks. His game consists of two steps: he chooses an arbitrary box \(i\); he tries to move all blocks from the \(i\)-th box to other boxes. If he can make the same number of blocks in each of \(n - 1\) other boxes then he will be happy, otherwise, will be sad. Note that your nephew can only move the blocks from the chosen box to the other boxes; he cannot move blocks from the other boxes.You don't want to make your nephew sad, so you decided to put several extra blocks into some boxes in such a way that no matter which box \(i\) he chooses he won't be sad. What is the minimum number of extra blocks you need to put?
|
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 the integer \(n\) (\(2 \le n \le 10^5\)) β the number of boxes.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^9\)) β the number of blocks in each box.It's guaranteed that the sum of \(n\) over test cases doesn't exceed \(10^5\).
|
For each test case, print a single integer β the minimum number of blocks you need to put. It can be proved that the answer always exists, i. e. the number of blocks is finite.
|
In the first test case, you can, for example, put one extra block into the first box and make \(a = [4, 2, 2]\). If your nephew chooses the box with \(4\) blocks, then we will move two blocks to the second box and two blocks to the third box. If he chooses the box with \(2\) blocks then he will move these two blocks to the other box with \(2\) blocks.In the second test case, you don't need to put any extra blocks, since no matter which box your nephew chooses, he can always make other boxes equal.In the third test case, you should put \(3\) extra blocks. For example, you can put \(2\) blocks in the first box and \(1\) block in the third box. You'll get array \(a = [2, 3, 1]\).
|
Input: 3 3 3 2 2 4 2 2 3 2 3 0 3 0 | Output: 1 0 3
|
Easy
| 4 | 761 | 423 | 176 | 14 |
1,402 |
A
|
1402A
|
A. Fancy Fence
| 1,800 |
*special; data structures; dsu; implementation; math; sortings
|
Everybody knows that BalΓ‘zs has the fanciest fence in the whole town. It's built up from \(N\) fancy sections. The sections are rectangles standing closely next to each other on the ground. The \(i\)th section has integer height \(h_i\) and integer width \(w_i\). We are looking for fancy rectangles on this fancy fence. A rectangle is fancy if: its sides are either horizontal or vertical and have integer lengths the distance between the rectangle and the ground is integer the distance between the rectangle and the left side of the first section is integer it's lying completely on sections What is the number of fancy rectangles? This number can be very big, so we are interested in it modulo \(10^9+7\).
|
The first line contains \(N\) (\(1\leq N \leq 10^{5}\)) β the number of sections. The second line contains \(N\) space-separated integers, the \(i\)th number is \(h_i\) (\(1 \leq h_i \leq 10^{9}\)). The third line contains \(N\) space-separated integers, the \(i\)th number is \(w_i\) (\(1 \leq w_i \leq 10^{9}\)).
|
You should print a single integer, the number of fancy rectangles modulo \(10^9+7\). So the output range is \(0,1,2,\ldots, 10^9+6\).
|
The fence looks like this: There are 5 fancy rectangles of shape: There are 3 fancy rectangles of shape: There is 1 fancy rectangle of shape: There are 2 fancy rectangles of shape: There is 1 fancy rectangle of shape:
|
Input: 2 1 2 1 2 | Output: 12
|
Medium
| 6 | 709 | 314 | 133 | 14 |
33 |
C
|
33C
|
C. Wonderful Randomized Sum
| 1,800 |
greedy
|
Learn, learn and learn again β Valera has to do this every day. He is studying at mathematical school, where math is the main discipline. The mathematics teacher loves her discipline very much and tries to cultivate this love in children. That's why she always gives her students large and difficult homework. Despite that Valera is one of the best students, he failed to manage with the new homework. That's why he asks for your help. He has the following task. A sequence of n numbers is given. A prefix of a sequence is the part of the sequence (possibly empty), taken from the start of the sequence. A suffix of a sequence is the part of the sequence (possibly empty), taken from the end of the sequence. It is allowed to sequentially make two operations with the sequence. The first operation is to take some prefix of the sequence and multiply all numbers in this prefix by - 1. The second operation is to take some suffix and multiply all numbers in it by - 1. The chosen prefix and suffix may intersect. What is the maximum total sum of the sequence that can be obtained by applying the described operations?
|
The first line contains integer n (1 β€ n β€ 105) β amount of elements in the sequence. The second line contains n integers ai ( - 104 β€ ai β€ 104) β the sequence itself.
|
The first and the only line of the output should contain the answer to the problem.
|
Input: 3-1 -2 -3 | Output: 6
|
Medium
| 1 | 1,116 | 167 | 83 | 0 |
|
1,324 |
C
|
1324C
|
C. Frog Jumps
| 1,100 |
binary search; data structures; dfs and similar; greedy; implementation
|
There is a frog staying to the left of the string \(s = s_1 s_2 \ldots s_n\) consisting of \(n\) characters (to be more precise, the frog initially stays at the cell \(0\)). Each character of \(s\) is either 'L' or 'R'. It means that if the frog is staying at the \(i\)-th cell and the \(i\)-th character is 'L', the frog can jump only to the left. If the frog is staying at the \(i\)-th cell and the \(i\)-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell \(0\).Note that the frog can jump into the same cell twice and can perform as many jumps as it needs.The frog wants to reach the \(n+1\)-th cell. The frog chooses some positive integer value \(d\) before the first jump (and cannot change it later) and jumps by no more than \(d\) cells at once. I.e. if the \(i\)-th character is 'L' then the frog can jump to any cell in a range \([max(0, i - d); i - 1]\), and if the \(i\)-th character is 'R' then the frog can jump to any cell in a range \([i + 1; min(n + 1; i + d)]\).The frog doesn't want to jump far, so your task is to find the minimum possible value of \(d\) such that the frog can reach the cell \(n+1\) from the cell \(0\) if it can jump by no more than \(d\) cells at once. It is guaranteed that it is always possible to reach \(n+1\) from \(0\).You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The next \(t\) lines describe test cases. The \(i\)-th test case is described as a string \(s\) consisting of at least \(1\) and at most \(2 \cdot 10^5\) characters 'L' and 'R'.It is guaranteed that the sum of lengths of strings over all test cases does not exceed \(2 \cdot 10^5\) (\(\sum |s| \le 2 \cdot 10^5\)).
|
For each test case, print the answer β the minimum possible value of \(d\) such that the frog can reach the cell \(n+1\) from the cell \(0\) if it jumps by no more than \(d\) at once.
|
The picture describing the first test case of the example and one of the possible answers:In the second test case of the example, the frog can only jump directly from \(0\) to \(n+1\).In the third test case of the example, the frog can choose \(d=3\), jump to the cell \(3\) from the cell \(0\) and then to the cell \(4\) from the cell \(3\).In the fourth test case of the example, the frog can choose \(d=1\) and jump \(5\) times to the right.In the fifth test case of the example, the frog can only jump directly from \(0\) to \(n+1\).In the sixth test case of the example, the frog can choose \(d=1\) and jump \(2\) times to the right.
|
Input: 6 LRLRRLL L LLR RRRR LLLLLL R | Output: 3 2 3 1 7 1
|
Easy
| 5 | 1,369 | 419 | 183 | 13 |
1,659 |
A
|
1659A
|
A. Red Versus Blue
| 1,000 |
constructive algorithms; greedy; implementation; math
|
Team Red and Team Blue competed in a competitive FPS. Their match was streamed around the world. They played a series of \(n\) matches.In the end, it turned out Team Red won \(r\) times and Team Blue won \(b\) times. Team Blue was less skilled than Team Red, so \(b\) was strictly less than \(r\).You missed the stream since you overslept, but you think that the match must have been neck and neck since so many people watched it. So you imagine a string of length \(n\) where the \(i\)-th character denotes who won the \(i\)-th match β it is R if Team Red won or B if Team Blue won. You imagine the string was such that the maximum number of times a team won in a row was as small as possible. For example, in the series of matches RBBRRRB, Team Red won \(3\) times in a row, which is the maximum.You must find a string satisfying the above conditions. If there are multiple answers, print any.
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case has a single line containing three integers \(n\), \(r\), and \(b\) (\(3 \leq n \leq 100\); \(1 \leq b < r \leq n\), \(r+b=n\)).
|
For each test case, output a single line containing a string satisfying the given conditions. If there are multiple answers, print any.
|
The first test case of the first example gives the optimal answer for the example in the statement. The maximum number of times a team wins in a row in RBRBRBR is \(1\). We cannot minimize it any further.The answer for the second test case of the second example is RRBRBRBRBR. The maximum number of times a team wins in a row is \(2\), given by RR at the beginning. We cannot minimize the answer any further.
|
Input: 37 4 36 5 119 13 6 | Output: RBRBRBR RRRBRR RRBRRBRRBRRBRRBRRBR
|
Beginner
| 4 | 895 | 240 | 135 | 16 |
2,124 |
D
|
2124D
|
D. Make a Palindrome
| 1,700 |
greedy; sortings; two pointers
|
You are given an array \(a\) of size \(n\), and an integer \(k\). You may perform the following operation any number of times: Select two integers \(l\) and \(r\) \((1 \le l \le r \le |a|)\) such that \(r-l+1 \geq k\). Then, select an index \(i\) such that \(l\leq i \leq r\) where \(a_i\) is the \(k\)-th smallest number out of the subarray \([a_l,a_{l+1},\ldots,a_r]\). If there are multiple possible \(i\), you may select any. For example, consider \(a = [1, 2, 2, 1, 3], l = 1, r = 5\) and \(k = 3\), the possible candidates of \(i\) are indices \(2\) and \(3\). Then, delete \(a_i\) from \(a\), concatenating the remaining parts of the array. Determine if it is possible to get an array that is a palindrome\(^{\text{β}}\) after any number of operations. Note that an empty array is considered a palindrome. \(^{\text{β}}\)An array \(b=[b_1,b_2,\ldots,b_m]\) is a palindrome if for each \(1 \leq i \leq m\), \(b_i=b_{m+1-i}\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 2\cdot 10^5\)).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \leq a_i \leq n\)) β denoting the array \(a\).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 create a palindrome, and NO otherwise. You can output 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, \(a\) is already a palindrome.In the second test case, we can perform two operations as follows: \([\mathbf{1,1},2,1]\rightarrow [1,\mathbf{2},1]\rightarrow[1,1]\)In the third test case, we can perform one operation as follows: \([\mathbf{2,3,4,5,3,2}]\rightarrow[2,3,4,3,2]\).In the fourth and fifth test cases, it can be shown it is impossible to create a palindrome no matter what operations are used.
|
Input: 85 35 4 3 4 54 11 1 2 16 62 3 4 5 3 25 45 2 4 3 18 54 7 1 2 3 1 3 45 41 2 1 2 23 31 2 24 42 1 2 2 | Output: YES YES YES NO NO YES NO YES
|
Medium
| 3 | 931 | 459 | 241 | 21 |
207 |
D4
|
207D4
|
D4. The Beaver's Problem - 3
| 2,100 |
The Smart Beaver from ABBYY came up with another splendid problem for the ABBYY Cup participants! This time the Beaver invites the contest participants to check out a problem on sorting documents by their subjects. Let's describe the problem:You've got some training set of documents. For each document you know its subject. The subject in this problem is an integer from 1 to 3. Each of these numbers has a physical meaning. For instance, all documents with subject 3 are about trade.You can download the training set of documents at the following link: http://download4.abbyy.com/a2/X2RZ2ZWXBG5VYWAL61H76ZQM/train.zip. The archive contains three directories with names ""1"", ""2"", ""3"". Directory named ""1"" contains documents on the 1-st subject, directory ""2"" contains documents on the 2-nd subject, and directory ""3"" contains documents on the 3-rd subject. Each document corresponds to exactly one file from some directory.All documents have the following format: the first line contains the document identifier, the second line contains the name of the document, all subsequent lines contain the text of the document. The document identifier is used to make installing the problem more convenient and has no useful information for the participants.You need to write a program that should indicate the subject for a given document. It is guaranteed that all documents given as input to your program correspond to one of the three subjects of the training set.
|
The first line contains integer id (0 β€ id β€ 106) β the document identifier. The second line contains the name of the document. The third and the subsequent lines contain the text of the document. It is guaranteed that the size of any given document will not exceed 10 kilobytes.The tests for this problem are divided into 10 groups. Documents of groups 1 and 2 are taken from the training set, but their identifiers will not match the identifiers specified in the training set. Groups from the 3-rd to the 10-th are roughly sorted by the author in ascending order of difficulty (these groups contain documents which aren't present in the training set).
|
Print an integer from 1 to 3, inclusive β the number of the subject the given document corresponds to.
|
Hard
| 0 | 1,472 | 653 | 102 | 2 |
|||
285 |
D
|
285D
|
D. Permutation Sum
| 1,900 |
bitmasks; combinatorics; dp; implementation; meet-in-the-middle
|
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.Petya decided to introduce the sum operation on the set of permutations of length n. Let's assume that we are given two permutations of length n: a1, a2, ..., an and b1, b2, ..., bn. Petya calls the sum of permutations a and b such permutation c of length n, where ci = ((ai - 1 + bi - 1) mod n) + 1 (1 β€ i β€ n).Operation means taking the remainder after dividing number x by number y.Obviously, not for all permutations a and b exists permutation c that is sum of a and b. That's why Petya got sad and asked you to do the following: given n, count the number of such pairs of permutations a and b of length n, that exists permutation c that is sum of a and b. The pair of permutations x, y (x β y) and the pair of permutations y, x are considered distinct pairs.As the answer can be rather large, print the remainder after dividing it by 1000000007 (109 + 7).
|
The single line contains integer n (1 β€ n β€ 16).
|
In the single line print a single non-negative integer β the number of such pairs of permutations a and b, that exists permutation c that is sum of a and b, modulo 1000000007 (109 + 7).
|
Input: 3 | Output: 18
|
Hard
| 5 | 1,124 | 48 | 185 | 2 |
|
2,049 |
F
|
2049F
|
F. MEX OR Mania
| 2,700 |
bitmasks; brute force; data structures; dsu; implementation
|
An integer sequence \(b_1, b_2, \ldots, b_n\) is good if \(\operatorname{mex}(b_1, b_2, \ldots, b_n) - (b_1 | b_2 | \ldots | b_n) = 1\). Here, \(\operatorname{mex(c)}\) denotes the MEX\(^{\text{β}}\) of the collection \(c\), and \(|\) is the bitwise OR operator.Shohag has an integer sequence \(a_1, a_2, \ldots, a_n\). He will perform the following \(q\) updates on \(a\): \(i\) \(x\) β increase \(a_i\) by \(x\). After each update, help him find the length of the longest good subarray\(^{\text{β }}\) of \(a\).\(^{\text{β}}\)The minimum excluded (MEX) of a collection of integers \(c_1, c_2, \ldots, c_k\) is defined as the smallest non-negative integer \(y\) which does not occur in the collection \(c\). \(^{\text{β }}\)An array \(d\) is a subarray of an array \(f\) if \(d\) can be obtained from \(f\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
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 space-separated integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le n\)).The next \(q\) lines of each test case are of the following form: \(i\) \(x\) (\(1 \le i, x \le n\)) β it means you should increase \(a_i\) by \(x\). It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\) and the sum of \(q\) doesn't exceed \(10^5\).
|
For each test case, output \(q\) lines β on the \(i\)-th line output the length of the longest good subarray of \(a\) after the \(i\)-th update.
|
In the first test case, after the first update, the array becomes \([0, 0, 1, 0, 1, 1]\), and here the whole array is good because \(\operatorname{mex}([0, 0, 1, 0, 1, 1]) - (0 | 0 | 1 | 0 | 1 | 1) = 2 - 1 = 1\).After the second update, the array becomes \([0, 0, 3, 0, 1, 1]\), and here the subarray \([0, 1, 1]\) has the maximum length among all the good subarrays.Finally, after the third update, the array becomes \([0, 0, 3, 0, 1, 4]\), and here the subarrays \([0, 0]\) and \([0, 1]\) both have the maximum length among all the good subarrays.
|
Input: 26 30 0 1 0 1 06 13 26 33 11 3 11 1 | Output: 6 3 2 0
|
Master
| 5 | 943 | 659 | 144 | 20 |
46 |
F
|
46F
|
F. Hercule Poirot Problem
| 2,300 |
dsu; graphs
|
Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books. You are not informed on what crime was committed, when and where the corpse was found and other details. We only know that the crime was committed in a house that has n rooms and m doors between the pairs of rooms. The house residents are very suspicious, that's why all the doors can be locked with keys and all the keys are different. According to the provided evidence on Thursday night all the doors in the house were locked, and it is known in what rooms were the residents, and what kind of keys had any one of them. The same is known for the Friday night, when all the doors were also locked. On Friday it was raining heavily, that's why nobody left the house and nobody entered it. During the day the house residents could open and close doors to the neighboring rooms using the keys at their disposal (every door can be opened and closed from each side); move freely from a room to a room if a corresponding door is open; give keys to one another, being in one room. ""Little grey matter"" of Hercule Poirot are not capable of coping with such amount of information. Find out if the positions of people and keys on the Thursday night could result in the positions on Friday night, otherwise somebody among the witnesses is surely lying.
|
The first line contains three preset integers n, m ΠΈ k (1 β€ n, m, k β€ 1000) β the number of rooms, the number of doors and the number of house residents respectively. The next m lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to n. There cannot be more that one door between the pair of rooms. No door connects a room with itself. The next k lines describe the residents' position on the first night. Every line contains a resident's name (a non-empty line consisting of no more than 10 Latin letters), then after a space follows the room number, then, after a space β the number of keys the resident has. Then follow written space-separated numbers of the doors that can be unlocked by these keys. The doors are numbered with integers from 1 to m in the order in which they are described in the input data. All the residents have different names, uppercase and lowercase letters considered to be different. Every m keys occurs exactly once in the description. Multiple people may be present in one room, some rooms may be empty. The next k lines describe the position of the residents on the second night in the very same format. It is guaranteed that in the second night's description the residents' names remain the same and every m keys occurs exactly once.
|
Print ""YES"" (without quotes) if the second arrangement can result from the first one, otherwise, print ""NO"".
|
Input: 2 1 21 2Dmitry 1 1 1Natalia 2 0Natalia 1 1 1Dmitry 2 0 | Output: YES
|
Expert
| 2 | 1,452 | 1,315 | 112 | 0 |
|
739 |
C
|
739C
|
C. Alyona and towers
| 2,500 |
data structures
|
Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 Γ 1 Γ 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them.Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l β€ k β€ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.
|
The first line contain single integer n (1 β€ n β€ 3Β·105) β the number of towers.The second line contain n integers a1, a2, ..., an (1 β€ ai β€ 109) β the number of cubes in each tower. The third line contain single integer m (1 β€ m β€ 3Β·105) β the number of additions.The next m lines contain 3 integers each. The i-th of these lines contains integers li, ri and di (1 β€ l β€ r β€ n, 1 β€ di β€ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.
|
Print m lines. In i-th line print the maximum width of the hills after the i-th addition.
|
The first sample is as follows:After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.
|
Input: 55 5 5 5 531 3 22 2 14 4 1 | Output: 245
|
Expert
| 1 | 879 | 476 | 89 | 7 |
1,030 |
D
|
1030D
|
D. Vasya and Triangle
| 1,800 |
geometry; number theory
|
Vasya has got three integers \(n\), \(m\) and \(k\). He'd like to find three integer points \((x_1, y_1)\), \((x_2, y_2)\), \((x_3, y_3)\), such that \(0 \le x_1, x_2, x_3 \le n\), \(0 \le y_1, y_2, y_3 \le m\) and the area of the triangle formed by these points is equal to \(\frac{nm}{k}\).Help Vasya! Find such points (if it's possible). If there are multiple solutions, print any of them.
|
The single line contains three integers \(n\), \(m\), \(k\) (\(1\le n, m \le 10^9\), \(2 \le k \le 10^9\)).
|
If there are no such points, print ""NO"".Otherwise print ""YES"" in the first line. The next three lines should contain integers \(x_i, y_i\) β coordinates of the points, one point per line. If there are multiple solutions, print any of them.You can print each letter in any case (upper or lower).
|
In the first example area of the triangle should be equal to \(\frac{nm}{k} = 4\). The triangle mentioned in the output is pictured below: In the second example there is no triangle with area \(\frac{nm}{k} = \frac{16}{7}\).
|
Input: 4 3 3 | Output: YES1 02 34 1
|
Medium
| 2 | 392 | 107 | 298 | 10 |
1,933 |
A
|
1933A
|
A. Turtle Puzzle: Rearrange and Negate
| 800 |
greedy; math; sortings
|
You are given an array \(a\) of \(n\) integers. You must perform the following two operations on the array (the first, then the second): Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged. Choose at most one contiguous segment of elements and replace the signs of all elements in this segment with their opposites. Formally, you can choose a pair of indices \(l, r\) such that \(1 \le l \le r \le n\) and assign \(a_i = -a_i\) for all \(l \le i \le r\) (negate elements). Note that you may choose not to select a pair of indices and leave all the signs of the elements unchanged. What is the maximum sum of the array elements after performing these two operations (the first, then the second)?
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)) β the number of elements in array \(a\). The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-100 \le a_i \le 100\)) β elements of the array.
|
For each test case, output the maximum sum of the array elements after sequentially performing the two given operations.
|
In the first test case, you can first rearrange the array to get \([3,-2,-3]\) (operation 1), then choose \(l = 2, r = 3\) and get the sum \(3 + -((-2) + (-3)) = 8\) (operation 2).In the second test case, you can do nothing in both operations and get the sum \(0\).In the third test case, you can do nothing in both operations and get the sum \(0 + 1 = 1\).In the fourth test case, you can first leave the order unchanged (operation 1), then choose \(l = 1, r = 1\) and get the sum \(-(-99) = 99\) (operation 2).In the fifth test case, you can first leave the order unchanged (operation 1), then choose \(l = 2, r = 3\) and get the sum \(10 + -((-2) + (-3)) + 7 = 22\) (operation 2).In the sixth test case, you can first leave the order unchanged (operation 1), then choose \(l = 1, r = 5\) and get the sum \(-((-1)+(-2)+(-3)+(-4)+(-5))=15\) (operation 2).
|
Input: 83-2 3 -31020 11-99410 -2 -3 75-1 -2 -3 -4 -56-41 22 -69 73 -15 -50121 2 3 4 5 6 7 8 9 10 11 12 | Output: 8 0 1 99 22 15 270 78
|
Beginner
| 3 | 735 | 415 | 120 | 19 |
340 |
C
|
340C
|
C. Tourist Problem
| 1,600 |
combinatorics; implementation; math
|
Iahub is a big fan of tourists. He wants to become a tourist himself, so he planned a trip. There are n destinations on a straight road that Iahub wants to visit. Iahub starts the excursion from kilometer 0. The n destinations are described by a non-negative integers sequence a1, a2, ..., an. The number ak represents that the kth destination is at distance ak kilometers from the starting point. No two destinations are located in the same place. Iahub wants to visit each destination only once. Note that, crossing through a destination is not considered visiting, unless Iahub explicitly wants to visit it at that point. Also, after Iahub visits his last destination, he doesn't come back to kilometer 0, as he stops his trip at the last destination. The distance between destination located at kilometer x and next destination, located at kilometer y, is |x - y| kilometers. We call a ""route"" an order of visiting the destinations. Iahub can visit destinations in any order he wants, as long as he visits all n destinations and he doesn't visit a destination more than once. Iahub starts writing out on a paper all possible routes and for each of them, he notes the total distance he would walk. He's interested in the average number of kilometers he would walk by choosing a route. As he got bored of writing out all the routes, he asks you to help him.
|
The first line contains integer n (2 β€ n β€ 105). Next line contains n distinct integers a1, a2, ..., an (1 β€ ai β€ 107).
|
Output two integers β the numerator and denominator of a fraction which is equal to the wanted average number. The fraction must be irreducible.
|
Consider 6 possible routes: [2, 3, 5]: total distance traveled: |2 β 0| + |3 β 2| + |5 β 3| = 5; [2, 5, 3]: |2 β 0| + |5 β 2| + |3 β 5| = 7; [3, 2, 5]: |3 β 0| + |2 β 3| + |5 β 2| = 7; [3, 5, 2]: |3 β 0| + |5 β 3| + |2 β 5| = 8; [5, 2, 3]: |5 β 0| + |2 β 5| + |3 β 2| = 9; [5, 3, 2]: |5 β 0| + |3 β 5| + |2 β 3| = 8. The average travel distance is = = .
|
Input: 32 3 5 | Output: 22 3
|
Medium
| 3 | 1,361 | 119 | 144 | 3 |
1,102 |
F
|
1102F
|
F. Elongated Matrix
| 2,000 |
binary search; bitmasks; brute force; dp; graphs
|
You are given a matrix \(a\), consisting of \(n\) rows and \(m\) columns. Each cell contains an integer in it.You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: firstly visit all cells of the first column from the top row to the bottom one, then the same for the second column and so on. During the traversal you write down the sequence of the numbers on the cells in the same order you visited them. Let that sequence be \(s_1, s_2, \dots, s_{nm}\). The traversal is \(k\)-acceptable if for all \(i\) (\(1 \le i \le nm - 1\)) \(|s_i - s_{i + 1}| \ge k\).Find the maximum integer \(k\) such that there exists some order of rows of matrix \(a\) that it produces a \(k\)-acceptable traversal.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 16\), \(1 \le m \le 10^4\), \(2 \le nm\)) β the number of rows and the number of columns, respectively.Each of the next \(n\) lines contains \(m\) integers (\(1 \le a_{i, j} \le 10^9\)) β the description of the matrix.
|
Print a single integer \(k\) β the maximum number such that there exists some order of rows of matrix \(a\) that it produces an \(k\)-acceptable traversal.
|
In the first example you can rearrange rows as following to get the \(5\)-acceptable traversal:5 310 84 39 9Then the sequence \(s\) will be \([5, 10, 4, 9, 3, 8, 3, 9]\). Each pair of neighbouring elements have at least \(k = 5\) difference between them.In the second example the maximum \(k = 0\), any order is \(0\)-acceptable.In the third example the given order is already \(3\)-acceptable, you can leave it as it is.
|
Input: 4 2 9 9 10 8 5 3 4 3 | Output: 5
|
Hard
| 5 | 857 | 286 | 155 | 11 |
1,988 |
E
|
1988E
|
E. Range Minimum Sum
| 2,300 |
binary search; brute force; data structures; divide and conquer; implementation
|
For an array \([a_1,a_2,\ldots,a_n]\) of length \(n\), define \(f(a)\) as the sum of the minimum element over all subsegments. That is, $$$\(f(a)=\sum_{l=1}^n\sum_{r=l}^n \min_{l\le i\le r}a_i.\)\(A permutation is a sequence of integers from \)1\( to \)n\( of length \)n\( containing each number exactly once. You are given a permutation \)[a_1,a_2,\ldots,a_n]\(. For each \)i\(, solve the following problem independently: Erase \)a_i\( from \)a\(, concatenating the remaining parts, resulting in \)b = [a_1,a_2,\ldots,a_{i-1},\;a_{i+1},\ldots,a_{n}]\(. Calculate \)f(b)$$$.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1\le n\le 5\cdot 10^5\)).The second line of each test case contains \(n\) distinct integers \(a_1,\ldots,a_n\) (\(1\le a_i\le n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, print one line containing \(n\) integers. The \(i\)-th integer should be the answer when erasing \(a_i\).
|
In the second test case, \(a=[3,1,2]\). When removing \(a_1\), \(b=[1,2]\). \(f(b)=1+2+\min\{1,2\}=4\). When removing \(a_2\), \(b=[3,2]\). \(f(b)=3+2+\min\{3,2\}=7\). When removing \(a_3\), \(b=[3,1]\). \(f(b)=3+1+\min\{3,1\}=5\).
|
Input: 41133 1 254 2 1 5 388 1 4 6 7 3 5 2 | Output: 0 4 7 5 19 21 27 17 19 79 100 72 68 67 80 73 80
|
Expert
| 5 | 574 | 434 | 125 | 19 |
2,084 |
F
|
2084F
|
F. Skyscape
| 2,900 |
constructive algorithms; data structures; greedy
|
You are given a permutation \(a\) of length \(n\)\(^{\text{β}}\).We say that a permutation \(b\) of length \(n\) is good if the two permutations \(a\) and \(b\) can become the same after performing the following operation at most \(n\) times (possibly zero): Choose two integers \(l, r\) such that \(1 \le l < r \le n\) and \(a_r = \min(a_l, a_{l + 1}, \ldots, a_r)\). Cyclically shift the subsegment \([a_l, a_{l + 1}, \ldots, a_r]\) one position to the right. In other words, replace \(a\) with \([a_1, \ldots, a_{l - 1}, \; a_r, a_l, a_{l + 1}, \ldots, a_{r - 1}, \; a_{r + 1}, \ldots, a_n]\). You are also given a permutation \(c\) of length \(n\) where some elements are missing and represented by \(0\).You need to find a good permutation \(b_1, b_2, \ldots, b_n\) such that \(b\) can be formed by filling in the missing elements of \(c\) (i.e., for all \(1 \le i \le n\), if \(c_i \ne 0\), then \(b_i = c_i\)). If it is impossible, output \(-1\).\(^{\text{β}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)). It is guaranteed that \(a\) is a permutation of length \(n\).The third line of each test case contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(0 \le c_i \le n\)). It is guaranteed that the elements of \(c\) that are not \(0\) are distinct.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case: If it is impossible to find such a good permutation \(b\), output a single integer \(-1\). Otherwise, output \(n\) integers \(b_1, b_2, \ldots, b_n\) β the good permutation \(b\) you've found. You need to ensure that for all \(1 \le i \le n\), if \(c_i \ne 0\), then \(b_i = c_i\). If there are multiple answers, output any of them.
|
In the first test case, \(b = [1, 2]\) is a valid answer since after performing the following operation, \(a\) and \(b\) will become the same: Choose \(l = 1, r = 2\) and cyclically shift the subsegment \([a_1, a_2]\) one position to the right. Then \(a\) will become \([1, 2]\). In the second test case, \(b = [2, 3, 4, 1]\) is a valid answer since after performing the following operation, \(a\) and \(b\) will become the same: Choose \(l = 1, r = 2\) and cyclically shift the subsegment \([a_1, a_2]\) one position to the right. Then \(a\) will become \([2, 3, 4, 1]\). In the third test case, \(b = [1, 3, 2, 4, 5]\) is a valid answer since after performing the following operation, \(a\) and \(b\) will become the same: Choose \(l = 1, r = 3\) and cyclically shift the subsegment \([a_1, a_2, a_3]\) one position to the right. Then \(a\) will become \([1, 3, 2, 5, 4]\). Choose \(l = 4, r = 5\) and cyclically shift the subsegment \([a_4, a_5]\) one position to the right. Then \(a\) will become \([1, 3, 2, 4, 5]\). In the fourth test case, \(b = [3, 2, 1, 5, 4]\) is a valid answer since \(a\) and \(b\) are already the same.In the fifth test case, it is impossible to find such a good permutation \(b\), so you should output \(-1\).In the sixth test case, \(b = [3, 2, 1, 5, 4, 6]\) is a valid answer since after performing the following operation, \(a\) and \(b\) will become the same: Choose \(l = 2, r = 4\) and cyclically shift the subsegment \([a_2, a_3, a_4]\) one position to the right. Then \(a\) will become \([3, 2, 5, 6, 1, 4]\). Choose \(l = 3, r = 5\) and cyclically shift the subsegment \([a_3, a_4, a_5]\) one position to the right. Then \(a\) will become \([3, 2, 1, 5, 6, 4]\). Choose \(l = 5, r = 6\) and cyclically shift the subsegment \([a_5, a_6]\) one position to the right. Then \(a\) will become \([3, 2, 1, 5, 4, 6]\).
|
Input: 922 11 243 2 4 12 0 0 153 2 1 5 41 3 0 0 053 2 1 5 43 2 1 5 453 2 1 5 43 2 5 1 463 5 6 2 1 40 2 0 5 0 063 5 6 2 1 40 2 0 6 4 096 9 2 4 1 7 8 3 50 2 5 9 0 0 0 8 098 5 3 9 1 7 4 6 20 0 8 0 7 0 4 0 2 | Output: 1 2 2 3 4 1 1 3 2 4 5 3 2 1 5 4 -1 3 2 1 5 4 6 -1 -1 1 3 8 5 7 9 4 6 2
|
Master
| 3 | 1,292 | 700 | 352 | 20 |
513 |
B1
|
513B1
|
B1. Permutations
| 1,400 |
brute force
|
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum:Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p).
|
The single line of input contains two integers n and m (1 β€ m β€ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p).The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem B1 (3 points), the constraint 1 β€ n β€ 8 will hold. In subproblem B2 (4 points), the constraint 1 β€ n β€ 50 will hold.
|
Output n number forming the required permutation.
|
In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
|
Input: 2 2 | Output: 2 1
|
Easy
| 1 | 219 | 502 | 49 | 5 |
1,650 |
A
|
1650A
|
A. Deletions of Two Adjacent Letters
| 800 |
implementation; strings
|
The string \(s\) is given, the string length is odd number. The string consists of lowercase letters of the Latin alphabet.As long as the string length is greater than \(1\), the following operation can be performed on it: select any two adjacent letters in the string \(s\) and delete them from the string. For example, from the string ""lemma"" in one operation, you can get any of the four strings: ""mma"", ""lma"", ""lea"" or ""lem"" In particular, in one operation, the length of the string reduces by \(2\).Formally, let the string \(s\) have the form \(s=s_1s_2 \dots s_n\) (\(n>1\)). During one operation, you choose an arbitrary index \(i\) (\(1 \le i < n\)) and replace \(s=s_1s_2 \dots s_{i-1}s_{i+2} \dots s_n\).For the given string \(s\) and the letter \(c\), determine whether it is possible to make such a sequence of operations that in the end the equality \(s=c\) will be true? In other words, is there such a sequence of operations that the process will end with a string of length \(1\), which consists of the letter \(c\)?
|
The first line of input data contains an integer \(t\) (\(1 \le t \le 10^3\)) β the number of input test cases.The descriptions of the \(t\) cases follow. Each test case is represented by two lines: string \(s\), which has an odd length from \(1\) to \(49\) inclusive and consists of lowercase letters of the Latin alphabet; is a string containing one letter \(c\), where \(c\) is a lowercase letter of the Latin alphabet.
|
For each test case in a separate line output: YES, if the string \(s\) can be converted so that \(s=c\) is true; NO otherwise. You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response).
|
In the first test case, \(s\)=""abcde"". You need to get \(s\)=""c"". For the first operation, delete the first two letters, we get \(s\)=""cde"". In the second operation, we delete the last two letters, so we get the expected value of \(s\)=""c"".In the third test case, \(s\)=""x"", it is required to get \(s\)=""y"". Obviously, this cannot be done.
|
Input: 5abcdecabcdebxyaaaaaaaaaaaaaaaacontestt | Output: YES NO NO YES YES
|
Beginner
| 2 | 1,043 | 422 | 256 | 16 |
1,002 |
D3
|
1002D3
|
D3. Oracle for majority function
| 1,600 |
*special
|
Implement a quantum oracle on 3 qubits which implements a majority function. Majority function on 3-bit vectors is defined as follows: if vector has two or three 1s, and 0 if it has zero or one 1s.For an explanation on how this type of quantum oracles works, see Introduction to quantum oracles.You have to implement an operation which takes the following inputs: an array of 3 qubits x in arbitrary state (input register), a qubit y in arbitrary state (output qubit).The operation doesn't have an output; its ""output"" is the state in which it leaves the qubits.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (x : Qubit[], y : Qubit) : () { body { // your code here } }}
|
Medium
| 1 | 772 | 0 | 0 | 10 |
||||
1,352 |
F
|
1352F
|
F. Binary String Reconstruction
| 1,500 |
constructive algorithms; dfs and similar; math
|
For some binary string \(s\) (i.e. each character \(s_i\) is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length \(2\) were written. For each pair (substring of length \(2\)), the number of '1' (ones) in it was calculated.You are given three numbers: \(n_0\) β the number of such pairs of consecutive characters (substrings) where the number of ones equals \(0\); \(n_1\) β the number of such pairs of consecutive characters (substrings) where the number of ones equals \(1\); \(n_2\) β the number of such pairs of consecutive characters (substrings) where the number of ones equals \(2\). For example, for the string \(s=\)""1110011110"", the following substrings would be written: ""11"", ""11"", ""10"", ""00"", ""01"", ""11"", ""11"", ""11"", ""10"". Thus, \(n_0=1\), \(n_1=3\), \(n_2=5\).Your task is to restore any suitable binary string \(s\) from the given values \(n_0, n_1, n_2\). It is guaranteed that at least one of the numbers \(n_0, n_1, n_2\) is greater than \(0\). Also, it is guaranteed that a solution exists.
|
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases in the input. Then test cases follow.Each test case consists of one line which contains three integers \(n_0, n_1, n_2\) (\(0 \le n_0, n_1, n_2 \le 100\); \(n_0 + n_1 + n_2 > 0\)). It is guaranteed that the answer for given \(n_0, n_1, n_2\) exists.
|
Print \(t\) lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
|
Input: 7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0 | Output: 1110011110 0011 0110001100101011 10 0000111 1111 000
|
Medium
| 3 | 1,098 | 340 | 157 | 13 |
|
1,779 |
G
|
1779G
|
G. The Game of the Century
| 3,000 |
constructive algorithms; graphs; shortest paths
|
The time has finally come, MKnez and Baltic are to host The Game of the Century. For that purpose, they built a village to lodge its participants.The village has the shape of an equilateral triangle delimited by three roads of length \(n\). It is cut into \(n^2\) smaller equilateral triangles, of side length \(1\), by \(3n-3\) additional roads which run parallel to the sides. See the figure for \(n=3\). Each of the \(3n\) roads is made of multiple (possibly \(1\)) road segments of length \(1\) which connect adjacent intersections. The direction has already been chosen for each of the \(3n\) roads (so, for each road, the same direction is assigned to all its road segments). Traffic can only go in the specified directions (i. e. the roads are monodirectional).You are tasked with making adjustments to the traffic plan so that from each intersection it is possible to reach every other intersection. Specifically, you can invert the traffic direction of any number of road segments of length \(1\). What is the minimal number of road segments for which you need to invert the traffic direction?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of the test cases follows.The first line of each test case contains a positive integer \(n\) (\(1\leq n\leq 10^5\)) β the size of the triangular village's sides.Three lines follow, each containing a binary string of length \(n\) which describes the traffic directions of the roads. The \(i\)-th of the following three lines contains a binary string \(s_i\) of length \(n\) representing the direction of each road parallel to the road segment denoted by \(i\) in the picture above. In particular, the \(j\)-th character of \(s_i\) is ""1"" if the \(j\)-th shortest road (parallel to the road segment denoted by \(i\) in the picture) has the same direction of the road segment denoted by \(i\) in the picture, while it is ""0"" if it has the opposite direction. So the first character of \(s_i\) describes the direction of the road containing only \(1\) road segment, while the last character describes the direction of the road containing \(n\) road segments.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print the minimum number of road segments for which you need to invert the traffic direction.
|
The first example corresponds to the picture in the statement. There exist multiple solutions that invert the traffic direction of exactly \(2\) road segments, but inverting only \(1\) road segment never makes it possible to reach every intersection from any other. One of the possible solutions is shown in the picture below in which the inverted road segments are highlighted in blue. In the second example, the answer is \(0\) since it is already possible to reach every intersection from any other.
|
Input: 3300100101010003111011100 | Output: 2 0 3
|
Master
| 3 | 1,102 | 1,182 | 113 | 17 |
2,125 |
F
|
2125F
|
F. Timofey and Docker
| 3,000 |
binary search; divide and conquer; dp
|
Not so long ago, a certain Timofey learned about docker and now wants to present a report about it at a conference. He has already prepared the text \(s\).There are \(n\) people attending the conference. The \(i\)-th attendee will understand Timofey's report if the number of occurrences of the word ""docker"" as a contiguous substring in the text is not less than \(l_i\) and not greater than \(r_i\).To ensure that as many people as possible learn about docker, Timofey can change characters in his text.Help Timofey determine the minimum number of characters that need to be changed so that the topic is understood by the maximum possible number of attendees.
|
Each test consists of several 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 of each test case contains one string \(s\) (\(1 \le |s| \le 5 \cdot 10^{5}\)) β Timofey's text, which consists of lowercase Latin letters.The second line of each test case contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^{5}\)) β the number of attendees.The next \(n\) lines of each test case contain two integers \(l_{i}, r_{i}\) (\(1 \le l_{i} \le r_{i} \le 10^{9}\)).Additional constraints on the input data: The sum of \(|s|\) across all test cases does not exceed \(5 \cdot 10^{5}\); The sum of \(n\) across all test cases does not exceed \(5 \cdot 10^{5}\).
|
For each test case, print one integer β the minimum number of characters that need to be changed so that the topic is understood by the maximum number of attendees.
|
Let's consider the first test in more detail: in the first test case, it is possible to change all the characters '\(x\)' at the end of the string to the whole word ""\(docker\)"", so that all \(3\) people will understand the topic; in the second test case, it is possible to change some characters in \(s\) as follows: ""\(l\color{red}{docker}kd\color{red}{ocker}\)"" and the topic will be understood by \(3\) people, which is the maximum.
|
Input: 2dockerdockerxxxxxx33 32 41 5ljglsjfkdieufj51 53 32 43 72 9 | Output: 6 11
|
Master
| 3 | 663 | 761 | 164 | 21 |
515 |
B
|
515B
|
B. Drazil and His Happy Friends
| 1,300 |
brute force; dsu; meet-in-the-middle; number theory
|
Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.There are n boys and m girls among his friends. Let's number them from 0 to n - 1 and 0 to m - 1 separately. In i-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, i starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.Drazil wants to know whether he can use this plan to make all his friends become happy at some moment.
|
The first line contains two integer n and m (1 β€ n, m β€ 100).The second line contains integer b (0 β€ b β€ n), denoting the number of happy boys among friends of Drazil, and then follow b distinct integers x1, x2, ..., xb (0 β€ xi < n), denoting the list of indices of happy boys.The third line conatins integer g (0 β€ g β€ m), denoting the number of happy girls among friends of Drazil, and then follow g distinct integers y1, y2, ... , yg (0 β€ yj < m), denoting the list of indices of happy girls.It is guaranteed that there is at least one person that is unhappy among his friends.
|
If Drazil can make all his friends become happy by this plan, print ""Yes"". Otherwise, print ""No"".
|
By we define the remainder of integer division of i by k.In first sample case: On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment.
|
Input: 2 301 0 | Output: Yes
|
Easy
| 4 | 706 | 580 | 101 | 5 |
830 |
E
|
830E
|
E. Perpetual Motion Machine
| 3,100 |
constructive algorithms; dp; graphs; implementation; math; trees
|
Developer Petr thinks that he invented a perpetual motion machine. Namely, he has a lot of elements, which work in the following way.Each element has one controller that can be set to any non-negative real value. If a controller is set on some value x, then the controller consumes x2 energy units per second. At the same time, any two elements connected by a wire produce yΒ·z energy units per second, where y and z are the values set on their controllers.Petr has only a limited number of wires, so he has already built some scheme of elements and wires, and is now interested if it's possible to set the controllers in such a way that the system produces at least as much power as it consumes, and at least one controller is set on the value different from 0. Help him check this, and if it's possible, find the required integer values that should be set.It is guaranteed that if there exist controllers' settings satisfying the above conditions, then there exist required integer values not greater than 106.
|
There are several (at least one) test cases in the input. The first line contains single integer β the number of test cases.There is an empty line before each test case. The first line of test case contains two integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of elements in the scheme and the number of wires.After that, m lines follow, each of them contains two integers a and b (1 β€ a, b β€ n) β two elements connected by a wire. No element is connected with itself, no two elements are connected by more than one wire.It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 105.For hacks you can only use tests with one test case.
|
Print answer for each test case.For each test case print ""YES"" if it's possible to set the controllers in such a way that the consumed power is not greater than the power produced, and the required values on the next line. The settings should be integers from 0 to 106, inclusive, and at least one value should be different from 0. If there are multiple answers, print any of them.If it's not possible to set the controllers in the required way, print one line ""NO"".
|
In the first example it's possible to set the controllers in the required way, for example, in the following way: set 1 on the first element, set 2 on the second and on the third, set 1 on the fourth. The consumed power is then equal to 12 + 22 + 22 + 12 = 10 energy units per second, the produced power is equal to 1Β·2 + 2Β·2 + 2Β·1 + 2Β·1 = 10 energy units per second. Thus the answer is ""YES"".In the second test case it's not possible to set the controllers in the required way. For example, if we set all controllers to 0.5, then the consumed powers equals 0.75 energy units per second, while produced power equals 0.5 energy units per second.
|
Input: 4 4 41 22 33 44 2 3 22 33 1 4 61 23 44 21 41 33 2 10 92 13 25 26 22 72 82 92 104 2 | Output: YES1 2 2 1NOYES1 1 1 1YES1 5 1 1 1 1 1 1 1 1
|
Master
| 6 | 1,011 | 671 | 470 | 8 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.