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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
547 |
C
|
547C
|
C. Mike and Foam
| 2,300 |
bitmasks; combinatorics; dp; math; number theory
|
Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are n kinds of beer at Rico's numbered from 1 to n. i-th kind of beer has ai milliliters of foam on it. Maxim is Mike's boss. Today he told Mike to perform q queries. Initially the shelf is empty. In each request, Maxim gives him a number x. If beer number x is already in the shelf, then Mike should remove it from the shelf, otherwise he should put it in the shelf.After each query, Mike should tell him the score of the shelf. Bears are geeks. So they think that the score of a shelf is the number of pairs (i, j) of glasses in the shelf such that i < j and where is the greatest common divisor of numbers a and b.Mike is tired. So he asked you to help him in performing these requests.
|
The first line of input contains numbers n and q (1 β€ n, q β€ 2 Γ 105), the number of different kinds of beer and number of queries.The next line contains n space separated integers, a1, a2, ... , an (1 β€ ai β€ 5 Γ 105), the height of foam in top of each kind of beer.The next q lines contain the queries. Each query consists of a single integer integer x (1 β€ x β€ n), the index of a beer that should be added or removed from the shelf.
|
For each query, print the answer for that query in one line.
|
Input: 5 61 2 3 4 6123451 | Output: 013562
|
Expert
| 5 | 783 | 434 | 60 | 5 |
|
1,424 |
M
|
1424M
|
M. Ancient Language
| 2,200 |
graphs; sortings
|
While exploring the old caves, researchers found a book, or more precisely, a stash of mixed pages from a book. Luckily, all of the original pages are present and each page contains its number. Therefore, the researchers can reconstruct the book.After taking a deeper look into the contents of these pages, linguists think that this may be some kind of dictionary. What's interesting is that this ancient civilization used an alphabet which is a subset of the English alphabet, however, the order of these letters in the alphabet is not like the one in the English language.Given the contents of pages that researchers have found, your task is to reconstruct the alphabet of this ancient civilization using the provided pages from the dictionary.
|
First-line contains two integers: \(n\) and \(k\) (\(1 \le n, k \le 10^3\)) β the number of pages that scientists have found and the number of words present at each page. Following \(n\) groups contain a line with a single integer \(p_i\) (\(0 \le n \lt 10^3\)) β the number of \(i\)-th page, as well as \(k\) lines, each line containing one of the strings (up to \(100\) characters) written on the page numbered \(p_i\).
|
Output a string representing the reconstructed alphabet of this ancient civilization. If the book found is not a dictionary, output ""IMPOSSIBLE"" without quotes. In case there are multiple solutions, output any of them.
|
Input: 3 3 2 b b bbac 0 a aca acba 1 ab c ccb | Output: acb
|
Hard
| 2 | 746 | 421 | 220 | 14 |
|
1,841 |
A
|
1841A
|
A. Game with Board
| 800 |
constructive algorithms; games
|
Alice and Bob play a game. They have a blackboard; initially, there are \(n\) integers written on it, and each integer is equal to \(1\).Alice and Bob take turns; Alice goes first. On their turn, the player has to choose several (at least two) equal integers on the board, wipe them and write a new integer which is equal to their sum.For example, if the board currently contains integers \(\{1, 1, 2, 2, 2, 3\}\), then the following moves are possible: choose two integers equal to \(1\), wipe them and write an integer \(2\), then the board becomes \(\{2, 2, 2, 2, 3\}\); choose two integers equal to \(2\), wipe them and write an integer \(4\), then the board becomes \(\{1, 1, 2, 3, 4\}\); choose three integers equal to \(2\), wipe them and write an integer \(6\), then the board becomes \(\{1, 1, 3, 6\}\). If a player cannot make a move (all integers on the board are different), that player wins the game.Determine who wins if both players play optimally.
|
The first line contains one integer \(t\) (\(1 \le t \le 99\)) β the number of test cases.Each test case consists of one line containing one integer \(n\) (\(2 \le n \le 100\)) β the number of integers equal to \(1\) on the board.
|
For each test case, print Alice if Alice wins when both players play optimally. Otherwise, print Bob.
|
In the first test case, \(n = 3\), so the board initially contains integers \(\{1, 1, 1\}\). We can show that Bob can always win as follows: there are two possible first moves for Alice. if Alice chooses two integers equal to \(1\), wipes them and writes \(2\), the board becomes \(\{1, 2\}\). Bob cannot make a move, so he wins; if Alice chooses three integers equal to \(1\), wipes them and writes \(3\), the board becomes \(\{3\}\). Bob cannot make a move, so he wins. In the second test case, \(n = 6\), so the board initially contains integers \(\{1, 1, 1, 1, 1, 1\}\). Alice can win by, for example, choosing two integers equal to \(1\), wiping them and writing \(2\) on the first turn. Then the board becomes \(\{1, 1, 1, 1, 2\}\), and there are three possible responses for Bob: if Bob chooses four integers equal to \(1\), wipes them and writes \(4\), the board becomes \(\{2,4\}\). Alice cannot make a move, so she wins; if Bob chooses three integers equal to \(1\), wipes them and writes \(3\), the board becomes \(\{1,2,3\}\). Alice cannot make a move, so she wins; if Bob chooses two integers equal to \(1\), wipes them and writes \(2\), the board becomes \(\{1, 1, 2, 2\}\). Alice can continue by, for example, choosing two integers equal to \(2\), wiping them and writing \(4\). Then the board becomes \(\{1,1,4\}\). The only possible response for Bob is to choose two integers equal to \(1\) and write \(2\) instead of them; then the board becomes \(\{2,4\}\), Alice cannot make a move, so she wins.
|
Input: 236 | Output: Bob Alice
|
Beginner
| 2 | 963 | 230 | 101 | 18 |
633 |
H
|
633H
|
H. Fibonacci-ish II
| 3,100 |
data structures; implementation
|
Yash is finally tired of computing the length of the longest Fibonacci-ish sequence. He now plays around with more complex things such as Fibonacci-ish potentials. Fibonacci-ish potential of an array ai is computed as follows: Remove all elements j if there exists i < j such that ai = aj. Sort the remaining elements in ascending order, i.e. a1 < a2 < ... < an. Compute the potential as P(a) = a1Β·F1 + a2Β·F2 + ... + anΒ·Fn, where Fi is the i-th Fibonacci number (see notes for clarification). You are given an array ai of length n and q ranges from lj to rj. For each range j you have to compute the Fibonacci-ish potential of the array bi, composed using all elements of ai from lj to rj inclusive. Find these potentials modulo m.
|
The first line of the input contains integers of n and m (1 β€ n, m β€ 30 000) β the length of the initial array and the modulo, respectively.The next line contains n integers ai (0 β€ ai β€ 109) β elements of the array.Then follow the number of ranges q (1 β€ q β€ 30 000).Last q lines contain pairs of indices li and ri (1 β€ li β€ ri β€ n) β ranges to compute Fibonacci-ish potentials.
|
Print q lines, i-th of them must contain the Fibonacci-ish potential of the i-th range modulo m.
|
For the purpose of this problem define Fibonacci numbers as follows: F1 = F2 = 1. Fn = Fn - 1 + Fn - 2 for each n > 2. In the first query, the subarray [1,2,1] can be formed using the minimal set {1,2}. Thus, the potential of this subarray is 1*1+2*1=3.
|
Input: 5 102 1 2 1 222 44 5 | Output: 33
|
Master
| 2 | 731 | 379 | 96 | 6 |
16 |
A
|
16A
|
A. Flag
| 800 |
implementation
|
According to a new ISO standard, a flag of every country should have a chequered field n Γ m, each square should be of one of 10 colours, and the flag should be Β«stripedΒ»: each horizontal row of the flag should contain squares of the same colour, and the colours of adjacent horizontal rows should be different. Berland's government asked you to find out whether their flag meets the new ISO standard.
|
The first line of the input contains numbers n and m (1 β€ n, m β€ 100), n β the amount of rows, m β the amount of columns on the flag of Berland. Then there follows the description of the flag: each of the following n lines contain m characters. Each character is a digit between 0 and 9, and stands for the colour of the corresponding square.
|
Output YES, if the flag meets the new ISO standard, and NO otherwise.
|
Input: 3 3000111222 | Output: YES
|
Beginner
| 1 | 401 | 342 | 69 | 0 |
|
1,023 |
B
|
1023B
|
B. Pair of Toys
| 1,000 |
math
|
Tanechka is shopping in the toy shop. There are exactly \(n\) toys in the shop for sale, the cost of the \(i\)-th toy is \(i\) burles. She wants to choose two toys in such a way that their total cost is \(k\) burles. How many ways to do that does she have?Each toy appears in the shop exactly once. Pairs \((a, b)\) and \((b, a)\) are considered equal. Pairs \((a, b)\), where \(a=b\), are not allowed.
|
The first line of the input contains two integers \(n\), \(k\) (\(1 \le n, k \le 10^{14}\)) β the number of toys and the expected total cost of the pair of toys.
|
Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is \(k\) burles.
|
In the first example Tanechka can choose the pair of toys (\(1, 4\)) or the pair of toys (\(2, 3\)).In the second example Tanechka can choose only the pair of toys (\(7, 8\)).In the third example choosing any pair of toys will lead to the total cost less than \(20\). So the answer is 0.In the fourth example she can choose the following pairs: \((1, 1000000000000)\), \((2, 999999999999)\), \((3, 999999999998)\), ..., \((500000000000, 500000000001)\). The number of such pairs is exactly \(500000000000\).
|
Input: 8 5 | Output: 2
|
Beginner
| 1 | 402 | 161 | 184 | 10 |
938 |
G
|
938G
|
G. Shortest Path Queries
| 2,900 |
bitmasks; data structures; dsu; graphs
|
You are given an undirected connected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). There are three types of queries you have to process: 1 x y d β add an edge connecting vertex x to vertex y with weight d. It is guaranteed that there is no edge connecting x to y before this query; 2 x y β remove an edge connecting vertex x to vertex y. It is guaranteed that there was such edge in the graph, and the graph stays connected after this query; 3 x y β calculate the length of the shortest path (possibly non-simple) from vertex x to vertex y. Print the answers for all queries of type 3.
|
The first line contains two numbers n and m (1 β€ n, m β€ 200000) β the number of vertices and the number of edges in the graph, respectively.Then m lines follow denoting the edges of the graph. Each line contains three integers x, y and d (1 β€ x < y β€ n, 0 β€ d β€ 230 - 1). Each pair (x, y) is listed at most once. The initial graph is connected.Then one line follows, containing an integer q (1 β€ q β€ 200000) β the number of queries you have to process.Then q lines follow, denoting queries in the following form: 1 x y d (1 β€ x < y β€ n, 0 β€ d β€ 230 - 1) β add an edge connecting vertex x to vertex y with weight d. It is guaranteed that there is no edge connecting x to y before this query; 2 x y (1 β€ x < y β€ n) β remove an edge connecting vertex x to vertex y. It is guaranteed that there was such edge in the graph, and the graph stays connected after this query; 3 x y (1 β€ x < y β€ n) β calculate the length of the shortest path (possibly non-simple) from vertex x to vertex y. It is guaranteed that at least one query has type 3.
|
Print the answers for all queries of type 3 in the order they appear in input.
|
Input: 5 51 2 32 3 43 4 54 5 61 5 153 1 51 1 3 13 1 52 1 53 1 5 | Output: 112
|
Master
| 4 | 783 | 1,034 | 78 | 9 |
|
1,698 |
B
|
1698B
|
B. Rising Sand
| 800 |
constructive algorithms; greedy; implementation
|
There are \(n\) piles of sand where the \(i\)-th pile has \(a_i\) blocks of sand. The \(i\)-th pile is called too tall if \(1 < i < n\) and \(a_i > a_{i-1} + a_{i+1}\). That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)You are given an integer \(k\). An operation consists of picking \(k\) consecutive piles of sand and adding one unit of sand to them all. Formally, pick \(1 \leq l,r \leq n\) such that \(r-l+1=k\). Then for all \(l \leq i \leq r\), update \(a_i \gets a_i+1\).What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations?
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(3 \leq n \leq 2 \cdot 10^5\); \(1 \leq k \leq n\)) β the number of piles of sand and the size of the operation, respectively.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the sizes of the piles.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the maximum number of piles that are simultaneously too tall after some (possibly zero) operations.
|
In the first test case, we can perform the following three operations: Add one unit of sand to piles \(1\) and \(2\): \([\color{red}{3}, \color{red}{10}, 2, 4, 1]\). Add one unit of sand to piles \(4\) and \(5\): \([3, 10, 2, \color{red}{5}, \color{red}{2}]\). Add one unit of sand to piles \(3\) and \(4\): \([3, 10, \color{red}{3}, \color{red}{6}, 2]\). Now piles \(2\) and \(4\) are too tall, so in this case the answer is \(2\). It can be shown that it is impossible to make more than \(2\) piles too tall.In the second test case, any operation will increase all piles by \(1\) unit, so the number of too tall piles will always be \(0\).In the third test case, we can increase any pile by \(1\) unit of sand. It can be shown that the maximum number of too tall piles is \(1\).
|
Input: 35 22 9 2 4 14 41 3 2 13 11 3 1 | Output: 2 0 1
|
Beginner
| 3 | 682 | 604 | 145 | 16 |
1,858 |
B
|
1858B
|
B. The Walkway
| 1,500 |
brute force; dp; greedy; math; number theory
|
There are \(n\) benches near the Main Walkway in Summer Infomatics School. These benches are numbered by integers from \(1\) to \(n\) in order they follow. Also there are \(m\) cookie sellers near the Walkway. The \(i\)-th (\(1 \le i \le m\)) cookie sellers is located near the \(s_i\)-th bench.Petya is standing in the beginning of the Walkway. He will pass near all benches starting from the \(1\)-st bench and ending with the \(n\)-th bench. Petya passes the distance between two consecutive benches in \(1\) minute. He has a knapsack with an infinite amount of cookies. Petya is going to eat cookies from his knapsack and buy them from cookie sellers during the walk.Petya eats cookies only near the benches according to the following rule: he will eat the cookie near the \(i\)-th (\(1 \le i \le n\)) bench if and only if at least one of the following conditions holds: There is a cookie seller near the \(i\)-th bench. Then Petya will buy a cookie from cookie seller and eat it immediately. Petya has not yet eaten a cookie. Then Petya will take a cookie from his knapsack and eat it immediately. At least \(d\) minutes passed since Petya ate the previous cookie. In other words, Petya has not eaten a cookie near the benches \(i-1, i-2, \ldots, \max(i-d+1, 1)\). Then Petya will take a cookie from his knapsack and eat it immediately. You may assume that Petya eats cookies instantly. Petya will not eat two or more cookies near the same bench.You want to minimize the number of cookies Petya will eat during his walk. In order to do this, you will ask the administration of the Summer Informatics School to remove exactly one cookie seller from the Walkway before Petya starts his walk.Please determine the minimum possible number of cookies Petya can eat after removing exactly one cookie seller. Also determine the number of cookie sellers, such that if you remove one of them, Petya will eat the minimum possible number of cookies.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(m\) and \(d\) (\(2 \le d \le n \le 10^9\), \(2 \le m \le \min(10^{5}, n)\)) β the number of benches, the number of cookie sellers and the value of parameter \(d\) from the statement, respectively.The second line of each test case contains \(m\) integers \(s_1, s_2, \ldots, s_m\) (\(1 \le s_i \le n\)) β the locations of the cookie sellers. It is guaranteed that \(s_{i} < s_{i+1}\) for all \(1 \leq i \leq m - 1\).It is guaranteed that the sum of \(m\) over all test cases does not exceed \(10^5\).
|
For each test case print two integers β the minimum number of cookies that Petya can eat if exactly one cookie seller is removed, and the number of cookie sellers such that if one of them is removed, Petya will eat the minimum possible number of cookies.
|
In the first test case \(n=6\), \(m=2\), \(d=2\) and \(s=[2, 5]\). If no cookie seller is removed, then Petya will eat \(4\) cookies during his walk (note that you have to remove exactly one cookie seller; this case is explained only to show how Petya decides whether to eat a cookie): Petya will eat a cookie near the \(1\)-st bench since he has not yet eaten a cookie. Petya will eat a cookie near the \(2\)-nd bench since there is a cookie seller near this bench. Petya will not eat a cookie near the \(3\)-rd bench since only \(1<d\) minute passed since he ate a cookie. Petya will eat a cookie near the \(4\)-th bench since \(2\ge d\) minutes passed since he ate a cookie. Petya will eat a cookie near the \(5\)-th bench since there is a cookie seller near this bench. Petya will not eat a cookie near the \(6\)-th bench since only \(1<d\) minute passed since he ate a cookie. If the \(1\)-st cookie seller is removed, Petya will eat \(3\) cookies (near the benches \(1\), \(3\) and \(5\)). If the \(2\)-nd cookie seller is removed, Petya will eat \(4\) cookies (near the benches \(1\), \(2\), \(4\) and \(6\)).Thus, the minimum number of cookies Petya will eat is \(3\); there is only one cookie seller such that removing it results in minimizing the number of cookies Petya will eat.In the second test case the removal of the \(1\)-st or the \(2\)-nd cookie seller results in Petya eating \(5\) cookies near the benches \(1\), \(3\), \(5\), \(7\), \(8\); the removal of the \(3\)-rd cookie seller results in Petya eating \(4\) cookies near the benches \(1\), \(3\), \(5\), \(7\). Note that the second integer you should output is the number of (that is, amount) cookie sellers, not the index of a cookie seller to remove. Thus, the answer to the second test case is 4 1 because there is only one cookie seller such that removing it results in Petya eating four cookies, which is the minimum possible.In the third test case Petya will eat \(4\) cookies no matter what cookie seller is removed.Note that Petya is not interested in minimizing the number of cookies he will eat, so he eats cookies whenever it is possible under the rule from the statement.
|
Input: 86 2 22 58 3 23 5 810 4 92 8 9 1030 5 86 8 15 24 2930 5 86 8 12 20 278 8 31 2 3 4 5 6 7 82 2 21 21000000000 3 2000000057008429 66778899 837653445 | Output: 3 1 4 1 4 4 6 4 5 2 7 7 1 1 51 1
|
Medium
| 5 | 1,942 | 662 | 254 | 18 |
788 |
B
|
788B
|
B. Weird journey
| 2,100 |
combinatorics; constructive algorithms; dfs and similar; dsu; graphs
|
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherland β Uzhlyandia.It is widely known that Uzhlyandia has n cities connected with m bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over m - 2 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia.Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help Igor β calculate the number of good paths.
|
The first line contains two integers n, m (1 β€ n, m β€ 106) β the number of cities and roads in Uzhlyandia, respectively.Each of the next m lines contains two integers u and v (1 β€ u, v β€ n) that mean that there is road between cities u and v.It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself.
|
Print out the only integer β the number of good paths in Uzhlyandia.
|
In first sample test case the good paths are: 2 β 1 β 3 β 1 β 4 β 1 β 5, 2 β 1 β 3 β 1 β 5 β 1 β 4, 2 β 1 β 4 β 1 β 5 β 1 β 3, 3 β 1 β 2 β 1 β 4 β 1 β 5, 3 β 1 β 2 β 1 β 5 β 1 β 4, 4 β 1 β 2 β 1 β 3 β 1 β 5. There are good paths that are same with displayed above, because the sets of roads they pass over once are same: 2 β 1 β 4 β 1 β 3 β 1 β 5, 2 β 1 β 5 β 1 β 3 β 1 β 4, 2 β 1 β 5 β 1 β 4 β 1 β 3, 3 β 1 β 4 β 1 β 2 β 1 β 5, 3 β 1 β 5 β 1 β 2 β 1 β 4, 4 β 1 β 3 β 1 β 2 β 1 β 5, and all the paths in the other direction. Thus, the answer is 6.In the second test case, Igor simply can not walk by all the roads.In the third case, Igor walks once over every road.
|
Input: 5 41 21 31 41 5 | Output: 6
|
Hard
| 5 | 767 | 406 | 68 | 7 |
1,346 |
C
|
1346C
|
C. Spring Cleaning
| 1,600 |
*special; greedy; sortings
|
Tanya wants to organize her bookcase. There are \(n\) bookshelves in the bookcase, the \(i\)-th bookshelf contains \(a_i\) books on it. Tanya will be satisfied if each bookshelf contains no more than \(k\) books.Tanya can do one of the two following operations to achieve her goal: Choose exactly one bookshelf and put all the books from it in the storage room (i. e. choose some \(i\) and assign \(a_i := 0\)). During this operation she spends \(x\) seconds. Take all books from all \(n\) bookshelves and distribute them between all \(n\) bookshelves evenly (the definition of the term is given below). During this operation she spends \(y\) seconds. Consider the sequence \(a\) of \(n\) integers. Then its even distribution is such a sequence \(b\) of \(n\) integers that the sum of \(b\) equals the sum of \(a\) and the value \(max(b) - min(b)\) is the minimum possible.For example, if the array \(a=[5, 4, 3]\) then its even distribution is \(b=[4, 4, 4]\). If \(a=[1, 2, 3, 4]\) then its even distribution is \(b=[2, 3, 3, 2]\) (or any permutation of this array).Your task is to find the minimum number of seconds Tanya has to spend to obtain the bookcase with no more than \(k\) books on each bookshelf.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains four integers \(n, k, x\) and \(y\) (\(1 \le k \le n \le 2 \cdot 10^5; 1 \le x, y \le 10^4\)) β the number of bookshelves, the maximum required number of books on each bookshelf and the number of seconds Tanya spends during the first and the second operation respectively.The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)), where \(a_i\) is the number of books on the \(i\)-th bookshelf.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
For each test case, print the answer β the minimum number of seconds Tanya has to spend to obtain the bookcase with no more than \(k\) books on each bookshelf.
|
In the first test case of the example, it's optimal to use the first operation on the fifth bookshelf. So the array \(a\) becomes \([1, 2, 2, 3, 5] \rightarrow [1, 2, 2, 3, 0]\).In the second test case of the example, it's optimal to use the first operation on the second bookshelf and then use the second operation. So the array \(a\) becomes \([1, 5, 1, 5, 5] \rightarrow [1, 0, 1, 5, 5] \rightarrow [2, 2, 3, 3, 2]\).In the third test case of the example, it's optimal to use the second operation. So the array \(a\) becomes \([1, 2, 5, 3, 5] \rightarrow [4, 3, 3, 3, 3]\).In the fourth test case of the example, it's optimal to use the first operation on the first and the second bookshelves. So the array \(a\) becomes \([4, 4, 1, 1] \rightarrow [0, 0, 1, 1]\).In the fifth test case of the example, it's optimal to use the second operation. So the array \(a\) becomes \([4, 4, 1, 1] \rightarrow [2, 3, 2, 3]\).
|
Input: 6 5 4 3 5 1 2 2 3 5 5 3 4 5 1 5 1 5 5 5 4 5 6 1 2 5 3 5 4 3 2 10 4 4 1 1 4 3 10 2 4 4 1 1 4 1 5 4 1 2 1 3 | Output: 3 9 6 4 2 9
|
Medium
| 3 | 1,209 | 738 | 159 | 13 |
1,405 |
B
|
1405B
|
B. Array Cancellation
| 1,000 |
constructive algorithms; implementation
|
You're given an array \(a\) of \(n\) integers, such that \(a_1 + a_2 + \cdots + a_n = 0\).In one operation, you can choose two different indices \(i\) and \(j\) (\(1 \le i, j \le n\)), decrement \(a_i\) by one and increment \(a_j\) by one. If \(i < j\) this operation is free, otherwise it costs one coin.How many coins do you have to spend in order to make all elements equal to \(0\)?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 10^5\)) β the number of elements.The next line contains \(n\) integers \(a_1, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)). It is given that \(\sum_{i=1}^n a_i = 0\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print the minimum number of coins we have to spend in order to make all elements equal to \(0\).
|
Possible strategy for the first test case: Do \((i=2, j=3)\) three times (free), \(a = [-3, 2, 0, 1]\). Do \((i=2, j=1)\) two times (pay two coins), \(a = [-1, 0, 0, 1]\). Do \((i=4, j=1)\) one time (pay one coin), \(a = [0, 0, 0, 0]\).
|
Input: 7 4 -3 5 -3 1 2 1 -1 4 -3 2 -3 4 4 -1 1 1 -1 7 -5 7 -6 -4 17 -13 4 6 -1000000000 -1000000000 -1000000000 1000000000 1000000000 1000000000 1 0 | Output: 3 0 4 1 8 3000000000 0
|
Beginner
| 2 | 386 | 479 | 116 | 14 |
329 |
E
|
329E
|
E. Evil
| 3,100 |
math
|
There are n cities on a two dimensional Cartesian plane. The distance between two cities is equal to the Manhattan distance between them (see the Notes for definition). A Hamiltonian cycle of the cities is defined as a permutation of all n cities. The length of this Hamiltonian cycle is defined as the sum of the distances between adjacent cities in the permutation plus the distance between the first and final city in the permutation. Please compute the longest possible length of a Hamiltonian cycle of the given cities.
|
The first line contains an integer n (3 β€ n β€ 105). Then n lines follow, each consisting of two integers xi and yi (0 β€ xi, yi β€ 109), denoting the coordinates of a city. All given points will be distinct.
|
A single line denoting the longest possible length of a Hamiltonian cycle of the given cities. You should not output the cycle, only its length.Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In the example, one of the possible Hamiltonian cycles with length 6 is (1, 1) (1, 2) (2, 1) (2, 2). There does not exist any other Hamiltonian cycle with a length greater than 6.The Manhattan distance between two cities (xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
|
Input: 41 11 22 12 2 | Output: 6
|
Master
| 1 | 524 | 205 | 293 | 3 |
2,038 |
A
|
2038A
|
A. Bonus Project
| 1,400 |
games; greedy
|
There is a team of \(n\) software engineers numbered from \(1\) to \(n\). Their boss promises to give them a bonus if they complete an additional project. The project requires \(k\) units of work in total. The bonus promised to the \(i\)-th engineer is \(a_i\) burles. The boss doesn't assign specific tasks to engineers; it is expected that every engineer will voluntarily complete some integer amount of work units. The bonus will be paid to the entire team only if the project is completed; in other words, if the total amount of voluntary work units on the project is greater than or equal to \(k\).The amount of work that can be performed by each engineer is not limited. However, all engineers value their labour. The \(i\)-th engineer estimates one unit of their work as \(b_i\) burles. If the bonus is paid, the benefit \(s_i\) of the \(i\)-th engineer for completing \(c\) units of work is defined as \(s_i = a_i - c \cdot b_i\). If the bonus is not paid, the engineer will not volunteer to do any work.Engineers work together for many years, so they know how the bonus is going to be distributed and how much their colleagues value the labour. That is, all \(a_i\) and all \(b_i\) are known to every engineer in the team.Engineers are eager to get the bonus, so they agreed on the following process of work distribution between them: the first engineer says: ""I will complete \(c_1\) units of work"", where \(c_1\) is a non-negative integer; then, the second engineer says: ""I will complete \(c_2\) units of work"", where \(c_2\) is a non-negative integer; ... and so on; finally, the \(n\)-th engineer says: ""I will complete \(c_n\) units of work"", where \(c_n\) is a non-negative integer. Every engineer voices \(c_i\) in a way to maximize their own benefit \(s_i\). If the expected benefit is going to be zero, an engineer will still agree to work to get the experience and to help their colleagues obtain the bonus. However, if the benefit is expected to be negative for some reason (an engineer needs to perform an excessive amount of work or the project is not going to be completed), that engineer will not work at all (completes zero amount of work units).Given that every engineer acts perfectly, your task is to find out the numbers \(c_i\) voiced by every engineer.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 1000\); \(1 \le k \le 10^6\)) β the number of engineers in the company and the number of work units the project requires, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the bonus which will be paid to the \(i\)-th engineer if the project is completed.The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 1000\)), where \(b_i\) is the work unit cost for the \(i\)-th engineer.
|
Print \(n\) integers \(c_1, c_2, \dots, c_n\) (\(0 \le c_i \le k\)) β the amount of work completed by each engineer given that every engineer behaves optimally. Note that the answer is unique.
|
In the first example, engineers distributed the work across them and got the bonus, even though the benefit for the third engineer is zero. In the second example, the bonus project requires too many work units to complete, so it's more beneficial for engineers not to work at all.
|
Input: 3 64 7 61 2 3 | Output: 1 3 2
|
Easy
| 2 | 2,290 | 545 | 192 | 20 |
936 |
A
|
936A
|
A. Save Energy!
| 1,700 |
binary search; implementation; math
|
Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k minutes after turning on.During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.It is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.
|
The single line contains three integers k, d and t (1 β€ k, d, t β€ 1018).
|
Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10 - 9.Namely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if .
|
In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready .In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes.
|
Input: 3 2 6 | Output: 6.5
|
Medium
| 3 | 704 | 72 | 249 | 9 |
571 |
B
|
571B
|
B. Minimization
| 2,000 |
dp; greedy; sortings
|
You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n.You need to permute the array elements so that value became minimal possible. In particular, it is allowed not to change order of elements at all.
|
The first line contains two integers n, k (2 β€ n β€ 3Β·105, 1 β€ k β€ min(5000, n - 1)). The second line contains n integers A[1], A[2], ..., A[n] ( - 109 β€ A[i] β€ 109), separate by spaces β elements of the array A.
|
Print the minimum possible value of the sum described in the statement.
|
In the first test one of the optimal permutations is 1 4 2. In the second test the initial order is optimal. In the third test one of the optimal permutations is 2 3 4 4 3 5.
|
Input: 3 21 2 4 | Output: 1
|
Hard
| 3 | 260 | 211 | 71 | 5 |
1,494 |
E
|
1494E
|
E. A-Z Graph
| 2,400 |
constructive algorithms; data structures; graphs; hashing
|
You are given a directed graph consisting of \(n\) vertices. Each directed edge (or arc) labeled with a single character. Initially, the graph is empty.You should process \(m\) queries with it. Each query is one of three types: ""\(+\) \(u\) \(v\) \(c\)"" β add arc from \(u\) to \(v\) with label \(c\). It's guaranteed that there is no arc \((u, v)\) in the graph at this moment; ""\(-\) \(u\) \(v\)"" β erase arc from \(u\) to \(v\). It's guaranteed that the graph contains arc \((u, v)\) at this moment; ""\(?\) \(k\)"" β find the sequence of \(k\) vertices \(v_1, v_2, \dots, v_k\) such that there exist both routes \(v_1 \to v_2 \to \dots \to v_k\) and \(v_k \to v_{k - 1} \to \dots \to v_1\) and if you write down characters along both routes you'll get the same string. You can visit the same vertices any number of times.
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le m \le 2 \cdot 10^5\)) β the number of vertices in the graph and the number of queries.The next \(m\) lines contain queries β one per line. Each query is one of three types: ""\(+\) \(u\) \(v\) \(c\)"" (\(1 \le u, v \le n\); \(u \neq v\); \(c\) is a lowercase Latin letter); ""\(-\) \(u\) \(v\)"" (\(1 \le u, v \le n\); \(u \neq v\)); ""\(?\) \(k\)"" (\(2 \le k \le 10^5\)). It's guaranteed that you don't add multiple edges and erase only existing edges. Also, there is at least one query of the third type.
|
For each query of the third type, print YES if there exist the sequence \(v_1, v_2, \dots, v_k\) described above, or NO otherwise.
|
In the first query of the third type \(k = 3\), we can, for example, choose a sequence \([1, 2, 3]\), since \(1 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3\) and \(3 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 1\).In the second query of the third type \(k = 2\), and we can't find sequence \(p_1, p_2\) such that arcs \((p_1, p_2)\) and \((p_2, p_1)\) have the same characters.In the third query of the third type, we can, for example, choose a sequence \([1, 2, 3, 2, 1]\), where \(1 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3 \xrightarrow{\text{d}} 2 \xrightarrow{\text{c}} 1\).
|
Input: 3 11 + 1 2 a + 2 3 b + 3 2 a + 2 1 b ? 3 ? 2 - 2 1 - 3 2 + 2 1 c + 3 2 d ? 5 | Output: YES NO YES
|
Expert
| 4 | 829 | 599 | 130 | 14 |
1,239 |
B
|
1239B
|
B. The World Is Just a Programming Task (Hard Version)
| 2,500 |
implementation
|
This is a harder version of the problem. In this version, \(n \le 300\,000\).Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.We remind that bracket sequence \(s\) is called correct if: \(s\) is empty; \(s\) is equal to ""(\(t\))"", where \(t\) is correct bracket sequence; \(s\) is equal to \(t_1 t_2\), i.e. concatenation of \(t_1\) and \(t_2\), where \(t_1\) and \(t_2\) are correct bracket sequences. For example, ""(()())"", ""()"" are correct, while "")("" and ""())"" are not.The cyclical shift of the string \(s\) of length \(n\) by \(k\) (\(0 \leq k < n\)) is a string formed by a concatenation of the last \(k\) symbols of the string \(s\) with the first \(n - k\) symbols of string \(s\). For example, the cyclical shift of string ""(())()"" by \(2\) equals ""()(())"".Cyclical shifts \(i\) and \(j\) are considered different, if \(i \ne j\).
|
The first line contains an integer \(n\) (\(1 \le n \le 300\,000\)), the length of the string.The second line contains a string, consisting of exactly \(n\) characters, where each of the characters is either ""("" or "")"".
|
The first line should contain a single integer β the largest beauty of the string, which can be achieved by swapping some two characters.The second line should contain integers \(l\) and \(r\) (\(1 \leq l, r \leq n\)) β the indices of two characters, which should be swapped in order to maximize the string's beauty.In case there are several possible swaps, print any of them.
|
In the first example, we can swap \(7\)-th and \(8\)-th character, obtaining a string ""()()()()()"". The cyclical shifts by \(0, 2, 4, 6, 8\) of this string form a correct bracket sequence.In the second example, after swapping \(5\)-th and \(10\)-th character, we obtain a string "")(())()()(()"". The cyclical shifts by \(11, 7, 5, 3\) of this string form a correct bracket sequence.In the third example, swap of any two brackets results in \(0\) cyclical shifts being correct bracket sequences.
|
Input: 10 ()()())(() | Output: 5 8 7
|
Expert
| 1 | 1,479 | 223 | 376 | 12 |
1,980 |
F2
|
1980F2
|
F2. Field Division (hard version)
| 2,400 |
math; sortings
|
This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.Alice and Bob are dividing the field. The field is a rectangle of size \(n \times m\) (\(2 \le n, m \le 10^9\)); the rows are numbered from \(1\) to \(n\) from top to bottom, and the columns are numbered from \(1\) to \(m\) from left to right. The cell at the intersection of row \(r\) and column \(c\) is denoted as (\(r, c\)).Bob has \(k\) (\(2 \le k \le 2 \cdot 10^5\)) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions: To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field. Alice's path will divide the field into two parts β one part will belong to Alice (this part includes the cells of her path), the other part β to Bob. Alice will own the part that includes the cell (\(n, 1\)). Bob will own the part that includes the cell (\(1, m\)). Alice wants to divide the field in such a way as to get as many cells as possible.Bob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer \(\alpha\) β the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot).Then output \(k\) non-negative integers \(a_1, a_2, \dots, a_k\), where \(a_i\) is a value such that after Bob gives Alice the \(i\)-th fountain, the maximum size of her plot will be \(\alpha + a_i\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(2 \le n, m \le 10^9\), \(2 \le k \le 2 \cdot 10^5\)) β the field sizes and the number of fountains, respectively.Then follow \(k\) lines, each containing two numbers \(r_i\) and \(c_i\) (\(1 \le r_i \le n\), \(1 \le c_i \le m\)) β the coordinates of the cell with the \(i\)-th fountain. It is guaranteed that all cells are distinct and none of them is (\(n, 1\)).It is guaranteed that the sum of \(k\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, first output \(\alpha\) β the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output \(k\) non-negative integers \(a_1, a_2, \dots, a_k\), where \(a_i\) is a value such that after Bob gives Alice the \(i\)-th fountain, the maximum size of her plot will be \(\alpha + a_i\).
|
Below are the images for the second example: The indices of the fountains are labeled in green. The cells belonging to Alice are marked in blue. Note that if Bob gives Alice fountain \(1\) or fountain \(3\), then that fountain cannot be on Alice's plot.
|
Input: 52 2 31 11 22 25 5 41 22 23 44 32 5 91 21 51 12 22 42 51 42 31 36 4 46 21 31 41 23 4 52 13 21 41 32 4 | Output: 1 1 0 1 11 0 1 0 4 1 0 0 1 1 0 0 0 0 0 6 15 0 0 0 1 2 3 0 0 0
|
Expert
| 2 | 1,808 | 636 | 347 | 19 |
870 |
F
|
870F
|
F. Paths
| 2,700 |
data structures; number theory
|
You are given a positive integer n. Let's build a graph on vertices 1, 2, ..., n in such a way that there is an edge between vertices u and v if and only if . Let d(u, v) be the shortest distance between u and v, or 0 if there is no path between them. Compute the sum of values d(u, v) over all 1 β€ u < v β€ n.The gcd (greatest common divisor) of two positive integers is the maximum positive integer that divides both of the integers.
|
Single integer n (1 β€ n β€ 107).
|
Print the sum of d(u, v) over all 1 β€ u < v β€ n.
|
All shortest paths in the first example: There are no paths between other pairs of vertices.The total distance is 2 + 1 + 1 + 2 + 1 + 1 = 8.
|
Input: 6 | Output: 8
|
Master
| 2 | 434 | 31 | 48 | 8 |
2,014 |
G
|
2014G
|
G. Milky Days
| 2,200 |
brute force; data structures; greedy; implementation
|
What is done is done, and the spoilt milk cannot be helped.Little John is as little as night is day β he was known to be a giant, at possibly \(2.1\) metres tall. It has everything to do with his love for milk.His dairy diary has \(n\) entries, showing that he acquired \(a_i\) pints of fresh milk on day \(d_i\). Milk declines in freshness with time and stays drinkable for a maximum of \(k\) days. In other words, fresh milk acquired on day \(d_i\) will be drinkable between days \(d_i\) and \(d_i+k-1\) inclusive.Every day, Little John drinks drinkable milk, up to a maximum of \(m\) pints. In other words, if there are less than \(m\) pints of milk, he will drink them all and not be satisfied; if there are at least \(m\) pints of milk, he will drink exactly \(m\) pints and be satisfied, and it's a milk satisfaction day.Little John always drinks the freshest drinkable milk first.Determine the number of milk satisfaction days for Little John.
|
The first line of the input contains a single integer \(t\) (\(1\leq t \leq 10^4\)), the number of test cases.The first line of each test case consists of three integers \(n\), \(m\), \(k\) (\(1\le n\), \(m\), \(k \le 10^5\)), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk's freshness.Then follow \(n\) lines of each test case, each with two integers \(d_i\) and \(a_i\) (\(1\le d_i\), \(a_i \le 10^6\)), the day on which the milk was acquired and the number of pints acquired. They are sorted in increasing values of \(d_i\), and all values of \(d_i\) are distinct.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer, the number of milk satisfaction days.
|
In the first test case, \(5\) pints of milk are good for \(3\) days before spoiling.In the second test case, the following will happen: On day \(1\), he will receive \(5\) pints of milk and drink \(3\) of them (leaving \(2\) pints from day \(1\)); On day \(2\), he will receive \(7\) pints of milk and drink \(3\) of them (leaving \(2\) pints from day \(1\) and \(4\) pints from day \(2\)); On day \(3\), he will drink \(3\) pints from day \(2\) (leaving \(2\) pints from day \(1\) and \(1\) pint from day \(2\)); On day \(4\), the milk acquired on day \(1\) will spoil, and he will drink \(1\) pint from day \(2\) (no more milk is left).
|
Input: 61 1 31 52 3 31 52 74 5 21 92 64 95 65 2 44 75 37 111 212 14 1 35 109 414 815 35 5 58 910 716 1021 528 9 | Output: 3 3 4 5 10 6
|
Hard
| 4 | 950 | 720 | 82 | 20 |
1,335 |
D
|
1335D
|
D. Anti-Sudoku
| 1,300 |
constructive algorithms; implementation
|
You are given a correct solution of the sudoku puzzle. If you don't know what is the sudoku, you can read about it here.The picture showing the correct sudoku solution:Blocks are bordered with bold black color.Your task is to change at most \(9\) elements of this field (i.e. choose some \(1 \le i, j \le 9\) and change the number at the position \((i, j)\) to any other number in range \([1; 9]\)) to make it anti-sudoku. The anti-sudoku is the \(9 \times 9\) field, in which: Any number in this field is in range \([1; 9]\); each row contains at least two equal elements; each column contains at least two equal elements; each \(3 \times 3\) block (you can read what is the block in the link above) contains at least two equal elements. It is guaranteed that the answer exists.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of \(9\) lines, each line consists of \(9\) characters from \(1\) to \(9\) without any whitespaces β the correct solution of the sudoku puzzle.
|
For each test case, print the answer β the initial field with at most \(9\) changed elements so that the obtained field is anti-sudoku. If there are several solutions, you can print any. It is guaranteed that the answer exists.
|
Input: 1 154873296 386592714 729641835 863725149 975314628 412968357 631457982 598236471 247189563 | Output: 154873396 336592714 729645835 863725145 979314628 412958357 631457992 998236471 247789563
|
Easy
| 2 | 827 | 302 | 227 | 13 |
|
286 |
A
|
286A
|
A. Lucky Permutation
| 1,400 |
constructive algorithms; math
|
A permutation p of size n is the sequence p1, p2, ..., pn, consisting of n distinct integers, each of them is from 1 to n (1 β€ pi β€ n).A lucky permutation is such permutation p, that any integer i (1 β€ i β€ n) meets this condition ppi = n - i + 1.You have integer n. Find some lucky permutation p of size n.
|
The first line contains integer n (1 β€ n β€ 105) β the required permutation size.
|
Print ""-1"" (without the quotes) if the lucky permutation p of size n doesn't exist.Otherwise, print n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) after a space β the required permutation.If there are multiple answers, you can print any of them.
|
Input: 1 | Output: 1
|
Easy
| 2 | 306 | 80 | 249 | 2 |
|
141 |
A
|
141A
|
A. Amusing Joke
| 800 |
implementation; sortings; strings
|
So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and have guests at last. When two ""New Year and Christmas Men"" meet, thear assistants cut out of cardboard the letters from the guest's name and the host's name in honor of this event. Then the hung the letters above the main entrance. One night, when everyone went to bed, someone took all the letters of our characters' names. Then he may have shuffled the letters and put them in one pile in front of the door.The next morning it was impossible to find the culprit who had made the disorder. But everybody wondered whether it is possible to restore the names of the host and his guests from the letters lying at the door? That is, we need to verify that there are no extra letters, and that nobody will need to cut more letters.Help the ""New Year and Christmas Men"" and their friends to cope with this problem. You are given both inscriptions that hung over the front door the previous night, and a pile of letters that were found at the front door next morning.
|
The input file consists of three lines: the first line contains the guest's name, the second line contains the name of the residence host and the third line contains letters in a pile that were found at the door in the morning. All lines are not empty and contain only uppercase Latin letters. The length of each line does not exceed 100.
|
Print ""YES"" without the quotes, if the letters in the pile could be permuted to make the names of the ""New Year and Christmas Men"". Otherwise, print ""NO"" without the quotes.
|
In the first sample the letters written in the last line can be used to write the names and there won't be any extra letters left.In the second sample letter ""P"" is missing from the pile and there's an extra letter ""L"".In the third sample there's an extra letter ""L"".
|
Input: SANTACLAUSDEDMOROZSANTAMOROZDEDCLAUS | Output: YES
|
Beginner
| 3 | 1,052 | 338 | 179 | 1 |
351 |
B
|
351B
|
B. Jeff and Furik
| 1,900 |
combinatorics; dp; probabilities
|
Jeff has become friends with Furik. Now these two are going to play one quite amusing game.At the beginning of the game Jeff takes a piece of paper and writes down a permutation consisting of n numbers: p1, p2, ..., pn. Then the guys take turns to make moves, Jeff moves first. During his move, Jeff chooses two adjacent permutation elements and then the boy swaps them. During his move, Furic tosses a coin and if the coin shows ""heads"" he chooses a random pair of adjacent elements with indexes i and i + 1, for which an inequality pi > pi + 1 holds, and swaps them. But if the coin shows ""tails"", Furik chooses a random pair of adjacent elements with indexes i and i + 1, for which the inequality pi < pi + 1 holds, and swaps them. If the coin shows ""heads"" or ""tails"" and Furik has multiple ways of adjacent pairs to take, then he uniformly takes one of the pairs. If Furik doesn't have any pair to take, he tosses a coin one more time. The game ends when the permutation is sorted in the increasing order.Jeff wants the game to finish as quickly as possible (that is, he wants both players to make as few moves as possible). Help Jeff find the minimum mathematical expectation of the number of moves in the game if he moves optimally well.You can consider that the coin shows the heads (or tails) with the probability of 50 percent.
|
The first line contains integer n (1 β€ n β€ 3000). The next line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n) β the permutation p. The numbers are separated by spaces.
|
In a single line print a single real value β the answer to the problem. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.
|
In the first test the sequence is already sorted, so the answer is 0.
|
Input: 21 2 | Output: 0.000000
|
Hard
| 3 | 1,345 | 179 | 166 | 3 |
1,654 |
E
|
1654E
|
E. Arithmetic Operations
| 2,300 |
brute force; data structures; graphs; math
|
You are given an array of integers \(a_1, a_2, \ldots, a_n\).You can do the following operation any number of times (possibly zero): Choose any index \(i\) and set \(a_i\) to any integer (positive, negative or \(0\)). What is the minimum number of operations needed to turn \(a\) into an arithmetic progression? The array \(a\) is an arithmetic progression if \(a_{i+1}-a_i=a_i-a_{i-1}\) for any \(2 \leq i \leq n-1\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^5\)).
|
Print a single integer: the minimum number of operations needed to turn \(a\) into an arithmetic progression.
|
In the first test, you can get the array \(a = [11, 10, 9, 8, 7, 6, 5, 4, 3]\) by performing \(6\) operations: Set \(a_3\) to \(9\): the array becomes \([3, 2, 9, 8, 6, 9, 5, 4, 1]\); Set \(a_2\) to \(10\): the array becomes \([3, 10, 9, 8, 6, 9, 5, 4, 1]\); Set \(a_6\) to \(6\): the array becomes \([3, 10, 9, 8, 6, 6, 5, 4, 1]\); Set \(a_9\) to \(3\): the array becomes \([3, 10, 9, 8, 6, 6, 5, 4, 3]\); Set \(a_5\) to \(7\): the array becomes \([3, 10, 9, 8, 7, 6, 5, 4, 3]\); Set \(a_1\) to \(11\): the array becomes \([11, 10, 9, 8, 7, 6, 5, 4, 3]\). \(a\) is an arithmetic progression: in fact, \(a_{i+1}-a_i=a_i-a_{i-1}=-1\) for any \(2 \leq i \leq n-1\).There is no sequence of less than \(6\) operations that makes \(a\) an arithmetic progression.In the second test, you can get the array \(a = [-1, 2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38]\) by performing \(10\) operations.In the third test, you can get the array \(a = [100000, 80000, 60000, 40000, 20000, 0, -20000, -40000, -60000, -80000]\) by performing \(7\) operations.
|
Input: 9 3 2 7 8 6 9 5 4 1 | Output: 6
|
Expert
| 4 | 418 | 163 | 109 | 16 |
1,418 |
G
|
1418G
|
G. Three Occurrences
| 2,500 |
data structures; divide and conquer; hashing; two pointers
|
You are given an array \(a\) consisting of \(n\) integers. We denote the subarray \(a[l..r]\) as the array \([a_l, a_{l + 1}, \dots, a_r]\) (\(1 \le l \le r \le n\)).A subarray is considered good if every integer that occurs in this subarray occurs there exactly thrice. For example, the array \([1, 2, 2, 2, 1, 1, 2, 2, 2]\) has three good subarrays: \(a[1..6] = [1, 2, 2, 2, 1, 1]\); \(a[2..4] = [2, 2, 2]\); \(a[7..9] = [2, 2, 2]\). Calculate the number of good subarrays of the given array \(a\).
|
The first line contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le n\)).
|
Print one integer β the number of good subarrays of the array \(a\).
|
Input: 9 1 2 2 2 1 1 2 2 2 | Output: 3
|
Expert
| 4 | 500 | 166 | 68 | 14 |
|
1,498 |
E
|
1498E
|
E. Two Houses
| 2,200 |
brute force; graphs; greedy; interactive; sortings
|
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.There is a city in which Dixit lives. In the city, there are \(n\) houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the \(i\)-th house is \(k_i\).Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B.Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of \(|k_A - k_B|\), where \(k_i\) is the number of roads leading to the house \(i\). If more than one optimal pair exists, any of them is suitable.Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist?In the problem input, you are not given the direction of each road. You are given β for each house β only the number of incoming roads to that house (\(k_i\)).You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers ""Yes"" to any of your queries. Also, you cannot ask the same query twice.Once you have exhausted all your queries (or the judge responds ""Yes"" to any of your queries), your program must output its guess for the two houses and quit.See the Interaction section below for more details.
|
The first line contains a single integer \(n\) (\(3 \le n \le 500\)) denoting the number of houses in the city. The next line contains \(n\) space-separated integers \(k_1, k_2, \dots, k_n\) (\(0 \le k_i \le n - 1\)), the \(i\)-th of them represents the number of incoming roads to the \(i\)-th house.
|
In the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: ""? 1 2"": asking whether we can reach from house \(1\) to house \(2\). The judge responds with ""Yes"". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs ""! 1 2"" and quits.In the second sample input, the user program queries for six different pairs of houses, and finally answers ""! 0 0"" as it is convinced that no two houses as desired in the question exist in this city.
|
Input: 3 1 1 1 Yes | Output: ? 1 2 ! 1 2
|
Hard
| 5 | 2,158 | 301 | 0 | 14 |
|
850 |
F
|
850F
|
F. Rainbow Balls
| 2,800 |
math
|
You have a bag of balls of n different colors. You have ai balls of the i-th color.While there are at least two different colored balls in the bag, perform the following steps: Take out two random balls without replacement one by one. These balls might be the same color. Color the second ball to the color of the first ball. You are not allowed to switch the order of the balls in this step. Place both balls back in the bag. All these actions take exactly one second. Let M = 109 + 7. It can be proven that the expected amount of time needed before you stop can be represented as a rational number , where P and Q are coprime integers and where Q is not divisible by M. Return the value .
|
The first line of input will contain a single integer n (1 β€ n β€ 2 500) β the number of colors.The next line of input will contain n space separated integers a1, a2, ..., an (1 β€ ai β€ 105) β the number of balls of each color.
|
Print a single integer, the answer to the problem.
|
In the first sample, no matter what happens, the balls will become the same color after one step.For the second sample, we have 6 balls. Letβs label the balls from 1 to 6, and without loss of generality, letβs say balls 1,2,3 are initially color 1, balls 4,5 are color 2, and ball 6 are color 3.Here is an example of how these steps can go: We choose ball 5 and ball 6. Ball 6 then becomes color 2. We choose ball 4 and ball 5. Ball 5 remains the same color (color 2). We choose ball 1 and ball 5. Ball 5 becomes color 1. We choose ball 6 and ball 5. Ball 5 becomes color 2. We choose ball 3 and ball 4. Ball 4 becomes color 1. We choose ball 4 and ball 6. Ball 6 becomes color 1. We choose ball 2 and ball 5. Ball 5 becomes color 1. At this point, the game ends since all the balls are the same color. This particular sequence took 7 seconds.It can be shown that the answer to this case is .
|
Input: 21 1 | Output: 1
|
Master
| 1 | 690 | 225 | 50 | 8 |
1,433 |
F
|
1433F
|
F. Zero Remainder Sum
| 2,100 |
dp
|
You are given a matrix \(a\) of size \(n \times m\) consisting of integers.You can choose no more than \(\left\lfloor\frac{m}{2}\right\rfloor\) elements in each row. Your task is to choose these elements in such a way that their sum is divisible by \(k\) and this sum is the maximum.In other words, you can choose no more than a half (rounded down) of elements in each row, you have to find the maximum sum of these elements divisible by \(k\).Note that you can choose zero elements (and the sum of such set is \(0\)).
|
The first line of the input contains three integers \(n\), \(m\) and \(k\) (\(1 \le n, m, k \le 70\)) β the number of rows in the matrix, the number of columns in the matrix and the value of \(k\). The next \(n\) lines contain \(m\) elements each, where the \(j\)-th element of the \(i\)-th row is \(a_{i, j}\) (\(1 \le a_{i, j} \le 70\)).
|
Print one integer β the maximum sum divisible by \(k\) you can obtain.
|
In the first example, the optimal answer is \(2\) and \(4\) in the first row, \(5\) and \(2\) in the second row and \(7\) and \(4\) in the third row. The total sum is \(2 + 4 + 5 + 2 + 7 + 4 = 24\).
|
Input: 3 4 3 1 2 3 4 5 2 2 2 7 1 1 4 | Output: 24
|
Hard
| 1 | 518 | 339 | 70 | 14 |
1,076 |
B
|
1076B
|
B. Divisor Subtraction
| 1,200 |
implementation; math; number theory
|
You are given an integer number \(n\). The following algorithm is applied to it: if \(n = 0\), then end algorithm; find the smallest prime divisor \(d\) of \(n\); subtract \(d\) from \(n\) and go to step \(1\). Determine the number of subtrations the algorithm will make.
|
The only line contains a single integer \(n\) (\(2 \le n \le 10^{10}\)).
|
Print a single integer β the number of subtractions the algorithm will make.
|
In the first example \(5\) is the smallest prime divisor, thus it gets subtracted right away to make a \(0\).In the second example \(2\) is the smallest prime divisor at both steps.
|
Input: 5 | Output: 1
|
Easy
| 3 | 271 | 72 | 76 | 10 |
1,618 |
A
|
1618A
|
A. Polycarp and Sums of Subsequences
| 800 |
math; sortings
|
Polycarp had an array \(a\) of \(3\) positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array \(b\) of \(7\) integers.For example, if \(a = \{1, 4, 3\}\), then Polycarp wrote out \(1\), \(4\), \(3\), \(1 + 4 = 5\), \(1 + 3 = 4\), \(4 + 3 = 7\), \(1 + 4 + 3 = 8\). After sorting, he got an array \(b = \{1, 3, 4, 4, 5, 7, 8\}.\)Unfortunately, Polycarp lost the array \(a\). He only has the array \(b\) left. Help him to restore the array \(a\).
|
The first line contains one integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases.Each test case consists of one line which contains \(7\) integers \(b_1, b_2, \dots, b_7\) (\(1 \le b_i \le 10^9\); \(b_i \le b_{i+1}\)). Additional constraint on the input: there exists at least one array \(a\) which yields this array \(b\) as described in the statement.
|
For each test case, print \(3\) integers β \(a_1\), \(a_2\) and \(a_3\). If there can be several answers, print any of them.
|
The subsequence of the array \(a\) is a sequence that can be obtained from \(a\) by removing zero or more of its elements.Two subsequences are considered different if index sets of elements included in them are different. That is, the values of the elements don't matter in the comparison of subsequences. In particular, any array of length \(3\) has exactly \(7\) different non-empty subsequences.
|
Input: 5 1 3 4 4 5 7 8 1 2 3 4 5 6 7 300000000 300000000 300000000 600000000 600000000 600000000 900000000 1 1 2 999999998 999999999 999999999 1000000000 1 2 2 3 3 4 5 | Output: 1 4 3 4 1 2 300000000 300000000 300000000 999999998 1 1 1 2 2
|
Beginner
| 2 | 526 | 364 | 124 | 16 |
129 |
B
|
129B
|
B. Students and Shoelaces
| 1,200 |
brute force; dfs and similar; graphs; implementation
|
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.
|
The first line contains two integers n and m β the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b β the numbers of students tied by the i-th lace (1 β€ a, b β€ n, a β b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.
|
Print the single number β the number of groups of students that will be kicked out from the club.
|
In the first sample Anna and Maria won't kick out any group of students β in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then β two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
|
Input: 3 31 22 33 1 | Output: 0
|
Easy
| 4 | 1,058 | 395 | 97 | 1 |
1,739 |
F
|
1739F
|
F. Keyboard Design
| 2,600 |
bitmasks; data structures; dp; string suffix structures; strings
|
Monocarp has a dictionary of \(n\) words, consisting of \(12\) first letters of the Latin alphabet. The words are numbered from \(1\) to \(n\). In every pair of adjacent characters in each word, the characters are different. For every word \(i\), Monocarp also has an integer \(c_i\) denoting how often he uses this word.Monocarp wants to design a keyboard that would allow him to type some of the words easily. A keyboard can be denoted as a sequence of \(12\) first letters of the Latin alphabet, where each letter from a to l appears exactly once.A word can be typed with the keyboard easily if, for every pair of adjacent characters in the word, these characters are adjacent in the keyboard as well. The optimality of the keyboard is the sum of \(c_i\) over all words \(i\) that can be typed easily with it.Help Monocarp to design a keyboard with the maximum possible optimality.
|
The first line contains one integer \(n\) (\(1 \le n \le 1000\)) β the number of words.Then, \(n\) lines follow. The \(i\)-th of them contains an integer \(c_i\) (\(1 \le c_i \le 10^5\)) and a string \(s_i\) (\(2 \le |s_i| \le 2000\)) denoting the \(i\)-th word. Each character of \(s_i\) is one of \(12\) first letters of Latin alphabet, in lowercase. For every \(j \in [1, |s_i| - 1]\), the \(j\)-th character of \(s_i\) is different from the \((j+1)\)-th one.Additional constraint on the input: \(\sum \limits_{i=1}^{n} |s_i| \le 2000\).
|
Print a sequence of \(12\) first letters of the Latin alphabet, where each letter from a to l appears exactly once, denoting the optimal keyboard. If there are multiple answers, you may print any of them.
|
Input: 3 7 abacaba 10 cba 4 db | Output: hjkildbacefg
|
Expert
| 5 | 884 | 540 | 204 | 17 |
|
1,427 |
G
|
1427G
|
G. One Billion Shades of Grey
| 3,300 |
flows; graphs
|
You have to paint with shades of grey the tiles of an \(n\times n\) wall. The wall has \(n\) rows of tiles, each with \(n\) tiles.The tiles on the boundary of the wall (i.e., on the first row, last row, first column and last column) are already painted and you shall not change their color. All the other tiles are not painted. Some of the tiles are broken, you shall not paint those tiles. It is guaranteed that the tiles on the boundary are not broken.You shall paint all the non-broken tiles that are not already painted. When you paint a tile you can choose from \(10^9\) shades of grey, indexed from \(1\) to \(10^9\). You can paint multiple tiles with the same shade. Formally, painting the wall is equivalent to assigning a shade (an integer between \(1\) and \(10^9\)) to each non-broken tile that is not already painted.The contrast between two tiles is the absolute value of the difference between the shades of the two tiles. The total contrast of the wall is the sum of the contrast of all the pairs of adjacent non-broken tiles (two tiles are adjacent if they share a side).Compute the minimum possible total contrast of the wall.
|
The first line contains \(n\) (\(3\le n\le 200\)) β the number of rows and columns.Then \(n\) lines, each containing \(n\) integers, follow. The \(i\)-th of these lines describe the \(i\)-th row of tiles. It contains the \(n\) integers \(a_{ij}\) (\(-1\le a_{ij} \le 10^9)\). The value of \(a_{ij}\) described the tile on the \(i\)-th row and \(j\)-th column: If \(a_{ij}=0\), then the tile is not painted and shall be painted. If \(a_{ij}=-1\), then the tile is broken and shall not be painted. If \(1\le a_{ij}\le 10^9\), then the tile is already painted with the shade \(a_{ij}\). It is guaranteed that the tiles on the boundary are already painted, the tiles not on the boundary are not already painted, and the tiles on the boundary are not broken.
|
Print a single integer β the minimum possible total contrast of the wall.
|
Explanation of the first testcase: The initial configuration of the tiles is (tiles to paint are denoted by ?): 1 7 64 ? 61 1 1 A possible way to paint the tile achieving the minimum possible contrast of \(26\) is: 1 7 64 5 61 1 1 Explanation of the second testcase: Since all tiles are either painted or broken, there is nothing to do. The total contrast is \(396\).Explanation of the third testcase: The initial configuration of the tiles is (tiles to paint are denoted by ?): 6 6 5 4 46 ? ? ? 47 ? ? ? 38 ? ? ? 28 8 1 2 2 A possible way to paint the tiles achieving the minimum possible contrast of \(34\) is: 6 6 5 4 46 6 5 4 47 7 5 3 38 8 2 2 28 8 1 2 2
|
Input: 3 1 7 6 4 0 6 1 1 1 | Output: 26
|
Master
| 2 | 1,143 | 753 | 73 | 14 |
1,786 |
B
|
1786B
|
B. Cake Assembly Line
| 1,300 |
brute force; sortings
|
A cake assembly line in a bakery was once again optimized, and now \(n\) cakes are made at a time! In the last step, each of the \(n\) cakes should be covered with chocolate.Consider a side view on the conveyor belt, let it be a number line. The \(i\)-th cake occupies the segment \([a_i - w, a_i + w]\) on this line, each pair of these segments does not have common points. Above the conveyor, there are \(n\) dispensers, and when a common button is pressed, chocolate from the \(i\)-th dispenser will cover the conveyor segment \([b_i - h, b_i + h]\). Each pair of these segments also does not have common points. Cakes and dispensers corresponding to the first example. The calibration of this conveyor belt part has not yet been performed, so you are to make it. Determine if it's possible to shift the conveyor so that each cake has some chocolate on it, and there is no chocolate outside the cakes. You can assume that the conveyour is long enough, so the cakes never fall. Also note that the button can only be pressed once. In the first example we can shift the cakes as shown in the picture.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains three integers \(n\), \(w\), and \(h\) (\(1 \le n \le 10^5\); \(1 \le w, h \le 10^5\); \(h \le w\)) β the number of cakes and dispensers, as well as the halfwidths of cakes and segments on which the chocolate is dispensed.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(1 \le a_i \le 10^9\)) β the positions of the cakes centers. It is guaranteed that \(a_i + w < a_{i + 1} - w\) for all \(i\).The third line contains \(n\) integers \(b_1\), \(b_2\), ..., \(b_n\) (\(1 \le b_i \le 10^9\)) β the positions of the dispensers. It is guaranteed that \(b_i + h < b_{i + 1} - h\) for all \(i\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case output ""YES"", if it's possible to shift the conveyor in such a way that each cake ends up with some chocolate, and no chocolate is outside the cakes, and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
The first example is shown in the figures in the statement.In the second example, we can move the conveyor, for example, so that the centers of the cakes are at \(4, 9, 14, 19, 24\).In the third example, we can't move the conveyor accordingly.
|
Input: 43 10 565 95 16540 65 1455 2 11 6 11 16 214 9 14 19 243 3 213 22 295 16 254 4 127 36 127 13635 50 141 144 | Output: YES YES NO YES
|
Easy
| 2 | 1,100 | 901 | 353 | 17 |
2,025 |
B
|
2025B
|
B. Binomial Coefficients, Kind Of
| 1,100 |
combinatorics; dp; math
|
Recently, akshiM met a task that needed binomial coefficients to solve. He wrote a code he usually does that looked like this: for (int n = 0; n < N; n++) { // loop over n from 0 to N-1 (inclusive) C[n][0] = 1; C[n][n] = 1; for (int k = 1; k < n; k++) // loop over k from 1 to n-1 (inclusive) C[n][k] = C[n][k - 1] + C[n - 1][k - 1]; }Unfortunately, he made an error, since the right formula is the following: C[n][k] = C[n - 1][k] + C[n - 1][k - 1]But his team member keblidA is interested in values that were produced using the wrong formula. Please help him to calculate these coefficients for \(t\) various pairs \((n_i, k_i)\). Note that they should be calculated according to the first (wrong) formula.Since values \(C[n_i][k_i]\) may be too large, print them modulo \(10^9 + 7\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of pairs. Next, \(t\) pairs are written in two lines.The second line contains \(t\) integers \(n_1, n_2, \dots, n_t\) (\(2 \le n_i \le 10^5\)).The third line contains \(t\) integers \(k_1, k_2, \dots, k_t\) (\(1 \le k_i < n_i\)).
|
Print \(t\) integers \(C[n_i][k_i]\) modulo \(10^9 + 7\).
|
Input: 72 5 5 100000 100000 100000 1000001 2 3 1 33333 66666 99999 | Output: 2 4 8 2 326186014 984426998 303861760
|
Easy
| 3 | 786 | 312 | 57 | 20 |
|
223 |
C
|
223C
|
C. Partial Sums
| 1,900 |
combinatorics; math; number theory
|
You've got an array a, consisting of n integers. The array elements are indexed from 1 to n. Let's determine a two step operation like that: First we build by the array a an array s of partial sums, consisting of n elements. Element number i (1 β€ i β€ n) of array s equals . The operation x mod y means that we take the remainder of the division of number x by number y. Then we write the contents of the array s to the array a. Element number i (1 β€ i β€ n) of the array s becomes the i-th element of the array a (ai = si). You task is to find array a after exactly k described operations are applied.
|
The first line contains two space-separated integers n and k (1 β€ n β€ 2000, 0 β€ k β€ 109). The next line contains n space-separated integers a1, a2, ..., an β elements of the array a (0 β€ ai β€ 109).
|
Print n integers β elements of the array a after the operations are applied to it. Print the elements in the order of increasing of their indexes in the array a. Separate the printed numbers by spaces.
|
Input: 3 11 2 3 | Output: 1 3 6
|
Hard
| 3 | 600 | 197 | 201 | 2 |
|
1,931 |
E
|
1931E
|
E. Anna and the Valentine's Day Gift
| 1,400 |
games; greedy; math; sortings
|
Sasha gave Anna a list \(a\) of \(n\) integers for Valentine's Day. Anna doesn't need this list, so she suggests destroying it by playing a game.Players take turns. Sasha is a gentleman, so he gives Anna the right to make the first move. On her turn, Anna must choose an element \(a_i\) from the list and reverse the sequence of its digits. For example, if Anna chose the element with a value of \(42\), it would become \(24\); if Anna chose the element with a value of \(1580\), it would become \(851\). Note that leading zeros are removed. After such a turn, the number of elements in the list does not change. On his turn, Sasha must extract two elements \(a_i\) and \(a_j\) (\(i \ne j\)) from the list, concatenate them in any order and insert the result back into the list. For example, if Sasha chose the elements equal to \(2007\) and \(19\), he would remove these two elements from the list and add the integer \(200719\) or \(192007\). After such a turn, the number of elements in the list decreases by \(1\).Players can't skip turns. The game ends when Sasha can't make a move, i.e. after Anna's move there is exactly one number left in the list. If this integer is not less than \(10^m\) (i.e., \(\ge 10^m\)), Sasha wins. Otherwise, Anna wins.It can be shown that the game will always end. Determine who will win if both players play optimally.
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follows the description of the test cases.The first line of each test case contains integers \(n\), \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 2 \cdot 10^6\)) β the number of integers in the list and the parameter determining when Sasha wins.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the list that Sasha gave to Anna.It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output: ""Sasha"", if Sasha wins with optimal play; ""Anna"", if Anna wins with optimal play.
|
Consider the first test case.Anna can reverse the integer \(2\), then Sasha can concatenate the integers \(2\) and \(14\), obtaining the integer \(214\), which is greater than \(10^2 = 100\). If Anna had reversed the integer \(14\), Sasha would have concatenated the integers \(41\) and \(2\), obtaining the integer \(412\), which is greater than \(10^2 = 100\). Anna has no other possible moves, so she loses.
|
Input: 92 214 23 59 56 14 101 2007 800 15804 55000 123 30 410 106 4 6 2 3 1 10 9 10 71 161 1108 91 2 9 10 10 2 10 24 510 10 10 10 | Output: Sasha Anna Anna Sasha Sasha Anna Anna Anna Sasha
|
Easy
| 4 | 1,355 | 581 | 113 | 19 |
721 |
B
|
721B
|
B. Passwords
| 1,100 |
implementation; math; sortings; strings
|
Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).
|
The first line of the input contains two integers n and k (1 β€ n, k β€ 100) β the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.The next n lines contains passwords, one per line β pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.
|
Print two integers β time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.
|
Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5 seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.
|
Input: 5 2cbaabcbb1abCABCabc | Output: 1 15
|
Easy
| 4 | 950 | 540 | 143 | 7 |
1,551 |
B2
|
1551B2
|
B2. Wonderful Coloring - 2
| 1,400 |
binary search; constructive algorithms; data structures; greedy
|
This problem is an extension of the problem ""Wonderful Coloring - 1"". It has quite many differences, so you should read this statement completely.Recently, Paul and Mary have found a new favorite sequence of integers \(a_1, a_2, \dots, a_n\). They want to paint it using pieces of chalk of \(k\) colors. The coloring of a sequence is called wonderful if the following conditions are met: each element of the sequence is either painted in one of \(k\) colors or isn't painted; each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); let's calculate for each of \(k\) colors the number of elements painted in the color β all calculated numbers must be equal; the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E. g. consider a sequence \(a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]\) and \(k=3\). One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence \(a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]\) and \(k=3\). Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence \(a\).
|
The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of two lines. The first one contains two integers \(n\) and \(k\) (\(1 \le n \le 2\cdot10^5\), \(1 \le k \le n\)) β the length of a given sequence and the number of colors, respectively. The second one contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
Output \(t\) lines, each of them must contain a description of a wonderful coloring for the corresponding test case.Each wonderful coloring must be printed as a sequence of \(n\) integers \(c_1, c_2, \dots, c_n\) (\(0 \le c_i \le k\)) separated by spaces where \(c_i=0\), if \(i\)-th element isn't painted; \(c_i>0\), if \(i\)-th element is painted in the \(c_i\)-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one.
|
In the first test case, the answer is shown in the figure in the statement. The red color has number \(1\), the blue color β \(2\), the green β \(3\).
|
Input: 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 | Output: 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0
|
Easy
| 4 | 1,251 | 511 | 520 | 15 |
1,070 |
J
|
1070J
|
J. Streets and Avenues in Berhattan
| 2,300 |
dp
|
Berhattan is the capital of Berland. There are \(n\) streets running parallel in the east-west direction (horizontally), and there are \(m\) avenues running parallel in the south-north direction (vertically). Each street intersects with each avenue, forming a crossroad. So in total there are \(n \cdot m\) crossroads in Berhattan.Recently, the government has changed in Berland. The new government wants to name all avenues and all streets after heroes of revolution.The special committee prepared the list of \(k\) names. Only these names can be used as new names for streets and avenues. Each name can be used at most once.The members of committee want to name streets and avenues in the way that minimizes inconvenience for residents. They believe that if street and avenue names start with the same letter, then their crossroad will be inconvenient. Hence only the first letter of each name matters.Given first letters of \(k\) names, find \(C\) β minimal possible number of inconvenient crossroads in Berhattan after the naming process.
|
Input contains one or several test cases to process. The first line contains \(t\) (\(1 \le t \le 30000\)) β the number of test cases. Solve test cases separately, test cases are completely independent and do not affect each other.The description of \(t\) test cases follows. Each test case starts with line with space-separated numbers \(n, m, k\) (\(1 \le n,m \le 30000\); \(n+m \le k \le 2\cdot10^5\)) β the number of streets, number of avenues and the number of names in the committee's list, respectively.The the second line of each test case contains a string of \(k\) uppercase English letters. \(i\)-th letter of the string is the first letter of \(i\)-th name from the committee's list. It's guaranteed that the sum of numbers \(n\) from all test cases is not greater than \(30000\). Similarly, the sum of numbers \(m\) from all test cases is not greater than \(30000\). The sum of numbers \(k\) from all test cases is not greater than \(2\cdot10^5\).
|
For each test case print single number \(C\) in the separate line β minimal possible number of inconvenient crossroads in Berhattan after the naming process.
|
Input: 22 3 9EEZZEEZZZ2 7 9EEZZEEZZZ | Output: 04
|
Expert
| 1 | 1,042 | 960 | 157 | 10 |
|
725 |
G
|
725G
|
G. Messages on a Tree
| 3,300 |
Alice and Bob are well-known for sending messages to each other. This time you have a rooted tree with Bob standing in the root node and copies of Alice standing in each of the other vertices. The root node has number 0, the rest are numbered 1 through n.At some moments of time some copies of Alice want to send a message to Bob and receive an answer. We will call this copy the initiator. The process of sending a message contains several steps: The initiator sends the message to the person standing in the parent node and begins waiting for the answer. When some copy of Alice receives a message from some of her children nodes, she sends the message to the person standing in the parent node and begins waiting for the answer. When Bob receives a message from some of his child nodes, he immediately sends the answer to the child node where the message came from. When some copy of Alice (except for initiator) receives an answer she is waiting for, she immediately sends it to the child vertex where the message came from. When the initiator receives the answer she is waiting for, she doesn't send it to anybody. There is a special case: a copy of Alice can't wait for two answers at the same time, so if some copy of Alice receives a message from her child node while she already waits for some answer, she rejects the message and sends a message saying this back to the child node where the message came from. Then the copy of Alice in the child vertex processes this answer as if it was from Bob. The process of sending a message to a parent node or to a child node is instant but a receiver (a parent or a child) gets a message after 1 second. If some copy of Alice receives several messages from child nodes at the same moment while she isn't waiting for an answer, she processes the message from the initiator with the smallest number and rejects all the rest. If some copy of Alice receives messages from children nodes and also receives the answer she is waiting for at the same instant, then Alice first processes the answer, then immediately continue as normal with the incoming messages.You are given the moments of time when some copy of Alice becomes the initiator and sends a message to Bob. For each message, find the moment of time when the answer (either from Bob or some copy of Alice) will be received by the initiator.You can assume that if Alice wants to send a message (i.e. become the initiator) while waiting for some answer, she immediately rejects the message and receives an answer from herself in no time.
|
The first line of input contains two integers n and m (1 β€ n, m β€ 200 000) β the number of nodes with Alices and the number of messages.Second line contains n integers p1, p2, ..., pn (0 β€ pi < i). The integer pi is the number of the parent node of node i.The next m lines describe the messages. The i-th of them contains two integers xi and ti (1 β€ xi β€ n, 1 β€ ti β€ 109) β the number of the vertex of the initiator of the i-th message and the time of the initiation (in seconds). The messages are given in order of increasing initiation time (i.e. ti + 1 β₯ ti holds for 1 β€ i < m). The pairs (xi, ti) are distinct.
|
Print m integers β the i-th of them is the moment of time when the answer for the i-th message will be received by the initiator.
|
In the first example the first message is initiated at the moment 6, reaches Bob at the moment 10, and the answer reaches the initiator at the moment 14. The second message reaches vertex 2 at the moment 11. At this moment the copy of Alice in this vertex is still waiting for the answer for the first message, so she rejects the second message. The answer reaches the initiator at the moment 13. The third message is not sent at all, because at the moment 11 Alice in vertex 5 is waiting for the answer for the second message.In the second example the first message reaches Bob, the second is rejected by Alice in vertex 1. This is because the message with smaller initiator number has the priority.In the third example the first and the third messages reach Bob, while the second message is rejected by Alice in vertex 3.
|
Input: 6 30 1 2 3 2 54 66 95 11 | Output: 14 13 11
|
Master
| 0 | 2,540 | 615 | 129 | 7 |
|
1,831 |
B
|
1831B
|
B. Array merging
| 1,000 |
constructive algorithms; greedy
|
You are given two arrays \(a\) and \(b\) both of length \(n\).You will merge\(^\dagger\) these arrays forming another array \(c\) of length \(2 \cdot n\). You have to find the maximum length of a subarray consisting of equal values across all arrays \(c\) that could be obtained.\(^\dagger\) A merge of two arrays results in an array \(c\) composed by successively taking the first element of either array (as long as that array is nonempty) and removing it. After this step, the element is appended to the back of \(c\). We repeat this operation as long as we can (i.e. at least one array is nonempty).
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 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\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 2 \cdot n\)) β the elements of array \(a\).The third line of each test case contains \(n\) integers \(b_1,b_2,\ldots,b_n\) (\(1 \le b_i \le 2 \cdot n\)) β the elements of array \(b\).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the maximum length of a subarray consisting of equal values across all merges.
|
In the first test case, we can only make \(c=[2,2]\), thus the answer is \(2\).In the second test case, since all values are distinct, the answer must be \(1\).In the third test case, the arrays \(c\) we can make are \([1,2,1,2]\), \([1,2,2,1]\), \([2,1,1,2]\), \([2,1,2,1]\). We can see that the answer is \(2\) when we choose \(c=[1,2,2,1]\).
|
Input: 412231 2 34 5 621 22 151 2 2 2 22 1 1 1 1 | Output: 2 1 2 5
|
Beginner
| 2 | 603 | 698 | 105 | 18 |
831 |
B
|
831B
|
B. Keyboard Layouts
| 800 |
implementation; strings
|
There are two popular keyboard layouts in Berland, they differ only in letters positions. All the other keys are the same. In Berland they use alphabet with 26 letters which coincides with English alphabet.You are given two strings consisting of 26 distinct letters each: all keys of the first and the second layouts in the same order. You are also given some text consisting of small and capital English letters and digits. It is known that it was typed in the first layout, but the writer intended to type it in the second layout. Print the text if the same keys were pressed in the second layout.Since all keys but letters are the same in both layouts, the capitalization of the letters should remain the same, as well as all other characters.
|
The first line contains a string of length 26 consisting of distinct lowercase English letters. This is the first layout.The second line contains a string of length 26 consisting of distinct lowercase English letters. This is the second layout.The third line contains a non-empty string s consisting of lowercase and uppercase English letters and digits. This is the text typed in the first layout. The length of s does not exceed 1000.
|
Print the text if the same keys were pressed in the second layout.
|
Input: qwertyuiopasdfghjklzxcvbnmveamhjsgqocnrbfxdtwkylupziTwccpQZAvb2017 | Output: HelloVKCup2017
|
Beginner
| 2 | 746 | 436 | 66 | 8 |
|
325 |
C
|
325C
|
C. Monsters and Diamonds
| 2,600 |
dfs and similar; graphs; shortest paths
|
Piegirl has found a monster and a book about monsters and pies. When she is reading the book, she found out that there are n types of monsters, each with an ID between 1 and n. If you feed a pie to a monster, the monster will split into some number of monsters (possibly zero), and at least one colorful diamond. Monsters may be able to split in multiple ways.At the begining Piegirl has exactly one monster. She begins by feeding the monster a pie. She continues feeding pies to monsters until no more monsters are left. Then she collects all the diamonds that were created.You will be given a list of split rules describing the way in which the various monsters can split. Every monster can split in at least one way, and if a monster can split in multiple ways then each time when it splits Piegirl can choose the way it splits.For each monster, determine the smallest and the largest number of diamonds Piegirl can possibly collect, if initially she has a single instance of that monster. Piegirl has an unlimited supply of pies.
|
The first line contains two integers: m and n (1 β€ m, n β€ 105), the number of possible splits and the number of different monster types. Each of the following m lines contains a split rule. Each split rule starts with an integer (a monster ID) mi (1 β€ mi β€ n), and a positive integer li indicating the number of monsters and diamonds the current monster can split into. This is followed by li integers, with positive integers representing a monster ID and -1 representing a diamond.Each monster will have at least one split rule. Each split rule will have at least one diamond. The sum of li across all split rules will be at most 105.
|
For each monster, in order of their IDs, print a line with two integers: the smallest and the largest number of diamonds that can possibly be collected by starting with that monster. If Piegirl cannot possibly end up in a state without monsters, print -1 for both smallest and the largest value. If she can collect an arbitrarily large number of diamonds, print -2 as the largest number of diamonds. If any number in output exceeds 314000000 (but is finite), print 314000000 instead of that number.
|
Input: 6 41 3 -1 1 -11 2 -1 -12 3 -1 3 -12 3 -1 -1 -13 2 -1 -14 2 4 -1 | Output: 2 -23 42 2-1 -1
|
Expert
| 3 | 1,033 | 635 | 498 | 3 |
|
748 |
A
|
748A
|
A. Santa Claus and a Place in a Class
| 800 |
implementation; math
|
Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture).The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. The picture illustrates the first and the second samples. Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right!
|
The only line contains three integers n, m and k (1 β€ n, m β€ 10 000, 1 β€ k β€ 2nm) β the number of lanes, the number of desks in each lane and the number of Santa Claus' place.
|
Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The character s should be ""L"", if Santa Clause should sit on the left, and ""R"" if his place is on the right.
|
The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example.In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.
|
Input: 4 3 9 | Output: 2 2 L
|
Beginner
| 2 | 1,071 | 175 | 246 | 7 |
891 |
D
|
891D
|
D. Sloth
| 3,100 |
dfs and similar; dp; graph matchings; trees
|
Sloth is bad, mkay? So we decided to prepare a problem to punish lazy guys.You are given a tree, you should count the number of ways to remove an edge from it and then add an edge to it such that the final graph is a tree and has a perfect matching. Two ways of this operation are considered different if their removed edges or their added edges aren't the same. The removed edge and the added edge can be equal.A perfect matching is a subset of edges such that each vertex is an endpoint of exactly one of these edges.
|
The first line contains n (2 β€ n β€ 5Β·105) β the number of vertices.Each of the next n - 1 lines contains two integers a and b (1 β€ a, b β€ n) β the endpoints of one edge. It's guaranteed that the graph is a tree.
|
Output a single integer β the answer to the problem.
|
In first sample, there are 8 ways: edge between 2 and 3 turns to edge between 1 and 3, edge between 2 and 3 turns to edge between 1 and 4, edge between 2 and 3 turns to edge between 2 and 3, edge between 2 and 3 turns to edge between 2 and 4, edge between 1 and 2 turns to edge between 1 and 2, edge between 1 and 2 turns to edge between 1 and 4, edge between 3 and 4 turns to edge between 1 and 4, edge between 3 and 4 turns to edge between 3 and 4.
|
Input: 41 22 33 4 | Output: 8
|
Master
| 4 | 519 | 211 | 52 | 8 |
1,208 |
F
|
1208F
|
F. Bits And Pieces
| 2,600 |
bitmasks; dfs and similar; dp; greedy
|
You are given an array \(a\) of \(n\) integers.You need to find the maximum value of \(a_{i} | ( a_{j} \& a_{k} )\) over all triplets \((i,j,k)\) such that \(i < j < k\).Here \(\&\) denotes the bitwise AND operation, and \(|\) denotes the bitwise OR operation.
|
The first line of input contains the integer \(n\) (\(3 \le n \le 10^{6}\)), the size of the array \(a\).Next line contains \(n\) space separated integers \(a_1\), \(a_2\), ..., \(a_n\) (\(0 \le a_{i} \le 2 \cdot 10^{6}\)), representing the elements of the array \(a\).
|
Output a single integer, the maximum value of the expression given in the statement.
|
In the first example, the only possible triplet is \((1, 2, 3)\). Hence, the answer is \(2 | (4 \& 6) = 6\).In the second example, there are \(4\) possible triplets: \((1, 2, 3)\), value of which is \(2|(8\&4) = 2\). \((1, 2, 4)\), value of which is \(2|(8\&7) = 2\). \((1, 3, 4)\), value of which is \(2|(4\&7) = 6\). \((2, 3, 4)\), value of which is \(8|(4\&7) = 12\). The maximum value hence is \(12\).
|
Input: 3 2 4 6 | Output: 6
|
Expert
| 4 | 260 | 269 | 84 | 12 |
921 |
04
|
92104
|
04. Labyrinth-4
| 3,200 |
See the problem statement here: http://codeforces.com/contest/921/problem/01.
|
Master
| 0 | 77 | 0 | 0 | 9 |
|||||
343 |
D
|
343D
|
D. Water Tree
| 2,100 |
data structures; dfs and similar; graphs; trees
|
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.The vertices of the tree are numbered from 1 to n with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards.Mike wants to do the following operations with the tree: Fill vertex v with water. Then v and all its children are filled with water. Empty vertex v. Then v and all its ancestors are emptied. Determine whether vertex v is filled with water at the moment. Initially all vertices of the tree are empty.Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations.
|
The first line of the input contains an integer n (1 β€ n β€ 500000) β the number of vertices in the tree. Each of the following n - 1 lines contains two space-separated numbers ai, bi (1 β€ ai, bi β€ n, ai β bi) β the edges of the tree.The next line contains a number q (1 β€ q β€ 500000) β the number of operations to perform. Each of the following q lines contains two space-separated numbers ci (1 β€ ci β€ 3), vi (1 β€ vi β€ n), where ci is the operation type (according to the numbering given in the statement), and vi is the vertex on which the operation is performed.It is guaranteed that the given graph is a tree.
|
For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input.
|
Input: 51 25 12 34 2121 12 33 13 23 33 41 22 43 13 33 43 5 | Output: 00010101
|
Hard
| 4 | 984 | 613 | 192 | 3 |
|
1,354 |
A
|
1354A
|
A. Alarm Clock
| 900 |
math
|
Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least \(a\) minutes to feel refreshed.Polycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in \(b\) minutes.Every time Polycarp wakes up, he decides if he wants to sleep for some more time or not. If he's slept for less than \(a\) minutes in total, then he sets his alarm to go off in \(c\) minutes after it is reset and spends \(d\) minutes to fall asleep again. Otherwise, he gets out of his bed and proceeds with the day.If the alarm goes off while Polycarp is falling asleep, then he resets his alarm to go off in another \(c\) minutes and tries to fall asleep for \(d\) minutes again.You just want to find out when will Polycarp get out of his bed or report that it will never happen.Please check out the notes for some explanations of the example.
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of testcases.The only line of each testcase contains four integers \(a, b, c, d\) (\(1 \le a, b, c, d \le 10^9\)) β the time Polycarp has to sleep for to feel refreshed, the time before the first alarm goes off, the time before every succeeding alarm goes off and the time Polycarp spends to fall asleep.
|
For each test case print one integer. If Polycarp never gets out of his bed then print -1. Otherwise, print the time it takes for Polycarp to get out of his bed.
|
In the first testcase Polycarp wakes up after \(3\) minutes. He only rested for \(3\) minutes out of \(10\) minutes he needed. So after that he sets his alarm to go off in \(6\) minutes and spends \(4\) minutes falling asleep. Thus, he rests for \(2\) more minutes, totaling in \(3+2=5\) minutes of sleep. Then he repeats the procedure three more times and ends up with \(11\) minutes of sleep. Finally, he gets out of his bed. He spent \(3\) minutes before the first alarm and then reset his alarm four times. The answer is \(3+4 \cdot 6 = 27\).The second example is almost like the first one but Polycarp needs \(11\) minutes of sleep instead of \(10\). However, that changes nothing because he gets \(11\) minutes with these alarm parameters anyway.In the third testcase Polycarp wakes up rested enough after the first alarm. Thus, the answer is \(b=9\).In the fourth testcase Polycarp wakes up after \(5\) minutes. Unfortunately, he keeps resetting his alarm infinitely being unable to rest for even a single minute :(
|
Input: 7 10 3 6 4 11 3 6 4 5 9 4 10 6 5 2 3 1 1 1 1 3947465 47342 338129 123123 234123843 13 361451236 361451000 | Output: 27 27 9 -1 1 6471793 358578060125049
|
Beginner
| 1 | 908 | 382 | 161 | 13 |
2,073 |
E
|
2073E
| 3,300 |
interactive
|
Master
| 1 | 0 | 0 | 0 | 20 |
||||||
126 |
B
|
126B
|
B. Password
| 1,700 |
binary search; dp; hashing; string suffix structures; strings
|
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.A little later they found a string s, carved on a rock below the temple's gates. Asterix supposed that that's the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s.Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end.Asterix chose the substring t so as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substring t aloud, the temple doors opened. You know the string s. Find the substring t or determine that such substring does not exist and all that's been written above is just a nice legend.
|
You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters.
|
Print the string t. If a suitable t string does not exist, then print ""Just a legend"" without the quotes.
|
Input: fixprefixsuffix | Output: fix
|
Medium
| 5 | 1,101 | 110 | 107 | 1 |
|
1,045 |
B
|
1045B
|
B. Space Isaac
| 2,400 |
hashing; number theory
|
Everybody seems to think that the Martians are green, but it turns out they are metallic pink and fat. Ajs has two bags of distinct nonnegative integers. The bags are disjoint, and the union of the sets of numbers in the bags is \(\{0,1,β¦,M-1\}\), for some positive integer \(M\). Ajs draws a number from the first bag and a number from the second bag, and then sums them modulo \(M\).What are the residues modulo \(M\) that Ajs cannot obtain with this action?
|
The first line contains two positive integer \(N\) (\(1 \leq N \leq 200\,000\)) and \(M\) (\(N+1 \leq M \leq 10^{9}\)), denoting the number of the elements in the first bag and the modulus, respectively.The second line contains \(N\) nonnegative integers \(a_1,a_2,\ldots,a_N\) (\(0 \leq a_1<a_2< \ldots< a_N<M\)), the contents of the first bag.
|
In the first line, output the cardinality \(K\) of the set of residues modulo \(M\) which Ajs cannot obtain.In the second line of the output, print \(K\) space-separated integers greater or equal than zero and less than \(M\), which represent the residues Ajs cannot obtain. The outputs should be sorted in increasing order of magnitude. If \(K\)=0, do not output the second line.
|
In the first sample, the first bag and the second bag contain \(\{3,4\}\) and \(\{0,1,2\}\), respectively. Ajs can obtain every residue modulo \(5\) except the residue \(2\): \( 4+1 \equiv 0, \, 4+2 \equiv 1, \, 3+0 \equiv 3, \, 3+1 \equiv 4 \) modulo \(5\). One can check that there is no choice of elements from the first and the second bag which sum to \(2\) modulo \(5\).In the second sample, the contents of the first bag are \(\{5,25,125,625\}\), while the second bag contains all other nonnegative integers with at most \(9\) decimal digits. Every residue modulo \(1\,000\,000\,000\) can be obtained as a sum of an element in the first bag and an element in the second bag.
|
Input: 2 53 4 | Output: 12
|
Expert
| 2 | 460 | 345 | 380 | 10 |
1,305 |
A
|
1305A
|
A. Kuroni and the Gifts
| 800 |
brute force; constructive algorithms; greedy; sortings
|
Kuroni has \(n\) daughters. As gifts for them, he bought \(n\) necklaces and \(n\) bracelets: the \(i\)-th necklace has a brightness \(a_i\), where all the \(a_i\) are pairwise distinct (i.e. all \(a_i\) are different), the \(i\)-th bracelet has a brightness \(b_i\), where all the \(b_i\) are pairwise distinct (i.e. all \(b_i\) are different). Kuroni wants to give exactly one necklace and exactly one bracelet to each of his daughters. To make sure that all of them look unique, the total brightnesses of the gifts given to each daughter should be pairwise distinct. Formally, if the \(i\)-th daughter receives a necklace with brightness \(x_i\) and a bracelet with brightness \(y_i\), then the sums \(x_i + y_i\) should be pairwise distinct. Help Kuroni to distribute the gifts.For example, if the brightnesses are \(a = [1, 7, 5]\) and \(b = [6, 1, 2]\), then we may distribute the gifts as follows: Give the third necklace and the first bracelet to the first daughter, for a total brightness of \(a_3 + b_1 = 11\). Give the first necklace and the third bracelet to the second daughter, for a total brightness of \(a_1 + b_3 = 3\). Give the second necklace and the second bracelet to the third daughter, for a total brightness of \(a_2 + b_2 = 8\). Here is an example of an invalid distribution: Give the first necklace and the first bracelet to the first daughter, for a total brightness of \(a_1 + b_1 = 7\). Give the second necklace and the second bracelet to the second daughter, for a total brightness of \(a_2 + b_2 = 8\). Give the third necklace and the third bracelet to the third daughter, for a total brightness of \(a_3 + b_3 = 7\). This distribution is invalid, as the total brightnesses of the gifts received by the first and the third daughter are the same. Don't make them this upset!
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of daughters, necklaces and bracelets.The second line of each test case contains \(n\) distinct integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 1000\)) β the brightnesses of the necklaces.The third line of each test case contains \(n\) distinct integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 1000\)) β the brightnesses of the bracelets.
|
For each test case, print a line containing \(n\) integers \(x_1, x_2, \dots, x_n\), representing that the \(i\)-th daughter receives a necklace with brightness \(x_i\). In the next line print \(n\) integers \(y_1, y_2, \dots, y_n\), representing that the \(i\)-th daughter receives a bracelet with brightness \(y_i\).The sums \(x_1 + y_1, x_2 + y_2, \dots, x_n + y_n\) should all be distinct. The numbers \(x_1, \dots, x_n\) should be equal to the numbers \(a_1, \dots, a_n\) in some order, and the numbers \(y_1, \dots, y_n\) should be equal to the numbers \(b_1, \dots, b_n\) in some order. It can be shown that an answer always exists. If there are multiple possible answers, you may print any of them.
|
In the first test case, it is enough to give the \(i\)-th necklace and the \(i\)-th bracelet to the \(i\)-th daughter. The corresponding sums are \(1 + 8 = 9\), \(8 + 4 = 12\), and \(5 + 5 = 10\).The second test case is described in the statement.
|
Input: 2 3 1 8 5 8 4 5 3 1 7 5 6 1 2 | Output: 1 8 5 8 4 5 5 1 7 6 2 1
|
Beginner
| 4 | 1,804 | 619 | 706 | 13 |
1,687 |
A
|
1687A
|
A. The Enchanted Forest
| 1,600 |
brute force; greedy
|
The enchanted forest got its name from the magical mushrooms growing here. They may cause illusions and generally should not be approached.βPerfect Memento in Strict SenseMarisa comes to pick mushrooms in the Enchanted Forest. The Enchanted forest can be represented by \(n\) points on the \(X\)-axis numbered \(1\) through \(n\). Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by \(a_1,a_2,\ldots,a_n\).Marisa can start out at any point in the forest on minute \(0\). Each minute, the followings happen in order: She moves from point \(x\) to \(y\) (\(|x-y|\le 1\), possibly \(y=x\)). She collects all mushrooms on point \(y\). A new mushroom appears on each point in the forest. Note that she cannot collect mushrooms on minute \(0\).Now, Marisa wants to know the maximum number of mushrooms she can pick after \(k\) minutes.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(k\) (\(1 \le n \le 2 \cdot 10 ^ 5\), \(1\le k \le 10^9\)) β the number of positions with mushrooms and the time Marisa has, respectively.The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 10^9\)) β the initial number of mushrooms on point \(1,2,\ldots,n\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10 ^ 5\).
|
For each test case, print the maximum number of mushrooms Marisa can pick after \(k\) minutes.
|
Test case 1:Marisa can start at \(x=2\). In the first minute, she moves to \(x=1\) and collect \(5\) mushrooms. The number of mushrooms will be \([1,7,2,3,4]\). In the second minute, she moves to \(x=2\) and collects \(7\) mushrooms. The numbers of mushrooms will be \([2,1,3,4,5]\). After \(2\) minutes, Marisa collects \(12\) mushrooms.It can be shown that it is impossible to collect more than \(12\) mushrooms.Test case 2:This is one of her possible moving path:\(2 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 \rightarrow 5\)It can be shown that it is impossible to collect more than \(37\) mushrooms.
|
Input: 45 25 6 1 2 35 75 6 1 2 31 29999995 700001000000000 1000000000 1000000000 1000000000 1000000000 | Output: 12 37 1000000 5000349985
|
Medium
| 2 | 905 | 643 | 94 | 16 |
1,008 |
B
|
1008B
|
B. Turn the Rectangles
| 1,000 |
greedy; sortings
|
There are \(n\) rectangles in a row. You can either turn each rectangle by \(90\) degrees or leave it as it is. If you turn a rectangle, its width will be height, and its height will be width. Notice that you can turn any number of rectangles, you also can turn all or none of them. You can not change the order of the rectangles.Find out if there is a way to make the rectangles go in order of non-ascending height. In other words, after all the turns, a height of every rectangle has to be not greater than the height of the previous rectangle (if it is such).
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of rectangles.Each of the next \(n\) lines contains two integers \(w_i\) and \(h_i\) (\(1 \leq w_i, h_i \leq 10^9\)) β the width and the height of the \(i\)-th rectangle.
|
Print ""YES"" (without quotes) if there is a way to make the rectangles go in order of non-ascending height, otherwise print ""NO"".You can print each letter in any case (upper or lower).
|
In the first test, you can rotate the second and the third rectangles so that the heights will be [4, 4, 3].In the second test, there is no way the second rectangle will be not higher than the first one.
|
Input: 33 44 63 5 | Output: YES
|
Beginner
| 2 | 562 | 255 | 187 | 10 |
292 |
E
|
292E
|
E. Copying Data
| 1,900 |
data structures
|
We often have to copy large volumes of information. Such operation can take up many computer resources. Therefore, in this problem you are advised to come up with a way to copy some part of a number array into another one, quickly.More formally, you've got two arrays of integers a1, a2, ..., an and b1, b2, ..., bn of length n. Also, you've got m queries of two types: Copy the subsegment of array a of length k, starting from position x, into array b, starting from position y, that is, execute by + q = ax + q for all integer q (0 β€ q < k). The given operation is correct β both subsegments do not touch unexistent elements. Determine the value in position x of array b, that is, find value bx. For each query of the second type print the result β the value of the corresponding element of array b.
|
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of elements in the arrays and the number of queries, correspondingly. The second line contains an array of integers a1, a2, ..., an (|ai| β€ 109). The third line contains an array of integers b1, b2, ..., bn (|bi| β€ 109).Next m lines contain the descriptions of the queries. The i-th line first contains integer ti β the type of the i-th query (1 β€ ti β€ 2). If ti = 1, then the i-th query means the copying operation. If ti = 2, then the i-th query means taking the value in array b. If ti = 1, then the query type is followed by three integers xi, yi, ki (1 β€ xi, yi, ki β€ n) β the parameters of the copying query. If ti = 2, then the query type is followed by integer xi (1 β€ xi β€ n) β the position in array b.All numbers in the lines are separated with single spaces. It is guaranteed that all the queries are correct, that is, the copying borders fit into the borders of arrays a and b.
|
For each second type query print the result on a single line.
|
Input: 5 101 2 0 -1 33 1 5 -2 02 51 3 3 32 52 42 11 2 1 42 12 41 4 2 12 2 | Output: 03-1323-1
|
Hard
| 1 | 801 | 980 | 61 | 2 |
|
1,743 |
C
|
1743C
|
C. Save the Magazines
| 1,100 |
constructive algorithms; dp; greedy
|
Monocarp has been collecting rare magazines for quite a while, and now he has decided to sell them. He distributed the magazines between \(n\) boxes, arranged in a row. The \(i\)-th box contains \(a_i\) magazines. Some of the boxes are covered with lids, others are not. Suddenly it started to rain, and now Monocarp has to save as many magazines from the rain as possible. To do this, he can move the lids between boxes as follows: if the \(i\)-th box was covered with a lid initially, he can either move the lid from the \(i\)-th box to the box \((i-1)\) (if it exists), or keep the lid on the \(i\)-th box. You may assume that Monocarp can move the lids instantly at the same moment, and no lid can be moved more than once. If a box will be covered with a lid after Monocarp moves the lids, the magazines in it will be safe from the rain; otherwise they will soak.You have to calculate the maximum number of magazines Monocarp can save from the rain.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of the testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of boxes.The second line contains a string of \(n\) characters 0 and/or 1. If the \(i\)-th character is 1, the \(i\)-th box is initially covered with a lid. If the \(i\)-th character is 0, the \(i\)-th box is initially not covered.The third line contains a sequence of integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^4\)), where \(a_i\) is the number of magazines in the \(i\)-th box.The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print one integer β the maximum number of magazines Monocarp can save from the rain.
|
In the first testcase of the example, Monocarp can move the lid from the second box to the first box, so the boxes \(1\), \(3\) and \(4\) are covered, and \(10 + 8 + 9 = 27\) magazines are saved.In the second testcase, Monocarp can move the lid from the second box to the first box, then from the third box to the second box, then from the fifth box to the fourth box, and then from the sixth box to the fifth box. The boxes \(1\), \(2\), \(4\) and \(5\) will be covered, so \(20 + 10 + 30 + 20 = 80\) magazines can be saved.There are no lids in the third testcase, so it's impossible to save even a single magazine.
|
Input: 450111010 5 8 9 6601101120 10 9 30 20 1940000100 100 100 100401115 4 5 1 | Output: 27 80 0 14
|
Easy
| 3 | 953 | 666 | 103 | 17 |
899 |
A
|
899A
|
A. Splitting in Teams
| 800 |
constructive algorithms; greedy; math
|
There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team.The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team.
|
The first line contains single integer n (2 β€ n β€ 2Β·105) β the number of groups.The second line contains a sequence of integers a1, a2, ..., an (1 β€ ai β€ 2), where ai is the number of people in group i.
|
Print the maximum number of teams of three people the coach can form.
|
In the first example the coach can form one team. For example, he can take students from the first, second and fourth groups.In the second example he can't make a single team.In the third example the coach can form three teams. For example, he can do this in the following way: The first group (of two people) and the seventh group (of one person), The second group (of two people) and the sixth group (of one person), The third group (of two people) and the fourth group (of one person).
|
Input: 41 1 2 1 | Output: 1
|
Beginner
| 3 | 585 | 202 | 69 | 8 |
2,062 |
E2
|
2062E2
|
E2. The Game (Hard Version)
| 3,000 |
data structures; dfs and similar; games; graphs; implementation; trees
|
This is the hard version of the problem. The difference between the versions is that in this version, you need to find all possible nodes Cirno may choose. You can hack only if you solved all versions of this problem. Cirno and Daiyousei are playing a game with a tree\(^{\text{β}}\) of \(n\) nodes, rooted at node \(1\). The value of the \(i\)-th node is \(w_i\). They take turns to play the game; Cirno goes first.In each turn, assuming the opponent chooses \(j\) in the last turn, the player can choose any remaining node \(i\) satisfying \(w_i>w_j\) and delete the subtree\(^{\text{β }}\) of node \(i\). In particular, Cirno can choose any node and delete its subtree in the first turn.The first player who can not operate wins, and they all hope to win. Find all possible nodes Cirno may choose in the first turn so that she wins if both of them play optimally.\(^{\text{β}}\)A tree is a connected graph without cycles. \(^{\text{β }}\)Node \(u\) is considered in the subtree of node \(i\) if any path from \(1\) to \(u\) must go through \(i\).
|
The first line of input contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of input test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 4\cdot 10^5\)) β the number of nodes in the tree.The second line contains \(n\) integers \(w_1,w_2,\ldots,w_n\) (\(1 \le w_i \le n\)) β the value of each node.The next \(n-1\) lines contain the edges of the tree. The \(i\)-th line contains two integers \(u_i,v_i\) (\(1\le u_i,v_i \le n\), \(u_i \neq v_i\)) β an edge connecting \(u_i\) and \(v_i\). 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 \(4\cdot 10^5\).
|
For each test case, print one line.If Cirno wins the game, print several integers. The first integer \(k\) represents the number of possible nodes she may choose in the first turn. The other \(k\) integers are all possible nodes in increasing order.Otherwise, print ""0"" (without quotes).
|
In the first test case: If Cirno chooses \(1\) or \(3\) in the first turn, Daiyousei cannot make a move, so Daiyousei wins. If Cirno chooses \(2\) or \(4\) in the first turn, Daiyousei can only choose \(3\), but after it Cirno cannot make a move, so Cirno wins.Therefore, all possible nodes Cirno may choose are \(2\) and \(4\).In the second test case, regardless of which node Cirno chooses, Daiyousei cannot make a move, so Daiyousei wins.In the third and fourth test case, the only possible node Cirno may choose in the first turn is \(2\).In the fifth test case, all possible nodes Cirno may choose in the first turn are \(3,4,6,7\) and \(10\).
|
Input: 542 2 4 31 21 32 451 2 3 4 51 22 33 44 531 2 31 21 353 1 3 4 51 22 33 44 5101 2 3 2 4 3 3 4 4 31 44 67 46 96 57 81 22 32 10 | Output: 2 2 4 0 1 2 1 2 5 3 4 6 7 10
|
Master
| 6 | 1,047 | 680 | 289 | 20 |
1,837 |
B
|
1837B
|
B. Comparison String
| 900 |
greedy
|
You are given a string \(s\) of length \(n\), where each character is either < or >.An array \(a\) consisting of \(n+1\) elements is compatible with the string \(s\) if, for every \(i\) from \(1\) to \(n\), the character \(s_i\) represents the result of comparing \(a_i\) and \(a_{i+1}\), i. e.: \(s_i\) is < if and only if \(a_i < a_{i+1}\); \(s_i\) is > if and only if \(a_i > a_{i+1}\). For example, the array \([1, 2, 5, 4, 2]\) is compatible with the string <<>>. There are other arrays with are compatible with that string, for example, \([13, 37, 42, 37, 13]\).The cost of the array is the number of different elements in it. For example, the cost of \([1, 2, 5, 4, 2]\) is \(4\); the cost of \([13, 37, 42, 37, 13]\) is \(3\).You have to calculate the minimum cost among all arrays which are compatible with the given string \(s\).
|
The first line contains one integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.Each test case consists of two lines: the first line contains one integer \(n\) (\(1 \le n \le 100\)); the second line contains the string \(s\), consisting of \(n\) characters. Each character of \(s\) is either < or >.
|
For each test case, print one integer β the minimum cost among all arrays which are compatible with the given string \(s\).
|
In the first test case of the example, the array can be \([13, 37, 42, 37, 13]\).In the second test case of the example, the array can be \([42, 37, 13, 37, 42]\).
|
Input: 44<<>>4>><<5>>>>>7<><><>< | Output: 3 3 6 2
|
Beginner
| 1 | 839 | 310 | 123 | 18 |
337 |
B
|
337B
|
B. Routine Problem
| 1,400 |
greedy; math; number theory
|
Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio a:b. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio c:d. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on the screen as possible and fits within it completely. Thus, he may have to zoom the movie in or out, but Manao will always change the frame proportionally in both dimensions.Calculate the ratio of empty screen (the part of the screen not occupied by the movie) to the total screen size. Print the answer as an irreducible fraction p / q.
|
A single line contains four space-separated integers a, b, c, d (1 β€ a, b, c, d β€ 1000).
|
Print the answer to the problem as ""p/q"", where p is a non-negative integer, q is a positive integer and numbers p and q don't have a common divisor larger than 1.
|
Sample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension: Sample 2. This time the monitor's width is 4/3 times larger than its height and the movie's frame is square. In this case, the picture must take up the whole monitor in the vertical dimension and only 3/4 in the horizontal dimension:
|
Input: 1 1 3 2 | Output: 1/3
|
Easy
| 3 | 649 | 88 | 165 | 3 |
1,271 |
C
|
1271C
|
C. Shawarma Tent
| 1,300 |
brute force; geometry; greedy; implementation
|
The map of the capital of Berland can be viewed on the infinite coordinate plane. Each point with integer coordinates contains a building, and there are streets connecting every building to four neighbouring buildings. All streets are parallel to the coordinate axes.The main school of the capital is located in \((s_x, s_y)\). There are \(n\) students attending this school, the \(i\)-th of them lives in the house located in \((x_i, y_i)\). It is possible that some students live in the same house, but no student lives in \((s_x, s_y)\).After classes end, each student walks from the school to his house along one of the shortest paths. So the distance the \(i\)-th student goes from the school to his house is \(|s_x - x_i| + |s_y - y_i|\).The Provision Department of Berland has decided to open a shawarma tent somewhere in the capital (at some point with integer coordinates). It is considered that the \(i\)-th student will buy a shawarma if at least one of the shortest paths from the school to the \(i\)-th student's house goes through the point where the shawarma tent is located. It is forbidden to place the shawarma tent at the point where the school is located, but the coordinates of the shawarma tent may coincide with the coordinates of the house of some student (or even multiple students).You want to find the maximum possible number of students buying shawarma and the optimal location for the tent itself.
|
The first line contains three integers \(n\), \(s_x\), \(s_y\) (\(1 \le n \le 200\,000\), \(0 \le s_x, s_y \le 10^{9}\)) β the number of students and the coordinates of the school, respectively.Then \(n\) lines follow. The \(i\)-th of them contains two integers \(x_i\), \(y_i\) (\(0 \le x_i, y_i \le 10^{9}\)) β the location of the house where the \(i\)-th student lives. Some locations of houses may coincide, but no student lives in the same location where the school is situated.
|
The output should consist of two lines. The first of them should contain one integer \(c\) β the maximum number of students that will buy shawarmas at the tent. The second line should contain two integers \(p_x\) and \(p_y\) β the coordinates where the tent should be located. If there are multiple answers, print any of them. Note that each of \(p_x\) and \(p_y\) should be not less than \(0\) and not greater than \(10^{9}\).
|
In the first example, If we build the shawarma tent in \((4, 2)\), then the students living in \((4, 2)\), \((4, 1)\) and \((5, 1)\) will visit it.In the second example, it is possible to build the shawarma tent in \((1, 1)\), then both students living in \((0, 0)\) will visit it.
|
Input: 4 3 2 1 3 4 2 5 1 4 1 | Output: 3 4 2
|
Easy
| 4 | 1,426 | 483 | 427 | 12 |
1,812 |
B
|
1812B
|
B. Was it Rated?
| 0 |
*special; *special; *special; brute force; implementation
|
The only line of the input contains a single integer \(n\) (\(1 \leq n \leq 25\)).
|
Input: 1 | Output: YES
|
Beginner
| 5 | 0 | 82 | 0 | 18 |
|||
1,531 |
B2
|
1531B2
|
B2. ΠΠΎΠ½ΠΈΡΠΎΡΠΈΠ½Π³
| 0 |
*special
|
ΠΠΠΎΠ½ΡΠ°ΠΊΡΠ΅ ΠΎΡΠΊΡΡΠ»Π° Π²ΡΠΎΡΠΎΠΉ ΡΡΠ°Π± Π² Π‘Π°Π½ΠΊΡ-ΠΠ΅ΡΠ΅ΡΠ±ΡΡΠ³Π΅! ΠΡ Π½Π΅ ΠΏΡΠ΅ΠΌΠΈΠ½ΡΠ»ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΡΡ ΡΠΌΠ΅Π½ΠΈΡΡ ΠΎΠ±ΡΡΠ°Π½ΠΎΠ²ΠΊΡ ΠΈ ΡΠ΅ΡΠΈΠ»ΠΈ ΠΏΠ΅ΡΠ΅Π΅Ρ
Π°ΡΡ ΠΈΠ· ΠΎΡΠΈΡΠ° Π² ΠΠΎΠΌΠ΅ ΠΠΈΠ½Π³Π΅ΡΠ° Π² ΠΎΡΠΈΡ Π½Π° ΠΡΠ°ΡΠ½ΠΎΠΌ ΠΌΠΎΡΡΡ.ΠΠ»Ρ ΠΊΠΎΠΌΡΠΎΡΡΠ½ΠΎΠΉ ΡΠ°Π±ΠΎΡΡ Π²Π°ΠΌ ΠΏΠΎΡΡΠ΅Π±ΡΡΡΡΡ Π΄Π²Π° ΠΌΠΎΠ½ΠΈΡΠΎΡΠ° Ρ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠΉ Π²ΡΡΠΎΡΠΎΠΉ, ΡΡΠΎΠ±Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° Π½ΠΈΡ
Π²ΡΠ³Π»ΡΠ΄Π΅Π»ΠΎ Π΅Π΄ΠΈΠ½ΡΠΌ ΡΠ΅Π»ΡΠΌ. ΠΠ° ΡΠΊΠ»Π°Π΄Π΅ ΠΎΡΠΈΡΠ° Π½Π° ΠΡΠ°ΡΠ½ΠΎΠΌ ΠΌΠΎΡΡΡ Π΅ΡΡΡ \(n\) ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ², \(i\)-ΠΉ ΠΈΠ· Π½ΠΈΡ
ΠΈΠΌΠ΅Π΅Ρ ΡΠΈΡΠΈΠ½Ρ \(w_i\) ΠΈ Π²ΡΡΠΎΡΡ \(h_i\). ΠΡΠ±ΠΎΠΉ ΠΌΠΎΠ½ΠΈΡΠΎΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ²Π΅ΡΠ½ΡΡΡ Π½Π° 90 Π³ΡΠ°Π΄ΡΡΠΎΠ², ΠΈ ΡΠΎΠ³Π΄Π° ΠΎΠ½ Π±ΡΠ΄Π΅Ρ ΠΈΠΌΠ΅ΡΡ ΡΠΈΡΠΈΠ½Ρ \(h_i\) ΠΈ Π²ΡΡΠΎΡΡ \(w_i\).ΠΠ°Π·ΠΎΠ²ΡΠΌ Π½Π΅ΡΠΏΠΎΡΡΠ΄ΠΎΡΠ΅Π½Π½ΡΡ ΠΏΠ°ΡΡ ΠΈΠ· Π΄Π²ΡΡ
ΡΠ°Π·Π»ΠΈΡΠ½ΡΡ
ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠ΅ΠΉ, Π΅ΡΠ»ΠΈ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡ
ΠΏΠΎΠ²Π΅ΡΠ½ΡΡΡ ΡΠ°ΠΊ, ΡΡΠΎΠ±Ρ ΠΎΠ½ΠΈ ΠΈΠΌΠ΅Π»ΠΈ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΡΡ Π²ΡΡΠΎΡΡ. ΠΡΠ±ΠΎΠΉ ΠΈΠ· ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² Π² ΠΏΠ°ΡΠ΅ ΠΌΠΎΠΆΠ½ΠΎ ΠΊΠ°ΠΊ ΠΏΠΎΠ²Π΅ΡΠ½ΡΡΡ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΠΎ ΠΈΡΡ
ΠΎΠ΄Π½ΠΎΠΉ ΠΎΡΠΈΠ΅Π½ΡΠ°ΡΠΈΠΈ, ΡΠ°ΠΊ ΠΈ Π½Π΅ ΠΏΠΎΠ²ΠΎΡΠ°ΡΠΈΠ²Π°ΡΡ.ΠΠΎΠ΄ΡΡΠΈΡΠ°ΠΉΡΠ΅ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΠ΅ ΠΏΠ°ΡΡ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ².
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π·Π°Π΄Π°Π½ΠΎ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ \(n\) β ΡΠΈΡΠ»ΠΎ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² Π½Π° ΡΠΊΠ»Π°Π΄Π΅.Π ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ· ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
\(n\) ΡΡΡΠΎΠΊ Π·Π°Π΄Π°Π½Ρ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° \(w_i\) ΠΈ \(h_i\) (\(1 \le w_i, h_i \le 10^9\)) β ΡΠΈΡΠΈΠ½Π° ΠΈ Π²ΡΡΠΎΡΠ° \(i\)-Π³ΠΎ ΠΌΠΎΠ½ΠΈΡΠΎΡΠ°. ΠΠ±ΡΠ°ΡΠΈΡΠ΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, ΡΡΠΎ ΠΌΠΎΠ½ΠΈΡΠΎΡΡ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ ΠΊΠ²Π°Π΄ΡΠ°ΡΠ½ΡΠΌΠΈ (\(w_i = h_i\)), Π° ΡΠ°Π·ΠΌΠ΅ΡΡ ΡΠ°Π·Π½ΡΡ
ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² ΠΌΠΎΠ³ΡΡ ΡΠΎΠ²ΠΏΠ°Π΄Π°ΡΡ.Π ΡΡΠΎΠΉ Π²Π΅ΡΡΠΈΠΈ Π·Π°Π΄Π°ΡΠΈ \(2 \le n \le 10^5\).
|
ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠΈΡΠ»ΠΎ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΡ
ΠΏΠ°Ρ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ².
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΠΌΠΈ ΡΠ²Π»ΡΡΡΡΡ ΠΏΠ°ΡΡ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² Ρ Π½ΠΎΠΌΠ΅ΡΠ°ΠΌΠΈ \((1, 2)\), \((1, 4)\), \((1, 5)\), \((3, 4)\), \((4, 5)\).ΠΠΎ Π²ΡΠΎΡΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π²ΡΠ΅ ΠΏΠ°ΡΡ ΠΌΠΎΠ½ΠΈΡΠΎΡΠΎΠ² β ΠΏΠΎΠ΄Ρ
ΠΎΠ΄ΡΡΠΈΠ΅.
|
Input: 5 3 2 2 2 5 5 3 5 4 3 | Output: 5
|
Beginner
| 1 | 770 | 373 | 40 | 15 |
1,004 |
D
|
1004D
|
D. Sonya and Matrix
| 2,300 |
brute force; constructive algorithms; implementation
|
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so.The Manhattan distance between two cells (\(x_1\), \(y_1\)) and (\(x_2\), \(y_2\)) is defined as \(|x_1 - x_2| + |y_1 - y_2|\). For example, the Manhattan distance between the cells \((5, 2)\) and \((7, 1)\) equals to \(|5-7|+|2-1|=3\). Example of a rhombic matrix. Note that rhombic matrices are uniquely defined by \(n\), \(m\), and the coordinates of the cell containing the zero.She drew a \(n\times m\) rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of \(n\cdot m\) numbers). Note that Sonya will not give you \(n\) and \(m\), so only the sequence of numbers in this matrix will be at your disposal.Write a program that finds such an \(n\times m\) rhombic matrix whose elements are the same as the elements in the sequence in some order.
|
The first line contains a single integer \(t\) (\(1\leq t\leq 10^6\)) β the number of cells in the matrix.The second line contains \(t\) integers \(a_1, a_2, \ldots, a_t\) (\(0\leq a_i< t\)) β the values in the cells in arbitrary order.
|
In the first line, print two positive integers \(n\) and \(m\) (\(n \times m = t\)) β the size of the matrix.In the second line, print two integers \(x\) and \(y\) (\(1\leq x\leq n\), \(1\leq y\leq m\)) β the row number and the column number where the cell with \(0\) is located.If there are multiple possible answers, print any of them. If there is no solution, print the single integer \(-1\).
|
You can see the solution to the first example in the legend. You also can choose the cell \((2, 2)\) for the cell where \(0\) is located. You also can choose a \(5\times 4\) matrix with zero at \((4, 2)\).In the second example, there is a \(3\times 6\) matrix, where the zero is located at \((2, 3)\) there.In the third example, a solution does not exist.
|
Input: 201 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4 | Output: 4 52 2
|
Expert
| 3 | 1,239 | 236 | 395 | 10 |
1,583 |
D
|
1583D
|
D. Omkar and the Meaning of Life
| 1,800 |
constructive algorithms; greedy; interactive
|
It turns out that the meaning of life is a permutation \(p_1, p_2, \ldots, p_n\) of the integers \(1, 2, \ldots, n\) (\(2 \leq n \leq 100\)). Omkar, having created all life, knows this permutation, and will allow you to figure it out using some queries.A query consists of an array \(a_1, a_2, \ldots, a_n\) of integers between \(1\) and \(n\). \(a\) is not required to be a permutation. Omkar will first compute the pairwise sum of \(a\) and \(p\), meaning that he will compute an array \(s\) where \(s_j = p_j + a_j\) for all \(j = 1, 2, \ldots, n\). Then, he will find the smallest index \(k\) such that \(s_k\) occurs more than once in \(s\), and answer with \(k\). If there is no such index \(k\), then he will answer with \(0\).You can perform at most \(2n\) queries. Figure out the meaning of life \(p\).
|
In the sample, the hidden permutation \(p\) is \([3, 2, 1, 5, 4]\). Three queries were made.The first query is \(a = [4, 4, 2, 3, 2]\). This yields \(s = [3 + 4, 2 + 4, 1 + 2, 5 + 3, 4 + 2] = [7, 6, 3, 8, 6]\). \(6\) is the only number that appears more than once, and it appears first at index \(2\), making the answer to the query \(2\).The second query is \(a = [3, 5, 1, 5, 5]\). This yields \(s = [3 + 3, 2 + 5, 1 + 1, 5 + 5, 4 + 5] = [6, 7, 2, 10, 9]\). There are no numbers that appear more than once here, so the answer to the query is \(0\).The third query is \(a = [5, 2, 4, 3, 1]\). This yields \(s = [3 + 5, 2 + 2, 1 + 4, 5 + 3, 4 + 1] = [8, 4, 5, 8, 5]\). \(5\) and \(8\) both occur more than once here. \(5\) first appears at index \(3\), while \(8\) first appears at index \(1\), and \(1 < 3\), making the answer to the query \(1\).Note that the sample is only meant to provide an example of how the interaction works; it is not guaranteed that the above queries represent a correct strategy with which to determine the answer.
|
Input: 5 2 0 1 | Output: ? 4 4 2 3 2 ? 3 5 1 5 5 ? 5 2 4 3 1 ! 3 2 1 5 4
|
Medium
| 3 | 811 | 0 | 0 | 15 |
||
1,848 |
A
|
1848A
|
A. Vika and Her Friends
| 900 |
games; math
|
Vika and her friends went shopping in a mall, which can be represented as a rectangular grid of rooms with sides of length \(n\) and \(m\). Each room has coordinates \((a, b)\), where \(1 \le a \le n, 1 \le b \le m\). Thus we call a hall with coordinates \((c, d)\) a neighbouring for it if \(|a - c| + |b - d| = 1\).Tired of empty fashion talks, Vika decided to sneak away unnoticed. But since she hasn't had a chance to visit one of the shops yet, she doesn't want to leave the mall. After a while, her friends noticed Vika's disappearance and started looking for her.Currently, Vika is in a room with coordinates \((x, y)\), and her \(k\) friends are in rooms with coordinates \((x_1, y_1)\), \((x_2, y_2)\), ... \(, (x_k, y_k)\), respectively. The coordinates can coincide. Note that all the girls must move to the neighbouring rooms.Every minute, first Vika moves to one of the adjacent to the side rooms of her choice, and then each friend (seeing Vika's choice) also chooses one of the adjacent rooms to move to.If at the end of the minute (that is, after all the girls have moved on to the neighbouring rooms) at least one friend is in the same room as Vika, she is caught and all the other friends are called.Tell us, can Vika run away from her annoying friends forever, or will she have to continue listening to empty fashion talks after some time?
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), \(k\) (\(1 \le n, m, k \le 100\)) β the sizes of the mall and the number of Vika's friends.The second line of each test case contains a pair of integers \(x\) and \(y\) (\(1 \le x \le n\), \(1 \le y \le m\)) β the coordinates of the room where Vika is.Each of the next \(k\) lines of each test case contains a pair of integers \(x_i\) and \(y_i\) (\(1 \le x_i \le n\), \(1 \le y_i \le m\)) β the coordinates of the room where the \(i\)-th friend is.
|
For each test case, output ""YES"" if Vika can run away from her friends forever, otherwise output ""NO"".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 test case, the friend will never catch up with Vika, because Vika can always move to the room diagonally opposite to the one where the friend is.In the second test case, no matter where Vika goes, each of her friends can catch her after the first move.In the third test case, Vika and her friend will always be in different halls.
|
Input: 62 2 11 11 22 2 21 12 22 21 2 11 11 25 5 43 31 11 55 15 52 2 21 12 11 23 4 11 23 3 | Output: YES NO YES NO YES YES
|
Beginner
| 2 | 1,358 | 702 | 273 | 18 |
1,455 |
B
|
1455B
|
B. Jumps
| 1,200 |
constructive algorithms; math
|
You are standing on the \(\mathit{OX}\)-axis at point \(0\) and you want to move to an integer point \(x > 0\).You can make several jumps. Suppose you're currently at point \(y\) (\(y\) may be negative) and jump for the \(k\)-th time. You can: either jump to the point \(y + k\) or jump to the point \(y - 1\). What is the minimum number of jumps you need to reach the point \(x\)?
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first and only line of each test case contains the single integer \(x\) (\(1 \le x \le 10^6\)) β the destination point.
|
For each test case, print the single integer β the minimum number of jumps to reach \(x\). It can be proved that we can reach any integer point \(x\).
|
In the first test case \(x = 1\), so you need only one jump: the \(1\)-st jump from \(0\) to \(0 + 1 = 1\).In the second test case \(x = 2\). You need at least three jumps: the \(1\)-st jump from \(0\) to \(0 + 1 = 1\); the \(2\)-nd jump from \(1\) to \(1 + 2 = 3\); the \(3\)-rd jump from \(3\) to \(3 - 1 = 2\); Two jumps are not enough because these are the only possible variants: the \(1\)-st jump as \(-1\) and the \(2\)-nd one as \(-1\) β you'll reach \(0 -1 -1 =-2\); the \(1\)-st jump as \(-1\) and the \(2\)-nd one as \(+2\) β you'll reach \(0 -1 +2 = 1\); the \(1\)-st jump as \(+1\) and the \(2\)-nd one as \(-1\) β you'll reach \(0 +1 -1 = 0\); the \(1\)-st jump as \(+1\) and the \(2\)-nd one as \(+2\) β you'll reach \(0 +1 +2 = 3\); In the third test case, you need two jumps: the \(1\)-st one as \(+1\) and the \(2\)-nd one as \(+2\), so \(0 + 1 + 2 = 3\).In the fourth test case, you need three jumps: the \(1\)-st one as \(-1\), the \(2\)-nd one as \(+2\) and the \(3\)-rd one as \(+3\), so \(0 - 1 + 2 + 3 = 4\).
|
Input: 5 1 2 3 4 5 | Output: 1 3 2 3 4
|
Easy
| 2 | 381 | 220 | 150 | 14 |
1,494 |
F
|
1494F
|
F. Delete The Edges
| 2,900 |
brute force; constructive algorithms; dfs and similar; graphs; implementation
|
You are given an undirected connected graph consisting of \(n\) vertices and \(m\) edges. Your goal is to destroy all edges of the given graph.You may choose any vertex as the starting one and begin walking from it along the edges. When you walk along an edge, you destroy it. Obviously, you cannot walk along an edge if it is destroyed.You can perform the mode shift operation at most once during your walk, and this operation can only be performed when you are at some vertex (you cannot perform it while traversing an edge). After the mode shift, the edges you go through are deleted in the following way: the first edge after the mode shift is not destroyed, the second one is destroyed, the third one is not destroyed, the fourth one is destroyed, and so on. You cannot switch back to the original mode, and you don't have to perform this operation if you don't want to.Can you destroy all the edges of the given graph?
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 3000\); \(n - 1 \le m \le \min(\frac{n(n-1)}{2}, 3000\))) β the numbef of vertices and the number of edges in the graph.Then \(m\) lines follow, each containing two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)) β the endpoints of the \(i\)-th edge. These edges form a connected undirected graph without multiple edges.
|
If it's impossible to destroy all of the edges, print 0.Otherwise, print the sequence of your actions as follows. First, print \(k\) β the number of actions (\(k \le 2m + 2\)). Then, print the sequence itself, consisting of \(k\) integers. The first integer should be the index of the starting vertex. Then, each of the next integers should be either the index of the next vertex in your traversal, or \(-1\) if you use mode shift. You are allowed to use mode shift at most once.If there are multiple answers, print any of them.
|
Input: 3 3 1 2 2 3 3 1 | Output: 4 1 2 3 1
|
Master
| 5 | 924 | 413 | 528 | 14 |
|
771 |
E
|
771E
|
E. Bear and Rectangle Strips
| 3,000 |
dp; greedy
|
Limak has a grid that consists of 2 rows and n columns. The j-th cell in the i-th row contains an integer ti, j which can be positive, negative or zero.A non-empty rectangle of cells is called nice if and only if the sum of numbers in its cells is equal to 0.Limak wants to choose some nice rectangles and give them to his friends, as gifts. No two chosen rectangles should share a cell. What is the maximum possible number of nice rectangles Limak can choose?
|
The first line of the input contains an integer n (1 β€ n β€ 300 000) β the number of columns in the grid.The next two lines contain numbers in the grid. The i-th of those two lines contains n integers ti, 1, ti, 2, ..., ti, n ( - 109 β€ ti, j β€ 109).
|
Print one integer, denoting the maximum possible number of cell-disjoint nice rectangles.
|
In the first sample, there are four nice rectangles: Limak can't choose all of them because they are not disjoint. He should take three nice rectangles: those denoted as blue frames on the drawings.In the second sample, it's optimal to choose six nice rectangles, each consisting of one cell with a number 0.In the third sample, the only nice rectangle is the whole grid β the sum of all numbers is 0. Clearly, Limak can choose at most one nice rectangle, so the answer is 1.
|
Input: 670 70 70 70 70 -1590 -60 -30 30 -30 15 | Output: 3
|
Master
| 2 | 460 | 248 | 89 | 7 |
1,272 |
C
|
1272C
|
C. Yet Another Broken Keyboard
| 1,200 |
combinatorics; dp; implementation
|
Recently, Norge found a string \(s = s_1 s_2 \ldots s_n\) consisting of \(n\) lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string \(s\). Yes, all \(\frac{n (n + 1)}{2}\) of them!A substring of \(s\) is a non-empty string \(x = s[a \ldots b] = s_{a} s_{a + 1} \ldots s_{b}\) (\(1 \leq a \leq b \leq n\)). For example, ""auto"" and ""ton"" are substrings of ""automaton"".Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only \(k\) Latin letters \(c_1, c_2, \ldots, c_k\) out of \(26\).After that, Norge became interested in how many substrings of the string \(s\) he could still type using his broken keyboard. Help him to find this number.
|
The first line contains two space-separated integers \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq k \leq 26\)) β the length of the string \(s\) and the number of Latin letters still available on the keyboard.The second line contains the string \(s\) consisting of exactly \(n\) lowercase Latin letters.The third line contains \(k\) space-separated distinct lowercase Latin letters \(c_1, c_2, \ldots, c_k\) β the letters still available on the keyboard.
|
Print a single number β the number of substrings of \(s\) that can be typed using only available letters \(c_1, c_2, \ldots, c_k\).
|
In the first example Norge can print substrings \(s[1\ldots2]\), \(s[2\ldots3]\), \(s[1\ldots3]\), \(s[1\ldots1]\), \(s[2\ldots2]\), \(s[3\ldots3]\), \(s[5\ldots6]\), \(s[6\ldots7]\), \(s[5\ldots7]\), \(s[5\ldots5]\), \(s[6\ldots6]\), \(s[7\ldots7]\).
|
Input: 7 2 abacaba a b | Output: 12
|
Easy
| 3 | 762 | 466 | 131 | 12 |
1,525 |
C
|
1525C
|
C. Robot Collisions
| 2,000 |
data structures; greedy; implementation; sortings
|
There are \(n\) robots driving along an OX axis. There are also two walls: one is at coordinate \(0\) and one is at coordinate \(m\).The \(i\)-th robot starts at an integer coordinate \(x_i~(0 < x_i < m)\) and moves either left (towards the \(0\)) or right with the speed of \(1\) unit per second. No two robots start at the same coordinate.Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.For each robot find out if it ever explodes and print the time of explosion if it happens and \(-1\) otherwise.
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of testcases.Then the descriptions of \(t\) testcases follow.The first line of each testcase contains two integers \(n\) and \(m\) (\(1 \le n \le 3 \cdot 10^5\); \(2 \le m \le 10^8\)) β the number of robots and the coordinate of the right wall.The second line of each testcase contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(0 < x_i < m\)) β the starting coordinates of the robots.The third line of each testcase contains \(n\) space-separated characters 'L' or 'R' β the starting directions of the robots ('L' stands for left and 'R' stands for right).All coordinates \(x_i\) in the testcase are distinct.The sum of \(n\) over all testcases doesn't exceed \(3 \cdot 10^5\).
|
For each testcase print \(n\) integers β for the \(i\)-th robot output the time it explodes at if it does and \(-1\) otherwise.
|
Here is the picture for the seconds \(0, 1, 2\) and \(3\) of the first testcase: Notice that robots \(2\) and \(3\) don't collide because they meet at the same point \(2.5\), which is not integer.After second \(3\) robot \(6\) just drive infinitely because there's no robot to collide with.
|
Input: 5 7 12 1 2 3 4 9 10 11 R R L L R R R 2 10 1 6 R R 2 10 1 3 L L 1 10 5 R 7 8 6 1 7 2 3 5 4 R L R L L L L | Output: 1 1 1 1 2 -1 2 -1 -1 2 2 -1 -1 2 7 3 2 7 3
|
Hard
| 4 | 823 | 761 | 127 | 15 |
322 |
A
|
322A
|
A. Ciel and Dancing
| 1,000 |
greedy
|
Fox Ciel and her friends are in a dancing room. There are n boys and m girls here, and they never danced before. There will be some songs, during each song, there must be exactly one boy and one girl are dancing. Besides, there is a special rule: either the boy in the dancing pair must dance for the first time (so, he didn't dance with anyone before); or the girl in the dancing pair must dance for the first time. Help Fox Ciel to make a schedule that they can dance as many songs as possible.
|
The first line contains two integers n and m (1 β€ n, m β€ 100) β the number of boys and girls in the dancing room.
|
In the first line print k β the number of songs during which they can dance. Then in the following k lines, print the indexes of boys and girls dancing during songs chronologically. You can assume that the boys are indexed from 1 to n, and the girls are indexed from 1 to m.
|
In test case 1, there are 2 boys and 1 girl. We can have 2 dances: the 1st boy and 1st girl (during the first song), the 2nd boy and 1st girl (during the second song).And in test case 2, we have 2 boys with 2 girls, the answer is 3.
|
Input: 2 1 | Output: 21 12 1
|
Beginner
| 1 | 496 | 113 | 274 | 3 |
1,346 |
D
|
1346D
|
D. Constructing the Dungeon
| 1,600 |
*special; graphs; greedy
|
Polycarp is developing an RPG game where the main character fights monsters and searches for treasure in dungeons. Now Polycarp is making one of the dungeons the character can explore.The dungeon consists of \(n\) rooms connected by \(m\) two-way tunnels, and it is possible to reach every room from every other room using tunnels. The rooms are guarded by monsters (the number of monsters in the \(i\)-th room is \(a_i\)), and the tunnels contain gold coins (the number of coins in the \(i\)-th tunnel is \(w_i\)). The \(i\)-th two-way tunnel connects rooms \(v_i\) and \(u_i\).Polycarp has already fixed the number of coins in each tunnel (the values of \(w_i\) are already known), and now he tries to place the monsters in the rooms (the values of \(a_i\) are not known yet). Polycarp wants to choose the number of monsters in each room in such a way that the following two conditions are met: the number of coins for the tunnel connecting the rooms \(x\) and \(y\) should be equal to the minimum of \(a_x\) and \(a_y\). That is, for each tunnel \(i\), \(w_i = \min (a_{v_i}, a_{u_i})\); the number of monsters in the dungeon is as small as possible. That is, the value of \(a_1 + a_2 + \dots + a_n\) is minimum possible. Help Polycarp to choose the values \(a_1\), \(a_2\), ..., \(a_n\), or tell him that it is impossible and he has to change something in his dungeon plan.
|
The first line contains one integer \(t\) (\(1 \le t \le 100000\)) β the number of test cases. Then the test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n \le 200000\); \(n - 1 \le m \le \min(200000, \frac{n(n-1)}{2})\)) β the number of rooms and tunnels in the dungeon, respectively.Then \(m\) lines follow, each line describing one of the tunnels in the dungeon. The \(i\)-th line contains three integers \(v_i\), \(u_i\) and \(w_i\) (\(1 \le v_i, u_i \le n\); \(v_i \ne u_i\); \(1 \le w_i \le 10^9\)) denoting a two-way tunnel that connects rooms \(v_i\) and \(u_i\), and contains \(w_i\) coins. The tunnel system is connected in each test case (it is possible to reach every room from every other room using the tunnels). Each pair of rooms is connected by at most one tunnel.The sum of \(n\) over all test cases does not exceed \(200000\). Similarly, the sum of \(m\) over all test cases does not exceed \(200000\).
|
For each test case, print the answer as follows:If it is impossible to find the values of \(a_1\), \(a_2\), ..., \(a_n\) satisfying all the constraints, print one single string NO on a separate line. Otherwise, print YES in the first line, and \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) in the second line. If there are multiple valid answers, print any of them.
|
Input: 3 3 2 1 2 1 2 3 1 5 7 3 2 7 3 4 9 1 5 5 1 2 5 4 1 5 4 2 7 3 1 5 4 4 1 2 5 3 2 2 4 1 3 3 4 4 | Output: YES 1 1 1 YES 5 7 9 9 5 NO
|
Medium
| 3 | 1,377 | 969 | 365 | 13 |
|
955 |
B
|
955B
|
B. Not simply beatiful strings
| 1,400 |
implementation
|
Let's call a string adorable if its letters can be realigned in such a way that they form two consequent groups of equal symbols (note that different groups must contain different symbols). For example, ababa is adorable (you can transform it to aaabb, where the first three letters form a group of a-s and others β a group of b-s), but cccc is not since in each possible consequent partition letters in these two groups coincide.You're given a string s. Check whether it can be split into two non-empty subsequences such that the strings formed by these subsequences are adorable. Here a subsequence is an arbitrary set of indexes of the string.
|
The only line contains s (1 β€ |s| β€ 105) consisting of lowercase latin letters.
|
Print Β«YesΒ» if the string can be split according to the criteria above or Β«NoΒ» otherwise.Each letter can be printed in arbitrary case.
|
In sample case two zzcxx can be split into subsequences zc and zxx each of which is adorable.There's no suitable partition in sample case three.
|
Input: ababa | Output: Yes
|
Easy
| 1 | 646 | 79 | 134 | 9 |
1,924 |
D
|
1924D
|
D. Balanced Subsequences
| 2,700 |
combinatorics; dp; math
|
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not.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.You are given three integers \(n\), \(m\) and \(k\). Find the number of sequences consisting of \(n\) '(' and \(m\) ')', such that the longest balanced subsequence is of length \(2 \cdot k\). Since the answer can be large calculate it modulo \(1\,000\,000\,007\) (\(10^9 + 7\)).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 3 \cdot 10^3\)). Description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\) and \(k\) (\(1 \le n, m, k \le 2 \cdot 10^3\))
|
For each test case, print one integer β the answer to the problem.
|
For the first test case ""()()"", ""(())"" are the \(2\) sequencesFor the second test case no sequence is possible.For the third test case "")((()"", "")(()("", "")()(("", ""())(("" are the \(4\) sequences.
|
Input: 32 2 23 2 33 2 1 | Output: 2 0 4
|
Master
| 3 | 669 | 281 | 66 | 19 |
1,806 |
E
|
1806E
|
E. Tree Master
| 2,200 |
brute force; data structures; dfs and similar; trees
|
You are given a tree with \(n\) weighted vertices labeled from \(1\) to \(n\) rooted at vertex \(1\). The parent of vertex \(i\) is \(p_i\) and the weight of vertex \(i\) is \(a_i\). For convenience, define \(p_1=0\).For two vertices \(x\) and \(y\) of the same depth\(^\dagger\), define \(f(x,y)\) as follows: Initialize \(\mathrm{ans}=0\). While both \(x\) and \(y\) are not \(0\): \(\mathrm{ans}\leftarrow \mathrm{ans}+a_x\cdot a_y\); \(x\leftarrow p_x\); \(y\leftarrow p_y\). \(f(x,y)\) is the value of \(\mathrm{ans}\). You will process \(q\) queries. In the \(i\)-th query, you are given two integers \(x_i\) and \(y_i\) and you need to calculate \(f(x_i,y_i)\).\(^\dagger\) The depth of vertex \(v\) is the number of edges on the unique simple path from the root of the tree to vertex \(v\).
|
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 10^5\); \(1 \le q \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^5\)).The third line contains \(n-1\) integers \(p_2, \ldots, p_n\) (\(1 \le p_i < i\)).Each of the next \(q\) lines contains two integers \(x_i\) and \(y_i\) (\(1\le x_i,y_i\le n\)). It is guaranteed that \(x_i\) and \(y_i\) are of the same depth.
|
Output \(q\) lines, the \(i\)-th line contains a single integer, the value of \(f(x_i,y_i)\).
|
Consider the first example:In the first query, the answer is \(a_4\cdot a_5+a_3\cdot a_3+a_2\cdot a_2+a_1\cdot a_1=3+4+25+1=33\).In the second query, the answer is \(a_6\cdot a_6+a_2\cdot a_2+a_1\cdot a_1=1+25+1=27\).
|
Input: 6 2 1 5 2 3 1 1 1 2 3 3 2 4 5 6 6 | Output: 33 27
|
Hard
| 4 | 798 | 431 | 93 | 18 |
1,718 |
D
|
1718D
|
D. Permutation for Burenka
| 3,300 |
data structures; graph matchings; greedy; math; trees
|
We call an array \(a\) pure if all elements in it are pairwise distinct. For example, an array \([1, 7, 9]\) is pure, \([1, 3, 3, 7]\) isn't, because \(3\) occurs twice in it.A pure array \(b\) is similar to a pure array \(c\) if their lengths \(n\) are the same and for all pairs of indices \(l\), \(r\), such that \(1 \le l \le r \le n\), it's true that $$$\(\operatorname{argmax}([b_l, b_{l + 1}, \ldots, b_r]) = \operatorname{argmax}([c_l, c_{l + 1}, \ldots, c_r]),\)\( where \)\operatorname{argmax}(x)\( is defined as the index of the largest element in \)x\( (which is unique for pure arrays). For example, \)\operatorname{argmax}([3, 4, 2]) = 2\(, \)\operatorname{argmax}([1337, 179, 57]) = 1\(.Recently, Tonya found out that Burenka really likes a permutation \)p\( of length \)n\(. Tonya decided to please her and give her an array \)a\( similar to \)p\(. He already fixed some elements of \)a\(, but exactly \)k\( elements are missing (in these positions temporarily \)a_i = 0\(). It is guaranteed that \)k \ge 2\(. Also, he has a set \)S\( of \)k - 1\( numbers.Tonya realized that he was missing one number to fill the empty places of \)a\(, so he decided to buy it. He has \)q\( options to buy. Tonya thinks that the number \)d\( suits him, if it is possible to replace all zeros in \)a\( with numbers from \)S\( and the number \)d\(, so that \)a\( becomes a pure array similar to \)p\(. For each option of \)d$$$, output whether this number is suitable for him or not.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) is the number of test cases. The description of the test cases follows.The first line of each test case contains a couple of integers \(n\) and \(q\) (\(1 \le n, q \le 3 \cdot 10^5\)).The second line of each input test case contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) β the permutation Burenka likes.The third line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^6\)) β elements of Tonya's array, where \(0\) denotes a missing element. It is guaranteed that there are two indexes \(i, j\) \((1 \le i, j \le n, i \ne j)\) such that \(a_i = 0, a_j = 0\), which implies that \(k \geq 2\).The fourth line of each test case contains \(k - 1\) distinct integers \(s_1, s_2, \ldots, s_{k-1}\) (\(1 \le s_i \le 10^6\)) β elements of Tonya's set \(S\).Each of the next \(q\) lines contains a single integer \(d\) (\(1 \le d \le 10^6\)) β the number that Tonya plans to buy.It is guaranteed that for each given \(d\) it's possible to fill in the gaps in \(a\) with numbers from \(S\) and the number \(d\) to get a pure array.It is guaranteed that the sum of \(n\) and the sum of \(q\) in all tests does not exceed \(3 \cdot 10^5\).
|
Output \(q\) lines. For each value \(d\), print ""YES"" if there is a way to fill the array \(a\) to make it similar to \(p\), and ""NO"" otherwise.
|
In the first test case for \(d = 9\), you can get \(a = [5, 9, 7, 6]\), it can be proved that \(a\) is similar to \(p\), for \(d=1\) and \(d=4\) it can be proved that there is no answer.In the second test case for \(d = 1\), you can get \(a = [1, 5, 10, 9, 3]\), for \(d = 8\), you can get \(a = [3, 5, 10, 9, 8]\), it can be proved that for \(d = 11\) there is no answer.
|
Input: 44 31 4 3 25 0 7 069145 31 2 5 4 30 5 10 0 03 918115 21 4 3 2 50 0 0 0 07 9 1 561004 24 1 3 20 5 3 0246 | Output: YES NO NO YES YES NO YES YES NO NO
|
Master
| 5 | 1,481 | 1,259 | 148 | 17 |
946 |
C
|
946C
|
C. String Transformation
| 1,300 |
greedy; strings
|
You are given a string s consisting of |s| small english letters.In one move you can replace any character of this string to the next character in alphabetical order (a will be replaced with b, s will be replaced with t, etc.). You cannot replace letter z with any other letter.Your target is to make some number of moves (not necessary minimal) to get string abcdefghijklmnopqrstuvwxyz (english alphabet) as a subsequence. Subsequence of the string is the string that is obtained by deleting characters at some positions. You need to print the string that will be obtained from the given string and will be contain english alphabet as a subsequence or say that it is impossible.
|
The only one line of the input consisting of the string s consisting of |s| (1 β€ |s| β€ 105) small english letters.
|
If you can get a string that can be obtained from the given string and will contain english alphabet as a subsequence, print it. Otherwise print Β«-1Β» (without quotes).
|
Input: aacceeggiikkmmooqqssuuwwyy | Output: abcdefghijklmnopqrstuvwxyz
|
Easy
| 2 | 679 | 114 | 167 | 9 |
|
978 |
A
|
978A
|
A. Remove Duplicates
| 800 |
implementation
|
Petya has an array \(a\) consisting of \(n\) integers. He wants to remove duplicate (equal) elements.Petya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed.
|
The first line contains a single integer \(n\) (\(1 \le n \le 50\)) β the number of elements in Petya's array.The following line contains a sequence \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 1\,000\)) β the Petya's array.
|
In the first line print integer \(x\) β the number of elements which will be left in Petya's array after he removed the duplicates.In the second line print \(x\) integers separated with a space β Petya's array after he removed the duplicates. For each unique element only the rightmost entry should be left.
|
In the first example you should remove two integers \(1\), which are in the positions \(1\) and \(4\). Also you should remove the integer \(5\), which is in the position \(2\).In the second example you should remove integer \(2\), which is in the position \(1\), and two integers \(4\), which are in the positions \(2\) and \(4\).In the third example you should remove four integers \(6\), which are in the positions \(1\), \(2\), \(3\) and \(4\).
|
Input: 61 5 5 1 6 1 | Output: 35 6 1
|
Beginner
| 1 | 265 | 221 | 307 | 9 |
1,987 |
E
|
1987E
|
E. Wonderful Tree!
| 2,000 |
brute force; data structures; dfs and similar; dsu; greedy; trees
|
God's Blessing on This ArrayForces!A Random PebbleYou are given a tree with \(n\) vertices, rooted at vertex \(1\). The \(i\)-th vertex has an integer \(a_i\) written on it.Let \(L\) be the set of all direct children\(^{\text{β}}\) of \(v\). A tree is called wonderful, if for all vertices \(v\) where \(L\) is not empty, $$$\(a_v \le \sum_{u \in L}{a_u}.\)\( In one operation, you choose any vertex \)v\( and increase \)a_v\( by \)1\(.Find the minimum number of operations needed to make the given tree wonderful!\)^{\text{β}}\( Vertex \)u\( is called a direct child of vertex \)v\( if: \)u\( and \)v\( are connected by an edge, and \)v\( is on the (unique) path from \)u$$$ to the root of the tree.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 5000\)) β the number of vertices in the tree.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β the values initially written on the vertices.The third line of each test case contains \(n - 1\) integers \(p_2, p_3 , \ldots, p_n\) (\(1 \le p_i < i\)), indicating that there is an edge from vertex \(p_i\) to vertex \(i\). 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 \(5000\).
|
For each test case, output a single integer β the minimum number of operations needed to make the tree wonderful.
|
The tree in the first test case: You can apply the operation once on vertex \(5\) and twice on vertex \(2\) to get a wonderful tree.In the second test case, you can apply the operation twice on vertex \(2\) to get a wonderful tree.In the third and fourth test cases, the tree is already wonderful, so you don't need to apply any operations.
|
Input: 459 3 4 1 21 1 3 325 31236 54130 0 01 2 | Output: 3 2 0 0
|
Hard
| 6 | 700 | 783 | 113 | 19 |
1,197 |
E
|
1197E
|
E. Culture Code
| 2,300 |
binary search; combinatorics; data structures; dp; shortest paths; sortings
|
There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has \(n\) different matryoshkas. Any matryoshka is a figure of volume \(out_i\) with an empty space inside of volume \(in_i\) (of course, \(out_i > in_i\)).You don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll β inside the third one and so on. Matryoshka \(i\) can be nested inside matryoshka \(j\) if \(out_i \le in_j\). So only the last doll will take space inside your bag.Let's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to \(in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + \dots + (in_{i_k} - out_{i_{k-1}})\), where \(i_1\), \(i_2\), ..., \(i_k\) are the indices of the chosen dolls in the order they are nested in each other.Finally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property.You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index \(i\) such that one of the subsets contains the \(i\)-th doll, and another subset doesn't.Since the answer can be large, print it modulo \(10^9 + 7\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of matryoshkas.The next \(n\) lines contain a description of each doll: two integers \(out_i\) and \(in_i\) (\(1 \le in_i < out_i \le 10^9\)) β the outer and inners volumes of the \(i\)-th matryoshka.
|
Print one integer β the number of big enough nested subsets such that extra space of each of these subsets is minimum possible. Since the answer can be large, print it modulo \(10^9 + 7\).
|
There are \(6\) big enough nested subsets with minimum possible extra space in the example: \(\{1, 5\}\): we can't add any other matryoshka and keep it nested; it's extra space is \(1\); \(\{1, 6\}\); \(\{2, 4, 5\}\); \(\{2, 4, 6\}\); \(\{3, 4, 5\}\); \(\{3, 4, 6\}\). There are no more ""good"" subsets because, for example, subset \(\{6, 7\}\) is not big enough (we can add the \(4\)-th matryoshka to it) or subset \(\{4, 6, 7\}\) has extra space equal to \(2\).
|
Input: 7 4 1 4 2 4 2 2 1 5 4 6 4 3 2 | Output: 6
|
Expert
| 6 | 2,008 | 291 | 188 | 11 |
2,004 |
C
|
2004C
|
C. Splitting Items
| 1,100 |
games; greedy; sortings
|
Alice and Bob have \(n\) items they'd like to split between them, so they decided to play a game. All items have a cost, and the \(i\)-th item costs \(a_i\). Players move in turns starting from Alice.In each turn, the player chooses one of the remaining items and takes it. The game goes on until no items are left.Let's say that \(A\) is the total cost of items taken by Alice and \(B\) is the total cost of Bob's items. The resulting score of the game then will be equal to \(A - B\).Alice wants to maximize the score, while Bob wants to minimize it. Both Alice and Bob will play optimally.But the game will take place tomorrow, so today Bob can modify the costs a little. He can increase the costs \(a_i\) of several (possibly none or all) items by an integer value (possibly, by the same value or by different values for each item). However, the total increase must be less than or equal to \(k\). Otherwise, Alice may suspect something. Note that Bob can't decrease costs, only increase.What is the minimum possible score Bob can achieve?
|
The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases. Then \(t\) cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\); \(0 \le k \le 10^9\)) β the number of items and the maximum total increase Bob can make.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the initial costs of the items.It's guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the minimum possible score \(A - B\) after Bob increases the costs of several (possibly none or all) items.
|
In the first test case, Bob can increase \(a_1\) by \(5\), making costs equal to \([6, 10]\). Tomorrow, Alice will take \(10\) and Bob will take \(6\). The total score will be equal to \(10 - 6 = 4\), and it's the minimum possible.In the second test case, Bob can't change costs. So the score will be equal to \((15 + 10) - 12 = 13\), since Alice will take \(15\), Bob will take \(12\), and Alice β \(10\).In the third test case, Bob, for example, can increase \(a_1\) by \(1\), \(a_2\) by \(3\), and \(a_3\) by \(2\). The total change is equal to \(1 + 3 + 2 \le 6\) and costs will be equal to \([4, 4, 4, 4]\). Obviously, the score will be equal to \((4 + 4) - (4 + 4) = 0\).In the fourth test case, Bob can increase \(a_1\) by \(3\), making costs equal to \([9, 9]\). The score will be equal to \(9 - 9 = 0\).
|
Input: 42 51 103 010 15 124 63 1 2 42 46 9 | Output: 4 13 0 0
|
Easy
| 3 | 1,043 | 543 | 152 | 20 |
248 |
B
|
248B
|
B. Chilly Willy
| 1,400 |
math; number theory
|
Chilly Willy loves playing with numbers. He only knows prime numbers that are digits yet. These numbers are 2, 3, 5 and 7. But Willy grew rather bored of such numbers, so he came up with a few games that were connected with them.Chilly Willy wants to find the minimum number of length n, such that it is simultaneously divisible by all numbers Willy already knows (2, 3, 5 and 7). Help him with that.A number's length is the number of digits in its decimal representation without leading zeros.
|
A single input line contains a single integer n (1 β€ n β€ 105).
|
Print a single integer β the answer to the problem without leading zeroes, or ""-1"" (without the quotes), if the number that meet the problem condition does not exist.
|
Input: 1 | Output: -1
|
Easy
| 2 | 494 | 62 | 168 | 2 |
|
1,895 |
G
|
1895G
|
G. Two Characters, Two Colors
| 3,100 |
binary search; data structures; dp; flows; greedy
|
You are given a string consisting of characters 0 and/or 1. You have to paint every character of this string into one of two colors, red or blue.If you paint the \(i\)-th character red, you get \(r_i\) coins. If you paint it blue, you get \(b_i\) coins.After coloring the string, you remove every blue character from it, and count the number of inversions in the resulting string (i. e. the number of pairs of characters such that the left character in the pair is 1, and the right character in the pair is 0). For each inversion, you have to pay \(1\) coin.What is the maximum number of coins you can earn?
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of four lines: the first line contains one integer \(n\) (\(1 \le n \le 4 \cdot 10^5\)) β the length of the string; the second line contains \(s\) β a string of \(n\) characters, where each character is either 0 or 1; the third line contains \(n\) integers \(r_1, r_2, \dots, r_n\) (\(1 \le r_i \le 10^{12}\)); the fourth line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 10^{12}\)). Additional constraint on the input: the sum of values of \(n\) over all test cases does not exceed \(4 \cdot 10^5\).
|
For each test case, print one integer β the maximum number of coins you can earn.
|
Explanations for the test cases for the example (blue characters are underlined, red ones are not): \(0100\underline{0}1\underline{0}\); \(10\underline{11}1\); \(\underline{0}1\underline{00000000}\); \(0\underline{1}010000\).
|
Input: 4701000106 6 6 7 7 6 63 3 5 4 7 6 75101119 8 5 7 54 4 7 8 41001000000007 7 6 5 2 2 5 3 8 38 6 9 6 6 8 9 7 7 98010100008 7 7 7 8 7 7 84 4 4 2 1 4 4 4 | Output: 43 36 76 52
|
Master
| 5 | 607 | 650 | 81 | 18 |
1,017 |
H
|
1017H
|
H. The Films
| 3,300 |
brute force
|
In ""The Man in the High Castle"" world, there are \(m\) different film endings. Abendsen owns a storage and a shelf. At first, he has \(n\) ordered films on the shelf. In the \(i\)-th month he will do: Empty the storage. Put \(k_i \cdot m\) films into the storage, \(k_i\) films for each ending. He will think about a question: if he puts all the films from the shelf into the storage, then randomly picks \(n\) films (from all the films in the storage) and rearranges them on the shelf, what is the probability that sequence of endings in \([l_i, r_i]\) on the shelf will not be changed? Notice, he just thinks about this question, so the shelf will not actually be changed.Answer all Abendsen's questions.Let the probability be fraction \(P_i\). Let's say that the total number of ways to take \(n\) films from the storage for \(i\)-th month is \(A_i\), so \(P_i \cdot A_i\) is always an integer. Print for each month \(P_i \cdot A_i \pmod {998244353}\).\(998244353\) is a prime number and it is equal to \(119 \cdot 2^{23} + 1\).It is guaranteed that there will be only no more than \(100\) different \(k\) values.
|
The first line contains three integers \(n\), \(m\), and \(q\) (\(1 \le n, m, q \le 10^5\), \(n+q\leq 10^5\)) β the number of films on the shelf initially, the number of endings, and the number of months.The second line contains \(n\) integers \(e_1, e_2, \ldots, e_n\) (\(1\leq e_i\leq m\)) β the ending of the \(i\)-th film on the shelf.Each of the next \(q\) lines contains three integers \(l_i\), \(r_i\), and \(k_i\) (\(1 \le l_i \le r_i \le n, 0 \le k_i \le 10^5\)) β the \(i\)-th query.It is guaranteed that there will be only no more than \(100\) different \(k\) values.
|
Print the answer for each question in a separate line.
|
In the first sample in the second query, after adding \(2 \cdot m\) films into the storage, the storage will look like this: \(\{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4\}\).There are \(26730\) total ways of choosing the films so that \(e_l, e_{l+1}, \ldots, e_r\) will not be changed, for example, \([1, 2, 3, 2, 2]\) and \([1, 2, 3, 4, 3]\) are such ways.There are \(2162160\) total ways of choosing the films, so you're asked to print \((\frac{26730}{2162160} \cdot 2162160) \mod 998244353 = 26730\).
|
Input: 6 4 41 2 3 4 4 41 4 01 3 21 4 21 5 2 | Output: 626730121504860
|
Master
| 1 | 1,118 | 578 | 54 | 10 |
807 |
B
|
807B
|
B. T-Shirt Hunt
| 1,300 |
brute force; implementation
|
Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participants out of those who got into top 500, will receive a Codeforces T-shirt.Unfortunately, you didn't manage to get into top 25, but you got into top 500, taking place p.Now the elimination round of 8VC Venture Cup 2017 is being held. It has been announced that the Codecraft-17 T-shirt winners will be chosen as follows. Let s be the number of points of the winner of the elimination round of 8VC Venture Cup 2017. Then the following pseudocode will be executed: i := (s div 50) mod 475repeat 25 times: i := (i * 96 + 42) mod 475 print (26 + i)Here ""div"" is the integer division operator, ""mod"" is the modulo (the remainder of division) operator.As the result of pseudocode execution, 25 integers between 26 and 500, inclusive, will be printed. These will be the numbers of places of the participants who get the Codecraft-17 T-shirts. It is guaranteed that the 25 printed integers will be pairwise distinct for any value of s.You're in the lead of the elimination round of 8VC Venture Cup 2017, having x points. You believe that having at least y points in the current round will be enough for victory.To change your final score, you can make any number of successful and unsuccessful hacks. A successful hack brings you 100 points, an unsuccessful one takes 50 points from you. It's difficult to do successful hacks, though.You want to win the current round and, at the same time, ensure getting a Codecraft-17 T-shirt. What is the smallest number of successful hacks you have to do to achieve that?
|
The only line contains three integers p, x and y (26 β€ p β€ 500; 1 β€ y β€ x β€ 20000) β your place in Codecraft-17, your current score in the elimination round of 8VC Venture Cup 2017, and the smallest number of points you consider sufficient for winning the current round.
|
Output a single integer β the smallest number of successful hacks you have to do in order to both win the elimination round of 8VC Venture Cup 2017 and ensure getting a Codecraft-17 T-shirt.It's guaranteed that your goal is achievable for any valid input data.
|
In the first example, there is no need to do any hacks since 10880 points already bring the T-shirt to the 239-th place of Codecraft-17 (that is, you). In this case, according to the pseudocode, the T-shirts will be given to the participants at the following places: 475 422 84 411 453 210 157 294 146 188 420 367 29 356 398 155 102 239 91 133 365 312 449 301 343In the second example, you have to do two successful and one unsuccessful hack to make your score equal to 7408.In the third example, you need to do as many as 24 successful hacks to make your score equal to 10400.In the fourth example, it's sufficient to do 6 unsuccessful hacks (and no successful ones) to make your score equal to 6500, which is just enough for winning the current round and also getting the T-shirt.
|
Input: 239 10880 9889 | Output: 0
|
Easy
| 2 | 1,629 | 270 | 260 | 8 |
1,617 |
D1
|
1617D1
|
D1. Too Many Impostors (easy version)
| 1,800 |
constructive algorithms; implementation; interactive
|
This is an interactive problem. The only difference between the easy and hard version is the limit on number of questions.There are \(n\) players labelled from \(1\) to \(n\). It is guaranteed that \(n\) is a multiple of \(3\).Among them, there are \(k\) impostors and \(n-k\) crewmates. The number of impostors, \(k\), is not given to you. It is guaranteed that \(\frac{n}{3} < k < \frac{2n}{3}\).In each question, you can choose three distinct integers \(a\), \(b\), \(c\) (\(1 \le a, b, c \le n\)) and ask: ""Among the players labelled \(a\), \(b\) and \(c\), are there more impostors or more crewmates?"" You will be given the integer \(0\) if there are more impostors than crewmates, and \(1\) otherwise.Find the number of impostors \(k\) and the indices of players that are impostors after asking at most \(2n\) questions.The jury is adaptive, which means the indices of impostors may not be fixed beforehand and can depend on your questions. It is guaranteed that there is at least one set of impostors which fulfills the constraints and the answers to your questions at any time.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first and only line of each test case contains a single integer \(n\) (\(6 \le n < 10^4\), \(n\) is a multiple of \(3\)) β the number of players.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^4\).
|
Explanation for example interaction (note that this example only exists to demonstrate the interaction procedure and does not provide any hint for the solution):For the first test case:Question ""? 1 2 3"" returns \(0\), so there are more impostors than crewmates among players \(1\), \(2\) and \(3\).Question ""? 3 4 5"" returns \(1\), so there are more crewmates than impostors among players \(3\), \(4\) and \(5\).Outputting ""! 3 4 1 2"" means that one has found all the impostors, by some miracle. There are \(k = 3\) impostors. The players who are impostors are players \(4\), \(1\) and \(2\).For the second test case:Question ""? 7 1 9"" returns \(1\), so there are more crewmates than impostors among players \(7\), \(1\) and \(9\).Outputting ""! 4 2 3 6 8"" means that one has found all the impostors, by some miracle. There are \(k = 4\) impostors. The players who are impostors are players \(2\), \(3\), \(6\) and \(8\).
|
Input: 2 6 0 1 9 1 | Output: ? 1 2 3 ? 3 4 5 ! 3 4 1 2 ? 7 1 9 ! 4 2 3 6 8
|
Medium
| 3 | 1,087 | 397 | 0 | 16 |
|
753 |
A
|
753A
|
A. Santa Claus and Candies
| 1,000 |
dp; greedy; math
|
Santa Claus has n candies, he dreams to give them as gifts to children.What is the maximal number of children for whose he can give candies if Santa Claus want each kid should get distinct positive integer number of candies. Santa Class wants to give all n candies he has.
|
The only line contains positive integer number n (1 β€ n β€ 1000) β number of candies Santa Claus has.
|
Print to the first line integer number k β maximal number of kids which can get candies.Print to the second line k distinct integer numbers: number of candies for each of k kid. The sum of k printed numbers should be exactly n.If there are many solutions, print any of them.
|
Input: 5 | Output: 22 3
|
Beginner
| 3 | 272 | 100 | 274 | 7 |
|
351 |
C
|
351C
|
C. Jeff and Brackets
| 2,500 |
dp; matrices
|
Jeff loves regular bracket sequences.Today Jeff is going to take a piece of paper and write out the regular bracket sequence, consisting of nm brackets. Let's number all brackets of this sequence from 0 to nm - 1 from left to right. Jeff knows that he is going to spend ai mod n liters of ink on the i-th bracket of the sequence if he paints it opened and bi mod n liters if he paints it closed.You've got sequences a, b and numbers n, m. What minimum amount of ink will Jeff need to paint a regular bracket sequence of length nm?Operation x mod y means taking the remainder after dividing number x by number y.
|
The first line contains two integers n and m (1 β€ n β€ 20; 1 β€ m β€ 107; m is even). The next line contains n integers: a0, a1, ..., an - 1 (1 β€ ai β€ 10). The next line contains n integers: b0, b1, ..., bn - 1 (1 β€ bi β€ 10). The numbers are separated by spaces.
|
In a single line print the answer to the problem β the minimum required amount of ink in liters.
|
In the first test the optimal sequence is: ()()()()()(), the required number of ink liters is 12.
|
Input: 2 61 22 1 | Output: 12
|
Expert
| 2 | 611 | 259 | 96 | 3 |
138 |
E
|
138E
|
E. Hellish Constraints
| 2,900 |
brute force; dp; two pointers
|
Katya recently started to invent programming tasks and prepare her own contests. What she does not like is boring and simple constraints. Katya is fed up with all those ""N does not exceed a thousand"" and ""the sum of ai does not exceed a million"" and she decided to come up with something a little more complicated.The last problem written by Katya deals with strings. The input is a string of small Latin letters. To make the statement longer and strike terror into the people who will solve the contest, Katya came up with the following set of k restrictions of the same type (characters in restrictions can be repeated and some restrictions may contradict each other): The number of characters c1 in a string is not less than l1 and not more than r1. ... The number of characters ci in a string is not less than li and not more than ri. ... The number of characters ck in a string is not less than lk and not more than rk. However, having decided that it is too simple and obvious, Katya added the following condition: a string meets no less than L and not more than R constraints from the above given list.Katya does not like to compose difficult and mean tests, so she just took a big string s and wants to add to the tests all its substrings that meet the constraints. However, Katya got lost in her conditions and asked you to count the number of substrings of the string s that meet the conditions (each occurrence of the substring is counted separately).
|
The first line contains a non-empty string s, consisting of small Latin letters. The length of the string s does not exceed 105.The second line contains three space-separated integers k, L and R (0 β€ L β€ R β€ k β€ 500).Next k lines contain Katya's constrictions in the following form ""ci li ri"". All letters ci are small Latin letters, li and ri are integers (0 β€ li β€ ri β€ |s|, where |s| is the length of string s). Letters ci are not necessarily different.
|
Print a single number β the number of substrings that meet the constrictions.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cout stream or the %I64d specificator.
|
In the first test we should count the number of strings that do not contain characters ""e"" and ""o"". All such strings are as follows (in the order of occurrence in the initial string from the left to the right): ""c"", ""d""', ""f"", ""r"", ""rc"", ""c"", ""s"".In the second test we cannot achieve fulfilling exactly one of the two identical constrictions, so the answer is 0.
|
Input: codeforces2 0 0o 1 2e 1 2 | Output: 7
|
Master
| 3 | 1,466 | 458 | 223 | 1 |
1,823 |
D
|
1823D
|
D. Unique Palindromes
| 1,900 |
constructive algorithms; math; strings
|
A palindrome is a string that reads the same backwards as forwards. For example, the string abcba is palindrome, while the string abca is not.Let \(p(t)\) be the number of unique palindromic substrings of string \(t\), i. e. the number of substrings \(t[l \dots r]\) that are palindromes themselves. Even if some substring occurs in \(t\) several times, it's counted exactly once. (The whole string \(t\) is also counted as a substring of \(t\)).For example, string \(t\) \(=\) abcbbcabcb has \(p(t) = 6\) unique palindromic substrings: a, b, c, bb, bcb and cbbc.Now, let's define \(p(s, m) = p(t)\) where \(t = s[1 \dots m]\). In other words, \(p(s, m)\) is the number of palindromic substrings in the prefix of \(s\) of length \(m\). For example, \(p(\)abcbbcabcb\(, 5)\) \(=\) \(p(\)abcbb\() = 5\).You are given an integer \(n\) and \(k\) ""conditions"" (\(k \le 20\)). Let's say that string \(s\), consisting of \(n\) lowercase Latin letters, is good if all \(k\) conditions are satisfied at the same time. A condition is a pair \((x_i, c_i)\) and have the following meaning: \(p(s, x_i) = c_i\), i. e. a prefix of \(s\) of length \(x_i\) contains exactly \(c_i\) unique palindromic substrings. Find a good string \(s\) or report that such \(s\) doesn't exist.Look in Notes if you need further clarifications.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(3 \le n \le 2 \cdot 10^5\); \(1 \le k \le 20\)) β length of good string \(s\) and number of conditions.The second line of each test case contains \(k\) integers \(x_1, x_2, \dots, x_k\) (\(3 \le x_1 < x_2 < \dots < x_k = n\)) where \(x_i\) is the length of the prefix in the \(i\)-th condition.The third line of each test case contains \(k\) integers \(c_1, c_2, \dots, c_k\) (\(3 \le c_1 \le c_2 \le \dots \le c_k \le \min{\left(10^9, \frac{(n + 1) n}{2} \right)}\)) where \(c_i\) is the number of palindromic substrings in the \(i\)-th condition.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10 ^ 5\).
|
For each test case, if there is no good string \(s\) of length \(n\) that satisfies all conditions, print NO.Otherwise, print YES and a string \(s\) of length \(n\), consisting of lowercase Latin letters, that satisfies all conditions. If there are multiple answers, print any of them.
|
In the first test case, string \(s\) \(=\) abcbbcabcb satisfies \(k = 2\) conditions: \(p(s, x_1) = p(s, 5) =\) \(p(\)abcbb\() = 5 = s_1\). Palindromic substrings are a, b, c, bb and bcb. \(p(s, x_2) = p(s, 10) =\) \(p(\)abcbbcabcb\() = 6 = s_2\). Palindromic substrings are the same as above, and one extra substring cbbc. In the second test case, string foo satisfies \(k = 1\) condition: \(p(\)foo\() = 3\). Palindromic substrings are f, o and oo. There are other possible answers.In the third test case, string ayda satisfies \(k = 2\) conditions: \(p(\)ayd\() = 3\). Palindromic substrings are a, y and d. \(p(\)ayda\() = 3\). Palindromic substrings are the same. In the fourth test case, string wada satisfies \(k = 2\) conditions: \(p(\)wad\() = 3\). Palindromic substrings are w, a and d. \(p(\)wada\() = 4\). Palindromic substrings are the same, and one extra substring ada. In the fifth test case, it can be proven that there is no string of length \(4\) which has \(5\) palindromic substrings.In the sixth test case, string abcbcacbab satisfies \(k = 3\) conditions: \(p(\)abcb\() = 4\). Palindromic substrings are a, b, c and bcb. \(p(\)abcbca\() = 5\). Palindromic substrings are the same, and one extra substring cbc. \(p(\)abcbcacbab\() = 8\). Palindromic substrings are the same, and three extra substrings cac, bab and bcacb.
|
Input: 710 25 105 63 1334 23 43 34 23 43 44 14510 34 6 104 5 810 44 6 7 104 5 7 8 | Output: YES abcbbcabcb YES foo YES ayda YES wada NO YES abcbcacbab NO
|
Hard
| 3 | 1,313 | 877 | 285 | 18 |
845 |
C
|
845C
|
C. Two TVs
| 1,500 |
data structures; greedy; sortings
|
Polycarp is a great fan of television.He wrote down all the TV programs he is interested in for today. His list contains n shows, i-th of them starts at moment li and ends at moment ri.Polycarp owns two TVs. He can watch two different shows simultaneously with two TVs but he can only watch one show at any given moment on a single TV. If one show ends at the same moment some other show starts then you can't watch them on a single TV.Polycarp wants to check out all n shows. Are two TVs enough to do so?
|
The first line contains one integer n (1 β€ n β€ 2Β·105) β the number of shows.Each of the next n lines contains two integers li and ri (0 β€ li < ri β€ 109) β starting and ending time of i-th show.
|
If Polycarp is able to check out all the shows using only two TVs then print ""YES"" (without quotes). Otherwise, print ""NO"" (without quotes).
|
Input: 31 22 34 5 | Output: YES
|
Medium
| 3 | 505 | 193 | 144 | 8 |
|
1,361 |
A
|
1361A
|
A. Johnny and Contribution
| 1,700 |
constructive algorithms; graphs; greedy; sortings
|
Today Johnny wants to increase his contribution. His plan assumes writing \(n\) blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network.There are \(n\) different topics, numbered from \(1\) to \(n\) sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most \(n - 1\) neighbors.For example, if already written neighbors of the current blog have topics number \(1\), \(3\), \(1\), \(5\), and \(2\), Johnny will choose the topic number \(4\) for the current blog, because topics number \(1\), \(2\) and \(3\) are already covered by neighbors and topic number \(4\) isn't covered.As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you?
|
The first line contains two integers \(n\) \((1 \leq n \leq 5 \cdot 10^5)\) and \(m\) \((0 \leq m \leq 5 \cdot 10^5)\) β the number of blogs and references, respectively.Each of the following \(m\) lines contains two integers \(a\) and \(b\) (\(a \neq b\); \(1 \leq a, b \leq n\)), which mean that there is a reference between blogs \(a\) and \(b\). It's guaranteed that the graph doesn't contain multiple edges.The last line contains \(n\) integers \(t_1, t_2, \ldots, t_n\), \(i\)-th of them denotes desired topic number of the \(i\)-th blog (\(1 \le t_i \le n\)).
|
If the solution does not exist, then write \(-1\). Otherwise, output \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) \((1 \leq p_i \leq n)\), which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any.
|
In the first example, Johnny starts with writing blog number \(2\), there are no already written neighbors yet, so it receives the first topic. Later he writes blog number \(1\), it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number \(3\), it has references to blogs number \(1\) and \(2\) so it receives the third topic.Second example: There does not exist any permutation fulfilling given conditions.Third example: First Johnny writes blog \(2\), it receives the topic \(1\). Then he writes blog \(5\), it receives the topic \(1\) too because it doesn't have reference to single already written blog \(2\). Then he writes blog number \(1\), it has reference to blog number \(2\) with topic \(1\), so it receives the topic \(2\). Then he writes blog number \(3\) which has reference to blog \(2\), so it receives the topic \(2\). Then he ends with writing blog number \(4\) which has reference to blog \(5\) and receives the topic \(2\).
|
Input: 3 3 1 2 2 3 3 1 2 1 3 | Output: 2 1 3
|
Medium
| 4 | 1,524 | 566 | 262 | 13 |
628 |
A
|
628A
|
A. Tennis Tournament
| 1,000 |
implementation; math
|
A tennis tournament with n participants is running. The participants are playing by an olympic system, so the winners move on and the losers drop out.The tournament takes place in the following way (below, m is the number of the participants of the current round): let k be the maximal power of the number 2 such that k β€ m, k participants compete in the current round and a half of them passes to the next round, the other m - k participants pass to the next round directly, when only one participant remains, the tournament finishes. Each match requires b bottles of water for each participant and one bottle for the judge. Besides p towels are given to each participant for the whole tournament.Find the number of bottles and towels needed for the tournament.Note that it's a tennis tournament so in each match two participants compete (one of them will win and the other will lose).
|
The only line contains three integers n, b, p (1 β€ n, b, p β€ 500) β the number of participants and the parameters described in the problem statement.
|
Print two integers x and y β the number of bottles and towels need for the tournament.
|
In the first example will be three rounds: in the first round will be two matches and for each match 5 bottles of water are needed (two for each of the participants and one for the judge), in the second round will be only one match, so we need another 5 bottles of water, in the third round will also be only one match, so we need another 5 bottles of water. So in total we need 20 bottles of water.In the second example no participant will move on to some round directly.
|
Input: 5 2 3 | Output: 20 15
|
Beginner
| 2 | 886 | 149 | 86 | 6 |
847 |
D
|
847D
|
D. Dog Show
| 2,200 |
constructive algorithms; data structures; greedy
|
A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win!On the show a dog needs to eat as many bowls of dog food as possible (bottomless stomach helps here). Dogs compete separately of each other and the rules are as follows:At the start of the show the dog and the bowls are located on a line. The dog starts at position x = 0 and n bowls are located at positions x = 1, x = 2, ..., x = n. The bowls are numbered from 1 to n from left to right. After the show starts the dog immediately begins to run to the right to the first bowl.The food inside bowls is not ready for eating at the start because it is too hot (dog's self-preservation instinct prevents eating). More formally, the dog can eat from the i-th bowl after ti seconds from the start of the show or later.It takes dog 1 second to move from the position x to the position x + 1. The dog is not allowed to move to the left, the dog runs only to the right with the constant speed 1 distance unit per second. When the dog reaches a bowl (say, the bowl i), the following cases are possible: the food had cooled down (i.e. it passed at least ti seconds from the show start): the dog immediately eats the food and runs to the right without any stop, the food is hot (i.e. it passed less than ti seconds from the show start): the dog has two options: to wait for the i-th bowl, eat the food and continue to run at the moment ti or to skip the i-th bowl and continue to run to the right without any stop. After T seconds from the start the show ends. If the dog reaches a bowl of food at moment T the dog can not eat it. The show stops before T seconds if the dog had run to the right of the last bowl.You need to help your dog create a strategy with which the maximum possible number of bowls of food will be eaten in T seconds.
|
Two integer numbers are given in the first line - n and T (1 β€ n β€ 200 000, 1 β€ T β€ 2Β·109) β the number of bowls of food and the time when the dog is stopped.On the next line numbers t1, t2, ..., tn (1 β€ ti β€ 109) are given, where ti is the moment of time when the i-th bowl of food is ready for eating.
|
Output a single integer β the maximum number of bowls of food the dog will be able to eat in T seconds.
|
In the first example the dog should skip the second bowl to eat from the two bowls (the first and the third).
|
Input: 3 51 5 3 | Output: 2
|
Hard
| 3 | 1,982 | 303 | 103 | 8 |
1,679 |
F
|
1679F
|
F. Formalism for Formalism
| 2,600 |
bitmasks; dp; math
|
Yura is a mathematician, and his cognition of the world is so absolute as if he have been solving formal problems a hundred of trillions of billions of years. This problem is just that!Consider all non-negative integers from the interval \([0, 10^{n})\). For convenience we complement all numbers with leading zeros in such way that each number from the given interval consists of exactly \(n\) decimal digits.You are given a set of pairs \((u_i, v_i)\), where \(u_i\) and \(v_i\) are distinct decimal digits from \(0\) to \(9\).Consider a number \(x\) consisting of \(n\) digits. We will enumerate all digits from left to right and denote them as \(d_1, d_2, \ldots, d_n\). In one operation you can swap digits \(d_i\) and \(d_{i + 1}\) if and only if there is a pair \((u_j, v_j)\) in the set such that at least one of the following conditions is satisfied: \(d_i = u_j\) and \(d_{i + 1} = v_j\), \(d_i = v_j\) and \(d_{i + 1} = u_j\). We will call the numbers \(x\) and \(y\), consisting of \(n\) digits, equivalent if the number \(x\) can be transformed into the number \(y\) using some number of operations described above. In particular, every number is considered equivalent to itself.You are given an integer \(n\) and a set of \(m\) pairs of digits \((u_i, v_i)\). You have to find the maximum integer \(k\) such that there exists a set of integers \(x_1, x_2, \ldots, x_k\) (\(0 \le x_i < 10^{n}\)) such that for each \(1 \le i < j \le k\) the number \(x_i\) is not equivalent to the number \(x_j\).
|
The first line contains an integer \(n\) (\(1 \le n \le 50\,000\)) β the number of digits in considered numbers.The second line contains an integer \(m\) (\(0 \le m \le 45\)) β the number of pairs of digits in the set.Each of the following \(m\) lines contains two digits \(u_i\) and \(v_i\), separated with a space (\(0 \le u_i < v_i \le 9\)).It's guaranteed that all described pairs are pairwise distinct.
|
Print one integer β the maximum value \(k\) such that there exists a set of integers \(x_1, x_2, \ldots, x_k\) (\(0 \le x_i < 10^{n}\)) such that for each \(1 \le i < j \le k\) the number \(x_i\) is not equivalent to the number \(x_j\).As the answer can be big enough, print the number \(k\) modulo \(998\,244\,353\).
|
In the first example we can construct a set that contains all integers from \(0\) to \(9\). It's easy to see that there are no two equivalent numbers in the set.In the second example there exists a unique pair of equivalent numbers: \(01\) and \(10\). We can construct a set that contains all integers from \(0\) to \(99\) despite number \(1\).
|
Input: 1 0 | Output: 10
|
Expert
| 3 | 1,509 | 407 | 317 | 16 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.