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
87
D
87D
D. Beautiful Road
2,300
dfs and similar; dp; dsu; graphs; implementation; sortings; trees
A long time ago in some country in Asia were civil wars.Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city.Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on t...
The first line contains an integer n (2 ≀ n ≀ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 ≀ ai, bi ≀ n), connected by the i-th road and the number of days di the army spends to go on it (1 ≀ di ≀ 109). The lengths of several roads may coinci...
Print on the first line two integers β€” the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are ...
Input: 22 1 5 | Output: 2 11
Expert
7
1,024
323
495
0
1,033
E
1033E
E. Hidden Bipartite Graph
2,800
binary search; constructive algorithms; dfs and similar; graphs; interactive
Bob has a simple undirected connected graph (without self-loops and multiple edges). He wants to learn whether his graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color) or not. As he is not very good at programming, he as...
The first line contains a single integer \(n\) (\(1 \leq n \leq 600\)) β€” the number of vertices in Bob's graph.
In the first case, Alice learns that there are \(4\) edges in the whole graph. Over the course of the next three queries, she learns that vertex \(1\) has two neighbors: \(3\) and \(4\). She then learns that while vertex \(2\) is adjacent to \(4\), the vertex \(3\) isn't adjacent to \(4\). There is only one option for ...
Input: 4401110 | Output: ? 4 1 2 3 4? 21 2? 21 3? 21 4? 22 4? 23 4Y 21 2
Master
5
1,117
111
0
10
123
A
123A
A. Prime Permutation
1,300
implementation; number theory; strings
You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≀ |s| and for any integer i ranging from 1 t...
The only line contains the initial string s, consisting of small Latin letters (1 ≀ |s| ≀ 1000).
If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line ""YES"" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string ""NO...
In the first sample any of the six possible strings will do: ""abc"", ""acb"", ""bac"", ""bca"", ""cab"" or ""cba"".In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4).In the third test any string where character ""y"" doesn't occupy positions 2, 3, 4, 6 will be valid.
Input: abc | Output: YESabc
Easy
3
461
96
323
1
1,725
A
1725A
A. Accumulation of Dominoes
800
math
Pak Chanek has a grid that has \(N\) rows and \(M\) columns. Each row is numbered from \(1\) to \(N\) from top to bottom. Each column is numbered from \(1\) to \(M\) from left to right.Each tile in the grid contains a number. The numbers are arranged as follows: Row \(1\) contains integers from \(1\) to \(M\) from left...
The only line contains two integers \(N\) and \(M\) (\(1 \leq N, M \leq 10^9\)) β€” the number of rows and columns in the grid.
An integer representing the number of distinct tight dominoes in the grid.
The picture below is the grid that Pak Chanek has in the first example. The picture below is an example of a tight domino in the grid.
Input: 3 4 | Output: 9
Beginner
1
900
125
74
17
825
B
825B
B. Five-In-a-Row
1,600
brute force; implementation
Alice and Bob play 5-in-a-row game. They have a playing field of size 10 Γ— 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immed...
You are given matrix 10 Γ— 10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.It is guaranteed that i...
Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.
Input: XX.XX..........OOOO................................................................................. | Output: YES
Medium
2
458
367
116
8
2,050
F
2050F
F. Maximum modulo equality
1,700
data structures; divide and conquer; math; number theory
You are given an array \(a\) of length \(n\) and \(q\) queries \(l\), \(r\).For each query, find the maximum possible \(m\), such that all elements \(a_l\), \(a_{l+1}\), ..., \(a_r\) are equal modulo \(m\). In other words, \(a_l \bmod m = a_{l+1} \bmod m = \dots = a_r \bmod m\), where \(a \bmod b\) β€” is the remainder o...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\), \(q\) (\(1 \le n, q \le 2\cdot 10^5\)) β€” the length of the array and the number of queries.The second line of each test case contains \(n\) integers \(a_i\) (\(1...
For each query, output the maximum value \(m\) described in the statement.
In the first query of the first sample, \(6 \bmod 3 = 3 \bmod 3 = 0\). It can be shown that for greater \(m\), the required condition will not be fulfilled.In the third query of the first sample, \(14 \bmod 4 = 2 \bmod 4 = 6 \bmod 4 = 2\). It can be shown that for greater \(m\), the required condition will not be fulfi...
Input: 35 55 14 2 6 34 51 42 43 51 11 171 13 21 7 82 31 2 | Output: 3 1 4 1 0 0 1 6
Medium
4
402
654
74
20
1,236
C
1236C
C. Labs
1,300
constructive algorithms; greedy; implementation
In order to do some research, \(n^2\) labs are built on different heights of a mountain. Let's enumerate them with integers from \(1\) to \(n^2\), such that the lab with the number \(1\) is at the lowest place, the lab with the number \(2\) is at the second-lowest place, \(\ldots\), the lab with the number \(n^2\) is a...
The only line contains one number \(n\) (\(2 \leq n \leq 300\)).
Output \(n\) lines:In the \(i\)-th line print \(n\) numbers, the numbers of labs of the \(i\)-th group, in any order you want.If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any.
In the first test we can divide \(9\) labs into groups \(\{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}\).From the first group to the second group we can transport \(4\) units of water (\(8 \rightarrow 3, 8 \rightarrow 4, 5 \rightarrow 3, 5 \rightarrow 4\)).From the first group to the third group we can transport \(5\) units of...
Input: 3 | Output: 2 8 5 9 3 4 7 6 1
Easy
3
2,645
64
289
12
981
A
981A
A. Antipalindrome
900
brute force; implementation; strings
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings ""kek"", ""abacaba"", ""r"" and ""papicipap"" are palindromes, while the strings ""abb"" and ""iq"" are not.A substring \(s[l \ldots r]\) (\(1 \leq l \leq r \leq |s|\)) of a string \(s = s_{...
The first line contains a non-empty string \(s\) with length at most \(50\) characters, containing lowercase English letters only.
If there is such a substring in \(s\) that is not a palindrome, print the maximum length of such a substring. Otherwise print \(0\).Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
""mew"" is not a palindrome, so the longest substring of it that is not a palindrome, is the string ""mew"" itself. Thus, the answer for the first example is \(3\).The string ""uffuw"" is one of the longest non-palindrome substrings (of length \(5\)) of the string ""wuffuw"", so the answer for the second example is \(5...
Input: mew | Output: 3
Beginner
3
759
130
236
9
107
E
107E
E. Darts
2,700
geometry; probabilities
The night after the graduation ceremony graduate students of German University in Cairo (GUC) are playing darts. As there's no real dart board available, the photographs of members of the GUC upper management are being used.So, n rectangular photos are placed on the wall. They can overlap arbitrary and even coincide. T...
The first line of input contains integer n (1 ≀ n ≀ 500) β€” the number of photos on the wall. Then follow n lines describing the photos, each containing 8 single-space-separated integers (coordinates of 4 vertices): x1, y1, x2, y2, x3, y3, x4, y4. Each photo is a rectangle with a nonzero area. The coordinates are intege...
Print the expected score of the throw. The answer will be accepted if it has absolute or relative error not exceeding 10 - 6.
Input: 10 0 0 2 2 2 2 0 | Output: 1.0000000000
Master
2
779
450
125
1
981
B
981B
B. Businessmen Problems
1,000
sortings
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.In order to avoid this representatives of both companies decided to make an agreement on the se...
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the number of elements discovered by ChemForces.The \(i\)-th of the next \(n\) lines contains two integers \(a_i\) and \(x_i\) (\(1 \leq a_i \leq 10^9\), \(1 \leq x_i \leq 10^9\)) β€” the index of the \(i\)-th element and the income of its usage on...
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
In the first example ChemForces can choose the set (\(3, 7\)), while TopChemist can choose (\(1, 2, 4\)). This way the total income is \((10 + 2) + (4 + 4 + 4) = 24\).In the second example ChemForces can choose the only element \(10^9\), while TopChemist can choose (\(14, 92, 35\)). This way the total income is \((239)...
Input: 31 27 23 1041 42 43 44 4 | Output: 24
Beginner
1
1,365
767
142
9
1,910
G
1910G
G. Pool Records
2,700
*special; greedy
Alice and Bob are swimming in the pool under the guidance of their instructor Monocarp.The pool can be represented as a segment on the OX-axis from \(0\) to \(50\). Both Alice and Bob started at moment \(0\) at point \(0\) with positive real speeds \(v_A\) and \(v_B\) correspondingly.Both Alice and Bob swim from \(0\) ...
The first line contains a single integer \(c\) (\(1 \le c \le 10^4\)) β€” the number of test cases. Then \(c\) cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the size of array \(t\).The second line of each test case contains \(n\) integers \(t_1, t_2, \dots,...
For each test case, print VALID if the array \(t\) is a valid array, or (in other words) exist such real values \(v_A\) and \(v_B\) (\(v_A, v_B > 0\); \(v_A \neq v_B\)) that array \(t\) contains the first \(n\) meeting moments of Alice and Bob in the pool.Otherwise, print INVALID.You can output the answer in any case (...
In the first test case, imagine a situation: \(v_A = 1\) and \(v_B = \frac{29}{21}\). Then, at moment \(42\) Alice and Bob will be at point \(42\).In the second test case, imagine a situation: \(v_A = \frac{175}{6}\) and \(v_B = \frac{25}{6}\). Then, at moment \(3\) Alice and Bob will meet at point \(12.5\), at \(4\) t...
Input: 714243 4 6 851 2 3 4 53999999998 999999999 100000000054 6 8 12 1646 11 12 14210 30 | Output: VALID VALID VALID INVALID VALID INVALID INVALID
Master
2
1,205
508
450
19
1,109
E
1109E
E. Sasha and a Very Easy Test
2,700
data structures; number theory
Egor likes math, and not so long ago he got the highest degree of recognition in the math community β€” Egor became a red mathematician. In this regard, Sasha decided to congratulate Egor and give him a math test as a present. This test contains an array \(a\) of integers of length \(n\) and exactly \(q\) queries. Querie...
The first line contains two integers \(n\) and \(mod\) (\(1 \le n \le 10^5\), \(2 \le mod \le 10^9 + 9\)) β€” the size of the array and the number \(mod\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^5\)) β€” the array itself.The third line contains one integer \(q\)(\(1 \le q \le 1...
For each query of the \(3\)-rd type print the answer on a new line modulo \(mod\).
The first example:Inital array is \([4, 1, 2, 3, 5]\) In the first query, you must calculate the sum of the whole array, it's equal to \((4 + 1 + 2 + 3 + 5) \bmod 100 = 15 \bmod 100 = 15\) In the second query, you must multiply each number on the range from \(2\) to \(3\) by \(6\). The resulting array will be \([4, 6, ...
Input: 5 100 4 1 2 3 5 5 3 1 5 1 2 3 6 3 1 2 1 1 5 1 3 2 4 | Output: 15 10 21
Master
2
905
871
82
11
1,203
E
1203E
E. Boxers
1,500
greedy; sortings
There are \(n\) boxers, the weight of the \(i\)-th boxer is \(a_i\). Each of them can change the weight by no more than \(1\) before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.It is necessary to choose the largest boxing team in terms o...
The first line contains an integer \(n\) (\(1 \le n \le 150000\)) β€” the number of boxers. The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\), where \(a_i\) (\(1 \le a_i \le 150000\)) is the weight of the \(i\)-th boxer.
Print a single integer β€” the maximum possible number of people in a team.
In the first example, boxers should not change their weights β€” you can just make a team out of all of them.In the second example, one boxer with a weight of \(1\) can be increased by one (get the weight of \(2\)), one boxer with a weight of \(4\) can be reduced by one, and the other can be increased by one (resulting t...
Input: 43 2 4 1 | Output: 4
Medium
2
617
231
73
12
86
C
86C
C. Genetic engineering
2,500
dp; string suffix structures; trees
""Multidimensional spaces are completely out of style these days, unlike genetics problems"" β€” thought physicist Woll and changed his subject of study to bioinformatics. Analysing results of sequencing he faced the following problem concerning DNA sequences. We will further think of a DNA sequence as an arbitrary strin...
First line contains two integer numbers n and m (1 ≀ n ≀ 1000, 1 ≀ m ≀ 10) β€” the length of the string and the number of sequences in the collection correspondently. Next m lines contain the collection sequences si, one per line. Each si is a nonempty string of length not greater than 10. All the strings consist of uppe...
Output should contain a single integer β€” the number of strings filtered by the collection modulo 1000000009 (109 + 9).
In the first sample, a string has to be filtered by ""A"". Clearly, there is only one such string: ""AA"".In the second sample, there exist exactly two different strings satisfying the condition (see the pictures below).
Input: 2 1A | Output: 1
Expert
3
1,315
407
118
0
1,383
E
1383E
E. Strange Operation
2,800
combinatorics; data structures; dp
Koa the Koala has a binary string \(s\) of length \(n\). Koa can perform no more than \(n-1\) (possibly zero) operations of the following form:In one operation Koa selects positions \(i\) and \(i+1\) for some \(i\) with \(1 \le i < |s|\) and sets \(s_i\) to \(max(s_i, s_{i+1})\). Then Koa deletes position \(i+1\) from ...
The only line of input contains binary string \(s\) (\(1 \le |s| \le 10^6\)). For all \(i\) (\(1 \le i \le |s|\)) \(s_i = 0\) or \(s_i = 1\).
On a single line print the answer to the problem modulo \(10^9+7\) (\(1000000007\)).
In the first sample Koa can obtain binary strings: \(0\), \(00\) and \(000\).In the second sample Koa can obtain binary strings: \(1\), \(01\), \(11\), \(011\), \(101\) and \(0101\). For example: to obtain \(01\) from \(0101\) Koa can operate as follows: \(0101 \rightarrow 0(10)1 \rightarrow 011 \rightarrow 0(11) \righ...
Input: 000 | Output: 3
Master
3
596
141
84
13
1,950
C
1950C
C. Clock Conversion
800
implementation; math
Given the time in 24-hour format, output the equivalent time in 12-hour format. 24-hour format divides the day into 24 hours from \(00\) to \(23\), each of which has 60 minutes from \(00\) to \(59\). 12-hour format divides the day into two halves: the first half is \(\mathrm{AM}\), and the second half is \(\mathrm{PM}\...
The first line contains a single integer \(t\) (\(1 \leq t \leq 1440\)) β€” the number of test cases.The only line of each test case contains a string \(s\) of length \(5\) with format hh:mm representing a valid time in the 24-hour format. hh represents the hour from \(00\) to \(23\), and mm represents the minute from \(...
For each test case, output two strings separated by a space (""hh:mm AM"" or ""hh:mm PM""), which are the 12-hour equivalent to the time provided in the test case (without quotes).You should output the time exactly as indicated; in particular, you should not remove leading zeroes.
Input: 1109:4118:0612:1400:5900:0014:3401:0119:0711:5912:0021:37 | Output: 09:41 AM 06:06 PM 12:14 PM 12:59 AM 12:00 AM 02:34 PM 01:01 AM 07:07 PM 11:59 AM 12:00 PM 09:37 PM
Beginner
2
460
391
281
19
2,048
F
2048F
F. Kevin and Math Class
2,500
brute force; data structures; divide and conquer; dp; implementation; math; trees
Kevin is a student from Eversleeping Town, currently attending a math class where the teacher is giving him division exercises.On the board, there are two rows of positive integers written, each containing \( n \) numbers. The first row is \( a_1, a_2, \ldots, a_n \), and the second row is \( b_1, b_2, \ldots, b_n \). ...
Each test contains multiple test cases. The first line contains the number of test cases \( t \) (\( 1 \le t \le 10^4 \)).The first line of each test case contains an integer \( n \) (\( 1 \le n \leq 2 \cdot 10^5 \)) β€” the length of the sequence \( a \) and \( b \).The second line of each test case contains \( n \) int...
For each test case, output one integer β€” the minimum number of division exercises required to leave class.
For the first test case: \( [{\color{red}{5,4}},2]\xrightarrow[\min(b_1,b_2)=3]{\text{operate segment }[1,2]}[{\color{red}{2,2,2}}]\xrightarrow[\min(b_1,b_2,b_3)=2]{\text{operate segment }[1,3]}[1,1,1] \).For the second test case: \( [{\color{red}{3,6,1}},3,2]\xrightarrow[\min(b_1,b_2,b_3)=3]{\text{operate segment }[1,...
Input: 335 4 26 3 253 6 1 3 23 5 3 2 268 3 3 7 5 83 2 3 4 2 3 | Output: 2 3 3
Expert
7
946
679
106
20
1,394
A
1394A
A. Boboniu Chats with Du
1,800
dp; greedy; sortings; two pointers
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for \(n\) days. On the \(i\)-th day: If Du can speak, he'll make fun of Boboniu w...
The first line contains three integers \(n\), \(d\) and \(m\) (\(1\le d\le n\le 10^5,0\le m\le 10^9\)).The next line contains \(n\) integers \(a_1, a_2, \ldots,a_n\) (\(0\le a_i\le 10^9\)).
Print one integer: the maximum total fun factor among all permutations of \(a\).
In the first example, you can set \(a'=[15, 5, 8, 10, 23]\). Then Du's chatting record will be: Make fun of Boboniu with fun factor \(15\). Be muzzled. Be muzzled. Make fun of Boboniu with fun factor \(10\). Make fun of Boboniu with fun factor \(23\). Thus the total fun factor is \(48\).
Input: 5 2 11 8 10 15 23 5 | Output: 48
Medium
4
889
189
80
13
1,051
C
1051C
C. Vasya and Multisets
1,500
brute force; dp; greedy; implementation; math
Vasya has a multiset \(s\) consisting of \(n\) integer numbers. Vasya calls some number \(x\) nice if it appears in the multiset exactly once. For example, multiset \(\{1, 1, 2, 3, 3, 3, 4\}\) contains nice numbers \(2\) and \(4\).Vasya wants to split multiset \(s\) into two multisets \(a\) and \(b\) (one of which may ...
The first line contains a single integer \(n~(2 \le n \le 100)\).The second line contains \(n\) integers \(s_1, s_2, \dots s_n~(1 \le s_i \le 100)\) β€” the multiset \(s\).
If there exists no split of \(s\) to satisfy the given requirements, then print ""NO"" in the first line.Otherwise print ""YES"" in the first line.The second line should contain a string, consisting of \(n\) characters. \(i\)-th character should be equal to 'A' if the \(i\)-th element of multiset \(s\) goes to multiset...
Input: 43 5 7 1 | Output: YESBABA
Medium
5
600
170
545
10
652
B
652B
B. z-sort
1,000
sortings
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: ai β‰₯ ai - 1 for all even i, ai ≀ ai - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.Can you make the array z-sorted?
The first line contains a single integer n (1 ≀ n ≀ 1000) β€” the number of elements in the array a.The second line contains n integers ai (1 ≀ ai ≀ 109) β€” the elements of the array a.
If it's possible to make the array a z-sorted print n space separated integers ai β€” the elements after z-sort. Otherwise print the only word ""Impossible"".
Input: 41 2 2 1 | Output: 1 2 1 2
Beginner
1
318
182
156
6
670
F
670F
F. Restore a Number
2,300
brute force; constructive algorithms; strings
Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k β€” the number of digits in n. Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substrin...
The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000.The second line contains the substring of n which Vasya remembers. This string can contain leading zeroes. It is guaranteed that the input data is correct, and the answer always exists.
Print the smalles integer n which Vasya could pass to Kate.
Input: 003512021 | Output: 30021
Expert
3
703
313
59
6
1,462
E1
1462E1
E1. Close Tuples (easy version)
1,500
binary search; combinatorics; math; sortings; two pointers
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on \(k\) and \(m\) (in this version \(k=2\) and \(m=3\)). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.You are given a sequence \(a\) of length \(n\) consisting of integ...
The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the sequence \(a\).The next line contains \(n\) integers \(a_1, a_2,\ldots, a_n...
Output \(t\) answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than \(2\). Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output th...
Input: 4 4 1 2 4 3 4 1 1 1 1 1 1 10 5 6 1 3 2 9 8 1 2 4 | Output: 2 4 0 15
Medium
5
894
457
348
14
1,826
A
1826A
A. Trust Nobody
1,200
brute force; greedy; implementation; sortings
There is a group of \(n\) people. Some of them might be liars, who always tell lies. Other people always tell the truth. The \(i\)-th person says ""There are at least \(l_i\) liars amongst us"". Determine if what people are saying is contradictory, or if it is possible. If it is possible, output the number of liars in ...
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 a single integer \(n\) (\(1 \leq n \leq 100\)).The second line of each test case contains \(n\) integers \(l_i\) (\(0 \leq l_i \leq n\)) β€” the number said by the \(i\)-th person.I...
For each test case output a single integer. If what people are saying is contradictory, output \(-1\). Otherwise, output the number of liars in the group. If there are multiple possible answers, output any one of them.
In the first example, the only possible answer is that the second person is a liar, so the answer is \(1\) liar.In the second example, it can be proven that we can't choose the liars so that all the requirements are satisfied.In the third example, everybody tells the truth, so the answer is \(0\) liars.
Input: 721 222 220 0111055 5 3 3 565 3 6 6 3 5 | Output: 1 -1 0 -1 0 3 4
Easy
4
394
386
218
18
814
A
814A
A. An abandoned sentiment from past
900
constructive algorithms; greedy; implementation; sortings
A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since, she tried to avoid contact with others, for fear that this secret might be noticed.To get rid of the oddity and recover her weight, a special integer sequence is needed. Hitagi's sequence has been broken for a long tim...
The first line of input contains two space-separated positive integers n (2 ≀ n ≀ 100) and k (1 ≀ k ≀ n) β€” the lengths of sequence a and b respectively.The second line contains n space-separated integers a1, a2, ..., an (0 ≀ ai ≀ 200) β€” Hitagi's broken sequence with exactly k zero elements.The third line contains k spa...
Output ""Yes"" if it's possible to replace zeros in a with elements in b and make the resulting sequence not increasing, and ""No"" otherwise.
In the first sample: Sequence a is 11, 0, 0, 14. Two of the elements are lost, and the candidates in b are 5 and 4. There are two possible resulting sequences: 11, 5, 4, 14 and 11, 4, 5, 14, both of which fulfill the requirements. Thus the answer is ""Yes"". In the second sample, the only possible resulting sequence is...
Input: 4 211 0 0 145 4 | Output: Yes
Beginner
4
1,174
508
142
8
107
B
107B
B. Basketball Team
1,600
combinatorics; dp; math; probabilities
As a German University in Cairo (GUC) student and a basketball player, Herr Wafa was delighted once he heard the news. GUC is finally participating in the Annual Basketball Competition (ABC). A team is to be formed of n players, all of which are GUC students. However, the team might have players belonging to different ...
The first line contains three integers n, m and h (1 ≀ n ≀ 100, 1 ≀ m ≀ 1000, 1 ≀ h ≀ m) β€” the number of players on the team, the number of departments in GUC and Herr Wafa's department, correspondingly. The second line contains a single-space-separated list of m integers si (1 ≀ si ≀ 100), denoting the number of stude...
Print the probability that Herr Wafa will have at least one teammate from his department. If there is not enough basketball players in GUC to participate in ABC, print -1. The answer will be accepted if it has absolute or relative error not exceeding 10 - 6.
In the first example all 3 players (2 from department 1 and 1 from department 2) must be chosen for the team. Both players from Wafa's departments will be chosen, so he's guaranteed to have a teammate from his department.In the second example, there are not enough players.In the third example, there are three possibili...
Input: 3 2 12 1 | Output: 1
Medium
4
904
380
258
1
1,939
A
1939A
0
*special; constructive algorithms; dp; geometry; interactive
Beginner
5
0
0
0
19
954
A
954A
A. Diagonal Walking
800
implementation
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any ...
The first line of the input contains one integer n (1 ≀ n ≀ 100) β€” the length of the sequence. The second line contains the sequence consisting of n characters U and R.
Print the minimum possible length of the sequence of moves after all replacements are done.
In the first test the shortened sequence of moves may be DUD (its length is 3).In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
Input: 5RUURU | Output: 3
Beginner
1
620
168
91
9
524
B
524B
B. Π€ΠΎΡ‚ΠΎ Π½Π° ΠΏΠ°ΠΌΡΡ‚ΡŒ - 2 (round version)
1,700
dp; greedy
ΠŸΡ€ΠΎΡˆΠ»ΠΎ ΠΌΠ½ΠΎΠ³ΠΎ Π»Π΅Ρ‚, ΠΈ Π½Π° Π²Π΅Ρ‡Π΅Ρ€ΠΈΠ½ΠΊΠ΅ снова Π²ΡΡ‚Ρ€Π΅Ρ‚ΠΈΠ»ΠΈΡΡŒ n Π΄Ρ€ΡƒΠ·Π΅ΠΉ. Π‘ ΠΌΠΎΠΌΠ΅Π½Ρ‚Π° послСднСй встрСчи Ρ‚Π΅Ρ…Π½ΠΈΠΊΠ° ΡˆΠ°Π³Π½ΡƒΠ»Π° Π΄Π°Π»Π΅ΠΊΠΎ Π²ΠΏΠ΅Ρ€Ρ‘Π΄, появились Ρ„ΠΎΡ‚ΠΎΠ°ΠΏΠΏΠ°Ρ€Π°Ρ‚Ρ‹ с автоспуском, ΠΈ Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π½Π΅ трСбуСтся, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· Π΄Ρ€ΡƒΠ·Π΅ΠΉ стоял с Ρ„ΠΎΡ‚ΠΎΠ°ΠΏΠΏΠ°Ρ€Π°Ρ‚ΠΎΠΌ, ΠΈ, Ρ‚Π΅ΠΌ самым, оказывался Π½Π΅ Π·Π°ΠΏΠ΅Ρ‡Π°Ρ‚Π»Ρ‘Π½Π½Ρ‹ΠΌ Π½Π° снимкС.Π£ΠΏΡ€ΠΎΡ‰Π΅Π½Π½ΠΎ процСсс фотографирования ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠΏ...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС слСдуСт Ρ†Π΅Π»ΠΎΠ΅ число n (1 ≀ n ≀ 1000) β€” количСство Π΄Ρ€ΡƒΠ·Π΅ΠΉ.Π’ ΠΏΠΎΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… n строках ΡΠ»Π΅Π΄ΡƒΡŽΡ‚ ΠΏΠΎ Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… числа wi, hi (1 ≀ wi, hi ≀ 1000), ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°ΡŽΡ‰ΠΈΠ΅ Ρ€Π°Π·ΠΌΠ΅Ρ€Ρ‹ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΈΠΊΠ°, ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰Π΅Π³ΠΎ i-ΠΌΡƒ ΠΈΠ· Π΄Ρ€ΡƒΠ·Π΅ΠΉ.
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ СдинствСнноС Ρ†Π΅Π»ΠΎΠ΅ число, Ρ€Π°Π²Π½ΠΎΠ΅ минимальной Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠΉ ΠΏΠ»ΠΎΡ‰Π°Π΄ΠΈ Ρ„ΠΎΡ‚ΠΎΠ³Ρ€Π°Ρ„ΠΈΠΈ, Π²ΠΌΠ΅Ρ‰Π°ΡŽΡ‰Π΅ΠΉ всСх Π΄Ρ€ΡƒΠ·Π΅ΠΉ.
Input: 310 120 230 3 | Output: 180
Medium
2
871
221
106
5
488
A
488A
A. Giga Tower
1,100
brute force
Giga Tower is the tallest and deepest building in Cyberland. There are 17 777 777 777 floors, numbered from - 8 888 888 888 to 8 888 888 888. In particular, there is floor 0 between floor - 1 and floor 1. Every day, thousands of tourists come to this place to enjoy the wonderful view. In Cyberland, it is believed that ...
The only line of input contains an integer a ( - 109 ≀ a ≀ 109).
Print the minimum b in a line.
For the first sample, he has to arrive at the floor numbered 180.For the second sample, he will arrive at 8.Note that b should be positive, so the answer for the third sample is 10, not 0.
Input: 179 | Output: 1
Easy
1
957
64
30
4
505
B
505B
B. Mr. Kitayuta's Colorful Graph
1,400
dfs and similar; dp; dsu; graphs
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.Mr. Kitayuta wants you to process the following q queries.In the i-th query, he gives you two integers β€” ui and...
The first line of the input contains space-separated two integers β€” n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100), denoting the number of the vertices and the number of the edges, respectively.The next m lines contain space-separated three integers β€” ai, bi (1 ≀ ai < bi ≀ n) and ci (1 ≀ ci ≀ m). Note that there can be multiple ed...
For each query, print the answer in a separate line.
Let's consider the first sample. The figure above shows the first sample. Vertex 1 and vertex 2 are connected by color 1 and 2. Vertex 3 and vertex 4 are connected by color 3. Vertex 1 and vertex 4 are not connected by any single color.
Input: 4 51 2 11 2 22 3 12 3 32 4 331 23 41 4 | Output: 210
Easy
4
471
679
52
5
1,781
H1
1781H1
H1. Window Signals (easy version)
3,200
This is the easy version of the problem. In this version, the constraints on \(h\) and \(w\) are lower.A house at the sea has \(h\) floors, all of the same height. The side of the house facing the sea has \(w\) windows at equal distances from each other on every floor. Thus, the windows are positioned in cells of a rec...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The first line of each test case contains three integers \(h\), \(w\), and \(k\) (\(1 \le h, w \le 40\); \(0 \le k \le \min(h \cdot w, 2)\)) β€” the height of the...
For each test case, print a single integer β€” the number of signals the ship can receive, modulo \(998\,244\,353\).
In the first test case, the ship can receive four different signals: all lights on; lights on in two neighboring windows; lights on in the leftmost and the rightmost windows; or light on in any single window.
Input: 101 3 02 2 03 2 04 5 012 7 01 1 11 11 3 11 23 4 13 42 3 21 12 14 5 22 34 2 | Output: 4 10 44 954368 34903934 0 2 1696 10 253144
Master
0
1,146
878
114
17
432
C
432C
C. Prime Swaps
1,800
greedy; sortings
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 ≀ i < j ≀ n; (j - i + 1) is a prime number); swap the elements on positions i and ...
The first line contains integer n (1 ≀ n ≀ 105). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 ≀ a[i] ≀ n).
In the first line, print integer k (0 ≀ k ≀ 5n) β€” the number of used operations. Next, print the operations. Each operation must be printed as ""i j"" (1 ≀ i < j ≀ n; (j - i + 1) is a prime).If there are multiple answers, you can print any of them.
Input: 33 2 1 | Output: 11 3
Medium
2
597
129
248
4
1,239
F
1239F
F. Swiper, no swiping!
3,400
graphs; implementation
I'm the Map, I'm the Map! I'm the MAP!!!MapIn anticipation of new adventures Boots wanted to do a good deed. After discussion with the Map and Backpack, they decided to gift Dora a connected graph. After a long search, Boots chose \(t\) graph's variants, which Dora might like. However fox Swiper wants to spoil his plan...
The first line contains a single integer \(t\) (\(1 \le t \le 100\,000\)) β€” the number of graph variants.The first line of each variant contains integers \(n\), \(m\) (\(1 \le n \le 500\,000\), \(0 \le m \le 500\,000\)), the number of vertexes and edges in the graph.Then \(m\) lines follow, each containing integers \(a...
For each variant: In case the answer exists, print ""Yes"" and then the answer itself.The first line should contain an integer \(c\) (\(1 < c < n\)), the number of vertices the Crook can steal, without Dora noticing the loss. On the next line print \(c\) distinct integers, the indices of the graph's vertices in arbitra...
The picture below shows the third variant from the example test. The set of the vertices the Crook can steal is denoted with bold.
Input: 3 3 3 1 2 2 3 3 1 6 6 1 2 1 3 2 3 2 5 2 6 2 4 8 12 1 2 1 3 2 3 1 4 4 5 5 1 3 6 3 7 3 8 6 1 7 1 8 1 | Output: No Yes 3 4 5 6 Yes 3 6 7 8
Master
2
1,115
735
512
12
837
A
837A
A. Text Volume
800
implementation
You are given a text of single-space separated words, consisting of small and capital Latin letters.Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text.Calculate the volume of the given text.
The first line contains one integer number n (1 ≀ n ≀ 200) β€” length of the text.The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters.
Print one integer number β€” volume of text.
In the first example there is only one word, there are 5 capital letters in it.In the second example all of the words contain 0 capital letters.
Input: 7NonZERO | Output: 5
Beginner
1
262
210
42
8
518
C
518C
C. Anya and Smartphone
1,600
constructive algorithms; data structures; implementation
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th ...
The first line of the input contains three numbers n, m, k (1 ≀ n, m, k ≀ 105) β€” the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen.The next line contains n integers, permutation a1, a2, ..., an β€” the i...
Print a single number β€” the number of gestures that Anya needs to make to launch all the applications in the desired order.
In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8. After application 7 is launched, we get the new arrangement of the icons β€” (123)(457)(68). To launch it...
Input: 8 3 31 2 3 4 5 6 7 87 8 1 | Output: 7
Medium
3
1,974
701
123
5
2,094
B
2094B
B. Bobritto Bandito
800
brute force; constructive algorithms
In Bobritto Bandito's home town of residence, there are an infinite number of houses on an infinite number line, with houses at \(\ldots, -2, -1, 0, 1, 2, \ldots\). On day \(0\), he started a plague by giving an infection to the unfortunate residents of house \(0\). Each succeeding day, the plague spreads to exactly on...
The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) – the number of independent test cases.The only line of each test case contains four integers \(n\), \(m\), \(l\), and \(r\) (\(1 \leq m\leq n \leq 2000, -n \leq l \leq 0 \leq r \leq n, r-l=n\)).
For each test case, output two integers \(l'\) and \(r'\) on a new line. If there are multiple solutions, output any.
In the first test case, it is possible that on the \(1\)-st, \(2\)-nd, and \(3\)-rd days the interval of houses affected is \([-1,0]\), \([-1,1]\), \([-2,1]\). Therefore, \([-1,1]\) is a valid output.
Input: 44 2 -2 24 1 0 43 3 -1 29 8 -6 3 | Output: -1 1 0 1 -1 2 -5 3
Beginner
2
732
260
117
20
1,535
F
1535F
F. String Distance
3,000
binary search; brute force; data structures; hashing; implementation; strings
Suppose you are given two strings \(a\) and \(b\). You can apply the following operation any number of times: choose any contiguous substring of \(a\) or \(b\), and sort the characters in it in non-descending order. Let \(f(a, b)\) the minimum number of operations you have to apply in order to make them equal (or \(f(a...
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of strings.Then \(n\) lines follow, each line contains one of the strings \(s_i\), consisting of lowercase Latin letters. \(|s_1| = |s_2| = \ldots = |s_n|\), and \(n \cdot |s_1| \le 2 \cdot 10^5\). All these strings are pairwise disti...
Print one integer: \(\sum \limits_{i = 1}^{n} \sum\limits_{j = i + 1}^{n} f(s_i, s_j)\).
Input: 4 zzz bac abc acb | Output: 4015
Master
6
911
324
88
15
333
D
333D
D. Characteristics of Rectangles
2,100
binary search; bitmasks; brute force; implementation; sortings
Gerald found a table consisting of n rows and m columns. As a prominent expert on rectangular tables, he immediately counted the table's properties, that is, the minimum of the numbers in the corners of the table (minimum of four numbers). However, he did not like the final value β€” it seemed to be too small. And to mak...
The first line contains two space-separated integers n and m (2 ≀ n, m ≀ 1000). The following n lines describe the table. The i-th of these lines lists the space-separated integers ai, 1, ai, 2, ..., ai, m (0 ≀ ai, j ≀ 109) β€” the m numbers standing in the i-th row of the table.
Print the answer to the problem.
In the first test case Gerald cannot crop the table β€” table contains only two rows and only two columns.In the second test case if we'll crop the table, the table will contain zero in some corner cell. Also initially it contains two zeros in the corner cells, so the answer is 0.
Input: 2 21 23 4 | Output: 1
Hard
5
737
278
32
3
1,284
D
1284D
D. New Year and Conference
2,100
binary search; data structures; hashing; sortings
Filled with optimism, Hyunuk will host a conference about how great this new year will be!The conference will have \(n\) lectures. Hyunuk has two candidate venues \(a\) and \(b\). For each of the \(n\) lectures, the speaker specified two time intervals \([sa_i, ea_i]\) (\(sa_i \le ea_i\)) and \([sb_i, eb_i]\) (\(sb_i \...
The first line contains an integer \(n\) (\(1 \le n \le 100\,000\)), the number of lectures held in the conference.Each of the next \(n\) lines contains four integers \(sa_i\), \(ea_i\), \(sb_i\), \(eb_i\) (\(1 \le sa_i, ea_i, sb_i, eb_i \le 10^9\), \(sa_i \le ea_i, sb_i \le eb_i\)).
Print ""YES"" if Hyunuk will be happy. Print ""NO"" otherwise.You can print each letter in any case (upper or lower).
In second example, lecture set \(\{1, 3\}\) is venue-sensitive. Because participant can't attend this lectures in venue \(a\), but can attend in venue \(b\).In first and third example, venue-sensitive set does not exist.
Input: 2 1 2 3 6 3 4 7 8 | Output: YES
Hard
4
1,588
284
117
12
799
A
799A
A. Carrot Cakes
1,100
brute force; implementation
In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are ready at the same moment t minutes after they started baking. Arkady needs at least n cakes to complete a task, but he currently don't have any. However, he has infinitely many ingredients and one oven. Moreover, Arkady can bui...
The only line contains four integers n, t, k, d (1 ≀ n, t, k, d ≀ 1 000) β€” the number of cakes needed, the time needed for one oven to bake k cakes, the number of cakes baked at the same time, the time needed to build the second oven.
If it is reasonable to build the second oven, print ""YES"". Otherwise print ""NO"".
In the first example it is possible to get 8 cakes in 12 minutes using one oven. The second oven can be built in 5 minutes, so after 6 minutes the first oven bakes 4 cakes, the second oven bakes 4 more ovens after 11 minutes. Thus, it is reasonable to build the second oven. In the second example it doesn't matter wheth...
Input: 8 6 4 5 | Output: YES
Easy
2
798
234
84
7
1,658
C
1658C
C. Shinju and the Lost Permutation
1,700
constructive algorithms; math
Shinju loves permutations very much! Today, she has borrowed a permutation \(p\) from Juju to play with.The \(i\)-th cyclic shift of a permutation \(p\) is a transformation on the permutation such that \(p = [p_1, p_2, \ldots, p_n] \) will now become \( p = [p_{n-i+1}, \ldots, p_n, p_1,p_2, \ldots, p_{n-i}]\).Let's def...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 5 \cdot 10^3\)) β€” the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(c_1,c_2,\ldots,c_n\) (\(1 \l...
For each test case, print ""YES"" if there is a permutation \(p\) exists that satisfies the array \(c\), and ""NO"" otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive response).
In the first test case, the permutation \([1]\) satisfies the array \(c\).In the second test case, the permutation \([2,1]\) satisfies the array \(c\).In the fifth test case, the permutation \([5, 1, 2, 4, 6, 3]\) satisfies the array \(c\). Let's see why this is true. The zeroth cyclic shift of \(p\) is \([5, 1, 2, 4, ...
Input: 61121 222 261 2 4 6 3 562 3 1 2 3 433 2 1 | Output: YES YES NO NO YES NO
Medium
2
1,447
422
275
16
431
D
431D
D. Random Task
2,100
binary search; bitmasks; combinatorics; dp; math
One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: ""Find such positive integer n, that among numbers n + 1, n + 2, ..., 2Β·n there are exactly m numbers which binary representation contains exactly k digits one"".The girl got interested in the t...
The first line contains two space-separated integers, m and k (0 ≀ m ≀ 1018; 1 ≀ k ≀ 64).
Print the required number n (1 ≀ n ≀ 1018). If there are multiple answers, print any of them.
Input: 1 1 | Output: 1
Hard
5
481
89
93
4
226
E
226E
E. Noble Knight's Path
2,900
data structures; trees
In Berland each feudal owns exactly one castle and each castle belongs to exactly one feudal.Each feudal, except one (the King) is subordinate to another feudal. A feudal can have any number of vassals (subordinates).Some castles are connected by roads, it is allowed to move along the roads in both ways. Two castles ha...
The first input line contains integer n (2 ≀ n ≀ 105) β€” the number of feudals. The next line contains n space-separated integers: the i-th integer shows either the number of the i-th feudal's master, or a 0, if the i-th feudal is the King. The third line contains integer m (1 ≀ m ≀ 105) β€” the number of queries.Then fol...
For each second type event print an integer β€” the number of the castle where the knight must stay to rest, or -1, if he will have to cover the distance from ai to bi without a rest. Separate the answers by whitespaces.Print the answers in the order, in which the second type events are given in the input.
In the first sample there is only castle 2 on the knight's way from castle 1 to castle 3. When the knight covers the path 1 - 3 for the first time, castle 2 won't be desecrated by an enemy and the knight will stay there. In the second year the castle 2 will become desecrated, so the knight won't have anywhere to stay f...
Input: 30 1 252 1 3 1 01 22 1 3 1 02 1 3 1 12 1 3 1 2 | Output: 2-1-12
Master
2
1,747
1,254
305
2
1,353
E
1353E
E. K-periodic Garland
1,900
brute force; dp; greedy
You are given a garland consisting of \(n\) lamps. States of the lamps are represented by the string \(s\) of length \(n\). The \(i\)-th character of the string \(s_i\) equals '0' if the \(i\)-th lamp is turned off or '1' if the \(i\)-th lamp is turned on. You are also given a positive integer \(k\).In one move, you ca...
The first line of the input contains one integer \(t\) (\(1 \le t \le 25~ 000\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^6; 1 \le k \le n\)) β€” the length of \(s\) and the required period. The second line of the test...
For each test case, print the answer β€” the minimum number of moves you need to make to obtain \(k\)-periodic garland from the given one.
Input: 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 | Output: 1 2 5 4 0 0
Hard
3
982
501
136
13
1,142
D
1142D
D. Foreigner
2,800
dp
Traveling around the world you noticed that many shop owners raise prices to inadequate values if the see you are a foreigner.You define inadequate numbers as follows: all integers from \(1\) to \(9\) are inadequate; for an integer \(x \ge 10\) to be inadequate, it is required that the integer \(\lfloor x / 10 \rfloor\...
The only line contains the string \(s\) (\(1 \le |s| \le 10^5\)), consisting only of digits. It is guaranteed that the first digit of \(s\) is not zero.
In the only line print the number of substrings of \(s\) that form an inadequate number.
In the first example the inadequate numbers in the string are \(1, 2, 4, 21, 40, 402\). In the second example the inadequate numbers in the string are \(1\) and \(10\), and \(1\) appears twice (on the first and on the second positions).
Input: 4021 | Output: 6
Master
1
1,865
152
88
11
1,956
E2
1956E2
E2. Nene vs. Monsters (Hard Version)
2,700
brute force; greedy; implementation; math
This is the hard version of the problem. The only difference between the versions is the constraints on \(a_i\). You can make hacks only if both versions of the problem are solved.Nene is fighting with \(n\) monsters, located in a circle. These monsters are numbered from \(1\) to \(n\), and the \(i\)-th (\(1 \le i \le ...
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 test cases follows.The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of monsters.The second line contains \(n\) integers \(a_1, a_2, \ldots, ...
For each test case, in the first line output an integer \(m\) β€” the number of monsters with non-zero energy level after \(10^{100}\) uses of the spell; in the second line of output \(m\) integers \(i_1,i_2,\ldots,i_m\) (\(1 \le i_1 < i_2 < \ldots < i_m \le n\)) β€” the indices of these monsters in the increasing order. I...
In the first test case, the following actions happen during the first \(3\) uses of the spell in this order: Nene uses the Attack Your Neighbour spell for the first time; the \(1\)-st monster attacks the \(2\)-nd monster, after the attack the energy level of the \(2\)-nd monster becomes equal to \(\max(0, 5-2)=3\); the...
Input: 532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0 | Output: 1 1 0 1 1 2 1 3 6 1 3 6 8 10 12
Master
4
1,272
483
386
19
173
B
173B
B. Chamber of Secrets
1,800
dfs and similar; shortest paths
""The Chamber of Secrets has been opened again"" β€” this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want an...
The first line of the input contains two integer numbers n and m (2 ≀ n, m ≀ 1000). Each of the next n lines contains m characters. Each character is either ""."" or ""#"" and represents one cell of the Chamber grid. It's ""."" if the corresponding cell is empty and ""#"" if it's a regular column.
Print the minimum number of columns to make magic or -1 if it's impossible to do.
The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk...
Input: 3 3.#.....#. | Output: 2
Medium
2
1,774
298
81
1
109
A
109A
A. Lucky Sum of Digits
1,000
brute force; implementation
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with t...
The single line contains an integer n (1 ≀ n ≀ 106) β€” the sum of digits of the required lucky number.
Print on the single line the result β€” the minimum lucky number, whose sum of digits equals n. If such number does not exist, print -1.
Input: 11 | Output: 47
Beginner
2
328
101
134
1
566
A
566A
A. Matching Names
2,300
dfs and similar; strings; trees
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the ""Hobbit"" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching ...
The first line contains number n (1 ≀ n ≀ 100 000) β€” the number of students in the summer school.Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.The last n lines contain the given pseudonyms. Each pseudonym is a non-empty ...
In the first line print the maximum possible quality of matching pseudonyms to students.In the next n lines describe the optimal matching. Each line must have the form a b (1 ≀ a, b ≀ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.The matching should be ...
The first test from the statement the match looks as follows: bill β†’ bilbo (lcp = 3) galya β†’ galadriel (lcp = 3) gennady β†’ gendalf (lcp = 3) toshik β†’ torin (lcp = 2) boris β†’ smaug (lcp = 0)
Input: 5gennadygalyaborisbilltoshikbilbotoringendalfsmauggaladriel | Output: 114 12 51 35 23 4
Expert
3
882
478
481
5
1,519
E
1519E
E. Off by One
2,700
constructive algorithms; dfs and similar; geometry; graphs; sortings; trees
There are \(n\) points on an infinite plane. The \(i\)-th point has coordinates \((x_i, y_i)\) such that \(x_i > 0\) and \(y_i > 0\). The coordinates are not necessarily integer.In one move you perform the following operations: choose two points \(a\) and \(b\) (\(a \neq b\)); move point \(a\) from \((x_a, y_a)\) to ei...
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of points.The \(i\)-th of the next \(n\) lines contains four integers \(a_i, b_i, c_i, d_i\) (\(1 \le a_i, b_i, c_i, d_i \le 10^9\)). The coordinates of the \(i\)-th point are \(x_i = \frac{a_i}{b_i}\) and \(y_i = \frac{c_i}{d_i}...
In the first line print a single integer \(c\) β€” the maximum number of moves you can perform.Each of the next \(c\) lines should contain a description of a move: two integers \(a\) and \(b\) (\(1 \le a, b \le n\), \(a \neq b\)) β€” the points that are removed during the current move. There should be a way to move points ...
Here are the points and the moves for the ones that get chosen for the moves from the first example:
Input: 7 4 1 5 1 1 1 1 1 3 3 3 3 1 1 4 1 6 1 1 1 5 1 4 1 6 1 1 1 | Output: 3 1 6 2 4 5 7
Master
6
1,158
323
660
15
1,302
I
1302I
I. Deja vu
0
This is an unusual problem in an unusual contest, here is the announcement: http://codeforces.com/blog/entry/73543You run a string shop. During a day your customers want to buy strings of certain lengths and sometimes satisfying other properties.Today your first customer asked you if you have a string of length \(k\). ...
The first line contains two integers \(n\) and \(k\) (\(1\leq k\leq n\leq 200\,000\)). The second line contains a binary string \(s\) of length \(n\).
In the only line print the number of distinct substrings of length \(k\) of the string \(s\).
The second sample test represents the de Bruijn sequence of order 3.
Input: 5 2 01101 | Output: 3
Beginner
0
567
150
93
13
2,013
A
2013A
A. Zhan's Blender
800
constructive algorithms; math
Today, a club fair was held at ""NSPhM"". In order to advertise his pastry club, Zhan decided to demonstrate the power of his blender.To demonstrate the power of his blender, Zhan has \(n\) fruits.The blender can mix up to \(x\) fruits per second.In each second, Zhan can put up to \(y\) fruits into the blender. After t...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(0 \le n \le 10^9\)) β€” the number of fruits Zhan has.The second line of each test case contains t...
For each testcase, output a single integer β€” the minimum number of seconds to blend all fruits.
In the first example, you can first put \(2\) fruits in the blender. After that, the blender will mix these \(2\) fruits, and in the end, there will be \(0\) fruits left in the blender. Then you can put \(3\) fruits into the blender, after which the blender will mix these \(3\) fruits.In the second example, you can put...
Input: 553 431 264 31004 393 3 | Output: 2 3 2 34 3
Beginner
2
572
496
95
20
1,315
C
1315C
C. Restoring Permutation
1,200
greedy
You are given a sequence \(b_1, b_2, \ldots, b_n\). Find the lexicographically minimal permutation \(a_1, a_2, \ldots, a_{2n}\) such that \(b_i = \min(a_{2i-1}, a_{2i})\), or determine that it is impossible.
Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)).The first line of each test case consists of one integer \(n\) β€” the number of elements in the sequence \(b\) (\(1 \le n \le 100\)).The second line of each test case consists of \(n\) different intege...
For each test case, if there is no appropriate permutation, print one number \(-1\).Otherwise, print \(2n\) integers \(a_1, \ldots, a_{2n}\) β€” required lexicographically minimal permutation of numbers from \(1\) to \(2n\).
Input: 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 | Output: 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
Easy
1
207
480
222
13
2,056
F2
2056F2
F2. Xor of Median (Hard Version)
3,000
bitmasks; brute force; combinatorics; dp; math
This is the hard version of the problem. The difference between the versions is that in this version, the constraints on \(t\), \(k\), and \(m\) are higher. You can hack only if you solved all versions of this problem. A sequence \(a\) of \(n\) integers is called good if the following condition holds: Let \(\text{cnt}_...
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 \(k\) and \(m\) (\(1 \le k \le 2 \cdot 10^5\), \(1 \le m \le 10^9\)) β€” the number of bits in \(n\) and ...
For each test case, output a single integer representing the bitwise XOR of the median of all good sequences \(a\) of length \(n\) where \(0\le a_i < m\).
In the first example, \(n = 10_2 = 2\) and \(m = 3\). All possible sequences with elements less than \(m\) are: \([0, 0]\), \([0, 1]\), \([0, 2]\), \([1, 0]\), \([1, 1]\), \([1, 2]\), \([2, 0]\), \([2, 1]\), \([2, 2]\). All of them are good, so the answer is: \(0 \oplus 0 \oplus 0 \oplus 0 \oplus 1 \oplus 1 \oplus 0 \o...
Input: 62 3102 3115 1111017 9110101117 34110010100010100101 10000000001 | Output: 3 2 0 8 32 0
Master
5
1,167
595
154
20
2,023
F
2023F
F. Hills and Pits
3,500
data structures; greedy; math; matrices
In a desert city with a hilly landscape, the city hall decided to level the road surface by purchasing a dump truck. The road is divided into \(n\) sections, numbered from \(1\) to \(n\) from left to right. The height of the surface in the \(i\)-th section is equal to \(a_i\). If the height of the \(i\)-th section is g...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \le n, q \le 3 \cdot 10^5\)) β€” the number of sections and the...
For each query, output the minimum time required to level the sand in the segment \([l_i, r_i]\), or \(-1\) if it is impossible.
In the first test case, \(179\) units of sand need to be added to the only section. However, there is nowhere to take it from, so this is impossible.In the second test case: In the first query, the dump truck can start its journey at the second section. It can take \(2\) units of sand, after which the height in the sec...
Input: 51 1-1791 15 3-2 2 -1 3 -12 41 51 37 11 1 1 -4 1 1 11 77 22 -2 2 -2 1 2 -11 72 74 41000000000 1000000000 999999999 -10000000002 43 42 31 3 | Output: -1 2 5 -1 8 6 6 2 -1 1 2
Master
4
1,645
844
128
20
1,706
B
1706B
B. Making Towers
1,100
dp; greedy; math
You have a sequence of \(n\) colored blocks. The color of the \(i\)-th block is \(c_i\), an integer between \(1\) and \(n\).You will place the blocks down in sequence on an infinite coordinate grid in the following way. Initially, you place block \(1\) at \((0, 0)\). For \(2 \le i \le n\), if the \((i - 1)\)-th block i...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le n\)).It is guaranteed that the su...
For each test case, output \(n\) integers. The \(r\)-th of them should be the maximum size of an tower of color \(r\) you can form by following the given rules. If you cannot form any tower of color \(r\), the \(r\)-th integer should be \(0\).
In the first test case, one of the possible ways to form a tower of color \(1\) and size \(3\) is: place block \(1\) at position \((0, 0)\); place block \(2\) to the right of block \(1\), at position \((1, 0)\); place block \(3\) above block \(2\), at position \((1, 1)\); place block \(4\) to the left of block \(3\), a...
Input: 671 2 3 1 2 3 164 2 2 2 4 41155 4 5 3 563 3 3 1 3 381 2 3 4 4 3 2 1 | Output: 3 2 2 0 0 0 0 0 3 0 2 0 0 1 0 0 1 1 1 1 0 4 0 0 0 2 2 2 2 0 0 0 0
Easy
3
1,062
384
243
17
153
B
153B
B. Binary notation
1,800
*special
You are given a positive integer n. Output its binary notation.
The only line of input data contains an integer n (1 ≀ n ≀ 106).
Output the binary notation of n (without any leading zeros).
In the first example 5 = 1 * 22 + 0 * 21 + 1 * 20.
Input: 5 | Output: 101
Medium
1
63
64
60
1
2,072
D
2072D
D. For Wizards, the Exam Is Easy, but I Couldn't Handle It
1,300
brute force; greedy; implementation
Akito got tired of being a simple locksmith at a bank, so he decided to enroll in the Magical Academy and become the best wizard in the world! However, to enroll, he needed to solve a single problem on the exam, which the ambitious hero could not manage.In the problem, he was given an array \(a\) of length \(n\). He ne...
The first line of input contains a number \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.In the first line of each test case, there is a number \(n\) (\(1 \le n \le 2000\)) β€” the length of the array \(a\).In the second line of each test case, there are \(n\) numbers \(a_i\) (\(1 \le a_i \le 2000\)) β€” the eleme...
For each test case, output two numbers \(l\) and \(r\) (\(1 \le l \le r \le n\)) β€” the boundaries of the subarray that should be chosen so that after applying the spell, the number of inversions in the array is minimized.If there are multiple suitable pairs of boundaries, you may output any of them.
In the first example, the array \([1, 4, 3, 2, 5, 3, 3]\) will turn into the array \([1, 3, 2, 5, 3, 3, 4]\). The inversions in it are \((2, 3)\), \((4, 5)\), \((4, 6)\) and \((4, 7)\). It can be shown that it is not possible to achieve fewer than \(4\) inversions.In the second example, the array \([1, 4, 3, 2, 5, 3]\)...
Input: 971 4 3 2 5 3 361 4 3 2 5 387 6 5 8 4 3 2 1101 1 1 5 1 1 5 6 7 821337 6942 1 2 13998 244 35331 2 191 1 2 3 5 8 13 21 34 | Output: 2 7 2 4 1 8 4 6 1 2 1 4 1 3 2 3 5 5
Easy
3
1,520
439
300
20
2,001
B
2001B
B. Generate Permutation
800
constructive algorithms
There is an integer sequence \(a\) of length \(n\), where each element is initially \(-1\).Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to \(1\), and another writes letters from right to left with a pointer initially pointing to \(n\).Misuki would c...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 500\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of the permutation.It i...
For each test case, output a line of \(n\) integers, representing the permutation \(p\) of length \(n\) such that the minimum number of carriage return operations needed to make \(a = p\) is the same no matter which typewriter Misuki is using, or \(-1\) if it is impossible to do so.If there are multiple valid permutati...
In the first testcase, it's possible to make \(a = p = [1]\) using \(0\) carriage return operations.In the second testcase, it is possible to make \(a = p = [1, 2]\) with the minimal number of carriage returns as follows:If Misuki is using the first typewriter: Write number: write \(1\) to \(a_1\), \(a\) becomes \([1, ...
Input: 3 1 2 3 | Output: 1 -1 3 1 2
Beginner
1
1,307
408
352
20
1,627
A
1627A
A. Not Shading
800
constructive algorithms; implementation
There is a grid with \(n\) rows and \(m\) columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers \(r\) an...
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains four integers \(n\), \(m\), \(r\), and \(c\) (\(1 \leq n, m \leq 50\); \(1 \leq r \leq n\); \(1 \leq...
For each test case, if it is impossible to make the cell in row \(r\) and column \(c\) black, output \(-1\). Otherwise, output a single integer β€” the minimum number of operations required to make the cell in row \(r\) and column \(c\) black.
The first test case is pictured below. We can take the black cell in row \(1\) and column \(2\), and make all cells in its row black. Therefore, the cell in row \(1\) and column \(4\) will become black. In the second test case, the cell in row \(2\) and column \(1\) is already black.In the third test case, it is imposs...
Input: 93 5 1 4WBWWWBBBWBWWBBB4 3 2 1BWWBBWWBBWWB2 3 2 2WWWWWW2 2 1 1WWWB5 9 5 9WWWWWWWWWWBWBWBBBWWBBBWWBWWWBWBWBBBWWWWWWWWWW1 1 1 1B1 1 1 1W1 2 1 1WB2 1 1 1WB | Output: 1 0 -1 2 2 0 -1 1 1
Beginner
2
465
611
241
16
11
C
11C
C. How Many Squares?
2,200
implementation
You are given a 0-1 rectangular matrix. What is the number of squares in it? A square is a solid square frame (border) with linewidth equal to 1. A square should be at least 2 Γ— 2. We are only interested in two types of squares: squares with each side parallel to a side of the matrix; squares with each side parallel to...
The first line contains integer t (1 ≀ t ≀ 10000), where t is the number of test cases in the input. Then test cases follow. Each case starts with a line containing integers n and m (2 ≀ n, m ≀ 250), where n is the number of rows and m is the number of columns. The following n lines contain m characters each (0 or 1).T...
You should output exactly t lines, with the answer to the i-th test case on the i-th line.
Input: 28 8000100010010100001000100100000100100010000101000110100111100001110 101111111000100000100010110010001011001010100000110110010010101010101000100100100010000010001111111000 | Output: 12
Hard
1
779
406
90
0
820
B
820B
B. Mister B and Angle in Polygon
1,300
constructive algorithms; geometry; math
On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).That's why Mister B decided to use this polygon. Now Mister B must find th...
First and only line contains two space-separated integers n and a (3 ≀ n ≀ 105, 1 ≀ a ≀ 180) β€” the number of vertices in the polygon and the needed angle, in degrees.
Print three space-separated integers: the vertices v1, v2, v3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.
In first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct.Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45 - 67| < |90 - 67|. Other correct answers are: ""3 1...
Input: 3 15 | Output: 1 2 3
Easy
3
609
166
193
8
1,969
B
1969B
B. Shifts and Sorting
1,000
constructive algorithms; greedy
Let's define a cyclic shift of some string \(s\) as a transformation from \(s_1 s_2 \dots s_{n-1} s_{n}\) into \(s_{n} s_1 s_2 \dots s_{n-1}\). In other words, you take one last character \(s_n\) and place it before the first character while moving all other characters to the right.You are given a binary string \(s\) (...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first and only line of each test case contains a binary string \(s\) (\(2 \le |s| \le 2 \cdot 10^5\); \(s_i \in\) {0, 1}) β€” the string you need to sort.Additional constraint on the input: the sum of lengths of strings o...
For each test case, print the single integer β€” the minimum total cost to make string sorted using operation above any number of times.
In the first test case, you can choose the whole string and perform a cyclic shift: 10 \(\rightarrow\) 01. The length of the substring is \(2\), so the cost is \(2\).In the second test case, the string is already sorted, so you don't need to perform any operations.In the third test case, one of the optimal strategies i...
Input: 51000001100010101101101001 | Output: 2 0 9 5 11
Beginner
2
714
371
134
19
1,198
C
1198C
C. Matching vs Independent Set
2,000
constructive algorithms; graphs; greedy; sortings
You are given a graph with \(3 \cdot n\) vertices and \(m\) edges. You are to find a matching of \(n\) edges, or an independent set of \(n\) vertices.A set of edges is called a matching if no two edges share an endpoint.A set of vertices is called an independent set if no two vertices are connected with an edge.
The first line contains a single integer \(T \ge 1\) β€” the number of graphs you need to process. The description of \(T\) graphs follows.The first line of description of a single graph contains two integers \(n\) and \(m\), where \(3 \cdot n\) is the number of vertices, and \(m\) is the number of edges in the graph (\(...
Print your answer for each of the \(T\) graphs. Output your answer for a single graph in the following format.If you found a matching of size \(n\), on the first line print ""Matching"" (without quotes), and on the second line print \(n\) integers β€” the indices of the edges in the matching. The edges are numbered from ...
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 ...
Input: 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 | Output: Matching 2 IndSet 1 IndSet 2 4 Matching 1 15
Hard
4
313
832
937
11
2,032
C
2032C
C. Trinity
1,400
binary search; math; sortings; two pointers
You are given an array \(a\) of \(n\) elements \(a_1, a_2, \ldots, a_n\).You can perform the following operation any number (possibly \(0\)) of times: Choose two integers \(i\) and \(j\), where \(1 \le i, j \le n\), and assign \(a_i := a_j\). Find the minimum number of operations required to make the array \(a\) satisf...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β€” the number of elements in the array \(a...
For each test case, output a single integer β€” the minimum number of operations required.
In the first test case, one of the possible series of operations would be: Assign \(a_1 := a_4 = 4\). The array will become \([4, 2, 3, 4, 5, 6, 7]\). Assign \(a_2 := a_5 = 5\). The array will become \([4, 5, 3, 4, 5, 6, 7]\). Assign \(a_7 := a_1 = 4\). The array will become \([4, 5, 3, 4, 5, 6, 4]\). It can be proven ...
Input: 471 2 3 4 5 6 731 3 234 5 3159 3 8 1 6 5 3 8 2 1 4 2 9 4 7 | Output: 3 1 0 8
Easy
4
621
558
88
20
1,217
D
1217D
D. Coloring Edges
2,100
constructive algorithms; dfs and similar; graphs
You are given a directed graph with \(n\) vertices and \(m\) directed edges without self-loops or multiple edges.Let's denote the \(k\)-coloring of a digraph as following: you color each edge in one of \(k\) colors. The \(k\)-coloring is good if and only if there no cycle formed by edges of same color.Find a good \(k\)...
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 5000\), \(1 \le m \le 5000\)) β€” the number of vertices and edges in the digraph, respectively.Next \(m\) lines contain description of edges β€” one per line. Each edge is a pair of integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) β€” there is d...
In the first line print single integer \(k\) β€” the number of used colors in a good \(k\)-coloring of given graph.In the second line print \(m\) integers \(c_1, c_2, \dots, c_m\) (\(1 \le c_i \le k\)), where \(c_i\) is a color of the \(i\)-th edge (in order as they are given in the input).If there are multiple answers p...
Input: 4 5 1 2 1 3 3 4 2 4 1 4 | Output: 1 1 1 1 1 1
Hard
3
375
459
372
12
1,675
F
1675F
F. Vlad and Unfinished Business
1,800
dfs and similar; dp; greedy; trees
Vlad and Nastya live in a city consisting of \(n\) houses and \(n-1\) road. From each house, you can get to the other by moving only along the roads. That is, the city is a tree.Vlad lives in a house with index \(x\), and Nastya lives in a house with index \(y\). Vlad decided to visit Nastya. However, he remembered tha...
The first line of input contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of input test cases. There is an empty line before each test case.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2\cdot 10^5\)) β€” the number of houses and things, respectively.The second li...
Output \(t\) lines, each of which contains the answer to the corresponding test case of input. As an answer output single integer β€” the minimum number of minutes Vlad needs on the road to do all the things and come to Nastya.
Tree and best path for the first test case: \(1 \rightarrow 2 \rightarrow 1 \rightarrow 3\) Tree and best path for the second test case: \(3 \rightarrow 1 \rightarrow 3 \rightarrow 5 \rightarrow 2 \rightarrow 5 \rightarrow 6 \rightarrow 5\) Tree and best path for the third test case: \(3 \rightarrow 5 \rightarrow 2\)
Input: 33 11 321 31 26 43 51 6 2 11 33 43 55 65 26 23 25 31 33 43 55 65 2 | Output: 3 7 2
Medium
4
867
886
225
16
1,223
G
1223G
G. Wooden Raft
3,200
binary search; math; number theory
Suppose you are stuck on a desert island. The only way to save yourself is to craft a wooden raft and go to the sea. Fortunately, you have a hand-made saw and a forest nearby. Moreover, you've already cut several trees and prepared it to the point that now you have \(n\) logs and the \(i\)-th log has length \(a_i\).The...
The first line contains the only integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β€” the number of logs you have.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(2 \le a_i \le 5 \cdot 10^5\)) β€” the corresponding lengths of the logs.It's guaranteed that you can always craft at least \(2 \times 2\) raft.
Print the only integer β€” the maximum area of the raft you can craft.
In the first example, you can cut the log of the length \(9\) in \(5\) parts: \(2 + 2 + 2 + 2 + 1\). Now you can build \(2 \times 2\) raft using \(2\) logs of length \(x = 2\) and \(x = 2\) logs of length \(y = 2\).In the second example, you can cut \(a_4 = 18\) into two pieces \(9 + 9\) and \(a_8 = 28\) in three piece...
Input: 1 9 | Output: 4
Master
3
828
317
68
12
1,533
D
1533D
D. String Searching
0
*special; hashing
You are given an array \(s\) consisting of \(n\) different strings. Each string consists of \(m\) lowercase Latin letters.You have to respond to \(q\) queries. Each query contains a string \(t\) of length \(m+1\). Count the number of indices \(i\), such that the string \(t\) can be obtained from the string \(s_i\), if ...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5\); \(1 \le m \le 10\)) β€” the number of strings in the array and the length of each string.The following \(n\) lines contain strings \(s_i\). All of the given strings are different.The next line contains a single integer \(q\) (\(1 \le q \le 10^5\)...
For each query, print the number of indices \(i\), such that a string from the query can be obtained from the string \(s_i\), if it is allowed to insert one letter in an arbitrary position.
Explanation of the first test of the example: the string a can be transformed into aa by inserting one letter; both strings a and c can be transformed into ca by inserting one letter; neither a nor c can be transformed into mm by inserting one letter; c can be transformed into cf by inserting one letter.
Input: 2 1 a c 4 aa ca mm cf | Output: 1 2 0 1
Beginner
2
380
420
189
15
1,740
G
1740G
G. Dangerous Laser Power
3,100
constructive algorithms; dsu; sortings
Pak Chanek has an \(n \times m\) grid of portals. The portal on the \(i\)-th row and \(j\)-th column is denoted as portal \((i,j)\). The portals \((1,1)\) and \((n,m)\) are on the north-west and south-east corner of the grid respectively.The portal \((i,j)\) has two settings: Type \(t_{i,j}\), which is either \(0\) or ...
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) β€” the number of rows and columns in the grid.The \(i\)-th of the next \(n\) lines contains \(m\) integers, with the \(j\)-th integer being \(s_{i,j}\) (\(1 \leq s_{i,j} \leq 10^9\)) β€” the strength of portal \((i, j)\).
Print \(n\) lines with each line containing a string of length \(m\) consisting of characters \(0\) or \(1\) representing the type settings. The \(j\)-th character in the \(i\)-th string is the type setting of portal \((i, j)\).If there are multiple solutions, you can output any of them.
In the first example, let's consider the laser Pak Chanek shoots into face \(1\) of portal \((2, 2)\). The laser travels as follows: The laser enters face \(1\) of portal \((2, 2)\) with speed \(1\). It leaves the portal going out of face \(3\) with speed \(5\). Portal \((2, 2)\) consumes \(4\) units of energy. The las...
Input: 2 3 8 8 2 6 5 7 | Output: 110 100
Master
3
1,381
297
288
17
1,408
B
1408B
B. Arrays Sum
1,400
constructive algorithms; greedy; math
You are given a non-decreasing array of non-negative integers \(a_1, a_2, \ldots, a_n\). Also you are given a positive integer \(k\).You want to find \(m\) non-decreasing arrays of non-negative integers \(b_1, b_2, \ldots, b_m\), such that: The size of \(b_i\) is equal to \(n\) for all \(1 \leq i \leq m\). For all \(1 ...
The first line contains one integer \(t\) (\(1 \leq t \leq 100\)): the number of test cases.The first line of each test case contains two integers \(n\), \(k\) (\(1 \leq n \leq 100\), \(1 \leq k \leq n\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_1 \leq a_2 \leq \ldots \leq a_n \leq ...
For each test case print a single integer: the minimum possible value of \(m\). If there is no such \(m\), print \(-1\).
In the first test case, there is no possible \(m\), because all elements of all arrays should be equal to \(0\). But in this case, it is impossible to get \(a_4 = 1\) as the sum of zeros.In the second test case, we can take \(b_1 = [3, 3, 3]\). \(1\) is the smallest possible value of \(m\).In the third test case, we ca...
Input: 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 | Output: -1 1 2 2 2 1
Easy
3
633
340
120
14
29
C
29C
C. Mail Stamps
1,700
data structures; dfs and similar; graphs; implementation
One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city Β«AΒ» to city Β«BΒ», they stamp it with Β«A BΒ», or Β«B AΒ». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sen...
The first line contains integer n (1 ≀ n ≀ 105) β€” amount of mail stamps on the envelope. Then there follow n lines with two integers each β€” description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of all the c...
Output n + 1 numbers β€” indexes of cities in one of the two possible routes of the letter.
Input: 21 100100 2 | Output: 2 100 1
Medium
4
815
543
89
0
717
A
717A
A. Festival Organization
2,900
combinatorics; math; number theory
The Prodiggers are quite a cool band and for this reason, they have been the surprise guest at the ENTER festival for the past 80 years. At the beginning of their careers, they weren’t so successful, so they had to spend time digging channels to earn money; hence the name. Anyway, they like to tour a lot and have surpr...
The first line of the input contains three integers k, l and r (1 ≀ k ≀ 200, 1 ≀ l ≀ r ≀ 1018).
Output a single number: the number of ways to select k different tours of the same length, modulo 1 000 000 007.
Input: 1 1 2 | Output: 5
Master
3
1,144
95
112
7
991
A
991A
A. If at first you don't succeed...
1,000
implementation
Each student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn't pass it. However, many of Vasya's fellow students from the same group were more successful and celebrated after the exam.Some of them celebrated in the BugDonalds restaurant, some of them β€”...
The first line contains four integers β€” \(A\), \(B\), \(C\) and \(N\) (\(0 \leq A, B, C, N \leq 100\)).
If a distribution of \(N\) students exists in which \(A\) students visited BugDonalds, \(B\) β€” BeaverKing, \(C\) β€” both of the restaurants and at least one student is left home (it is known that Vasya didn't pass the exam and stayed at home), output one integer β€” amount of students (including Vasya) who did not pass th...
The first sample describes following situation: \(5\) only visited BugDonalds, \(5\) students only visited BeaverKing, \(5\) visited both of them and \(5\) students (including Vasya) didn't pass the exam.In the second sample \(2\) students only visited BugDonalds and \(2\) only visited BeaverKing, but that means all \(...
Input: 10 10 5 20 | Output: 5
Beginner
1
1,094
103
489
9
1,177
B
1177B
B. Digits Sequence (Hard Edition)
1,800
binary search; divide and conquer; implementation
Let's write all the positive integer numbers one after another from \(1\) without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...Your task is to print the \(k\)-th digit of this sequence.
The first and only line contains integer \(k\) (\(1 \le k \le 10^{12}\)) β€” the position to process (\(1\)-based index).
Print the \(k\)-th digit of the resulting infinite sequence.
Input: 7 | Output: 7
Medium
3
295
119
60
11
802
A
802A
A. Heidi and Library (easy)
1,800
greedy
Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.We will look a...
The first line of input will contain two integers n and k (1 ≀ n, k ≀ 80). The second line will contain n integers a1, a2, ..., an (1 ≀ ai ≀ n) – the sequence of book requests.
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.In the third ...
Input: 4 801 2 2 1 | Output: 2
Medium
1
1,574
176
99
8
620
B
620B
B. Grandfather Dovlet’s calculator
1,000
implementation
Once Max found an electronic calculator from his grandfather Dovlet's chest. He noticed that the numbers were written with seven-segment indicators (https://en.wikipedia.org/wiki/Seven-segment_display). Max starts to type all the values from a to b. After typing each number Max resets the calculator. Find the total num...
The only line contains two integers a, b (1 ≀ a ≀ b ≀ 106) β€” the first and the last number typed by Max.
Print the only integer a β€” the total number of printed segments.
Input: 1 3 | Output: 12
Beginner
1
547
104
64
6
808
B
808B
B. Average Sleep Time
1,300
data structures; implementation; math
It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept ...
The first line contains two integer numbers n and k (1 ≀ k ≀ n ≀ 2Β·105).The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 105).
Output average sleeping time over all weeks. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 6. In particular, it is enough to output real number with at least 6 digits after the decimal point.
In the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7.
Input: 3 23 4 7 | Output: 9.0000000000
Easy
3
910
146
239
8
2,117
F
2117F
F. Wildflower
1,800
combinatorics; dfs and similar; trees
Yousef has a rooted tree\(^{\text{βˆ—}}\) consisting of exactly \(n\) vertices, which is rooted at vertex \(1\). You would like to give Yousef an array \(a\) of length \(n\), where each \(a_i\) \((1 \le i \le n)\) can either be \(1\) or \(2\).Let \(s_u\) denote the sum of \(a_v\) where vertex \(v\) is in the subtree\(^{\...
The first line contains an integer \(t\) \((1 \le t \le 10^4)\) β€” the number of test cases.Each test case consists of several lines. The first line of the test case contains an integer \(n\) \((2 \le n \le 2 \cdot 10^5)\) β€” the number of vertices in the tree.Then \(nβˆ’1\) lines follow, each of them contains two integers...
For each test case, print one integer \(x\) β€” the number of different arrays \(a\) that result in the tree being special, modulo \(10^9 + 7\).
The tree given in the fifth test case:
Input: 721 281 22 33 82 44 55 66 7101 22 33 44 55 64 77 84 99 1071 44 23 23 52 66 771 22 33 43 54 66 775 74 61 61 32 66 753 41 21 32 5 | Output: 4 24 0 16 48 0 4
Medium
3
1,001
598
142
21
1,398
D
1398D
D. Colored Rectangles
1,800
dp; greedy; sortings
You are given three multisets of pairs of colored sticks: \(R\) pairs of red sticks, the first pair has length \(r_1\), the second pair has length \(r_2\), \(\dots\), the \(R\)-th pair has length \(r_R\); \(G\) pairs of green sticks, the first pair has length \(g_1\), the second pair has length \(g_2\), \(\dots\), the ...
The first line contains three integers \(R\), \(G\), \(B\) (\(1 \le R, G, B \le 200\)) β€” the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.The second line contains \(R\) integers \(r_1, r_2, \dots, r_R\) (\(1 \le r_i \le 2000\)) β€” the lengths of sticks in each...
Print the maximum possible total area of the constructed rectangles.
In the first example you can construct one of these rectangles: red and green with sides \(3\) and \(5\), red and blue with sides \(3\) and \(4\) and green and blue with sides \(5\) and \(4\). The best area of them is \(4 \times 5 = 20\).In the second example the best rectangles are: red/blue \(9 \times 8\), red/blue \...
Input: 1 1 1 3 5 4 | Output: 20
Medium
3
1,054
624
68
13
364
C
364C
C. Beautiful Set
2,300
brute force; number theory
We'll call a set of positive integers a beautiful if the following condition fulfills: for any prime p, if , then . In other words, if one number from the set is divisible by prime p, then at least half of numbers from the set is divisible by p.Your task is to find any beautiful set, where the number of elements is equ...
The first line contains integer k (10 ≀ k ≀ 5000) that shows how many numbers the required beautiful set should have.
In the first line print k space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them.
Input: 10 | Output: 16 18 24 27 36 48 54 72 108 144
Expert
2
364
117
147
3
1,668
A
1668A
A. Direction Change
800
implementation; math
You are given a grid with \(n\) rows and \(m\) columns. Rows and columns are numbered from \(1\) to \(n\), and from \(1\) to \(m\). The intersection of the \(a\)-th row and \(b\)-th column is denoted by \((a, b)\). Initially, you are standing in the top left corner \((1, 1)\). Your goal is to reach the bottom right cor...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β€” the number of the test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^9\)) β€” the size of the grid.
For each test case, print a single integer: \(-1\) if it is impossible to reach \((n, m)\) under the given conditions, otherwise the minimum number of moves.
Test case \(1\): \(n=1\), \(m=1\), and initially you are standing in \((1, 1)\) so \(0\) move is required to reach \((n, m) = (1, 1)\).Test case \(2\): you should go down to reach \((2, 1)\).Test case \(3\): it is impossible to reach \((1, 3)\) without moving right two consecutive times, or without leaving the grid.Tes...
Input: 61 12 11 34 24 610 5 | Output: 0 1 -1 6 10 17
Beginner
2
623
307
157
16
1,980
D
1980D
D. GCD-sequence
1,400
greedy; implementation; math; number theory
GCD (Greatest Common Divisor) of two integers \(x\) and \(y\) is the maximum integer \(z\) by which both \(x\) and \(y\) are divisible. For example, \(GCD(36, 48) = 12\), \(GCD(5, 10) = 5\), and \(GCD(7,11) = 1\).Kristina has an array \(a\) consisting of exactly \(n\) positive integers. She wants to count the GCD of ea...
The first line of input data contains a single number \(t\) (\(1 \le t \le 10^4\)) β€” he number of test cases in the test.This is followed by the descriptions of the test cases.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β€” the number of elements in the array \(a\).The ...
For each test case, output a single line: ""YES"" if you can remove exactly one number from the array \(a\) so that the GCD-sequence of \(b\) is non-decreasing; ""NO"" otherwise. You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will all be recognized as a positive a...
The first test case is explained in the problem statement.
Input: 12620 6 12 3 48 36412 6 3 4310 12 3532 16 8 4 25100 50 2 10 2042 4 8 1107 4 6 2 4 5 1 4 2 875 9 6 8 5 9 2611 14 8 12 9 395 7 3 10 6 3 12 6 334 2 481 6 11 12 6 12 3 6 | Output: YES NO YES NO YES YES NO YES YES YES YES YES
Easy
4
1,082
536
327
19
1,992
C
1992C
C. Gorilla and Permutation
900
constructive algorithms; math
Gorilla and Noobish_Monk found three numbers \(n\), \(m\), and \(k\) (\(m < k\)). They decided to construct a permutation\(^{\dagger}\) of length \(n\).For the permutation, Noobish_Monk came up with the following function: \(g(i)\) is the sum of all the numbers in the permutation on a prefix of length \(i\) that are no...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The only line of each case contains three integers \(n\), \(m\), \(k\) (\(2\le n \le 10^5\); \(1 \le m < k \le n\)) β€” the size of the permutation to be constructed and two integers.It is guaranteed that the sum of \(n\) ove...
For each test case, output the permutation β€” a set of numbers that satisfies the conditions of the problem. If there are multiple solutions, output any of them.
In the first example, \(\left(\sum_{i=1}^n f(i) - \sum_{i=1}^n g(i)\right) = 5 \cdot 5 - (0 \cdot 3 + 1 + 3) = 25 - 4 = 21\)
Input: 35 2 53 1 310 3 8 | Output: 5 3 4 1 2 3 2 1 10 9 8 4 7 5 6 1 2 3
Beginner
2
1,516
370
160
19
390
E
390E
E. Inna and Large Sweet Matrix
0
Inna loves sweets very much. That's why she wants to play the ""Sweet Matrix"" game with Dima and Sereja. But Sereja is a large person, so the game proved small for him. Sereja suggested playing the ""Large Sweet Matrix"" game.The ""Large Sweet Matrix"" playing field is an n Γ— m matrix. Let's number the rows of the mat...
The first line of the input contains three integers n, m and w (3 ≀ n, m ≀ 4Β·106; 1 ≀ w ≀ 105).The next w lines describe the moves that were made in the game. A line that describes an event of the first type contains 6 integers: 0, x1, y1, x2, y2 and v (1 ≀ x1 ≀ x2 ≀ n; 1 ≀ y1 ≀ y2 ≀ m; 1 ≀ v ≀ 109). A line that descri...
For each second type move print a single integer on a single line β€” the difference between Dima and Inna's numbers.
Note to the sample. After the first query the matrix looks as: 2220022200 0000000000After the second one it is: 222002550003300 00000After the third one it is: 22201255010330100001For the fourth query, Dima's sum equals 5 + 0 + 3 + 0 = 8 and Inna's sum equals 4 + 1 + 0 + 1 = 6. The answer to the query equals 8 - 6 = 2....
Input: 4 5 50 1 1 2 3 20 2 2 3 3 30 1 5 4 5 11 2 3 3 41 3 4 3 4 | Output: 2-21
Beginner
0
1,333
689
115
3
675
A
675A
A. Infinite Sequence
1,100
math
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, the...
The first line of the input contain three integers a, b and c ( - 109 ≀ a, b, c ≀ 109) β€” the first element of the sequence, Vasya's favorite number and the difference between any two neighbouring elements of the sequence, respectively.
If b appears in the sequence s print ""YES"" (without quotes), otherwise print ""NO"" (without quotes).
In the first sample, the sequence starts from integers 1, 4, 7, so 7 is its element.In the second sample, the favorite integer of Vasya is equal to the first element of the sequence.In the third sample all elements of the sequence are greater than Vasya's favorite integer.In the fourth sample, the sequence starts from ...
Input: 1 7 3 | Output: YES
Easy
1
419
235
103
6
925
A
925A
A. Stairs and Elevators
1,600
binary search
In the year of \(30XX\) participants of some world programming championship live in a single large hotel. The hotel has \(n\) floors. Each floor has \(m\) sections with a single corridor connecting all of them. The sections are enumerated from \(1\) to \(m\) along the corridor, and all sections with equal numbers on di...
The first line contains five integers \(n, m, c_l, c_e, v\) (\(2 \leq n, m \leq 10^8\), \(0 \leq c_l, c_e \leq 10^5\), \(1 \leq c_l + c_e \leq m - 1\), \(1 \leq v \leq n - 1\)) β€” the number of floors and section on each floor, the number of stairs, the number of elevators and the maximum speed of an elevator, respectiv...
Print \(q\) integers, one per line β€” the answers for the queries.
In the first query the optimal way is to go to the elevator in the 5-th section in four time units, use it to go to the fifth floor in two time units and go to the destination in one more time unit.In the second query it is still optimal to use the elevator, but in the third query it is better to use the stairs in the ...
Input: 5 6 1 1 32531 1 5 61 3 5 43 3 5 3 | Output: 754
Medium
1
1,376
1,245
65
9
288
C
288C
C. Polo the Penguin and XOR operation
1,700
implementation; math
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.For permutation p = p0, p1, ..., pn, Polo has defined its beauty β€” number .Expression means applying the operation of bitwise excluding ""OR"" to numbers x and y. This operation exists in all modern programm...
The single line contains a positive integer n (1 ≀ n ≀ 106).
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.If there are several suitable permutations, you are allowed to print any of them.
Input: 4 | Output: 200 2 1 4 3
Medium
2
528
60
236
2
994
A
994A
A. Fingerprints
800
implementation
You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits.Some keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequ...
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10\)) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints.The next line contains \(n\) distinct space-separated integers \(x_1, x_2, \ldots, x_n\) (\(0 \le x_i \le 9\)) representing ...
In a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable.
In the first example, the only digits with fingerprints are \(1\), \(2\) and \(7\). All three of them appear in the sequence you know, \(7\) first, then \(1\) and then \(2\). Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence.In the second exampl...
Input: 7 33 5 7 1 6 2 81 2 7 | Output: 7 1 2
Beginner
1
436
473
190
9
1,920
F2
1920F2
F2. Smooth Sailing (Hard Version)
3,000
binary search; data structures; dsu; geometry; graphs; trees
The only difference between the two versions of this problem is the constraint on \(q\). You can make hacks only if both versions of the problem are solved.Thomas is sailing around an island surrounded by the ocean. The ocean and island can be represented by a grid with \(n\) rows and \(m\) columns. The rows are number...
The first line contains three integers \(n\), \(m\), and \(q\) (\(3 \leq n, m \leq 10^5\), \(9 \leq n \cdot m \leq 3 \cdot 10^5\), \(1 \leq q \leq 3 \cdot 10^5\)) β€” the number of rows and columns of the grid and the number of queries. Each of the following \(n\) lines contains \(m\) characters describing the cells of t...
For each query, output a single integer β€” the maximum safety of a round trip starting from the specified position.
For the first example, the image below shows an optimal round trip starting from \((1, 1)\). The round trip has a safety of \(3\) as the minimum Manhattan distance from a cell on the round trip to an underwater volcano is \(3\). Example of an optimal round trip For the fourth example, remember that it is allowed for Th...
Input: 9 9 3 ......... ......... ....###.. ...v#.... ..###.... ...##...v ...##.... ......... v........ 1 1 9 1 5 7 | Output: 3 0 3
Master
6
3,081
1,166
114
19
413
B
413B
B. Spyke Chatting
1,300
implementation
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messa...
The first line contains three space-separated integers n, m and k (2 ≀ n ≀ 2Β·104; 1 ≀ m ≀ 10; 1 ≀ k ≀ 2Β·105) β€” the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n Γ— m, consisting of numbers zero and one. The element of this matri...
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
Input: 3 4 51 1 1 11 0 1 11 1 0 01 13 11 32 43 2 | Output: 3 3 1
Easy
1
992
936
145
4
1,860
C
1860C
C. Game on Permutation
1,400
data structures; dp; games; greedy
Alice and Bob are playing a game. They have a permutation \(p\) of size \(n\) (a permutation of size \(n\) is an array of size \(n\) where each element from \(1\) to \(n\) occurs exactly once). They also have a chip, which can be placed on any element of the permutation.Alice and Bob make alternating moves: Alice makes...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) – the number of elements in the permutation.The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n...
For each test case, print a single integer β€” the number of lucky elements in the permutation.
In the first test case of the example, the \(3\)-rd element of the permutation is lucky.In the second test case of the example, there are no lucky elements.In the third test case of the example, the \(2\)-nd element of the permutation is lucky.In the fourth test case of the example, the \(3\)-rd and the \(4\)-th elemen...
Input: 432 1 322 131 2 342 1 4 3 | Output: 1 0 1 2
Easy
4
1,252
420
93
18
1,857
E
1857E
E. Power of Points
1,500
math; sortings
You are given \(n\) points with integer coordinates \(x_1,\dots x_n\), which lie on a number line.For some integer \(s\), we construct segments [\(s,x_1\)], [\(s,x_2\)], \(\dots\), [\(s,x_n\)]. Note that if \(x_i<s\), then the segment will look like [\(x_i,s\)]. The segment [\(a, b\)] covers all integer points \(a, a+1...
The first line contains an integer \(t\) (\(1\le t\le 10^4\)) β€” the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2\cdot 10^5\)) β€” the number of points.The second line contains \(n\) integers \(x_1,x_2 \dots x_n\) (\(1 \le x_i \le 10^9\)) β€” the coordinates of the points....
For each test case, output \(n\) integers, where the \(i\)-th integer is equal to the sum of the powers of all points for \(s=x_i\).
In the first test case we first choose \(s=x_1=1\), then the following segments are formed: \([1,1]\),\([1,4]\),\([1,3]\). The powers of the points will be as follows: \(f_1=3, f_2=2, f_3=2, f_4=1, f_5=0 \dots\) The sum of powers of the points: \(3+2+2+1+0+\dots+0=8\). After that we choose \(s=x_2=4\). Then there will ...
Input: 331 4 351 2 5 7 141 10 100 1000 | Output: 8 7 6 16 15 18 24 16 1111 1093 1093 2893
Medium
2
945
425
132
18
513
G1
513G1
G1. Inversions problem
1,800
brute force; dfs and similar; dp; meet-in-the-middle
You are given a permutation of n numbers p1, p2, ..., pn. We perform k operations of the following type: choose uniformly at random two indices l and r (l ≀ r) and reverse the order of the elements pl, pl + 1, ..., pr. Your task is to find the expected value of the number of inversions in the resulting permutation.
The first line of input contains two integers n and k (1 ≀ n ≀ 100, 1 ≀ k ≀ 109). The next line contains n integers p1, p2, ..., pn β€” the given permutation. All pi are different and in range from 1 to n.The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some...
Output the answer with absolute or relative error no more than 1e - 9.
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability , the interval will consist of a single element and the permutation will not be altered. With probability we will inverse the first two element...
Input: 3 11 2 3 | Output: 0.833333333333333
Medium
4
316
655
70
5
1,575
H
1575H
H. Holiday Wall Ornaments
2,200
dp; strings
The Winter holiday will be here soon. Mr. Chanek wants to decorate his house's wall with ornaments. The wall can be represented as a binary string \(a\) of length \(n\). His favorite nephew has another binary string \(b\) of length \(m\) (\(m \leq n\)).Mr. Chanek's nephew loves the non-negative integer \(k\). His nephe...
The first line contains two integers \(n\) and \(m\) (\(1 \leq m \leq n \leq 500\)) β€” size of the binary string \(a\) and \(b\) respectively.The second line contains a binary string \(a\) of length \(n\).The third line contains a binary string \(b\) of length \(m\).
Output \(n - m + 2\) integers β€” the \((k+1)\)-th integer denotes the minimal number of elements in \(a\) that have to be changed so there are exactly \(k\) occurrences of \(b\) as a substring in \(a\).
For \(k = 0\), to make the string \(a\) have no occurrence of 101, you can do one character change as follows.100101011 \(\rightarrow\) 100100011For \(k = 1\), you can also change a single character.100101011 \(\rightarrow\) 100001011For \(k = 2\), no changes are needed.
Input: 9 3 100101011 101 | Output: 1 1 0 1 6 -1 -1 -1
Hard
2
849
266
201
15
2,068
H
2068H
H. Statues
2,700
constructive algorithms; greedy; math
The mayor of a city wants to place \(n\) statues at intersections around the city. The intersections in the city are at all points \((x, y)\) with integer coordinates. Distances between intersections are measured using Manhattan distance, defined as follows: $$$\( \text{distance}((x_1, y_1), (x_2, y_2)) = |x_1 - x_2| +...
The first line contains an integer \(n\) (\(3 \le n \le 50\)) β€” the number of statues.The second line contains two integers \(a\) and \(b\) (\(0 \le a, b \le 10^9\)) β€” the coordinates of the intersection where the \(n\)-th statue must be placed.The third line contains \(n-1\) integers \(d_1, \dots, d_{n-1}\) (\(0 \le d...
Print \(\texttt{YES}\) if there is a valid arrangement of the \(n\) statues. Otherwise, print \(\texttt{NO}\).If there is a valid arrangement, print a valid arrangement in the following \(n\) lines. The \(i\)-th of these lines must contain two integers \(x_i\) and \(y_i\) β€” the coordinates of the intersection where the...
In the first sample, there is no valid arrangement of the 3 statues.In the second sample, the sample output is shown in the following picture. Note that this is not the only valid arrangement of the 4 statues.
Input: 35 89 0 | Output: NO
Master
3
788
406
402
20
784
C
784C
C. INTERCALC
1,800
*special; implementation
DO YOU EXPECT ME TO FIND THIS OUT?WHAT BASE AND/XOR LANGUAGE INCLUDES string?DON'T BYTE OF MORE THAN YOU CAN CHEWYOU CAN ONLY DISTORT THE LARGEST OF MATHEMATICS SO FARSAYING ""ABRACADABRA"" WITHOUT A MAGIC AND WON'T DO YOU ANY GOODTHE LAST STACK RUPTURES. ALL DIE. OH, THE EMBARRASSMENT!I HAVE NO ARRAY AND I MUST SCREAM...
The first line of input data contains a single integer n (1 ≀ n ≀ 10).The second line of input data contains n space-separated integers ai (1 ≀ ai ≀ 11).
Output a single integer.
Input: 42 5 3 1 | Output: 4
Medium
2
365
153
24
7
621
B
621B
B. Wet Shark and Bishops
1,300
combinatorics; implementation
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only ...
The first line of the input contains n (1 ≀ n ≀ 200 000) β€” the number of bishops.Each of next n lines contains two space separated integers xi and yi (1 ≀ xi, yi ≀ 1000) β€” the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.
Output one integer β€” the number of pairs of bishops which attack each other.
In the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.
Input: 51 11 53 35 15 5 | Output: 6
Easy
2
529
308
76
6
1,482
B
1482B
B. Restore Modulo
1,500
implementation; math
For the first place at the competition, Alex won many arrays of integers and was assured that these arrays are very expensive. After the award ceremony Alex decided to sell them. There is a rule in arrays pawnshop: you can sell array only if it can be compressed to a generator.This generator takes four non-negative num...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β€” the number of arrays.The first line of array description contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the size of this array.The second line of array description contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 1...
For every array print: \(-1\), if there are no such four numbers that generate this array; \(0\), if \(m\) can be arbitrary large; the maximum value \(m\) and any appropriate \(c\) (\(0 \leq c < m\)) in other cases.
Input: 6 6 1 9 17 6 14 3 3 4 2 2 3 7 3 4 3 2 2 4 5 0 1000000000 0 1000000000 0 2 1 1 | Output: 19 8 -1 -1 -1 2000000000 1000000000 0
Medium
2
1,091
423
215
14
1,712
C
1712C
C. Sort Zero
1,100
greedy; sortings
An array is sorted if it has no inversionsA Young BoyYou are given an array of \(n\) positive integers \(a_1,a_2,\ldots,a_n\). In one operation you do the following: Choose any integer \(x\). For all \(i\) such that \(a_i = x\), do \(a_i := 0\) (assign \(0\) to \(a_i\)). Find the minimum number of operations required t...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) positive integers \(a_1,a_...
For each test case print one integer β€” the minimum number of operations required to sort the array in non-decreasing order.
In the first test case, you can choose \(x = 3\) for the operation, the resulting array is \([0, 0, 2]\).In the second test case, you can choose \(x = 1\) for the first operation and \(x = 3\) for the second operation, the resulting array is \([0, 0, 0, 0]\).
Input: 533 3 241 3 1 354 1 5 3 242 4 1 211 | Output: 1 2 4 3 0
Easy
2
361
441
123
17