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\) consisti...
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...
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 constr...
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 h...
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 varia...
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...
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 ahh...
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 tran...
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...
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\...
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 t...
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...
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 ...
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 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 \(...
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...
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 ...
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 po...
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 t...
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 whe...
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...
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,...
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...
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 ...
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 c...
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 vec...
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)...
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\) hol...
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...
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, s...
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...
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 eithe...
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...
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 inte...
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 ...
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 sev...
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 ho...
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 ...
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 \...
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[...
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_...
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\)) β€” t...
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 wo...
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...
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 guarantee...
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 \(O...
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...
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 becaus...
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\)...
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\) li...
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 co...
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...
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...
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 \...
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 s...
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\) int...
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. Fo...
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 cur...
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...
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 horizontal...
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 ...
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 int...
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....
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 ...
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 de...
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 pos...
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...
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 p...
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...
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_...
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 ...
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 t...
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. Eac...
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 recogniti...
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 numbe...
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\) ...
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]\)...
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 p...
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, t...
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 ≀ a...
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 prob...
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...
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...
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 r...
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}\), \...
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 writt...
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. chan...
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 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...
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...
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 to...
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 thi...
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 (b...
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...
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 choos...
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...
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 ...
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 +...
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 s...
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 p...
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 ...
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 ...
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, finis...
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 t...
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 \...
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 ...
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 ne...
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...
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 pr...
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 o...
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 necessar...
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 ...
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 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 ...
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 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 tu...
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...
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\) h...
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 + (...
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...
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 polynomia...
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 ≀ a...
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 ...
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 eithe...
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Β·...
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-t...
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...
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 ...
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 pow...
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...
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 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...
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...
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 s...
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\), s...
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.I...
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 o...
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\) ...
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\)) β€” ...
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, ...
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 posit...
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 so...
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 tot...
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 le...
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...
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 ...
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\)...
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 citi...
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 roa...
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 sho...
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 ...
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. T...
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 fro...
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...
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...
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 que...
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\)....
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\) intege...
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 become...
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.O...
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...
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 th...
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 v...
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 ...
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...
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,...
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}, {\t...
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 tabl...
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...
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 β‰  b...
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 ...
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 nondecrea...
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 c...
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}, ...
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...
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...
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 suit...
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 tea...
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...
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 ...
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 ob...
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 ch...
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 hi...
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 ...
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 importan...
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 li...
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\) t...
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 rema...
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, th...
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 ...
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-bipart...
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 Hedgeho...
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 fil...
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 ...
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 ...
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 o...
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 substrin...
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 ch...
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 ...
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 satisf...
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 c...
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...
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...
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...
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...
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 \...
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 conn...
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...
Easy
1
825
0
0
10