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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
919 |
C
|
919C
|
C. Seat Arrangements
| 1,300 |
brute force; implementation
|
Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied.The classroom contains \(n\) rows of seats and there are \(m\) seats in each row. Then the classroom can be represented as an \(n \times m\) matrix. The character '.' represents an empty seat, while '*' means that the seat is occupied. You need to find \(k\) consecutive empty seats in the same row or column and arrange those seats for you and your friends. Your task is to find the number of ways to arrange the seats. Two ways are considered different if sets of places that students occupy differs.
|
The first line contains three positive integers \(n,m,k\) (\(1 \leq n, m, k \leq 2\,000\)), where \(n,m\) represent the sizes of the classroom and \(k\) is the number of consecutive seats you need to find.Each of the next \(n\) lines contains \(m\) characters '.' or '*'. They form a matrix representing the classroom, '.' denotes an empty seat, and '*' denotes an occupied seat.
|
A single number, denoting the number of ways to find \(k\) empty seats in the same row or column.
|
In the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement. \((1,3)\), \((2,3)\) \((2,2)\), \((2,3)\) \((2,1)\), \((2,2)\)
|
Input: 2 3 2**.... | Output: 3
|
Easy
| 2 | 781 | 379 | 97 | 9 |
1,690 |
E
|
1690E
|
E. Price Maximization
| 1,500 |
binary search; greedy; math; two pointers
|
A batch of \(n\) goods (\(n\) β an even number) is brought to the store, \(i\)-th of which has weight \(a_i\). Before selling the goods, they must be packed into packages. After packing, the following will be done: There will be \(\frac{n}{2}\) packages, each package contains exactly two goods; The weight of the package that contains goods with indices \(i\) and \(j\) (\(1 \le i, j \le n\)) is \(a_i + a_j\). With this, the cost of a package of weight \(x\) is always \(\left \lfloor\frac{x}{k}\right\rfloor\) burles (rounded down), where \(k\) β a fixed and given value.Pack the goods to the packages so that the revenue from their sale is maximized. In other words, make such \(\frac{n}{2}\) pairs of given goods that the sum of the values \(\left \lfloor\frac{x_i}{k} \right \rfloor\), where \(x_i\) is the weight of the package number \(i\) (\(1 \le i \le \frac{n}{2}\)), is maximal.For example, let \(n = 6, k = 3\), weights of goods \(a = [3, 2, 7, 1, 4, 8]\). Let's pack them into the following packages. In the first package we will put the third and sixth goods. Its weight will be \(a_3 + a_6 = 7 + 8 = 15\). The cost of the package will be \(\left \lfloor\frac{15}{3}\right\rfloor = 5\) burles. In the second package put the first and fifth goods, the weight is \(a_1 + a_5 = 3 + 4 = 7\). The cost of the package is \(\left \lfloor\frac{7}{3}\right\rfloor = 2\) burles. In the third package put the second and fourth goods, the weight is \(a_2 + a_4 = 2 + 1 = 3\). The cost of the package is \(\left \lfloor\frac{3}{3}\right\rfloor = 1\) burle. With this packing, the total cost of all packs would be \(5 + 2 + 1 = 8\) burles.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) βthe number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) (\(2 \le n \le 2\cdot10^5\)) and \(k\) (\(1 \le k \le 1000\)). The number \(n\) β is even.The second line of each test case contains exactly \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^9\)).It is guaranteed that the sum of \(n\) over all the test cases does not exceed \(2\cdot10^5\).
|
For each test case, print on a separate line a single number β the maximum possible total cost of all the packages.
|
The first test case is analyzed in the statement.In the second test case, you can get a total value equal to \(4\) if you put the first and second goods in the first package and the third and fourth goods in the second package.In the third test case, the cost of each item is \(0\), so the total cost will also be \(0\).
|
Input: 66 33 2 7 1 4 84 32 1 5 64 120 0 0 02 11 16 102 0 0 5 9 46 55 3 8 6 3 2 | Output: 8 4 0 2 1 5
|
Medium
| 4 | 1,640 | 518 | 115 | 16 |
1,316 |
E
|
1316E
|
E. Team Building
| 2,300 |
bitmasks; dp; greedy; sortings
|
Alice, the president of club FCB, wants to build a team for the new volleyball tournament. The team should consist of \(p\) players playing in \(p\) different positions. She also recognizes the importance of audience support, so she wants to select \(k\) people as part of the audience.There are \(n\) people in Byteland. Alice needs to select exactly \(p\) players, one for each position, and exactly \(k\) members of the audience from this pool of \(n\) people. Her ultimate goal is to maximize the total strength of the club.The \(i\)-th of the \(n\) persons has an integer \(a_{i}\) associated with him β the strength he adds to the club if he is selected as a member of the audience.For each person \(i\) and for each position \(j\), Alice knows \(s_{i, j}\) β the strength added by the \(i\)-th person to the club if he is selected to play in the \(j\)-th position.Each person can be selected at most once as a player or a member of the audience. You have to choose exactly one player for each position.Since Alice is busy, she needs you to help her find the maximum possible strength of the club that can be achieved by an optimal choice of players and the audience.
|
The first line contains \(3\) integers \(n,p,k\) (\(2 \leq n \leq 10^{5}, 1 \leq p \leq 7, 1 \le k, p+k \le n\)).The second line contains \(n\) integers \(a_{1},a_{2},\ldots,a_{n}\). (\(1 \leq a_{i} \leq 10^{9}\)).The \(i\)-th of the next \(n\) lines contains \(p\) integers \(s_{i, 1}, s_{i, 2}, \dots, s_{i, p}\). (\(1 \leq s_{i,j} \leq 10^{9}\))
|
Print a single integer \({res}\) β the maximum possible strength of the club.
|
In the first sample, we can select person \(1\) to play in the \(1\)-st position and persons \(2\) and \(3\) as audience members. Then the total strength of the club will be equal to \(a_{2}+a_{3}+s_{1,1}\).
|
Input: 4 1 2 1 16 10 3 18 19 13 15 | Output: 44
|
Expert
| 4 | 1,173 | 348 | 77 | 13 |
1,332 |
F
|
1332F
|
F. Independent Set
| 2,500 |
dfs and similar; dp; trees
|
Eric is the teacher of graph theory class. Today, Eric teaches independent set and edge-induced subgraph.Given a graph \(G=(V,E)\), an independent set is a subset of vertices \(V' \subset V\) such that for every pair \(u,v \in V'\), \((u,v) \not \in E\) (i.e. no edge in \(E\) connects two vertices from \(V'\)).An edge-induced subgraph consists of a subset of edges \(E' \subset E\) and all the vertices in the original graph that are incident on at least one edge in the subgraph.Given \(E' \subset E\), denote \(G[E']\) the edge-induced subgraph such that \(E'\) is the edge set of the subgraph. Here is an illustration of those definitions: In order to help his students get familiar with those definitions, he leaves the following problem as an exercise:Given a tree \(G=(V,E)\), calculate the sum of \(w(H)\) over all except null edge-induced subgraph \(H\) of \(G\), where \(w(H)\) is the number of independent sets in \(H\). Formally, calculate \(\sum \limits_{\emptyset \not= E' \subset E} w(G[E'])\).Show Eric that you are smarter than his students by providing the correct answer as quickly as possible. Note that the answer might be large, you should output the answer modulo \(998,244,353\).
|
The first line contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)), representing the number of vertices of the graph \(G\).Each of the following \(n-1\) lines contains two integers \(u\) and \(v\) (\(1 \le u,v \le n\), \(u \not= v\)), describing edges of the given tree.It is guaranteed that the given edges form a tree.
|
Output one integer, representing the desired value modulo \(998,244,353\).
|
For the second example, all independent sets are listed below.
|
Input: 2 2 1 | Output: 3
|
Expert
| 3 | 1,204 | 331 | 74 | 13 |
1,941 |
C
|
1941C
|
C. Rudolf and the Ugly String
| 900 |
dp; greedy; strings
|
Rudolf has a string \(s\) of length \(n\). Rudolf considers the string \(s\) to be ugly if it contains the substring\(^\dagger\) ""pie"" or the substring ""map"", otherwise the string \(s\) will be considered beautiful.For example, ""ppiee"", ""mmap"", ""dfpiefghmap"" are ugly strings, while ""mathp"", ""ppiiee"" are beautiful strings.Rudolf wants to shorten the string \(s\) by removing some characters to make it beautiful.The main character doesn't like to strain, so he asks you to make the string beautiful by removing the minimum number of characters. He can remove characters from any positions in the string (not just from the beginning or end of the string).\(^\dagger\) String \(a\) is a substring of \(b\) if there exists a consecutive segment of characters in string \(b\) equal to \(a\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β the length of the string \(s\).The next line of each test case contains the string \(s\) of length \(n\). The string \(s\) consists of lowercase Latin letters.The sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output a single integer β the minimum number of characters that need to be deleted to make the string \(s\) beautiful. If the string is initially beautiful, then output \(0\).
|
In the first test case, for example, you can delete the \(4\)th and \(9\)th characters to make the string beautiful.In the second test case, the string is already beautiful.
|
Input: 69mmapnapie9azabazapi8mappppie18mapmapmapmapmapmap1p11pppiepieeee | Output: 2 0 2 6 0 2
|
Beginner
| 3 | 802 | 451 | 195 | 19 |
1,409 |
F
|
1409F
|
F. Subsequences of Length Two
| 2,100 |
dp; strings
|
You are given two strings \(s\) and \(t\) consisting of lowercase Latin letters. The length of \(t\) is \(2\) (i.e. this string consists only of two characters).In one move, you can choose any character of \(s\) and replace it with any lowercase Latin letter. More formally, you choose some \(i\) and replace \(s_i\) (the character at the position \(i\)) with some character from 'a' to 'z'.You want to do no more than \(k\) replacements in such a way that maximizes the number of occurrences of \(t\) in \(s\) as a subsequence.Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains two integers \(n\) and \(k\) (\(2 \le n \le 200\); \(0 \le k \le n\)) β the length of \(s\) and the maximum number of moves you can make. The second line of the input contains the string \(s\) consisting of \(n\) lowercase Latin letters. The third line of the input contains the string \(t\) consisting of two lowercase Latin letters.
|
Print one integer β the maximum possible number of occurrences of \(t\) in \(s\) as a subsequence if you replace no more than \(k\) characters in \(s\) optimally.
|
In the first example, you can obtain the string ""abab"" replacing \(s_1\) with 'a' and \(s_4\) with 'b'. Then the answer is \(3\).In the second example, you can obtain the string ""ssddsdd"" and get the answer \(10\).In the fourth example, you can obtain the string ""aaacaaa"" and get the answer \(15\).
|
Input: 4 2 bbaa ab | Output: 3
|
Hard
| 2 | 699 | 371 | 162 | 14 |
469 |
A
|
469A
|
A. I Wanna Be the Guy
| 800 |
greedy; implementation
|
There is a game called ""I Wanna Be the Guy"", consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game.Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?
|
The first line contains a single integer n (1 β€ n β€ 100). The next line contains an integer p (0 β€ p β€ n) at first, then follows p distinct integers a1, a2, ..., ap (1 β€ ai β€ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It's assumed that levels are numbered from 1 to n.
|
If they can pass all the levels, print ""I become the guy."". If it's impossible, print ""Oh, my keyboard!"" (without the quotes).
|
In the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.In the second sample, no one can pass level 4.
|
Input: 43 1 2 32 2 4 | Output: I become the guy.
|
Beginner
| 2 | 442 | 364 | 130 | 4 |
1,322 |
C
|
1322C
|
C. Instant Noodles
| 2,300 |
graphs; hashing; math; number theory
|
Wu got hungry after an intense training session, and came to a nearby store to buy his favourite instant noodles. After Wu paid for his purchase, the cashier gave him an interesting task.You are given a bipartite graph with positive integers in all vertices of the right half. For a subset \(S\) of vertices of the left half we define \(N(S)\) as the set of all vertices of the right half adjacent to at least one vertex in \(S\), and \(f(S)\) as the sum of all numbers in vertices of \(N(S)\). Find the greatest common divisor of \(f(S)\) for all possible non-empty subsets \(S\) (assume that GCD of empty set is \(0\)).Wu is too tired after his training to solve this problem. Help him!
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 500\,000\)) β the number of test cases in the given test set. Test case descriptions follow.The first line of each case description contains two integers \(n\) and \(m\) (\(1~\leq~n,~m~\leq~500\,000\)) β the number of vertices in either half of the graph, and the number of edges respectively.The second line contains \(n\) integers \(c_i\) (\(1 \leq c_i \leq 10^{12}\)). The \(i\)-th number describes the integer in the vertex \(i\) of the right half of the graph.Each of the following \(m\) lines contains a pair of integers \(u_i\) and \(v_i\) (\(1 \leq u_i, v_i \leq n\)), describing an edge between the vertex \(u_i\) of the left half and the vertex \(v_i\) of the right half. It is guaranteed that the graph does not contain multiple edges.Test case descriptions are separated with empty lines. The total value of \(n\) across all test cases does not exceed \(500\,000\), and the total value of \(m\) across all test cases does not exceed \(500\,000\) as well.
|
For each test case print a single integer β the required greatest common divisor.
|
The greatest common divisor of a set of integers is the largest integer \(g\) such that all elements of the set are divisible by \(g\).In the first sample case vertices of the left half and vertices of the right half are pairwise connected, and \(f(S)\) for any non-empty subset is \(2\), thus the greatest common divisor of these values if also equal to \(2\).In the second sample case the subset \(\{1\}\) in the left half is connected to vertices \(\{1, 2\}\) of the right half, with the sum of numbers equal to \(2\), and the subset \(\{1, 2\}\) in the left half is connected to vertices \(\{1, 2, 3\}\) of the right half, with the sum of numbers equal to \(3\). Thus, \(f(\{1\}) = 2\), \(f(\{1, 2\}) = 3\), which means that the greatest common divisor of all values of \(f(S)\) is \(1\).
|
Input: 3 2 4 1 1 1 1 1 2 2 1 2 2 3 4 1 1 1 1 1 1 2 2 2 2 3 4 7 36 31 96 29 1 2 1 3 1 4 2 2 2 4 3 1 4 3 | Output: 2 1 12
|
Expert
| 4 | 688 | 1,030 | 81 | 13 |
1,185 |
D
|
1185D
|
D. Extra Element
| 1,700 |
implementation; math
|
A sequence \(a_1, a_2, \dots, a_k\) is called an arithmetic progression if for each \(i\) from \(1\) to \(k\) elements satisfy the condition \(a_i = a_1 + c \cdot (i - 1)\) for some fixed \(c\).For example, these five sequences are arithmetic progressions: \([5, 7, 9, 11]\), \([101]\), \([101, 100, 99]\), \([13, 97]\) and \([5, 5, 5, 5, 5]\). And these four sequences aren't arithmetic progressions: \([3, 1, 2]\), \([1, 2, 4, 8]\), \([1, -1, 1, -1]\) and \([1, 2, 3, 3, 3]\).You are given a sequence of integers \(b_1, b_2, \dots, b_n\). Find any index \(j\) (\(1 \le j \le n\)), such that if you delete \(b_j\) from the sequence, you can reorder the remaining \(n-1\) elements, so that you will get an arithmetic progression. If there is no such index, output the number -1.
|
The first line of the input contains one integer \(n\) (\(2 \le n \le 2\cdot10^5\)) β length of the sequence \(b\). The second line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(-10^9 \le b_i \le 10^9\)) β elements of the sequence \(b\).
|
Print such index \(j\) (\(1 \le j \le n\)), so that if you delete the \(j\)-th element from the sequence, you can reorder the remaining elements, so that you will get an arithmetic progression. If there are multiple solutions, you are allowed to print any of them. If there is no such index, print -1.
|
Note to the first example. If you delete the \(4\)-th element, you can get the arithmetic progression \([2, 4, 6, 8]\).Note to the second example. The original sequence is already arithmetic progression, so you can delete \(1\)-st or last element and you will get an arithmetical progression again.
|
Input: 5 2 6 8 7 4 | Output: 4
|
Medium
| 2 | 778 | 243 | 301 | 11 |
1,252 |
F
|
1252F
|
F. Regular Forestation
| 2,400 |
hashing; trees
|
A forestation is an act of planting a bunch of trees to grow a forest, usually to replace a forest that had been cut down. Strangely enough, graph theorists have another idea on how to make a forest, i.e. by cutting down a tree!A tree is a graph of \(N\) nodes connected by \(N - 1\) edges. Let \(u\) be a node in a tree \(U\) which degree is at least \(2\) (i.e. directly connected to at least \(2\) other nodes in \(U\)). If we remove \(u\) from \(U\), then we will get two or more disconnected (smaller) trees, or also known as forest by graph theorists. In this problem, we are going to investigate a special forestation of a tree done by graph theorists.Let \(V(S)\) be the set of nodes in a tree \(S\) and \(V(T)\) be the set of nodes in a tree \(T\). Tree \(S\) and tree \(T\) are identical if there exists a bijection \(f : V(S) \rightarrow V(T)\) such that for all pairs of nodes \((s_i, s_j)\) in \(V(S)\), \(s_i\) and \(s_j\) is connected by an edge in \(S\) if and only if node \(f(s_i)\) and \(f(s_j)\) is connected by an edge in \(T\). Note that \(f(s) = t\) implies node \(s\) in \(S\) corresponds to node \(t\) in \(T\).We call a node \(u\) in a tree \(U\) as a good cutting point if and only if the removal of \(u\) from \(U\) causes two or more disconnected trees, and all those disconnected trees are pairwise identical.Given a tree \(U\), your task is to determine whether there exists a good cutting point in \(U\). If there is such a node, then you should output the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point.For example, consider the following tree of \(13\) nodes.There is exactly one good cutting point in this tree, i.e. node \(4\). Observe that by removing node \(4\), we will get three identical trees (in this case, line graphs), i.e. \(\{5, 1, 7, 13\}\), \(\{8, 2, 11, 6\}\), and \(\{3, 12, 9, 10\}\), which are denoted by \(A\), \(B\), and \(C\) respectively in the figure. The bijection function between \(A\) and \(B\): \(f(5) = 8\), \(f(1) = 2\), \(f(7) = 11\), and \(f(13) = 6\). The bijection function between \(A\) and \(C\): \(f(5) = 3\), \(f(1) = 12\), \(f(7) = 9\), and \(f(13) = 10\). The bijection function between \(B\) and \(C\): \(f(8) = 3\), \(f(2) = 12\), \(f(11) = 9\), and \(f(6) = 10\). Of course, there exists other bijection functions for those trees.
|
Input begins with a line containting an integer: \(N\) (\(3 \le N \le 4000\)) representing the number of nodes in the given tree. The next \(N - 1\) lines each contains two integers: \(a_i\) \(b_i\) (\(1 \le a_i < b_i \le N\)) representing an edge \((a_i,b_i)\) in the given tree. It is guaranteed that any two nodes in the given tree are connected to each other by a sequence of edges.
|
Output in a line an integer representing the maximum number of disconnected trees that can be obtained by removing exactly one good cutting point, or output -1 if there is no such good cutting point.
|
Explanation for the sample input/output #1This is the example from the problem description.
|
Input: 13 1 5 1 7 2 4 2 8 2 11 3 12 4 7 4 12 6 11 7 13 9 10 9 12 | Output: 3
|
Expert
| 2 | 2,362 | 386 | 199 | 12 |
1,139 |
A
|
1139A
|
A. Even Substrings
| 800 |
implementation; strings
|
You are given a string \(s=s_1s_2\dots s_n\) of length \(n\), which only contains digits \(1\), \(2\), ..., \(9\).A substring \(s[l \dots r]\) of \(s\) is a string \(s_l s_{l + 1} s_{l + 2} \ldots s_r\). A substring \(s[l \dots r]\) of \(s\) is called even if the number represented by it is even. Find the number of even substrings of \(s\). Note, that even if some substrings are equal as strings, but have different \(l\) and \(r\), they are counted as different substrings.
|
The first line contains an integer \(n\) (\(1 \le n \le 65000\)) β the length of the string \(s\).The second line contains a string \(s\) of length \(n\). The string \(s\) consists only of digits \(1\), \(2\), ..., \(9\).
|
Print the number of even substrings of \(s\).
|
In the first example, the \([l, r]\) pairs corresponding to even substrings are: \(s[1 \dots 2]\) \(s[2 \dots 2]\) \(s[1 \dots 4]\) \(s[2 \dots 4]\) \(s[3 \dots 4]\) \(s[4 \dots 4]\) In the second example, all \(10\) substrings of \(s\) are even substrings. Note, that while substrings \(s[1 \dots 1]\) and \(s[2 \dots 2]\) both define the substring ""2"", they are still counted as different substrings.
|
Input: 4 1234 | Output: 6
|
Beginner
| 2 | 477 | 221 | 45 | 11 |
15 |
E
|
15E
|
E. Triangles
| 2,600 |
combinatorics; dp
|
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has n levels, where n is an even number.In the local council you were given an area map, where the granny's house is marked by point H, parts of dense forest are marked grey (see the picture to understand better).After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: it starts and ends in the same place β the granny's house; the route goes along the forest paths only (these are the segments marked black in the picture); the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); the route cannot cross itself; there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. The example of the area map for n = 12 is given in the picture. Since the map has a regular structure, you can construct it for other n by analogy using the example.
|
The input data contain the only even integer n (2 β€ n β€ 106).
|
Output the only number β the amount of Peter's routes modulo 1000000009.
|
Input: 2 | Output: 10
|
Expert
| 2 | 1,400 | 61 | 72 | 0 |
|
57 |
B
|
57B
|
B. Martian Architecture
| 1,600 |
implementation
|
Chris the Rabbit found the traces of an ancient Martian civilization. The brave astronomer managed to see through a small telescope an architecture masterpiece β ""A Road to the Sun"". The building stands on cubical stones of the same size. The foundation divides the entire ""road"" into cells, into which the cubical stones are fit tightly. Thus, to any cell of the foundation a coordinate can be assigned. To become the leader of the tribe, a Martian should build a Road to the Sun, that is to build from those cubical stones on a given foundation a stairway. The stairway should be described by the number of stones in the initial coordinate and the coordinates of the stairway's beginning and end. Each following cell in the coordinate's increasing order should contain one cubical stone more than the previous one. At that if the cell has already got stones, they do not count in this building process, the stairways were simply built on them. In other words, let us assume that a stairway is built with the initial coordinate of l, the final coordinate of r and the number of stones in the initial coordinate x. That means that x stones will be added in the cell l, x + 1 stones will be added in the cell l + 1, ..., x + r - l stones will be added in the cell r.Chris managed to find an ancient manuscript, containing the descriptions of all the stairways. Now he wants to compare the data to be sure that he has really found ""A Road to the Sun"". For that he chose some road cells and counted the total number of cubical stones that has been accumulated throughout the Martian history and then asked you to count using the manuscript to what the sum should ideally total.
|
The first line contains three space-separated integers: n, m, k (1 β€ n, m β€ 105, 1 β€ k β€ min(n, 100)) which is the number of cells, the number of ""Roads to the Sun"" and the number of cells in the query correspondingly. Each of the following m roads contain three space-separated integers: ai, bi, ci (1 β€ ai β€ bi β€ n, 1 β€ ci β€ 1000) which are the stairway's description, its beginning, end and the initial cell's height. Then follow a line, containing k different space-separated integers bi. All these numbers ranging from 1 to n are cells, the number of stones in which interests Chris.
|
You have to print a single number on a single line which is the sum of stones in all the cells Chris is interested in.Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
|
Input: 5 2 11 5 12 4 13 | Output: 5
|
Medium
| 1 | 1,680 | 590 | 248 | 0 |
|
1,045 |
G
|
1045G
|
G. AI robots
| 2,200 |
data structures
|
In the last mission, MDCS has successfully shipped \(N\) AI robots to Mars. Before they start exploring, system initialization is required so they are arranged in a line. Every robot can be described with three numbers: position (\(x_i\)), radius of sight (\(r_i\)) and IQ (\(q_i\)).Since they are intelligent robots, some of them will talk if they see each other. Radius of sight is inclusive, so robot can see other all robots in range \([x_i - r_i, x_i + r_i]\). But they don't walk to talk with anybody, but only with robots who have similar IQ. By similar IQ we mean that their absolute difference isn't more than \(K\). Help us and calculate how many pairs of robots are going to talk with each other, so we can timely update their software and avoid any potential quarrel.
|
The first line contains two integers, numbers \(N (1 \leq N \leq 10^5) \) and \(K (0 \leq K \leq 20)\).Next \(N\) lines contain three numbers each \(x_i, r_i, q_i (0 \leq x_i,r_i,q_i \leq 10^9)\) β position, radius of sight and IQ of every robot respectively.
|
Output contains only one number β solution to the problem.
|
The first robot can see the second, but not vice versa. The first robot can't even see the third. The second and the third robot can see each other and their IQs don't differ more than 2 so only one conversation will happen.
|
Input: 3 23 6 17 3 1010 5 8 | Output: 1
|
Hard
| 1 | 779 | 259 | 58 | 10 |
111 |
C
|
111C
|
C. Petya and Spiders
| 2,100 |
bitmasks; dp; dsu
|
Little Petya loves training spiders. Petya has a board n Γ m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.
|
The first line contains two space-separated integers n and m (1 β€ n, m β€ 40, nΒ·m β€ 40) β the board sizes.
|
In the first line print the maximum number of cells without spiders.
|
In the first sample the only possible answer is:sIn the second sample one of the possible solutions is: rdlruls denotes command ""stay idle"", l, r, d, u denote commands ""crawl left"", ""crawl right"", ""crawl down"", ""crawl up"", correspondingly.
|
Input: 1 1 | Output: 0
|
Hard
| 3 | 758 | 105 | 68 | 1 |
1,200 |
B
|
1200B
|
B. Block Adventure
| 1,200 |
dp; greedy
|
Gildong is playing a video game called Block Adventure. In Block Adventure, there are \(n\) columns of blocks in a row, and the columns are numbered from \(1\) to \(n\). All blocks have equal heights. The height of the \(i\)-th column is represented as \(h_i\), which is the number of blocks stacked in the \(i\)-th column.Gildong plays the game as a character that can stand only on the top of the columns. At the beginning, the character is standing on the top of the \(1\)-st column. The goal of the game is to move the character to the top of the \(n\)-th column.The character also has a bag that can hold infinitely many blocks. When the character is on the top of the \(i\)-th column, Gildong can take one of the following three actions as many times as he wants: if there is at least one block on the column, remove one block from the top of the \(i\)-th column and put it in the bag; if there is at least one block in the bag, take one block out of the bag and place it on the top of the \(i\)-th column; if \(i < n\) and \(|h_i - h_{i+1}| \le k\), move the character to the top of the \(i+1\)-st column. \(k\) is a non-negative integer given at the beginning of the game. Note that it is only possible to move to the next column. In actions of the first two types the character remains in the \(i\)-th column, and the value \(h_i\) changes.The character initially has \(m\) blocks in the bag. Gildong wants to know if it is possible to win the game. Help Gildong find the answer to his question.
|
Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1 \le n \le 100\), \(0 \le m \le 10^6\), \(0 \le k \le 10^6\)) β the number of columns in the game, the number of blocks in the character's bag at the beginning, and the non-negative integer \(k\) described in the statement.The second line of each test case contains \(n\) integers. The \(i\)-th integer is \(h_i\) (\(0 \le h_i \le 10^6\)), the initial height of the \(i\)-th column.
|
For each test case, print ""YES"" if it is possible to win the game. Otherwise, print ""NO"".You can print each letter in any case (upper or lower).
|
In the first case, Gildong can take one block from the \(1\)-st column, move to the \(2\)-nd column, put the block on the \(2\)-nd column, then move to the \(3\)-rd column.In the second case, Gildong has to put the block in his bag on the \(1\)-st column to get to the \(2\)-nd column. But it is impossible to get to the \(3\)-rd column because \(|h_2 - h_3| = 3 > k\) and there is no way to decrease the gap.In the fifth case, the character is already on the \(n\)-th column from the start so the game is won instantly.
|
Input: 53 0 14 3 53 1 21 4 74 10 010 20 10 202 5 50 111 9 999 | Output: YESNOYESNOYES
|
Easy
| 2 | 1,504 | 627 | 148 | 12 |
582 |
D
|
582D
|
D. Number of Binominal Coefficients
| 3,300 |
dp; math; number theory
|
For a given prime integer p and integers Ξ±, A calculate the number of pairs of integers (n, k), such that 0 β€ k β€ n β€ A and is divisible by pΞ±. As the answer can be rather large, print the remainder of the answer moduly 109 + 7.Let us remind you that is the number of ways k objects can be chosen from the set of n objects.
|
The first line contains two integers, p and Ξ± (1 β€ p, Ξ± β€ 109, p is prime). The second line contains the decimal record of integer A (0 β€ A < 101000) without leading zeroes.
|
In the single line print the answer to the problem.
|
In the first sample three binominal coefficients divisible by 4 are , and .
|
Input: 2 27 | Output: 3
|
Master
| 3 | 323 | 173 | 51 | 5 |
1,928 |
E
|
1928E
|
E. Modular Sequence
| 2,300 |
brute force; constructive algorithms; dp; graphs; greedy; math; number theory
|
You are given two integers \(x\) and \(y\). A sequence \(a\) of length \(n\) is called modular if \(a_1=x\), and for all \(1 < i \le n\) the value of \(a_{i}\) is either \(a_{i-1} + y\) or \(a_{i-1} \bmod y\). Here \(x \bmod y\) denotes the remainder from dividing \(x\) by \(y\).Determine if there exists a modular sequence of length \(n\) with the sum of its elements equal to \(S\), and if it exists, find any such sequence.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2 \cdot 10^4\)). The description of the test cases follows.The first and only line of each test case contains four integers \(n\), \(x\), \(y\), and \(s\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le x \le 2 \cdot 10^5\), \(1 \le y \le 2 \cdot 10^5\), \(0 \le s \le 2 \cdot 10^5\)) β the length of the sequence, the parameters \(x\) and \(y\), and the required sum of the sequence elements.The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\), and also the sum of \(s\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, if the desired sequence exists, output ""Yes"" on the first line (without quotes). Then, on the second line, output \(n\) integers \(a_1, a_2, \ldots, a_n\) separated by a space β the elements of the sequence \(a\). If there are multiple suitable sequences, output any of them.If the sequence does not exist, output ""No"" on a single line.You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer.
|
In the first example, the sequence \([8, 11, 2, 5, 2]\) satisfies the conditions. Thus, \(a_1 = 8 = x\), \(a_2 = 11 = a_1 + 3\), \(a_3 = 2 = a_2 \bmod 3\), \(a_4 = 5 = a_3 + 3\), \(a_5 = 2 = a_4 \bmod 3\).In the second example, the first element of the sequence should be equal to \(5\), so the sequence \([2, 2, 2]\) is not suitable.
|
Input: 35 8 3 283 5 3 69 1 5 79 | Output: YES 8 11 2 2 5 NO NO
|
Expert
| 7 | 427 | 644 | 527 | 19 |
1,370 |
F1
|
1370F1
|
F1. The Hidden Pair (Easy Version)
| 2,400 |
binary search; dfs and similar; graphs; interactive; shortest paths; trees
|
Note that the only difference between the easy and hard version is the constraint on the number of queries. You can make hacks only if all versions of the problem are solved.This is an interactive problem.You are given a tree consisting of \(n\) nodes numbered with integers from \(1\) to \(n\). Ayush and Ashish chose two secret distinct nodes in the tree. You need to find out both the nodes. You can make the following query: Provide a list of nodes and you will receive a node from that list whose sum of distances to both the hidden nodes is minimal (if there are multiple such nodes in the list, you will receive any one of them). You will also get the sum of distances of that node to the hidden nodes. Recall that a tree is a connected graph without cycles. The distance between two nodes is defined as the number of edges in the simple path between them.More formally, let's define two hidden nodes as \(s\) and \(f\). In one query you can provide the set of nodes \(\{a_1, a_2, \ldots, a_c\}\) of the tree. As a result, you will get two numbers \(a_i\) and \(dist(a_i, s) + dist(a_i, f)\). The node \(a_i\) is any node from the provided set, for which the number \(dist(a_i, s) + dist(a_i, f)\) is minimal.You can ask no more than \(14\) queries.
|
The first line contains a single integer \(t\) \((1 \leq t \leq 10)\) β the number of test cases. Please note, how the interaction process is organized.The first line of each test case consists of a single integer \(n\) \((2 \le n \le 1000)\) β the number of nodes in the tree.The next \(n - 1\) lines consist of two integers \(u\), \(v\) \((1 \le u, v \le n, u \ne v)\) β the edges of the tree.
|
The tree from the first test is shown below, and the hidden nodes are \(1\) and \(3\).
|
Input: 1 3 1 2 1 3 1 1 2 3 3 1 3 1 Correct | Output: ? 1 1 ? 1 2 ? 1 3 ? 2 2 3 ! 1 3
|
Expert
| 6 | 1,256 | 395 | 0 | 13 |
|
2,052 |
K
|
2052K
| 2,800 |
Master
| 0 | 0 | 0 | 0 | 20 |
|||||||
1,884 |
A
|
1884A
|
A. Simple Design
| 800 |
brute force; greedy; math
|
A positive integer is called \(k\)-beautiful, if the digit sum of the decimal representation of this number is divisible by \(k^{\dagger}\). For example, \(9272\) is \(5\)-beautiful, since the digit sum of \(9272\) is \(9 + 2 + 7 + 2 = 20\).You are given two integers \(x\) and \(k\). Please find the smallest integer \(y \ge x\) which is \(k\)-beautiful.\(^{\dagger}\) An integer \(n\) is divisible by \(k\) if there exists an integer \(m\) such that \(n = k \cdot m\).
|
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 only line of each test case contains two integers \(x\) and \(k\) (\(1 \le x \le 10^9\), \(1 \le k \le 10\)).
|
For each test case, output the smallest integer \(y \ge x\) which is \(k\)-beautiful.
|
In the first test case, numbers from \(1\) to \(4\) consist of a single digit, thus the digit sum is equal to the number itself. None of the integers from \(1\) to \(4\) are divisible by \(5\).In the fourth test case, the digit sum of \(777\) is \(7 + 7 + 7 = 21\) which is already divisible by \(3\).
|
Input: 61 510 837 9777 31235 101 10 | Output: 5 17 45 777 1243 19
|
Beginner
| 3 | 470 | 274 | 85 | 18 |
370 |
D
|
370D
|
D. Broken Monitor
| 2,100 |
brute force; constructive algorithms; greedy; implementation
|
Innocentius has a problem β his computer monitor has broken. Now some of the pixels are ""dead"", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus.Innocentius is touch-typing a program that paints a white square one-pixel wide frame on the black screen. As the monitor is broken, some pixels that should be white remain black. Polycarpus should look at what the program displayed on the screen and guess the position and size of the frame Innocentius has painted. Polycarpus doesn't like the game but Innocentius persuaded brother to play as ""the game is good for the imagination and attention"".Help Polycarpus, automatize his part in the gaming process. Write the code that finds such possible square frame that: the frame's width is 1 pixel, the frame doesn't go beyond the borders of the screen, all white pixels of the monitor are located on the frame, of all frames that satisfy the previous three conditions, the required frame must have the smallest size. Formally, a square frame is represented by such pixels of the solid square, that are on the square's border, that is, are not fully surrounded by the other pixels of the square. For example, if the frame's size is d = 3, then it consists of 8 pixels, if its size is d = 2, then it contains 4 pixels and if d = 1, then the frame is reduced to a single pixel.
|
The first line contains the resolution of the monitor as a pair of integers n, m (1 β€ n, m β€ 2000). The next n lines contain exactly m characters each β the state of the monitor pixels at the moment of the game. Character ""."" (period, ASCII code 46) corresponds to the black pixel, and character ""w"" (lowercase English letter w) corresponds to the white pixel. It is guaranteed that at least one pixel of the monitor is white.
|
Print the monitor screen. Represent the sought frame by characters ""+"" (the ""plus"" character). The pixels that has become white during the game mustn't be changed. Print them as ""w"". If there are multiple possible ways to position the frame of the minimum size, print any of them.If the required frame doesn't exist, then print a single line containing number -1.
|
In the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist.
|
Input: 4 8..w..w....................w..w.. | Output: ..w++w....+..+....+..+....w++w..
|
Hard
| 4 | 1,448 | 430 | 369 | 3 |
1,661 |
B
|
1661B
|
B. Getting Zero
| 1,300 |
bitmasks; brute force; dfs and similar; dp; graphs; greedy; shortest paths
|
Suppose you have an integer \(v\). In one operation, you can: either set \(v = (v + 1) \bmod 32768\) or set \(v = (2 \cdot v) \bmod 32768\). You are given \(n\) integers \(a_1, a_2, \dots, a_n\). What is the minimum number of operations you need to make each \(a_i\) equal to \(0\)?
|
The first line contains the single integer \(n\) (\(1 \le n \le 32768\)) β the number of integers.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i < 32768\)).
|
Print \(n\) integers. The \(i\)-th integer should be equal to the minimum number of operations required to make \(a_i\) equal to \(0\).
|
Let's consider each \(a_i\): \(a_1 = 19\). You can, firstly, increase it by one to get \(20\) and then multiply it by two \(13\) times. You'll get \(0\) in \(1 + 13 = 14\) steps. \(a_2 = 32764\). You can increase it by one \(4\) times: \(32764 \rightarrow 32765 \rightarrow 32766 \rightarrow 32767 \rightarrow 0\). \(a_3 = 10240\). You can multiply it by two \(4\) times: \(10240 \rightarrow 20480 \rightarrow 8192 \rightarrow 16384 \rightarrow 0\). \(a_4 = 49\). You can multiply it by two \(15\) times.
|
Input: 4 19 32764 10240 49 | Output: 14 4 4 15
|
Easy
| 7 | 282 | 187 | 135 | 16 |
235 |
C
|
235C
|
C. Cyclical Quest
| 2,700 |
data structures; string suffix structures; strings
|
Some days ago, WJMZBMR learned how to answer the query ""how many times does a string x occur in a string s"" quickly by preprocessing the string s. But now he wants to make it harder.So he wants to ask ""how many consecutive substrings of s are cyclical isomorphic to a given string x"". You are given string s and n strings xi, for each string xi find, how many consecutive substrings of s are cyclical isomorphic to xi.Two strings are called cyclical isomorphic if one can rotate one string to get the other one. 'Rotate' here means 'to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order'. For example, string ""abcde"" can be rotated to string ""deabc"". We can take characters ""abc"" from the beginning and put them at the end of ""de"".
|
The first line contains a non-empty string s. The length of string s is not greater than 106 characters.The second line contains an integer n (1 β€ n β€ 105) β the number of queries. Then n lines follow: the i-th line contains the string xi β the string for the i-th query. The total length of xi is less than or equal to 106 characters.In this problem, strings only consist of lowercase English letters.
|
For each query xi print a single integer that shows how many consecutive substrings of s are cyclical isomorphic to xi. Print the answers to the queries in the order they are given in the input.
|
Input: baabaabaaa5ababaaaabaaaaba | Output: 75735
|
Master
| 3 | 825 | 402 | 194 | 2 |
|
266 |
B
|
266B
|
B. Queue at the School
| 800 |
constructive algorithms; graph matchings; implementation; shortest paths
|
During the break the schoolchildren, boys and girls, formed a queue of n people in the canteen. Initially the children stood in the order they entered the canteen. However, after a while the boys started feeling awkward for standing in front of the girls in the queue and they started letting the girls move forward each second. Let's describe the process more precisely. Let's say that the positions in the queue are sequentially numbered by integers from 1 to n, at that the person in the position number 1 is served first. Then, if at time x a boy stands on the i-th position and a girl stands on the (i + 1)-th position, then at time x + 1 the i-th position will have a girl and the (i + 1)-th position will have a boy. The time is given in seconds.You've got the initial position of the children, at the initial moment of time. Determine the way the queue is going to look after t seconds.
|
The first line contains two integers n and t (1 β€ n, t β€ 50), which represent the number of children in the queue and the time after which the queue will transform into the arrangement you need to find. The next line contains string s, which represents the schoolchildren's initial arrangement. If the i-th position in the queue contains a boy, then the i-th character of string s equals ""B"", otherwise the i-th character equals ""G"".
|
Print string a, which describes the arrangement after t seconds. If the i-th position has a boy after the needed time, then the i-th character a must equal ""B"", otherwise it must equal ""G"".
|
Input: 5 1BGGBG | Output: GBGGB
|
Beginner
| 4 | 894 | 437 | 193 | 2 |
|
1,712 |
B
|
1712B
|
B. Woeful Permutation
| 800 |
constructive algorithms; greedy; number theory
|
I wonder, does the falling rain Forever yearn for it's disdain?Effluvium of the MindYou are given a positive integer \(n\).Find any permutation \(p\) of length \(n\) such that the sum \(\operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n)\) is as large as possible. Here \(\operatorname{lcm}(x, y)\) denotes the least common multiple (LCM) of integers \(x\) and \(y\).A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1\,000\)). Description of the test cases follows.The only line for each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case print \(n\) integers \(p_1\), \(p_2\), \(\ldots\), \(p_n\) β the permutation with the maximum possible value of \(\operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n)\).If there are multiple answers, print any of them.
|
For \(n = 1\), there is only one permutation, so the answer is \([1]\).For \(n = 2\), there are two permutations: \([1, 2]\) β the sum is \(\operatorname{lcm}(1,1) + \operatorname{lcm}(2, 2) = 1 + 2 = 3\). \([2, 1]\) β the sum is \(\operatorname{lcm}(1,2) + \operatorname{lcm}(2, 1) = 2 + 2 = 4\).
|
Input: 212 | Output: 1 2 1
|
Beginner
| 3 | 715 | 331 | 277 | 17 |
1,575 |
L
|
1575L
|
L. Longest Array Deconstruction
| 2,100 |
data structures; divide and conquer; dp; sortings
|
Mr. Chanek gives you a sequence \(a\) indexed from \(1\) to \(n\). Define \(f(a)\) as the number of indices where \(a_i = i\). You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the \(3\)-rd element from the sequence \([4, 2, 3, 1]\), the resulting sequence will be \([4, 2, 1]\). You want to remove some elements from \(a\) in order to maximize \(f(a)\), using zero or more operations. Find the largest possible \(f(a)\).
|
The first line contains one integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the initial length of the sequence.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 2 \cdot 10^5\)) β the initial sequence \(a\).
|
Output an integer denoting the largest \(f(a)\) that can be obtained by doing zero or more operations.
|
In the first example, \(f(A) = 3\) by doing the following operations.\([2,1,\textbf{4},2,5,3,7] \rightarrow [\textbf{2},1,2,5,3,7] \rightarrow [1,2,5,3,\textbf{7}] \rightarrow [1,2,\textbf{5},3] \rightarrow [1,2,3]\)In the second example, \(f(A) = 2\) and no additional operation is needed.
|
Input: 7 2 1 4 2 5 3 7 | Output: 3
|
Hard
| 4 | 513 | 242 | 102 | 15 |
722 |
B
|
722B
|
B. Verse Pattern
| 1,200 |
implementation; strings
|
You are given a text consisting of n lines. Each line contains some space-separated words, consisting of lowercase English letters.We define a syllable as a string that contains exactly one vowel and any arbitrary number (possibly none) of consonants. In English alphabet following letters are considered to be vowels: 'a', 'e', 'i', 'o', 'u' and 'y'.Each word of the text that contains at least one vowel can be divided into syllables. Each character should be a part of exactly one syllable. For example, the word ""mamma"" can be divided into syllables as ""ma"" and ""mma"", ""mam"" and ""ma"", and ""mamm"" and ""a"". Words that consist of only consonants should be ignored.The verse patterns for the given text is a sequence of n integers p1, p2, ..., pn. Text matches the given verse pattern if for each i from 1 to n one can divide words of the i-th line in syllables in such a way that the total number of syllables is equal to pi.You are given the text and the verse pattern. Check, if the given text matches the given verse pattern.
|
The first line of the input contains a single integer n (1 β€ n β€ 100) β the number of lines in the text.The second line contains integers p1, ..., pn (0 β€ pi β€ 100) β the verse pattern.Next n lines contain the text itself. Text consists of lowercase English letters and spaces. It's guaranteed that all lines are non-empty, each line starts and ends with a letter and words are separated by exactly one space. The length of each line doesn't exceed 100 characters.
|
If the given text matches the given verse pattern, then print ""YES"" (without quotes) in the only line of the output. Otherwise, print ""NO"" (without quotes).
|
In the first sample, one can split words into syllables in the following way: in-telco-dech al-len-geSince the word ""ch"" in the third line doesn't contain vowels, we can ignore it. As the result we get 2 syllabels in first two lines and 3 syllables in the third one.
|
Input: 32 2 3intelcodech allenge | Output: YES
|
Easy
| 2 | 1,043 | 464 | 160 | 7 |
86 |
D
|
86D
|
D. Powerful array
| 2,200 |
data structures; implementation; math; two pointers
|
An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 β€ l β€ r β€ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of products KsΒ·KsΒ·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.You should calculate the power of t given subarrays.
|
First line contains two integers n and t (1 β€ n, t β€ 200000) β the array length and the number of queries correspondingly.Second line contains n positive integers ai (1 β€ ai β€ 106) β the elements of the array.Next t lines contain two positive integers l, r (1 β€ l β€ r β€ n) each β the indices of the left and the right ends of the corresponding subarray.
|
Output t lines, the i-th line of the output should contain single positive integer β the power of the i-th query subarray.Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use %I64d).
|
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored): Then K1 = 3, K2 = 2, K3 = 1, so the power is equal to 32Β·1 + 22Β·2 + 12Β·3 = 20.
|
Input: 3 21 2 11 21 3 | Output: 36
|
Hard
| 4 | 491 | 353 | 260 | 0 |
1,300 |
A
|
1300A
|
A. Non-zero
| 800 |
implementation; math
|
Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \dots, a_n\)]. In one step they can add \(1\) to any element of the array. Formally, in one step they can choose any integer index \(i\) (\(1 \le i \le n\)) and do \(a_i := a_i + 1\).If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make \(a_1 + a_2 +\) \(\dots\) \(+ a_n \ne 0\) and \(a_1 \cdot a_2 \cdot\) \(\dots\) \(\cdot a_n \ne 0\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 100\)) β the size of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-100 \le a_i \le 100\)) β elements of the array .
|
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
|
In the first test case, the sum is \(0\). If we add \(1\) to the first element, the array will be \([3,-1,-1]\), the sum will be equal to \(1\) and the product will be equal to \(3\).In the second test case, both product and sum are \(0\). If we add \(1\) to the second and the third element, the array will be \([-1,1,1,1]\), the sum will be equal to \(2\) and the product will be equal to \(-1\). It can be shown that fewer steps can't be enough.In the third test case, both sum and product are non-zero, we don't need to do anything.In the fourth test case, after adding \(1\) twice to the first element the array will be \([2,-2,1]\), the sum will be \(1\) and the product will be \(-4\).
|
Input: 4 3 2 -1 -1 4 -1 0 0 1 2 -1 2 3 0 -2 1 | Output: 1 2 0 2
|
Beginner
| 2 | 695 | 401 | 142 | 13 |
304 |
A
|
304A
|
A. Pythagorean Theorem II
| 1,200 |
brute force; math
|
In mathematics, the Pythagorean theorem β is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states: In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle). The theorem can be written as an equation relating the lengths of the sides a, b and c, often called the Pythagorean equation:a2 + b2 = c2where c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides. Given n, your task is to count how many right-angled triangles with side-lengths a, b and c that satisfied an inequality 1 β€ a β€ b β€ c β€ n.
|
The only line contains one integer n (1 β€ n β€ 104) as we mentioned above.
|
Print a single integer β the answer to the problem.
|
Input: 5 | Output: 1
|
Easy
| 2 | 782 | 73 | 51 | 3 |
|
1,075 |
A
|
1075A
|
A. The King's Race
| 800 |
implementation; math
|
On a chessboard with a width of \(n\) and a height of \(n\), rows are numbered from bottom to top from \(1\) to \(n\), columns are numbered from left to right from \(1\) to \(n\). Therefore, for each cell of the chessboard, you can assign the coordinates \((r,c)\), where \(r\) is the number of the row, and \(c\) is the number of the column.The white king has been sitting in a cell with \((1,1)\) coordinates for a thousand years, while the black king has been sitting in a cell with \((n,n)\) coordinates. They would have sat like that further, but suddenly a beautiful coin fell on the cell with coordinates \((x,y)\)...Each of the monarchs wanted to get it, so they decided to arrange a race according to slightly changed chess rules:As in chess, the white king makes the first move, the black king makes the second one, the white king makes the third one, and so on. However, in this problem, kings can stand in adjacent cells or even in the same cell at the same time.The player who reaches the coin first will win, that is to say, the player who reaches the cell with the coordinates \((x,y)\) first will win.Let's recall that the king is such a chess piece that can move one cell in all directions, that is, if the king is in the \((a,b)\) cell, then in one move he can move from \((a,b)\) to the cells \((a + 1,b)\), \((a - 1,b)\), \((a,b + 1)\), \((a,b - 1)\), \((a + 1,b - 1)\), \((a + 1,b + 1)\), \((a - 1,b - 1)\), or \((a - 1,b + 1)\). Going outside of the field is prohibited.Determine the color of the king, who will reach the cell with the coordinates \((x,y)\) first, if the white king moves first.
|
The first line contains a single integer \(n\) (\(2 \le n \le 10^{18}\)) β the length of the side of the chess field.The second line contains two integers \(x\) and \(y\) (\(1 \le x,y \le n\)) β coordinates of the cell, where the coin fell.
|
In a single line print the answer ""White"" (without quotes), if the white king will win, or ""Black"" (without quotes), if the black king will win.You can print each letter in any case (upper or lower).
|
An example of the race from the first sample where both the white king and the black king move optimally: The white king moves from the cell \((1,1)\) into the cell \((2,2)\). The black king moves form the cell \((4,4)\) into the cell \((3,3)\). The white king moves from the cell \((2,2)\) into the cell \((2,3)\). This is cell containing the coin, so the white king wins. An example of the race from the second sample where both the white king and the black king move optimally: The white king moves from the cell \((1,1)\) into the cell \((2,2)\). The black king moves form the cell \((5,5)\) into the cell \((4,4)\). The white king moves from the cell \((2,2)\) into the cell \((3,3)\). The black king moves from the cell \((4,4)\) into the cell \((3,5)\). This is the cell, where the coin fell, so the black king wins. In the third example, the coin fell in the starting cell of the black king, so the black king immediately wins.
|
Input: 42 3 | Output: White
|
Beginner
| 2 | 1,617 | 240 | 203 | 10 |
940 |
D
|
940D
|
D. Alena And The Heater
| 1,600 |
binary search; implementation
|
""We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme.""""Little Alena got an array as a birthday present...""The array b of length n is obtained from the array a of length n and two integers l and r (l β€ r) using the following procedure:b1 = b2 = b3 = b4 = 0.For all 5 β€ i β€ n: bi = 0 if ai, ai - 1, ai - 2, ai - 3, ai - 4 > r and bi - 1 = bi - 2 = bi - 3 = bi - 4 = 1 bi = 1 if ai, ai - 1, ai - 2, ai - 3, ai - 4 < l and bi - 1 = bi - 2 = bi - 3 = bi - 4 = 0 bi = bi - 1 otherwise You are given arrays a and b' of the same length. Find two integers l and r (l β€ r), such that applying the algorithm described above will yield an array b equal to b'.It's guaranteed that the answer exists.
|
The first line of input contains a single integer n (5 β€ n β€ 105) β the length of a and b'.The second line of input contains n space separated integers a1, ..., an ( - 109 β€ ai β€ 109) β the elements of a.The third line of input contains a string of n characters, consisting of 0 and 1 β the elements of b'. Note that they are not separated by spaces.
|
Output two integers l and r ( - 109 β€ l β€ r β€ 109), conforming to the requirements described above.If there are multiple solutions, output any of them.It's guaranteed that the answer exists.
|
In the first test case any pair of l and r pair is valid, if 6 β€ l β€ r β€ 109, in that case b5 = 1, because a1, ..., a5 < l.
|
Input: 51 2 3 4 500001 | Output: 6 15
|
Medium
| 2 | 758 | 350 | 190 | 9 |
1,096 |
A
|
1096A
|
A. Find Divisible
| 800 |
greedy; implementation; math
|
You are given a range of positive integers from \(l\) to \(r\).Find such a pair of integers \((x, y)\) that \(l \le x, y \le r\), \(x \ne y\) and \(x\) divides \(y\).If there are multiple answers, print any of them.You are also asked to answer \(T\) independent queries.
|
The first line contains a single integer \(T\) (\(1 \le T \le 1000\)) β the number of queries.Each of the next \(T\) lines contains two integers \(l\) and \(r\) (\(1 \le l \le r \le 998244353\)) β inclusive borders of the range.It is guaranteed that testset only includes queries, which have at least one suitable pair.
|
Print \(T\) lines, each line should contain the answer β two integers \(x\) and \(y\) such that \(l \le x, y \le r\), \(x \ne y\) and \(x\) divides \(y\). The answer in the \(i\)-th line should correspond to the \(i\)-th query from the input.If there are multiple answers, print any of them.
|
Input: 3 1 10 3 14 1 10 | Output: 1 7 3 9 5 10
|
Beginner
| 3 | 270 | 319 | 291 | 10 |
|
1,430 |
G
|
1430G
|
G. Yet Another DAG Problem
| 2,600 |
bitmasks; dfs and similar; dp; flows; graphs; math
|
You are given a directed acyclic graph (a directed graph that does not contain cycles) of \(n\) vertices and \(m\) arcs. The \(i\)-th arc leads from the vertex \(x_i\) to the vertex \(y_i\) and has the weight \(w_i\).Your task is to select an integer \(a_v\) for each vertex \(v\), and then write a number \(b_i\) on each arcs \(i\) such that \(b_i = a_{x_i} - a_{y_i}\). You must select the numbers so that: all \(b_i\) are positive; the value of the expression \(\sum \limits_{i = 1}^{m} w_i b_i\) is the lowest possible. It can be shown that for any directed acyclic graph with non-negative \(w_i\), such a way to choose numbers exists.
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 18\); \(0 \le m \le \dfrac{n(n - 1)}{2}\)).Then \(m\) lines follow, the \(i\)-th of them contains three integers \(x_i\), \(y_i\) and \(w_i\) (\(1 \le x_i, y_i \le n\), \(1 \le w_i \le 10^5\), \(x_i \ne y_i\)) β the description of the \(i\)-th arc.It is guaranteed that the lines describe \(m\) arcs of a directed acyclic graph without multiple arcs between the same pair of vertices.
|
Print \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(0 \le a_v \le 10^9\)), which must be written on the vertices so that all \(b_i\) are positive, and the value of the expression \(\sum \limits_{i = 1}^{m} w_i b_i\) is the lowest possible. If there are several answers, print any of them. It can be shown that the answer always exists, and at least one of the optimal answers satisfies the constraints \(0 \le a_v \le 10^9\).
|
Input: 3 2 2 1 4 1 3 2 | Output: 1 2 0
|
Expert
| 6 | 639 | 452 | 428 | 14 |
|
486 |
D
|
486D
|
D. Valid Sets
| 2,100 |
dfs and similar; dp; math; trees
|
As you know, an undirected connected graph with n nodes and n - 1 edges is called a tree. You are given an integer d and a tree consisting of n nodes. Each node i has a value ai associated with it.We call a set S of tree nodes valid if following conditions are satisfied: S is non-empty. S is connected. In other words, if nodes u and v are in S, then all nodes lying on the simple path between u and v should also be presented in S. .Your task is to count the number of valid sets. Since the result can be very large, you must print its remainder modulo 1000000007 (109 + 7).
|
The first line contains two space-separated integers d (0 β€ d β€ 2000) and n (1 β€ n β€ 2000).The second line contains n space-separated positive integers a1, a2, ..., an(1 β€ ai β€ 2000).Then the next n - 1 line each contain pair of integers u and v (1 β€ u, v β€ n) denoting that there is an edge between u and v. It is guaranteed that these edges form a tree.
|
Print the number of valid sets modulo 1000000007.
|
In the first sample, there are exactly 8 valid sets: {1}, {2}, {3}, {4}, {1, 2}, {1, 3}, {3, 4} and {1, 3, 4}. Set {1, 2, 3, 4} is not valid, because the third condition isn't satisfied. Set {1, 4} satisfies the third condition, but conflicts with the second condition.
|
Input: 1 42 1 3 21 21 33 4 | Output: 8
|
Hard
| 4 | 576 | 355 | 49 | 4 |
2,095 |
D
|
2095D
|
D. Where Am I?
| 0 |
*special; geometry
|
Output two real numbers \(-90 \leq a \leq 90\) and \(-180 \leq b \leq 180\), with up to 6 digits after the decimal point.
|
Input: β | Output: β
|
Beginner
| 2 | 0 | 0 | 121 | 20 |
|||
106 |
B
|
106B
|
B. Choosing Laptop
| 1,000 |
brute force; implementation
|
Vasya is choosing a laptop. The shop has n laptops to all tastes.Vasya is interested in the following properties: processor speed, ram and hdd. Vasya is a programmer and not a gamer which is why he is not interested in all other properties.If all three properties of a laptop are strictly less than those properties of some other laptop, then the first laptop is considered outdated by Vasya. Among all laptops Vasya does not consider outdated, he chooses the cheapest one.There are very many laptops, which is why Vasya decided to write a program that chooses the suitable laptop. However, Vasya doesn't have his own laptop yet and he asks you to help him.
|
The first line contains number n (1 β€ n β€ 100).Then follow n lines. Each describes a laptop as speed ram hdd cost. Besides, speed, ram, hdd and cost are integers 1000 β€ speed β€ 4200 is the processor's speed in megahertz 256 β€ ram β€ 4096 the RAM volume in megabytes 1 β€ hdd β€ 500 is the HDD in gigabytes 100 β€ cost β€ 1000 is price in tugriks All laptops have different prices.
|
Print a single number β the number of a laptop Vasya will choose. The laptops are numbered with positive integers from 1 to n in the order in which they are given in the input data.
|
In the third sample Vasya considers the first and fifth laptops outdated as all of their properties cannot match those of the third laptop. The fourth one is the cheapest among the laptops that are left. Thus, Vasya chooses the fourth laptop.
|
Input: 52100 512 150 2002000 2048 240 3502300 1024 200 3202500 2048 80 3002000 512 180 150 | Output: 4
|
Beginner
| 2 | 657 | 375 | 181 | 1 |
1,531 |
A
|
1531A
|
A. ΠΠΈΠ½Π³Π΅Ρ | color
| 0 |
*special; implementation
|
ΠΠ΅ΠΌΠ½ΠΎΠ³ΠΈΠ΅ Π·Π½Π°ΡΡ, ΡΡΠΎ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠΈ ΠΠΠΎΠ½ΡΠ°ΠΊΡΠ΅ ΠΌΠΎΠ³ΡΡ ΠΌΠ΅Π½ΡΡΡ ΡΠ²Π΅Ρ ΠΏΠΎΠ΄ΡΠ²Π΅ΡΠΊΠΈ Π² ΠΊΡΠΏΠΎΠ»Π΅ Π·Π½Π°ΠΌΠ΅Π½ΠΈΡΠΎΠ³ΠΎ ΠΠΎΠΌΠ° ΠΠΈΠ½Π³Π΅ΡΠ°, Π³Π΄Π΅ ΡΠ°ΡΠΏΠΎΠ»ΠΎΠΆΠ΅Π½Π° ΡΡΠ°Π±-ΠΊΠ²Π°ΡΡΠΈΡΠ° ΠΠΠΎΠ½ΡΠ°ΠΊΡΠ΅. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ Π½ΡΠΆΠ½ΠΎ Π²ΡΠ΅Π³ΠΎ Π»ΠΈΡΡ ΠΎΡΠΏΡΠ°Π²ΠΈΡΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Ρ ΡΠ²Π΅ΡΠΎΠΌ Π² ΡΠΏΠ΅ΡΠΈΠ°Π»ΡΠ½ΡΠΉ ΡΠ°Ρ Β«ΠΠΈΠ½Π³Π΅Ρ | colorΒ», Π° Π±ΠΎΡ Π΅Π³ΠΎ ΡΠ°ΡΠΏΠΎΠ·Π½Π°Π΅Ρ ΠΈ ΡΠΌΠ΅Π½ΠΈΡ ΠΏΠΎΠ΄ΡΠ²Π΅ΡΠΊΡ. ΠΡΠΈ ΡΡΠΎΠΌ Π½Π° Π²ΡΠ΅ΠΌΡ Π³ΠΎΡΠΎΠ΄ΡΠΊΠΈΡ
ΠΌΠ΅ΡΠΎΠΏΡΠΈΡΡΠΈΠΉ ΡΠΌΠ΅Π½Π° ΡΠ²Π΅ΡΠ° Π±Π»ΠΎΠΊΠΈΡΡΠ΅ΡΡΡ.Π€ΠΎΡΠΌΠ°Π»ΡΠ½ΠΎ, Π±ΠΎΡ ΠΎΠ±ΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ ΡΡΠΈ ΡΠΈΠΏΠ° ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ: lock: Π·Π°Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°ΡΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΡΠ²Π΅ΡΠ°. ΠΡΠ»ΠΈ ΠΎΠ½ΠΎ ΠΈ ΡΠ°ΠΊ Π·Π°Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°Π½ΠΎ Π½Π° Π΄Π°Π½Π½ΡΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ, ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΠΈΠ³Π½ΠΎΡΠΈΡΡΠ΅ΡΡΡ. unlock: ΡΠ°Π·Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°ΡΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΡΠ²Π΅ΡΠ°. ΠΡΠ»ΠΈ ΠΎΠ½ΠΎ ΠΈ ΡΠ°ΠΊ ΡΠ°Π·Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°Π½ΠΎ Π½Π° Π΄Π°Π½Π½ΡΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ, ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΠΈΠ³Π½ΠΎΡΠΈΡΡΠ΅ΡΡΡ. red / orange / yellow / green / blue / indigo / violet: ΠΈΠ·ΠΌΠ΅Π½ΠΈΡΡ ΡΠ²Π΅Ρ ΠΊΡΠΏΠΎΠ»Π° Π½Π° Π·Π°Π΄Π°Π½Π½ΡΠΉ, Π΅ΡΠ»ΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΡΠ²Π΅ΡΠ° Π½Π° Π΄Π°Π½Π½ΡΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ Π½Π΅ Π·Π°Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°Π½ΠΎ. ΠΠ°ΠΌ Π΄Π°Π½Π° ΠΈΡΡΠΎΡΠΈΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ, ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΡ
Π±ΠΎΡΠΎΠΌ, Π² Ρ
ΡΠΎΠ½ΠΎΠ»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ ΠΏΠ΅ΡΠ΅Π΄ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ΠΌ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ ΠΊΡΠΏΠΎΠ» ΠΏΠΎΠ΄ΡΠ²Π΅ΡΠΈΠ²Π°Π΅ΡΡΡ Π³ΠΎΠ»ΡΠ±ΡΠΌ (blue), Π° ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ ΡΠ²Π΅ΡΠ° Π½Π΅ Π·Π°Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°Π½ΠΎ.ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΠ΅, ΠΊΠ°ΠΊΠΎΠΉ ΡΠ²Π΅Ρ Π±ΡΠ΄Π΅Ρ Ρ ΠΊΡΠΏΠΎΠ»Π° ΠΠΎΠΌΠ° ΠΠΈΠ½Π³Π΅ΡΠ° ΠΏΠΎΡΠ»Π΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠΈ ΡΡΠΈΡ
ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ.
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π·Π°Π΄Π°Π½ΠΎ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ \(n\) (\(1 \le n \le 100\)) β ΡΠΈΡΠ»ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ, ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΡ
Π±ΠΎΡΠΎΠΌ.Π ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
\(n\) ΡΡΡΠΎΠΊΠ°Ρ
Π·Π°Π΄Π°Π½Ρ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ, ΠΏΠΎΠ»ΡΡΠ΅Π½Π½ΡΠ΅ Π±ΠΎΡΠΎΠΌ, Π² Ρ
ΡΠΎΠ½ΠΎΠ»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅, ΠΏΠΎ ΠΎΠ΄Π½ΠΎΠΌΡ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ Π² ΡΡΡΠΎΠΊΠ΅. ΠΠ°ΠΆΠ΄ΠΎΠ΅ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ β ΡΡΡΠΎΠΊΠ° ΠΈΠ· ΡΠ»Π΅Π΄ΡΡΡΠ΅Π³ΠΎ Π½Π°Π±ΠΎΡΠ°: lock, unlock, red, orange, yellow, green, blue, indigo, violet.
|
ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠ²Π΅Ρ ΠΊΡΠΏΠΎΠ»Π° ΠΏΠΎΡΠ»Π΅ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠΈ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠΉ Π±ΠΎΡΠΎΠΌ.
|
Input: 7 red violet unlock red orange lock indigo | Output: orange
|
Beginner
| 2 | 1,032 | 334 | 53 | 15 |
|
2,094 |
C
|
2094C
|
C. Brr Brrr Patapim
| 900 |
math
|
Brr Brrr Patapim is trying to learn of TiramisΓΉ's secret passcode, which is a permutation\(^{\text{β}}\) of \(2\cdot n\) elements. To help Patapim guess, TiramisΓΉ gave him an \(n\times n\) grid \(G\), in which \(G_{i,j}\) (or the element in the \(i\)-th row and \(j\)-th column of the grid) contains \(p_{i+j}\), or the \((i+j)\)-th element in the permutation. Given this grid, please help Patapim crack the forgotten code. It is guaranteed that the permutation exists, and it can be shown that the permutation can be determined uniquely.\(^{\text{β}}\)A permutation of \(m\) integers is a sequence of \(m\) integers which contains each of \(1,2,\ldots,m\) exactly once. For example, \([1, 3, 2]\) and \([2, 1]\) are permutations, while \([1, 2, 4]\) and \([1, 3, 2, 3]\) are not.
|
The first line contains an integer \(t\) β the number of test cases (\(1 \leq t \leq 200\)).The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 800\)).Each of the following \(n\) lines contains \(n\) integers, giving the grid \(G\). The first of these lines contains \(G_{1,1}, G_{1,2},\ldots,G_{1,n}\); the second of these lines contains \(G_{2,1}, G_{2,2},\ldots,G_{2,n}\), and so on. (\(1 \leq G_{i,j} \leq 2\cdot n\)).It is guaranteed that the grid encodes a valid permutation, and the sum of \(n\) over all test cases does not exceed \(800\).
|
For each test case, please output \(2n\) numbers on a new line: \(p_1,p_2,\ldots,p_{2n}\).
|
Input: 331 6 26 2 42 4 31122 33 4 | Output: 5 1 6 2 4 3 2 1 1 2 3 4
|
Beginner
| 1 | 780 | 571 | 90 | 20 |
|
459 |
B
|
459B
|
B. Pashmak and Flowers
| 1,300 |
combinatorics; implementation; sortings
|
Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!Your task is to write a program which calculates two things: The maximum beauty difference of flowers that Pashmak can give to Parmida. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.
|
The first line of the input contains n (2 β€ n β€ 2Β·105). In the next line there are n space-separated integers b1, b2, ..., bn (1 β€ bi β€ 109).
|
The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.
|
In the third sample the maximum beauty difference is 2 and there are 4 ways to do this: choosing the first and the second flowers; choosing the first and the fifth flowers; choosing the fourth and the second flowers; choosing the fourth and the fifth flowers.
|
Input: 21 2 | Output: 1 1
|
Easy
| 3 | 672 | 141 | 136 | 4 |
1,956 |
E1
|
1956E1
|
E1. Nene vs. Monsters (Easy Version)
| 2,500 |
brute force; implementation; math
|
This is the easy version of the problem. The only difference between the versions is the constraints on \(a_i\). You can make hacks only if both versions of the problem are solved.Nene is fighting with \(n\) monsters, located in a circle. These monsters are numbered from \(1\) to \(n\), and the \(i\)-th (\(1 \le i \le n\)) monster's current energy level is \(a_i\).Since the monsters are too strong, Nene decided to fight with them using the Attack Your Neighbour spell. When Nene uses this spell, the following actions happen in the following order one by one: The \(1\)-st monster attacks the \(2\)-nd monster; The \(2\)-nd monster attacks the \(3\)-rd monster; \(\ldots\) The \((n-1)\)-th monster attacks the \(n\)-th monster; The \(n\)-th monster attacks the \(1\)-st monster. When the monster with energy level \(x\) attacks the monster with the energy level \(y\), the energy level of the defending monster becomes \(\max(0, y-x)\) (the energy level of the attacking monster remains equal to \(x\)).Nene is going to use this spell \(10^{100}\) times and deal with the monsters that will still have a non-zero energy level herself. She wants you to determine which monsters will have a non-zero energy level once she will use the described spell \(10^{100}\) times.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of test cases follows.The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of monsters.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 2 \cdot 10^5\)) β the current energy levels of monsters.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, in the first line output an integer \(m\) β the number of monsters with non-zero energy level after \(10^{100}\) uses of the spell; in the second line of output \(m\) integers \(i_1,i_2,\ldots,i_m\) (\(1 \le i_1 < i_2 < \ldots < i_m \le n\)) β the indices of these monsters in the increasing order. If \(m=0\), you may either output an empty line or don't output it.
|
In the first test case, the following actions happen during the first \(3\) uses of the spell in this order: Nene uses the Attack Your Neighbour spell for the first time; the \(1\)-st monster attacks the \(2\)-nd monster, after the attack the energy level of the \(2\)-nd monster becomes equal to \(\max(0, 5-2)=3\); the \(2\)-nd monster attacks the \(3\)-rd monster, after the attack the energy level of the \(3\)-rd monster becomes equal to \(\max(0, 3-3)=0\); the \(3\)-rd monster attacks the \(1\)-st monster, after the attack the energy level of the \(1\)-st monster becomes equal to \(\max(0, 2-0)=2\); Nene uses the Attack Your Neighbour spell for the second time; the \(1\)-st monster attacks the \(2\)-nd monster, after the attack the energy level of the \(2\)-nd monster becomes equal to \(\max(0, 3-2)=1\); the \(2\)-nd monster attacks the \(3\)-rd monster, after the attack the energy level of the \(3\)-rd monster becomes equal to \(\max(0, 0-1)=0\); the \(3\)-rd monster attacks the \(1\)-st monster, after the attack the energy level of the \(1\)-st monster becomes equal to \(\max(0, 2-0)=2\); Nene uses the Attack Your Neighbour spell for the third time; the \(1\)-st monster attacks the \(2\)-nd monster, after the attack the energy level of the \(2\)-nd monster becomes equal to \(\max(0, 1-2)=0\); the \(2\)-nd monster attacks the \(3\)-rd monster, after the attack the energy level of the \(3\)-rd monster becomes equal to \(\max(0, 0-0)=0\); the \(3\)-rd monster attacks the \(1\)-st monster, after the attack the energy level of the \(1\)-st monster becomes equal to \(\max(0, 2-0)=2\). After each of the next uses of the spell, energy levels of monsters do not change. Thus, only the \(1\)-st monster has a non-zero energy level in the end.In the second test case, both monsters initially have zero energy level.
|
Input: 532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0 | Output: 1 1 0 1 1 2 1 3 6 1 3 6 8 10 12
|
Expert
| 3 | 1,272 | 491 | 386 | 19 |
2,002 |
C
|
2002C
|
C. Black Circles
| 1,200 |
brute force; geometry; greedy; math
|
There are \(n\) circles on a two-dimensional plane. The \(i\)-th circle is centered at \((x_i,y_i)\). Initially, all circles have a radius of \(0\).The circles' radii increase at a rate of \(1\) unit per second.You are currently at \((x_s,y_s)\); your goal is to reach \((x_t,y_t)\) without touching the circumference of any circle (including the moment you reach \((x_t,y_t)\)). You can move in any direction you want. However, your speed is limited to \(1\) unit per second.Please determine whether this is possible.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le10^5\)) β the number of circles.The next \(n\) lines each contain two integers \(x_i\), \(y_i\) (\(1\le x_i,y_i\le10^9\)) β the center of each circle.The final line contains four integers \(x_s\), \(y_s\), \(x_t\), \(y_t\) (\(1\le x_s,y_s,x_t,y_t\le10^9\)) β the coordinates of the starting point and the goal, respectively.It is guaranteed that these \(n+2\) points are distinct.It is guaranteed that the sum of \(n\) over all testcases does not exceed \(10^5\).
|
For each test case, output \(\texttt{YES}\) if it is possible to reach the goal without touching the circle boundaries, and output \(\texttt{NO}\) otherwise.You can output \(\texttt{Yes}\) and \(\texttt{No}\) in any case (for example, strings \(\texttt{yEs}\), \(\texttt{yes}\), \(\texttt{Yes}\), and \(\texttt{YES}\) will be recognized as a positive response).
|
In the first test case, a feasible way of movement is as follows.
|
Input: 732 52 1410 134 9 9 7310 116 912 1214 13 4 815 712 6 11 1321000000000 22 10000000001 1 2 21999999998 1000000000999999999 999999999 1 111000000000 11 1000000000 1 110989237121 2397081206669655 527238537522705783 380636165532545346 320061691207818728 199485303884520552 315781807992311437 802563521205138355 324818663223575704 395073023281560523 236279118216941610 572010615 323956540 794523071 | Output: YES NO YES YES YES NO YES
|
Easy
| 4 | 518 | 698 | 361 | 20 |
1,647 |
B
|
1647B
|
B. Madoka and the Elegant Gift
| 1,200 |
brute force; constructive algorithms; graphs; implementation
|
Madoka's father just reached \(1\) million subscribers on Mathub! So the website decided to send him a personalized award β The Mathhub's Bit Button! The Bit Button is a rectangular table with \(n\) rows and \(m\) columns with \(0\) or \(1\) in each cell. After exploring the table Madoka found out that: A subrectangle \(A\) is contained in a subrectangle \(B\) if there's no cell contained in \(A\) but not contained in \(B\). Two subrectangles intersect if there is a cell contained in both of them. A subrectangle is called black if there's no cell with value \(0\) inside it. A subrectangle is called nice if it's black and it's not contained in another black subrectangle. The table is called elegant if there are no two nice intersecting subrectangles.For example, in the first illustration the red subrectangle is nice, but in the second one it's not, because it's contained in the purple subrectangle. Help Madoka to determine whether the table is elegant.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 200\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two positive integers \(n, m\) (\(1 \le n, m \le 100\)).The next \(n\) lines contain strings of length \(m\) consisting of zeros and ones β the description of the table.It is guaranteed that the sum of the values of \(n\) and the sum of the values of \(m\) for all test cases do not exceed \(777\).
|
For each test case print ""YES"" if its table is elegant or print ""NO"" otherwise.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer).
|
In the second test case the table is not elegant, because the red and the purple subrectangles are nice and intersect. In the fourth test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.
|
Input: 53 31000110113 31101111101 5011114 5111110101001000010003 2110011 | Output: YES NO YES NO YES
|
Easy
| 4 | 965 | 515 | 213 | 16 |
69 |
B
|
69B
|
B. Bets
| 1,200 |
greedy; implementation
|
In Chelyabinsk lives a much respected businessman Nikita with a strange nickname ""Boss"". Once Nikita decided to go with his friend Alex to the Summer Biathlon World Cup. Nikita, as a very important person, received a token which allows to place bets on each section no more than on one competitor.To begin with friends learned the rules: in the race there are n sections of equal length and m participants. The participants numbered from 1 to m. About each participant the following is known: li β the number of the starting section, ri β the number of the finishing section (li β€ ri), ti β the time a biathlete needs to complete an section of the path, ci β the profit in roubles. If the i-th sportsman wins on one of the sections, the profit will be given to the man who had placed a bet on that sportsman. The i-th biathlete passes the sections from li to ri inclusive. The competitor runs the whole way in (ri - li + 1)Β·ti time units. It takes him exactly ti time units to pass each section. In case of the athlete's victory on k sections the man who has betted on him receives kΒ·ci roubles.In each section the winner is determined independently as follows: if there is at least one biathlete running this in this section, then among all of them the winner is the one who has ran this section in minimum time (spent minimum time passing this section). In case of equality of times the athlete with the smaller index number wins. If there are no participants in this section, then the winner in this section in not determined. We have to say that in the summer biathlon all the participants are moving at a constant speed.We should also add that Nikita can bet on each section and on any contestant running in this section.Help the friends find the maximum possible profit.
|
The first line contains two integers n and m (1 β€ n, m β€ 100). Then follow m lines, each containing 4 integers li, ri, ti, ci (1 β€ li β€ ri β€ n, 1 β€ ti, ci β€ 1000).
|
Print a single integer, the maximal profit in roubles that the friends can get. In each of n sections it is not allowed to place bets on more than one sportsman.
|
In the first test the optimal bet is: in the 1-2 sections on biathlete 1, in section 3 on biathlete 3, in section 4 on biathlete 4. Total: profit of 5 rubles for 1 section, the profit of 5 rubles for 2 section, profit of 30 rubles for a 3 section, profit of 20 rubles for 4 section. Total profit 60 rubles.In the second test the optimal bet is: on 1 and 5 sections on biathlete 1, in the 2-4 sections on biathlete 2, in the 6-7 sections on athlete 4. There is no winner in the 8 section. Total: profit of 10 rubles for 1 section, the profit of 15 rubles for 2,3,4 section, profit of 10 rubles for a 5 section, profit of 20 rubles for 6, 7 section. Total profit 105 rubles.
|
Input: 4 41 4 20 51 3 21 103 3 4 303 4 4 20 | Output: 60
|
Easy
| 2 | 1,778 | 163 | 161 | 0 |
1,599 |
E
|
1599E
|
E. Two Arrays
| 3,200 |
data structures; matrices
|
You are given two integer arrays of length \(N\), \(A1\) and \(A2\). You are also given \(Q\) queries of 4 types: 1 k l r x: set \(Ak_i:=min(Ak_i, x)\) for each \(l \leq i \leq r\).2 k l r x: set \(Ak_i:=max(Ak_i, x)\) for each \(l \leq i \leq r\).3 k l r x: set \(Ak_i:=Ak_i+x\) for each \(l \leq i \leq r\).4 l r: find the \((\sum_{i=l}^r F(A1_i+A2_i)) \% (10^9+7)\) where \(F(k)\) is the \(k\)-th Fibonacci number (\(F(0)=0, F(1)=1, F(k)=F(k-1)+F(k-2)\)), and \(x \% y\) denotes the remainder of the division of \(x\) by \(y\).You should process these queries and answer each query of the fourth type.
|
The first line contains two integers \(N\) and \(Q\). (\(1 \leq N, Q \leq 5 \times 10^4\)) The second line contains \(N\) integers, array \(A1_1, A1_2, \dots A1_N\). (\(0 \leq A1_i \leq 10^6\))The third line contains \(N\) integers, array \(A2_1, A2_2, \dots A2_N\). (\(0 \leq A2_i \leq 10^6\))The next \(Q\) lines describe the queries. Each line contains 5 or 3 integers, where the first integer denotes the type of the query. (\(k \in \{1, 2\}\), \(1 \leq l \leq r \leq N\))For queries of type 1 and 2, \(0 \leq x \leq 10^9\) holds.For queries of type 3, \(β10^6 \leq x \leq 10^6\) holds.It is guaranteed that after every query each number in arrays \(A1\) and \(A2\) will be nonnegative.
|
Print the answer to each query of the fourth type, in separate lines.
|
In the first example: The answer for the first query is \(F(1 + 2) + F(0 + 1) + F(2 + 0) = F(3) + F(1) + F(2) = 2 + 1 + 1 = 4\). After the second query, the array \(A2\) changes to \([2, 4, 0]\). After the third query, the array \(A1\) changes to \([0, 0, 0]\). The answer for the fourth query is \(F(0 + 2) + F(0 + 4) + F(0 + 0) = F(2) + F(4) + F(0) = 1 + 3 + 0 = 4\). In the second example: The answer for the first query is \(F(1 + 4) + F(3 + 2) + F(5 + 1) = F(5) + F(5) + F(6) = 5 + 5 + 8 = 18\). The answer for the second query is \(F(3 + 2) + F(5 + 1) + F(3 + 3) + F(2 + 3) = F(5) + F(6) + F(6) + F(5) = 5 + 8 + 8 + 5 = 26\). After the third query, the array \(A1\) changes to \([1, 6, 6, 6, 2]\). The answer for the fourth query is \(F(6 + 2) + F(6 + 1) + F(6 + 3) = F(8) + F(7) + F(9) = 21 + 13 + 34 = 68\).
|
Input: 3 4 1 0 2 2 1 0 4 1 3 3 2 2 2 3 1 1 1 3 0 4 1 3 | Output: 4 4
|
Master
| 2 | 604 | 690 | 69 | 15 |
813 |
F
|
813F
|
F. Bipartite Checking
| 2,500 |
data structures; dsu; graphs
|
You are given an undirected graph consisting of n vertices. Initially there are no edges in the graph. Also you are given q queries, each query either adds one undirected edge to the graph or removes it. After each query you have to check if the resulting graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color).
|
The first line contains two integers n and q (2 β€ n, q β€ 100000).Then q lines follow. ith line contains two numbers xi and yi (1 β€ xi < yi β€ n). These numbers describe ith query: if there is an edge between vertices xi and yi, then remove it, otherwise add it.
|
Print q lines. ith line must contain YES if the graph is bipartite after ith query, and NO otherwise.
|
Input: 3 52 31 31 21 21 2 | Output: YESYESNOYESNO
|
Expert
| 3 | 409 | 260 | 101 | 8 |
|
152 |
B
|
152B
|
B. Steps
| 1,300 |
binary search; implementation
|
One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: ""How did he do that?"" The answer is simple.Vasya noticed that the yard is a rectangular n Γ m field. The squares have coordinates (x, y) (1 β€ x β€ n, 1 β€ y β€ m), where x is the index of the row and y is the index of the column.Initially Vasya stands in the square with coordinates (xc, yc). To play, he has got a list of k vectors (dxi, dyi) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to k, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps).A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (x, y), and the current vector is (dx, dy), one step moves Vasya to square (x + dx, y + dy). A step is considered valid, if the boy does not go out of the yard if he performs the step.Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made.
|
The first input line contains two integers n and m (1 β€ n, m β€ 109) β the yard's sizes. The second line contains integers xc and yc β the initial square's coordinates (1 β€ xc β€ n, 1 β€ yc β€ m).The third line contains an integer k (1 β€ k β€ 104) β the number of vectors. Then follow k lines, each of them contains two integers dxi and dyi (|dxi|, |dyi| β€ 109, |dx| + |dy| β₯ 1).
|
Print the single number β the number of steps Vasya had made.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
|
In the first sample Vasya is initially positioned at square (1, 1) and makes 3 steps by the first vector (1, 1). So, he consecutively visits the squares (2, 2), (3, 3), (4, 4). Then he makes 0 steps by the second vector (1, 1). He makes 1 more step by the third vector (0, - 2) and he ends up in square (4, 2). Overall, Vasya makes 4 steps.In the second sample Vasya is initially positioned in square (1, 2) and makes 0 steps by vector ( - 1, 0), as the square with coordinates (0, 2) is located outside the yard.
|
Input: 4 51 131 11 10 -2 | Output: 4
|
Easy
| 2 | 1,461 | 374 | 213 | 1 |
1,830 |
A
|
1830A
|
A. Copil Copac Draws Trees
| 1,400 |
dfs and similar; dp; graphs; trees
|
Copil Copac is given a list of \(n-1\) edges describing a tree of \(n\) vertices. He decides to draw it using the following algorithm: Step \(0\): Draws the first vertex (vertex \(1\)). Go to step \(1\). Step \(1\): For every edge in the input, in order: if the edge connects an already drawn vertex \(u\) to an undrawn vertex \(v\), he will draw the undrawn vertex \(v\) and the edge. After checking every edge, go to step \(2\). Step \(2\): If all the vertices are drawn, terminate the algorithm. Else, go to step \(1\). The number of readings is defined as the number of times Copil Copac performs step \(1\).Find the number of readings needed by Copil Copac to draw the tree.
|
Each test contains multiple test cases. The first line of input contains a single 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 a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of vertices of the tree.The following \(n - 1\) lines of each test case contain two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \neq v_i\)) β indicating that \((u_i,v_i)\) is the \(i\)-th edge in the list. It is guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the number of readings Copil Copac needs to draw the tree.
|
In the first test case:After the first reading, the tree will look like this: After the second reading: Therefore, Copil Copac needs \(2\) readings to draw the tree.
|
Input: 264 51 31 23 41 675 62 42 71 31 24 5 | Output: 2 3
|
Easy
| 4 | 679 | 662 | 85 | 18 |
1,742 |
A
|
1742A
|
A. Sum
| 800 |
implementation
|
You are given three integers \(a\), \(b\), and \(c\). Determine if one of them is the sum of the other two.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 9261\)) β the number of test cases.The description of each test case consists of three integers \(a\), \(b\), \(c\) (\(0 \leq a, b, c \leq 20\)).
|
For each test case, output ""YES"" if one of the numbers is the sum of the other two, and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
|
In the first test case, \(1 + 3 = 4\).In the second test case, none of the numbers is the sum of the other two.In the third test case, \(9 + 11 = 20\).
|
Input: 71 4 32 5 89 11 200 0 020 20 204 12 315 7 8 | Output: YES NO YES YES NO NO YES
|
Beginner
| 1 | 107 | 209 | 250 | 17 |
1,360 |
A
|
1360A
|
A. Minimal Square
| 800 |
greedy; math
|
Find the minimum area of a square land on which you can place two identical rectangular \(a \times b\) houses. The sides of the houses should be parallel to the sides of the desired square land.Formally, You are given two identical rectangles with side lengths \(a\) and \(b\) (\(1 \le a, b \le 100\)) β positive integers (you are given just the sizes, but not their positions). Find the square of the minimum area that contains both given rectangles. Rectangles can be rotated (both or just one), moved, but the sides of the rectangles should be parallel to the sides of the desired square. Two rectangles can touch each other (side or corner), but cannot intersect. Rectangles can also touch the sides of the square but must be completely inside it. You can rotate the rectangles. Take a look at the examples for a better understanding. The picture shows a square that contains red and green rectangles.
|
The first line contains an integer \(t\) (\(1 \le t \le 10\,000\)) βthe number of test cases in the input. Then \(t\) test cases follow.Each test case is a line containing two integers \(a\), \(b\) (\(1 \le a, b \le 100\)) β side lengths of the rectangles.
|
Print \(t\) answers to the test cases. Each answer must be a single integer β minimal area of square land, that contains two rectangles with dimensions \(a \times b\).
|
Below are the answers for the first two test cases:
|
Input: 8 3 2 4 2 1 1 3 1 4 7 1 3 7 4 100 100 | Output: 16 16 4 9 64 9 64 40000
|
Beginner
| 2 | 905 | 256 | 167 | 13 |
753 |
B
|
753B
|
B. Interactive Bulls and Cows (Easy)
| 1,600 |
brute force; constructive algorithms; implementation
|
This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java β call System.out.flush(), and in Pascal β flush(output).Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking paper and pencil game for two players, predating the similar commercially marketed board game Mastermind.On a sheet of paper, the first player thinks a secret string. This string consists only of digits and has the length 4. The digits in the string must be all different, no two or more equal digits are allowed.Then the second player tries to guess his opponent's string. For every guess the first player gives the number of matches. If the matching digits are on their right positions, they are ""bulls"", if on different positions, they are ""cows"". Thus a response is a pair of numbers β the number of ""bulls"" and the number of ""cows"". A try can contain equal digits.More formally, let's the secret string is s and the second player are trying to guess it with a string x. The number of ""bulls"" is a number of such positions i (1 β€ i β€ 4) where s[i] = x[i]. The number of ""cows"" is a number of such digits c that s contains c in the position i (i.e. s[i] = c), x contains c, but x[i] β c.For example, the secret string is ""0427"", the opponent's try is ""0724"", then the answer is 2 bulls and 2 cows (the bulls are ""0"" and ""2"", the cows are ""4"" and ""7""). If the secret string is ""0123"", the opponent's try is ""0330"", then the answer is 1 bull and 1 cow.In this problem you are to guess the string s that the system has chosen. You only know that the chosen string consists of 4 distinct digits.You can make queries to the testing system, each query is the output of a single 4-digit string. The answer to the query is the number of bulls and number of cows. If the system's response equals ""4 0"", that means the interaction with your problem is over and the program must terminate. That is possible for two reasons β the program either guessed the number x or made an invalid action (for example, printed letters instead of digits).Your program is allowed to do at most 50 queries.You can hack solutions of other participants providing a 4-digit string containing distinct digits β the secret string.
|
To read answers to the queries, the program must use the standard input.The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string s and the string xi printed by your program. If the system response equals ""4 0"", then your solution should terminate.The testing system will let your program read the i-th pair of integers from the input only after your program displays the corresponding system query in the output: prints value xi in a single line and executes operation flush.
|
The program must use the standard output to print queries.Your program must output requests β 4-digit strings x1, x2, ..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.Your program is allowed to do at most 50 queries.
|
The secret string s in the example is ""0123"".
|
Input: 0 12 01 10 42 14 0 | Output: 800001793159321001120123
|
Medium
| 3 | 2,583 | 610 | 330 | 7 |
208 |
B
|
208B
|
B. Solitaire
| 1,900 |
dfs and similar; dp
|
A boy named Vasya wants to play an old Russian solitaire called ""Accordion"". In this solitaire, the player must observe the following rules: A deck of n cards is carefully shuffled, then all n cards are put on the table in a line from left to right; Before each move the table has several piles of cards lying in a line (initially there are n piles, each pile has one card). Let's number the piles from left to right, from 1 to x. During one move, a player can take the whole pile with the maximum number x (that is the rightmost of remaining) and put it on the top of pile x - 1 (if it exists) or on the top of pile x - 3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile x goes on top of pile y, then the top card of pile x becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; The solitaire is considered completed if all cards are in the same pile. Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not.
|
The first input line contains a single integer n (1 β€ n β€ 52) β the number of cards in Vasya's deck. The next line contains n space-separated strings c1, c2, ..., cn, where string ci describes the i-th card on the table. Each string ci consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right. A card's value is specified by one of these characters: ""2"", ""3"", ""4"", ""5"", ""6"", ""7"", ""8"", ""9"", ""T"", ""J"", ""Q"", ""K"", ""A"". A card's suit is specified by one of these characters: ""S"", ""D"", ""H"", ""C"".It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat.
|
On a single line print the answer to the problem: string ""YES"" (without the quotes) if completing the solitaire is possible, string ""NO"" (without the quotes) otherwise.
|
In the first sample you can act like that: put the 4-th pile on the 1-st one; put the 3-rd pile on the 2-nd one; put the 2-nd pile on the 1-st one. In the second sample there is no way to complete the solitaire.
|
Input: 42S 2S 2C 2C | Output: YES
|
Hard
| 2 | 1,150 | 734 | 172 | 2 |
1,583 |
E
|
1583E
|
E. Moment of Bloom
| 2,200 |
constructive algorithms; dfs and similar; graph matchings; graphs; greedy; trees
|
She does her utmost to flawlessly carry out a person's last rites and preserve the world's balance of yin and yang.Hu Tao, being the little prankster she is, has tried to scare you with this graph problem! You are given a connected undirected graph of \(n\) nodes with \(m\) edges. You also have \(q\) queries. Each query consists of two nodes \(a\) and \(b\).Initially, all edges in the graph have a weight of \(0\). For each query, you must choose a simple path starting from \(a\) and ending at \(b\). Then you add \(1\) to every edge along this path. Determine if it's possible, after processing all \(q\) queries, for all edges in this graph to have an even weight. If so, output the choice of paths for each query. If it is not possible, determine the smallest number of extra queries you could add to make it possible. It can be shown that this number will not exceed \(10^{18}\) under the given constraints.A simple path is defined as any path that does not visit a node more than once.An edge is said to have an even weight if its value is divisible by \(2\).
|
The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 3 \cdot 10^5\), \(n-1 \leq m \leq \min{\left(\frac{n(n-1)}{2}, 3 \cdot 10^5\right)}\)).Each of the next \(m\) lines contains two integers \(x\) and \(y\) (\(1 \leq x, y \leq n\), \(x\neq y\)) indicating an undirected edge between node \(x\) and \(y\). The input will not contain self-loops or duplicate edges, and the provided graph will be connected.The next line contains a single integer \(q\) (\(1 \leq q \leq 3 \cdot 10^5\)).Each of the next \(q\) lines contains two integers \(a\) and \(b\) (\(1 \leq a, b \leq n, a \neq b\)), the description of each query.It is guaranteed that \(nq \leq 3 \cdot 10^5\).
|
If it is possible to force all edge weights to be even, print ""YES"" on the first line, followed by \(2q\) lines indicating the choice of path for each query in the same order the queries are given. For each query, the first line should contain a single integer \(x\): the number of nodes in the chosen path. The next line should then contain \(x\) spaced separated integers \(p_i\) indicating the path you take (\(p_1 = a, p_x = b\) and all numbers should fall between \(1\) and \(n\)). This path cannot contain duplicate nodes and must be a valid simple path in the graph.If it is impossible to force all edge weights to be even, print ""NO"" on the first line and the minimum number of added queries on the second line.
|
Here is what the queries look like for the first test case (red corresponds to the 1st query, blue 2nd query, and green 3rd query): Notice that every edge in the graph is part of either \(0\) or \(2\) colored query edges.The graph in the second test case looks like this: There does not exist an assignment of paths that will force all edges to have even weights with the given queries. One must add at least \(2\) new queries to obtain a set of queries that can satisfy the condition.
|
Input: 6 7 2 1 2 3 3 5 1 4 6 1 5 6 4 5 3 1 4 5 1 4 5 | Output: YES 2 1 4 4 5 3 2 1 5 4 1 2 3 5
|
Hard
| 6 | 1,068 | 680 | 723 | 15 |
414 |
E
|
414E
|
E. Mashmokh's Designed Problem
| 3,200 |
data structures
|
After a lot of trying, Mashmokh designed a problem and it's your job to solve it.You have a tree T with n vertices. Each vertex has a unique index from 1 to n. The root of T has index 1. For each vertex of this tree v, you are given a list of its children in a specific order. You must perform three types of query on this tree: find distance (the number of edges in the shortest path) between u and v; given v and h, disconnect v from its father and connect it to its h-th ancestor; more formally, let's denote the path from v to the root by x1, x2, ..., xl (h < l), so that x1 = v and xl is root; disconnect v from its father (x2) and connect it to xh + 1; vertex v must be added to the end of the child-list of vertex xh + 1; in the vertex sequence produced by calling function dfs(root) find the latest vertex that has distance k from the root. The pseudo-code of function dfs(v): // ls[v]: list of children of vertex v // its i-th element is ls[v][i]// its size is size(ls[v])sequence result = empty sequence;void dfs(vertex now){ add now to end of result; for(int i = 1; i <= size(ls[v]); i = i + 1) //loop from i = 1 to i = size(ls[v]) dfs(ls[v][i]);}
|
The first line of input contains two space-separated integers n, m (2 β€ n β€ 105; 1 β€ m β€ 105), the number of vertices of T and number of queries to perform.The i-th of the following n lines contains an integer li (0 β€ li β€ n), number of i-th vertex's children. Then li space-separated integers follow, the j-th of them is the index of j-th child of i-th vertex. Note that the order of these vertices is important.Each of the following m lines has one of the following format: ""1 v u"", ""2 v h"", or ""3 k"". The first number in the line is the type of query to perform according to the problem statement. The next numbers are description of the query.It's guaranteed that all the queries are correct. For example, in the second-type query h is at least 2 and at most distance of v from root. Also in the third-type query there is at least one vertex with distance k from the root at the time the query is given.
|
For each query of the first or third type output one line containing the result of the query.
|
Input: 4 91 21 31 401 1 42 4 21 3 43 13 22 3 21 1 23 13 2 | Output: 3224134
|
Master
| 1 | 1,158 | 913 | 93 | 4 |
|
859 |
D
|
859D
|
D. Third Month Insanity
| 2,100 |
dp; probabilities; trees
|
The annual college sports-ball tournament is approaching, which for trademark reasons we'll refer to as Third Month Insanity. There are a total of 2N teams participating in the tournament, numbered from 1 to 2N. The tournament lasts N rounds, with each round eliminating half the teams. The first round consists of 2N - 1 games, numbered starting from 1. In game i, team 2Β·i - 1 will play against team 2Β·i. The loser is eliminated and the winner advances to the next round (there are no ties). Each subsequent round has half as many games as the previous round, and in game i the winner of the previous round's game 2Β·i - 1 will play against the winner of the previous round's game 2Β·i.Every year the office has a pool to see who can create the best bracket. A bracket is a set of winner predictions for every game. For games in the first round you may predict either team to win, but for games in later rounds the winner you predict must also be predicted as a winner in the previous round. Note that the bracket is fully constructed before any games are actually played. Correct predictions in the first round are worth 1 point, and correct predictions in each subsequent round are worth twice as many points as the previous, so correct predictions in the final game are worth 2N - 1 points.For every pair of teams in the league, you have estimated the probability of each team winning if they play against each other. Now you want to construct a bracket with the maximum possible expected score.
|
Input will begin with a line containing N (2 β€ N β€ 6).2N lines follow, each with 2N integers. The j-th column of the i-th row indicates the percentage chance that team i will defeat team j, unless i = j, in which case the value will be 0. It is guaranteed that the i-th column of the j-th row plus the j-th column of the i-th row will add to exactly 100.
|
Print the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10 - 9.Formally, let your answer be a, and the jury's answer be b. Your answer will be considered correct, if .
|
In the first example, you should predict teams 1 and 4 to win in round 1, and team 1 to win in round 2. Recall that the winner you predict in round 2 must also be predicted as a winner in round 1.
|
Input: 20 40 100 10060 0 40 400 60 0 450 60 55 0 | Output: 1.75
|
Hard
| 3 | 1,498 | 354 | 252 | 8 |
1,462 |
A
|
1462A
|
A. Favorite Sequence
| 800 |
implementation; two pointers
|
Polycarp has a favorite sequence \(a[1 \dots n]\) consisting of \(n\) integers. He wrote it out on the whiteboard as follows: he wrote the number \(a_1\) to the left side (at the beginning of the whiteboard); he wrote the number \(a_2\) to the right side (at the end of the whiteboard); then as far to the left as possible (but to the right from \(a_1\)), he wrote the number \(a_3\); then as far to the right as possible (but to the left from \(a_2\)), he wrote the number \(a_4\); Polycarp continued to act as well, until he wrote out the entire sequence on the whiteboard. The beginning of the result looks like this (of course, if \(n \ge 4\)). For example, if \(n=7\) and \(a=[3, 1, 4, 1, 5, 9, 2]\), then Polycarp will write a sequence on the whiteboard \([3, 4, 5, 2, 9, 1, 1]\).You saw the sequence written on the whiteboard and now you want to restore Polycarp's favorite sequence.
|
The first line contains a single positive integer \(t\) (\(1 \le t \le 300\)) β the number of test cases in the test. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 300\)) β the length of the sequence written on the whiteboard.The next line contains \(n\) integers \(b_1, b_2,\ldots, b_n\) (\(1 \le b_i \le 10^9\)) β the sequence written on the whiteboard.
|
Output \(t\) answers to the test cases. Each answer β is a sequence \(a\) that Polycarp wrote out on the whiteboard.
|
In the first test case, the sequence \(a\) matches the sequence from the statement. The whiteboard states after each step look like this:\([3] \Rightarrow [3, 1] \Rightarrow [3, 4, 1] \Rightarrow [3, 4, 1, 1] \Rightarrow [3, 4, 5, 1, 1] \Rightarrow [3, 4, 5, 9, 1, 1] \Rightarrow [3, 4, 5, 2, 9, 1, 1]\).
|
Input: 6 7 3 4 5 2 9 1 1 4 9 2 7 1 11 8 4 3 1 2 7 8 7 9 4 2 1 42 2 11 7 8 1 1 1 1 1 1 1 1 | Output: 3 1 4 1 5 9 2 9 1 2 7 8 2 4 4 3 9 1 7 2 8 7 42 11 7 1 1 1 1 1 1 1 1
|
Beginner
| 2 | 890 | 412 | 116 | 14 |
2,005 |
B2
|
2005B2
|
B2. The Strict Teacher (Hard Version)
| 1,200 |
binary search; greedy; math; sortings
|
This is the hard version of the problem. The only differences between the two versions are the constraints on \(m\) and \(q\). In this version, \(m, q \le 10^5\). You can make hacks only if both versions of the problem are solved.Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now \(m\) teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide.The classroom can be represented as a one-dimensional line with cells from \(1\) to \(n\), inclusive.At the start, all \(m\) teachers and David are in distinct cells. Then they make moves. During each move David goes to an adjacent cell or stays at the current one. Then, each of the \(m\) teachers simultaneously goes to an adjacent cell or stays at the current one. This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally.Your task is to find how many moves it will take for the teachers to catch David if they all act optimally.Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student.Also, as Narek and Tsovak think this task is easy, they decided to give you \(q\) queries on David's position.
|
In the first line of the input, you are given a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The description of each test case follows.In the first line of each test case, you are given three integers \(n\), \(m\), and \(q\) (\(3 \le n \le 10^9\), \(1 \le m, q \le 10^5\)) β the number of cells on the line, the number of teachers, and the number of queries.In the second line of each test case, you are given \(m\) distinct integers \(b_1, b_2, \ldots, b_m\) (\(1 \le b_i \le n\)) β the cell numbers of the teachers.In the third line of each test case, you are given \(q\) integers \(a_1, a_2, \ldots, a_q\) (\(1 \le a_i \le n\)) β David's cell number for every query.It is guaranteed that for any \(i\), \(j\) such that \(1 \le i \le m\) and \(1 \le j \le q\), \(b_i \neq a_j\).It is guaranteed that the sum of values of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).It is guaranteed that the sum of values of \(q\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(q\) lines, the \(i\)-th of them containing the answer of the \(i\)-th query.
|
In the only query of the first example, the student can run to cell \(1\). It will take the teacher five moves to reach from cell \(6\) to cell \(1\), so the answer is \(5\).In the second query of the second example, the student can just stay at cell \(3\). The teacher, initially located in cell \(4\), can reach cell \(3\) in one move. Therefore, the answer is \(1\).
|
Input: 28 1 16310 3 31 4 82 3 10 | Output: 5 1 1 2
|
Easy
| 4 | 1,658 | 1,011 | 105 | 20 |
1,183 |
C
|
1183C
|
C. Computer Game
| 1,400 |
binary search; math
|
Vova is playing a computer game. There are in total \(n\) turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is \(k\).During each turn Vova can choose what to do: If the current charge of his laptop battery is strictly greater than \(a\), Vova can just play, and then the charge of his laptop battery will decrease by \(a\); if the current charge of his laptop battery is strictly greater than \(b\) (\(b<a\)), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by \(b\); if the current charge of his laptop battery is less than or equal to \(a\) and \(b\) at the same time then Vova cannot do anything and loses the game. Regardless of Vova's turns the charge of the laptop battery is always decreases.Vova wants to complete the game (Vova can complete the game if after each of \(n\) turns the charge of the laptop battery is strictly greater than \(0\)). Vova has to play exactly \(n\) turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.You have to answer \(q\) independent queries.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 10^5\)) β the number of queries. Each query is presented by a single line.The only line of the query contains four integers \(k, n, a\) and \(b\) (\(1 \le k, n \le 10^9, 1 \le b < a \le 10^9\)) β the initial charge of Vova's laptop battery, the number of turns in the game and values \(a\) and \(b\), correspondingly.
|
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.
|
In the first example query Vova can just play \(4\) turns and spend \(12\) units of charge and then one turn play and charge and spend \(2\) more units. So the remaining charge of the battery will be \(1\).In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be \(0\) after the last turn.
|
Input: 6 15 5 3 2 15 5 4 3 15 5 2 1 15 5 5 1 16 7 5 2 20 5 7 3 | Output: 4 -1 5 2 0 1
|
Easy
| 2 | 1,454 | 387 | 157 | 11 |
2,073 |
I
|
2073I
| 3,100 |
Master
| 0 | 0 | 0 | 0 | 20 |
|||||||
1,108 |
B
|
1108B
|
B. Divisors of Two Integers
| 1,100 |
brute force; greedy; math; number theory
|
Recently you have received two positive integer numbers \(x\) and \(y\). You forgot them, but you remembered a shuffled list containing all divisors of \(x\) (including \(1\) and \(x\)) and all divisors of \(y\) (including \(1\) and \(y\)). If \(d\) is a divisor of both numbers \(x\) and \(y\) at the same time, there are two occurrences of \(d\) in the list.For example, if \(x=4\) and \(y=6\) then the given list can be any permutation of the list \([1, 2, 4, 1, 2, 3, 6]\). Some of the possible lists are: \([1, 1, 2, 4, 6, 3, 2]\), \([4, 6, 1, 1, 2, 3, 2]\) or \([1, 6, 3, 2, 4, 1, 2]\).Your problem is to restore suitable positive integer numbers \(x\) and \(y\) that would yield the same list of divisors (possibly in different order).It is guaranteed that the answer exists, i.e. the given list of divisors corresponds to some positive integers \(x\) and \(y\).
|
The first line contains one integer \(n\) (\(2 \le n \le 128\)) β the number of divisors of \(x\) and \(y\).The second line of the input contains \(n\) integers \(d_1, d_2, \dots, d_n\) (\(1 \le d_i \le 10^4\)), where \(d_i\) is either divisor of \(x\) or divisor of \(y\). If a number is divisor of both numbers \(x\) and \(y\) then there are two copies of this number in the list.
|
Print two positive integer numbers \(x\) and \(y\) β such numbers that merged list of their divisors is the permutation of the given list of integers. It is guaranteed that the answer exists.
|
Input: 10 10 2 8 1 2 4 1 20 4 5 | Output: 20 8
|
Easy
| 4 | 869 | 382 | 191 | 11 |
|
270 |
B
|
270B
|
B. Multithreading
| 1,400 |
data structures; greedy; implementation
|
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the ""recent actions"" list. He likes to read thread conversations where each thread consists of multiple messages.Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the ai-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a1, a2, ..., an.
|
The first line of input contains an integer n, the number of threads (1 β€ n β€ 105). The next line contains a list of n space-separated integers a1, a2, ..., an where ai (1 β€ ai β€ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the ai are distinct.
|
Output a single integer β the number of threads that surely contain a new message.
|
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.In the second test case, there may be no new messages at all, since the thread order hasn't changed.In the third test case, only thread 1 can contain no new messages.
|
Input: 55 2 1 3 4 | Output: 2
|
Easy
| 3 | 1,179 | 287 | 82 | 2 |
1,876 |
G
|
1876G
|
G. Clubstep
| 3,500 |
binary search; brute force; data structures; greedy; trees
|
There is an extremely hard video game that is one of Chaneka's favourite video games. One of the hardest levels in the game is called Clubstep. Clubstep consists of \(n\) parts, numbered from \(1\) to \(n\). Chaneka has practised the level a good amount, so currently, her familiarity value with each part \(i\) is \(a_i\).After this, Chaneka can do several (possibly zero) attempts on Clubstep. In each attempt, she dies on one of the \(n\) parts. If an attempt dies on part \(p\), that means it only successfully passes through every part \(k\) for all \(1 \leq k \leq p-1\) and it does not reach any part \(k\) for all \(p+1 \leq k \leq n\). An attempt that dies on part \(p\) takes \(p\) seconds.It is known that Chaneka improves much more on the part she dies on than anything else. It is also known that during an attempt, Chaneka does not get to practise that much on the parts she does not reach. So, the effect of an attempt that dies on part \(p\) is as follows: Chaneka's familiarity value with part \(p\) increases by \(2\). Chaneka's familiarity value with each part \(k\) for all \(1 \leq k \leq p-1\) increases by \(1\). There will be \(q\) questions. For the \(j\)-th question, you are given three integers \(l_j\), \(r_j\), and \(x_j\). Then, you are asked to find out the minimum time (in seconds) for Chaneka to make it such that the familiarity value for every part \(p\) (\(l_j \leq p \leq r_j\)) is at least \(x_j\).Note that each question is independent, so the attempt Chaneka does on a question does not affect the familiarity values of any other questions.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 3\cdot10^5\)) β the number of parts in Clubstep.The second line contains \(n\) integers \(a_1,a_2,a_3,\ldots,a_n\) (\(1\leq a_i\leq10^9\)) β Chaneka's familiarity value with each part.The third line contains a single integer \(q\) (\(1\leq q\leq3\cdot10^5\)) β the number of questions.The \(j\)-th of the next \(q\) lines contains three integers \(l_j\), \(r_j\), and \(x_j\) (\(1\leq l_j\leq r_j\leq n\); \(1\leq x_j\leq10^9\)) β the description of the \(j\)-th question.
|
Output \(q\) lines with an integer in each line. The integer in the \(j\)-th line represents the minimum time (in seconds) for Chaneka to make it such that the familiarity value for every part \(p\) (\(l_j \leq p \leq r_j\)) is at least \(x_j\).
|
For the \(1\)-st question, one possible strategy is to do the following: Do \(1\) attempt that dies on part \(1\). This takes \(1\) second. The familiarity values become \([3, 3, 2, 1, 2]\). Do \(1\) attempt that dies on part \(4\). This takes \(4\) seconds. The familiarity values become \([4, 4, 3, 3, 2]\). Do \(2\) attempts that die on part \(5\). This takes \(10\) seconds. The familiarity values become \([6, 6, 5, 5, 6]\). The total time taken (in seconds) is \(1+4+10=15\).For the \(2\)-nd question, one possible strategy is to do the following: Do \(1\) attempt that dies on part \(3\). This takes \(3\) seconds. The familiarity values become \([2, 4, 4, 1, 2]\). Do \(2\) attempts that die on part \(4\). This takes \(8\) seconds. The familiarity values become \([4, 6, 6, 5, 2]\). The total time taken (in seconds) is \(3+8=11\).
|
Input: 5 1 3 2 1 2 3 1 5 5 2 4 5 3 3 1 | Output: 15 11 0
|
Master
| 5 | 1,582 | 536 | 245 | 18 |
1,210 |
A
|
1210A
|
A. Anadi and Domino
| 1,700 |
brute force; graphs
|
Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every \(a\) and \(b\) such that \(1 \leq a \leq b \leq 6\), there is exactly one domino with \(a\) dots on one half and \(b\) dots on the other half. The set contains exactly \(21\) dominoes. Here is an exact illustration of his set: Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.How many dominoes at most can Anadi place on the edges of his graph?
|
The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 7\), \(0 \leq m \leq \frac{n\cdot(n-1)}{2}\)) β the number of vertices and the number of edges in the graph.The next \(m\) lines contain two integers each. Integers in the \(i\)-th line are \(a_i\) and \(b_i\) (\(1 \leq a, b \leq n\), \(a \neq b\)) and denote that there is an edge which connects vertices \(a_i\) and \(b_i\).The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.
|
Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.
|
Here is an illustration of Anadi's graph from the first sample test: And here is one of the ways to place a domino on each of its edges: Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex \(1\) have three dots.
|
Input: 4 4 1 2 2 3 3 4 4 1 | Output: 4
|
Medium
| 2 | 1,073 | 564 | 112 | 12 |
1,868 |
C
|
1868C
|
C. Travel Plan
| 2,400 |
combinatorics; dp; implementation; math; trees
|
During the summer vacation after Zhongkao examination, Tom and Daniel are planning to go traveling.There are \(n\) cities in their country, numbered from \(1\) to \(n\). And the traffic system in the country is very special. For each city \(i\) (\(1 \le i \le n\)), there is a road between city \(i\) and \(2i\), if \(2i\le n\); a road between city \(i\) and \(2i+1\), if \(2i+1\le n\). Making a travel plan, Daniel chooses some integer value between \(1\) and \(m\) for each city, for the \(i\)-th city we denote it by \(a_i\).Let \(s_{i,j}\) be the maximum value of cities in the simple\(^\dagger\) path between cities \(i\) and \(j\). The score of the travel plan is \(\sum_{i=1}^n\sum_{j=i}^n s_{i,j}\). Tom wants to know the sum of scores of all possible travel plans. Daniel asks you to help him find it. You just need to tell him the answer modulo \(998\,244\,353\).\(^\dagger\) A simple path between cities \(x\) and \(y\) is a path between them that passes through each city at most once.
|
The first line of input contains a single integer \(t\) (\(1\le t\le 200\)) β the number of test cases. The description of test cases follows.The only line of each test case contains two integers \(n\) and \(m\) (\(1\leq n\leq 10^{18}\), \(1\leq m\leq 10^5\)) β the number of the cities and the maximum value of a city.It is guaranteed that the sum of \(m\) over all test cases does not exceed \(10^5\).
|
For each test case output one integer β the sum of scores of all possible travel plans, modulo \(998\,244\,353\).
|
In the first test case, there is only one possible travel plan: Path \(1\rightarrow 1\): \(s_{1,1}=a_1=1\).Path \(1\rightarrow 2\): \(s_{1,2}=\max(1,1)=1\).Path \(1\rightarrow 3\): \(s_{1,3}=\max(1,1)=1\).Path \(2\rightarrow 2\): \(s_{2,2}=a_2=1\).Path \(2\rightarrow 1\rightarrow 3\): \(s_{2,3}=\max(1,1,1)=1\).Path \(3\rightarrow 3\): \(s_{3,3}=a_3=1\).The score is \(1+1+1+1+1+1=6\).In the second test case, there are four possible travel plans: Score of plan \(1\): \(1+1+1=3\).Score of plan \(2\): \(1+2+2=5\).Score of plan \(3\): \(2+2+1=5\).Score of plan \(4\): \(2+2+2=6\).Therefore, the sum of score is \(3+5+5+6=19\).
|
Input: 53 12 210 943 20154 147 | Output: 6 19 583217643 68816635 714002110
|
Expert
| 5 | 997 | 403 | 113 | 18 |
2,072 |
E
|
2072E
|
E. Do You Love Your Hero and His Two-Hit Multi-Target Attacks?
| 1,500 |
binary search; brute force; constructive algorithms; dp; geometry; greedy; math
|
Akito decided to study a new powerful spell. Since it possesses immeasurable strength, it certainly requires a lot of space and careful preparation. For this, Akito went out into the field. Let's represent the field as a Cartesian coordinate system.For the spell, Akito needs to place \(0 \le n \le 500\) staffs at distinct integer coordinates in the field such that there will be exactly \(k\) pairs \((i, j)\) such that \(1 \le i < j \le n\) and \(\rho(i, j) = d(i, j)\).Here, for two points with integer coordinates \(a = (x_a, y_a)\) and \(b = (x_b, y_b)\), \(\rho(a, b) = \sqrt{(x_a - x_b)^2 + (y_a - y_b)^2}\) and \(d(a, b) = |x_a - x_b| + |y_a - y_b|\).
|
The first line of input contains a single number \(t\) (\(1 \le t \le 1000\)) β the number of test cases.In the only line of each test case, there is a single number \(k\) (\(0 \le k \le 10^5\)) β the number of pairs of staffs for which the equality \(\rho(i, j) = d(i, j)\) must hold.
|
For each test case, the first line of output should print the number \(n\) (\(0 \le n \le 500\)) β the number of placed staffs.In the following \(n\) lines, pairs of integers \(x_i, y_i\) \((-10^9 \le x_i, y_i \le 10^9)\) should be printed β the coordinates of the \(i\)-th staff. The points in which staffs stand must be distinct.
|
Input: 3025 | Output: 6 69 52 4 20 789 9308706 1337 1337 -1234 -5678 23456178 707 10 -236 -346262358 273568 6435267 2365437 31441367 246574 -45642372 -236 56 4743623 -192892 10408080 -8173135 -237415357 31441367 -78125638 278 56 143231 5 1 1 2 1 1 5 3 5 1 10
|
Medium
| 7 | 660 | 285 | 331 | 20 |
|
763 |
D
|
763D
|
D. Timofey and a flat tree
| 2,900 |
data structures; graphs; hashing; shortest paths; trees
|
Little Timofey has a big tree β an undirected connected graph with n vertices and no simple cycles. He likes to walk along it. His tree is flat so when he walks along it he sees it entirely. Quite naturally, when he stands on a vertex, he sees the tree as a rooted tree with the root in this vertex.Timofey assumes that the more non-isomorphic subtrees are there in the tree, the more beautiful the tree is. A subtree of a vertex is a subgraph containing this vertex and all its descendants. You should tell Timofey the vertex in which he should stand to see the most beautiful rooted tree.Subtrees of vertices u and v are isomorphic if the number of children of u equals the number of children of v, and their children can be arranged in such a way that the subtree of the first son of u is isomorphic to the subtree of the first son of v, the subtree of the second son of u is isomorphic to the subtree of the second son of v, and so on. In particular, subtrees consisting of single vertex are isomorphic to each other.
|
First line contains single integer n (1 β€ n β€ 105) β number of vertices in the tree.Each of the next n - 1 lines contains two integers ui and vi (1 β€ ui, vi β€ 105, ui β vi), denoting the vertices the i-th edge connects.It is guaranteed that the given graph is a tree.
|
Print single integer β the index of the vertex in which Timofey should stand. If there are many answers, you can print any of them.
|
In the first example we can stand in the vertex 1 or in the vertex 3 so that every subtree is non-isomorphic. If we stand in the vertex 2, then subtrees of vertices 1 and 3 are isomorphic.In the second example, if we stand in the vertex 1, then only subtrees of vertices 4 and 5 are isomorphic.In the third example, if we stand in the vertex 1, then subtrees of vertices 2, 3, 4, 6, 7 and 8 are isomorphic. If we stand in the vertex 2, than only subtrees of vertices 3, 4, 6, 7 and 8 are isomorphic. If we stand in the vertex 5, then subtrees of vertices 2, 3, 4, 6, 7 and 8 are isomorphic, and subtrees of vertices 1 and 9 are isomorphic as well: 1 9 /\ /\7 8 4 2
|
Input: 31 22 3 | Output: 1
|
Master
| 5 | 1,021 | 267 | 131 | 7 |
254 |
B
|
254B
|
B. Jury Size
| 1,500 |
brute force; implementation
|
In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pi people for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time.
|
The first line contains integer n β the number of Olympiads in 2013 (1 β€ n β€ 100). Each of the following n lines contains four integers mi, di, pi and ti β the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 β€ mi β€ 12, di β₯ 1, 1 β€ pi, ti β€ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day.Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year.
|
Print a single number β the minimum jury size.
|
Input: 25 23 1 23 13 2 3 | Output: 2
|
Medium
| 2 | 1,420 | 712 | 46 | 2 |
|
1,159 |
A
|
1159A
|
A. A pile of stones
| 800 |
implementation; math
|
Vasya has a pile, that consists of some number of stones. \(n\) times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile.You are given \(n\) operations which Vasya has made. Find the minimal possible number of stones that can be in the pile after making these operations.
|
The first line contains one positive integer \(n\) β the number of operations, that have been made by Vasya (\(1 \leq n \leq 100\)).The next line contains the string \(s\), consisting of \(n\) symbols, equal to ""-"" (without quotes) or ""+"" (without quotes). If Vasya took the stone on \(i\)-th operation, \(s_i\) is equal to ""-"" (without quotes), if added, \(s_i\) is equal to ""+"" (without quotes).
|
Print one integer β the minimal possible number of stones that can be in the pile after these \(n\) operations.
|
In the first test, if Vasya had \(3\) stones in the pile at the beginning, after making operations the number of stones will be equal to \(0\). It is impossible to have less number of piles, so the answer is \(0\). Please notice, that the number of stones at the beginning can't be less, than \(3\), because in this case, Vasya won't be able to take a stone on some operation (the pile will be empty).In the second test, if Vasya had \(0\) stones in the pile at the beginning, after making operations the number of stones will be equal to \(4\). It is impossible to have less number of piles because after making \(4\) operations the number of stones in the pile increases on \(4\) stones. So, the answer is \(4\).In the third test, if Vasya had \(1\) stone in the pile at the beginning, after making operations the number of stones will be equal to \(1\). It can be proved, that it is impossible to have less number of stones after making the operations.In the fourth test, if Vasya had \(0\) stones in the pile at the beginning, after making operations the number of stones will be equal to \(3\).
|
Input: 3 --- | Output: 0
|
Beginner
| 2 | 370 | 405 | 111 | 11 |
1,967 |
F
|
1967F
|
F. Next and Prev
| 3,200 |
brute force; data structures; implementation
|
Let \(p_1, \ldots, p_n\) be a permutation of \([1, \ldots, n]\).Let the \(q\)-subsequence of \(p\) be a permutation of \([1, q]\), whose elements are in the same relative order as in \(p_1, \ldots, p_n\). That is, we extract all elements not exceeding \(q\) together from \(p\) in the original order, and they make the \(q\)-subsequence of \(p\).For a given array \(a\), let \(pre(i)\) be the largest value satisfying \(pre(i) < i\) and \(a_{pre(i)} > a_i\). If it does not exist, let \(pre(i) = -10^{100}\). Let \(nxt(i)\) be the smallest value satisfying \(nxt(i) > i\) and \(a_{nxt(i)} > a_i\). If it does not exist, let \(nxt(i) = 10^{100}\).For each \(q\) such that \(1 \leq q \leq n\), let \(a_1, \ldots, a_q\) be the \(q\)-subsequence of \(p\). For each \(i\) such that \(1 \leq i \leq q\), \(pre(i)\) and \(nxt(i)\) will be calculated as defined. Then, you will be given some integer values of \(x\), and for each of them you have to calculate \(\sum\limits_{i=1}^q \min(nxt(i) - pre(i), x)\).
|
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 \leq n \leq 3\cdot 10^5\)) β the length of the permutation.The second line of each test case contains \(n\) integers \(p_1, \ldots, p_n\) (\(1 \leq p_i \leq n\)) β the initial permutation.Then, for each \(q\) such that \(1 \leq q \leq n\) in ascending order, you will be given an integer \(k\) (\(0 \leq k \leq 10^5\)), representing the number of queries for the \(q\)-subsequence. Then \(k\) numbers in a line follow: each of them is the value of \(x\) for a single query (\(1 \leq x \leq q\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3\cdot 10^5\) and the sum of \(k\) over all test cases does not exceed \(10^5\).
|
For each test case, for each query, print a single line with an integer: the answer to the query.
|
The \(1\)-subsequence is \([1]\), and \(pre=[-10^{100}]\), \(nxt=[10^{100}]\). \(ans(1)=\min(10^{100}-(-10^{100}),1)=1\).The \(5\)-subsequence is \([1,4,3,2,5]\), and \(pre=[-10^{100},-10^{100},2,3,-10^{100}]\), \(nxt=[2,5,5,5,10^{100}]\). \(ans(1)=5,ans(2)=10,ans(3)=14\).
|
Input: 176 1 4 3 2 5 71 101 31 23 1 2 31 32 2 6 | Output: 1 9 8 5 10 14 16 14 30
|
Master
| 3 | 1,001 | 881 | 97 | 19 |
19 |
E
|
19E
|
E. Fairy
| 2,900 |
dfs and similar; divide and conquer; dsu
|
Once upon a time there lived a good fairy A. One day a fine young man B came to her and asked to predict his future. The fairy looked into her magic ball and said that soon the fine young man will meet the most beautiful princess ever and will marry her. Then she drew on a sheet of paper n points and joined some of them with segments, each of the segments starts in some point and ends in some other point. Having drawn that picture, she asked the young man to erase one of the segments from the sheet. Then she tries to colour each point red or blue so, that there is no segment having points of the same colour as its ends. If she manages to do so, the prediction will come true. B wants to meet the most beautiful princess, that's why he asks you to help him. Find all the segments that will help him to meet the princess.
|
The first input line contains two integer numbers: n β amount of the drawn points and m β amount of the drawn segments (1 β€ n β€ 104, 0 β€ m β€ 104). The following m lines contain the descriptions of the segments. Each description contains two different space-separated integer numbers v, u (1 β€ v β€ n, 1 β€ u β€ n) β indexes of the points, joined by this segment. No segment is met in the description twice.
|
In the first line output number k β amount of the segments in the answer. In the second line output k space-separated numbers β indexes of these segments in ascending order. Each index should be output only once. Segments are numbered from 1 in the input order.
|
Input: 4 41 21 32 43 4 | Output: 41 2 3 4
|
Master
| 3 | 827 | 403 | 261 | 0 |
|
382 |
B
|
382B
|
B. Number Busters
| 2,000 |
binary search; math
|
Arthur and Alexander are number busters. Today they've got a competition. Arthur took a group of four integers a, b, w, x (0 β€ b < w, 0 < x < w) and Alexander took integer Ρ. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: c = c - 1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if b β₯ x, perform the assignment b = b - x, if b < x, then perform two consecutive assignments a = a - 1; b = w - (x - b).You've got numbers a, b, w, x, c. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if c β€ a.
|
The first line contains integers a, b, w, x, c (1 β€ a β€ 2Β·109, 1 β€ w β€ 1000, 0 β€ b < w, 0 < x < w, 1 β€ c β€ 2Β·109).
|
Print a single integer β the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits.
|
Input: 4 2 3 1 6 | Output: 2
|
Hard
| 2 | 793 | 114 | 178 | 3 |
|
150 |
D
|
150D
|
D. Mission Impassable
| 2,600 |
dp; strings
|
Market stalls now have the long-awaited game The Colder Scrools V: Nvodsk. The game turned out to be difficult as hell and most students can't complete the last quest (""We don't go to Nvodsk...""). That threatened winter exams. The rector already started to wonder whether he should postpone the winter exams till April (in fact, he wanted to complete the quest himself). But all of a sudden a stranger appeared at the door of his office. ""Good afternoon. My name is Chuck and I solve any problems"" β he said.And here they are sitting side by side but still they can't complete the mission. The thing is, to kill the final boss one should prove one's perfect skills in the art of managing letters. One should be a real magician to do that. And can you imagine what happens when magicians start competing... But let's put it more formally: you are given a string and a set of integers ai. You are allowed to choose any substring that is a palindrome and delete it. At that we receive some number of points equal to ak, where k is the length of the deleted palindrome. For some k, ak = -1, which means that deleting palindrome strings of such length is forbidden. After a substring is deleted, the remaining part ""shifts together"", that is, at no moment of time the string has gaps. The process is repeated while the string has at least one palindrome substring that can be deleted. All gained points are summed up.Determine what maximum number of points can be earned.""Oh"" β said Chuck, raising from the chair, β ""I used to love deleting palindromes, just like you, but one day I took an arrow in the Knee"".
|
The first line contains an integer l (1 β€ l β€ 150) β the length of the string.The second line contains exactly l integers ak ( - 1 β€ ak β€ 105) β the points a player gains for deleting.The third line contains exactly l lowercase Latin letters β the original string from which a player can delete palindromes. The line contains no other characters apart from the newline character at the end of the string.
|
Print a single number β the maximum number of points one can gain if he plays on the given string.
|
In the first sample we cannot delete any substring, so the best result is 0. In the second sample we are allowed to delete only those palindromes whose length equals 1, thus, if we delete the whole string, we get 7 points. In the third sample the optimal strategy is: first we delete character c, then string aa, then bb, and the last one aa. At that we get 1 + 3 * 5 = 16 points.
|
Input: 7-1 -1 -1 -1 -1 -1 -1abacaba | Output: 0
|
Expert
| 2 | 1,615 | 404 | 98 | 1 |
1,000 |
A
|
1000A
|
A. Codehorses T-shirts
| 1,200 |
greedy; implementation
|
Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners.The valid sizes of T-shirts are either ""M"" or from \(0\) to \(3\) ""X"" followed by ""S"" or ""L"". For example, sizes ""M"", ""XXS"", ""L"", ""XXXL"" are valid and ""XM"", ""Z"", ""XXXXL"" are not.There are \(n\) winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office. Organizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words.What is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one?The lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists.
|
The first line contains one integer \(n\) (\(1 \le n \le 100\)) β the number of T-shirts.The \(i\)-th of the next \(n\) lines contains \(a_i\) β the size of the \(i\)-th T-shirt of the list for the previous year.The \(i\)-th of the next \(n\) lines contains \(b_i\) β the size of the \(i\)-th T-shirt of the list for the current year.It is guaranteed that all the sizes in the input are valid. It is also guaranteed that Ksenia can produce list \(b\) from the list \(a\).
|
Print the minimal number of seconds Ksenia is required to spend to change the last year list to the current one. If the lists are already equal, print 0.
|
In the first example Ksenia can replace ""M"" with ""S"" and ""S"" in one of the occurrences of ""XS"" with ""L"".In the second example Ksenia should replace ""L"" in ""XXXL"" with ""S"".In the third example lists are equal.
|
Input: 3XSXSMXLSXS | Output: 2
|
Easy
| 2 | 1,185 | 471 | 153 | 10 |
354 |
B
|
354B
|
B. Game with Strings
| 2,400 |
bitmasks; dp; games
|
Given an n Γ n table T consisting of lowercase English letters. We'll consider some string s good if the table contains a correct path corresponding to the given string. In other words, good strings are all strings we can obtain by moving from the left upper cell of the table only to the right and down. Here's the formal definition of correct paths:Consider rows of the table are numbered from 1 to n from top to bottom, and columns of the table are numbered from 1 to n from left to the right. Cell (r, c) is a cell of table T on the r-th row and in the c-th column. This cell corresponds to letter Tr, c.A path of length k is a sequence of table cells [(r1, c1), (r2, c2), ..., (rk, ck)]. The following paths are correct: There is only one correct path of length 1, that is, consisting of a single cell: [(1, 1)]; Let's assume that [(r1, c1), ..., (rm, cm)] is a correct path of length m, then paths [(r1, c1), ..., (rm, cm), (rm + 1, cm)] and [(r1, c1), ..., (rm, cm), (rm, cm + 1)] are correct paths of length m + 1. We should assume that a path [(r1, c1), (r2, c2), ..., (rk, ck)] corresponds to a string of length k: Tr1, c1 + Tr2, c2 + ... + Trk, ck.Two players play the following game: initially they have an empty string. Then the players take turns to add a letter to the end of the string. After each move (adding a new letter) the resulting string must be good. The game ends after 2n - 1 turns. A player wins by the following scenario: If the resulting string has strictly more letters ""a"" than letters ""b"", then the first player wins; If the resulting string has strictly more letters ""b"" than letters ""a"", then the second player wins; If the resulting string has the same number of letters ""a"" and ""b"", then the players end the game with a draw. Your task is to determine the result of the game provided that both players played optimally well.
|
The first line contains a single number n (1 β€ n β€ 20).Next n lines contain n lowercase English letters each β table T.
|
In a single line print string ""FIRST"", if the first player wins, ""SECOND"", if the second player wins and ""DRAW"", if the game ends with a draw.
|
Consider the first sample:Good strings are strings: a, ab, ac, abd, acd.The first player moves first and adds letter a to the string, as there is only one good string of length 1. Then the second player can add b or c and the game will end with strings abd or acd, correspondingly. In the first case it will be a draw (the string has one a and one b), in the second case the first player wins. Naturally, in this case the second player prefers to choose letter b and end the game with a draw.Consider the second sample:Good strings are: x, xa, xay.We can see that the game will end with string xay and the first player wins.
|
Input: 2abcd | Output: DRAW
|
Expert
| 3 | 1,873 | 119 | 148 | 3 |
354 |
C
|
354C
|
C. Vasya and Beautiful Arrays
| 2,100 |
brute force; dp; number theory
|
Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).The seller can obtain array b from array a if the following conditions hold: bi > 0; 0 β€ ai - bi β€ k for all 1 β€ i β€ n.Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).
|
The first line contains two integers n and k (1 β€ n β€ 3Β·105; 1 β€ k β€ 106). The second line contains n integers ai (1 β€ ai β€ 106) β array a.
|
In the single line print a single number β the maximum possible beauty of the resulting array.
|
In the first sample we can obtain the array:3 6 9 12 12 15In the second sample we can obtain the next array:7 21 49 14 77
|
Input: 6 13 6 10 12 13 16 | Output: 3
|
Hard
| 3 | 693 | 139 | 94 | 3 |
1,949 |
E
|
1949E
|
E. Damage per Second
| 2,900 |
brute force; math
|
You just created a new character in your favourite role-playing game and now have to decide how to skill him. The two skill attributes to be chosen are: damage per hit and hits per second. Damage per hit is the amount of damage you deal with a single hit, while hits per second is the number of hits you can make in one second. Initially, both skill attributes are set at \(0\). You have \(k\) skill points to distribute as you want; in other words, you can choose the values of the two skills so that they are positive integers with sum at most \(k\). The tutorial of the game (the boring part you want to finish as soon as possible) consists of \(n\) monsters to be killed one after the other. The \(i\)-th monster has \(h_i\) health points, i.e., it dies after you have inflicted at least \(h_i\) damage.How can you assign the two skill attributes to minimize the time necessary to kill all the \(n\) monsters?
|
The first line contains two integers \(n\) and \(k\) (\(1\leq n\leq200\,000\), \(2\leq k\leq200\,000\)) β the number of enemies and the number of skill points.The second line contains \(n\) integers \(h_i\) (\(1\leq h_i\leq10^{13}\)) β the health of the \(i\)th enemy.
|
Print two positive integers \(x\) and \(y\) (\(1\le x, y\) and \(x+y\le k\)) β the number of skill points you want to invest in damage per hit and hits per second. If there are multiple optimal solutions, print any of them.
|
In the first sample, there is only one monster and you have \(7\) skill points to distribute. If you make \(3\) damage per hit, you will need \(5\) hits to kill it. If you do \(4\) hits per second, you will need \(1.25\) seconds to beat the monster. There is no way to beat the monster faster than this.In the second sample, you will need one hit for each monster and a total time of \(0.8\) seconds if you distribute \(4\) skill points on damage per hit and the remaining \(5\) points on hits per second.
|
Input: 1 714 | Output: 3 4
|
Master
| 2 | 913 | 268 | 223 | 19 |
245 |
A
|
245A
|
A. System Administrator
| 800 |
implementation
|
Polycarpus is a system administrator. There are two servers under his strict guidance β a and b. To stay informed about the servers' performance, Polycarpus executes commands ""ping a"" and ""ping b"". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y β₯ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is ""alive"" or not. Polycarpus thinks that the server is ""alive"", if at least half of the packets that we send to this server reached it successfully along the network.Help Polycarpus, determine for each server, whether it is ""alive"" or not by the given commands and their results.
|
The first line contains a single integer n (2 β€ n β€ 1000) β the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers β the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 β€ ti β€ 2; xi, yi β₯ 0; xi + yi = 10). If ti = 1, then the i-th command is ""ping a"", otherwise the i-th command is ""ping b"". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost.It is guaranteed that the input has at least one ""ping a"" command and at least one ""ping b"" command.
|
In the first line print string ""LIVE"" (without the quotes) if server a is ""alive"", otherwise print ""DEAD"" (without the quotes).In the second line print the state of server b in the similar format.
|
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
|
Input: 21 5 52 6 4 | Output: LIVELIVE
|
Beginner
| 1 | 927 | 661 | 202 | 2 |
1,481 |
C
|
1481C
|
C. Fence Painting
| 1,600 |
brute force; constructive algorithms; greedy
|
You finally woke up after this crazy dream and decided to walk around to clear your head. Outside you saw your house's fence β so plain and boring, that you'd like to repaint it. You have a fence consisting of \(n\) planks, where the \(i\)-th plank has the color \(a_i\). You want to repaint the fence in such a way that the \(i\)-th plank has the color \(b_i\).You've invited \(m\) painters for this purpose. The \(j\)-th painter will arrive at the moment \(j\) and will recolor exactly one plank to color \(c_j\). For each painter you can choose which plank to recolor, but you can't turn them down, i. e. each painter has to color exactly one plank.Can you get the coloring \(b\) you want? If it's possible, print for each painter which plank he must paint.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^5\)) β the number of planks in the fence and the number of painters.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β the initial colors of the fence.The third line of each test case contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le n\)) β the desired colors of the fence.The fourth line of each test case contains \(m\) integers \(c_1, c_2, \dots, c_m\) (\(1 \le c_j \le n\)) β the colors painters have.It's guaranteed that the sum of \(n\) doesn't exceed \(10^5\) and the sum of \(m\) doesn't exceed \(10^5\) over all test cases.
|
For each test case, output ""NO"" if it is impossible to achieve the coloring \(b\).Otherwise, print ""YES"" and \(m\) integers \(x_1, x_2, \dots, x_m\), where \(x_j\) is the index of plank the \(j\)-th painter should paint.You may print every letter in any case you want (so, for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" are all recognized as positive answer).
|
Input: 6 1 1 1 1 1 5 2 1 2 2 1 1 1 2 2 1 1 1 2 3 3 2 2 2 2 2 2 2 3 2 10 5 7 3 2 1 7 9 4 2 7 9 9 9 2 1 4 9 4 2 3 9 9 9 7 4 3 5 2 1 2 2 1 1 1 2 2 1 1 3 3 6 4 3 4 2 4 1 2 2 3 1 3 1 1 2 2 3 4 | Output: YES 1 YES 2 2 YES 1 1 1 YES 2 1 9 5 9 NO NO
|
Medium
| 3 | 760 | 818 | 379 | 14 |
|
1,700 |
D
|
1700D
|
D. River Locks
| 1,900 |
binary search; dp; greedy; math
|
Recently in Divanovo, a huge river locks system was built. There are now \(n\) locks, the \(i\)-th of them has the volume of \(v_i\) liters, so that it can contain any amount of water between \(0\) and \(v_i\) liters. Each lock has a pipe attached to it. When the pipe is open, \(1\) liter of water enters the lock every second.The locks system is built in a way to immediately transfer all water exceeding the volume of the lock \(i\) to the lock \(i + 1\). If the lock \(i + 1\) is also full, water will be transferred further. Water exceeding the volume of the last lock pours out to the river. The picture illustrates \(5\) locks with two open pipes at locks \(1\) and \(3\). Because locks \(1\), \(3\), and \(4\) are already filled, effectively the water goes to locks \(2\) and \(5\). Note that the volume of the \(i\)-th lock may be greater than the volume of the \(i + 1\)-th lock.To make all locks work, you need to completely fill each one of them. The mayor of Divanovo is interested in \(q\) independent queries. For each query, suppose that initially all locks are empty and all pipes are closed. Then, some pipes are opened simultaneously. For the \(j\)-th query the mayor asks you to calculate the minimum number of pipes to open so that all locks are filled no later than after \(t_j\) seconds.Please help the mayor to solve this tricky problem and answer his queries.
|
The first lines contains one integer \(n\) (\(1 \le n \le 200\,000\)) β the number of locks. The second lines contains \(n\) integers \(v_1, v_2, \dots, v_n\) (\(1 \le v_i \le 10^9\))) β volumes of the locks. The third line contains one integer \(q\) (\(1 \le q \le 200\,000\)) β the number of queries. Each of the next \(q\) lines contains one integer \(t_j\) (\(1 \le t_j \le 10^9\)) β the number of seconds you have to fill all the locks in the query \(j\).
|
Print \(q\) integers. The \(j\)-th of them should be equal to the minimum number of pipes to turn on so that after \(t_j\) seconds all of the locks are filled. If it is impossible to fill all of the locks in given time, print \(-1\).
|
There are \(6\) queries in the first example test. In the queries \(1, 3, 4\) the answer is \(-1\). We need to wait \(4\) seconds to fill the first lock even if we open all the pipes. In the sixth query we can open pipes in locks \(1\), \(3\), and \(4\). After \(4\) seconds the locks \(1\) and \(4\) are full. In the following \(1\) second \(1\) liter of water is transferred to the locks \(2\) and \(5\). The lock \(3\) is filled by its own pipe. Similarly, in the second query one can open pipes in locks \(1\), \(3\), and \(4\).In the fifth query one can open pipes \(1, 2, 3, 4\).
|
Input: 5 4 1 5 4 1 6 1 6 2 3 4 5 | Output: -1 3 -1 -1 4 3
|
Hard
| 4 | 1,384 | 460 | 233 | 17 |
876 |
B
|
876B
|
B. Divisiblity of Differences
| 1,300 |
implementation; math; number theory
|
You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.
|
First line contains three integers n, k and m (2 β€ k β€ n β€ 100 000, 1 β€ m β€ 100 000) β number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.Second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109) β the numbers in the multiset.
|
If it is not possible to select k numbers in the desired way, output Β«NoΒ» (without the quotes).Otherwise, in the first line of output print Β«YesΒ» (without the quotes). In the second line print k integers b1, b2, ..., bk β the selected numbers. If there are multiple possible solutions, print any of them.
|
Input: 3 2 31 8 4 | Output: Yes1 4
|
Easy
| 3 | 418 | 311 | 304 | 8 |
|
1,045 |
F
|
1045F
|
F. Shady Lady
| 3,400 |
geometry; math
|
Ani and Borna are playing a short game on a two-variable polynomial. It's a special kind of a polynomial: the monomials are fixed, but all of its coefficients are fill-in-the-blanks dashes, e.g. $$$\( \_ xy + \_ x^4 y^7 + \_ x^8 y^3 + \ldots \)\(Borna will fill in the blanks with positive integers. He wants the polynomial to be bounded from below, i.e. his goal is to make sure there exists a real number \)M\( such that the value of the polynomial at any point is greater than \)M$$$. Ani is mischievous, and wants the polynomial to be unbounded. Along with stealing Borna's heart, she can also steal parts of polynomials. Ani is only a petty kind of thief, though: she can only steal at most one monomial from the polynomial before Borna fills in the blanks. If Ani and Borna play their only moves optimally, who wins?
|
The first line contains a positive integer \(N\) \((2 \leq N \leq 200\, 000)\), denoting the number of the terms in the starting special polynomial.Each of the following \(N\) lines contains a description of a monomial: the \(k\)-th line contains two **space**-separated integers \(a_k\) and \(b_k\) \((0 \leq a_k, b_k \leq 10^9\)) which mean the starting polynomial has the term \(\_ x^{a_k} y^{b_k}\). It is guaranteed that for \(k \neq l\), either \(a_k \neq a_l\) or \(b_k \neq b_l\).
|
If Borna can always choose the coefficients such that the resulting polynomial is bounded from below, regardless of what monomial Ani steals, output ""Borna"". Else, output ""Ani"". You shouldn't output the quotation marks.
|
In the first sample, the initial polynomial is \(\_xy+ \_x^2 + \_y^2\). If Ani steals the \(\_y^2\) term, Borna is left with \(\_xy+\_x^2\). Whatever positive integers are written on the blanks, \(y \rightarrow -\infty\) and \(x := 1\) makes the whole expression go to negative infinity.In the second sample, the initial polynomial is \(\_1 + \_x + \_x^2 + \_x^8\). One can check that no matter what term Ani steals, Borna can always win.
|
Input: 31 12 00 2 | Output: Ani
|
Master
| 2 | 822 | 488 | 223 | 10 |
220 |
D
|
220D
|
D. Little Elephant and Triangle
| 2,500 |
geometry; math
|
The Little Elephant is playing with the Cartesian coordinates' system. Most of all he likes playing with integer points. The Little Elephant defines an integer point as a pair of integers (x; y), such that 0 β€ x β€ w and 0 β€ y β€ h. Thus, the Little Elephant knows only (w + 1)Β·(h + 1) distinct integer points.The Little Elephant wants to paint a triangle with vertexes at integer points, the triangle's area must be a positive integer. For that, he needs to find the number of groups of three points that form such triangle. At that, the order of points in a group matters, that is, the group of three points (0;0), (0;2), (2;2) isn't equal to the group (0;2), (0;0), (2;2).Help the Little Elephant to find the number of groups of three integer points that form a nondegenerate triangle with integer area.
|
A single line contains two integers w and h (1 β€ w, h β€ 4000).
|
In a single output line print an integer β the remainder of dividing the answer to the problem by 1000000007 (109 + 7).
|
Input: 2 1 | Output: 36
|
Expert
| 2 | 804 | 62 | 119 | 2 |
|
1,883 |
C
|
1883C
|
C. Raspberries
| 1,000 |
dp; math
|
You are given an array of integers \(a_1, a_2, \ldots, a_n\) and a number \(k\) (\(2 \leq k \leq 5\)). In one operation, you can do the following: Choose an index \(1 \leq i \leq n\), Set \(a_i = a_i + 1\).Find the minimum number of operations needed to make the product of all the numbers in the array \(a_1 \cdot a_2 \cdot \ldots \cdot a_n\) divisible by \(k\).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 10^5\), \(2 \leq k \leq 5\)) β the size of the array \(a\) and the number \(k\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the minimum number of operations needed to make the product of all the numbers in the array divisible by \(k\).
|
In the first test case, we need to choose the index \(i = 2\) twice. After that, the array will be \(a = [7, 5]\). The product of all the numbers in the array is \(35\).In the fourth test case, the product of the numbers in the array is \(120\), which is already divisible by \(5\), so no operations are needed.In the eighth test case, we can perform two operations by choosing \(i = 2\) and \(i = 3\) in any order. After that, the array will be \(a = [1, 6, 10]\). The product of the numbers in the array is \(60\).
|
Input: 152 57 33 37 4 15 29 7 7 3 95 55 4 1 2 37 49 5 1 5 9 5 13 46 3 63 46 1 53 41 5 94 41 4 1 13 43 5 34 58 9 9 32 51 62 510 104 51 6 1 12 57 7 | Output: 2 2 1 0 2 0 1 2 0 1 1 4 0 4 3
|
Beginner
| 2 | 363 | 559 | 138 | 18 |
1,792 |
D
|
1792D
|
D. Fixed Prefix Permutations
| 1,700 |
binary search; bitmasks; data structures; hashing; math; sortings
|
You are given \(n\) permutations \(a_1, a_2, \dots, a_n\), each of length \(m\). Recall that a permutation of length \(m\) is a sequence of \(m\) distinct integers from \(1\) to \(m\).Let the beauty of a permutation \(p_1, p_2, \dots, p_m\) be the largest \(k\) such that \(p_1 = 1, p_2 = 2, \dots, p_k = k\). If \(p_1 \neq 1\), then the beauty is \(0\).The product of two permutations \(p \cdot q\) is a permutation \(r\) such that \(r_j = q_{p_j}\).For each \(i\) from \(1\) to \(n\), print the largest beauty of a permutation \(a_i \cdot a_j\) over all \(j\) from \(1\) to \(n\) (possibly, \(i = j\)).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains two integers \(n\) and \(m\) (\(1 \le n \le 5 \cdot 10^4\); \(1 \le m \le 10\)) β the number of permutations and the length of each permutation.The \(i\)-th of the next \(n\) lines contains a permutation \(a_i\) β \(m\) distinct integers from \(1\) to \(m\).The sum of \(n\) doesn't exceed \(5 \cdot 10^4\) over all testcases.
|
For each testcase, print \(n\) integers. The \(i\)-th value should be equal to the largest beauty of a permutation \(a_i \cdot a_j\) over all \(j\) (\(1 \le j \le n\)).
|
Input: 33 42 4 1 31 2 4 32 1 3 42 21 22 18 103 4 9 6 10 2 7 8 1 53 9 1 8 5 7 4 10 2 63 10 1 7 5 9 6 4 2 81 2 3 4 8 6 10 7 9 51 2 3 4 10 6 8 5 7 99 6 1 2 10 4 7 8 3 57 9 3 2 5 6 4 8 1 109 4 3 7 5 6 1 10 8 2 | Output: 1 4 4 2 2 10 8 1 6 8 10 1 7
|
Medium
| 6 | 604 | 463 | 168 | 17 |
|
1,796 |
E
|
1796E
|
E. Colored Subgraphs
| 2,500 |
dfs and similar; dp; games; greedy; trees
|
Monocarp has a tree, consisting of \(n\) vertices.He is going to select some vertex \(r\) and perform the following operations on each vertex \(v\) from \(1\) to \(n\): set \(d_v\) equal to the distance from \(v\) to \(r\) (the number of edges on the shortest path); color \(v\) some color. A nice coloring satisfies two conditions: for each pair of vertices of the same color \((v, u)\), there exists a path from \(v\) to \(u\) that only visits vertices of the same color; for each pair of vertices of the same color \((v, u)\), \(d_v \neq d_u\). Note that Monocarp can choose any amount of different colors he wants to use.For each used color, he then counts the number of vertices of this color. The cost of the tree is the minimum of these numbers.What can be the maximum cost of the tree?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.Each of the next \(n-1\) lines contains two integers \(v\) and \(u\) (\(1 \le v, u \le n\)) β the description of an edge.The given edges form a tree. The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase print a single integer β the maximum possible cost of the tree.
|
Input: 441 22 33 451 21 31 41 531 33 273 22 57 53 11 61 4 | Output: 4 1 3 3
|
Expert
| 5 | 793 | 446 | 81 | 17 |
|
300 |
C
|
300C
|
C. Beautiful Numbers
| 1,800 |
brute force; combinatorics
|
Vitaly is a very weird man. He's got two favorite digits a and b. Vitaly calls a positive integer good, if the decimal representation of this integer only contains digits a and b. Vitaly calls a good number excellent, if the sum of its digits is a good number.For example, let's say that Vitaly's favourite digits are 1 and 3, then number 12 isn't good and numbers 13 or 311 are. Also, number 111 is excellent and number 11 isn't. Now Vitaly is wondering, how many excellent numbers of length exactly n are there. As this number can be rather large, he asks you to count the remainder after dividing it by 1000000007 (109 + 7).A number's length is the number of digits in its decimal representation without leading zeroes.
|
The first line contains three integers: a, b, n (1 β€ a < b β€ 9, 1 β€ n β€ 106).
|
Print a single integer β the answer to the problem modulo 1000000007 (109 + 7).
|
Input: 1 3 3 | Output: 1
|
Medium
| 2 | 722 | 77 | 79 | 3 |
|
1,070 |
F
|
1070F
|
F. Debate
| 1,500 |
greedy
|
Elections in Berland are coming. There are only two candidates β Alice and Bob.The main Berland TV channel plans to show political debates. There are \(n\) people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views: supporting none of candidates (this kind is denoted as ""00""), supporting Alice but not Bob (this kind is denoted as ""10""), supporting Bob but not Alice (this kind is denoted as ""01""), supporting both candidates (this kind is denoted as ""11""). The direction of the TV channel wants to invite some of these people to the debate. The set of invited spectators should satisfy three conditions: at least half of spectators support Alice (i.e. \(2 \cdot a \ge m\), where \(a\) is number of spectators supporting Alice and \(m\) is the total number of spectators), at least half of spectators support Bob (i.e. \(2 \cdot b \ge m\), where \(b\) is number of spectators supporting Bob and \(m\) is the total number of spectators), the total influence of spectators is maximal possible. Help the TV channel direction to select such non-empty set of spectators, or tell that this is impossible.
|
The first line contains integer \(n\) (\(1 \le n \le 4\cdot10^5\)) β the number of people who want to take part in the debate as a spectator.These people are described on the next \(n\) lines. Each line describes a single person and contains the string \(s_i\) and integer \(a_i\) separated by space (\(1 \le a_i \le 5000\)), where \(s_i\) denotes person's political views (possible values β ""00"", ""10"", ""01"", ""11"") and \(a_i\) β the influence of the \(i\)-th person.
|
Print a single integer β maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead.
|
In the first example \(4\) spectators can be invited to maximize total influence: \(1\), \(2\), \(3\) and \(6\). Their political views are: ""11"", ""10"", ""01"" and ""00"". So in total \(2\) out of \(4\) spectators support Alice and \(2\) out of \(4\) spectators support Bob. The total influence is \(6+4+3+9=22\).In the second example the direction can select all the people except the \(5\)-th person.In the third example the direction can select people with indices: \(1\), \(4\), \(5\) and \(6\).In the fourth example it is impossible to select any non-empty set of spectators.
|
Input: 611 610 401 300 300 700 9 | Output: 22
|
Medium
| 1 | 1,212 | 475 | 200 | 10 |
382 |
E
|
382E
|
E. Ksenia and Combinatorics
| 2,600 |
combinatorics; dp
|
Ksenia has her winter exams. Today she is learning combinatorics. Here's one of the problems she needs to learn to solve.How many distinct trees are there consisting of n vertices, each with the following properties: the tree is marked, that is, the vertices of the tree are numbered from 1 to n; each vertex of the tree is connected with at most three other vertices, and at the same moment the vertex with number 1 is connected with at most two other vertices; the size of the tree's maximum matching equals k. Two trees are considered distinct if there are such two vertices u and v, that in one tree they are connected by an edge and in the other tree they are not.Help Ksenia solve the problem for the given n and k. As the answer to the problem can be very huge you should output it modulo 1000000007 (109 + 7).
|
The first line contains two integers n, k (1 β€ n, k β€ 50).
|
Print a single integer β the answer to the problem modulo 1000000007 (109 + 7).
|
If you aren't familiar with matchings, please, read the following link: http://en.wikipedia.org/wiki/Matching_(graph_theory).
|
Input: 1 1 | Output: 0
|
Expert
| 2 | 817 | 58 | 79 | 3 |
2,123 |
A
|
2123A
|
A. Blackboard Game
| 800 |
math
|
Initially, the integers from \(0\) to \(n-1\) are written on a blackboard.In one round, Alice chooses an integer \(a\) on the blackboard and erases it; then Bob chooses an integer \(b\) on the blackboard such that \(a+b \equiv 3 \pmod 4\)\(^{\text{β}}\) and erases it. Rounds take place in succession until a player is unable to make a move β the first player who is unable to make a move loses. Determine who wins with optimal play. \(^{\text{β}}\)We define that \(x\equiv y\pmod m\) whenever \(x-y\) is an integer multiple of \(m\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The only line of each test case contains an integer \(n\) (\(1\leq n \leq 100\)) β the number of integers written on the blackboard.
|
For each test case, output on a single line ""Alice"" if Alice wins with optimal play, and ""Bob"" if Bob wins with optimal play.You can output the answer in any case (upper or lower). For example, the strings ""aLiCe"", ""alice"", ""ALICE"", and ""alICE"" will be recognized as ""Alice"".
|
In the first sample, suppose Alice chooses \(0\), then Bob cannot choose any number so Alice wins immediately.In the second sample, suppose Alice chooses \(0\), then Bob can choose \(3\). Then suppose Alice chooses \(2\), then Bob can choose \(1\). Then Alice has no numbers remaining, so Bob wins.
|
Input: 52457100 | Output: Alice Bob Alice Alice Bob
|
Beginner
| 1 | 534 | 224 | 289 | 21 |
1,423 |
J
|
1423J
|
J. Bubble Cup hypothesis
| 2,400 |
bitmasks; constructive algorithms; dp; math
|
The Bubble Cup hypothesis stood unsolved for \(130\) years. Who ever proves the hypothesis will be regarded as one of the greatest mathematicians of our time! A famous mathematician Jerry Mao managed to reduce the hypothesis to this problem:Given a number \(m\), how many polynomials \(P\) with coefficients in set \({\{0,1,2,3,4,5,6,7\}}\) have: \(P(2)=m\)?Help Jerry Mao solve the long standing problem!
|
The first line contains a single integer \(t\) \((1 \leq t \leq 5\cdot 10^5)\) - number of test cases.On next line there are \(t\) numbers, \(m_i\) \((1 \leq m_i \leq 10^{18})\) - meaning that in case \(i\) you should solve for number \(m_i\).
|
For each test case \(i\), print the answer on separate lines: number of polynomials \(P\) as described in statement such that \(P(2)=m_i\), modulo \(10^9 + 7\).
|
In first case, for \(m=2\), polynomials that satisfy the constraint are \(x\) and \(2\).In second case, for \(m=4\), polynomials that satisfy the constraint are \(x^2\), \(x + 2\), \(2x\) and \(4\).
|
Input: 2 2 4 | Output: 2 4
|
Expert
| 4 | 405 | 243 | 160 | 14 |
1,607 |
A
|
1607A
|
A. Linear Keyboard
| 800 |
implementation; strings
|
You are given a keyboard that consists of \(26\) keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter.You have to type the word \(s\) on this keyboard. It also consists only of lowercase Latin letters.To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it.Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word.For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions \(8\), \(5\), \(12\) and \(15\), respectively. Therefore, it will take \(|5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13\) units of time to type the word ""hello"". Determine how long it will take to print the word \(s\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The next \(2t\) lines contain descriptions of the test cases.The first line of a description contains a keyboard β a string of length \(26\), which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard.The second line of the description contains the word \(s\). The word has a length from \(1\) to \(50\) letters inclusive and consists of lowercase Latin letters.
|
Print \(t\) lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word \(s\) on the given keyboard.
|
Input: 5 abcdefghijklmnopqrstuvwxyz hello abcdefghijklmnopqrstuvwxyz i abcdefghijklmnopqrstuvwxyz codeforces qwertyuiopasdfghjklzxcvbnm qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qwertyuiopasdfghjklzxcvbnm abacaba | Output: 13 0 68 0 74
|
Beginner
| 2 | 1,115 | 517 | 185 | 16 |
|
1,252 |
G
|
1252G
|
G. Performance Review
| 2,100 |
data structures
|
Randall is a software engineer at a company with \(N\) employees. Every year, the company re-evaluates its employees. At the end of every year, the company replaces its several worst-performing employees and replaces with the same number of new employees, so that the company keeps having \(N\) employees. Each person has a constant performance and can be represented by an integer (higher integer means better performance), and no two people have the same performance.The performance of the initial employees are represented by an array of integers \(A = [A_1, A_2, \dots, A_N]\) where \(A_i\) is the performance of the \(i^{th}\) employee. Randall is employee \(1\), so his performance is \(A_1\). We will consider the first \(M\) years. At the end of the \(i^{th}\) year, the company replaces its \(R_i\) worst-performing employees and replaces with \(R_i\) new employees. The performance of these new employees are represented by an array of integers \(B_i = [(B_i)_1, (B_i)_2, \dots, (B_i)_{R_i}]\) where \((B_i)_j\) is the performance of the \(j^{th}\) new employee.He will consider \(Q\) scenarios. On the \(i^{th}\) scenario, he will change the value of \((B_{X_i})_{Y_i}\) to \(Z_i\). For each scenario, Randall is wondering whether he will still be in the company after \(M\) years. Note that the changes in each scenario are kept for the subsequent scenarios.
|
Input begins with a line containing three integers: \(N\) \(M\) \(Q\) (\(2 \le N \le 100\,000\); \(1 \le M, Q \le 100\,000\)) representing the number of employees, the number of years to be considered, and the number of scenarios, respectively. The next line contains \(N\) integers: \(A_i\) (\(0 \le A_i \le 10^9\)) representing the performance of the initial employees. The next \(M\) lines each contains several integers: \(R_i\) \((B_i)_1\), \((B_i)_2\), \(\cdots\), \((B_i)_{R_i}\) (\(1 \le R_i < N\); \(0 \le (B_i)_j \le 10^9\)) representing the number of employees replaced and the performance of the new employees, respectively. It is guaranteed that the sum of \(R_i\) does not exceed \(10^6\). The next \(Q\) lines each contains three integers: \(X_i\) \(Y_i\) \(Z_i\) (\(1 \le X_i \le M\); \(1 \le Y_i \le R_{(X_i)}\); \(0 \le Z_i \le 10^9\)) representing a scenario. It is guaranteed that all integers in all \(A_i\), \((B_i)_j\), and \(Z_i\) (combined together) are distinct.
|
For each scenario in the same order as input, output in a line an integer \(0\) if Randall will not be in the company after \(M\) years, or \(1\) if Randall will still be in the company after \(M\) years.
|
Explanation for the sample input/output #1Randall performance is represented by \(50\). For the first scenario, the value of \((B_1)_3\) is updated to \(300\), causes the following: Initially, the performance of the employees is \([50, 40, 30, 20, 10]\). At the end of the first year, \(4\) worst-performing employees are replaced by employees with performance \([300, 100, 2, 1]\). Therefore, the performance of the employees is \([300, 100, 50, 2, 1]\). At the end of the second year, the performance of the employees is \([300, 100, 50, 4, 2]\). At the end of the third year, the performance of the employees is \([300, 100, 50, 7, 6]\). Therefore, Randall will still be in the company after \(3\) years.For the second scenario, the value of \((B_2)_1\) is updated to \(400\), causes the following: Initially, the performance of the employees is \([50, 40, 30, 20, 10]\). At the end of the first year, the performance of the employees is \([300, 100, 50, 2, 1]\). Recall that the change in the first scenario is kept for this scenario as well. At the end of the second year, the performance of the employees is \([400, 300, 100, 50, 2]\). At the end of the third year, the performance of the employees is \([400, 300, 100, 7, 6]\). Therefore, Randall will not be in the company after \(3\) years.
|
Input: 5 3 3 50 40 30 20 10 4 1 2 3 100 1 4 2 6 7 1 3 300 2 1 400 2 1 5 | Output: 1 0 1
|
Hard
| 1 | 1,370 | 988 | 204 | 12 |
463 |
B
|
463B
|
B. Caisa and Pylons
| 1,100 |
brute force; implementation; math
|
Caisa solved the problem with the sugar and now he is on the way back to home. Caisa is playing a mobile game during his path. There are (n + 1) pylons numbered from 0 to n in this game. The pylon with number 0 has zero height, the pylon with number i (i > 0) has height hi. The goal of the game is to reach n-th pylon, and the only move the player can do is to jump from the current pylon (let's denote its number as k) to the next one (its number will be k + 1). When the player have made such a move, its energy increases by hk - hk + 1 (if this value is negative the player loses energy). The player must have non-negative amount of energy at any moment of the time. Initially Caisa stand at 0 pylon and has 0 energy. The game provides a special opportunity: one can pay a single dollar and increase the height of anyone pylon by one. Caisa may use that opportunity several times, but he doesn't want to spend too much money. What is the minimal amount of money he must paid to reach the goal of the game?
|
The first line contains integer n (1 β€ n β€ 105). The next line contains n integers h1, h2, ..., hn (1 β€ hi β€ 105) representing the heights of the pylons.
|
Print a single number representing the minimum number of dollars paid by Caisa.
|
In the first sample he can pay 4 dollars and increase the height of pylon with number 0 by 4 units. Then he can safely pass to the last pylon.
|
Input: 53 4 3 2 4 | Output: 4
|
Easy
| 3 | 1,009 | 153 | 79 | 4 |
478 |
B
|
478B
|
B. Random Teams
| 1,300 |
combinatorics; constructive algorithms; greedy; math
|
n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition.
|
The only line of input contains two integers n and m, separated by a single space (1 β€ m β€ n β€ 109) β the number of participants and the number of teams respectively.
|
The only line of the output should contain two integers kmin and kmax β the minimum possible number of pairs of friends and the maximum possible number of pairs of friends respectively.
|
In the first sample all the participants get into one team, so there will be exactly ten pairs of friends.In the second sample at any possible arrangement one team will always have two participants and the other team will always have one participant. Thus, the number of pairs of friends will always be equal to one.In the third sample minimum number of newly formed friendships can be achieved if participants were split on teams consisting of 2 people, maximum number can be achieved if participants were split on teams of 1, 1 and 4 people.
|
Input: 5 1 | Output: 10 10
|
Easy
| 4 | 358 | 166 | 185 | 4 |
1,264 |
C
|
1264C
|
C. Beautiful Mirrors with queries
| 2,400 |
data structures; probabilities
|
Creatnx has \(n\) mirrors, numbered from \(1\) to \(n\). Every day, Creatnx asks exactly one mirror ""Am I beautiful?"". The \(i\)-th mirror will tell Creatnx that he is beautiful with probability \(\frac{p_i}{100}\) for all \(1 \le i \le n\).Some mirrors are called checkpoints. Initially, only the \(1\)st mirror is a checkpoint. It remains a checkpoint all the time.Creatnx asks the mirrors one by one, starting from the \(1\)-st mirror. Every day, if he asks \(i\)-th mirror, there are two possibilities: The \(i\)-th mirror tells Creatnx that he is beautiful. In this case, if \(i = n\) Creatnx will stop and become happy, otherwise he will continue asking the \(i+1\)-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to \(i\). There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given \(q\) queries, each query is represented by an integer \(u\): If the \(u\)-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the \(u\)-th mirror is no longer a checkpoint.After each query, you need to calculate the expected number of days until Creatnx becomes happy.Each of this numbers should be found by modulo \(998244353\). Formally, let \(M = 998244353\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
The first line contains two integers \(n\), \(q\) (\(2 \leq n, q \le 2 \cdot 10^5\)) β the number of mirrors and queries.The second line contains \(n\) integers: \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq 100\)).Each of \(q\) following lines contains a single integer \(u\) (\(2 \leq u \leq n\)) β next query.
|
Print \(q\) numbers β the answers after each query by modulo \(998244353\).
|
In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to \(\frac{1}{2}\). So, the expected number of days, until one mirror will say, that he is beautiful is equal to \(2\) and the answer will be equal to \(4 = 2 + 2\).
|
Input: 2 2 50 50 2 2 | Output: 4 6
|
Expert
| 2 | 1,690 | 311 | 75 | 12 |
665 |
B
|
665B
|
B. Shopping
| 1,400 |
brute force
|
Ayush is a cashier at the shopping center. Recently his department has started a ''click and collect"" service which allows users to shop online. The store contains k items. n customers have already used the above service. Each user paid for m items. Let aij denote the j-th item in the i-th person's order.Due to the space limitations all the items are arranged in one single row. When Ayush receives the i-th order he will find one by one all the items aij (1 β€ j β€ m) in the row. Let pos(x) denote the position of the item x in the row at the moment of its collection. Then Ayush takes time equal to pos(ai1) + pos(ai2) + ... + pos(aim) for the i-th customer.When Ayush accesses the x-th element he keeps a new stock in the front of the row and takes away the x-th element. Thus the values are updating.Your task is to calculate the total time it takes for Ayush to process all the orders.You can assume that the market has endless stock.
|
The first line contains three integers n, m and k (1 β€ n, k β€ 100, 1 β€ m β€ k) β the number of users, the number of items each user wants to buy and the total number of items at the market.The next line contains k distinct integers pl (1 β€ pl β€ k) denoting the initial positions of the items in the store. The items are numbered with integers from 1 to k.Each of the next n lines contains m distinct integers aij (1 β€ aij β€ k) β the order of the i-th person.
|
Print the only integer t β the total time needed for Ayush to process all the orders.
|
Customer 1 wants the items 1 and 5.pos(1) = 3, so the new positions are: [1, 3, 4, 2, 5].pos(5) = 5, so the new positions are: [5, 1, 3, 4, 2].Time taken for the first customer is 3 + 5 = 8.Customer 2 wants the items 3 and 1.pos(3) = 3, so the new positions are: [3, 5, 1, 4, 2].pos(1) = 3, so the new positions are: [1, 3, 5, 4, 2].Time taken for the second customer is 3 + 3 = 6.Total time is 8 + 6 = 14.Formally pos(x) is the index of x in the current row.
|
Input: 2 2 53 4 1 2 51 53 1 | Output: 14
|
Easy
| 1 | 941 | 457 | 85 | 6 |
64 |
D
|
64D
|
D. Presents
| 1,800 |
*special; greedy
|
You are given the prices of three presents. Also there are three sisters. It is known that the most valuable present is for the eldest sister. The second (by price) is for the second sister. And the less valuable present is for the youngest sister. If two (or three) presents have the same price, corresponding sisters may get the in a random way.
|
The only line contains three integer numbers a1, a2, a3 (1 β€ a1, a2, a3 β€ 100) β the prices of the presents.
|
Print three numbers i1, i2, i3 (1 β€ i1, i2, i3 β€ 3), all of them should be distinct. The first number stands for the seniority of the sister which will get the first present (1 stands for the eldest, 3 for the youngest). The second and third numbers mean the seniority of the sisters which get the second and the third present respectively.If there are multiple answers, print any of them.
|
In the second sample another possible answer is ""2 3 1"".
|
Input: 11 13 1 | Output: 2 1 3
|
Medium
| 2 | 347 | 108 | 389 | 0 |
1,468 |
J
|
1468J
|
J. Road Reform
| 1,800 |
dsu; graphs; greedy
|
There are \(n\) cities and \(m\) bidirectional roads in Berland. The \(i\)-th road connects the cities \(x_i\) and \(y_i\), and has the speed limit \(s_i\). The road network allows everyone to get from any city to any other city. The Berland Transport Ministry is planning a road reform.First of all, maintaining all \(m\) roads is too costly, so \(m - (n - 1)\) roads will be demolished in such a way that the remaining \((n - 1)\) roads still allow to get to any city from any other city. Formally, the remaining roads should represent an undirected tree.Secondly, the speed limits on the remaining roads might be changed. The changes will be done sequentially, each change is either increasing the speed limit on some road by \(1\), or decreasing it by \(1\). Since changing the speed limit requires a lot of work, the Ministry wants to minimize the number of changes.The goal of the Ministry is to have a road network of \((n - 1)\) roads with the maximum speed limit over all roads equal to exactly \(k\). They assigned you the task of calculating the minimum number of speed limit changes they have to perform so the road network meets their requirements.For example, suppose the initial map of Berland looks like that, and \(k = 7\): Then one of the optimal courses of action is to demolish the roads \(1\)β\(4\) and \(3\)β\(4\), and then decrease the speed limit on the road \(2\)β\(3\) by \(1\), so the resulting road network looks like that:
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(m\) and \(k\) (\(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 k \le 10^9\)) β the number of cities, the number of roads and the required maximum speed limit, respectively.Then \(m\) lines follow. The \(i\)-th line contains three integers \(x_i\), \(y_i\) and \(s_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\); \(1 \le s_i \le 10^9\)) β the cities connected by the \(i\)-th road and the speed limit on it, respectively. All roads are bidirectional.The road network in each test case is connected (that is, it is possible to reach any city from any other city by traveling along the road), and each pair of cities is connected by at most one road.The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). Similarly, the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print one integer β the minimum number of changes the Ministry has to perform so that the maximum speed limit among the remaining \((n - 1)\) roads is exactly \(k\).
|
The explanation for the example test:The first test case is described in the problem statement.In the second test case, the road network initially looks like that: The Ministry can demolish the roads \(1\)β\(2\), \(3\)β\(2\) and \(3\)β\(4\), and then increase the speed limit on the road \(1\)β\(4\) three times.In the third test case, the road network already meets all the requirements.In the fourth test case, it is enough to demolish the road \(1\)β\(2\) so the resulting road network meets the requirements.
|
Input: 44 5 74 1 31 2 52 3 82 4 13 4 44 6 51 2 11 3 11 4 22 4 14 3 13 2 13 2 101 2 81 3 105 5 151 2 173 1 152 3 101 4 142 5 8 | Output: 1 3 0 0
|
Medium
| 3 | 1,451 | 1,005 | 185 | 14 |
847 |
C
|
847C
|
C. Sum of Nestings
| 1,800 |
constructive algorithms
|
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence ""(()())"" is regular, because we can get correct arithmetic expression insering symbols '+' and '1': ""((1+1)+(1+1))"". Also the following sequences are regular: ""()()()"", ""(())"" and ""()"". The following sequences are not regular bracket sequences: "")("", ""(()"" and ""())(()"".In this problem you are given two integers n and k. Your task is to construct a regular bracket sequence consisting of round brackets with length 2Β·n with total sum of nesting of all opening brackets equals to exactly k. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.For example, in the sequence ""()(())"" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
|
The first line contains two integers n and k (1 β€ n β€ 3Β·105, 0 β€ k β€ 1018) β the number of opening brackets and needed total nesting.
|
Print the required regular bracket sequence consisting of round brackets.If there is no solution print ""Impossible"" (without quotes).
|
The first example is examined in the statement.In the second example the answer is ""(((())))"". The nesting of the first opening bracket is 0, the nesting of the second is 1, the nesting of the third is 2, the nesting of fourth is 3. So the total sum of nestings equals to 0 + 1 + 2 + 3 = 6.In the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence ""(())"".
|
Input: 3 1 | Output: ()(())
|
Medium
| 1 | 1,066 | 133 | 135 | 8 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.