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,967 |
E1
|
1967E1
|
E1. Again Counting Arrays (Easy Version)
| 3,100 |
combinatorics; dp; fft; math
|
This is the easy version of the problem. The differences between the two versions are the constraints on \(n, m, b_0\) and the time limit. You can make hacks only if both versions are solved.Little R has counted many sets before, and now she decides to count arrays.Little R thinks an array \(b_0, \ldots, b_n\) consisting of non-negative integers is continuous if and only if, for each \(i\) such that \(1 \leq i \leq n\), \(\lvert b_i - b_{i-1} \rvert = 1\) is satisfied. She likes continuity, so she only wants to generate continuous arrays.If Little R is given \(b_0\) and \(a_1, \ldots, a_n\), she will try to generate a non-negative continuous array \(b\), which has no similarity with \(a\). More formally, for all \(1 \leq i \leq n\), \(a_i \neq b_i\) holds.However, Little R does not have any array \(a\). Instead, she gives you \(n\), \(m\) and \(b_0\). She wants to count the different integer arrays \(a_1, \ldots, a_n\) satisfying: \(1 \leq a_i \leq m\); At least one non-negative continuous array \(b_0, \ldots, b_n\) can be generated. Note that \(b_i \geq 0\), but the \(b_i\) can be arbitrarily large.Since the actual answer may be enormous, please just tell her the answer modulo \(998\,244\,353\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\ (1 \leq t \leq 10^4)\). The description of the test cases follows.The first and only line of each test case contains three integers \(n\), \(m\), and \(b_0\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq m \leq 2 \cdot 10^5\), \(0 \leq b_0 \leq 2\cdot 10^5\)) β the length of the array \(a_1, \ldots, a_n\), the maximum possible element in \(a_1, \ldots, a_n\), and the initial element of the array \(b_0, \ldots, b_n\).It is guaranteed that the sum of \(n\) over all test cases does not exceeds \(2\cdot 10^5\).
|
For each test case, output a single line containing an integer: the number of different arrays \(a_1, \ldots, a_n\) satisfying the conditions, modulo \(998\,244\,353\).
|
In the first test case, for example, when \(a = [1, 2, 1]\), we can set \(b = [1, 0, 1, 0]\). When \(a = [1, 1, 2]\), we can set \(b = [1, 2, 3, 4]\). In total, there are \(6\) valid choices of \(a_1, \ldots, a_n\): in fact, it can be proved that only \(a = [2, 1, 1]\) and \(a = [2, 1, 2]\) make it impossible to construct a non-negative continuous \(b_0, \ldots, b_n\), so the answer is \(2^3 - 2 = 6\).
|
Input: 63 2 15 5 313 4 1100 6 7100 11 31000 424 132 | Output: 6 3120 59982228 943484039 644081522 501350342
|
Master
| 4 | 1,215 | 602 | 168 | 19 |
538 |
F
|
538F
|
F. A Heap of Heaps
| 2,200 |
brute force; data structures; math; sortings
|
Andrew skipped lessons on the subject 'Algorithms and Data Structures' for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment.The teacher gave Andrew an array of n numbers a1, ..., an. After that he asked Andrew for each k from 1 to n - 1 to build a k-ary heap on the array and count the number of elements for which the property of the minimum-rooted heap is violated, i.e. the value of an element is less than the value of its parent.Andrew looked up on the Wikipedia that a k-ary heap is a rooted tree with vertices in elements of the array. If the elements of the array are indexed from 1 to n, then the children of element v are elements with indices k(v - 1) + 2, ..., kv + 1 (if some of these elements lie outside the borders of the array, the corresponding children are absent). In any k-ary heap every element except for the first one has exactly one parent; for the element 1 the parent is absent (this element is the root of the heap). Denote p(v) as the number of the parent of the element with the number v. Let's say that for a non-root element v the property of the heap is violated if av < ap(v).Help Andrew cope with the task!
|
The first line contains a single integer n (2 β€ n β€ 2Β·105).The second line contains n space-separated integers a1, ..., an ( - 109 β€ ai β€ 109).
|
in a single line print n - 1 integers, separate the consecutive numbers with a single space β the number of elements for which the property of the k-ary heap is violated, for k = 1, 2, ..., n - 1.
|
Pictures with the heaps for the first sample are given below; elements for which the property of the heap is violated are marked with red. In the second sample all elements are equal, so the property holds for all pairs.
|
Input: 51 5 4 3 2 | Output: 3 2 1 0
|
Hard
| 4 | 1,206 | 143 | 196 | 5 |
1,538 |
E
|
1538E
|
E. Funny Substrings
| 2,100 |
data structures; hashing; implementation; matrices; strings
|
Polycarp came up with a new programming language. There are only two types of statements in it: ""x := s"": assign the variable named x the value s (where s is a string). For example, the statement var := hello assigns the variable named var the value hello. Note that s is the value of a string, not the name of a variable. Between the variable name, the := operator and the string contains exactly one space each. ""x = a + b"": assign the variable named x the concatenation of values of two variables a and b. For example, if the program consists of three statements a := hello, b := world, c = a + b, then the variable c will contain the string helloworld. It is guaranteed that the program is correct and the variables a and b were previously defined. There is exactly one space between the variable names and the = and + operators. All variable names and strings only consist of lowercase letters of the English alphabet and do not exceed \(5\) characters.The result of the program is the number of occurrences of string haha in the string that was written to the variable in the last statement.Polycarp was very tired while inventing that language. He asks you to implement it. Your task is β for given program statements calculate the number of occurrences of string haha in the last assigned variable.
|
The first line contains an integer \(t\) (\(1 \le t \le 10^3\)). Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)) β the number of statements in the program. All variable names and strings are guaranteed to consist only of lowercase letters of the English alphabet and do not exceed \(5\) characters.This is followed by \(n\) lines describing the statements in the format described above. It is guaranteed that the program is correct.
|
For each set of input data, output the number of occurrences of the haha substring in the string that was written to the variable in the last statement.
|
In the first test case the resulting value of d is hhahahaha.
|
Input: 4 6 a := h b := aha c = a + b c = c + c e = c + c d = a + c 15 x := haha x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x 1 haha := hah 5 haahh := aaaha ahhhh = haahh + haahh haahh = haahh + haahh ahhhh = ahhhh + haahh ahhaa = haahh + ahhhh | Output: 3 32767 0 0
|
Hard
| 5 | 1,310 | 499 | 152 | 15 |
117 |
D
|
117D
|
D. Not Quick Transformation
| 2,500 |
divide and conquer; math
|
Let a be an array consisting of n numbers. The array's elements are numbered from 1 to n, even is an array consisting of the numerals whose numbers are even in a (eveni = a2i, 1 β€ 2i β€ n), odd is an array consisting of the numberals whose numbers are odd in Π° (oddi = a2i - 1, 1 β€ 2i - 1 β€ n). Then let's define the transformation of array F(a) in the following manner: if n > 1, F(a) = F(odd) + F(even), where operation "" + "" stands for the arrays' concatenation (joining together) if n = 1, F(a) = a Let a be an array consisting of n numbers 1, 2, 3, ..., n. Then b is the result of applying the transformation to the array a (so b = F(a)). You are given m queries (l, r, u, v). Your task is to find for each query the sum of numbers bi, such that l β€ i β€ r and u β€ bi β€ v. You should print the query results modulo mod.
|
The first line contains three integers n, m, mod (1 β€ n β€ 1018, 1 β€ m β€ 105, 1 β€ mod β€ 109). Next m lines describe the queries. Each query is defined by four integers l, r, u, v (1 β€ l β€ r β€ n, 1 β€ u β€ v β€ 1018).Please do not use the %lld specificator to read or write 64-bit integers in C++. Use %I64d specificator.
|
Print m lines each containing an integer β remainder modulo mod of the query result.
|
Let's consider the first example. First let's construct an array b = F(a) = F([1, 2, 3, 4]). Step 1. F([1, 2, 3, 4]) = F([1, 3]) + F([2, 4]) Step 2. F([1, 3]) = F([1]) + F([3]) = [1] + [3] = [1, 3] Step 3. F([2, 4]) = F([2]) + F([4]) = [2] + [4] = [2, 4] Step 4. b = F([1, 2, 3, 4]) = F([1, 3]) + F([2, 4]) = [1, 3] + [2, 4] = [1, 3, 2, 4] Thus b = [1, 3, 2, 4]. Let's consider the first query l = 2, r = 3, u = 4, v = 5. The second and third positions in the array b do not have numbers in the range [4, 5], so the sum obviously equals zero. Let's consider the second query l = 2, r = 4, u = 1, v = 3. The second and third positions have two numbers that belong to the range [1, 3], their sum equals 5.
|
Input: 4 5 100002 3 4 52 4 1 31 2 2 42 3 3 51 3 3 4 | Output: 05333
|
Expert
| 2 | 824 | 316 | 84 | 1 |
1,753 |
B
|
1753B
|
B. Factorial Divisibility
| 1,600 |
math; number theory
|
You are given an integer \(x\) and an array of integers \(a_1, a_2, \ldots, a_n\). You have to determine if the number \(a_1! + a_2! + \ldots + a_n!\) is divisible by \(x!\).Here \(k!\) is a factorial of \(k\) β the product of all positive integers less than or equal to \(k\). For example, \(3! = 1 \cdot 2 \cdot 3 = 6\), and \(5! = 1 \cdot 2 \cdot 3 \cdot 4 \cdot 5 = 120\).
|
The first line contains two integers \(n\) and \(x\) (\(1 \le n \le 500\,000\), \(1 \le x \le 500\,000\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le x\)) β elements of given array.
|
In the only line print ""Yes"" (without quotes) if \(a_1! + a_2! + \ldots + a_n!\) is divisible by \(x!\), and ""No"" (without quotes) otherwise.
|
In the first example \(3! + 2! + 2! + 2! + 3! + 3! = 6 + 2 + 2 + 2 + 6 + 6 = 24\). Number \(24\) is divisible by \(4! = 24\).In the second example \(3! + 2! + 2! + 2! + 2! + 2! + 1! + 1! = 18\), is divisible by \(3! = 6\).In the third example \(7! + 7! + 7! + 7! + 7! + 7! + 7! = 7 \cdot 7!\). It is easy to prove that this number is not divisible by \(8!\).
|
Input: 6 4 3 2 2 2 3 3 | Output: Yes
|
Medium
| 2 | 376 | 220 | 145 | 17 |
707 |
C
|
707C
|
C. Pythagorean Triples
| 1,500 |
math; number theory
|
Katya studies in a fifth grade. Recently her class studied right triangles and the Pythagorean theorem. It appeared, that there are triples of positive integers such that you can construct a right triangle with segments of lengths corresponding to triple. Such triples are called Pythagorean triples.For example, triples (3, 4, 5), (5, 12, 13) and (6, 8, 10) are Pythagorean triples.Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse.Katya had no problems with completing this task. Will you do the same?
|
The only line of the input contains single integer n (1 β€ n β€ 109) β the length of some side of a right triangle.
|
Print two integers m and k (1 β€ m, k β€ 1018), such that n, m and k form a Pythagorean triple, in the only line.In case if there is no any Pythagorean triple containing integer n, print - 1 in the only line. If there are many answers, print any of them.
|
Illustration for the first sample.
|
Input: 3 | Output: 4 5
|
Medium
| 2 | 681 | 113 | 252 | 7 |
1,051 |
F
|
1051F
|
F. The Shortest Statement
| 2,400 |
graphs; shortest paths; trees
|
You are given a weighed undirected connected graph, consisting of \(n\) vertices and \(m\) edges.You should answer \(q\) queries, the \(i\)-th query is to find the shortest distance between vertices \(u_i\) and \(v_i\).
|
The first line contains two integers \(n\) and \(m~(1 \le n, m \le 10^5, m - n \le 20)\) β the number of vertices and edges in the graph.Next \(m\) lines contain the edges: the \(i\)-th edge is a triple of integers \(v_i, u_i, d_i~(1 \le u_i, v_i \le n, 1 \le d_i \le 10^9, u_i \neq v_i)\). This triple means that there is an edge between vertices \(u_i\) and \(v_i\) of weight \(d_i\). It is guaranteed that graph contains no self-loops and multiple edges.The next line contains a single integer \(q~(1 \le q \le 10^5)\) β the number of queries.Each of the next \(q\) lines contains two integers \(u_i\) and \(v_i~(1 \le u_i, v_i \le n)\) β descriptions of the queries.Pay attention to the restriction \(m - n ~ \le ~ 20\).
|
Print \(q\) lines.The \(i\)-th line should contain the answer to the \(i\)-th query β the shortest distance between vertices \(u_i\) and \(v_i\).
|
Input: 3 31 2 32 3 13 1 531 21 32 3 | Output: 341
|
Expert
| 3 | 219 | 724 | 145 | 10 |
|
2,107 |
D
|
2107D
|
D. Apple Tree Traversing
| 2,100 |
brute force; dfs and similar; greedy; implementation; trees
|
There is an apple tree with \(n\) nodes, initially with one apple at each node. You have a paper with you, initially with nothing written on it.You are traversing on the apple tree, by doing the following action as long as there is at least one apple left: Choose an apple path \((u,v)\). A path \((u,v)\) is called an apple path if and only if for every node on the path \((u,v)\), there's an apple on it. Let \(d\) be the number of apples on the path, write down three numbers \((d,u,v)\), in this order, on the paper. Then remove all the apples on the path \((u,v)\). Here, the path \((u, v)\) refers to the sequence of vertices on the unique shortest walk from \(u\) to \(v\). Let the number sequence on the paper be \(a\). Your task is to find the lexicographically largest possible sequence \(a\).
|
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 number \(n\) (\(1 \le n \le 1.5 \cdot 10^5\)).The following \(n-1\) lines of each test case contain two numbers \(u,v\) (\(1 \le u,v \le n\)). It's guaranteed that the input forms a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1.5 \cdot 10^5\).
|
For each test case, output the lexicographically largest sequence possible \(a_1, a_2, \ldots, a_{|a|}\). It can be shown that \(|a| \le 3 \cdot n\).
|
In the first test case, we do the following steps: Choose the apple path \((4, 3)\). This path consists of the nodes \(4, 1, 3\), and each of them have an apple (so it is a valid apple path). \(d = 3\) as there are \(3\) apples on this path. Append \(3, 4, 3\) in that order to our sequence \(a\). Now, remove the apples from these \(3\) vertices. Only node \(2\) has an apple left. Choose the apple path \((2, 2)\). This path only consists of the single node \(2\). \(d = 1\) as there is \(1\) apple on this path. Append \(1, 2, 2\) in that order to our sequence \(a\) and remove the apple from \(2\). The final sequence is thus \([3, 4, 3, 1, 2, 2]\). It can be shown this is the lexicographically largest sequence possible.
|
Input: 641 21 31 442 12 42 351 22 33 44 5186 33 55 44 25 11 83 763 22 62 55 44 1 | Output: 3 4 3 1 2 2 3 4 3 1 1 1 5 5 1 1 1 1 5 8 7 2 4 2 1 6 6 5 6 1 1 3 3
|
Hard
| 5 | 803 | 487 | 149 | 21 |
284 |
B
|
284B
|
B. Cows and Poker Game
| 1,000 |
brute force; implementation
|
There are n cows playing poker at a table. For the current betting phase, each player's status is either ""ALLIN"", ""IN"", or ""FOLDED"", and does not change throughout the phase. To increase the suspense, a player whose current status is not ""FOLDED"" may show his/her hand to the table. However, so as not to affect any betting decisions, he/she may only do so if all other players have a status of either ""ALLIN"" or ""FOLDED"". The player's own status may be either ""ALLIN"" or ""IN"".Find the number of cows that can currently show their hands without affecting any betting decisions.
|
The first line contains a single integer, n (2 β€ n β€ 2Β·105). The second line contains n characters, each either ""A"", ""I"", or ""F"". The i-th character is ""A"" if the i-th player's status is ""ALLIN"", ""I"" if the i-th player's status is ""IN"", or ""F"" if the i-th player's status is ""FOLDED"".
|
The first line should contain a single integer denoting the number of players that can currently show their hands.
|
In the first sample, cows 1, 4, 5, and 6 can show their hands. In the second sample, only cow 3 can show her hand.
|
Input: 6AFFAAA | Output: 4
|
Beginner
| 2 | 593 | 302 | 114 | 2 |
138 |
C
|
138C
|
C. Mushroom Gnomes - 2
| 2,200 |
binary search; data structures; probabilities; sortings
|
One day Natalia was walking in the woods when she met a little mushroom gnome. The gnome told her the following story:Everybody knows that the mushroom gnomes' power lies in the magic mushrooms that grow in the native woods of the gnomes. There are n trees and m magic mushrooms in the woods: the i-th tree grows at a point on a straight line with coordinates ai and has the height of hi, the j-th mushroom grows at the point with coordinates bj and has magical powers zj.But one day wild mushroommunchers, the sworn enemies of mushroom gnomes unleashed a terrible storm on their home forest. As a result, some of the trees began to fall and crush the magic mushrooms. The supreme oracle of mushroom gnomes calculated in advance the probability for each tree that it will fall to the left, to the right or will stand on. If the tree with the coordinate x and height h falls to the left, then all the mushrooms that belong to the right-open interval [x - h, x), are destroyed. If a tree falls to the right, then the mushrooms that belong to the left-open interval (x, x + h] are destroyed. Only those mushrooms that are not hit by a single tree survive.Knowing that all the trees fall independently of each other (i.e., all the events are mutually independent, and besides, the trees do not interfere with other trees falling in an arbitrary direction), the supreme oracle was also able to quickly calculate what would be the expectation of the total power of the mushrooms which survived after the storm. His calculations ultimately saved the mushroom gnomes from imminent death.Natalia, as a good Olympiad programmer, got interested in this story, and she decided to come up with a way to quickly calculate the expectation of the sum of the surviving mushrooms' power.
|
The first line contains two integers n and m (1 β€ n β€ 105, 1 β€ m β€ 104) β the number of trees and mushrooms, respectively.Each of the next n lines contain four integers β ai, hi, li, ri (|ai| β€ 109, 1 β€ hi β€ 109, 0 β€ li, ri, li + ri β€ 100) which represent the coordinate of the i-th tree, its height, the percentage of the probabilities that the tree falls to the left and to the right, respectively (the remaining percentage is the probability that the tree will stand on).Each of next m lines contain two integers bj, zj (|bj| β€ 109, 1 β€ zj β€ 103) which represent the coordinate and the magical power of the j-th mushroom, respectively.An arbitrary number of trees and mushrooms can grow in one point.
|
Print a real number β the expectation of the total magical power of the surviving mushrooms. The result is accepted with relative or absolute accuracy 10 - 4.
|
It is believed that the mushroom with the coordinate x belongs to the right-open interval [l, r) if and only if l β€ x < r. Similarly, the mushroom with the coordinate x belongs to the left-open interval (l, r] if and only if l < x β€ r.In the first test the mushroom survives with the probability of 50%, depending on where the single tree falls.In the second test the mushroom survives only if neither of the two trees falls on it. It occurs with the probability of 50% Γ 50% = 25%.Pretest β12 is the large test with 105 trees and one mushroom.
|
Input: 1 12 2 50 501 1 | Output: 0.5000000000
|
Hard
| 4 | 1,769 | 703 | 158 | 1 |
371 |
A
|
371A
|
A. K-Periodic Array
| 1,000 |
greedy; implementation; math
|
This task will exclusively concentrate only on the arrays where all elements equal 1 and/or 2.Array a is k-period if its length is divisible by k and there is such array b of length k, that a is represented by array b written exactly times consecutively. In other words, array a is k-periodic, if it has period of length k.For example, any array is n-periodic, where n is the array length. Array [2, 1, 2, 1, 2, 1] is at the same time 2-periodic and 6-periodic and array [1, 2, 1, 1, 2, 1, 1, 2, 1] is at the same time 3-periodic and 9-periodic.For the given array a, consisting only of numbers one and two, find the minimum number of elements to change to make the array k-periodic. If the array already is k-periodic, then the required value equals 0.
|
The first line of the input contains a pair of integers n, k (1 β€ k β€ n β€ 100), where n is the length of the array and the value n is divisible by k. The second line contains the sequence of elements of the given array a1, a2, ..., an (1 β€ ai β€ 2), ai is the i-th element of the array.
|
Print the minimum number of array elements we need to change to make the array k-periodic. If the array already is k-periodic, then print 0.
|
In the first sample it is enough to change the fourth element from 2 to 1, then the array changes to [2, 1, 2, 1, 2, 1].In the second sample, the given array already is 4-periodic.In the third sample it is enough to replace each occurrence of number two by number one. In this case the array will look as [1, 1, 1, 1, 1, 1, 1, 1, 1] β this array is simultaneously 1-, 3- and 9-periodic.
|
Input: 6 22 1 2 2 2 1 | Output: 1
|
Beginner
| 3 | 753 | 285 | 140 | 3 |
1,609 |
E
|
1609E
|
E. William The Oblivious
| 2,400 |
bitmasks; data structures; dp; matrices
|
Before becoming a successful trader William got a university degree. During his education an interesting situation happened, after which William started to listen to homework assignments much more attentively. What follows is a formal description of the homework assignment as William heard it:You are given a string \(s\) of length \(n\) only consisting of characters ""a"", ""b"" and ""c"". There are \(q\) queries of format (\(pos, c\)), meaning replacing the element of string \(s\) at position \(pos\) with character \(c\). After each query you must output the minimal number of characters in the string, which have to be replaced, so that the string doesn't contain string ""abc"" as a subsequence. A valid replacement of a character is replacing it with ""a"", ""b"" or ""c"".A string \(x\) is said to be a subsequence of string \(y\) if \(x\) can be obtained from \(y\) by deleting some characters without changing the ordering of the remaining characters.
|
The first line contains two integers \(n\) and \(q\) \((1 \le n, q \le 10^5)\), the length of the string and the number of queries, respectively.The second line contains the string \(s\), consisting of characters ""a"", ""b"" and ""c"".Each of the next \(q\) lines contains an integer \(i\) and character \(c\) \((1 \le i \le n)\), index and the value of the new item in the string, respectively. It is guaranteed that character's \(c\) value is ""a"", ""b"" or ""c"".
|
For each query output the minimal number of characters that would have to be replaced so that the string doesn't contain ""abc"" as a subsequence.
|
Let's consider the state of the string after each query: \(s = \)""aaaaccccc"". In this case the string does not contain ""abc"" as a subsequence and no replacements are needed. \(s = \)""aaabccccc"". In this case 1 replacements can be performed to get, for instance, string \(s = \)""aaaaccccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""ababccccc"". In this case 2 replacements can be performed to get, for instance, string \(s = \)aaaaccccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""ababacccc"". In this case 2 replacements can be performed to get, for instance, string \(s = \)""aaaaacccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""bbabacccc"". In this case 1 replacements can be performed to get, for instance, string \(s = \)""bbacacccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""bbababccc"". In this case 2 replacements can be performed to get, for instance, string \(s = \)""bbacacccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""bbabcbccc"". In this case 1 replacements can be performed to get, for instance, string \(s = \)""bbcbcbccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""baabcbccc"". In this case 2 replacements can be performed to get, for instance, string \(s = \)""bbbbcbccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""aaabcbccc"". In this case 2 replacements can be performed to get, for instance, string \(s = \)""aaacccccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""aaababccc"". In this case 2 replacements can be performed to get, for instance, string \(s = \)""aaacacccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""aaababccc"". In this case 2 replacements can be performed to get, for instance, string \(s = \)""aaacacccc"". This string does not contain ""abc"" as a subsequence. \(s = \)""aaababbcc"". In this case 2 replacements can be performed to get, for instance, string \(s = \)""aaababbbb"". This string does not contain ""abc"" as a subsequence.
|
Input: 9 12 aaabccccc 4 a 4 b 2 b 5 a 1 b 6 b 5 c 2 a 1 a 5 a 6 b 7 b | Output: 0 1 2 2 1 2 1 2 2 2 2 2
|
Expert
| 4 | 964 | 468 | 146 | 16 |
610 |
C
|
610C
|
C. Harmony Analysis
| 1,800 |
constructive algorithms
|
The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or - 1 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is: .Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2k vectors in 2k-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it?
|
The only line of the input contains a single integer k (0 β€ k β€ 9).
|
Print 2k lines consisting of 2k characters each. The j-th character of the i-th line must be equal to ' * ' if the j-th coordinate of the i-th vector is equal to - 1, and must be equal to ' + ' if it's equal to + 1. It's guaranteed that the answer always exists.If there are many correct answers, print any.
|
Consider all scalar products in example: Vectors 1 and 2: ( + 1)Β·( + 1) + ( + 1)Β·( - 1) + ( - 1)Β·( + 1) + ( - 1)Β·( - 1) = 0 Vectors 1 and 3: ( + 1)Β·( + 1) + ( + 1)Β·( + 1) + ( - 1)Β·( + 1) + ( - 1)Β·( + 1) = 0 Vectors 1 and 4: ( + 1)Β·( + 1) + ( + 1)Β·( - 1) + ( - 1)Β·( - 1) + ( - 1)Β·( + 1) = 0 Vectors 2 and 3: ( + 1)Β·( + 1) + ( - 1)Β·( + 1) + ( + 1)Β·( + 1) + ( - 1)Β·( + 1) = 0 Vectors 2 and 4: ( + 1)Β·( + 1) + ( - 1)Β·( - 1) + ( + 1)Β·( - 1) + ( - 1)Β·( + 1) = 0 Vectors 3 and 4: ( + 1)Β·( + 1) + ( + 1)Β·( - 1) + ( + 1)Β·( - 1) + ( + 1)Β·( + 1) = 0
|
Input: 2 | Output: ++**+*+*+++++**+
|
Medium
| 1 | 807 | 67 | 307 | 6 |
2,085 |
C
|
2085C
|
C. Serval and The Formula
| 1,600 |
bitmasks; constructive algorithms; dp; greedy
|
You are given two positive integers \(x\) and \(y\) (\(1\le x, y\le 10^9\)).Find a non-negative integer \(k\le 10^{18}\), such that \((x+k) + (y+k) = (x+k)\oplus (y+k)\) holds\(^{\text{β}}\), or determine that such an integer does not exist.\(^{\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 only line of each test case contains two integers \(x\) and \(y\) (\(1\le x, y\le 10^9\)) β the given integers.
|
For each test case, output a single integer \(k\) (\(0\le k\le 10^{18}\)) β the integer you found. Print \(-1\) if it is impossible to find such an integer.If there are multiple answers, you may print any of them.
|
In the first test case, since \((2 + 0) + (5 + 0) = (2 + 0) \oplus (5 + 0) = 7\), \(k=0\) is a possible answer. Note that \(k=4\) is also a possible answer because \((2 + 4) + (5 + 4) = (2 + 4) \oplus (5 + 4) = 15\).In the second test case, \((x+k)\oplus (y+k) = (6+k)\oplus (6+k) = 0\). However, \((x+k)+(y+k) > 0\) holds for every \(k \ge 0\), implying that such an integer \(k\) does not exist.
|
Input: 52 56 619 101024 40961198372 599188 | Output: 0 -1 1 1024 28
|
Medium
| 4 | 301 | 277 | 213 | 20 |
1,148 |
G
|
1148G
|
G. Gold Experience
| 3,300 |
constructive algorithms; graphs; math; number theory; probabilities
|
Consider an undirected graph \(G\) with \(n\) vertices. There is a value \(a_i\) in each vertex.Two vertices \(i\) and \(j\) are connected with an edge if and only if \(gcd(a_i, a_j) > 1\), where \(gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\).Consider a set of vertices. Let's call a vertex in this set fair if it is connected with an edge with all other vertices in this set.You need to find a set of \(k\) vertices (where \(k\) is a given integer, \(2 \cdot k \le n\)) where all vertices are fair or all vertices are not fair. One can show that such a set always exists.
|
The first line contains integers \(n\) and \(k\) (\(6 \leq 2 \cdot k \leq n \leq 10^5\)) β the number of vertices and parameter \(k\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(2 \le a_i \le 10^7\)) β the values in the vertices.
|
Print exactly \(k\) distinct integers β the indices of the vertices in the chosen set in any order.
|
In the first test case, set \(\{2, 4, 5\}\) is an example of set where no vertices are fair. The vertex \(2\) does not share an edge with vertex \(4\) since \(gcd(15, 8) = 1\). The vertex \(4\) does not share an edge with vertex \(2\). The vertex \(5\) does not share an edge with vertex \(2\).In the second test case, set \(\{8, 5, 6, 4\}\) is an example of a set where all vertices are fair.
|
Input: 6 3 6 15 10 8 14 12 | Output: 1 3 6
|
Master
| 5 | 609 | 254 | 99 | 11 |
1,200 |
D
|
1200D
|
D. White Lines
| 1,900 |
brute force; data structures; dp; implementation; two pointers
|
Gildong has bought a famous painting software cfpaint. The working screen of cfpaint is square-shaped consisting of \(n\) rows and \(n\) columns of square cells. The rows are numbered from \(1\) to \(n\), from top to bottom, and the columns are numbered from \(1\) to \(n\), from left to right. The position of a cell at row \(r\) and column \(c\) is represented as \((r, c)\). There are only two colors for the cells in cfpaint β black and white.There is a tool named eraser in cfpaint. The eraser has an integer size \(k\) (\(1 \le k \le n\)). To use the eraser, Gildong needs to click on a cell \((i, j)\) where \(1 \le i, j \le n - k + 1\). When a cell \((i, j)\) is clicked, all of the cells \((i', j')\) where \(i \le i' \le i + k - 1\) and \(j \le j' \le j + k - 1\) become white. In other words, a square with side equal to \(k\) cells and top left corner at \((i, j)\) is colored white.A white line is a row or a column without any black cells.Gildong has worked with cfpaint for some time, so some of the cells (possibly zero or all) are currently black. He wants to know the maximum number of white lines after using the eraser exactly once. Help Gildong find the answer to his question.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2000\)) β the number of rows and columns, and the size of the eraser.The next \(n\) lines contain \(n\) characters each without spaces. The \(j\)-th character in the \(i\)-th line represents the cell at \((i,j)\). Each character is given as either 'B' representing a black cell, or 'W' representing a white cell.
|
Print one integer: the maximum number of white lines after using the eraser exactly once.
|
In the first example, Gildong can click the cell \((2, 2)\), then the working screen becomes: BWWWWWWWWWWWWWWBThen there are four white lines β the \(2\)-nd and \(3\)-rd row, and the \(2\)-nd and \(3\)-rd column.In the second example, clicking the cell \((2, 3)\) makes the \(2\)-nd row a white line.In the third example, both the \(2\)-nd column and \(5\)-th row become white lines by clicking the cell \((3, 2)\).
|
Input: 4 2 BWWW WBBW WBBW WWWB | Output: 4
|
Hard
| 5 | 1,198 | 386 | 89 | 12 |
1,579 |
D
|
1579D
|
D. Productive Meeting
| 1,400 |
constructive algorithms; graphs; greedy
|
An important meeting is to be held and there are exactly \(n\) people invited. At any moment, any two people can step back and talk in private. The same two people can talk several (as many as they want) times per meeting.Each person has limited sociability. The sociability of the \(i\)-th person is a non-negative integer \(a_i\). This means that after exactly \(a_i\) talks this person leaves the meeting (and does not talk to anyone else anymore). If \(a_i = 0\), the \(i\)-th person leaves the meeting immediately after it starts.A meeting is considered most productive if the maximum possible number of talks took place during it.You are given an array of sociability \(a\), determine which people should talk to each other so that the total number of talks is as large as possible.
|
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The next \(2t\) lines contain descriptions of the test cases.The first line of each test case description contains an integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) βthe number of people in the meeting. The second line consists of \(n\) space-separated integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2 \cdot 10^5\)) β the sociability parameters of all people. It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). It is also guaranteed that the sum of all \(a_i\) (over all test cases and all \(i\)) does not exceed \(2 \cdot 10^5\).
|
Print \(t\) answers to all test cases.On the first line of each answer print the number \(k\) β the maximum number of talks possible in a meeting.On each of the next \(k\) lines print two integers \(i\) and \(j\) (\(1 \le i, j \le n\) and \(i \neq j\)) β the numbers of people who will have another talk.If there are several possible answers, you may print any of them.
|
Input: 8 2 2 3 3 1 2 3 4 1 2 3 4 3 0 0 2 2 6 2 3 0 0 2 5 8 2 0 1 1 5 0 1 0 0 6 | Output: 2 1 2 1 2 3 1 3 2 3 2 3 5 1 3 2 4 2 4 3 4 3 4 0 2 1 2 1 2 0 4 1 2 1 5 1 4 1 2 1 5 2
|
Easy
| 3 | 788 | 665 | 369 | 15 |
|
256 |
D
|
256D
|
D. Liars and Serge
| 2,700 |
dp
|
There are n people, sitting in a line at the table. For each person we know that he always tells either the truth or lies.Little Serge asked them: how many of you always tell the truth? Each of the people at the table knows everything (who is an honest person and who is a liar) about all the people at the table. The honest people are going to say the correct answer, the liars are going to say any integer from 1 to n, which is not the correct answer. Every liar chooses his answer, regardless of the other liars, so two distinct liars may give distinct answer.Serge does not know any information about the people besides their answers to his question. He took a piece of paper and wrote n integers a1, a2, ..., an, where ai is the answer of the i-th person in the row. Given this sequence, Serge determined that exactly k people sitting at the table apparently lie.Serge wonders, how many variants of people's answers (sequences of answers a of length n) there are where one can say that exactly k people sitting at the table apparently lie. As there can be rather many described variants of answers, count the remainder of dividing the number of the variants by 777777777.
|
The first line contains two integers n, k, (1 β€ k β€ n β€ 28). It is guaranteed that n β is the power of number 2.
|
Print a single integer β the answer to the problem modulo 777777777.
|
Input: 1 1 | Output: 0
|
Master
| 1 | 1,176 | 112 | 68 | 2 |
|
1,731 |
A
|
1731A
|
A. Joey Takes Money
| 800 |
greedy; math
|
Joey is low on money. His friend Chandler wants to lend Joey some money, but can't give him directly, as Joey is too proud of himself to accept it. So, in order to trick him, Chandler asks Joey to play a game.In this game, Chandler gives Joey an array \(a_1, a_2, \dots, a_n\) (\(n \geq 2\)) of positive integers (\(a_i \ge 1\)).Joey can perform the following operation on the array any number of times: Take two indices \(i\) and \(j\) (\(1 \le i < j \le n)\). Choose two integers \(x\) and \(y\) (\(x, y \ge 1\)) such that \(x \cdot y = a_i \cdot a_j\). Replace \(a_i\) by \(x\) and \(a_j\) by \(y\). In the end, Joey will get the money equal to the sum of elements of the final array. Find the maximum amount of money \(\mathrm{ans}\) Joey can get but print \(2022 \cdot \mathrm{ans}\). Why multiplied by \(2022\)? Because we are never gonna see it again!It is guaranteed that the product of all the elements of the array \(a\) doesn't exceed \(10^{12}\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 4000\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 50\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^6\)) β the array itself.It's guaranteed that the product of all \(a_i\) doesn't exceed \(10^{12}\) (i. e. \(a_1 \cdot a_2 \cdot \ldots \cdot a_n \le 10^{12}\)).
|
For each test case, print the maximum money Joey can get multiplied by \(2022\).
|
In the first test case, Joey can do the following: He chooses \(i = 1\) and \(j = 2\) (so he has \(a[i] \cdot a[j] = 6\)), chooses \(x = 6\) and \(y = 1\) and makes \(a[i] = 6\) and \(a[j] = 1\). $$$\([2, 3, 2] \xrightarrow[x = 6,\; y = 1]{i = 1,\; j = 2} [6, 1, 2]\)\( He chooses \)i = 1\( and \)j = 3\( (so he has \)a[i] \cdot a[j] = 12\(), chooses \)x = 12\( and \)y = 1\( and makes \)a[i] = 12\( and \)a[j] = 1\(. \)\([6, 1, 2] \xrightarrow[x = 12,\; y = 1]{i = 1,\; j = 3} [12, 1, 1]\)\( The sum is \)14\( which is the maximum of all possible sums. The answer is \)2022 \cdot 14 = 28308$$$.
|
Input: 332 3 221 331000000 1000000 1 | Output: 28308 8088 2022000000004044
|
Beginner
| 2 | 958 | 526 | 80 | 17 |
1,622 |
B
|
1622B
|
B. Berland Music
| 1,000 |
data structures; greedy; math; sortings
|
Berland Music is a music streaming service built specifically to support Berland local artist. Its developers are currently working on a song recommendation module.So imagine Monocarp got recommended \(n\) songs, numbered from \(1\) to \(n\). The \(i\)-th song had its predicted rating equal to \(p_i\), where \(1 \le p_i \le n\) and every integer from \(1\) to \(n\) appears exactly once. In other words, \(p\) is a permutation.After listening to each of them, Monocarp pressed either a like or a dislike button. Let his vote sequence be represented with a string \(s\), such that \(s_i=0\) means that he disliked the \(i\)-th song, and \(s_i=1\) means that he liked it.Now the service has to re-evaluate the song ratings in such a way that: the new ratings \(q_1, q_2, \dots, q_n\) still form a permutation (\(1 \le q_i \le n\); each integer from \(1\) to \(n\) appears exactly once); every song that Monocarp liked should have a greater rating than every song that Monocarp disliked (formally, for all \(i, j\) such that \(s_i=1\) and \(s_j=0\), \(q_i>q_j\) should hold). Among all valid permutations \(q\) find the one that has the smallest value of \(\sum\limits_{i=1}^n |p_i-q_i|\), where \(|x|\) is an absolute value of \(x\).Print the permutation \(q_1, q_2, \dots, q_n\). If there are multiple answers, you can print any of them.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of songs.The second line of each testcase contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n\)) β the permutation of the predicted ratings.The third line contains a single string \(s\), consisting of \(n\) characters. Each character is either a \(0\) or a \(1\). \(0\) means that Monocarp disliked the song, and \(1\) means that he liked it.The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print a permutation \(q\) β the re-evaluated ratings of the songs. If there are multiple answers such that \(\sum\limits_{i=1}^n |p_i-q_i|\) is minimum possible, you can print any of them.
|
In the first testcase, there exists only one permutation \(q\) such that each liked song is rating higher than each disliked song: song \(1\) gets rating \(2\) and song \(2\) gets rating \(1\). \(\sum\limits_{i=1}^n |p_i-q_i|=|1-2|+|2-1|=2\).In the second testcase, Monocarp liked all songs, so all permutations could work. The permutation with the minimum sum of absolute differences is the permutation equal to \(p\). Its cost is \(0\).
|
Input: 3 2 1 2 10 3 3 1 2 111 8 2 3 1 8 5 4 7 6 01110001 | Output: 2 1 3 1 2 1 6 5 8 3 2 4 7
|
Beginner
| 4 | 1,338 | 630 | 207 | 16 |
191 |
D
|
191D
|
D. Metro Scheme
| 2,700 |
graphs; greedy
|
Berland is very concerned with privacy, so almost all plans and blueprints are secret. However, a spy of the neighboring state managed to steal the Bertown subway scheme.The Bertown Subway has n stations, numbered from 1 to n, and m bidirectional tunnels connecting them. All Bertown Subway consists of lines. To be more precise, there are two types of lines: circular and radial.A radial line is a sequence of stations v1, ..., vk (k > 1), where stations vi and vi + 1 (i < k) are connected by a tunnel and no station occurs in the line more than once (vi β vj for i β j).A loop line is a series of stations, v1, ..., vk (k > 2), where stations vi ΠΈ vi + 1 are connected by a tunnel. In addition, stations v1 and vk are also connected by a tunnel. No station is occurs in the loop line more than once.Note that a single station can be passed by any number of lines.According to Berland standards, there can't be more than one tunnel between two stations and each tunnel belongs to exactly one line. Naturally, each line has at least one tunnel. Between any two stations there is the way along the subway tunnels. In addition, in terms of graph theory, a subway is a vertex cactus: if we consider the subway as a graph in which the stations are the vertexes and the edges are tunnels, then each vertex lies on no more than one simple cycle.Unfortunately, scheme, stolen by the spy, had only the stations and the tunnels. It was impossible to determine to which line every tunnel corresponds. But to sabotage successfully, the spy needs to know what minimum and maximum number of lines may be in the Bertown subway.Help him!
|
The first line contains two integers n and m (1 β€ n β€ 105, 0 β€ m β€ 3Β·105) β the number of stations and the number of tunnels, correspondingly.Each of the next m lines contain two integers β the numbers of stations connected by the corresponding tunnel. The stations are numbered with integers from 1 to n.It is guaranteed that the graph that corresponds to the subway has no multiple edges or loops, it is connected and it is a vertex cactus.
|
Print two numbers β the minimum and maximum number of lines correspondingly.
|
The subway scheme with minimum possible number of lines for the second sample is:
|
Input: 3 31 22 33 1 | Output: 1 3
|
Master
| 2 | 1,623 | 442 | 76 | 1 |
1,369 |
A
|
1369A
|
A. FashionabLee
| 800 |
geometry; math
|
Lee is going to fashionably decorate his house for a party, using some regular convex polygons...Lee thinks a regular \(n\)-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the \(OX\)-axis and at least one of its edges is parallel to the \(OY\)-axis at the same time.Recall that a regular \(n\)-sided polygon is a convex polygon with \(n\) vertices such that all the edges and angles are equal.Now he is shopping: the market has \(t\) regular polygons. For each of them print YES if it is beautiful and NO otherwise.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of polygons in the market. Each of the next \(t\) lines contains a single integer \(n_i\) (\(3 \le n_i \le 10^9\)): it means that the \(i\)-th polygon is a regular \(n_i\)-sided polygon.
|
For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).
|
In the example, there are \(4\) polygons in the market. It's easy to see that an equilateral triangle (a regular \(3\)-sided polygon) is not beautiful, a square (a regular \(4\)-sided polygon) is beautiful and a regular \(12\)-sided polygon (is shown below) is beautiful as well.
|
Input: 4 3 4 12 1000000000 | Output: NO YES YES YES
|
Beginner
| 2 | 595 | 269 | 81 | 13 |
977 |
C
|
977C
|
C. Less or Equal
| 1,200 |
sortings
|
You are given a sequence of integers of length \(n\) and integer number \(k\). You should print any integer number \(x\) in the range of \([1; 10^9]\) (i.e. \(1 \le x \le 10^9\)) such that exactly \(k\) elements of given sequence are less than or equal to \(x\).Note that the sequence can contain equal elements.If there is no such \(x\), print ""-1"" (without quotes).
|
The first line of the input contains integer numbers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le k \le n\)). The second line of the input contains \(n\) integer numbers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the sequence itself.
|
Print any integer number \(x\) from range \([1; 10^9]\) such that exactly \(k\) elements of given sequence is less or equal to \(x\).If there is no such \(x\), print ""-1"" (without quotes).
|
In the first example \(5\) is also a valid answer because the elements with indices \([1, 3, 4, 6]\) is less than or equal to \(5\) and obviously less than or equal to \(6\).In the second example you cannot choose any number that only \(2\) elements of the given sequence will be less than or equal to this number because \(3\) elements of the given sequence will be also less than or equal to this number.
|
Input: 7 43 7 5 1 10 3 20 | Output: 6
|
Easy
| 1 | 369 | 252 | 190 | 9 |
1,552 |
C
|
1552C
|
C. Maximize the Intersections
| 1,800 |
combinatorics; constructive algorithms; geometry; greedy; sortings
|
On a circle lie \(2n\) distinct points, with the following property: however you choose \(3\) chords that connect \(3\) disjoint pairs of points, no point strictly inside the circle belongs to all \(3\) chords. The points are numbered \(1, \, 2, \, \dots, \, 2n\) in clockwise order.Initially, \(k\) chords connect \(k\) pairs of points, in such a way that all the \(2k\) endpoints of these chords are distinct.You want to draw \(n - k\) additional chords that connect the remaining \(2(n - k)\) points (each point must be an endpoint of exactly one chord).In the end, let \(x\) be the total number of intersections among all \(n\) chords. Compute the maximum value that \(x\) can attain if you choose the \(n - k\) chords optimally.Note that the exact position of the \(2n\) points is not relevant, as long as the property stated in the first paragraph holds.
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 100\), \(0 \le k \le n\)) β half the number of points and the number of chords initially drawn.Then \(k\) lines follow. The \(i\)-th of them contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, \, y_i \le 2n\), \(x_i \ne y_i\)) β the endpoints of the \(i\)-th chord. It is guaranteed that the \(2k\) numbers \(x_1, \, y_1, \, x_2, \, y_2, \, \dots, \, x_k, \, y_k\) are all distinct.
|
For each test case, output the maximum number of intersections that can be obtained by drawing \(n - k\) additional chords.
|
In the first test case, there are three ways to draw the \(2\) additional chords, shown below (black chords are the ones initially drawn, while red chords are the new ones): We see that the third way gives the maximum number of intersections, namely \(4\).In the second test case, there are no more chords to draw. Of course, with only one chord present there are no intersections.In the third test case, we can make at most one intersection by drawing chords \(1-3\) and \(2-4\), as shown below:
|
Input: 4 4 2 8 2 1 5 1 1 2 1 2 0 10 6 14 6 2 20 9 10 13 18 15 12 11 7 | Output: 4 0 1 14
|
Medium
| 5 | 860 | 598 | 123 | 15 |
1,537 |
A
|
1537A
|
A. Arithmetic Array
| 800 |
greedy; math
|
An array \(b\) of length \(k\) is called good if its arithmetic mean is equal to \(1\). More formally, if $$$\(\frac{b_1 + \cdots + b_k}{k}=1.\)\(Note that the value \)\frac{b_1+\cdots+b_k}{k}\( is not rounded up or down. For example, the array \)[1,1,1,2]\( has an arithmetic mean of \)1.25\(, which is not equal to \)1\(.You are given an integer array \)a\( of length \)n$$$. In an operation, you can append a non-negative integer to the end of the array. What's the minimum number of operations required to make the array good?We have a proof that it is always possible with finitely many operations.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 50\)) β the length of the initial array \(a\).The second line of each test case contains \(n\) integers \(a_1,\ldots,a_n\) (\(-10^4\leq a_i \leq 10^4\)), the elements of the array.
|
For each test case, output a single integer β the minimum number of non-negative integers you have to append to the array so that the arithmetic mean of the array will be exactly \(1\).
|
In the first test case, we don't need to add any element because the arithmetic mean of the array is already \(1\), so the answer is \(0\).In the second test case, the arithmetic mean is not \(1\) initially so we need to add at least one more number. If we add \(0\) then the arithmetic mean of the whole array becomes \(1\), so the answer is \(1\).In the third test case, the minimum number of elements that need to be added is \(16\) since only non-negative integers can be added.In the fourth test case, we can add a single integer \(4\). The arithmetic mean becomes \(\frac{-2+4}{2}\) which is equal to \(1\).
|
Input: 4 3 1 1 1 2 1 2 4 8 4 6 2 1 -2 | Output: 0 1 16 1
|
Beginner
| 2 | 603 | 391 | 185 | 15 |
1,616 |
D
|
1616D
|
D. Keep the Average High
| 2,000 |
dp; greedy; math
|
You are given an array of integers \(a_1, a_2, \ldots, a_n\) and an integer \(x\).You need to select the maximum number of elements in the array, such that for every subsegment \(a_l, a_{l + 1}, \ldots, a_r\) containing strictly more than one element \((l < r)\), either: At least one element on this subsegment is not selected, or \(a_l + a_{l+1} + \ldots + a_r \geq x \cdot (r - l + 1)\).
|
The first line of input contains one integer \(t\) (\(1 \leq t \leq 10\)): the number of test cases.The descriptions of \(t\) test cases follow, three lines per test case.In the first line you are given one integer \(n\) (\(1 \leq n \leq 50\,000\)): the number of integers in the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-100\,000 \leq a_i \leq 100\,000\)).The third line contains one integer \(x\) (\(-100\,000 \leq x \leq 100\,000\)).
|
For each test case, print one integer: the maximum number of elements that you can select.
|
In the first example, one valid way to select the elements is \([\underline{1}, 2, \underline{3}, \underline{4}, \underline{5}]\). All subsegments satisfy at least one of the criteria. For example, for the subsegment \(l = 1\), \(r = 2\) we have that the element \(2\) is not selected, satisfying the first criterion. For the subsegment \(l = 3\), \(r = 5\) we have \(3 + 4 + 5 = 12 \ge 2 \cdot 3\), satisfying the second criterion.We can't select all elements, because in this case for \(l = 1\), \(r = 2\) all elements are selected and we have \(a_1 + a_2 = 3 < 2 \cdot 2\). Thus, the maximum number of selected elements is \(4\).In the second example, one valid solution is \([\underline{2}, \underline{4}, 2, \underline{4}, \underline{2}, \underline{4}, 2, \underline{4}, \underline{2}, \underline{4}]\).In the third example, one valid solution is \([\underline{-10}, -5, \underline{-10}]\).In the fourth example, one valid solution is \([\underline{9}, \underline{9}, -3]\).
|
Input: 4 5 1 2 3 4 5 2 10 2 4 2 4 2 4 2 4 2 4 3 3 -10 -5 -10 -8 3 9 9 -3 5 | Output: 4 8 2 2
|
Hard
| 3 | 390 | 470 | 90 | 16 |
1,862 |
C
|
1862C
|
C. Flower City Fence
| 1,100 |
binary search; data structures; implementation; sortings
|
Anya lives in the Flower City. By order of the city mayor, she has to build a fence for herself.The fence consists of \(n\) planks, each with a height of \(a_i\) meters. According to the order, the heights of the planks must not increase. In other words, it is true that \(a_i \ge a_j\) for all \(i < j\).Anya became curious whether her fence is symmetrical with respect to the diagonal. In other words, will she get the same fence if she lays all the planks horizontally in the same order.For example, for \(n = 5\), \(a = [5, 4, 3, 2, 1]\), the fence is symmetrical. Because if all the planks are laid horizontally, the fence will be \([5, 4, 3, 2, 1]\), as shown in the diagram. On the left is the fence \([5, 4, 3, 2, 1]\), on the right is the same fence laid horizontally But for \(n = 3\), \(a = [4, 2, 1]\), the fence is not symmetrical. Because if all the planks are laid horizontally, the fence will be \([3, 2, 1, 1]\), as shown in the diagram. On the left is the fence \([4, 2, 1]\), on the right is the same fence laid horizontally Help Anya and determine whether her fence is symmetrical.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of a test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the fence.The second line of a test case contains \(n\) integers \(a_1 \ge a_2 \ge a_3 \ge \dots \ge a_n\) (\(1 \le a_i \le 10^9\)) β the heights of the planks.The sum of the values of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output ""YES"" if the fence is symmetrical, otherwise output ""NO"".You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be accepted as a positive answer.
|
In the first and second test cases of the example, the fence is symmetrical.In the third test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be \([3, 2, 1, 1]\).In the fourth test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be \([1, 1]\).In the fifth and sixth test cases of the example, the fence is symmetrical.In the seventh test case of the example, the fence is not symmetrical. If the planks are laid horizontally, the fence will be \([2, 1, 1, 1, 1, 1]\).
|
Input: 755 4 3 2 133 1 134 2 11255 3 3 1 155 5 5 3 326 1 | Output: YES YES NO NO YES YES NO
|
Easy
| 4 | 1,101 | 499 | 254 | 18 |
542 |
F
|
542F
|
F. Quest
| 2,100 |
dp; greedy
|
Polycarp is making a quest for his friends. He has already made n tasks, for each task the boy evaluated how interesting it is as an integer qi, and the time ti in minutes needed to complete the task. An interesting feature of his quest is: each participant should get the task that is best suited for him, depending on his preferences. The task is chosen based on an interactive quiz that consists of some questions. The player should answer these questions with ""yes"" or ""no"". Depending on the answer to the question, the participant either moves to another question or goes to one of the tasks that are in the quest. In other words, the quest is a binary tree, its nodes contain questions and its leaves contain tasks. We know that answering any of the questions that are asked before getting a task takes exactly one minute from the quest player. Polycarp knows that his friends are busy people and they can't participate in the quest for more than T minutes. Polycarp wants to choose some of the n tasks he made, invent the corresponding set of questions for them and use them to form an interactive quiz as a binary tree so that no matter how the player answers quiz questions, he spends at most T minutes on completing the whole quest (that is, answering all the questions and completing the task). Specifically, the quest can contain zero questions and go straight to the task. Each task can only be used once (i.e., the people who give different answers to questions should get different tasks).Polycarp wants the total ""interest"" value of the tasks involved in the quest to be as large as possible. Help him determine the maximum possible total interest value of the task considering that the quest should be completed in T minutes at any variant of answering questions.
|
The first line contains two integers n and T (1 β€ n β€ 1000, 1 β€ T β€ 100) β the number of tasks made by Polycarp and the maximum time a quest player should fit into.Next n lines contain two integers ti, qi (1 β€ ti β€ T, 1 β€ qi β€ 1000) each β the time in minutes needed to complete the i-th task and its interest value.
|
Print a single integer β the maximum possible total interest value of all the tasks in the quest.
|
In the first sample test all the five tasks can be complemented with four questions and joined into one quest.In the second sample test it is impossible to use all the five tasks, but you can take two of them, the most interesting ones.In the third sample test the optimal strategy is to include only the second task into the quest.Here is the picture that illustrates the answers to the sample tests. The blue circles represent the questions, the two arrows that go from every circle represent where a person goes depending on his answer to that question. The tasks are the red ovals.
|
Input: 5 51 11 12 23 34 4 | Output: 11
|
Hard
| 2 | 1,786 | 316 | 97 | 5 |
1,787 |
G
|
1787G
|
G. Colorful Tree Again
| 3,000 |
brute force; data structures; trees
|
An edge-weighted tree of \(n\) nodes is given with each edge colored in some color. Each node of this tree can be blocked or unblocked, all nodes are unblocked initially.A simple path is a path in a graph that does not have repeating nodes. The length of a path is defined as the sum of weights of all edges on the path.A path is good when it is a simple path consisting of edges of the same color \(c\), all edges of color \(c\) are on this path, and every node on the path is unblocked.You need to operate \(2\) kinds of queries: block a node, unblock a node. After each query, print the maximum length among all good paths. If there are no good paths, print \(0\).
|
The first line contains two integers \(n\), \(q\) (\(1 \leq n,q \leq 2\cdot 10^5\)) β the number of nodes and the number of queries.Then \(n-1\) lines follow, each containing four integers \(u\), \(v\), \(w\) and \(c\) (\(1 \leq u,v,w,c \leq n\); \(u \not = v\)), denoting a weighted edge connecting node \(u\) and node \(v\) with weight \(w\) and color \(c\). It is guaranteed that these edges form a tree.Then \(q\) lines follow, each containing two integers \(p\) and \(x\) (\(p = 0\) or \(p = 1\), \(1\leq x\leq n\)), denoting a query: if \(p = 0\), block the node \(x\). It's guaranteed that it's not blocked at this time; if \(p = 1\), unblock the node \(x\). It's guaranteed that it's blocked at this time.
|
For each query, print the maximum length of a good path. If there are no good paths, print \(0\).
|
Input: 5 4 4 1 3 4 5 2 4 4 3 1 3 2 1 2 5 1 0 4 0 3 0 2 1 3 | Output: 5 5 0 3
|
Master
| 3 | 667 | 713 | 97 | 17 |
|
768 |
G
|
768G
|
G. The Winds of Winter
| 3,300 |
binary search; data structures
|
Given a rooted tree with n nodes. The Night King removes exactly one node from the tree and all the edges associated with it. Doing this splits the tree and forms a forest. The node which is removed is not a part of the forest.The root of a tree in the forest is the node in that tree which does not have a parent. We define the strength of the forest as the size of largest tree in forest.Jon Snow wants to minimize the strength of the forest. To do this he can perform the following operation at most once.He removes the edge between a node and its parent and inserts a new edge between this node and any other node in forest such that the total number of trees in forest remain same.For each node v you need to find the minimum value of strength of the forest formed when node v is removed.
|
The first line of the input contains an integer n (1 β€ n β€ 105) β the number of vertices in the tree. Each of the next n lines contains a pair of vertex indices ui and vi (1 β€ ui, vi β€ n) where ui is the parent of vi. If ui = 0 then vi is the root.
|
Print n line each containing a single integer. The i-th of them should be equal to minimum value of strength of forest formed when i-th node is removed and Jon Snow performs the operation described above at most once.
|
The tree for first test case is depicted below. When you remove the first node, the tree splits to form the following forest. The strength of this forest is 4. Jon Snow now changes the parent of vertex 10 from 5 to 3. The strength of forest now becomes 3.
|
Input: 100 11 21 31 42 52 63 74 84 95 10 | Output: 3455599999
|
Master
| 2 | 793 | 248 | 217 | 7 |
983 |
A
|
983A
|
A. Finite or not?
| 1,700 |
implementation; math
|
You are given several queries. Each query consists of three integers \(p\), \(q\) and \(b\). You need to answer whether the result of \(p/q\) in notation with base \(b\) is a finite fraction.A fraction in notation with base \(b\) is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of queries.Next \(n\) lines contain queries, one per line. Each line contains three integers \(p\), \(q\), and \(b\) (\(0 \le p \le 10^{18}\), \(1 \le q \le 10^{18}\), \(2 \le b \le 10^{18}\)). All numbers are given in notation with base \(10\).
|
For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.
|
\(\frac{6}{12} = \frac{1}{2} = 0,5_{10}\)\(\frac{4}{3} = 1,(3)_{10}\)\(\frac{9}{36} = \frac{1}{4} = 0,01_2\)\(\frac{4}{12} = \frac{1}{3} = 0,1_3\)
|
Input: 26 12 104 3 10 | Output: FiniteInfinite
|
Medium
| 2 | 384 | 328 | 101 | 9 |
224 |
B
|
224B
|
B. Array
| 1,500 |
bitmasks; implementation; two pointers
|
You've got an array a, consisting of n integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 β€ l β€ r β€ n) such, that among numbers al, al + 1, ..., ar there are exactly k distinct numbers.Segment [l, r] (1 β€ l β€ r β€ n; l, r are integers) of length m = r - l + 1, satisfying the given property, is called minimal by inclusion, if there is no segment [x, y] satisfying the property and less then m in length, such that 1 β€ l β€ x β€ y β€ r β€ n. Note that the segment [l, r] doesn't have to be minimal in length among all segments, satisfying the given property.
|
The first line contains two space-separated integers: n and k (1 β€ n, k β€ 105). The second line contains n space-separated integers a1, a2, ..., an β elements of the array a (1 β€ ai β€ 105).
|
Print a space-separated pair of integers l and r (1 β€ l β€ r β€ n) such, that the segment [l, r] is the answer to the problem. If the sought segment does not exist, print ""-1 -1"" without the quotes. If there are multiple correct answers, print any of them.
|
In the first sample among numbers a1 and a2 there are exactly two distinct numbers.In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.In the third sample there is no segment with four distinct numbers.
|
Input: 4 21 2 2 3 | Output: 1 2
|
Medium
| 3 | 591 | 189 | 256 | 2 |
1,027 |
D
|
1027D
|
D. Mouse Hunt
| 1,700 |
dfs and similar; graphs
|
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about \(80\%\) of applicants are girls and majority of them are going to live in the university dormitory for the next \(4\) (hopefully) years.The dormitory consists of \(n\) rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number \(i\) costs \(c_i\) burles. Rooms are numbered from \(1\) to \(n\).Mouse doesn't sit in place all the time, it constantly runs. If it is in room \(i\) in second \(t\) then it will run to room \(a_i\) in second \(t + 1\) without visiting any other rooms inbetween (\(i = a_i\) means that mouse won't leave room \(i\)). It's second \(0\) in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from \(1\) to \(n\) at second \(0\).What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?
|
The first line contains as single integers \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of rooms in the dormitory.The second line contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le 10^4\)) β \(c_i\) is the cost of setting the trap in room number \(i\).The third line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β \(a_i\) is the room the mouse will run to the next second after being in room \(i\).
|
Print a single integer β the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.
|
In the first example it is enough to set mouse trap in rooms \(1\) and \(4\). If mouse starts in room \(1\) then it gets caught immideately. If mouse starts in any other room then it eventually comes to room \(4\).In the second example it is enough to set mouse trap in room \(2\). If mouse starts in room \(2\) then it gets caught immideately. If mouse starts in any other room then it runs to room \(2\) in second \(1\).Here are the paths of the mouse from different starts from the third example: \(1 \rightarrow 2 \rightarrow 2 \rightarrow \dots\); \(2 \rightarrow 2 \rightarrow \dots\); \(3 \rightarrow 2 \rightarrow 2 \rightarrow \dots\); \(4 \rightarrow 3 \rightarrow 2 \rightarrow 2 \rightarrow \dots\); \(5 \rightarrow 6 \rightarrow 7 \rightarrow 6 \rightarrow \dots\); \(6 \rightarrow 7 \rightarrow 6 \rightarrow \dots\); \(7 \rightarrow 6 \rightarrow 7 \rightarrow \dots\); So it's enough to set traps in rooms \(2\) and \(6\).
|
Input: 51 2 3 2 101 3 4 3 3 | Output: 3
|
Medium
| 2 | 1,205 | 444 | 191 | 10 |
574 |
B
|
574B
|
B. Bear and Three Musketeers
| 1,500 |
brute force; dfs and similar; graphs; hashing
|
Do you know a story about the three musketeers? Anyway, you will learn about its origins now.Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.There are n warriors. Richelimakieu wants to choose three of them to become musketeers but it's not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn't be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.
|
The first line contains two space-separated integers, n and m (3 β€ n β€ 4000, 0 β€ m β€ 4000) β respectively number of warriors and number of pairs of warriors knowing each other.i-th of the following m lines contains two space-separated integers ai and bi (1 β€ ai, bi β€ n, ai β bi). Warriors ai and bi know each other. Each pair of warriors will be listed at most once.
|
If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print ""-1"" (without the quotes).
|
In the first sample Richelimakieu should choose a triple 1, 2, 3. The first musketeer doesn't know anyone except other two musketeers so his recognition is 0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition 1 because he knows warrior 4. Sum of recognitions is 0 + 1 + 1 = 2.The other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to 1 + 1 + 1 = 3.In the second sample there is no triple of warriors knowing each other.
|
Input: 5 61 21 32 32 43 44 5 | Output: 2
|
Medium
| 4 | 795 | 367 | 145 | 5 |
1,731 |
C
|
1731C
|
C. Even Subarrays
| 1,700 |
bitmasks; brute force; hashing; math; number theory
|
You are given an integer array \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).Find the number of subarrays of \(a\) whose \(\operatorname{XOR}\) has an even number of divisors. In other words, find all pairs of indices \((i, j)\) (\(i \le j\)) such that \(a_i \oplus a_{i + 1} \oplus \dots \oplus a_j\) has an even number of divisors.For example, numbers \(2\), \(3\), \(5\) or \(6\) have an even number of divisors, while \(1\) and \(4\) β odd. Consider that \(0\) has an odd number of divisors in this task.Here \(\operatorname{XOR}\) (or \(\oplus\)) denotes the bitwise XOR operation.Print the number of subarrays but multiplied by 2022... Okay, let's stop. Just print the actual answer.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print the number of subarrays, whose \(\operatorname{XOR}\) has an even number of divisors.
|
In the first test case, there are \(4\) subarrays whose \(\operatorname{XOR}\) has an even number of divisors: \([3]\), \([3,1]\), \([1,2]\), \([2]\).In the second test case, there are \(11\) subarrays whose \(\operatorname{XOR}\) has an even number of divisors: \([4,2]\), \([4,2,1]\), \([4,2,1,5]\), \([2]\), \([2,1]\), \([2,1,5]\), \([2,1,5,3]\), \([1,5,3]\), \([5]\), \([5,3]\), \([3]\).In the third test case, there is no subarray whose \(\operatorname{XOR}\) has an even number of divisors since \(\operatorname{XOR}\) of any subarray is either \(4\) or \(0\).
|
Input: 433 1 254 2 1 5 344 4 4 475 7 3 7 1 7 3 | Output: 4 11 0 20
|
Medium
| 5 | 690 | 470 | 111 | 17 |
1,767 |
D
|
1767D
|
D. Playoff
| 1,500 |
combinatorics; constructive algorithms; dp; greedy; math
|
\(2^n\) teams participate in a playoff tournament. The tournament consists of \(2^n - 1\) games. They are held as follows: in the first phase of the tournament, the teams are split into pairs: team \(1\) plays against team \(2\), team \(3\) plays against team \(4\), and so on (so, \(2^{n-1}\) games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only \(2^{n-1}\) teams remain. If only one team remains, it is declared the champion; otherwise, the second phase begins, where \(2^{n-2}\) games are played: in the first one of them, the winner of the game ""\(1\) vs \(2\)"" plays against the winner of the game ""\(3\) vs \(4\)"", then the winner of the game ""\(5\) vs \(6\)"" plays against the winner of the game ""\(7\) vs \(8\)"", and so on. This process repeats until only one team remains. The skill level of the \(i\)-th team is \(p_i\), where \(p\) is a permutation of integers \(1\), \(2\), ..., \(2^n\) (a permutation is an array where each element from \(1\) to \(2^n\) occurs exactly once).You are given a string \(s\) which consists of \(n\) characters. These characters denote the results of games in each phase of the tournament as follows: if \(s_i\) is equal to 0, then during the \(i\)-th phase (the phase with \(2^{n-i}\) games), in each match, the team with the lower skill level wins; if \(s_i\) is equal to 1, then during the \(i\)-th phase (the phase with \(2^{n-i}\) games), in each match, the team with the higher skill level wins. Let's say that an integer \(x\) is winning if it is possible to find a permutation \(p\) such that the team with skill \(x\) wins the tournament. Find all winning integers.
|
The first line contains one integer \(n\) (\(1 \le n \le 18\)).The second line contains the string \(s\) of length \(n\) consisting of the characters 0 and/or 1.
|
Print all the winning integers \(x\) in ascending order.
|
Input: 3 101 | Output: 4 5 6 7
|
Medium
| 5 | 1,734 | 161 | 56 | 17 |
|
118 |
E
|
118E
|
E. Bertown roads
| 2,000 |
dfs and similar; graphs
|
Bertown has n junctions and m bidirectional roads. We know that one can get from any junction to any other one by the existing roads. As there were more and more cars in the city, traffic jams started to pose real problems. To deal with them the government decided to make the traffic one-directional on all the roads, thus easing down the traffic. Your task is to determine whether there is a way to make the traffic one-directional so that there still is the possibility to get from any junction to any other one. If the answer is positive, you should also find one of the possible ways to orient the roads.
|
The first line contains two space-separated integers n and m (2 β€ n β€ 105, n - 1 β€ m β€ 3Β·105) which represent the number of junctions and the roads in the town correspondingly. Then follow m lines, each containing two numbers which describe the roads in the city. Each road is determined by two integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the numbers of junctions it connects.It is guaranteed that one can get from any junction to any other one along the existing bidirectional roads. Each road connects different junctions, there is no more than one road between each pair of junctions.
|
If there's no solution, print the single number 0. Otherwise, print m lines each containing two integers pi and qi β each road's orientation. That is the traffic flow will move along a one-directional road from junction pi to junction qi. You can print the roads in any order. If there are several solutions to that problem, print any of them.
|
Input: 6 81 22 31 34 54 65 62 43 5 | Output: 1 22 33 14 55 66 44 23 5
|
Hard
| 2 | 609 | 590 | 343 | 1 |
|
461 |
D
|
461D
|
D. Appleman and Complicated Task
| 2,800 |
dsu; math
|
Toastman came up with a very complicated task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?Given a n Γ n checkerboard. Each cell of the board has either character 'x', or character 'o', or nothing. How many ways to fill all the empty cells with 'x' or 'o' (each cell must contain only one character in the end) are there, such that for each cell the number of adjacent cells with 'o' will be even? Find the number of ways modulo 1000000007 (109 + 7). Two cells of the board are adjacent if they share a side.
|
The first line contains two integers n, k (1 β€ n, k β€ 105) β the size of the board, and the number of cells that has characters initially. Then k lines follows. The i-th line contains two integers and a character: ai, bi, ci (1 β€ ai, bi β€ n; ci is either 'o' or 'x'). This line means: there is a character ci in the cell that is located on the intersection of the ai-th row and bi-th column. All the given cells are distinct.Consider that the rows are numbered from 1 to n from top to bottom. Analogically, the columns are numbered from 1 to n from left to right.
|
Print a single integer β the answer to the problem.
|
In the first example there are two ways: xxo xoo xox ooo oxx oox
|
Input: 3 21 1 x2 2 o | Output: 2
|
Master
| 2 | 549 | 563 | 51 | 4 |
617 |
E
|
617E
|
E. XOR and Favorite Number
| 2,200 |
data structures
|
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l β€ i β€ j β€ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.
|
The first line of the input contains integers n, m and k (1 β€ n, m β€ 100 000, 0 β€ k β€ 1 000 000) β the length of the array, the number of queries and Bob's favorite number respectively.The second line contains n integers ai (0 β€ ai β€ 1 000 000) β Bob's array.Then m lines follow. The i-th line contains integers li and ri (1 β€ li β€ ri β€ n) β the parameters of the i-th query.
|
Print m lines, answer the queries in the order they appear in the input.
|
In the first sample the suitable pairs of i and j for the first query are: (1, 2), (1, 4), (1, 5), (2, 3), (3, 6), (5, 6), (6, 6). Not a single of these pairs is suitable for the second query.In the second sample xor equals 1 for all subarrays of an odd length.
|
Input: 6 2 31 2 1 1 0 31 63 5 | Output: 70
|
Hard
| 1 | 273 | 375 | 72 | 6 |
1,703 |
E
|
1703E
|
E. Mirror Grid
| 1,200 |
implementation
|
You are given a square grid with \(n\) rows and \(n\) columns. Each cell contains either \(0\) or \(1\). In an operation, you can select a cell of the grid and flip it (from \(0 \to 1\) or \(1 \to 0\)). Find the minimum number of operations you need to obtain a square that remains the same when rotated \(0^{\circ}\), \(90^{\circ}\), \(180^{\circ}\) and \(270^{\circ}\).The picture below shows an example of all rotations of a grid.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 100\)) β the size of the grid.Then \(n\) lines follow, each with \(n\) characters \(a_{i,j}\) (\(0 \leq a_{i,j} \leq 1\)) β the number written in each cell.
|
For each test case output a single integer β the minimum number of operations needed to make the square look the same rotated \(0^{\circ}\), \(90^{\circ}\), \(180^{\circ}\) and \(270^{\circ}\).
|
In the first test case, we can perform one operations to make the grid \(\begin{matrix}0 & 1 & 0\\ 1 & 1 & \color{red}{1}\\ 0 & 1 & 0\end{matrix}\). Now, all rotations of the square are the same.In the second test case, all rotations of the square are already the same, so we don't need any flips.
|
Input: 5301011001010511100110110101110011110005010001010101010000100100151100100000111111011001111 | Output: 1 0 9 7 6
|
Easy
| 1 | 433 | 336 | 193 | 17 |
1,031 |
E
|
1031E
|
E. Triple Flips
| 2,600 |
constructive algorithms
|
You are given an array \(a\) of length \(n\) that consists of zeros and ones.You can perform the following operation multiple times. The operation consists of two steps: Choose three integers \(1 \le x < y < z \le n\), that form an arithmetic progression (\(y - x = z - y\)). Flip the values \(a_x, a_y, a_z\) (i.e. change \(1\) to \(0\), change \(0\) to \(1\)). Determine if it is possible to make all elements of the array equal to zero. If yes, print the operations that lead the the all-zero state. Your solution should not contain more than \((\lfloor \frac{n}{3} \rfloor + 12)\) operations. Here \(\lfloor q \rfloor\) denotes the number \(q\) rounded down. We can show that it is possible to make all elements equal to zero in no more than this number of operations whenever it is possible to do so at all.
|
The first line contains a single integer \(n\) (\(3 \le n \le 10^5\)) β the length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 1\)) β the elements of the array.
|
Print ""YES"" (without quotes) if the answer exists, otherwise print ""NO"" (without quotes). You can print each letter in any case (upper or lower).If there is an answer, in the second line print an integer \(m\) (\(0 \le m \le (\lfloor \frac{n}{3} \rfloor + 12)\)) β the number of operations in your answer.After that in (\(i + 2\))-th line print the \(i\)-th operations β the integers \(x_i, y_i, z_i\). You can print them in arbitrary order.
|
In the first sample the shown output corresponds to the following solution: 1 1 0 1 1 (initial state); 0 1 1 1 0 (the flipped positions are the first, the third and the fifth elements); 0 0 0 0 0 (the flipped positions are the second, the third and the fourth elements). Other answers are also possible. In this test the number of operations should not exceed \(\lfloor \frac{5}{3} \rfloor + 12 = 1 + 12 = 13\).In the second sample the only available operation is to flip all the elements. This way it is only possible to obtain the arrays 0 1 0 and 1 0 1, but it is impossible to make all elements equal to zero.
|
Input: 51 1 0 1 1 | Output: YES21 3 52 3 4
|
Expert
| 1 | 812 | 212 | 445 | 10 |
1,849 |
E
|
1849E
|
E. Max to the Right of Min
| 2,300 |
binary search; data structures; divide and conquer; dp; dsu; two pointers
|
You are given a permutation \(p\) of length \(n\) β an array, consisting of integers from \(1\) to \(n\), all distinct.Let \(p_{l,r}\) denote a subarray β an array formed by writing down elements from index \(l\) to index \(r\), inclusive.Let \(\mathit{maxpos}_{l,r}\) denote the index of the maximum element on \(p_{l,r}\). Similarly, let \(\mathit{minpos}_{l,r}\) denote the index of the minimum element on it.Calculate the number of subarrays \(p_{l,r}\) such that \(\mathit{maxpos}_{l,r} > \mathit{minpos}_{l,r}\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^6\)) β the number of elements in the permutation.The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n\)). All \(p_i\) are distinct.
|
Print a single integer β the number of subarrays \(p_{l,r}\) such that \(\mathit{maxpos}_{l,r} > \mathit{minpos}_{l,r}\).
|
Input: 3 1 2 3 | Output: 3
|
Expert
| 6 | 518 | 227 | 121 | 18 |
|
936 |
E
|
936E
|
E. Iqea
| 3,400 |
data structures; dfs and similar; divide and conquer; dsu; shortest paths; trees
|
Gridland is placed on infinite grid and has a shape of figure consisting of cells. Every cell of Gridland is a city. Two cities that are placed in adjacent cells are connected by the road of length \(1\). It's possible to get from any city to any other city using roads. The distance between two cities is the minimum total road length on path from one city to another. It's possible to get from any cell that doesn't belong to Gridland to any other cell that doesn't belong to Gridland by using only cells which don't belong to Gridland. In other words, Gridland is connected and complement of Gridland is also connected.At the moment no city in Gridland has Iqea famous shop. But Iqea has great plans for building shops in Gridland. For customers' convenience Iqea decided to develop an application. Using this application everyone can learn the distance to the nearest Iqea. You are to develop this application.You are asked to process two types of queries: new Iqea shop has been opened in the city with coordinates \((x, y)\); customer wants to know the distance to the nearest already opened Iqea shop from his city located in a cell with coordinates \((x, y)\). Pay attention that customer can move only by roads and can never leave Gridland on his way to the shop.
|
The first line contains one integer \(n\) β number of cities in Gridland (\(1 \le n \le 300\,000\)). The following \(n\) lines contain two integers \(x_i\) and \(y_i\) β coordinates of cell that contains \(i\)-th city (\(1 \le x_i, y_i \le 300\,000\)). No two cells coincide. Cells form connected area, complement of this area is also connected.The following line contains single integer \(q\) β number of queries (\(0 \le q \le 300\,000\)). Following \(q\) lines contain queries, \(i\)-th line consists of three integers \(t_i\), \(x_i\) and \(y_i\) (\(t_i \in \{1, 2\}\), \(1 \le x_i, y_i \le 300\,000\)). If \(t_i = 1\), new Iqea shop has been opened in the city with coordinates \((x_i, y_i)\). It's guaranteed that there was no Iqea shop in this city before. If \(t_i = 2\), you are to find the distance to the nearest already opened Iqea shop from the city with coordinates \((x_i, y_i)\). It's guaranteed that in queries of both types cell \((x_i, y_i)\) belongs to Gridland.
|
For every query of second type output single integer β minimum distance to the nearest Iqea shop. If no Iqea shop has been already opened, output \(-1\).
|
Input: 71 21 32 33 13 23 34 252 3 21 4 22 1 21 3 32 2 3 | Output: -151
|
Master
| 6 | 1,272 | 982 | 153 | 9 |
|
1,779 |
A
|
1779A
|
A. Hall of Fame
| 800 |
constructive algorithms; greedy; strings
|
Thalia is a Legendary Grandmaster in chess. She has \(n\) trophies in a line numbered from \(1\) to \(n\) (from left to right) and a lamp standing next to each of them (the lamps are numbered as the trophies).A lamp can be directed either to the left or to the right, and it illuminates all trophies in that direction (but not the one it is next to). More formally, Thalia has a string \(s\) consisting only of characters 'L' and 'R' which represents the lamps' current directions. The lamp \(i\) illuminates: trophies \(1,2,\ldots, i-1\) if \(s_i\) is 'L'; trophies \(i+1,i+2,\ldots, n\) if \(s_i\) is 'R'. She can perform the following operation at most once: Choose an index \(i\) (\(1 \leq i < n\)); Swap the lamps \(i\) and \(i+1\) (without changing their directions). That is, swap \(s_i\) with \(s_{i+1}\). Thalia asked you to illuminate all her trophies (make each trophy illuminated by at least one lamp), or to tell her that it is impossible to do so. If it is possible, you can choose to perform an operation or to do nothing. Notice that lamps cannot change direction, it is only allowed to swap adjacent ones.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of the test cases follows.The first line of each test case contains a positive integer \(n\) (\(2 \leq n \leq 100\,000\)) β the number of trophies.The second line of each test case contains a string \(s\) of length \(n\) consisting only of characters 'L' and 'R' β the \(i\)-th character describes the direction of the \(i\)-th lamp.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(100\,000\).
|
For each test case print \(-1\) if it is impossible to illuminate all trophies by performing one operation (or doing nothing). Otherwise, print \(0\) if you choose not to perform the operation (i.e., the trophies are illuminated by the initial positioning of the lamps), or an index \(i\) (\(1 \leq i < n\)) if you choose to swap lamps \(i\) and \(i+1\).If there are multiple answers, print any.
|
In the first example, it is possible to swap lamps \(1\) and \(2\), or do nothing. In any case, the string ""LL"" is obtained. Not all trophies are illuminated since trophy \(2\) is not illuminated by any lamp β lamp \(1\) illuminates nothing and lamp \(2\) illuminates only the trophy \(1\).In the second example, it is necessary to swap lamps \(1\) and \(2\). The string becomes ""RL"". Trophy \(1\) is illuminated by lamp \(2\) and trophy \(2\) is illuminated by lamp \(1\), hence it is possible to illuminate all trophies.In the third example, all trophies are initially illuminated β hence, not performing any operation is a valid solution.In the last two examples performing swaps is not necessary as all trophies are illuminated initially. But, the presented solutions are also valid.
|
Input: 6 2 LL 2 LR 2 RL 2 RR 7 LLRLLLR 7 RRLRRRL | Output: -1 1 0 -1 3 6
|
Beginner
| 3 | 1,122 | 561 | 395 | 17 |
61 |
E
|
61E
|
E. Enemy is weak
| 1,900 |
data structures; trees
|
The Romans have attacked again. This time they are much more than the Persians but Shapur is ready to defeat them. He says: ""A lion is never afraid of a hundred sheep"". Nevertheless Shapur has to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.In Shapur's opinion the weakness of an army is equal to the number of triplets i, j, k such that i < j < k and ai > aj > ak where ax is the power of man standing at position x. The Roman army has one special trait β powers of all the people in it are distinct.Help Shapur find out how weak the Romans are.
|
The first line of input contains a single number n (3 β€ n β€ 106) β the number of men in Roman army. Next line contains n different positive integers ai (1 β€ i β€ n, 1 β€ ai β€ 109) β powers of men in the Roman army.
|
A single integer number, the weakness of the Roman army. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
|
Input: 33 2 1 | Output: 1
|
Hard
| 2 | 592 | 212 | 188 | 0 |
|
1,887 |
C
|
1887C
|
C. Minimum Array
| 2,400 |
binary search; brute force; constructive algorithms; data structures; greedy; hashing; two pointers
|
Given an array \(a\) of length \(n\) consisting of integers. Then the following operation is sequentially applied to it \(q\) times: Choose indices \(l\) and \(r\) (\(1 \le l \le r \le n\)) and an integer \(x\); Add \(x\) to all elements of the array \(a\) in the segment \([l, r]\). More formally, assign \(a_i := a_i + x\) for all \(l \le i \le r\).Let \(b_j\) be the array \(a\) obtained after applying the first \(j\) operations (\(0 \le j \le q\)). Note that \(b_0\) is the array \(a\) before applying any operations.You need to find the lexicographically minimum\(^{\dagger}\) array among all arrays \(b_j\).\(^{\dagger}\)An array \(x\) is lexicographically smaller than array \(y\) if there is an index \(i\) such that \(x_i < y_i\), and \(x_j = y_j\) for all \(j < i\). In other words, for the first index \(i\) where the arrays differ, \(x_i < y_i\).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 5 \cdot 10^5\)) β 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 5 \cdot 10^5\)) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)) β the elements of array \(a\).The third line of each test case contains a single integer \(q\) (\(0 \le q \le 5 \cdot 10^5\)) β the number of operations on the array.In each of the next \(q\) lines, there are three integers \(l_j\), \(r_j\), and \(x_j\) \((1 \le l_j \le r_j \le n, -10^9 \le x_j \le 10^9)\) β the description of each operation. The operations are given in the order they are applied.It is guaranteed that the sum of \(n\) over all test cases and the sum of \(q\) over all test cases do not exceed \(5 \cdot 10^5\).
|
For each test case, output the lexicographically minimum array among all arrays \(b_j\).
|
In the first test case: \(b_0 = [1,2,3,4]\); \(b_1 = [1,2,3,4]\); \(b_2 = [-99,-98,-97,4]\).Thus, the lexicographically minimum array is \(b_2\).In the second test case, the lexicographically minimum array is \(b_0\).
|
Input: 241 2 3 421 4 01 3 -10052 1 2 5 432 4 32 5 -21 3 1 | Output: -99 -98 -97 4 2 1 2 5 4
|
Expert
| 7 | 859 | 959 | 88 | 18 |
1,295 |
D
|
1295D
|
D. Same GCDs
| 1,800 |
math; number theory
|
You are given two integers \(a\) and \(m\). Calculate the number of integers \(x\) such that \(0 \le x < m\) and \(\gcd(a, m) = \gcd(a + x, m)\).Note: \(\gcd(a, b)\) is the greatest common divisor of \(a\) and \(b\).
|
The first line contains the single integer \(T\) (\(1 \le T \le 50\)) β the number of test cases.Next \(T\) lines contain test cases β one per line. Each line contains two integers \(a\) and \(m\) (\(1 \le a < m \le 10^{10}\)).
|
Print \(T\) integers β one per test case. For each test case print the number of appropriate \(x\)-s.
|
In the first test case appropriate \(x\)-s are \([0, 1, 3, 4, 6, 7]\).In the second test case the only appropriate \(x\) is \(0\).
|
Input: 3 4 9 5 10 42 9999999967 | Output: 6 1 9999999966
|
Medium
| 2 | 216 | 227 | 101 | 12 |
2 |
A
|
2A
|
A. Winner
| 1,500 |
hashing; implementation
|
The winner of the card game popular in Berland ""Berlogging"" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line ""name score"", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to m) at the end of the game, than wins the one of them who scored at least m points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points.
|
The first line contains an integer number n (1 β€ n β€ 1000), n is the number of rounds played. Then follow n lines, containing the information about the rounds in ""name score"" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive.
|
Print the name of the winner.
|
Input: 3mike 3andrew 5mike 2 | Output: andrew
|
Medium
| 2 | 949 | 354 | 29 | 0 |
|
864 |
C
|
864C
|
C. Bus
| 1,500 |
greedy; implementation; math
|
A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the point x = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0 is called a bus journey. In total, the bus must make k journeys.The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the point x = 0.
|
The first line contains four integers a, b, f, k (0 < f < a β€ 106, 1 β€ b β€ 109, 1 β€ k β€ 104) β the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.
|
Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.
|
In the first example the bus needs to refuel during each journey.In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty. In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.
|
Input: 6 9 2 4 | Output: 4
|
Medium
| 3 | 1,134 | 256 | 139 | 8 |
1,936 |
C
|
1936C
|
C. PokΓ©mon Arena
| 2,400 |
data structures; graphs; greedy; implementation; shortest paths; sortings
|
You are at a dueling arena. You also possess \(n\) PokΓ©mons. Initially, only the \(1\)-st PokΓ©mon is standing in the arena.Each PokΓ©mon has \(m\) attributes. The \(j\)-th attribute of the \(i\)-th PokΓ©mon is \(a_{i,j}\). Each PokΓ©mon also has a cost to be hired: the \(i\)-th PokΓ©mon's cost is \(c_i\).You want to have the \(n\)-th PokΓ©mon stand in the arena. To do that, you can perform the following two types of operations any number of times in any order: Choose three integers \(i\), \(j\), \(k\) (\(1 \le i \le n\), \(1 \le j \le m\), \(k > 0\)), increase \(a_{i,j}\) by \(k\) permanently. The cost of this operation is \(k\). Choose two integers \(i\), \(j\) (\(1 \le i \le n\), \(1 \le j \le m\)) and hire the \(i\)-th PokΓ©mon to duel with the current PokΓ©mon in the arena based on the \(j\)-th attribute. The \(i\)-th PokΓ©mon will win if \(a_{i,j}\) is greater than or equal to the \(j\)-th attribute of the current PokΓ©mon in the arena (otherwise, it will lose). After the duel, only the winner will stand in the arena. The cost of this operation is \(c_i\). Find the minimum cost you need to pay to have the \(n\)-th PokΓ©mon stand in the arena.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n \le 4 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\), \(2 \leq n \cdot m \leq 4 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le 10^9\)).The \(i\)-th of the following \(n\) lines contains \(m\) integers \(a_{i,1}, a_{i,2}, \ldots, a_{i,m}\) (\(1 \le a_{i,j} \le 10^9\)).It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(4 \cdot 10^5\).
|
For each test case, output the minimum cost to make the \(n\)-th PokΓ©mon stand in the arena.
|
In the first test case, the attribute array of the \(1\)-st PokΓ©mon (which is standing in the arena initially) is \([2,9,9]\).In the first operation, you can choose \(i=3\), \(j=1\), \(k=1\), and increase \(a_{3,1}\) by \(1\) permanently. Now the attribute array of the \(3\)-rd PokΓ©mon is \([2,2,1]\). The cost of this operation is \(k = 1\).In the second operation, you can choose \(i=3\), \(j=1\), and hire the \(3\)-rd PokΓ©mon to duel with the current PokΓ©mon in the arena based on the \(1\)-st attribute. Since \(a_{i,j}=a_{3,1}=2 \ge 2=a_{1,1}\), the \(3\)-rd PokΓ©mon will win. The cost of this operation is \(c_3 = 1\).Thus, we have made the \(3\)-rd PokΓ©mon stand in the arena within the cost of \(2\). It can be proven that \(2\) is minimum possible.In the second test case, the attribute array of the \(1\)-st PokΓ©mon in the arena is \([9,9,9]\).In the first operation, you can choose \(i=2\), \(j=3\), \(k=2\), and increase \(a_{2,3}\) by \(2\) permanently. Now the attribute array of the \(2\)-nd PokΓ©mon is \([6,1,9]\). The cost of this operation is \(k = 2\).In the second operation, you can choose \(i=2\), \(j=3\), and hire the \(2\)-nd PokΓ©mon to duel with the current PokΓ©mon in the arena based on the \(3\)-rd attribute. Since \(a_{i,j}=a_{2,3}=9 \ge 9=a_{1,3}\), the \(2\)-nd PokΓ©mon will win. The cost of this operation is \(c_2 = 3\).In the third operation, you can choose \(i=3\), \(j=2\), and hire the \(3\)-rd PokΓ©mon to duel with the current PokΓ©mon in the arena based on the \(2\)-nd attribute. Since \(a_{i,j}=a_{1,2}=2 \ge 1=a_{2,2}\), the \(3\)-rd PokΓ©mon can win. The cost of this operation is \(c_3 = 1\).Thus, we have made the \(3\)-rd PokΓ©mon stand in the arena within the cost of \(6\). It can be proven that \(6\) is minimum possible.
|
Input: 43 32 3 12 9 96 1 71 2 13 32 3 19 9 96 1 71 2 14 22 8 3 518 2417 101 101 16 321412674 3212925 172015806 250849370 306960171 333018900950000001 950000001 950000001821757276 783362401 760000001570000001 700246226 600757652380000001 423513575 474035234315201473 300580025 2870234451 1 1 | Output: 2 6 17 1224474550
|
Expert
| 6 | 1,155 | 675 | 92 | 19 |
241 |
E
|
241E
|
E. Flights
| 2,600 |
graphs; shortest paths
|
LiLand is a country, consisting of n cities. The cities are numbered from 1 to n. The country is well known because it has a very strange transportation system. There are many one-way flights that make it possible to travel between the cities, but the flights are arranged in a way that once you leave a city you will never be able to return to that city again.Previously each flight took exactly one hour, but recently Lily has become the new manager of transportation system and she wants to change the duration of some flights. Specifically, she wants to change the duration of some flights to exactly 2 hours in such a way that all trips from city 1 to city n take the same time regardless of their path.Your task is to help Lily to change the duration of flights.
|
First line of the input contains two integer numbers n and m (2 β€ n β€ 1000; 1 β€ m β€ 5000) specifying the number of cities and the number of flights.Each of the next m lines contains two integers ai and bi (1 β€ ai < bi β€ n) specifying a one-directional flight from city ai to city bi. It is guaranteed that there exists a way to travel from city number 1 to city number n using the given flights. It is guaranteed that there is no sequence of flights that forms a cyclical path and no two flights are between the same pair of cities.
|
If it is impossible for Lily to do her task, print ""No"" (without quotes) on the only line of the output. Otherwise print ""Yes"" (without quotes) on the first line of output, then print an integer ansi (1 β€ ansi β€ 2) to each of the next m lines being the duration of flights in new transportation system. You should print these numbers in the order that flights are given in the input.If there are multiple solutions for the input, output any of them.
|
Input: 3 31 22 31 3 | Output: Yes112
|
Expert
| 2 | 768 | 532 | 453 | 2 |
|
353 |
D
|
353D
|
D. Queue
| 2,000 |
constructive algorithms; dp
|
There are n schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes.Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginning of the line). In other words, if at some time the i-th position has a boy and the (i + 1)-th position has a girl, then in a second, the i-th position will have a girl and the (i + 1)-th one will have a boy.Let's take an example of a line of four people: a boy, a boy, a girl, a girl (from the beginning to the end of the line). Next second the line will look like that: a boy, a girl, a boy, a girl. Next second it will be a girl, a boy, a girl, a boy. Next second it will be a girl, a girl, a boy, a boy. The line won't change any more.Your task is: given the arrangement of the children in the line to determine the time needed to move all girls in front of boys (in the example above it takes 3 seconds). Baking buns takes a lot of time, so no one leaves the line until the line stops changing.
|
The first line contains a sequence of letters without spaces s1s2... sn (1 β€ n β€ 106), consisting of capital English letters M and F. If letter si equals M, that means that initially, the line had a boy on the i-th position. If letter si equals F, then initially the line had a girl on the i-th position.
|
Print a single integer β the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0.
|
In the first test case the sequence of changes looks as follows: MFM β FMM.The second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF β MFMF β FMFM β FFMM.
|
Input: MFM | Output: 1
|
Hard
| 2 | 1,116 | 304 | 159 | 3 |
412 |
B
|
412B
|
B. Network Configuration
| 900 |
greedy; sortings
|
The R1 company wants to hold a web search championship. There were n computers given for the competition, each of them is connected to the Internet. The organizers believe that the data transfer speed directly affects the result. The higher the speed of the Internet is, the faster the participant will find the necessary information. Therefore, before the competition started, each computer had its maximum possible data transfer speed measured. On the i-th computer it was ai kilobits per second.There will be k participants competing in the championship, each should get a separate computer. The organizing company does not want any of the participants to have an advantage over the others, so they want to provide the same data transfer speed to each participant's computer. Also, the organizers want to create the most comfortable conditions for the participants, so the data transfer speed on the participants' computers should be as large as possible.The network settings of the R1 company has a special option that lets you to cut the initial maximum data transfer speed of any computer to any lower speed. How should the R1 company configure the network using the described option so that at least k of n computers had the same data transfer speed and the data transfer speed on these computers was as large as possible?
|
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 100) β the number of computers and the number of participants, respectively. In the second line you have a space-separated sequence consisting of n integers: a1, a2, ..., an (16 β€ ai β€ 32768); number ai denotes the maximum data transfer speed on the i-th computer.
|
Print a single integer β the maximum Internet speed value. It is guaranteed that the answer to the problem is always an integer.
|
In the first test case the organizers can cut the first computer's speed to 30 kilobits. Then two computers (the first and the third one) will have the same speed of 30 kilobits. They should be used as the participants' computers. This answer is optimal.
|
Input: 3 240 20 30 | Output: 30
|
Beginner
| 2 | 1,329 | 338 | 128 | 4 |
436 |
E
|
436E
|
E. Cardboard Box
| 2,600 |
data structures; greedy
|
Everyone who has played Cut the Rope knows full well how the gameplay is organized. All levels in the game are divided into boxes. Initially only one box with some levels is available. Player should complete levels to earn stars, collecting stars opens new box with levels. Imagine that you are playing Cut the Rope for the first time. Currently you have only the levels of the first box (by the way, it is called ""Cardboard Box""). Each level is characterized by two integers: ai β how long it takes to complete the level for one star, bi β how long it takes to complete the level for two stars (ai < bi).You want to open the next box as quickly as possible. So, you need to earn at least w stars. How do make it happen? Note that the level can be passed only once: either for one star or for two. You do not necessarily need to pass all the levels.
|
The first line contains two integers n and w (1 β€ n β€ 3Β·105; 1 β€ w β€ 2n) β the number of levels in the first box and the number of stars you need to open another box. Each of the following n lines contains two integers ai and bi (1 β€ ai < bi β€ 109) β the attributes of the i-th level.
|
In the first line print integer t β the minimum time you need to open the next box. In the next line, print n digits without spaces β the description of the optimal scenario: if you need to pass the i-th level for one star, the i-th digit should equal 1; if you need to pass the i-th level for two stars, the i-th digit should equal 2; if you do not need to pass the i-th level at all, the i-th digit should equal 0.
|
In the first test sample, answer 21 is also assumed correct.
|
Input: 2 31 21 2 | Output: 312
|
Expert
| 2 | 851 | 284 | 416 | 4 |
1,032 |
B
|
1032B
|
B. Personalized Cup
| 1,200 |
At many competitions that have a word Β«cupΒ» in its official name the winner is presented with an actual cup. This time the organizers of one unusual programming competition have decided to please the winner even more and to add a nameplate to the cup with the handle of the winner.The nameplate is to be rectangular and the text on it will be printed as a table of several rows and columns. Having some measurements done, the organizers have found out that the number \(a\) of rows cannot be greater than \(5\) while the number \(b\) of columns cannot exceed \(20\). Every cell of the table will contain either an asterisk (Β«*Β») or a letter of user's handle.Furthermore, the organizers want the rows of the table to be uniform, which means that the number of asterisks used in different rows should differ by at most one (i.e. you can't have two asterisks in the first row and none in the second). The main goal, however, is to obtain the winner's handle precisely when reading the table from top to bottom and from left to right in every row (skipping asterisks).The organizers want for the nameplate to have as few rows as possible and among all valid tables with the minimum number of rows they want to choose the one that has the minimum number of columns.The winner is not yet determined so your task is to write a program that, given a certain handle, generates the necessary table.
|
The only line contains one string \(s\) (\(1 \le |s| \le 100\)), comprised of uppercase and lowercase Latin letters, β the handle of the winner.
|
In the first line output the minimum number \(a\) of rows in the table and the minimum number \(b\) of columns in an optimal table with rows.The following \(a\) lines should contain \(b\) characters each β any valid table.
|
Input: tourist | Output: 1 7 tourist
|
Easy
| 0 | 1,388 | 144 | 222 | 10 |
||
1,985 |
F
|
1985F
|
F. Final Boss
| 1,500 |
binary search; data structures
|
You are facing the final boss in your favorite video game. The boss enemy has \(h\) health. Your character has \(n\) attacks. The \(i\)'th attack deals \(a_i\) damage to the boss but has a cooldown of \(c_i\) turns, meaning the next time you can use this attack is turn \(x + c_i\) if your current turn is \(x\). Each turn, you can use all attacks that are not currently on cooldown, all at once. If all attacks are on cooldown, you do nothing for the turn and skip to the next turn.Initially, all attacks are not on cooldown. How many turns will you take to beat the boss? The boss is beaten when its health is \(0\) or less.
|
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains two integers \(h\) and \(n\) (\(1 \leq h, n \leq 2 \cdot 10^5\)) β the health of the boss and the number of attacks you have.The following line of each test case contains \(n\) integers \(a_1, a_2, ..., a_n\) (\(1 \leq a_i \leq 2 \cdot 10^5\)) β the damage of your attacks.The following line of each test case contains \(n\) integers \(c_1, c_2, ..., c_n\) (\(1 \leq c_i \leq 2 \cdot 10^5\)) β the cooldown of your attacks.It is guaranteed that the sum of \(h\) and \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output an integer, the minimum number of turns required to beat the boss.
|
For the first test case, you can use attacks \(1\) and \(2\) on the first turn, dealing \(3\) damage in total, and slaying the boss.For the second case, you can beat the boss in \(3\) turns by using the following attacks:Turn \(1\): Use attacks \(1\) and \(2\), dealing \(3\) damage to the boss. The boss now has \(2\) health left.Turn \(2\): Use attack \(2\), dealing \(1\) damage to the boss. The boss now has \(1\) health left.Turn \(3\): Use attack \(1\), dealing \(2\) damage to the boss. The boss now has \(-1\) health left. Since its health is less than or equal to \(0\), you beat the boss.For the sixth test case: remember to use 64-bit integers as the answer can get large.
|
Input: 83 22 12 15 22 12 150 35 6 75 6 750 32 2 23 3 390000 2200000 2000001 1100000 112000006 73 2 3 2 3 1 26 5 9 5 10 7 721 61 1 1 1 1 15 5 8 10 7 6 | Output: 1 3 15 25 1 19999800001 1 21
|
Medium
| 2 | 626 | 649 | 93 | 19 |
535 |
C
|
535C
|
C. Tavas and Karafs
| 1,900 |
binary search; greedy; math
|
Karafs is some kind of vegetable in shape of an 1 Γ h rectangle. Tavaspolis people love Karafs and they use Karafs in almost any kind of food. Tavas, himself, is crazy about Karafs. Each Karafs has a positive integer height. Tavas has an infinite 1-based sequence of Karafses. The height of the i-th Karafs is si = A + (i - 1) Γ B.For a given m, let's define an m-bite operation as decreasing the height of at most m distinct not eaten Karafses by 1. Karafs is considered as eaten when its height becomes zero.Now SaDDas asks you n queries. In each query he gives you numbers l, t and m and you should find the largest number r such that l β€ r and sequence sl, sl + 1, ..., sr can be eaten by performing m-bite no more than t times or print -1 if there is no such number r.
|
The first line of input contains three integers A, B and n (1 β€ A, B β€ 106, 1 β€ n β€ 105).Next n lines contain information about queries. i-th line contains integers l, t, m (1 β€ l, t, m β€ 106) for i-th query.
|
For each query, print its answer in a single line.
|
Input: 2 1 41 5 33 3 107 10 26 4 8 | Output: 4-18-1
|
Hard
| 3 | 773 | 208 | 50 | 5 |
|
1,508 |
B
|
1508B
|
B. Almost Sorted
| 1,800 |
binary search; combinatorics; constructive algorithms; implementation
|
Seiji Maki doesn't only like to observe relationships being unfolded, he also likes to observe sequences of numbers, especially permutations. Today, he has his eyes on almost sorted permutations.A permutation \(a_1, a_2, \dots, a_n\) of \(1, 2, \dots, n\) is said to be almost sorted if the condition \(a_{i + 1} \ge a_i - 1\) holds for all \(i\) between \(1\) and \(n - 1\) inclusive.Maki is considering the list of all almost sorted permutations of \(1, 2, \dots, n\), given in lexicographical order, and he wants to find the \(k\)-th permutation in this list. Can you help him to find such permutation?Permutation \(p\) is lexicographically smaller than a permutation \(q\) if and only if the following holds: in the first position where \(p\) and \(q\) differ, the permutation \(p\) has a smaller element than the corresponding element in \(q\).
|
The first line contains a single integer \(t\) (\(1\le t\le 1000\)) β the number of test cases.Each test case consists of a single line containing two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(1 \le k \le 10^{18}\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print a single line containing the \(k\)-th almost sorted permutation of length \(n\) in lexicographical order, or \(-1\) if it doesn't exist.
|
For the first and second test, the list of almost sorted permutations with \(n = 1\) is \(\{[1]\}\).For the third and fifth test, the list of almost sorted permutations with \(n = 3\) is \(\{[1, 2, 3], [1, 3, 2], [2, 1, 3], [3, 2, 1]\}\).
|
Input: 5 1 1 1 2 3 3 6 5 3 4 | Output: 1 -1 2 1 3 1 2 4 3 5 6 3 2 1
|
Medium
| 4 | 849 | 308 | 162 | 15 |
676 |
E
|
676E
|
E. The Last Fight Between Human and AI
| 2,400 |
math
|
100 years have passed since the last victory of the man versus computer in Go. Technologies made a huge step forward and robots conquered the Earth! It's time for the final fight between human and robot that will decide the faith of the planet.The following game was chosen for the fights: initially there is a polynomial P(x) = anxn + an - 1xn - 1 + ... + a1x + a0, with yet undefined coefficients and the integer k. Players alternate their turns. At each turn, a player pick some index j, such that coefficient aj that stay near xj is not determined yet and sets it to any value (integer or real, positive or negative, 0 is also allowed). Computer moves first. The human will be declared the winner if and only if the resulting polynomial will be divisible by Q(x) = x - k.Polynomial P(x) is said to be divisible by polynomial Q(x) if there exists a representation P(x) = B(x)Q(x), where B(x) is also some polynomial.Some moves have been made already and now you wonder, is it true that human can guarantee the victory if he plays optimally?
|
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, |k| β€ 10 000) β the size of the polynomial and the integer k.The i-th of the following n + 1 lines contain character '?' if the coefficient near xi - 1 is yet undefined or the integer value ai, if the coefficient is already known ( - 10 000 β€ ai β€ 10 000). Each of integers ai (and even an) may be equal to 0.Please note, that it's not guaranteed that you are given the position of the game where it's computer's turn to move.
|
Print ""Yes"" (without quotes) if the human has winning strategy, or ""No"" (without quotes) otherwise.
|
In the first sample, computer set a0 to - 1 on the first move, so if human can set coefficient a1 to 0.5 and win.In the second sample, all coefficients are already set and the resulting polynomial is divisible by x - 100, so the human has won.
|
Input: 1 2-1? | Output: Yes
|
Expert
| 1 | 1,043 | 502 | 103 | 6 |
1,639 |
A
|
1639A
|
A. Treasure Hunt
| 0 |
graphs; interactive
|
All problems in this contest share the same statement, the only difference is the test your solution runs on. For further information on scoring please refer to ""Scoring"" section of the statement.This is an interactive problem.Imagine you are a treasure hunter, a very skillful one. One day you came across an ancient map which could help you to become rich. The map shows multiple forestry roads, and at each junction there is a treasure. So, you start your journey hoping to retrieve all the hidden treasures, but you don't know yet that there is a wicked wizard standing against you and craving to tangle up these roads and impede your achievements.The treasure map is represented as an undirected graph in which vertices correspond to junctions and edges correspond to roads. Your path begins at a certain fixed vertex with a label known to you. Every time you come to a vertex that you have not been to before, you dig up a treasure chest and put a flag in this vertex. At the initial vertex you'll find a treasure chest immediately and, consequently, you'll put a flag there immediately as well.When you are standing at the junction you can see for each of the adjacent vertices its degree and if there is a flag there. There are no other things you can see from there. Besides, the power of the wicked wizard is so great that he is able to change the location of the roads and junctions on the map without changing the graph structure. Therefore, the sequence of the roads coming from the junction \(v\) might be different each time you come in the junction \(v\). However, keep in mind that the set of adjacent crossroads does not change, and you are well aware of previously dug treasures at each adjacent to \(v\) vertex.Your goal is to collect treasures from all vertices of the graph as fast as you can. Good luck in hunting!
|
Beginner
| 2 | 1,839 | 0 | 0 | 16 |
||||
427 |
C
|
427C
|
C. Checkposts
| 1,700 |
dfs and similar; graphs; two pointers
|
Your city has n junctions. There are m one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction i can protect junction j if either i = j or the police patrol car can go to j from i and then come back to i.Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
|
In the first line, you will be given an integer n, number of junctions (1 β€ n β€ 105). In the next line, n space-separated integers will be given. The ith integer is the cost of building checkpost at the ith junction (costs will be non-negative and will not exceed 109).The next line will contain an integer m (0 β€ m β€ 3Β·105). And each of the next m lines contains two integers ui and vi (1 β€ ui, vi β€ n; u β v). A pair ui, vi means, that there is a one-way road which goes from ui to vi. There will not be more than one road between two nodes in the same direction.
|
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo 1000000007 (109 + 7).
|
Input: 31 2 331 22 33 2 | Output: 3 1
|
Medium
| 3 | 918 | 565 | 233 | 4 |
|
339 |
B
|
339B
|
B. Xenia and Ringroad
| 1,000 |
implementation
|
Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.Xenia has recently moved into the ringroad house number 1. As a result, she's got m things to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.
|
The first line contains two integers n and m (2 β€ n β€ 105, 1 β€ m β€ 105). The second line contains m integers a1, a2, ..., am (1 β€ ai β€ n). Note that Xenia can have multiple consecutive tasks in one house.
|
Print a single integer β the time Xenia needs to complete all tasks.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 β 2 β 3 β 4 β 1 β 2 β 3. This is optimal sequence. So, she needs 6 time units.
|
Input: 4 33 2 3 | Output: 6
|
Beginner
| 1 | 603 | 204 | 215 | 3 |
1,327 |
F
|
1327F
|
F. AND Segments
| 2,500 |
bitmasks; combinatorics; data structures; dp; two pointers
|
You are given three integers \(n\), \(k\), \(m\) and \(m\) conditions \((l_1, r_1, x_1), (l_2, r_2, x_2), \dots, (l_m, r_m, x_m)\).Calculate the number of distinct arrays \(a\), consisting of \(n\) integers such that: \(0 \le a_i < 2^k\) for each \(1 \le i \le n\); bitwise AND of numbers \(a[l_i] \& a[l_i + 1] \& \dots \& a[r_i] = x_i\) for each \(1 \le i \le m\). Two arrays \(a\) and \(b\) are considered different if there exists such a position \(i\) that \(a_i \neq b_i\). The number can be pretty large so print it modulo \(998244353\).
|
The first line contains three integers \(n\), \(k\) and \(m\) (\(1 \le n \le 5 \cdot 10^5\), \(1 \le k \le 30\), \(0 \le m \le 5 \cdot 10^5\)) β the length of the array \(a\), the value such that all numbers in \(a\) should be smaller than \(2^k\) and the number of conditions, respectively.Each of the next \(m\) lines contains the description of a condition \(l_i\), \(r_i\) and \(x_i\) (\(1 \le l_i \le r_i \le n\), \(0 \le x_i < 2^k\)) β the borders of the condition segment and the required bitwise AND value on it.
|
Print a single integer β the number of distinct arrays \(a\) that satisfy all the above conditions modulo \(998244353\).
|
You can recall what is a bitwise AND operation here.In the first example, the answer is the following arrays: \([3, 3, 7, 6]\), \([3, 7, 7, 6]\) and \([7, 3, 7, 6]\).
|
Input: 4 3 2 1 3 3 3 4 6 | Output: 3
|
Expert
| 5 | 544 | 520 | 120 | 13 |
603 |
C
|
603C
|
C. Lieges of Legendre
| 2,200 |
games; math
|
Kevin and Nicky Sun have invented a new game called Lieges of Legendre. In this game, two players take turns modifying the game state with Kevin moving first. Initially, the game is set up so that there are n piles of cows, with the i-th pile containing ai cows. During each player's turn, that player calls upon the power of Sunlight, and uses it to either: Remove a single cow from a chosen non-empty pile. Choose a pile of cows with even size 2Β·x (x > 0), and replace it with k piles of x cows each. The player who removes the last cow wins. Given n, k, and a sequence a1, a2, ..., an, help Kevin and Nicky find the winner, given that both sides play in optimal way.
|
The first line of the input contains two space-separated integers n and k (1 β€ n β€ 100 000, 1 β€ k β€ 109).The second line contains n integers, a1, a2, ... an (1 β€ ai β€ 109) describing the initial state of the game.
|
Output the name of the winning player, either ""Kevin"" or ""Nicky"" (without quotes).
|
In the second sample, Nicky can win in the following way: Kevin moves first and is forced to remove a cow, so the pile contains two cows after his move. Next, Nicky replaces this pile of size 2 with two piles of size 1. So the game state is now two piles of size 1. Kevin then removes one of the remaining cows and Nicky wins by removing the other.
|
Input: 2 13 4 | Output: Kevin
|
Hard
| 2 | 669 | 213 | 86 | 6 |
404 |
E
|
404E
|
E. Maze 1D
| 2,200 |
binary search; greedy; implementation
|
Valera has a strip infinite in both directions and consisting of cells. The cells are numbered by integers. The cell number 0 has a robot.The robot has instructions β the sequence of moves that he must perform. In one move, the robot moves one cell to the left or one cell to the right, according to instructions. Before the robot starts moving, Valera puts obstacles in some cells of the strip, excluding cell number 0. If the robot should go into the cell with an obstacle according the instructions, it will skip this move.Also Valera indicates the finish cell in which the robot has to be after completing the entire instructions. The finishing cell should be different from the starting one. It is believed that the robot completed the instructions successfully, if during the process of moving he visited the finish cell exactly once β at its last move. Moreover, the latter move cannot be skipped.Let's assume that k is the minimum number of obstacles that Valera must put to make the robot able to complete the entire sequence of instructions successfully and end up in some finishing cell. You need to calculate in how many ways Valera can choose k obstacles and the finishing cell so that the robot is able to complete the instructions successfully.
|
The first line contains a sequence of characters without spaces s1s2... sn (1 β€ n β€ 106), consisting only of letters ""L"" and ""R"". If character si equals ""L"", then the robot on the i-th move must try to move one cell to the left. If the si-th character equals ""R"", then the robot on the i-th move must try to move one cell to the right.
|
Print a single integer β the required number of ways. It's guaranteed that this number fits into 64-bit signed integer type.
|
In the first sample Valera mustn't add any obstacles and his finishing cell must be cell 2.In the second sample, Valera must add an obstacle in cell number 1, and his finishing cell must be cell number - 1. In this case robot skips the first two moves and on the third move he goes straight from the starting cell to the finishing one. But if Valera doesn't add any obstacles, or adds an obstacle to another cell, then the robot visits the finishing cell more than once.
|
Input: RR | Output: 1
|
Hard
| 3 | 1,259 | 343 | 124 | 4 |
1,130 |
A
|
1130A
|
A. Be Positive
| 800 |
implementation
|
You are given an array of \(n\) integers: \(a_1, a_2, \ldots, a_n\). Your task is to find some non-zero integer \(d\) (\(-10^3 \leq d \leq 10^3\)) such that, after each number in the array is divided by \(d\), the number of positive numbers that are presented in the array is greater than or equal to half of the array size (i.e., at least \(\lceil\frac{n}{2}\rceil\)). Note that those positive numbers do not need to be an integer (e.g., a \(2.5\) counts as a positive number). If there are multiple values of \(d\) that satisfy the condition, you may print any of them. In case that there is no such \(d\), print a single integer \(0\).Recall that \(\lceil x \rceil\) represents the smallest integer that is not less than \(x\) and that zero (\(0\)) is neither positive nor negative.
|
The first line contains one integer \(n\) (\(1 \le n \le 100\)) β the number of elements in the array.The second line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\) (\(-10^3 \le a_i \le 10^3\)).
|
Print one integer \(d\) (\(-10^3 \leq d \leq 10^3\) and \(d \neq 0\)) that satisfies the given condition. If there are multiple values of \(d\) that satisfy the condition, you may print any of them. In case that there is no such \(d\), print a single integer \(0\).
|
In the first sample, \(n = 5\), so we need at least \(\lceil\frac{5}{2}\rceil = 3\) positive numbers after division. If \(d = 4\), the array after division is \([2.5, 0, -1.75, 0.5, 1.5]\), in which there are \(3\) positive numbers (namely: \(2.5\), \(0.5\), and \(1.5\)).In the second sample, there is no valid \(d\), so \(0\) should be printed.
|
Input: 510 0 -7 2 6 | Output: 4
|
Beginner
| 1 | 785 | 213 | 265 | 11 |
509 |
B
|
509B
|
B. Painting Pebbles
| 1,300 |
constructive algorithms; greedy; implementation
|
There are n piles of pebbles on the table, the i-th pile contains ai pebbles. Your task is to paint each pebble using one of the k given colors so that for each color c and any two piles i and j the difference between the number of pebbles of color c in pile i and number of pebbles of color c in pile j is at most one.In other words, let's say that bi, c is the number of pebbles of color c in the i-th pile. Then for any 1 β€ c β€ k, 1 β€ i, j β€ n the following condition must be satisfied |bi, c - bj, c| β€ 1. It isn't necessary to use all k colors: if color c hasn't been used in pile i, then bi, c is considered to be zero.
|
The first line of the input contains positive integers n and k (1 β€ n, k β€ 100), separated by a space β the number of piles and the number of colors respectively.The second line contains n positive integers a1, a2, ..., an (1 β€ ai β€ 100) denoting number of pebbles in each of the piles.
|
If there is no way to paint the pebbles satisfying the given condition, output ""NO"" (without quotes) .Otherwise in the first line output ""YES"" (without quotes). Then n lines should follow, the i-th of them should contain ai space-separated integers. j-th (1 β€ j β€ ai) of these integers should be equal to the color of the j-th pebble in the i-th pile. If there are several possible answers, you may output any of them.
|
Input: 4 41 2 3 4 | Output: YES11 41 2 41 2 3 4
|
Easy
| 3 | 625 | 286 | 422 | 5 |
|
1,676 |
H2
|
1676H2
|
H2. Maximum Crossings (Hard Version)
| 1,500 |
data structures; divide and conquer; sortings
|
The only difference between the two versions is that in this version \(n \leq 2 \cdot 10^5\) and the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).A terminal is a row of \(n\) equal segments numbered \(1\) to \(n\) in order. There are two terminals, one above the other. You are given an array \(a\) of length \(n\). For all \(i = 1, 2, \dots, n\), there should be a straight wire from some point on segment \(i\) of the top terminal to some point on segment \(a_i\) of the bottom terminal. You can't select the endpoints of a segment. For example, the following pictures show two possible wirings if \(n=7\) and \(a=[4,1,4,6,7,7,5]\). A crossing occurs when two wires share a point in common. In the picture above, crossings are circled in red.What is the maximum number of crossings there can be if you place the wires optimally?
|
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq n\)) β the elements of the array.The sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the maximum number of crossings there can be if you place the wires optimally.
|
The first test case is shown in the second picture in the statement.In the second test case, the only wiring possible has the two wires cross, so the answer is \(1\).In the third test case, the only wiring possible has one wire, so the answer is \(0\).
|
Input: 474 1 4 6 7 7 522 11132 2 2 | Output: 6 1 0 3
|
Medium
| 3 | 851 | 418 | 124 | 16 |
212 |
A
|
212A
|
A. Privatization
| 3,000 |
flows; graphs
|
There is a developed network of flights between Berland and Beerland. All of them belong to the Berland state company BerAvia. Each flight connects some Berland city with some Beerland city. For each flight airplanes fly in both directions.Changes are coming to Berland β the state decided to privatize BerAvia, namely, to sell out all flights to t private companies. Each of these companies wants to get the maximal number of flights, so if the Berland flights are sold unevenly, Berland can be accused of partiality. Berland Government decided to sell the flights as evenly as possible between the t companies.The unevenness of the distribution of flights between companies is calculated as follows. For each city i (both Berland and Beerland) we'll calculate the value of where aij is the number of flights from city i, which belong to company j. The sum of wi for all cities in both countries is called the unevenness of the distribution. The distribution with the minimal unevenness is the most even one.Help the Berland government come up with the most even distribution plan of selling flights.
|
The first input line contains four integers n, m, k and t (1 β€ n, m, t β€ 200;1 β€ k β€ 5000), where n, m are the numbers of cities in Berland and Beerland, correspondingly, k is the number of flights between them, and t is the number of private companies. Next k lines describe the flights, one per line, as pairs of positive integers xi, yi (1 β€ xi β€ n;1 β€ yi β€ m), where xi and yi are the indexes of cities in Berland and Beerland, correspondingly, connected by the i-th flight. There is at most one flight between any pair of cities, each flight connects cities of different countries. The cities in Berland are indexed from 1 to n, and in Beerland β from 1 to m.
|
Print the unevenness of the sought plan on the first line. On the second line print a sequence of k integers c1, c2, ..., ck (1 β€ ci β€ t), where ci is the index of the company that should buy the i-th flight. Assume that the flights are indexed from 1 to k in the order they appear in the input. If there are multiple solutions, print any of them.
|
Input: 3 5 8 21 41 33 31 21 12 11 52 2 | Output: 42 1 2 1 2 1 2 2
|
Master
| 2 | 1,101 | 664 | 347 | 2 |
|
500 |
D
|
500D
|
D. New Year Santa Network
| 1,900 |
combinatorics; dfs and similar; graphs; trees
|
New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads are numbered by integers from 1 to n - 1. Let's define d(u, v) as total length of roads on the path between city u and city v.As an annual event, people in Tree World repairs exactly one road per year. As a result, the length of one road decreases. It is already known that in the i-th year, the length of the ri-th road is going to become wi, which is shorter than its length before. Assume that the current year is year 1.Three Santas are planning to give presents annually to all the children in Tree World. In order to do that, they need some preparation, so they are going to choose three distinct cities c1, c2, c3 and make exactly one warehouse in each city. The k-th (1 β€ k β€ 3) Santa will take charge of the warehouse in city ck.It is really boring for the three Santas to keep a warehouse alone. So, they decided to build an only-for-Santa network! The cost needed to build this network equals to d(c1, c2) + d(c2, c3) + d(c3, c1) dollars. Santas are too busy to find the best place, so they decided to choose c1, c2, c3 randomly uniformly over all triples of distinct numbers from 1 to n. Santas would like to know the expected value of the cost needed to build the network.However, as mentioned, each year, the length of exactly one road decreases. So, the Santas want to calculate the expected after each length change. Help them to calculate the value.
|
The first line contains an integer n (3 β€ n β€ 105) β the number of cities in Tree World.Next n - 1 lines describe the roads. The i-th line of them (1 β€ i β€ n - 1) contains three space-separated integers ai, bi, li (1 β€ ai, bi β€ n, ai β bi, 1 β€ li β€ 103), denoting that the i-th road connects cities ai and bi, and the length of i-th road is li.The next line contains an integer q (1 β€ q β€ 105) β the number of road length changes.Next q lines describe the length changes. The j-th line of them (1 β€ j β€ q) contains two space-separated integers rj, wj (1 β€ rj β€ n - 1, 1 β€ wj β€ 103). It means that in the j-th repair, the length of the rj-th road becomes wj. It is guaranteed that wj is smaller than the current length of the rj-th road. The same road can be repaired several times.
|
Output q numbers. For each given change, print a line containing the expected cost needed to build the network in Tree World. The answer will be considered correct if its absolute and relative error doesn't exceed 10 - 6.
|
Consider the first sample. There are 6 triples: (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1). Because n = 3, the cost needed to build the network is always d(1, 2) + d(2, 3) + d(3, 1) for all the triples. So, the expected cost equals to d(1, 2) + d(2, 3) + d(3, 1).
|
Input: 32 3 51 3 351 42 21 22 11 1 | Output: 14.000000000012.00000000008.00000000006.00000000004.0000000000
|
Hard
| 4 | 1,617 | 781 | 221 | 5 |
2,121 |
H
|
2121H
|
H. Ice Baby
| 2,300 |
binary search; brute force; data structures; dp; implementation; sortings
|
The longest non-decreasing subsequence of an array of integers \(a_1, a_2, \ldots, a_n\) is the longest sequence of indices \(1 \leq i_1 < i_2 < \ldots < i_k \leq n\) such that \(a_{i_1} \leq a_{i_2} \leq \ldots \leq a_{i_k}\). The length of the sequence is defined as the number of elements in the sequence. For example, the length of the longest non-decreasing subsequence of the array \(a = [3, 1, 4, 1, 2]\) is \(3\).You are given two arrays of integers \(l_1, l_2, \ldots, l_n\) and \(r_1, r_2, \ldots, r_n\). For each \(1 \le k \le n\), solve the following problem: Consider all arrays of integers \(a\) of length \(k\), such that for each \(1 \leq i \leq k\), it holds that \(l_i \leq a_i \leq r_i\). Find the maximum length of the longest non-decreasing subsequence among all such arrays.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of the arrays \(l\) and \(r\).The next \(n\) lines of each test case contain two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \leq r_i \leq 10^9\)).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(n\) integers: for each \(k\) from \(1\) to \(n\), output the maximum length of the longest non-decreasing subsequence among all suitable arrays.
|
In the first test case, the only possible array is \(a = [1]\). The length of the longest non-decreasing subsequence of this array is \(1\).In the second test case, for \(k = 2\), no matter how we choose the values of \(a_1\) and \(a_2\), the condition \(a_1 > a_2\) will always hold. Therefore, the answer for \(k = 2\) will be \(1\).In the third test case, for \(k = 4\), we can choose the array \(a = [5, 3, 3, 3]\). The length of the longest non-decreasing subsequence of this array is \(3\).In the fourth test case, for \(k = 8\), we can choose the array \(a = [7, 5, 3, 5, 3, 3, 3, 3]\). The length of the longest non-decreasing subsequence of this array is \(5\).In the fifth test case, for \(k = 5\), we can choose the array \(a = [2, 8, 5, 3, 3]\). The length of the longest non-decreasing subsequence of this array is \(3\).
|
Input: 611 123 41 244 53 41 33 386 84 63 55 53 41 32 43 351 26 84 52 33 31135 12066 22941 26698 16455 153125 174139 23730 72138 212109 123174 196 | Output: 1 1 1 1 2 2 3 1 2 2 3 3 3 4 5 1 2 2 2 3 1 2 3 4 5 6 7 7 8 8 9
|
Expert
| 6 | 796 | 536 | 173 | 21 |
748 |
F
|
748F
|
F. Santa Clauses and a Soccer Championship
| 2,300 |
constructive algorithms; dfs and similar; graphs; trees
|
The country Treeland consists of n cities connected with n - 1 bidirectional roads in such a way that it's possible to reach every city starting from any other city using these roads. There will be a soccer championship next year, and all participants are Santa Clauses. There are exactly 2k teams from 2k different cities.During the first stage all teams are divided into k pairs. Teams of each pair play two games against each other: one in the hometown of the first team, and the other in the hometown of the other team. Thus, each of the 2k cities holds exactly one soccer game. However, it's not decided yet how to divide teams into pairs.It's also necessary to choose several cities to settle players in. Organizers tend to use as few cities as possible to settle the teams.Nobody wants to travel too much during the championship, so if a team plays in cities u and v, it wants to live in one of the cities on the shortest path between u and v (maybe, in u or in v). There is another constraint also: the teams from one pair must live in the same city.Summarizing, the organizers want to divide 2k teams into pairs and settle them in the minimum possible number of cities m in such a way that teams from each pair live in the same city which lies between their hometowns.
|
The first line of input contains two integers n and k (2 β€ n β€ 2Β·105, 2 β€ 2k β€ n) β the number of cities in Treeland and the number of pairs of teams, respectively.The following n - 1 lines describe roads in Treeland: each of these lines contains two integers a and b (1 β€ a, b β€ n, a β b) which mean that there is a road between cities a and b. It's guaranteed that there is a path between any two cities.The last line contains 2k distinct integers c1, c2, ..., c2k (1 β€ ci β€ n), where ci is the hometown of the i-th team. All these numbers are distinct.
|
The first line of output must contain the only positive integer m which should be equal to the minimum possible number of cities the teams can be settled in.The second line should contain m distinct numbers d1, d2, ..., dm (1 β€ di β€ n) denoting the indices of the cities where the teams should be settled.The k lines should follow, the j-th of them should contain 3 integers uj, vj and xj, where uj and vj are the hometowns of the j-th pair's teams, and xj is the city they should live in during the tournament. Each of the numbers c1, c2, ..., c2k should occur in all uj's and vj's exactly once. Each of the numbers xj should belong to {d1, d2, ..., dm}.If there are several possible answers, print any of them.
|
In the first test the orginizers can settle all the teams in the city number 2. The way to divide all teams into pairs is not important, since all requirements are satisfied anyway, because the city 2 lies on the shortest path between every two cities from {2, 4, 5, 6}.
|
Input: 6 21 21 32 42 53 62 5 4 6 | Output: 125 4 26 2 2
|
Expert
| 4 | 1,277 | 555 | 712 | 7 |
272 |
D
|
272D
|
D. Dima and Two Sequences
| 1,600 |
combinatorics; math; sortings
|
Little Dima has two sequences of points with integer coordinates: sequence (a1, 1), (a2, 2), ..., (an, n) and sequence (b1, 1), (b2, 2), ..., (bn, n).Now Dima wants to count the number of distinct sequences of points of length 2Β·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence.Dima considers two assembled sequences (p1, q1), (p2, q2), ..., (p2Β·n, q2Β·n) and (x1, y1), (x2, y2), ..., (x2Β·n, y2Β·n) distinct, if there is such i (1 β€ i β€ 2Β·n), that (pi, qi) β (xi, yi).As the answer can be rather large, print the remainder from dividing the answer by number m.
|
The first line contains integer n (1 β€ n β€ 105). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109). The third line contains n integers b1, b2, ..., bn (1 β€ bi β€ 109). The numbers in the lines are separated by spaces.The last line contains integer m (2 β€ m β€ 109 + 7).
|
In the single line print the remainder after dividing the answer to the problem by number m.
|
In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2.
|
Input: 1127 | Output: 1
|
Medium
| 3 | 760 | 284 | 92 | 2 |
79 |
A
|
79A
|
A. Bus Game
| 1,200 |
greedy
|
After Fox Ciel won an onsite round of a programming contest, she took a bus to return to her castle. The fee of the bus was 220 yen. She met Rabbit Hanako in the bus. They decided to play the following game because they got bored in the bus. Initially, there is a pile that contains x 100-yen coins and y 10-yen coins. They take turns alternatively. Ciel takes the first turn. In each turn, they must take exactly 220 yen from the pile. In Ciel's turn, if there are multiple ways to take 220 yen, she will choose the way that contains the maximal number of 100-yen coins. In Hanako's turn, if there are multiple ways to take 220 yen, she will choose the way that contains the maximal number of 10-yen coins. If Ciel or Hanako can't take exactly 220 yen from the pile, she loses. Determine the winner of the game.
|
The first line contains two integers x (0 β€ x β€ 106) and y (0 β€ y β€ 106), separated by a single space.
|
If Ciel wins, print ""Ciel"". Otherwise, print ""Hanako"".
|
In the first turn (Ciel's turn), she will choose 2 100-yen coins and 2 10-yen coins. In the second turn (Hanako's turn), she will choose 1 100-yen coin and 12 10-yen coins. In the third turn (Ciel's turn), she can't pay exactly 220 yen, so Ciel will lose.
|
Input: 2 2 | Output: Ciel
|
Easy
| 1 | 812 | 102 | 58 | 0 |
38 |
F
|
38F
|
F. Smart Boy
| 2,100 |
dp; games; strings
|
Once Petya and Vasya invented a new game and called it ""Smart Boy"". They located a certain set of words β the dictionary β for the game. It is admissible for the dictionary to contain similar words. The rules of the game are as follows: first the first player chooses any letter (a word as long as 1) from any word from the dictionary and writes it down on a piece of paper. The second player adds some other letter to this one's initial or final position, thus making a word as long as 2, then it's the first player's turn again, he adds a letter in the beginning or in the end thus making a word as long as 3 and so on. But the player mustn't break one condition: the newly created word must be a substring of a word from a dictionary. The player who can't add a letter to the current word without breaking the condition loses.Also if by the end of a turn a certain string s is written on paper, then the player, whose turn it just has been, gets a number of points according to the formula:where is a sequence number of symbol c in Latin alphabet, numbered starting from 1. For example, , and . is the number of words from the dictionary where the line s occurs as a substring at least once. Your task is to learn who will win the game and what the final score will be. Every player plays optimally and most of all tries to win, then β to maximize the number of his points, then β to minimize the number of the points of the opponent.
|
The first input line contains an integer n which is the number of words in the located dictionary (1 β€ n β€ 30). The n lines contain the words from the dictionary β one word is written on one line. Those lines are nonempty, consisting of Latin lower-case characters no longer than 30 characters. Equal words can be in the list of words.
|
On the first output line print a line ""First"" or ""Second"" which means who will win the game. On the second line output the number of points of the first player and the number of points of the second player after the game ends. Separate the numbers by a single space.
|
Input: 2abaabac | Output: Second29 35
|
Hard
| 3 | 1,439 | 335 | 270 | 0 |
|
558 |
E
|
558E
|
E. A Simple Task
| 2,300 |
data structures; sortings; strings
|
This task is very simple. Given a string S of length n and q queries each query is on the format i j k which means sort the substring consisting of the characters from i to j in non-decreasing order if k = 1 or in non-increasing order if k = 0.Output the final string after applying the queries.
|
The first line will contain two integers n, q (1 β€ n β€ 105, 0 β€ q β€ 50 000), the length of the string and the number of queries respectively. Next line contains a string S itself. It contains only lowercase English letters.Next q lines will contain three integers each i, j, k (1 β€ i β€ j β€ n, ).
|
Output one line, the string S after applying the queries.
|
First sample test explanation:
|
Input: 10 5abacdabcda7 10 05 8 11 4 03 6 07 10 1 | Output: cbcaaaabdd
|
Expert
| 3 | 295 | 295 | 57 | 5 |
26 |
A
|
26A
|
A. Almost Prime
| 900 |
number theory
|
A number is called almost prime if it has exactly two distinct prime divisors. For example, numbers 6, 18, 24 are almost prime, while 4, 8, 9, 42 are not. Find the amount of almost prime numbers which are between 1 and n, inclusive.
|
Input contains one integer number n (1 β€ n β€ 3000).
|
Output the amount of almost prime numbers between 1 and n, inclusive.
|
Input: 10 | Output: 2
|
Beginner
| 1 | 232 | 51 | 69 | 0 |
|
863 |
D
|
863D
|
D. Yet Another Array Queries Problem
| 1,800 |
data structures; implementation
|
You are given an array a of size n, and q queries to it. There are queries of two types: 1 li ri β perform a cyclic shift of the segment [li, ri] to the right. That is, for every x such that li β€ x < ri new value of ax + 1 becomes equal to old value of ax, and new value of ali becomes equal to old value of ari; 2 li ri β reverse the segment [li, ri]. There are m important indices in the array b1, b2, ..., bm. For each i such that 1 β€ i β€ m you have to output the number that will have index bi in the array after all queries are performed.
|
The first line contains three integer numbers n, q and m (1 β€ n, q β€ 2Β·105, 1 β€ m β€ 100). The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109). Then q lines follow. i-th of them contains three integer numbers ti, li, ri, where ti is the type of i-th query, and [li, ri] is the segment where this query is performed (1 β€ ti β€ 2, 1 β€ li β€ ri β€ n). The last line contains m integer numbers b1, b2, ..., bm (1 β€ bi β€ n) β important indices of the array.
|
Print m numbers, i-th of which is equal to the number at index bi after all queries are done.
|
Input: 6 3 51 2 3 4 5 62 1 32 3 61 1 62 2 1 5 3 | Output: 3 3 1 5 2
|
Medium
| 2 | 543 | 470 | 93 | 8 |
|
938 |
F
|
938F
|
F. Erasing Substrings
| 2,700 |
bitmasks; dp; greedy
|
You are given a string s, initially consisting of n lowercase Latin letters. After that, you perform k operations with it, where . During i-th operation you must erase some substring of length exactly 2i - 1 from s.Print the lexicographically minimal string you may obtain after performing k such operations.
|
The only line contains one string s consisting of n lowercase Latin letters (1 β€ n β€ 5000).
|
Print the lexicographically minimal string you may obtain after performing k operations.
|
Possible operations in examples: adcbca adcba aba; abacabadabacaba abcabadabacaba aabadabacaba aabacaba.
|
Input: adcbca | Output: aba
|
Master
| 3 | 308 | 91 | 88 | 9 |
1,870 |
G
|
1870G
|
G. MEXanization
| 3,300 |
data structures
|
Let's define \(f(S)\). Let \(S\) be a multiset (i.e., it can contain repeated elements) of non-negative integers. In one operation, you can choose any non-empty subset of \(S\) (which can also contain repeated elements), remove this subset (all elements in it) from \(S\), and add the MEX of the removed subset to \(S\). You can perform any number of such operations. After all the operations, \(S\) should contain exactly \(1\) number. \(f(S)\) is the largest number that could remain in \(S\) after any sequence of operations.You are given an array of non-negative integers \(a\) of length \(n\). For each of its \(n\) prefixes, calculate \(f(S)\) if \(S\) is the corresponding prefix (for the \(i\)-th prefix, \(S\) consists of the first \(i\) elements of array \(a\)).The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of \([2,2,1]\) is \(0\), because \(0\) does not belong to the array. The MEX of \([3,1,0,1]\) is \(2\), because \(0\) and \(1\) belong to the array, but \(2\) does not. The MEX of \([0,3,1,2]\) is \(4\), because \(0\), \(1\), \(2\) and \(3\) belong to the array, but \(4\) does not.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the size of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 2 \cdot 10^5\)) β the array \(a\).It is guaranteed that the sum of all \(n\) values across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(n\) numbers: \(f(S)\) for each of the \(n\) prefixes of array \(a\).
|
Consider the first test case. For a prefix of length \(1\), the initial multiset is \(\{179\}\). If we do nothing, we get \(179\).For a prefix of length \(2\), the initial multiset is \(\{57, 179\}\). Using the following sequence of operations, we can obtain \(2\). Apply the operation to \(\{57\}\), the multiset becomes \(\{0, 179\}\). Apply the operation to \(\{179\}\), the multiset becomes \(\{0, 0\}\). Apply the operation to \(\{0\}\), the multiset becomes \(\{0, 1\}\). Apply the operation to \(\{0, 1\}\), the multiset becomes \(\{2\}\). This is our answer. Consider the second test case. For a prefix of length \(1\), the initial multiset is \(\{0\}\). If we apply the operation to \(\{0\}\), the multiset becomes \(\{1\}\). This is the answer.
|
Input: 48179 57 2 0 2 3 2 31031 0 381 0 1 2 4 3 0 2 | Output: 179 2 3 3 3 4 4 5 1 1 2 2 1 2 2 3 3 5 5 5
|
Master
| 1 | 1,187 | 507 | 97 | 18 |
1,040 |
A
|
1040A
|
A. Palindrome Dance
| 1,000 |
greedy
|
A group of \(n\) dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them β a black one, and for the rest the suit will be bought in the future.On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on.The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
|
The first line contains three integers \(n\), \(a\), and \(b\) (\(1 \leq n \leq 20\), \(1 \leq a, b \leq 100\)) β the number of dancers, the cost of a white suit, and the cost of a black suit.The next line contains \(n\) numbers \(c_i\), \(i\)-th of which denotes the color of the suit of the \(i\)-th dancer. Number \(0\) denotes the white color, \(1\) β the black color, and \(2\) denotes that a suit for this dancer is still to be bought.
|
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.
|
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring.In the third sample, all suits are already bought and their colors form a palindrome.
|
Input: 5 100 10 1 2 1 2 | Output: 101
|
Beginner
| 1 | 1,256 | 441 | 198 | 10 |
1,366 |
F
|
1366F
|
F. Jog Around The Graph
| 2,700 |
binary search; dp; geometry; graphs
|
You are given a simple weighted connected undirected graph, consisting of \(n\) vertices and \(m\) edges.A path in the graph of length \(k\) is a sequence of \(k+1\) vertices \(v_1, v_2, \dots, v_{k+1}\) such that for each \(i\) \((1 \le i \le k)\) the edge \((v_i, v_{i+1})\) is present in the graph. A path from some vertex \(v\) also has vertex \(v_1=v\). Note that edges and vertices are allowed to be included in the path multiple times.The weight of the path is the total weight of edges in it.For each \(i\) from \(1\) to \(q\) consider a path from vertex \(1\) of length \(i\) of the maximum weight. What is the sum of weights of these \(q\) paths?Answer can be quite large, so print it modulo \(10^9+7\).
|
The first line contains a three integers \(n\), \(m\), \(q\) (\(2 \le n \le 2000\); \(n - 1 \le m \le 2000\); \(m \le q \le 10^9\)) β the number of vertices in the graph, the number of edges in the graph and the number of lengths that should be included in the answer.Each of the next \(m\) lines contains a description of an edge: three integers \(v\), \(u\), \(w\) (\(1 \le v, u \le n\); \(1 \le w \le 10^6\)) β two vertices \(v\) and \(u\) are connected by an undirected edge with weight \(w\). The graph contains no loops and no multiple edges. It is guaranteed that the given edges form a connected graph.
|
Print a single integer β the sum of the weights of the paths from vertex \(1\) of maximum weights of lengths \(1, 2, \dots, q\) modulo \(10^9+7\).
|
Here is the graph for the first example: Some maximum weight paths are: length \(1\): edges \((1, 7)\) β weight \(3\); length \(2\): edges \((1, 2), (2, 3)\) β weight \(1+10=11\); length \(3\): edges \((1, 5), (5, 6), (6, 4)\) β weight \(2+7+15=24\); length \(4\): edges \((1, 5), (5, 6), (6, 4), (6, 4)\) β weight \(2+7+15+15=39\); \(\dots\) So the answer is the sum of \(25\) terms: \(3+11+24+39+\dots\)In the second example the maximum weight paths have weights \(4\), \(8\), \(12\), \(16\) and \(20\).
|
Input: 7 8 25 1 2 1 2 3 10 3 4 2 1 5 2 5 6 7 6 4 15 5 3 1 1 7 3 | Output: 4361
|
Master
| 4 | 713 | 610 | 146 | 13 |
1,097 |
A
|
1097A
|
A. Gennady and a Card Game
| 800 |
brute force; implementation
|
Gennady owns a small hotel in the countryside where he lives a peaceful life. He loves to take long walks, watch sunsets and play cards with tourists staying in his hotel. His favorite game is called ""Mau-Mau"".To play Mau-Mau, you need a pack of \(52\) cards. Each card has a suit (Diamonds β D, Clubs β C, Spades β S, or Hearts β H), and a rank (2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, or A).At the start of the game, there is one card on the table and you have five cards in your hand. You can play a card from your hand if and only if it has the same rank or the same suit as the card on the table.In order to check if you'd be a good playing partner, Gennady has prepared a task for you. Given the card on the table and five cards in your hand, check if you can play at least one card.
|
The first line of the input contains one string which describes the card on the table. The second line contains five strings which describe the cards in your hand.Each string is two characters long. The first character denotes the rank and belongs to the set \(\{{\tt 2}, {\tt 3}, {\tt 4}, {\tt 5}, {\tt 6}, {\tt 7}, {\tt 8}, {\tt 9}, {\tt T}, {\tt J}, {\tt Q}, {\tt K}, {\tt A}\}\). The second character denotes the suit and belongs to the set \(\{{\tt D}, {\tt C}, {\tt S}, {\tt H}\}\).All the cards in the input are different.
|
If it is possible to play a card from your hand, print one word ""YES"". Otherwise, print ""NO"".You can print each letter in any case (upper or lower).
|
In the first example, there is an Ace of Spades (AS) on the table. You can play an Ace of Diamonds (AD) because both of them are Aces.In the second example, you cannot play any card.In the third example, you can play an Ace of Diamonds (AD) because it has the same suit as a Four of Diamonds (4D), which lies on the table.
|
Input: AS 2H 4C TH JH AD | Output: YES
|
Beginner
| 2 | 787 | 529 | 152 | 10 |
567 |
E
|
567E
|
E. President and Roads
| 2,200 |
dfs and similar; graphs; hashing; shortest paths
|
Berland has n cities, the capital is located in city s, and the historic home town of the President is in city t (s β t). The cities are connected by one-way roads, the travel time for each of the road is a positive integer.Once a year the President visited his historic home town t, for which his motorcade passes along some path from s to t (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from s to t, along which he will travel the fastest.The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer.
|
The first lines contain four integers n, m, s and t (2 β€ n β€ 105; 1 β€ m β€ 105; 1 β€ s, t β€ n) β the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town (s β t).Next m lines contain the roads. Each road is given as a group of three integers ai, bi, li (1 β€ ai, bi β€ n; ai β bi; 1 β€ li β€ 106) β the cities that are connected by the i-th road and the time needed to ride along it. The road is directed from city ai to city bi.The cities are numbered from 1 to n. Each pair of cities can have multiple roads between them. It is guaranteed that there is a path from s to t along the roads.
|
Print m lines. The i-th line should contain information about the i-th road (the roads are numbered in the order of appearance in the input).If the president will definitely ride along it during his travels, the line must contain a single word ""YES"" (without the quotes).Otherwise, if the i-th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word ""CAN"" (without the quotes), and the minimum cost of repairing.If we can't make the road be such that president will definitely ride along it, print ""NO"" (without the quotes).
|
The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing.In the first sample president initially may choose one of the two following ways for a ride: 1 β 2 β 4 β 5 β 6 or 1 β 2 β 3 β 5 β 6.
|
Input: 6 7 1 61 2 21 3 102 3 72 4 83 5 34 5 25 6 1 | Output: YESCAN 2CAN 1CAN 1CAN 1CAN 1YES
|
Hard
| 4 | 1,170 | 630 | 619 | 5 |
1,392 |
C
|
1392C
|
C. Omkar and Waterslide
| 1,200 |
greedy; implementation
|
Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.Omkar currently has \(n\) supports arranged in a line, the \(i\)-th of which has height \(a_i\). Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In \(1\) operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add \(1\) to each of their heights. Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!An array \(b\) is a subsegment of an array \(c\) if \(b\) can be obtained from \(c\) by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.An array \(b_1, b_2, \dots, b_n\) is called nondecreasing if \(b_i\le b_{i+1}\) for every \(i\) from \(1\) to \(n-1\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 100\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of supports Omkar has.The second line of each test case contains \(n\) integers \(a_{1},a_{2},...,a_{n}\) \((0 \leq a_{i} \leq 10^9)\) β the heights of the supports.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 minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.
|
The subarray with which Omkar performs the operation is bolded.In the first test case:First operation:\([5, 3, \textbf{2}, 5] \to [5, 3, \textbf{3}, 5]\)Second operation:\([5, \textbf{3}, \textbf{3}, 5] \to [5, \textbf{4}, \textbf{4}, 5]\)Third operation:\([5, \textbf{4}, \textbf{4}, 5] \to [5, \textbf{5}, \textbf{5}, 5]\)In the third test case, the array is already nondecreasing, so Omkar does \(0\) operations.
|
Input: 3 4 5 3 2 5 5 1 2 3 5 3 3 1 1 1 | Output: 3 2 0
|
Easy
| 2 | 991 | 520 | 154 | 13 |
1,341 |
B
|
1341B
|
B. Nastya and Door
| 1,300 |
greedy; implementation
|
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length \(k\) (\(k \ge 3\)). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.Mountains are described by a sequence of heights \(a_1, a_2, \dots, a_n\) in order from left to right (\(k \le n\)). It is guaranteed that neighboring heights are not equal to each other (that is, \(a_i \ne a_{i+1}\) for all \(i\) from \(1\) to \(n-1\)).Peaks of mountains on the segment \([l,r]\) (from \(l\) to \(r\)) are called indexes \(i\) such that \(l < i < r\), \(a_{i - 1} < a_i\) and \(a_i > a_{i + 1}\). It is worth noting that the boundary indexes \(l\) and \(r\) for the segment are not peaks. For example, if \(n=8\) and \(a=[3,1,4,1,5,9,2,6]\), then the segment \([1,8]\) has only two peaks (with indexes \(3\) and \(6\)), and there are no peaks on the segment \([3, 6]\).To break the door, Nastya throws it to a segment \([l,l+k-1]\) of consecutive mountains of length \(k\) (\(1 \le l \le n-k+1\)). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to \(p+1\), where \(p\) is the number of peaks on the segment \([l,l+k-1]\).Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains \([l, l+k-1]\) that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value \(l\) is minimal.Formally, you need to choose a segment of mountains \([l, l+k-1]\) that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value \(l\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(3 \leq k \leq n \leq 2 \cdot 10^5\)) β the number of mountains and the length of the door.The second line of the input data set contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \leq a_i \leq 10 ^ 9\), \(a_i \neq a_{i + 1}\)) β the heights of mountains.It is guaranteed that the sum of \(n\) over all the test cases will not exceed \(2 \cdot 10^5\).
|
For each test case, output two integers \(t\) and \(l\) β the maximum number of parts that the door can split into, and the left border of the segment of length \(k\) that the door should be reset to.
|
In the first example, you need to select a segment of mountains from \(2\) to \(7\). In this segment, the indexes \(3\) and \(6\) are peaks, so the answer is \(3\) (only \(2\) peaks, so the door will break into \(3\) parts). It is not difficult to notice that the mountain segments \([1, 6]\) and \([3, 8]\) are not suitable since they only have a \(1\) peak (for the first segment, the \(6\) index is not a peak, and for the second segment, the \(3\) index is not a peak).In the second example, you need to select a segment of mountains from \(2\) to \(4\). In this segment, the index \(3\) is a peak, so the answer is \(2\) (only \(1\) peak, so the door will break into \(2\) parts).In the third example, you need to select a segment of mountains from \(1\) to \(4\). In this segment, the index \(3\) is a peak, so the answer is \(2\) (only \(1\) peak, so the door will break into \(2\) parts). You can see that on the segments \([2, 5]\), \([4, 7]\) and \([5, 8]\) the number of peaks is also \(1\), but these segments have a left border greater than the segment \([1, 4]\), so they are not the correct answer.
|
Input: 5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1 | Output: 3 2 2 2 2 1 3 1 2 3
|
Easy
| 2 | 1,929 | 568 | 200 | 13 |
81 |
E
|
81E
|
E. Pairs
| 2,700 |
dfs and similar; dp; dsu; graphs; implementation; trees
|
There are n students in Polycarp's class (including himself). A few days ago all students wrote an essay ""My best friend"". Each student's essay was dedicated to one of the students of class, to his/her best friend. Note that student b's best friend is not necessarily student a, if a's best friend is b.And now the teacher leads the whole class to the museum of the history of sports programming. Exciting stories of legendary heroes await the students: tourist, Petr, tomek, SnapDragon β that's who they will hear about!The teacher decided to divide students into pairs so that each pair consisted of a student and his best friend. She may not be able to split all the students into pairs, it's not a problem β she wants to pick out the maximum number of such pairs. If there is more than one variant of doing so, she wants to pick out the pairs so that there were as much boy-girl pairs as possible. Of course, each student must not be included in more than one pair.
|
The first line contains an integer n (2 β€ n β€ 105), n is the number of students per class. Next, n lines contain information about the students, one per line. Each line contains two integers fi, si (1 β€ fi β€ n, fi β i, 1 β€ si β€ 2), where fi is the number of i-th student's best friend and si denotes the i-th pupil's sex (si = 1 for a boy and si = 2 for a girl).
|
Print on the first line two numbers t, e, where t is the maximum number of formed pairs, and e is the maximum number of boy-girl type pairs among them. Then print t lines, each line must contain a pair ai, bi (1 β€ ai, bi β€ n), they are numbers of pupils in the i-th pair. Print the pairs in any order. Print the numbers in pairs in any order. If there are several solutions, output any of them.
|
The picture corresponds to the first sample. On the picture rhomb stand for boys, squares stand for girls, arrows lead from a pupil to his/her best friend. Bold non-dashed arrows stand for pairs in the answer.
|
Input: 55 23 25 12 14 2 | Output: 2 25 34 2
|
Master
| 6 | 971 | 362 | 394 | 0 |
380 |
E
|
380E
|
E. Sereja and Dividing
| 2,600 |
data structures
|
Let's assume that we have a sequence of doubles a1, a2, ..., a|a| and a double variable x. You are allowed to perform the following two-staged operation: choose an index of the sequence element i (1 β€ i β€ |a|); consecutively perform assignments: . Let's use function g(a, x) to represent the largest value that can be obtained from variable x, using the described operation any number of times and sequence a.Sereja has sequence b1, b2, ..., b|b|. Help Sereja calculate sum: . Record [bi, bi + 1, ..., bj] represents a sequence containing the elements in brackets in the given order. To avoid problems with precision, please, print the required sum divided by |b|2.
|
The first line contains integer |b| (1 β€ |b| β€ 3Β·105) β the length of sequence b. The second line contains |b| integers b1, b2, ..., b|b| (1 β€ bi β€ 105).
|
In a single line print a real number β the required sum divided by |b|2. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
|
Input: 51 2 3 4 1 | Output: 1.238750000000000
|
Expert
| 1 | 665 | 153 | 166 | 3 |
|
1,060 |
D
|
1060D
|
D. Social Circles
| 1,900 |
greedy; math
|
You invited \(n\) guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles. Your guests happen to be a little bit shy, so the \(i\)-th guest wants to have a least \(l_i\) free chairs to the left of his chair, and at least \(r_i\) free chairs to the right. The ""left"" and ""right"" directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the \(l_i\) chairs to his left and \(r_i\) chairs to his right may overlap.What is smallest total number of chairs you have to use?
|
First line contains one integer \(n\) β number of guests, (\(1 \leqslant n \leqslant 10^5\)). Next \(n\) lines contain \(n\) pairs of space-separated integers \(l_i\) and \(r_i\) (\(0 \leqslant l_i, r_i \leqslant 10^9\)).
|
Output a single integer β the smallest number of chairs you have to use.
|
In the second sample the only optimal answer is to use two circles: a circle with \(5\) chairs accomodating guests \(1\) and \(2\), and another one with \(10\) chairs accomodationg guests \(3\) and \(4\).In the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7.
|
Input: 31 11 11 1 | Output: 6
|
Hard
| 2 | 683 | 221 | 72 | 10 |
360 |
C
|
360C
|
C. Levko and Strings
| 2,500 |
combinatorics; dp
|
Levko loves strings of length n, consisting of lowercase English letters, very much. He has one such string s. For each string t of length n, Levko defines its beauty relative to s as the number of pairs of indexes i, j (1 β€ i β€ j β€ n), such that substring t[i..j] is lexicographically larger than substring s[i..j].The boy wondered how many strings t are there, such that their beauty relative to s equals exactly k. Help him, find the remainder after division this number by 1000000007 (109 + 7).A substring s[i..j] of string s = s1s2... sn is string sisi + 1... sj.String x = x1x2... xp is lexicographically larger than string y = y1y2... yp, if there is such number r (r < p), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 > yr + 1. The string characters are compared by their ASCII codes.
|
The first line contains two integers n and k (1 β€ n β€ 2000, 0 β€ k β€ 2000).The second line contains a non-empty string s of length n. String s consists only of lowercase English letters.
|
Print a single number β the answer to the problem modulo 1000000007 (109 + 7).
|
Input: 2 2yz | Output: 26
|
Expert
| 2 | 795 | 185 | 78 | 3 |
|
50 |
D
|
50D
|
D. Bombing
| 2,100 |
binary search; dp; probabilities
|
The commanding officers decided to drop a nuclear bomb on the enemy's forces. You are ordered to determine the power of the warhead that needs to be used.The enemy has N strategically important objects. Their positions are known due to the intelligence service. The aim of the strike is to deactivate at least K important objects of the enemy. The bombing impact point is already determined and has coordinates of [X0; Y0].The nuclear warhead is marked by the estimated impact radius R β₯ 0. All the buildings that are located closer than R to the bombing epicentre will be destroyed. All the buildings that are located further than R from the epicentre, can also be deactivated with some degree of probability. Let's assume that D is the distance between a building and the epicentre. This building's deactivation probability P(D, R) is calculated according to the following formula: We should regard as ea, where e β 2.7182818284590452353602874713527If the estimated impact radius of the warhead is equal to zero, then all the buildings located in the impact point will be completely demolished and all the rest of important objects will not be damaged.The commanding officers want the probability of failing the task to be no more than Ξ΅. Nuclear warheads are too expensive a luxury, that's why you have to minimise the estimated impact radius of the warhead.
|
The first line contains an integer N which represents the number of the enemy's objects (1 β€ N β€ 100). The second line contains two integers: K is the required number of deactivated objects, and Ξ΅ is the maximally permitted probability of not completing the task, given in per mils (1 β€ K β€ N, 1 β€ Ξ΅ β€ 999). The third line contains X0 and Y0 which are the coordinates of the strike impact point. The next N lines contain two numbers Xi and Yi each which are the coordinates of every strategically important object. All the coordinates are integer, their absolute values do not exceed 1000.Let us remind you that there are a thousand per mils in unity (number one).There can be several objects in one point.
|
Print the sought estimated impact radius of the warhead. The absolute or relative measure of the inaccuracy of your answer should not exceed 10 - 6.
|
Input: 11 5005 51 2 | Output: 3.84257761518762740
|
Hard
| 3 | 1,361 | 706 | 148 | 0 |
|
1,032 |
A
|
1032A
|
A. Kitchen Utensils
| 900 |
The king's birthday dinner was attended by \(k\) guests. The dinner was quite a success: every person has eaten several dishes (though the number of dishes was the same for every person) and every dish was served alongside with a new set of kitchen utensils.All types of utensils in the kingdom are numbered from \(1\) to \(100\). It is known that every set of utensils is the same and consist of different types of utensils, although every particular type may appear in the set at most once. For example, a valid set of utensils can be composed of one fork, one spoon and one knife.After the dinner was over and the guests were dismissed, the king wondered what minimum possible number of utensils could be stolen. Unfortunately, the king has forgotten how many dishes have been served for every guest but he knows the list of all the utensils left after the dinner. Your task is to find the minimum possible number of stolen utensils.
|
The first line contains two integer numbers \(n\) and \(k\) (\(1 \le n \le 100, 1 \le k \le 100\)) β the number of kitchen utensils remaining after the dinner and the number of guests correspondingly.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 100\)) β the types of the utensils remaining. Equal values stand for identical utensils while different values stand for different utensils.
|
Output a single value β the minimum number of utensils that could be stolen by the guests.
|
In the first example it is clear that at least one utensil of type \(3\) has been stolen, since there are two guests and only one such utensil. But it is also possible that every person received only one dish and there were only six utensils in total, when every person got a set \((1, 2, 3)\) of utensils. Therefore, the answer is \(1\).One can show that in the second example at least \(2\) dishes should have been served for every guest, so the number of utensils should be at least \(24\): every set contains \(4\) utensils and every one of the \(3\) guests gets two such sets. Therefore, at least \(14\) objects have been stolen. Please note that utensils of some types (for example, of types \(2\) and \(4\) in this example) may be not present in the set served for dishes.
|
Input: 5 2 1 2 2 1 3 | Output: 1
|
Beginner
| 0 | 936 | 421 | 90 | 10 |
|
862 |
B
|
862B
|
B. Mahmoud and Ehab and the bipartiteness
| 1,300 |
dfs and similar; graphs; trees
|
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .
|
The first line of input contains an integer n β the number of nodes in the tree (1 β€ n β€ 105).The next n - 1 lines contain integers u and v (1 β€ u, v β€ n, u β v) β the description of the edges of the tree.It's guaranteed that the given graph is a tree.
|
Output one integer β the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.
|
Tree definition: https://en.wikipedia.org/wiki/Tree_(graph_theory)Bipartite graph definition: https://en.wikipedia.org/wiki/Bipartite_graphIn the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).
|
Input: 31 21 3 | Output: 0
|
Easy
| 3 | 939 | 252 | 123 | 8 |
54 |
C
|
54C
|
C. First Digit Law
| 2,000 |
dp; math; probabilities
|
In the probability theory the following paradox called Benford's law is known: ""In many lists of random numbers taken from real sources, numbers starting with digit 1 occur much more often than numbers starting with any other digit"" (that's the simplest form of the law).Having read about it on Codeforces, the Hedgehog got intrigued by the statement and wishes to thoroughly explore it. He finds the following similar problem interesting in particular: there are N random variables, the i-th of which can take any integer value from some segment [Li;Ri] (all numbers from this segment are equiprobable). It means that the value of the i-th quantity can be equal to any integer number from a given interval [Li;Ri] with probability 1 / (Ri - Li + 1).The Hedgehog wants to know the probability of the event that the first digits of at least K% of those values will be equal to one. In other words, let us consider some set of fixed values of these random variables and leave only the first digit (the MSD β most significant digit) of each value. Then let's count how many times the digit 1 is encountered and if it is encountered in at least K per cent of those N values, than such set of values will be called a good one. You have to find the probability that a set of values of the given random variables will be a good one.
|
The first line contains number N which is the number of random variables (1 β€ N β€ 1000). Then follow N lines containing pairs of numbers Li, Ri, each of whom is a description of a random variable. It is guaranteed that 1 β€ Li β€ Ri β€ 1018.The last line contains an integer K (0 β€ K β€ 100).All the numbers in the input file are integers.Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
|
Print the required probability. Print the fractional number with such a precision that the relative or absolute error of the result won't exceed 10 - 9.
|
Input: 11 250 | Output: 0.500000000000000
|
Hard
| 3 | 1,327 | 465 | 152 | 0 |
|
815 |
C
|
815C
|
C. Karen and Supermarket
| 2,400 |
brute force; dp; trees
|
On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to b dollars.The supermarket sells n goods. The i-th good can be bought for ci dollars. Of course, each good can only be bought once.Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given n coupons. If Karen purchases the i-th good, she can use the i-th coupon to decrease its price by di. Of course, a coupon cannot be used without buying the corresponding good.There is, however, a constraint with the coupons. For all i β₯ 2, in order to use the i-th coupon, Karen must also use the xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget b?
|
The first line of input contains two integers n and b (1 β€ n β€ 5000, 1 β€ b β€ 109), the number of goods in the store and the amount of money Karen has, respectively.The next n lines describe the items. Specifically: The i-th line among these starts with two integers, ci and di (1 β€ di < ci β€ 109), the price of the i-th good and the discount when using the coupon for the i-th good, respectively. If i β₯ 2, this is followed by another integer, xi (1 β€ xi < i), denoting that the xi-th coupon must also be used before this coupon can be used.
|
Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.
|
In the first test case, Karen can purchase the following 4 items: Use the first coupon to buy the first item for 10 - 9 = 1 dollar. Use the third coupon to buy the third item for 12 - 2 = 10 dollars. Use the fourth coupon to buy the fourth item for 20 - 18 = 2 dollars. Buy the sixth item for 2 dollars. The total cost of these goods is 15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.In the second test case, Karen has enough money to use all the coupons and purchase everything.
|
Input: 6 1610 910 5 112 2 120 18 310 2 32 1 5 | Output: 4
|
Expert
| 3 | 956 | 541 | 119 | 8 |
805 |
B
|
805B
|
B. 3-palindrome
| 1,000 |
constructive algorithms
|
In the beginning of the new year Keivan decided to reverse his name. He doesn't like palindromes, so he changed Naviek to Navick.He is too selfish, so for a given n he wants to obtain a string of n characters, each of which is either 'a', 'b' or 'c', with no palindromes of length 3 appearing in the string as a substring. For example, the strings ""abc"" and ""abca"" suit him, while the string ""aba"" doesn't. He also want the number of letters 'c' in his string to be as little as possible.
|
The first line contains single integer n (1 β€ n β€ 2Β·105) β the length of the string.
|
Print the string that satisfies all the constraints.If there are multiple answers, print any of them.
|
A palindrome is a sequence of characters which reads the same backward and forward.
|
Input: 2 | Output: aa
|
Beginner
| 1 | 494 | 84 | 101 | 8 |
1,054 |
E
|
1054E
|
E. Chips Puzzle
| 2,400 |
constructive algorithms; implementation; math
|
Egor came up with a new chips puzzle and suggests you to play.The puzzle has the form of a table with \(n\) rows and \(m\) columns, each cell can contain several black or white chips placed in a row. Thus, the state of the cell can be described by a string consisting of characters '0' (a white chip) and '1' (a black chip), possibly empty, and the whole puzzle can be described as a table, where each cell is a string of zeros and ones. The task is to get from one state of the puzzle some other state.To do this, you can use the following operation. select 2 different cells \((x_1, y_1)\) and \((x_2, y_2)\): the cells must be in the same row or in the same column of the table, and the string in the cell \((x_1, y_1)\) must be non-empty; in one operation you can move the last character of the string at the cell \((x_1, y_1)\) to the beginning of the string at the cell \((x_2, y_2)\). Egor came up with two states of the table for you: the initial state and the final one. It is guaranteed that the number of zeros and ones in the tables are the same. Your goal is with several operations get the final state from the initial state. Of course, Egor does not want the number of operations to be very large. Let's denote as \(s\) the number of characters in each of the tables (which are the same). Then you should use no more than \(4 \cdot s\) operations.
|
The first line contains two integers \(n\) and \(m\) (\(2 \leq n, m \leq 300\)) β the number of rows and columns of the table, respectively.The following \(n\) lines describe the initial state of the table in the following format: each line contains \(m\) non-empty strings consisting of zeros and ones. In the \(i\)-th of these lines, the \(j\)-th string is the string written at the cell \((i, j)\). The rows are enumerated from \(1\) to \(n\), the columns are numerated from \(1\) to \(m\).The following \(n\) lines describe the final state of the table in the same format.Let's denote the total length of strings in the initial state as \(s\). It is guaranteed that \(s \leq 100\, 000\). It is also guaranteed that the numbers of zeros and ones coincide in the initial and final states.
|
On the first line print \(q\) β the number of operations used. You should find such a solution that \(0 \leq q \leq 4 \cdot s\). In each the next \(q\) lines print 4 integers \(x_1\), \(y_1\), \(x_2\), \(y_2\). On the \(i\)-th line you should print the description of the \(i\)-th operation. These integers should satisfy the conditions \(1 \leq x_1, x_2 \leq n\), \(1 \leq y_1, y_2 \leq m\), \((x_1, y_1) \neq (x_2, y_2)\), \(x_1 = x_2\) or \(y_1 = y_2\). The string in the cell \((x_1, y_1)\) should be non-empty. This sequence of operations should transform the initial state of the table to the final one.We can show that a solution exists. If there is more than one solution, find any of them.
|
Consider the first example. The current state of the table:00 1001 11The first operation. The cell \((2, 1)\) contains the string \(01\). Applying the operation to cells \((2, 1)\) and \((1, 1)\), we move the \(1\) from the end of the string \(01\) to the beginning of the string \(00\) and get the string \(100\). The current state of the table:100 100 11The second operation. The cell \((1, 1)\) contains the string \(100\). Applying the operation to cells \((1, 1)\) and \((1, 2)\), we move the \(0\) from the end of the string \(100\) to the beginning of the string \(10\) and get the string \(010\). The current state of the table:10 0100 11The third operation. The cell \((1, 2)\) contains the string \(010\). Applying the operation to cells \((1, 2)\) and \((2, 2)\), we move the \(0\) from the end of the string \(010\) to the beginning of the string \(11\) and get the string \(011\). The current state of the table:10 010 011The fourth operation. The cell \((2, 2)\) contains the string \(011\). Applying the operation to cells \((2, 2)\) and \((2, 1)\), we move the \(1\) from the end of the string \(011\) to the beginning of the string \(0\) and get the string \(10\). The current state of the table:10 0110 01 It's easy to see that we have reached the final state of the table.
|
Input: 2 200 1001 1110 0110 01 | Output: 42 1 1 11 1 1 21 2 2 22 2 2 1
|
Expert
| 3 | 1,362 | 790 | 698 | 10 |
1,870 |
C
|
1870C
|
C. Colorful Table
| 1,300 |
binary search; data structures; dp; implementation; math; two pointers
|
You are given two integers \(n\) and \(k\). You are also given an array of integers \(a_1, a_2, \ldots, a_n\) of size \(n\). It is known that for all \(1 \leq i \leq n\), \(1 \leq a_i \leq k\).Define a two-dimensional array \(b\) of size \(n \times n\) as follows: \(b_{i, j} = \min(a_i, a_j)\). Represent array \(b\) as a square, where the upper left cell is \(b_{1, 1}\), rows are numbered from top to bottom from \(1\) to \(n\), and columns are numbered from left to right from \(1\) to \(n\). Let the color of a cell be the number written in it (for a cell with coordinates \((i, j)\), this is \(b_{i, j}\)).For each color from \(1\) to \(k\), find the smallest rectangle in the array \(b\) containing all cells of this color. Output the sum of width and height of this rectangle.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq n, k \leq 10^5\)) β the size of array \(a\) and the number of colors.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq k\)) β the array \(a\).It is guaranteed that the sum of the values of \(n\) and \(k\) over all test cases does not exceed \(10^5\).
|
For each test case, output \(k\) numbers: the sums of width and height of the smallest rectangle containing all cells of a color, for each color from \(1\) to \(k\).
|
In the first test case, the entire array \(b\) consists of color \(1\), so the smallest rectangle for color \(1\) has a size of \(2 \times 2\), and the sum of its sides is \(4\).In the second test case, the array \(b\) looks like this:1112One of the corner cells has color \(2\), and the other three cells have color \(1\). Therefore, the smallest rectangle for color \(1\) has a size of \(2 \times 2\), and for color \(2\) it is \(1 \times 1\).In the last test case, the array \(b\) looks like this:1111112221123211222111111
|
Input: 52 11 12 21 23 53 2 44 21 2 1 25 31 2 3 2 1 | Output: 4 4 2 0 6 6 2 0 8 6 10 6 2
|
Easy
| 6 | 784 | 531 | 165 | 18 |
1,737 |
D
|
1737D
|
D. Ela and the Wiring Wizard
| 2,200 |
brute force; dp; graphs; greedy; implementation; shortest paths
|
Ela needs to send a large package from machine \(1\) to machine \(n\) through a network of machines. Currently, with the network condition, she complains that the network is too slow and the package can't arrive in time. Luckily, a Wiring Wizard offered her a helping hand.The network can be represented as an undirected connected graph with \(n\) nodes, each node representing a machine. \(m\) wires are used to connect them. Wire \(i\) is used to connect machines \(u_i\) and \(v_i\), and has a weight \(w_i\). The aforementioned large package, if going through wire \(i\), will move from machine \(u_i\) to machine \(v_i\) (or vice versa) in exactly \(w_i\) microseconds. The Wiring Wizard can use his spell an arbitrary number of times. For each spell, he will choose the wire of index \(i\), connecting machine \(u_i\) and \(v_i\), and rewire it following these steps: Choose one machine that is connected by this wire. Without loss of generality, let's choose \(v_i\). Choose a machine that is currently connecting to \(v_i\) (including \(u_i\)), call it \(t_i\). Disconnect the wire indexed \(i\) from \(v_i\), then using it to connect \(u_i\) and \(t_i\). The rewiring of wire \(i\) will takes \(w_i\) microseconds, and the weight of the wire will not change after this operation. After a rewiring, a machine might have some wire connect it with itself. Also, the Wiring Wizard has warned Ela that rewiring might cause temporary disconnections between some machines, but Ela just ignores it anyway. Her mission is to send the large package from machine \(1\) to machine \(n\) as fast as possible. Note that the Wizard can use his spell on a wire zero, one, or many times. To make sure the network works seamlessly while transferring the large package, once the package starts transferring from machine \(1\), the Wiring Wizard cannot use his spell to move wires around anymore.Ela wonders, with the help of the Wiring Wizard, what is the least amount of time needed to transfer the large package from machine \(1\) to \(n\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The first line contains \(n\) and \(m\) (\(2 \le n \le 500\), \(n - 1 \le m \le 250 000\)), the number of nodes and number of wires, respectively.For the next \(m\) lines, \(i\)-th line will contains \(u_i\), \(v_i\) and \(w_i\) (\(1 \le u_i, v_i \le n\), \(1 \le w_i \le 10^9\)) - the indices 2 machines that are connected by the \(i\)-th edge and the weight of it.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(500\) and the sum of \(m\) over all test cases does not exceed \(250 000\). The graph in each test case is guaranteed to be connected, no self-loops, but it can contain multiple edges.
|
For each test case, output one integer denotes the least amount of time needed to transfer the large package from machine \(1\) to \(n\).
|
Here is the graph in the first test case in the sample input: Ela can ask the Wiring Wizard to use his spell on wire with the index of \(7\), which is connecting machines \(2\) and \(3\). Then, since the machine \(8\) is connected to machine \(3\), the Wiring Wizard can disconnect wire \(7\) from machine \(3\) and connect it to machine \(8\) in \(3\) microseconds (weight of wire \(3\)).After that, the package can be sent from machine \(1\) to machine \(8\) in \(6\) microseconds. Therefore, the answer is \(3 + 6 = 9\) microseconds.Here is the graph in the third test case in the sample input:
|
Input: 38 91 2 36 4 53 5 66 1 37 4 43 8 42 3 37 8 54 5 24 51 2 12 4 13 4 13 1 11 3 28 84 6 927 1 656 5 436 7 964 3 744 8 547 4 992 5 22 | Output: 9 2 154
|
Hard
| 6 | 2,032 | 787 | 137 | 17 |
1,002 |
D1
|
1002D1
|
D1. Oracle for f(x) = b * x mod 2
| 1,200 |
*special
|
Implement a quantum oracle on N qubits which implements the following function: , where (a vector of N integers, each of which can be 0 or 1).For an explanation on how this type of quantum oracles works, see Introduction to quantum oracles.You have to implement an operation which takes the following inputs: an array of N qubits x in arbitrary state (input register), 1 β€ N β€ 8, a qubit y in arbitrary state (output qubit), an array of N integers b, representing the vector . Each element of b will be 0 or 1.The operation doesn't have an output; its ""output"" is the state in which it leaves the qubits.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (x : Qubit[], y : Qubit, b : Int[]) : () { body { // your code here } }}
|
Easy
| 1 | 825 | 0 | 0 | 10 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.