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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,461 |
F
|
1461F
|
F. Mathematical Expression
| 2,700 |
constructive algorithms; dp; greedy
|
Barbara was late for her math class so as a punishment the teacher made her solve the task on a sheet of paper. Barbara looked at the sheet of paper and only saw \(n\) numbers \(a_1, a_2, \ldots, a_n\) without any mathematical symbols. The teacher explained to Barbara that she has to place the available symbols between the numbers in a way that would make the resulting expression's value as large as possible. To find out which symbols were available the teacher has given Barbara a string \(s\) which contained that information. It's easy to notice that Barbara has to place \(n - 1\) symbols between numbers in total. The expression must start with a number and all symbols must be allowed (i.e. included in \(s\)). Note that multiplication takes precedence over addition or subtraction, addition and subtraction have the same priority and performed from left to right. Help Barbara and create the required expression!
|
The first line of the input contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the amount of numbers on the paper.The second line of the input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 9\)), where \(a_i\) is the \(i\)-th element of \(a\).The third line of the input contains the string \(s\) (\(1 \le |s| \le 3\)) β symbols allowed in the expression. It is guaranteed that the string may only consist of symbols ""-"", ""+"" and ""*"". It is also guaranteed that all symbols in the string are distinct.
|
Print \(n\) numbers separated by \(n - 1\) symbols β a mathematical expression with the greatest result. If there are multiple equally valid results β output any one of them.
|
The following answers also fit the first example: ""2+2+0"", ""2+2-0"", ""2*2+0"".
|
Input: 3 2 2 0 +-* | Output: 2*2-0
|
Master
| 3 | 923 | 533 | 174 | 14 |
1,428 |
B
|
1428B
|
B. Belted Rooms
| 1,200 |
graphs; implementation
|
In the snake exhibition, there are \(n\) rooms (numbered \(0\) to \(n - 1\)) arranged in a circle, with a snake in each room. The rooms are connected by \(n\) conveyor belts, and the \(i\)-th conveyor belt connects the rooms \(i\) and \((i+1) \bmod n\). In the other words, rooms \(0\) and \(1\), \(1\) and \(2\), \(\ldots\), \(n-2\) and \(n-1\), \(n-1\) and \(0\) are connected with conveyor belts.The \(i\)-th conveyor belt is in one of three states: If it is clockwise, snakes can only go from room \(i\) to \((i+1) \bmod n\). If it is anticlockwise, snakes can only go from room \((i+1) \bmod n\) to \(i\). If it is off, snakes can travel in either direction. Above is an example with \(4\) rooms, where belts \(0\) and \(3\) are off, \(1\) is clockwise, and \(2\) is anticlockwise.Each snake wants to leave its room and come back to it later. A room is returnable if the snake there can leave the room, and later come back to it using the conveyor belts. How many such returnable rooms are there?
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)): the number of test cases. The description of the test cases follows. The first line of each test case description contains a single integer \(n\) (\(2 \le n \le 300\,000\)): the number of rooms. The next line of each test case description contains a string \(s\) of length \(n\), consisting of only '<', '>' and '-'. If \(s_{i} = \) '>', the \(i\)-th conveyor belt goes clockwise. If \(s_{i} = \) '<', the \(i\)-th conveyor belt goes anticlockwise. If \(s_{i} = \) '-', the \(i\)-th conveyor belt is off. It is guaranteed that the sum of \(n\) among all test cases does not exceed \(300\,000\).
|
For each test case, output the number of returnable rooms.
|
In the first test case, all rooms are returnable except room \(2\). The snake in the room \(2\) is trapped and cannot exit. This test case corresponds to the picture from the problem statement. In the second test case, all rooms are returnable by traveling on the series of clockwise belts.
|
Input: 4 4 -><- 5 >>>>> 3 <-- 2 <> | Output: 3 5 3 0
|
Easy
| 2 | 1,001 | 705 | 58 | 14 |
216 |
D
|
216D
|
D. Spider's Web
| 1,700 |
binary search; sortings; two pointers
|
Paw the Spider is making a web. Web-making is a real art, Paw has been learning to do it his whole life. Let's consider the structure of the web. There are n main threads going from the center of the web. All main threads are located in one plane and divide it into n equal infinite sectors. The sectors are indexed from 1 to n in the clockwise direction. Sectors i and i + 1 are adjacent for every i, 1 β€ i < n. In addition, sectors 1 and n are also adjacent.Some sectors have bridge threads. Each bridge connects the two main threads that make up this sector. The points at which the bridge is attached to the main threads will be called attachment points. Both attachment points of a bridge are at the same distance from the center of the web. At each attachment point exactly one bridge is attached. The bridges are adjacent if they are in the same sector, and there are no other bridges between them.A cell of the web is a trapezoid, which is located in one of the sectors and is bounded by two main threads and two adjacent bridges. You can see that the sides of the cell may have the attachment points of bridges from adjacent sectors. If the number of attachment points on one side of the cell is not equal to the number of attachment points on the other side, it creates an imbalance of pulling forces on this cell and this may eventually destroy the entire web. We'll call such a cell unstable. The perfect web does not contain unstable cells.Unstable cells are marked red in the figure. Stable cells are marked green.Paw the Spider isn't a skillful webmaker yet, he is only learning to make perfect webs. Help Paw to determine the number of unstable cells in the web he has just spun.
|
The first line contains integer n (3 β€ n β€ 1000) β the number of main threads.The i-th of following n lines describe the bridges located in the i-th sector: first it contains integer ki (1 β€ ki β€ 105) equal to the number of bridges in the given sector. Then follow ki different integers pij (1 β€ pij β€ 105; 1 β€ j β€ ki). Number pij equals the distance from the attachment points of the j-th bridge of the i-th sector to the center of the web.It is guaranteed that any two bridges between adjacent sectors are attached at a different distance from the center of the web. It is guaranteed that the total number of the bridges doesn't exceed 105.
|
Print a single integer β the number of unstable cells in Paw the Spider's web.
|
Input: 73 1 6 74 3 5 2 92 8 14 3 7 6 43 2 5 93 6 3 83 4 2 9 | Output: 6
|
Medium
| 3 | 1,695 | 642 | 78 | 2 |
|
1,494 |
A
|
1494A
|
A. ABC String
| 900 |
bitmasks; brute force; implementation
|
You are given a string \(a\), consisting of \(n\) characters, \(n\) is even. For each \(i\) from \(1\) to \(n\) \(a_i\) is one of 'A', 'B' or 'C'.A bracket sequence is a string containing only characters ""("" and "")"". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example, bracket sequences ""()()"" and ""(())"" are regular (the resulting expressions are: ""(1)+(1)"" and ""((1+1)+1)""), and "")("", ""("" and "")"" are not.You want to find a string \(b\) that consists of \(n\) characters such that: \(b\) is a regular bracket sequence; if for some \(i\) and \(j\) (\(1 \le i, j \le n\)) \(a_i=a_j\), then \(b_i=b_j\). In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.Your task is to determine if such a string \(b\) exists.
|
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 only line of each testcase contains a string \(a\). \(a\) consists only of uppercase letters 'A', 'B' and 'C'. Let \(n\) be the length of \(a\). It is guaranteed that \(n\) is even and \(2 \le n \le 50\).
|
For each testcase print ""YES"" if there exists such a string \(b\) that: \(b\) is a regular bracket sequence; if for some \(i\) and \(j\) (\(1 \le i, j \le n\)) \(a_i=a_j\), then \(b_i=b_j\). Otherwise, print ""NO"".You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
|
In the first testcase one of the possible strings \(b\) is ""(())()"".In the second testcase one of the possible strings \(b\) is ""()()"".
|
Input: 4 AABBAC CACA BBBBAC ABCA | Output: YES YES NO NO
|
Beginner
| 3 | 1,042 | 352 | 356 | 14 |
78 |
D
|
78D
|
D. Archer's Shot
| 2,300 |
binary search; geometry; math; two pointers
|
A breakthrough among computer games, ""Civilization XIII"", is striking in its scale and elaborate details. Let's take a closer look at one of them.The playing area in the game is split into congruent cells that are regular hexagons. The side of each cell is equal to 1. Each unit occupies exactly one cell of the playing field. The field can be considered infinite. Let's take a look at the battle unit called an ""Archer"". Each archer has a parameter ""shot range"". It's a positive integer that determines the radius of the circle in which the archer can hit a target. The center of the circle coincides with the center of the cell in which the archer stays. A cell is considered to be under the archerβs fire if and only if all points of this cell, including border points are located inside the circle or on its border.The picture below shows the borders for shot ranges equal to 3, 4 and 5. The archer is depicted as A. Find the number of cells that are under fire for some archer.
|
The first and only line of input contains a single positive integer k β the archer's shot range (1 β€ k β€ 106).
|
Print the single number, the number of cells that are under fire.Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
|
Input: 3 | Output: 7
|
Expert
| 4 | 988 | 110 | 227 | 0 |
|
1,708 |
B
|
1708B
|
B. Difference of GCDs
| 1,100 |
constructive algorithms; math
|
You are given three integers \(n\), \(l\), and \(r\). You need to construct an array \(a_1,a_2,\dots,a_n\) (\(l\le a_i\le r\)) such that \(\gcd(i,a_i)\) are all distinct or report there's no solution.Here \(\gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases. The description of the test cases follows.The first line contains three integers \(n\), \(l\), \(r\) (\(1 \le n \le 10^5\), \(1\le l\le r\le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, if there is no solution, print ""NO"" (without quotes). You can print letters in any case (upper or lower).Otherwise, print ""YES"" (without quotes). In the next line, print \(n\) integers \(a_1,a_2,\ldots,a_n\) β the array you construct.If there are multiple solutions, you may output any.
|
In the first test case, \(\gcd(1,a_1),\gcd(2,a_2),\ldots,\gcd(5,a_5)\) are equal to \(1\), \(2\), \(3\), \(4\), \(5\), respectively.
|
Input: 4 5 1 5 9 1000 2000 10 30 35 1 1000000000 1000000000 | Output: YES 1 2 3 4 5 YES 1145 1926 1440 1220 1230 1350 1001 1000 1233 NO YES 1000000000
|
Easy
| 2 | 290 | 372 | 310 | 17 |
1,896 |
A
|
1896A
|
A. Jagged Swaps
| 800 |
sortings
|
You are given a permutation\(^\dagger\) \(a\) of size \(n\). You can do the following operation Select an index \(i\) from \(2\) to \(n - 1\) such that \(a_{i - 1} < a_i\) and \(a_i > a_{i+1}\). Swap \(a_i\) and \(a_{i+1}\). Determine whether it is possible to sort the permutation after a finite number of operations.\(^\dagger\) A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 10\)) β the size of the permutation.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the elements of permutation \(a\).
|
For each test case, print ""YES"" if it is possible to sort the permutation, and ""NO"" otherwise.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer).
|
In the first test case, the permutation is already sorted.In the second test case, we can choose index \(i=2\) as \(1<3\) and \(3>2\) to form \([1, 2, 3, 5, 4]\). Then, we can choose index \(i=4\) as \(3<5\) and \(5>4\) to form \([1, 2, 3, 4, 5]\).In the third test case, it can be proven that it is impossible to sort the permutation.
|
Input: 631 2 351 3 2 5 455 4 3 2 133 1 242 3 1 455 1 2 3 4 | Output: YES YES NO NO NO NO
|
Beginner
| 1 | 638 | 415 | 228 | 18 |
1,922 |
E
|
1922E
|
E. Increasing Subsequences
| 1,800 |
bitmasks; constructive algorithms; divide and conquer; greedy; math
|
Let's recall that an increasing subsequence of the array \(a\) is a sequence that can be obtained from it by removing some elements without changing the order of the remaining elements, and the remaining elements are strictly increasing (i. e \(a_{b_1} < a_{b_2} < \dots < a_{b_k}\) and \(b_1 < b_2 < \dots < b_k\)). Note that an empty subsequence is also increasing.You are given a positive integer \(X\). Your task is to find an array of integers of length at most \(200\), such that it has exactly \(X\) increasing subsequences, or report that there is no such array. If there are several answers, you can print any of them.If two subsequences consist of the same elements, but correspond to different positions in the array, they are considered different (for example, the array \([2, 2]\) has two different subsequences equal to \([2]\)).
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains a single integer \(X\) (\(2 \le X \le 10^{18}\)).
|
For each query, print the answer to it. If it is impossible to find the required array, print -1 on the first line. Otherwise, print a positive integer \(n\) on the first line β the length of the array. On the second line, print \(n\) integers β the required array itself. If there are several answers, you can print any of them. All elements of the array should be in the range \([-10^9; 10^9]\).
|
Input: 4251337 | Output: 1 0 3 0 1 0 5 2 2 3 4 2 7 -1 -1 0 0 2 3 -1
|
Medium
| 5 | 843 | 187 | 397 | 19 |
|
142 |
D
|
142D
|
D. Help Shrek and Donkey 2
| 2,600 |
games
|
Having learned (not without some help from the Codeforces participants) to play the card game from the previous round optimally, Shrek and Donkey (as you may remember, they too live now in the Kingdom of Far Far Away) have decided to quit the boring card games and play with toy soldiers.The rules of the game are as follows: there is a battlefield, its size equals n Γ m squares, some squares contain the toy soldiers (the green ones belong to Shrek and the red ones belong to Donkey). Besides, each of the n lines of the area contains not more than two soldiers. During a move a players should select not less than 1 and not more than k soldiers belonging to him and make them either attack or retreat.An attack is moving all of the selected soldiers along the lines on which they stand in the direction of an enemy soldier, if he is in this line. If this line doesn't have an enemy soldier, then the selected soldier on this line can move in any direction during the player's move. Each selected soldier has to move at least by one cell. Different soldiers can move by a different number of cells. During the attack the soldiers are not allowed to cross the cells where other soldiers stand (or stood immediately before the attack). It is also not allowed to go beyond the battlefield or finish the attack in the cells, where other soldiers stand (or stood immediately before attack).A retreat is moving all of the selected soldiers along the lines on which they stand in the direction from an enemy soldier, if he is in this line. The other rules repeat the rules of the attack.For example, let's suppose that the original battlefield had the form (here symbols ""G"" mark Shrek's green soldiers and symbols ""R"" mark Donkey's red ones): -G-R--R-G- Let's suppose that k = 2 and Shrek moves first. If he decides to attack, then after his move the battlefield can look like that: --GR- --GR- -G-R--RG-- -R-G- -RG-- If in the previous example Shrek decides to retreat, then after his move the battlefield can look like that: G--R- G--R- -G-R--R--G -R-G- -R--G On the other hand, the followings fields cannot result from Shrek's correct move: G--R- ---RG --GR--RG-- -R-G- GR--- Shrek starts the game. To make a move means to attack or to retreat by the rules. A player who cannot make a move loses and his opponent is the winner. Determine the winner of the given toy soldier game if Shrek and Donkey continue to be under the yellow pills from the last rounds' problem. Thus, they always play optimally (that is, they try to win if it is possible, or finish the game in a draw, by ensuring that it lasts forever, if they cannot win).
|
The first line contains space-separated integers n, m and k (1 β€ n, m, k β€ 100). Then n lines contain m characters each. These characters belong to the set {""-"", ""G"", ""R""}, denoting, respectively, a battlefield's free cell, a cell occupied by Shrek's soldiers and a cell occupied by Donkey's soldiers.It is guaranteed that each line contains no more than two soldiers.
|
Print ""First"" (without the quotes) if Shrek wins in the given Toy Soldier game. If Donkey wins, print ""Second"" (without the quotes). If the game continues forever, print ""Draw"" (also without the quotes).
|
Input: 2 3 1R-GRG- | Output: First
|
Expert
| 1 | 2,634 | 374 | 209 | 1 |
|
500 |
C
|
500C
|
C. New Year Book Reading
| 1,600 |
constructive algorithms; greedy; implementation; math
|
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 β€ i β€ n) book is wi.As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below. He lifts all the books above book x. He pushes book x out of the stack. He puts down the lifted books without changing their order. After reading book x, he puts book x on the top of the stack. He decided to read books for m days. In the j-th (1 β€ j β€ m) day, he will read the book that is numbered with integer bj (1 β€ bj β€ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?
|
The first line contains two space-separated integers n (2 β€ n β€ 500) and m (1 β€ m β€ 1000) β the number of books, and the number of days for which Jaehyun would read books.The second line contains n space-separated integers w1, w2, ..., wn (1 β€ wi β€ 100) β the weight of each book.The third line contains m space separated integers b1, b2, ..., bm (1 β€ bj β€ n) β the order of books that he would read. Note that he can read the same book more than once.
|
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
|
Here's a picture depicting the example. Each vertical column presents the stacked books.
|
Input: 3 51 2 31 3 2 3 1 | Output: 12
|
Medium
| 4 | 1,258 | 452 | 120 | 5 |
1,065 |
B
|
1065B
|
B. Vasya and Isolated Vertices
| 1,300 |
constructive algorithms; graphs
|
Vasya has got an undirected graph consisting of \(n\) vertices and \(m\) edges. This graph doesn't contain any self-loops or multiple edges. Self-loop is an edge connecting a vertex to itself. Multiple edges are a pair of edges such that they connect the same pair of vertices. Since the graph is undirected, the pair of edges \((1, 2)\) and \((2, 1)\) is considered to be multiple edges. Isolated vertex of the graph is a vertex such that there is no edge connecting this vertex to any other vertex.Vasya wants to know the minimum and maximum possible number of isolated vertices in an undirected graph consisting of \(n\) vertices and \(m\) edges.
|
The only line contains two integers \(n\) and \(m~(1 \le n \le 10^5, 0 \le m \le \frac{n (n - 1)}{2})\).It is guaranteed that there exists a graph without any self-loops or multiple edges with such number of vertices and edges.
|
In the only line print two numbers \(min\) and \(max\) β the minimum and maximum number of isolated vertices, respectively.
|
In the first example it is possible to construct a graph with \(0\) isolated vertices: for example, it should contain edges \((1, 2)\) and \((3, 4)\). To get one isolated vertex, we may construct a graph with edges \((1, 2)\) and \((1, 3)\). In the second example the graph will always contain exactly one isolated vertex.
|
Input: 4 2 | Output: 0 1
|
Easy
| 2 | 649 | 227 | 123 | 10 |
1,468 |
C
|
1468C
|
C. Berpizza
| 1,400 |
data structures; implementation
|
Monocarp and Polycarp are working as waiters in Berpizza, a pizzeria located near the center of Bertown. Since they are waiters, their job is to serve the customers, but they choose whom they serve first differently.At the start of the working day, there are no customers at the Berpizza. They come there one by one. When a customer comes into the pizzeria, she sits and waits for Monocarp or Polycarp to serve her. Monocarp has been working in Berpizza for just two weeks, so whenever he serves a customer, he simply chooses the one who came to Berpizza first, and serves that customer. On the other hand, Polycarp is an experienced waiter at Berpizza, and he knows which customers are going to spend a lot of money at the pizzeria (and which aren't) as soon as he sees them. For each customer, Polycarp estimates the amount of money this customer can spend, and when he serves a customer, he chooses the one that is expected to leave the most money at Berpizza (in case there are several such customers, he chooses the one who came first among them). Obviously, no customer can be served twice, so Monocarp and Polycarp choose which customer to serve only among those who haven't been served yet.When the number of customers gets really high, it becomes difficult for both Monocarp and Polycarp to choose the customer they are going to serve. Your task is to write a program that makes these choices for them. Formally, your program should be able to process three types of queries: \(1\) \(m\) β a customer comes to Berpizza, and Polycarp estimates the amount of money that they will spend as \(m\); \(2\) β Monocarp serves a customer which came to the pizzeria first; \(3\) β Polycarp serves a customer which is expected to spend the largest amount of money at the pizzeria (if there are several such customers, the one that came to the pizzeria first is chosen). For each query of types \(2\) and \(3\), report the number of the customer who was served (the customers are numbered in the order they come to the pizzeria, starting from \(1\)).
|
The first line contains one integer \(q\) (\(2 \le q \le 5 \cdot 10^5\)) β the number of queries.Then \(q\) lines follow, each describing a query in one of the following formats: \(1\) \(m\) (\(1 \le m \le 5 \cdot 10^5\)) β a customer comes to Berpizza, and Polycarp estimates the amount of money that they will spend as \(m\); \(2\) β Monocarp serves a customer which came to the pizzeria first; \(3\) β Polycarp serves a customer which is expected to spend the largest amount of money at the pizzeria (if there are multiple such customers, the one that came to the pizzeria first is chosen). Queries of type \(2\) and \(3\) are asked only when there exists at least one customer that hasn't been served yet. There is at least one query of type \(2\) or \(3\) in the input.
|
For each query of type \(2\) or \(3\), print one integer β the number of the customer that has been served in that event. The customers are numbered in the order in which they come to the pizzeria, starting from \(1\).
|
Input: 8 1 8 1 10 1 6 3 2 1 9 2 3 | Output: 2 1 3 4
|
Easy
| 2 | 2,047 | 774 | 218 | 14 |
|
683 |
D
|
683D
|
D. Chocolate Bar
| 1,400 |
*special; math
|
A chocolate bar has a rectangular shape and consists of n Γ m slices. In other words, a bar consists of n rows with m slices of chocolate in each row.Each slice of chocolate is known to weigh 1 gram. Your task is to determine for each of the q chocolate bars whether it is possible to obtain a piece weighing p grams by breaking the bar several (possibly zero) times. The final piece of the chocolate bar should be whole, and breaks are made along the line of slices' section for the whole length of the current piece.
|
The first line contains the positive integer q (1 β€ q β€ 100) β the number of chocolate bars. Each of the following q lines contains three positive integers n, m and p (1 β€ n, m, p β€ 1000) β the size of the chocolate bar, and the weight of the piece which should be obtained.
|
The output should contain q lines and the i-th line must contain ""Yes"" (without the quotes), if it is possible to perform the task for i-th chocolate bar, or ""No"" otherwise.
|
Input: 23 3 44 4 7 | Output: YesNo
|
Easy
| 2 | 518 | 274 | 177 | 6 |
|
148 |
D
|
148D
|
D. Bag of mice
| 1,800 |
dp; games; math; probabilities
|
The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, while the princess thinks they should just go to bed early. They are desperate to come to an amicable agreement, so they decide to leave this up to chance.They take turns drawing a mouse from a bag which initially contains w white and b black mice. The person who is the first to draw a white mouse wins. After each mouse drawn by the dragon the rest of mice in the bag panic, and one of them jumps out of the bag itself (the princess draws her mice carefully and doesn't scare other mice). Princess draws first. What is the probability of the princess winning?If there are no more mice in the bag and nobody has drawn a white mouse, the dragon wins. Mice which jump out of the bag themselves are not considered to be drawn (do not define the winner). Once a mouse has left the bag, it never returns to it. Every mouse is drawn from the bag with the same probability as every other one, and every mouse jumps out of the bag with the same probability as every other one.
|
The only line of input data contains two integers w and b (0 β€ w, b β€ 1000).
|
Output the probability of the princess winning. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
|
Let's go through the first sample. The probability of the princess drawing a white mouse on her first turn and winning right away is 1/4. The probability of the dragon drawing a black mouse and not winning on his first turn is 3/4 * 2/3 = 1/2. After this there are two mice left in the bag β one black and one white; one of them jumps out, and the other is drawn by the princess on her second turn. If the princess' mouse is white, she wins (probability is 1/2 * 1/2 = 1/4), otherwise nobody gets the white mouse, so according to the rule the dragon wins.
|
Input: 1 3 | Output: 0.500000000
|
Medium
| 4 | 1,136 | 76 | 144 | 1 |
437 |
E
|
437E
|
E. The Child and Polygon
| 2,500 |
dp; geometry
|
This time our child has a simple polygon. He has to find the number of ways to split the polygon into non-degenerate triangles, each way must satisfy the following requirements: each vertex of each triangle is one of the polygon vertex; each side of the polygon must be the side of exactly one triangle; the area of intersection of every two triangles equals to zero, and the sum of all areas of triangles equals to the area of the polygon; each triangle must be completely inside the polygon; each side of each triangle must contain exactly two vertices of the polygon. The picture below depicts an example of a correct splitting. Please, help the child. Calculate the described number of ways modulo 1000000007 (109 + 7) for him.
|
The first line contains one integer n (3 β€ n β€ 200) β the number of vertices of the polygon. Then follow n lines, each line containing two integers. The i-th line contains xi, yi (|xi|, |yi| β€ 107) β the i-th vertex of the polygon in clockwise or counterclockwise order.It's guaranteed that the polygon is simple.
|
Output the number of ways modulo 1000000007 (109 + 7).
|
In the first sample, there are two possible splittings: In the second sample, there are only one possible splitting:
|
Input: 40 00 11 11 0 | Output: 2
|
Expert
| 2 | 731 | 313 | 54 | 4 |
750 |
H
|
750H
|
H. New Year and Snowy Grid
| 3,500 |
dfs and similar; dsu; graphs; interactive
|
Pay attention to the output section below, where you will see the information about flushing the output.Bearland is a grid with h rows and w columns. Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right. Every cell is either allowed (denoted by '.' in the input) or permanently blocked (denoted by '#').Bearland is a cold land, where heavy snow often makes travelling harder. Every day a few allowed cells are temporarily blocked by snow. Note, that this block works only on this particular day and next day any of these cells might be allowed again (unless there is another temporarily block).It's possible to move directly between two cells only if they share a side and none of them is permanently or temporarily blocked.Limak is a little polar bear who lives in Bearland. His house is at the top left cell, while his school is at the bottom right cell. Every day Limak should first go from his house to the school and then return back to his house. Since he gets bored easily, he doesn't want to visit the same cell twice on one day, except for the cell with his house, where he starts and ends. If Limak can reach a school and return home avoiding revisiting cells, he calls a day interesting.There are q days you must process, one after another. For each of these days you should check if it's interesting and print ""YES"" or ""NO"" on a separate line. In order to be able to read the description of the next day you should print the answer for the previous one and flush the output.It's guaranteed that a day with no cells temporarily blocked by snow would be interesting. It's also guaranteed that cells with Limak's house and school are never blocked (neither permanently or temporarily).
|
The first line of the input contains three integers h, w and q (2 β€ h, w β€ 1000, 1 β€ q β€ 10 000) β the height and the width of the grid, and the number of days, respectively.Next h lines describe which cells are allowed and which permanently blocked. The i-th line contains a string of length w, describing the i-th row. Every character is either '.' (denoting an allowed cell) or '#' (denoting a permanently blocked cell). It's guaranteed that a day with no cells temporarily blocked by snow would be interesting.Then, the description of q days is given. The description of the i-th day starts with a line containing a single integer ki (1 β€ ki β€ 10) β the number of cells that are temporarily blocked by snow on that day. Each of next ki lines contains two integers ri, j and ci, j (1 β€ ri, j β€ h, 1 β€ ci, j β€ w), representing a cell at the intersection of the row ri, j and the column ci, j. The given ki cells are distinct and none of them is permanently blocked. Also, none of them contains Limak's house or school.
|
For each of q days print ""YES"" if that day is interesting, and otherwise print ""NO"", both without the quotes. After printing an answer, you have to both print the end-of-line character and flush the output. Then you can proceed to the next day. You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output.To flush you can use (just after printing a YES/NO and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages.
|
In the first sample, there are 4 days. Drawings below show how Limak could go to school and return to his home in the second and the third day (on the left and on the right respectively). A permanently blocked cell is painted red, while cells temporarily blocked by snow are painted orange. Black and green arrows should Limak's way to the school and back to the house respectively. For the second sample, below you can see how the grid looks like on each day, where '#' denotes a cell that is blocked, either temporarily or permanently.
|
Input: 3 5 4...........#...11 411 522 43 121 53 3 | Output: NOYESYESNO
|
Master
| 4 | 1,747 | 1,020 | 563 | 7 |
989 |
A
|
989A
|
A. A Blend of Springtime
| 900 |
implementation; strings
|
When the curtains are opened, a canvas unfolds outside. Kanno marvels at all the blonde colours along the riverside β not tangerines, but blossoms instead.""What a pity it's already late spring,"" sighs Mino with regret, ""one more drizzling night and they'd be gone.""""But these blends are at their best, aren't they?"" Absorbed in the landscape, Kanno remains optimistic. The landscape can be expressed as a row of consecutive cells, each of which either contains a flower of colour amber or buff or canary yellow, or is empty.When a flower withers, it disappears from the cell that it originally belonged to, and it spreads petals of its colour in its two neighbouring cells (or outside the field if the cell is on the side of the landscape). In case petals fall outside the given cells, they simply become invisible.You are to help Kanno determine whether it's possible that after some (possibly none or all) flowers shed their petals, at least one of the cells contains all three colours, considering both petals and flowers. Note that flowers can wither in arbitrary order.
|
The first and only line of input contains a non-empty string \(s\) consisting of uppercase English letters 'A', 'B', 'C' and characters '.' (dots) only (\(\lvert s \rvert \leq 100\)) β denoting cells containing an amber flower, a buff one, a canary yellow one, and no flowers, respectively.
|
Output ""Yes"" if it's possible that all three colours appear in some cell, and ""No"" otherwise.You can print each letter in any case (upper or lower).
|
In the first example, the buff and canary yellow flowers can leave their petals in the central cell, blending all three colours in it.In the second example, it's impossible to satisfy the requirement because there is no way that amber and buff meet in any cell.
|
Input: .BAC. | Output: Yes
|
Beginner
| 2 | 1,080 | 290 | 152 | 9 |
241 |
A
|
241A
|
A. Old Peykan
| 1,300 |
greedy
|
There are n cities in the country where the Old Peykan lives. These cities are located on a straight line, we'll denote them from left to right as c1, c2, ..., cn. The Old Peykan wants to travel from city c1 to cn using roads. There are (n - 1) one way roads, the i-th road goes from city ci to city ci + 1 and is di kilometers long.The Old Peykan travels 1 kilometer in 1 hour and consumes 1 liter of fuel during this time.Each city ci (except for the last city cn) has a supply of si liters of fuel which immediately transfers to the Old Peykan if it passes the city or stays in it. This supply refreshes instantly k hours after it transfers. The Old Peykan can stay in a city for a while and fill its fuel tank many times. Initially (at time zero) the Old Peykan is at city c1 and s1 liters of fuel is transferred to it's empty tank from c1's supply. The Old Peykan's fuel tank capacity is unlimited. Old Peykan can not continue its travel if its tank is emptied strictly between two cities.Find the minimum time the Old Peykan needs to reach city cn.
|
The first line of the input contains two space-separated integers m and k (1 β€ m, k β€ 1000). The value m specifies the number of roads between cities which is equal to n - 1.The next line contains m space-separated integers d1, d2, ..., dm (1 β€ di β€ 1000) and the following line contains m space-separated integers s1, s2, ..., sm (1 β€ si β€ 1000).
|
In the only line of the output print a single integer β the minimum time required for The Old Peykan to reach city cn from city c1.
|
In the second sample above, the Old Peykan stays in c1 for 3 hours.
|
Input: 4 61 2 5 22 3 3 4 | Output: 10
|
Easy
| 1 | 1,054 | 347 | 131 | 2 |
1,250 |
J
|
1250J
|
J. The Parade
| 1,800 |
binary search; greedy
|
The Berland Army is preparing for a large military parade. It is already decided that the soldiers participating in it will be divided into \(k\) rows, and all rows will contain the same number of soldiers.Of course, not every arrangement of soldiers into \(k\) rows is suitable. Heights of all soldiers in the same row should not differ by more than \(1\). The height of each soldier is an integer between \(1\) and \(n\).For each possible height, you know the number of soldiers having this height. To conduct a parade, you have to choose the soldiers participating in it, and then arrange all of the chosen soldiers into \(k\) rows so that both of the following conditions are met: each row has the same number of soldiers, no row contains a pair of soldiers such that their heights differ by \(2\) or more. Calculate the maximum number of soldiers who can participate in the parade.
|
The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases. Then the test cases follow. Each test case begins with a line containing two integers \(n\) and \(k\) (\(1 \le n \le 30000\), \(1 \le k \le 10^{12}\)) β the number of different heights of soldiers and the number of rows of soldiers in the parade, respectively.The second (and final) line of each test case contains \(n\) integers \(c_1\), \(c_2\), ..., \(c_n\) (\(0 \le c_i \le 10^{12}\)), where \(c_i\) is the number of soldiers having height \(i\) in the Berland Army.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(30000\).
|
For each test case, print one integer β the maximum number of soldiers that can participate in the parade.
|
Explanations for the example test cases: the heights of soldiers in the rows can be: \([3, 3, 3, 3]\), \([1, 2, 1, 1]\), \([1, 1, 1, 1]\), \([3, 3, 3, 3]\) (each list represents a row); all soldiers can march in the same row; \(33\) soldiers with height \(1\) in each of \(3\) rows; all soldiers can march in the same row; all soldiers with height \(2\) and \(3\) can march in the same row.
|
Input: 5 3 4 7 1 13 1 1 100 1 3 100 2 1 1000000000000 1000000000000 4 1 10 2 11 1 | Output: 16 100 99 2000000000000 13
|
Medium
| 2 | 886 | 649 | 106 | 12 |
1,092 |
A
|
1092A
|
A. Uniform String
| 800 |
implementation
|
You are given two integers \(n\) and \(k\).Your task is to construct such a string \(s\) of length \(n\) that for each \(i\) from \(1\) to \(k\) there is at least one \(i\)-th letter of the Latin alphabet in this string (the first letter is 'a', the second is 'b' and so on) and there are no other letters except these. You have to maximize the minimal frequency of some letter (the frequency of a letter is the number of occurrences of this letter in a string). If there are several possible answers, you can print any.You have to answer \(t\) independent queries.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of queries.The next \(t\) lines are contain queries, one per line. The \(i\)-th line contains two integers \(n_i\) and \(k_i\) (\(1 \le n_i \le 100, 1 \le k_i \le min(n_i, 26)\)) β the length of the string in the \(i\)-th query and the number of characters in the \(i\)-th query.
|
Print \(t\) lines. In the \(i\)-th line print the answer to the \(i\)-th query: any string \(s_i\) satisfying the conditions in the problem statement with constraints from the \(i\)-th query.
|
In the first example query the maximum possible minimal frequency is \(2\), it can be easily seen that the better answer doesn't exist. Other examples of correct answers: ""cbcabba"", ""ccbbaaa"" (any permutation of given answers is also correct).In the second example query any permutation of first four letters is acceptable (the maximum minimal frequency is \(1\)).In the third example query any permutation of the given answer is acceptable (the maximum minimal frequency is \(3\)).
|
Input: 3 7 3 4 4 6 2 | Output: cbcacab abcd baabab
|
Beginner
| 1 | 565 | 369 | 191 | 10 |
2,066 |
A
|
2066A
|
A. Object Identification
| 1,400 |
graphs; greedy; implementation; interactive
|
This is an interactive problem. You are given an array \(x_1, \ldots, x_n\) of integers from \(1\) to \(n\). The jury also has a fixed but hidden array \(y_1, \ldots, y_n\) of integers from \(1\) to \(n\). The elements of array \(y\) are unknown to you. Additionally, it is known that for all \(i\), \(x_i \neq y_i\), and all pairs \((x_i, y_i)\) are distinct.The jury has secretly thought of one of two objects, and you need to determine which one it is: Object A: A directed graph with \(n\) vertices numbered from \(1\) to \(n\), and with \(n\) edges of the form \(x_i \to y_i\). Object B: \(n\) points on a coordinate plane. The \(i\)-th point has coordinates \((x_i, y_i)\). To guess which object the jury has thought of, you can make queries. In one query, you must specify two numbers \(i, j\) \((1 \leq i, j \leq n, i \neq j)\). In response, you receive one number: If the jury has thought of Object A, you receive the length of the shortest path (in edges) from vertex \(i\) to vertex \(j\) in the graph, or \(0\) if there is no path. If the jury has thought of Object B, you receive the Manhattan distance between points \(i\) and \(j\), that is \(|x_i -x_j| + |y_i - y_j|\).You have \(2\) queries to determine which of the objects the jury has thought of.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.
|
In the first test case, \(x = [2,2,3]\), \(y = [1,3,1]\) and Object A is guessed.In the second test case, \(x = [5,1,4,2,3]\), \(y = [3,3,2,4,1]\) and Object B is guessed.
|
Input: 2 3 2 2 3 1 0 5 5 1 4 2 3 4 4 | Output: ? 2 3 ? 1 2 ! A ? 1 5 ? 5 1 ! B
|
Easy
| 4 | 1,266 | 161 | 0 | 20 |
|
1,302 |
F
|
1302F
|
F. Keep talking and nobody explodes β easy
| 0 |
bitmasks; brute force; expression parsing
|
This is an unusual problem in an unusual contest, here is the announcement: http://codeforces.com/blog/entry/73543You have the safe lock which consists of 5 decimal digits. If you rotate some digit, it increases by one, except 9 which becomes 0.Initially, the lock contains number \(x\). To unlock the safe you must do the following operations in order (and be careful, don't mix up if and else statements).If sum of digits on positions 1 and 4 is greater than 10, rotate digit on position 1 by 3 times, else rotate digit on position 4 by 8 times.If sum of digits on positions 3 and 2 is greater than 8, rotate digit on position 4 by 9 times, else rotate digit on position 5 by 8 times.If digit on position 3 is odd, rotate digit on position 3 by 3 times, else rotate digit on position 3 by 4 times.If digit on position 5 is greater than digit on position 2, rotate digit on position 4 by 1 times, else rotate digit on position 2 by 7 times.If digit on position 1 is odd, rotate digit on position 1 by 3 times, else rotate digit on position 3 by 5 times.If digit on position 4 is odd, rotate digit on position 4 by 7 times, else rotate digit on position 1 by 9 times.If digit on position 4 is greater than digit on position 1, rotate digit on position 4 by 9 times, else rotate digit on position 4 by 2 times.If digit on position 1 is greater than digit on position 3, rotate digit on position 2 by 1 times, else rotate digit on position 3 by 1 times.If digit on position 5 is greater than digit on position 3, rotate digit on position 4 by 5 times, else rotate digit on position 5 by 8 times.If sum of digits on positions 1 and 3 is greater than 8, rotate digit on position 4 by 5 times, else rotate digit on position 2 by 5 times.If digit on position 1 is greater than digit on position 4, rotate digit on position 4 by 3 times, else rotate digit on position 2 by 3 times.If sum of digits on positions 3 and 1 is greater than 9, rotate digit on position 2 by 9 times, else rotate digit on position 2 by 2 times.If sum of digits on positions 4 and 3 is greater than 10, rotate digit on position 4 by 7 times, else rotate digit on position 5 by 7 times.If digit on position 3 is greater than digit on position 2, rotate digit on position 3 by 2 times, else rotate digit on position 4 by 6 times.If digit on position 1 is greater than digit on position 3, rotate digit on position 1 by 9 times, else rotate digit on position 2 by 9 times.If digit on position 3 is odd, rotate digit on position 3 by 9 times, else rotate digit on position 1 by 5 times.If sum of digits on positions 3 and 5 is greater than 9, rotate digit on position 3 by 4 times, else rotate digit on position 3 by 9 times.If digit on position 3 is greater than digit on position 1, rotate digit on position 5 by 1 times, else rotate digit on position 5 by 7 times.If digit on position 1 is greater than digit on position 3, rotate digit on position 2 by 9 times, else rotate digit on position 4 by 6 times.If sum of digits on positions 2 and 3 is greater than 10, rotate digit on position 2 by 2 times, else rotate digit on position 3 by 6 times.
|
Input contains single number \(x\) consisting of exactly 5 digits, leading zeroes are allowed.
|
Output the number after applying all operations.
|
Input: 00000 | Output: 61376
|
Beginner
| 3 | 3,113 | 94 | 48 | 13 |
|
187 |
E
|
187E
|
E. Heaven Tour
| 2,900 |
data structures; greedy
|
The story was not finished as PMP thought. God offered him one more chance to reincarnate and come back to life. But before he can come back, God told him that PMP should ask n great men including prominent programmers about their life experiences.The men are standing on a straight line. They are numbered 1 through n from left to right. The coordinate of the i-th man is xi (xi < xi + 1, i < n). PMP should visit all these people one by one in arbitrary order. Each men should be visited exactly once. At the beginning of his tour, he starts at location of s-th man and asks him about his experiences.Each time PMP wants to change his location, he should give a ticket to an angel and the angel carries him to his destination. Angels take PMP from one location, fly to his destination and put him down there. Nobody else is visited in this movement. Moving from i-th man to j-th man, takes |xi - xj| time. PMP can get back to life as soon as he visits all men.There are two types of angels: Some angels are going to the right and they only accept right tickets. Others are going the left and they only accept left tickets. There are an unlimited number of angels of each type. PMP has l left tickets and n - 1 - l right tickets.PMP wants to get back to life as soon as possible to be able to compete in this year's final instead of the final he missed last year. He wants to know the quickest way to visit all the men exactly once. He also needs to know the exact sequence moves he should make.
|
The first line of input contains three space-separated integers n, l, s (2 β€ n β€ 105, 0 β€ l < n, 1 β€ s β€ n) β the number of people to visit, the number left tickets PMP got, and initial location of PMP. Next line contains n space-separated integers. The i-th integer in this line is xi (0 = x1 < x2 < ... < xn β€ 109) β the location of i-th man.
|
If PMP cannot visit all men with the tickets he got print -1 in the only line of output. Otherwise, in the first line you should print the minimum time PMP can visit all men. In the second line you should print n - 1 integers that are the numbers of the men that PMP should visit in order in one optimal solution. If there are multiple answers, output any of them.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
|
Let us remind here, a great contestant of all times, who left us about a year ago. May Renat Mullakhanov rest in peace.
|
Input: 5 2 20 10 11 21 22 | Output: 331 3 5 4
|
Master
| 2 | 1,496 | 344 | 506 | 1 |
1,678 |
B1
|
1678B1
|
B1. Tokitsukaze and Good 01-String (easy version)
| 800 |
implementation
|
This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments.Tokitsukaze has a binary string \(s\) of length \(n\), consisting only of zeros and ones, \(n\) is even.Now Tokitsukaze divides \(s\) into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, \(s\) is considered good if the lengths of all subsegments are even.For example, if \(s\) is ""11001111"", it will be divided into ""11"", ""00"" and ""1111"". Their lengths are \(2\), \(2\), \(4\) respectively, which are all even numbers, so ""11001111"" is good. Another example, if \(s\) is ""1110011000"", it will be divided into ""111"", ""00"", ""11"" and ""000"", and their lengths are \(3\), \(2\), \(2\), \(3\). Obviously, ""1110011000"" is not good.Tokitsukaze wants to make \(s\) good by changing the values of some positions in \(s\). Specifically, she can perform the operation any number of times: change the value of \(s_i\) to '0' or '1'(\(1 \leq i \leq n\)). Can you tell her the minimum number of operations to make \(s\) good?
|
The first contains a single positive integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases.For each test case, the first line contains a single integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the length of \(s\), it is guaranteed that \(n\) is even.The second line contains a binary string \(s\) of length \(n\), consisting only of zeros and ones.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print a single line with one integer β the minimum number of operations to make \(s\) good.
|
In the first test case, one of the ways to make \(s\) good is the following.Change \(s_3\), \(s_6\) and \(s_7\) to '0', after that \(s\) becomes ""1100000000"", it can be divided into ""11"" and ""00000000"", which lengths are \(2\) and \(8\) respectively. There are other ways to operate \(3\) times to make \(s\) good, such as ""1111110000"", ""1100001100"", ""1111001100"".In the second, third and fourth test cases, \(s\) is good initially, so no operation is required.
|
Input: 51011100110008110011112002116100110 | Output: 3 0 0 0 3
|
Beginner
| 1 | 1,176 | 455 | 111 | 16 |
1,669 |
E
|
1669E
|
E. 2-Letter Strings
| 1,200 |
data structures; math; strings
|
Given \(n\) strings, each of length \(2\), consisting of lowercase Latin alphabet letters from 'a' to 'k', output the number of pairs of indices \((i, j)\) such that \(i < j\) and the \(i\)-th string and the \(j\)-th string differ in exactly one position.In other words, count the number of pairs \((i, j)\) (\(i < j\)) such that the \(i\)-th string and the \(j\)-th string have exactly one position \(p\) (\(1 \leq p \leq 2\)) such that \({s_{i}}_{p} \neq {s_{j}}_{p}\).The answer may not fit into 32-bit integer type, so you should use 64-bit integers like long long in C++ to avoid integer overflow.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β 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 10^5\)) β the number of strings.Then follows \(n\) lines, the \(i\)-th of which containing a single string \(s_i\) of length \(2\), consisting of lowercase Latin letters from 'a' to 'k'.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print a single integer β the number of pairs \((i, j)\) (\(i < j\)) such that the \(i\)-th string and the \(j\)-th string have exactly one position \(p\) (\(1 \leq p \leq 2\)) such that \({s_{i}}_{p} \neq {s_{j}}_{p}\). Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
|
For the first test case the pairs that differ in exactly one position are: (""ab"", ""cb""), (""ab"", ""db""), (""ab"", ""aa""), (""cb"", ""db"") and (""cb"", ""cc"").For the second test case the pairs that differ in exactly one position are: (""aa"", ""ac""), (""aa"", ""ca""), (""cc"", ""ac""), (""cc"", ""ca""), (""ac"", ""aa"") and (""ca"", ""aa"").For the third test case, the are no pairs satisfying the conditions.
|
Input: 46abcbdbaaccef7aabbccaccabbaa4kkkkabab5jfjfjkjkjk | Output: 5 6 0 6
|
Easy
| 3 | 602 | 498 | 426 | 16 |
982 |
C
|
982C
|
C. Cut 'em all!
| 1,500 |
dfs and similar; dp; graphs; greedy; trees
|
You're given a tree with \(n\) vertices.Your task is to determine the maximum possible number of edges that can be removed in such a way that all the remaining connected components will have even size.
|
The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) denoting the size of the tree. The next \(n - 1\) lines contain two integers \(u\), \(v\) (\(1 \le u, v \le n\)) each, describing the vertices connected by the \(i\)-th edge.It's guaranteed that the given edges form a tree.
|
Output a single integer \(k\) β the maximum number of edges that can be removed to leave all connected components with even size, or \(-1\) if it is impossible to remove edges in order to satisfy this property.
|
In the first example you can remove the edge between vertices \(1\) and \(4\). The graph after that will have two connected components with two vertices in each.In the second example you can't remove edges in such a way that all components have even number of vertices, so the answer is \(-1\).
|
Input: 42 44 13 1 | Output: 1
|
Medium
| 5 | 201 | 287 | 210 | 9 |
1,777 |
D
|
1777D
|
D. Score of a Tree
| 1,900 |
bitmasks; combinatorics; dfs and similar; dp; math; probabilities; trees
|
You are given a tree of \(n\) nodes, rooted at \(1\). Every node has a value of either \(0\) or \(1\) at time \(t=0\).At any integer time \(t>0\), the value of a node becomes the bitwise XOR of the values of its children at time \(t - 1\); the values of leaves become \(0\) since they don't have any children.Let \(S(t)\) denote the sum of values of all nodes at time \(t\). Let \(F(A)\) denote the sum of \(S(t)\) across all values of \(t\) such that \(0 \le t \le 10^{100}\), where \(A\) is the initial assignment of \(0\)s and \(1\)s in the tree.The task is to find the sum of \(F(A)\) for all \(2^n\) initial configurations of \(0\)s and \(1\)s in the tree. Print the sum modulo \(10^9+7\).
|
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 \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of nodes in the tree.The next \(n-1\) lines of each test case contain two integers each β \(u\), \(v\) indicating an edge between \(u\) and \(v\) (\(1 \le u, v \le n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
Output the sum modulo \(10^9+7\) for each test case.
|
Let us find \(F(A)\) for the configuration \(A = [0,1,0,0,1,1]\) (\(A[i]\) denotes the value of node \(i\)). Initially (at \(t = 0\)) our tree is as shown in the picture below. In each node, two values are shown: the number and the value of this node. \(S(0)\) for this configuration is \(3\). At \(t = 1\) the configuration changes to \([1,0,0,0,0,0]\). The tree looks as shown below. \(S(1) = 1\). At \(t = 2\) the configuration changes to \([0,0,0,0,0,0]\). The tree looks as shown below. \(S(2) = 0\). For all \(t>2\), the graph remains unchanged, so \(S(t)=0\) for all \(t > 2\). So, for the initial configuration \(A = [0,1,0,0,1,1]\), the value of \(F(A) = 3 + 1 = 4\).Doing this process for all possible \(2^{6}\) configurations yields us an answer of \(\textbf{288}\).
|
Input: 161 21 33 43 53 6 | Output: 288
|
Hard
| 7 | 694 | 514 | 52 | 17 |
1,396 |
C
|
1396C
|
C. Monster Invaders
| 2,300 |
dp; greedy; implementation
|
Ziota found a video game called ""Monster Invaders"".Similar to every other shooting RPG game, ""Monster Invaders"" involves killing monsters and bosses with guns.For the sake of simplicity, we only consider two different types of monsters and three different types of guns.Namely, the two types of monsters are: a normal monster with \(1\) hp. a boss with \(2\) hp. And the three types of guns are: Pistol, deals \(1\) hp in damage to one monster, \(r_1\) reloading time Laser gun, deals \(1\) hp in damage to all the monsters in the current level (including the boss), \(r_2\) reloading time AWP, instantly kills any monster, \(r_3\) reloading time The guns are initially not loaded, and the Ziota can only reload 1 gun at a time.The levels of the game can be considered as an array \(a_1, a_2, \ldots, a_n\), in which the \(i\)-th stage has \(a_i\) normal monsters and 1 boss. Due to the nature of the game, Ziota cannot use the Pistol (the first type of gun) or AWP (the third type of gun) to shoot the boss before killing all of the \(a_i\) normal monsters.If Ziota damages the boss but does not kill it immediately, he is forced to move out of the current level to an arbitrary adjacent level (adjacent levels of level \(i\) \((1 < i < n)\) are levels \(i - 1\) and \(i + 1\), the only adjacent level of level \(1\) is level \(2\), the only adjacent level of level \(n\) is level \(n - 1\)). Ziota can also choose to move to an adjacent level at any time. Each move between adjacent levels are managed by portals with \(d\) teleportation time.In order not to disrupt the space-time continuum within the game, it is strictly forbidden to reload or shoot monsters during teleportation. Ziota starts the game at level 1. The objective of the game is rather simple, to kill all the bosses in all the levels. He is curious about the minimum time to finish the game (assuming it takes no time to shoot the monsters with a loaded gun and Ziota has infinite ammo on all the three guns). Please help him find this value.
|
The first line of the input contains five integers separated by single spaces: \(n\) \((2 \le n \le 10^6)\) β the number of stages, \(r_1, r_2, r_3\) \((1 \le r_1 \le r_2 \le r_3 \le 10^9)\) β the reload time of the three guns respectively, \(d\) \((1 \le d \le 10^9)\) β the time of moving between adjacent levels.The second line of the input contains \(n\) integers separated by single spaces \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 10^6, 1 \le i \le n)\).
|
Print one integer, the minimum time to finish the game.
|
In the first test case, the optimal strategy is: Use the pistol to kill three normal monsters and AWP to kill the boss (Total time \(1\cdot3+4=7\)) Move to stage two (Total time \(7+3=10\)) Use the pistol twice and AWP to kill the boss (Total time \(10+1\cdot2+4=16\)) Move to stage three (Total time \(16+3=19\)) Use the laser gun and forced to move to either stage four or two, here we move to stage four (Total time \(19+3+3=25\)) Use the pistol once, use AWP to kill the boss (Total time \(25+1\cdot1+4=30\)) Move back to stage three (Total time \(30+3=33\)) Kill the boss at stage three with the pistol (Total time \(33+1=34\)) Note that here, we do not finish at level \(n\), but when all the bosses are killed.
|
Input: 4 1 3 4 3 3 2 5 1 | Output: 34
|
Expert
| 3 | 2,017 | 460 | 55 | 13 |
2,061 |
F1
|
2061F1
|
F1. Kevin and Binary String (Easy Version)
| 2,100 |
greedy; implementation
|
This is the easy version of the problem. The difference between the versions is that in this version, string \(t\) consists of only '0' and '1'. You can hack only if you solved all versions of this problem. Kevin has a binary string \(s\) of length \(n\). Kevin can perform the following operation: Choose two adjacent blocks of \(s\) and swap them. A block is a maximal substring\(^{\text{β}}\) of identical characters. Formally, denote \(s[l,r]\) as the substring \(s_l s_{l+1} \ldots s_r\). A block is \(s[l,r]\) satisfying: \(l=1\) or \(s_l\not=s_{l-1}\). \(s_l=s_{l+1} = \ldots = s_{r}\). \(r=n\) or \(s_r\not=s_{r+1}\). Adjacent blocks are two blocks \(s[l_1,r_1]\) and \(s[l_2,r_2]\) satisfying \(r_1+1=l_2\).For example, if \(s=\mathtt{000}\,\mathbf{11}\,\mathbf{00}\,\mathtt{111}\), Kevin can choose the two blocks \(s[4,5]\) and \(s[6,7]\) and swap them, transforming \(s\) into \(\mathtt{000}\,\mathbf{00}\,\mathbf{11}\,\mathtt{111}\).Given a string \(t\) of length \(n\) consisting of '0', '1' and '?', Kevin wants to determine the minimum number of operations required to perform such that for any index \(i\) (\(1\le i\le n\)), if \(t_i\not=\) '?' then \(s_i=t_i\). If it is impossible, output \(-1\).\(^{\text{β}}\)A string \(a\) is a substring of a string \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a string \(s\) consisting of '0' and '1'.The second line of each test case contains a string \(t\) consisting of '0' and '1'.It is guaranteed that the lengths of \(s\) and \(t\) are the same.It is guaranteed that the sum of the length of \(s\) over all test cases will not exceed \(4\cdot 10^5\).
|
For each test case, output one integer β the minimum number of operations required. If it is impossible, output \(-1\).
|
In the first test case, the possible way is shown in the statement.In the second test case, one possible way could be: Swap blocks \([2, 2], [3, 3]\), \(s\) will become \(\mathtt{001101}\). Swap blocks \([3, 4], [5, 5]\), \(s\) will become \(\mathtt{000111}\). Swap blocks \([1, 3], [4, 6]\), \(s\) will become \(\mathtt{111000}\).
|
Input: 600011001110000011111010101111000010101100101101001100100111001 | Output: 1 3 1 -1 -1 -1
|
Hard
| 2 | 1,455 | 500 | 119 | 20 |
1,814 |
A
|
1814A
|
A. Coins
| 800 |
implementation; math
|
In Berland, there are two types of coins, having denominations of \(2\) and \(k\) burles.Your task is to determine whether it is possible to represent \(n\) burles in coins, i. e. whether there exist non-negative integers \(x\) and \(y\) such that \(2 \cdot x + k \cdot y = n\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^{18}\); \(k \ne 2\)).
|
For each test case, print YES if it is possible to represent \(n\) burles in coins; otherwise, print NO. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
In the first test case, you can take one coin with denomination \(2\) and one coin with denomination \(k = 3\).In the second test case, you can take three coins with denomination \(2\). Alternatively, you can take six coins with denomination \(k = 1\).In the third test case, there is no way to represent \(7\) burles.In the fourth test case, you can take one coin with denomination \(k = 8\).
|
Input: 45 36 17 48 8 | Output: YES YES NO YES
|
Beginner
| 2 | 278 | 212 | 258 | 18 |
1,482 |
H
|
1482H
|
H. Exam
| 3,400 |
data structures; string suffix structures; trees
|
This year a Chunin Selection Exam is held again in Konoha, and taking part in it are \(n\) ninjas named \(s_1\), \(s_2\), ..., \(s_n\). All names are distinct. One of the exam stages consists of fights between the participants. This year the rules determining the ninjas for each fight are the following: ninjas \(i\) and \(j\) fight against each other if the following conditions are held: \(i \neq j\); \(s_{j}\) is a substring of \(s_{i}\); there is no \(k\) except \(i\) and \(j\) that \(s_{j}\) is a substring of \(s_{k}\), and \(s_{k}\) is a substring of \(s_{i}\). A string \(a\) is a substring of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.Your task is to find out how many fights are going to take place this year.
|
The first line consists of the only integer \(n\) (\(1 \leq n \leq 10^{6}\)) standing for the number of examinees.The following \(n\) lines contain their names. No two names coincide, all names are non-empty and consist of lowercase English letters. The total length of all names doesn't exceed \(10^6\).
|
Print the only integer standing for the number of fights.
|
In the first example hidan fights against dan, and hanabi fights against nabi, who also fights bi. Ninjas named hanabi and bi don't fight each other since there is the ninja called nabi who breaks the third condition for them.In the second example the fights are held between abacaba and acaba, abacaba and abaca, acaba and aca, abaca and aca.
|
Input: 5 hidan dan hanabi bi nabi | Output: 3
|
Master
| 3 | 868 | 304 | 57 | 14 |
209 |
A
|
209A
|
A. Multicolored Marbles
| 1,600 |
dp; math
|
Polycarpus plays with red and blue marbles. He put n marbles from the left to the right in a row. As it turned out, the marbles form a zebroid.A non-empty sequence of red and blue marbles is a zebroid, if the colors of the marbles in this sequence alternate. For example, sequences (red; blue; red) and (blue) are zebroids and sequence (red; red) is not a zebroid.Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109 + 7).
|
The first line contains a single integer n (1 β€ n β€ 106) β the number of marbles in Polycarpus's sequence.
|
Print a single number β the answer to the problem modulo 1000000007 (109 + 7).
|
Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: pick the first marble; pick the second marble; pick the third marble; pick the first and second marbles; pick the second and third marbles; pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
|
Input: 3 | Output: 6
|
Medium
| 2 | 542 | 106 | 78 | 2 |
44 |
B
|
44B
|
B. Cola
| 1,500 |
implementation
|
To celebrate the opening of the Winter Computer School the organizers decided to buy in n liters of cola. However, an unexpected difficulty occurred in the shop: it turned out that cola is sold in bottles 0.5, 1 and 2 liters in volume. At that, there are exactly a bottles 0.5 in volume, b one-liter bottles and c of two-liter ones. The organizers have enough money to buy any amount of cola. What did cause the heated arguments was how many bottles of every kind to buy, as this question is pivotal for the distribution of cola among the participants (and organizers as well).Thus, while the organizers are having the argument, discussing different variants of buying cola, the Winter School can't start. Your task is to count the number of all the possible ways to buy exactly n liters of cola and persuade the organizers that this number is too large, and if they keep on arguing, then the Winter Computer School will have to be organized in summer.All the bottles of cola are considered indistinguishable, i.e. two variants of buying are different from each other only if they differ in the number of bottles of at least one kind.
|
The first line contains four integers β n, a, b, c (1 β€ n β€ 10000, 0 β€ a, b, c β€ 5000).
|
Print the unique number β the solution to the problem. If it is impossible to buy exactly n liters of cola, print 0.
|
Input: 10 5 5 5 | Output: 9
|
Medium
| 1 | 1,134 | 87 | 116 | 0 |
|
669 |
A
|
669A
|
A. Little Artem and Presents
| 800 |
math
|
Little Artem got n stones on his birthday and now wants to give some of them to Masha. He knows that Masha cares more about the fact of receiving the present, rather than the value of that present, so he wants to give her stones as many times as possible. However, Masha remembers the last present she received, so Artem can't give her the same number of stones twice in a row. For example, he can give her 3 stones, then 1 stone, then again 3 stones, but he can't give her 3 stones and then again 3 stones right after that.How many times can Artem give presents to Masha?
|
The only line of the input contains a single integer n (1 β€ n β€ 109) β number of stones Artem received on his birthday.
|
Print the maximum possible number of times Artem can give presents to Masha.
|
In the first sample, Artem can only give 1 stone to Masha.In the second sample, Atrem can give Masha 1 or 2 stones, though he can't give her 1 stone two times.In the third sample, Atrem can first give Masha 2 stones, a then 1 more stone.In the fourth sample, Atrem can first give Masha 1 stone, then 2 stones, and finally 1 stone again.
|
Input: 1 | Output: 1
|
Beginner
| 1 | 572 | 119 | 76 | 6 |
559 |
C
|
559C
|
C. Gerald and Giant Chess
| 2,200 |
combinatorics; dp; math; number theory
|
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an h Γ w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.
|
The first line of the input contains three integers: h, w, n β the sides of the board and the number of black cells (1 β€ h, w β€ 105, 1 β€ n β€ 2000). Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 β€ ri β€ h, 1 β€ ci β€ w) β the number of the row and column of the i-th cell.It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.
|
Print a single line β the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo 109 + 7.
|
Input: 3 4 22 22 3 | Output: 2
|
Hard
| 4 | 985 | 440 | 141 | 5 |
|
1,740 |
C
|
1740C
|
C. Bricks and Bags
| 1,400 |
constructive algorithms; games; greedy; sortings
|
There are \(n\) bricks numbered from \(1\) to \(n\). Brick \(i\) has a weight of \(a_i\).Pak Chanek has \(3\) bags numbered from \(1\) to \(3\) that are initially empty. For each brick, Pak Chanek must put it into one of the bags. After this, each bag must contain at least one brick.After Pak Chanek distributes the bricks, Bu Dengklek will take exactly one brick from each bag. Let \(w_j\) be the weight of the brick Bu Dengklek takes from bag \(j\). The score is calculated as \(|w_1 - w_2| + |w_2 - w_3|\), where \(|x|\) denotes the absolute value of \(x\).It is known that Bu Dengklek will take the bricks in such a way that minimises the score. What is the maximum possible final score if Pak Chanek distributes the bricks optimally?
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)) β the number of test cases. The following lines contain the description of each test case.The first line of each test case contains an integer \(n\) (\(3 \leq n \leq 2 \cdot 10^5\)) β the number of bricks.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the weights of the bricks.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 line containing an integer representing the maximum possible final score if Pak Chanek distributes the bricks optimally.
|
In the first test case, one way of achieving a final score of \(6\) is to do the following: Put bricks \(1\), \(4\), and \(5\) into bag \(1\). Put brick \(3\) into bag \(2\). Put brick \(2\) into bag \(3\). If Pak Chanek distributes the bricks that way, a way Bu Dengklek can take the bricks is: Take brick \(5\) from bag \(1\). Take brick \(3\) from bag \(2\). Take brick \(2\) from bag \(3\). The score is \(|a_5 - a_3| + |a_3 - a_2| = |3 - 5| + |5 - 1| = 6\). It can be shown that Bu Dengklek cannot get a smaller score from this distribution.It can be shown that there is no other distribution that results in a final score bigger than \(6\).
|
Input: 353 1 5 2 3417 8 19 458265 265 265 265 265 265 265 265 | Output: 6 63 0
|
Easy
| 4 | 739 | 550 | 149 | 17 |
2,115 |
F2
|
2115F2
|
F2. Gellyfish and Lycoris Radiata (Hard Version)
| 3,500 |
data structures
|
This is the hard version of the problem. The difference between the versions is that in this version, the time limit and the constraints on \(n\) and \(q\) are higher. You can hack only if you solved all versions of this problem. Gellyfish has an array consisting of \(n\) sets. Initially, all the sets are empty.Now Gellyfish will do \(q\) operations. Each operation contains one modification operation and one query operation, for the \(i\)-th (\(1 \leq i \leq q\)) operation:First, there will be a modification operation, which is one of the following: Insert operation: You are given an integer \(r\). For the \(1\)-th to \(r\)-th sets, insert element \(i\). Note that the element inserted here is \(i\), the index of the operation, not the index of the set. Reverse operation: You are given an integer \(r\). Reverse the \(1\)-th to \(r\)-th sets. Delete operation: You are given an integer \(x\). Delete element \(x\) from all sets that contain \(x\). Followed by a query operation: Query operation: You are given an integer \(p\). Output the smallest element in the \(p\)-th set (If the \(p\)-th set is empty, the answer is considered to be \(0\)). Now, Flower needs to provide the answer for each query operation. Please help her!Additional constraint on the problem: Gellyfish will only give the next operation after Flower has answered the previous query operation. That is, you need to solve this problem online. Please refer to the input format for more details.
|
The first line contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 3 \cdot 10^5\)) β the number of the sets and the number of operations.As you need to respond to the operations online, the operations will be encoded.The \(i\)-th line of the following \(q\) lines contains three integers \(a\), \(b\), and \(c\) (\(1 \leq a \leq 3\), \(1 \leq c \leq n\)) β describing the \(i\)-th operation in an encoded form.Here, \(a\) represents the type of modification operation. Among them, \(a=1\) represents Insert operation, \(a=2\) represents Reverse operation, \(a=3\) represents Delete operation. If \(a = 1\), then the modification operation is the Insert operation. It will be guaranteed that \(1 \leq b \leq n\). \(r\) will be calculated as \(r=(b+\text{ans}_{i-1}-1) \bmod n + 1\). If \(a=2\), then the modification operation is the Reverse operation. It will be guaranteed that \(1 \leq b \leq n\). \(r\) will be calculated as \(r=(b+\text{ans}_{i-1}-1) \bmod n + 1\). If \(a=3\), then the modification operation is the Delete operation. It will be guaranteed that \(1 \leq b \leq q\). \(x\) will be calculated as \(x=(b+\text{ans}_{i-1}-1) \bmod q + 1\). For the query operation, \(p\) will be calculated as \(p = (c+\text{ans}_{i-1}-1) \bmod n + 1\).Here \( \text{ans}_{i} (1 \leq i \leq q)\) represents the answer to the query operation in the \(i\)-th operation. Additionally, we define \( \text{ans}_{0} = 0\).
|
For each query operation, output the answer to the query.
|
All the sets are empty in the beginning, so the array is \([\{\}, \{\}, \{\}, \{\}, \{\}]\).With the decoding method given before, we can see what happens in each operation: For the first operation: \(a = 1, r = 2, p = 2\). The modification operation is an Insert operation; element \(1\) is inserted into the first two sets; so the array becomes \([\{1\}, \{1\}, \{\}, \{\}, \{\}]\), and the smallest element in the second set is \(1\). For the second operation: \(a = 2, r = 4, p = 2\). The modification operation is a Reverse operation; the first four sets are reversed; so the array becomes \([\{\}, \{\}, \{1\}, \{1\}, \{\}]\), and the second set is empty, which means the answer is \(0\). For the third operation: \(a = 1, r = 5, p = 3\). The modification operation is an Insert operation; element \(3\) is inserted into all the sets; so the array becomes \([\{3\}, \{3\}, \{1, 3\}, \{1, 3\}, \{3\}]\), and the smallest element in the third set is \(1\). For the fourth operation: \(a = 2, r = 3, p = 1\). The modification operation is a Reverse operation; the first three sets are reversed; so the array becomes \([\{1, 3\}, \{3\}, \{3\}, \{1, 3\}, \{3\}]\), and the smallest element in the first set is \(1\). For the fifth operation: \(a = 1, r = 1, p = 3\). The modification operation is an Insert operation; element \(5\) is inserted into the first set; so the array becomes \([\{1, 3, 5\}, \{3\}, \{3\}, \{1, 3\}, \{3\}]\), and the smallest element in the third set is \(3\). For the sixth operation: \(a = 2, r = 2, p = 2\). The modification operation is a Reverse operation; the first two sets are reversed; so the array becomes \([\{3\}, \{1, 3, 5\}, \{3\}, \{1, 3\}, \{3\}]\), and the smallest element in the second set is \(1\). For the seventh operation: \(a = 3, x = 3, p = 3\). The modification operation is a Delete operation; element \(3\) is deleted from all the sets; so the array becomes \([\{\}, \{1, 5\}, \{\}, \{1\}, \{\}]\), and the third set is empty, which means the answer is \(0\). For the eighth operation: \(a = 3, x = 1, p = 2\). The modification operation is a Delete operation; element \(1\) is deleted from all the sets; so the array becomes \([\{\}, \{5\}, \{\}, \{\}, \{\}]\), and the smallest element in the second set is \(5\). For the ninth operation: \(a = 3, x = 5, p = 5\). The modification operation is a Delete operation; element \(5\) is deleted from all the sets; so the array becomes \([\{\}, \{\}, \{\}, \{\}, \{\}]\), and the fifth set is empty, which means the answer is \(0\). For the tenth operation: \(a = 3, x = 2, p = 4\). The modification operation is a Delete operation; element \(2\) is deleted from all the sets; so the array becomes \([\{\}, \{\}, \{\}, \{\}, \{\}]\), and the fourth set is empty, which means the answer is \(0\). Please note that although we have not inserted element \(2\) into the sets, we still delete element \(2\) from all the sets in the tenth operation, which means that the Delete operation doesn't necessarily require the existence of a set to contain the deleted element. It also shows that it is possible to have two Delete operations that delete the same element.
|
Input: 5 101 2 22 3 11 5 32 2 51 5 22 4 43 2 23 1 23 10 53 2 4 | Output: 1 0 1 1 3 1 0 5 0 0
|
Master
| 1 | 1,474 | 1,422 | 57 | 21 |
2,062 |
E1
|
2062E1
|
E1. The Game (Easy Version)
| 2,000 |
data structures; dfs and similar; games; graphs; greedy; trees
|
This is the easy version of the problem. The difference between the versions is that in this version, you only need to find one of the 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 one of the possible nodes Cirno may choose 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 any possible node she may choose in the first turn.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 0 2 2 10
|
Hard
| 6 | 1,048 | 680 | 156 | 20 |
914 |
D
|
914D
|
D. Bash and a Tough Math Puzzle
| 1,900 |
data structures; number theory
|
Bash likes playing with arrays. He has an array a1, a2, ... an of n integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct.Suppose he guesses that the gcd of the elements in the range [l, r] of a is x. He considers the guess to be almost correct if he can change at most one element in the segment such that the gcd of the segment is x after making the change. Note that when he guesses, he doesn't actually change the array β he just wonders if the gcd of the segment can be made x. Apart from this, he also sometimes makes changes to the array itself.Since he can't figure it out himself, Bash wants you to tell him which of his guesses are almost correct. Formally, you have to process q queries of one of the following forms: 1 l r x β Bash guesses that the gcd of the range [l, r] is x. Report if this guess is almost correct. 2 i y β Bash sets ai to y. Note: The array is 1-indexed.
|
The first line contains an integer n (1 β€ n β€ 5Β·105) β the size of the array.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the elements of the array.The third line contains an integer q (1 β€ q β€ 4Β·105) β the number of queries.The next q lines describe the queries and may have one of the following forms: 1 l r x (1 β€ l β€ r β€ n, 1 β€ x β€ 109). 2 i y (1 β€ i β€ n, 1 β€ y β€ 109). Guaranteed, that there is at least one query of first type.
|
For each query of first type, output ""YES"" (without quotes) if Bash's guess is almost correct and ""NO"" (without quotes) otherwise.
|
In the first sample, the array initially is {2, 6, 3}. For query 1, the first two numbers already have their gcd as 2.For query 2, we can achieve a gcd of 3 by changing the first element of the array to 3. Note that the changes made during queries of type 1 are temporary and do not get reflected in the array. After query 3, the array is now {9, 6, 3}. For query 4, no matter which element you change, you cannot get the gcd of the range to be 2.
|
Input: 32 6 341 1 2 21 1 3 32 1 91 1 3 2 | Output: YESYESNO
|
Hard
| 2 | 1,041 | 457 | 134 | 9 |
587 |
E
|
587E
|
E. Duff as a Queen
| 2,900 |
data structures
|
Duff is the queen of her country, Andarz Gu. She's a competitive programming fan. That's why, when he saw her minister, Malek, free, she gave her a sequence consisting of n non-negative integers, a1, a2, ..., an and asked him to perform q queries for her on this sequence. There are two types of queries: given numbers l, r and k, Malek should perform for each l β€ i β€ r (, bitwise exclusive OR of numbers a and b). given numbers l and r Malek should tell her the score of sequence al, al + 1, ... , ar. Score of a sequence b1, ..., bk is the number of its different Kheshtaks. A non-negative integer w is a Kheshtak of this sequence if and only if there exists a subsequence of b, let's denote it as bi1, bi2, ... , bix (possibly empty) such that (1 β€ i1 < i2 < ... < ix β€ k). If this subsequence is empty, then w = 0.Unlike Duff, Malek is not a programmer. That's why he asked for your help. Please help him perform these queries.
|
The first line of input contains two integers, n and q (1 β€ n β€ 2 Γ 105 and 1 β€ q β€ 4 Γ 104).The second line of input contains n integers, a1, a2, ..., an separated by spaces (0 β€ ai β€ 109 for each 1 β€ i β€ n).The next q lines contain the queries. Each line starts with an integer t (1 β€ t β€ 2), type of the corresponding query. If t = 1, then there are three more integers in that line, l, r and k. Otherwise there are two more integers, l and r. (1 β€ l β€ r β€ n and 0 β€ k β€ 109)
|
Print the answer of each query of the second type in one line.
|
In the first query, we want all Kheshtaks of sequence 1, 2, 3, 4, 2 which are: 0, 1, 2, 3, 4, 5, 6, 7.In the third query, we want all Khestaks of sequence 1, 10, 3, 4, 2 which are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.In the fifth query, we want all Kheshtaks of sequence 0 which is 0.
|
Input: 5 51 2 3 4 22 1 51 2 2 82 1 51 1 3 102 2 2 | Output: 8161
|
Master
| 1 | 932 | 478 | 62 | 5 |
1,808 |
A
|
1808A
|
A. Lucky Numbers
| 900 |
brute force; implementation
|
Olympus City recently launched the production of personal starships. Now everyone on Mars can buy one and fly to other planets inexpensively.Each starship has a number βsome positive integer \(x\). Let's define the luckiness of a number \(x\) as the difference between the largest and smallest digits of that number. For example, \(142857\) has \(8\) as its largest digit and \(1\) as its smallest digit, so its luckiness is \(8-1=7\). And the number \(111\) has all digits equal to \(1\), so its luckiness is zero.Hateehc is a famous Martian blogger who often flies to different corners of the solar system. To release interesting videos even faster, he decided to buy himself a starship. When he came to the store, he saw starships with numbers from \(l\) to \(r\) inclusively. While in the store, Hateehc wanted to find a starship with the luckiest number.Since there are a lot of starships in the store, and Hateehc can't program, you have to help the blogger and write a program that answers his question.
|
The first line contains an integer \(t\) (\(1 \le t \le 10\,000\)) βthe number of test cases.Each of the following \(t\) lines contains a description of the test case. The description consists of two integers \(l\) and \(r\) (\(1 \le l \le r \le 10^6\)) β the largest and smallest numbers of the starships in the store.
|
Print \(t\) lines, one line for each test case, containing the luckiest starship number in the store.If there are several ways to choose the luckiest number, output any of them.
|
Let's look at two test examples: the luckiness of the number \(59\) is \(9 - 5 = 4\); the luckiness of \(60\) equals \(6 - 0 = 6\); the luckiness of \(61\) equals \(6 - 1 = 5\); the luckiness of \(62\) equals \(6 - 2 = 4\); the luckiness of \(63\) is \(6 - 3 = 3\). Thus, the luckiest number is \(60\).In the fifth test example, the luckiest number is \(90\).
|
Input: 559 6342 4915 1553 571 100 | Output: 60 49 15 57 90
|
Beginner
| 2 | 1,010 | 319 | 177 | 18 |
961 |
C
|
961C
|
C. Chessboard
| 1,400 |
bitmasks; brute force; implementation
|
Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color ak, i, j; 1 being black and 0 being white. Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.
|
The first line contains odd integer n (1 β€ n β€ 100) β the size of all pieces of the board. Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.
|
Print one number β minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.
|
Input: 10010 | Output: 1
|
Easy
| 3 | 695 | 322 | 107 | 9 |
|
470 |
C
|
470C
|
C. Eval
| 1,900 |
*special
|
You are given a simple arithmetic expression of the form a?b, where a and b are integer constants, and ? can be one of the following operations: '+' (addition), '-' (subtraction), '*' (multiplication), '/' (integer division) or '%' (modulo operation).Output the result of evaluation of this expression.
|
The input is a single line containing an expression a?b. Here a and b are integers between 1 and 999, inclusive; ? is an operation character: '+', '-' (ASCII code 45), '*', '/' or '%'.
|
Output a single integer β the result of evaluation of this expression.
|
Input: 123+456 | Output: 579
|
Hard
| 1 | 302 | 184 | 70 | 4 |
|
1,221 |
G
|
1221G
|
G. Graph And Numbers
| 2,900 |
bitmasks; brute force; combinatorics; dp; meet-in-the-middle
|
You are given an undirected graph with \(n\) vertices and \(m\) edges. You have to write a number on each vertex of this graph, each number should be either \(0\) or \(1\). After that, you write a number on each edge equal to the sum of numbers on vertices incident to that edge.You have to choose the numbers you will write on the vertices so that there is at least one edge with \(0\) written on it, at least one edge with \(1\) and at least one edge with \(2\). How many ways are there to do it? Two ways to choose numbers are different if there exists at least one vertex which has different numbers written on it in these two ways.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 40\), \(0 \le m \le \frac{n(n - 1)}{2}\)) β the number of vertices and the number of edges, respectively.Then \(m\) lines follow, each line contains two numbers \(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. It is guaranteed that each pair of vertices is connected by at most one edge.
|
Print one integer β the number of ways to write numbers on all vertices so that there exists at least one edge with \(0\) written on it, at least one edge with \(1\) and at least one edge with \(2\).
|
Input: 6 5 1 2 2 3 3 4 4 5 5 1 | Output: 20
|
Master
| 5 | 636 | 408 | 199 | 12 |
|
1,555 |
F
|
1555F
|
F. Good Graph
| 2,700 |
data structures; dsu; graphs; trees
|
You have an undirected graph consisting of \(n\) vertices with weighted edges.A simple cycle is a cycle of the graph without repeated vertices. Let the weight of the cycle be the XOR of weights of edges it consists of.Let's say the graph is good if all its simple cycles have weight \(1\). A graph is bad if it's not good.Initially, the graph is empty. Then \(q\) queries follow. Each query has the next type: \(u\) \(v\) \(x\) β add edge between vertices \(u\) and \(v\) of weight \(x\) if it doesn't make the graph bad. For each query print, was the edge added or not.
|
The first line contains two integers \(n\) and \(q\) (\(3 \le n \le 3 \cdot 10^5\); \(1 \le q \le 5 \cdot 10^5\)) β the number of vertices and queries.Next \(q\) lines contain queries β one per line. Each query contains three integers \(u\), \(v\) and \(x\) (\(1 \le u, v \le n\); \(u \neq v\); \(0 \le x \le 1\)) β the vertices of the edge and its weight.It's guaranteed that there are no multiple edges in the input.
|
For each query, print YES if the edge was added to the graph, or NO otherwise (both case-insensitive).
|
Input: 9 12 6 1 0 1 3 1 3 6 0 6 2 0 6 4 1 3 4 1 2 4 0 2 5 0 4 5 0 7 8 1 8 9 1 9 7 0 | Output: YES YES YES YES YES NO YES YES NO YES YES NO
|
Master
| 4 | 570 | 418 | 102 | 15 |
|
1,687 |
C
|
1687C
|
C. Sanae and Giant Robot
| 2,500 |
binary search; brute force; data structures; dsu; greedy; sortings
|
Is it really?! The robot only existing in my imagination?! The Colossal Walking Robot?!!β Kochiya Sanae Sanae made a giant robot β Hisoutensoku, but something is wrong with it. To make matters worse, Sanae can not figure out how to stop it, and she is forced to fix it on-the-fly.The state of a robot can be represented by an array of integers of length \(n\). Initially, the robot is at state \(a\). She wishes to turn it into state \(b\). As a great programmer, Sanae knows the art of copy-and-paste. In one operation, she can choose some segment from given segments, copy the segment from \(b\) and paste it into the same place of the robot, replacing the original state there. However, she has to ensure that the sum of \(a\) does not change after each copy operation in case the robot go haywire. Formally, Sanae can choose segment \([l,r]\) and assign \(a_i = b_i\) (\(l\le i\le r\)) if \(\sum\limits_{i=1}^n a_i\) does not change after the operation.Determine whether it is possible for Sanae to successfully turn the robot from the initial state \(a\) to the desired state \(b\) with any (possibly, zero) operations.
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 2\cdot 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers \(n\), \(m\) (\(2 \leq n\leq 2\cdot 10^5\), \(1 \leq m\leq 2\cdot 10^5\)) β the length of \(a\), \(b\) and the number of segments.The second line contains \(n\) intergers \(a_1,a_2,\ldots,a_n\) (\(1 \leq a_i \leq 10^9\)) β the initial state \(a\).The third line contains \(n\) intergers \(b_1,b_2,\ldots,b_n\) (\(1 \leq b_i \leq 10^9\)) β the desired state \(b\).Then \(m\) lines follow, the \(i\)-th line contains two intergers \(l_i,r_i\) (\(1 \leq l_i < r_i \leq n\)) β the segments that can be copy-pasted by Sanae.It is guaranteed that both the sum of \(n\) and the sum of \(m\) over all test cases does not exceed \(2 \cdot 10 ^ 5\).
|
For each test case, print ""YES"" (without quotes) if \(a\) can be turned into \(b\), or ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"" and ""Yes"" will be recognized as a positive response).
|
Test case 1:One possible way of turning \(a\) to \(b\):First, select \([1,3]\). After the operation, \(a=[3,2,5,2,3]\).Then, select \([2,5]\). After the operation, \(a=[3,2,5,4,1]=b\).Test case 2:It can be shown that it is impossible to turn \(a\) into \(b\).
|
Input: 25 21 5 4 2 33 2 5 4 11 32 55 21 5 4 2 33 2 4 5 11 22 4 | Output: YES NO
|
Expert
| 6 | 1,124 | 883 | 263 | 16 |
379 |
G
|
379G
|
G. New Year Cactus
| 3,100 |
dp
|
Jack and Jill are tired of the New Year tree, now they've got a New Year cactus at home! A cactus is a connected undirected graph where any two simple cycles have at most one common vertex. In other words, this graph doesn't have any edges that lie on more than one simple cycle.On the 31st of December they are going to decorate the cactus by hanging toys to its vertices. At most one toy is going to hang on each vertex β it's either the toy Jack hung or the toy Jill hung. It's possible for a vertex to have no toys.Jack and Jill has been arguing, so they don't want any edge to connect two vertices where one vertex has Jack's toy and the other vertex has Jill's toy.Jack has decided to hang a toys. What maximum number of toys b can Jill hang if they both cooperate to maximize this value? Your task is to write a program that finds the sought b for all a from 0 to the number of vertices on the New Year Cactus.
|
The first line contains two integers n and m (1 β€ n β€ 2500, n - 1 β€ m) β the number of vertices and the number of edges, correspondingly. The next m lines contain two integers a, b each (1 β€ a, b β€ n, a β b) that mean that there is an edge connecting vertices a ΠΈ b. Any pair of vertices has at most one edge between them.
|
The first line must contain space-separated ba (for all 0 β€ a β€ n) where ba equals the maximum number of Jill's toys on the cactus considering that it has a Jack's toys. Numbers ba go in the order of increasing a.
|
The cactus from the second example is:
|
Input: 1 0 | Output: 1 0
|
Master
| 1 | 917 | 322 | 213 | 3 |
625 |
D
|
625D
|
D. Finals in arithmetic
| 2,400 |
constructive algorithms; implementation; math
|
Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.
|
The first line of the input contains a single integer n (1 β€ n β€ 10100 000).
|
If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.
|
In the first sample 4 = 2 + 2, a = 2 is the only possibility.In the second sample 11 = 10 + 1, a = 10 β the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.It's easy to check that there is no suitable a in the third sample.In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for a: a = 30, a = 12, a = 21. Any of these is considered to be correct answer.
|
Input: 4 | Output: 2
|
Expert
| 3 | 1,080 | 76 | 186 | 6 |
199 |
A
|
199A
|
A. Hexadecimal's theorem
| 900 |
brute force; constructive algorithms; implementation; number theory
|
Recently, a chaotic virus Hexadecimal advanced a new theorem which will shake the Universe. She thinks that each Fibonacci number can be represented as sum of three not necessary different Fibonacci numbers.Let's remember how Fibonacci numbers can be calculated. F0 = 0, F1 = 1, and all the next numbers are Fi = Fi - 2 + Fi - 1.So, Fibonacci numbers make a sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, ...If you haven't run away from the PC in fear, you have to help the virus. Your task is to divide given Fibonacci number n by three not necessary different Fibonacci numbers or say that it is impossible.
|
The input contains of a single integer n (0 β€ n < 109) β the number that should be represented by the rules described above. It is guaranteed that n is a Fibonacci number.
|
Output three required numbers: a, b and c. If there is no answer for the test you have to print ""I'm too stupid to solve this problem"" without the quotes.If there are multiple answers, print any of them.
|
Input: 3 | Output: 1 1 1
|
Beginner
| 4 | 608 | 171 | 205 | 1 |
|
1,172 |
A
|
1172A
|
A. Nauuo and Cards
| 1,800 |
greedy; implementation
|
Nauuo is a girl who loves playing cards.One day she was playing cards but found that the cards were mixed with some empty ones.There are \(n\) cards numbered from \(1\) to \(n\), and they were mixed with another \(n\) empty cards. She piled up the \(2n\) cards and drew \(n\) of them. The \(n\) cards in Nauuo's hands are given. The remaining \(n\) cards in the pile are also given in the order from top to bottom.In one operation she can choose a card in her hands and play it β put it at the bottom of the pile, then draw the top card from the pile.Nauuo wants to make the \(n\) numbered cards piled up in increasing order (the \(i\)-th card in the pile from top to bottom is the card \(i\)) as quickly as possible. Can you tell her the minimum number of operations?
|
The first line contains a single integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β the number of numbered cards.The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(0\le a_i\le n\)) β the initial cards in Nauuo's hands. \(0\) represents an empty card.The third line contains \(n\) integers \(b_1,b_2,\ldots,b_n\) (\(0\le b_i\le n\)) β the initial cards in the pile, given in order from top to bottom. \(0\) represents an empty card.It is guaranteed that each number from \(1\) to \(n\) appears exactly once, either in \(a_{1..n}\) or \(b_{1..n}\).
|
The output contains a single integer β the minimum number of operations to make the \(n\) numbered cards piled up in increasing order.
|
Example 1We can play the card \(2\) and draw the card \(3\) in the first operation. After that, we have \([0,3,0]\) in hands and the cards in the pile are \([0,1,2]\) from top to bottom.Then, we play the card \(3\) in the second operation. The cards in the pile are \([1,2,3]\), in which the cards are piled up in increasing order.Example 2Play an empty card and draw the card \(1\), then play \(1\), \(2\), \(3\) in order.
|
Input: 3 0 2 0 3 0 1 | Output: 2
|
Medium
| 2 | 768 | 554 | 134 | 11 |
685 |
D
|
685D
|
D. Kay and Eternity
| 2,600 |
brute force; implementation; sortings
|
Snow Queen told Kay to form a word ""eternity"" using pieces of ice. Kay is eager to deal with the task, because he will then become free, and Snow Queen will give him all the world and a pair of skates.Behind the palace of the Snow Queen there is an infinite field consisting of cells. There are n pieces of ice spread over the field, each piece occupying exactly one cell and no two pieces occupying the same cell. To estimate the difficulty of the task Kay looks at some squares of size k Γ k cells, with corners located at the corners of the cells and sides parallel to coordinate axis and counts the number of pieces of the ice inside them.This method gives an estimation of the difficulty of some part of the field. However, Kay also wants to estimate the total difficulty, so he came up with the following criteria: for each x (1 β€ x β€ n) he wants to count the number of squares of size k Γ k, such that there are exactly x pieces of the ice inside.Please, help Kay estimate the difficulty of the task given by the Snow Queen.
|
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 1 β€ k β€ 300) β the number of pieces of the ice and the value k, respectively. Each of the next n lines contains two integers xi and yi ( - 109 β€ xi, yi β€ 109) β coordinates of the cell containing i-th piece of the ice. It's guaranteed, that no two pieces of the ice occupy the same cell.
|
Print n integers: the number of squares of size k Γ k containing exactly 1, 2, ..., n pieces of the ice.
|
Input: 5 34 54 65 55 67 7 | Output: 10 8 1 4 0
|
Expert
| 3 | 1,033 | 363 | 104 | 6 |
|
455 |
E
|
455E
|
E. Function
| 2,900 |
data structures
|
Serega and Fedor play with functions. One day they came across a very interesting function. It looks like that: f(1, j) = a[j], 1 β€ j β€ n. f(i, j) = min(f(i - 1, j), f(i - 1, j - 1)) + a[j], 2 β€ i β€ n, i β€ j β€ n. Here a is an integer array of length n.Serega and Fedya want to know what values this function takes at some points. But they don't want to calculate the values manually. So they ask you to help them.
|
The first line contains integer n (1 β€ n β€ 105) β the length of array a. The next line contains n integers: a[1], a[2], ..., a[n] (0 β€ a[i] β€ 104).The next line contains integer m (1 β€ m β€ 105) β the number of queries. Each of the next m lines contains two integers: xi, yi (1 β€ xi β€ yi β€ n). Each line means that Fedor and Serega want to know the value of f(xi, yi).
|
Print m lines β the answers to the guys' queries.
|
Input: 62 2 3 4 3 444 53 43 42 3 | Output: 12995
|
Master
| 1 | 413 | 367 | 49 | 4 |
|
1,676 |
F
|
1676F
|
F. Longest Strike
| 1,300 |
data structures; greedy; implementation; sortings; two pointers
|
Given an array \(a\) of length \(n\) and an integer \(k\), you are tasked to find any two numbers \(l\) and \(r\) (\(l \leq r\)) such that: For each \(x\) \((l \leq x \leq r)\), \(x\) appears in \(a\) at least \(k\) times (i.e. \(k\) or more array elements are equal to \(x\)). The value \(r-l\) is maximized. If no numbers satisfy the conditions, output -1.For example, if \(a=[11, 11, 12, 13, 13, 14, 14]\) and \(k=2\), then: for \(l=12\), \(r=14\) the first condition fails because \(12\) does not appear at least \(k=2\) times. for \(l=13\), \(r=14\) the first condition holds, because \(13\) occurs at least \(k=2\) times in \(a\) and \(14\) occurs at least \(k=2\) times in \(a\). for \(l=11\), \(r=11\) the first condition holds, because \(11\) occurs at least \(k=2\) times in \(a\). A pair of \(l\) and \(r\) for which the first condition holds and \(r-l\) is maximal is \(l = 13\), \(r = 14\).
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains the integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \leq k \leq n\)) β the length of the array \(a\) and the minimum amount of times each number in the range \([l, r]\) should appear respectively.Then a single line follows, containing \(n\) integers describing the array \(a\) (\(1 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output \(2\) numbers, \(l\) and \(r\) that satisfy the conditions, or ""-1"" if no numbers satisfy the conditions.If multiple answers exist, you can output any.
|
Input: 47 211 11 12 13 13 14 145 16 3 5 2 16 44 3 4 3 3 414 21 1 2 2 2 3 3 3 3 4 4 4 4 4 | Output: 13 14 1 3 -1 1 4
|
Easy
| 5 | 903 | 599 | 179 | 16 |
|
801 |
B
|
801B
|
B. Valued Keys
| 900 |
constructive algorithms; greedy; strings
|
You found a mysterious function f. The function takes two strings s1 and s2. These strings must consist only of lowercase English letters, and must be the same length.The output of the function f is another string of the same length. The i-th character of the output is equal to the minimum of the i-th character of s1 and the i-th character of s2.For example, f(""ab"", ""ba"") = ""aa"", and f(""nzwzl"", ""zizez"") = ""niwel"".You found two strings x and y of the same length and consisting of only lowercase English letters. Find any string z such that f(x, z) = y, or print -1 if no such string z exists.
|
The first line of input contains the string x.The second line of input contains the string y.Both x and y consist only of lowercase English letters, x and y have same length and this length is between 1 and 100.
|
If there is no string z such that f(x, z) = y, print -1.Otherwise, print a string z such that f(x, z) = y. If there are multiple possible answers, print any of them. The string z should be the same length as x and y and consist only of lowercase English letters.
|
The first case is from the statement.Another solution for the second case is ""zizez""There is no solution for the third case. That is, there is no z such that f(""ab"", z) = ""ba"".
|
Input: abaa | Output: ba
|
Beginner
| 3 | 608 | 211 | 262 | 8 |
750 |
G
|
750G
|
G. New Year and Binary Tree Paths
| 3,200 |
bitmasks; brute force; combinatorics; dp
|
The New Year tree is an infinite perfect binary tree rooted in the node 1. Each node v has two children: nodes indexed (2Β·v) and (2Β·v + 1). Polar bears love decorating the New Year tree and Limak is no exception. As he is only a little bear, he was told to decorate only one simple path between some pair of nodes. Though he was given an opportunity to pick the pair himself! Now he wants to know the number of unordered pairs of indices (u, v) (u β€ v), such that the sum of indices of all nodes along the simple path between u and v (including endpoints) is equal to s. Can you help him and count this value?
|
The only line of the input contains a single integer s (1 β€ s β€ 1015).
|
Print one integer, denoting the number of unordered pairs of nodes indices defining simple paths with the sum of indices of vertices equal to s.
|
In sample test, there are 4 paths with the sum of indices equal to 10:
|
Input: 10 | Output: 4
|
Master
| 4 | 609 | 70 | 144 | 7 |
1,712 |
E2
|
1712E2
|
E2. LCM Sum (hard version)
| 2,500 |
brute force; data structures; math; number theory; two pointers
|
We are sum for we are manySome NumberThis version of the problem differs from the previous one only in the constraint on \(t\). You can make hacks only if both versions of the problem are solved.You are given two positive integers \(l\) and \(r\).Count the number of distinct triplets of integers \((i, j, k)\) such that \(l \le i < j < k \le r\) and \(\operatorname{lcm}(i,j,k) \ge i + j + k\).Here \(\operatorname{lcm}(i, j, k)\) denotes the least common multiple (LCM) of integers \(i\), \(j\), and \(k\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(\bf{1 \le t \le 10^5}\)). Description of the test cases follows.The only line for each test case contains two integers \(l\) and \(r\) (\(1 \le l \le r \le 2 \cdot 10^5\), \(l + 2 \le r\)).
|
For each test case print one integer β the number of suitable triplets.
|
In the first test case, there are \(3\) suitable triplets: \((1,2,3)\), \((1,3,4)\), \((2,3,4)\). In the second test case, there is \(1\) suitable triplet: \((3,4,5)\).
|
Input: 51 43 58 8668 866 86868 | Output: 3 1 78975 969 109229059713337
|
Expert
| 5 | 508 | 287 | 71 | 17 |
1,934 |
E
|
1934E
|
E. Weird LCM Operations
| 3,000 |
brute force; constructive algorithms; number theory
|
Given an integer \(n\), you construct an array \(a\) of \(n\) integers, where \(a_i = i\) for all integers \(i\) in the range \([1, n]\). An operation on this array is defined as follows: Select three distinct indices \(i\), \(j\), and \(k\) from the array, and let \(x = a_i\), \(y = a_j\), and \(z = a_k\). Update the array as follows: \(a_i = \operatorname{lcm}(y, z)\), \(a_j = \operatorname{lcm}(x, z)\), and \(a_k = \operatorname{lcm}(x, y)\), where \(\operatorname{lcm}\) represents the least common multiple. Your task is to provide a possible sequence of operations, containing at most \(\lfloor \frac{n}{6} \rfloor + 5\) operations such that after executing these operations, if you create a set containing the greatest common divisors (GCDs) of all subsequences with a size greater than \(1\), then all numbers from \(1\) to \(n\) should be present in this set.After all the operations \(a_i \le 10^{18}\) should hold for all \(1 \le i \le n\).We can show that an answer always exists.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^2\)) β the number of test cases. The description of the test cases follows.The first and only line of each test case contains an integer \(n\) (\(3 \leq n \leq 3 \cdot 10^{4}\)) β the length of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^{4}\).
|
The first line should contain an integer \(k\) (\(0 \leq k \leq \lfloor \frac{n}{6} \rfloor + 5\)) β where \(k\) is the number of operations.The next \(k\) lines should contain the description of each operation i.e. \(3\) integers \(i\), \(j\) and \(k\), where \(1 \leq i, j, k \leq n\) and all must be distinct.
|
In the third test case, \(a = [1, 2, 3, 4, 5, 6, 7]\).First operation:\(i = 3\), \(j = 5\), \(k = 7\)\(x = 3\), \(y = 5\), \(z = 7\).\(a = [1, 2, \operatorname{lcm}(y,z), 4, \operatorname{lcm}(x,z), 6, \operatorname{lcm}(x,y)]\) = \([1, 2, \color{red}{35}, 4, \color{red}{21}, 6, \color{red}{15}]\).Second operation:\(i = 5\), \(j = 6\), \(k = 7\)\(x = 21\), \(y = 6\), \(z = 15\).\(a = [1, 2, 35, 4, \operatorname{lcm}(y,z), \operatorname{lcm}(x,z), \operatorname{lcm}(x,y)]\) = \([1, 2, 35, 4, \color{red}{30}, \color{red}{105}, \color{red}{42}]\).Third operation:\(i = 2\), \(j = 3\), \(k = 4\)\(x = 2\), \(y = 35\), \(z = 4\).\(a = [1, \operatorname{lcm}(y,z), \operatorname{lcm}(x,z), \operatorname{lcm}(x,y), 30, 105, 42]\) = \([1, \color{red}{140}, \color{red}{4}, \color{red}{70}, 30, 105, 42]\).Subsequences whose GCD equal to \(i\) is as follows:\(\gcd(a_1, a_2) = \gcd(1, 140) = 1\)\(\gcd(a_3, a_4) = \gcd(4, 70) = 2\)\(\gcd(a_5, a_6, a_7) = \gcd(30, 105, 42) = 3\)\(\gcd(a_2, a_3) = \gcd(140, 4) = 4\)\(\gcd(a_2, a_4, a_5, a_6) = \gcd(140, 70, 30, 105) = 5\)\(\gcd(a_5, a_7) = \gcd(30, 42) = 6\)\(\gcd(a_2, a_4, a_6, a_7) = \gcd(140, 70, 105, 42) = 7\)
|
Input: 3347 | Output: 1 1 2 3 1 1 3 4 3 3 5 7 5 6 7 2 3 4
|
Master
| 3 | 996 | 358 | 312 | 19 |
1,585 |
A
|
1585A
|
A. Life of a Flower
| 800 |
implementation
|
Petya has got an interesting flower. Petya is a busy person, so he sometimes forgets to water it. You are given \(n\) days from Petya's live and you have to determine what happened with his flower in the end.The flower grows as follows: If the flower isn't watered for two days in a row, it dies. If the flower is watered in the \(i\)-th day, it grows by \(1\) centimeter. If the flower is watered in the \(i\)-th and in the \((i-1)\)-th day (\(i > 1\)), then it grows by \(5\) centimeters instead of \(1\). If the flower is not watered in the \(i\)-th day, it does not grow. At the beginning of the \(1\)-st day the flower is \(1\) centimeter tall. What is its height after \(n\) days?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains the only integer \(n\) (\(1 \leq n \leq 100\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(a_i = 0\) or \(a_i = 1\)). If \(a_i = 1\), the flower is watered in the \(i\)-th day, otherwise it is not watered.
|
For each test case print a single integer \(k\) β the flower's height after \(n\) days, or \(-1\), if the flower dies.
|
Input: 4 3 1 0 1 3 0 1 1 4 1 0 0 1 1 0 | Output: 3 7 -1 1
|
Beginner
| 1 | 686 | 445 | 118 | 15 |
|
1,974 |
B
|
1974B
|
B. Symmetric Encoding
| 800 |
implementation; sortings; strings
|
Polycarp has a string \(s\), which consists of lowercase Latin letters. He encodes this string using the following algorithm: first, he constructs a new auxiliary string \(r\), which consists of all distinct letters of the string \(s\), written in alphabetical order; then the encoding happens as follows: each character in the string \(s\) is replaced by its symmetric character from the string \(r\) (the first character of the string \(r\) will be replaced by the last, the second by the second from the end, and so on). For example, encoding the string \(s\)=""codeforces"" happens as follows: the string \(r\) is obtained as ""cdefors""; the first character \(s_1\)='c' is replaced by 's'; the second character \(s_2\)='o' is replaced by 'e'; the third character \(s_3\)='d' is replaced by 'r'; ... the last character \(s_{10}\)='s' is replaced by 'c'. The string \(r\) and replacements for \(s\)=""codeforces"". Thus, the result of encoding the string \(s\)=""codeforces"" is the string ""serofedsoc"".Write a program that performs decoding β that is, restores the original string \(s\) from the encoding result.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the string \(b\).The second line of each test case contains a string \(b\) of length \(n\), consisting of lowercase Latin letters β the result of encoding the original string \(s\).It is guaranteed that the sum of the values of \(n\) over all test cases in the test does not exceed \(2 \cdot 10^5\).
|
For each test case, output the string \(s\) from which the encoding result \(b\) was obtained.
|
Input: 510serofedsoc3ttf9tlrhgmaoi1w15hnndledmnhlttin | Output: codeforces fft algorithm w meetinthemiddle
|
Beginner
| 3 | 1,118 | 508 | 94 | 19 |
|
1,070 |
E
|
1070E
|
E. Getting Deals Done
| 2,100 |
binary search; data structures
|
Polycarp has a lot of work to do. Recently he has learned a new time management rule: ""if a task takes five minutes or less, do it immediately"". Polycarp likes the new rule, however he is not sure that five minutes is the optimal value. He supposes that this value \(d\) should be chosen based on existing task list.Polycarp has a list of \(n\) tasks to complete. The \(i\)-th task has difficulty \(p_i\), i.e. it requires exactly \(p_i\) minutes to be done. Polycarp reads the tasks one by one from the first to the \(n\)-th. If a task difficulty is \(d\) or less, Polycarp starts the work on the task immediately. If a task difficulty is strictly greater than \(d\), he will not do the task at all. It is not allowed to rearrange tasks in the list. Polycarp doesn't spend any time for reading a task or skipping it.Polycarp has \(t\) minutes in total to complete maximum number of tasks. But he does not want to work all the time. He decides to make a break after each group of \(m\) consecutive tasks he was working on. The break should take the same amount of time as it was spent in total on completion of these \(m\) tasks.For example, if \(n=7\), \(p=[3, 1, 4, 1, 5, 9, 2]\), \(d=3\) and \(m=2\) Polycarp works by the following schedule: Polycarp reads the first task, its difficulty is not greater than \(d\) (\(p_1=3 \le d=3\)) and works for \(3\) minutes (i.e. the minutes \(1\), \(2\), \(3\)); Polycarp reads the second task, its difficulty is not greater than \(d\) (\(p_2=1 \le d=3\)) and works for \(1\) minute (i.e. the minute \(4\)); Polycarp notices that he has finished \(m=2\) tasks and takes a break for \(3+1=4\) minutes (i.e. on the minutes \(5, 6, 7, 8\)); Polycarp reads the third task, its difficulty is greater than \(d\) (\(p_3=4 > d=3\)) and skips it without spending any time; Polycarp reads the fourth task, its difficulty is not greater than \(d\) (\(p_4=1 \le d=3\)) and works for \(1\) minute (i.e. the minute \(9\)); Polycarp reads the tasks \(5\) and \(6\), skips both of them (\(p_5>d\) and \(p_6>d\)); Polycarp reads the \(7\)-th task, its difficulty is not greater than \(d\) (\(p_7=2 \le d=3\)) and works for \(2\) minutes (i.e. the minutes \(10\), \(11\)); Polycarp notices that he has finished \(m=2\) tasks and takes a break for \(1+2=3\) minutes (i.e. on the minutes \(12, 13, 14\)). Polycarp stops exactly after \(t\) minutes. If Polycarp started a task but has not finished it by that time, the task is not considered as completed. It is allowed to complete less than \(m\) tasks in the last group. Also Polycarp considers acceptable to have shorter break than needed after the last group of tasks or even not to have this break at all β his working day is over and he will have enough time to rest anyway.Please help Polycarp to find such value \(d\), which would allow him to complete maximum possible number of tasks in \(t\) minutes.
|
The first line of the input contains single integer \(c\) (\(1 \le c \le 5 \cdot 10^4\)) β number of test cases. Then description of \(c\) test cases follows. Solve test cases separately, test cases are completely independent and do not affect each other.Each test case is described by two lines. The first of these lines contains three space-separated integers \(n\), \(m\) and \(t\) (\(1 \le n \le 2 \cdot 10^5, 1 \le m \le 2 \cdot 10^5, 1 \le t \le 4 \cdot 10^{10}\)) β the number of tasks in Polycarp's list, the number of tasks he can do without a break and the total amount of time Polycarp can work on tasks. The second line of the test case contains \(n\) space separated integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le 2 \cdot 10^5\)) β difficulties of the tasks.The sum of values \(n\) for all test cases in the input does not exceed \(2 \cdot 10^5\).
|
Print \(c\) lines, each line should contain answer for the corresponding test case β the maximum possible number of tasks Polycarp can complete and the integer value \(d\) (\(1 \le d \le t\)) Polycarp should use in time management rule, separated by space. If there are several possible values \(d\) for a test case, output any of them.
|
In the first test case of the first example \(n=5\), \(m=2\) and \(t=16\). The sequence of difficulties is \([5, 6, 1, 4, 7]\). If Polycarp chooses \(d=5\) then he will complete \(3\) tasks. Polycarp will work by the following schedule: Polycarp reads the first task, its difficulty is not greater than \(d\) (\(p_1=5 \le d=5\)) and works for \(5\) minutes (i.e. the minutes \(1, 2, \dots, 5\)); Polycarp reads the second task, its difficulty is greater than \(d\) (\(p_2=6 > d=5\)) and skips it without spending any time; Polycarp reads the third task, its difficulty is not greater than \(d\) (\(p_3=1 \le d=5\)) and works for \(1\) minute (i.e. the minute \(6\)); Polycarp notices that he has finished \(m=2\) tasks and takes a break for \(5+1=6\) minutes (i.e. on the minutes \(7, 8, \dots, 12\)); Polycarp reads the fourth task, its difficulty is not greater than \(d\) (\(p_4=4 \le d=5\)) and works for \(4\) minutes (i.e. the minutes \(13, 14, 15, 16\)); Polycarp stops work because of \(t=16\). In total in the first test case Polycarp will complete \(3\) tasks for \(d=5\). He can't choose other value for \(d\) to increase the number of completed tasks.
|
Input: 45 2 165 6 1 4 75 3 305 6 1 4 76 4 1512 5 15 7 20 171 1 50100 | Output: 3 54 72 100 25
|
Hard
| 2 | 2,884 | 864 | 336 | 10 |
706 |
B
|
706B
|
B. Interesting drink
| 1,100 |
binary search; dp; implementation
|
Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink ""Beecola"", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of ""Beecola"".
|
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of shops in the city that sell Vasiliy's favourite drink.The second line contains n integers xi (1 β€ xi β€ 100 000) β prices of the bottles of the drink in the i-th shop.The third line contains a single integer q (1 β€ q β€ 100 000) β the number of days Vasiliy plans to buy the drink.Then follow q lines each containing one integer mi (1 β€ mi β€ 109) β the number of coins Vasiliy can spent on the i-th day.
|
Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.
|
On the first day, Vasiliy won't be able to buy a drink in any of the shops.On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.On the third day, Vasiliy can buy a drink only in the shop number 1.Finally, on the last day Vasiliy can buy a drink in any shop.
|
Input: 53 10 8 6 114110311 | Output: 0415
|
Easy
| 3 | 513 | 491 | 146 | 7 |
369 |
A
|
369A
|
A. Valera and Plates
| 900 |
greedy; implementation
|
Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl. We know that Valera can cook only two types of dishes. He can eat dishes of the first type from bowls and dishes of the second type from either bowls or plates. When Valera finishes eating, he leaves a dirty plate/bowl behind. His life philosophy doesn't let him eat from dirty kitchenware. So sometimes he needs to wash his plate/bowl before eating. Find the minimum number of times Valera will need to wash a plate/bowl, if he acts optimally.
|
The first line of the input contains three integers n, m, k (1 β€ n, m, k β€ 1000) β the number of the planned days, the number of clean bowls and the number of clean plates.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 2). If ai equals one, then on day i Valera will eat a first type dish. If ai equals two, then on day i Valera will eat a second type dish.
|
Print a single integer β the minimum number of times Valera will need to wash a plate/bowl.
|
In the first sample Valera will wash a bowl only on the third day, so the answer is one.In the second sample, Valera will have the first type of the dish during all four days, and since there are only three bowls, he will wash a bowl exactly once.In the third sample, Valera will have the second type of dish for all three days, and as they can be eaten from either a plate or a bowl, he will never need to wash a plate/bowl.
|
Input: 3 1 11 2 1 | Output: 1
|
Beginner
| 2 | 695 | 372 | 91 | 3 |
821 |
A
|
821A
|
A. Okabe and Future Gadget Laboratory
| 800 |
implementation
|
Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 β€ x, y β€ n and ax, y β 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.Help Okabe determine whether a given lab is good!
|
The first line of input contains the integer n (1 β€ n β€ 50) β the size of the lab. The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 β€ ai, j β€ 105).
|
Print ""Yes"" if the given lab is good and ""No"" otherwise.You can output each letter in upper or lower case.
|
In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is ""Yes"".In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is ""No"".
|
Input: 31 1 22 3 16 4 1 | Output: Yes
|
Beginner
| 1 | 564 | 223 | 110 | 8 |
1,291 |
A
|
1291A
|
A. Even But Not Even
| 900 |
greedy; math; strings
|
Let's define a number ebne (even but not even) if and only if its sum of digits is divisible by \(2\) but the number itself is not divisible by \(2\). For example, \(13\), \(1227\), \(185217\) are ebne numbers, while \(12\), \(2\), \(177013\), \(265918\) are not. If you're still unsure what ebne numbers are, you can look at the sample notes for more clarification.You are given a non-negative integer \(s\), consisting of \(n\) digits. You can delete some digits (they are not necessary consecutive/successive) to make the given number ebne. You cannot change the order of the digits, that is, after deleting the digits the remaining digits collapse. The resulting number shouldn't contain leading zeros. You can delete any number of digits between \(0\) (do not delete any digits at all) and \(n-1\).For example, if you are given \(s=\)222373204424185217171912 then one of possible ways to make it ebne is: 222373204424185217171912 \(\rightarrow\) 2237344218521717191. The sum of digits of 2237344218521717191 is equal to \(70\) and is divisible by \(2\), but number itself is not divisible by \(2\): it means that the resulting number is ebne.Find any resulting number that is ebne. If it's impossible to create an ebne number from the given number report about it.
|
The input consists of multiple test cases. The first line 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\) (\(1 \le n \le 3000\)) β the number of digits in the original number.The second line of each test case contains a non-negative integer number \(s\), consisting of \(n\) digits.It is guaranteed that \(s\) does not contain leading zeros and the sum of \(n\) over all test cases does not exceed \(3000\).
|
For each test case given in the input print the answer in the following format: If it is impossible to create an ebne number, print ""-1"" (without quotes); Otherwise, print the resulting number after deleting some, possibly zero, but not all digits. This number should be ebne. If there are multiple answers, you can print any of them. Note that answers with leading zeros or empty strings are not accepted. It's not necessary to minimize or maximize the number of deleted digits.
|
In the first test case of the example, \(1227\) is already an ebne number (as \(1 + 2 + 2 + 7 = 12\), \(12\) is divisible by \(2\), while in the same time, \(1227\) is not divisible by \(2\)) so we don't need to delete any digits. Answers such as \(127\) and \(17\) will also be accepted.In the second test case of the example, it is clearly impossible to create an ebne number from the given number.In the third test case of the example, there are many ebne numbers we can obtain by deleting, for example, \(1\) digit such as \(17703\), \(77013\) or \(17013\). Answers such as \(1701\) or \(770\) will not be accepted as they are not ebne numbers. Answer \(013\) will not be accepted as it contains leading zeroes.Explanation: \(1 + 7 + 7 + 0 + 3 = 18\). As \(18\) is divisible by \(2\) while \(17703\) is not divisible by \(2\), we can see that \(17703\) is an ebne number. Same with \(77013\) and \(17013\); \(1 + 7 + 0 + 1 = 9\). Because \(9\) is not divisible by \(2\), \(1701\) is not an ebne number; \(7 + 7 + 0 = 14\). This time, \(14\) is divisible by \(2\) but \(770\) is also divisible by \(2\), therefore, \(770\) is not an ebne number.In the last test case of the example, one of many other possible answers is given. Another possible answer is: 222373204424185217171912 \(\rightarrow\) 22237320442418521717191 (delete the last digit).
|
Input: 4 4 1227 1 0 6 177013 24 222373204424185217171912 | Output: 1227 -1 17703 2237344218521717191
|
Beginner
| 3 | 1,269 | 549 | 481 | 12 |
989 |
E
|
989E
|
E. A Trance of Nightfall
| 2,700 |
dp; geometry; matrices; probabilities
|
The cool breeze blows gently, the flowing water ripples steadily.""Flowing and passing like this, the water isn't gone ultimately; Waxing and waning like that, the moon doesn't shrink or grow eventually.""""Everything is transient in a way and perennial in another.""Kanno doesn't seem to make much sense out of Mino's isolated words, but maybe it's time that they enjoy the gentle breeze and the night sky β the inexhaustible gifts from nature.Gazing into the sky of stars, Kanno indulges in a night's tranquil dreams. There is a set \(S\) of \(n\) points on a coordinate plane.Kanno starts from a point \(P\) that can be chosen on the plane. \(P\) is not added to \(S\) if it doesn't belong to \(S\). Then the following sequence of operations (altogether called a move) is repeated several times, in the given order: Choose a line \(l\) such that it passes through at least two elements in \(S\) and passes through Kanno's current position. If there are multiple such lines, one is chosen equiprobably. Move to one of the points that belong to \(S\) and lie on \(l\). The destination is chosen equiprobably among all possible ones, including Kanno's current position (if it does belong to \(S\)). There are \(q\) queries each consisting of two integers \((t_i, m_i)\). For each query, you're to help Kanno maximize the probability of the stopping position being the \(t_i\)-th element in \(S\) after \(m_i\) moves with a proper selection of \(P\), and output this maximum probability. Note that according to rule 1, \(P\) should belong to at least one line that passes through at least two points from \(S\).
|
The first line contains a positive integer \(n\) (\(2 \leq n \leq 200\)) β the number of points in \(S\).The \(i\)-th of the following \(n\) lines contains two space-separated integers \(x_i\) and \(y_i\) (\(-10^4 \leq x_i, y_i \leq 10^4\)) β the coordinates of the \(i\)-th point in \(S\). The input guarantees that for all \(1 \leq i \lt j \leq n\), \((x_i, y_i) \neq (x_j, y_j)\) holds.The next line contains a positive integer \(q\) (\(1 \leq q \leq 200\)) β the number of queries.Each of the following \(q\) lines contains two space-separated integers \(t\) and \(m\) (\(1 \leq t_i \leq n\), \(1 \leq m_i \leq 10^4\)) β the index of the target point and the number of moves, respectively.
|
Output \(q\) lines each containing a decimal number β the \(i\)-th among them denotes the maximum probability of staying on the \(t_i\)-th point after \(m_i\) steps, with a proper choice of starting position \(P\).Your answer will be considered correct if each number in your output differs from the corresponding one in jury's answer by at most \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is considered correct if \(|a - b| \le 10^{-6}\).
|
The points in \(S\) and possible candidates for line \(l\) are depicted in the following figure. For the first query, when \(P = (-1, -3)\), \(l\) is uniquely determined to be \(3x = y\), and thus Kanno will move to \((0, 0)\) with a probability of \(\frac 1 2\).For the third query, when \(P = (2, 2)\), \(l\) is chosen equiprobably between \(x + y = 4\) and \(x = y\). Kanno will then move to the other four points with a probability of \(\frac 1 2 \cdot \frac 1 3 = \frac 1 6\) each, or stay at \((2, 2)\) with a probability of \(\frac 1 3\).
|
Input: 50 01 32 23 14 4101 12 13 14 15 13 23 33 43 53 6 | Output: 0.500000000000000000000.500000000000000000000.333333333333333314830.500000000000000000000.500000000000000000000.185185185185185174910.152263374485596708620.144947416552354824140.143321648122745504140.14296036624949901017
|
Master
| 4 | 1,610 | 693 | 487 | 9 |
1,878 |
B
|
1878B
|
B. Aleksa and Stack
| 800 |
constructive algorithms; math
|
After the Serbian Informatics Olympiad, Aleksa was very sad, because he didn't win a medal (he didn't know stack), so Vasilije came to give him an easy problem, just to make his day better.Vasilije gave Aleksa a positive integer \(n\) (\(n \ge 3\)) and asked him to construct a strictly increasing array of size \(n\) of positive integers, such that \(3\cdot a_{i+2}\) is not divisible by \(a_i+a_{i+1}\) for each \(i\) (\(1\le i \le n-2\)). Note that a strictly increasing array \(a\) of size \(n\) is an array where \(a_i < a_{i+1}\) for each \(i\) (\(1 \le i \le n-1\)).Since Aleksa thinks he is a bad programmer now, he asked you to help him find such an array.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of elements in array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(n\) integers \(a_1, a_2, a_3, \dots, a_n\) (\(1 \le a_i \le 10^9\)).It can be proved that the solution exists for any \(n\). If there are multiple solutions, output any of them.
|
In the first test case, \(a_1=6\), \(a_2=8\), \(a_3=12\), so \(a_1+a_2=14\) and \(3 \cdot a_3=36\), so \(3 \cdot a_3\) is not divisible by \(a_1+a_2\).
|
Input: 3367 | Output: 6 8 12 7 11 14 20 22 100 9 15 18 27 36 90 120
|
Beginner
| 2 | 665 | 401 | 206 | 18 |
1,826 |
C
|
1826C
|
C. Dreaming of Freedom
| 1,300 |
greedy; math; number theory
|
Because to take away a man's freedom of choice, even his freedom to make the wrong choice, is to manipulate him as though he were a puppet and not a person.β Madeleine L'EngleThere are \(n\) programmers choosing their favorite algorithm amongst \(m\) different choice options. Before the first round, all \(m\) options are available. In each round, every programmer makes a vote for one of the remaining algorithms. After the round, only the algorithms with the maximum number of votes remain. The voting process ends when there is only one option left. Determine whether the voting process can continue indefinitely or no matter how people vote, they will eventually choose a single option after some finite amount of rounds?
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases.Each test case consists of a single line containing two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^6\)) β the number of people and choice options respectively.
|
For each test case output ""YES"" if the programmers will eventually choose a single option, and ""NO"" otherwise.You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as a positive answer).
|
In the first example, there are \(8\) ways people could vote: \(\{1|1|1, 1|1|2, 1|2|1, 1|2|2, 2|1|1, 2|1|2, 2|2|1, 2|2|2\}\).In cases \(1\), \(2\), \(3\), and \(5\), the programmers are left with the first algorithm, and in the remaining cases people are left with the second one, so the voting ends in one round in any case.In the second example, the programmers could always vote \(1|1|2|2\). Both algorithms have the maximum number of votes and remain for the next round, so the voting never ends.
|
Input: 53 24 25 31000000 10000001 1000000 | Output: YES NO YES NO YES
|
Easy
| 3 | 726 | 263 | 230 | 18 |
560 |
A
|
560A
|
A. Currency System in Geraldion
| 1,000 |
implementation; sortings
|
A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimum unfortunate sum?
|
The first line contains number n (1 β€ n β€ 1000) β the number of values of the banknotes that used in Geraldion. The second line contains n distinct space-separated numbers a1, a2, ..., an (1 β€ ai β€ 106) β the values of the banknotes.
|
Print a single line β the minimum unfortunate sum. If there are no unfortunate sums, print - 1.
|
Input: 51 2 3 4 5 | Output: -1
|
Beginner
| 2 | 415 | 233 | 95 | 5 |
|
1,499 |
A
|
1499A
|
A. Domino on Windowsill
| 800 |
combinatorics; constructive algorithms; math
|
You have a board represented as a grid with \(2 \times n\) cells.The first \(k_1\) cells on the first row and first \(k_2\) cells on the second row are colored in white. All other cells are colored in black.You have \(w\) white dominoes (\(2 \times 1\) tiles, both cells are colored in white) and \(b\) black dominoes (\(2 \times 1\) tiles, both cells are colored in black).You can place a white domino on the board if both board's cells are white and not occupied by any other domino. In the same way, you can place a black domino if both cells are black and not occupied by any other domino.Can you place all \(w + b\) dominoes on the board if you can place dominoes both horizontally and vertically?
|
The first line contains a single integer \(t\) (\(1 \le t \le 3000\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(k_1\) and \(k_2\) (\(1 \le n \le 1000\); \(0 \le k_1, k_2 \le n\)).The second line of each test case contains two integers \(w\) and \(b\) (\(0 \le w, b \le n\)).
|
For each test case, print YES if it's possible to place all \(w + b\) dominoes on the board and NO, otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
|
In the first test case, \(n = 1\), \(k_1 = 0\) and \(k_2 = 1\). It means that \(2 \times 1\) board has black cell \((1, 1)\) and white cell \((2, 1)\). So, you can't place any white domino, since there is only one white cell.In the second test case, the board of the same size \(2 \times 1\), but both cell are white. Since \(w = 0\) and \(b = 0\), so you can place \(0 + 0 = 0\) dominoes on the board.In the third test case, board \(2 \times 3\), but fully colored in black (since \(k_1 = k_2 = 0\)), so you can't place any white domino.In the fourth test case, cells \((1, 1)\), \((1, 2)\), \((1, 3)\), and \((2, 1)\) are white and other cells are black. You can place \(2\) white dominoes at positions \(((1, 1), (2, 1))\) and \(((1, 2), (1, 3))\) and \(2\) black dominoes at positions \(((1, 4), (2, 4))\) \(((2, 2), (2, 3))\).
|
Input: 5 1 0 1 1 0 1 1 1 0 0 3 0 0 1 3 4 3 1 2 2 5 4 3 3 1 | Output: NO YES NO YES YES
|
Beginner
| 3 | 702 | 325 | 249 | 14 |
2,073 |
L
|
2073L
| 1,300 |
Easy
| 0 | 0 | 0 | 0 | 20 |
|||||||
784 |
D
|
784D
|
D. Touchy-Feely Palindromes
| 1,900 |
*special; implementation
|
The only line of the input contains a string of digits. The length of the string is between 1 and 10, inclusive.
|
Output ""Yes"" or ""No"".
|
Input: 373 | Output: Yes
|
Hard
| 2 | 0 | 112 | 25 | 7 |
||
1,223 |
D
|
1223D
|
D. Sequence Sorting
| 2,000 |
dp; greedy; two pointers
|
You are given a sequence \(a_1, a_2, \dots, a_n\), consisting of integers.You can apply the following operation to this sequence: choose some integer \(x\) and move all elements equal to \(x\) either to the beginning, or to the end of \(a\). Note that you have to move all these elements in one direction in one operation.For example, if \(a = [2, 1, 3, 1, 1, 3, 2]\), you can get the following sequences in one operation (for convenience, denote elements equal to \(x\) as \(x\)-elements): \([1, 1, 1, 2, 3, 3, 2]\) if you move all \(1\)-elements to the beginning; \([2, 3, 3, 2, 1, 1, 1]\) if you move all \(1\)-elements to the end; \([2, 2, 1, 3, 1, 1, 3]\) if you move all \(2\)-elements to the beginning; \([1, 3, 1, 1, 3, 2, 2]\) if you move all \(2\)-elements to the end; \([3, 3, 2, 1, 1, 1, 2]\) if you move all \(3\)-elements to the beginning; \([2, 1, 1, 1, 2, 3, 3]\) if you move all \(3\)-elements to the end; You have to determine the minimum number of such operations so that the sequence \(a\) becomes sorted in non-descending order. Non-descending order means that for all \(i\) from \(2\) to \(n\), the condition \(a_{i-1} \le a_i\) is satisfied.Note that you have to answer \(q\) independent queries.
|
The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of the queries. Each query is represented by two consecutive lines.The first line of each query contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of elements.The second line of each query contains \(n\) integers \(a_1, a_2, \dots , a_n\) (\(1 \le a_i \le n\)) β the elements.It is guaranteed that the sum of all \(n\) does not exceed \(3 \cdot 10^5\).
|
For each query print one integer β the minimum number of operation for sorting sequence \(a\) in non-descending order.
|
In the first query, you can move all \(1\)-elements to the beginning (after that sequence turn into \([1, 1, 1, 3, 6, 6, 3]\)) and then move all \(6\)-elements to the end.In the second query, the sequence is sorted initially, so the answer is zero.In the third query, you have to move all \(2\)-elements to the beginning.
|
Input: 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 | Output: 2 0 1
|
Hard
| 3 | 1,219 | 458 | 118 | 12 |
2,038 |
H
|
2038H
|
H. Galactic Council
| 3,000 |
flows
|
Monocarp plays a computer game. In this game, he maintains a space empire. The empire is governed by \(n\) political parties. Initially, every party has political power equal to \(0\), and there is no ruling party.During each of the next \(m\) turns, the following happens: initially, Monocarp has to choose which party he supports. He can support any party, except for the ruling party. When Monocarp supports a party, its political power is increased by \(1\). If Monocarp supports the \(i\)-th party during the \(j\)-th turn, his score increases by \(a_{i,j}\) points; then, the elections happen, and the party with the maximum political power is chosen as the ruling party (if there are multiple such parties, the party with the lowest index among them is chosen). The former ruling party is replaced, unless it is chosen again; finally, an event happens. At the end of the \(j\)-th turn, the party \(p_j\) must be the ruling party to prevent a bad outcome of the event, otherwise Monocarp loses the game. Determine which party Monocarp has to support during each turn so that he doesn't lose the game due to the events, and the score he achieves is the maximum possible. Initially, Monocarp's score is \(0\).
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n, m \le 50\)) β the number of political parties and the number of turns, respectively.The second line contains \(m\) integers \(p_1, p_2, \dots, p_m\) (\(1 \le p_j \le n\)), where \(p_j\) is the index of the party which should be the ruling party at the end of the \(j\)-th turn.Then \(n\) lines follow. The \(i\)-th of them contains \(m\) integers \(a_{i,1}, a_{i,2}, \dots, a_{i,m}\) (\(1 \le a_{i,j} \le 10^5\)), where \(a_{i,j}\) is the amount of points Monocarp gets if he supports the \(i\)-th party during the \(j\)-th turn.
|
If Monocarp loses the game no matter how he acts, print one integer \(-1\).Otherwise, print \(m\) integers \(c_1, c_2, \dots, c_m\) (\(1 \le c_j \le n\)), where \(c_j\) is the index of the party Monocarp should support during the \(j\)-th turn. If there are multiple answers, print any of them.
|
Input: 2 32 1 21 2 34 5 6 | Output: 2 1 2
|
Master
| 1 | 1,213 | 594 | 294 | 20 |
|
552 |
B
|
552B
|
B. Vanya and Books
| 1,200 |
implementation; math
|
Vanya got an important task β he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers.Vanya wants to know how many digits he will have to write down as he labels the books.
|
The first line contains integer n (1 β€ n β€ 109) β the number of books in the library.
|
Print the number of digits needed to number all the books.
|
Note to the first test. The books get numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, which totals to 17 digits.Note to the second sample. The books get numbers 1, 2, 3, 4, which totals to 4 digits.
|
Input: 13 | Output: 17
|
Easy
| 2 | 322 | 85 | 58 | 5 |
53 |
D
|
53D
|
D. Physical Education
| 1,500 |
sortings
|
Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.
|
The first line contains an integer n (1 β€ n β€ 300) which is the number of students. The second line contains n space-separated integers ai (1 β€ ai β€ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 β€ bi β€ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.
|
In the first line print an integer k (0 β€ k β€ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 β€ pi β€ n - 1) means that Vasya should swap students occupying places pi and pi + 1.
|
Input: 41 2 3 23 2 1 2 | Output: 42 31 23 42 3
|
Medium
| 1 | 807 | 577 | 309 | 0 |
|
1,684 |
E
|
1684E
|
E. MEX vs DIFF
| 2,100 |
binary search; brute force; constructive algorithms; data structures; greedy; two pointers
|
You are given an array \(a\) of \(n\) non-negative integers. In one operation you can change any number in the array to any other non-negative integer.Let's define the cost of the array as \(\operatorname{DIFF}(a) - \operatorname{MEX}(a)\), where \(\operatorname{MEX}\) of a set of non-negative integers is the smallest non-negative integer not present in the set, and \(\operatorname{DIFF}\) is the number of different numbers in the array.For example, \(\operatorname{MEX}(\{1, 2, 3\}) = 0\), \(\operatorname{MEX}(\{0, 1, 2, 4, 5\}) = 3\).You should find the minimal cost of the array \(a\) if you are allowed to make at most \(k\) operations.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(0 \le k \le 10^5\)) β the length of the array \(a\) and the number of operations that you are allowed to make.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 elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case output a single integer β minimal cost that it is possible to get making at most \(k\) operations.
|
In the first test case no operations are needed to minimize the value of \(\operatorname{DIFF} - \operatorname{MEX}\).In the second test case it is possible to replace \(5\) by \(1\). After that the array \(a\) is \([0,\, 2,\, 4,\, 1]\), \(\operatorname{DIFF} = 4\), \(\operatorname{MEX} = \operatorname{MEX}(\{0, 1, 2, 4\}) = 3\), so the answer is \(1\).In the third test case one possible array \(a\) is \([4,\, 13,\, 0,\, 0,\, 13,\, 1,\, 2]\), \(\operatorname{DIFF} = 5\), \(\operatorname{MEX} = 3\).In the fourth test case one possible array \(a\) is \([1,\, 2,\, 3,\, 0,\, 0,\, 0]\).
|
Input: 44 13 0 1 24 10 2 4 57 24 13 0 0 13 1337 10000000006 21 2 8 0 0 0 | Output: 0 1 2 0
|
Hard
| 6 | 645 | 612 | 117 | 16 |
1,979 |
E
|
1979E
|
E. Manhattan Triangle
| 2,400 |
binary search; constructive algorithms; data structures; geometry; implementation; two pointers
|
The Manhattan distance between two points \((x_1, y_1)\) and \((x_2, y_2)\) is defined as: $$$\(|x_1 - x_2| + |y_1 - y_2|.\)\(We call a Manhattan triangle three points on the plane, the Manhattan distances between each pair of which are equal.You are given a set of pairwise distinct points and an even integer \)d\(. Your task is to find any Manhattan triangle, composed of three distinct points from the given set, where the Manhattan distance between any pair of vertices is equal to \)d$$$.
|
Each test consists of multiple test cases. The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(d\) (\(3 \le n \le 2 \cdot 10^5\), \(2 \le d \le 4 \cdot 10^5\), \(d\) is even) β the number of points and the required Manhattan distance between the vertices of the triangle.The \((i + 1)\)-th line of each test case contains two integers \(x_i\) and \(y_i\) (\(-10^5 \le x_i, y_i \le 10^5\)) β the coordinates of the \(i\)-th point. It is guaranteed that all points are pairwise 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 three distinct integers \(i\), \(j\), and \(k\) (\(1 \le i,j,k \le n\)) β the indices of the points forming the Manhattan triangle. If there is no solution, output ""\(0\ 0\ 0\)"" (without quotes).If there are multiple solutions, output any of them.
|
In the first test case: Points \(A\), \(B\), and \(F\) form a Manhattan triangle, the Manhattan distance between each pair of vertices is \(4\). Points \(D\), \(E\), and \(F\) can also be the answer. In the third test case: Points \(A\), \(C\), and \(E\) form a Manhattan triangle, the Manhattan distance between each pair of vertices is \(6\). In the fourth test case, there are no two points with a Manhattan distance of \(4\), and therefore there is no suitable Manhattan triangle.
|
Input: 66 43 10 00 -25 -33 -52 -25 40 00 -25 -33 -52 -26 63 10 00 -25 -33 -52 -24 43 00 3-3 00 -310 82 1-5 -1-4 -1-5 -30 1-2 5-4 4-4 20 0-4 14 400000100000 100000-100000 100000100000 -100000-100000 -100000 | Output: 2 6 1 4 3 5 3 5 1 0 0 0 6 1 3 0 0 0
|
Expert
| 6 | 494 | 727 | 276 | 19 |
1,872 |
D
|
1872D
|
D. Plus Minus Permutation
| 1,200 |
math
|
You are given \(3\) integers β \(n\), \(x\), \(y\). Let's call the score of a permutation\(^\dagger\) \(p_1, \ldots, p_n\) the following value:$$$\((p_{1 \cdot x} + p_{2 \cdot x} + \ldots + p_{\lfloor \frac{n}{x} \rfloor \cdot x}) - (p_{1 \cdot y} + p_{2 \cdot y} + \ldots + p_{\lfloor \frac{n}{y} \rfloor \cdot y})\)\(In other words, the score of a permutation is the sum of \)p_i\( for all indices \)i\( divisible by \)x\(, minus the sum of \)p_i\( for all indices \)i\( divisible by \)y\(.You need to find the maximum possible score among all permutations of length \)n\(.For example, if \)n = 7\(, \)x = 2\(, \)y = 3\(, the maximum score is achieved by the permutation \)[2,\color{red}{\underline{\color{black}{6}}},\color{blue}{\underline{\color{black}{1}}},\color{red}{\underline{\color{black}{7}}},5,\color{blue}{\underline{\color{red}{\underline{\color{black}{4}}}}},3]\( and is equal to \)(6 + 7 + 4) - (1 + 4) = 17 - 5 = 12\(.\)^\dagger\( A permutation of length \)n\( is an array consisting of \)n\( distinct integers from \)1\( to \)n\( in any order. For example, \)[2,3,1,5,4]\( is a permutation, but \)[1,2,2]\( is not a permutation (the number \)2\( appears twice in the array) and \)[1,3,4]\( is also not a permutation (\)n=3\(, but the array contains \)4$$$).
|
The first line of input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follows the description of each test case.The only line of each test case description contains \(3\) integers \(n\), \(x\), \(y\) (\(1 \le n \le 10^9\), \(1 \le x, y \le n\)).
|
For each test case, output a single integer β the maximum score among all permutations of length \(n\).
|
The first test case is explained in the problem statement above.In the second test case, one of the optimal permutations will be \([12,11,\color{blue}{\underline{\color{black}{2}}},4,8,\color{blue}{\underline{\color{red}{\underline{\color{black}{9}}}}},10,6,\color{blue}{\underline{\color{black}{1}}},5,3,\color{blue}{\underline{\color{red}{\underline{\color{black}{7}}}}}]\). The score of this permutation is \((9 + 7) - (2 + 9 + 1 + 7) = -3\). It can be shown that a score greater than \(-3\) can not be achieved. Note that the answer to the problem can be negative.In the third test case, the score of the permutation will be \((p_1 + p_2 + \ldots + p_9) - p_9\). One of the optimal permutations for this case is \([9, 8, 7, 6, 5, 4, 3, 2, 1]\), and its score is \(44\). It can be shown that a score greater than \(44\) can not be achieved.In the fourth test case, \(x = y\), so the score of any permutation will be \(0\).
|
Input: 87 2 312 6 39 1 92 2 2100 20 5024 4 61000000000 5575 254504 4 1 | Output: 12 -3 44 0 393 87 179179179436104 -6
|
Easy
| 1 | 1,276 | 280 | 103 | 18 |
2,071 |
D1
|
2071D1
|
D1. Infinite Sequence (Easy Version)
| 1,800 |
bitmasks; brute force; dp; implementation; math
|
This is the easy version of the problem. The difference between the versions is that in this version, \(l=r\). You can hack only if you solved all versions of this problem. You are given a positive integer \(n\) and the first \(n\) terms of an infinite binary sequence \(a\), which is defined as follows: For \(m>n\), \(a_m = a_1 \oplus a_2 \oplus \ldots \oplus a_{\lfloor \frac{m}{2} \rfloor}\)\(^{\text{β}}\). Your task is to compute the sum of elements in a given range \([l, r]\): \(a_l + a_{l + 1} + \ldots + a_r\).\(^{\text{β}}\)\(\oplus\) denotes the bitwise XOR operation.
|
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 three integers \(n\), \(l\), and \(r\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le l=r\le 10^{18}\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(\color{red}{a_i \in \{0, 1\}}\)) β the first \(n\) terms of the sequence \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the sum of elements in the given range.
|
In the first test case, the sequence \(a\) is equal to $$$\([\underline{\color{red}{1}}, 1, 1, 0, 0, 1, 1, 1, 1, 1, \ldots]\)\( where \)l = 1\(, and \)r = 1\(. The sum of elements in the range \)[1, 1]\( is equal to \)\(a_1 = 1.\)\(In the second test case, the sequence \)a\( is equal to \)\([\color{red}{1}, \color{red}{0}, \underline{1}, 1, 1, 0, 0, 1, 1, 0, \ldots]\)\( where \)l = 3\(, and \)r = 3\(. The sum of elements in the range \)[3, 3]\( is equal to \)\(a_3 = 1.\)$$$
|
Input: 91 1 112 3 31 03 5 51 1 11 234 23405 1111 11111 0 1 0 11 1000000000000000000 1000000000000000000110 87 870 1 1 1 1 1 1 1 0 012 69 691 0 0 0 0 1 0 1 0 1 1 013 46 460 1 0 1 1 1 1 1 1 0 1 1 1 | Output: 1 1 0 0 1 0 1 0 0
|
Medium
| 5 | 580 | 540 | 85 | 20 |
802 |
L
|
802L
|
L. Send the Fool Further! (hard)
| 2,400 |
dfs and similar; dp; math; trees
|
Heidi is terrified by your estimate and she found it unrealistic that her friends would collaborate to drive her into debt. She expects that, actually, each person will just pick a random friend to send Heidi to. (This randomness assumption implies, however, that she can now visit the same friend an arbitrary number of times...) Moreover, if a person only has one friend in common with Heidi (i.e., if that person is in a leaf of the tree), then that person will not send Heidi back (so that Heidi's travel will end at some point).Heidi also found unrealistic the assumption that she can make all the travels in one day. Therefore now she assumes that every time she travels a route between two friends, she needs to buy a new ticket. She wants to know: how much should she expect to spend on the trips?For what it's worth, Heidi knows that Jenny has at least two friends.
|
The first line contains the number of friends n (3 β€ n β€ 105). The next n - 1 lines each contain three space-separated integers u, v and c (0 β€ u, v β€ n - 1, 1 β€ c β€ 104) meaning that u and v are friends and the cost for traveling between u and v is c (paid every time!).It is again guaranteed that the social network of the input forms a tree.
|
Assume that the expected cost of the trips is written as an irreducible fraction a / b (that is, a and b are coprime). Then Heidi, the weird cow that she is, asks you to output . (Output a single integer between 0 and 109 + 6.)
|
In the first example, with probability 1 / 2 Heidi will go to 1 from 0, and with probability 1 / 2 she will go to 2. In the first case the cost would be 10, and in the second it would be 20. After reaching 1 or 2 she will stop, as 1 and 2 are leaves of the social tree. Hence, the expected cost she has to pay is 10Β·1 / 2 + 20Β·1 / 2 = 15.In the third example, the expected cost is 81 / 5. You should output 400000019.In her travels, Heidi has learned an intriguing fact about the structure of her social network. She tells you the following: The mysterious determinant that you might be wondering about is such that it does not cause strange errors in your reasonable solution... Did we mention that Heidi is a weird cow?
|
Input: 30 1 100 2 20 | Output: 15
|
Expert
| 4 | 874 | 344 | 227 | 8 |
696 |
B
|
696B
|
B. Puzzles
| 1,700 |
dfs and similar; math; probabilities; trees
|
Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads. Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS to search in the cities. A pseudo code of this algorithm is as follows:let starting_time be an array of length ncurrent_time = 0dfs(v): current_time = current_time + 1 starting_time[v] = current_time shuffle children[v] randomly (each permutation with equal possibility) // children[v] is vector of children cities of city v for u in children[v]: dfs(u)As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)).Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city i, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help.
|
The first line of input contains a single integer n (1 β€ n β€ 105) β the number of cities in USC.The second line contains n - 1 integers p2, p3, ..., pn (1 β€ pi < i), where pi is the number of the parent city of city number i in the tree, meaning there is a road between cities numbered pi and i in USC.
|
In the first and only line of output print n numbers, where i-th number is the expected value of starting_time[i].Your answer for each city will be considered correct if its absolute or relative error does not exceed 10 - 6.
|
Input: 71 2 1 1 4 4 | Output: 1.0 4.0 5.0 3.5 4.5 5.0 5.0
|
Medium
| 4 | 1,235 | 302 | 224 | 6 |
|
309 |
C
|
309C
|
C. Memory for Arrays
| 1,900 |
binary search; bitmasks; greedy
|
You get to work and turn on the computer. You start coding and give little thought to the RAM role in the whole process. In this problem your task is to solve one of the problems you encounter in your computer routine.We'll consider the RAM as a sequence of cells that can contain data. Some cells already contain some data, some are empty. The empty cells form the so-called memory clusters. Thus, a memory cluster is a sequence of some consecutive empty memory cells. You have exactly n memory clusters, the i-th cluster consists of ai cells. You need to find memory for m arrays in your program. The j-th array takes 2bj consecutive memory cells. There possibly isn't enough memory for all m arrays, so your task is to determine what maximum number of arrays can be located in the available memory clusters. Of course, the arrays cannot be divided between the memory clusters. Also, no cell can belong to two arrays.
|
The first line of the input contains two integers n and m (1 β€ n, m β€ 106). The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 109). The next line contains m integers b1, b2, ..., bm (1 β€ 2bi β€ 109).
|
Print a single integer β the answer to the problem.
|
In the first example you are given memory clusters with sizes 8, 4, 3, 2, 2 and arrays with sizes 8, 4, 4. There are few ways to obtain an answer equals 2: you can locate array with size 8 to the cluster with size 8, and one of the arrays with size 4 to the cluster with size 4. Another way is to locate two arrays with size 4 to the one cluster with size 8.In the second example you are given 10 memory clusters with size 1 and 6 arrays with size 1. You can choose any 6 clusters and locate all given arrays to them.
|
Input: 5 38 4 3 2 23 2 2 | Output: 2
|
Hard
| 3 | 919 | 208 | 51 | 3 |
1,758 |
E
|
1758E
|
E. Tick, Tock
| 2,500 |
combinatorics; dfs and similar; dsu; graphs
|
Tannhaus, the clockmaker in the town of Winden, makes mysterious clocks that measure time in \(h\) hours numbered from \(0\) to \(h-1\). One day, he decided to make a puzzle with these clocks. The puzzle consists of an \(n \times m\) grid of clocks, and each clock always displays some hour exactly (that is, it doesn't lie between two hours). In one move, he can choose any row or column and shift all clocks in that row or column one hour forward\(^\dagger\).The grid of clocks is called solvable if it is possible to make all the clocks display the same time.While building his puzzle, Tannhaus suddenly got worried that it might not be possible to make the grid solvable. Some cells of the grid have clocks already displaying a certain initial time, while the rest of the cells are empty.Given the partially completed grid of clocks, find the number of ways\(^\ddagger\) to assign clocks in the empty cells so that the grid is solvable. The answer can be enormous, so compute it modulo \(10^9 + 7\).\(^\dagger\) If a clock currently displays hour \(t\) and is shifted one hour forward, then the clock will instead display hour \((t+1) \bmod h\).\(^\ddagger\) Two assignments are different if there exists some cell with a clock that displays a different time in both arrangements.
|
The first line of input contains \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) β the number of test cases.The first line of each test case consists of 3 integers \(n\), \(m\), and \(h\) (\(1 \leq n, m \leq 2 \cdot 10^5\); \(1 \leq h \leq 10^9\)) β the number of rows in the grid, the number of columns in the grid, and the number of the hours in the day respectively.The next \(n\) lines each contain \(m\) integers, describing the clock grid. The integer \(x\) (\(-1 \leq x < h\)) in the \(i\)-th row and the \(j\)-th column represents the initial hour of the corresponding clock, or if \(x = -1\), an empty cell.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the number of ways to assign clocks in the empty cells so that the grid is solvable. The answer can be huge, so output it modulo \(10^9 + 7\).
|
For the first sample, this is a possible configuration for the clocks: 103032This is solvable since we can: Move the middle column forward one hour. Move the third column forward one hour. Move the third column forward one hour. Move the second row forward one hour. After that all the clocks show one hour.For the second test case, it can be shown that there are no possible solvable clock configurations.
|
Input: 52 3 41 0 -1-1 -1 22 2 101 23 54 5 10241 -1 -1 -1 -1-1 -1 -1 1000 -1-1 -1 -1 -1 69420 -1 -1 999 -13 3 3-1 -1 12 -1 12 -1 23 3 31 -1 2-1 0 -1-1 1 0 | Output: 4 0 73741817 0 1
|
Expert
| 4 | 1,284 | 707 | 169 | 17 |
870 |
B
|
870B
|
B. Maximum of Maximums of Minimums
| 1,200 |
greedy
|
You are given an array a1, a2, ..., an consisting of n integers, and an integer k. You have to split the array into exactly k non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the k obtained minimums. What is the maximum possible integer you can get?Definitions of subsegment and array splitting are given in notes.
|
The first line contains two integers n and k (1 β€ k β€ n β€ 105) β the size of the array a and the number of subsegments you have to split the array to.The second line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109).
|
Print single integer β the maximum possible integer you can get if you split the array into k non-empty subsegments and take maximum of minimums on the subsegments.
|
A subsegment [l, r] (l β€ r) of array a is the sequence al, al + 1, ..., ar.Splitting of array a of n elements into k subsegments [l1, r1], [l2, r2], ..., [lk, rk] (l1 = 1, rk = n, li = ri - 1 + 1 for all i > 1) is k sequences (al1, ..., ar1), ..., (alk, ..., ark).In the first example you should split the array into subsegments [1, 4] and [5, 5] that results in sequences (1, 2, 3, 4) and (5). The minimums are min(1, 2, 3, 4) = 1 and min(5) = 5. The resulting maximum is max(1, 5) = 5. It is obvious that you can't reach greater result.In the second example the only option you have is to split the array into one subsegment [1, 5], that results in one sequence ( - 4, - 5, - 3, - 2, - 1). The only minimum is min( - 4, - 5, - 3, - 2, - 1) = - 5. The resulting maximum is - 5.
|
Input: 5 21 2 3 4 5 | Output: 5
|
Easy
| 1 | 382 | 222 | 164 | 8 |
1,046 |
C
|
1046C
|
C. Space Formula
| 1,400 |
greedy
|
Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race.Given the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next race. It's guaranteed that given astronaut will have unique number of points before the race.
|
The first line contains two integer numbers \(N\) (\(1 \leq N \leq 200000\)) representing number of F1 astronauts, and current position of astronaut \(D\) (\(1 \leq D \leq N\)) you want to calculate best ranking for (no other competitor will have the same number of points before the race).The second line contains \(N\) integer numbers \(S_k\) (\(0 \leq S_k \leq 10^8\), \(k=1...N\)), separated by a single space, representing current ranking of astronauts. Points are sorted in non-increasing order.The third line contains \(N\) integer numbers \(P_k\) (\(0 \leq P_k \leq 10^8\), \(k=1...N\)), separated by a single space, representing point awards for the next race. Points are sorted in non-increasing order, so winner of the race gets the maximum number of points.
|
Output contains one integer number β the best possible ranking for astronaut after the race. If multiple astronauts have the same score after the race, they all share the best ranking.
|
If the third ranked astronaut wins the race, he will have 35 points. He cannot take the leading position, but he can overtake the second position if the second ranked astronaut finishes the race at the last position.
|
Input: 4 350 30 20 1015 10 7 3 | Output: 2
|
Easy
| 1 | 410 | 769 | 184 | 10 |
1,239 |
D
|
1239D
|
D. Catowice City
| 2,400 |
2-sat; dfs and similar; graph matchings; graphs
|
In the Catowice city next weekend the cat contest will be held. However, the jury members and the contestants haven't been selected yet. There are \(n\) residents and \(n\) cats in the Catowice, and each resident has exactly one cat living in his house. The residents and cats are numbered with integers from \(1\) to \(n\), where the \(i\)-th cat is living in the house of \(i\)-th resident.Each Catowice resident is in friendship with several cats, including the one living in his house. In order to conduct a contest, at least one jury member is needed and at least one cat contestant is needed. Of course, every jury member should know none of the contestants. For the contest to be successful, it's also needed that the number of jury members plus the number of contestants is equal to \(n\).Please help Catowice residents to select the jury and the contestants for the upcoming competition, or determine that it's impossible to do.
|
The first line contains an integer \(t\) (\(1 \le t \le 100\,000\)), the number of test cases. Then description of \(t\) test cases follow, where each description is as follows:The first line contains integers \(n\) and \(m\) (\(1 \le n \le m \le 10^6\)), the number of Catowice residents and the number of friendship pairs between residents and cats.Each of the next \(m\) lines contains integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le n\)), denoting that \(a_i\)-th resident is acquaintances with \(b_i\)-th cat. It's guaranteed that each pair of some resident and some cat is listed at most once.It's guaranteed, that for every \(i\) there exists a pair between \(i\)-th resident and \(i\)-th cat.Different test cases are separated with an empty line.It's guaranteed, that the sum of \(n\) over all test cases is at most \(10^6\) and that the sum of \(m\) over all test cases is at most \(10^6\).
|
For every test case print: ""No"", if it's impossible to select the jury and contestants. Otherwise print ""Yes"".In the second line print two integers \(j\) and \(p\) (\(1 \le j\), \(1 \le p\), \(j + p = n\)) β the number of jury members and the number of contest participants.In the third line print \(j\) distinct integers from \(1\) to \(n\), the indices of the residents forming a jury.In the fourth line print \(p\) distinct integers from \(1\) to \(n\), the indices of the cats, which will participate in the contest.In case there are several correct answers, print any of them.
|
In the first test case, we can select the first and the third resident as a jury. Both of them are not acquaintances with a second cat, so we can select it as a contestant.In the second test case, we can select the second resident as a jury. He is not an acquaintances with a first and a third cat, so they can be selected as contestants.In the third test case, the only resident is acquaintances with the only cat, so they can't be in the contest together. So it's not possible to make a contest with at least one jury and at least one cat.In the fourth test case, each resident is acquaintances with every cat, so it's again not possible to make a contest with at least one jury and at least one cat.
|
Input: 4 3 4 1 1 2 2 3 3 1 3 3 7 1 1 1 2 1 3 2 2 3 1 3 2 3 3 1 1 1 1 2 4 1 1 1 2 2 1 2 2 | Output: Yes 2 1 1 3 2 Yes 1 2 2 1 3 No No
|
Expert
| 4 | 937 | 902 | 585 | 12 |
242 |
B
|
242B
|
B. Big Segment
| 1,100 |
implementation; sortings
|
A coordinate line has n segments, the i-th segment starts at the position li and ends at the position ri. We will denote such a segment as [li, ri].You have suggested that one of the defined segments covers all others. In other words, there is such segment in the given set, which contains all other ones. Now you want to test your assumption. Find in the given set the segment which covers all other segments, and print its number. If such a segment doesn't exist, print -1.Formally we will assume that segment [a, b] covers segment [c, d], if they meet this condition a β€ c β€ d β€ b.
|
The first line contains integer n (1 β€ n β€ 105) β the number of segments. Next n lines contain the descriptions of the segments. The i-th line contains two space-separated integers li, ri (1 β€ li β€ ri β€ 109) β the borders of the i-th segment.It is guaranteed that no two segments coincide.
|
Print a single integer β the number of the segment that covers all other segments in the set. If there's no solution, print -1.The segments are numbered starting from 1 in the order in which they appear in the input.
|
Input: 31 12 23 3 | Output: -1
|
Easy
| 2 | 584 | 289 | 216 | 2 |
|
1,246 |
F
|
1246F
|
F. Cursor Distance
| 3,500 |
There is a string \(s\) of lowercase English letters. A cursor is positioned on one of the characters. The cursor can be moved with the following operation: choose a letter \(c\) and a direction (left or right). The cursor is then moved to the closest occurence of \(c\) in the chosen direction. If there is no letter \(c\) in that direction, the cursor stays in place. For example, if \(s = \mathtt{abaab}\) with the cursor on the second character (\(\mathtt{a[b]aab}\)), then: moving to the closest letter \(\mathtt{a}\) to the left places the cursor on the first character (\(\mathtt{[a]baab}\)); moving to the closest letter \(\mathtt{a}\) to the right places the cursor the third character (\(\mathtt{ab[a]ab}\)); moving to the closest letter \(\mathtt{b}\) to the right places the cursor on the fifth character (\(\mathtt{abaa[b]}\)); any other operation leaves the cursor in place.Let \(\mathrm{dist}(i, j)\) be the smallest number of operations needed to move the cursor from the \(i\)-th character to the \(j\)-th character. Compute \(\displaystyle \sum_{i = 1}^n \sum_{j = 1}^n \mathrm{dist}(i, j)\).
|
The only line contains a non-empty string \(s\) of at most \(10^5\) lowercase English letters.
|
Print a single integer \(\displaystyle \sum_{i = 1}^n \sum_{j = 1}^n \mathrm{dist}(i, j)\).
|
In the first sample case, \(\mathrm{dist}(i, j) = 0\) for any pair \(i = j\), and \(1\) for all other pairs.
|
Input: abcde | Output: 20
|
Master
| 0 | 1,110 | 94 | 91 | 12 |
|
1,862 |
B
|
1862B
|
B. Sequence Game
| 800 |
constructive algorithms
|
Tema and Vika are playing the following game.First, Vika comes up with a sequence of positive integers \(a\) of length \(m\) and writes it down on a piece of paper. Then she takes a new piece of paper and writes down the sequence \(b\) according to the following rule: First, she writes down \(a_1\). Then, she writes down only those \(a_i\) (\(2 \le i \le m\)) such that \(a_{i - 1} \le a_i\). Let the length of this sequence be denoted as \(n\). For example, from the sequence \(a=[4, 3, 2, 6, 3, 3]\), Vika will obtain the sequence \(b=[4, 6, 3]\).She then gives the piece of paper with the sequence \(b\) to Tema. He, in turn, tries to guess the sequence \(a\).Tema considers winning in such a game highly unlikely, but still wants to find at least one sequence \(a\) that could have been originally chosen by Vika. Help him and output any such sequence.Note that the length of the sequence you output should not exceed the input sequence length by more than two times.
|
Each test consists of multiple test cases. The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. This is followed by a description of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the sequence \(b\).The second line of each test case contains \(n\) integers \(b_1, b_2, b_3, \dots, b_n\) (\(1 \le b_i \le 10^9\)) β the elements of the sequence.The sum of the values of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output two lines. In the first line, output a single integer \(m\) β the length of the sequence (\(n \le m \le 2 \cdot n\)). In the second line, output \(m\) integers \(a_1, a_2, a_3, \dots, a_m\) (\(1 \le a_i \le 10^9\)) β the assumed sequence that Vika could have written on the first piece of paper.If there are multiple suitable sequences, you can output any of them.
|
The first sample is explained in the problem statement.In the second sample, Vika could have chosen the original sequence.
|
Input: 634 6 331 2 351 7 9 5 7114421 151 2 2 1 1 | Output: 6 4 3 2 6 3 3 3 1 2 3 6 1 7 9 3 5 7 1 144 2 1 1 6 1 2 2 1 1 1
|
Beginner
| 1 | 973 | 566 | 391 | 18 |
41 |
E
|
41E
|
E. 3-cycles
| 1,900 |
constructive algorithms; graphs; greedy
|
During a recent research Berland scientists found out that there were n cities in Ancient Berland, joined by two-way paths. Any two cities are joined by no more than one path. No path joins a city with itself. According to a well-known tradition, the road network was built so that it would be impossible to choose three cities from each of which one can get to any other one directly. That is, there was no cycle exactly as long as 3. Unfortunately, the road map has not been preserved till nowadays. Now the scientists are interested how much developed a country Ancient Berland was. Help them - find, what maximal number of roads could be in the country. You also have to restore any of the possible road maps.
|
The first line contains an integer n (1 β€ n β€ 100) β the number of cities in Berland.
|
On the first line must be printed number m β the maximal number of roads in Berland. Then print m lines containing two numbers each β the numbers of cities that the given road joins. The cities are numbered with integers from 1 to n. If there are several variants of solving the problem, print any of them.
|
Input: 3 | Output: 21 22 3
|
Hard
| 3 | 713 | 85 | 306 | 0 |
|
1,193 |
A
|
1193A
|
A. Amusement Park
| 0 |
*special; dp; math
|
You have been hired to supervise the project of a new amusement park. The park will have a special gimmick: directed slides that can get customers from one attraction to another quickly and in an entertaining way.The park owner has given you the current project: a list of planned attractions and a list of slides that should be built between them. However, him being a businessman, he casually envisioned the impossible: among other things, he projected a slide coming from the Haunted Castle to the Roller Coaster, another from the Roller Coaster to the Drop Tower, and a third from the Drop Tower to the Haunted Castle. As the slides can only go downhill, it is evident why this is a problem. You don't have the luxury of ignoring the laws of physics when building the park, so you have to request changes in the project. Maybe he would accept reversing the slide between the Drop Tower and the Haunted Castle?Formally: The project is a list of attractions and a list of directed slides. Each slide starts at one attraction and ends at another attraction. A proposal is obtained from the project by reversing the directions of some slides (possibly none or all of them). A proposal is legal if there is a way to assign an elevation to each attraction in such a way that every slide goes downhill. The cost of a proposal is the number of slides whose directions were reversed. For a given project, find and report the sum of costs all legal proposals. Since this number may be large, output it modulo \(998,244,353\).
|
The first line contains two space-separated integers \(n\), \(m\) (\(1 \leq n \leq 18\), \(0 \leq m \leq n(n-1)/2\)) β the number of attractions and the number of slides, respectively. The attractions are numbered \(1\) through \(n\).Then, \(m\) lines follow. The \(i\)-th of these lines contains two space-separated integers \(a_i\), \(b_i\) (\(1 \leq a_i, b_i \leq n\)) denoting a slide from \(a_i\) to \(b_i\). You may assume that: There are no self-loops. (For each \(i\): \(a_i \neq b_i\).) No slide appears twice. (For all \(i \neq j\): \(a_i \neq a_j\) or \(b_i \neq b_j\).) No pair of attractions is connected in both directions. (The unordered pairs \(\{a_i, b_i\}\) are distinct.)
|
Output one line with a single integer, the sum of costs of all legal proposals modulo \(998,244,353\).
|
In the first example, there are two proposals: The slide direction is not flipped. This proposal has cost \(0\). The slide direction is flipped. This proposal has cost \(1\). As both proposals are valid, the answer is \(0 + 1 = 1\).In the second example, there are eight proposals with the slide directions as follows: \(1 \rightarrow 2\), \(2 \rightarrow 3\), \(1 \rightarrow 3\) (cost \(0\)) \(1 \rightarrow 2\), \(2 \rightarrow 3\), \(3 \rightarrow 1\) (cost \(1\)) \(1 \rightarrow 2\), \(3 \rightarrow 2\), \(1 \rightarrow 3\) (cost \(1\)) \(1 \rightarrow 2\), \(3 \rightarrow 2\), \(3 \rightarrow 1\) (cost \(2\)) \(2 \rightarrow 1\), \(2 \rightarrow 3\), \(1 \rightarrow 3\) (cost \(1\)) \(2 \rightarrow 1\), \(2 \rightarrow 3\), \(3 \rightarrow 1\) (cost \(2\)) \(2 \rightarrow 1\), \(3 \rightarrow 2\), \(1 \rightarrow 3\) (cost \(2\)) \(2 \rightarrow 1\), \(3 \rightarrow 2\), \(3 \rightarrow 1\) (cost \(3\)) The second proposal is not legal, as there is a slide sequence \(1 \rightarrow 2 \rightarrow 3 \rightarrow 1\). This means that the attraction \(1\) has to be strictly higher than ifself, which is clearly impossible. Similarly, the seventh proposal is not legal. The answer is thus \(0 + 1 + 2 + 1 + 2 + 3 = 9\).
|
Input: 2 1 1 2 | Output: 1
|
Beginner
| 3 | 1,519 | 690 | 102 | 11 |
958 |
E2
|
958E2
|
E2. Guard Duty (medium)
| 2,200 |
binary search; dp; greedy; sortings
|
Princess Heidi decided to give orders to all her K Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at N specific moments t1, t2, ..., tN. The meetings with commanders must therefore start and stop at those times. Namely, each commander will board her ship at some time ti and disembark at some later time tj. Of course, Heidi needs to meet with all commanders, and no two meetings can be held during the same time. Two commanders cannot even meet at the beginnings/endings of the hyperspace jumps, because too many ships in one position could give out their coordinates to the enemy. Your task is to find minimum time that Princess Heidi has to spend on meetings, with her schedule satisfying the conditions above.
|
The first line contains two integers K, N (2 β€ 2K β€ N β€ 500000, K β€ 5000). The second line contains N distinct integers t1, t2, ..., tN (1 β€ ti β€ 109) representing the times when Heidi leaves hyperspace.
|
Output only one integer: the minimum time spent on meetings.
|
In the first example, there are five valid schedules: [1, 4], [6, 7] with total time 4, [1, 4], [6, 12] with total time 9, [1, 4], [7, 12] with total time 8, [1, 6], [7, 12] with total time 10, and [4, 6], [7, 12] with total time 7. So the answer is 4.In the second example, there is only 1 valid schedule: [1, 2], [3, 4], [5, 6].For the third example, one possible schedule with total time 6 is: [1, 3], [4, 5], [14, 15], [23, 25].
|
Input: 2 51 4 6 7 12 | Output: 4
|
Hard
| 4 | 785 | 203 | 60 | 9 |
921 |
09
|
92109
|
09. Labyrinth-9
| 3,200 |
See the problem statement here: http://codeforces.com/contest/921/problem/01.
|
Master
| 0 | 77 | 0 | 0 | 9 |
|||||
604 |
B
|
604B
|
B. More Cowbell
| 1,400 |
binary search; greedy
|
Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass instead of corn. Before moving, he must pack his cowbells into k boxes of a fixed size. In order to keep his collection safe during transportation, he won't place more than two cowbells into a single box. Since Kevin wishes to minimize expenses, he is curious about the smallest size box he can use to pack his entire collection. Kevin is a meticulous cowbell collector and knows that the size of his i-th (1 β€ i β€ n) cowbell is an integer si. In fact, he keeps his cowbells sorted by size, so si - 1 β€ si for any i > 1. Also an expert packer, Kevin can fit one or two cowbells into a box of size s if and only if the sum of their sizes does not exceed s. Given this information, help Kevin determine the smallest s for which it is possible to put all of his cowbells into k boxes of size s.
|
The first line of the input contains two space-separated integers n and k (1 β€ n β€ 2Β·k β€ 100 000), denoting the number of cowbells and the number of boxes, respectively.The next line contains n space-separated integers s1, s2, ..., sn (1 β€ s1 β€ s2 β€ ... β€ sn β€ 1 000 000), the sizes of Kevin's cowbells. It is guaranteed that the sizes si are given in non-decreasing order.
|
Print a single integer, the smallest s for which it is possible for Kevin to put all of his cowbells into k boxes of size s.
|
In the first sample, Kevin must pack his two cowbells into the same box. In the second sample, Kevin can pack together the following sets of cowbells: {2, 3}, {5} and {9}.In the third sample, the optimal solution is {3, 5} and {7}.
|
Input: 2 12 5 | Output: 7
|
Easy
| 2 | 911 | 373 | 124 | 6 |
1,840 |
G1
|
1840G1
|
G1. In Search of Truth (Easy Version)
| 2,200 |
constructive algorithms; interactive; math; meet-in-the-middle; probabilities
|
The only difference between easy and hard versions is the maximum number of queries. In this version, you are allowed to ask at most \(2023\) queries.This is an interactive problem.You are playing a game. The circle is divided into \(n\) sectors, sectors are numbered from \(1\) to \(n\) in some order. You are in the adjacent room and do not know either the number of sectors or their numbers. There is also an arrow that initially points to some sector. Initially, the host tells you the number of the sector to which the arrow points. After that, you can ask the host to move the arrow \(k\) sectors counterclockwise or clockwise at most \(2023\) times. And each time you are told the number of the sector to which the arrow points.Your task is to determine the integer \(n\) β the number of sectors in at most \(2023\) queries.It is guaranteed that \(1 \le n \le 10^6\).
|
The input consists of a single integer \(x\) (\(1 \le x \le n\)) β the number of the initial sector.
|
After you determine the integer \(n\) β the number of sectors, you should output ""! n"" (\(1 \le n \le 10^6\)). After that the program should immediately terminate.Note that, printing the answer does not count as a query.It is guaranteed that the integer \(n\) and the numbers of the sectors are fixed initially and will not be changed by the jury program depending on the queries.
|
HacksTo hack, use the following test format.In the first line, output a single integer \(n\) (\(1 \le n \le 10^6\)) β the number of sectors.In the second line, output \(n\) different integers \(1 \le a_1, a_2, \dots, a_n \le n\) β the numbers of the sectors in clockwise order, the arrow initially points to the sector with the number \(a_1\).
|
Input: 1 5 6 7 2 10 9 8 4 3 1 | Output: + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ! 10
|
Hard
| 5 | 874 | 100 | 382 | 18 |
1,810 |
C
|
1810C
|
C. Make It Permutation
| 1,300 |
brute force; greedy; sortings
|
You have an integer array \(a\) of length \(n\). There are two kinds of operations you can make. Remove an integer from \(a\). This operation costs \(c\). Insert an arbitrary positive integer \(x\) to any position of \(a\) (to the front, to the back, or between any two consecutive elements). This operation costs \(d\).You want to make the final array a permutation of any positive length. Please output the minimum cost of doing that. Note that you can make the array empty during the operations, but the final array must contain at least one integer.A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Their description follows.The first line of each test case contains three integers \(n\), \(c\), \(d\) (\(1 \le n \le 10^5\), \(1 \le c,d \le 10^9\)).The second line of each test case contains \(n\) integers \(a_{1}, a_{2}, \ldots, a_{n}\) (\(1 \le a_{i} \le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output in one line the minimum cost to make the final array a permutation.
|
In the first test case, the array is already a permutation, so there's no need for operations.In the second test case, we can remove numbers \(5\), \(6\) to get the permutation \([1,2,3]\) in cost \(2\). Note that we can also get a permutation by inserting a number \(4\), but it costs \(5\).In the third test case, we can just remove all the numbers except for the first number \(1\). It costs \(8\), and the final array is \([1]\), which is a permutation of length \(1\).In the fourth test case, we can remove all the numbers except for \(2\), and insert a number \(1\) to the first position. It costs \(4+10=14\), and the final array is \([1,2]\), which is a permutation of length \(2\).
|
Input: 83 3 31 2 35 1 51 2 3 5 65 2 31 1 1 3 35 1 102 4 6 8 106 2 87 3 5 4 4 84 10 11 2 6 74 3 32 5 8 72 1000000000 11000000000 1 | Output: 0 2 8 14 20 3 12 999999998
|
Easy
| 3 | 877 | 497 | 94 | 18 |
362 |
B
|
362B
|
B. Petya and Staircases
| 1,100 |
implementation; sortings
|
Little boy Petya loves stairs very much. But he is bored from simple going up and down them β he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them.Now Petya is on the first stair of the staircase, consisting of n stairs. He also knows the numbers of the dirty stairs of this staircase. Help Petya find out if he can jump through the entire staircase and reach the last stair number n without touching a dirty stair once.One has to note that anyway Petya should step on the first and last stairs, so if the first or the last stair is dirty, then Petya cannot choose a path with clean steps only.
|
The first line contains two integers n and m (1 β€ n β€ 109, 0 β€ m β€ 3000) β the number of stairs in the staircase and the number of dirty stairs, correspondingly. The second line contains m different space-separated integers d1, d2, ..., dm (1 β€ di β€ n) β the numbers of the dirty stairs (in an arbitrary order).
|
Print ""YES"" if Petya can reach stair number n, stepping only on the clean stairs. Otherwise print ""NO"".
|
Input: 10 52 4 8 3 6 | Output: NO
|
Easy
| 2 | 763 | 311 | 107 | 3 |
|
1,469 |
B
|
1469B
|
B. Red and Blue
| 1,000 |
dp; greedy
|
Monocarp had a sequence \(a\) consisting of \(n + m\) integers \(a_1, a_2, \dots, a_{n + m}\). He painted the elements into two colors, red and blue; \(n\) elements were painted red, all other \(m\) elements were painted blue.After painting the elements, he has written two sequences \(r_1, r_2, \dots, r_n\) and \(b_1, b_2, \dots, b_m\). The sequence \(r\) consisted of all red elements of \(a\) in the order they appeared in \(a\); similarly, the sequence \(b\) consisted of all blue elements of \(a\) in the order they appeared in \(a\) as well.Unfortunately, the original sequence was lost, and Monocarp only has the sequences \(r\) and \(b\). He wants to restore the original sequence. In case there are multiple ways to restore it, he wants to choose a way to restore that maximizes the value of $$$\(f(a) = \max(0, a_1, (a_1 + a_2), (a_1 + a_2 + a_3), \dots, (a_1 + a_2 + a_3 + \dots + a_{n + m}))\)\(Help Monocarp to calculate the maximum possible value of \)f(a)$$$.
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then the test cases follow. Each test case consists of four lines.The first line of each test case contains one integer \(n\) (\(1 \le n \le 100\)).The second line contains \(n\) integers \(r_1, r_2, \dots, r_n\) (\(-100 \le r_i \le 100\)).The third line contains one integer \(m\) (\(1 \le m \le 100\)).The fourth line contains \(m\) integers \(b_1, b_2, \dots, b_m\) (\(-100 \le b_i \le 100\)).
|
For each test case, print one integer β the maximum possible value of \(f(a)\).
|
In the explanations for the sample test cases, red elements are marked as bold.In the first test case, one of the possible sequences \(a\) is \([\mathbf{6}, 2, \mathbf{-5}, 3, \mathbf{7}, \mathbf{-3}, -4]\).In the second test case, one of the possible sequences \(a\) is \([10, \mathbf{1}, -3, \mathbf{1}, 2, 2]\).In the third test case, one of the possible sequences \(a\) is \([\mathbf{-1}, -1, -2, -3, \mathbf{-2}, -4, -5, \mathbf{-3}, \mathbf{-4}, \mathbf{-5}]\).In the fourth test case, one of the possible sequences \(a\) is \([0, \mathbf{0}]\).
|
Input: 4 4 6 -5 7 -3 3 2 3 -4 2 1 1 4 10 -3 2 2 5 -1 -2 -3 -4 -5 5 -1 -2 -3 -4 -5 1 0 1 0 | Output: 13 13 0 0
|
Beginner
| 2 | 975 | 489 | 79 | 14 |
1,996 |
B
|
1996B
|
B. Scale
| 800 |
greedy; implementation
|
Tina has a square grid with \(n\) rows and \(n\) columns. Each cell in the grid is either \(0\) or \(1\). Tina wants to reduce the grid by a factor of \(k\) (\(k\) is a divisor of \(n\)). To do this, Tina splits the grid into \(k \times k\) nonoverlapping blocks of cells such that every cell belongs to exactly one block.Tina then replaces each block of cells with a single cell equal to the value of the cells in the block. It is guaranteed that every cell in the same block has the same value. For example, the following demonstration shows a grid being reduced by factor of \(3\). Original grid \(0\)\(0\)\(0\)\(1\)\(1\)\(1\)\(0\)\(0\)\(0\)\(1\)\(1\)\(1\)\(0\)\(0\)\(0\)\(1\)\(1\)\(1\)\(1\)\(1\)\(1\)\(0\)\(0\)\(0\)\(1\)\(1\)\(1\)\(0\)\(0\)\(0\)\(1\)\(1\)\(1\)\(0\)\(0\)\(0\) Reduced grid \(0\)\(1\)\(1\)\(0\) Help Tina reduce the grid by a factor of \(k\).
|
The first line contains \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq n \leq 1000\), \(1 \le k \le n\), \(k\) is a divisor of \(n\)) β the number of rows and columns of the grid, and the factor that Tina wants to reduce the grid by. Each of the following \(n\) lines contain \(n\) characters describing the cells of the grid. Each character is either \(0\) or \(1\). It is guaranteed every \(k\) by \(k\) block has the same value.It is guaranteed the sum of \(n\) over all test cases does not exceed \(1000\).
|
For each test case, output the grid reduced by a factor of \(k\) on a new line.
|
Input: 44 400000000000000006 30001110001110001111110001110001110006 20011000011001111111111111100001100008 11111111111111111111111111111111111111111111111111111111111111111 | Output: 0 01 10 010 111 100 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
|
Beginner
| 2 | 861 | 601 | 79 | 19 |
|
2,106 |
C
|
2106C
|
C. Cherry Bomb
| 1,000 |
greedy; math; sortings
|
Two integer arrays \(a\) and \(b\) of size \(n\) are complementary if there exists an integer \(x\) such that \(a_i + b_i = x\) over all \(1 \le i \le n\). For example, the arrays \(a = [2, 1, 4]\) and \(b = [3, 4, 1]\) are complementary, since \(a_i + b_i = 5\) over all \(1 \le i \le 3\). However, the arrays \(a = [1, 3]\) and \(b = [2, 1]\) are not complementary.Cow the Nerd thinks everybody is interested in math, so he gave Cherry Bomb two integer arrays \(a\) and \(b\). It is known that \(a\) and \(b\) both contain \(n\) non-negative integers not greater than \(k\). Unfortunately, Cherry Bomb has lost some elements in \(b\). Lost elements in \(b\) are denoted with \(-1\). Help Cherry Bomb count the number of possible arrays \(b\) such that: \(a\) and \(b\) are complementary. All lost elements are replaced with non-negative integers no more than \(k\).
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le k \le 10^9\)) β the size of the arrays and the maximum possible value of their elements.The second line contains \(n\) integers \(a_1, a_2, ..., a_n\) (\(0 \le a_i \le k\)).The third line contains \(n\) integers \(b_1, b_2, ..., b_n\) (\(-1 \le b_i \le k\)). If \(b_i = -1\), then this element is missing.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
Output exactly one integer, the number of ways Cherry Bomb can fill in the missing elements from \(b\) such that \(a\) and \(b\) are complementary.
|
In the first example, the only way to fill in the missing elements in \(b\) such that \(a\) and \(b\) are complementary is if \(b = [2, 0, 1]\).In the second example, there is no way to fill in the missing elements of \(b\) such that \(a\) and \(b\) are complementary.In the fourth example, some \(b\) arrays that are complementary to \(a\) are: \([4, 2, 3, 0, 1], [7, 5, 6, 3, 4],\) and \([9, 7, 8, 5, 6]\).
|
Input: 73 101 3 2-1 -1 15 10 1 0 0 1-1 0 1 0 -15 10 1 0 0 1-1 1 -1 1 -15 101 3 2 5 4-1 -1 -1 -1 -15 41 3 2 1 31 -1 -1 1 -15 41 3 2 1 32 -1 -1 2 05 55 0 5 4 35 -1 -1 -1 -1 | Output: 1 0 0 7 0 1 0
|
Beginner
| 3 | 867 | 617 | 147 | 21 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.