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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
496 |
D
|
496D
|
D. Tennis Game
| 1,900 |
binary search
|
Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers.To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets.Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options?
|
The first line contains a single integer n β the length of the sequence of games (1 β€ n β€ 105).The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena.It is not guaranteed that at least one option for numbers s and t corresponds to the given record.
|
In the first line print a single number k β the number of options for numbers s and t.In each of the following k lines print two integers si and ti β the option for numbers s and t. Print the options in the order of increasing si, and for equal si β in the order of increasing ti.
|
Input: 51 2 1 2 1 | Output: 21 33 1
|
Hard
| 1 | 1,197 | 345 | 280 | 4 |
|
869 |
E
|
869E
|
E. The Untended Antiquity
| 2,400 |
data structures; hashing
|
Adieu l'ami.Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.The space is represented by a rectangular grid of n Γ m cells, arranged into n rows and m columns. The c-th cell in the r-th row is denoted by (r, c).Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by ""1 r1 c1 r2 c2"" means Oshino's placing barriers around a rectangle with two corners being (r1, c1) and (r2, c2) and sides parallel to squares sides. Similarly, ""2 r1 c1 r2 c2"" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the n Γ m area.Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. ""3 r1 c1 r2 c2"" means that Koyomi tries to walk from (r1, c1) to (r2, c2) without crossing barriers.And you're here to tell Koyomi the feasibility of each of his attempts.
|
The first line of input contains three space-separated integers n, m and q (1 β€ n, m β€ 2 500, 1 β€ q β€ 100 000) β the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively.The following q lines each describes an action, containing five space-separated integers t, r1, c1, r2, c2 (1 β€ t β€ 3, 1 β€ r1, r2 β€ n, 1 β€ c1, c2 β€ m) β the type and two coordinates of an action. Additionally, the following holds depending on the value of t: If t = 1: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1; If t = 2: 2 β€ r1 β€ r2 β€ n - 1, 2 β€ c1 β€ c2 β€ m - 1, the specified group of barriers exist on the ground before the removal. If t = 3: no extra restrictions.
|
For each of Koyomi's attempts (actions with t = 3), output one line β containing ""Yes"" (without quotes) if it's feasible, and ""No"" (without quotes) otherwise.
|
For the first example, the situations of Koyomi's actions are illustrated below.
|
Input: 5 6 51 2 2 4 51 3 3 3 33 4 4 1 12 2 2 4 53 1 1 4 4 | Output: NoYes
|
Expert
| 2 | 1,122 | 690 | 162 | 8 |
833 |
D
|
833D
|
D. Red-Black Cobweb
| 2,800 |
data structures; divide and conquer; implementation; trees
|
Slastyona likes to watch life of nearby grove's dwellers. This time she watches a strange red-black spider sitting at the center of a huge cobweb.The cobweb is a set of n nodes connected by threads, each of the treads is either red of black. Using these threads, the spider can move between nodes. No thread connects a node to itself, and between any two nodes there is a unique sequence of threads connecting them.Slastyona decided to study some special qualities of the cobweb. She noticed that each of the threads has a value of clamminess x.However, Slastyona is mostly interested in jelliness of the cobweb. Consider those of the shortest paths between each pair of nodes on which the numbers of red and black threads differ at most twice. For each such path compute the product of the clamminess of threads on the path.The jelliness of the cobweb is the product of all obtained values among all paths. Those paths that differ by direction only are counted only once.Of course, this number can be huge, so Slastyona asks you to compute the jelliness of the given cobweb and print the answer modulo 109 + 7.
|
The first line contains the number of nodes n (2 β€ n β€ 105).The next n - 1 lines contain four integers each, denoting the i-th thread of the cobweb: the nodes it connects ui, vi (1 β€ ui β€ n, 1 β€ vi β€ n), the clamminess of the thread xi (1 β€ x β€ 109 + 6), and the color of the thread ci (). The red color is denoted by 0, and the black color is denoted by 1.
|
Print single integer the jelliness of the cobweb modulo 109 + 7. If there are no paths such that the numbers of red and black threads differ at most twice, print 1.
|
In the first example there are 4 pairs of nodes such that the numbers of threads of both colors on them differ at most twice. There pairs are (1, 3) with product of clamminess equal to 45, (1, 5) with product of clamminess equal to 45, (3, 4) with product of clamminess equal to 25 and (4, 5) with product of clamminess equal to 25. The jelliness of the cobweb is equal to 1265625.
|
Input: 51 2 9 02 3 5 12 4 5 02 5 5 1 | Output: 1265625
|
Master
| 4 | 1,111 | 357 | 164 | 8 |
291 |
E
|
291E
|
E. Tree-String Problem
| 2,000 |
*special; dfs and similar; hashing; strings
|
A rooted tree is a non-directed connected graph without any cycles with a distinguished vertex, which is called the tree root. Consider the vertices of a rooted tree, that consists of n vertices, numbered from 1 to n. In this problem the tree root is the vertex number 1.Let's represent the length of the shortest by the number of edges path in the tree between vertices v and u as d(v, u).A parent of vertex v in the rooted tree with the root in vertex r (v β r) is vertex pv, such that d(r, pv) + 1 = d(r, v) and d(pv, v) = 1. For example, on the picture the parent of vertex v = 5 is vertex p5 = 2.One day Polycarpus came across a rooted tree, consisting of n vertices. The tree wasn't exactly ordinary: it had strings written on its edges. Polycarpus positioned the tree on the plane so as to make all edges lead from top to bottom if you go from the vertex parent to the vertex (see the picture). For any edge that lead from vertex pv to vertex v (1 < v β€ n), he knows string sv that is written on it. All strings are written on the edges from top to bottom. For example, on the picture s7=""ba"". The characters in the strings are numbered starting from 0. An example of Polycarpus's tree (corresponds to the example from the statement) Polycarpus defines the position in this tree as a specific letter on a specific string. The position is written as a pair of integers (v, x) that means that the position is the x-th letter of the string sv (1 < v β€ n, 0 β€ x < |sv|), where |sv| is the length of string sv. For example, the highlighted letters are positions (2, 1) and (3, 1).Let's consider the pair of positions (v, x) and (u, y) in Polycarpus' tree, such that the way from the first position to the second goes down on each step. We will consider that the pair of such positions defines string z. String z consists of all letters on the way from (v, x) to (u, y), written in the order of this path. For example, in the picture the highlighted positions define string ""bacaba"".Polycarpus has a string t, he wants to know the number of pairs of positions that define string t. Note that the way from the first position to the second in the pair must go down everywhere. Help him with this challenging tree-string problem!
|
The first line contains integer n (2 β€ n β€ 105) β the number of vertices of Polycarpus's tree. Next n - 1 lines contain the tree edges. The i-th of them contains number pi + 1 and string si + 1 (1 β€ pi + 1 β€ n; pi + 1 β (i + 1)). String si + 1 is non-empty and consists of lowercase English letters. The last line contains string t. String t consists of lowercase English letters, its length is at least 2.It is guaranteed that the input contains at most 3Β·105 English letters.
|
Print a single integer β the required number.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In the first test case string ""aba"" is determined by the pairs of positions: (2, 0) and (5, 0); (5, 2) and (6, 1); (5, 2) and (3, 1); (4, 0) and (4, 2); (4, 4) and (4, 6); (3, 3) and (3, 5).Note that the string is not defined by the pair of positions (7, 1) and (5, 0), as the way between them doesn't always go down.
|
Input: 71 ab5 bacaba1 abacaba2 aca5 ba2 baaba | Output: 6
|
Hard
| 4 | 2,231 | 477 | 192 | 2 |
567 |
B
|
567B
|
B. Berland National Library
| 1,300 |
implementation
|
Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form ""reader entered room"", ""reader left room"". Every reader is assigned a registration number during the registration procedure at the library β it's a unique integer from 1 to 106. Thus, the system logs events of two forms: ""+ ri"" β the reader with registration number ri entered the room; ""- ri"" β the reader with registration number ri left the room. The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you.
|
The first line contains a positive integer n (1 β€ n β€ 100) β the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as ""+ ri"" or ""- ri"", where ri is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers).It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors.
|
Print a single integer β the minimum possible capacity of the reading room.
|
In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.
|
Input: 6+ 12001- 12001- 1- 1200+ 1+ 7 | Output: 3
|
Easy
| 1 | 1,426 | 633 | 75 | 5 |
2,051 |
D
|
2051D
|
D. Counting Pairs
| 1,200 |
binary search; sortings; two pointers
|
You are given a sequence \(a\), consisting of \(n\) integers, where the \(i\)-th element of the sequence is equal to \(a_i\). You are also given two integers \(x\) and \(y\) (\(x \le y\)).A pair of integers \((i, j)\) is considered interesting if the following conditions are met: \(1 \le i < j \le n\); if you simultaneously remove the elements at positions \(i\) and \(j\) from the sequence \(a\), the sum of the remaining elements is at least \(x\) and at most \(y\). Your task is to determine the number of interesting pairs of integers for the given sequence \(a\).
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines: The first line contains three integers \(n, x, y\) (\(3 \le n \le 2 \cdot 10^5\), \(1 \le x \le y \le 2 \cdot 10^{14}\)); The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{9}\)). Additional constraint on the input: the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output one integer β the number of interesting pairs of integers for the given sequence \(a\).
|
In the first example, there are \(4\) interesting pairs of integers: \((1, 2)\); \((1, 4)\); \((2, 3)\); \((3, 4)\).
|
Input: 74 8 104 6 3 66 22 274 9 6 3 4 53 8 103 2 13 1 12 3 43 3 63 2 14 4 123 3 2 16 8 81 1 2 2 2 3 | Output: 4 7 0 0 1 5 6
|
Easy
| 3 | 570 | 453 | 114 | 20 |
1,320 |
B
|
1320B
|
B. Navigation System
| 1,700 |
dfs and similar; graphs; shortest paths
|
The map of Bertown can be represented as a set of \(n\) intersections, numbered from \(1\) to \(n\) and connected by \(m\) one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection \(v\) to another intersection \(u\) is the path that starts in \(v\), ends in \(u\) and has the minimum length among all such paths.Polycarp lives near the intersection \(s\) and works in a building near the intersection \(t\). Every day he gets from \(s\) to \(t\) by car. Today he has chosen the following path to his workplace: \(p_1\), \(p_2\), ..., \(p_k\), where \(p_1 = s\), \(p_k = t\), and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from \(s\) to \(t\).Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection \(s\), the system chooses some shortest path from \(s\) to \(t\) and shows it to Polycarp. Let's denote the next intersection in the chosen path as \(v\). If Polycarp chooses to drive along the road from \(s\) to \(v\), then the navigator shows him the same shortest path (obviously, starting from \(v\) as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection \(w\) instead, the navigator rebuilds the path: as soon as Polycarp arrives at \(w\), the navigation system chooses some shortest path from \(w\) to \(t\) and shows it to Polycarp. The same process continues until Polycarp arrives at \(t\): if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path \([1, 2, 3, 4]\) (\(s = 1\), \(t = 4\)): Check the picture by the link http://tk.codeforces.com/a.png When Polycarp starts at \(1\), the system chooses some shortest path from \(1\) to \(4\). There is only one such path, it is \([1, 5, 4]\); Polycarp chooses to drive to \(2\), which is not along the path chosen by the system. When Polycarp arrives at \(2\), the navigator rebuilds the path by choosing some shortest path from \(2\) to \(4\), for example, \([2, 6, 4]\) (note that it could choose \([2, 3, 4]\)); Polycarp chooses to drive to \(3\), which is not along the path chosen by the system. When Polycarp arrives at \(3\), the navigator rebuilds the path by choosing the only shortest path from \(3\) to \(4\), which is \([3, 4]\); Polycarp arrives at \(4\) along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get \(2\) rebuilds in this scenario. Note that if the system chose \([2, 3, 4]\) instead of \([2, 6, 4]\) during the second step, there would be only \(1\) rebuild (since Polycarp goes along the path, so the system maintains the path \([3, 4]\) during the third step).The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le m \le 2 \cdot 10^5\)) β the number of intersections and one-way roads in Bertown, respectively.Then \(m\) lines follow, each describing a road. Each line contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) denoting a road from intersection \(u\) to intersection \(v\). All roads in Bertown are pairwise distinct, which means that each ordered pair \((u, v)\) appears at most once in these \(m\) lines (but if there is a road \((u, v)\), the road \((v, u)\) can also appear).The following line contains one integer \(k\) (\(2 \le k \le n\)) β the number of intersections in Polycarp's path from home to his workplace.The last line contains \(k\) integers \(p_1\), \(p_2\), ..., \(p_k\) (\(1 \le p_i \le n\), all these integers are pairwise distinct) β the intersections along Polycarp's path in the order he arrived at them. \(p_1\) is the intersection where Polycarp lives (\(s = p_1\)), and \(p_k\) is the intersection where Polycarp's workplace is situated (\(t = p_k\)). It is guaranteed that for every \(i \in [1, k - 1]\) the road from \(p_i\) to \(p_{i + 1}\) exists, so the path goes along the roads of Bertown.
|
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.
|
Input: 6 9 1 5 5 4 1 2 2 3 3 4 4 1 2 6 6 4 4 2 4 1 2 3 4 | Output: 1 2
|
Medium
| 3 | 3,681 | 1,208 | 111 | 13 |
|
717 |
F
|
717F
|
F. Heroes of Making Magic III
| 2,600 |
data structures
|
Iβm strolling on sunshine, yeah-ah! And doesnβt it feel good! Well, it certainly feels good for our Heroes of Making Magic, who are casually walking on a one-directional road, fighting imps. Imps are weak and feeble creatures and they are not good at much. However, Heroes enjoy fighting them. For fun, if nothing else. Our Hero, Ignatius, simply adores imps. He is observing a line of imps, represented as a zero-indexed array of integers a of length n, where ai denotes the number of imps at the i-th position. Sometimes, imps can appear out of nowhere. When heroes fight imps, they select a segment of the line, start at one end of the segment, and finish on the other end, without ever exiting the segment. They can move exactly one cell left or right from their current position and when they do so, they defeat one imp on the cell that they moved to, so, the number of imps on that cell decreases by one. This also applies when heroes appear at one end of the segment, at the beginning of their walk. Their goal is to defeat all imps on the segment, without ever moving to an empty cell in it (without imps), since they would get bored. Since Ignatius loves imps, he doesnβt really want to fight them, so no imps are harmed during the events of this task. However, he would like you to tell him whether it would be possible for him to clear a certain segment of imps in the above mentioned way if he wanted to. You are given q queries, which have two types: 1 a b k β denotes that k imps appear at each cell from the interval [a, b] 2 a b - asks whether Ignatius could defeat all imps on the interval [a, b] in the way described above
|
The first line contains a single integer n (1 β€ n β€ 200 000), the length of the array a. The following line contains n integers a1, a2, ..., an (0 β€ ai β€ 5 000), the initial number of imps in each cell. The third line contains a single integer q (1 β€ q β€ 300 000), the number of queries. The remaining q lines contain one query each. Each query is provided by integers a, b and, possibly, k (0 β€ a β€ b < n, 0 β€ k β€ 5 000).
|
For each second type of query output 1 if it is possible to clear the segment, and 0 if it is not.
|
For the first query, one can easily check that it is indeed impossible to get from the first to the last cell while clearing everything. After we add 1 to the second position, we can clear the segment, for example by moving in the following way: .
|
Input: 32 2 232 0 21 1 1 12 0 2 | Output: 01
|
Expert
| 1 | 1,640 | 422 | 98 | 7 |
305 |
D
|
305D
|
D. Olya and Graph
| 2,200 |
combinatorics; math
|
Olya has got a directed non-weighted graph, consisting of n vertexes and m edges. We will consider that the graph vertexes are indexed from 1 to n in some manner. Then for any graph edge that goes from vertex v to vertex u the following inequation holds: v < u.Now Olya wonders, how many ways there are to add an arbitrary (possibly zero) number of edges to the graph so as the following conditions were met: You can reach vertexes number i + 1, i + 2, ..., n from any vertex number i (i < n). For any graph edge going from vertex v to vertex u the following inequation fulfills: v < u. There is at most one edge between any two vertexes. The shortest distance between the pair of vertexes i, j (i < j), for which j - i β€ k holds, equals j - i edges. The shortest distance between the pair of vertexes i, j (i < j), for which j - i > k holds, equals either j - i or j - i - k edges. We will consider two ways distinct, if there is the pair of vertexes i, j (i < j), such that first resulting graph has an edge from i to j and the second one doesn't have it.Help Olya. As the required number of ways can be rather large, print it modulo 1000000007 (109 + 7).
|
The first line contains three space-separated integers n, m, k (2 β€ n β€ 106, 0 β€ m β€ 105, 1 β€ k β€ 106).The next m lines contain the description of the edges of the initial graph. The i-th line contains a pair of space-separated integers ui, vi (1 β€ ui < vi β€ n) β the numbers of vertexes that have a directed edge from ui to vi between them. It is guaranteed that any pair of vertexes ui, vi has at most one edge between them. It also is guaranteed that the graph edges are given in the order of non-decreasing ui. If there are multiple edges going from vertex ui, then it is guaranteed that these edges are given in the order of increasing vi.
|
Print a single integer β the answer to the problem modulo 1000000007 (109 + 7).
|
In the first sample there are two ways: the first way is not to add anything, the second way is to add a single edge from vertex 2 to vertex 5.
|
Input: 7 8 21 22 33 43 64 54 75 66 7 | Output: 2
|
Hard
| 2 | 1,157 | 644 | 79 | 3 |
847 |
B
|
847B
|
B. Preparing for Merge Sort
| 1,600 |
binary search; data structures
|
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.Ivan represent his array with increasing sequences with help of the following algorithm.While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down. For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
|
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of elements in Ivan's array.The second line contains a sequence consisting of distinct integers a1, a2, ..., an (1 β€ ai β€ 109) β Ivan's array.
|
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
|
Input: 51 3 2 5 4 | Output: 1 3 5 2 4
|
Medium
| 2 | 1,083 | 214 | 182 | 8 |
|
1,203 |
F2
|
1203F2
|
F2. Complete the Projects (hard version)
| 2,300 |
dp; greedy
|
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.Polycarp is a very famous freelancer. His current rating is \(r\) units.Some very rich customers asked him to complete some projects for their companies. To complete the \(i\)-th project, Polycarp needs to have at least \(a_i\) units of rating; after he completes this project, his rating will change by \(b_i\) (his rating will increase or decrease by \(b_i\)) (\(b_i\) can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer.Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether.To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.Your task is to calculate the maximum possible size of such subset of projects.
|
The first line of the input contains two integers \(n\) and \(r\) (\(1 \le n \le 100, 1 \le r \le 30000\)) β the number of projects and the initial rating of Polycarp, respectively.The next \(n\) lines contain projects, one per line. The \(i\)-th project is represented as a pair of integers \(a_i\) and \(b_i\) (\(1 \le a_i \le 30000\), \(-300 \le b_i \le 300\)) β the rating required to complete the \(i\)-th project and the rating change after the project completion.
|
Print one integer β the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose.
|
Input: 3 4 4 6 10 -2 8 -1 | Output: 3
|
Expert
| 2 | 1,138 | 470 | 110 | 12 |
|
746 |
E
|
746E
|
E. Numbers Exchange
| 1,900 |
greedy; implementation; math
|
Eugeny has n cards, each of them has exactly one integer written on it. Eugeny wants to exchange some cards with Nikolay so that the number of even integers on his cards would equal the number of odd integers, and that all these numbers would be distinct. Nikolay has m cards, distinct numbers from 1 to m are written on them, one per card. It means that Nikolay has exactly one card with number 1, exactly one card with number 2 and so on. A single exchange is a process in which Eugeny gives one card to Nikolay and takes another one from those Nikolay has. Your task is to find the minimum number of card exchanges and determine which cards Eugeny should exchange.
|
The first line contains two integers n and m (2 β€ n β€ 2Β·105, 1 β€ m β€ 109) β the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that n is even.The second line contains a sequence of n positive integers a1, a2, ..., an (1 β€ ai β€ 109) β the numbers on Eugeny's cards.
|
If there is no answer, print -1.Otherwise, in the first line print the minimum number of exchanges. In the second line print n integers β Eugeny's cards after all the exchanges with Nikolay. The order of cards should coincide with the card's order in the input data. If the i-th card wasn't exchanged then the i-th number should coincide with the number from the input data. Otherwise, it is considered that this card was exchanged, and the i-th number should be equal to the number on the card it was exchanged to.If there are multiple answers, it is allowed to print any of them.
|
Input: 6 25 6 7 9 4 5 | Output: 15 6 7 9 4 2
|
Hard
| 3 | 667 | 298 | 581 | 7 |
|
1,451 |
B
|
1451B
|
B. Non-Substring Subsequence
| 900 |
dp; greedy; implementation; strings
|
Hr0d1y has \(q\) queries on a binary string \(s\) of length \(n\). A binary string is a string containing only characters '0' and '1'.A query is described by a pair of integers \(l_i\), \(r_i\) \((1 \leq l_i \lt r_i \leq n)\). For each query, he has to determine whether there exists a good subsequence in \(s\) that is equal to the substring \(s[l_i\ldots r_i]\). A substring \(s[i\ldots j]\) of a string \(s\) is the string formed by characters \(s_i s_{i+1} \ldots s_j\). String \(a\) is said to be a subsequence of string \(b\) if \(a\) can be obtained from \(b\) by deleting some characters without changing the order of the remaining characters. A subsequence is said to be good if it is not contiguous and has length \(\ge 2\). For example, if \(s\) is ""1100110"", then the subsequences \(s_1s_2s_4\) (""1100110"") and \(s_1s_5s_7\) (""1100110"") are good, while \(s_1s_2s_3\) (""1100110"") is not good. Can you help Hr0d1y answer each query?
|
The first line of the input contains a single integer \(t\) (\(1\leq t \leq 100\)) β the number of test cases. The description of each test case is as follows.The first line contains two integers \(n\) (\(2 \leq n \leq 100\)) and \(q\) (\(1\leq q \leq 100\)) β the length of the string and the number of queries. The second line contains the string \(s\).The \(i\)-th of the next \(q\) lines contains two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \lt r_i \leq n\)).
|
For each test case, output \(q\) lines. The \(i\)-th line of the output of each test case should contain ""YES"" if there exists a good subsequence equal to the substring \(s[l_i...r_i]\), and ""NO"" otherwise.You may print each letter in any case (upper or lower).
|
In the first test case, \(s[2\ldots 4] = \) ""010"". In this case \(s_1s_3s_5\) (""001000"") and \(s_2s_3s_6\) (""001000"") are good suitable subsequences, while \(s_2s_3s_4\) (""001000"") is not good. \(s[1\ldots 3] = \) ""001"". No suitable good subsequence exists. \(s[3\ldots 5] = \) ""100"". Here \(s_3s_5s_6\) (""001000"") is a suitable good subsequence.
|
Input: 2 6 3 001000 2 4 1 3 3 5 4 2 1111 1 4 2 3 | Output: YES NO YES NO YES
|
Beginner
| 4 | 950 | 466 | 265 | 14 |
1,487 |
C
|
1487C
|
C. Minimum Ties
| 1,500 |
brute force; constructive algorithms; dfs and similar; graphs; greedy; implementation; math
|
A big football championship will occur soon! \(n\) teams will compete in it, and each pair of teams will play exactly one game against each other.There are two possible outcomes of a game: the game may result in a tie, then both teams get \(1\) point; one team might win in a game, then the winning team gets \(3\) points and the losing team gets \(0\) points. The score of a team is the number of points it gained during all games that it played.You are interested in a hypothetical situation when all teams get the same score at the end of the championship. A simple example of that situation is when all games result in ties, but you want to minimize the number of ties as well.Your task is to describe a situation (choose the result of each game) so that all teams get the same score, and the number of ties is the minimum possible.
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Then the test cases follow. Each test case is described by one line containing one integer \(n\) (\(2 \le n \le 100\)) β the number of teams.
|
For each test case, print \(\frac{n(n - 1)}{2}\) integers describing the results of the games in the following order: the first integer should correspond to the match between team \(1\) and team \(2\), the second β between team \(1\) and team \(3\), then \(1\) and \(4\), ..., \(1\) and \(n\), \(2\) and \(3\), \(2\) and \(4\), ..., \(2\) and \(n\), and so on, until the game between the team \(n - 1\) and the team \(n\).The integer corresponding to the game between the team \(x\) and the team \(y\) should be \(1\) if \(x\) wins, \(-1\) if \(y\) wins, or \(0\) if the game results in a tie.All teams should get the same score, and the number of ties should be the minimum possible. If there are multiple optimal answers, print any of them. It can be shown that there always exists a way to make all teams have the same score.
|
In the first test case of the example, both teams get \(1\) point since the game between them is a tie.In the second test case of the example, team \(1\) defeats team \(2\) (team \(1\) gets \(3\) points), team \(1\) loses to team \(3\) (team \(3\) gets \(3\) points), and team \(2\) wins against team \(3\) (team \(2\) gets \(3\) points).
|
Input: 2 2 3 | Output: 0 1 -1 1
|
Medium
| 7 | 836 | 232 | 828 | 14 |
2,056 |
D
|
2056D
|
D. Unique Median
| 2,200 |
binary search; brute force; combinatorics; data structures; divide and conquer; dp
|
An array \(b\) of \(m\) integers is called good if, when it is sorted, \(b_{\left\lfloor \frac{m + 1}{2} \right\rfloor} = b_{\left\lceil \frac{m + 1}{2} \right\rceil}\). In other words, \(b\) is good if both of its medians are equal. In particular, \(\left\lfloor \frac{m + 1}{2} \right\rfloor = \left\lceil \frac{m + 1}{2} \right\rceil\) when \(m\) is odd, so \(b\) is guaranteed to be good if it has an odd length.You are given an array \(a\) of \(n\) integers. Calculate the number of good subarrays\(^{\text{β}}\) in \(a\).\(^{\text{β}}\)An array \(x\) is a subarray of an array \(y\) if \(x\) can be obtained from \(y\) 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 a single integer \(n\) (\(1 \le n \le 10^5\)) β the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le \color{red}{\textbf{10}}\)) β the given array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output a single integer representing the number of good subarrays in \(a\).
|
In the first case, every subarray is good since all its elements are equal to \(1\).In the second case, an example of a good subarray is \(b = [10, 2, 3, 3]\). When it is sorted, \(b = [2, 3, 3, 10]\), so \(b_{\left\lfloor \frac{4 + 1}{2} \right\rfloor} = b_{\left\lceil \frac{4 + 1}{2} \right\rceil} = b_2 = b_3 = 3\). Another example would be \(b = [1, 10, 2]\). On the other hand, \(b = [1, 10]\) is not good as its two medians are \(1\) and \(10\), which are not equal.
|
Input: 341 1 1 151 10 2 3 3106 3 2 3 5 3 4 2 3 5 | Output: 10 11 42
|
Hard
| 6 | 762 | 507 | 95 | 20 |
1,508 |
D
|
1508D
|
D. Swap Pass
| 3,000 |
constructive algorithms; geometry; sortings
|
Based on a peculiar incident at basketball practice, Akari came up with the following competitive programming problem!You are given \(n\) points on the plane, no three of which are collinear. The \(i\)-th point initially has a label \(a_i\), in such a way that the labels \(a_1, a_2, \dots, a_n\) form a permutation of \(1, 2, \dots, n\).You are allowed to modify the labels through the following operation: Choose two distinct integers \(i\) and \(j\) between \(1\) and \(n\). Swap the labels of points \(i\) and \(j\), and finally Draw the segment between points \(i\) and \(j\). A sequence of operations is valid if after applying all of the operations in the sequence in order, the \(k\)-th point ends up having the label \(k\) for all \(k\) between \(1\) and \(n\) inclusive, and the drawn segments don't intersect each other internally. Formally, if two of the segments intersect, then they must do so at a common endpoint of both segments.In particular, all drawn segments must be distinct.Find any valid sequence of operations, or say that none exist.
|
The first line contains an integer \(n\) (\(3 \le n \le 2000\)) β the number of points.The \(i\)-th of the following \(n\) lines contains three integers \(x_i\), \(y_i\), \(a_i\) (\(-10^6 \le x_i, y_i \le 10^6\), \(1 \le a_i \le n\)), representing that the \(i\)-th point has coordinates \((x_i, y_i)\) and initially has label \(a_i\).It is guaranteed that all points are distinct, no three points are collinear, and the labels \(a_1, a_2, \dots, a_n\) form a permutation of \(1, 2, \dots, n\).
|
If it is impossible to perform a valid sequence of operations, print \(-1\).Otherwise, print an integer \(k\) (\(0 \le k \le \frac{n(n - 1)}{2}\)) β the number of operations to perform, followed by \(k\) lines, each containing two integers \(i\) and \(j\) (\(1 \le i, j \le n\), \(i\neq j\)) β the indices of the points chosen for the operation.Note that you are not required to minimize or maximize the value of \(k\).If there are multiple possible answers, you may print any of them.
|
The following animation showcases the first sample test case. The black numbers represent the indices of the points, while the boxed orange numbers represent their labels. In the second test case, all labels are already in their correct positions, so no operations are necessary.
|
Input: 5 -1 -2 2 3 0 5 1 3 4 4 -3 3 5 2 1 | Output: 5 1 2 5 3 4 5 1 5 1 3
|
Master
| 3 | 1,059 | 494 | 485 | 15 |
560 |
B
|
560B
|
B. Gerald is into Art
| 1,200 |
constructive algorithms; implementation
|
Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an a1 Γ b1 rectangle, the paintings have shape of a a2 Γ b2 and a3 Γ b3 rectangles.Since the paintings are painted in the style of abstract art, it does not matter exactly how they will be rotated, but still, one side of both the board, and each of the paintings must be parallel to the floor. The paintings can touch each other and the edges of the board, but can not overlap or go beyond the edge of the board. Gerald asks whether it is possible to place the paintings on the board, or is the board he bought not large enough?
|
The first line contains two space-separated numbers a1 and b1 β the sides of the board. Next two lines contain numbers a2, b2, a3 and b3 β the sides of the paintings. All numbers ai, bi in the input are integers and fit into the range from 1 to 1000.
|
If the paintings can be placed on the wall, print ""YES"" (without the quotes), and if they cannot, print ""NO"" (without the quotes).
|
That's how we can place the pictures in the first test:And that's how we can do it in the third one.
|
Input: 3 21 32 1 | Output: YES
|
Easy
| 2 | 755 | 250 | 134 | 5 |
1,915 |
A
|
1915A
|
A. Odd One Out
| 800 |
bitmasks; implementation
|
You are given three digits \(a\), \(b\), \(c\). Two of them are equal, but the third one is different from the other two. Find the value that occurs exactly once.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 270\)) β the number of test cases.The only line of each test case contains three digits \(a\), \(b\), \(c\) (\(0 \leq a\), \(b\), \(c \leq 9\)). Two of the digits are equal, but the third one is different from the other two.
|
For each test case, output the value that occurs exactly once.
|
Input: 101 2 24 3 45 5 67 8 89 0 93 6 32 8 25 7 77 7 55 7 5 | Output: 1 3 6 7 0 6 8 5 5 7
|
Beginner
| 2 | 162 | 288 | 62 | 19 |
|
1,787 |
H
|
1787H
|
H. Codeforces Scoreboard
| 3,300 |
binary search; data structures; dp; geometry
|
You are participating in a Codeforces Round with \(n\) problems. You spend exactly one minute to solve each problem, the time it takes to submit a problem can be ignored. You can only solve at most one problem at any time. The contest starts at time \(0\), so you can make your first submission at any time \(t \ge 1\) minutes. Whenever you submit a problem, it is always accepted.The scoring of the \(i\)-th problem can be represented by three integers \(k_i\), \(b_i\), and \(a_i\). If you solve it at time \(t\) minutes, you get \(\max(b_i - k_i \cdot t,a_i)\) points.Your task is to choose an order to solve all these \(n\) problems to get the maximum possible score. You can assume the contest is long enough to solve all problems.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of problems.The \(n\) lines follow, the \(i\)-th of them contains three integers \(k_i\), \(b_i\), \(a_i\) (\(1\le k_i,b_i,a_i\le 10^9\); \(a_i < b_i\)), denoting that you get the score of \(\max(b_i - k_i \cdot t,a_i)\) if you solve the \(i\)-th task at time \(t\) minutes.It's guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\).
|
For each test case, print a line containing a single integer β the maximum score you can get.
|
In the second test case, the points for all problems at each minute are listed below. Time\(1\)\(2\)\(3\)\(4\)\(5\)\(6\)Problem \(1\)\(7\)\(6\)\(5\)\(\color{red}{4}\)\(3\)\(2\)Problem \(2\)\(\color{red}{20}\)\(11\)\(4\)\(4\)\(4\)\(4\)Problem \(3\)\(12\)\(10\)\(\color{red}{8}\)\(6\)\(4\)\(3\)Problem \(4\)\(9\)\(5\)\(1\)\(1\)\(\color{red}{1}\)\(1\)Problem \(5\)\(17\)\(\color{red}{15}\)\(13\)\(11\)\(9\)\(7\)Problem \(6\)\(5\)\(5\)\(5\)\(5\)\(5\)\(\color{red}{5}\) The points displayed in red denote one of the optimal orders with the score \(53\).
|
Input: 4410000 1000000000 200610000 1000000000 99992 999991010 10101000000000 1000000000 99999999961 8 19 29 42 14 34 13 12 19 510 12 584 10 14 19 81 14 34 15 62 9 61 11 102 19 124 19 14105 12 75 39 122 39 113 23 155 30 113 17 135 29 143 17 113 36 183 9 8 | Output: 3999961003 53 78 180
|
Master
| 4 | 736 | 581 | 93 | 17 |
2,096 |
D
|
2096D
|
D. Wonderful Lightbulbs
| 2,000 |
combinatorics; constructive algorithms; math
|
You are the proud owner of an infinitely large grid of lightbulbs, represented by a Cartesian coordinate system. Initially, all of the lightbulbs are turned off, except for one lightbulb, where you buried your proudest treasure.In order to hide your treasure's position, you perform the following operation an arbitrary number of times (possibly zero): Choose two integer numbers \(x\) and \(y\), and switch the state of the \(4\) lightbulbs at \((x, y)\), \((x, y + 1)\), \((x + 1, y - 1)\), and \((x + 1, y)\). In other words, for each lightbulb, turn it on if it was off, and turn it off if it was on. Note that there are no constraints on \(x\) and \(y\). In the end, there are \(n\) lightbulbs turned on at coordinates \((x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)\). Unfortunately, you have already forgotten where you buried your treasure, so now you have to figure out one possible position of the treasure. Good luck!
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of lightbulbs that are on.The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(\color{red}{-10^8} \le x_i, y_i \le \color{red}{10^8}\)) β the coordinates of the \(i\)-th lightbulb. It is guaranteed that all coordinates are distinct.Additional constraint: There exists at least one position \((s, t)\) (\(\color{red}{-10^9} \le s, t \le \color{red}{10^9}\)), such that if the lightbulb at position \((s, t)\) is initially turned on, then after performing an arbitrary number of operations (possibly zero), we will get the given configuration of lightbulbs.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output two integers \(s\) and \(t\) (\(-10^9 \le s, t \le 10^9\)) β one possible position of the buried treasure. If there are multiple solutions, print any of them.For this problem, hacks are disabled.
|
For the first test case, one possible scenario is that you hid your treasure at position \((2, 3)\). Then, you did not perform any operations.In the end, only the lightbulb at \((2, 3)\) is turned on.For the second test case, one possible scenario is that you hid your treasure at position \((-2, -2)\). Then, you performed \(1\) operation with \(x = -2\), \(y = -2\).The operation switches the state of the \(4\) lightbulbs at \((-2, -2)\), \((-2, -1)\), \((-1, -3)\), and \((-1, -2)\).In the end, the lightbulbs at \((-2, -1)\), \((-1, -2)\), and \((-1, -3)\) are turned on.
|
Input: 412 33-2 -1-1 -2-1 -377 266 276 287 278 268 277 281170 969 869 073 570 -170 571 770 473 471 372 3 | Output: 2 3 -2 -2 7 27 72 7
|
Hard
| 3 | 925 | 947 | 222 | 20 |
68 |
A
|
68A
|
A. Irrational problem
| 1,100 |
implementation; number theory
|
Little Petya was given this problem for homework:You are given function (here represents the operation of taking the remainder). His task is to count the number of integers x in range [a;b] with property f(x) = x.It is a pity that Petya forgot the order in which the remainders should be taken and wrote down only 4 numbers. Each of 24 possible orders of taking the remainder has equal probability of being chosen. For example, if Petya has numbers 1, 2, 3, 4 then he can take remainders in that order or first take remainder modulo 4, then modulo 2, 3, 1. There also are 22 other permutations of these numbers that represent orders in which remainder can be taken. In this problem 4 numbers wrote down by Petya will be pairwise distinct.Now it is impossible for Petya to complete the task given by teacher but just for fun he decided to find the number of integers with property that probability that f(x) = x is not less than 31.4159265352718281828459045%. In other words, Petya will pick up the number x if there exist at least 7 permutations of numbers p1, p2, p3, p4, for which f(x) = x.
|
First line of the input will contain 6 integers, separated by spaces: p1, p2, p3, p4, a, b (1 β€ p1, p2, p3, p4 β€ 1000, 0 β€ a β€ b β€ 31415). It is guaranteed that numbers p1, p2, p3, p4 will be pairwise distinct.
|
Output the number of integers in the given range that have the given property.
|
Input: 2 7 1 8 2 8 | Output: 0
|
Easy
| 2 | 1,092 | 210 | 78 | 0 |
|
1,744 |
D
|
1744D
|
D. Divisibility by 2^n
| 1,200 |
greedy; math; sortings
|
You are given an array of positive integers \(a_1, a_2, \ldots, a_n\).Make the product of all the numbers in the array (that is, \(a_1 \cdot a_2 \cdot \ldots \cdot a_n\)) divisible by \(2^n\).You can perform the following operation as many times as you like: select an arbitrary index \(i\) (\(1 \leq i \leq n\)) and replace the value \(a_i\) with \(a_i=a_i \cdot i\). You cannot apply the operation repeatedly to a single index. In other words, all selected values of \(i\) must be different.Find the smallest number of operations you need to perform to make the product of all the elements in the array divisible by \(2^n\). Note that such a set of operations does not always exist.
|
The first line of the input contains a single integer \(t\) \((1 \leq t \leq 10^4\)) β the number test cases.Then the descriptions of the input data sets follow.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of array \(a\).The second line of each test case contains exactly \(n\) integers: \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) values over all test cases in a test does not exceed \(2 \cdot 10^5\).
|
For each test case, print the least number of operations to make the product of all numbers in the array divisible by \(2^n\). If the answer does not exist, print -1.
|
In the first test case, the product of all elements is initially \(2\), so no operations needed.In the second test case, the product of elements initially equals \(6\). We can apply the operation for \(i = 2\), and then \(a_2\) becomes \(2\cdot2=4\), and the product of numbers becomes \(3\cdot4=12\), and this product of numbers is divided by \(2^n=2^2=4\). In the fourth test case, even if we apply all possible operations, we still cannot make the product of numbers divisible by \(2^n\) β it will be \((13\cdot1)\cdot(17\cdot2)\cdot(1\cdot3)\cdot(1\cdot4)=5304\), which does not divide by \(2^n=2^4=16\).In the fifth test case, we can apply operations for \(i = 2\) and \(i = 4\).
|
Input: 61223 2310 6 11413 17 1 151 1 12 1 1620 7 14 18 3 5 | Output: 0 1 1 -1 2 1
|
Easy
| 3 | 684 | 516 | 166 | 17 |
1,614 |
B
|
1614B
|
B. Divan and a New Project
| 1,000 |
constructive algorithms; sortings
|
The company ""Divan's Sofas"" is planning to build \(n + 1\) different buildings on a coordinate line so that: the coordinate of each building is an integer number; no two buildings stand at the same point. Let \(x_i\) be the coordinate of the \(i\)-th building. To get from the building \(i\) to the building \(j\), Divan spends \(|x_i - x_j|\) minutes, where \(|y|\) is the absolute value of \(y\).All buildings that Divan is going to build can be numbered from \(0\) to \(n\). The businessman will live in the building \(0\), the new headquarters of ""Divan's Sofas"". In the first ten years after construction Divan will visit the \(i\)-th building \(a_i\) times, each time spending \(2 \cdot |x_0-x_i|\) minutes for walking.Divan asks you to choose the coordinates for all \(n + 1\) buildings so that over the next ten years the businessman will spend as little time for walking as possible.
|
Each test contains several test cases. The first line contains one integer number \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.The first line of each case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of buildings that ""Divan's Sofas"" is going to build, apart from the headquarters.The second line contains the sequence \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^6\)), where \(a_i\) is the number of visits to the \(i\)-th building.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, on the first line print the number \(T\) β the minimum time Divan will spend walking. On the second line print the sequence \(x_0, x_1, \ldots, x_n\) of \(n + 1\) integers, where \(x_i\) (\(-10^6 \le x_i \le 10^6\)) is the selected coordinate of the \(i\)-th building. It can be shown that an optimal answer exists with coordinates not exceeding \(10^6\).If there are multiple answers, print any of them.
|
Let's look at the first example.Divan will visit the first building \(a_1 = 1\) times, the second \(a_2 = 2\) times and the third \(a_3 = 3\) times. Then one of the optimal solution will be as follows: the headquarters is located in \(x_0 = 2\); \(x_1 = 4\): Divan will spend \(2 \cdot |x_0-x_1| \cdot a_1 = 2 \cdot |2-4| \cdot 1 = 4\) minutes walking to the first building; \(x_2 = 1\): Divan will spend \(2 \cdot |x_0-x_2| \cdot a_2 = 2 \cdot |2-1| \cdot 2 = 4\) minutes walking to the second building; \(x_3 = 3\): Divan will spend \(2 \cdot |x_0-x_3| \cdot a_3 = 2 \cdot |2-3| \cdot 3 = 6\) minutes walking to the third building. In total, Divan will spend \(4 + 4 + 6 = 14\) minutes. It can be shown that it is impossible to arrange buildings so that the businessman spends less time.Among others, \(x = [1, 3, 2, 0]\), \(x = [-5, -3, -6, -4]\) are also correct answers for the first example.
|
Input: 4 3 1 2 3 5 3 8 10 6 1 5 1 1 1 1 1 1 0 | Output: 14 2 4 1 3 78 1 -1 0 2 3 4 18 3 6 1 5 2 4 0 1 2
|
Beginner
| 2 | 896 | 564 | 424 | 16 |
1,543 |
A
|
1543A
|
A. Exciting Bets
| 900 |
greedy; math; number theory
|
Welcome to Rockport City!It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet \(a\) dollars and Ronnie has bet \(b\) dollars. But the fans seem to be disappointed. The excitement of the fans is given by \(gcd(a,b)\), where \(gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\). To make the race more exciting, you can perform two types of operations: Increase both \(a\) and \(b\) by \(1\). Decrease both \(a\) and \(b\) by \(1\). This operation can only be performed if both \(a\) and \(b\) are greater than \(0\). In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.Note that \(gcd(x,0)=x\) for any \(x \ge 0\).
|
The first line of input contains a single integer \(t\) (\(1\leq t\leq 5\cdot 10^3\)) β the number of test cases.The first and the only line of each test case contains two integers \(a\) and \(b\) (\(0\leq a, b\leq 10^{18}\)).
|
For each test case, print a single line containing two integers. If the fans can get infinite excitement, print 0 0.Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.
|
For the first test case, you can apply the first operation \(1\) time to get \(a=9\) and \(b=6\). It can be shown that \(3\) is the maximum excitement possible.For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to \(1\). Since the initial excitement is also \(1\), you don't need to apply any operation.For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.For the fourth test case, you can apply the second operation \(3\) times to get \(a=0\) and \(b=6\). Since, \(gcd(0,6)=6\), the fans will get an excitement of \(6\).
|
Input: 4 8 5 1 2 4 4 3 9 | Output: 3 1 1 0 0 0 6 3
|
Beginner
| 3 | 867 | 226 | 289 | 15 |
391 |
B
|
391B
|
B. Word Folding
| 0 |
brute force
|
You will receive 5 points for solving this problem.Manao has invented a new operation on strings that is called folding. Each fold happens between a pair of consecutive letters and places the second part of the string above first part, running in the opposite direction and aligned to the position of the fold. Using this operation, Manao converts the string into a structure that has one more level than there were fold operations performed. See the following examples for clarity.We will denote the positions of folds with '|' characters. For example, the word ""ABRACADABRA"" written as ""AB|RACA|DAB|RA"" indicates that it has been folded three times: first, between the leftmost pair of 'B' and 'R' letters; second, between 'A' and 'D'; and third, between the rightmost pair of 'B' and 'R' letters. Here are several examples of folded strings:""ABCDEF|GHIJK"" | ""A|BCDEFGHIJK"" | ""AB|RACA|DAB|RA"" | ""X|XXXXX|X|X|XXXXXX"" | | | XXXXXX KJIHG | KJIHGFEDCB | AR | X ABCDEF | A | DAB | X | | ACAR | XXXXX | | AB | XOne last example for ""ABCD|EFGH|IJ|K"": KIJHGFEABCDManao noticed that each folded string can be viewed as several piles of letters. For instance, in the previous example, there are four piles, which can be read as ""AHI"", ""BGJK"", ""CF"", and ""DE"" from bottom to top. Manao wonders what is the highest pile of identical letters he can build using fold operations on a given word. Note that the pile should not contain gaps and should start at the bottom level. For example, in the rightmost of the four examples above, none of the piles would be considered valid since each of them has gaps, starts above the bottom level, or both.
|
The input will consist of one line containing a single string of n characters with 1 β€ n β€ 1000 and no spaces. All characters of the string will be uppercase letters.This problem doesn't have subproblems. You will get 5 points for the correct submission.
|
Print a single integer β the size of the largest pile composed of identical characters that can be seen in a valid result of folding operations on the given string.
|
Consider the first example. Manao can create a pile of three 'A's using the folding ""AB|RACAD|ABRA"", which results in the following structure: ABRADACAR ABIn the second example, Manao can create a pile of three 'B's using the following folding: ""AB|BB|CBDB"". CBDBBBABAnother way for Manao to create a pile of three 'B's with ""ABBBCBDB"" is the following folding: ""AB|B|BCBDB"". BCBDB BABIn the third example, there are no folds performed and the string is just written in one line.
|
Input: ABRACADABRA | Output: 3
|
Beginner
| 1 | 1,655 | 254 | 164 | 3 |
940 |
F
|
940F
|
F. Machine Learning
| 2,600 |
brute force; data structures
|
You come home and fell some unpleasant smell. Where is it coming from?You are given an array a. You have to answer the following queries: You are given two integers l and r. Let ci be the number of occurrences of i in al: r, where al: r is the subarray of a from l-th element to r-th inclusive. Find the Mex of {c0, c1, ..., c109} You are given two integers p to x. Change ap to x. The Mex of a multiset of numbers is the smallest non-negative integer not in the set.Note that in this problem all elements of a are positive, which means that c0 = 0 and 0 is never the answer for the query of the second type.
|
The first line of input contains two integers n and q (1 β€ n, q β€ 100 000) β the length of the array and the number of queries respectively.The second line of input contains n integers β a1, a2, ..., an (1 β€ ai β€ 109).Each of the next q lines describes a single query.The first type of query is described by three integers ti = 1, li, ri, where 1 β€ li β€ ri β€ n β the bounds of the subarray.The second type of query is described by three integers ti = 2, pi, xi, where 1 β€ pi β€ n is the index of the element, which must be changed and 1 β€ xi β€ 109 is the new value.
|
For each query of the first type output a single integer β the Mex of {c0, c1, ..., c109}.
|
The subarray of the first query consists of the single element β 1. The subarray of the second query consists of four 2s, one 3 and two 1s.The subarray of the fourth query consists of three 1s, three 2s and one 3.
|
Input: 10 41 2 3 1 1 2 2 2 9 91 1 11 2 82 7 11 2 8 | Output: 232
|
Expert
| 2 | 608 | 564 | 90 | 9 |
1,900 |
A
|
1900A
|
A. Cover in Water
| 800 |
constructive algorithms; greedy; implementation; strings
|
Filip has a row of cells, some of which are blocked, and some are empty. He wants all empty cells to have water in them. He has two actions at his disposal: \(1\) β place water in an empty cell. \(2\) β remove water from a cell and place it in any other empty cell. If at some moment cell \(i\) (\(2 \le i \le n-1\)) is empty and both cells \(i-1\) and \(i+1\) contains water, then it becomes filled with water.Find the minimum number of times he needs to perform action \(1\) in order to fill all empty cells with water. Note that you don't need to minimize the use of action \(2\). Note that blocked cells neither contain water nor can Filip place water in them.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of cells. The next line contains a string \(s\) of length \(n\). The \(i\)-th character of \(s\) is '.' if the cell \(i\) is empty and '#' if cell \(i\) is blocked.
|
For each test case, output a single number β the minimal amount of actions \(1\) needed to fill all empty cells with water.
|
Test Case 1In the first test case, Filip can put water in cells \(1\) and \(3\). As cell \(2\) is between \(2\) cells with water, it gets filled with water too.Test Case 2In the second case, he can put water sources in cells \(3\) and \(5\). That results in cell \(4\) getting filled with water. Then he will remove water from cell \(5\) and place it into cell \(6\). As cell \(5\)'s neighbors, cell \(4\) and cell \(6\), have water in them, cell \(5\) also gets filled with water. You can see the illustration of this case below. Operations in the second test case. White cells are empty, grey ones are blocked, and blue ones are water. Test Case 3In the third case, he can put water in all the empty cells. That requires \(5\) actions of type \(1\).Test Case 4In the fourth case, there are no empty cells. Therefore, he does not have to put any water in them.Test Case 5In the fifth test case, there exists a sequence of actions that requires only \(2\) type \(1\) actions.
|
Input: 53...7##....#7..#.#..4####10#...#..#.# | Output: 2 2 5 0 2
|
Beginner
| 4 | 664 | 424 | 123 | 19 |
238 |
E
|
238E
|
E. Meeting Her
| 2,600 |
dp; graphs; shortest paths
|
Urpal lives in a big city. He has planned to meet his lover tonight. The city has n junctions numbered from 1 to n. The junctions are connected by m directed streets, all the roads have equal length. Urpal lives in junction a and the date is planned in a restaurant in junction b. He wants to use public transportation to get to junction b. There are k bus transportation companies. At the beginning of every second, a bus from the i-th company chooses a random shortest path between junction si and junction ti and passes through it. There might be no path from si to ti. In that case no bus will leave from si to ti. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path. Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case.At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (si, ti) for each company.Note that Urpal doesn't know buses velocity.
|
The first line of the input contains four integers n, m, a, b (2 β€ n β€ 100; 0 β€ m β€ nΒ·(n - 1); 1 β€ a, b β€ n; a β b). The next m lines contain two integers each ui and vi (1 β€ ui, vi β€ n; ui β vi) describing a directed road from junction ui to junction vi. All roads in the input will be distinct. The next line contains an integer k (0 β€ k β€ 100). There will be k lines after this, each containing two integers si and ti (1 β€ si, ti β€ n; si β ti) saying there is a bus route starting at si and ending at ti. Please note that there might be no path from si to ti, this case is described in the problem statement.
|
In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1.
|
Input: 7 8 1 71 21 32 43 44 64 56 75 732 71 45 7 | Output: 2
|
Expert
| 3 | 1,294 | 611 | 184 | 2 |
|
982 |
E
|
982E
|
E. Billiard
| 2,600 |
geometry; number theory
|
Consider a billiard table of rectangular size \(n \times m\) with four pockets. Let's introduce a coordinate system with the origin at the lower left corner (see the picture). There is one ball at the point \((x, y)\) currently. Max comes to the table and strikes the ball. The ball starts moving along a line that is parallel to one of the axes or that makes a \(45^{\circ}\) angle with them. We will assume that: the angles between the directions of the ball before and after a collision with a side are equal, the ball moves indefinitely long, it only stops when it falls into a pocket, the ball can be considered as a point, it falls into a pocket if and only if its coordinates coincide with one of the pockets, initially the ball is not in a pocket. Note that the ball can move along some side, in this case the ball will just fall into the pocket at the end of the side.Your task is to determine whether the ball will fall into a pocket eventually, and if yes, which of the four pockets it will be.
|
The only line contains \(6\) integers \(n\), \(m\), \(x\), \(y\), \(v_x\), \(v_y\) (\(1 \leq n, m \leq 10^9\), \(0 \leq x \leq n\); \(0 \leq y \leq m\); \(-1 \leq v_x, v_y \leq 1\); \((v_x, v_y) \neq (0, 0)\)) β the width of the table, the length of the table, the \(x\)-coordinate of the initial position of the ball, the \(y\)-coordinate of the initial position of the ball, the \(x\)-component of its initial speed and the \(y\)-component of its initial speed, respectively. It is guaranteed that the ball is not initially in a pocket.
|
Print the coordinates of the pocket the ball will fall into, or \(-1\) if the ball will move indefinitely.
|
The first sample: The second sample: In the third sample the ball will never change its \(y\) coordinate, so the ball will never fall into a pocket.
|
Input: 4 3 2 2 -1 1 | Output: 0 0
|
Expert
| 2 | 1,005 | 538 | 106 | 9 |
1,991 |
I
|
1991I
|
I. Grid Game
| 3,500 |
constructive algorithms; games; graph matchings; greedy; interactive
|
This is an interactive problem.You are given a grid with \(n\) rows and \(m\) columns. You need to fill each cell with a unique integer from \(1\) to \(n \cdot m\).After filling the grid, you will play a game on this grid against the interactor. Players take turns selecting one of the previously unselected cells from the grid, with the interactor going first.On the first turn, the interactor can choose any cell from the grid. After that, any chosen cell must be orthogonally adjacent to at least one previously selected cell. Two cells are considered orthogonally adjacent if they share an edge. The game continues until all cells have been selected.Your goal is to let the sum of numbers in the cells selected by you be strictly less than the sum of numbers in the cells selected by the interactor.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of test cases follows.The only line of each test case contains two integers \(n\) and \(m\) (\(4 \le n, m \le 10\)) β the number of rows and columns in the grid.
|
Note that this is an example game and does not necessarily represent the optimal strategy for both players.First, we fill a \(4 \times 4\) grid with unique integers from \(1\) to \(16\) in the following way: \(2\)\(3\)\(4\)\(10\)\(12\)\(6\)\(11\)\(15\)\(5\)\(13\)\(16\)\(8\)\(9\)\(7\)\(1\)\(14\) Next, the game begins. The interactor first selects \((3, 4)\), which is the number \(8\). For this selection, the interactor could choose any number. From the next selection onwards, each chosen number has to be adjacent to any previously selected number. We select \((2, 4)\), which is the number \(15\), adjacent to \((3, 4)\). The interactor selects \((4, 4)\), which is the number \(14\), adjacent to \((3, 4)\). We select \((4, 3)\), which is the number \(1\), adjacent to \((4, 4)\). \(\ldots\) This is continued until all numbers are selected. In the end, the numbers we selected were \([15, 1, 16, 5, 4, 2, 11, 13]\), and the numbers selected by the interactor were \([8, 14, 7, 9, 10, 3, 6, 12]\). The sum of the numbers we selected is \(67\), which is less than the sum of the numbers selected by the interactor \(69\). Therefore, we have won this game.
|
Input: 1 4 4 3 4 4 4 4 2 4 1 1 4 1 2 2 2 2 1 | Output: 2 3 4 10 12 6 11 15 5 13 16 8 9 7 1 14 2 4 4 3 3 3 3 1 1 3 1 1 2 3 3 2
|
Master
| 5 | 803 | 314 | 0 | 19 |
|
1,987 |
G2
|
1987G2
|
G2. Spinning Round (Hard Version)
| 3,500 |
divide and conquer; dp; trees
|
This is the hard version of the problem. The only difference between the two versions are the allowed characters in \(s\). You can make hacks only if both versions of the problem are solved.You are given a permutation \(p\) of length \(n\). You are also given a string \(s\) of length \(n\), where each character is either L, R, or ?.For each \(i\) from \(1\) to \(n\): Define \(l_i\) as the largest index \(j < i\) such that \(p_j > p_i\). If there is no such index, \(l_i := i\). Define \(r_i\) as the smallest index \(j > i\) such that \(p_j > p_i\). If there is no such index, \(r_i := i\). Initially, you have an undirected graph with \(n\) vertices (numbered from \(1\) to \(n\)) and no edges. Then, for each \(i\) from \(1\) to \(n\), add one edge to the graph: If \(s_i =\) L, add the edge \((i, l_i)\) to the graph. If \(s_i =\) R, add the edge \((i, r_i)\) to the graph. If \(s_i =\) ?, either add the edge \((i, l_i)\) or the edge \((i, r_i)\) to the graph at your choice. Find the maximum possible diameter over all connected\(^{\text{β}}\) graphs that you can form. Output \(-1\) if it is not possible to form any connected graphs.\(^{\text{β}}\) Let \(d(s, t)\) denote the smallest number of edges on any path between \(s\) and \(t\).The diameter of the graph is defined as the maximum value of \(d(s, t)\) over all pairs of vertices \(s\) and \(t\).
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 4 \cdot 10^5\)) β the length of the permutation \(p\). The second line of each test case contains \(n\) integers \(p_1,p_2,\ldots, p_n\) (\(1 \le p_i \le n\)) β the elements of \(p\), which are guaranteed to form a permutation.The third line of each test case contains a string \(s\) of length \(n\). It is guaranteed that it consists only of the characters L, R, and ?.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(4 \cdot 10^5\).
|
For each test case, output the maximum possible diameter over all connected graphs that you form, or \(-1\) if it is not possible to form any connected graphs.
|
In the first test case, there are two connected graphs (the labels are indices): The graph on the left has a diameter of \(2\), while the graph on the right has a diameter of \(3\), so the answer is \(3\).In the second test case, there are no connected graphs, so the answer is \(-1\).
|
Input: 852 1 4 3 5R?RL?21 2LR33 1 2L?R75 3 1 6 4 2 7?R?R?R?55 2 1 3 4?????66 2 3 4 5 1?LLRLL81 7 5 6 2 8 4 3?R??????126 10 7 1 8 5 12 2 11 3 4 9???????????? | Output: 3 -1 -1 4 4 3 5 8
|
Master
| 3 | 1,364 | 739 | 159 | 19 |
1,730 |
A
|
1730A
|
A. Planets
| 800 |
data structures; greedy; sortings
|
One day, Vogons wanted to build a new hyperspace highway through a distant system with \(n\) planets. The \(i\)-th planet is on the orbit \(a_i\), there could be multiple planets on the same orbit. It's a pity that all the planets are on the way and need to be destructed.Vogons have two machines to do that. The first machine in one operation can destroy any planet at cost of \(1\) Triganic Pu. The second machine in one operation can destroy all planets on a single orbit in this system at the cost of \(c\) Triganic Pus. Vogons can use each machine as many times as they want.Vogons are very greedy, so they want to destroy all planets with minimum amount of money spent. Can you help them to know the minimum cost of this project?
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then the test cases follow.Each test case consists of two lines.The first line contains two integers \(n\) and \(c\) (\(1 \le n, c \le 100\)) β the number of planets and the cost of the second machine usage.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 100\)), where \(a_i\) is the orbit of the \(i\)-th planet.
|
For each test case print a single integer β the minimum cost of destroying all planets.
|
In the first test case, the cost of using both machines is the same, so you can always use the second one and destroy all planets in orbit \(1\), all planets in orbit \(2\), all planets in orbit \(4\), all planets in orbit \(5\).In the second test case, it is advantageous to use the second machine for \(2\) Triganic Pus to destroy all the planets in orbit \(2\), then destroy the remaining two planets using the first machine.In the third test case, you can use the first machine twice or the second machine once.In the fourth test case, it is advantageous to use the first machine twice.
|
Input: 410 12 1 4 5 2 4 5 5 1 25 23 2 1 2 22 21 12 21 2 | Output: 4 4 2 2
|
Beginner
| 3 | 735 | 444 | 87 | 17 |
1,064 |
A
|
1064A
|
A. Make a triangle!
| 800 |
brute force; geometry; math
|
Masha has three sticks of length \(a\), \(b\) and \(c\) centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks.What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices.
|
The only line contains tree integers \(a\), \(b\) and \(c\) (\(1 \leq a, b, c \leq 100\)) β the lengths of sticks Masha possesses.
|
Print a single integer β the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks.
|
In the first example, Masha can make a triangle from the sticks without increasing the length of any of them.In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length \(2\) centimeter stick by one and after that form a triangle with sides \(3\), \(3\) and \(5\) centimeters.In the third example, Masha can take \(33\) minutes to increase one of the \(10\) centimeters sticks by \(33\) centimeters, and after that take \(48\) minutes to increase another \(10\) centimeters stick by \(48\) centimeters. This way she can form a triangle with lengths \(43\), \(58\) and \(100\) centimeters in \(81\) minutes. One can show that it is impossible to get a valid triangle faster.
|
Input: 3 4 5 | Output: 0
|
Beginner
| 3 | 485 | 130 | 155 | 10 |
1,611 |
A
|
1611A
|
A. Make Even
| 800 |
constructive algorithms; math
|
Polycarp has an integer \(n\) that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times: Reverse the prefix of length \(l\) (in other words, \(l\) leftmost digits) of \(n\). So, the leftmost digit is swapped with the \(l\)-th digit from the left, the second digit from the left swapped with (\(l-1\))-th left, etc. For example, if \(n=123456789\) and \(l=5\), then the new value of \(n\) will be \(543216789\).Note that for different operations, the values of \(l\) can be different. The number \(l\) can be equal to the length of the number \(n\) β in this case, the whole number \(n\) is reversed.Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible.Help Polycarp. Determine the minimum number of operations he needs to perform with the number \(n\) to make it even or determine that this is impossible.You need to answer \(t\) independent test cases.
|
The first line contains the number \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each of the following \(t\) lines contains one integer \(n\) (\(1 \le n < 10^9\)). It is guaranteed that the given number doesn't contain the digit 0.
|
Print \(t\) lines. On each line print one integer β the answer to the corresponding test case. If it is impossible to make an even number, print -1.
|
In the first test case, \(n=3876\), which is already an even number. Polycarp doesn't need to do anything, so the answer is \(0\).In the second test case, \(n=387\). Polycarp needs to do \(2\) operations: Select \(l=2\) and reverse the prefix \(\underline{38}7\). The number \(n\) becomes \(837\). This number is odd. Select \(l=3\) and reverse the prefix \(\underline{837}\). The number \(n\) becomes \(738\). This number is even.It can be shown that \(2\) is the minimum possible number of operations that Polycarp needs to do with his number to make it even.In the third test case, \(n=4489\). Polycarp can reverse the whole number (choose a prefix of length \(l=4\)). It will become \(9844\) and this is an even number.In the fourth test case, \(n=3\). No matter how hard Polycarp tried, he would not be able to make an even number.
|
Input: 4 3876 387 4489 3 | Output: 0 2 1 -1
|
Beginner
| 2 | 1,021 | 241 | 148 | 16 |
1,707 |
A
|
1707A
|
A. Doremy's IQ
| 1,600 |
binary search; constructive algorithms; greedy; implementation
|
Doremy is asked to test \(n\) contests. Contest \(i\) can only be tested on day \(i\). The difficulty of contest \(i\) is \(a_i\). Initially, Doremy's IQ is \(q\). On day \(i\) Doremy will choose whether to test contest \(i\) or not. She can only test a contest if her current IQ is strictly greater than \(0\).If Doremy chooses to test contest \(i\) on day \(i\), the following happens: if \(a_i>q\), Doremy will feel she is not wise enough, so \(q\) decreases by \(1\); otherwise, nothing changes. If she chooses not to test a contest, nothing changes.Doremy wants to test as many contests as possible. Please give Doremy a solution.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases. The description of the test cases follows.The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 10^5\), \(1 \le q \le 10^9\)) β the number of contests and Doremy's IQ in the beginning.The second line contains \(n\) integers \(a_1,a_2,\cdots,a_n\) (\(1 \le a_i \le 10^9\)) β the difficulty of each contest.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, you need to output a binary string \(s\), where \(s_i=1\) if Doremy should choose to test contest \(i\), and \(s_i=0\) otherwise. The number of ones in the string should be maximum possible, and she should never test a contest when her IQ is zero or less.If there are multiple solutions, you may output any.
|
In the first test case, Doremy tests the only contest. Her IQ doesn't decrease.In the second test case, Doremy tests both contests. Her IQ decreases by \(1\) after testing contest \(2\).In the third test case, Doremy tests contest \(1\) and \(2\). Her IQ decreases to \(0\) after testing contest \(2\), so she can't test contest \(3\).
|
Input: 51 112 11 23 11 2 14 21 4 3 15 25 1 2 4 3 | Output: 1 11 110 1110 01111
|
Medium
| 4 | 635 | 542 | 327 | 17 |
1,843 |
D
|
1843D
|
D. Apple Tree
| 1,200 |
combinatorics; dfs and similar; dp; math; trees
|
Timofey has an apple tree growing in his garden; it is a rooted tree of \(n\) vertices with the root in vertex \(1\) (the vertices are numbered from \(1\) to \(n\)). A tree is a connected graph without loops and multiple edges.This tree is very unusual β it grows with its root upwards. However, it's quite normal for programmer's trees.The apple tree is quite young, so only two apples will grow on it. Apples will grow in certain vertices (these vertices may be the same). After the apples grow, Timofey starts shaking the apple tree until the apples fall. Each time Timofey shakes the apple tree, the following happens to each of the apples:Let the apple now be at vertex \(u\). If a vertex \(u\) has a child, the apple moves to it (if there are several such vertices, the apple can move to any of them). Otherwise, the apple falls from the tree. It can be shown that after a finite time, both apples will fall from the tree.Timofey has \(q\) assumptions in which vertices apples can grow. He assumes that apples can grow in vertices \(x\) and \(y\), and wants to know the number of pairs of vertices (\(a\), \(b\)) from which apples can fall from the tree, where \(a\) β the vertex from which an apple from vertex \(x\) will fall, \(b\) β the vertex from which an apple from vertex \(y\) will fall. Help him do this.
|
The first line contains integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the number of vertices in the tree.Then there are \(n - 1\) lines describing the tree. In line \(i\) there are two integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\), \(u_i \ne v_i\)) β edge in tree.The next line contains a single integer \(q\) (\(1 \leq q \leq 2 \cdot 10^5\)) β the number of Timofey's assumptions.Each of the next \(q\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \leq x_i, y_i \leq n\)) β the supposed vertices on which the apples will grow for the assumption \(i\).It is guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\). Similarly, It is guaranteed that the sum of \(q\) does not exceed \(2 \cdot 10^5\).
|
For each Timofey's assumption output the number of ordered pairs of vertices from which apples can fall from the tree if the assumption is true on a separate line.
|
In the first example: For the first assumption, there are two possible pairs of vertices from which apples can fall from the tree: \((4, 4), (5, 4)\). For the second assumption there are also two pairs: \((5, 4), (5, 5)\). For the third assumption there is only one pair: \((4, 4)\). For the fourth assumption, there are \(4\) pairs: \((4, 4), (4, 5), (5, 4), (5, 5)\). Tree from the first example. For the second example, there are \(4\) of possible pairs of vertices from which apples can fall: \((2, 3), (2, 2), (3, 2), (3, 3)\). For the second assumption, there is only one possible pair: \((2, 3)\). For the third assumption, there are two pairs: \((3, 2), (3, 3)\).
|
Input: 251 23 45 33 243 45 14 41 331 21 331 12 33 1 | Output: 2 2 1 4 4 1 2
|
Easy
| 5 | 1,320 | 838 | 163 | 18 |
1,002 |
D2
|
1002D2
|
D2. Oracle for f(x) = b * x + (1 - b) * (1 - x) mod 2
| 1,300 |
*special
|
Implement a quantum oracle on N qubits which implements the following function: Here (a vector of N integers, each of which can be 0 or 1), and is a vector of N 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 N qubits x in arbitrary state (input register), 1 β€ N β€ 8, a qubit y in arbitrary state (output qubit), an array of N integers b, representing the vector . Each element of b will be 0 or 1.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, b : Int[]) : () { body { // your code here } }}
|
Easy
| 1 | 847 | 0 | 0 | 10 |
||||
1,732 |
C2
|
1732C2
|
C2. Sheikh (Hard Version)
| 2,100 |
binary search; bitmasks; brute force; greedy; implementation; two pointers
|
This is the hard version of the problem. The only difference is that in this version \(q = n\).You are given an array of integers \(a_1, a_2, \ldots, a_n\).The cost of a subsegment of the array \([l, r]\), \(1 \leq l \leq r \leq n\), is the value \(f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r)\), where \(\operatorname{sum}(l, r) = a_l + a_{l+1} + \ldots + a_r\), and \(\operatorname{xor}(l, r) = a_l \oplus a_{l+1} \oplus \ldots \oplus a_r\) (\(\oplus\) stands for bitwise XOR).You will have \(q\) queries. Each query is given by a pair of numbers \(L_i\), \(R_i\), where \(1 \leq L_i \leq R_i \leq n\). You need to find the subsegment \([l, r]\), \(L_i \leq l \leq r \leq R_i\), with maximum value \(f(l, r)\). If there are several answers, then among them you need to find a subsegment with the minimum length, that is, the minimum value of \(r - l + 1\).
|
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n \leq 10^5\), \(q = n\)) β the length of the array and the number of queries.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\)) β array elements.\(i\)-th of the next \(q\) lines of each test case contains two integers \(L_i\) and \(R_i\) (\(1 \leq L_i \leq R_i \leq n\)) β the boundaries in which we need to find the segment.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).It is guaranteed that \(L_1 = 1\) and \(R_1 = n\).
|
For each test case print \(q\) pairs of numbers \(L_i \leq l \leq r \leq R_i\) such that the value \(f(l, r)\) is maximum and among such the length \(r - l + 1\) is minimum. If there are several correct answers, print any of them.
|
In all test cases, the first query is considered.In the first test case, \(f(1, 1) = 0 - 0 = 0\).In the second test case, \(f(1, 1) = 5 - 5 = 0\), \(f(2, 2) = 10 - 10 = 0\). Note that \(f(1, 2) = (10 + 5) - (10 \oplus 5) = 0\), but we need to find a subsegment with the minimum length among the maximum values of \(f(l, r)\). So, only segments \([1, 1]\) and \([2, 2]\) are the correct answers.In the fourth test case, \(f(2, 3) = (12 + 8) - (12 \oplus 8) = 16\). There are two correct answers in the fifth test case, since \(f(2, 3) = f(3, 4)\) and their lengths are equal.
|
Input: 61 101 12 25 101 22 23 30 2 41 31 22 34 40 12 8 31 41 32 42 35 521 32 32 32 101 51 41 32 53 57 70 1 0 1 0 1 01 73 62 51 44 72 62 7 | Output: 1 1 1 1 2 2 1 1 1 1 2 2 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 3 4 2 4 4 6 2 4 2 4 4 6 2 4 2 4
|
Hard
| 6 | 875 | 784 | 230 | 17 |
385 |
C
|
385C
|
C. Bear and Prime Numbers
| 1,700 |
binary search; brute force; data structures; dp; implementation; math; number theory
|
Recently, the bear started studying data structures and faced the following problem.You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: , where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).Help the bear cope with the problem.
|
The first line contains integer n (1 β€ n β€ 106). The second line contains n integers x1, x2, ..., xn (2 β€ xi β€ 107). The numbers are not necessarily distinct.The third line contains integer m (1 β€ m β€ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 β€ li β€ ri β€ 2Β·109) β the numbers that characterize the current query.
|
Print m integers β the answers to the queries on the order the queries appear in the input.
|
Consider the first sample. Overall, the first sample has 3 queries. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.
|
Input: 65 5 7 10 14 1532 113 124 4 | Output: 970
|
Medium
| 7 | 497 | 366 | 91 | 3 |
922 |
A
|
922A
|
A. Cloning Toys
| 1,300 |
implementation
|
Imp likes his plush toy a lot. Recently, he found a machine that can clone plush toys. Imp knows that if he applies the machine to an original toy, he additionally gets one more original toy and one copy, and if he applies the machine to a copied toy, he gets two additional copies.Initially, Imp has only one original toy. He wants to know if it is possible to use machine to get exactly x copied toys and y original toys? He can't throw toys away, and he can't apply the machine to a copy if he doesn't currently have any copies.
|
The only line contains two integers x and y (0 β€ x, y β€ 109) β the number of copies and the number of original toys Imp wants to get (including the initial one).
|
Print ""Yes"", if the desired configuration is possible, and ""No"" otherwise.You can print each letter in arbitrary case (upper or lower).
|
In the first example, Imp has to apply the machine twice to original toys and then twice to copies.
|
Input: 6 3 | Output: Yes
|
Easy
| 1 | 531 | 161 | 139 | 9 |
1,095 |
D
|
1095D
|
D. Circular Dance
| 1,600 |
implementation
|
There are \(n\) kids, numbered from \(1\) to \(n\), dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as \(p_1\), \(p_2\), ..., \(p_n\) (all these numbers are from \(1\) to \(n\) and are distinct, so \(p\) is a permutation). Let the next kid for a kid \(p_i\) be kid \(p_{i + 1}\) if \(i < n\) and \(p_1\) otherwise. After the dance, each kid remembered two kids: the next kid (let's call him \(x\)) and the next kid for \(x\). Each kid told you which kids he/she remembered: the kid \(i\) remembered kids \(a_{i, 1}\) and \(a_{i, 2}\). However, the order of \(a_{i, 1}\) and \(a_{i, 2}\) can differ from their order in the circle. Example: 5 kids in a circle, \(p=[3, 2, 4, 1, 5]\) (or any cyclic shift). The information kids remembered is: \(a_{1,1}=3\), \(a_{1,2}=5\); \(a_{2,1}=1\), \(a_{2,2}=4\); \(a_{3,1}=2\), \(a_{3,2}=4\); \(a_{4,1}=1\), \(a_{4,2}=5\); \(a_{5,1}=2\), \(a_{5,2}=3\). You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists.If you are Python programmer, consider using PyPy instead of Python when you submit your code.
|
The first line of the input contains one integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of the kids.The next \(n\) lines contain \(2\) integers each. The \(i\)-th line contains two integers \(a_{i, 1}\) and \(a_{i, 2}\) (\(1 \le a_{i, 1}, a_{i, 2} \le n, a_{i, 1} \ne a_{i, 2}\)) β the kids the \(i\)-th kid remembered, given in arbitrary order.
|
Print \(n\) integers \(p_1\), \(p_2\), ..., \(p_n\) β permutation of integers from \(1\) to \(n\), which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists.
|
Input: 5 3 5 1 4 2 4 1 5 2 3 | Output: 3 2 4 1 5
|
Medium
| 1 | 1,209 | 356 | 323 | 10 |
|
1,714 |
F
|
1714F
|
F. Build a Tree and That Is It
| 1,900 |
constructive algorithms; implementation; trees
|
A tree is a connected undirected graph without cycles. Note that in this problem, we are talking about not rooted trees.You are given four positive integers \(n, d_{12}, d_{23}\) and \(d_{31}\). Construct a tree such that: it contains \(n\) vertices numbered from \(1\) to \(n\), the distance (length of the shortest path) from vertex \(1\) to vertex \(2\) is \(d_{12}\), distance from vertex \(2\) to vertex \(3\) is \(d_{23}\), the distance from vertex \(3\) to vertex \(1\) is \(d_{31}\). Output any tree that satisfies all the requirements above, or determine that no such tree exists.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) βthe number of test cases in the test.This is followed by \(t\) test cases, each written on a separate line.Each test case consists of four positive integers \(n, d_{12}, d_{23}\) and \(d_{31}\) (\(3 \le n \le 2\cdot10^5; 1 \le d_{12}, d_{23}, d_{31} \le n-1\)).It is guaranteed that the sum of \(n\) values for all test cases does not exceed \(2\cdot10^5\).
|
For each test case, print YES if the suitable tree exists, and NO otherwise. If the answer is positive, print another \(n-1\) line each containing a description of an edge of the tree β a pair of positive integers \(x_i, y_i\), which means that the \(i\)th edge connects vertices \(x_i\) and \(y_i\). The edges and vertices of the edges can be printed in any order. If there are several suitable trees, output any of them.
|
Input: 95 1 2 15 2 2 25 2 2 35 2 2 45 3 2 34 2 1 14 3 1 14 1 2 37 1 4 1 | Output: YES 1 2 4 1 3 1 2 5 YES 4 3 2 5 1 5 5 3 NO YES 2 4 4 1 2 5 5 3 YES 5 4 4 1 2 5 3 5 YES 2 3 3 4 1 3 NO YES 4 3 1 2 2 4 NO
|
Hard
| 3 | 589 | 435 | 422 | 17 |
|
825 |
D
|
825D
|
D. Suitable Replacement
| 1,500 |
binary search; greedy; implementation
|
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric:Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
|
The first line contains string s (1 β€ |s| β€ 106).The second line contains string t (1 β€ |t| β€ 106).
|
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.If there are multiple strings with maximal suitability then print any of them.
|
In the first example string ""baab"" can be transformed to ""abab"" with swaps, this one has suitability of 2. That means that string ""baab"" also has suitability of 2.In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, ""azbz"" is just one of them.In the third example there are no '?' characters and the suitability of the string is 0.
|
Input: ?aa?ab | Output: baab
|
Medium
| 3 | 581 | 99 | 193 | 8 |
1,930 |
B
|
1930B
|
B. Permutation Printing
| 1,000 |
brute force; constructive algorithms; math
|
You are given a positive integer \(n\).Find a permutation\(^\dagger\) \(p\) of length \(n\) such that there do not exist two distinct indices \(i\) and \(j\) (\(1 \leq i, j < n\); \(i \neq j\)) such that \(p_i\) divides \(p_j\) and \(p_{i+1}\) divides \(p_{j+1}\).Refer to the Notes section for some examples.Under the constraints of this problem, it can be proven that at least one \(p\) exists.\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^5\)) β the length of the permutation \(p\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output \(p_1, p_2, \ldots, p_n\).If there are multiple solutions, you may output any one of them.
|
In the first test case, \(p=[4,1,2,3]\) is a valid permutation. However, the permutation \(p=[1,2,3,4]\) is not a valid permutation as we can choose \(i=1\) and \(j=3\). Then \(p_1=1\) divides \(p_3=3\) and \(p_2=2\) divides \(p_4=4\). Note that the permutation \(p=[3, 4, 2, 1]\) is also not a valid permutation as we can choose \(i=3\) and \(j=2\). Then \(p_3=2\) divides \(p_2=4\) and \(p_4=1\) divides \(p_3=2\).In the second test case, \(p=[1,2,3]\) is a valid permutation. In fact, all \(6\) permutations of length \(3\) are valid.
|
Input: 243 | Output: 4 1 2 3 1 2 3
|
Beginner
| 3 | 733 | 394 | 117 | 19 |
192 |
A
|
192A
|
A. Funky Numbers
| 1,300 |
binary search; brute force; implementation
|
As you very well know, this year's funkiest numbers are so called triangular numbers (that is, integers that are representable as , where k is some positive integer), and the coolest numbers are those that are representable as a sum of two triangular numbers.A well-known hipster Andrew adores everything funky and cool but unfortunately, he isn't good at maths. Given number n, help him define whether this number can be represented by a sum of two triangular numbers (not necessarily different)!
|
The first input line contains an integer n (1 β€ n β€ 109).
|
Print ""YES"" (without the quotes), if n can be represented as a sum of two triangular numbers, otherwise print ""NO"" (without the quotes).
|
In the first sample number .In the second sample number 512 can not be represented as a sum of two triangular numbers.
|
Input: 256 | Output: YES
|
Easy
| 3 | 497 | 57 | 140 | 1 |
297 |
C
|
297C
|
C. Splitting the Uniqueness
| 2,400 |
constructive algorithms
|
Polar bears like unique arrays β that is, arrays without repeated elements. You have got a unique array s with length n containing non-negative integers. Since you are good friends with Alice and Bob, you decide to split the array in two. Precisely, you need to construct two arrays a and b that are also of length n, with the following conditions for all i (1 β€ i β€ n): ai, bi are non-negative integers; si = ai + bi . Ideally, a and b should also be unique arrays. However, life in the Arctic is hard and this is not always possible. Fortunately, Alice and Bob are still happy if their arrays are almost unique. We define an array of length n to be almost unique, if and only if it can be turned into a unique array by removing no more than entries.For example, the array [1, 2, 1, 3, 2] is almost unique because after removing the first two entries, it becomes [1, 3, 2]. The array [1, 2, 1, 3, 1, 2] is not almost unique because we need to remove at least 3 entries to turn it into a unique array.So, your task is to split the given unique array s into two almost unique arrays a and b.
|
The first line of the input contains integer n (1 β€ n β€ 105).The second line contains n distinct integers s1, s2, ... sn (0 β€ si β€ 109).
|
If it is possible to make Alice and Bob happy (if you can split the given array), print ""YES"" (without quotes) in the first line. In the second line, print the array a. In the third line, print the array b. There may be more than one solution. Any of them will be accepted.If it is impossible to split s into almost unique arrays a and b, print ""NO"" (without quotes) in the first line.
|
In the sample, we can remove the first two entries from a and the second entry from b to make them both unique.
|
Input: 612 5 8 3 11 9 | Output: YES6 2 6 0 2 46 3 2 3 9 5
|
Expert
| 1 | 1,090 | 136 | 389 | 2 |
1,760 |
B
|
1760B
|
B. Atilla's Favorite Problem
| 800 |
greedy; implementation; strings
|
In order to write a string, Atilla needs to first learn all letters that are contained in the string.Atilla needs to write a message which can be represented as a string \(s\). He asks you what is the minimum alphabet size required so that one can write this message.The alphabet of size \(x\) (\(1 \leq x \leq 26\)) contains only the first \(x\) Latin letters. For example an alphabet of size \(4\) contains only the characters \(\texttt{a}\), \(\texttt{b}\), \(\texttt{c}\) and \(\texttt{d}\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 100\)) β the length of the string.The second line of each test case contains a string \(s\) of length \(n\), consisting of lowercase Latin letters.
|
For each test case, output a single integer β the minimum alphabet size required to so that Atilla can write his message \(s\).
|
For the first test case, Atilla needs to know only the character \(\texttt{a}\), so the alphabet of size \(1\) which only contains \(\texttt{a}\) is enough.For the second test case, Atilla needs to know the characters \(\texttt{d}\), \(\texttt{o}\), \(\texttt{w}\), \(\texttt{n}\). The smallest alphabet size that contains all of them is \(23\) (such alphabet can be represented as the string \(\texttt{abcdefghijklmnopqrstuvw}\)).
|
Input: 51a4down10codeforces3bcf5zzzzz | Output: 1 23 19 6 26
|
Beginner
| 3 | 495 | 328 | 127 | 17 |
1,685 |
B
|
1685B
|
B. Linguistics
| 2,000 |
greedy; implementation; sortings; strings
|
Alina has discovered a weird language, which contains only \(4\) words: \(\texttt{A}\), \(\texttt{B}\), \(\texttt{AB}\), \(\texttt{BA}\). It also turned out that there are no spaces in this language: a sentence is written by just concatenating its words into a single string.Alina has found one such sentence \(s\) and she is curious: is it possible that it consists of precisely \(a\) words \(\texttt{A}\), \(b\) words \(\texttt{B}\), \(c\) words \(\texttt{AB}\), and \(d\) words \(\texttt{BA}\)?In other words, determine, if it's possible to concatenate these \(a+b+c+d\) words in some order so that the resulting string is \(s\). Each of the \(a+b+c+d\) words must be used exactly once in the concatenation, but you can choose the order in which they are concatenated.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains four integers \(a\), \(b\), \(c\), \(d\) (\(0\le a,b,c,d\le 2\cdot 10^5\)) β the number of times that words \(\texttt{A}\), \(\texttt{B}\), \(\texttt{AB}\), \(\texttt{BA}\) respectively must be used in the sentence.The second line contains the string \(s\) (\(s\) consists only of the characters \(\texttt{A}\) and \(\texttt{B}\), \(1\le |s| \le 2\cdot 10^5\), \(|s|=a+b+2c+2d\)) β the sentence. Notice that the condition \(|s|=a+b+2c+2d\) (here \(|s|\) denotes the length of the string \(s\)) is equivalent to the fact that \(s\) is as long as the concatenation of the \(a+b+c+d\) words.The sum of the lengths of \(s\) over all test cases doesn't exceed \(2\cdot 10^5\).
|
For each test case output \(\texttt{YES}\) if it is possible that the sentence \(s\) consists of precisely \(a\) words \(\texttt{A}\), \(b\) words \(\texttt{B}\), \(c\) words \(\texttt{AB}\), and \(d\) words \(\texttt{BA}\), and \(\texttt{NO}\) otherwise. You can output each letter in any case.
|
In the first test case, the sentence \(s\) is \(\texttt{B}\). Clearly, it can't consist of a single word \(\texttt{A}\), so the answer is \(\texttt{NO}\).In the second test case, the sentence \(s\) is \(\texttt{AB}\), and it's possible that it consists of a single word \(\texttt{AB}\), so the answer is \(\texttt{YES}\).In the third test case, the sentence \(s\) is \(\texttt{ABAB}\), and it's possible that it consists of one word \(\texttt{A}\), one word \(\texttt{B}\), and one word \(\texttt{BA}\), as \(\texttt{A} + \texttt{BA} + \texttt{B} = \texttt{ABAB}\).In the fourth test case, the sentence \(s\) is \(\texttt{ABAAB}\), and it's possible that it consists of one word \(\texttt{A}\), one word \(\texttt{AB}\), and one word \(\texttt{BA}\), as \(\texttt{A} + \texttt{BA} + \texttt{AB} = \texttt{ABAAB}\). In the fifth test case, the sentence \(s\) is \(\texttt{BAABBABBAA}\), and it's possible that it consists of one word \(\texttt{A}\), one word \(\texttt{B}\), two words \(\texttt{AB}\), and two words \(\texttt{BA}\), as \(\texttt{BA} + \texttt{AB} + \texttt{B} + \texttt{AB} + \texttt{BA} + \texttt{A}= \texttt{BAABBABBAA}\).
|
Input: 8 1 0 0 0 B 0 0 1 0 AB 1 1 0 1 ABAB 1 0 1 1 ABAAB 1 1 2 2 BAABBABBAA 1 1 2 3 ABABABBAABAB 2 3 5 4 AABAABBABAAABABBABBBABB 1 3 3 10 BBABABABABBBABABABABABABAABABA | Output: NO YES YES YES YES YES NO YES
|
Hard
| 4 | 771 | 866 | 295 | 16 |
350 |
E
|
350E
|
E. Wrong Floyd
| 2,200 |
brute force; constructive algorithms; dfs and similar; graphs
|
Valera conducts experiments with algorithms that search for shortest paths. He has recently studied the Floyd's algorithm, so it's time to work with it.Valera's already written the code that counts the shortest distance between any pair of vertexes in a non-directed connected graph from n vertexes and m edges, containing no loops and multiple edges. Besides, Valera's decided to mark part of the vertexes. He's marked exactly k vertexes a1, a2, ..., ak.Valera's code is given below.ans[i][j] // the shortest distance for a pair of vertexes i, ja[i] // vertexes, marked by Valerafor(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { if (i == j) ans[i][j] = 0; else ans[i][j] = INF; //INF is a very large number }} for(i = 1; i <= m; i++) { read a pair of vertexes u, v that have a non-directed edge between them; ans[u][v] = 1; ans[v][u] = 1;}for (i = 1; i <= k; i++) { v = a[i]; for(j = 1; j <= n; j++) for(r = 1; r <= n; r++) ans[j][r] = min(ans[j][r], ans[j][v] + ans[v][r]);}Valera has seen that his code is wrong. Help the boy. Given the set of marked vertexes a1, a2, ..., ak, find such non-directed connected graph, consisting of n vertexes and m edges, for which Valera's code counts the wrong shortest distance for at least one pair of vertexes (i, j). Valera is really keen to get a graph without any loops and multiple edges. If no such graph exists, print -1.
|
The first line of the input contains three integers n, m, k (3 β€ n β€ 300, 2 β€ k β€ n , ) β the number of vertexes, the number of edges and the number of marked vertexes. The second line of the input contains k space-separated integers a1, a2, ... ak (1 β€ ai β€ n) β the numbers of the marked vertexes. It is guaranteed that all numbers ai are distinct.
|
If the graph doesn't exist, print -1 on a single line. Otherwise, print m lines, each containing two integers u, v β the description of the edges of the graph Valera's been looking for.
|
Input: 3 2 21 2 | Output: 1 32 3
|
Hard
| 4 | 1,370 | 350 | 185 | 3 |
|
1,249 |
A
|
1249A
|
A. Yet Another Dividing into Teams
| 800 |
math
|
You are a coach of a group consisting of \(n\) students. The \(i\)-th student has programming skill \(a_i\). All students have distinct programming skills. You want to divide them into teams in such a way that: No two students \(i\) and \(j\) such that \(|a_i - a_j| = 1\) belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than \(1\)); the number of teams is the minimum possible. You have to answer \(q\) independent queries.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 100\)) β the number of queries. Then \(q\) queries follow.The first line of the query contains one integer \(n\) (\(1 \le n \le 100\)) β the number of students in the query. The second line of the query contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 100\), all \(a_i\) are distinct), where \(a_i\) is the programming skill of the \(i\)-th student.
|
For each query, print the answer on it β the minimum number of teams you can form if no two students \(i\) and \(j\) such that \(|a_i - a_j| = 1\) may belong to the same team (i.e. skills of each pair of students in the same team has the difference strictly greater than \(1\))
|
In the first query of the example, there are \(n=4\) students with the skills \(a=[2, 10, 1, 20]\). There is only one restriction here: the \(1\)-st and the \(3\)-th students can't be in the same team (because of \(|a_1 - a_3|=|2-1|=1\)). It is possible to divide them into \(2\) teams: for example, students \(1\), \(2\) and \(4\) are in the first team and the student \(3\) in the second team.In the second query of the example, there are \(n=2\) students with the skills \(a=[3, 6]\). It is possible to compose just a single team containing both students.
|
Input: 4 4 2 10 1 20 2 3 6 5 2 3 4 99 100 1 42 | Output: 2 1 2 1
|
Beginner
| 1 | 492 | 436 | 277 | 12 |
2,039 |
C2
|
2039C2
|
C2. Shohag Loves XOR (Hard Version)
| 1,800 |
bitmasks; brute force; math; number theory
|
This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.Shohag has two integers \(x\) and \(m\). Help him count the number of integers \(1 \le y \le m\) such that \(x \oplus y\) is divisible\(^{\text{β}}\) by either \(x\), \(y\), or both. Here \(\oplus\) is the bitwise XOR operator.\(^{\text{β}}\)The number \(a\) is divisible by the number \(b\) if there exists an integer \(c\) such that \(a = b \cdot c\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first and only line of each test case contains two space-separated integers \(x\) and \(m\) (\(1 \le x \le 10^6\), \(1 \le m \le 10^{18}\)).It is guaranteed that the sum of \(x\) over all test cases does not exceed \(10^7\).
|
For each test case, print a single integer β the number of suitable \(y\).
|
In the first test case, for \(x = 7\), there are \(3\) valid values for \(y\) among the integers from \(1\) to \(m = 10\), and they are \(1\), \(7\), and \(9\). \(y = 1\) is valid because \(x \oplus y = 7 \oplus 1 = 6\) and \(6\) is divisible by \(y = 1\). \(y = 7\) is valid because \(x \oplus y = 7 \oplus 7 = 0\) and \(0\) is divisible by both \(x = 7\) and \(y = 7\). \(y = 9\) is valid because \(x \oplus y = 7 \oplus 9 = 14\) and \(14\) is divisible by \(x = 7\).
|
Input: 57 102 36 41 64 1 | Output: 3 2 2 6 1
|
Medium
| 4 | 527 | 325 | 74 | 20 |
1,725 |
G
|
1725G
|
G. Garage
| 1,500 |
binary search; geometry; math
|
Pak Chanek plans to build a garage. He wants the garage to consist of a square and a right triangle that are arranged like the following illustration. Define \(a\) and \(b\) as the lengths of two of the sides in the right triangle as shown in the illustration. An integer \(x\) is suitable if and only if we can construct a garage with assigning positive integer values for the lengths \(a\) and \(b\) (\(a<b\)) so that the area of the square at the bottom is exactly \(x\). As a good friend of Pak Chanek, you are asked to help him find the \(N\)-th smallest suitable number.
|
The only line contains a single integer \(N\) (\(1 \leq N \leq 10^9\)).
|
An integer that represents the \(N\)-th smallest suitable number.
|
The \(3\)-rd smallest suitable number is \(7\). A square area of \(7\) can be obtained by assigning \(a=3\) and \(b=4\).
|
Input: 3 | Output: 7
|
Medium
| 3 | 576 | 71 | 65 | 17 |
2,045 |
A
|
2045A
|
A. Scrambled Scrabble
| 1,700 |
brute force; greedy
|
You are playing a word game using a standard set of \(26\) uppercase English letters: A β Z. In this game, you can form vowels and consonants as follows. The letters A, E, I, O, and U can only form a vowel. The letter Y can form either a vowel or a consonant. Each of the remaining letters other than A, E, I, O, U, and Y can only form a consonant. The string NG can form a single consonant when concatenated together. Denote a syllable as a concatenation of a consonant, a vowel, and a consonant in that order. A word is a concatenation of one or more syllables.You are given a string \(S\) and you want to create a word from it. You are allowed to delete zero or more letters from \(S\) and rearrange the remaining letters to form the word. Find the length of the longest word that can be created, or determine if no words can be created.
|
A single line consisting of a string \(S\) (\(1 \leq |S| \leq 5000\)). The string \(S\) consists of only uppercase English letters.
|
If a word cannot be created, output 0. Otherwise, output a single integer representing the length of longest word that can be created.
|
Explanation for the sample input/output #1A possible longest word is JAKCARTAP, consisting of the syllables JAK, CAR, and TAP.Explanation for the sample input/output #2The whole string \(S\) is a word consisting of one syllable which is the concatenation of the consonant NG, the vowel E, and the consonant NG.Explanation for the sample input/output #3The whole string \(S\) is a word consisting of one syllable which is the concatenation of the consonant Y, the vowel Y, and the consonant Y.Explanation for the sample input/output #4The whole string \(S\) is a word consisting of two syllables: DAN and GAN.
|
Input: ICPCJAKARTA | Output: 9
|
Medium
| 2 | 840 | 131 | 134 | 20 |
954 |
C
|
954C
|
C. Matrix Walk
| 1,700 |
implementation
|
There is a matrix A of size x Γ y filled with integers. For every , Ai, j = y(i - 1) + j. Obviously, every integer from [1..xy] occurs exactly once in this matrix. You have traversed some path in this matrix. Your path can be described as a sequence of visited cells a1, a2, ..., an denoting that you started in the cell containing the number a1, then moved to the cell with the number a2, and so on.From the cell located in i-th line and j-th column (we denote this cell as (i, j)) you can move into one of the following cells: (i + 1, j) β only if i < x; (i, j + 1) β only if j < y; (i - 1, j) β only if i > 1; (i, j - 1) β only if j > 1.Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don't know x and y exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer a1, then move to the cell containing a2 (in one step), then move to the cell containing a3 (also in one step) and so on. Can you choose x and y so that they don't contradict with your sequence of moves?
|
The first line contains one integer number n (1 β€ n β€ 200000) β the number of cells you visited on your path (if some cell is visited twice, then it's listed twice).The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the integers in the cells on your path.
|
If all possible values of x and y such that 1 β€ x, y β€ 109 contradict with the information about your path, print NO.Otherwise, print YES in the first line, and in the second line print the values x and y such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109.
|
The matrix and the path on it in the first test looks like this: Also there exist multiple correct answers for both the first and the third examples.
|
Input: 81 2 3 6 9 8 5 2 | Output: YES3 3
|
Medium
| 1 | 1,104 | 273 | 354 | 9 |
685 |
C
|
685C
|
C. Optimal Point
| 2,900 |
binary search; math
|
When the river brought Gerda to the house of the Old Lady who Knew Magic, this lady decided to make Gerda her daughter. She wants Gerda to forget about Kay, so she puts all the roses from the garden underground.Mole, who lives in this garden, now can watch the roses without going up to the surface. Typical mole is blind, but this mole was granted as special vision by the Old Lady. He can watch any underground objects on any distance, even through the obstacles and other objects. However, the quality of the picture depends on the Manhattan distance to object being observed.Mole wants to find an optimal point to watch roses, that is such point with integer coordinates that the maximum Manhattan distance to the rose is minimum possible.As usual, he asks you to help.Manhattan distance between points (x1, y1, z1) and (x2, y2, z2) is defined as |x1 - x2| + |y1 - y2| + |z1 - z2|.
|
The first line of the input contains an integer t t (1 β€ t β€ 100 000) β the number of test cases. Then follow exactly t blocks, each containing the description of exactly one test.The first line of each block contains an integer ni (1 β€ ni β€ 100 000) β the number of roses in the test. Then follow ni lines, containing three integers each β the coordinates of the corresponding rose. Note that two or more roses may share the same position.It's guaranteed that the sum of all ni doesn't exceed 100 000 and all coordinates are not greater than 1018 by their absolute value.
|
For each of t test cases print three integers β the coordinates of the optimal point to watch roses. If there are many optimal answers, print any of them.The coordinates of the optimal point may coincide with the coordinates of any rose.
|
In the first sample, the maximum Manhattan distance from the point to the rose is equal to 4.In the second sample, the maximum possible distance is 0. Note that the positions of the roses may coincide with each other and with the position of the optimal point.
|
Input: 150 0 40 0 -40 4 04 0 01 1 1 | Output: 0 0 0
|
Master
| 2 | 885 | 572 | 237 | 6 |
298 |
B
|
298B
|
B. Sail
| 1,200 |
brute force; greedy; implementation
|
The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y). If the wind blows to the east, the boat will move to (x + 1, y). If the wind blows to the south, the boat will move to (x, y - 1). If the wind blows to the west, the boat will move to (x - 1, y). If the wind blows to the north, the boat will move to (x, y + 1). Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (ex, ey)?
|
The first line contains five integers t, sx, sy, ex, ey (1 β€ t β€ 105, - 109 β€ sx, sy, ex, ey β€ 109). The starting location and the ending location will be different.The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: ""E"" (east), ""S"" (south), ""W"" (west) and ""N"" (north).
|
If they can reach (ex, ey) within t seconds, print the earliest time they can achieve it. Otherwise, print ""-1"" (without quotes).
|
In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.In the second sample, they cannot sail to the destination.
|
Input: 5 0 0 1 1SESNW | Output: 4
|
Easy
| 3 | 691 | 375 | 131 | 2 |
402 |
C
|
402C
|
C. Searching for Graph
| 1,500 |
brute force; constructive algorithms; graphs
|
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill: the graph contains exactly 2n + p edges; the graph doesn't contain self-loops and multiple edges; for any integer k (1 β€ k β€ n), any subgraph consisting of k vertices contains at most 2k + p edges. A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. Your task is to find a p-interesting graph consisting of n vertices.
|
The first line contains a single integer t (1 β€ t β€ 5) β the number of tests in the input. Next t lines each contains two space-separated integers: n, p (5 β€ n β€ 24; p β₯ 0; ) β the number of vertices in the graph and the interest value for the appropriate test. It is guaranteed that the required graph exists.
|
For each of the t tests print 2n + p lines containing the description of the edges of a p-interesting graph: the i-th line must contain two space-separated integers ai, bi (1 β€ ai, bi β€ n; ai β bi) β two vertices, connected by an edge in the resulting graph. Consider the graph vertices numbered with integers from 1 to n. Print the answers to the tests in the order the tests occur in the input. If there are multiple solutions, you can print any of them.
|
Input: 16 0 | Output: 1 21 31 41 51 62 32 42 52 63 43 53 6
|
Medium
| 3 | 582 | 310 | 456 | 4 |
|
1,833 |
G
|
1833G
|
G. Ksyusha and Chinchilla
| 1,800 |
constructive algorithms; dfs and similar; dp; dsu; greedy; implementation; trees
|
Ksyusha has a pet chinchilla, a tree on \(n\) vertices and huge scissors. A tree is a connected graph without cycles. During a boring physics lesson Ksyusha thought about how to entertain her pet.Chinchillas like to play with branches. A branch is a tree of \(3\) vertices. The branch looks like this. A cut is the removal of some (not yet cut) edge in the tree. Ksyusha has plenty of free time, so she can afford to make enough cuts so that the tree splits into branches. In other words, after several (possibly zero) cuts, each vertex must belong to exactly one branch.Help Ksyusha choose the edges to be cut or tell that it is impossible.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.The next \(n - 1\) rows of each testcase contain integers \(v_i\) and \(u_i\) (\(1 \le v_i, u_i \le n\)) β the numbers of vertices that the \(i\)-th edge connects.It is guaranteed that this set of edges forms a tree. It is also guaranteed that the sum of \(n\) over all testcases does not exceed \(2 \cdot 10^5\).
|
Print the answer for each testcase.If the desired way to cut the tree does not exist, print \(-1\).Otherwise, print an integer \(k\) β the number of edges to be cut. In the next line, print \(k\) different integers \(e_i\) (\(1 \le e_i < n\)) β numbers of the edges to be cut. If \(k = 0\), print an empty string instead.If there are several solutions, you can print any.
|
The first testcase in first test.
|
Input: 491 24 37 95 44 63 28 71 761 21 34 31 56 161 23 23 44 56 551 35 35 23 4 | Output: 2 2 8 -1 1 3 -1
|
Medium
| 7 | 641 | 537 | 371 | 18 |
1,493 |
D
|
1493D
|
D. GCD of an Array
| 2,100 |
brute force; data structures; hashing; implementation; math; number theory; sortings; two pointers
|
You are given an array \(a\) of length \(n\). You are asked to process \(q\) queries of the following format: given integers \(i\) and \(x\), multiply \(a_i\) by \(x\).After processing each query you need to output the greatest common divisor (GCD) of all elements of the array \(a\).Since the answer can be too large, you are asked to output it modulo \(10^9+7\).
|
The first line contains two integers β \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)) β the elements of the array \(a\) before the changes.The next \(q\) lines contain queries in the following format: each line contains two integers \(i\) and \(x\) (\(1 \le i \le n\), \(1 \le x \le 2 \cdot 10^5\)).
|
Print \(q\) lines: after processing each query output the GCD of all elements modulo \(10^9+7\) on a separate line.
|
After the first query the array is \([12, 6, 8, 12]\), \(\operatorname{gcd}(12, 6, 8, 12) = 2\).After the second query β \([12, 18, 8, 12]\), \(\operatorname{gcd}(12, 18, 8, 12) = 2\).After the third query β \([12, 18, 24, 12]\), \(\operatorname{gcd}(12, 18, 24, 12) = 6\).Here the \(\operatorname{gcd}\) function denotes the greatest common divisor.
|
Input: 4 3 1 6 8 12 1 12 2 3 3 3 | Output: 2 2 6
|
Hard
| 8 | 364 | 401 | 115 | 14 |
643 |
G
|
643G
|
G. Choosing Ads
| 3,200 |
data structures
|
One social network developer recently suggested a new algorithm of choosing ads for users.There are n slots which advertisers can buy. It is possible to buy a segment of consecutive slots at once. The more slots you own, the bigger are the chances your ad will be shown to users.Every time it is needed to choose ads to show, some segment of slots is picked by a secret algorithm. Then some advertisers are chosen. The only restriction is that it should be guaranteed for advertisers which own at least p% of slots composing this segment that their ad will be shown.From the other side, users don't like ads. So it was decided to show no more than ads at once. You are asked to develop a system to sell segments of slots and choose ads in accordance with the rules described above.
|
The first line of the input contains three integers n, m and p (1 β€ n, m β€ 150 000, 20 β€ p β€ 100) β the number of slots, the number of queries to your system and threshold for which display of the ad is guaranteed.Next line contains n integers ai (1 β€ ai β€ 150 000), where the i-th number means id of advertiser who currently owns the i-th slot.Next m lines contain queries descriptions. Each description is of one of the following forms: 1 l r id (1 β€ l β€ r β€ n, 1 β€ id β€ 150 000) β advertiser id bought all slots in a range from l to r inclusive; 2 l r (1 β€ l β€ r) β you need to choose advertisers for segment [l, r].
|
For each query of the second type answer should be printed in a separate line. First integer of the answer should be the number of advertisements that will be shown . Next cnt integers should be advertisers' ids. It is allowed to print one advertiser more than once, but each advertiser that owns at least slots of the segment from l to r should be in your answer.
|
Samples demonstrate that you actually have quite a lot of freedom in choosing advertisers.
|
Input: 5 9 331 2 1 3 32 1 52 1 52 1 32 3 31 2 4 52 1 52 3 51 4 5 12 1 5 | Output: 3 1 2 32 1 32 2 13 1 1000 10001 52 5 32 1 5
|
Master
| 1 | 781 | 619 | 364 | 6 |
1,696 |
B
|
1696B
|
B. NIT Destroys the Universe
| 900 |
greedy
|
For a collection of integers \(S\), define \(\operatorname{mex}(S)\) as the smallest non-negative integer that does not appear in \(S\).NIT, the cleaver, decides to destroy the universe. He is not so powerful as Thanos, so he can only destroy the universe by snapping his fingers several times.The universe can be represented as a 1-indexed array \(a\) of length \(n\). When NIT snaps his fingers, he does the following operation on the array: He selects positive integers \(l\) and \(r\) such that \(1\le l\le r\le n\). Let \(w=\operatorname{mex}(\{a_l,a_{l+1},\dots,a_r\})\). Then, for all \(l\le i\le r\), set \(a_i\) to \(w\). We say the universe is destroyed if and only if for all \(1\le i\le n\), \(a_i=0\) holds.Find the minimum number of times NIT needs to snap his fingers to destroy the universe. That is, find the minimum number of operations NIT needs to perform to make all elements in the array equal to \(0\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1\le n\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 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, print one integer β the answer to the problem.
|
In the first test case, we do \(0\) operations and all elements in the array are already equal to \(0\).In the second test case, one optimal way is doing the operation with \(l=2\), \(r=5\).In the third test case, one optimal way is doing the operation twice, respectively with \(l=4\), \(r=4\) and \(l=2\), \(r=6\).In the fourth test case, one optimal way is doing the operation with \(l=1\), \(r=1\).
|
Input: 440 0 0 050 1 2 3 470 2 3 0 1 2 011000000000 | Output: 0 1 2 1
|
Beginner
| 1 | 925 | 448 | 66 | 16 |
2,117 |
A
|
2117A
|
A. False Alarm
| 800 |
greedy; implementation
|
Yousef is at the entrance of a long hallway with \(n\) doors in a row, numbered from \(1\) to \(n\). He needs to pass through all the doors from \(1\) to \(n\) in order of numbering and reach the exit (past door \(n\)). Each door can be open or closed. If a door is open, Yousef passes through it in \(1\) second. If the door is closed, Yousef can't pass through it.However, Yousef has a special button which he can use at most once at any moment. This button makes all closed doors become open for \(x\) seconds.Your task is to determine if Yousef can pass through all the doors if he can use the button at most once.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains two integers \(n, x\) (\(1 \le n, x \le 10\)) β the number of doors and the number of seconds of the button, respectively.The second line of each test case contains \(n\) integers \(a_1, a_2, ..., a_n\) (\(a_i \in \{0, 1\}\)) β the state of each door. Open doors are represented by '0', while closed doors are represented by '1'.It is guaranteed that each test case contains at least one closed door.
|
For each test case, output ""YES"" if Yousef can reach the exit, and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, the optimal way is as follows: At time \(0\), the door is open, so Yousef passes. At time \(1\), the door is closed, Yousef can use the button now and pass through the door. At time \(2\), the button's effect is still on, so Yousef can still pass. At time \(3\), the button's effect has finished, but the door is open. Yousef passes and reaches the exit. In the second test case, Yousef has a 3-second button, but he would need at least a 4-second button to reach the exit. Therefore, the answer is NO.In the third test case, Yousef can turn on the button before starting to move. All the doors will stay open until he reaches the exit.
|
Input: 74 20 1 1 06 31 0 1 1 0 08 81 1 1 0 0 1 1 11 215 11 0 1 0 17 40 0 0 1 1 0 110 30 1 0 0 1 0 0 1 0 0 | Output: YES NO YES YES NO YES NO
|
Beginner
| 2 | 618 | 546 | 247 | 21 |
164 |
A
|
164A
|
A. Variable, or There and Back Again
| 1,700 |
dfs and similar; graphs
|
Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used!Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or ignored. Between some states are directed (oriented) transitions.A path is a sequence of states v1, v2, ..., vx, where for any 1 β€ i < x exists a transition from vi to vi + 1.Vasya's value in state v is interesting to the world, if exists path p1, p2, ..., pk such, that pi = v for some i (1 β€ i β€ k), in state p1 Vasya gets assigned a value, in state pk Vasya is used and there is no state pi (except for p1) where Vasya gets assigned a value.Help Vasya, find the states in which Vasya's value is interesting to the world.
|
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the numbers of states and transitions, correspondingly.The second line contains space-separated n integers f1, f2, ..., fn (0 β€ fi β€ 2), fi described actions performed upon Vasya in state i: 0 represents ignoring, 1 β assigning a value, 2 β using.Next m lines contain space-separated pairs of integers ai, bi (1 β€ ai, bi β€ n, ai β bi), each pair represents the transition from the state number ai to the state number bi. Between two states can be any number of transitions.
|
Print n integers r1, r2, ..., rn, separated by spaces or new lines. Number ri should equal 1, if Vasya's value in state i is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to n in the order, in which they are described in the input.
|
In the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1 2 3 4; it includes all the states, so in all of them Vasya's value is interesting to the world.The second sample the only path in which Vasya's value is interesting to the world is , β 1 3; state 2 is not included there.In the third sample we cannot make from the states any path in which the value of Vasya would be interesting to the world, so the value of Vasya is never interesting to the world.
|
Input: 4 31 0 0 21 22 33 4 | Output: 1111
|
Medium
| 2 | 840 | 553 | 275 | 1 |
438 |
D
|
438D
|
D. The Child and Sequence
| 2,300 |
data structures; math
|
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array a[1], a[2], ..., a[n]. Then he should perform a sequence of m operations. An operation can be one of the following: Print operation l, r. Picks should write down the value of . Modulo operation l, r, x. Picks should perform assignment a[i] = a[i] mod x for each i (l β€ i β€ r). Set operation k, x. Picks should set the value of a[k] to x (in other words perform an assignment a[k] = x). Can you help Picks to perform the whole sequence of operations?
|
The first line of input contains two integer: n, m (1 β€ n, m β€ 105). The second line contains n integers, separated by space: a[1], a[2], ..., a[n] (1 β€ a[i] β€ 109) β initial value of array elements.Each of the next m lines begins with a number type . If type = 1, there will be two integers more in the line: l, r (1 β€ l β€ r β€ n), which correspond the operation 1. If type = 2, there will be three integers more in the line: l, r, x (1 β€ l β€ r β€ n; 1 β€ x β€ 109), which correspond the operation 2. If type = 3, there will be two integers more in the line: k, x (1 β€ k β€ n; 1 β€ x β€ 109), which correspond the operation 3.
|
For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer.
|
Consider the first testcase: At first, a = {1, 2, 3, 4, 5}. After operation 1, a = {1, 2, 3, 0, 1}. After operation 2, a = {1, 2, 5, 0, 1}. At operation 3, 2 + 5 + 0 + 1 = 8. After operation 4, a = {1, 2, 2, 0, 1}. At operation 5, 1 + 2 + 2 = 5.
|
Input: 5 51 2 3 4 52 3 5 43 3 51 2 52 1 3 31 1 3 | Output: 85
|
Expert
| 2 | 737 | 620 | 118 | 4 |
1,133 |
F2
|
1133F2
|
F2. Spanning Tree with One Fixed Degree
| 1,900 |
constructive algorithms; dfs and similar; dsu; graphs; greedy
|
You are given an undirected unweighted connected graph consisting of \(n\) vertices and \(m\) edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label \(1\) on it) is equal to \(D\) (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it.
|
The first line contains three integers \(n\), \(m\) and \(D\) (\(2 \le n \le 2 \cdot 10^5\), \(n - 1 \le m \le min(2 \cdot 10^5, \frac{n(n-1)}{2}), 1 \le D < n\)) β the number of vertices, the number of edges and required degree of the first vertex, respectively.The following \(m\) lines denote edges: edge \(i\) is represented by a pair of integers \(v_i\), \(u_i\) (\(1 \le v_i, u_i \le n\), \(u_i \ne v_i\)), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair (\(v_i, u_i\)) there are no other pairs (\(v_i, u_i\)) or (\(u_i, v_i\)) in the list of edges, and for each pair \((v_i, u_i)\) the condition \(v_i \ne u_i\) is satisfied.
|
If there is no spanning tree satisfying the condition from the problem statement, print ""NO"" in the first line.Otherwise print ""YES"" in the first line and then print \(n-1\) lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label \(1\) on it) is equal to \(D\). Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge \((v, u)\) is considered the same as the edge \((u, v)\)).If there are multiple possible answers, print any of them.
|
The picture corresponding to the first and second examples: The picture corresponding to the third example:
|
Input: 4 5 1 1 2 1 3 1 4 2 3 3 4 | Output: YES 2 1 2 3 3 4
|
Hard
| 5 | 449 | 718 | 553 | 11 |
1,334 |
B
|
1334B
|
B. Middle Class
| 1,100 |
greedy; sortings
|
Many years ago Berland was a small country where only \(n\) people lived. Each person had some savings: the \(i\)-th one had \(a_i\) burles.The government considered a person as wealthy if he had at least \(x\) burles. To increase the number of wealthy people Berland decided to carry out several reforms. Each reform looked like that: the government chooses some subset of people (maybe all of them); the government takes all savings from the chosen people and redistributes the savings among the chosen people equally. For example, consider the savings as list \([5, 1, 2, 1]\): if the government chose the \(1\)-st and the \(3\)-rd persons then it, at first, will take all \(5 + 2 = 7\) burles and after that will return \(3.5\) burles to the chosen people. As a result, the savings will become \([3.5, 1, 3.5, 1]\).A lot of data was lost from that time, so we don't know how many reforms were implemented and to whom. All we can do is ask you to calculate the maximum possible number of wealthy people after several (maybe zero) reforms.
|
The first line contains single integer \(T\) (\(1 \le T \le 1000\)) β the number of test cases.Next \(2T\) lines contain the test cases β two lines per test case. The first line contains two integers \(n\) and \(x\) (\(1 \le n \le 10^5\), \(1 \le x \le 10^9\)) β the number of people and the minimum amount of money to be considered as wealthy.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the initial savings of each person.It's guaranteed that the total sum of \(n\) doesn't exceed \(10^5\).
|
Print \(T\) integers β one per test case. For each test case print the maximum possible number of wealthy people after several (maybe zero) reforms.
|
The first test case is described in the statement.In the second test case, the government, for example, could carry out two reforms: \([\underline{11}, \underline{9}, 11, 9] \rightarrow [10, 10, \underline{11}, \underline{9}] \rightarrow [10, 10, 10, 10]\).In the third test case, the government couldn't make even one person wealthy.In the fourth test case, the government could choose all people to carry out a reform: \([\underline{9}, \underline{4}, \underline{9}] \rightarrow [7\frac{1}{3}, 7\frac{1}{3}, 7\frac{1}{3}]\).
|
Input: 4 4 3 5 1 2 1 4 10 11 9 11 9 2 5 4 3 3 7 9 4 9 | Output: 2 4 0 3
|
Easy
| 2 | 1,041 | 539 | 148 | 13 |
1,056 |
E
|
1056E
|
E. Check Transcription
| 2,100 |
brute force; data structures; hashing; strings
|
One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a signal \(s\) towards a faraway galaxy. Recently they've received a response \(t\) which they believe to be a response from aliens! The scientists now want to check if the signal \(t\) is similar to \(s\).The original signal \(s\) was a sequence of zeros and ones (everyone knows that binary code is the universe-wide language). The returned signal \(t\), however, does not look as easy as \(s\), but the scientists don't give up! They represented \(t\) as a sequence of English letters and say that \(t\) is similar to \(s\) if you can replace all zeros in \(s\) with some string \(r_0\) and all ones in \(s\) with some other string \(r_1\) and obtain \(t\). The strings \(r_0\) and \(r_1\) must be different and non-empty.Please help Arkady's friend and find the number of possible replacements for zeros and ones (the number of pairs of strings \(r_0\) and \(r_1\)) that transform \(s\) to \(t\).
|
The first line contains a string \(s\) (\(2 \le |s| \le 10^5\)) consisting of zeros and ones β the original signal.The second line contains a string \(t\) (\(1 \le |t| \le 10^6\)) consisting of lowercase English letters only β the received signal.It is guaranteed, that the string \(s\) contains at least one '0' and at least one '1'.
|
Print a single integer β the number of pairs of strings \(r_0\) and \(r_1\) that transform \(s\) to \(t\).In case there are no such pairs, print \(0\).
|
In the first example, the possible pairs \((r_0, r_1)\) are as follows: ""a"", ""aaaaa"" ""aa"", ""aaaa"" ""aaaa"", ""aa"" ""aaaaa"", ""a"" The pair ""aaa"", ""aaa"" is not allowed, since \(r_0\) and \(r_1\) must be different.In the second example, the following pairs are possible: ""ko"", ""kokotlin"" ""koko"", ""tlin""
|
Input: 01 aaaaaa | Output: 4
|
Hard
| 4 | 998 | 334 | 151 | 10 |
1,042 |
F
|
1042F
|
F. Leaf Sets
| 2,400 |
data structures; dfs and similar; dsu; graphs; greedy; sortings; trees
|
You are given an undirected tree, consisting of \(n\) vertices.The vertex is called a leaf if it has exactly one vertex adjacent to it.The distance between some pair of vertices is the number of edges in the shortest path between them.Let's call some set of leaves beautiful if the maximum distance between any pair of leaves in it is less or equal to \(k\).You want to split all leaves into non-intersecting beautiful sets. What is the minimal number of sets in such a split?
|
The first line contains two integers \(n\) and \(k\) (\(3 \le n \le 10^6\), \(1 \le k \le 10^6\)) β the number of vertices in the tree and the maximum distance between any pair of leaves in each beautiful set.Each of the next \(n - 1\) lines contains two integers \(v_i\) and \(u_i\) (\(1 \le v_i, u_i \le n\)) β the description of the \(i\)-th edge. It is guaranteed that the given edges form a tree.
|
Print a single integer β the minimal number of beautiful sets the split can have.
|
Here is the graph for the first example:
|
Input: 9 31 21 32 42 53 66 76 83 9 | Output: 2
|
Expert
| 7 | 476 | 401 | 81 | 10 |
83 |
D
|
83D
|
D. Numbers
| 2,400 |
dp; math; number theory
|
One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in the middle of a lesson. And only a loud ringing of a school bell could interrupt his sweet dream. Of course, the valuable material and the teacher's explanations were lost. However, Valera will one way or another have to do the homework. As he does not know the new material absolutely, he cannot do the job himself. That's why he asked you to help. You're his best friend after all, you just cannot refuse to help. Valera's home task has only one problem, which, though formulated in a very simple way, has not a trivial solution. Its statement looks as follows: if we consider all positive integers in the interval [a;b] then it is required to count the amount of such numbers in this interval that their smallest divisor will be a certain integer k (you do not have to consider divisor equal to one). In other words, you should count the amount of such numbers from the interval [a;b], that are not divisible by any number between 2 and k - 1 and yet are divisible by k.
|
The first and only line contains three positive integers a, b, k (1 β€ a β€ b β€ 2Β·109, 2 β€ k β€ 2Β·109).
|
Print on a single line the answer to the given problem.
|
Comments to the samples from the statement: In the first sample the answer is numbers 2, 4, 6, 8, 10.In the second one β 15, 21In the third one there are no such numbers.
|
Input: 1 10 2 | Output: 5
|
Expert
| 3 | 1,293 | 100 | 55 | 0 |
648 |
D
|
648D
|
D. Π‘ΠΎΠ±Π°ΡΠΊΠΈ ΠΈ ΠΌΠΈΡΠΊΠΈ
| 1,900 |
data structures; greedy; sortings
|
ΠΠ° ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ½ΠΎΠΉ ΠΏΡΡΠΌΠΎΠΉ ΡΠΈΠ΄ΠΈΡ n ΡΠΎΠ±Π°ΡΠ΅ΠΊ, i-Ρ ΡΠΎΠ±Π°ΡΠΊΠ° Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ Π² ΡΠΎΡΠΊΠ΅ xi. ΠΡΠΎΠΌΠ΅ ΡΠΎΠ³ΠΎ, Π½Π° ΠΏΡΡΠΌΠΎΠΉ Π΅ΡΡΡ m ΠΌΠΈΡΠΎΠΊ Ρ Π΅Π΄ΠΎΠΉ, Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ·Π²Π΅ΡΡΠ½Π° Π΅Ρ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° Π½Π° ΠΏΡΡΠΌΠΎΠΉ uj ΠΈ Π²ΡΠ΅ΠΌΡ tj, ΡΠ΅ΡΠ΅Π· ΠΊΠΎΡΠΎΡΠΎΠ΅ Π΅Π΄Π° Π² ΠΌΠΈΡΠΊΠ΅ ΠΎΡΡΡΠ½Π΅Ρ ΠΈ ΡΡΠ°Π½Π΅Ρ Π½Π΅Π²ΠΊΡΡΠ½ΠΎΠΉ. ΠΡΠΎ Π·Π½Π°ΡΠΈΡ, ΡΡΠΎ Π΅ΡΠ»ΠΈ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΡΠΈΠ±Π΅ΠΆΠΈΡ ΠΊ ΠΌΠΈΡΠΊΠ΅ Π² ΠΌΠΎΠΌΠ΅Π½Ρ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ, ΡΡΡΠΎΠ³ΠΎ Π±ΠΎΠ»ΡΡΠΈΠΉ tj, ΡΠΎ Π΅Π΄Π° ΡΠΆΠ΅ ΠΎΡΡΡΠ½Π΅Ρ, ΠΈ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΊΡΡΠ°ΡΡ Π΅Ρ Π½Π΅ ΡΡΠ°Π½Π΅Ρ.Π‘ΡΠΈΡΠ°Ρ, ΡΡΠΎ ΠΊΠ°ΠΆΠ΄Π°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° Π±Π΅ΠΆΠΈΡ ΡΠΎ ΡΠΊΠΎΡΠΎΡΡΡΡ 1, Π½Π°ΠΉΠ΄ΠΈΡΠ΅ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠΌΠΎΠ³ΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ. Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ ΡΠΎΠ±Π°ΡΠΊΠΈ ΠΏΠΎΠ±Π΅Π³ΡΡ ΠΊ ΡΠ΅ΠΌ ΠΌΠΈΡΠΊΠ°ΠΌ, Π½Π° ΠΊΠΎΡΠΎΡΡΠ΅ Π²Ρ ΠΈΠΌ ΡΠΊΠ°ΠΆΠ΅ΡΠ΅. ΠΠ· ΠΎΠ΄Π½ΠΎΠΉ ΠΌΠΈΡΠΊΠΈ Π½Π΅ ΠΌΠΎΠ³ΡΡ ΠΊΡΡΠ°ΡΡ Π΄Π²Π΅ ΠΈΠ»ΠΈ Π±ΠΎΠ»Π΅Π΅ ΡΠΎΠ±Π°ΡΠΊΠΈ.Π‘ΠΎΠ±Π°ΡΠΊΠΈ ΠΌΠΎΠ³ΡΡ ΠΎΠ±Π³ΠΎΠ½ΡΡΡ Π΄ΡΡΠ³ Π΄ΡΡΠ³Π°, ΡΠΎ Π΅ΡΡΡ, Π΅ΡΠ»ΠΈ ΠΎΠ΄Π½Π° ΠΈΠ· Π½ΠΈΡ
ΠΎΡΡΠ°Π½ΠΎΠ²ΠΈΡΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ, Π΄ΡΡΠ³Π°Ρ ΠΌΠΎΠΆΠ΅Ρ ΠΏΡΠΎΠΉΡΠΈ ΠΌΠΈΠΌΠΎ Π½Π΅Ρ, ΡΡΠΎΠ±Ρ ΠΏΠΎΠΏΠ°ΡΡΡ ΠΊ Π΄ΡΡΠ³ΠΎΠΉ ΠΌΠΈΡΠΊΠ΅.
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ ΠΏΠ°ΡΠ° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» n ΠΈ m (1 β€ n, m β€ 200 000) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ ΠΈ ΠΌΠΈΡΠΎΠΊ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ n ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» xi ( - 109 β€ xi β€ 109) β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° i-ΠΉ ΡΠΎΠ±Π°ΡΠΊΠΈ.Π ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
m ΡΡΡΠΎΠΊΠ°Ρ
Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ ΠΏΠ°ΡΡ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» uj ΠΈ tj ( - 109 β€ uj β€ 109, 1 β€ tj β€ 109) β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ° j-ΠΉ ΠΌΠΈΡΠΊΠΈ ΠΈ Π²ΡΠ΅ΠΌΡ, ΠΊΠΎΠ³Π΄Π° ΠΎΡΡΡΠ½Π΅Ρ Π΅Π΄Π° Π² Π½Π΅ΠΉ, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²Π΅Π½Π½ΠΎ.ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ Π½ΠΈΠΊΠ°ΠΊΠΈΠ΅ Π΄Π²Π΅ ΡΠΎΠ±Π°ΡΠΊΠΈ Π½Π΅ Π½Π°Ρ
ΠΎΠ΄ΡΡΡΡ Π² ΠΎΠ΄Π½ΠΎΠΉ ΡΠΎΡΠΊΠ΅. ΠΠΈΠΊΠ°ΠΊΠΈΠ΅ Π΄Π²Π΅ ΠΌΠΈΡΠΊΠΈ ΡΠ°ΠΊΠΆΠ΅ Π½Π΅ ΠΌΠΎΠ³ΡΡ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡΡ Π² ΠΎΠ΄Π½ΠΎΠΉ ΡΠΎΡΠΊΠ΅.
|
ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ a β ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠΎΠ±Π°ΡΠ΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠΌΠΎΠ³ΡΡ ΠΏΠΎΠΊΡΡΠ°ΡΡ.
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠ΅ΡΠ²Π°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π½Π°ΠΏΡΠ°Π²ΠΎ ΠΊ ΠΏΠ΅ΡΠ²ΠΎΠΉ ΠΌΠΈΡΠΊΠ΅, ΡΡΠ΅ΡΡΡ ΡΠΎΠ±Π°ΡΠΊΠ° ΡΡΠ°Π·Ρ Π½Π°ΡΠ½ΡΡ Π΅ΡΡΡ ΠΈΠ· Π²ΡΠΎΡΠΎΠΉ ΠΌΠΈΡΠΊΠΈ, ΡΠ΅ΡΠ²ΡΡΡΠ°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΊ ΡΡΠ΅ΡΡΠ΅ΠΉ ΠΌΠΈΡΠΊΠ΅, Π° ΠΏΡΡΠ°Ρ ΡΠΎΠ±Π°ΡΠΊΠ° ΠΏΠΎΠ±Π΅ΠΆΠΈΡ Π²Π»Π΅Π²ΠΎ ΠΊ ΡΠ΅ΡΠ²ΡΡΡΠΎΠΉ ΠΌΠΈΡΠΊΠ΅.
|
Input: 5 4-2 0 4 8 13-1 14 36 311 2 | Output: 4
|
Hard
| 3 | 743 | 491 | 87 | 6 |
853 |
E
|
853E
|
E. Lada Malina
| 3,400 |
data structures; geometry
|
After long-term research and lots of experiments leading Megapolian automobile manufacturer Β«AutoVozΒ» released a brand new car model named Β«Lada MalinaΒ». One of the most impressive features of Β«Lada MalinaΒ» is its highly efficient environment-friendly engines.Consider car as a point in Oxy plane. Car is equipped with k engines numbered from 1 to k. Each engine is defined by its velocity vector whose coordinates are (vxi, vyi) measured in distance units per day. An engine may be turned on at any level wi, that is a real number between - 1 and + 1 (inclusive) that result in a term of (wiΒ·vxi, wiΒ·vyi) in the final car velocity. Namely, the final car velocity is equal to (w1Β·vx1 + w2Β·vx2 + ... + wkΒ·vxk, w1Β·vy1 + w2Β·vy2 + ... + wkΒ·vyk) Formally, if car moves with constant values of wi during the whole day then its x-coordinate will change by the first component of an expression above, and its y-coordinate will change by the second component of an expression above. For example, if all wi are equal to zero, the car won't move, and if all wi are equal to zero except w1 = 1, then car will move with the velocity of the first engine.There are n factories in Megapolia, i-th of them is located in (fxi, fyi). On the i-th factory there are ai cars Β«Lada MalinaΒ» that are ready for operation.As an attempt to increase sales of a new car, Β«AutoVozΒ» is going to hold an international exposition of cars. There are q options of exposition location and time, in the i-th of them exposition will happen in a point with coordinates (pxi, pyi) in ti days. Of course, at the Β«AutoVozΒ» is going to bring as much new cars from factories as possible to the place of exposition. Cars are going to be moved by enabling their engines on some certain levels, such that at the beginning of an exposition car gets exactly to the exposition location. However, for some of the options it may be impossible to bring cars from some of the factories to the exposition location by the moment of an exposition. Your task is to determine for each of the options of exposition location and time how many cars will be able to get there by the beginning of an exposition.
|
The first line of input contains three integers k, n, q (2 β€ k β€ 10, 1 β€ n β€ 105, 1 β€ q β€ 105), the number of engines of Β«Lada MalinaΒ», number of factories producing Β«Lada MalinaΒ» and number of options of an exposition time and location respectively.The following k lines contain the descriptions of Β«Lada MalinaΒ» engines. The i-th of them contains two integers vxi, vyi ( - 1000 β€ vxi, vyi β€ 1000) defining the velocity vector of the i-th engine. Velocity vector can't be zero, i.e. at least one of vxi and vyi is not equal to zero. It is guaranteed that no two velosity vectors are collinear (parallel).Next n lines contain the descriptions of factories. The i-th of them contains two integers fxi, fyi, ai ( - 109 β€ fxi, fyi β€ 109, 1 β€ ai β€ 109) defining the coordinates of the i-th factory location and the number of cars that are located there.The following q lines contain the descriptions of the car exposition. The i-th of them contains three integers pxi, pyi, ti ( - 109 β€ pxi, pyi β€ 109, 1 β€ ti β€ 105) defining the coordinates of the exposition location and the number of days till the exposition start in the i-th option.
|
For each possible option of the exposition output the number of cars that will be able to get to the exposition location by the moment of its beginning.
|
Images describing sample tests are given below. Exposition options are denoted with crosses, factories are denoted with points. Each factory is labeled with a number of cars that it has.First sample test explanation: Car from the first factory is not able to get to the exposition location in time. Car from the second factory can get to the exposition in time if we set w1 = 0, w2 = 1. Car from the third factory can get to the exposition in time if we set , . Car from the fourth factory can get to the exposition in time if we set w1 = 1, w2 = 0.
|
Input: 2 4 11 1-1 12 3 12 -2 1-2 1 1-2 -2 10 0 2 | Output: 3
|
Master
| 2 | 2,147 | 1,133 | 152 | 8 |
1,545 |
C
|
1545C
|
C. AquaMoon and Permutations
| 2,800 |
2-sat; brute force; combinatorics; constructive algorithms; graph matchings; graphs
|
Cirno has prepared \(n\) arrays of length \(n\) each. Each array is a permutation of \(n\) integers from \(1\) to \(n\). These arrays are special: for all \(1 \leq i \leq n\), if we take the \(i\)-th element of each array and form another array of length \(n\) with these elements, the resultant array is also a permutation of \(n\) integers from \(1\) to \(n\). In the other words, if you put these \(n\) arrays under each other to form a matrix with \(n\) rows and \(n\) columns, this matrix is a Latin square.Afterwards, Cirno added additional \(n\) arrays, each array is a permutation of \(n\) integers from \(1\) to \(n\). For all \(1 \leq i \leq n\), there exists at least one position \(1 \leq k \leq n\), such that for the \(i\)-th array and the \((n + i)\)-th array, the \(k\)-th element of both arrays is the same. Notice that the arrays indexed from \(n + 1\) to \(2n\) don't have to form a Latin square. Also, Cirno made sure that for all \(2n\) arrays, no two arrays are completely equal, i. e. for all pair of indices \(1 \leq i < j \leq 2n\), there exists at least one position \(1 \leq k \leq n\), such that the \(k\)-th elements of the \(i\)-th and \(j\)-th array are different.Finally, Cirno arbitrarily changed the order of \(2n\) arrays.AquaMoon calls a subset of all \(2n\) arrays of size \(n\) good if these arrays from a Latin square.AquaMoon wants to know how many good subsets exist. Because this number may be particularly large, find it modulo \(998\,244\,353\). Also, she wants to find any good subset. Can you help her?
|
The input consists of multiple test cases. 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 a single integer \(n\) (\(5 \leq n \leq 500\)).Then \(2n\) lines followed. The \(i\)-th of these lines contains \(n\) integers, representing the \(i\)-th array.It is guaranteed, that the sum of \(n\) over all test cases does not exceed \(500\).
|
For each test case print two lines.In the first line, print the number of good subsets by modulo \(998\,244\,353\).In the second line, print \(n\) indices from \(1\) to \(2n\) β indices of the \(n\) arrays that form a good subset (you can print them in any order). If there are several possible answers β print any of them.
|
In the first test case, the number of good subsets is \(1\). The only such subset is the set of arrays with indices \(1\), \(2\), \(3\), \(4\), \(5\), \(6\), \(7\).In the second test case, the number of good subsets is \(2\). They are \(1\), \(3\), \(5\), \(6\), \(10\) or \(2\), \(4\), \(7\), \(8\), \(9\).
|
Input: 3 7 1 2 3 4 5 6 7 2 3 4 5 6 7 1 3 4 5 6 7 1 2 4 5 6 7 1 2 3 5 6 7 1 2 3 4 6 7 1 2 3 4 5 7 1 2 3 4 5 6 1 2 3 4 5 7 6 1 3 4 5 6 7 2 1 4 5 6 7 3 2 1 5 6 7 4 2 3 1 6 7 5 2 3 4 1 7 6 2 3 4 5 1 7 2 3 4 5 6 5 4 5 1 2 3 3 5 2 4 1 1 2 3 4 5 5 2 4 1 3 3 4 5 1 2 2 3 4 5 1 1 3 5 2 4 4 1 3 5 2 2 4 1 3 5 5 1 2 3 4 6 2 3 4 5 6 1 3 1 2 6 4 5 6 1 2 3 4 5 5 6 1 3 2 4 4 3 6 5 2 1 5 6 1 2 3 4 4 5 6 1 2 3 3 4 5 6 1 2 1 2 3 4 5 6 2 5 4 1 6 3 3 2 5 4 1 6 1 4 3 6 5 2 | Output: 1 1 2 3 4 5 6 7 2 1 3 5 6 10 4 1 3 6 7 8 9
|
Master
| 6 | 1,548 | 427 | 323 | 15 |
269 |
A
|
269A
|
A. Magical Boxes
| 1,600 |
greedy; math
|
Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes.From the top view each magical box looks like a square with side length equal to 2k (k is an integer, k β₯ 0) units. A magical box v can be put inside a magical box u, if side length of v is strictly less than the side length of u. In particular, Emuskald can put 4 boxes of side length 2k - 1 into one box of side length 2k, or as in the following figure: Emuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes.
|
The first line of input contains an integer n (1 β€ n β€ 105), the number of different sizes of boxes Emuskald has. Each of following n lines contains two integers ki and ai (0 β€ ki β€ 109, 1 β€ ai β€ 109), which means that Emuskald has ai boxes with side length 2ki. It is guaranteed that all of ki are distinct.
|
Output a single integer p, such that the smallest magical box that can contain all of Emuskaldβs boxes has side length 2p.
|
Picture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture.In the second test case, we can put all four small boxes into a box with side length 2.
|
Input: 20 31 5 | Output: 3
|
Medium
| 2 | 830 | 308 | 122 | 2 |
1,202 |
D
|
1202D
|
D. Print a 1337-string...
| 1,900 |
combinatorics; constructive algorithms; math; strings
|
The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.You are given an integer \(n\). You have to find a sequence \(s\) consisting of digits \(\{1, 3, 7\}\) such that it has exactly \(n\) subsequences equal to \(1337\).For example, sequence \(337133377\) has \(6\) subsequences equal to \(1337\): \(337\underline{1}3\underline{3}\underline{3}7\underline{7}\) (you can remove the second and fifth characters); \(337\underline{1}\underline{3}3\underline{3}7\underline{7}\) (you can remove the third and fifth characters); \(337\underline{1}\underline{3}\underline{3}37\underline{7}\) (you can remove the fourth and fifth characters); \(337\underline{1}3\underline{3}\underline{3}\underline{7}7\) (you can remove the second and sixth characters); \(337\underline{1}\underline{3}3\underline{3}\underline{7}7\) (you can remove the third and sixth characters); \(337\underline{1}\underline{3}\underline{3}3\underline{7}7\) (you can remove the fourth and sixth characters). Note that the length of the sequence \(s\) must not exceed \(10^5\).You have to answer \(t\) independent queries.
|
The first line contains one integer \(t\) (\(1 \le t \le 10\)) β the number of queries. Next \(t\) lines contains a description of queries: the \(i\)-th line contains one integer \(n_i\) (\(1 \le n_i \le 10^9\)).
|
For the \(i\)-th query print one string \(s_i\) (\(1 \le |s_i| \le 10^5\)) consisting of digits \(\{1, 3, 7\}\). String \(s_i\) must have exactly \(n_i\) subsequences \(1337\). If there are multiple such strings, print any of them.
|
Input: 2 6 1 | Output: 113337 1337
|
Hard
| 4 | 1,177 | 212 | 231 | 12 |
|
333 |
A
|
333A
|
A. Secrets
| 1,600 |
greedy
|
Gerald has been selling state secrets at leisure. All the secrets cost the same: n marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen.One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get?The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least n marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
|
The single line contains a single integer n (1 β€ n β€ 1017).Please, do not use the %lld specifier to read or write 64 bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
|
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
|
Input: 1 | Output: 1
|
Medium
| 1 | 1,151 | 206 | 102 | 3 |
1,936 |
B
|
1936B
|
B. Pinball
| 2,000 |
binary search; data structures; implementation; math; two pointers
|
There is a one-dimensional grid of length \(n\). The \(i\)-th cell of the grid contains a character \(s_i\), which is either '<' or '>'.When a pinball is placed on one of the cells, it moves according to the following rules: If the pinball is on the \(i\)-th cell and \(s_i\) is '<', the pinball moves one cell to the left in the next second. If \(s_i\) is '>', it moves one cell to the right. After the pinball has moved, the character \(s_i\) is inverted (i. e. if \(s_i\) used to be '<', it becomes '>', and vice versa). The pinball stops moving when it leaves the grid: either from the left border or from the right one. You need to answer \(n\) independent queries. In the \(i\)-th query, a pinball will be placed on the \(i\)-th cell. Note that we always place a pinball on the initial grid.For each query, calculate how many seconds it takes the pinball to leave the grid. It can be shown that the pinball will always leave the grid within a finite number of steps.
|
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 an integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line of each test case contains a string \(s_1s_2 \ldots s_{n}\) of length \(n\) consisting of characters '<' and '>'.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, for each \(i\) (\(1 \le i \le n\)) output the answer if a pinball is initially placed on the \(i\)-th cell.
|
In the first test case, the movement of the pinball for \(i=1\) is shown in the following pictures. It takes the pinball \(3\) seconds to leave the grid. The movement of the pinball for \(i=2\) is shown in the following pictures. It takes the pinball \(6\) seconds to leave the grid.
|
Input: 33><<4<<<<6<><<<> | Output: 3 6 5 1 2 3 4 1 4 7 10 8 1
|
Hard
| 5 | 972 | 472 | 127 | 19 |
1,476 |
F
|
1476F
|
F. Lanterns
| 3,000 |
binary search; data structures; dp
|
There are \(n\) lanterns in a row. The lantern \(i\) is placed in position \(i\) and has power equal to \(p_i\).Each lantern can be directed to illuminate either some lanterns to the left or some lanterns to the right. If the \(i\)-th lantern is turned to the left, it illuminates all such lanterns \(j\) that \(j \in [i - p_i, i - 1]\). Similarly, if it is turned to the right, it illuminates all such lanterns \(j\) that \(j \in [i + 1, i + p_i]\).Your goal is to choose a direction for each lantern so each lantern is illuminated by at least one other lantern, or report that it is impossible.
|
The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases.Each test case consists of two lines. The first line contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β the number of lanterns.The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(0 \le p_i \le n\)) β the power of the \(i\)-th lantern.The sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, print the answer as follows:If it is possible to direct all lanterns so that each lantern is illuminated, print YES in the first line and a string of \(n\) characters L and/or R (the \(i\)-th character is L if the \(i\)-th lantern is turned to the left, otherwise this character is R) in the second line. If there are multiple answers, you may print any of them.If there is no answer, simply print NO for that test case.
|
Input: 4 8 0 0 3 1 1 1 1 2 2 1 1 2 2 2 2 0 1 | Output: YES RRLLLLRL YES RL YES RL NO
|
Master
| 3 | 596 | 422 | 440 | 14 |
|
1,558 |
E
|
1558E
|
E. Down Below
| 3,000 |
binary search; dfs and similar; graphs; greedy; meet-in-the-middle; shortest paths
|
In a certain video game, the player controls a hero characterized by a single integer value: power.On the current level, the hero got into a system of \(n\) caves numbered from \(1\) to \(n\), and \(m\) tunnels between them. Each tunnel connects two distinct caves. Any two caves are connected with at most one tunnel. Any cave can be reached from any other cave by moving via tunnels.The hero starts the level in cave \(1\), and every other cave contains a monster.The hero can move between caves via tunnels. If the hero leaves a cave and enters a tunnel, he must finish his movement and arrive at the opposite end of the tunnel.The hero can use each tunnel to move in both directions. However, the hero can not use the same tunnel twice in a row. Formally, if the hero has just moved from cave \(i\) to cave \(j\) via a tunnel, he can not head back to cave \(i\) immediately after, but he can head to any other cave connected to cave \(j\) with a tunnel.It is known that at least two tunnels come out of every cave, thus, the hero will never find himself in a dead end even considering the above requirement.To pass the level, the hero must beat the monsters in all the caves. When the hero enters a cave for the first time, he will have to fight the monster in it. The hero can beat the monster in cave \(i\) if and only if the hero's power is strictly greater than \(a_i\). In case of beating the monster, the hero's power increases by \(b_i\). If the hero can't beat the monster he's fighting, the game ends and the player loses.After the hero beats the monster in cave \(i\), all subsequent visits to cave \(i\) won't have any consequences: the cave won't have any monsters, and the hero's power won't change either.Find the smallest possible power the hero must start the level with to be able to beat all the monsters and pass the level.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \le n \le 1000\); \(n \le m \le min(\frac{n(n-1)}{2}, 2000)\)) β the number of caves and tunnels.The second line contains \(n-1\) integers \(a_2, a_3, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β values the hero's power are compared to while fighting monsters in caves \(2, 3, \ldots, n\).The third line contains \(n-1\) integers \(b_2, b_3, \ldots, b_n\) (\(1 \le b_i \le 10^9\)) β increases applied to the hero's power for beating monsters in caves \(2, 3, \ldots, n\).Each of the next \(m\) lines contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \ne v_i\)) β the numbers of caves connected with a tunnel.No two caves are connected with more than one tunnel. Any cave can be reached from any other cave by moving via tunnels. At least two tunnels come out of every cave.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1000\), and the sum of \(m\) over all test cases does not exceed \(2000\).
|
For each test case print a single integer β the smallest possible power the hero must start the level with to be able to beat all the monsters and pass the level.
|
In the first test case, the hero can pass the level with initial power \(15\) as follows: move from cave \(1\) to cave \(2\): since \(15 > 11\), the hero beats the monster, and his power increases to \(15 + 8 = 23\); move from cave \(2\) to cave \(3\): since \(23 > 22\), the hero beats the monster, and his power increases to \(23 + 7 = 30\); move from cave \(3\) to cave \(4\): since \(30 > 13\), the hero beats the monster, and his power increases to \(30 + 5 = 35\). In the second test case, the situation is similar except that the power increases for beating monsters in caves \(2\) and \(4\) are exchanged. The hero can follow a different route, \(1 \rightarrow 4 \rightarrow 3 \rightarrow 2\), and pass the level with initial power \(15\).In the third test case, the hero can pass the level with initial power \(19\) as follows: move from cave \(1\) to cave \(2\): since \(19 > 10\), the hero beats the monster, and his power increases to \(19 + 7 = 26\); move from cave \(2\) to cave \(4\): since \(26 > 20\), the hero beats the monster, and his power increases to \(26 + 10 = 36\); move from cave \(4\) to cave \(5\): since \(36 > 30\), the hero beats the monster, and his power increases to \(36 + 5 = 41\); move from cave \(5\) to cave \(2\): there is no monster in this cave anymore, nothing happens; move from cave \(2\) to cave \(3\): since \(41 > 40\), the hero beats the monster, and his power increases to \(41 + 2 = 43\).
|
Input: 3 4 4 11 22 13 8 7 5 1 2 2 3 3 4 4 1 4 4 11 22 13 5 7 8 1 2 2 3 3 4 4 1 5 7 10 40 20 30 7 2 10 5 1 2 1 5 2 3 2 4 2 5 3 4 4 5 | Output: 15 15 19
|
Master
| 6 | 1,846 | 1,177 | 162 | 15 |
557 |
D
|
557D
|
D. Vitaly and Cycle
| 2,000 |
combinatorics; dfs and similar; graphs; math
|
After Vitaly was expelled from the university, he became interested in the graph theory.Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of n vertices and m edges, not necessarily connected, without parallel edges and loops. You need to find t β the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find w β the number of ways to add t edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.Two ways to add edges to the graph are considered equal if they have the same sets of added edges.Since Vitaly does not study at the university, he asked you to help him with this task.
|
The first line of the input contains two integers n and m ( β the number of vertices in the graph and the number of edges in the graph.Next m lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers ai, bi (1 β€ ai, bi β€ n) β the vertices that are connected by the i-th edge. All numbers in the lines are separated by a single space.It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected.
|
Print in the first line of the output two space-separated integers t and w β the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this.
|
The simple cycle is a cycle that doesn't contain any vertex twice.
|
Input: 4 41 21 34 24 3 | Output: 1 2
|
Hard
| 4 | 918 | 515 | 287 | 5 |
311 |
B
|
311B
|
B. Cats Transport
| 2,400 |
data structures; dp
|
Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There's a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1.One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want.For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units.Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
|
The first line of the input contains three integers n, m, p (2 β€ n β€ 105, 1 β€ m β€ 105, 1 β€ p β€ 100).The second line contains n - 1 positive integers d2, d3, ..., dn (1 β€ di < 104).Each of the next m lines contains two integers hi and ti (1 β€ hi β€ n, 0 β€ ti β€ 109).
|
Output an integer, the minimum sum of waiting time of all cats.Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Input: 4 6 21 3 51 02 14 91 102 103 12 | Output: 3
|
Expert
| 2 | 1,196 | 264 | 212 | 3 |
|
1,000 |
G
|
1000G
|
G. Two-Paths
| 2,700 |
data structures; dp; trees
|
You are given a weighted tree (undirected connected graph with no cycles, loops or multiple edges) with \(n\) vertices. The edge \(\{u_j, v_j\}\) has weight \(w_j\). Also each vertex \(i\) has its own value \(a_i\) assigned to it.Let's call a path starting in vertex \(u\) and ending in vertex \(v\), where each edge can appear no more than twice (regardless of direction), a 2-path. Vertices can appear in the 2-path multiple times (even start and end vertices).For some 2-path \(p\) profit \(\text{Pr}(p) = \sum\limits_{v \in \text{distinct vertices in } p}{a_v} - \sum\limits_{e \in \text{distinct edges in } p}{k_e \cdot w_e}\), where \(k_e\) is the number of times edge \(e\) appears in \(p\). That is, vertices are counted once, but edges are counted the number of times they appear in \(p\).You are about to answer \(m\) queries. Each query is a pair of vertices \((qu, qv)\). For each query find 2-path \(p\) from \(qu\) to \(qv\) with maximal profit \(\text{Pr}(p)\).
|
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 3 \cdot 10^5\), \(1 \le q \le 4 \cdot 10^5\)) β the number of vertices in the tree and the number of queries.The second line contains \(n\) space-separated integers \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 10^9)\) β the values of the vertices.Next \(n - 1\) lines contain descriptions of edges: each line contains three space separated integers \(u_i\), \(v_i\) and \(w_i\) (\(1 \le u_i, v_i \le n\), \(u_i \neq v_i\), \(1 \le w_i \le 10^9\)) β there is edge \(\{u_i, v_i\}\) with weight \(w_i\) in the tree.Next \(q\) lines contain queries (one per line). Each query contains two integers \(qu_i\) and \(qv_i\) \((1 \le qu_i, qv_i \le n)\) β endpoints of the 2-path you need to find.
|
For each query print one integer per line β maximal profit \(\text{Pr}(p)\) of the some 2-path \(p\) with the corresponding endpoints.
|
Explanation of queries: \((1, 1)\) β one of the optimal 2-paths is the following: \(1 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1\). \(\text{Pr}(p) = (a_1 + a_2 + a_3 + a_4 + a_5) - (2 \cdot w(1,2) + 2 \cdot w(2,3) + 2 \cdot w(2,4) + 2 \cdot w(4,5)) = 21 - 2 \cdot 12 = 9\). \((4, 4)\): \(4 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 4\). \(\text{Pr}(p) = (a_1 + a_2 + a_3 + a_4) - 2 \cdot (w(1,2) + w(2,3) + w(2,4)) = 19 - 2 \cdot 10 = 9\). \((5, 6)\): \(5 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 4 \rightarrow 6\). \((6, 4)\): \(6 \rightarrow 4 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 2 \rightarrow 4\). \((3, 4)\): \(3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 4\). \((3, 7)\): \(3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 4 \rightarrow 5 \rightarrow 4 \rightarrow 2 \rightarrow 3 \rightarrow 7\).
|
Input: 7 66 5 5 3 2 1 21 2 22 3 22 4 14 5 16 4 27 3 251 14 45 66 43 43 7 | Output: 999812-14
|
Master
| 3 | 976 | 753 | 134 | 10 |
626 |
F
|
626F
|
F. Group Projects
| 2,400 |
dp
|
There are n students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the i-th student ai minutes to finish his/her independent piece.If students work at different paces, it can be frustrating for the faster students and stressful for the slower ones. In particular, the imbalance of a group is defined as the maximum ai in the group minus the minimum ai in the group. Note that a group containing a single student has an imbalance of 0. How many ways are there for the students to divide into groups so that the total imbalance of all groups is at most k?Two divisions are considered distinct if there exists a pair of students who work in the same group in one division but different groups in the other.
|
The first line contains two space-separated integers n and k (1 β€ n β€ 200, 0 β€ k β€ 1000) β the number of students and the maximum total imbalance allowed, respectively.The second line contains n space-separated integers ai (1 β€ ai β€ 500) β the time it takes the i-th student to complete his/her independent piece of work.
|
Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109 + 7.
|
In the first sample, we have three options: The first and second students form a group, and the third student forms a group. Total imbalance is 2 + 0 = 2. The first student forms a group, and the second and third students form a group. Total imbalance is 0 + 1 = 1. All three students form their own groups. Total imbalance is 0. In the third sample, the total imbalance must be 0, so each student must work individually.
|
Input: 3 22 4 5 | Output: 3
|
Expert
| 1 | 854 | 321 | 132 | 6 |
1,997 |
B
|
1997B
|
B. Make Three Regions
| 1,100 |
constructive algorithms; two pointers
|
There is a grid, consisting of \(2\) rows and \(n\) columns. Each cell of the grid is either free or blocked.A free cell \(y\) is reachable from a free cell \(x\) if at least one of these conditions holds: \(x\) and \(y\) share a side; there exists a free cell \(z\) such that \(z\) is reachable from \(x\) and \(y\) is reachable from \(z\). A connected region is a set of free cells of the grid such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.For example, consider the following layout, where white cells are free, and dark grey cells are blocked: There are \(3\) regions in it, denoted with red, green and blue color respectively: The given grid contains at most \(1\) connected region. Your task is to calculate the number of free cells meeting the following constraint: if this cell is blocked, the number of connected regions becomes exactly \(3\).
|
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 number of columns.The \(i\)-th of the next two lines contains a description of the \(i\)-th row of the grid β the string \(s_i\), consisting of \(n\) characters. Each character is either . (denoting a free cell) or x (denoting a blocked cell). Additional constraint on the input: the given grid contains at most \(1\) connected region; the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the number of cells such that the number of connected regions becomes \(3\) if this cell is blocked.
|
In the first test case, if the cell \((1, 3)\) is blocked, the number of connected regions becomes \(3\) (as shown in the picture from the statement).
|
Input: 48.......x.x.xx...2....3xxxxxx9..x.x.x.xx.......x | Output: 1 0 0 2
|
Easy
| 2 | 924 | 604 | 145 | 19 |
1,264 |
E
|
1264E
|
E. Beautiful League
| 2,700 |
constructive algorithms; flows; graph matchings
|
A football league has recently begun in Beautiful land. There are \(n\) teams participating in the league. Let's enumerate them with integers from \(1\) to \(n\).There will be played exactly \(\frac{n(n-1)}{2}\) matches: each team will play against all other teams exactly once. In each match, there is always a winner and loser and there is no draw.After all matches are played, the organizers will count the number of beautiful triples. Let's call a triple of three teams \((A, B, C)\) beautiful if a team \(A\) win against a team \(B\), a team \(B\) win against a team \(C\) and a team \(C\) win against a team \(A\). We look only to a triples of different teams and the order of teams in the triple is important.The beauty of the league is the number of beautiful triples.At the moment, \(m\) matches were played and their results are known.What is the maximum beauty of the league that can be, after playing all remaining matches? Also find a possible results for all remaining \(\frac{n(n-1)}{2} - m\) matches, so that the league has this maximum beauty.
|
The first line contains two integers \(n, m\) (\(3 \leq n \leq 50, 0 \leq m \leq \frac{n(n-1)}{2}\)) β the number of teams in the football league and the number of matches that were played.Each of \(m\) following lines contains two integers \(u\) and \(v\) (\(1 \leq u, v \leq n\), \(u \neq v\)) denoting that the \(u\)-th team won against the \(v\)-th team. It is guaranteed that each unordered pair of teams appears at most once.
|
Print \(n\) lines, each line having a string of exactly \(n\) characters. Each character must be either \(0\) or \(1\).Let \(a_{ij}\) be the \(j\)-th number in the \(i\)-th line. For all \(1 \leq i \leq n\) it should be true, that \(a_{ii} = 0\). For all pairs of teams \(i \neq j\) the number \(a_{ij}\) indicates the result of the match between the \(i\)-th team and the \(j\)-th team: If \(a_{ij}\) is \(1\), the \(i\)-th team wins against the \(j\)-th team; Otherwise the \(j\)-th team wins against the \(i\)-th team; Also, it should be true, that \(a_{ij} + a_{ji} = 1\). Also note that the results of the \(m\) matches that were already played cannot be changed in your league.The beauty of the league in the output should be maximum possible. If there are multiple possible answers with maximum beauty, you can print any of them.
|
The beauty of league in the first test case is equal to \(3\) because there exists three beautiful triples: \((1, 2, 3)\), \((2, 3, 1)\), \((3, 1, 2)\).The beauty of league in the second test is equal to \(6\) because there exists six beautiful triples: \((1, 2, 4)\), \((2, 4, 1)\), \((4, 1, 2)\), \((2, 4, 3)\), \((4, 3, 2)\), \((3, 2, 4)\).
|
Input: 3 1 1 2 | Output: 010 001 100
|
Master
| 3 | 1,060 | 431 | 836 | 12 |
508 |
E
|
508E
|
E. Arthur and Brackets
| 2,200 |
dp; greedy
|
Notice that the memory limit is non-standard.Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2n. Unlike Arthur, Sasha understood the topic very badly, and broke Arthur's favorite correct bracket sequence just to spite him.All Arthur remembers about his favorite sequence is for each opening parenthesis ('(') the approximate distance to the corresponding closing one (')'). For the i-th opening bracket he remembers the segment [li, ri], containing the distance to the corresponding closing bracket.Formally speaking, for the i-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [li, ri].Help Arthur restore his favorite correct bracket sequence!
|
The first line contains integer n (1 β€ n β€ 600), the number of opening brackets in Arthur's favorite correct bracket sequence. Next n lines contain numbers li and ri (1 β€ li β€ ri < 2n), representing the segment where lies the distance from the i-th opening bracket and the corresponding closing one. The descriptions of the segments are given in the order in which the opening brackets occur in Arthur's favorite sequence if we list them from left to right.
|
If it is possible to restore the correct bracket sequence by the given data, print any possible choice.If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line ""IMPOSSIBLE"" (without the quotes).
|
Input: 41 11 11 11 1 | Output: ()()()()
|
Hard
| 2 | 945 | 457 | 257 | 5 |
|
408 |
B
|
408B
|
B. Garland
| 1,200 |
implementation
|
Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of ββm pieces of paper in the garland. Calculate what the maximum total area of ββthe pieces of paper in the garland Vasya can get.
|
The first line contains a non-empty sequence of n (1 β€ n β€ 1000) small English letters (""a""...""z""). Each letter means that Vasya has a sheet of paper of the corresponding color.The second line contains a non-empty sequence of m (1 β€ m β€ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.
|
Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.
|
In the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.In the second test sample Vasya cannot make a garland at all β he doesn't have a sheet of color z.
|
Input: aaabbacaabbccac | Output: 6
|
Easy
| 1 | 725 | 362 | 241 | 4 |
1,001 |
D
|
1001D
|
D. Distinguish plus state and minus state
| 1,400 |
*special
|
You are given a qubit which is guaranteed to be either in or in state. Your task is to perform necessary operations and measurements to figure out which state it was and to return 1 if it was a state or -1 if it was state. The state of the qubit after the operations does not matter.
|
You have to implement an operation which takes a qubit as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (q : Qubit) : Int { body { // your code here } }}
|
Easy
| 1 | 283 | 287 | 0 | 10 |
|||
263 |
A
|
263A
|
A. Beautiful Matrix
| 800 |
implementation
|
You've got a 5 Γ 5 matrix, consisting of 24 zeroes and a single number one. Let's index the matrix rows by numbers from 1 to 5 from top to bottom, let's index the matrix columns by numbers from 1 to 5 from left to right. In one move, you are allowed to apply one of the two following transformations to the matrix: Swap two neighboring matrix rows, that is, rows with indexes i and i + 1 for some integer i (1 β€ i < 5). Swap two neighboring matrix columns, that is, columns with indexes j and j + 1 for some integer j (1 β€ j < 5). You think that a matrix looks beautiful, if the single number one of the matrix is located in its middle (in the cell that is on the intersection of the third row and the third column). Count the minimum number of moves needed to make the matrix beautiful.
|
The input consists of five lines, each line contains five integers: the j-th integer in the i-th line of the input represents the element of the matrix that is located on the intersection of the i-th row and the j-th column. It is guaranteed that the matrix consists of 24 zeroes and a single number one.
|
Print a single integer β the minimum number of moves needed to make the matrix beautiful.
|
Input: 0 0 0 0 00 0 0 0 10 0 0 0 00 0 0 0 00 0 0 0 0 | Output: 3
|
Beginner
| 1 | 787 | 304 | 89 | 2 |
|
1,452 |
G
|
1452G
|
G. Game On Tree
| 2,700 |
data structures; dfs and similar; greedy; trees
|
Alice and Bob are playing a game. They have a tree consisting of \(n\) vertices. Initially, Bob has \(k\) chips, the \(i\)-th chip is located in the vertex \(a_i\) (all these vertices are unique). Before the game starts, Alice will place a chip into one of the vertices of the tree.The game consists of turns. Each turn, the following events happen (sequentially, exactly in the following order): Alice either moves her chip to an adjacent vertex or doesn't move it; for each Bob's chip, he either moves it to an adjacent vertex or doesn't move it. Note that this choice is done independently for each chip. The game ends when Alice's chip shares the same vertex with one (or multiple) of Bob's chips. Note that Bob's chips may share the same vertex, even though they are in different vertices at the beginning of the game.Alice wants to maximize the number of turns, Bob wants to minimize it. If the game ends in the middle of some turn (Alice moves her chip to a vertex that contains one or multiple Bob's chips), this turn is counted.For each vertex, calculate the number of turns the game will last if Alice places her chip in that vertex.
|
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.Then \(n - 1\) lines follow, each line contains two integers \(u_i\), \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \ne v_i\)) that denote the endpoints of an edge. These edges form a tree.The next line contains one integer \(k\) (\(1 \le k \le n - 1\)) β the number of Bob's chips.The last line contains \(k\) integers \(a_1\), \(a_2\), ..., \(a_k\) (\(1 \le a_i \le n\); \(a_i \ne a_j\) if \(i \ne j\)) β the vertices where the Bob's chips are initially placed.
|
Print \(n\) integers. The \(i\)-th of them should be equal to the number of turns the game will last if Alice initially places her chip in the vertex \(i\). If one of Bob's chips is already placed in vertex \(i\), then the answer for vertex \(i\) is \(0\).
|
Input: 5 2 4 3 1 3 4 3 5 2 4 5 | Output: 2 1 2 0 0
|
Master
| 4 | 1,143 | 568 | 256 | 14 |
|
597 |
A
|
597A
|
A. Divisibility
| 1,600 |
math
|
Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a β€ x β€ b and x is divisible by k.
|
The only line contains three space-separated integers k, a and b (1 β€ k β€ 1018; - 1018 β€ a β€ b β€ 1018).
|
Print the required number.
|
Input: 1 1 10 | Output: 10
|
Medium
| 1 | 169 | 103 | 26 | 5 |
|
120 |
D
|
120D
|
D. Three Sons
| 1,400 |
brute force
|
Three sons inherited from their father a rectangular corn fiend divided into n Γ m squares. For each square we know how many tons of corn grows on it. The father, an old farmer did not love all three sons equally, which is why he bequeathed to divide his field into three parts containing A, B and C tons of corn.The field should be divided by two parallel lines. The lines should be parallel to one side of the field and to each other. The lines should go strictly between the squares of the field. Each resulting part of the field should consist of at least one square. Your task is to find the number of ways to divide the field as is described above, that is, to mark two lines, dividing the field in three parts so that on one of the resulting parts grew A tons of corn, B on another one and C on the remaining one.
|
The first line contains space-separated integers n and m β the sizes of the original (1 β€ n, m β€ 50, max(n, m) β₯ 3). Then the field's description follows: n lines, each containing m space-separated integers cij, (0 β€ cij β€ 100) β the number of tons of corn each square contains. The last line contains space-separated integers A, B, C (0 β€ A, B, C β€ 106).
|
Print the answer to the problem: the number of ways to divide the father's field so that one of the resulting parts contained A tons of corn, another one contained B tons, and the remaining one contained C tons. If no such way exists, print 0.
|
The lines dividing the field can be horizontal or vertical, but they should be parallel to each other.
|
Input: 3 31 1 11 1 11 1 13 3 3 | Output: 2
|
Easy
| 1 | 820 | 355 | 243 | 1 |
1,379 |
D
|
1379D
|
D. New Passenger Trams
| 2,300 |
binary search; brute force; data structures; sortings; two pointers
|
There are many freight trains departing from Kirnes planet every day. One day on that planet consists of \(h\) hours, and each hour consists of \(m\) minutes, where \(m\) is an even number. Currently, there are \(n\) freight trains, and they depart every day at the same time: \(i\)-th train departs at \(h_i\) hours and \(m_i\) minutes.The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at \(0\) hours and \(t\) minutes, where \(0 \le t < {m \over 2}\), the second tram departs \(m \over 2\) minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.To allow passengers to board the tram safely, the tram must arrive \(k\) minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at \(0\) hours and \(t\) minutes, where \(t < k\), then the freight trains can not depart during the last \(k - t\) minutes of the day. A schematic picture of the correct way to run passenger trams. Here \(h=2\) (therefore, the number of passenger trams is \(2h=4\)), the number of freight trains is \(n=6\). The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length \(k\) before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments. Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of \(t\) to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.
|
The first line of input contains four integers \(n\), \(h\), \(m\), \(k\) (\(1 \le n \le 100\,000\), \(1 \le h \le 10^9\), \(2 \le m \le 10^9\), \(m\) is even, \(1 \le k \le {m \over 2}\)) β the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.\(n\) lines follow, each contains two integers \(h_i\) and \(m_i\) (\(0 \le h_i < h\), \(0 \le m_i < m\)) β the time when \(i\)-th freight train departs. It is guaranteed that no freight trains depart at the same time.
|
The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time \(t\). Second line of output should contain freight trains that need to be canceled.
|
In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if \(t \in [1, 15]\), then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If \(t = 0\) or \(t \in [16, 29]\), then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose \(t = 0\). Another possible option is to cancel the first train and choose \(t = 13\).
|
Input: 2 24 60 15 16 0 17 15 | Output: 0 0
|
Expert
| 5 | 2,003 | 540 | 223 | 13 |
2,061 |
F2
|
2061F2
|
F2. Kevin and Binary String (Hard Version)
| 3,500 |
data structures; dp
|
This is the hard version of the problem. The difference between the versions is that in this version, string \(t\) consists of '0', '1' and '?'. You can hack only if you solved all versions of this problem. Kevin has a binary string \(s\) of length \(n\). Kevin can perform the following operation: Choose two adjacent blocks of \(s\) and swap them. A block is a maximal substring\(^{\text{β}}\) of identical characters. Formally, denote \(s[l,r]\) as the substring \(s_l s_{l+1} \ldots s_r\). A block is \(s[l,r]\) satisfying: \(l=1\) or \(s_l\not=s_{l-1}\). \(s_l=s_{l+1} = \ldots = s_{r}\). \(r=n\) or \(s_r\not=s_{r+1}\). Adjacent blocks are two blocks \(s[l_1,r_1]\) and \(s[l_2,r_2]\) satisfying \(r_1+1=l_2\).For example, if \(s=\mathtt{000}\,\mathbf{11}\,\mathbf{00}\,\mathtt{111}\), Kevin can choose the two blocks \(s[4,5]\) and \(s[6,7]\) and swap them, transforming \(s\) into \(\mathtt{000}\,\mathbf{00}\,\mathbf{11}\,\mathtt{111}\).Given a string \(t\) of length \(n\) consisting of '0', '1' and '?', Kevin wants to determine the minimum number of operations required to perform such that for any index \(i\) (\(1\le i\le n\)), if \(t_i\not=\) '?' then \(s_i=t_i\). If it is impossible, output \(-1\).\(^{\text{β}}\)A string \(a\) is a substring of a string \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters 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 a string \(s\) consisting of '0' and '1'.The second line of each test case contains a string \(t\) consisting of '0', '1' and '?'.It is guaranteed that the lengths of \(s\) and \(t\) are the same.It is guaranteed that the sum of the length of \(s\) over all test cases will not exceed \(4\cdot 10^5\).
|
For each test case, output one integer β the minimum number of operations required. If it is impossible, output \(-1\).
|
In the first test case of the first example, the possible way is shown in the statement.In the second test case of the first example, one possible way could be: Swap blocks \([2, 2], [3, 3]\), \(s\) will become \(\mathtt{001101}\). Swap blocks \([3, 4], [5, 5]\), \(s\) will become \(\mathtt{000111}\). Swap blocks \([1, 3], [4, 6]\), \(s\) will become \(\mathtt{111000}\). In the first test case of the second example, one possible way could be: Swap blocks \([1, 1], [2, 2]\), \(s\) will become \(\mathtt{100101}\). Swap blocks \([4, 4], [5, 5]\), \(s\) will become \(\mathtt{100011}\).
|
Input: 600011001110000011111010101111000010101100101101001100100111001 | Output: 1 3 1 -1 -1 -1
|
Master
| 2 | 1,455 | 505 | 119 | 20 |
1,763 |
A
|
1763A
|
A. Absolute Maximization
| 800 |
bitmasks; constructive algorithms; greedy; math
|
You are given an array \(a\) of length \(n\). You can perform the following operation several (possibly, zero) times: Choose \(i\), \(j\), \(b\): Swap the \(b\)-th digit in the binary representation of \(a_i\) and \(a_j\). Find the maximum possible value of \(\max(a) - \min(a)\).In a binary representation, bits are numbered from right (least significant) to left (most significant). Consider that there are an infinite number of leading zero bits at the beginning of any binary representation.For example, swap the \(0\)-th bit for \(4=100_2\) and \(3=11_2\) will result \(101_2=5\) and \(10_2=2\). Swap the \(2\)-nd bit for \(4=100_2\) and \(3=11_2\) will result \(000_2=0_2=0\) and \(111_2=7\). Here, \(\max(a)\) denotes the maximum element of array \(a\) and \(\min(a)\) denotes the minimum element of array \(a\).The binary representation of \(x\) is \(x\) written in base \(2\). For example, \(9\) and \(6\) written in base \(2\) are \(1001\) and \(110\), respectively.
|
The first line contains a single integer \(t\) (\(1 \le t \le 128\)) β the number of testcases.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 512\)) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < 1024\)) β the elements of array \(a\).It's guaranteed that the sum of \(n\) over all testcases does not exceed \(512\).
|
For each testcase, print one integer β the maximum possible value of \(\max(a) - \min(a)\).
|
In the first example, it can be shown that we do not need to perform any operations β the maximum value of \(\max(a) - \min(a)\) is \(1 - 0 = 1\).In the second example, no operation can change the array β the maximum value of \(\max(a) - \min(a)\) is \(5 - 5 = 0\).In the third example, initially \(a = [1, 2, 3, 4, 5]\), we can perform one operation taking \(i = 2\), \(j = 5\), \(b = 1\). The array now becomes \(a = [1, 0, 3, 4, 7]\). It can be shown that any further operations do not lead to a better answer β therefore the answer is \(\max(a) - \min(a) = 7 - 0 = 7\).
|
Input: 431 0 145 5 5 551 2 3 4 5720 85 100 41 76 49 36 | Output: 1 0 7 125
|
Beginner
| 4 | 976 | 428 | 91 | 17 |
793 |
A
|
793A
|
A. Oleg and shares
| 900 |
implementation; math
|
Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?
|
The first line contains two integers n and k (1 β€ n β€ 105, 1 β€ k β€ 109) β the number of share prices, and the amount of rubles some price decreases each second.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the initial prices.
|
Print the only line containing the minimum number of seconds needed for prices to become equal, of Β«-1Β» if it is impossible.
|
Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal.In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.
|
Input: 3 312 9 15 | Output: 3
|
Beginner
| 2 | 571 | 248 | 124 | 7 |
1,991 |
D
|
1991D
|
D. Prime XOR Coloring
| 1,900 |
bitmasks; constructive algorithms; graphs; greedy; math; number theory
|
You are given an undirected graph with \(n\) vertices, numbered from \(1\) to \(n\). There is an edge between vertices \(u\) and \(v\) if and only if \(u \oplus v\) is a prime number, where \(\oplus\) denotes the bitwise XOR operator.Color all vertices of the graph using the minimum number of colors, such that no two vertices directly connected by an edge have the same color.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of test cases follows.The only line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of vertices in the graph.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output two lines.The first line should contain a single integer \(k\) (\(1 \le k \le n\)) β the minimum number of colors required.The second line should contain \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le k\)) β the color of each vertex.If there are multiple solutions, output any of them.
|
In the first test case, the minimum number of colors is \(1\), because there is only one vertex.In the second test case, the minimum number of colors is \(2\), because there is an edge connecting \(1\) and \(2\) (\(1 \oplus 2 = 3\), which is a prime number).In the third test case, the minimum number of colors is still \(2\), because \(2\) and \(3\) can be colored the same since there is no edge between \(2\) and \(3\) (\(2 \oplus 3 = 1\), which is not a prime number).In the fourth test case, it can be shown that the minimum number of colors is \(3\).In the fifth test case, it can be shown that the minimum number of colors is \(3\).In the sixth test case, it can be shown that the minimum number of colors is \(4\).
|
Input: 6123456 | Output: 1 1 2 1 2 2 1 2 2 3 1 2 2 3 3 1 2 2 3 3 4 1 2 2 3 3 4
|
Hard
| 6 | 378 | 363 | 323 | 19 |
570 |
E
|
570E
|
E. Pig and Palindromes
| 2,300 |
combinatorics; dp
|
Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of n rows and m columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to n, and the columns β from left to right with numbers from 1 to m. Let's denote the cell at the intersection of the r-th row and the c-th column as (r, c).Initially the pig stands in cell (1, 1), and in the end she wants to be in cell (n, m). Since the pig is in a hurry to get home, she can go from cell (r, c), only to either cell (r + 1, c) or (r, c + 1). She cannot leave the forest.The forest, where the pig is, is very unusual. Some cells of the forest similar to each other, and some look very different. Peppa enjoys taking pictures and at every step she takes a picture of the cell where she is now. The path through the forest is considered to be beautiful if photographs taken on her way, can be viewed in both forward and in reverse order, showing the same sequence of photos. More formally, the line formed by the cells in order of visiting should be a palindrome (you can read a formal definition of a palindrome in the previous problem).Count the number of beautiful paths from cell (1, 1) to cell (n, m). Since this number can be very large, determine the remainder after dividing it by 109 + 7.
|
The first line contains two integers n, m (1 β€ n, m β€ 500) β the height and width of the field.Each of the following n lines contains m lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by different letters.
|
Print a single integer β the number of beautiful paths modulo 109 + 7.
|
Picture illustrating possibilities for the sample test.
|
Input: 3 4aaabbaaaabba | Output: 3
|
Expert
| 2 | 1,350 | 315 | 70 | 5 |
1,039 |
E
|
1039E
|
E. Summer Oenothera Exhibition
| 3,400 |
data structures
|
While some people enjoy spending their time solving programming contests, Dina prefers taking beautiful pictures. As soon as Byteland Botanical Garden announced Summer Oenothera Exhibition she decided to test her new camera there.The exhibition consists of \(l = 10^{100}\) Oenothera species arranged in a row and consecutively numbered with integers from \(0\) to \(l - 1\). Camera lens allows to take a photo of \(w\) species on it, i.e. Dina can take a photo containing flowers with indices from \(x\) to \(x + w - 1\) for some integer \(x\) between \(0\) and \(l - w\). We will denote such photo with \([x, x + w - 1]\).She has taken \(n\) photos, the \(i\)-th of which (in chronological order) is \([x_i, x_i + w - 1]\) in our notation. She decided to build a time-lapse video from these photos once she discovered that Oenothera blossoms open in the evening. Dina takes each photo and truncates it, leaving its segment containing exactly \(k\) flowers, then she composes a video of these photos keeping their original order and voilΓ , a beautiful artwork has been created!A scene is a contiguous sequence of photos such that the set of flowers on them is the same. The change between two scenes is called a cut. For example, consider the first photo contains flowers \([1, 5]\), the second photo contains flowers \([3, 7]\) and the third photo contains flowers \([8, 12]\). If \(k = 3\), then Dina can truncate the first and the second photo into \([3, 5]\), and the third photo into \([9, 11]\). First two photos form a scene, third photo also forms a scene and the transition between these two scenes which happens between the second and the third photos is a cut. If \(k = 4\), then each of the transitions between photos has to be a cut.Dina wants the number of cuts to be as small as possible. Please help her! Calculate the minimum possible number of cuts for different values of \(k\).
|
The first line contains three positive integer \(n\), \(w\), \(q\) (\(1 \leq n, q \leq 100\,000\), \(1 \leq w \leq 10^9\)) β the number of taken photos, the number of flowers on a single photo and the number of queries.Next line contains \(n\) non-negative integers \(x_i\) (\(0 \le x_i \le 10^9\)) β the indices of the leftmost flowers on each of the photos.Next line contains \(q\) positive integers \(k_i\) (\(1 \le k_i \le w\)) β the values of \(k\) for which you have to solve the problem.It's guaranteed that all \(k_i\) are distinct.
|
Print \(q\) integers β for each width of the truncated photo \(k_i\), the minimum number of cuts that is possible.
|
Input: 3 6 52 4 01 2 3 4 5 | Output: 00112
|
Master
| 1 | 1,898 | 540 | 114 | 10 |
|
1,559 |
C
|
1559C
|
C. Mocha and Hiking
| 1,200 |
constructive algorithms; graphs
|
The city where Mocha lives in is called Zhijiang. There are \(n+1\) villages and \(2n-1\) directed roads in this city. There are two kinds of roads: \(n-1\) roads are from village \(i\) to village \(i+1\), for all \(1\leq i \leq n-1\). \(n\) roads can be described by a sequence \(a_1,\ldots,a_n\). If \(a_i=0\), the \(i\)-th of these roads goes from village \(i\) to village \(n+1\), otherwise it goes from village \(n+1\) to village \(i\), for all \(1\leq i\leq n\). Mocha plans to go hiking with Taki this weekend. To avoid the trip being boring, they plan to go through every village exactly once. They can start and finish at any villages. Can you help them to draw up a plan?
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 20\)) β the number of test cases. Each test case consists of two lines.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^4\)) β indicates that the number of villages is \(n+1\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 1\)). If \(a_i=0\), it means that there is a road from village \(i\) to village \(n+1\). If \(a_i=1\), it means that there is a road from village \(n+1\) to village \(i\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^4\).
|
For each test case, print a line with \(n+1\) integers, where the \(i\)-th number is the \(i\)-th village they will go through. If the answer doesn't exist, print \(-1\).If there are multiple correct answers, you can print any one of them.
|
In the first test case, the city looks like the following graph:So all possible answers are \((1 \to 4 \to 2 \to 3)\), \((1 \to 2 \to 3 \to 4)\).In the second test case, the city looks like the following graph:So all possible answers are \((4 \to 1 \to 2 \to 3)\), \((1 \to 2 \to 3 \to 4)\), \((3 \to 4 \to 1 \to 2)\), \((2 \to 3 \to 4 \to 1)\).
|
Input: 2 3 0 1 0 3 1 1 0 | Output: 1 4 2 3 4 1 2 3
|
Easy
| 2 | 681 | 668 | 239 | 15 |
1,111 |
D
|
1111D
|
D. Destroy the Colony
| 2,600 |
combinatorics; dp; math
|
There is a colony of villains with several holes aligned in a row, where each hole contains exactly one villain.Each colony arrangement can be expressed as a string of even length, where the \(i\)-th character of the string represents the type of villain in the \(i\)-th hole. Iron Man can destroy a colony only if the colony arrangement is such that all villains of a certain type either live in the first half of the colony or in the second half of the colony.His assistant Jarvis has a special power. It can swap villains of any two holes, i.e. swap any two characters in the string; he can do this operation any number of times.Now Iron Man asks Jarvis \(q\) questions. In each question, he gives Jarvis two numbers \(x\) and \(y\). Jarvis has to tell Iron Man the number of distinct colony arrangements he can create from the original one using his powers such that all villains having the same type as those originally living in \(x\)-th hole or \(y\)-th hole live in the same half and the Iron Man can destroy that colony arrangement.Two colony arrangements are considered to be different if there exists a hole such that different types of villains are present in that hole in the arrangements.
|
The first line contains a string \(s\) (\(2 \le |s| \le 10^{5}\)), representing the initial colony arrangement. String \(s\) can have both lowercase and uppercase English letters and its length is even.The second line contains a single integer \(q\) (\(1 \le q \le 10^{5}\)) β the number of questions.The \(i\)-th of the next \(q\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le |s|\), \(x_i \ne y_i\)) β the two numbers given to the Jarvis for the \(i\)-th question.
|
For each question output the number of arrangements possible modulo \(10^9+7\).
|
Consider the first example. For the first question, the possible arrangements are ""aabb"" and ""bbaa"", and for the second question, index \(1\) contains 'a' and index \(2\) contains 'b' and there is no valid arrangement in which all 'a' and 'b' are in the same half.
|
Input: abba 2 1 4 1 2 | Output: 2 0
|
Expert
| 3 | 1,202 | 490 | 79 | 11 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.