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,737
B
1737B
B. Ela's Fitness and the Luxury Number
1,300
binary search; implementation; math
While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football.Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being ""Luxury"" days. She counts the days since she started th...
Each test contains multiple test cases. The first line has the number of test cases \(t\) (\(1 \le t \le 10\ 000\)). The description of the test cases follows.The only line of each test case contains two integers \(l\) and \(r\) (\(1 \le l \le r \le 10^{18}\)) β€” the intervals at which you want to know how many times El...
For each test case, output an integer that denotes the answer.
In the first test case, \(5\) luxury numbers in range \([8, 19]\) are: \(8, 9, 12, 15, 16\).
Input: 58 198 20119 1211 1000000000000000001234567891011 1000000000000000000 | Output: 5 6 2 948683296 2996666667
Easy
3
1,503
341
62
17
1,152
A
1152A
A. Neko Finds Grapes
800
greedy; implementation; math
On a random day, Neko found \(n\) treasure chests and \(m\) keys. The \(i\)-th chest has an integer \(a_i\) written on it and the \(j\)-th key has an integer \(b_j\) on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible.The \(j\)-th key ...
The first line contains integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^5\)) β€” the number of chests and the number of keys.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) β€” the numbers written on the treasure chests.The third line contains \(m\) integers \(b_1, b_2, \ldots, ...
Print the maximum number of chests you can open.
In the first example, one possible way to unlock \(3\) chests is as follows: Use first key to unlock the fifth chest, Use third key to unlock the second chest, Use fourth key to unlock the first chest. In the second example, you can use the only key to unlock any single chest (note that one key can't be used twice).In ...
Input: 5 4 9 14 6 2 11 8 4 7 20 | Output: 3
Beginner
3
620
387
48
11
2,101
A
2101A
A. Mex in the Grid
1,300
constructive algorithms; implementation
You are given \(n^2\) cards with values from \(0\) to \(n^2-1\). You are to arrange them in a \(n\) by \(n\) grid such that there is exactly one card in each cell.The MEX (minimum excluded value) of a subgrid\(^{\text{βˆ—}}\) is defined as the smallest non-negative integer that does not appear in the subgrid.Your task is...
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 of each test case contains a single integer \(n\) (\(1\le n\le 500\)) β€” the side length of the grid.It is guaranteed that the sum of \(n\) over ...
For each test case, output \(n\) lines, each containing \(n\) integers representing the elements of the grid. If there are multiple answers, you can output any of them.
In the first test case, one valid arrangement is: 0123 There are \(9\) subgrids in total, and the \(4\) of them with non-zero MEX are shown below:0 values:\([0]\) β€” MEX: \(1\)01 values:\([0, 1]\) β€” MEX: \(2\)02 values:\([0, 2]\) β€” MEX: \(1\)0123 values:\([0, 1, 2, 3]\) β€” MEX: \(4\)The sum of MEX over all subgrids would...
Input: 223 | Output: 0 1 2 3 8 4 5 6 0 1 7 2 3
Easy
2
759
360
168
21
755
F
755F
F. PolandBall and Gifts
2,600
bitmasks; dp; greedy
It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are n Balls overall. Each Ball has someone for whom he should bring a present according to some permutation p, pi β‰  i for all i.Unfortunately, Balls are quite clumsy. We know earlier that exactly k of them will forget to bring their ...
The first line of input contains two integers n and k (2 ≀ n ≀ 106, 0 ≀ k ≀ n), representing the number of Balls and the number of Balls who will forget to bring their presents. The second line contains the permutation p of integers from 1 to n, where pi is the index of Ball who should get a gift from the i-th Ball. Fo...
You should output two values β€” minimum and maximum possible number of Balls who will not get their presents, in that order.
In the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer i...
Input: 5 23 4 1 5 2 | Output: 2 4
Expert
3
629
342
123
7
1,603
C
1603C
C. Extreme Extension
2,300
dp; greedy; math; number theory
For an array \(b\) of \(n\) integers, the extreme value of this array is the minimum number of times (possibly, zero) the following operation has to be performed to make \(b\) non-decreasing: Select an index \(i\) such that \(1 \le i \le |b|\), where \(|b|\) is the current length of \(b\). Replace \(b_i\) with two elem...
The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^5\)).It is guaranteed that t...
For each test case, print a single integer β€” the sum of extreme values of all subarrays of \(a\) modulo \(998\,244\,353\).
Let \(f(l, r)\) denote the extreme value of \([a_l, a_{l+1}, \ldots, a_r]\).In the first test case, \(f(1, 3) = 3\), because YouKn0wWho can perform the following operations on the subarray \([5, 4, 3]\) (the newly inserted elements are underlined):\([5, 4, 3] \rightarrow [\underline{3}, \underline{2}, 4, 3] \rightarrow...
Input: 4 3 5 4 3 4 3 2 1 4 1 69 8 7264 40515 28226 92776 35285 21709 75124 48163 | Output: 5 9 0 117
Expert
4
1,480
380
122
16
1,826
F
1826F
F. Fading into Fog
2,800
geometry; interactive; math; probabilities
This is an interactive problem.There are \(n\) distinct hidden points with real coordinates on a two-dimensional Euclidean plane. In one query, you can ask some line \(ax + by + c = 0\) and get the projections of all \(n\) points to this line in some order. The given projections are not exact, please read the interacti...
The first line contains a single integer \(t\) (\(1 \leq t \leq 50\)) β€” the number of test cases.The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 25\)) β€” the number of hidden points.For each test case, it is guaranteed that for any pair of hidde...
In the sample the hidden points are \((1, 3)\) and \((2.5, 0.5)\)A picture, which describes the first query:A picture, which describes the second query:
Input: 1 2 1 1 2.5 1 1.500000001 1.500000000 2 2 | Output: ? 0 1 -1 ? 0.2 -0.2 0 ! 1 3 2.5 0.500000001
Master
4
780
542
0
18
935
C
935C
C. Fifa and Fafa
1,600
geometry
Fifa and Fafa are sharing a flat. Fifa loves video games and wants to download a new soccer game. Unfortunately, Fafa heavily uses the internet which consumes the quota. Fifa can access the internet through his Wi-Fi access point. This access point can be accessed within a range of r meters (this range can be chosen by...
The single line of the input contains 5 space-separated integers R, x1, y1, x2, y2 (1 ≀ R ≀ 105, |x1|, |y1|, |x2|, |y2| ≀ 105).
Print three space-separated numbers xap, yap, r where (xap, yap) is the position which Fifa chose for the access point and r is the radius of its range. Your answer will be considered correct if the radius does not differ from optimal more than 10 - 6 absolutely or relatively, and also the radius you printed can be cha...
Input: 5 3 3 1 1 | Output: 3.7677669529663684 3.7677669529663684 3.914213562373095
Medium
1
878
127
494
9
205
A
205A
A. Little Elephant and Rozdil
900
brute force; implementation
The Little Elephant loves Ukraine very much. Most of all he loves town Rozdol (ukr. ""Rozdil"").However, Rozdil is dangerous to settle, so the Little Elephant wants to go to some other town. The Little Elephant doesn't like to spend much time on travelling, so for his journey he will choose a town that needs minimum ti...
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of cities. The next line contains n integers, separated by single spaces: the i-th integer represents the time needed to go from town Rozdil to the i-th town. The time values are positive integers, not exceeding 109.You can consider the cities number...
Print the answer on a single line β€” the number of the town the Little Elephant will go to. If there are multiple cities with minimum travel time, print ""Still Rozdil"" (without the quotes).
In the first sample there are only two cities where the Little Elephant can go. The travel time for the first town equals 7, to the second one β€” 4. The town which is closest to Rodzil (the only one) is the second one, so the answer is 2.In the second sample the closest cities are cities two and five, the travelling tim...
Input: 27 4 | Output: 2
Beginner
2
590
387
190
2
1,327
C
1327C
C. Game with Chips
1,600
constructive algorithms; implementation
Petya has a rectangular Board of size \(n \times m\). Initially, \(k\) chips are placed on the board, \(i\)-th chip is located in the cell at the intersection of \(sx_i\)-th row and \(sy_i\)-th column.In one action, Petya can move all the chips to the left, right, down or up by \(1\) cell.If the chip was in the \((x, y...
The first line contains three integers \(n, m, k\) (\(1 \le n, m, k \le 200\)) β€” the number of rows and columns of the board and the number of chips, respectively.The next \(k\) lines contains two integers each \(sx_i, sy_i\) (\( 1 \le sx_i \le n, 1 \le sy_i \le m\)) β€” the starting position of the \(i\)-th chip.The nex...
In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once.In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters \(L, R, D, U\) respectively.If the required sequence does not exist, ...
Input: 3 3 2 1 2 2 1 3 3 3 2 | Output: 3 DRD
Medium
2
1,168
480
348
13
20
A
20A
A. BerOS file system
1,700
implementation
The new operating system BerOS has a nice feature. It is possible to use any number of characters '/' as a delimiter in path instead of one traditional '/'. For example, strings //usr///local//nginx/sbin// and /usr/local/nginx///sbin are equivalent. The character '/' (or some sequence of such characters) at the end of ...
The first line of the input contains only lowercase Latin letters and character '/' β€” the path to some directory. All paths start with at least one character '/'. The length of the given line is no more than 100 characters, it is not empty.
The path in normalized form.
Input: //usr///local//nginx/sbin | Output: /usr/local/nginx/sbin
Medium
1
587
240
28
0
809
C
809C
C. Find a car
2,600
combinatorics; divide and conquer; dp
After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.Formal...
The first line contains one integer q (1 ≀ q ≀ 104) β€” the number of Leha's requests.The next q lines contain five integers x1, y1, x2, y2, k (1 ≀ x1 ≀ x2 ≀ 109, 1 ≀ y1 ≀ y2 ≀ 109, 1 ≀ k ≀ 2Β·109) β€” parameters of Leha's requests.
Print exactly q lines β€” in the first line print the answer to the first request, in the second β€” the answer to the second request and so on.
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentall...
Input: 41 1 1 1 13 2 5 4 51 1 5 5 100001 4 2 5 2 | Output: 113930
Expert
3
1,588
227
140
8
25
C
25C
C. Roads in Berland
1,900
graphs; shortest paths
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length β€” an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance betw...
The first line contains integer n (2 ≀ n ≀ 300) β€” amount of cities in Berland. Then there follow n lines with n integer numbers each β€” the matrix of shortest distances. j-th integer in the i-th row β€” di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix ...
Output k space-separated integers qi (1 ≀ i ≀ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we cou...
Input: 20 55 011 2 3 | Output: 3
Hard
2
868
923
339
0
1,400
B
1400B
B. RPG Protagonist
1,700
brute force; greedy; math
You are playing one RPG from the 2010s. You are planning to raise your smithing skill, so you need as many resources as possible. So how to get resources? By stealing, of course.You decided to rob a town's blacksmith and you take a follower with you. You can carry at most \(p\) units and your follower β€” at most \(f\) u...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(p\) and \(f\) (\(1 \le p, f \le 10^9\)) β€” yours and your follower's capacities.The second line of each test case contains two integers \(cnt_s\) and \(cnt_w\) (\(1 \l...
For each test case, print the maximum number of weapons (both swords and war axes) you and your follower can carry.
In the first test case: you should take \(3\) swords and \(3\) war axes: \(3 \cdot 5 + 3 \cdot 6 = 33 \le 33\) and your follower β€” \(3\) swords and \(2\) war axes: \(3 \cdot 5 + 2 \cdot 6 = 27 \le 27\). \(3 + 3 + 3 + 2 = 11\) weapons in total.In the second test case, you can take all available weapons even without your...
Input: 3 33 27 6 10 5 6 100 200 10 10 5 5 1 19 1 3 19 5 | Output: 11 20 3
Medium
3
657
674
115
14
1,121
A
1121A
A. Technogoblet of Fire
1,100
implementation; sortings
Everybody knows that the \(m\)-coder Tournament will happen soon. \(m\) schools participate in the tournament, and only one student from each school participates.There are a total of \(n\) students in those schools. Before the tournament, all students put their names and the names of their schools into the Technogoblet...
The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \le n \le 100\), \(1 \le m, k \le n\)) β€” the total number of students, the number of schools and the number of the Chosen Ones.The second line contains \(n\) different integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)), where \(p_i\) denotes the p...
Output a single integer β€” the minimal number of schools to be made up by Arkady so that \(k\) Chosen Ones would be selected by the Technogoblet.
In the first example there's just a single Chosen One with id \(3\). His power is equal to \(3\), but in the same school \(1\), there's a student with id \(5\) and power \(6\), and that means inaction would not lead to the latter being chosen. If we, however, make up a new school (let its id be \(4\)) for the Chosen On...
Input: 7 3 1 1 5 3 4 6 7 2 1 3 1 2 1 2 3 3 | Output: 1
Easy
2
995
725
144
11
461
C
461C
C. Appleman and a Sheet of Paper
2,200
data structures; implementation
Appleman has a very big sheet of paper. This sheet has a form of rectangle with dimensions 1 Γ— n. Your task is help Appleman with folding of such a sheet. Actually, you need to perform q queries. Each query will have one of the following types: Fold the sheet of paper at position pi. After this query the leftmost part ...
The first line contains two integers: n and q (1 ≀ n ≀ 105; 1 ≀ q ≀ 105) β€” the width of the paper and the number of queries.Each of the following q lines contains one of the described queries in the following format: ""1 pi"" (1 ≀ pi < [current width of sheet]) β€” the first type query. ""2 li ri"" (0 ≀ li < ri ≀ [curren...
For each query of the second type, output the answer.
The pictures below show the shapes of the paper during the queries of the first example: After the first fold operation the sheet has width equal to 4, after the second one the width of the sheet equals to 2.
Input: 7 41 31 22 0 12 1 2 | Output: 43
Hard
2
859
363
53
4
78
A
78A
A. Haiku
800
implementation; strings
Haiku is a genre of Japanese traditional poetry.A haiku poem consists of 17 syllables split into three phrases, containing 5, 7 and 5 syllables correspondingly (the first phrase should contain exactly 5 syllables, the second phrase should contain exactly 7 syllables, and the third phrase should contain exactly 5 syllab...
The input data consists of three lines. The length of each line is between 1 and 100, inclusive. The i-th line contains the i-th phrase of the poem. Each phrase consists of one or more words, which are separated by one or more spaces. A word is a non-empty sequence of lowercase Latin letters. Leading and/or trailing sp...
Print ""YES"" (without the quotes) if the poem is a haiku. Otherwise, print ""NO"" (also without the quotes).
Input: on codeforces beta round is running a rustling of keys | Output: YES
Beginner
2
925
434
109
0
138
A
138A
A. Literature Lesson
1,600
implementation
Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes.Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters ""a"", ""e"", ""i"", ""o"", ""u"" are considered vowels.Two lines rhyme if ...
The first line contains two integers n and k (1 ≀ n ≀ 2500, 1 ≀ k ≀ 5) β€” the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104.If we assume that the l...
Print the rhyme scheme of the poem as ""aabb"", ""abab"", ""abba"", ""aaaa""; or ""NO"" if the poem does not belong to any of the above mentioned schemes.
In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is ""NO"".
Input: 1 1daymaysunfun | Output: aabb
Medium
1
1,510
472
154
1
638
B
638B
B. Making Genome in Berland
1,500
*special; dfs and similar; strings
Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we've used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct En...
The first line of the input contains a positive integer n (1 ≀ n ≀ 100) β€” the number of genome fragments.Each of the next lines contains one descriptions of a fragment. Each fragment is a non-empty string consisting of distinct small letters of the English alphabet. It is not guaranteed that the given fragments are dis...
In the single line of the output print the genome of the minimum length that contains all the given parts. All the nucleotides in the genome must be distinct. If there are multiple suitable strings, print the string of the minimum length. If there also are multiple suitable strings, you can print any of them.
Input: 3bcdabcdef | Output: abcdef
Medium
3
1,015
531
310
6
1,408
I
1408I
I. Bitwise Magic
3,200
dp; math
You are given a positive integer \(k\) and an array \(a_1, a_2, \ldots, a_n\) of non-negative distinct integers not smaller than \(k\) and not greater than \(2^c-1\).In each of the next \(k\) seconds, one element is chosen randomly equiprobably out of all \(n\) elements and decreased by \(1\).For each integer \(x\), \(...
The first line of input contains three integers \(n, k, c\) (\(1 \leq n \leq (2^c - k)\), \(1 \leq k \leq 16\), \(1 \leq c \leq 16\)).The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(k \leq a_i \leq 2^c-1\)).
Print \(2^c\) integers: the probability that the bitwise XOR is equal to \(x\) in the end for \(x\) in \(\{0, 1, \ldots, 2^c-1\}\) modulo \(998\,244\,353\).
Input: 4 1 3 1 2 3 4 | Output: 0 0 0 748683265 0 499122177 0 748683265
Master
2
620
237
156
14
858
A
858A
A. k-rounding
1,100
brute force; math; number theory
For a given positive integer n denote its k-rounding as the minimum positive integer x, such that x ends with k or more zeros in base 10 and is divisible by n.For example, 4-rounding of 375 is 375Β·80 = 30000. 30000 is the minimum integer such that it ends with 4 or more zeros and is divisible by 375.Write a program tha...
The only line contains two integers n and k (1 ≀ n ≀ 109, 0 ≀ k ≀ 8).
Print the k-rounding of n.
Input: 375 4 | Output: 30000
Easy
3
355
69
26
8
778
B
778B
B. Bitwise Formula
1,800
bitmasks; brute force; dfs and similar; expression parsing; implementation
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game.Initially, Bob chooses integer m, bit depth of the game, which means that all numbers in the game will consist of m bits. Then he asks Peter to choose some m-bit number. After that, Bo...
The first line contains two integers n and m, the number of variables and bit depth, respectively (1 ≀ n ≀ 5000; 1 ≀ m ≀ 1000). The following n lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign "":="", space, ...
In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as m-bit binary number...
In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15.For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn't...
Input: 3 3a := 101b := 011c := ? XOR b | Output: 011100
Medium
5
879
709
322
7
1,090
K
1090K
2,000
math
Hard
1
0
0
0
10
846
D
846D
D. Monitor
1,900
binary search; data structures
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n Γ— m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k Γ— k consisting entirely of broken pixels. She knows that q pixels are alread...
The first line contains four integer numbers n, m, k, q (1 ≀ n, m ≀ 500, 1 ≀ k ≀ min(n, m), 0 ≀ q ≀ nΒ·m) β€” the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.Each of next q lines contain three intege...
Print one number β€” the minimum moment the monitor became broken, or ""-1"" if it's still not broken after these q pixels stopped working.
Input: 2 3 2 52 1 82 2 81 2 11 3 42 3 2 | Output: 8
Hard
2
528
568
137
8
1,774
F2
1774F2
F2. Magician and Pigs (Hard Version)
2,700
binary search; brute force; data structures; implementation
This is the hard version of the problem. The only difference between the two versions is the constraint on \(n\) and \(x\). You can make hacks only if both versions of the problem are solved.Little09 has been interested in magic for a long time, and it's so lucky that he meets a magician! The magician will perform \(n\...
The first line contains a single integer \(n\) (\(1\leq n\leq 8\cdot 10^5\)) β€” the number of operations.Each of the following \(n\) lines contains an operation given in the form described in the problem statement. It's guaranteed that \(1\leq x\leq 10^9\) in operations of the first two types.
Print a single integer β€” the number of living pigs after all the operations, modulo \(998\,244\,353\).
In the first example, the operations are equivalent to repeating four times: create a pig with \(8\) Health Points and then reduce the Health Points of all living pigs by \(3\). It is easy to find that there are two living pigs in the end with \(2\) and \(5\) Health Points.
Input: 4 1 8 2 3 3 3 | Output: 2
Master
4
898
293
102
17
2,011
F
2011F
F. Good Subarray
0
*special; data structures; greedy
You are given an integer array \(a\) of size \(n\).Let's call an array good if it can be obtained using the following algorithm: create an array consisting of any single integer; and then perform the following operation an arbitrary number of times: choose an element from the already existing array (let's call it \(x\)...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\( 1 \le a_i \le n)\).Additional constraint...
For each test case, print a single integer β€” the number of good contiguous subarrays of the array \(a\).
In the first example, the following four subarrays are good: from the \(1\)-st to the \(1\)-st element; from the \(1\)-st to the \(2\)-nd element; from the \(2\)-nd to the \(2\)-nd element; from the \(3\)-rd to the \(3\)-rd element. In the second example, the only subarray which is not good is the subarray from the \(3...
Input: 431 1 343 2 3 11184 5 6 5 3 2 3 1 | Output: 4 9 1 23
Beginner
3
705
405
104
20
981
G
981G
G. Magic multisets
2,500
data structures
In the School of Magic in Dirtpolis a lot of interesting objects are studied on Computer Science lessons.Consider, for example, the magic multiset. If you try to add an integer to it that is already presented in the multiset, each element in the multiset duplicates. For example, if you try to add the integer \(2\) to t...
The first line contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 2 \cdot 10^{5}\)) β€” the number of magic multisets in the array and the number of queries, respectively.The next \(q\) lines describe queries, one per line. Each line starts with an integer \(t\) (\(1 \leq t \leq 2\)) β€” the type of the query. If \(...
For each query of the second type print the sum of sizes of multisets on the given segment.The answers can be large, so print them modulo \(998244353\).
In the first example after the first two queries the multisets are equal to \([\{1, 2\},\{1, 2\},\{\},\{\}]\), after the third query they are equal to \([\{1, 1, 2, 2\},\{1, 1, 2, 2\},\{1\},\{1\}]\).In the second example the first multiset evolves as follows: \(\{\} \to \{3\} \to \{3, 3\} \to \{2, 3, 3\} \to \{1, 2, 3,...
Input: 4 41 1 2 11 1 2 21 1 4 12 1 4 | Output: 10
Expert
1
1,010
741
152
9
1,950
G
1950G
G. Shuffling Songs
1,900
bitmasks; dfs and similar; dp; graphs; hashing; implementation; strings
Vladislav has a playlist consisting of \(n\) songs, numbered from \(1\) to \(n\). Song \(i\) has genre \(g_i\) and writer \(w_i\). He wants to make a playlist in such a way that every pair of adjacent songs either have the same writer or are from the same genre (or both). He calls such a playlist exciting. Both \(g_i\)...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 16\)) β€” the number of songs in the original playlist.Then \(n\) lines follow, the \(i\)-th ...
For each test case, output a single integer β€” the minimum number of removals necessary so that the resulting playlist can be made exciting.
In the first test case, the playlist is already exciting.In the second test case, if you have the songs in the order \(4, 3, 1, 2\), it is exciting, so you don't need to remove any songs.In the third test case, you can remove songs \(4, 5, 6, 7\). Then the playlist with songs in the order \(1, 2, 3\) is exciting.
Input: 41pop taylorswift4electronic themotanselectronic carlasdreamspop themotanspop irinarimes7rap eminemrap drdrerap kanyewestpop taylorswiftindierock arcticmonkeysindierock arcticmonkeyspunkrock theoffspring4a bc de fg h | Output: 0 0 4 3
Hard
7
942
681
139
19
325
B
325B
B. Stadium and Games
1,800
binary search; math
Daniel is organizing a football tournament. He has come up with the following tournament format: In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are h...
The first line contains a single integer n (1 ≀ n ≀ 1018), the number of games that should be played.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.
Print all possible numbers of invited teams in ascending order, one per line. If exactly n games cannot be played, output one number: -1.
Input: 3 | Output: 34
Medium
2
1,249
248
137
3
1,533
B
1533B
B. Nearest Point Function
0
*special; implementation
Your friend has created a nearest point function. For a given array of integer points \(x\) (sorted in ascending order, without any duplicates) and a point \(y\) it can find the nearest point from \(x\) to the point \(y\). In other words, it will find such a point \(x_i\) that \(|y - x_i|\) is the minimum possible, whe...
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of the test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of points in the array \(x\).The second line of the test case contains \(n\) integers \(x_1, x_2, \ldots, x_n\) ...
For each test case, print YES if it is possible to find some integer point \(y\) that will crash the function and NO otherwise.
In the first test case of the example, the function crashes if \(y = 2\) is chosen.In the fifth test case of the example, the function crashes if \(y = 500000000\) is chosen.In the sixth test case of the example, the function crashes if \(y = 10\) is chosen.
Input: 7 2 1 3 2 1 100 3 1 50 101 2 1 1000000000 2 1 999999999 6 1 2 5 7 9 11 6 1 2 5 8 9 12 | Output: YES NO NO NO YES YES NO
Beginner
2
1,053
654
127
15
57
D
57D
D. Journey
2,500
dp; math
Stewie the Rabbit explores a new parallel universe. This two dimensional universe has the shape of a rectangular grid, containing n lines and m columns. The universe is very small: one cell of the grid can only contain one particle. Each particle in this universe is either static or dynamic. Each static particle always...
The first line contains two space-separated integers: n, m (2 ≀ n, m ≀ 1000) which represent the sizes of the universe. The next n lines containing m symbols each describe the universe without dynamic particles β€” the j-th symbol of the i-th line equals to 'X' if the cell is occupied by a static particle, and to '.' if ...
You have to print on a single line a single number which is the average life span of a particle with an accuracy of at least 6 decimal places.The answer will be accepted if it is within 10 - 6 of absolute or relative error from the correct answer.
Input: 2 2...X | Output: 0.888888888889
Expert
2
1,243
555
247
0
593
B
593B
B. Anton and Lines
1,600
geometry; sortings
The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = kiΒ·x + bi. It was necessary to determine whether there is at least one point of intersection of two of these lines,...
The first line of the input contains an integer n (2 ≀ n ≀ 100 000) β€” the number of lines in the task given to Anton. The second line contains integers x1 and x2 ( - 1 000 000 ≀ x1 < x2 ≀ 1 000 000) defining the strip inside which you need to find a point of intersection of at least two lines.The following n lines cont...
Print ""Yes"" (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print ""No"" (without quotes).
In the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it.
Input: 41 21 21 00 10 2 | Output: NO
Medium
2
765
534
168
5
449
C
449C
C. Jzzhu and Apples
2,500
constructive algorithms; number theory
Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to an apple store. Jzzhu will pack his apples into groups and then sell them. Each group must contain two apples, and the greatest common divisor of numbers of the apples in each group must be greater t...
A single integer n (1 ≀ n ≀ 105), the number of the apples.
The first line must contain a single integer m, representing the maximum number of groups he can get. Each of the next m lines must contain two integers β€” the numbers of apples in the current group.If there are several optimal answers you can print any of them.
Input: 6 | Output: 26 32 4
Expert
2
463
59
261
4
1,149
B
1149B
B. Three Religions
2,200
dp; implementation; strings
During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now wonder if the followers of each religion could coexist in peace.The Word of Unive...
The first line of the input contains two integers \(n, q\) (\(1 \leq n \leq 100\,000\), \(1 \leq q \leq 1000\)) β€” the length of the Word of Universe and the number of religion evolutions, respectively. The following line contains the Word of Universe β€” a string of length \(n\) consisting of lowercase English characters...
Write \(q\) lines. The \(i\)-th of them should be YES if the religions could coexist in peace after the \(i\)-th evolution, or NO otherwise.You can print each character in any case (either upper or lower).
In the first example, after the 6th evolution the religion descriptions are: ad, bc, and ab. The following figure shows how these descriptions form three disjoint subsequences of the Word of Universe:
Input: 6 8 abdabc + 1 a + 1 d + 2 b + 2 c + 3 a + 3 b + 1 c - 2 | Output: YES YES YES YES YES YES NO YES
Hard
3
1,265
815
205
11
196
C
196C
C. Paint Tree
2,200
constructive algorithms; divide and conquer; geometry; sortings; trees
You are given a tree with n vertexes and n points on a plane, no three points lie on one straight line.Your task is to paint the given tree on a plane, using the given points as vertexes. That is, you should correspond each vertex of the tree to exactly one point and each point should correspond to a vertex. If two ver...
The first line contains an integer n (1 ≀ n ≀ 1500) β€” the number of vertexes on a tree (as well as the number of chosen points on the plane).Each of the next n - 1 lines contains two space-separated integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the numbers of tree vertexes connected by the i-th edge.Each of the next n...
Print n distinct space-separated integers from 1 to n: the i-th number must equal the number of the vertex to place at the i-th point (the points are numbered in the order, in which they are listed in the input).If there are several solutions, print any of them.
The possible solutions for the sample are given below.
Input: 31 32 30 01 12 0 | Output: 1 3 2
Hard
5
605
558
262
1
30
B
30B
B. Codeforces World Finals
1,700
implementation
The king Copa often has been reported about the Codeforces site, which is rapidly getting more and more popular among the brightest minds of the humanity, who are using it for training and competing. Recently Copa understood that to conquer the world he needs to organize the world Codeforces tournament. He hopes that a...
The first line contains the date DD.MM.YY, the second line contains the date BD.BM.BY. It is guaranteed that both dates are correct, and YY and BY are always in [01;99].It could be that by passport Bob was born after the finals. In this case, he can still change the order of numbers in date.
If it is possible to rearrange the numbers in the date of birth so that Bob will be at least 18 years old on the DD.MM.YY, output YES. In the other case, output NO. Each number contains exactly two digits and stands for day, month or year in a date. Note that it is permitted to rearrange only numbers, not digits.
Input: 01.01.9801.01.80 | Output: YES
Medium
1
1,700
292
314
0
1,798
A
1798A
A. Showstopper
800
greedy; implementation; sortings
You are given two arrays \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\).In one operation, you can choose any integer \(i\) from \(1\) to \(n\) and swap the numbers \(a_i\) and \(b_i\).Determine whether, after using any (possibly zero) number of operations, the following two conditions can be satisfied simultan...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 200\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the length of the arrays.The second line of each test case contains \(n...
For each test case, print ""Yes"" if after using any (possibly zero) number of operations the conditions described above are satisfied. Otherwise, print ""No"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses...
In the first test case, you can swap the numbers \(a_3\) and \(b_3\), after which the array \(a\) becomes equal to \([7, 9, 9]\), and the array \(b\) becomes equal to \([7, 6, 7]\), and both conditions are met.In the second test case, it can be proved that it is impossible to satisfy both conditions.In the third test c...
Input: 737 9 77 6 9410 10 15 1510 16 15 152100 9999 10011191 2 3 4 5 6 7 8 99 9 9 9 9 9 6 6 671 1 2 2 1 1 21 2 1 2 1 2 1230 45 30 | Output: Yes No Yes Yes Yes No No
Beginner
3
581
550
321
17
1,667
E
1667E
E. Centroid Probabilities
3,000
combinatorics; dp; fft; math
Consider every tree (connected undirected acyclic graph) with \(n\) vertices (\(n\) is odd, vertices numbered from \(1\) to \(n\)), and for each \(2 \le i \le n\) the \(i\)-th vertex is adjacent to exactly one vertex with a smaller index.For each \(i\) (\(1 \le i \le n\)) calculate the number of trees for which the \(i...
The first line contains an odd integer \(n\) (\(3 \le n < 2 \cdot 10^5\), \(n\) is odd) β€” the number of the vertices in the tree.
Print \(n\) integers in a single line, the \(i\)-th integer is the answer for the \(i\)-th vertex (modulo \(998\,244\,353\)).
Example \(1\): there are two possible trees: with edges \((1-2)\), and \((1-3)\) β€” here the centroid is \(1\); and with edges \((1-2)\), and \((2-3)\) β€” here the centroid is \(2\). So the answer is \(1, 1, 0\).Example \(2\): there are \(24\) possible trees, for example with edges \((1-2)\), \((2-3)\), \((3-4)\), and \(...
Input: 3 | Output: 1 1 0
Master
4
528
129
125
16
2,072
G
2072G
G. I've Been Flipping Numbers for 300 Years and Calculated the Sum
2,200
binary search; brute force; combinatorics; divide and conquer; math; number theory
After three hundred years of slime farming, Akito finally obtained the magical number \(n\). Upon reaching the merchant, he wanted to exchange the number for gold, but the merchant gave the hero a quest.The merchant said that for the quest, the skill \(\text{rev}(n, p)\) would be required, which Akito, by happy coincid...
The first line contains the number \(t\) (\(1 \le t \le 5000\)) β€” the number of test cases.In the only line of each test case, two numbers \(n\) and \(k\) are given (\(1 \le n \le 3 \cdot 10^5, 2 \le k \le 10^{18}\)) β€” the magical number and the upper limit for summation.Note that the sum of \(n\) across all test cases...
For each test case, you need to output a single number β€” the remainder of \(x = \sum\limits_{p = 2}^{k} \text{rev}(n, p)\) when divided by \(10^9 + 7\).
In the third test case, \(n = 1\). The number one in any numeral system is represented by a single digit, which means \(\text{rev}(1, p) = 1\) for any \(p \ge 2\). Thus, \(x = \sum\limits_{p = 2}^{k} 1 = \sum\limits_{p = 2}^{10} 1 = 10 - 2 + 1 = 9\).In the fourth test case, \(x = \text{rev}(4, 2) + \text{rev}(4, 3) + \...
Input: 123 242 521 104 416 269 699 319 849982 44353100000 100000000717 30777 1000000000000000000 | Output: 3 7594 9 6 1 33471 10 2006 120792461 584502117 775 46058362
Hard
6
1,156
336
152
20
1,106
C
1106C
C. Lunar New Year and Number Division
900
greedy; implementation; math; sortings
Lunar New Year is approaching, and Bob is struggling with his homework – a number division problem.There are \(n\) positive integers \(a_1, a_2, \ldots, a_n\) on Bob's homework paper, where \(n\) is always an even number. Bob is asked to divide those numbers into groups, where each group must contain at least \(2\) num...
The first line contains an even integer \(n\) (\(2 \leq n \leq 3 \cdot 10^5\)), denoting that there are \(n\) integers on Bob's homework paper.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^4\)), describing the numbers you need to deal with.
A single line containing one integer, denoting the minimum of the sum of the square of \(s_j\), which is $$$\(\sum_{i = j}^{m} s_j^2,\)\( where \)m$$$ is the number of groups.
In the first sample, one of the optimal solutions is to divide those \(4\) numbers into \(2\) groups \(\{2, 8\}, \{5, 3\}\). Thus the answer is \((2 + 8)^2 + (5 + 3)^2 = 164\).In the second sample, one of the optimal solutions is to divide those \(6\) numbers into \(3\) groups \(\{1, 2\}, \{1, 2\}, \{1, 2\}\). Thus the...
Input: 4 8 5 2 3 | Output: 164
Beginner
4
610
282
175
11
623
A
623A
A. Graph and String
1,800
constructive algorithms; graphs
One day student Vasya was sitting on a lecture and mentioned a string s1s2... sn, consisting of letters ""a"", ""b"" and ""c"" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties: G has exactly n vertices, numbered from 1 to...
The first line of the input contains two integers n and m β€” the number of vertices and edges in the graph found by Petya, respectively.Each of the next m lines contains two integers ui and vi (1 ≀ ui, vi ≀ n, ui β‰  vi) β€” the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of ve...
In the first line print ""Yes"" (without the quotes), if the string s Petya is interested in really exists and ""No"" (without the quotes) otherwise.If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters ""a"", ""b"" and ""c"" only, and...
In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings ""aa"", ""ab"", ""ba"", ""bb"", ""bc"", ""cb"", ""cc"" meets the graph's conditions. In the second sample the first vertex is co...
Input: 2 11 2 | Output: Yesaa
Medium
2
1,025
365
443
6
1,384
A
1384A
A. Common Prefixes
1,200
constructive algorithms; greedy; strings
The length of the longest common prefix of two strings \(s = s_1 s_2 \ldots s_n\) and \(t = t_1 t_2 \ldots t_m\) is defined as the maximum integer \(k\) (\(0 \le k \le min(n,m)\)) such that \(s_1 s_2 \ldots s_k\) equals \(t_1 t_2 \ldots t_k\).Koa the Koala initially has \(n+1\) strings \(s_1, s_2, \dots, s_{n+1}\).For ...
Each test contains multiple test cases. The first line contains \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the number of elements in the list \(a\).The second line of each test cas...
For each test case:Output \(n+1\) lines. In the \(i\)-th line print string \(s_i\) (\(1 \le |s_i| \le 200\)), consisting of lowercase Latin letters. Length of the longest common prefix of strings \(s_i\) and \(s_{i+1}\) has to be equal to \(a_i\).If there are many answers print any. We can show that answer always exist...
In the \(1\)-st test case one of the possible answers is \(s = [aeren, ari, arousal, around, ari]\).Lengths of longest common prefixes are: Between \(\color{red}{a}eren\) and \(\color{red}{a}ri\) \(\rightarrow 1\) Between \(\color{red}{ar}i\) and \(\color{red}{ar}ousal\) \(\rightarrow 2\) Between \(\color{red}{arou}sal...
Input: 4 4 1 2 4 2 2 5 3 3 1 3 1 3 0 0 0 | Output: aeren ari arousal around ari monogon monogamy monthly kevinvu kuroni kurioni korone anton loves adhoc problems
Easy
3
772
502
348
13
325
D
325D
D. Reclamation
2,900
dsu
In a far away land, there exists a planet shaped like a cylinder. There are three regions in this planet: top, bottom, and side as shown in the following picture. Both the top and the bottom areas consist of big cities. The side area consists entirely of the sea.One day, a city decides that it has too little space and ...
The first line consists of three integers r, c, and n (1 ≀ r, c ≀ 3000, 1 ≀ n ≀ 3Β·105). Then, n lines follow, describing the cells in the order you will reclamate them. Each line will consists of two integers: ri and ci (1 ≀ ri ≀ r, 1 ≀ ci ≀ c), which represents the cell located at row ri and column ci. All of the line...
You should output a single number representing the number of cells that were successfully turned to land.
The pictures below show the sequence of reclamations that are performed in the example input. Blue cells represent the cells occupied by sea, while other colored cells represent land. The latest cell that are reclamated is colored either yellow or red, depending on whether the addition violates the condition in the sta...
Input: 3 4 92 23 22 33 43 11 32 11 11 4 | Output: 6
Master
1
1,840
360
105
3
802
G
802G
G. Fake News (easy)
800
implementation; strings
As it's the first of April, Heidi is suspecting that the news she reads today are fake, and she does not want to look silly in front of all the contestants. She knows that a newspiece is fake if it contains heidi as a subsequence. Help Heidi assess whether the given piece is true, but please be discreet about it...
The first and only line of input contains a single nonempty string s of length at most 1000 composed of lowercase letters (a-z).
Output YES if the string s contains heidi as a subsequence and NO otherwise.
A string s contains another string p as a subsequence if it is possible to delete some characters from s and obtain p.
Input: abcheaibcdi | Output: YES
Beginner
2
316
128
76
8
1,666
C
1666C
C. Connect the Points
1,800
brute force; constructive algorithms; geometry
You are given three points on a plane. You should choose some segments on the plane that are parallel to coordinate axes, so that all three points become connected. The total length of the chosen segments should be the minimal possible.Two points \(a\) and \(b\) are considered connected if there is a sequence of points...
The input consists of three lines describing three points. Each line contains two integers \(x\) and \(y\) separated by a space β€” the coordinates of the point (\(-10^9 \le x, y \le 10^9\)). The points are pairwise distinct.
On the first line output \(n\) β€” the number of segments, at most 100.The next \(n\) lines should contain descriptions of segments. Output four integers \(x_1\), \(y_1\), \(x_2\), \(y_2\) on a line β€” the coordinates of the endpoints of the corresponding segment (\(-10^9 \le x_1, y_1, x_2, y_2 \le 10^9\)). Each segment s...
The points and the segments from the example are shown below.
Input: 1 1 3 5 8 6 | Output: 3 1 1 1 5 1 5 8 5 8 5 8 6
Medium
3
420
223
428
16
618
F
618F
F. Double Knapsack
3,000
constructive algorithms; two pointers
You are given two multisets A and B. Each multiset has exactly n integers each between 1 and n inclusive. Multisets may contain multiple copies of the same number.You would like to find a nonempty subset of A and a nonempty subset of B such that the sum of elements in these subsets are equal. Subsets are also multisets...
The first line of the input contains a single integer n (1 ≀ n ≀ 1 000 000) β€” the size of both multisets.The second line contains n integers, denoting the elements of A. Each element will be between 1 and n inclusive.The third line contains n integers, denoting the elements of B. Each element will be between 1 and n in...
If there is no solution, print a single integer - 1. Otherwise, your solution should be printed on four lines.The first line should contain a single integer ka, the size of the corresponding subset of A. The second line should contain ka distinct integers, the indices of the subset of A.The third line should contain a ...
Input: 1010 10 10 10 10 10 10 10 10 1010 9 8 7 6 5 4 3 2 1 | Output: 1235 8 10
Master
2
500
328
574
6
760
A
760A
A. Petr and a calendar
800
implementation; math
Petr wants to make a calendar for current month. For this purpose he draws a table in which columns correspond to weeks (a week is seven consequent days from Monday to Sunday), rows correspond to weekdays, and cells contain dates. For example, a calendar for January 2017 should look like on the picture: Petr wants to k...
The only line contain two integers m and d (1 ≀ m ≀ 12, 1 ≀ d ≀ 7) β€” the number of month (January is the first month, December is the twelfth) and the weekday of the first date of this month (1 is Monday, 7 is Sunday).
Print single integer: the number of columns the table should have.
The first example corresponds to the January 2017 shown on the picture in the statements.In the second example 1-st January is Monday, so the whole month fits into 5 columns.In the third example 1-st November is Saturday and 5 columns is enough.
Input: 1 7 | Output: 6
Beginner
2
461
218
66
7
141
C
141C
C. Queue
1,800
constructive algorithms; greedy; sortings
In the Main Berland Bank n people stand in a queue at the cashier, everyone knows his/her height hi, and the heights of the other people in the queue. Each of them keeps in mind number ai β€” how many people who are taller than him/her and stand in queue in front of him.After a while the cashier has a lunch break and the...
The first input line contains integer n β€” the number of people in the queue (1 ≀ n ≀ 3000). Then n lines contain descriptions of the people as ""namei ai"" (one description on one line), where namei is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 characters (the i-th person's...
If there's no acceptable order of the people in the queue, print the single line containing ""-1"" without the quotes. Otherwise, print in n lines the people as ""namei hi"", where hi is the integer from 1 to 109 (inclusive), the possible height of a man whose name is namei. Print the people in the order in which they ...
Input: 4a 0b 2c 0d 0 | Output: a 150c 170d 180b 160
Medium
3
841
506
438
1
417
D
417D
D. Cunning Gena
1,900
bitmasks; dp; greedy; sortings
A boy named Gena really wants to get to the ""Russian Code Cup"" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his n friends that they will solve the problems for him.The participants are offered m problems on the contest. For each friend, Gena knows what pr...
The first line contains three integers n, m and b (1 ≀ n ≀ 100; 1 ≀ m ≀ 20; 1 ≀ b ≀ 109) β€” the number of Gena's friends, the number of problems and the cost of a single monitor.The following 2n lines describe the friends. Lines number 2i and (2i + 1) contain the information about the i-th friend. The 2i-th line contain...
Print the minimum amount of money Gena needs to spend to solve all the problems. Or print -1, if this cannot be achieved.
Input: 2 2 1100 1 12100 2 11 | Output: 202
Hard
4
858
637
121
4
2,014
B
2014B
B. Robin Hood and the Major Oak
800
math
In Sherwood, the trees are our shelter, and we are all children of the forest.The Major Oak in Sherwood is known for its majestic foliage, which provided shelter to Robin Hood and his band of merry men and women.The Major Oak grows \(i^i\) new leaves in the \(i\)-th year. It starts with \(1\) leaf in year \(1\).Leaves ...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Each test case consists of two integers \(n\), \(k\) (\(1 \le n \le 10^9\), \(1 \le k \le n\)) β€” the requested year and the number of years during which the leaves remain.
For each test case, output one line, ""YES"" if in year \(n\) the Major Oak will have an even number of leaves and ""NO"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
In the first test case, there is only \(1\) leaf.In the second test case, \(k=1\), so in the \(2\)-nd year there will be \(2^2=4\) leaves.In the third test case, \(k=2\), so in the \(2\)-nd year there will be \(1+2^2=5\) leaves.In the fourth test case, \(k=2\), so in the \(3\)-rd year there will be \(2^2 + 3^3 = 4 + 27...
Input: 51 12 12 23 24 4 | Output: NO YES NO NO YES
Beginner
1
574
281
293
20
1,763
B
1763B
B. Incinerate
1,200
binary search; brute force; data structures; implementation; math; sortings
To destroy humanity, The Monster Association sent \(n\) monsters to Earth's surface. The \(i\)-th monster has health \(h_i\) and power \(p_i\).With his last resort attack, True Spiral Incineration Cannon, Genos can deal \(k\) damage to all monsters alive. In other words, Genos can reduce the health of all monsters by \...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains two integers, \(n\) and \(k\) (\(1 \le n, k \le 10^5\)) β€” the number of monsters and Genos' initial attack damage. Then two lines ...
For each test case, print the answer β€” ""YES"" (without quotes) if Genos could kill all monsters and ""NO"" otherwise.
In the first example, after Genos' first attack, \(h\) and \(k\) will update to: \(h: [11,0,6,2,3,0]\) \(k: 7-1 = 6\) After second attack: \(h: [5,0,0,0,0,0]\) \(k: 6-2 = 4\) After third attack: \(h: [1,0,0,0,0,0]\) \(k: 4-2 = 2\) After fourth attack: \(h: [0,0,0,0,0,0]\) As Genos could kill all monsters, the answer is...
Input: 36 718 5 13 9 10 12 7 2 1 2 63 45 5 54 4 43 22 1 31 1 1 | Output: YES NO YES
Easy
6
877
518
118
17
1,795
E
1795E
E. Explosions?
2,200
binary search; data structures; dp; greedy; math
You are playing yet another game where you kill monsters using magic spells. There are \(n\) cells in the row, numbered from \(1\) to \(n\). Initially, the \(i\)-th cell contains the \(i\)-th monster with \(h_i\) health.You have a basic spell that costs \(1\) MP and deals \(1\) damage to the monster you choose. You can...
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains the single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β€” the number of cells in the row, i. e. the number of monsters.The second line of each test case contains \(n\) integers \(h_1, h_...
For each test case, print one integer β€” the minimum total MP you need to kill all monsters by finishing them with explosion.
In the first test case, you can, for example, use basic spell on monsters \(1\) and \(2\) (once per monster) to kill them. After that, you cast ""Explosion"" of power \(x = 1\) on monster \(3\) to kill it. The total MP you need is \(2 + 1 = 3\).In the second test case, it's optimal to cast basic spell \(4\) times onto ...
Input: 531 1 144 1 2 145 10 15 1014291 2 3 2 2 2 3 2 1 | Output: 3 6 15 42 12
Hard
5
2,102
488
124
17
1,152
F1
1152F1
F1. Neko Rules the Catniverse (Small Version)
2,800
bitmasks; dp; matrices
This problem is same as the next one, but has smaller constraints.Aki is playing a new video game. In the video game, he will control Neko, the giant cat, to fly between planets in the Catniverse.There are \(n\) planets in the Catniverse, numbered from \(1\) to \(n\). At the beginning of the game, Aki chooses the plane...
The only line contains three integers \(n\), \(k\) and \(m\) (\(1 \le n \le 10^5\), \(1 \le k \le \min(n, 12)\), \(1 \le m \le 4\)) β€” the number of planets in the Catniverse, the number of planets Neko needs to visit and the said constant \(m\).
Print exactly one integer β€” the number of different ways Neko can visit exactly \(k\) planets. Since the answer can be quite large, print it modulo \(10^9 + 7\).
In the first example, there are \(4\) ways Neko can visit all the planets: \(1 \rightarrow 2 \rightarrow 3\) \(2 \rightarrow 3 \rightarrow 1\) \(3 \rightarrow 1 \rightarrow 2\) \(3 \rightarrow 2 \rightarrow 1\) In the second example, there are \(9\) ways Neko can visit exactly \(2\) planets: \(1 \rightarrow 2\) \(2 \ri...
Input: 3 3 1 | Output: 4
Master
3
990
245
161
11
1,619
F
1619F
F. Let's Play the Hat?
2,000
brute force; constructive algorithms; greedy; math
The Hat is a game of speedy explanation/guessing words (similar to Alias). It's fun. Try it! In this problem, we are talking about a variant of the game when the players are sitting at the table and everyone plays individually (i.e. not teams, but individual gamers play).\(n\) people gathered in a room with \(m\) table...
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases in the test.Each test case consists of one line that contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 2\cdot10^5\), \(1 \le m \le \lfloor\frac{n}{2}\rfloor\), \(1 \le k \le 10^5\)) β€” the number of people...
For each test case print a required schedule β€” a sequence of \(k\) blocks of \(m\) lines. Each block corresponds to one game, a line in a block corresponds to one table. In each line print the number of players at the table and the indices of the players (numbers from \(1\) to \(n\)) who should play at this table.If th...
Input: 3 5 2 2 8 3 1 2 1 3 | Output: 3 1 2 3 2 4 5 3 4 5 2 2 1 3 2 6 2 3 3 5 1 3 4 7 8 2 2 1 2 2 1 2 2 1
Hard
4
2,246
472
517
16
1,898
B
1898B
B. Milena and Admirer
1,500
greedy; math
Milena has received an array of integers \(a_1, a_2, \ldots, a_n\) of length \(n\) from a secret admirer. She thinks that making it non-decreasing should help her identify the secret admirer.She can use the following operation to make this array non-decreasing: Select an element \(a_i\) of array \(a\) and an integer \(...
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 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 array \(a\).The second line of each test ca...
For each test case, output one integer β€” the minimum number of operations required to make the array non-decreasing.It can be shown that it is always possible to make the array \(a\) non-decreasing in the finite number of operations.
In the first test case, Milena can replace the second element of array \(a\) by integers \(1\) and \(2\), so the array would become \([\, 1, \, \underline{1}, \, \underline{2}, \, 2 \,]\). Only \(1\) operation is required.In the second test case, the array \(a\) is already non-decreasing, so the answer is \(0\).In the ...
Input: 431 3 241 2 3 433 2 171 4 4 3 5 7 6 | Output: 1 0 3 9
Medium
2
1,114
507
233
18
1,370
A
1370A
A. Maximum GCD
800
greedy; implementation; math; number theory
Let's consider all integers in the range from \(1\) to \(n\) (inclusive).Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of \(\mathrm{gcd}(a, b)\), where \(1 \leq a < b \leq n\).The greatest common divisor, \(\ma...
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer \(n\) (\(2 \leq n \leq 10^6\)).
For each test case, output the maximum value of \(\mathrm{gcd}(a, b)\) among all \(1 \leq a < b \leq n\).
In the first test case, \(\mathrm{gcd}(1, 2) = \mathrm{gcd}(2, 3) = \mathrm{gcd}(1, 3) = 1\).In the second test case, \(2\) is the maximum possible value, corresponding to \(\mathrm{gcd}(2, 4)\).
Input: 2 3 5 | Output: 1 2
Beginner
4
445
230
105
13
1,331
F
1331F
F. Elementary!
0
*special; brute force; dp; strings
The input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive.
Output ""YES"" or ""NO"".
Input: GENIUS | Output: YES
Beginner
4
0
131
25
13
1,329
D
1329D
D. Dreamoon Likes Strings
3,100
constructive algorithms; data structures
Dreamoon likes strings. Today he created a game about strings:String \(s_1, s_2, \ldots, s_n\) is beautiful if and only if for each \(1 \le i < n, s_i \ne s_{i+1}\).Initially, Dreamoon has a string \(a\). In each step Dreamoon can choose a beautiful substring of \(a\) and remove it. Then he should concatenate the remai...
The first line contains an integer \(t\) (\(1 \leq t \leq 200\,000\)), denoting the number of test cases in the input.For each test case, there's one line with a non-empty string of lowercase Latin letters \(a\).The total sum of lengths of strings in all test cases is at most \(200\,000\).
For each test case, in the first line, you should print \(m\): the smallest number of steps to make \(a\) empty. Each of the following \(m\) lines should contain two integers \(l_i, r_i\) (\(1 \leq l_i \leq r_i \leq |a|\)), denoting, that the \(i\)-th step is removing the characters from index \(l_i\) to \(r_i\) in the...
Input: 4 aabbcc aaabbb aaa abacad | Output: 3 3 3 2 4 1 2 3 3 4 2 3 1 2 3 1 1 1 1 1 1 1 1 6
Master
2
525
290
589
13
1,989
A
1989A
A. Catch the Coin
800
implementation
Monocarp visited a retro arcade club with arcade cabinets. There got curious about the ""Catch the Coin"" cabinet.The game is pretty simple. The screen represents a coordinate grid such that: the X-axis is directed from left to right; the Y-axis is directed from bottom to top; the center of the screen has coordinates \...
The first line contains a single integer \(n\) (\(1 \le n \le 500\)) β€” the number of coins.In the \(i\)-th of the next \(n\) lines, two integers \(x_i\) and \(y_i\) (\(-50 \le x_i, y_i \le 50\)) are written β€” the coordinates of the \(i\)-th coin. The coordinates of all coins are different. No coin is located at \((0, 0...
For each coin, print ""YES"" if Monocarp can collect it. Otherwise, print ""NO"".
Pay attention to the second coin in the example. Monocarp can first move from \((0, 0)\) to \((-1, -1)\). Then the coin falls \(1\) down and ends up at \((-2, -2)\). Finally, Monocarp moves to \((-2, -2)\) and collects the coin.
Input: 524 42-2 -1-1 -20 -5015 0 | Output: YES YES NO NO YES
Beginner
1
1,287
324
81
19
1,581
A
1581A
A. CQXYM Count Permutations
800
combinatorics; math; number theory
CQXYM is counting permutations length of \(2n\).A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\...
The input consists of multiple test cases. The first line contains an integer \(t (t \geq 1)\) β€” the number of test cases. The description of the test cases follows.Only one line of each test case contains an integer \(n(1 \leq n \leq 10^5)\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(...
For each test case, print the answer in a single line.
\(n=1\), there is only one permutation that satisfies the condition: \([1,2].\)In permutation \([1,2]\), \(p_1<p_2\), and there is one \(i=1\) satisfy the condition. Since \(1 \geq n\), this permutation should be counted. In permutation \([2,1]\), \(p_1>p_2\). Because \(0<n\), this permutation should not be counted.\(n...
Input: 4 1 2 9 91234 | Output: 1 12 830455698 890287984
Beginner
3
1,032
326
54
15
1,772
B
1772B
B. Matrix Rotation
800
brute force; implementation
You have a matrix \(2 \times 2\) filled with distinct integers. You want your matrix to become beautiful. The matrix is beautiful if the following two conditions are satisfied: in each row, the first element is smaller than the second element; in each column, the first element is smaller than the second element. You ca...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Each test case consists of two lines. Each of those lines contains two integers β€” the elements of the corresponding row of the matrix. In each matrix, all four elements are distinct integers from \(1\) to \(100\).
For each test case, print YES if the matrix can become beautiful, or NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
Input: 61 35 78 103 48 104 36 19 27 54 21 24 3 | Output: YES YES NO YES YES NO
Beginner
2
641
305
236
17
557
A
557A
A. Ilya and Diplomas
1,100
greedy; implementation; math
Soon a school Olympiad in Informatics will be held in Berland, n schoolchildren will participate there.At a meeting of the jury of the Olympiad it was decided that each of the n participants, depending on the results, will get a diploma of the first, second or third degree. Thus, each student will receive exactly one d...
The first line of the input contains a single integer n (3 ≀ n ≀ 3Β·106) β€” the number of schoolchildren who will participate in the Olympiad.The next line of the input contains two integers min1 and max1 (1 ≀ min1 ≀ max1 ≀ 106) β€” the minimum and maximum limits on the number of diplomas of the first degree that can be di...
In the first line of the output print three numbers, showing how many diplomas of the first, second and third degree will be given to students in the optimal variant of distributing diplomas.The optimal variant of distributing diplomas is the one that maximizes the number of students who receive diplomas of the first d...
Input: 61 52 63 7 | Output: 1 2 3
Easy
3
1,505
780
587
5
1,556
A
1556A
A. A Variety of Operations
800
math
William has two numbers \(a\) and \(b\) initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer \(k\) is picked, which is then used to perform one of the following operations: (note, that for each operation you can ch...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The only line of each test case contains two integers \(c\) and \(d\) \((0 \le c, d \le 10^9)\), which are William's favorite numbers and which he wants \(a\) and ...
For each test case output a single number, which is the minimal number of operations which William would have to perform to make \(a\) equal to \(c\) and \(b\) equal to \(d\), or \(-1\) if it is impossible to achieve this using the described operations.
Let us demonstrate one of the suboptimal ways of getting a pair \((3, 5)\): Using an operation of the first type with \(k=1\), the current pair would be equal to \((1, 1)\). Using an operation of the third type with \(k=8\), the current pair would be equal to \((-7, 9)\). Using an operation of the second type with \(k=...
Input: 6 1 2 3 5 5 3 6 6 8 0 0 0 | Output: -1 2 2 1 2 0
Beginner
1
793
349
253
15
1,059
D
1059D
D. Nature Reserve
2,200
binary search; geometry; ternary search
There is a forest that we model as a plane and live \(n\) rare animals. Animal number \(i\) has its lair in the point \((x_{i}, y_{i})\). In order to protect them, a decision to build a nature reserve has been made.The reserve must have a form of a circle containing all lairs. There is also a straight river flowing thr...
The first line contains one integer \(n\) (\(1 \le n \le 10^5\)) β€” the number of animals. Each of the next \(n\) lines contains two integers \(x_{i}\), \(y_{i}\) (\(-10^7 \le x_{i}, y_{i} \le 10^7\)) β€” the coordinates of the \(i\)-th animal's lair. It is guaranteed that \(y_{i} \neq 0\). No two lairs coincide.
If the reserve cannot be built, print \(-1\). Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is considered correct if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}\)...
In the first sample it is optimal to build the reserve with the radius equal to \(0.5\) and the center in \((0,\ 0.5)\).In the second sample it is impossible to build a reserve.In the third sample it is optimal to build the reserve with the radius equal to \(\frac{5}{8}\) and the center in \((\frac{1}{2},\ \frac{5}{8})...
Input: 10 1 | Output: 0.5
Hard
3
798
311
321
10
987
A
987A
A. Infinity Gauntlet
800
implementation
You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: the Power Gem of purple color, the Time Gem of green color, the Space Gem of blue color, the Soul Gem of orange color, the Reality Gem of red color, the Mind Gem of yellow color. Using colors of Gems you saw in ...
In the first line of input there is one integer \(n\) (\(0 \le n \le 6\)) β€” the number of Gems in Infinity Gauntlet.In next \(n\) lines there are colors of Gems you saw. Words used for colors are: purple, green, blue, orange, red, yellow. It is guaranteed that all the colors are distinct. All colors are given in lowerc...
In the first line output one integer \(m\) (\(0 \le m \le 6\)) β€” the number of absent Gems.Then in \(m\) lines print the names of absent Gems, each on its own line. Words used for names are: Power, Time, Space, Soul, Reality, Mind. Names can be printed in any order. Keep the first letter uppercase, others lowercase.
In the first sample Thanos already has Reality, Power, Mind and Soul Gems, so he needs two more: Time and Space.In the second sample Thanos doesn't have any Gems, so he needs all six.
Input: 4redpurpleyelloworange | Output: 2SpaceTime
Beginner
1
368
340
317
9
1,639
K
1639K
K. 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
1,109
B
1109B
B. Sasha and One More Name
1,800
constructive algorithms; hashing; strings
Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: ""Many are my names in many countries. Mithrandir among the Elves, TharkΓ»n to the Dwarves, OlΓ³rin I was in my youth in the West that is forgotten, in ...
The first line contains one string \(s\) (\(1 \le |s| \le 5\,000\)) β€” the initial name, which consists only of lowercase Latin letters. It is guaranteed that \(s\) is a palindrome.
Print one integer \(k\) β€” the minimum number of cuts needed to get a new name, or ""Impossible"" (without quotes).
In the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.In the third example, you can't make a string, that...
Input: nolon | Output: 2
Medium
3
1,683
180
114
11
2,075
D
2075D
D. Equalization
2,000
bitmasks; brute force; dp; graphs; math
You are given two non-negative integers \(x\) and \(y\).You can perform the following operation any number of times (possibly zero): choose a positive integer \(k\) and divide either \(x\) or \(y\) by \(2^k\) rounding down. The cost of this operation is \(2^k\). However, there is an additional constraint: you cannot se...
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases.The only line of each test case contains two integers \(x\) and \(y\) (\(0 \le x, y \le 10^{17}\)).
For each test case, print a single integer β€” the minimum possible cost in order to make \(x\) equal to \(y\).
In the first example, you can proceed as follows: choose \(k=1\) and divide \(y\) by \(2\). After that, \(x\) and \(y\) are equal to \(0\).In the second example, you can proceed as follows: choose \(k=2\) and divide \(x\) by \(4\); choose \(k=1\) and divide \(y\) by \(2\). After that, \(x\) and \(y\) are equal to \(1\)...
Input: 50 16 23 313 374238659325782394 12983091057341925 | Output: 2 6 0 26 32764
Hard
5
454
196
109
20
1,948
E
1948E
E. Clique Partition
2,100
brute force; constructive algorithms; graphs; greedy; implementation
You are given two integers, \(n\) and \(k\). There is a graph on \(n\) vertices, numbered from \(1\) to \(n\), which initially has no edges.You have to assign each vertex an integer; let \(a_i\) be the integer on the vertex \(i\). All \(a_i\) should be distinct integers from \(1\) to \(n\).After assigning integers, for...
The first line contains one integer \(t\) (\(1 \le t \le 1600\)) β€” the number of test cases.Each test case consists of one line containing two integers \(n\) and \(k\) (\(2 \le n \le 40\); \(1 \le k \le 2n\)).
For each test case, print three lines: the first line should contain \(n\) distinct integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β€” the values you assign to the vertices; the second line should contain one integer \(q\) (\(1 \le q \le n\)) β€” the number of cliques you partition the graph into; the third line s...
Input: 32 35 48 16 | Output: 2 1 1 1 1 3 1 5 2 4 2 1 1 2 1 2 1 2 3 4 5 6 7 8 1 1 1 1 1 1 1 1 1
Hard
5
941
209
564
19
1,083
F
1083F
F. The Fair Nut and Amusing Xor
3,300
data structures
The Fair Nut has two arrays \(a\) and \(b\), consisting of \(n\) numbers. He found them so long ago that no one knows when they came to him.The Fair Nut often changes numbers in his arrays. He also is interested in how similar \(a\) and \(b\) are after every modification.Let's denote similarity of two arrays as the min...
The first line contains three numbers \(n\), \(k\) and \(q\) (\(1 \le k \le n \le 2 \cdot 10^5\), \(0 \le q \le 2 \cdot 10^5\)) β€” the length of the arrays, the length of the subarrays, to which the operations are applied, and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \...
On the first line print initial similarity of arrays \(a\) and \(b\).On the \(i\)-th of following \(q\) lines print similarity of \(a\) and \(b\) after applying first \(i\) modifications.
In the first sample making arrays \([0, 4, 2]\) and \([1, 2, 3]\) is impossible with \(k=3\). After the modification, you can apply the operation with \(x=1\) to the whole first array (its length is equal to \(k\)), and it will be equal to the second array.In order to make arrays equal in the second sample before chang...
Input: 3 3 10 4 21 2 3b 2 5 | Output: -11
Master
1
921
734
187
10
215
C
215C
C. Crosses
2,100
brute force; implementation
There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number i and column number j as (i, j).A group of six numbers (a, b, c, d, x0, y0), where 0 ≀ a, ...
The input consists of a single line containing three integers n, m and s (1 ≀ n, m ≀ 500, 1 ≀ s ≀ nΒ·m). The integers are separated by a space.
Print a single integer β€” the number of distinct groups of six integers that denote crosses with area s and that are fully placed on the n Γ— m grid.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 sample the sought groups of six numbers are: (0, 0, 0, 0, 1, 1), (0, 0, 0, 0, 1, 2), (0, 0, 0, 0, 2, 1), (0, 0, 0, 0, 2, 2).In the second sample the sought groups of six numbers are: (0, 1, 1, 0, 2, 2), (0, 1, 1, 0, 2, 3), (1, 0, 0, 1, 2, 2), (1, 0, 0, 1, 2, 3).
Input: 2 2 1 | Output: 4
Hard
2
1,156
142
294
2
1,814
F
1814F
F. Communication Towers
2,700
brute force; divide and conquer; dsu
There are \(n\) communication towers, numbered from \(1\) to \(n\), and \(m\) bidirectional wires between them. Each tower has a certain set of frequencies that it accepts, the \(i\)-th of them accepts frequencies from \(l_i\) to \(r_i\).Let's say that a tower \(b\) is accessible from a tower \(a\), if there exists a f...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(0 \le m \le 4 \cdot 10^5\)) β€” the number of communication towers and the number of wires, respectively.Then \(n\) lines follows, the \(i\)-th of them contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le 2 \cdot 10^5\)) β€”...
In a single line, print distinct integers from \(1\) to \(n\) in ascending order β€” the indices of the communication towers that are accessible from the \(1\)-st tower.
Input: 6 5 3 5 1 2 2 4 2 3 3 3 4 6 1 3 6 1 3 5 3 6 2 3 | Output: 1 3 5 6
Master
3
750
642
167
18
626
G
626G
G. Raffles
3,100
data structures; dp; greedy; math
Johnny is at a carnival which has n raffles. Raffle i has a prize with value pi. Each participant can put tickets in whichever raffles they choose (they may have more than one ticket in a single raffle). At the end of the carnival, one ticket is selected at random from each raffle, and the owner of the ticket wins the ...
The first line contains two integers n, t, and q (1 ≀ n, t, q ≀ 200 000) β€” the number of raffles, the number of tickets Johnny has, and the total number of updates, respectively.The second line contains n space-separated integers pi (1 ≀ pi ≀ 1000) β€” the value of the i-th prize.The third line contains n space-separated...
Print q lines, each containing a single real number β€” the maximum expected value of Johnny's winnings after the k-th update. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program w...
In the first case, Johnny only has one ticket to distribute. The prizes are worth 4 and 5, and the raffles initially have 1 and 2 tickets, respectively. After the first update, each raffle has 2 tickets, so Johnny has expected value of winning by placing his ticket into the second raffle. The second update adds a ticke...
Input: 2 1 34 51 21 11 22 1 | Output: 1.6666666671.3333333332.000000000
Master
4
1,332
851
358
6
24
E
24E
E. Berland collider
2,300
binary search
Recently the construction of Berland collider has been completed. Collider can be represented as a long narrow tunnel that contains n particles. We associate with collider 1-dimensional coordinate system, going from left to right. For each particle we know its coordinate and velocity at the moment of start of the colli...
The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” amount of particles in the collider. Next n lines contain description of particles. Each particle is described by two integers xi, vi ( - 109 ≀ xi, vi ≀ 109, vi β‰  0) β€” coordinate and velocity respectively. All the coordinates are distinct. The particles are lis...
If there will be no big bang, output -1. Otherwise output one number β€” how much time in seconds elapses after the launch of the collider before the big bang happens. Your answer must have a relative or absolute error less than 10 - 9.
Input: 3-5 90 15 -1 | Output: 1.00000000000000000000
Expert
1
642
602
234
0
1,108
E1
1108E1
E1. Array and Segments (Easy version)
1,800
brute force; greedy; implementation
The only difference between easy and hard versions is a number of elements in the array.You are given an array \(a\) consisting of \(n\) integers. The value of the \(i\)-th element of the array is \(a_i\).You are also given a set of \(m\) segments. The \(j\)-th segment is \([l_j; r_j]\), where \(1 \le l_j \le r_j \le n...
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n \le 300, 0 \le m \le 300\)) β€” the length of the array \(a\) and the number of segments, respectively.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^6 \le a_i \le 10^6\)), where \(a_i\) is the value of the ...
In the first line of the output print one integer \(d\) β€” the maximum possible value \(\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i\) if \(b\) is the array obtained by applying some subset of the given segments to the array \(a\).In the second line of the output print one integer \(q\) (\(0 \le q \le m\)) β€” the ...
In the first example the obtained array \(b\) will be \([0, -4, 1, 1, 2]\) so the answer is \(6\).In the second example the obtained array \(b\) will be \([2, -3, 1, -1, 4]\) so the answer is \(7\).In the third example you cannot do anything so the answer is \(0\).
Input: 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 | Output: 6 2 1 4
Medium
3
1,127
565
690
11
2,025
G
2025G
G. Variable Damage
3,000
data structures; flows
Monocarp is gathering an army to fight a dragon in a videogame.The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameter β€” his health. Each defensive artifact also has one parameter β€” its durability.Before the battle begins, Monocarp distributes artifacts to the heroes so that ...
The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β€” the number of queries.In the \(i\)-th of the following \(q\) lines, there are two integers \(t_i\) and \(v_i\) (\(t_i \in \{1, 2\}\); \(1 \le v_i \le 10^9\)) β€” the type of the query and the value of the query parameter. If the type is \(1\), a h...
Print \(q\) integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
Let's consider the first example. An artifact with durability \(5\) is added. Since there are no heroes yet, the battle ends immediately. A hero with health \(4\) is added. Monocarp can give him an artifact with durability \(5\). First, there are rounds in which the hero takes \(\frac{1}{1 + 1} = \frac{1}{2}\) damage. ...
Input: 32 51 41 10 | Output: 0 8 19
Master
2
1,292
421
144
20
2,129
F1
2129F1
F1. Top-K Tracker (Easy Version)
3,500
interactive
This is an interactive problem.This is the easy version of the problem. The only difference is that \(n \le 845\) in this version. You can make hacks only if all versions of the problem are solved.There is a hidden permutation \(p\), which is a permutation of \(\{1,2,\ldots,n\}\). Let \(pos_i\) denote the position of t...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 40\)). The description of the test cases follows.
In the first test case, the hidden permutation is \(p = [3, 1, 2]\), thus \(pos = [2, 3, 1]\).For the query ""3 2 3 1"", the jury returns \(2\) and \(3\) because \(2\) and \(3\) are the top \(k\) largest number(s) in \([p_3, p_1]\), where \(k = \min(300, m) = \min(300, 2) = 2\).For the query ""2 1 2"", the jury returns...
Input: 2 3 2 3 3 2 2 | Output: 3 2 3 1 2 1 2 ! 3 1 2 4 1 1 ! 2 1
Master
1
1,607
159
0
21
683
J
683J
J. The Hero with Bombs
3,000
*special
In a new computer game you need to help the hero to get out of the maze, which is a rectangular field of size n Γ— m. The hero is located in one of the cells of this field. He knows where the exit of the maze is, and he wants to reach it.In one move, the hero can either move to the next cell (i.e. the cell which has a c...
The first line contains two integers n and m (1 ≀ n, m ≀ 100, nΒ·m > 1) β€” sizes of the maze.Each of the following n lines contains m characters β€” description of the maze. The character ""."" means a free cell, ""E"" β€” the hero, ""T"" β€” the exit, ""X"" β€” the obstacle.It is guaranteed that there is exactly one hero and ex...
Print the hero's actions which will help him get out of the maze (""M"" β€” to plant a bomb, ""T"" β€” to skip the move, ""S"" β€” to go down, ""W"" β€” to go left, ""N"" β€” to go up, ""E"" β€” to go right). If the hero can not reach the exit, print ""No solution"" (without quotes).The length of the resulting sequence should not ...
Input: 3 5XEX.XX.XXTX.X.X | Output: SSMNNTSSNEMWWTEEEE
Master
1
1,183
347
410
6
2,126
B
2126B
B. No Casino in the Mountains
800
dp; greedy
You are given an array \(a\) of \(n\) numbers and a number \(k\). The value \(a_i\) describes the weather on the \(i\)-th day: if it rains on the \(i\)-th day, then \(a_i = 1\); otherwise, if the weather is good on the \(i\)-th day, then \(a_i = 0\).Jean wants to visit as many peaks as possible. One hike to a peak take...
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(1 \le k \le n\)). The second line contain...
For each test case, output a single integer: the maximum number of hikes that Jean can make.
In the first sample: Day \(1\) β€” good weather, Jean goes on a hike. (\(a_1 = 0\)) Day \(2\) β€” mandatory break. Day \(3\) β€” again good weather, Jean goes on the second hike. (\(a_3 = 0\)) Day \(4\) β€” break. Day \(5\) β€” good weather, third hike. (\(a_5 = 0\)) Thus, Jean can make 3 hikes, alternating each with a mandatory...
Input: 55 10 1 0 0 07 30 0 0 0 0 0 03 11 1 14 20 1 0 16 20 0 1 0 0 0 | Output: 3 2 0 0 2
Beginner
2
748
516
92
21
79
E
79E
E. Security System
2,900
math
Fox Ciel safely returned to her castle, but there was something wrong with the security system of the castle: sensors attached in the castle were covering her.Ciel is at point (1, 1) of the castle now, and wants to move to point (n, n), which is the position of her room. By one step, Ciel can move from point (x, y) to ...
In the first line there are five integers n, t, a, b, c (2 ≀ n ≀ 2Β·105, 0 ≀ t ≀ 1014, 1 ≀ a ≀ n - c + 1, 1 ≀ b ≀ n - c + 1, 1 ≀ c ≀ n).Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin stream (also you may use the %I64d specificator).
If Ciel's objective is possible, output in first line 2n - 2 characters that represent her feasible steps, where i-th character is R if i-th step is moving rightward, or U if moving upward. If there are several solution, output lexicographically first one. Character R is lexicographically earlier than the character U.I...
The answers for the first sample and the second sample are shown on the picture: Here, a red point represents a point that contains a sensor.
Input: 5 25 2 4 1 | Output: RRUURURU
Master
1
1,069
296
369
0
1,520
C
1520C
C. Not Adjacent Matrix
1,000
constructive algorithms
We will consider the numbers \(a\) and \(b\) as adjacent if they differ by exactly one, that is, \(|a-b|=1\).We will consider cells of a square matrix \(n \times n\) as adjacent if they have a common side, that is, for cell \((r, c)\) cells \((r, c-1)\), \((r, c+1)\), \((r-1, c)\) and \((r+1, c)\) are adjacent to it.Fo...
The first line contains one integer \(t\) (\(1 \le t \le 100\)). Then \(t\) test cases follow.Each test case is characterized by one integer \(n\) (\(1 \le n \le 100\)).
For each test case, output: -1, if the required matrix does not exist; the required matrix, otherwise (any such matrix if many of them exist). The matrix should be outputted as \(n\) lines, where each line contains \(n\) integers.
Input: 3 1 2 3 | Output: 1 -1 2 9 7 4 6 3 1 8 5
Beginner
1
578
169
230
15
678
B
678B
B. The Same Calendar
1,600
implementation
The girl Taylor has a beautiful calendar for the year y. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.The calendar is so beautiful that she wants to know what is the next year after y when the calendar will be exactly the same. Help Taylor...
The only line contains integer y (1000 ≀ y < 100'000) β€” the year of the calendar.
Print the only integer y' β€” the next year after y when the calendar will be the same. Note that you should find the first year after y with the same calendar.
Today is Monday, the 13th of June, 2016.
Input: 2016 | Output: 2044
Medium
1
499
81
158
6
190
A
190A
A. Vasya and the Bus
1,100
greedy; math
One day Vasya heard a story: ""In the city of High Bertown a bus number 62 left from the bus station. It had n grown-ups and m kids...""The latter events happen to be of no importance to us. Vasya is an accountant and he loves counting money. So he wondered what maximum and minimum sum of money these passengers could h...
The input file consists of a single line containing two space-separated numbers n and m (0 ≀ n, m ≀ 105) β€” the number of the grown-ups and the number of the children in the bus, correspondingly.
If n grown-ups and m children could have ridden in the bus, then print on a single line two space-separated integers β€” the minimum and the maximum possible total bus fare, correspondingly. Otherwise, print ""Impossible"" (without the quotes).
In the first sample a grown-up rides with two children and pays two rubles.In the second sample there are only children in the bus, so the situation is impossible. In the third sample there are two cases: Each of the two grown-ups rides with one children and pays one ruble for the tickets. In this case the passengers p...
Input: 1 2 | Output: 2 2
Easy
2
955
194
242
1
950
B
950B
B. Intercepted Message
1,100
greedy; implementation
Hacker Zhorik wants to decipher two secret messages he intercepted yesterday. Yeah message is a sequence of encrypted blocks, each of them consists of several bytes of information.Zhorik knows that each of the messages is an archive containing one or more files. Zhorik knows how each of these archives was transferred t...
The first line contains two integers n, m (1 ≀ n, m ≀ 105) β€” the number of blocks in the first and in the second messages.The second line contains n integers x1, x2, ..., xn (1 ≀ xi ≀ 106) β€” the length of the blocks that form the first message.The third line contains m integers y1, y2, ..., ym (1 ≀ yi ≀ 106) β€” the leng...
Print the maximum number of files the intercepted array could consist of.
In the first example the maximum number of files in the archive is 3. For example, it is possible that in the archive are three files of sizes 2 + 5 = 7, 15 = 3 + 1 + 11 = 8 + 2 + 4 + 1 and 4 + 4 = 8.In the second example it is possible that the archive contains two files of sizes 1 and 110 = 10 + 100 = 100 + 10. Note ...
Input: 7 62 5 3 1 11 4 47 8 2 4 1 8 | Output: 3
Easy
2
1,067
467
73
9
1,788
B
1788B
B. Sum of Two Numbers
1,100
constructive algorithms; greedy; implementation; math; probabilities
The sum of digits of a non-negative integer \(a\) is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of \(123\) is \(6\) and the sum of digits of \(10\) is \(1\). In a formal way, the sum of digits of \(\displaystyle a=\sum_{i=0}^{\infty} a_i \cdot 10^i\),...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10\,000\)). Each test case consists of a single integer \(n\) (\(1 \leq n \leq 10^9\))
For each test case, print two integers \(x\) and \(y\).If there are multiple answers, print any.
In the second test case, the sum of digits of \(67\) and the sum of digits of \(94\) are both \(13\).In the third test case, the sum of digits of \(60\) is \(6\), and the sum of digits of \(7\) is \(7\).
Input: 5 1 161 67 1206 19 | Output: 1 0 67 94 60 7 1138 68 14 5
Easy
5
670
196
96
17
2,036
G
2036G
G. Library of Magic
2,200
binary search; constructive algorithms; divide and conquer; interactive; math; number theory
This is an interactive problem.The Department of Supernatural Phenomena at the Oxenfurt Academy has opened the Library of Magic, which contains the works of the greatest sorcerers of Redania β€” \(n\) (\(3 \leq n \leq 10^{18}\)) types of books, numbered from \(1\) to \(n\). Each book's type number is indicated on its spi...
The first line of input contains an integer \(t\) (\(1 \le t \le 300\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^{18}\)) β€” the number of types of tomes.
In the first test case, the books in the library after the theft look like this:Now consider the answers to the queries: For the query ""xor 1 1"", you receive the result \(1 \oplus 1 = 0\). Two tomes satisfy the condition specified in the query β€” both with the number \(1\). For the query ""xor 2 2"", you receive the r...
Input: 2 6 0 2 3 5 3 | Output: xor 1 1 xor 2 2 xor 3 3 xor 4 6 ans 2 3 5 ans 1 2 3
Hard
6
1,774
223
0
20
1,728
C
1728C
C. Digital Logarithm
1,400
data structures; greedy; sortings
Let's define \(f(x)\) for a positive integer \(x\) as the length of the base-10 representation of \(x\) without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that.You are given two arrays \(a\) and \(b\), each containing \(n\) positive integers. In one operati...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of testcases.The first line of the testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of elements in each of the arrays.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i < 10^...
For each testcase, print the smallest number of operations required to make \(a\) and \(b\) similar to each other.
In the first testcase, you can apply the digital logarithm to \(b_1\) twice.In the second testcase, the arrays are already similar to each other.In the third testcase, you can first apply the digital logarithm to \(a_1\), then to \(b_2\).
Input: 411100041 2 3 43 1 4 232 9 31 100 91075019 709259 5 611271314 9024533 81871864 9 3 6 48659503 2 371245467 6 7 37376159 8 364036498 52295554 169 | Output: 2 0 2 18
Easy
3
727
480
114
17
45
F
45F
F. Goats and Wolves
2,500
greedy
Once Vasya needed to transport m goats and m wolves from riverbank to the other as quickly as possible. The boat can hold n animals and Vasya, in addition, he is permitted to put less than n animals in the boat. If in one place (on one of the banks or in the boat) the wolves happen to strictly outnumber the goats, then...
The first line contains two space-separated numbers m and n (1 ≀ m, n ≀ 105) β€” the number of animals and the boat's capacity.
If it is impossible to transport all the animals so that no one got upset, and all the goats survived, print -1. Otherwise print the single number β€” how many times Vasya will have to cross the river.
The first sample match to well-known problem for children.
Input: 3 2 | Output: 11
Expert
1
1,065
125
199
0
1,996
A
1996A
A. Legs
800
binary search; math; ternary search
It's another beautiful day on Farmer John's farm.After Farmer John arrived at his farm, he counted \(n\) legs. It is known only chickens and cows live on the farm, and a chicken has \(2\) legs while a cow has \(4\). What is the minimum number of animals Farmer John can have on his farm assuming he counted the legs of a...
The first line contains single integer \(t\) (\(1 \leq t \leq 10^3\)) β€” the number of test cases.Each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^3\), \(n\) is even).
For each test case, output an integer, the minimum number of animals Farmer John can have on his farm.
Input: 3268 | Output: 1 2 2
Beginner
3
331
186
102
19
1,551
D1
1551D1
D1. Domino (easy version)
1,700
constructive algorithms; math
The only difference between this problem and D2 is that you don't have to provide the way to construct the answer in this problem, but you have to do it in D2.There's a table of \(n \times m\) cells (\(n\) rows and \(m\) columns). The value of \(n \cdot m\) is even.A domino is a figure that consists of two cells having...
The first line contains one integer \(t\) (\(1 \le t \le 10\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case consists of a single line. The line contains three integers \(n\), \(m\), \(k\) (\(1 \le n,m \le 100\), \(0 \le k \le \frac{nm}{2}\), \(n \cdot m\) is even) β€” the number of rows, column...
For each test case output ""YES"", if it is possible to place dominoes in the desired way, or ""NO"" otherwise.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
Input: 8 4 4 2 2 3 0 3 2 3 1 2 0 2 4 2 5 2 2 2 17 16 2 1 1 | Output: YES YES YES NO YES NO YES NO
Medium
2
691
360
264
15
1,852
A
1852A
A. Ntarsis' Set
1,800
binary search; math; number theory
Ntarsis has been given a set \(S\), initially containing integers \(1, 2, 3, \ldots, 10^{1000}\) in sorted order. Every day, he will remove the \(a_1\)-th, \(a_2\)-th, \(\ldots\), \(a_n\)-th smallest numbers in \(S\) simultaneously.What is the smallest element in \(S\) after \(k\) days?
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 consists of two integers \(n\) and \(k\) (\(1 \leq n,k \leq 2 \cdot 10^5\)) β€” the length of \(a\) and the number of days.The f...
For each test case, print an integer that is the smallest element in \(S\) after \(k\) days.
For the first test case, each day the \(1\)-st, \(2\)-nd, \(4\)-th, \(5\)-th, and \(6\)-th smallest elements need to be removed from \(S\). So after the first day, \(S\) will become \(\require{cancel}\) \(\{\cancel 1, \cancel 2, 3, \cancel 4, \cancel 5, \cancel 6, 7, 8, 9, \ldots\} = \{3, 7, 8, 9, \ldots\}\). The small...
Input: 75 11 2 4 5 65 31 3 5 6 74 10002 3 4 59 14341 4 7 9 12 15 17 18 2010 41 3 5 7 9 11 13 15 17 1910 61 4 7 10 13 16 19 22 25 2810 1500001 3 4 5 10 11 12 13 14 15 | Output: 3 9 1 12874 16 18 1499986
Medium
3
287
669
92
18
665
D
665D
D. Simple Subset
1,800
constructive algorithms; greedy; number theory
A tuple of positive integers {x1, x2, ..., xk} is called simple if for all pairs of positive integers (i, j) (1 ≀ i < j ≀ k), xi + xj is a prime.You are given an array a with n positive integers a1, a2, ..., an (not necessary distinct). You want to find a simple subset of the array a with the maximum size.A prime numbe...
The first line contains integer n (1 ≀ n ≀ 1000) β€” the number of integers in the array a.The second line contains n integers ai (1 ≀ ai ≀ 106) β€” the elements of the array a.
On the first line print integer m β€” the maximum possible size of simple subset of a.On the second line print m integers bl β€” the elements of the simple subset of the array a with the maximum size.If there is more than one solution you can print any of them. You can print the elements of the subset in any order.
Input: 22 3 | Output: 23 2
Medium
3
547
173
312
6
1,185
F
1185F
F. Two Pizzas
2,100
bitmasks; brute force
A company of \(n\) friends wants to order exactly two pizzas. It is known that in total there are \(9\) pizza ingredients in nature, which are denoted by integers from \(1\) to \(9\).Each of the \(n\) friends has one or more favorite ingredients: the \(i\)-th of friends has the number of favorite ingredients equal to \...
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5, 2 \le m \le 10^5\)) β€” the number of friends in the company and the number of pizzas, respectively.Next, the \(n\) lines contain descriptions of favorite ingredients of the friends: the \(i\)-th of them contains the number of favorite...
Output two integers \(j_1\) and \(j_2\) (\(1 \le j_1,j_2 \le m\), \(j_1 \ne j_2\)) denoting the indices of two pizzas in the required set. If there are several solutions, output any of them. Pizza indices can be printed in any order.
Input: 3 4 2 6 7 4 2 3 9 5 3 2 3 9 100 1 7 400 3 3 2 5 100 2 9 2 500 3 2 9 5 | Output: 2 3
Hard
2
1,131
788
233
11
570
B
570B
B. Simple Game
1,300
constructive algorithms; games; greedy; implementation; math
One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to n. Let's assume that Misha chose number m, and Andrew chose number a.Then, by using a random generator they choose a random integer c in the range between 1 and n (any integer from 1 to n is chosen wit...
The first line contains two integers n and m (1 ≀ m ≀ n ≀ 109) β€” the range of numbers in the game, and the number selected by Misha respectively.
Print a single number β€” such value a, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.
In the first sample test: Andrew wins if c is equal to 2 or 3. The probability that Andrew wins is 2 / 3. If Andrew chooses a = 3, the probability of winning will be 1 / 3. If a = 1, the probability of winning is 0.In the second sample test: Andrew wins if c is equal to 1 and 2. The probability that Andrew wins is 1 / ...
Input: 3 1 | Output: 2
Easy
5
896
145
149
5
594
C
594C
C. Edo and Magnets
2,300
brute force; greedy; implementation; two pointers
Edo has got a collection of n refrigerator magnets!He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door that meets the following restrictions: the refrigerator door must be rectangle, and both the length and the width of the door must be positiv...
The first line contains two integers n and k (1 ≀ n ≀ 100 000, 0 ≀ k ≀ min(10, n - 1)) β€” the number of magnets that Edo has and the maximum number of magnets Edo may not place on the refrigerator.Next n lines describe the initial plan of placing magnets. Each line contains four integers x1, y1, x2, y2 (1 ≀ x1 < x2 ≀ 10...
Print a single integer β€” the minimum area of the door of refrigerator, which can be used to place at least n - k magnets, preserving the relative positions.
In the first test sample it is optimal to remove either the first or the third magnet. If we remove the first magnet, the centers of two others will lie at points (2.5, 2.5) and (3.5, 3.5). Thus, it is enough to buy a fridge with door width 1 and door height 1, the area of the door also equals one, correspondingly.In t...
Input: 3 11 1 2 22 2 3 33 3 4 4 | Output: 1
Expert
4
1,576
482
156
5
1,129
A1
1129A1
A1. Toy Train (Simplified)
1,700
brute force; greedy
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of \(n\) stations, enumerated from \(1\) through \(n\). The train occupies one ...
The first line contains two space-separated integers \(n\) and \(m\) (\(2 \leq n \leq 100\); \(1 \leq m \leq 200\)) β€” the number of stations and the number of candies, respectively.The \(i\)-th of the following \(m\) lines contains two space-separated integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n\); \(a_i \neq...
In the first and only line, print \(n\) space-separated integers, the \(i\)-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station \(i\).
Consider the second sample.If the train started at station \(1\), the optimal strategy is as follows. Load the first candy onto the train. Proceed to station \(2\). This step takes \(1\) second. Deliver the first candy. Proceed to station \(1\). This step takes \(1\) second. Load the second candy onto the train. Procee...
Input: 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 | Output: 10 9 10 10 9
Medium
2
1,580
433
205
11
1,906
M
1906M
M. Triangle Construction
1,700
greedy; math
You are given a regular \(N\)-sided polygon. Label one arbitrary side as side \(1\), then label the next sides in clockwise order as side \(2\), \(3\), \(\dots\), \(N\). There are \(A_i\) special points on side \(i\). These points are positioned such that side \(i\) is divided into \(A_i + 1\) segments with equal lengt...
The first line consists of an integer \(N\) (\(3 \leq N \leq 200\,000\)).The following line consists of \(N\) integers \(A_i\) (\(1 \leq A_i \leq 2 \cdot 10^9\)).
Output a single integer representing the maximum number of non-degenerate triangles that you can create.
Explanation for the sample input/output #1One possible construction which achieves maximum number of non-degenerate triangles can be seen in the following illustration. Explanation for the sample input/output #2One possible construction which achieves maximum number of non-degenerate triangles can be seen in the follow...
Input: 4 3 1 4 6 | Output: 4
Medium
2
1,037
162
104
19
717
C
717C
C. Potions Homework
1,200
implementation; sortings
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe ...
The first line of input contains integer n (1 ≀ n ≀ 100 000) β€” the number of tasks. The next n lines contain exactly one integer number ai (1 ≀ ai ≀ 100 000) β€” both the difficulty of the initial task and the laziness of the i-th students.
Print the minimum total time to finish all tasks modulo 10 007.
In the first sample, if the students switch their tasks, they will be able to finish them in 3 + 3 = 6 time units.
Input: 213 | Output: 6
Easy
2
1,422
238
63
7
162
I
162I
I. Truncatable primes
2,000
*special
A truncatable prime is a prime number which contains no zeros in decimal notation and all its suffixes are primes. 1 is considered to be not a prime.You are given a positive integer n. Figure out whether it is a truncatable prime.
The only line of input contains an integer n (2 ≀ n ≀ 107).
Output ""YES"" if n is a truncatable prime. Output ""NO"" otherwise. Quotes for clarity only.
In the first sample 19 is a prime but its suffix 9 is not.In the second sample 9137, 137, 37 and 7 are all primes, so 9137 is a truncatable prime.
Input: 19 | Output: NO
Hard
1
230
59
93
1
1,858
D
1858D
D. Trees and Segments
2,200
brute force; data structures; dp; greedy; two pointers
The teachers of the Summer Informatics School decided to plant \(n\) trees in a row, and it was decided to plant only oaks and firs. To do this, they made a plan, which can be represented as a binary string \(s\) of length \(n\). If \(s_i = 0\), then the \(i\)-th tree in the row should be an oak, and if \(s_i = 1\), th...
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 3000\), \(0 \le k \le n\)) β€” the number of trees in the row and the maximum number of changes.The second line contains a string \(s\) of ...
For each test case, print \(n\) integers, the \(j\)-th (\(1 \le j \le n\)) of which is the maximum beauty of the row of trees after no more than \(k\) changes if \(a = j\) is used to calculate the beauty.
In the first test case no changes are allowed, so \(l_0 = 0\) and \(l_1 = 3\) always hold. Thus, regardless of the value of \(a\), the beauty of the row of trees will be \(3\).In the second test case for \(a \in \{1, 2\}\) the teachers can, for example, change the plan \(s\) to \(0111\) (by changing \(s_4\)), and for \...
Input: 53 01114 101105 0100006 21011017 10001101 | Output: 3 3 3 4 5 7 9 5 9 13 17 21 6 9 13 17 21 25 7 10 13 17 21 25 29
Hard
5
1,814
480
204
18
623
B
623B
B. Array GCD
2,300
dp; greedy; number theory
You are given array ai of length n. You may consecutively apply two operations to this array: remove some subsegment (continuous subsequence) of length m < n and pay for it mΒ·a coins; change some elements of the array by at most 1, and pay b coins for each change. Please note that each of operations may be applied at m...
The first line of the input contains integers n, a and b (1 ≀ n ≀ 1 000 000, 0 ≀ a, b ≀ 109) β€” the length of the array, the cost of removing a single element in the first operation and the cost of changing an element, respectively.The second line contains n integers ai (2 ≀ ai ≀ 109) β€” elements of the array.
Print a single number β€” the minimum cost of changes needed to obtain an array, such that the greatest common divisor of all its elements is greater than 1.
In the first sample the optimal way is to remove number 3 and pay 1 coin for it.In the second sample you need to remove a segment [17, 13] and then decrease number 6. The cost of these changes is equal to 2Β·3 + 2 = 8 coins.
Input: 3 1 44 2 3 | Output: 1
Expert
3
707
309
155
6