contest_id
int32 1
2.13k
| index
stringclasses 62
values | problem_id
stringlengths 2
6
| title
stringlengths 0
67
| rating
int32 0
3.5k
| tags
stringlengths 0
139
| statement
stringlengths 0
6.96k
| input_spec
stringlengths 0
2.32k
| output_spec
stringlengths 0
1.52k
| note
stringlengths 0
5.06k
| sample_tests
stringlengths 0
1.02k
| difficulty_category
stringclasses 6
values | tag_count
int8 0
11
| statement_length
int32 0
6.96k
| input_spec_length
int16 0
2.32k
| output_spec_length
int16 0
1.52k
| contest_year
int16 0
21
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,146 |
F
|
1146F
|
F. Leaf Partition
| 2,500 |
dp; trees
|
You are given a rooted tree with \(n\) nodes, labeled from \(1\) to \(n\). The tree is rooted at node \(1\). The parent of the \(i\)-th node is \(p_i\). A leaf is node with no children. For a given set of leaves \(L\), let \(f(L)\) denote the smallest connected subgraph that contains all leaves \(L\).You would like to partition the leaves such that for any two different sets \(x, y\) of the partition, \(f(x)\) and \(f(y)\) are disjoint. Count the number of ways to partition the leaves, modulo \(998244353\). Two ways are different if there are two leaves such that they are in the same set in one way but in different sets in the other.
|
The first line contains an integer \(n\) (\(2 \leq n \leq 200\,000\)) β the number of nodes in the tree.The next line contains \(n-1\) integers \(p_2, p_3, \ldots, p_n\) (\(1 \leq p_i < i\)).
|
Print a single integer, the number of ways to partition the leaves, modulo \(998244353\).
|
In the first example, the leaf nodes are \(2,3,4,5\). The ways to partition the leaves are in the following image In the second example, the only leaf is node \(10\) so there is only one partition. Note that node \(1\) is not a leaf.
|
Input: 5 1 1 1 1 | Output: 12
|
Expert
| 2 | 641 | 191 | 89 | 11 |
1,856 |
A
|
1856A
|
A. Tales of a Sort
| 800 |
implementation
|
Alphen has an array of positive integers \(a\) of length \(n\).Alphen can perform the following operation: For all \(i\) from \(1\) to \(n\), replace \(a_i\) with \(\max(0, a_i - 1)\). Alphen will perform the above operation until \(a\) is sorted, that is \(a\) satisfies \(a_1 \leq a_2 \leq \ldots \leq a_n\). How many operations will Alphen perform? Under the constraints of the problem, it can be proven that Alphen will perform a finite number of operations.
|
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\) (\(2 \le n \le 50\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10 ^ 9\)) β the elements of the array \(a\).
|
For each test case, output a single integer β the number of operations that Alphen will perform.
|
In the first test case, we have \(a=[1,2,3]\). Since \(a\) is already sorted, Alphen will not need to perform any operations. So, the answer is \(0\).In the second test case, we have \(a=[2,1,2,1,2]\). Since \(a\) is not initially sorted, Alphen will perform one operation to make \(a=[1,0,1,0,1]\). After performing one operation, \(a\) is still not sorted, so Alphen will perform another operation to make \(a=[0,0,0,0,0]\). Since \(a\) is sorted, Alphen will not perform any other operations. Since Alphen has performed two operations in total, the answer is \(2\).
|
Input: 731 2 352 1 2 1 243 1 5 427 754 1 3 2 552 3 1 4 531000000000 1 2 | Output: 0 2 5 0 4 3 1000000000
|
Beginner
| 1 | 462 | 450 | 96 | 18 |
2,107 |
A
|
2107A
|
A. LRC and VIP
| 800 |
greedy; number theory
|
You have an array \(a\) of size \(n\) β \(a_1, a_2, \ldots a_n\). You need to divide the \(n\) elements into \(2\) sequences \(B\) and \(C\), satisfying the following conditions: Each element belongs to exactly one sequence. Both sequences \(B\) and \(C\) contain at least one element. \(\gcd\) \((B_1, B_2, \ldots, B_{|B|}) \ne \gcd(C_1, C_2, \ldots, C_{|C|})\) \(^{\text{β}}\) \(^{\text{β}}\)\(\gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(2 \le n \le 100\)).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 10^4\)).
|
For each test case, first output \(\texttt{Yes}\) if a solution exists or \(\texttt{No}\) if no solution exists. You may print each character in either case, for example \(\texttt{YES}\) and \(\texttt{yEs}\) will also be accepted.Only when there is a solution, output \(n\) integers on the second line. The \(i\)-th number should be either \(1\) or \(2\). \(1\) represents that the element belongs to sequence \(B\) and \(2\) represents that the element belongs to sequence \(C\). You should guarantee that \(1\) and \(2\) both appear at least once.
|
In the first test case, \(B = [51, 9]\) and \(C = [1, 20]\). You can verify \(\gcd(B_1, B_2) = 3 \ne 1 = \gcd(C_1, C_2)\).In the second test case, it is impossible to find a solution. For example, suppose you distributed the first \(3\) elements to array \(B\) and then the last element to array \(C\). You have \(B = [5, 5, 5]\) and \(C = [5]\), but \(\gcd(B_1, B_2, B_3) = 5 = \gcd(C_1)\). Hence it is invalid.
|
Input: 341 20 51 945 5 5 531 2 2 | Output: Yes 2 2 1 1 No Yes 1 2 2
|
Beginner
| 2 | 479 | 348 | 549 | 21 |
967 |
A
|
967A
|
A. Mind the Gap
| 1,100 |
implementation
|
These days Arkady works as an air traffic controller at a large airport. He controls a runway which is usually used for landings only. Thus, he has a schedule of planes that are landing in the nearest future, each landing lasts \(1\) minute.He was asked to insert one takeoff in the schedule. The takeoff takes \(1\) minute itself, but for safety reasons there should be a time space between the takeoff and any landing of at least \(s\) minutes from both sides.Find the earliest time when Arkady can insert the takeoff.
|
The first line of input contains two integers \(n\) and \(s\) (\(1 \le n \le 100\), \(1 \le s \le 60\)) β the number of landings on the schedule and the minimum allowed time (in minutes) between a landing and a takeoff.Each of next \(n\) lines contains two integers \(h\) and \(m\) (\(0 \le h \le 23\), \(0 \le m \le 59\)) β the time, in hours and minutes, when a plane will land, starting from current moment (i. e. the current time is \(0\) \(0\)). These times are given in increasing order.
|
Print two integers \(h\) and \(m\) β the hour and the minute from the current moment of the earliest time Arkady can insert the takeoff.
|
In the first example note that there is not enough time between 1:20 and 3:21, because each landing and the takeoff take one minute.In the second example there is no gaps in the schedule, so Arkady can only add takeoff after all landings. Note that it is possible that one should wait more than \(24\) hours to insert the takeoff.In the third example Arkady can insert the takeoff even between the first landing.
|
Input: 6 600 01 203 215 019 3023 40 | Output: 6 1
|
Easy
| 1 | 520 | 493 | 136 | 9 |
1,980 |
G
|
1980G
|
G. Yasya and the Mysterious Tree
| 2,300 |
bitmasks; data structures; dfs and similar; graphs; greedy; strings; trees
|
Yasya was walking in the forest and accidentally found a tree with \(n\) vertices. A tree is a connected undirected graph with no cycles.Next to the tree, the girl found an ancient manuscript with \(m\) queries written on it. The queries can be of two types.The first type of query is described by the integer \(y\). The weight of each edge in the tree is replaced by the bitwise exclusive OR of the weight of that edge and the integer \(y\).The second type is described by the vertex \(v\) and the integer \(x\). Yasya chooses a vertex \(u\) (\(1 \le u \le n\), \(u \neq v\)) and mentally draws a bidirectional edge of weight \(x\) from \(v\) to \(u\) in the tree.Then Yasya finds a simple cycle in the resulting graph and calculates the bitwise exclusive OR of all the edges in it. She wants to choose a vertex \(u\) such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of \(u\)). If an edge between \(v\) and \(u\) already existed, a simple cycle is the path \(v \to u \to v\).Note that the second type of query is performed mentally, meaning the tree does not change in any way after it.Help Yasya answer all the queries.
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The descriptions of the test cases follow.The first line of each test case contains two integers \(n\), \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)) β the number of vertices in the tree and the number of queries.The next \(n - 1\) lines of each test case contain three integers \(v\), \(u\), \(w\) (\(1 \le v, u \le n\), \(1 \le w \le 10^9\)) β the ends of some edge in the tree and its weight.It is guaranteed that the given set of edges forms a tree.The next \(m\) lines of each test case describe the queries: ^ \(y\) (\(1 \le y \le 10^9\)) β parameter of the first type query; ? \(v\) \(x\) (\(1 \le v \le n\), \(1 \le x \le 10^9\)) β parameters of the second type query. It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). The same is guaranteed for \(m\).
|
For each test case, output the answers to the queries of the second type.
|
Input: 23 71 2 13 1 8^ 5? 2 9^ 1? 1 10^ 6? 3 1? 2 95 61 2 7773 2 28124 1 165 3 1000000000^ 4? 3 123? 5 1000000000^ 1000000000? 1 908070? 2 1 | Output: 13 15 11 10 1000000127 2812 999756331 999999756
|
Expert
| 7 | 1,280 | 915 | 73 | 19 |
|
249 |
E
|
249E
|
E. Endless Matrix
| 2,600 |
math
|
A Russian space traveller Alisa Selezneva, like any other schoolgirl of the late 21 century, is interested in science. She has recently visited the MIT (Moscow Institute of Time), where its chairman and the co-inventor of the time machine academician Petrov told her about the construction of a time machine.During the demonstration of the time machine performance Alisa noticed that the machine does not have high speed and the girl got interested in the reason for such disadvantage. As it turns out on closer examination, one of the problems that should be solved for the time machine isn't solved by an optimal algorithm. If you find a way to solve this problem optimally, the time machine will run faster and use less energy.A task that none of the staff can solve optimally is as follows. There exists a matrix a, which is filled by the following rule:The cells are consecutive positive integers, starting with one. Besides, ai, j < at, k (i, j, t, k β₯ 1), if: max(i, j) < max(t, k); max(i, j) = max(t, k) and j < k; max(i, j) = max(t, k), j = k and i > t. So, after the first 36 numbers are inserted, matrix a will look as follows: To solve the problem, you should learn to find rather quickly for the given values of x1, y1, x2 and y2 (x1 β€ x2, y1 β€ y2) the meaning of expression:As the meaning of this expression can be large enough, it is sufficient to know only the last 10 digits of the sought value.So, no one in MTI can solve the given task. Alice was brave enough to use the time machine and travel the past to help you.Your task is to write a program that uses the given values x1, y1, x2 and y2 finds the last 10 digits of the given expression.
|
The first input line contains a single integer t (1 β€ t β€ 105) β the number of test sets for which you should solve the problem. Each of the next t lines contains the description of a test β four positive integers x1, y1, x2 and y2 (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109), separated by spaces.
|
For each query print the meaning of the expression if it contains at most 10 characters. Otherwise, print three characters ""."" (without the quotes), and then ten last digits of the time expression. Print the answer to each query on a single line. Follow the format, given in the sample as closely as possible.
|
Input: 51 1 1 12 2 3 32 3 5 6100 87 288 20024 2 5 4 | Output: 124300...5679392764111
|
Expert
| 1 | 1,661 | 292 | 311 | 2 |
|
198 |
D
|
198D
|
D. Cube Snake
| 2,700 |
constructive algorithms
|
You've got an n Γ n Γ n cube, split into unit cubes. Your task is to number all unit cubes in this cube with positive integers from 1 to n3 so that: each number was used as a cube's number exactly once; for each 1 β€ i < n3, unit cubes with numbers i and i + 1 were neighbouring (that is, shared a side); for each 1 β€ i < n there were at least two different subcubes with sizes i Γ i Γ i, made from unit cubes, which are numbered with consecutive numbers. That is, there are such two numbers x and y, that the unit cubes of the first subcube are numbered by numbers x, x + 1, ..., x + i3 - 1, and the unit cubes of the second subcube are numbered by numbers y, y + 1, ..., y + i3 - 1. Find and print the required numeration of unit cubes of the cube.
|
The first line contains a single integer n (1 β€ n β€ 50) β the size of the cube, whose unit cubes need to be numbered.
|
Print all layers of the cube as n n Γ n matrices. Separate them with new lines. Print the layers in the order in which they follow in the cube. See the samples for clarifications. It is guaranteed that there always is a solution that meets the conditions given in the problem statement.
|
In the sample the cubes with sizes 2 Γ 2 Γ 2 are numbered with integers 1, ..., 8 and 5, ..., 12.
|
Input: 3 | Output: 1 4 17 2 3 18 27 26 19 8 5 16 7 6 15 24 25 20 9 12 13 10 11 14 23 22 21
|
Master
| 1 | 749 | 117 | 286 | 1 |
429 |
B
|
429B
|
B. Working out
| 1,600 |
dp
|
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in the i-th line and the j-th column.Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workout a[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j]. There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.
|
The first line of the input contains two integers n and m (3 β€ n, m β€ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 β€ a[i][j] β€ 105).
|
The output contains a single number β the maximum total gain possible.
|
Iahub will choose exercises a[1][1] β a[1][2] β a[2][2] β a[3][2] β a[3][3]. Iahubina will choose exercises a[3][1] β a[2][1] β a[2][2] β a[2][3] β a[1][3].
|
Input: 3 3100 100 100100 1 100100 100 100 | Output: 800
|
Medium
| 1 | 1,231 | 194 | 70 | 4 |
2,073 |
G
|
2073G
| 2,200 |
Hard
| 0 | 0 | 0 | 0 | 20 |
|||||||
2,050 |
B
|
2050B
|
B. Transfusion
| 1,100 |
brute force; greedy; math
|
You are given an array \(a\) of length \(n\). In one operation, you can pick an index \(i\) from \(2\) to \(n-1\) inclusive, and do one of the following actions: Decrease \(a_{i-1}\) by \(1\), then increase \(a_{i+1}\) by \(1\). Decrease \(a_{i+1}\) by \(1\), then increase \(a_{i-1}\) by \(1\). After each operation, all the values must be non-negative. Can you make all the elements equal after any number of operations?
|
First line of input consists of one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.First line of each test case consists of one integer \(n\) (\(3 \le n \le 2\cdot 10^5\)).Second line of each test case consists of \(n\) integers \(a_i\) (\(1 \le a_i \le 10^9\)).It is guaranteed that the sum of \(n\) of all test cases doesn't exceed \(2\cdot 10^5\).
|
For each test case, print ""YES"" without quotation marks if it is possible to make all the elements equal after any number of operations; otherwise, print ""NO"" without quotation marks.You can print answers in any register: ""yes"", ""YeS"", ""nO"" β will also be considered correct.
|
Input: 833 2 131 1 341 2 5 441 6 6 156 2 1 4 241 4 2 153 1 2 1 332 4 2 | Output: YES NO YES NO YES NO NO NO
|
Easy
| 3 | 422 | 367 | 285 | 20 |
|
1,771 |
E
|
1771E
|
E. Hossam and a Letter
| 2,500 |
brute force; dp; implementation; two pointers
|
Hossam bought a new piece of ground with length \(n\) and width \(m\), he divided it into an \(n \cdot m\) grid, each cell being of size \(1\times1\).Since Hossam's name starts with the letter 'H', he decided to draw the capital letter 'H' by building walls of size \(1\times1\) on some squares of the ground. Each square \(1\times1\) on the ground is assigned a quality degree: perfect, medium, or bad.The process of building walls to form up letter 'H' has the following constraints: The letter must consist of one horizontal and two vertical lines. The vertical lines must not be in the same or neighboring columns. The vertical lines must start in the same row and end in the same row (and thus have the same length). The horizontal line should connect the vertical lines, but must not cross them. The horizontal line can be in any row between the vertical lines (not only in the middle), except the top and the bottom one. (With the horizontal line in the top row the letter looks like 'n', and in the bottom row like 'U'.) It is forbidden to build walls in cells of bad quality. You can use at most one square of medium quality. You can use any number of squares of perfect quality. Find the maximum number of walls that can be used to draw the letter 'H'.Check the note for more clarification.
|
The first line of the input contains two integer numbers \(n\), \(m\) (\(1 \le n, m \le 400\)).The next \(n\) lines of the input contain \(m\) characters each, describing the grid. The character '.' stands for a perfect square, the character 'm' stands for a medium square, and the character '#' stands for a bad square.
|
Print a single integer β the maximum number of walls that form a capital letter 'H'.If it is not possible to draw any letter 'H', print \(0\).
|
In the first test case, we can't build the letter 'H'.For the second test case, the figure below represents the grid and some of the valid letters 'H'. Perfect, medium, and bad squares are represented with white, yellow, and black colors respectively.
|
Input: 2 3 #m. .#. | Output: 0
|
Expert
| 4 | 1,300 | 320 | 142 | 17 |
1,205 |
F
|
1205F
|
F. Beauty of a Permutation
| 3,400 |
constructive algorithms; math
|
Define the beauty of a permutation of numbers from \(1\) to \(n\) \((p_1, p_2, \dots, p_n)\) as number of pairs \((L, R)\) such that \(1 \le L \le R \le n\) and numbers \(p_L, p_{L+1}, \dots, p_R\) are consecutive \(R-L+1\) numbers in some order. For example, the beauty of the permutation \((1, 2, 5, 3, 4)\) equals \(9\), and segments, corresponding to pairs, are \([1]\), \([2]\), \([5]\), \([4]\), \([3]\), \([1, 2]\), \([3, 4]\), \([5, 3, 4]\), \([1, 2, 5, 3, 4]\).Answer \(q\) independent queries. In each query, you will be given integers \(n\) and \(k\). Determine if there exists a permutation of numbers from \(1\) to \(n\) with beauty equal to \(k\), and if there exists, output one of them.
|
The first line contains a single integer \(q\) (\(1\le q \le 10\,000\)) β the number of queries.Follow \(q\) lines. Each line contains two integers \(n\), \(k\) (\(1 \le n \le 100\), \(1 \le k \le \frac{n(n+1)}{2}\)) β the length of permutation and needed beauty respectively.
|
For a query output ""NO"", if such a permutation doesn't exist. Otherwise, output ""YES"", and in the next line output \(n\) numbers β elements of permutation in the right order.
|
Let's look at the first example.The first query: in \((1)\) there is only one segment consisting of consecutive numbers β the entire permutation.The second query: in \((2, 4, 1, 5, 3)\) there are \(6\) such segments: \([2]\), \([4]\), \([1]\), \([5]\), \([3]\), \([2, 4, 1, 5, 3]\).There is no such permutation for the second query.The fourth query: in \((2, 3, 1, 4, 5)\) there are \(10\) such segments: \([2]\), \([3]\), \([1]\), \([4]\), \([5]\), \([2, 3]\), \([2, 3, 1]\), \([2, 3, 1, 4]\), \([4, 5]\), \([2, 3, 1, 4, 5]\).
|
Input: 4 1 1 5 6 5 8 5 10 | Output: YES 1 YES 2 4 1 5 3 NO YES 2 3 1 4 5
|
Master
| 2 | 702 | 276 | 178 | 12 |
1,761 |
G
|
1761G
|
G. Centroid Guess
| 3,500 |
interactive; probabilities; trees
|
This in an interactive problem.There is an unknown tree consisting of \(n\) nodes, which has exactly one centroid. You only know \(n\) at first, and your task is to find the centroid of the tree.You can ask the distance between any two vertices for at most \(2\cdot10^5\) times. Note that the interactor is not adaptive. That is, the tree is fixed in each test beforehand and does not depend on your queries.A vertex is called a centroid if its removal splits the tree into subtrees with at most \(\lfloor\frac{n}{2}\rfloor\) vertices each.
|
The only line of the input contains an integer \(n\) (\(3\le n\le 7.5\cdot10^4\)) β the number of nodes in the tree.
|
Here is an image of the tree from the sample.
|
Input: 5 2 1 2 3 1 1 1 | Output: ? 1 2 ? 1 3 ? 1 4 ? 1 5 ? 2 3 ? 3 4 ? 4 5 ! 3
|
Master
| 3 | 540 | 116 | 0 | 17 |
|
1,480 |
A
|
1480A
|
A. Yet Another String Game
| 800 |
games; greedy; strings
|
Homer has two friends Alice and Bob. Both of them are string fans. One day, Alice and Bob decide to play a game on a string \(s = s_1 s_2 \dots s_n\) of length \(n\) consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.In a move, a player must choose an index \(i\) (\(1 \leq i \leq n\)) that has not been chosen before, and change \(s_i\) to any other lowercase English letter \(c\) that \(c \neq s_i\).When all indices have been chosen, the game ends. The goal of Alice is to make the final string lexicographically as small as possible, while the goal of Bob is to make the final string lexicographically as large as possible. Both of them are game experts, so they always play games optimally. Homer is not a game expert, so he wonders what the final string will be.A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).
|
Each test contains multiple test cases. The first line contains \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Description of the test cases follows.The only line of each test case contains a single string \(s\) (\(1 \leq |s| \leq 50\)) consisting of lowercase English letters.
|
For each test case, print the final string in a single line.
|
In the first test case: Alice makes the first move and must change the only letter to a different one, so she changes it to 'b'.In the second test case: Alice changes the first letter to 'a', then Bob changes the second letter to 'z', Alice changes the third letter to 'a' and then Bob changes the fourth letter to 'z'.In the third test case: Alice changes the first letter to 'b', and then Bob changes the second letter to 'y'.
|
Input: 3 a bbbb az | Output: b azaz by
|
Beginner
| 3 | 1,132 | 287 | 60 | 14 |
189 |
B
|
189B
|
B. Counting Rhombi
| 1,300 |
brute force; math
|
You have two positive integers w and h. Your task is to count the number of rhombi which have the following properties: Have positive area. With vertices at integer points. All vertices of the rhombi are located inside or on the border of the rectangle with vertices at points (0, 0), (w, 0), (w, h), (0, h). In other words, for all vertices (xi, yi) of the rhombus the following conditions should fulfill: 0 β€ xi β€ w and 0 β€ yi β€ h. Its diagonals are parallel to the axis. Count the number of such rhombi.Let us remind you that a rhombus is a quadrilateral whose four sides all have the same length.
|
The first line contains two integers w and h (1 β€ w, h β€ 4000) β the rectangle's sizes.
|
Print a single number β the number of sought rhombi.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
|
In the first example there exists only one such rhombus. Its vertices are located at points (1, 0), (2, 1), (1, 2), (0, 1).
|
Input: 2 2 | Output: 1
|
Easy
| 2 | 600 | 87 | 194 | 1 |
136 |
A
|
136A
|
A. Presents
| 800 |
implementation
|
Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there.If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi. He also remembered that each of his friends received exactly one gift.Now Petya wants to know for each friend i the number of a friend who has given him a gift.
|
The first line contains one integer n (1 β€ n β€ 100) β the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi β the number of a friend who gave a gift to friend number i. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves.
|
Print n space-separated integers: the i-th number should equal the number of the friend who gave a gift to friend number i.
|
Input: 42 3 4 1 | Output: 4 1 2 3
|
Beginner
| 1 | 878 | 440 | 123 | 1 |
|
158 |
B
|
158B
|
B. Taxi
| 1,100 |
*special; greedy; implementation
|
After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 β€ si β€ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum number of cars will the children need if all members of each group should ride in the same taxi (but one taxi can take more than one group)?
|
The first line contains integer n (1 β€ n β€ 105) β the number of groups of schoolchildren. The second line contains a sequence of integers s1, s2, ..., sn (1 β€ si β€ 4). The integers are separated by a space, si is the number of children in the i-th group.
|
Print the single number β the minimum number of taxis necessary to drive all children to Polycarpus.
|
In the first test we can sort the children into four cars like this: the third group (consisting of four children), the fourth group (consisting of three children), the fifth group (consisting of three children), the first and the second group (consisting of one and two children, correspondingly). There are other ways to sort the groups into four cars.
|
Input: 51 2 4 3 3 | Output: 4
|
Easy
| 3 | 458 | 254 | 100 | 1 |
452 |
D
|
452D
|
D. Washer, Dryer, Folder
| 1,900 |
greedy; implementation
|
You have k pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has n1 washing machines, n2 drying machines and n3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before it is dried. Moreover, after a piece of laundry is washed, it needs to be immediately moved into a drying machine, and after it is dried, it needs to be immediately moved into a folding machine.It takes t1 minutes to wash one piece of laundry in a washing machine, t2 minutes to dry it in a drying machine, and t3 minutes to fold it in a folding machine. Find the smallest number of minutes that is enough to wash, dry and fold all the laundry you have.
|
The only line of the input contains seven integers: k, n1, n2, n3, t1, t2, t3 (1 β€ k β€ 104; 1 β€ n1, n2, n3, t1, t2, t3 β€ 1000).
|
Print one integer β smallest number of minutes to do all your laundry.
|
In the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it.In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, there will be no folding machine available, so you have to wait, and start washing third piece at moment 2. Similarly, you can't start washing next piece until moment 5, since otherwise there will be no dryer available, when it is washed. Start time for each of the eight pieces of laundry is 0, 0, 2, 5, 10, 10, 12 and 15 minutes respectively. The last piece of laundry will be ready after 15 + 10 + 5 + 2 = 32 minutes.
|
Input: 1 1 1 1 5 5 5 | Output: 15
|
Hard
| 2 | 772 | 127 | 70 | 4 |
1,729 |
F
|
1729F
|
F. Kirei and the Linear Function
| 1,900 |
hashing; math
|
Given the string \(s\) of decimal digits (0-9) of length \(n\).A substring is a sequence of consecutive characters of a string. The substring of this string is defined by a pair of indexes β with its left and right ends. So, each pair of indexes (\(l, r\)), where \(1 \le l \le r \le n\), corresponds to a substring of the string \(s\). We will define as \(v(l,r)\) the numeric value of the corresponding substring (leading zeros are allowed in it).For example, if \(n=7\), \(s=\)""1003004"", then \(v(1,3)=100\), \(v(2,3)=0\) and \(v(2,7)=3004\).You are given \(n\), \(s\) and an integer \(w\) (\(1 \le w < n\)).You need to process \(m\) queries, each of which is characterized by \(3\) numbers \(l_i, r_i, k_i\) (\(1 \le l_i \le r_i \le n; 0 \le k_i \le 8\)).The answer to the \(i\)th query is such a pair of substrings of length \(w\) that if we denote them as \((L_1, L_1+w-1)\) and \((L_2, L_2+w-1)\), then: \(L_1 \ne L_2\), that is, the substrings are different; the remainder of dividing a number \(v(L_1, L_1+w-1) \cdot v(l_i, r_i) + v(L_2, L_2 + w - 1)\) by \(9\) is equal to \(k_i\). If there are many matching substring pairs, then find a pair where \(L_1\) is as small as possible. If there are many matching pairs in this case, then minimize \(L_2\).Note that the answer may not exist.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β number of input test cases.The first line of each test case contains a string \(s\), which contains only the characters 0-9 and has a length \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line contains two integers \(w, m\) (\(1 \le w < n, 1 \le m \le 2 \cdot 10^5\)), where \(n\) β is the length of the given string \(s\). The number \(w\) denotes the lengths of the substrings being searched for, and \(m\) is the number of queries to be processed.The following \(m\) lines contain integers \(l_i, r_i, k_i\) (\(1 \le l_i \le r_i \le n\), \(0 \le k_i \le 8\)) β \(i\)th query parameters.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). It is also guaranteed that the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each request, print in a separate line: left borders of the required substrings: \(L_1\) and \(L_2\); -1 -1 otherwise, if there is no solution. If there are several solutions, minimize \(L_1\) first, and minimize \(L_2\) second.
|
Consider the first test case of example inputs. In this test case \(n=7\), \(s=\)""1003004"", \(w=4\) and one query \(l_1=1\), \(r_1=2\), \(k_1=1\). Note that \(v(1,2)=10\). We need to find a pair of substrings of length \(4\) such that \(v(L_1, L_1+3)\cdot10+v(L_2,L_2+3)\) has a remainder of \(k_1=1\) when divided by \(9\). The values \(L_1=2, L_2=4\) actually satisfy all the requirements: \(v(L_1, L_1+w-1)=v(2,5)=30\), \(v(L_2, L_2+w-1)=v(4,7)=3004\). Indeed, \(30\cdot10+3004=3304\), which has a remainder of \(1\) when divided by \(9\). It can be shown that \(L_1=2\) is the minimum possible value, and \(L_2=4\) is the minimum possible with \(L_1=2\).
|
Input: 510030044 11 2 11795720074 22 7 32 7 41112 12 2 600001 21 4 01 4 14841 52 2 02 3 71 2 53 3 82 2 6 | Output: 2 4 1 5 1 2 -1 -1 1 2 -1 -1 1 3 1 3 -1 -1 -1 -1 -1 -1
|
Hard
| 2 | 1,298 | 852 | 232 | 17 |
1,389 |
C
|
1389C
|
C. Good String
| 1,500 |
brute force; dp; greedy; two pointers
|
Let's call left cyclic shift of some string \(t_1 t_2 t_3 \dots t_{n - 1} t_n\) as string \(t_2 t_3 \dots t_{n - 1} t_n t_1\).Analogically, let's call right cyclic shift of string \(t\) as string \(t_n t_1 t_2 t_3 \dots t_{n - 1}\).Let's say string \(t\) is good if its left cyclic shift is equal to its right cyclic shift.You are given string \(s\) which consists of digits 0β9.What is the minimum number of characters you need to erase from \(s\) to make it good?
|
The first line contains single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Next \(t\) lines contains test cases β one per line. The first and only line of each test case contains string \(s\) (\(2 \le |s| \le 2 \cdot 10^5\)). Each character \(s_i\) is digit 0β9.It's guaranteed that the total length of strings doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print the minimum number of characters you need to erase from \(s\) to make it good.
|
In the first test case, you can erase any \(3\) characters, for example, the \(1\)-st, the \(3\)-rd, and the \(4\)-th. You'll get string 51 and it is good.In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good.In the third test case, the given string \(s\) is already good.
|
Input: 3 95831 100120013 252525252525 | Output: 3 5 0
|
Medium
| 4 | 465 | 363 | 104 | 13 |
2,009 |
G2
|
2009G2
|
G2. Yunli's Subarray Queries (hard version)
| 2,200 |
binary search; data structures; dp
|
This is the hard version of the problem. In this version, it is guaranteed that \(r \geq l+k-1\) for all queries.For an arbitrary array \(b\), Yunli can perform the following operation any number of times: Select an index \(i\). Set \(b_i = x\) where \(x\) is any integer she desires (\(x\) is not limited to the interval \([1,n]\)). Denote \(f(b)\) as the minimum number of operations she needs to perform until there exists a consecutive subarray\(^{\text{β}}\) of length at least \(k\) in \(b\).Yunli is given an array \(a\) of size \(n\) and asks you \(q\) queries. In each query, you must output \(\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])\).\(^{\text{β}}\)If there exists a consecutive subarray of length \(k\) that starts at index \(i\) (\(1 \leq i \leq |b|-k+1\)), then \(b_j = b_{j-1} + 1\) for all \(i < j \leq i+k-1\).
|
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(k\), and \(q\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\), \(1 \leq q \leq 2 \cdot 10^5\)) β the length of the array, the length of the consecutive subarray, and the number of queries.The following line contains \(n\) integers \(a_1, a_2, ..., a_n\) (\(1 \leq a_i \leq n\)).The following \(q\) lines contain two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq n\), \(r \geq l+k-1\)) β the bounds of the query.It is guaranteed the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\) and the sum of \(q\) over all test cases does not exceed \(2 \cdot 10^5\).
|
Output \(\sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, \ldots, a_j])\) for each query on a new line.
|
In the second query of the first testcase, we calculate the following function values: \(f([2,3,2,1,2])=3\) because Yunli can set \(b_3=4\), \(b_4=5\), and \(b_5=6\), making a consecutive subarray of size \(5\) in \(3\) moves. \(f([2,3,2,1,2,3])=2\) because we can set \(b_3=0\) and \(b_2=-1\), making a consecutive subarray of size \(5\) in \(2\) moves (starting at position \(2\)) The answer to this query is \(3+2=5\).
|
Input: 37 5 31 2 3 2 1 2 31 72 73 78 4 24 3 1 1 2 4 3 23 61 55 4 24 5 1 2 31 41 5 | Output: 6 5 2 2 5 2 3
|
Hard
| 3 | 837 | 715 | 90 | 20 |
1,802 |
B
|
1802B
|
B. Settlement of Guinea Pigs
| 1,000 |
greedy; implementation; math
|
Dasha loves guinea pigs very much. In this regard, she decided to settle as many guinea pigs at home as possible and developed a plan for the next \(n\) days. Every day, she will either buy a new guinea pig or call a doctor to examine all her pets.Unfortunately, the store where she was going to buy guinea pigs does not understand them. Therefore, it cannot determine their gender. Dasha can't do it either. The only one who can help is a doctor. To keep guinea pigs, aviaries are needed. Dasha plans to buy them in the same store. Unfortunately, only one species is sold there β a double aviary. No more than two guinea pigs can live in it.Since Dasha does not want to cause moral injury to her pets β she will not settle two guinea pigs of different genders in one aviary.Help Dasha calculate how many aviaries in the worst case you need to buy so that you can be sure that at no moment of time do two guinea pigs of different genders live in the same aviary.As part of this task, we believe that guinea pigs have only two genders β male and female.
|
The first line of input data contains one number \(t\) (\(1 \leqslant t \leqslant 10^5\)) β the number of input data sets.The first line of each input data set contains one number \(n\) (\(1 \leqslant n \leqslant 10^5\)) β the number of days Dasha has a plan for.The next line contains \(n\) numbers \(b_1, b_2, b_3, \ldots, b_n\) (\(1 \leqslant b_i \leqslant 2\)) β Dasha's plan. If \(b_i = 1\), then on the \(i\)th day, Dasha will buy a new guinea pig. If \(b_i = 2\), then on the \(i\)th day, a doctor will come to Dasha and help determine the sex of all guinea pigs that Dasha already has.It is guaranteed that the sum of \(n\) for all input data sets does not exceed \(10^5\).
|
For each set of input data, output one number β the minimum number of aviaries Dasha needs to buy so that no matter what the genders of the pigs turn out to be, we can settle them so that at no point in time do two guinea pigs of different genders live together.
|
In the first set of input data, Dasha needs to put each guinea pig in a separate enclosure, since she does not know their gender.In the second set of input data, Dasha will buy \(0\) guinea pigs, which means she will need \(0\) aviaries.In the third set of input data, you even need \(3\) aviaries to put each guinea pig in a separate aviary before the doctor arrives at the \(4\)th day. When she finds out their gender, at least two guinea pigs will be of the same gender and they can be placed in one aviary, and the third in another aviary. Thus, she will have one free aviary in which she can settle a new guinea pig. So answer is \(3\).In the fourth set of input data, we show that \(4\) is the optimal answer. To begin with, we note that the first four guinea pigs can be placed one at a time in an aviary. Then a doctor will come and determine their gender. Among these four guinea pigs there will be at least one pair of the same gender, because: either male guinea pigs are at least \(2\), or they are not more than \(1\), which means that the female is at least \(3\). Now we can put this couple in one aviary, and the other two in separate ones. We will have one more empty aviary where we can put a new pig.Now let's show that the answer is at least \(4\). Let's say that among the first \(4\) guinea pigs, \(3\) are female and \(1\) is male. We need at least \(3\) aviaries to settle them. Then, when we buy a new guinea pig, we will need another aviary in which we will put it, since we do not know its gender.
|
Input: 631 1 132 2 251 1 1 2 1101 2 1 2 1 2 1 2 1 2201 2 1 1 1 1 1 2 1 2 1 2 2 1 1 1 1 1 1 1202 1 1 2 1 1 2 1 2 2 1 1 1 2 2 1 1 1 1 2 | Output: 3 0 3 4 12 9
|
Beginner
| 3 | 1,052 | 681 | 262 | 18 |
448 |
D
|
448D
|
D. Multiplication Table
| 1,800 |
binary search; brute force
|
Bizon the Champion isn't just charming, he also is very smart.While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n Γ m multiplication table, where the element on the intersection of the i-th row and j-th column equals iΒ·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?Consider the given multiplication table. If you write out all nΒ·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
|
The single line contains integers n, m and k (1 β€ n, m β€ 5Β·105; 1 β€ k β€ nΒ·m).
|
Print the k-th largest number in a n Γ m multiplication table.
|
A 2 Γ 3 multiplication table looks like this:1 2 32 4 6
|
Input: 2 2 2 | Output: 2
|
Medium
| 2 | 718 | 77 | 62 | 4 |
1,081 |
F
|
1081F
|
F. Tricky Interactor
| 2,600 |
constructive algorithms; implementation; interactive
|
This is an interactive problem.Chouti was tired of studying, so he opened the computer and started playing a puzzle game.Long long ago, the boy found a sequence \(s_1, s_2, \ldots, s_n\) of length \(n\), kept by a tricky interactor. It consisted of \(0\)s and \(1\)s only and the number of \(1\)s is \(t\). The boy knows nothing about this sequence except \(n\) and \(t\), but he can try to find it out with some queries with the interactor.We define an operation called flipping. Flipping \([l,r]\) (\(1 \leq l \leq r \leq n\)) means for each \(x \in [l,r]\), changing \(s_x\) to \(1-s_x\).In each query, the boy can give the interactor two integers \(l,r\) satisfying \(1 \leq l \leq r \leq n\) and the interactor will either flip \([1,r]\) or \([l,n]\) (both outcomes have the same probability and all decisions made by interactor are independent from each other, see Notes section for more details). The interactor will tell the current number of \(1\)s after each operation. Note, that the sequence won't be restored after each operation.Help the boy to find the original sequence in no more than \(10000\) interactions.""Weird legend, dumb game."" he thought. However, after several tries, he is still stuck with it. Can you help him beat this game?
|
For first query \(1,1\), the interactor should flip \([1,1]\) or \([1,4]\). It chose to flip \([1,4]\), so the sequence became 1100.For second query \(1,1\), the interactor should flip \([1,1]\) or \([1,4]\). It again chose to flip \([1,4]\), so the sequence became 0011.For third query \(3,4\), the interactor should flip \([1,4]\) or \([3,4]\). It chose to flip \([3,4]\), so the sequence became 0000.Q: How does interactor choose between \([1,r]\) and \([l,n]\)? Is it really random?A: The interactor will use a secret pseudorandom number generator. Only \(s\) and your queries will be hashed and used as the seed. So if you give the same sequence of queries twice for the same secret string, you will get the same results. Except this, you can consider the choices fully random, like flipping a fair coin. You needn't (and shouldn't) exploit the exact generator to pass this problem.
|
Input: 4 2220 | Output: ? 1 1? 1 1? 3 4! 0011
|
Expert
| 3 | 1,255 | 0 | 0 | 10 |
||
345 |
E
|
345E
|
E. Black Cat Rush
| 2,700 |
*special
|
Today you go out of your house and immediately notice that something is weird. Around your door there is a swarm of black cats β all tense paws and twitching tails. As you do your first step, they all dart off and start running towards you. It looks like they want to thwart you!You are moving in a straight line from point (0, 0) to point (a, 0) with velocity v. There are n black cats around you who want to cross your paths. A cat can move in any direction with velocity at most u. A cat assumes it has crossed your path if it managed to get to at least one point of your path earlier or at the same time as you got there.You are given four integers: a, v, u, n, as well as cats' coordinates (xi, yi). What is the greatest number of cats who manage to cross your path?
|
The first line contains four integers a, v, u, n (1 β€ a β€ 10000; 1 β€ v, u β€ 100; 1 β€ n β€ 1000). Each of the next n lines contains two integers xi, yi ( - 100 β€ xi, yi β€ 100) β location of the i-th cat.It's guaranteed that all cats are located in distinct points.
|
Output a single integer β what is the greatest number of cats who manage to cross your path?
|
Input: 1 1 5 40 34 -47 0-2 -2 | Output: 3
|
Master
| 1 | 771 | 262 | 92 | 3 |
|
1,855 |
B
|
1855B
|
B. Longest Divisors Interval
| 900 |
brute force; combinatorics; greedy; math; number theory
|
Given a positive integer \(n\), find the maximum size of an interval \([l, r]\) of positive integers such that, for every \(i\) in the interval (i.e., \(l \leq i \leq r\)), \(n\) is a multiple of \(i\).Given two integers \(l\le r\), the size of the interval \([l, r]\) is \(r-l+1\) (i.e., it coincides with the number of integers belonging to the interval).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of the description of each test case contains one integer \(n\) (\(1 \leq n \leq 10^{18}\)).
|
For each test case, print a single integer: the maximum size of a valid interval.
|
In the first test case, a valid interval with maximum size is \([1, 1]\) (it's valid because \(n = 1\) is a multiple of \(1\)) and its size is \(1\).In the second test case, a valid interval with maximum size is \([4, 5]\) (it's valid because \(n = 40\) is a multiple of \(4\) and \(5\)) and its size is \(2\).In the third test case, a valid interval with maximum size is \([9, 11]\).In the fourth test case, a valid interval with maximum size is \([8, 13]\).In the seventh test case, a valid interval with maximum size is \([327869, 327871]\).
|
Input: 1014099099042044745601699589137065729723659882203458280803877017195378264306201968835781298538648023412808056621000000000000000000 | Output: 1 2 3 6 4 22 3 1 2 2
|
Beginner
| 5 | 357 | 203 | 81 | 18 |
1,530 |
B
|
1530B
|
B. Putting Plates
| 800 |
constructive algorithms; implementation
|
To celebrate your birthday you have prepared a festive table! Now you want to seat as many guests as possible.The table can be represented as a rectangle with height \(h\) and width \(w\), divided into \(h \times w\) cells. Let \((i, j)\) denote the cell in the \(i\)-th row and the \(j\)-th column of the rectangle (\(1 \le i \le h\); \(1 \le j \le w\)).Into each cell of the table you can either put a plate or keep it empty.As each guest has to be seated next to their plate, you can only put plates on the edge of the table β into the first or the last row of the rectangle, or into the first or the last column. Formally, for each cell \((i, j)\) you put a plate into, at least one of the following conditions must be satisfied: \(i = 1\), \(i = h\), \(j = 1\), \(j = w\).To make the guests comfortable, no two plates must be put into cells that have a common side or corner. In other words, if cell \((i, j)\) contains a plate, you can't put plates into cells \((i - 1, j)\), \((i, j - 1)\), \((i + 1, j)\), \((i, j + 1)\), \((i - 1, j - 1)\), \((i - 1, j + 1)\), \((i + 1, j - 1)\), \((i + 1, j + 1)\).Put as many plates on the table as possible without violating the rules above.
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Each of the following \(t\) lines describes one test case and contains two integers \(h\) and \(w\) (\(3 \le h, w \le 20\)) β the height and the width of the table.
|
For each test case, print \(h\) lines containing \(w\) characters each. Character \(j\) in line \(i\) must be equal to \(1\) if you are putting a plate into cell \((i, j)\), and \(0\) otherwise. If there are multiple answers, print any.All plates must be put on the edge of the table. No two plates can be put into cells that have a common side or corner. The number of plates put on the table under these conditions must be as large as possible.You are allowed to print additional empty lines.
|
For the first test case, example output contains the only way to put \(6\) plates on the table.For the second test case, there are many ways to put \(4\) plates on the table, example output contains one of them.Putting more than \(6\) plates in the first test case or more than \(4\) plates in the second test case is impossible.
|
Input: 3 3 5 4 4 5 6 | Output: 10101 00000 10101 0100 0001 1000 0010 010101 000000 100001 000000 101010
|
Beginner
| 2 | 1,187 | 260 | 494 | 15 |
1,639 |
D
|
1639D
|
D. Treasure Hunt
| 0 |
graphs; interactive
|
All problems in this contest share the same statement, the only difference is the test your solution runs on. For further information on scoring please refer to ""Scoring"" section of the statement.This is an interactive problem.Imagine you are a treasure hunter, a very skillful one. One day you came across an ancient map which could help you to become rich. The map shows multiple forestry roads, and at each junction there is a treasure. So, you start your journey hoping to retrieve all the hidden treasures, but you don't know yet that there is a wicked wizard standing against you and craving to tangle up these roads and impede your achievements.The treasure map is represented as an undirected graph in which vertices correspond to junctions and edges correspond to roads. Your path begins at a certain fixed vertex with a label known to you. Every time you come to a vertex that you have not been to before, you dig up a treasure chest and put a flag in this vertex. At the initial vertex you'll find a treasure chest immediately and, consequently, you'll put a flag there immediately as well.When you are standing at the junction you can see for each of the adjacent vertices its degree and if there is a flag there. There are no other things you can see from there. Besides, the power of the wicked wizard is so great that he is able to change the location of the roads and junctions on the map without changing the graph structure. Therefore, the sequence of the roads coming from the junction \(v\) might be different each time you come in the junction \(v\). However, keep in mind that the set of adjacent crossroads does not change, and you are well aware of previously dug treasures at each adjacent to \(v\) vertex.Your goal is to collect treasures from all vertices of the graph as fast as you can. Good luck in hunting!
|
Beginner
| 2 | 1,839 | 0 | 0 | 16 |
||||
1,285 |
D
|
1285D
|
D. Dr. Evil Underscores
| 1,900 |
bitmasks; brute force; dfs and similar; divide and conquer; dp; greedy; strings; trees
|
Today, as a friendship gift, Bakry gave Badawy \(n\) integers \(a_1, a_2, \dots, a_n\) and challenged him to choose an integer \(X\) such that the value \(\underset{1 \leq i \leq n}{\max} (a_i \oplus X)\) is minimum possible, where \(\oplus\) denotes the bitwise XOR operation.As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of \(\underset{1 \leq i \leq n}{\max} (a_i \oplus X)\).
|
The first line contains integer \(n\) (\(1\le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2^{30}-1\)).
|
Print one integer β the minimum possible value of \(\underset{1 \leq i \leq n}{\max} (a_i \oplus X)\).
|
In the first sample, we can choose \(X = 3\).In the second sample, we can choose \(X = 5\).
|
Input: 3 1 2 3 | Output: 2
|
Hard
| 8 | 426 | 154 | 102 | 12 |
578 |
A
|
578A
|
A. A Problem about Polyline
| 1,700 |
geometry; math
|
There is a polyline going through points (0, 0) β (x, x) β (2x, 0) β (3x, x) β (4x, 0) β ... - (2kx, 0) β (2kx + x, x) β .... We know that the polyline passes through the point (a, b). Find minimum positive value x such that it is true or determine that there is no such x.
|
Only one line containing two positive integers a and b (1 β€ a, b β€ 109).
|
Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10 - 9. If there is no such x then output - 1 as the answer.
|
You can see following graphs for sample 1 and sample 3.
|
Input: 3 1 | Output: 1.000000000000
|
Medium
| 2 | 273 | 72 | 192 | 5 |
13 |
A
|
13A
|
A. Numbers
| 1,000 |
implementation; math
|
Little Petya likes numbers a lot. He found that number 123 in base 16 consists of two digits: the first is 7 and the second is 11. So the sum of digits of 123 in base 16 is equal to 18.Now he wonders what is an average value of sum of digits of the number A written in all bases from 2 to A - 1.Note that all computations should be done in base 10. You should find the result as an irreducible fraction, written in base 10.
|
Input contains one integer number A (3 β€ A β€ 1000).
|
Output should contain required average value in format Β«X/YΒ», where X is the numerator and Y is the denominator.
|
In the first sample number 5 written in all bases from 2 to 4 looks so: 101, 12, 11. Sums of digits are 2, 3 and 2, respectively.
|
Input: 5 | Output: 7/3
|
Beginner
| 2 | 423 | 51 | 112 | 0 |
177 |
C2
|
177C2
|
C2. Party
| 1,500 |
brute force; dfs and similar; dsu; graphs
|
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.More formally, for each invited person the following conditions should be fulfilled: all his friends should also be invited to the party; the party shouldn't have any people he dislikes; all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 β€ i < p) are friends. Help the Beaver find the maximum number of acquaintances he can invite.
|
The first line of input contains an integer n β the number of the Beaver's acquaintances. The second line contains an integer k β the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi β indices of people who form the i-th pair of friends.The next line contains an integer m β the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.Each pair of people is mentioned in the input at most once . In particular, two persons cannot be friends and dislike each other at the same time.The input limitations for getting 30 points are: 2 β€ n β€ 14 The input limitations for getting 100 points are: 2 β€ n β€ 2000
|
Output a single number β the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
|
Let's have a look at the example. Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
|
Input: 981 21 32 34 56 77 88 99 621 67 9 | Output: 3
|
Medium
| 4 | 1,052 | 757 | 175 | 1 |
1,249 |
D1
|
1249D1
|
D1. Too Many Segments (easy version)
| 1,800 |
greedy
|
The only difference between easy and hard versions is constraints.You are given \(n\) segments on the coordinate axis \(OX\). Segments can intersect, lie inside each other and even coincide. The \(i\)-th segment is \([l_i; r_i]\) (\(l_i \le r_i\)) and it covers all integer points \(j\) such that \(l_i \le j \le r_i\).The integer point is called bad if it is covered by strictly more than \(k\) segments.Your task is to remove the minimum number of segments so that there are no bad points at all.
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 200\)) β the number of segments and the maximum number of segments by which each integer point can be covered.The next \(n\) lines contain segments. The \(i\)-th line contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le 200\)) β the endpoints of the \(i\)-th segment.
|
In the first line print one integer \(m\) (\(0 \le m \le n\)) β the minimum number of segments you need to remove so that there are no bad points.In the second line print \(m\) distinct integers \(p_1, p_2, \dots, p_m\) (\(1 \le p_i \le n\)) β indices of segments you remove in any order. If there are multiple answers, you can print any of them.
|
Input: 7 2 11 11 9 11 7 8 8 9 7 8 9 11 7 9 | Output: 3 1 4 7
|
Medium
| 1 | 498 | 368 | 346 | 12 |
|
1,474 |
D
|
1474D
|
D. Cleaning
| 2,200 |
data structures; dp; greedy; math
|
During cleaning the coast, Alice found \(n\) piles of stones. The \(i\)-th pile has \(a_i\) stones.Piles \(i\) and \(i + 1\) are neighbouring for all \(1 \leq i \leq n - 1\). If pile \(i\) becomes empty, piles \(i - 1\) and \(i + 1\) doesn't become neighbouring.Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation: Select two neighboring piles and, if both of them are not empty, remove one stone from each of them. Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability: Before the start of cleaning, you can select two neighboring piles and swap them. Determine, if it is possible to remove all stones using the superability not more than once.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains the single integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the number of piles.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the number of stones in each pile.It is guaranteed that the total sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print YES or NO β is it possible to remove all stones using the superability not more than once or not.
|
In the first test case, you can remove all stones without using a superability: \([1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]\).In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.In the third test case, you can apply superability to the fourth and the fifth piles, thus getting \(a = [2, 2, 2, 3, 1]\).In the fourth test case, you can apply superability to the first and the second piles, thus getting \(a = [1900, 2100, 1600, 3000, 1600]\).
|
Input: 5 3 1 2 1 3 1 1 2 5 2 2 2 1 3 5 2100 1900 1600 3000 1600 2 2443 2445 | Output: YES YES YES YES NO
|
Hard
| 4 | 817 | 464 | 123 | 14 |
1,955 |
B
|
1955B
|
B. Progressive Square
| 1,000 |
constructive algorithms; data structures; implementation; sortings
|
A progressive square of size \(n\) is an \(n \times n\) matrix. Maxim chooses three integers \(a_{1,1}\), \(c\), and \(d\) and constructs a progressive square according to the following rules:$$$\(a_{i+1,j} = a_{i,j} + c\)$$$$$$\(a_{i,j+1} = a_{i,j} + d\)\(For example, if \)n = 3\(, \)a_{1,1} = 1\(, \)c=2\(, and \)d=3\(, then the progressive square looks as follows:\)\( \begin{pmatrix} 1 & 4 & 7 \\ 3 & 6 & 9 \\ 5 & 8 & 11 \end{pmatrix} \)\(Last month Maxim constructed a progressive square and remembered the values of \)n\(, \)c\(, and \)d\(. Recently, he found an array \)b\( of \)n^2\( integers in random order and wants to make sure that these elements are the elements of that specific square.It can be shown that for any values of \)n\(, \)a_{1,1}\(, \)c\(, and \)d$$$, there exists exactly one progressive square that satisfies all the rules.
|
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 three integers \(n\), \(c\), and \(d\) (\(2 \le n \le 500\), \(1 \le c, d \le 10^6\)) β the size of the square and the values of \(c\) and \(d\) as described in the statement.The second line of each test case contains \(n \cdot n\) integers \(b_1, b_2, \dots, b_{n \cdot n}\) (\(1 \le b_i \le 10^9\)) β the elements found by Maxim.It is guaranteed that the sum of \(n ^ 2\) over all test cases does not exceed \(25 \cdot {10} ^ 4\).
|
For each test case, output ""YES"" in a separate line if a progressive square for the given \(n\), \(c\), and \(d\) can be constructed from the array elements \(a\), otherwise output ""NO"".You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer.
|
Input: 53 2 33 9 6 5 7 1 10 4 83 2 33 9 6 5 7 1 11 4 82 100 100400 300 400 5003 2 33 9 6 6 5 1 11 4 84 4 415 27 7 19 23 23 11 15 7 3 19 23 11 15 11 15 | Output: NO YES YES NO NO
|
Beginner
| 4 | 853 | 569 | 357 | 19 |
|
490 |
E
|
490E
|
E. Restoring Increasing Sequence
| 2,000 |
binary search; brute force; greedy; implementation
|
Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.Restore the the original sequence knowing digits remaining on the board.
|
The first line of the input contains integer n (1 β€ n β€ 105) β the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
|
If the answer exists, print in the first line ""YES"" (without the quotes). Next n lines must contain the sequence of positive integers β a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them.If there is no answer, print a single line ""NO"" (without the quotes).
|
Input: 3?181? | Output: YES11819
|
Hard
| 4 | 312 | 293 | 519 | 4 |
|
182 |
C
|
182C
|
C. Optimal Sum
| 2,000 |
data structures; greedy
|
And here goes another problem on arrays. You are given positive integer len and array a which consists of n integers a1, a2, ..., an. Let's introduce two characteristics for the given array. Let's consider an arbitrary interval of the array with length len, starting in position i. Value , is the modular sum on the chosen interval. In other words, the modular sum is the sum of integers on the chosen interval with length len, taken in its absolute value. Value is the optimal sum of the array. In other words, the optimal sum of an array is the maximum of all modular sums on various intervals of array with length len. Your task is to calculate the optimal sum of the given array a. However, before you do the calculations, you are allowed to produce no more than k consecutive operations of the following form with this array: one operation means taking an arbitrary number from array ai and multiply it by -1. In other words, no more than k times you are allowed to take an arbitrary number ai from the array and replace it with - ai. Each number of the array is allowed to choose an arbitrary number of times.Your task is to calculate the maximum possible optimal sum of the array after at most k operations described above are completed.
|
The first line contains two integers n, len (1 β€ len β€ n β€ 105) β the number of elements in the array and the length of the chosen subinterval of the array, correspondingly. The second line contains a sequence consisting of n integers a1, a2, ..., an (|ai| β€ 109) β the original array. The third line contains a single integer k (0 β€ k β€ n) β the maximum allowed number of operations. All numbers in lines are separated by a single space.
|
In a single line print the maximum possible optimal sum after no more than k acceptable operations are fulfilled. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
|
Input: 5 30 -2 3 -5 12 | Output: 10
|
Hard
| 2 | 1,244 | 438 | 256 | 1 |
|
142 |
A
|
142A
|
A. Help Farmer
| 1,600 |
brute force; math
|
Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of storing hay simpler and more convenient to use. He collected the hay into cubical hay blocks of the same size. Then he stored the blocks in his barn. After a summer spent in hard toil Sam stored AΒ·BΒ·C hay blocks and stored them in a barn as a rectangular parallelepiped A layers high. Each layer had B rows and each row had C blocks.At the end of the autumn Sam came into the barn to admire one more time the hay he'd been stacking during this hard summer. Unfortunately, Sam was horrified to see that the hay blocks had been carelessly scattered around the barn. The place was a complete mess. As it turned out, thieves had sneaked into the barn. They completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides. As a result, the barn only had a parallelepiped containing (A - 1) Γ (B - 2) Γ (C - 2) hay blocks. To hide the evidence of the crime, the thieves had dissembled the parallelepiped into single 1 Γ 1 Γ 1 blocks and scattered them around the barn. After the theft Sam counted n hay blocks in the barn but he forgot numbers A, B ΠΈ C.Given number n, find the minimally possible and maximally possible number of stolen hay blocks.
|
The only line contains integer n from the problem's statement (1 β€ n β€ 109).
|
Print space-separated minimum and maximum number of hay blocks that could have been stolen by the thieves.Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator.
|
Let's consider the first sample test. If initially Sam has a parallelepiped consisting of 32 = 2 Γ 4 Γ 4 hay blocks in his barn, then after the theft the barn has 4 = (2 - 1) Γ (4 - 2) Γ (4 - 2) hay blocks left. Thus, the thieves could have stolen 32 - 4 = 28 hay blocks. If Sam initially had a parallelepiped consisting of 45 = 5 Γ 3 Γ 3 hay blocks in his barn, then after the theft the barn has 4 = (5 - 1) Γ (3 - 2) Γ (3 - 2) hay blocks left. Thus, the thieves could have stolen 45 - 4 = 41 hay blocks. No other variants of the blocks' initial arrangement (that leave Sam with exactly 4 blocks after the theft) can permit the thieves to steal less than 28 or more than 41 blocks.
|
Input: 4 | Output: 28 41
|
Medium
| 2 | 1,487 | 76 | 370 | 1 |
656 |
A
|
656A
|
A. Da Vinci Powers
| 1,900 |
*special
|
The input contains a single integer a (0 β€ a β€ 35).
|
Output a single integer.
|
Input: 3 | Output: 8
|
Hard
| 1 | 0 | 51 | 24 | 6 |
||
525 |
B
|
525B
|
B. Pasha and String
| 1,400 |
constructive algorithms; greedy; math; strings
|
Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string β each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2Β·ai β€ |s|.You face the following task: determine what Pasha's string will look like after m days.
|
The first line of the input contains Pasha's string s of length from 2 to 2Β·105 characters, consisting of lowercase Latin letters.The second line contains a single integer m (1 β€ m β€ 105) β the number of days when Pasha changed his string.The third line contains m space-separated elements ai (1 β€ ai; 2Β·ai β€ |s|) β the position from which Pasha started transforming the string on the i-th day.
|
In the first line of the output print what Pasha's string s will look like after m days.
|
Input: abcdef12 | Output: aedcbf
|
Easy
| 4 | 617 | 394 | 88 | 5 |
|
639 |
C
|
639C
|
C. Bear and Polynomials
| 2,200 |
hashing; implementation; math
|
Limak is a little polar bear. He doesn't have many toys and thus he often plays with polynomials.He considers a polynomial valid if its degree is n and its coefficients are integers not exceeding k by the absolute value. More formally:Let a0, a1, ..., an denote the coefficients, so . Then, a polynomial P(x) is valid if all the following conditions are satisfied: ai is integer for every i; |ai| β€ k for every i; an β 0. Limak has recently got a valid polynomial P with coefficients a0, a1, a2, ..., an. He noticed that P(2) β 0 and he wants to change it. He is going to change one coefficient to get a valid polynomial Q of degree n that Q(2) = 0. Count the number of ways to do so. You should count two ways as a distinct if coefficients of target polynoms differ.
|
The first line contains two integers n and k (1 β€ n β€ 200 000, 1 β€ k β€ 109) β the degree of the polynomial and the limit for absolute values of coefficients.The second line contains n + 1 integers a0, a1, ..., an (|ai| β€ k, an β 0) β describing a valid polynomial . It's guaranteed that P(2) β 0.
|
Print the number of ways to change one coefficient to get a valid polynomial Q that Q(2) = 0.
|
In the first sample, we are given a polynomial P(x) = 10 - 9x - 3x2 + 5x3.Limak can change one coefficient in three ways: He can set a0 = - 10. Then he would get Q(x) = - 10 - 9x - 3x2 + 5x3 and indeed Q(2) = - 10 - 18 - 12 + 40 = 0. Or he can set a2 = - 8. Then Q(x) = 10 - 9x - 8x2 + 5x3 and indeed Q(2) = 10 - 18 - 32 + 40 = 0. Or he can set a1 = - 19. Then Q(x) = 10 - 19x - 3x2 + 5x3 and indeed Q(2) = 10 - 38 - 12 + 40 = 0. In the second sample, we are given the same polynomial. This time though, k is equal to 12 instead of 109. Two first of ways listed above are still valid but in the third way we would get |a1| > k what is not allowed. Thus, the answer is 2 this time.
|
Input: 3 100000000010 -9 -3 5 | Output: 3
|
Hard
| 3 | 767 | 296 | 93 | 6 |
360 |
B
|
360B
|
B. Levko and Array
| 2,000 |
binary search; dp
|
Levko has an array that consists of integers: a1, a2, ... , an. But he doesnβt like this array at all.Levko thinks that the beauty of the array a directly depends on value c(a), which can be calculated by the formula: The less value c(a) is, the more beautiful the array is.Itβs time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most k array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible.Help Levko and calculate what minimum number c(a) he can reach.
|
The first line contains two integers n and k (1 β€ k β€ n β€ 2000). The second line contains space-separated integers a1, a2, ... , an ( - 109 β€ ai β€ 109).
|
A single number β the minimum value of c(a) Levko can get.
|
In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4.In the third sample he can get array: 1, 2, 3, 4, 5, 6.
|
Input: 5 24 7 4 7 4 | Output: 0
|
Hard
| 2 | 621 | 152 | 58 | 3 |
1,493 |
A
|
1493A
|
A. Anti-knapsack
| 800 |
constructive algorithms; greedy
|
You are given two integers \(n\) and \(k\). You are asked to choose maximum number of distinct integers from \(1\) to \(n\) so that there is no subset of chosen numbers with sum equal to \(k\).A subset of a set is a set that can be obtained from initial one by removing some (possibly all or none) elements of it.
|
The first line contains the number of test cases \(T\) (\(1 \le T \le 100\)).Each of the next \(T\) lines contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 1000\)) β the description of test cases.
|
For each test case output two lines. In the first line output a single integer \(m\) β the number of chosen integers.In the second line output \(m\) distinct integers from \(1\) to \(n\) β the chosen numbers.If there are multiple answers, print any. You can print the numbers in any order.
|
Input: 3 3 2 5 3 1 1 | Output: 2 3 1 3 4 5 2 0
|
Beginner
| 2 | 313 | 205 | 289 | 14 |
|
1,796 |
F
|
1796F
|
F. Strange Triples
| 2,900 |
brute force; math; number theory
|
Let's call a triple of positive integers (\(a, b, n\)) strange if the equality \(\frac{an}{nb} = \frac{a}{b}\) holds, where \(an\) is the concatenation of \(a\) and \(n\) and \(nb\) is the concatenation of \(n\) and \(b\). For the purpose of concatenation, the integers are considered without leading zeroes.For example, if \(a = 1\), \(b = 5\) and \(n = 9\), then the triple is strange, because \(\frac{19}{95} = \frac{1}{5}\). But \(a = 7\), \(b = 3\) and \(n = 11\) is not strange, because \(\frac{711}{113} \ne \frac{7}{3}\).You are given three integers \(A\), \(B\) and \(N\). Calculate the number of strange triples \((a, b, n\)), such that \(1 \le a < A\), \(1 \le b < B\) and \(1 \le n < N\).
|
The only line contains three integers \(A\), \(B\) and \(N\) (\(1 \le A, B \le 10^5\); \(1 \le N \le 10^9\)).
|
Print one integer β the number of strange triples \((a, b, n\)) such that \(1 \le a < A\), \(1 \le b < B\) and \(1 \le n < N\).
|
In the first example, there are \(7\) strange triples: \((1, 1, 1\)), (\(1, 4, 6\)), (\(1, 5, 9\)), (\(2, 2, 2\)), (\(2, 5, 6\)), (\(3, 3, 3\)) and (\(4, 4, 4\)).
|
Input: 5 6 10 | Output: 7
|
Master
| 3 | 700 | 109 | 127 | 17 |
978 |
F
|
978F
|
F. Mentors
| 1,500 |
binary search; data structures; implementation
|
In BerSoft \(n\) programmers work, the programmer \(i\) is characterized by a skill \(r_i\).A programmer \(a\) can be a mentor of a programmer \(b\) if and only if the skill of the programmer \(a\) is strictly greater than the skill of the programmer \(b\) \((r_a > r_b)\) and programmers \(a\) and \(b\) are not in a quarrel.You are given the skills of each programmers and a list of \(k\) pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer \(i\), find the number of programmers, for which the programmer \(i\) can be a mentor.
|
The first line contains two integers \(n\) and \(k\) \((2 \le n \le 2 \cdot 10^5\), \(0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))\) β total number of programmers and number of pairs of programmers which are in a quarrel.The second line contains a sequence of integers \(r_1, r_2, \dots, r_n\) \((1 \le r_i \le 10^{9})\), where \(r_i\) equals to the skill of the \(i\)-th programmer.Each of the following \(k\) lines contains two distinct integers \(x\), \(y\) \((1 \le x, y \le n\), \(x \ne y)\) β pair of programmers in a quarrel. The pairs are unordered, it means that if \(x\) is in a quarrel with \(y\) then \(y\) is in a quarrel with \(x\). Guaranteed, that for each pair \((x, y)\) there are no other pairs \((x, y)\) and \((y, x)\) in the input.
|
Print \(n\) integers, the \(i\)-th number should be equal to the number of programmers, for which the \(i\)-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input.
|
In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel.
|
Input: 4 210 4 10 151 24 3 | Output: 0 0 1 2
|
Medium
| 3 | 569 | 767 | 223 | 9 |
1,486 |
D
|
1486D
|
D. Max Median
| 2,100 |
binary search; data structures; dp
|
You are a given an array \(a\) of length \(n\). Find a subarray \(a[l..r]\) with length at least \(k\) with the largest median.A median in an array of length \(n\) is an element which occupies position number \(\lfloor \frac{n + 1}{2} \rfloor\) after we sort the elements in non-decreasing order. For example: \(median([1, 2, 3, 4]) = 2\), \(median([3, 2, 1]) = 2\), \(median([2, 1, 2, 1]) = 1\).Subarray \(a[l..r]\) is a contiguous part of the array \(a\), i. e. the array \(a_l,a_{l+1},\ldots,a_r\) for some \(1 \leq l \leq r \leq n\), its length is \(r - l + 1\).
|
The first line contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)).
|
Output one integer \(m\) β the maximum median you can get.
|
In the first example all the possible subarrays are \([1..3]\), \([1..4]\), \([1..5]\), \([2..4]\), \([2..5]\) and \([3..5]\) and the median for all of them is \(2\), so the maximum possible median is \(2\) too.In the second example \(median([3..4]) = 3\).
|
Input: 5 3 1 2 3 2 1 | Output: 2
|
Hard
| 3 | 566 | 183 | 58 | 14 |
1,845 |
D
|
1845D
|
D. Rating System
| 1,800 |
binary search; brute force; data structures; dp; dsu; greedy; math; two pointers
|
You are developing a rating system for an online game. Every time a player participates in a match, the player's rating changes depending on the results.Initially, the player's rating is \(0\). There are \(n\) matches; after the \(i\)-th match, the rating change is equal to \(a_i\) (the rating increases by \(a_i\) if \(a_i\) is positive, or decreases by \(|a_i|\) if it's negative. There are no zeros in the sequence \(a\)).The system has an additional rule: for a fixed integer \(k\), if a player's rating has reached the value \(k\), it will never fall below it. Formally, if a player's rating at least \(k\), and a rating change would make it less than \(k\), then the rating will decrease to exactly \(k\).Your task is to determine the value \(k\) in such a way that the player's rating after all \(n\) matches is the maximum possible (among all integer values of \(k\)). If there are multiple possible answers, you can print any of them.
|
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 matches.The second line contains \(n\) integer \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\); \(a_i \ne 0\)) β the rating change after the \(i\)-th match.The sum of \(n\) over all test cases doesn't exceed \(3 \cdot 10^5\).
|
For each test case, print one integer \(m\) (\(-10^{18} \le m \le 10^{18}\)) β the value of \(k\) such that the rating of the player will be the maximum possible when using this value. It can be shown that at least one of the optimal answers meets the constraint \(-10^{18} \le m \le 10^{18}\).
|
In the first example, if \(k=3\), then the rating changes as follows: \(0 \rightarrow 3 \rightarrow 3 \rightarrow 4 \rightarrow 6\).In the second example, if \(k=0\), then the rating changes as follows: \(0 \rightarrow 0 \rightarrow 0 \rightarrow 0\).In the third example, if \(k=25\), then the rating changes as follows: \(0 \rightarrow 4 \rightarrow 6\).In the fourth example, if \(k=6\), then the rating changes as follows: \(0 \rightarrow 5 \rightarrow 6 \rightarrow 6 \rightarrow 8 \rightarrow 7 \rightarrow 6 \rightarrow 8\).
|
Input: 443 -2 1 23-1 -2 -124 275 1 -3 2 -1 -2 2 | Output: 3 0 25 6
|
Medium
| 8 | 944 | 439 | 294 | 18 |
981 |
F
|
981F
|
F. Round Marriage
| 2,500 |
binary search; graph matchings; greedy
|
It's marriage season in Ringland!Ringland has a form of a circle's boundary of length \(L\). There are \(n\) bridegrooms and \(n\) brides, and bridegrooms decided to marry brides.Of course, each bridegroom should choose exactly one bride, and each bride should be chosen by exactly one bridegroom.All objects in Ringland are located on the boundary of the circle, including the capital, bridegrooms' castles and brides' palaces. The castle of the \(i\)-th bridegroom is located at the distance \(a_i\) from the capital in clockwise direction, and the palace of the \(i\)-th bride is located at the distance \(b_i\) from the capital in clockwise direction.Let's define the inconvenience of a marriage the maximum distance that some bride should walk along the circle from her palace to her bridegroom's castle in the shortest direction (in clockwise or counter-clockwise direction).Help the bridegrooms of Ringland to choose brides in such a way that the inconvenience of the marriage is the smallest possible.
|
The first line contains two integers \(n\) and \(L\) (\(1 \leq n \leq 2 \cdot 10^{5}\), \(1 \leq L \leq 10^{9}\)) β the number of bridegrooms and brides and the length of Ringland.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < L\)) β the distances from the capital to the castles of bridegrooms in clockwise direction.The next line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(0 \leq b_i < L\)) β the distances from the capital to the palaces of brides in clockwise direction.
|
In the only line print the smallest possible inconvenience of the wedding, where the inconvenience is the largest distance traveled by a bride.
|
In the first example the first bridegroom should marry the second bride, the second bridegroom should marry the first bride. This way, the second bride should walk the distance of \(1\), and the first bride should also walk the same distance. Thus, the inconvenience is equal to \(1\).In the second example let \(p_i\) be the bride the \(i\)-th bridegroom will marry. One of optimal \(p\) is the following: \((6,8,1,4,5,10,3,2,7,9)\).
|
Input: 2 40 12 3 | Output: 1
|
Expert
| 3 | 1,009 | 517 | 143 | 9 |
1,365 |
G
|
1365G
|
G. Secure Password
| 2,800 |
bitmasks; combinatorics; constructive algorithms; interactive; math
|
This is an interactive problem.Ayush devised yet another scheme to set the password of his lock. The lock has \(n\) slots where each slot can hold any non-negative integer. The password \(P\) is a sequence of \(n\) integers, \(i\)-th element of which goes into the \(i\)-th slot of the lock.To set the password, Ayush comes up with an array \(A\) of \(n\) integers each in the range \([0, 2^{63}-1]\). He then sets the \(i\)-th element of \(P\) as the bitwise OR of all integers in the array except \(A_i\).You need to guess the password. To make a query, you can choose a non-empty subset of indices of the array and ask the bitwise OR all elements of the array with index in this subset. You can ask no more than 13 queries.
|
The first line of input contains one integer \(n\) \((2 \le n \le 1000)\) β the number of slots in the lock.
|
The array \(A\) in the example is \(\{{1, 2, 4\}}\). The first element of the password is bitwise OR of \(A_2\) and \(A_3\), the second element is bitwise OR of \(A_1\) and \(A_3\) and the third element is bitwise OR of \(A_1\) and \(A_2\). Hence the password sequence is \(\{{6, 5, 3\}}\).
|
Input: 3 1 2 4 | Output: ? 1 1 ? 1 2 ? 1 3 ! 6 5 3
|
Master
| 5 | 726 | 108 | 0 | 13 |
|
1,776 |
H
|
1776H
|
H. Beppa and SwerChat
| 1,300 |
two pointers
|
Beppa and her circle of geek friends keep up to date on a group chat in the instant messaging app SwerChat\(^{\text{TM}}\).The group has \(n\) members, excluding Beppa. Each of those members has a unique ID between \(1\) and \(n\). When a user opens a group chat, SwerChat\(^{\text{TM}}\) displays the list of other members of that group, sorted by decreasing times of last seen online (so the member who opened the chat most recently is the first of the list). However, the times of last seen are not displayed.Today, Beppa has been busy all day: she has only opened the group chat twice, once at 9:00 and once at 22:00. Both times, she wrote down the list of members in the order they appeared at that time. Now she wonders: what is the minimum number of other members that must have been online at least once between 9:00 and 22:00?Beppa is sure that no two members are ever online at the same time and no members are online when Beppa opens the group chat at 9:00 and 22:00.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10\,000\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of members of the group excluding Beppa.The second line contains \(n\) integers \(a_1, \, a_2, \, \dots, \, a_n\) (\(1 \le a_i \le n\)) β the list of IDs of the members, sorted by decreasing times of last seen online at 9:00.The third line contains \(n\) integers \(b_1, \, b_2, \, \dots, \, b_n\) (\(1 \le b_i \le n\)) β the list of IDs of the members, sorted by decreasing times of last seen online at 22:00.For all \(1\le i < j\le n\), it is guaranteed that \(a_i \ne a_j\) and \(b_i \ne b_j\).It is also guaranteed that the sum of the values of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print the minimum number of members that must have been online between 9:00 and 22:00.
|
In the first test case, members \(4, 5\) must have been online between 9:00 and 22:00.In the second test case, it is possible that nobody has been online between 9:00 and 22:00.
|
Input: 451 4 2 5 34 5 1 2 361 2 3 4 5 61 2 3 4 5 688 2 4 7 1 6 5 35 6 1 4 8 2 7 3111 | Output: 2 0 4 0
|
Easy
| 1 | 978 | 882 | 106 | 17 |
1,147 |
D
|
1147D
|
D. Palindrome XOR
| 2,400 |
dfs and similar; graphs
|
You are given a string \(s\) consisting of characters ""1"", ""0"", and ""?"". The first character of \(s\) is guaranteed to be ""1"". Let \(m\) be the number of characters in \(s\).Count the number of ways we can choose a pair of integers \(a, b\) that satisfies the following: \(1 \leq a < b < 2^m\) When written without leading zeros, the base-2 representations of \(a\) and \(b\) are both palindromes. The base-2 representation of bitwise XOR of \(a\) and \(b\) matches the pattern \(s\). We say that \(t\) matches \(s\) if the lengths of \(t\) and \(s\) are the same and for every \(i\), the \(i\)-th character of \(t\) is equal to the \(i\)-th character of \(s\), or the \(i\)-th character of \(s\) is ""?"". Compute this count modulo \(998244353\).
|
The first line contains a single string \(s\) (\(1 \leq |s| \leq 1\,000\)). \(s\) consists only of characters ""1"", ""0"" and ""?"". It is guaranteed that the first character of \(s\) is a ""1"".
|
Print a single integer, the count of pairs that satisfy the conditions modulo \(998244353\).
|
For the first example, the pairs in base-2 are \((111, 10001), (11, 10101), (1001, 11111)\).
|
Input: 10110 | Output: 3
|
Expert
| 2 | 755 | 196 | 92 | 11 |
1,995 |
A
|
1995A
|
A. Diagonals
| 800 |
brute force; greedy; implementation; math
|
Vitaly503 is given a checkered board with a side of \(n\) and \(k\) chips. He realized that all these \(k\) chips need to be placed on the cells of the board (no more than one chip can be placed on a single cell).Let's denote the cell in the \(i\)-th row and \(j\)-th column as \((i ,j)\). A diagonal is the set of cells for which the value \(i + j\) is the same. For example, cells \((3, 1)\), \((2, 2)\), and \((1, 3)\) lie on the same diagonal, but \((1, 2)\) and \((2, 3)\) do not. A diagonal is called occupied if it contains at least one chip. Determine what is the minimum possible number of occupied diagonals among all placements of \(k\) chips.
|
Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of sets of input data. Then follow the descriptions of the sets of input data.The only line of each set of input data contains two integers \(n\), \(k\) (\(1 \le n \le 100, 0 \le k \le n^2\)) β the side of the checkered board and the number of available chips, respectively.
|
For each set of input data, output a single integer β the minimum number of occupied diagonals with at least one chip that he can get after placing all \(k\) chips.
|
In the first test case, there are no chips, so 0 diagonals will be occupied. In the second test case, both chips can be placed on diagonal \((2, 1), (1, 2)\), so the answer is 1. In the third test case, 3 chips can't be placed on one diagonal, but placing them on \((1, 2), (2, 1), (1, 1)\) makes 2 diagonals occupied. In the 7th test case, chips will occupy all 5 diagonals in any valid placing.
|
Input: 71 02 22 32 410 50100 2393 9 | Output: 0 1 2 3 6 3 5
|
Beginner
| 4 | 654 | 406 | 164 | 19 |
1,821 |
F
|
1821F
|
F. Timber
| 2,600 |
combinatorics; dp; fft; math
|
There is a beautiful alley with trees in front of a shopping mall. Unfortunately, it has to go to make space for the parking lot.The trees on the alley all grow in a single line. There are \(n\) spots for trees, index \(0\) is the shopping mall, index \(n+1\) is the road and indices from \(1\) to \(n\) are the spots for trees. Some of them are taken β there grow trees of the same height \(k\). No more than one tree grows in each spot.When you chop down a tree in the spot \(x\), you can make it fall either left or right. If it falls to the left, it takes up spots from \(x-k\) to \(x\), inclusive. If it falls to the right, it takes up spots from \(x\) to \(x+k\), inclusive.Let \(m\) trees on the alley grow in some spots \(x_1, x_2, \dots, x_m\). Let an alley be called unfortunate if all \(m\) trees can be chopped down in such a way that: no tree falls on the shopping mall or the road; each spot is taken up by no more than one fallen tree. Calculate the number of different unfortunate alleys with \(m\) trees of height \(k\). Two alleys are considered different if there is a spot \(y\) such that a tree grows in \(y\) on the first alley and doesn't grow in \(y\) on the second alley.Output the number modulo \(998\,244\,353\).
|
The only line contains three integers \(n, m\) and \(k\) (\(1 \le m, k \le n \le 3 \cdot 10^5\)) β the number of spots for the trees, the number of trees and the height of each tree.
|
Print a single integer β the number of different unfortunate alleys with \(m\) trees of height \(k\), modulo \(998\,244\,353\).
|
Input: 6 1 4 | Output: 4
|
Expert
| 4 | 1,239 | 182 | 127 | 18 |
|
1,893 |
C
|
1893C
|
C. Freedom of Choice
| 2,000 |
brute force; greedy; implementation
|
Let's define the anti-beauty of a multiset \(\{b_1, b_2, \ldots, b_{len}\}\) as the number of occurrences of the number \(len\) in the multiset.You are given \(m\) multisets, where the \(i\)-th multiset contains \(n_i\) distinct elements, specifically: \(c_{i, 1}\) copies of the number \(a_{i,1}\), \(c_{i, 2}\) copies of the number \(a_{i,2}, \ldots, c_{i, n_i}\) copies of the number \(a_{i, n_i}\). It is guaranteed that \(a_{i, 1} < a_{i, 2} < \ldots < a_{i, n_i}\). You are also given numbers \(l_1, l_2, \ldots, l_m\) and \(r_1, r_2, \ldots, r_m\) such that \(1 \le l_i \le r_i \le c_{i, 1} + \ldots + c_{i, n_i}\).Let's create a multiset \(X\), initially empty. Then, for each \(i\) from \(1\) to \(m\), you must perform the following action exactly once: Choose some \(v_i\) such that \(l_i \le v_i \le r_i\) Choose any \(v_i\) numbers from the \(i\)-th multiset and add them to the multiset \(X\).You need to choose \(v_1, \ldots, v_m\) and the added numbers in such a way that the resulting multiset \(X\) has the minimum possible anti-beauty.
|
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 \(m\) (\(1 \le m \le 10^5\)) β the number of given multisets.Then, for each \(i\) from \(1\) to \(m\), a data block consisting of three lines is entered.The first line of each block contains three integers \(n_i, l_i, r_i\) (\(1 \le n_i \le 10^5, 1 \le l_i \le r_i \le c_{i, 1} + \ldots + c_{i, n_i} \le 10^{17}\)) β the number of distinct numbers in the \(i\)-th multiset and the limits on the number of elements to be added to \(X\) from the \(i\)-th multiset.The second line of the block contains \(n_i\) integers \(a_{i, 1}, \ldots, a_{i, n_i}\) (\(1 \le a_{i, 1} < \ldots < a_{i, n_i} \le 10^{17}\)) β the distinct elements of the \(i\)-th multiset.The third line of the block contains \(n_i\) integers \(c_{i, 1}, \ldots, c_{i, n_i}\) (\(1 \le c_{i, j} \le 10^{12}\)) β the number of copies of the elements in the \(i\)-th multiset.It is guaranteed that the sum of the values of \(m\) for all test cases does not exceed \(10^5\), and also the sum of \(n_i\) for all blocks of all test cases does not exceed \(10^5\).
|
For each test case, output the minimum possible anti-beauty of the multiset \(X\) that you can achieve.
|
In the first test case, the multisets have the following form: \(\{10, 10, 10, 11, 11, 11, 12\}\). From this multiset, you need to select between \(5\) and \(6\) numbers. \(\{12, 12, 12, 12\}\). From this multiset, you need to select between \(1\) and \(3\) numbers. \(\{12, 13, 13, 13, 13, 13\}\). From this multiset, you need to select \(4\) numbers.You can select the elements \(\{10, 11, 11, 11, 12\}\) from the first multiset, \(\{12\}\) from the second multiset, and \(\{13, 13, 13, 13\}\) from the third multiset. Thus, \(X = \{10, 11, 11, 11, 12, 12, 13, 13, 13, 13\}\). The size of \(X\) is \(10\), the number \(10\) appears exactly \(1\) time in \(X\), so the anti-beauty of \(X\) is \(1\). It can be shown that it is not possible to achieve an anti-beauty less than \(1\).
|
Input: 733 5 610 11 123 3 11 1 31242 4 412 131 517 1000 10061000 1001 1002 1003 1004 1005 1006147 145 143 143 143 143 14212 48 5048 5025 2521 1 1111 1 12111 1 11221 1 1112 1 11 21 124 8 1011 12 13 143 3 3 32 3 411 122 2 | Output: 1 139 0 1 1 0 0
|
Hard
| 3 | 1,054 | 1,264 | 103 | 18 |
1,490 |
G
|
1490G
|
G. Old Floppy Drive
| 1,900 |
binary search; data structures; math
|
Polycarp was dismantling his attic and found an old floppy drive on it. A round disc was inserted into the drive with \(n\) integers written on it.Polycarp wrote the numbers from the disk into the \(a\) array. It turned out that the drive works according to the following algorithm: the drive takes one positive number \(x\) as input and puts a pointer to the first element of the \(a\) array; after that, the drive starts rotating the disk, every second moving the pointer to the next element, counting the sum of all the elements that have been under the pointer. Since the disk is round, in the \(a\) array, the last element is again followed by the first one; as soon as the sum is at least \(x\), the drive will shut down. Polycarp wants to learn more about the operation of the drive, but he has absolutely no free time. So he asked you \(m\) questions. To answer the \(i\)-th of them, you need to find how many seconds the drive will work if you give it \(x_i\) as input. Please note that in some cases the drive can work infinitely.For example, if \(n=3, m=3\), \(a=[1, -3, 4]\) and \(x=[1, 5, 2]\), then the answers to the questions are as follows: the answer to the first query is \(0\) because the drive initially points to the first item and the initial sum is \(1\). the answer to the second query is \(6\), the drive will spin the disk completely twice and the amount becomes \(1+(-3)+4+1+(-3)+4+1=5\). the answer to the third query is \(2\), the amount is \(1+(-3)+4=2\).
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case consists of two positive integers \(n\), \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of numbers on the disk and the number of asked questions.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)).The third line of each test case contains \(m\) positive integers \(x_1, x_2, \ldots, x_m\) (\(1 \le x \le 10^9\)).It is guaranteed that the sums of \(n\) and \(m\) over all test cases do not exceed \(2 \cdot 10^5\).
|
Print \(m\) numbers on a separate line for each test case. The \(i\)-th number is: \(-1\) if the drive will run infinitely; the number of seconds the drive will run, otherwise.
|
Input: 3 3 3 1 -3 4 1 5 2 2 2 -2 0 1 2 2 2 0 1 1 2 | Output: 0 6 2 -1 -1 1 3
|
Hard
| 3 | 1,486 | 635 | 176 | 14 |
|
1,618 |
G
|
1618G
|
G. Trader Problem
| 2,200 |
data structures; dsu; greedy; sortings
|
Monocarp plays a computer game (yet again!). This game has a unique trading mechanics.To trade with a character, Monocarp has to choose one of the items he possesses and trade it for some item the other character possesses. Each item has an integer price. If Monocarp's chosen item has price \(x\), then he can trade it for any item (exactly one item) with price not greater than \(x+k\).Monocarp initially has \(n\) items, the price of the \(i\)-th item he has is \(a_i\). The character Monocarp is trading with has \(m\) items, the price of the \(i\)-th item they have is \(b_i\). Monocarp can trade with this character as many times as he wants (possibly even zero times), each time exchanging one of his items with one of the other character's items according to the aforementioned constraints. Note that if Monocarp gets some item during an exchange, he can trade it for another item (since now the item belongs to him), and vice versa: if Monocarp trades one of his items for another item, he can get his item back by trading something for it.You have to answer \(q\) queries. Each query consists of one integer, which is the value of \(k\), and asks you to calculate the maximum possible total cost of items Monocarp can have after some sequence of trades, assuming that he can trade an item of cost \(x\) for an item of cost not greater than \(x+k\) during each trade. Note that the queries are independent: the trades do not actually occur, Monocarp only wants to calculate the maximum total cost he can get.
|
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m, q \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the prices of the items Monocarp has.The third line contains \(m\) integers \(b_1, b_2, \dots, b_m\) (\(1 \le b_i \le 10^9\)) β the prices of the items the other character has.The fourth line contains \(q\) integers, where the \(i\)-th integer is the value of \(k\) for the \(i\)-th query (\(0 \le k \le 10^9\)).
|
For each query, print one integer β the maximum possible total cost of items Monocarp can have after some sequence of trades, given the value of \(k\) from the query.
|
Input: 3 4 5 10 30 15 12 31 14 18 0 1 2 3 4 | Output: 55 56 60 64 64
|
Hard
| 4 | 1,517 | 503 | 166 | 16 |
|
1,023 |
D
|
1023D
|
D. Array Restoration
| 1,700 |
constructive algorithms; data structures
|
Initially there was an array \(a\) consisting of \(n\) integers. Positions in it are numbered from \(1\) to \(n\).Exactly \(q\) queries were performed on the array. During the \(i\)-th query some segment \((l_i, r_i)\) \((1 \le l_i \le r_i \le n)\) was selected and values of elements on positions from \(l_i\) to \(r_i\) inclusive got changed to \(i\). The order of the queries couldn't be changed and all \(q\) queries were applied. It is also known that every position from \(1\) to \(n\) got covered by at least one segment.We could have offered you the problem about checking if some given array (consisting of \(n\) integers with values from \(1\) to \(q\)) can be obtained by the aforementioned queries. However, we decided that it will come too easy for you.So the enhancement we introduced to it is the following. Some set of positions (possibly empty) in this array is selected and values of elements on these positions are set to \(0\).Your task is to check if this array can be obtained by the aforementioned queries. Also if it can be obtained then restore this array.If there are multiple possible arrays then print any of them.
|
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β the number of elements of the array and the number of queries perfomed on it.The second line contains \(n\) integer numbers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le q\)) β the resulting array. If element at some position \(j\) is equal to \(0\) then the value of element at this position can be any integer from \(1\) to \(q\).
|
Print ""YES"" if the array \(a\) can be obtained by performing \(q\) queries. Segments \((l_i, r_i)\) \((1 \le l_i \le r_i \le n)\) are chosen separately for each query. Every position from \(1\) to \(n\) should be covered by at least one segment. Otherwise print ""NO"".If some array can be obtained then print \(n\) integers on the second line β the \(i\)-th number should be equal to the \(i\)-th element of the resulting array and should have value from \(1\) to \(q\). This array should be obtainable by performing exactly \(q\) queries.If there are multiple possible arrays then print any of them.
|
In the first example you can also replace \(0\) with \(1\) but not with \(3\).In the second example it doesn't really matter what segments to choose until query \(10\) when the segment is \((1, 3)\).The third example showcases the fact that the order of queries can't be changed, you can't firstly set \((1, 3)\) to \(6\) and after that change \((2, 2)\) to \(5\). The segment of \(5\) should be applied before segment of \(6\).There is a lot of correct resulting arrays for the fourth example.
|
Input: 4 31 0 2 3 | Output: YES1 2 2 3
|
Medium
| 2 | 1,142 | 417 | 603 | 10 |
64 |
F
|
64F
|
F. Domain
| 2,000 |
*special; expression parsing
|
This problem doesn't contain real-world specifications about domains, just use the problem statement as a formal document to solve the problem.The given string s is a domain name if it contains the characters ""a""-""z"", ""0""-""9"" and dots. No two dots shoud follow one after another (consecutive). The dots split the given string s into the parts, the last (most right) part should have the length 2 or 3. Domain can't start or end with a dot.You are given the string s, check if it is domain name.
|
The only line of the input contains given string s. The string may contain any characters with ASCII codes from 33 to 127, inclusive. The string length is between 1 and 100, inclusive.
|
Print ""YES"" if the given string s is a domain name, or print ""NO"" if it is not.
|
Input: codeforces.com | Output: YES
|
Hard
| 2 | 502 | 184 | 83 | 0 |
|
68 |
E
|
68E
|
E. Contact
| 2,900 |
geometry
|
Little Petya is preparing for the first contact with aliens. He knows that alien spaceships have shapes of non-degenerate triangles and there will be exactly 4 ships. Landing platform for a ship can be made of 3 special columns located at some points of a Cartesian plane such that these 3 points form a triangle equal to the ship with respect to rotations, translations (parallel shifts along some vector) and reflections (symmetries along the edges). The ships can overlap after the landing.Each column can be used to land more than one ship, for example, if there are two equal ships, we don't need to build 6 columns to land both ships, 3 will be enough. Petya wants to know what minimum number of columns will be enough to land all ships.
|
Each of 4 lines will contain 6 integers x1 y1 x2 y2 x3 y3 (0 β€ x1, y1, x2, y2, x3, y3 β€ 20), representing 3 points that describe the shape of each of 4 ships. It is guaranteed that 3 points in each line will represent a non-degenerate triangle.
|
First line should contain minimum number of columns enough to land all spaceships.
|
In the first test case columns can be put in these points: (0, 0), (1, 0), (3, 0), (1, 2). Note that the second ship can land using last 3 columns.In the second test case following points can be chosen: (0, 0), (0, 1), (1, 0), (0, 2), (2, 0), (0, 5), (5, 0), (0, 17), (17, 0). It is impossible to use less than 9 columns.
|
Input: 0 0 1 0 1 20 0 0 2 2 20 0 3 0 1 20 0 3 0 2 2 | Output: 4
|
Master
| 1 | 743 | 244 | 82 | 0 |
749 |
B
|
749B
|
B. Parallelogram is Back
| 1,200 |
brute force; constructive algorithms; geometry
|
Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not necessary were given in the order of clockwise or counterclockwise traversal.Alex had very nice test for this problem, but is somehow happened that the last line of the input was lost and now he has only three out of four points of the original parallelogram. He remembers that test was so good that he asks you to restore it given only these three points.
|
The input consists of three lines, each containing a pair of integer coordinates xi and yi ( - 1000 β€ xi, yi β€ 1000). It's guaranteed that these three points do not lie on the same line and no two of them coincide.
|
First print integer k β the number of ways to add one new integer point such that the obtained set defines some parallelogram of positive area. There is no requirement for the points to be arranged in any special order (like traversal), they just define the set of vertices.Then print k lines, each containing a pair of integer β possible coordinates of the fourth point.
|
If you need clarification of what parallelogram is, please check Wikipedia page:https://en.wikipedia.org/wiki/Parallelogram
|
Input: 0 01 00 1 | Output: 31 -1-1 11 1
|
Easy
| 3 | 611 | 214 | 371 | 7 |
119 |
E
|
119E
|
E. Alternative Reality
| 2,400 |
geometry
|
In the year of 3000 travelling around parallel realities became a routine thing. However one has to take into consideration that travelling like that is highly dangerous as you never know beforehand where you're gonna get...Little Vasya, for instance, found himself in a gaming reality and now he has to successfully complete all levels of a very weird game to get back. The gaming reality is a three-dimensional space where n points are given. The game has m levels and at the beginning of the i-th level the player is positioned at some plane Qi that passes through the origin. On each level Vasya has to use special robots to construct and activate n powerful energy spheres of the equal radius with centers at the given points. The player chooses the radius of the spheres himself. The player has to spend R units of money to construct spheres whose radius equals R (consequently, one can construct spheres whose radius equals zero for free). Besides, once for each level a player can choose any point in space and release a laser ray from there, perpendicular to plane Qi (this action costs nothing). The ray can either be directed towards the plane or from the plane. The spheres that share at least one point with the ray will be immediately activated. The level is considered completed if the player has managed to activate all spheres. Note that the centers of the spheres are the same for all m levels but the spheres do not remain: the player should construct them anew on each new level.Help Vasya find out what minimum sum of money will be enough to complete each level.
|
The first line contains two integers n and m (1 β€ n β€ 900, 1 β€ m β€ 100) β the number of energetic spheres and the number of levels in the game correspondingly. Each of the following n lines contains three integers xi, yi, zi (0 β€ xi, yi, zi β€ 104) β the coordinates of the center of the i-th sphere. Assume that these points do not change their positions throughout the game.Then follow m lines, each containing three integers ai, bi, ci (0 β€ ai, bi, ci β€ 100, ai2 + bi2 + ci2 > 0). These numbers are the coefficients in the equation of plane Qi (aix + biy + ciz = 0), where the player is positioned at the beginning of the i-th level.
|
Print m numbers, one per line: the i-th line should contain the minimum sum of money needed to complete the i-th level. The absolute or relative error should not exceed 10 - 6.
|
Input: 4 10 0 00 1 01 0 01 1 00 0 1 | Output: 0.7071067812
|
Expert
| 1 | 1,583 | 635 | 176 | 1 |
|
976 |
D
|
976D
|
D. Degree Set
| 2,500 |
constructive algorithms; graphs; implementation
|
You are given a sequence of n positive integers d1, d2, ..., dn (d1 < d2 < ... < dn). Your task is to construct an undirected graph such that: there are exactly dn + 1 vertices; there are no self-loops; there are no multiple edges; there are no more than 106 edges; its degree set is equal to d. Vertices should be numbered 1 through (dn + 1).Degree sequence is an array a with length equal to the number of vertices in a graph such that ai is the number of vertices adjacent to i-th vertex.Degree set is a sorted in increasing order sequence of all distinct values from the degree sequence.It is guaranteed that there exists such a graph that all the conditions hold, and it contains no more than 106 edges.Print the resulting graph.
|
The first line contains one integer n (1 β€ n β€ 300) β the size of the degree set.The second line contains n integers d1, d2, ..., dn (1 β€ di β€ 1000, d1 < d2 < ... < dn) β the degree set.
|
In the first line print one integer m (1 β€ m β€ 106) β the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges.Each of the next m lines should contain two integers vi and ui (1 β€ vi, ui β€ dn + 1) β the description of the i-th edge.
|
Input: 32 3 4 | Output: 83 14 24 52 55 13 22 15 3
|
Expert
| 3 | 734 | 186 | 334 | 9 |
|
141 |
E
|
141E
|
E. Clearing Up
| 2,300 |
constructive algorithms; dp; dsu; graphs
|
After Santa Claus and his assistant Elf delivered all the presents and made all the wishes come true, they returned to the North Pole and found out that it is all covered with snow. Both of them were quite tired and they decided only to remove the snow from the roads connecting huts. The North Pole has n huts connected with m roads. One can go along the roads in both directions. The Elf offered to split: Santa Claus will clear up the wide roads and the Elf will tread out the narrow roads. For each road they decided who will clear it: Santa Claus or the Elf. To minimize the efforts they decided to clear the road so as to fulfill both those conditions: between any two huts should exist exactly one simple path along the cleared roads; Santa Claus and the Elf should clear the same number of roads. At this point Santa Claus and his assistant Elf wondered which roads should they clear up?
|
The first input line contains two positive integers n and m (1 β€ n β€ 103, 1 β€ m β€ 105) β the number of huts and the number of roads. Then follow m lines, each of them contains a road description: the numbers of huts it connects β x and y (1 β€ x, y β€ n) and the person responsible for clearing out this road (""S"" β for the Elf or ""M"" for Santa Claus). It is possible to go on each road in both directions. Note that there can be more than one road between two huts and a road can begin and end in the same hut.
|
Print ""-1"" without the quotes if it is impossible to choose the roads that will be cleared by the given rule. Otherwise print in the first line how many roads should be cleared and in the second line print the numbers of those roads (the roads are numbered from 1 in the order of occurrence in the input). It is allowed to print the numbers of the roads in any order. Each number should be printed exactly once. As you print the numbers, separate them with spaces.
|
A path is called simple if all huts on it are pairwise different.
|
Input: 1 21 1 S1 1 M | Output: 0
|
Expert
| 4 | 895 | 513 | 466 | 1 |
643 |
C
|
643C
|
C. Levels and Regions
| 2,400 |
dp
|
Radewoosh is playing a computer game. There are n levels, numbered 1 through n. Levels are divided into k regions (groups). Each region contains some positive number of consecutive levels.The game repeats the the following process: If all regions are beaten then the game ends immediately. Otherwise, the system finds the first region with at least one non-beaten level. Let X denote this region. The system creates an empty bag for tokens. Each token will represent one level and there may be many tokens representing the same level. For each already beaten level i in the region X, the system adds ti tokens to the bag (tokens representing the i-th level). Let j denote the first non-beaten level in the region X. The system adds tj tokens to the bag. Finally, the system takes a uniformly random token from the bag and a player starts the level represented by the token. A player spends one hour and beats the level, even if he has already beaten it in the past. Given n, k and values t1, t2, ..., tn, your task is to split levels into regions. Each level must belong to exactly one region, and each region must contain non-empty consecutive set of levels. What is the minimum possible expected number of hours required to finish the game?
|
The first line of the input contains two integers n and k (1 β€ n β€ 200 000, 1 β€ k β€ min(50, n)) β the number of levels and the number of regions, respectively.The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 100 000).
|
Print one real number β the minimum possible expected value of the number of hours spent to finish the game if levels are distributed between regions in the optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .
|
In the first sample, we are supposed to split 4 levels into 2 regions. It's optimal to create the first region with only one level (it must be the first level). Then, the second region must contain other three levels.In the second sample, it's optimal to split levels into two regions with 3 levels each.
|
Input: 4 2100 3 5 7 | Output: 5.7428571429
|
Expert
| 1 | 1,242 | 230 | 401 | 6 |
1,070 |
B
|
1070B
|
B. Berkomnadzor
| 2,400 |
data structures; greedy
|
Berkomnadzor β Federal Service for Supervision of Communications, Information Technology and Mass Media β is a Berland federal executive body that protects ordinary residents of Berland from the threats of modern internet.Berkomnadzor maintains a list of prohibited IPv4 subnets (blacklist) and a list of allowed IPv4 subnets (whitelist). All Internet Service Providers (ISPs) in Berland must configure the network equipment to block access to all IPv4 addresses matching the blacklist. Also ISPs must provide access (that is, do not block) to all IPv4 addresses matching the whitelist. If an IPv4 address does not match either of those lists, it's up to the ISP to decide whether to block it or not. An IPv4 address matches the blacklist (whitelist) if and only if it matches some subnet from the blacklist (whitelist). An IPv4 address can belong to a whitelist and to a blacklist at the same time, this situation leads to a contradiction (see no solution case in the output description).An IPv4 address is a 32-bit unsigned integer written in the form \(a.b.c.d\), where each of the values \(a,b,c,d\) is called an octet and is an integer from \(0\) to \(255\) written in decimal notation. For example, IPv4 address \(192.168.0.1\) can be converted to a 32-bit number using the following expression \(192 \cdot 2^{24} + 168 \cdot 2^{16} + 0 \cdot 2^8 + 1 \cdot 2^0\). First octet \(a\) encodes the most significant (leftmost) \(8\) bits, the octets \(b\) and \(c\) β the following blocks of \(8\) bits (in this order), and the octet \(d\) encodes the least significant (rightmost) \(8\) bits.The IPv4 network in Berland is slightly different from the rest of the world. There are no reserved or internal addresses in Berland and use all \(2^{32}\) possible values.An IPv4 subnet is represented either as \(a.b.c.d\) or as \(a.b.c.d/x\) (where \(0 \le x \le 32\)). A subnet \(a.b.c.d\) contains a single address \(a.b.c.d\). A subnet \(a.b.c.d/x\) contains all IPv4 addresses with \(x\) leftmost (most significant) bits equal to \(x\) leftmost bits of the address \(a.b.c.d\). It is required that \(32 - x\) rightmost (least significant) bits of subnet \(a.b.c.d/x\) are zeroes.Naturally it happens that all addresses matching subnet \(a.b.c.d/x\) form a continuous range. The range starts with address \(a.b.c.d\) (its rightmost \(32 - x\) bits are zeroes). The range ends with address which \(x\) leftmost bits equal to \(x\) leftmost bits of address \(a.b.c.d\), and its \(32 - x\) rightmost bits are all ones. Subnet contains exactly \(2^{32-x}\) addresses. Subnet \(a.b.c.d/32\) contains exactly one address and can also be represented by just \(a.b.c.d\).For example subnet \(192.168.0.0/24\) contains range of 256 addresses. \(192.168.0.0\) is the first address of the range, and \(192.168.0.255\) is the last one.Berkomnadzor's engineers have devised a plan to improve performance of Berland's global network. Instead of maintaining both whitelist and blacklist they want to build only a single optimised blacklist containing minimal number of subnets. The idea is to block all IPv4 addresses matching the optimised blacklist and allow all the rest addresses. Of course, IPv4 addresses from the old blacklist must remain blocked and all IPv4 addresses from the old whitelist must still be allowed. Those IPv4 addresses which matched neither the old blacklist nor the old whitelist may be either blocked or allowed regardless of their accessibility before. Please write a program which takes blacklist and whitelist as input and produces optimised blacklist. The optimised blacklist must contain the minimal possible number of subnets and satisfy all IPv4 addresses accessibility requirements mentioned above. IPv4 subnets in the source lists may intersect arbitrarily. Please output a single number -1 if some IPv4 address matches both source whitelist and blacklist.
|
The first line of the input contains single integer \(n\) (\(1 \le n \le 2\cdot10^5\)) β total number of IPv4 subnets in the input.The following \(n\) lines contain IPv4 subnets. Each line starts with either '-' or '+' sign, which indicates if the subnet belongs to the blacklist or to the whitelist correspondingly. It is followed, without any spaces, by the IPv4 subnet in \(a.b.c.d\) or \(a.b.c.d/x\) format (\(0 \le x \le 32\)). The blacklist always contains at least one subnet.All of the IPv4 subnets given in the input are valid. Integer numbers do not start with extra leading zeroes. The provided IPv4 subnets can intersect arbitrarily.
|
Output -1, if there is an IPv4 address that matches both the whitelist and the blacklist. Otherwise output \(t\) β the length of the optimised blacklist, followed by \(t\) subnets, with each subnet on a new line. Subnets may be printed in arbitrary order. All addresses matching the source blacklist must match the optimised blacklist. All addresses matching the source whitelist must not match the optimised blacklist. You can print a subnet \(a.b.c.d/32\) in any of two ways: as \(a.b.c.d/32\) or as \(a.b.c.d\).If there is more than one solution, output any.
|
Input: 1-149.154.167.99 | Output: 10.0.0.0/0
|
Expert
| 2 | 3,876 | 645 | 561 | 10 |
|
42 |
C
|
42C
|
C. Safe cracking
| 2,200 |
brute force; constructive algorithms
|
Right now you are to solve a very, very simple problem β to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this striking safe you have to make all four numbers equal to one. Operations are as follows: you may choose two adjacent numbers and increase both by one; you may choose two adjacent even numbers and divide both by two. Nothing else. Crack the safe!
|
The single line of the input contains four space-separated integer positive numbers not greater than 109 each β four numbers on the circle in consecutive order.
|
The output should contain ""-1"" (quotes for clarity) if the safe is secure, that is it's impossible to crack it. Otherwise, output should contain the sequence of operations (one operations per line) leading to unlocking the safe. You don't have to minimize the number of operations, but it should not exceed 1000. To make things clear, assume numbers stand on positions 1 through 4. Each operation is encoded by two symbols. If the following operation is dividing then first symbol is '/'; otherwise it's '+' (addition). The second symbol is the position of the first number in pair in consecutive order. (see samples for clarification).If there are several solutions, output any of them.
|
Input: 1 1 1 1 | Output:
|
Hard
| 2 | 423 | 160 | 689 | 0 |
|
1,956 |
A
|
1956A
|
A. Nene's Game
| 800 |
binary search; brute force; data structures; games; greedy
|
Nene invented a new game based on an increasing sequence of integers \(a_1, a_2, \ldots, a_k\).In this game, initially \(n\) players are lined up in a row. In each of the rounds of this game, the following happens: Nene finds the \(a_1\)-th, \(a_2\)-th, \(\ldots\), \(a_k\)-th players in a row. They are kicked out of the game simultaneously. If the \(i\)-th player in a row should be kicked out, but there are fewer than \(i\) players in a row, they are skipped. Once no one is kicked out of the game in some round, all the players that are still in the game are declared as winners.For example, consider the game with \(a=[3, 5]\) and \(n=5\) players. Let the players be named player A, player B, \(\ldots\), player E in the order they are lined up initially. Then, Before the first round, players are lined up as ABCDE. Nene finds the \(3\)-rd and the \(5\)-th players in a row. These are players C and E. They are kicked out in the first round. Now players are lined up as ABD. Nene finds the \(3\)-rd and the \(5\)-th players in a row. The \(3\)-rd player is player D and there is no \(5\)-th player in a row. Thus, only player D is kicked out in the second round. In the third round, no one is kicked out of the game, so the game ends after this round. Players A and B are declared as the winners. Nene has not yet decided how many people would join the game initially. Nene gave you \(q\) integers \(n_1, n_2, \ldots, n_q\) and you should answer the following question for each \(1 \le i \le q\) independently: How many people would be declared as winners if there are \(n_i\) players in the game initially?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 250\)). The description of test cases follows.The first line case contains two integers \(k\) and \(q\) (\(1 \le k, q \le 100\)) β the length of the sequence \(a\) and the number of values \(n_i\) you should solve this problem for.The second line contains \(k\) integers \(a_1,a_2,\ldots,a_k\) (\(1\leq a_1<a_2<\ldots<a_k\leq 100\)) β the sequence \(a\).The third line contains \(q\) integers \(n_1,n_2,\ldots,n_q\) (\(1\leq n_i \leq 100\)).
|
For each test case, output \(q\) integers: the \(i\)-th (\(1\le i \le q\)) of them should be the number of players declared as winners if initially \(n_i\) players join the game.
|
The first test case was explained in the statement.In the second test case, when \(n=1\), the only player stays in the game in the first round. After that, the game ends and the only player is declared as a winner.
|
Input: 62 13 555 32 4 6 7 91 3 55 43 4 5 6 71 2 3 42 369 961 10 1001 1100503 310 20 301 10 100 | Output: 2 1 1 1 1 2 2 2 1 10 68 50 1 9 9
|
Beginner
| 5 | 1,614 | 551 | 178 | 19 |
1,750 |
G
|
1750G
|
G. Doping
| 3,300 |
combinatorics; dp; math
|
We call an array \(a\) of length \(n\) fancy if for each \(1 < i \le n\) it holds that \(a_i = a_{i-1} + 1\).Let's call \(f(p)\) applied to a permutation\(^\dagger\) of length \(n\) as the minimum number of subarrays it can be partitioned such that each one of them is fancy. For example \(f([1,2,3]) = 1\), while \(f([3,1,2]) = 2\) and \(f([3,2,1]) = 3\).Given \(n\) and a permutation \(p\) of length \(n\), we define a permutation \(p'\) of length \(n\) to be \(k\)-special if and only if: \(p'\) is lexicographically smaller\(^\ddagger\) than \(p\), and \(f(p') = k\). Your task is to count for each \(1 \le k \le n\) the number of \(k\)-special permutations modulo \(m\).\(^\dagger\) A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).\(^\ddagger\) A permutation \(a\) of length \(n\) is lexicographically smaller than a permutation \(b\) of length \(n\) if and only if the following holds: in the first position where \(a\) and \(b\) differ, the permutation \(a\) has a smaller element than the corresponding element in \(b\).
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2000\), \(10 \le m \le 10^9\)) β the length of the permutation and the required modulo.The second line contains \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) β the permutation \(p\).
|
Print \(n\) integers, where the \(k\)-th integer is the number of \(k\)-special permutations modulo \(m\).
|
In the first example, the permutations that are lexicographically smaller than \([1,3,4,2]\) are: \([1,2,3,4]\), \(f([1,2,3,4])=1\); \([1,2,4,3]\), \(f([1,2,4,3])=3\); \([1,3,2,4]\), \(f([1,3,2,4])=4\). Thus our answer is \([1,0,1,1]\).In the second example, the permutations that are lexicographically smaller than \([3,2,1]\) are: \([1,2,3]\), \(f([1,2,3])=1\); \([1,3,2]\), \(f([1,3,2])=3\); \([2,1,3]\), \(f([2,1,3])=3\); \([2,3,1]\), \(f([2,3,1])=2\); \([3,1,2]\), \(f([3,1,2])=2\). Thus our answer is \([1,2,2]\).
|
Input: 4 666012 1 3 4 2 | Output: 1 0 1 1
|
Master
| 3 | 1,287 | 276 | 106 | 17 |
626 |
C
|
626C
|
C. Block Towers
| 1,600 |
brute force; greedy; math; number theory
|
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.The students donβt want to use too many blocks, but they also want to be unique, so no two studentsβ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers.
|
The first line of the input contains two space-separated integers n and m (0 β€ n, m β€ 1 000 000, n + m > 0) β the number of students using two-block pieces and the number of students using three-block pieces, respectively.
|
Print a single integer, denoting the minimum possible height of the tallest tower.
|
In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.
|
Input: 1 3 | Output: 9
|
Medium
| 4 | 462 | 222 | 82 | 6 |
1,695 |
A
|
1695A
|
A. Subrectangle Guess
| 800 |
games
|
Michael and Joe are playing a game. The game is played on a grid with \(n\) rows and \(m\) columns, filled with distinct integers. We denote the square on the \(i\)-th (\(1\le i\le n\)) row and \(j\)-th (\(1\le j\le m\)) column by \((i, j)\) and the number there by \(a_{ij}\).Michael starts by saying two numbers \(h\) (\(1\le h \le n\)) and \(w\) (\(1\le w \le m\)). Then Joe picks any \(h\times w\) subrectangle of the board (without Michael seeing).Formally, an \(h\times w\) subrectangle starts at some square \((a,b)\) where \(1 \le a \le n-h+1\) and \(1 \le b \le m-w+1\). It contains all squares \((i,j)\) for \(a \le i \le a+h-1\) and \(b \le j \le b+w-1\). Possible move by Joe if Michael says \(3\times 2\) (with maximum of \(15\)). Finally, Michael has to guess the maximum number in the subrectangle. He wins if he gets it right.Because Michael doesn't like big numbers, he wants the area of the chosen subrectangle (that is, \(h \cdot w\)), to be as small as possible, while still ensuring that he wins, not depending on Joe's choice. Help Michael out by finding this minimum possible area. It can be shown that Michael can always choose \(h, w\) for which he can ensure that he wins.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 20\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 40\)) β the size of the grid.Each of the following \(n\) lines contains \(m\) integers. The \(j\)-th integer on the \(i\)-th line is \(a_{ij}\) (\(-10^9 \le a_{ij} \le 10^9\)) β the element in the cell \((i, j)\).It is guaranteed that all the numbers are distinct (that is, if \(a_{i_1j_1} = a_{i_2j_2}\), then \(i_1 = i_2, j_1 = j_2\)).
|
For each test case print a single positive integer β the minimum possible area the subrectangle can have while still ensuring that Michael can guarantee the victory.
|
In the first test case, the grid is \(1\times 1\), so the only possible choice for \(h, w\) is \(h = 1, w = 1\), giving an area of \(h\cdot w = 1\).The grid from the second test case is drawn in the statement. It can be shown that with \(h = 3, w = 3\) Michael can guarantee the victory and that any choice with \(h\cdot w \le 8\) doesn't.
|
Input: 31 134 42 12 6 103 15 16 41 13 8 1114 7 9 52 3-7 5 20 8 -3 | Output: 1 9 4
|
Beginner
| 1 | 1,198 | 581 | 165 | 16 |
921 |
14
|
92114
|
14. Labyrinth-14
| 3,200 |
See the problem statement here: http://codeforces.com/contest/921/problem/01.
|
Master
| 0 | 77 | 0 | 0 | 9 |
|||||
317 |
B
|
317B
|
B. Ants
| 2,000 |
brute force; implementation
|
It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) β one ant in each direction. No other ant movements will happen. Ants never interfere with each other.Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops.
|
First input line contains integers n (0 β€ n β€ 30000) and t (1 β€ t β€ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 β€ xi, yi β€ 109). Queries may coincide.It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end).
|
Print t integers, one per line β the number of ants at the corresponding junctions when the movement of the ants stops.
|
In the first sample the colony consists of the one ant, so nothing happens at all.In the second sample the colony consists of 6 ants. At the first minute 4 ants scatter from (0, 0) to the neighbouring junctions. After that the process stops.
|
Input: 1 30 10 00 -1 | Output: 010
|
Hard
| 2 | 633 | 430 | 119 | 3 |
1,912 |
L
|
1912L
|
L. LOL Lovers
| 800 |
strings
|
There are \(n\) food items lying in a row on a long table. Each of these items is either a loaf of bread (denoted as a capital Latin letter 'L' with ASCII code 76) or an onion (denoted as a capital Latin letter 'O' with ASCII code 79). There is at least one loaf of bread and at least one onion on the table.You and your friend want to divide the food on the table: you will take a prefix of this row (several leftmost items), and the friend will take the rest. However, there are several restrictions: Each person should have at least one item. The number of your loaves should differ from the number of your friend's loaves. The number of your onions should differ from the number of your friend's onions. Find any correct division and print the number of items you take or report that there is no answer.
|
The first line contains one integer \(n\) (\(2 \le n \le 200\)) β the number of food items on the table. The second line contains a string of length \(n\) consisting of letters 'L' and 'O'. \(i\)-th symbol represents the type of the \(i\)-th food item on the table: 'L' stands for a loaf of bread, and 'O' stands for an onion. It is guaranteed that this string contains at least one letter 'L' and at least one letter 'O'.
|
Print one integer β a number \(k\) such that, if you take \(k\) leftmost items and your friend takes the remaining \(n - k\) items, each of you and your friend get at least one item, your number of loaves is different from your friend's, and your number of onions is different from your friend's. If there are several possible answers, print any of them. If there are no possible answers, print the number \(-1\).
|
In the first example, in any division the left and the right part contain one loaf of bread.In the second example, the division is 'L' and 'O', and in these two strings the number of loaves is different (1 and 0) and the number of onions is different (0 and 1).In the third example, any number 1, 2 or 3 is a correct answer.
|
Input: 3LOL | Output: -1
|
Beginner
| 1 | 807 | 422 | 413 | 19 |
1,223 |
F
|
1223F
|
F. Stack Exterminable Arrays
| 2,600 |
data structures; divide and conquer; dp; hashing
|
Let's look at the following process: initially you have an empty stack and an array \(s\) of the length \(l\). You are trying to push array elements to the stack in the order \(s_1, s_2, s_3, \dots s_{l}\). Moreover, if the stack is empty or the element at the top of this stack is not equal to the current element, then you just push the current element to the top of the stack. Otherwise, you don't push the current element to the stack and, moreover, pop the top element of the stack. If after this process the stack remains empty, the array \(s\) is considered stack exterminable.There are samples of stack exterminable arrays: \([1, 1]\); \([2, 1, 1, 2]\); \([1, 1, 2, 2]\); \([1, 3, 3, 1, 2, 2]\); \([3, 1, 3, 3, 1, 3]\); \([3, 3, 3, 3, 3, 3]\); \([5, 1, 2, 2, 1, 4, 4, 5]\); Let's consider the changing of stack more details if \(s = [5, 1, 2, 2, 1, 4, 4, 5]\) (the top of stack is highlighted). after pushing \(s_1 = 5\) the stack turn into \([\textbf{5}]\); after pushing \(s_2 = 1\) the stack turn into \([5, \textbf{1}]\); after pushing \(s_3 = 2\) the stack turn into \([5, 1, \textbf{2}]\); after pushing \(s_4 = 2\) the stack turn into \([5, \textbf{1}]\); after pushing \(s_5 = 1\) the stack turn into \([\textbf{5}]\); after pushing \(s_6 = 4\) the stack turn into \([5, \textbf{4}]\); after pushing \(s_7 = 4\) the stack turn into \([\textbf{5}]\); after pushing \(s_8 = 5\) the stack is empty. You are given an array \(a_1, a_2, \ldots, a_n\). You have to calculate the number of its subarrays which are stack exterminable.Note, that you have to answer \(q\) independent queries.
|
The first line contains one integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.The first line of each query contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the length of array \(a\).The second line of each query contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the elements.It is guaranteed that the sum of all \(n\) over all queries does not exceed \(3 \cdot 10^5\).
|
For each test case print one integer in single line β the number of stack exterminable subarrays of the array \(a\).
|
In the first query there are four stack exterminable subarrays: \(a_{1 \ldots 4} = [2, 1, 1, 2], a_{2 \ldots 3} = [1, 1], a_{2 \ldots 5} = [1, 1, 2, 2], a_{4 \ldots 5} = [2, 2]\).In the second query, only one subarray is exterminable subarray β \(a_{3 \ldots 4}\).In the third query, there are eight stack exterminable subarrays: \(a_{1 \ldots 8}, a_{2 \ldots 5}, a_{2 \ldots 7}, a_{2 \ldots 9}, a_{3 \ldots 4}, a_{6 \ldots 7}, a_{6 \ldots 9}, a_{8 \ldots 9}\).
|
Input: 3 5 2 1 1 2 2 6 1 2 1 1 3 2 9 3 1 2 2 1 6 6 3 3 | Output: 4 1 8
|
Expert
| 4 | 1,597 | 422 | 116 | 12 |
67 |
C
|
67C
|
C. Sequence of Balls
| 2,600 |
dp
|
You are given a sequence of balls A by your teacher, each labeled with a lowercase Latin letter 'a'-'z'. You don't like the given sequence. You want to change it into a new sequence, B that suits you better. So, you allow yourself four operations: You can insert any ball with any label into the sequence at any position. You can delete (remove) any ball from any position. You can replace any ball with any other ball. You can exchange (swap) two adjacent balls. Your teacher now places time constraints on each operation, meaning that an operation can only be performed in certain time. So, the first operation takes time ti, the second one takes td, the third one takes tr and the fourth one takes te. Also, it is given that 2Β·te β₯ ti + td.Find the minimal time to convert the sequence A to the sequence B.
|
The first line contains four space-separated integers ti, td, tr, te (0 < ti, td, tr, te β€ 100). The following two lines contain sequences A and B on separate lines. The length of each line is between 1 and 4000 characters inclusive.
|
Print a single integer representing minimum time to convert A into B.
|
In the second sample, you could delete the ball labeled 'a' from the first position and then insert another 'a' at the new second position with total time 6. However exchanging the balls give total time 3.
|
Input: 1 1 1 1youshouldnotthoushaltnot | Output: 5
|
Expert
| 1 | 809 | 233 | 69 | 0 |
1,508 |
C
|
1508C
|
C. Complete the MST
| 2,500 |
bitmasks; brute force; data structures; dfs and similar; dsu; graphs; greedy; trees
|
As a teacher, Riko Hakozaki often needs to help her students with problems from various subjects. Today, she is asked a programming task which goes as follows.You are given an undirected complete graph with \(n\) nodes, where some edges are pre-assigned with a positive weight while the rest aren't. You need to assign all unassigned edges with non-negative weights so that in the resulting fully-assigned complete graph the XOR sum of all weights would be equal to \(0\).Define the ugliness of a fully-assigned complete graph the weight of its minimum spanning tree, where the weight of a spanning tree equals the sum of weights of its edges. You need to assign the weights so that the ugliness of the resulting graph is as small as possible.As a reminder, an undirected complete graph with \(n\) nodes contains all edges \((u, v)\) with \(1 \le u < v \le n\); such a graph has \(\frac{n(n-1)}{2}\) edges.She is not sure how to solve this problem, so she asks you to solve it for her.
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(0 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2} - 1)\)) β the number of nodes and the number of pre-assigned edges. The inputs are given so that there is at least one unassigned edge.The \(i\)-th of the following \(m\) lines contains three integers \(u_i\), \(v_i\), and \(w_i\) (\(1 \le u_i, v_i \le n\), \(u \ne v\), \(1 \le w_i < 2^{30}\)), representing the edge from \(u_i\) to \(v_i\) has been pre-assigned with the weight \(w_i\). No edge appears in the input more than once.
|
Print on one line one integer β the minimum ugliness among all weight assignments with XOR sum equal to \(0\).
|
The following image showcases the first test case. The black weights are pre-assigned from the statement, the red weights are assigned by us, and the minimum spanning tree is denoted by the blue edges.
|
Input: 4 4 2 1 14 1 4 14 3 2 15 4 3 8 | Output: 15
|
Expert
| 8 | 985 | 567 | 110 | 15 |
938 |
C
|
938C
|
C. Constructing Tests
| 1,700 |
binary search; brute force; constructive algorithms
|
Let's denote a m-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size m Γ m of this matrix contains at least one zero. Consider the following problem:You are given two integers n and m. You have to construct an m-free square matrix of size n Γ n such that the number of 1's in this matrix is maximum possible. Print the maximum possible number of 1's in such matrix.You don't have to solve this problem. Instead, you have to construct a few tests for it.You will be given t numbers x1, x2, ..., xt. For every , find two integers ni and mi (ni β₯ mi) such that the answer for the aforementioned problem is exactly xi if we set n = ni and m = mi.
|
The first line contains one integer t (1 β€ t β€ 100) β the number of tests you have to construct.Then t lines follow, i-th line containing one integer xi (0 β€ xi β€ 109).Note that in hacks you have to set t = 1.
|
For each test you have to construct, output two positive numbers ni and mi (1 β€ mi β€ ni β€ 109) such that the maximum number of 1's in a mi-free ni Γ ni matrix is exactly xi. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer - 1.
|
Input: 32101 | Output: 5 21 1-1
|
Medium
| 3 | 704 | 209 | 310 | 9 |
|
484 |
D
|
484D
|
D. Kindergarten
| 2,400 |
data structures; dp; greedy
|
In a kindergarten, the children are being divided into groups. The teacher put the children in a line and associated each child with his or her integer charisma value. Each child should go to exactly one group. Each group should be a nonempty segment of consecutive children of a line. A group's sociability is the maximum difference of charisma of two children in the group (in particular, if the group consists of one child, its sociability equals a zero). The teacher wants to divide the children into some number of groups in such way that the total sociability of the groups is maximum. Help him find this value.
|
The first line contains integer n β the number of children in the line (1 β€ n β€ 106).The second line contains n integers ai β the charisma of the i-th child ( - 109 β€ ai β€ 109).
|
Print the maximum possible total sociability of all groups.
|
In the first test sample one of the possible variants of an division is following: the first three children form a group with sociability 2, and the two remaining children form a group with sociability 1.In the second test sample any division leads to the same result, the sociability will be equal to 0 in each group.
|
Input: 51 2 3 1 2 | Output: 3
|
Expert
| 3 | 617 | 177 | 59 | 4 |
1,515 |
F
|
1515F
|
F. Phoenix and Earthquake
| 2,600 |
constructive algorithms; dfs and similar; dsu; graphs; greedy; trees
|
Phoenix's homeland, the Fire Nation had \(n\) cities that were connected by \(m\) roads, but the roads were all destroyed by an earthquake. The Fire Nation wishes to repair \(n-1\) of these roads so that all the cities are connected again. The \(i\)-th city has \(a_i\) tons of asphalt. \(x\) tons of asphalt are used up when repairing a road, and to repair a road between \(i\) and \(j\), cities \(i\) and \(j\) must have at least \(x\) tons of asphalt between them. In other words, if city \(i\) had \(a_i\) tons of asphalt and city \(j\) had \(a_j\) tons, there would remain \(a_i+a_j-x\) tons after repairing the road between them. Asphalt can be moved between cities if the road between them is already repaired.Please determine if it is possible to connect all the cities, and if so, output any sequence of roads to repair.
|
The first line contains integers \(n\), \(m\), and \(x\) (\(2 \le n \le 3 \cdot 10^5\); \(n-1 \le m \le 3 \cdot 10^5\); \(1 \le x \le 10^9\)) β the number of cities, number of roads, and amount of asphalt needed to repair one road.The next line contains \(n\) space-separated integer \(a_i\) (\(0 \le a_i \le 10^9\)) β the amount of asphalt initially at city \(i\).The next \(m\) lines contains two integers \(x_i\) and \(y_i\) (\(x_i\ne y_i\); \(1 \le x_i, y_i \le n\)) β the cities connected by the \(i\)-th road. It is guaranteed that there is at most one road between each pair of cities, and that the city was originally connected before the earthquake.
|
If it is not possible to connect all the cities, print NO. Otherwise, print YES followed by \(n-1\) integers \(e_1, e_2, \dots, e_{n-1}\), the order in which the roads should be repaired. \(e_i\) is the index of the \(i\)-th road to repair. If there are multiple solutions, print any.
|
In the first example, the roads are repaired in the following order: Road \(3\) is repaired, connecting cities \(3\) and \(4\). City \(4\) originally had \(4\) tons of asphalt. After this road is constructed, \(3\) tons remain. Road \(2\) is repaired, connecting cities \(2\) and \(3\). The asphalt from city \(4\) can be transported to city \(3\) and used for the road. \(2\) tons remain. Road \(1\) is repaired, connecting cities \(1\) and \(2\). The asphalt is transported to city \(2\) and used for the road. \(1\) ton remain. Road \(4\) is repaired, connecting cities \(4\) and \(5\). The asphalt is transported to city \(4\) and used for the road. No asphalt remains. All the cities are now connected.In the second example, cities \(1\) and \(2\) use all their asphalt together to build the road. They each have \(1\) ton, so together they have \(2\) tons, which is enough.In the third example, there isn't enough asphalt to connect cities \(1\) and \(2\).
|
Input: 5 4 1 0 0 0 4 0 1 2 2 3 3 4 4 5 | Output: YES 3 2 1 4
|
Expert
| 6 | 829 | 658 | 284 | 15 |
1,506 |
F
|
1506F
|
F. Triangular Paths
| 2,000 |
constructive algorithms; graphs; math; shortest paths; sortings
|
Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The \(k\)-th layer of the triangle contains \(k\) points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers \((r, c)\) (\(1 \le c \le r\)), where \(r\) is the number of the layer, and \(c\) is the number of the point in the layer. From each point \((r, c)\) there are two directed edges to the points \((r+1, c)\) and \((r+1, c+1)\), but only one of the edges is activated. If \(r + c\) is even, then the edge to the point \((r+1, c)\) is activated, otherwise the edge to the point \((r+1, c+1)\) is activated. Look at the picture for a better understanding. Activated edges are colored in black. Non-activated edges are colored in gray. From the point \((r_1, c_1)\) it is possible to reach the point \((r_2, c_2)\), if there is a path between them only from activated edges. For example, in the picture above, there is a path from \((1, 1)\) to \((3, 2)\), but there is no path from \((2, 1)\) to \((1, 1)\).Initially, you are at the point \((1, 1)\). For each turn, you can: Replace activated edge for point \((r, c)\). That is if the edge to the point \((r+1, c)\) is activated, then instead of it, the edge to the point \((r+1, c+1)\) becomes activated, otherwise if the edge to the point \((r+1, c+1)\), then instead if it, the edge to the point \((r+1, c)\) becomes activated. This action increases the cost of the path by \(1\); Move from the current point to another by following the activated edge. This action does not increase the cost of the path. You are given a sequence of \(n\) points of an infinite triangle \((r_1, c_1), (r_2, c_2), \ldots, (r_n, c_n)\). Find the minimum cost path from \((1, 1)\), passing through all \(n\) points in arbitrary order.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) is the number of test cases. Then \(t\) test cases follow.Each test case begins with a line containing one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) is the number of points to visit.The second line contains \(n\) numbers \(r_1, r_2, \ldots, r_n\) (\(1 \le r_i \le 10^9\)), where \(r_i\) is the number of the layer in which \(i\)-th point is located.The third line contains \(n\) numbers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le r_i\)), where \(c_i\) is the number of the \(i\)-th point in the \(r_i\) layer.It is guaranteed that all \(n\) points are distinct.It is guaranteed that there is always at least one way to traverse all \(n\) points.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the minimum cost of a path passing through all points in the corresponding test case.
|
Input: 4 3 1 4 2 1 3 1 2 2 4 2 3 2 1 1000000000 1 1000000000 4 3 10 5 8 2 5 2 4 | Output: 0 1 999999999 2
|
Hard
| 5 | 1,862 | 805 | 112 | 15 |
|
1,355 |
A
|
1355A
|
A. Sequence with Digits
| 1,200 |
brute force; implementation; math
|
Let's define the following recurrence: $$$\(a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n}).\)\(Here \)minDigit(x)\( and \)maxDigit(x)\( are the minimal and maximal digits in the decimal representation of \)x\( without leading zeroes. For examples refer to notes.Your task is calculate \)a_{K}\( for given \)a_{1}\( and \)K$$$.
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of independent test cases.Each test case consists of a single line containing two integers \(a_{1}\) and \(K\) (\(1 \le a_{1} \le 10^{18}\), \(1 \le K \le 10^{16}\)) separated by a space.
|
For each test case print one integer \(a_{K}\) on a separate line.
|
\(a_{1} = 487\) \(a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519\) \(a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528\) \(a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544\) \(a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564\) \(a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588\) \(a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628\)
|
Input: 8 1 4 487 1 487 2 487 3 487 4 487 5 487 6 487 7 | Output: 42 487 519 528 544 564 588 628
|
Easy
| 3 | 335 | 265 | 66 | 13 |
237 |
E
|
237E
|
E. Build String
| 2,000 |
flows; graphs
|
You desperately need to build some string t. For that you've got n more strings s1, s2, ..., sn. To build string t, you are allowed to perform exactly |t| (|t| is the length of string t) operations on these strings. Each operation looks like that: choose any non-empty string from strings s1, s2, ..., sn; choose an arbitrary character from the chosen string and write it on a piece of paper; remove the chosen character from the chosen string. Note that after you perform the described operation, the total number of characters in strings s1, s2, ..., sn decreases by 1. We are assumed to build string t, if the characters, written on the piece of paper, in the order of performed operations form string t.There are other limitations, though. For each string si you know number ai β the maximum number of characters you are allowed to delete from string si. You also know that each operation that results in deleting a character from string si, costs i rubles. That is, an operation on string s1 is the cheapest (it costs 1 ruble), and the operation on string sn is the most expensive one (it costs n rubles).Your task is to count the minimum amount of money (in rubles) you will need to build string t by the given rules. Consider the cost of building string t to be the sum of prices of the operations you use.
|
The first line of the input contains string t β the string that you need to build.The second line contains a single integer n (1 β€ n β€ 100) β the number of strings to which you are allowed to apply the described operation. Each of the next n lines contains a string and an integer. The i-th line contains space-separated string si and integer ai (0 β€ ai β€ 100). Number ai represents the maximum number of characters that can be deleted from string si.All strings in the input only consist of lowercase English letters. All strings are non-empty. The lengths of all strings do not exceed 100 characters.
|
Print a single number β the minimum money (in rubles) you need in order to build string t. If there is no solution, print -1.
|
Notes to the samples:In the first sample from the first string you should take characters ""b"" and ""z"" with price 1 ruble, from the second string characters ""a"", ""e"" ΠΈ ""b"" with price 2 rubles. The price of the string t in this case is 2Β·1 + 3Β·2 = 8.In the second sample from the first string you should take two characters ""a"" with price 1 ruble, from the second string character ""c"" with price 2 rubles, from the third string two characters ""a"" with price 3 rubles, from the fourth string two characters ""b"" with price 4 rubles. The price of the string t in this case is 2Β·1 + 1Β·2 + 2Β·3 + 2Β·4 = 18.In the third sample the solution doesn't exist because there is no character ""y"" in given strings.
|
Input: bbaze3bzb 2aeb 3ba 10 | Output: 8
|
Hard
| 2 | 1,313 | 602 | 125 | 2 |
261 |
D
|
261D
|
D. Maxim and Increasing Subsequence
| 2,600 |
dp
|
Maxim loves sequences, especially those that strictly increase. He is wondering, what is the length of the longest increasing subsequence of the given sequence a?Sequence a is given as follows: the length of the sequence equals n Γ t; (1 β€ i β€ n Γ t), where operation means taking the remainder after dividing number x by number y. Sequence s1, s2, ..., sr of length r is a subsequence of sequence a1, a2, ..., an, if there is such increasing sequence of indexes i1, i2, ..., ir (1 β€ i1 < i2 < ... < ir β€ n), that aij = sj. In other words, the subsequence can be obtained from the sequence by crossing out some elements.Sequence s1, s2, ..., sr is increasing, if the following inequality holds: s1 < s2 < ... < sr.Maxim have k variants of the sequence a. Help Maxim to determine for each sequence the length of the longest increasing subsequence.
|
The first line contains four integers k, n, maxb and t (1 β€ k β€ 10; 1 β€ n, maxb β€ 105; 1 β€ t β€ 109; n Γ maxb β€ 2Β·107). Each of the next k lines contain n integers b1, b2, ..., bn (1 β€ bi β€ maxb). Note that for each variant of the sequence a the values n, maxb and t coincide, the only arrays bs differ.The numbers in the lines are separated by single spaces.
|
Print k integers β the answers for the variants of the sequence a. Print the answers in the order the variants follow in the input.
|
Input: 3 3 5 23 2 11 2 32 3 1 | Output: 233
|
Expert
| 1 | 846 | 358 | 131 | 2 |
|
1,609 |
D
|
1609D
|
D. Social Network
| 1,600 |
dsu; graphs; greedy; implementation; trees
|
William arrived at a conference dedicated to cryptocurrencies. Networking, meeting new people, and using friends' connections are essential to stay up to date with the latest news from the world of cryptocurrencies.The conference has \(n\) participants, who are initially unfamiliar with each other. William can introduce any two people, \(a\) and \(b\), who were not familiar before, to each other. William has \(d\) conditions, \(i\)'th of which requires person \(x_i\) to have a connection to person \(y_i\). Formally, two people \(x\) and \(y\) have a connection if there is such a chain \(p_1=x, p_2, p_3, \dots, p_k=y\) for which for all \(i\) from \(1\) to \(k - 1\) it's true that two people with numbers \(p_i\) and \(p_{i + 1}\) know each other.For every \(i\) (\(1 \le i \le d\)) William wants you to calculate the maximal number of acquaintances one person can have, assuming that William satisfied all conditions from \(1\) and up to and including \(i\) and performed exactly \(i\) introductions. The conditions are being checked after William performed \(i\) introductions. The answer for each \(i\) must be calculated independently. It means that when you compute an answer for \(i\), you should assume that no two people have been introduced to each other yet.
|
The first line contains two integers \(n\) and \(d\) (\(2 \le n \le 10^3, 1 \le d \le n - 1\)), the number of people, and number of conditions, respectively.Each of the next \(d\) lines each contain two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n, x_i \neq y_i\)), the numbers of people which must have a connection according to condition \(i\).
|
Output \(d\) integers. \(i\)th number must equal the number of acquaintances the person with the maximal possible acquaintances will have, if William performed \(i\) introductions and satisfied the first \(i\) conditions.
|
The explanation for the first test case:In this explanation, the circles and the numbers in them denote a person with the corresponding number. The line denotes that William introduced two connected people. The person marked with red has the most acquaintances. These are not the only correct ways to introduce people.
|
Input: 7 6 1 2 3 4 2 4 7 6 6 5 1 7 | Output: 1 1 3 3 3 6
|
Medium
| 5 | 1,276 | 354 | 221 | 16 |
1,341 |
A
|
1341A
|
A. Nastya and Rice
| 900 |
math
|
Nastya just made a huge mistake and dropped a whole package of rice on the floor. Mom will come soon. If she sees this, then Nastya will be punished.In total, Nastya dropped \(n\) grains. Nastya read that each grain weighs some integer number of grams from \(a - b\) to \(a + b\), inclusive (numbers \(a\) and \(b\) are known), and the whole package of \(n\) grains weighs from \(c - d\) to \(c + d\) grams, inclusive (numbers \(c\) and \(d\) are known). The weight of the package is the sum of the weights of all \(n\) grains in it.Help Nastya understand if this information can be correct. In other words, check whether each grain can have such a mass that the \(i\)-th grain weighs some integer number \(x_i\) \((a - b \leq x_i \leq a + b)\), and in total they weigh from \(c - d\) to \(c + d\), inclusive (\(c - d \leq \sum\limits_{i=1}^{n}{x_i} \leq c + d\)).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) \((1 \leq t \leq 1000)\) β the number of test cases. The next \(t\) lines contain descriptions of the test cases, each line contains \(5\) integers: \(n\) \((1 \leq n \leq 1000)\) β the number of grains that Nastya counted and \(a, b, c, d\) \((0 \leq b < a \leq 1000, 0 \leq d < c \leq 1000)\) β numbers that determine the possible weight of one grain of rice (from \(a - b\) to \(a + b\)) and the possible total weight of the package (from \(c - d\) to \(c + d\)).
|
For each test case given in the input print ""Yes"", if the information about the weights is not inconsistent, and print ""No"" if \(n\) grains with masses from \(a - b\) to \(a + b\) cannot make a package with a total mass from \(c - d\) to \(c + d\).
|
In the first test case of the example, we can assume that each grain weighs \(17\) grams, and a pack \(119\) grams, then really Nastya could collect the whole pack.In the third test case of the example, we can assume that each grain weighs \(16\) grams, and a pack \(128\) grams, then really Nastya could collect the whole pack.In the fifth test case of the example, we can be assumed that \(3\) grains of rice weigh \(2\), \(2\), and \(3\) grams, and a pack is \(7\) grams, then really Nastya could collect the whole pack.In the second and fourth test cases of the example, we can prove that it is impossible to determine the correct weight of all grains of rice and the weight of the pack so that the weight of the pack is equal to the total weight of all collected grains.
|
Input: 5 7 20 3 101 18 11 11 10 234 2 8 9 7 250 122 19 41 21 321 10 3 10 8 6 1 | Output: Yes No Yes No Yes
|
Beginner
| 1 | 864 | 556 | 252 | 13 |
687 |
C
|
687C
|
C. The Values You Can Make
| 1,900 |
dp
|
Pari wants to buy an expensive chocolate from Arya. She has n coins, the value of the i-th coin is ci. The price of the chocolate is k, so Pari will take a subset of her coins with sum equal to k and give it to Arya.Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values x, such that Arya will be able to make x using some subset of coins with the sum k.Formally, Pari wants to know the values x such that there exists a subset of coins with the sum k such that some subset of this subset has the sum x, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum x using these coins.
|
The first line contains two integers n and k (1 β€ n, k β€ 500) β the number of coins and the price of the chocolate, respectively.Next line will contain n integers c1, c2, ..., cn (1 β€ ci β€ 500) β the values of Pari's coins.It's guaranteed that one can make value k using these coins.
|
First line of the output must contain a single integer qβ the number of suitable values x. Then print q integers in ascending order β the values that Arya can make for some subset of coins of Pari that pays for the chocolate.
|
Input: 6 185 6 1 10 12 2 | Output: 160 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18
|
Hard
| 1 | 796 | 283 | 225 | 6 |
|
1,634 |
E
|
1634E
|
E. Fair Share
| 2,400 |
constructive algorithms; data structures; dfs and similar; graph matchings; graphs
|
Even a cat has things it can do that AI cannot.β Fei-Fei LiYou are given \(m\) arrays of positive integers. Each array is of even length.You need to split all these integers into two equal multisets \(L\) and \(R\), that is, each element of each array should go into one of two multisets (but not both). Additionally, for each of the \(m\) arrays, exactly half of its elements should go into \(L\), and the rest should go into \(R\).Give an example of such a division or determine that no such division exists.
|
The first line contains an integer \(m\) (\(1 \le m \le 10 ^ 5\)) β the number of arrays.The next \(2 \cdot m\) lines contain descriptions of the arrays.For each array, the first line contains an even integer \(n\) (\(2 \le n \le 2 \cdot 10 ^ 5\)) β the length of the array. The second line consists of \(n\) space-separated integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10 ^ 9\)) β array elements.It is guaranteed that the sum of \(n\) over all arrays does not exceed \(2 \cdot 10^5\).
|
If the answer exists, print ""YES"", and then print \(m\) lines.On each line, for each element, print the letter ""L"" or ""R"" (capitalized, without spaces), depending on which multiset the element should go into.If there is no answer, print ""NO"" on the only line.
|
In the first array, we add the first element to \(R\) and the second to \(L\). Now \(L = \{2\}\), and \(R = \{1\}\).In the second array, we add the first and third elements to \(L\) and the rest to \(R\). Now \(L = \{1, 2, 3\}\) and \(R = \{1, 2, 3\}\).In the third array, we add elements 2, 3, and 6 to \(L\), and others β to \(R\). As a result, \(L = R = \{1, 1, 2, 2, 3, 3\}\).
|
Input: 3 2 1 2 4 1 2 3 3 6 1 1 2 2 3 3 | Output: YES RL LRLR RLLRRL
|
Expert
| 5 | 510 | 491 | 267 | 16 |
1,260 |
E
|
1260E
|
E. Tournament
| 2,400 |
brute force; dp; greedy
|
You are organizing a boxing tournament, where \(n\) boxers will participate (\(n\) is a power of \(2\)), and your friend is one of them. All boxers have different strength from \(1\) to \(n\), and boxer \(i\) wins in the match against boxer \(j\) if and only if \(i\) is stronger than \(j\).The tournament will be organized as follows: \(n\) boxers will be divided into pairs; the loser in each pair leaves the tournament, and \(\frac{n}{2}\) winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner).Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower.Furthermore, during each stage you distribute the boxers into pairs as you wish.The boxer with strength \(i\) can be bribed if you pay him \(a_i\) dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish?
|
The first line contains one integer \(n\) (\(2 \le n \le 2^{18}\)) β the number of boxers. \(n\) is a power of \(2\).The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\), where \(a_i\) is the number of dollars you have to pay if you want to bribe the boxer with strength \(i\). Exactly one of \(a_i\) is equal to \(-1\) β it means that the boxer with strength \(i\) is your friend. All other values are in the range \([1, 10^9]\).
|
Print one integer β the minimum number of dollars you have to pay so your friend wins.
|
In the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.In the second test case you can distribute boxers as follows (your friend is number \(2\)):\(1 : 2, 8 : 5, 7 : 3, 6 : 4\) (boxers \(2, 8, 7\) and \(6\) advance to the next stage);\(2 : 6, 8 : 7\) (boxers \(2\) and \(8\) advance to the next stage, you have to bribe the boxer with strength \(6\));\(2 : 8\) (you have to bribe the boxer with strength \(8\));
|
Input: 4 3 9 1 -1 | Output: 0
|
Expert
| 3 | 1,234 | 448 | 86 | 12 |
218 |
B
|
218B
|
B. Airport
| 1,100 |
implementation
|
Lolek and Bolek are about to travel abroad by plane. The local airport has a special ""Choose Your Plane"" offer. The offer's conditions are as follows: it is up to a passenger to choose a plane to fly on; if the chosen plane has x (x > 0) empty seats at the given moment, then the ticket for such a plane costs x zlotys (units of Polish currency). The only ticket office of the airport already has a queue of n passengers in front of it. Lolek and Bolek have not stood in the queue yet, but they are already wondering what is the maximum and the minimum number of zlotys the airport administration can earn if all n passengers buy tickets according to the conditions of this offer?The passengers buy tickets in turn, the first person in the queue goes first, then goes the second one, and so on up to n-th person.
|
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the number of passengers in the queue and the number of planes in the airport, correspondingly. The next line contains m integers a1, a2, ..., am (1 β€ ai β€ 1000) β ai stands for the number of empty seats in the i-th plane before the ticket office starts selling tickets.The numbers in the lines are separated by a space. It is guaranteed that there are at least n empty seats in total.
|
Print two integers β the maximum and the minimum number of zlotys that the airport administration can earn, correspondingly.
|
In the first test sample the number of passengers is equal to the number of empty seats, so regardless of the way the planes are chosen, the administration will earn the same sum.In the second sample the sum is maximized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person β to the 2-nd plane, the 3-rd person β to the 3-rd plane, the 4-th person β to the 1-st plane. The sum is minimized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person β to the 1-st plane, the 3-rd person β to the 2-nd plane, the 4-th person β to the 2-nd plane.
|
Input: 4 32 1 1 | Output: 5 5
|
Easy
| 1 | 814 | 450 | 124 | 2 |
1,983 |
G
|
1983G
|
G. Your Loss
| 3,000 |
bitmasks; brute force; dp; trees
|
You are given a tree with \(n\) nodes numbered from \(1\) to \(n\), along with an array of size \(n\). The value of \(i\)-th node is \(a_{i}\). There are \(q\) queries. In each query, you are given 2 nodes numbered as \(x\) and \(y\). Consider the path from the node numbered as \(x\) to the node numbered as \(y\). Let the path be represented by \(x = p_0, p_1, p_2, \ldots, p_r = y\), where \(p_i\) are the intermediate nodes. Compute the sum of \(a_{p_i}\oplus i\) for each \(i\) such that \(0 \le i \le r\) where \(\oplus\) is the XOR operator. More formally, compute $$$\(\sum_{i =0}^{r} a_{p_i}\oplus i\)$$$.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Each test case contains several sets of input data.The first line of each set of input data contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β the number of nodes.The next \(n-1\) lines of each set of input data contain \(2\) integers, \(u\) and \(v\) representing an edge between the node numbered \(u\) and the node numbered \(v\). It is guaranteed that \(u \ne v\) and that the edges form a tree. The next line of each set of input data contains \(n\) integers, \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 5 \cdot 10^5\)) β values of the nodes.The next line contains a single integer \(q\) (\(1 \le q \le 10^5\)) β the number of queries.The next \(q\) lines describe the queries. The \(i\)-th query contains \(2\) integers \(x\) and \(y\) (\(1 \le x,y \le n\)) denoting the starting and the ending node of the path. It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\) and sum of \(q\) over all test cases does not exceed \(10^5\).
|
For each query, output a single number β the sum from the problem statement.
|
Input: 141 22 33 42 3 6 531 43 41 1 | Output: 14 10 2
|
Master
| 4 | 614 | 1,084 | 76 | 19 |
|
1,534 |
F2
|
1534F2
|
F2. Falling Sand (Hard Version)
| 3,000 |
dfs and similar; dp; graphs; greedy
|
This is the hard version of the problem. The difference between the versions is the constraints on \(a_i\). You can make hacks only if all versions of the problem are solved.Little Dormi has recently received a puzzle from his friend and needs your help to solve it. The puzzle consists of an upright board with \(n\) rows and \(m\) columns of cells, some empty and some filled with blocks of sand, and \(m\) non-negative integers \(a_1,a_2,\ldots,a_m\) (\(0 \leq a_i \leq n\)). In this version of the problem, \(a_i\) will always be not greater than the number of blocks of sand in column \(i\).When a cell filled with a block of sand is disturbed, the block of sand will fall from its cell to the sand counter at the bottom of the column (each column has a sand counter). While a block of sand is falling, other blocks of sand that are adjacent at any point to the falling block of sand will also be disturbed and start to fall. Specifically, a block of sand disturbed at a cell \((i,j)\) will pass through all cells below and including the cell \((i,j)\) within the column, disturbing all adjacent cells along the way. Here, the cells adjacent to a cell \((i,j)\) are defined as \((i-1,j)\), \((i,j-1)\), \((i+1,j)\), and \((i,j+1)\) (if they are within the grid). Note that the newly falling blocks can disturb other blocks.In one operation you are able to disturb any piece of sand. The puzzle is solved when there are at least \(a_i\) blocks of sand counted in the \(i\)-th sand counter for each column from \(1\) to \(m\).You are now tasked with finding the minimum amount of operations in order to solve the puzzle. Note that Little Dormi will never give you a puzzle that is impossible to solve.
|
The first line consists of two space-separated positive integers \(n\) and \(m\) (\(1 \leq n \cdot m \leq 400\,000\)).Each of the next \(n\) lines contains \(m\) characters, describing each row of the board. If a character on a line is '.', the corresponding cell is empty. If it is '#', the cell contains a block of sand.The final line contains \(m\) non-negative integers \(a_1,a_2,\ldots,a_m\) (\(0 \leq a_i \leq n\)) β the minimum amount of blocks of sand that needs to fall below the board in each column. In this version of the problem, \(a_i\) will always be not greater than the number of blocks of sand in column \(i\).
|
Print one non-negative integer, the minimum amount of operations needed to solve the puzzle.
|
For example \(1\), by disturbing both blocks of sand on the first row from the top at the first and sixth columns from the left, and the block of sand on the second row from the top and the fourth column from the left, it is possible to have all the required amounts of sand fall in each column. It can be proved that this is not possible with fewer than \(3\) operations, and as such the answer is \(3\). Here is the puzzle from the first example. For example \(2\), by disturbing the cell on the top row and rightmost column, one can cause all of the blocks of sand in the board to fall into the counters at the bottom. Thus, the answer is \(1\). Here is the puzzle from the second example. For example \(3\), by disturbing the cell on the top row and rightmost column, it is possible to have all the required amounts of sand fall in each column. It can be proved that this is not possible with fewer than \(1\) operation, and as such the answer is \(1\). Here is the puzzle from the third example.
|
Input: 5 7 #....#. .#.#... #....#. #....## #.#.... 4 1 1 1 0 3 1 | Output: 3
|
Master
| 4 | 1,704 | 628 | 92 | 15 |
1,245 |
A
|
1245A
|
A. Good ol' Numbers Coloring
| 1,000 |
math; number theory
|
Consider the set of all nonnegative integers: \({0, 1, 2, \dots}\). Given two integers \(a\) and \(b\) (\(1 \le a, b \le 10^4\)). We paint all the numbers in increasing number first we paint \(0\), then we paint \(1\), then \(2\) and so on.Each number is painted white or black. We paint a number \(i\) according to the following rules: if \(i = 0\), it is colored white; if \(i \ge a\) and \(i - a\) is colored white, \(i\) is also colored white; if \(i \ge b\) and \(i - b\) is colored white, \(i\) is also colored white; if \(i\) is still not colored white, it is colored black. In this way, each nonnegative integer gets one of two colors.For example, if \(a=3\), \(b=5\), then the colors of the numbers (in the order from \(0\)) are: white (\(0\)), black (\(1\)), black (\(2\)), white (\(3\)), black (\(4\)), white (\(5\)), white (\(6\)), black (\(7\)), white (\(8\)), white (\(9\)), ...Note that: It is possible that there are infinitely many nonnegative integers colored black. For example, if \(a = 10\) and \(b = 10\), then only \(0, 10, 20, 30\) and any other nonnegative integers that end in \(0\) when written in base 10 are white. The other integers are colored black. It is also possible that there are only finitely many nonnegative integers colored black. For example, when \(a = 1\) and \(b = 10\), then there is no nonnegative integer colored black at all. Your task is to determine whether or not the number of nonnegative integers colored black is infinite.If there are infinitely many nonnegative integers colored black, simply print a line containing ""Infinite"" (without the quotes). Otherwise, print ""Finite"" (without the quotes).
|
The first line of input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases in the input. Then \(t\) lines follow, each line contains two space-separated integers \(a\) and \(b\) (\(1 \le a, b \le 10^4\)).
|
For each test case, print one line containing either ""Infinite"" or ""Finite"" (without the quotes). Output is case-insensitive (i.e. ""infinite"", ""inFiNite"" or ""finiTE"" are all valid answers).
|
Input: 4 10 10 1 10 6 9 7 3 | Output: Infinite Finite Infinite Finite
|
Beginner
| 2 | 1,657 | 234 | 199 | 12 |
|
1,666 |
L
|
1666L
|
L. Labyrinth
| 1,800 |
dfs and similar; graphs
|
Leslie and Leon entered a labyrinth. The labyrinth consists of \(n\) halls and \(m\) one-way passages between them. The halls are numbered from \(1\) to \(n\).Leslie and Leon start their journey in the hall \(s\). Right away, they quarrel and decide to explore the labyrinth separately. However, they want to meet again at the end of their journey.To help Leslie and Leon, your task is to find two different paths from the given hall \(s\) to some other hall \(t\), such that these two paths do not share halls other than the staring hall \(s\) and the ending hall \(t\). The hall \(t\) has not been determined yet, so you can choose any of the labyrinth's halls as \(t\) except \(s\).Leslie's and Leon's paths do not have to be the shortest ones, but their paths must be simple, visiting any hall at most once. Also, they cannot visit any common halls except \(s\) and \(t\) during their journey, even at different times.
|
The first line contains three integers \(n\), \(m\), and \(s\), where \(n\) (\(2 \le n \le 2 \cdot 10^5\)) is the number of vertices, \(m\) (\(0 \le m \le 2 \cdot 10^5\)) is the number of edges in the labyrinth, and \(s\) (\(1 \le s \le n\)) is the starting hall.Then \(m\) lines with descriptions of passages follow. Each description contains two integers \(u_i\), \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \neq v_i\)), denoting a passage from the hall \(u_i\) to the hall \(v_i\). The passages are one-way. Each tuple \((u_i, v_i)\) is present in the input at most once. The labyrinth can contain cycles and is not necessarily connected in any way.
|
If it is possible to find the desired two paths, output ""Possible"", otherwise output ""Impossible"".If the answer exists, output two path descriptions. Each description occupies two lines. The first line of the description contains an integer \(h\) (\(2 \le h \le n\)) β the number of halls in a path, and the second line contains distinct integers \(w_1, w_2, \dots, w_h\) (\(w_1 = s\); \(1 \le w_j \le n\); \(w_h = t\)) β the halls in the path in the order of passing. Both paths must end at the same vertex \(t\). The paths must be different, and all intermediate halls in these paths must be distinct.
|
Input: 5 5 1 1 2 2 3 1 4 4 3 3 5 | Output: Possible 3 1 2 3 3 1 4 3
|
Medium
| 2 | 922 | 649 | 607 | 16 |
|
331 |
B2
|
331B2
|
B2. Shave Beaver!
| 1,900 |
data structures
|
The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, ""Beavershave 5000"". Beavershave 5000 can shave beavers by families! How does it work? Very easily!There are n beavers, each of them has a unique id from 1 to n. Consider a permutation a1, a2, ..., an of n these beavers. Beavershave 5000 needs one session to shave beavers with ids from x to y (inclusive) if and only if there are such indices i1 < i2 < ... < ik, that ai1 = x, ai2 = x + 1, ..., aik - 1 = y - 1, aik = y. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1, 2, 3, ..., n.If we can't shave beavers from x to y in one session, then we can split these beavers into groups [x, p1], [p1 + 1, p2], ..., [pm + 1, y] (x β€ p1 < p2 < ... < pm < y), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs m + 1 working sessions to shave beavers from x to y.All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types: what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from x to y, inclusive? two beavers on positions x and y (the beavers ax and ay) swapped. You can assume that any beaver can be shaved any number of times.
|
The first line contains integer n β the total number of beavers, 2 β€ n. The second line contains n space-separated integers β the initial beaver permutation.The third line contains integer q β the number of queries, 1 β€ q β€ 105. The next q lines contain the queries. Each query i looks as pi xi yi, where pi is the query type (1 is to shave beavers from xi to yi, inclusive, 2 is to swap beavers on positions xi and yi). All queries meet the condition: 1 β€ xi < yi β€ n. to get 30 points, you need to solve the problem with constraints: n β€ 100 (subproblem B1); to get 100 points, you need to solve the problem with constraints: n β€ 3Β·105 (subproblems B1+B2). Note that the number of queries q is limited 1 β€ q β€ 105 in both subproblem B1 and subproblem B2.
|
For each query with pi = 1, print the minimum number of Beavershave 5000 sessions.
|
Input: 51 3 4 2 561 1 51 3 42 2 31 1 52 1 51 1 5 | Output: 2135
|
Hard
| 1 | 1,375 | 756 | 82 | 3 |
|
1,512 |
E
|
1512E
|
E. Permutation by Sum
| 1,600 |
brute force; greedy; math
|
A permutation is a sequence of \(n\) integers from \(1\) to \(n\), in which all the numbers occur exactly once. For example, \([1]\), \([3, 5, 2, 1, 4]\), \([1, 3, 2]\) are permutations, and \([2, 3, 2]\), \([4, 3, 1]\), \([0]\) are not.Polycarp was given four integers \(n\), \(l\), \(r\) (\(1 \le l \le r \le n)\) and \(s\) (\(1 \le s \le \frac{n (n+1)}{2}\)) and asked to find a permutation \(p\) of numbers from \(1\) to \(n\) that satisfies the following condition: \(s = p_l + p_{l+1} + \ldots + p_r\). For example, for \(n=5\), \(l=3\), \(r=5\), and \(s=8\), the following permutations are suitable (not all options are listed): \(p = [3, 4, 5, 2, 1]\); \(p = [5, 2, 4, 3, 1]\); \(p = [5, 2, 1, 3, 4]\). But, for example, there is no permutation suitable for the condition above for \(n=4\), \(l=1\), \(r=1\), and \(s=5\).Help Polycarp, for the given \(n\), \(l\), \(r\), and \(s\), find a permutation of numbers from \(1\) to \(n\) that fits the condition above. If there are several suitable permutations, print any of them.
|
The first line contains a single integer \(t\) (\(1 \le t \le 500\)). Then \(t\) test cases follow.Each test case consist of one line with four integers \(n\) (\(1 \le n \le 500\)), \(l\) (\(1 \le l \le n\)), \(r\) (\(l \le r \le n\)), \(s\) (\(1 \le s \le \frac{n (n+1)}{2}\)).It is guaranteed that the sum of \(n\) for all input data sets does not exceed \(500\).
|
For each test case, output on a separate line: \(n\) integers β a permutation of length \(n\) that fits the condition above if such a permutation exists; -1, otherwise. If there are several suitable permutations, print any of them.
|
Input: 5 5 2 3 5 5 3 4 1 3 1 2 4 2 2 2 2 2 1 1 3 | Output: 1 2 3 4 5 -1 1 3 2 1 2 -1
|
Medium
| 3 | 1,033 | 365 | 231 | 15 |
|
2,114 |
G
|
2114G
|
G. Build an Array
| 2,200 |
brute force; constructive algorithms; dp; greedy; math; number theory
|
Yesterday, Dima found an empty array and decided to add some integers to it. He can perform the following operation an unlimited number of times: add any integer to the left or right end of the array. then, as long as there is a pair of identical adjacent elements in the array, they will be replaced by their sum. It can be shown that there can be at most one such pair in the array at the same time.For example, if the array is \([3, 6, 4]\) and we add the number \(3\) to the left, the array will first become \([3, 3, 6, 4]\), then the first two elements will be replaced by \(6\), and the array will become \([6, 6, 4]\), and then β \([12, 4]\).After performing the operation exactly \(k\) times, he thinks he has obtained an array \(a\) of length \(n\), but he does not remember which operations he applied. Determine if there exists a sequence of \(k\) operations that could result in the given array \(a\) from an empty array, or determine that it is impossible.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case description contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(n \le k \le 10^6\)) β the length of the resulting array and the number of operations.The second line contains \(n\) integers \(a_i\) (\(1 \le a_i \le 10^9, a_{i - 1} \ne a_i\)) β the elements of the resulting array.It is guaranteed that the sum of the values of \(n\) across all test cases does not exceed \(10^5\).
|
For each test case, if there is no suitable sequence of operations of length \(k\), output ""NO"". Otherwise, output ""YES"".You may output ""YES"" and ""NO"" in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
|
Input: 83 32 1 43 72 1 42 152 163 10256 32 13 289768 96 13 290768 96 15 75 1 6 3 104 66 8 5 10 | Output: YES NO YES YES YES NO YES YES
|
Hard
| 6 | 970 | 563 | 277 | 21 |
|
667 |
B
|
667B
|
B. Coat of Anticubism
| 1,100 |
constructive algorithms; geometry
|
As some of you know, cubism is a trend in art, where the problem of constructing volumetrical shape on a plane with a combination of three-dimensional geometric shapes comes to the fore. A famous sculptor Cicasso, whose self-portrait you can contemplate, hates cubism. He is more impressed by the idea to transmit two-dimensional objects through three-dimensional objects by using his magnificent sculptures. And his new project is connected with this. Cicasso wants to make a coat for the haters of anticubism. To do this, he wants to create a sculpture depicting a well-known geometric primitive β convex polygon.Cicasso prepared for this a few blanks, which are rods with integer lengths, and now he wants to bring them together. The i-th rod is a segment of length li.The sculptor plans to make a convex polygon with a nonzero area, using all rods he has as its sides. Each rod should be used as a side to its full length. It is forbidden to cut, break or bend rods. However, two sides may form a straight angle .Cicasso knows that it is impossible to make a convex polygon with a nonzero area out of the rods with the lengths which he had chosen. Cicasso does not want to leave the unused rods, so the sculptor decides to make another rod-blank with an integer length so that his problem is solvable. Of course, he wants to make it as short as possible, because the materials are expensive, and it is improper deed to spend money for nothing. Help sculptor!
|
The first line contains an integer n (3 β€ n β€ 105) β a number of rod-blanks.The second line contains n integers li (1 β€ li β€ 109) β lengths of rods, which Cicasso already has. It is guaranteed that it is impossible to make a polygon with n vertices and nonzero area using the rods Cicasso already has.
|
Print the only integer z β the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with (n + 1) vertices and nonzero area from all of the rods.
|
In the first example triangle with sides {1 + 1 = 2, 2, 1} can be formed from a set of lengths {1, 1, 1, 2}. In the second example you can make a triangle with lengths {20, 11, 4 + 3 + 2 + 1 = 10}.
|
Input: 31 2 1 | Output: 1
|
Easy
| 2 | 1,462 | 301 | 189 | 6 |
1,607 |
D
|
1607D
|
D. Blue-Red Permutation
| 1,300 |
greedy; math; sortings
|
You are given an array of integers \(a\) of length \(n\). The elements of the array can be either different or the same. Each element of the array is colored either blue or red. There are no unpainted elements in the array. One of the two operations described below can be applied to an array in a single step: either you can select any blue element and decrease its value by \(1\); or you can select any red element and increase its value by \(1\). Situations in which there are no elements of some color at all are also possible. For example, if the whole array is colored blue or red, one of the operations becomes unavailable.Determine whether it is possible to make \(0\) or more steps such that the resulting array is a permutation of numbers from \(1\) to \(n\)?In other words, check whether there exists a sequence of steps (possibly empty) such that after applying it, the array \(a\) contains in some order all numbers from \(1\) to \(n\) (inclusive), each exactly once.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of input data sets in the test.The description of each set of input data consists of three lines. The first line contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of the original array \(a\). The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(-10^9 \leq a_i \leq 10^9\)) β the array elements themselves.The third line has length \(n\) and consists exclusively of the letters 'B' and/or 'R': \(i\)th character is 'B' if \(a_i\) is colored blue, and is 'R' if colored red.It is guaranteed that the sum of \(n\) over all input sets does not exceed \(2 \cdot 10^5\).
|
Print \(t\) lines, each of which contains the answer to the corresponding test case of the input. Print YES as an answer if the corresponding array can be transformed into a permutation, and NO otherwise.You can print the answer in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
|
In the first test case of the example, the following sequence of moves can be performed: choose \(i=3\), element \(a_3=5\) is blue, so we decrease it, we get \(a=[1,2,4,2]\); choose \(i=2\), element \(a_2=2\) is red, so we increase it, we get \(a=[1,3,4,2]\); choose \(i=3\), element \(a_3=4\) is blue, so we decrease it, we get \(a=[1,3,3,2]\); choose \(i=2\), element \(a_2=2\) is red, so we increase it, we get \(a=[1,4,3,2]\). We got that \(a\) is a permutation. Hence the answer is YES.
|
Input: 8 4 1 2 5 2 BRBR 2 1 1 BB 5 3 1 4 2 5 RBRRB 5 3 1 3 1 3 RBRRB 5 5 1 5 1 5 RBRRB 4 2 2 2 2 BRBR 2 1 -2 BR 4 -2 -1 4 0 RRRR | Output: YES NO YES YES NO YES YES YES
|
Easy
| 3 | 980 | 686 | 331 | 16 |
1,075 |
B
|
1075B
|
B. Taxi drivers and Lyft
| 1,200 |
implementation; sortings
|
Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5.Lyft has become so popular so that it is now used by all \(m\) taxi drivers in the city, who every day transport the rest of the city residents β \(n\) riders.Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same).The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected.But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver \(i\) the number \(a_{i}\) β the number of riders that would call the \(i\)-th taxi driver when all drivers and riders are at their home?The taxi driver can neither transport himself nor other taxi drivers.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n,m \le 10^5\)) β number of riders and taxi drivers.The second line contains \(n + m\) integers \(x_1, x_2, \ldots, x_{n+m}\) (\(1 \le x_1 < x_2 < \ldots < x_{n+m} \le 10^9\)), where \(x_i\) is the coordinate where the \(i\)-th resident lives. The third line contains \(n + m\) integers \(t_1, t_2, \ldots, t_{n+m}\) (\(0 \le t_i \le 1\)). If \(t_i = 1\), then the \(i\)-th resident is a taxi driver, otherwise \(t_i = 0\).It is guaranteed that the number of \(i\) such that \(t_i = 1\) is equal to \(m\).
|
Print \(m\) integers \(a_1, a_2, \ldots, a_{m}\), where \(a_i\) is the answer for the \(i\)-th taxi driver. The taxi driver has the number \(i\) if among all the taxi drivers he lives in the \(i\)-th smallest coordinate (see examples for better understanding).
|
In the first example, we have only one taxi driver, which means an order from any of \(n\) riders will go to him.In the second example, the first taxi driver lives at the point with the coordinate \(2\), and the second one lives at the point with the coordinate \(6\). Obviously, the nearest taxi driver to the rider who lives on the \(3\) coordinate is the first one, and to the rider who lives on the coordinate \(5\) is the second one. The rider who lives on the \(4\) coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver.In the third example, we have one rider and the taxi driver nearest to him is the fourth one.
|
Input: 3 11 2 3 100 0 1 0 | Output: 3
|
Easy
| 2 | 1,117 | 567 | 260 | 10 |
120 |
G
|
120G
|
G. Boom
| 1,800 |
implementation
|
Let's consider the famous game called Boom (aka Hat) with simplified rules.There are n teams playing the game. Each team has two players. The purpose of the game is to explain the words to the teammate without using any words that contain the same root or that sound similarly. Player j from team i (1 β€ i β€ n, 1 β€ j β€ 2) is characterized by two numbers: aij and bij. The numbers correspondingly represent the skill of explaining and the skill of understanding this particular player has. Besides, m cards are used for the game. Each card has a word written on it. The card number k (1 β€ k β€ m) is characterized by number ck β the complexity of the word it contains.Before the game starts the cards are put in a deck and shuffled. Then the teams play in turns like that: the 1-st player of the 1-st team, the 1-st player of the 2-nd team, ... , the 1-st player of the n-th team, the 2-nd player of the 1-st team, ... , the 2-nd player of the n-th team, the 1-st player of the 1-st team and so on.Each turn continues for t seconds. It goes like that: Initially the time for each turn is t. While the time left to a player is more than 0, a player takes a card from the top of the deck and starts explaining the word it has to his teammate. The time needed for the j-th player of the i-th team to explain the word from the card k to his teammate (the q-th player of the i-th team) equals max(1, ck - (aij + biq) - dik) (if j = 1, then q = 2, else q = 1). The value dik is the number of seconds the i-th team has already spent explaining the word k during the previous turns. Initially, all dik equal 0. If a team manages to guess the word before the end of the turn, then the time given above is substracted from the duration of the turn, the card containing the guessed word leaves the game, the team wins one point and the game continues. If the team doesn't manage to guess the word, then the card is put at the bottom of the deck, dik increases on the amount of time of the turn, spent on explaining the word. Thus, when this team gets the very same word, they start explaining it not from the beginning, but from the point where they stopped. The game ends when words from all m cards are guessed correctly.You are given n teams and a deck of m cards. You should determine for each team, how many points it will have by the end of the game and which words the team will have guessed.
|
The first line contains two integers n, t (1 β€ n, t β€ 100), which correspondingly denote the number of teams and a turn's duration.Next n lines of the input file contain four integers each: ai1, bi1, ai2, bi2 (1 β€ aij, bij β€ 100) β the skills of the first and the second player of the i-th team. The teams are given in the order in which they play.The next line of the input file contains integer m (1 β€ m β€ 100) the number of cards.Next 2m lines contain the cards' descriptions. Two lines describe each card. The first line of the description contains the word that consists of no more than 20 characters. The words only contain small Latin letters. The second line of the description contains an integer ck (1 β€ ck β€ 100) β the complexity of the word written on the k-th card. The cards are listed in the order in which the lie in the deck from top to bottom. The words on all cards are different.
|
Print n lines. On the i-th line first print number si the number of points the i-th team wins. Then print si space-separated words β the words from the cards guessed by team i in the order in which they were guessed.
|
Input: 2 21 1 1 11 1 1 13home1car1brother1 | Output: 2 home car 1 brother
|
Medium
| 1 | 2,386 | 899 | 216 | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.