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,165
B
1165B
B. Polycarp Training
1,000
data structures; greedy; sortings
Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly \(1\) problem, during the second day β€” exactly \(2\) problems, during the third day β€” exactly \(3\) problems, and so on. During the \(k\)-th day he should solve \(k\) problems.Polycarp has a list...
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of contests.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)) β€” the number of problems in the \(i\)-th contest.
Print one integer β€” the maximum number of days Polycarp can train if he chooses the contests optimally.
Input: 4 3 1 4 1 | Output: 3
Beginner
3
804
271
103
11
818
F
818F
F. Level Generation
2,100
binary search; math; ternary search
Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level.Ivan decided that there should be exactly ni vertices in the graph representing level i, and the edges have to be bidirectional. When con...
The first line of input file contains a positive integer q (1 ≀ q ≀ 100 000) β€” the number of graphs Ivan needs to construct.Then q lines follow, i-th line contains one positive integer ni (1 ≀ ni ≀ 2Β·109) β€” the number of vertices in i-th graph.Note that in hacks you have to use q = 1.
Output q numbers, i-th of them must be equal to the maximum number of edges in i-th graph.
In the first example it is possible to construct these graphs: 1 - 2, 1 - 3; 1 - 2, 1 - 3, 2 - 4; 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6.
Input: 3346 | Output: 236
Hard
3
1,000
285
90
8
416
A
416A
A. Guess a number!
1,400
greedy; implementation; two pointers
A TV show called ""Guess a number!"" is gathering popularity. The whole Berland, the old and the young, are watching the show.The rules are simple. The host thinks of an integer y and the participants guess it by asking questions to the host. There are four types of acceptable questions: Is it true that y is strictly l...
The first line of the input contains a single integer n (1 ≀ n ≀ 10000) β€” the number of questions (and answers). Next n lines each contain one question and one answer to it. The format of each line is like that: ""sign x answer"", where the sign is: "">"" (for the first type queries), ""<"" (for the second type queries...
Print any of such integers y, that the answers to all the queries are correct. The printed number y must meet the inequation - 2Β·109 ≀ y ≀ 2Β·109. If there are many answers, print any of them. If such value doesn't exist, print word ""Impossible"" (without the quotes).
Input: 4>= 1 Y< 3 N<= -3 N> 55 N | Output: 17
Easy
3
730
603
268
4
765
E
765E
E. Tree Folding
2,200
dfs and similar; dp; greedy; implementation; trees
Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex v, and two disjoint (except for v) paths of equal length a0 = v, a1, ..., ak, and b0 = v, b1, ..., bk. Additionally, vertices a1, ..., ak, b1, ..., bk must not have any neighbours in the tree other than adjacent verti...
The first line of input contains the number of vertices n (2 ≀ n ≀ 2Β·105).Next n - 1 lines describe edges of the tree. Each of these lines contains two space-separated integers u and v (1 ≀ u, v ≀ n, u β‰  v) β€” indices of endpoints of the corresponding edge. It is guaranteed that the given graph is a tree.
If it is impossible to obtain a path, print -1. Otherwise, print the minimum number of edges in a possible path.
In the first sample case, a path of three edges is obtained after merging paths 2 - 1 - 6 and 2 - 4 - 5.It is impossible to perform any operation in the second sample case. For example, it is impossible to merge paths 1 - 3 - 4 and 1 - 5 - 6, since vertex 6 additionally has a neighbour 7 that is not present in the corr...
Input: 61 22 32 44 51 6 | Output: 3
Hard
5
652
305
112
7
1,423
G
1423G
G. Growing flowers
3,500
data structures
Sarah has always been a lover of nature, and a couple of years ago she saved up enough money to travel the world and explore all the things built by nature over its lifetime on earth. During this time she visited some truly special places which were left untouched for centuries, from watching icebergs in freezing weath...
First line contains two integers \(N\) and \(Q \;(1 \leq N, Q \leq 10^5)\,\) β€” number of flowers and the number of queries, respectively.The second line contains \(N\) integers \(A_1, A_2, ..., A_N\;(1 \leq A_i \leq 10^9)\,\) β€” where \(A_i\) represents type of the \(i\)-th flower.Each of the next \(Q\) lines describe q...
For each query of the second type print the beautiness \(B\) of the collection.
Let's look at the example.Initially the collection is \([1, 2, 3, 4, 5]\). In the first query \(K = 3\), we consider sections of three flowers with the first being \([1, 2, 3]\). Since beautiness of the section is the number of distinct flower types in that section, \(B_1 = 3\). Second section is \([2, 3, 4]\) and \(B_...
Input: 5 5 1 2 3 4 5 2 3 1 1 2 5 2 4 1 2 4 5 2 2 | Output: 9 6 4
Master
1
2,854
673
79
14
921
12
92112
12. Labyrinth-12
3,200
See the problem statement here: http://codeforces.com/contest/921/problem/01.
Master
0
77
0
0
9
1,671
F
1671F
F. Permutation Counting
2,700
brute force; combinatorics; dp; fft; math
Calculate the number of permutations \(p\) of size \(n\) with exactly \(k\) inversions (pairs of indices \((i, j)\) such that \(i < j\) and \(p_i > p_j\)) and exactly \(x\) indices \(i\) such that \(p_i > p_{i+1}\).Yep, that's the whole problem. Good luck!
The first line contains one integer \(t\) (\(1 \le t \le 3 \cdot 10^4\)) β€” the number of test cases.Each test case consists of one line which contains three integers \(n\), \(k\) and \(x\) (\(1 \le n \le 998244352\); \(1 \le k \le 11\); \(1 \le x \le 11\)).
For each test case, print one integer β€” the answer to the problem, taken modulo \(998244353\).
Input: 510 6 47 3 1163316 11 7136373 11 1325902 11 11 | Output: 465 12 986128624 7636394 57118194
Master
5
256
257
94
16
1,599
J
1599J
J. Bob's Beautiful Array
2,600
bitmasks; brute force; greedy
Bob really likes playing with arrays of numbers. That's why for his birthday, his friends bought him a really interesting machine – an array beautifier. The array beautifier takes an array \(A\) consisting of \(N\) integers, and it outputs a new array \(B\) of length N that it constructed based on the array given to it...
The first line contains one positive integer \(N\) (\(2 \leq N \leq 10^3\)) – the length of Bob's array.The second line contains \(N\) integers \(B_1\), \(B_2\), ..., \(B_N\) (\(1 \leq B_i \leq 10^6\)) – the elements of the array the machine outputted.
If an appropriate input array exists, print ""YES"", followed by the input array \(A_1\), \(A_2\), ..., \(A_N\) (\(-10^9 \leq A_i \leq 10^9\)) in the next line. Otherwise, print ""NO"".
Input: 2 5 5 | Output: YES 2 3
Expert
3
1,243
252
185
15
900
C
900C
C. Remove Extra One
1,700
brute force; data structures; math
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible.We remind that in a sequence of numbers a1, a2, ..., ak the element ai is a record if for every integer j (1 ≀ j < i) the following holds: aj < ai.
The first line contains the only integer n (1 ≀ n ≀ 105) β€” the length of the permutation.The second line contains n integers p1, p2, ..., pn (1 ≀ pi ≀ n) β€” the permutation. All the integers are distinct.
Print the only integer β€” the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one.
In the first example the only element can be removed.
Input: 11 | Output: 1
Medium
3
277
203
172
9
405
E
405E
E. Graph Cutting
2,300
dfs and similar; graphs
Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest.Chris is given a simple undirected connected graph with n vertices (numbered from 1 to n) and m edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partition...
The first line of input contains two space-separated integers n and m (1 ≀ n, m ≀ 105), the number of vertices and the number of edges in the graph. The next m lines contain the description of the graph's edges. The i-th line contains two space-separated integers ai and bi (1 ≀ ai, bi ≀ n; ai β‰  bi), the numbers of the ...
If it is possible to cut the given graph into edge-distinct paths of length 2, output lines. In the i-th line print three space-separated integers xi, yi and zi, the description of the i-th path. The graph should contain this path, i.e., the graph should contain edges (xi, yi) and (yi, zi). Each edge should appear in e...
Input: 8 121 22 33 44 11 32 43 53 65 66 76 87 8 | Output: 1 2 41 3 21 4 35 3 65 6 86 7 8
Expert
2
702
641
484
4
1,148
F
1148F
F. Foo Fighters
2,700
bitmasks; constructive algorithms
You are given \(n\) objects. Each object has two integer properties: \(val_i\) β€” its price β€” and \(mask_i\). It is guaranteed that the sum of all prices is initially non-zero.You want to select a positive integer \(s\). All objects will be modified after that. The \(i\)-th object will be modified using the following pr...
The first line contains a single integer \(n\) (\(1 \leq n \leq 3 \cdot 10^5\)) β€” the number of objects.The \(i\)-th of next \(n\) lines contains integers \(val_i\) and \(mask_i\) (\(-10^9 \leq val_i \leq 10^9\), \(1 \le mask_i \le 2^{62} - 1\)) β€” the price of the object and its mask.It is guaranteed that the sum of \(...
Print an integer \(s\) (\(1 \le s \le 2^{62} - 1\)), such that if we modify the objects as described above, the sign of the sum of \(val_i\) changes its sign.If there are multiple such \(s\), print any of them. One can show that there is always at least one valid \(s\).
In the first test sample all objects will change their prices except for the object with mask \(151\). So their total sum will change its sign: initially \(24\), after modifications β€” \(-28\).In the second test sample the only object will change its price. So the total sum will change its sign.
Input: 5 17 206 -6 117 -2 151 9 93 6 117 | Output: 64
Master
2
861
350
270
11
1,056
H
1056H
H. Detect Robots
3,200
data structures; strings
You successfully found poor Arkady near the exit of the station you've perfectly predicted. You sent him home on a taxi and suddenly came up with a question.There are \(n\) crossroads in your city and several bidirectional roads connecting some of them. A taxi ride is a path from some crossroads to another one without ...
Each test contains one or more test cases. The first line contains a single integer \(t\) (\(1 \le t \le 3 \cdot 10^5\)) β€” 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 number of crossroads in the city.The next line contains a single integ...
Output a single line for each test case.If the driver can be a robot, output ""Robot"" in a single line. Otherwise, output ""Human"".You can print each letter in any case (upper or lower).
In the first example it is clear that the driver used two different ways to get from crossroads \(1\) to crossroads \(3\). It must be a human.In the second example the driver always drives the cycle \(1 \to 2 \to 3 \to 4 \to 1\) until he reaches destination.
Input: 1 5 2 4 1 2 3 5 3 1 4 3 | Output: Human
Master
2
1,018
994
188
10
1,080
F
1080F
F. Katya and Segments Sets
2,400
data structures; interactive; sortings
It is a very important day for Katya. She has a test in a programming class. As always, she was given an interesting problem that she solved very fast. Can you solve that problem?You are given \(n\) ordered segments sets. Each segment can be represented as a pair of two integers \([l, r]\) where \(l\leq r\). Each set c...
The first line contains three integers \(n\), \(m\), and \(k\) \((1\leq n,m\leq 10^5, 1\leq k\leq 3\cdot10^5)\) β€” the number of sets, queries, and segments respectively.Each of the next \(k\) lines contains three integers \(l\), \(r\), and \(p\) \((1\leq l\leq r\leq 10^9, 1\leq p\leq n)\) β€” the limits of the segment an...
For each query, print ""yes"" or ""no"" in a new line.
For the first query, the answer is negative since the second set does not contain a segment that lies on the segment \([2, 3]\).In the second query, the first set contains \([2, 3]\), and the second set contains \([2, 4]\).In the third query, the first set contains \([2, 3]\), the second set contains \([2, 4]\), and th...
Input: 5 5 9 3 6 3 1 3 1 2 4 2 1 2 3 4 6 5 2 5 3 7 9 4 2 3 1 4 10 4 1 2 2 3 1 2 2 4 1 3 1 5 2 3 3 6 2 4 2 9 | Output: no yes yes no yes
Expert
3
898
520
54
10
1,204
D2
1204D2
D2. Kirk and a Binary String (hard version)
2,100
data structures; greedy; math; strings
The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.Kirk has a binary string \(s\) (a string which consists of zeroes and ones) of length \(n\) and he is asking you to find a bina...
The first line contains a binary string of length not more than \(10^5\).
Output a binary string which satisfied the above conditions. If there are many such strings, output any of them.
In the first example: For the substrings of the length \(1\) the length of the longest non-decreasing subsequnce is \(1\); For \(l = 1, r = 2\) the longest non-decreasing subsequnce of the substring \(s_{1}s_{2}\) is \(11\) and the longest non-decreasing subsequnce of the substring \(t_{1}t_{2}\) is \(01\); For \(l = 1...
Input: 110 | Output: 010
Hard
4
1,022
73
112
12
1,521
B
1521B
B. Nastia and a Good Array
1,300
constructive algorithms; math; number theory
Nastia has received an array of \(n\) positive integers as a gift.She calls such an array \(a\) good that for all \(i\) (\(2 \le i \le n\)) takes place \(gcd(a_{i - 1}, a_{i}) = 1\), where \(gcd(u, v)\) denotes the greatest common divisor (GCD) of integers \(u\) and \(v\).You can perform the operation: select two diffe...
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 length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_{n}\) (\(1 \le a_i \le 10^...
For each of \(t\) test cases print a single integer \(k\) (\(0 \le k \le n\)) β€” the number of operations. You don't need to minimize this number.In each of the next \(k\) lines print \(4\) integers \(i\), \(j\), \(x\), \(y\) (\(1 \le i \neq j \le n\), \(1 \le x, y \le 2 \cdot 10^9\)) so that \(\min{(a_i, a_j)} = \min{(...
Consider the first test case.Initially \(a = [9, 6, 3, 11, 15]\).In the first operation replace \(a_1\) with \(11\) and \(a_5\) with \(9\). It's valid, because \(\min{(a_1, a_5)} = \min{(11, 9)} = 9\).After this \(a = [11, 6, 3, 11, 9]\).In the second operation replace \(a_2\) with \(7\) and \(a_5\) with \(6\). It's va...
Input: 2 5 9 6 3 11 15 3 7 5 13 | Output: 2 1 5 11 9 2 5 7 6 0
Easy
3
652
455
441
15
328
A
328A
A. IQ Test
1,800
implementation
Petya is preparing for IQ test and he has noticed that there many problems like: you are given a sequence, find the next number. Now Petya can solve only problems with arithmetic or geometric progressions.Arithmetic progression is a sequence a1, a1 + d, a1 + 2d, ..., a1 + (n - 1)d, where a1 and d are any numbers.Geomet...
The first line contains exactly four integer numbers between 1 and 1000, inclusively.
Print the required number. If the given sequence is arithmetic progression, print the next progression element. Similarly, if the given sequence is geometric progression, print the next progression element.Print 42 if the given sequence is not an arithmetic or geometric progression.
This problem contains very weak pretests!
Input: 836 624 412 200 | Output: -12
Medium
1
761
85
283
3
550
A
550A
A. Two Substrings
1,500
brute force; dp; greedy; implementation; strings
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings ""AB"" and ""BA"" (the substrings can go in any order).
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Print ""YES"" (without the quotes), if string s contains two non-overlapping substrings ""AB"" and ""BA"", and ""NO"" otherwise.
In the first sample test, despite the fact that there are substrings ""AB"" and ""BA"", their occurrences overlap, so the answer is ""NO"".In the second sample test there are the following occurrences of the substrings: BACFAB.In the third sample test there is no substring ""AB"" nor substring ""BA"".
Input: ABA | Output: NO
Medium
5
167
109
128
5
1,615
C
1615C
C. Menorah
1,600
brute force; graphs; greedy; math
There are \(n\) candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string \(s\), where the \(i\)-th candle is lit if and only if \(s_i=1\). Initially, the candle lights are described by a string \(a\). In an operation, you select a candle that i...
The first line contains an integer \(t\) (\(1\le t\le 10^4\)) β€” the number of test cases. Then \(t\) cases follow.The first line of each test case contains a single integer \(n\) (\(1\le n\le 10^5\)) β€” the number of candles.The second line contains a string \(a\) of length \(n\) consisting of symbols 0 and 1 β€” the init...
For each test case, output the minimum number of operations required to transform \(a\) to \(b\), or \(-1\) if it's impossible.
In the first test case, the two strings are already equal, so we don't have to perform any operations.In the second test case, we can perform a single operation selecting the second candle to transform \(01\) into \(11\).In the third test case, it's impossible to perform any operations because there are no lit candles ...
Input: 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 | Output: 0 1 -1 3 4
Medium
4
676
523
127
16
1,088
A
1088A
A. Ehab and another construction problem
800
brute force; constructive algorithms
Given an integer \(x\), find 2 integers \(a\) and \(b\) such that: \(1 \le a,b \le x\) \(b\) divides \(a\) (\(a\) is divisible by \(b\)). \(a \cdot b>x\). \(\frac{a}{b}<x\).
The only line contains the integer \(x\) \((1 \le x \le 100)\).
You should output two integers \(a\) and \(b\), satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print ""-1"" (without quotes).
Input: 10 | Output: 6 3
Beginner
2
173
63
186
10
514
B
514B
B. Han Solo and Lazer Gun
1,400
brute force; data structures; geometry; implementation; math
There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane. Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0, y0). In one shot it can can de...
The first line contains three integers n, x0 ΠΈ y0 (1 ≀ n ≀ 1000, - 104 ≀ x0, y0 ≀ 104) β€” the number of stormtroopers on the battle field and the coordinates of your gun.Next n lines contain two integers each xi, yi ( - 104 ≀ xi, yi ≀ 104) β€” the coordinates of the stormtroopers on the battlefield. It is guaranteed that ...
Print a single integer β€” the minimum number of shots Han Solo needs to destroy all the stormtroopers.
Explanation to the first and second samples from the statement, respectively:
Input: 4 0 01 12 22 0-1 -1 | Output: 2
Easy
5
696
426
101
5
1,603
D
1603D
D. Artistic Partition
3,000
divide and conquer; dp; number theory
For two positive integers \(l\) and \(r\) (\(l \le r\)) let \(c(l, r)\) denote the number of integer pairs \((i, j)\) such that \(l \le i \le j \le r\) and \(\operatorname{gcd}(i, j) \ge l\). Here, \(\operatorname{gcd}(i, j)\) is the greatest common divisor (GCD) of integers \(i\) and \(j\).YouKn0wWho has two integers ...
The first line contains a single integer \(t\) (\(1 \le t \le 3 \cdot 10^5\)) β€” the number of test cases.The first and only line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)).
For each test case, print a single integer β€” \(f(n, k)\).
In the first test case, YouKn0wWho can select the sequence \([0, 2, 6]\). So \(f(6, 2) = c(1, 2) + c(3, 6) = 3 + 5 = 8\) which is the minimum possible.
Input: 4 6 2 4 4 3 1 10 3 | Output: 8 4 6 11
Master
3
556
214
57
16
1,511
C
1511C
C. Yet Another Card Deck
1,100
brute force; data structures; implementation; trees
You have a card deck of \(n\) cards, numbered from top to bottom, i. e. the top card has index \(1\) and bottom card β€” index \(n\). Each card has its color: the \(i\)-th card has color \(a_i\).You should process \(q\) queries. The \(j\)-th query is described by integer \(t_j\). For each query you should: find the highe...
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 3 \cdot 10^5\); \(1 \le q \le 3 \cdot 10^5\)) β€” the number of cards in the deck and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 50\)) β€” the colors of cards.The third line contains \(q\) intege...
Print \(q\) integers β€” the answers for each query.
Description of the sample: the deck is \([2, 1, 1, 4, \underline{3}, 3, 1]\) and the first card with color \(t_1 = 3\) has position \(5\); the deck is \([3, \underline{2}, 1, 1, 4, 3, 1]\) and the first card with color \(t_2 = 2\) has position \(2\); the deck is \([2, 3, \underline{1}, 1, 4, 3, 1]\) and the first card ...
Input: 7 5 2 1 1 4 3 3 1 3 2 1 1 4 | Output: 5 2 3 1 5
Easy
4
483
465
50
15
1,474
A
1474A
A. Puzzle From the Future
800
greedy
In the \(2022\) year, Mike found two binary integers \(a\) and \(b\) of length \(n\) (both of them are written only by digits \(0\) and \(1\)) that can have leading zeroes. In order not to forget them, he wanted to construct integer \(d\) in the following way: he creates an integer \(c\) as a result of bitwise summing ...
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The first line of each test case contains the integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the length of \(a\) and \(b\).The second line of each test case contains binary integer \(b\) of length \(n\). The integer \(b\) consi...
For each test case output one binary integer \(a\) of length \(n\). Note, that \(a\) or \(b\) may have leading zeroes but must have the same length \(n\).
In the first test case, \(b = 0\) and choosing \(a = 1\) gives \(d = 1\) as a result.In the second test case, \(b = 011\) so: if you choose \(a = 000\), \(c\) will be equal to \(011\), so \(d = 01\); if you choose \(a = 111\), \(c\) will be equal to \(122\), so \(d = 12\); if you choose \(a = 010\), you'll get \(d = 02...
Input: 5 1 0 3 011 3 110 6 111000 6 001011 | Output: 1 110 100 101101 101110
Beginner
1
1,083
450
154
14
316
G2
316G2
G2. Good Substrings
2,200
string suffix structures
Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or not the game uses rules. Overall there are n rules. Each rule is described by a group of three (p, l, r), where p is a string and l and r (...
The first line contains string s. The second line contains integer n. Next n lines contain the rules, one per line. Each of these lines contains a string and two integers pi, li, ri, separated by single spaces (0 ≀ li ≀ ri ≀ |pi|). It is guaranteed that all the given strings are non-empty and only contain lowercase Eng...
Print a single integer β€” the number of good substrings of string s.
There are three good substrings in the first sample test: Β«aabΒ», Β«abΒ» and Β«bΒ».In the second test only substrings Β«eΒ» and Β«tΒ» are good.
Input: aaab2aa 0 0aab 1 1 | Output: 3
Hard
1
1,167
767
67
3
35
E
35E
E. Parade
2,100
data structures; sortings
No Great Victory anniversary in Berland has ever passed without the war parade. This year is not an exception. That’s why the preparations are on in full strength. Tanks are building a line, artillery mounts are ready to fire, soldiers are marching on the main square... And the air forces general Mr. Generalov is in tr...
The first input line contains integer n (1 ≀ n ≀ 100000). Then follow n lines, each containing three integers hi, li, ri (1 ≀ hi ≀ 109, - 109 ≀ li < ri ≀ 109).
In the first line output integer m β€” amount of vertices of the enveloping polyline. The next m lines should contain 2 integers each β€” the position and the height of the polyline’s vertex. Output the coordinates of each vertex in the order of traversing the polyline from west to east. Remember that the first and the las...
Input: 23 0 24 1 3 | Output: 60 00 31 31 43 43 0
Hard
2
2,044
159
375
0
1,737
F
1737F
F. Ela and Prime GCD
3,300
constructive algorithms; math; number theory
After a long, tough, but fruitful day at DTL, Ela goes home happily. She entertains herself by solving Competitive Programming problems. She prefers short statements, because she already read too many long papers and documentation at work. The problem of the day reads:You are given an integer \(c\). Suppose that \(c\) ...
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 one integer \(m\) (\(1 \le m \le 16\)) - the number of prime factor of \(c\).The second line of each test case contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(1 \le b_i < 2^{20}\)) ...
Print the answer for each test case, one per line. If there is no sequence for the given \(c\), print \(-1\).Otherwise, print \(n - 1\) lines. In \(i\)-th line, print \(m\) space-separated integers. The \(j\)-th integer of \(i\)-th line is equal to the exponent of \(j\)-th prime number from \(a_i\).If there are multipl...
In each test case, the values of \(c\) are \(6\), \(2\), \(30\), \(16\), and \(12\) in that order.In the first test case, \(1\), \(2\), \(3\), \(6\) are divisors of \(6\). Here, sequences \([2, 6, 3]\) and \([3, 6, 2]\) can be answer. Permutation \([3, 2, 6]\) is invalid because \(\gcd(a_1, a_2) = 1\) is not a prime nu...
Input: 521 11131 1 11422 1 | Output: 0 1 1 1 1 0 1 0 1 1 0 0 1 1 0 1 1 1 0 0 1 0 1 1 1 1 0 0 -1 2 0 1 1 0 1 2 1 1 0
Master
3
923
638
349
17
1,635
B
1635B
B. Avoid Local Maximums
800
greedy
You are given an array \(a\) of size \(n\). Each element in this array is an integer between \(1\) and \(10^9\).You can perform several operations to this array. During an operation, you can replace an element in the array with any integer between \(1\) and \(10^9\). Output the minimum number of operations needed such ...
Each test contains multiple test cases. The first line will contain a single integer \(t\) \((1 \leq t \leq 10000)\) β€” the number of test cases. Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) \((2 \leq n \leq 2 \cdot 10^5)\) β€” the size of the array \(a\).The second line of...
For each test case, first output a line containing a single integer \(m\) β€” minimum number of operations required. Then ouput a line consist of \(n\) integers β€” the resulting array after the operations. Note that this array should differ in exactly \(m\) elements from the initial array.If there are multiple answers, pr...
In the first example, the array contains no local maximum, so we don't need to perform operations.In the second example, we can change \(a_2\) to \(3\), then the array don't have local maximums.
Input: 5 3 2 1 2 4 1 2 3 1 5 1 2 1 2 1 9 1 2 1 3 2 3 1 2 1 9 2 1 3 1 3 1 3 1 3 | Output: 0 2 1 2 1 1 3 3 1 1 1 2 2 2 1 2 1 2 3 3 2 3 3 2 1 2 2 1 3 3 3 1 1 1 3
Beginner
1
662
528
328
16
1,580
E
1580E
E. Railway Construction
3,400
brute force; constructive algorithms; data structures; graphs; shortest paths
Because the railway system in Gensokyo is often congested, as an enthusiastic engineer, Kawasiro Nitori plans to construct more railway to ease the congestion.There are \(n\) stations numbered from \(1\) to \(n\) and \(m\) two-way railways in Gensokyo. Every two-way railway connects two different stations and has a pos...
The first line contains three integers \(n\), \(m\), and \(q\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le m \le 3 \cdot 10^5\), \(0 \le q \le 2\cdot10^5\)).The second line contains \(n\) integers \(w_1,w_2,\ldots,w_n\) (\(1 \le w_i \le 10^9\)).Each of the next \(m\) lines contains three integers \(u\), \(v\), \(d\) (\(1 \l...
Print \(q+1\) lines, and the \(i\)-th of these lines contains one integer, denoting the minimal cost of railway construction after the \(i-1\)-th incident (especially, the \(0\)-th incident means no incident occurred).
In the second example, Nitori can build railways as follows: \(1 \rightarrow 2\), \(1 \rightarrow 3\), \(1 \rightarrow 4\), \(2 \rightarrow 8\), and the cost is \(14 + 14 + 14 + 4 = 46\).
Input: 5 5 1 1 1 1 1 1 1 2 1 2 3 1 2 4 1 3 5 1 4 5 1 1 2 | Output: 3 9
Master
5
1,679
582
218
15
1,551
D2
1551D2
D2. Domino (hard version)
2,100
constructive algorithms; implementation; math
The only difference between this problem and D1 is that you don't have to provide the way to construct the answer in D1, but you have to do it in this problem.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 count of rows, columns...
For each test case: print ""NO"" if it's not possible to place the dominoes on the table in the described way; otherwise, print ""YES"" on a separate line, then print \(n\) lines so that each of them contains \(m\) lowercase letters of the Latin alphabet β€” the layout of the dominoes on the table. Each cell of the table...
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 accx aegx bega bdda YES aha aha YES zz aa zz NO YES aaza bbza NO YES bbaabbaabbaabbaay ddccddccddccddccy NO
Hard
3
656
359
686
15
2,051
G
2051G
G. Snakes
2,100
bitmasks; dp; dsu; graphs
Suppose you play a game where the game field looks like a strip of \(1 \times 10^9\) square cells, numbered from \(1\) to \(10^9\).You have \(n\) snakes (numbered from \(1\) to \(n\)) you need to place into some cells. Initially, each snake occupies exactly one cell, and you can't place more than one snake into one cel...
The first line contains two integers \(n\) and \(q\) (\(1 \le n \le 20\); \(1 \le q \le 2 \cdot 10^5\)) β€” the number of snakes and the number of events. Next \(q\) lines contain the description of events β€” one per line.The \(i\)-th line contains either ""\(s_i\) +"" (\(1 \le s_i \le n\)) meaning that the \(s_i\)-th sna...
Print one integer β€” the minimum possible score.
In the first test, the optimal strategy is to place the second snake at cell \(1\), the third snake β€” at \(2\), and the first one β€” at \(3\). The maximum occupied cell is cell \(4\), and it's the minimum possible score.In the second test, one of the optimal strategies is to place: snake \(2\) at position \(1\); snake \...
Input: 3 61 +1 -3 +3 -2 +2 - | Output: 4
Hard
4
965
533
47
20
592
D
592D
D. Super M
2,200
dfs and similar; dp; graphs; trees
Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of n cities, connected by n - 1 bidirectional roads. Every road connects exactly two distinct cities, and the whole road system is designed in a way that one is able to go ...
The first line of the input contains two integers n and m (1 ≀ m ≀ n ≀ 123456) - the number of cities in Byteforces, and the number of cities being attacked respectively.Then follow n - 1 lines, describing the road system. Each line contains two city numbers ui and vi (1 ≀ ui, vi ≀ n) - the ends of the road i.The last ...
First print the number of the city Super M should teleport to. If there are many possible optimal answers, print the one with the lowest city number.Then print the minimum possible time needed to scare all humans in cities being attacked, measured in Krons.Note that the correct answer is always unique.
In the first sample, there are two possibilities to finish the Super M's job in 3 krons. They are: and .However, you should choose the first one as it starts in the city with the lower number.
Input: 7 21 21 31 43 53 63 72 7 | Output: 23
Hard
4
1,290
437
303
5
526
D
526D
D. Om Nom and Necklace
2,200
hashing; string suffix structures; strings
One day Om Nom found a thread with n beads of different colors. He decided to cut the first several beads from this thread to make a bead necklace and present it to his girlfriend Om Nelly. Om Nom knows that his girlfriend loves beautiful patterns. That's why he wants the beads on the necklace to form a regular pattern...
The first line contains two integers n, k (1 ≀ n, k ≀ 1 000 000) β€” the number of beads on the thread that Om Nom found and number k from the definition of the regular sequence above.The second line contains the sequence of n lowercase Latin letters that represent the colors of the beads. Each color corresponds to a sin...
Print a string consisting of n zeroes and ones. Position i (1 ≀ i ≀ n) must contain either number one if the first i beads on the thread form a regular sequence, or a zero otherwise.
In the first sample test a regular sequence is both a sequence of the first 6 beads (we can take A = """", B = ""bca""), and a sequence of the first 7 beads (we can take A = ""b"", B = ""ca"").In the second sample test, for example, a sequence of the first 13 beads is regular, if we take A = ""aba"", B = ""ba"".
Input: 7 2bcabcab | Output: 0000011
Hard
3
986
331
182
5
1,248
D1
1248D1
D1. The World Is Just a Programming Task (Easy Version)
2,000
brute force; dp; greedy; implementation
This is an easier version of the problem. In this version, \(n \le 500\).Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya bel...
The first line contains an integer \(n\) (\(1 \le n \le 500\)), the length of the string.The second line contains a string, consisting of exactly \(n\) characters, where each of the characters is either ""("" or "")"".
The first line should contain a single integer β€” the largest beauty of the string, which can be achieved by swapping some two characters.The second line should contain integers \(l\) and \(r\) (\(1 \leq l, r \leq n\)) β€” the indices of two characters, which should be swapped in order to maximize the string's beauty.In c...
In the first example, we can swap \(7\)-th and \(8\)-th character, obtaining a string ""()()()()()"". The cyclical shifts by \(0, 2, 4, 6, 8\) of this string form a correct bracket sequence.In the second example, after swapping \(5\)-th and \(10\)-th character, we obtain a string "")(())()()(()"". The cyclical shifts b...
Input: 10 ()()())(() | Output: 5 8 7
Hard
4
1,475
218
376
12
871
E
871E
E. Restore the Tree
2,900
graphs; greedy; trees
Petya had a tree consisting of n vertices numbered with integers from 1 to n. Accidentally he lost his tree. Petya remembers information about k vertices: distances from each of them to each of the n tree vertices.Your task is to restore any tree that satisfies the information that Petya remembers or report that such t...
The first line contains two integers n and k (2 ≀ n ≀ 30 000, 1 ≀ k ≀ min(200, n)) β€” the number of vertices in the tree and the number of vertices about which Petya remembers distance information.The following k lines contain remembered information. The i-th line contains n integers di, 1, di, 2, ..., di, n (0 ≀ di, j ...
If there are no suitable trees, print -1.In the other case, print n - 1 lines: each line should contain two vertices connected by edge in the required tree. You can print edges and vertices in an edge in any order. The tree vertices are enumerated from 1 to n.If there are many solutions print any of them.
Picture for the first sample:
Input: 5 20 1 2 3 22 1 0 1 2 | Output: 2 13 24 35 2
Master
3
338
414
306
8
492
A
492A
A. Vanya and Cubes
800
implementation
Vanya got n cubes. He decided to build a pyramid from them. Vanya wants to build the pyramid as follows: the top level of the pyramid must consist of 1 cube, the second level must consist of 1 + 2 = 3 cubes, the third level must have 1 + 2 + 3 = 6 cubes, and so on. Thus, the i-th level of the pyramid must have 1 + 2 + ...
The first line contains integer n (1 ≀ n ≀ 104) β€” the number of cubes given to Vanya.
Print the maximum possible height of the pyramid in the single line.
Illustration to the second sample:
Input: 1 | Output: 1
Beginner
1
445
85
68
4
1,493
B
1493B
B. Planet Lapituletti
1,300
brute force; implementation
The time on the planet Lapituletti goes the same way it goes on Earth but a day lasts \(h\) hours and each hour lasts \(m\) minutes. The inhabitants of that planet use digital clocks similar to earth ones. Clocks display time in a format HH:MM (the number of hours in decimal is displayed first, then (after the colon) f...
The first line contains a single integer \(T\) (\(1 \le T \le 100\)) β€” the number of test cases.The next \(2 \cdot T\) lines contain the description of test cases. The description of each test case consists of two lines.The first line of a test case contains two integers \(h\), \(m\) (\(1 \le h, m \le 100\)).The second...
For each test case output in a separate line the nearest moment in format HH:MM when the reflected time is correct.
In the second test case it is not hard to show that the reflection of 23:59 is incorrect, while the reflection of the moment 00:00 on the next day is correct.
Input: 5 24 60 12:21 24 60 23:59 90 80 52:26 1 100 00:01 10 10 04:04 | Output: 12:21 00:00 52:28 00:00 00:00
Easy
2
1,765
386
115
14
919
D
919D
D. Substring
1,700
dfs and similar; dp; graphs
You are given a graph with \(n\) nodes and \(m\) directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are ""abaca"", then the value of that path is \(3\). Your task is find a path whose value is t...
The first line contains two positive integers \(n, m\) (\(1 \leq n, m \leq 300\,000\)), denoting that the graph has \(n\) nodes and \(m\) directed edges.The second line contains a string \(s\) with only lowercase English letters. The \(i\)-th character is the letter assigned to the \(i\)-th node.Then \(m\) lines follow...
Output a single line with a single integer denoting the largest value. If the value can be arbitrarily large, output -1 instead.
In the first sample, the path with largest value is \(1 \to 3 \to 4 \to 5\). The value is \(3\) because the letter 'a' appears \(3\) times.
Input: 5 4abaca1 21 33 44 5 | Output: 3
Medium
3
331
568
128
9
1,765
J
1765J
J. Hero to Zero
2,900
graph matchings; math
There are no heroes in this problem. I guess we should have named it ""To Zero"".You are given two arrays \(a\) and \(b\), each of these arrays contains \(n\) non-negative integers.Let \(c\) be a matrix of size \(n \times n\) such that \(c_{i,j} = |a_i - b_j|\) for every \(i \in [1, n]\) and every \(j \in [1, n]\).Your...
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^8\)).The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(0 \le b_j \le 10^8\)).
Print one integer β€” the minimum number of coins required to transform the matrix \(c\) into the zero matrix.
In the first example, the matrix looks as follows: 111000111 You can turn it into a zero matrix using \(2\) coins as follows: subtract \(1\) from the first row, paying \(1\) coin; subtract \(1\) from the third row, paying \(1\) coin. In the second example, the matrix looks as follows: 221001221 You can turn it into a z...
Input: 3 1 2 3 2 2 2 | Output: 2
Master
2
1,696
252
108
17
1,873
G
1873G
G. ABBC or BACB
1,500
constructive algorithms; greedy
You are given a string \(s\) made up of characters \(\texttt{A}\) and \(\texttt{B}\). Initially you have no coins. You can perform two types of operations: Pick a substring\(^\dagger\) \(\texttt{AB}\), change it to \(\texttt{BC}\), and get a coin. Pick a substring\(^\dagger\) \(\texttt{BA}\), change it to \(\texttt{CB}...
The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The only line of each test case contains the string \(s\) (\(1 \leq |s| \leq 2 \cdot 10^5\)). All characters of \(s\) are either \(\texttt{A}\) or \(\texttt{B}\).The...
For each test case, output a single integer β€” the maximum number of coins you can obtain.
In the first test case you can perform the following operations to get \(2\) coins: $$$\(\color{red}{\texttt{AB}}\texttt{BA} \to \texttt{BC}\color{red}{\texttt{BA}} \to \texttt{BCCB}\)\(In the second test case you can perform the following operation to get \)1\( coin: \)\(\color{red}{\texttt{AB}}\texttt{A} \to \texttt{...
Input: 8ABBAABABAABAABBAAAAAABBABABAAA | Output: 2 1 3 1 6 2 0 0
Medium
2
482
402
89
18
900
B
900B
B. Position in Fraction
1,300
math; number theory
You have a fraction . You need to find the first occurrence of digit c into decimal notation of the fraction after decimal point.
The first contains three single positive integers a, b, c (1 ≀ a < b ≀ 105, 0 ≀ c ≀ 9).
Print position of the first occurrence of digit c into the fraction. Positions are numbered from 1 after decimal point. It there is no such position, print -1.
The fraction in the first example has the following decimal notation: . The first zero stands on second position.The fraction in the second example has the following decimal notation: . There is no digit 7 in decimal notation of the fraction.
Input: 1 2 0 | Output: 2
Easy
2
129
87
159
9
1,660
B
1660B
B. Vlad and Candies
800
math
Not so long ago, Vlad had a birthday, for which he was presented with a package of candies. There were \(n\) types of candies, there are \(a_i\) candies of the type \(i\) (\(1 \le i \le n\)).Vlad decided to eat exactly one candy every time, choosing any of the candies of a type that is currently the most frequent (if t...
The first line of input data contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of input test cases.The following is a description of \(t\) test cases of input, two lines for each.The first line of the case contains the single number \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of types of candies in th...
Output \(t\) lines, each of which contains the answer to the corresponding test case of input. As an answer, output ""YES"" if Vlad can eat candy as planned, and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer...
In the first example, it is necessary to eat sweets in this order: a candy of the type \(2\), it is the most frequent, now \(a = [2, 2]\); a candy of the type \(1\), there are the same number of candies of the type \(2\), but we just ate one, now \(a = [1, 2]\); a candy of the type \(2\), it is the most frequent, now \...
Input: 622 31251 6 2 4 342 2 2 131 1000000000 99999999911 | Output: YES NO NO YES YES YES
Beginner
1
578
543
322
16
1,716
C
1716C
C. Robot in a Hallway
2,000
data structures; dp; greedy; implementation; ternary search
There is a grid, consisting of \(2\) rows and \(m\) columns. The rows are numbered from \(1\) to \(2\) from top to bottom. The columns are numbered from \(1\) to \(m\) from left to right.The robot starts in a cell \((1, 1)\). In one second, it can perform either of two actions: move into a cell adjacent by a side: up, ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of testcases.The first line of each testcase contains a single integer \(m\) (\(2 \le m \le 2 \cdot 10^5\)) β€” the number of columns of the grid.The \(i\)-th of the next \(2\) lines contains \(m\) integers \(a_{i,1}, a_{i,2}, \dots, a_{i,...
For each testcase, print a single integer β€” the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.
Input: 430 0 14 3 250 4 8 12 162 6 10 14 1840 10 10 1010 10 10 1020 00 0 | Output: 5 19 17 3
Hard
5
882
553
157
17
1,917
A
1917A
A. Least Product
800
constructive algorithms; math
You are given an array of integers \(a_1, a_2, \dots, a_n\). You can perform the following operation any number of times (possibly zero): Choose any element \(a_i\) from the array and change its value to any integer between \(0\) and \(a_i\) (inclusive). More formally, if \(a_i < 0\), replace \(a_i\) with any integer i...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 500\)) - the number of test cases. This is followed by their description.The first line of each test case contains the a single integer \(n\) (\(1 \leq n \leq 100\)) β€” the length of the array.The second line of ea...
For each test case: The first line must contain the minimum number of operations \(k\) (\(0 \leq k \leq n\)). The \(j\)-th of the next \(k\) lines must contain two integers \(i\) and \(x\), which represent the \(j\)-th operation. That operation consists in replacing \(a_i\) with \(x\).
In the first test case, we can change the value of the first integer into \(0\) and the product will become \(0\), which is the minimum possible.In the second test case, initially, the product of integers is equal to \(2 \cdot 8 \cdot (-1) \cdot 3 = -48\) which is the minimum possible, so we should do nothing in this c...
Input: 4 1 155 4 2 8 -1 3 4 -1 0 -2 -5 4 -15 -75 -25 -30 | Output: 1 1 0 0 0 1 3 0
Beginner
2
702
414
286
19
485
B
485B
B. Valuable Resources
1,300
brute force; greedy
Many computer strategy games require building cities, recruiting army, conquering tribes, collecting resources. Sometimes it leads to interesting problems. Let's suppose that your task is to build a square city. The world map uses the Cartesian coordinates. The sides of the city should be parallel to coordinate axes. T...
The first line of the input contains number n β€” the number of mines on the map (2 ≀ n ≀ 1000). Each of the next n lines contains a pair of integers xi and yi β€” the coordinates of the corresponding mine ( - 109 ≀ xi, yi ≀ 109). All points are pairwise distinct.
Print the minimum area of the city that can cover all the mines with valuable resources.
Input: 20 02 2 | Output: 4
Easy
2
807
260
88
4
2,118
D1
2118D1
D1. Red Light, Green Light (Easy version)
1,700
brute force; dfs and similar; dp; graphs; implementation; number theory
This is the easy version of the problem. The only difference is the constraint on \(k\) and the total sum of \(n\) and \(q\) across all test cases. You can make hacks only if both versions of the problem are solved.You are given a strip of length \(10^{15}\) and a constant \(k\). There are exactly \(n\) cells that cont...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(k\) (\(\mathbf{1 \le n \le 500}\) and \(\mathbf{1 \le k \le 500}\)) β€” the number of traffic lig...
For each test case, output \(q\) lines. Each line should contain ""YES"" if you will eventually leave the strip 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, the following happens at starting positions \(1\), \(2\), and \(3\): And the following in the second test case at starting position \(2\):
Input: 42 21 41 031 2 39 41 2 3 4 5 6 7 8 93 2 1 0 1 3 3 1 152 5 6 7 84 21 2 3 40 0 0 041 2 3 43 41 2 33 1 131 2 3 | Output: YES NO YES YES YES YES NO NO YES YES NO NO YES NO YES
Medium
6
1,011
1,016
295
21
960
G
960G
G. Bandit Blues
2,900
combinatorics; dp; fft; math
Japate, while traveling through the forest of Mala, saw N bags of gold lying in a row. Each bag has some distinct weight of gold between 1 to N. Japate can carry only one bag of gold with him, so he uses the following strategy to choose a bag.Initially, he starts with an empty bag (zero weight). He considers the bags i...
The only line of input contains three space separated integers N (1 ≀ N ≀ 105), A and B (0 ≀ A, B ≀ N).
Output a single integer β€” the number of valid permutations modulo 998244353.
In sample case 1, the only possible permutation is [1]In sample cases 2 and 3, only two permutations of size 2 are possible:{[1, 2], [2, 1]}. The values of a and b for first permutation is 2 and 1, and for the second permutation these values are 1 and 2. In sample case 4, out of 120 permutations of [1, 2, 3, 4, 5] poss...
Input: 1 1 1 | Output: 1
Master
4
884
103
76
9
1,644
D
1644D
D. Cross Coloring
1,700
data structures; implementation; math
There is a sheet of paper that can be represented with a grid of size \(n \times m\): \(n\) rows and \(m\) columns of cells. All cells are colored in white initially.\(q\) operations have been applied to the sheet. The \(i\)-th of them can be described as follows: \(x_i\) \(y_i\) β€” choose one of \(k\) non-white colors ...
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 four integers \(n, m, k\) and \(q\) (\(1 \le n, m, k, q \le 2 \cdot 10^5\)) β€” the size of the sheet, the number of non-white colors and the number of operations.The \(i\)-th of the fol...
For each testcase, print a single integer β€” the number of different colorings modulo \(998\,244\,353\).
Input: 21 1 3 21 11 12 2 2 32 11 12 2 | Output: 3 4
Medium
3
742
587
103
16
1,844
H
1844H
H. Multiple of Three Cycles
3,400
combinatorics; data structures; dp; dsu; math
An array \(a_1,\dots,a_n\) of length \(n\) is initially all blank. There are \(n\) updates where one entry of \(a\) is updated to some number, such that \(a\) becomes a permutation of \(1,2,\dots,n\) after all the updates.After each update, find the number of ways (modulo \(998\,244\,353\)) to fill in the remaining bla...
The first line contains a single integer \(n\) (\(3 \le n \le 3 \cdot 10^5\), \(n \equiv 0 \pmod 3\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\), representing that the \(i\)-th update changes \(a_{x_i}\) to \(y_i\).It is guaranteed that \(x_1,\dots,x_n\) and \(y_1,\dots,y_n\) are per...
Output \(n\) lines: the number of ways (modulo \(998\,244\,353\)) after the first \(1,2,\dots,n\) updates.
In the first sample, for example, after the \(3\)rd update the \(3\) ways to complete the permutation \(a = [4,\_,2,5,\_,\_]\) are as follows: \([4,1,2,5,6,3]\): The only cycle is \((1\,4\,5\,6\,3\,2)\), with length \(6\). \([4,6,2,5,1,3]\): The cycles are \((1\,4\,5)\) and \((2\,6\,3)\), with lengths \(3\) and \(3\). ...
Input: 6 3 2 1 4 4 5 2 6 5 1 6 3 | Output: 32 8 3 2 1 1
Master
5
877
424
106
18
864
D
864D
D. Make a Permutation!
1,500
greedy; implementation; math
Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n.Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from...
The first line contains an single integer n (2 ≀ n ≀ 200 000) β€” the number of elements in Ivan's array.The second line contains a sequence of integers a1, a2, ..., an (1 ≀ ai ≀ n) β€” the description of Ivan's array.
In the first line print q β€” the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with q changes.
In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β€” this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to ...
Input: 43 2 2 3 | Output: 21 2 4 3
Medium
3
980
214
257
8
1,221
A
1221A
A. 2048 Game
1,000
brute force; greedy; math
You are playing a variation of game 2048. Initially you have a multiset \(s\) of \(n\) integers. Every integer in this multiset is a power of two. You may perform any number (possibly, zero) operations with this multiset.During each operation you choose two equal integers from \(s\), remove them from \(s\) and insert t...
The first line contains one integer \(q\) (\(1 \le q \le 100\)) – the number of queries.The first line of each query contains one integer \(n\) (\(1 \le n \le 100\)) β€” the number of elements in multiset.The second line of each query contains \(n\) integers \(s_1, s_2, \dots, s_n\) (\(1 \le s_i \le 2^{29}\)) β€” the descr...
For each query print YES if it is possible to obtain the number \(2048\) in your multiset, and NO otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
In the first query you can win as follows: choose \(512\) and \(512\), and \(s\) turns into \(\{1024, 64, 1024\}\). Then choose \(1024\) and \(1024\), and \(s\) turns into \(\{2048, 64\}\) and you win.In the second query \(s\) contains \(2048\) initially.
Input: 6 4 1024 512 64 512 1 2048 3 64 512 2 2 4096 4 7 2048 2 2048 2048 2048 2048 2048 2 2048 4096 | Output: YES YES NO NO YES YES
Beginner
3
883
413
251
12
1,954
A
1954A
A. Painting the Ribbon
900
constructive algorithms; greedy; math
Alice and Bob have bought a ribbon consisting of \(n\) parts. Now they want to paint it.First, Alice will paint every part of the ribbon into one of \(m\) colors. For each part, she can choose its color arbitrarily.Then, Bob will choose at most \(k\) parts of the ribbon and repaint them into the same color (he chooses ...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Each test case consists of one line containing three integers \(n\), \(m\) and \(k\) (\(1 \le m, k \le n \le 50\)) β€” the number of parts, the number of colors and the number of parts Bob can repaint, respectively.
For each test case, print YES if Alice can paint the ribbon so that Bob cannot make all parts have the same color. Otherwise, print NO.You can print every letter in any register. For example, Yes, yes, yEs will all be recognized as positive answer.
In the first test case, a ribbon consists of \(1\) part. So all its parts will always have the same color.In the second test case, there is only \(1\) color.In the third test case, Alice can paint the ribbon as follows: \([1, 2, 1, 2, 1]\). It's impossible to change the color of at most \(1\) part so that all parts hav...
Input: 51 1 15 1 15 2 15 2 25 5 3 | Output: NO NO YES NO YES
Beginner
3
607
305
248
19
780
H
780H
H. Intranet of Buses
3,100
binary search; geometry; implementation; two pointers
A new bus route is opened in the city . The route is a closed polygon line in the place, with all segments parallel to one of the axes. m buses will operate on the route. All buses move in a loop along the route in the same direction with equal constant velocities (stopping times are negligible in this problem).Buses s...
The first line contains two integers n and m (2 ≀ n, m ≀ 105) β€” the number of vertices of the polygonal line, and the number of buses respectively.Next n lines describe the vertices of the route in the traversing order. Each of these lines contains two integers xi, yi ( - 1000 ≀ xi, yi ≀ 1000) β€” coordinates of respecti...
Print one real number β€” the answer to the problem. Your answer will be accepted if the relative or the absolute error doesn't exceed 10 - 6.
Suppose that each bus travel 1 distance unit per second.In the first sample case, in 0.5 seconds buses will be at distance 1, hence we can choose D = 1.In the second sample case, in 0.5 seconds both buses will be at (0.5, 0), hence we can choose D = 0.
Input: 4 20 00 11 11 0 | Output: 1.000000000
Master
4
1,497
633
140
7
1,055
D
1055D
D. Refactoring
2,400
greedy; implementation; strings
Alice has written a program and now tries to improve its readability. One of the ways to improve readability is to give sensible names to the variables, so now Alice wants to rename some variables in her program. In her IDE there is a command called ""massive refactoring"", which can replace names of many variable in j...
The first line contains the only integer \(n\) (\(1 \le n \le 3000\)) β€” the number of variables in Alice's program.The following \(n\) lines contain the initial names of variables \(w_1, w_2, \ldots, w_n\), one per line. After that, \(n\) more lines go, the \(i\)-th of them contains the target name \(w'_i\) for the \(i...
If it is impossible to rename all variables with one call of ""massive refactoring"", print ""NO"" (quotes for clarity).Otherwise, on the first line print ""YES"" (quotes for clarity) and on the following lines print \(s\) and \(t\) (\(1 \le |s|, |t| \le 5000\)), which should be used for replacement. Strings \(s\) and ...
Input: 1topforcescodecoder | Output: YEStopforcescodecoder
Expert
3
1,090
665
476
10
279
B
279B
B. Books
1,400
binary search; brute force; implementation; two pointers
When Valera has got some free time, he goes to the library to read some books. Today he's got t free minutes to read. That's why Valera took n books in the library and for each book he estimated the time he is going to need to read it. Let's number the books by integers from 1 to n. Valera needs ai minutes to read the ...
The first line contains two integers n and t (1 ≀ n ≀ 105; 1 ≀ t ≀ 109) β€” the number of books and the number of free minutes Valera's got. The second line contains a sequence of n integers a1, a2, ..., an (1 ≀ ai ≀ 104), where number ai shows the number of minutes that the boy needs to read the i-th book.
Print a single integer β€” the maximum number of books Valera can read.
Input: 4 53 1 2 1 | Output: 3
Easy
4
842
306
69
2
1,850
D
1850D
D. Balanced Round
900
brute force; greedy; implementation; sortings
You are the author of a Codeforces round and have prepared \(n\) problems you are going to set, problem \(i\) having difficulty \(a_i\). You will do the following process: remove some (possibly zero) problems from the list; rearrange the remaining problems in any order you wish. A round is considered balanced if and on...
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The first line of each test case contains two positive integers \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) and \(k\) (\(1 \leq k \leq 10^9\)) β€” the number of problems, and the maximum allowed absolute difference between conse...
For each test case, output a single integer β€” the minimum number of problems you have to remove so that an arrangement of problems is balanced.
For the first test case, we can remove the first \(2\) problems and construct a set using problems with the difficulties \([4, 5, 6]\), with difficulties between adjacent problems equal to \(|5 - 4| = 1 \leq 1\) and \(|6 - 5| = 1 \leq 1\).For the second test case, we can take the single problem and compose a round usin...
Input: 75 11 2 4 5 61 2108 317 3 1 20 12 5 17 124 22 4 6 85 32 3 19 10 83 41 10 58 18 3 1 4 5 10 7 3 | Output: 2 0 5 0 3 1 4
Beginner
4
554
557
143
18
1,722
A
1722A
A. Spell Check
800
implementation
Timur likes his name. As a spelling of his name, he allows any permutation of the letters of the name. For example, the following strings are valid spellings of his name: Timur, miurT, Trumi, mriTu. Note that the correct spelling must have uppercased T and lowercased other letters.Today he wrote string \(s\) of length ...
The first line of the input contains an integer \(t\) (\(1 \leq t \leq 10^3\)) β€” the number of test cases.The first line of each test case contains an integer \(n\) \((1 \leq n \leq 10)\) β€” the length of string \(s\).The second line of each test case contains a string \(s\) consisting of only uppercase or lowercase Lat...
For each test case, output ""YES"" (without quotes) if \(s\) satisfies the condition, and ""NO"" (without quotes) otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
Input: 105Timur5miurT5Trumi5mriTu5timur4Timr6Timuur10codeforces10TimurTimur5TIMUR | Output: YES YES YES YES NO NO NO NO NO NO
Beginner
1
449
334
267
17
1,218
A
1218A
A. BubbleReactor
2,800
dp; graphs
You are in charge of the BubbleReactor. It consists of \(N\) BubbleCores connected with \(N\) lines of electrical wiring. Each electrical wiring connects two distinct BubbleCores. There are no BubbleCores connected with more than one line of electrical wiring.Your task is to start the BubbleReactor by starting each Bub...
First line contains one integer \(N (3 \leq N \leq 15.000)\), the number of BubbleCores.The following N lines contain two integers \(U, V (0 \leq U \neq V < N)\) denoting that there exists electrical wiring between BubbleCores \(U\) and \(V\).
Single integer, the maximum sum of all BubbleCores' potentials.
If we start by kickstarting BubbleCup 8 and then turning on cores 7, 2, 1, 3, 0, 9, 4, 5, 6 in that order we get potentials 10 + 9 + 8 + 7 + 6 + 5 + 1 + 3 + 1 + 1 = 51
Input: 10 0 1 0 3 0 4 0 9 1 2 2 3 2 7 4 5 4 6 7 8 | Output: 51
Master
2
928
243
63
12
2,101
D
2101D
D. Mani and Segments
2,500
data structures; implementation; sortings; two pointers
An array \(b\) of length \(|b|\) is cute if the sum of the length of its Longest Increasing Subsequence (LIS) and the length of its Longest Decreasing Subsequence (LDS)\(^{\text{βˆ—}}\) is exactly one more than the length of the array. More formally, the array \(b\) is cute if \(\operatorname{LIS}(b) + \operatorname{LDS}...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1\le n\le 2 \cdot 10^5 \)) β€” the length of permutation \(a\).The second line of each test ...
For each test case, output the number of cute non-empty subarrays of permutation \(a\).
In the first test case, all of the \(6\) non-empty subarrays are cute: \([3]\): \(\operatorname{LIS}([3]) + \operatorname{LDS}([3]) = 1 + 1 = 2\). \([1]\): \(\operatorname{LIS}([1]) + \operatorname{LDS}([1]) = 1 + 1 = 2\). \([2]\): \(\operatorname{LIS}([2]) + \operatorname{LDS}([2]) = 1 + 1 = 2\). \([3, 1]\): \(\operat...
Input: 533 1 252 3 4 5 143 4 1 271 2 3 4 5 6 7107 8 2 4 5 10 1 3 6 9 | Output: 6 15 9 28 36
Expert
4
1,437
525
87
21
804
D
804D
D. Expected diameter of a tree
2,500
binary search; brute force; dfs and similar; dp; sortings; trees
Pasha is a good student and one of MoJaK's best friends. He always have a problem to think about. Today they had a talk about the following problem.We have a forest (acyclic undirected graph) with n vertices and m edges. There are q queries we should answer. In each query two vertices v and u are given. Let V be the se...
The first line contains three integers n, m and q(1 ≀ n, m, q ≀ 105) β€” the number of vertices, the number of edges in the graph and the number of queries.Each of the next m lines contains two integers ui and vi (1 ≀ ui, vi ≀ n), that means there is an edge between vertices ui and vi.It is guaranteed that the given grap...
For each query print the expected value of d as described in the problem statement.Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Let's assume that your answer is a, and the jury's answer is b. The checker program will consider your answer correct, if .
In the first example the vertices 1 and 3 are in the same component, so the answer for the first query is -1. For the second query there are two options to add the edge: one option is to add the edge 1 - 2, the other one is 2 - 3. In both ways the resulting diameter is 2, so the answer is 2.In the second example the an...
Input: 3 1 21 33 12 3 | Output: -12.0000000000
Expert
6
1,100
447
303
8
1,388
E
1388E
E. Uncle Bogdan and Projections
2,700
data structures; geometry; sortings
After returning to shore, uncle Bogdan usually visits the computer club ""The Rock"", to solve tasks in a pleasant company. One day, uncle Bogdan met his good old friend who told him one unusual task...There are \(n\) non-intersecting horizontal segments with ends in integers points on the plane with the standard carte...
The first line contains the single integer \(n\) (\(1 \le n \le 2000\)) β€” the number of segments.The \(i\)-th of the next \(n\) lines contains three integers \(xl_i\), \(xr_i\) and \(y_i\) (\(-10^6 \le xl_i < xr_i \le 10^6\); \(1 \le y_i \le 10^6\)) β€” coordinates of the corresponding segment.It's guaranteed that the se...
Print the minimum possible difference you can get.Your answer will be considered correct if its absolute or relative error doesn't exceed \(10^{-6}\).Formally, if your answer is \(a\) and jury's answer is \(b\) then your answer will be considered correct if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}\).
In the first example if we project segments along the vector \((1, -1)\) then we get an answer \(12-3=9\) and (it can be proven) it is impossible to get less. It is optimal to project along the vector \((1, -3)\) in the second example. The answer is \(8\frac{2}{3}-2\frac{1}{3}=6\frac{1}{3}\)
Input: 3 1 6 2 4 6 4 4 6 6 | Output: 9.000000000
Master
3
785
352
305
13
39
H
39H
H. Multiplication Table
1,300
implementation
Petya studies positional notations. He has already learned to add and subtract numbers in the systems of notations with different radices and has moved on to a more complicated action β€” multiplication. To multiply large numbers one has to learn the multiplication table. Unfortunately, in the second grade students learn...
The first line contains a single integer k (2 ≀ k ≀ 10) β€” the radix of the system.
Output the multiplication table for the system of notations with the radix k. The table must contain k - 1 rows and k - 1 columns. The element on the crossing of the i-th row and the j-th column is equal to the product of i and j in the system of notations with the radix k. Each line may have any number of spaces betwe...
Input: 10 | Output: 1 2 3 4 5 6 7 8 92 4 6 8 10 12 14 16 183 6 9 12 15 18 21 24 274 8 12 16 20 24 28 32 365 10 15 20 25 30 35 40 456 12 18 24 30 36 42 48 547 14 21 28 35 42 49 56 638 16 24 32 40 48 56 64 729 18 27 36 45 54 63 72 81
Easy
1
512
82
389
0
1,677
C
1677C
C. Tokitsukaze and Two Colorful Tapes
1,900
constructive algorithms; dfs and similar; graphs; greedy
Tokitsukaze has two colorful tapes. There are \(n\) distinct colors, numbered \(1\) through \(n\), and each color appears exactly once on each of the two tapes. Denote the color of the \(i\)-th position of the first tape as \(ca_i\), and the color of the \(i\)-th position of the second tape as \(cb_i\).Now Tokitsukaze ...
The first contains a single positive integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.For each test case, the first line contains a single integer \(n\) (\(1\leq n \leq 10^5\)) β€” the number of colors.The second line contains \(n\) integers \(ca_1, ca_2, \ldots, ca_n\) (\(1 \leq ca_i \leq n\)) β€” the col...
For each test case, print a single integer β€” the highest possible beauty.
An optimal solution for the first test case is shown in the following figure: The beauty is \(\left|4-3 \right|+\left|3-5 \right|+\left|2-4 \right|+\left|5-2 \right|+\left|1-6 \right|+\left|6-1 \right|=18\).An optimal solution for the second test case is shown in the following figure: The beauty is \(\left|2-2 \right|+...
Input: 361 5 4 3 2 65 3 1 4 6 263 5 4 6 2 13 6 4 5 2 1111 | Output: 18 10 0
Hard
4
1,191
687
73
16
2,002
G
2002G
G. Lattice Optimizing
3,400
bitmasks; brute force; hashing; meet-in-the-middle
Consider a grid graph with \(n\) rows and \(n\) columns. Let the cell in row \(x\) and column \(y\) be \((x,y)\). There exists a directed edge from \((x,y)\) to \((x+1,y)\), with non-negative integer value \(d_{x,y}\), for all \(1\le x < n, 1\le y \le n\), and there also exists a directed edge from \((x,y)\) to \((x,y+...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le100\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(2\le n\le20\)) β€” the number of rows and columns.Each of the next \(n-1\) lines contains \(n\) ...
For each test case, print a single integer β€” the maximum MEX of \(S\) when you reach \((n,n)\).
In the first test case, the grid graph and one of the optimal paths are as follows: In the second test case, the grid graph and one of the optimal paths are as follows:
Input: 231 0 20 1 32 10 33 031 2 00 1 22 01 20 1 | Output: 3 2
Master
4
1,093
600
95
20
2,097
E
2097E
E. Clearing the Snowdrift
3,100
data structures; dfs and similar; dp; greedy
Boy Vasya loves to travel very much. In particular, flying in airplanes brings him extraordinary pleasure. He was about to fly to another city, but the runway was heavily covered with snow and needed to be cleared.The runway can be represented as \(n\) consecutive sections numbered from \(1\) to \(n\). The snowstorm wa...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2 \cdot 10^5\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(d\) (\(1 \le n \le 5 \cdot 10^5, 1 \le d \le n\)) β€” the number of sections on the r...
For each test case, output a single integer β€” the minimum number of minutes required for the snowplow to achieve \(a_i = 0\) for all \(i\) from \(1\) to \(n\).
In the first test case, there is an optimal sequence of operations. First, select the segment \([2, 3]\) four times. After three operations, \(a_2\) will turn into \(2\), and the array \(a\) will look like \([1, 2, 2, 1, 2]\). After the fourth operation, the array \(a\) will become \([1, 1, 1, 1, 2]\). Next, the array ...
Input: 25 21 5 2 1 23 11000000000 1000000000 1000000000 | Output: 8 3000000000
Master
4
1,311
665
159
20
1,656
D
1656D
D. K-good
1,900
constructive algorithms; math; number theory
We say that a positive integer \(n\) is \(k\)-good for some positive integer \(k\) if \(n\) can be expressed as a sum of \(k\) positive integers which give \(k\) distinct remainders when divided by \(k\).Given a positive integer \(n\), find some \(k \geq 2\) so that \(n\) is \(k\)-good or tell that such a \(k\) does no...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β€” the number of test cases.Each test case consists of one line with an integer \(n\) (\(2 \leq n \leq 10^{18}\)).
For each test case, print a line with a value of \(k\) such that \(n\) is \(k\)-good (\(k \geq 2\)), or \(-1\) if \(n\) is not \(k\)-good for any \(k\). If there are multiple valid values of \(k\), you can print any of them.
\(6\) is a \(3\)-good number since it can be expressed as a sum of \(3\) numbers which give different remainders when divided by \(3\): \(6 = 1 + 2 + 3\).\(15\) is also a \(3\)-good number since \(15 = 1 + 5 + 9\) and \(1, 5, 9\) give different remainders when divided by \(3\).\(20\) is a \(5\)-good number since \(20 =...
Input: 5 2 4 6 15 20 | Output: -1 -1 3 3 5
Hard
3
328
228
224
16
369
C
369C
C. Valera and Elections
1,600
dfs and similar; graphs; trees
The city Valera lives in is going to hold elections to the city Parliament.The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in some way by integers from 1 to n, inclusive. Furthermore, for each roa...
The first line contains a single integer n (2 ≀ n ≀ 105) β€” the number of districts in the city.Then n - 1 lines follow. Each line contains the description of a city road as three positive integers xi, yi, ti (1 ≀ xi, yi ≀ n, 1 ≀ ti ≀ 2) β€” the districts connected by the i-th bidirectional road and the road type. If ti e...
In the first line print a single non-negative number k β€” the minimum size of the required subset of candidates. Then on the second line print k space-separated integers a1, a2, ... ak β€” the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them.
Input: 51 2 22 3 23 4 24 5 2 | Output: 15
Medium
3
1,070
500
317
3
2,120
E
2120E
E. Lanes of Cars
2,300
binary search; dp; ternary search
Harshith is the president of TollClub. He tasks his subordinate Aryan to oversee a toll plaza with \(n\) lanes. Initially, the \(i\)-th lane has \(a_i\) cars waiting in a queue. Exactly one car from the front of each lane passes through the toll every second.The angriness of a car is defined as the number of seconds it...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le k \le 10^6\)) β€” the number of lanes and the incr...
For each test case, output a single integer in a new line, denoting the minimum total angriness.
In the first test case, Aryan shifts two cars from lane \(1\) to lane \(3\), after which the array becomes \([11, 7, 6]\). The total angriness is \(\frac{11\cdot 12}{2}+\frac{7\cdot 8}{2}+\frac{6\cdot 7}{2}+2\cdot 4=123\). It can be proven that this is the minimum possible angriness.In the fourth test case, there is on...
Input: 63 413 7 44 96 12 14 55 32 4 13 5 101 7613 410 26 34 39 9 43 48 41 1 38 13 4 4616 3176342 171863 70145 80835 160257 136105 78541 100795 114461 45482 68210 51656 29593 8750 173743 156063 | Output: 123 219 156 21 5315 82302351405
Expert
3
1,182
713
96
21
407
A
407A
A. Triangle
1,600
brute force; geometry; implementation; math
There is a right triangle with legs of length a and b. Your task is to determine whether it is possible to locate the triangle on the plane in such a way that none of its sides is parallel to the coordinate axes. All the vertices must have integer coordinates. If there exists such a location, you have to output the app...
The first line contains two integers a, b (1 ≀ a, b ≀ 1000), separated by a single space.
In the first line print either ""YES"" or ""NO"" (without the quotes) depending on whether the required location exists. If it does, print in the next three lines three pairs of integers β€” the coordinates of the triangle vertices, one pair per line. The coordinates must be integers, not exceeding 109 in their absolute ...
Input: 1 1 | Output: NO
Medium
4
353
89
326
4
620
C
620C
C. Pearls in a Row
1,500
greedy
There are n pearls in a row. Let's enumerate them with integers from 1 to n from the left to the right. The pearl number i has the type ai.Let's call a sequence of consecutive pearls a segment. Let's call a segment good if it contains two pearls of the same type.Split the row of the pearls to the maximal number of good...
The first line contains integer n (1 ≀ n ≀ 3Β·105) β€” the number of pearls in a row.The second line contains n integers ai (1 ≀ ai ≀ 109) – the type of the i-th pearl.
On the first line print integer k β€” the maximal number of segments in a partition of the row.Each of the next k lines should contain two integers lj, rj (1 ≀ lj ≀ rj ≀ n) β€” the number of the leftmost and the rightmost pearls in the j-th segment.Note you should print the correct partition of the row of the pearls, so ea...
Input: 51 2 3 4 1 | Output: 11 5
Medium
1
641
165
590
6
66
B
66B
B. Petya and Countryside
1,100
brute force; implementation
Little Petya often travels to his grandmother in the countryside. The grandmother has a large garden, which can be represented as a rectangle 1 Γ— n in size, when viewed from above. This rectangle is divided into n equal square sections. The garden is very unusual as each of the square sections possesses its own fixed h...
The first line contains a positive integer n (1 ≀ n ≀ 1000). The second line contains n positive integers which are the height of the sections. All the numbers are no less than 1 and not more than 1000.
Print a single number, the maximal number of watered sections if we create artificial rain above exactly one section.
Input: 12 | Output: 1
Easy
2
1,212
202
117
0
331
C1
331C1
C1. The Great Julya Calendar
1,100
dp
Yet another Armageddon is coming! This time the culprit is the Julya tribe calendar. The beavers in this tribe knew math very well. Smart Beaver, an archaeologist, got a sacred plate with a magic integer on it. The translation from Old Beaverish is as follows: ""May the Great Beaver bless you! May your chacres open and...
The single line contains the magic integer n, 0 ≀ n. to get 20 points, you need to solve the problem with constraints: n ≀ 106 (subproblem C1); to get 40 points, you need to solve the problem with constraints: n ≀ 1012 (subproblems C1+C2); to get 100 points, you need to solve the problem with constraints: n ≀ 1018 (sub...
Print a single integer β€” the minimum number of subtractions that turns the magic number to a zero.
In the first test sample the minimum number of operations can be reached by the following sequence of subtractions: 24 β†’ 20 β†’ 18 β†’ 10 β†’ 9 β†’ 0
Input: 24 | Output: 5
Easy
1
891
339
98
3
1,462
D
1462D
D. Add to Neighbour and Remove
1,400
greedy; math; number theory
Polycarp was given an array of \(a[1 \dots n]\) of \(n\) integers. He can perform the following operation with the array \(a\) no more than \(n\) times: Polycarp selects the index \(i\) and adds the value \(a_i\) to one of his choice of its neighbors. More formally, Polycarp adds the value of \(a_i\) to \(a_{i-1}\) or ...
The first line contains a single integer \(t\) (\(1 \leq t \leq 3000\)) β€” the number of test cases in the test. Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 3000\)) β€” the length of the array. The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\...
For each test case, output a single number β€” the minimum number of operations that Polycarp needs to perform so that all elements of the \(a\) array are the same (equal).
In the first test case of the example, the answer can be constructed like this (just one way among many other ways):\([3, 1, 6, 6, 2]\) \(\xrightarrow[]{i=4,~add~to~left}\) \([3, 1, 12, 2]\) \(\xrightarrow[]{i=2,~add~to~right}\) \([3, 13, 2]\) \(\xrightarrow[]{i=1,~add~to~right}\) \([16, 2]\) \(\xrightarrow[]{i=2,~add~...
Input: 4 5 3 1 6 6 2 4 1 2 2 1 3 2 2 2 4 6 3 2 1 | Output: 4 2 0 2
Easy
3
1,348
447
170
14
1,860
D
1860D
D. Balanced String
2,200
dp
You are given a binary string \(s\) (a binary string is a string consisting of characters 0 and/or 1).Let's call a binary string balanced if the number of subsequences 01 (the number of indices \(i\) and \(j\) such that \(1 \le i < j \le n\), \(s_i=0\) and \(s_j=1\)) equals to the number of subsequences 10 (the number ...
The only line contains the string \(s\) (\(3 \le |s| \le 100\)) consisting of characters 0 and/or 1. Additional constraint on the input: the string \(s\) can be made balanced.
Print a single integer β€” the minimum number of swap operations to make the string \(s\) balanced.
In the first example, the string is already balanced, the number of both 01 and 10 is equal to \(1\).In the second example, the string is already balanced, the number of both 01 and 10 is equal to \(6\).In the third example, one of the possible answers is the following one: 11010 \(\rightarrow\) 01110. After that, the ...
Input: 101 | Output: 0
Hard
1
887
175
97
18
73
F
73F
F. Plane of Tanks
2,900
brute force; geometry
Vasya plays the Plane of Tanks. The tanks in this game keep trying to finish each other off. But your ""Pedalny"" is not like that... He just needs to drive in a straight line from point A to point B on the plane. Unfortunately, on the same plane are n enemy tanks. We shall regard all the tanks as points. At the initia...
The first line contains 4 numbers – the coordinates of points A and B (in meters), the points do not coincide. On the second line number n is given (1 ≀ n ≀ 104). It is the number of enemy tanks. Each of the following n lines contain the coordinates of a corresponding tank xi, yi and its parameters ai and wi (0 ≀ ai ≀ ...
Print a single number with absolute or relative error no more than 10 - 4 β€” the minimum required speed of Pedalny in meters per second.
Input: 0 0 10 015 -5 4.71238 10 | Output: 4.2441
Master
2
1,240
864
135
0
1,299
C
1299C
C. Water Balance
2,100
data structures; geometry; greedy
There are \(n\) water tanks in a row, \(i\)-th of them contains \(a_i\) liters of water. The tanks are numbered from \(1\) to \(n\) from left to right.You can perform the following operation: choose some subsegment \([l, r]\) (\(1\le l \le r \le n\)), and redistribute water in tanks \(l, l+1, \dots, r\) evenly. In othe...
The first line contains an integer \(n\) (\(1 \le n \le 10^6\)) β€” the number of water tanks.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)) β€” initial volumes of water in the water tanks, in liters.Because of large input, reading input as doubles is not recommended.
Print the lexicographically smallest sequence you can get. In the \(i\)-th line print the final volume of water in the \(i\)-th tank.Your answer is considered correct if the absolute or relative error of each \(a_i\) does not exceed \(10^{-9}\).Formally, let your answer be \(a_1, a_2, \dots, a_n\), and the jury's answe...
In the first sample, you can get the sequence by applying the operation for subsegment \([1, 3]\).In the second sample, you can't get any lexicographically smaller sequence.
Input: 4 7 5 5 7 | Output: 5.666666667 5.666666667 5.666666667 7.000000000
Hard
3
970
307
458
12
383
C
383C
C. Propagating tree
2,000
data structures; dfs and similar; trees
Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value ai. The root of the tree is node 1.This tree has a special property: when a value val is added to a value of node i, the value -val is ad...
The first line contains two integers n and m (1 ≀ n, m ≀ 200000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000). Each of the next n–1 lines contains two integers vi and ui (1 ≀ vi, ui ≀ n), meaning that there is an edge between nodes vi and ui.Each of the next m lines contains a query in the forma...
For each query of type two (print the value of node x) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input.
The values of the nodes are [1, 2, 1, 1, 2] at the beginning.Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1, 5, 1, - 2, - 1].Then value 2 is added to node 1. It propagates and value -2 is added to...
Input: 5 51 2 1 1 21 21 32 42 51 2 31 1 22 12 22 4 | Output: 330
Hard
3
790
435
175
3
1,971
F
1971F
F. Circle Perimeter
1,600
binary search; brute force; dfs and similar; geometry; implementation; math
Given an integer \(r\), find the number of lattice points that have a Euclidean distance from \((0, 0)\) greater than or equal to \(r\) but strictly less than \(r+1\).A lattice point is a point with integer coordinates. The Euclidean distance from \((0, 0)\) to the point \((x,y)\) is \(\sqrt{x^2 + y^2}\).
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.The only line of each test case contains a single integer \(r\) (\(1 \leq r \leq 10^5\)).The sum of \(r\) over all test cases does not exceed \(10^5\).
For each test case, output a single integer β€” the number of lattice points that have an Euclidean distance \(d\) from \((0, 0)\) such that \(r \leq d < r+1\).
The points for the first three test cases are shown below.
Input: 6123451984 | Output: 8 16 20 24 40 12504
Medium
6
306
250
158
19
620
E
620E
E. New Year Tree
2,100
bitmasks; data structures; trees
The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited his best friends Kerim and Gural to help him to redecorate the New Year tree.The New Year tree is an undirected tree with n vertices and root in the vertex 1.You should process the queries of the two types: Change the col...
The first line contains two integers n, m (1 ≀ n, m ≀ 4Β·105) β€” the number of vertices in the tree and the number of the queries.The second line contains n integers ci (1 ≀ ci ≀ 60) β€” the colour of the i-th vertex.Each of the next n - 1 lines contains two integers xj, yj (1 ≀ xj, yj ≀ n) β€” the vertices of the j-th edge....
For each query of the second type print the integer a β€” the number of different colours in the subtree of the vertex given in the query.Each of the numbers should be printed on a separate line in order of query appearing in the input.
Input: 7 101 1 1 1 1 1 11 21 31 43 53 63 71 3 22 11 4 32 11 2 52 11 6 42 12 22 3 | Output: 234512
Hard
3
457
864
234
6
2,092
F
2092F
F. Andryusha and CCB
2,900
brute force; constructive algorithms; greedy; math; number theory; strings
Let us define the beauty of a binary string \(z\) as the number of indices \(i\) such that \(1 \le i < |z|\) and \(z_i \neq z_{i+1}\).While waiting for his friends from the CCB, Andryusha baked a pie, represented by a binary string \(s\) of length \(n\). To avoid offending anyone, he wants to divide this string into \(...
Each test consists of several test cases. The first line of the input data contains one integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^6\)) β€” the length of the binary string.T...
For each test case, output a single line containing \(n\) integers \(c_i\) (\(0 \le c_i \le n\)) β€” the number of values of \(k\) for which it is possible to split the prefix \(s_1 s_2 \ldots s_i\) into exactly \(k\) parts with equal beauties.
In the third case, the values of \(k\) that satisfy the conditions are: \(i = 1\): \(k \in \{1\}\), \(i = 2\): \(k \in \{1, 2\}\), \(i = 3\): \(k \in \{1, 2, 3\}\), \(i = 4\): \(k \in \{1, 3, 4\}\), \(i = 5\): \(k \in \{1, 2, 4, 5\}\), \(i = 6\): \(k \in \{1, 5, 6\}\), \(i = 7\): \(k \in \{1, 5, 6, 7\}\).
Input: 350001110010101010170010100 | Output: 1 2 3 4 5 1 2 2 3 2 4 2 4 3 4 1 2 3 3 4 3 4
Master
6
1,077
515
242
20
1,932
F
1932F
F. Feed Cats
1,900
data structures; dp; sortings
There is a fun game where you need to feed cats that come and go. The level of the game consists of \(n\) steps. There are \(m\) cats; the cat \(i\) is present in steps from \(l_i\) to \(r_i\), inclusive. In each step, you can feed all the cats that are currently present or do nothing. If you feed the same cat more tha...
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^6\), \(1 \le m\le 2\cdot 10^5\)). The \(i\)-th of the next \(m\) lines contai...
For each test case, print a single integer, the maximum number of cats you can feed.
In the first example, one of the ways to feed five cats is to feed at steps \(4\) and \(11\). At step \(4\), cats \(1\), \(2\), and \(3\) will be fed. At step \(11\), cats \(5\) and \(6\) will be fed.
Input: 315 62 103 52 47 78 1211 111000 11 10005 101 23 43 43 43 41 11 23 33 43 4 | Output: 5 1 10
Hard
3
756
512
84
19
679
C
679C
C. Bear and Square Grid
2,400
dfs and similar; dsu; implementation
You have a grid with n rows and n columns. Each cell is either empty (denoted by '.') or blocked (denoted by 'X').Two empty cells are directly connected if they share a side. Two cells (r1, c1) (located in the row r1 and column c1) and (r2, c2) are connected if there exists a sequence of empty cells that starts with (r...
The first line of the input contains two integers n and k (1 ≀ k ≀ n ≀ 500) β€” the size of the grid and Limak's range, respectively.Each of the next n lines contains a string with n characters, denoting the i-th row of the grid. Each character is '.' or 'X', denoting an empty cell or a blocked one, respectively.
Print the maximum possible size (the number of cells) of the biggest connected component, after using Limak's help.
In the first sample, you can choose a square of size 2 Γ— 2. It's optimal to choose a square in the red frame on the left drawing below. Then, you will get a connected component with 10 cells, marked blue in the right drawing.
Input: 5 2..XXXXX.XXX.XXXX...XXXXX. | Output: 10
Expert
3
1,147
312
115
6
154
A
154A
A. Hometask
1,600
greedy
Sergey attends lessons of the N-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the N-ish language. Sentences of the N-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks.Sergey totally forgot about th...
The first line contains a non-empty string s, consisting of lowercase Latin letters β€” that's the initial sentence in N-ish, written by Sergey. The length of string s doesn't exceed 105.The next line contains integer k (0 ≀ k ≀ 13) β€” the number of forbidden pairs of letters.Next k lines contain descriptions of forbidden...
Print the single number β€” the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters.
In the first sample you should remove two letters b.In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution.
Input: ababa1ab | Output: 2
Medium
1
1,481
530
238
1
1,466
G
1466G
G. Song of the Sirens
2,600
combinatorics; divide and conquer; hashing; math; string suffix structures; strings
Whoso in ignorance draws near to them and hears the Sirens' voice, he nevermore returns.Homer, OdysseyIn the times of Jason and the Argonauts, it was well known that sirens use the sound of their songs to lure sailors into their demise. Yet only a few knew that every time sirens call a sailor by his name, his will weak...
In the first line of input there are two integers \(n\), \(q\) ( \( 1 \leq n, q \leq 10^5\)) meaning that there are \(n+1\) songs and \(q\) queries. In the next two lines strings \(s_0\) and \(t\) follow (\(1 \leq |s_0| \leq 100, |t| = n\)). Next \(q\) lines describe the queries; each one contains an integer \(k\) (\( ...
Output \(q\) lines, \(i\)-th of them should contain the remainder modulo \(10^9+7\) of the number of occurrences of \(w\) in \(s_k\).
In the first example songs of the sirens are as follows: Song \(0\): aa Song \(1\): aabaa Song \(2\): aabaacaabaa Song \(3\): aabaacaabaadaabaacaabaa
Input: 3 3 aa bcd 2 aba 3 ca 3 aa | Output: 2 2 8
Expert
6
1,331
580
133
14
1,593
D2
1593D2
D2. Half of Same
1,900
brute force; math; number theory
This problem is a complicated version of D1, but it has significant differences, so read the whole statement.Polycarp has an array of \(n\) (\(n\) is even) integers \(a_1, a_2, \dots, a_n\). Polycarp conceived of a positive integer \(k\). After that, Polycarp began performing the following operations on the array: take...
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 two lines. The first line contains an even integer \(n\) (\(4 \le n \le 40\)) (\(n\) is even). The second line contains \(n\) integers \(a_1, a_2, \dots a_n\) (\(-10^6 \le a...
For each test case output on a separate line an integer \(k\) (\(k \ge 1\)) β€” the maximum possible number that Polycarp used in operations on the array, or \(-1\), if such a number can be arbitrarily large.
Input: 4 6 48 13 22 -15 16 35 8 -1 0 1 -1 0 1 -1 0 4 100 -1000 -1000 -1000 4 1 1 1 1 | Output: 13 2 -1 -1
Hard
3
666
436
206
15
1,951
E
1951E
E. No Palindromes
2,000
brute force; constructive algorithms; divide and conquer; greedy; hashing; implementation; math; strings
Christopher Tin ft. Soweto Gospel Choir - Baba Yetu࢞You are given a string \(s\) consisting of lowercase Latin characters. You need to partition\(^\dagger\) this string into some substrings, such that each substring is not a palindrome\(^\ddagger\).\(^\dagger\) A partition of a string \(s\) is an ordered sequence of so...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Each test case contains a string \(s\) consisting of lowercase Latin characters (\(1 \le |s| \le 10^6\)).It is guaranteed that the sum of \(|s|\) over all test cases does not exceed \(10^6\...
For each test case, print on one line ""YES"" if there exists a partition of \(s\) whose parts are not palindromes, or ""NO"" if there is no such partition.If the answer is ""YES"", on the second line, print an integer \(k\) β€” the number of parts that \(s\) needs to be partitioned to such that each part is not a palind...
In the first test case, since \(\mathtt{sinktheyacht}\) is already non-palindrome, the partition \([\mathtt{sinktheyacht}]\) is valid.In the second test case, as any substring of the string \(s\) is palindrome, there are no valid partitions.In the third test case, another valid partition is \([\mathtt{uw},\mathtt{uo}, ...
Input: 3sinktheyachtllllllllluwuowouwu | Output: YES 1 sinktheyacht NO YES 3 uw uow ouwu
Hard
8
735
322
479
19
1,142
B
1142B
B. Lynyrd Skynyrd
2,000
data structures; dfs and similar; dp; math; trees
Recently Lynyrd and Skynyrd went to a shop where Lynyrd bought a permutation \(p\) of length \(n\), and Skynyrd bought an array \(a\) of length \(m\), consisting of integers from \(1\) to \(n\). Lynyrd and Skynyrd became bored, so they asked you \(q\) queries, each of which has the following form: ""does the subsegment...
The first line contains three integers \(n\), \(m\), \(q\) (\(1 \le n, m, q \le 2 \cdot 10^5\)) β€” the length of the permutation \(p\), the length of the array \(a\) and the number of queries.The next line contains \(n\) integers from \(1\) to \(n\), where the \(i\)-th of them is the \(i\)-th element of the permutation....
Print a single string of length \(q\), consisting of \(0\) and \(1\), the digit on the \(i\)-th positions should be \(1\), if the subsegment of array \(a\) from the \(l_i\)-th to the \(r_i\)-th positions, inclusive, contains a subsequence that is a cyclic shift of \(p\), and \(0\) otherwise.
In the first example the segment from the \(1\)-st to the \(5\)-th positions is \(1, 2, 3, 1, 2\). There is a subsequence \(1, 3, 2\) that is a cyclic shift of the permutation. The subsegment from the \(2\)-nd to the \(6\)-th positions also contains a subsequence \(2, 1, 3\) that is equal to the permutation. The subseg...
Input: 3 6 32 1 31 2 3 1 2 31 52 63 5 | Output: 110
Hard
5
1,136
766
292
11
1,082
D
1082D
D. Maximum Diameter Graph
1,800
constructive algorithms; graphs; implementation
Graph constructive problems are back! This time the graph you are asked to build should match the following properties.The graph is connected if and only if there exists a path between every pair of vertices.The diameter (aka ""longest shortest path"") of a connected undirected graph is the maximum number of edges in t...
The first line contains a single integer \(n\) (\(3 \le n \le 500\)) β€” the number of vertices in the graph.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n - 1\)) β€” the upper limits to vertex degrees.
Print ""NO"" if no graph can be constructed under the given conditions.Otherwise print ""YES"" and the diameter of the resulting graph in the first line.The second line should contain a single integer \(m\) β€” the number of edges in the resulting graph.The \(i\)-th of the next \(m\) lines should contain two integers \(v...
Here are the graphs for the first two example cases. Both have diameter of \(2\). \(d_1 = 1 \le a_1 = 2\)\(d_2 = 2 \le a_2 = 2\)\(d_3 = 1 \le a_3 = 2\) \(d_1 = 1 \le a_1 = 1\)\(d_2 = 4 \le a_2 = 4\)\(d_3 = 1 \le a_3 = 1\)\(d_4 = 1 \le a_4 = 1\)
Input: 3 2 2 2 | Output: YES 2 2 1 2 2 3
Medium
3
812
235
554
10
1,910
I
1910I
I. Inverse Problem
2,700
*special; combinatorics; dp
Consider the following problem.You are given a string \(s\), consisting of \(n\) lowercase Latin letters, and an integer \(k\) (\(n\) is not divisible by \(k\)). Each letter is one of the first \(c\) letters of the alphabet.You apply the following operation until the length of the string is less than \(k\): choose a co...
The first line contains three integers \(n, k\) and \(c\) (\(1 \le n \le 10^6\); \(2 \le k \le 10^6\); \(1 \le c \le 26\); \(n\) is not divisible by \(k\)) β€” the length of string \(s\), the length of the substring that is being removed and the number of first letters from the alphabet.The second line contains string \(...
Print a single integer β€” the number of strings \(s\) that produce \(t\) as the lexicographically smallest solution.
The strings \(s\) in the first example: ""aaa"", ""aab"", ""aba"", ""abb"", ""baa"", ""bba"".The strings \(s\) in the second example: ""bcabc"", ""bcacc"", ""bcbbc"", ""bcbcc"", ""bccbc"", ""bcccc"", ""caabc"", ""cabbc"", ""cacbc"", ""cbabc"", ""cbbbc"", ""cbcbc"", ""ccabc"", ""ccbbc"", ""cccbc"".
Input: 3 2 2 a | Output: 6
Master
3
925
450
115
19
939
D
939D
D. Love Rescue
1,600
dfs and similar; dsu; graphs; greedy; strings
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.This story cou...
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the length of the letterings.The second line contains a string with length n, consisting of lowercase English letters β€” the lettering on Valya's pullover.The third line contains the lettering on Tolya's t-shirt in the same format.
In the first line output a single integer β€” the minimum amount of mana t required for rescuing love of Valya and Tolya.In the next t lines output pairs of space-separated lowercase English letters β€” spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.If there are many op...
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
Input: 3abbdad | Output: 2a db a
Medium
5
1,352
289
346
9
1,846
C
1846C
C. Rudolf and the Another Competition
1,200
constructive algorithms; data structures; dp; greedy; sortings
Rudolf has registered for a programming competition that will follow the rules of ICPC. The rules imply that for each solved problem, a participant gets \(1\) point, and also incurs a penalty equal to the number of minutes passed from the beginning of the competition to the moment of solving the problem. In the final t...
The first line contains an integer \(t\) (\(1 \le t \le 10^3\)) β€” the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains three integers \(n, m, h\) (\(1 \le n \cdot m \le 2 \cdot 10^5, 1 \le h \le 10^6\)) β€” the number of participants, the number of problems, an...
For each test case, output an integer β€” Rudolf's place in the final table if all participants solve problems in the optimal order.
In the first example, Rudolf will get \(2\) points and \(50\) penalty minutes. The second participant will solve only one problem and get \(1\) point and \(90\) penalty minutes. And the third participant will solve all \(3\) problems and get \(3\) points and \(240\) penalty minutes. Thus, Rudolf will take the second pl...
Input: 53 3 12020 15 11090 90 10040 40 402 1 12030301 3 12010 20 303 2 278 910 710 83 3 157 2 67 5 41 9 8 | Output: 2 1 1 2 1
Easy
5
1,926
644
130
18
1,382
B
1382B
B. Sequential Nim
1,100
dp; games
There are \(n\) piles of stones, where the \(i\)-th pile has \(a_i\) stones. Two people play a game, where they take alternating turns removing stones.In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player...
The first line contains a single integer \(t\) (\(1\le t\le 1000\)) β€” the number of test cases. Next \(2t\) lines contain descriptions of test cases.The first line of each test case contains a single integer \(n\) (\(1\le n\le 10^5\)) β€” the number of piles.The second line of each test case contains \(n\) integers \(a_1...
For each test case, if the player who makes the first move will win, output ""First"". Otherwise, output ""Second"".
In the first test case, the first player will win the game. His winning strategy is: The first player should take the stones from the first pile. He will take \(1\) stone. The numbers of stones in piles will be \([1, 5, 4]\). The second player should take the stones from the first pile. He will take \(1\) stone because...
Input: 7 3 2 5 4 8 1 1 1 1 1 1 1 1 6 1 2 3 4 5 6 6 1 1 2 1 2 2 1 1000000000 5 1 2 2 1 1 3 1 1 1 | Output: First Second Second First First Second First
Easy
2
455
504
116
13
1,759
E
1759E
E. The Humanoid
1,500
brute force; dp; sortings
There are \(n\) astronauts working on some space station. An astronaut with the number \(i\) (\(1 \le i \le n\)) has power \(a_i\).An evil humanoid has made his way to this space station. The power of this humanoid is equal to \(h\). Also, the humanoid took with him two green serums and one blue serum.In one second , a...
The first line of each test contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” number of test cases.The first line of each test case contains integers \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” number of astronauts and \(h\) (\(1 \le h \le 10^6\)) β€” the initial power of the humanoid.The second line of each test case contai...
For each test case, in a separate line, print the maximum number of astronauts that a humanoid can absorb.
In the first case, you can proceed as follows: use green serum. \(h = 1 \cdot 2 = 2\) absorb the cosmonaut \(2\). \(h = 2 + \lfloor \frac{1}{2} \rfloor = 2\) use green serum. \(h = 2 \cdot 2 = 4\) absorb the spaceman \(1\). \(h = 4 + \lfloor \frac{2}{2} \rfloor = 5\) use blue serum. \(h = 5 \cdot 3 = 15\) absorb the sp...
Input: 84 12 1 8 93 36 2 604 55 1 100 53 238 6 31 1124 612 12 36 1004 12 1 1 153 515 1 13 | Output: 4 3 3 3 0 4 4 3
Medium
3
1,252
485
106
17
1,097
B
1097B
B. Petr and a Combination Lock
1,200
bitmasks; brute force; dp
Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to refuel it when he suddenly discovered that the petrol tank is secured with a combination lock! The lock has a scale of \(360\) degrees and a pointer which initially points at zero: Petr called his car dealer, who instruct...
The first line contains one integer \(n\) (\(1 \leq n \leq 15\)) β€” the number of rotations.Each of the following \(n\) lines contains one integer \(a_i\) (\(1 \leq a_i \leq 180\)) β€” the angle of the \(i\)-th rotation in degrees.
If it is possible to do all the rotations so that the pointer will point at zero after all of them are performed, print a single word ""YES"". Otherwise, print ""NO"". Petr will probably buy a new car in this case.You can print each letter in any case (upper or lower).
In the first example, we can achieve our goal by applying the first and the second rotation clockwise, and performing the third rotation counterclockwise.In the second example, it's impossible to perform the rotations in order to make the pointer point at zero in the end.In the third example, Petr can do all three rota...
Input: 3 10 20 30 | Output: YES
Easy
3
848
228
269
10
420
A
420A
A. Start Up
1,000
implementation
Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it?The market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if we write out t...
The first line contains a non-empty name that needs to be checked. The name contains at most 105 large English letters. The name will be written with the next sans serif font:
Print 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes).
Input: AHA | Output: YES
Beginner
1
906
175
126
4
803
A
803A
A. Maximal Binary Matrix
1,400
constructive algorithms
You are given matrix with n rows and n columns filled with zeroes. You should put k ones in it in such a way that the resulting matrix is symmetrical with respect to the main diagonal (the diagonal that goes from the top left to the bottom right corner) and is lexicographically maximal.One matrix is lexicographically g...
The first line consists of two numbers n and k (1 ≀ n ≀ 100, 0 ≀ k ≀ 106).
If the answer exists then output resulting matrix. Otherwise output -1.
Input: 2 1 | Output: 1 0 0 0
Easy
1
537
74
71
8
1,477
B
1477B
B. Nezzar and Binary String
1,900
data structures; greedy
Nezzar has a binary string \(s\) of length \(n\) that he wants to share with his best friend, Nanako. Nanako will spend \(q\) days inspecting the binary string. At the same time, Nezzar wants to change the string \(s\) into string \(f\) during these \(q\) days, because it looks better.It is known that Nanako loves cons...
The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β€” the number of test cases.The first line of each test case contains two integers \(n,q\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le q \le 2 \cdot 10^5\)).The second line of each test case contains a binary string \(s\) of length \(n\).The third ...
For each test case, print ""YES"" on the single line if it is possible to avoid Nanako being unhappy and have the string \(f\) at the end of \(q\) days and nights. Otherwise, print ""NO"".You can print each letter in any case (upper or lower).
In the first test case, \(\underline{00000} \rightarrow \underline{000}11 \rightarrow 00111\) is one of the possible sequences of string changes.In the second test case, it can be shown that it is impossible to have the string \(f\) at the end.
Input: 4 5 2 00000 00111 1 5 1 3 2 1 00 01 1 2 10 6 1111111111 0110001110 1 10 5 9 7 10 1 7 3 5 6 10 5 2 10000 11000 2 5 1 3 | Output: YES NO YES NO
Hard
2
946
729
243
14
242
A
242A
A. Heads or Tails
1,100
brute force; implementation
Petya and Vasya are tossing a coin. Their friend Valera is appointed as a judge. The game is very simple. First Vasya tosses a coin x times, then Petya tosses a coin y times. If the tossing player gets head, he scores one point. If he gets tail, nobody gets any points. The winner is the player with most points by the e...
The single line contains four integers x, y, a, b (1 ≀ a ≀ x ≀ 100, 1 ≀ b ≀ y ≀ 100). The numbers on the line are separated by a space.
In the first line print integer n β€” the number of possible outcomes of the game. Then on n lines print the outcomes. On the i-th line print a space-separated pair of integers ci, di β€” the number of heads Vasya and Petya got in the i-th outcome of the game, correspondingly. Print pairs of integers (ci, di) in the strict...
Input: 3 2 1 1 | Output: 32 13 13 2
Easy
2
842
135
475
2
1,166
B
1166B
B. All the Vowels Please
1,100
constructive algorithms; math; number theory
Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length \(k\) is vowelly if there are positive integers \(n\) and \(m\) such that \(n\cdot m = k\) and when the word is written by using \(n\) rows and \(m\) columns (the first row is filled first, then the...
Input consists of a single line containing the integer \(k\) (\(1\leq k \leq 10^4\)) β€” the required length.
The output must consist of a single line, consisting of a vowelly word of length \(k\) consisting of lowercase English letters if it exists or \(-1\) if it does not.If there are multiple possible words, you may output any of them.
In the second example, the word ""agoeuioaeiruuimaeoieauoweouoiaouimae"" can be arranged into the following \(6 \times 6\) grid: It is easy to verify that every row and every column contain all the vowels.
Input: 7 | Output: -1
Easy
3
675
107
230
11
1,184
E1
1184E1
E1. Daleks' Invasion (easy)
1,900
graphs; trees
Heidi found out that the Daleks have created a network of bidirectional Time Corridors connecting different destinations (at different times!). She suspects that they are planning another invasion on the entire Space and Time. In order to counter the invasion, she plans to deploy a trap in the Time Vortex, along a care...
The first line contains integers \(n\) and \(m\) (\(2 \leq n \leq 10^5\), \(n - 1 \leq m \leq 10^6\)), number of destinations to be invaded and the number of Time Corridors.Each of the next \(m\) lines describes a Corridor: destinations \(a\), \(b\) and energy \(e\) (\(1 \leq a, b \leq n\), \(a \neq b\), \(0 \leq e \le...
Output a single integer: \(E_{max}(c_1)\) for the first Corridor \(c_1\) from the input.
After the trap is set, the new energy requirement for the first Corridor may be either smaller, larger, or equal to the old energy requiremenet.In the example, if the energy of the first Corridor is set to \(4\) or less, then the Daleks may use the set of Corridors \(\{ \{ 1,2 \}, \{ 2,3 \} \}\) (in particular, if it w...
Input: 3 31 2 82 3 33 1 4 | Output: 4
Hard
2
1,620
516
88
11
798
E
798E
E. Mike and code of a permutation
3,000
constructive algorithms; data structures; graphs; sortings
Mike has discovered a new way to encode permutations. If he has a permutation P = [p1, p2, ..., pn], he will encode it in the following way:Denote by A = [a1, a2, ..., an] a sequence of length n which will represent the code of the permutation. For each i from 1 to n sequentially, he will choose the smallest unmarked j...
The first line contains single integer n (1 ≀ n ≀ 500 000) β€” length of permutation.The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ n or ai = - 1) β€” the code of Mike's permutation.You may assume that all positive values from A are different.
In first and only line print n numbers p1, p2, ..., pn (1 ≀ pi ≀ n) β€” a permutation P which has the same code as the given one. Note that numbers in permutation are distinct.
For the permutation from the first example:i = 1, the smallest j is 2 because p2 = 6 > p1 = 2.i = 2, there is no j because p2 = 6 is the greatest element in the permutation.i = 3, the smallest j is 1 because p1 = 2 > p3 = 1.i = 4, the smallest j is 5 (2 was already marked) because p5 = 5 > p4 = 4.i = 5, there is no j b...
Input: 62 -1 1 5 -1 4 | Output: 2 6 1 4 5 3
Master
4
772
270
174
7