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,614 |
E
|
1614E
|
E. Divan and a Cottage
| 2,600 |
binary search; data structures
|
Divan's new cottage is finally complete! However, after a thorough inspection, it turned out that the workers had installed the insulation incorrectly, and now the temperature in the house directly depends on the temperature outside. More precisely, if the temperature in the house is \(P\) in the morning, and the street temperature is \(T\), then by the next morning the temperature in the house changes according to the following rule: \(P_{new} = P + 1\), if \(P < T\), \(P_{new} = P - 1\), if \(P > T\), \(P_{new} = P\), if \(P = T\). Here \(P_{new}\) is the temperature in the house next morning.Divan is a very busy businessman, so sometimes he is not at home for long periods and does not know what the temperature is there now, so he hired you to find it. You will work for \(n\) days. In the beginning of the \(i\)-th day, the temperature outside \(T_i\) is first given to you. After that, on the \(i\)-th day, you will receive \(k_i\) queries. Each query asks the following: ""if the temperature in the house was \(x_i\) at the morning of the first day, what would be the temperature in the house next morning (after day \(i\))?""Please answer all the businessman's queries.
|
The first line of the input contains the number \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of days.The following is a description of \(n\) days in the following format.The first line of the description contains an integer \(T_i\) (\(0 \leq T_i \leq 10^9\)) β the temperature on that day.The second line contains a non-negative integer \(k_i\) (\(0 \le k_i \le 2 \cdot 10^5\)) β the number of queries that day.The third line contains \(k\) integers \(x'_i\) (\(0 \leq x'_{i} \leq 10^9\)) β the encrypted version of Divan's queries.Let \(lastans = 0\) initially. Divan's actual queries are given by \(x_i = (x'_i + lastans) \bmod (10^9 + 1)\), where \(a \bmod b\) is the reminder when \(a\) is divided by \(b\). After answering the query, set \(lastans\) to the answer.It is guaranteed that the total number of queries (the sum of all \(k_i\)) does not exceed \(2 \cdot 10^5\).
|
For each query, output a single integer β the temperature in the house after day \(i\).
|
Let's look at the first four queries from the example input.The temperature is \(50\) on the first day, \(50\) on the second day, and \(0\) on the third day.Note that \(lastans = 0\) initially. The initial temperature of the first query of the first day is \((1 \, + \, lastans) \bmod (10^9 + 1) = 1\). After the first day, the temperature rises by \(1\), because \(1 < 50\). So the answer to the query is \(2\). Then, we set \(lastans = 2\). The initial temperature of the second query of the first day is \((2 \, + \, lastans) \bmod (10^9 + 1) = 4\). After the first day, the temperature rises by \(1\), because \(4 < 50\). So the answer to the query is \(5\). Then, we set \(lastans = 5\). The initial temperature of the third query of the first day is \((3 \, + \, lastans) \bmod (10^9 + 1) = 8\). After the first day, the temperature rises by \(1\). So the answer to the query is \(9\). Then, we set \(lastans = 9\). The initial temperature of the first query of the second day is \((4 \, + \, lastans) \bmod (10^9 + 1) = 13\). After the first day, the temperature rises by \(1\). After the second day, the temperature rises by \(1\). So the answer to the query is \(15\). Then, we set \(lastans = 15\).
|
Input: 3 50 3 1 2 3 50 3 4 5 6 0 3 7 8 9 | Output: 2 5 9 15 22 30 38 47 53
|
Expert
| 2 | 1,185 | 884 | 87 | 16 |
1,547 |
D
|
1547D
|
D. Co-growing Sequence
| 1,300 |
bitmasks; constructive algorithms; greedy
|
A sequence of non-negative integers \(a_1, a_2, \dots, a_n\) is called growing if for all \(i\) from \(1\) to \(n - 1\) all ones (of binary representation) in \(a_i\) are in the places of ones (of binary representation) in \(a_{i + 1}\) (in other words, \(a_i \:\&\: a_{i + 1} = a_i\), where \(\&\) denotes bitwise AND). If \(n = 1\) then the sequence is considered growing as well.For example, the following four sequences are growing: \([2, 3, 15, 175]\) β in binary it's \([10_2, 11_2, 1111_2, 10101111_2]\); \([5]\) β in binary it's \([101_2]\); \([1, 3, 7, 15]\) β in binary it's \([1_2, 11_2, 111_2, 1111_2]\); \([0, 0, 0]\) β in binary it's \([0_2, 0_2, 0_2]\). The following three sequences are non-growing: \([3, 4, 5]\) β in binary it's \([11_2, 100_2, 101_2]\); \([5, 4, 3]\) β in binary it's \([101_2, 100_2, 011_2]\); \([1, 2, 4, 8]\) β in binary it's \([0001_2, 0010_2, 0100_2, 1000_2]\). Consider two sequences of non-negative integers \(x_1, x_2, \dots, x_n\) and \(y_1, y_2, \dots, y_n\). Let's call this pair of sequences co-growing if the sequence \(x_1 \oplus y_1, x_2 \oplus y_2, \dots, x_n \oplus y_n\) is growing where \(\oplus\) denotes bitwise XOR.You are given a sequence of integers \(x_1, x_2, \dots, x_n\). Find the lexicographically minimal sequence \(y_1, y_2, \dots, y_n\) such that sequences \(x_i\) and \(y_i\) are co-growing.The sequence \(a_1, a_2, \dots, a_n\) is lexicographically smaller than the sequence \(b_1, b_2, \dots, b_n\) if there exists \(1 \le k \le n\) such that \(a_i = b_i\) for any \(1 \le i < k\) but \(a_k < b_k\).
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β length of the sequence \(x_i\).The second line contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(0 \le x_i < 2^{30}\)) β elements of the sequence \(x_i\).It is guaranteed that the sum of \(n\) overall all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print \(n\) integers \(y_1, y_2, \dots, y_n\) (\(0 \le y_i < 2^{30}\)) β lexicographically minimal sequence such that such that it's co-growing with given sequence \(x_i\).
|
Input: 5 4 1 3 7 15 4 1 2 4 8 5 1 2 3 4 5 4 11 13 15 1 1 0 | Output: 0 0 0 0 0 1 3 7 0 1 0 3 2 0 2 0 14 0
|
Easy
| 3 | 1,570 | 436 | 192 | 15 |
|
1,750 |
C
|
1750C
|
C. Complementary XOR
| 1,400 |
constructive algorithms; implementation
|
You have two binary strings \(a\) and \(b\) of length \(n\). You would like to make all the elements of both strings equal to \(0\). Unfortunately, you can modify the contents of these strings using only the following operation: You choose two indices \(l\) and \(r\) (\(1 \le l \le r \le n\)); For every \(i\) that respects \(l \le i \le r\), change \(a_i\) to the opposite. That is, \(a_i := 1 - a_i\); For every \(i\) that respects either \(1 \le i < l\) or \(r < i \le n\), change \(b_i\) to the opposite. That is, \(b_i := 1 - b_i\). Your task is to determine if this is possible, and if it is, to find such an appropriate chain of operations. The number of operations should not exceed \(n + 5\). It can be proven that if such chain of operations exists, one exists with at most \(n + 5\) operations.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the strings.The second line of each test case contains a binary string \(a\), consisting only of characters 0 and 1, of length \(n\).The third line of each test case contains a binary string \(b\), consisting only of characters 0 and 1, of length \(n\).It is guaranteed that sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print first ""YES"" if it's possible to make all the elements of both strings equal to \(0\). Otherwise, print ""NO"". If the answer is ""YES"", on the next line print a single integer \(k\) (\(0 \le k \le n + 5\)) β the number of operations. Then \(k\) lines follows, each contains two integers \(l\) and \(r\) (\(1 \le l \le r \le n\)) β the description of the operation.If there are several correct answers, print any of them.
|
In the first test case, we can perform one operation with \(l = 2\) and \(r = 2\). So \(a_2 := 1 - 1 = 0\) and string \(a\) became equal to 000. \(b_1 := 1 - 1 = 0\), \(b_3 := 1 - 1 = 0\) and string \(b\) became equal to 000.In the second and in the third test cases, it can be proven that it's impossible to make all elements of both strings equal to \(0\).In the fourth test case, we can perform an operation with \(l = 1\) and \(r = 2\), then string \(a\) became equal to 01, and string \(b\) doesn't change. Then we perform an operation with \(l = 2\) and \(r = 2\), then \(a_2 := 1 - 1 = 0\) and \(b_1 = 1 - 1 = 0\). So both of string \(a\) and \(b\) became equal to 00.In the fifth test case, we can perform an operation with \(l = 1\) and \(r = 1\). Then string \(a\) became equal to 011 and string \(b\) became equal to 100. Then we can perform an operation with \(l = 2\) and \(r = 3\), so both of string \(a\) and \(b\) became equal to 000.
|
Input: 5301010121110410000011210103111111 | Output: YES 1 2 2 NO NO YES 2 1 2 2 2 YES 2 1 1 2 3
|
Easy
| 2 | 806 | 633 | 448 | 17 |
1,293 |
B
|
1293B
|
B. JOE is on TV!
| 1,000 |
combinatorics; greedy; math
|
3R2 - Standby for ActionOur dear Cafe's owner, JOE Miller, will soon take part in a new game TV-show ""1 vs. \(n\)""!The game goes in rounds, where in each round the host asks JOE and his opponents a common question. All participants failing to answer are eliminated. The show ends when only JOE remains (we assume that JOE never answers a question wrong!).For each question JOE answers, if there are \(s\) (\(s > 0\)) opponents remaining and \(t\) (\(0 \le t \le s\)) of them make a mistake on it, JOE receives \(\displaystyle\frac{t}{s}\) dollars, and consequently there will be \(s - t\) opponents left for the next question.JOE wonders what is the maximum possible reward he can receive in the best possible scenario. Yet he has little time before show starts, so can you help him answering it instead?
|
The first and single line contains a single integer \(n\) (\(1 \le n \le 10^5\)), denoting the number of JOE's opponents in the show.
|
Print a number denoting the maximum prize (in dollars) JOE could have.Your answer will be considered correct if it's absolute or relative error won't exceed \(10^{-4}\). In other words, if your answer is \(a\) and the jury answer is \(b\), then it must hold that \(\frac{|a - b|}{max(1, b)} \le 10^{-4}\).
|
In the second example, the best scenario would be: one contestant fails at the first question, the other fails at the next one. The total reward will be \(\displaystyle \frac{1}{2} + \frac{1}{1} = 1.5\) dollars.
|
Input: 1 | Output: 1.000000000000
|
Beginner
| 3 | 806 | 133 | 305 | 12 |
893 |
F
|
893F
|
F. Subtree Minimum Query
| 2,300 |
data structures; trees
|
You are given a rooted tree consisting of n vertices. Each vertex has a number written on it; number ai is written on vertex i.Let's denote d(i, j) as the distance between vertices i and j in the tree (that is, the number of edges in the shortest path from i to j). Also let's denote the k-blocked subtree of vertex x as the set of vertices y such that both these conditions are met: x is an ancestor of y (every vertex is an ancestor of itself); d(x, y) β€ k. You are given m queries to the tree. i-th query is represented by two numbers xi and ki, and the answer to this query is the minimum value of aj among such vertices j such that j belongs to ki-blocked subtree of xi.Write a program that would process these queries quickly!Note that the queries are given in a modified way.
|
The first line contains two integers n and r (1 β€ r β€ n β€ 100000) β the number of vertices in the tree and the index of the root, respectively.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β the numbers written on the vertices.Then n - 1 lines follow, each containing two integers x and y (1 β€ x, y β€ n) and representing an edge between vertices x and y. It is guaranteed that these edges form a tree.Next line contains one integer m (1 β€ m β€ 106) β the number of queries to process.Then m lines follow, i-th line containing two numbers pi and qi, which can be used to restore i-th query (1 β€ pi, qi β€ n).i-th query can be restored as follows:Let last be the answer for previous query (or 0 if i = 1). Then xi = ((pi + last) mod n) + 1, and ki = (qi + last) mod n.
|
Print m integers. i-th of them has to be equal to the answer to i-th query.
|
Input: 5 21 3 2 3 52 35 13 44 121 22 3 | Output: 25
|
Expert
| 2 | 782 | 785 | 75 | 8 |
|
120 |
F
|
120F
|
F. Spiders
| 1,400 |
dp; greedy; trees
|
One day mum asked Petya to sort his toys and get rid of some of them. Petya found a whole box of toy spiders. They were quite dear to him and the boy didn't want to throw them away. Petya conjured a cunning plan: he will glue all the spiders together and attach them to the ceiling. Besides, Petya knows that the lower the spiders will hang, the more mum is going to like it and then she won't throw his favourite toys away. Help Petya carry out the plan.A spider consists of k beads tied together by k - 1 threads. Each thread connects two different beads, at that any pair of beads that make up a spider is either directly connected by a thread, or is connected via some chain of threads and beads.Petya may glue spiders together directly gluing their beads. The length of each thread equals 1. The sizes of the beads can be neglected. That's why we can consider that gluing spiders happens by identifying some of the beads (see the picture). Besides, the construction resulting from the gluing process should also represent a spider, that is, it should have the given features. After Petya glues all spiders together, he measures the length of the resulting toy. The distance between a pair of beads is identified as the total length of the threads that connect these two beads. The length of the resulting construction is the largest distance between all pairs of beads. Petya wants to make the spider whose length is as much as possible. The picture two shows two spiders from the second sample. We can glue to the bead number 2 of the first spider the bead number 1 of the second spider. The threads in the spiders that form the sequence of threads of maximum lengths are highlighted on the picture.
|
The first input file line contains one integer n (1 β€ n β€ 100) β the number of spiders. Next n lines contain the descriptions of each spider: integer ni (2 β€ ni β€ 100) β the number of beads, then ni - 1 pairs of numbers denoting the numbers of the beads connected by threads. The beads that make up each spider are numbered from 1 to ni.
|
Print a single number β the length of the required construction.
|
Input: 13 1 2 2 3 | Output: 2
|
Easy
| 3 | 1,705 | 337 | 64 | 1 |
|
59 |
E
|
59E
|
E. Shortest Path
| 2,000 |
graphs; shortest paths
|
In Ancient Berland there were n cities and m two-way roads of equal length. The cities are numbered with integers from 1 to n inclusively. According to an ancient superstition, if a traveller visits three cities ai, bi, ci in row, without visiting other cities between them, a great disaster awaits him. Overall there are k such city triplets. Each triplet is ordered, which means that, for example, you are allowed to visit the cities in the following order: ai, ci, bi. Vasya wants to get from the city 1 to the city n and not fulfil the superstition. Find out which minimal number of roads he should take. Also you are required to find one of his possible path routes.
|
The first line contains three integers n, m, k (2 β€ n β€ 3000, 1 β€ m β€ 20000, 0 β€ k β€ 105) which are the number of cities, the number of roads and the number of the forbidden triplets correspondingly. Then follow m lines each containing two integers xi, yi (1 β€ xi, yi β€ n) which are the road descriptions. The road is described by the numbers of the cities it joins. No road joins a city with itself, there cannot be more than one road between a pair of cities. Then follow k lines each containing three integers ai, bi, ci (1 β€ ai, bi, ci β€ n) which are the forbidden triplets. Each ordered triplet is listed mo more than one time. All three cities in each triplet are distinct.City n can be unreachable from city 1 by roads.
|
If there are no path from 1 to n print -1. Otherwise on the first line print the number of roads d along the shortest path from the city 1 to the city n. On the second line print d + 1 numbers β any of the possible shortest paths for Vasya. The path should start in the city 1 and end in the city n.
|
Input: 4 4 11 22 33 41 31 4 3 | Output: 21 3 4
|
Hard
| 2 | 671 | 726 | 299 | 0 |
|
681 |
D
|
681D
|
D. Gifts by the List
| 2,000 |
constructive algorithms; dfs and similar; graphs; trees
|
Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are n men in Sasha's family, so let's number them with integers from 1 to n.Each man has at most one father but may have arbitrary number of sons.Man number A is considered to be the ancestor of the man number B if at least one of the following conditions is satisfied: A = B; the man number A is the father of the man number B; there is a man number C, such that the man number A is his ancestor and the man number C is the father of the man number B. Of course, if the man number A is an ancestor of the man number B and A β B, then the man number B is not an ancestor of the man number A.The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. A list of candidates is prepared, containing some (possibly all) of the n men in some order. Each of the n men decides to give a gift. In order to choose a person to give a gift to, man A looks through the list and picks the first man B in the list, such that B is an ancestor of A and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the n men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above?
|
In the first line of the input two integers n and m (0 β€ m < n β€ 100 000) are given β the number of the men in the Sasha's family and the number of family relations in it respectively.The next m lines describe family relations: the (i + 1)th line consists of pair of integers pi and qi (1 β€ pi, qi β€ n, pi β qi) meaning that the man numbered pi is the father of the man numbered qi. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father.The next line contains n integers a1, a2, ..., an (1 β€ ai β€ n), ith of which means that the man numbered i wants to give a gift to the man numbered ai. It is guaranteed that for every 1 β€ i β€ n the man numbered ai is an ancestor of the man numbered i.
|
Print an integer k (1 β€ k β€ n) β the number of the men in the list of candidates, in the first line.Print then k pairwise different positive integers not exceeding n β the numbers of the men in the list in an order satisfying every of the men's wishes, one per line.If there are more than one appropriate lists, print any of them. If there is no appropriate list print - 1 in the only line.
|
The first sample explanation: if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (a1 = a3 = 1); if there would be no 2 in the list then the second man wish would not be satisfied (a2 = 2); if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (a2 = 2). if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (a3 = 1).
|
Input: 3 21 22 31 2 1 | Output: -1
|
Hard
| 4 | 1,716 | 836 | 390 | 6 |
365 |
A
|
365A
|
A. Good Number
| 1,100 |
implementation
|
Let's call a number k-good if it contains all digits not exceeding k (0, ..., k). You've got a number k and an array a containing n numbers. Find out how many k-good numbers are in a (count each number every time it occurs in array a).
|
The first line contains integers n and k (1 β€ n β€ 100, 0 β€ k β€ 9). The i-th of the following n lines contains integer ai without leading zeroes (1 β€ ai β€ 109).
|
Print a single integer β the number of k-good numbers in a.
|
Input: 10 61234560123456012345601234560123456012345601234560123456012345601234560 | Output: 10
|
Easy
| 1 | 235 | 159 | 59 | 3 |
|
1,918 |
C
|
1918C
|
C. XOR-distance
| 1,400 |
bitmasks; greedy; implementation; math
|
You are given integers \(a\), \(b\), \(r\). Find the smallest value of \(|({a \oplus x}) - ({b \oplus x})|\) among all \(0 \leq x \leq r\).\(\oplus\) is the operation of bitwise XOR, and \(|y|\) is absolute value of \(y\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case contains integers \(a\), \(b\), \(r\) (\(0 \le a, b, r \le 10^{18}\)).
|
For each test case, output a single number β the smallest possible value.
|
In the first test, when \(r = 0\), then \(x\) is definitely equal to \(0\), so the answer is \(|{4 \oplus 0} - {6 \oplus 0}| = |4 - 6| = 2\).In the second test: When \(x = 0\), \(|{0 \oplus 0} - {3 \oplus 0}| = |0 - 3| = 3\). When \(x = 1\), \(|{0 \oplus 1} - {3 \oplus 1}| = |1 - 2| = 1\). When \(x = 2\), \(|{0 \oplus 2} - {3 \oplus 2}| = |2 - 1| = 1\). Therefore, the answer is \(1\).In the third test, the minimum is achieved when \(x = 1\).
|
Input: 104 6 00 3 29 6 1092 256 23165 839 2011 14 52 7 296549 34359 13851853686404475946 283666553522252166 127929199446003072735268590557942972 916721749674600979 895150420120690183 | Output: 2 1 1 164 542 5 3 37102 27934920819538516 104449824168870225
|
Easy
| 4 | 222 | 182 | 73 | 19 |
2,093 |
F
|
2093F
|
F. Hackers and Neural Networks
| 1,800 |
bitmasks; brute force; greedy
|
Hackers are once again trying to create entertaining phrases using the output of neural networks. This time, they want to obtain an array of strings \(a\) of length \(n\).Initially, they have an array \(c\) of length \(n\), filled with blanks, which are denoted by the symbol \(*\). Thus, if \(n=4\), then initially \(c=[*,*,*,*]\).The hackers have access to \(m\) neural networks, each of which has its own version of the answer to their request β an array of strings \(b_i\) of length \(n\).The hackers are trying to obtain the array \(a\) from the array \(c\) using the following operations: Choose a neural network \(i\), which will perform the next operation on the array \(c\): it will select a random blank, for example, at position \(j\), and replace \(c_j\) with \(b_{i, j}\).For example, if the first neural network is chosen and \(c = [*, \text{Β«likeΒ»}, *]\), and \(b_1 = [\text{Β«IΒ»}, \text{Β«loveΒ»}, \text{Β«applesΒ»}]\), then after the operation with the first neural network, \(c\) may become either \([\text{Β«IΒ»}, \text{Β«likeΒ»}, *]\) or \([*, \text{Β«likeΒ»}, \text{Β«applesΒ»}]\). Choose position \(j\) and replace \(c_j\) with a blank. Unfortunately, because of the way hackers access neural networks, they will only be able to see the modified array \(c\) after all operations are completed, so they will have to specify the entire sequence of operations in advance.However, the random behavior of the neural networks may lead to the situation where the desired array is never obtained, or obtaining it requires an excessive number of operations. Therefore, the hackers are counting on your help in choosing a sequence of operations that will guarantee the acquisition of array \(a\) in the minimum number of operations.More formally, if there exists a sequence of operations that can guarantee obtaining array \(a\) from array \(c\), then among all such sequences, find the one with the minimum number of operations, and output the number of operations in it.If there is no sequence of operations that transforms array \(c\) into array \(a\), then output \(-1\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 500\)) β the length of the original array \(a\) and the number of neural networks, respectively.The second line of each test case contains the array \(a\), consisting of \(n\) strings \(a_i\) (\(1 \le |a_i| \le 10\)), separated by spaces.The next \(m\) lines of each test case contain the arrays \(b_i\) β one in each line, consisting of \(n\) strings \(b_{i, j}\) (\(1 \le |b_{i,j}| \le 10\)), separated by spaces.It is guaranteed that the sum of \(|a_i|\) and \(|b_{i, j}|\) across all test cases does not exceed \(2 \cdot 10^5\), and that the sum of \(n \cdot m\) across all test cases also does not exceed \(2 \cdot 10^5\).It is guaranteed that the input strings consist only of characters from the Latin alphabet in both lowercase and uppercase.Note that the length of each individual input string does not exceed \(10\).
|
Output \(t\) numbers β one number for each test case, each on a separate line.If there exists a sequence of operations that guarantees obtaining array \(a\) from the \(i\)-th test case, then the \(i\)-th number is the number of operations in the minimum such sequence.Otherwise, for the \(i\)-th number, output \(-1\).
|
Input: 4 3 3 I love apples He likes apples I love cats They love dogs 3 2 Icy wake up wake Icy up wake up Icy 4 3 c o D E c o D s c O l S c o m E 4 5 a s k A d s D t O R i A a X b Y b a k A u s k J | Output: 5 -1 6 8
|
Medium
| 3 | 2,074 | 1,012 | 318 | 20 |
|
873 |
F
|
873F
|
F. Forbidden Indices
| 2,400 |
dsu; string suffix structures; strings
|
You are given a string s consisting of n lowercase Latin letters. Some indices in this string are marked as forbidden.You want to find a string a such that the value of |a|Β·f(a) is maximum possible, where f(a) is the number of occurences of a in s such that these occurences end in non-forbidden indices. So, for example, if s is aaaa, a is aa and index 3 is forbidden, then f(a) = 2 because there are three occurences of a in s (starting in indices 1, 2 and 3), but one of them (starting in index 2) ends in a forbidden index.Calculate the maximum possible value of |a|Β·f(a) you can get.
|
The first line contains an integer number n (1 β€ n β€ 200000) β the length of s.The second line contains a string s, consisting of n lowercase Latin letters.The third line contains a string t, consisting of n characters 0 and 1. If i-th character in t is 1, then i is a forbidden index (otherwise i is not forbidden).
|
Print the maximum possible value of |a|Β·f(a).
|
Input: 5ababa00100 | Output: 5
|
Expert
| 3 | 588 | 316 | 45 | 8 |
|
1,242 |
A
|
1242A
|
A. Tile Painting
| 1,500 |
constructive algorithms; math; number theory
|
Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate.The path consists of \(n\) consecutive tiles, numbered from \(1\) to \(n\). Ujan will paint each tile in some color. He will consider the path aesthetic if for any two different tiles with numbers \(i\) and \(j\), such that \(|j - i|\) is a divisor of \(n\) greater than \(1\), they have the same color. Formally, the colors of two tiles with numbers \(i\) and \(j\) should be the same if \(|i-j| > 1\) and \(n \bmod |i-j| = 0\) (where \(x \bmod y\) is the remainder when dividing \(x\) by \(y\)).Ujan wants to brighten up space. What is the maximum number of different colors that Ujan can use, so that the path is aesthetic?
|
The first line of input contains a single integer \(n\) (\(1 \leq n \leq 10^{12}\)), the length of the path.
|
Output a single integer, the maximum possible number of colors that the path can be painted in.
|
In the first sample, two colors is the maximum number. Tiles \(1\) and \(3\) should have the same color since \(4 \bmod |3-1| = 0\). Also, tiles \(2\) and \(4\) should have the same color since \(4 \bmod |4-2| = 0\).In the second sample, all five colors can be used.
|
Input: 4 | Output: 2
|
Medium
| 3 | 769 | 108 | 95 | 12 |
1,618 |
F
|
1618F
|
F. Reverse
| 2,000 |
bitmasks; constructive algorithms; dfs and similar; implementation; math; strings
|
You are given two positive integers \(x\) and \(y\). You can perform the following operation with \(x\): write it in its binary form without leading zeros, add \(0\) or \(1\) to the right of it, reverse the binary form and turn it into a decimal number which is assigned as the new value of \(x\).For example: \(34\) can be turned into \(81\) via one operation: the binary form of \(34\) is \(100010\), if you add \(1\), reverse it and remove leading zeros, you will get \(1010001\), which is the binary form of \(81\). \(34\) can be turned into \(17\) via one operation: the binary form of \(34\) is \(100010\), if you add \(0\), reverse it and remove leading zeros, you will get \(10001\), which is the binary form of \(17\). \(81\) can be turned into \(69\) via one operation: the binary form of \(81\) is \(1010001\), if you add \(0\), reverse it and remove leading zeros, you will get \(1000101\), which is the binary form of \(69\). \(34\) can be turned into \(69\) via two operations: first you turn \(34\) into \(81\) and then \(81\) into \(69\). Your task is to find out whether \(x\) can be turned into \(y\) after a certain number of operations (possibly zero).
|
The only line of the input contains two integers \(x\) and \(y\) (\(1 \le x, y \le 10^{18}\)).
|
Print YES if you can make \(x\) equal to \(y\) and NO if you can't.
|
In the first example, you don't even need to do anything.The fourth example is described in the statement.
|
Input: 3 3 | Output: YES
|
Hard
| 6 | 1,172 | 94 | 67 | 16 |
580 |
E
|
580E
|
E. Kefa and Watch
| 2,500 |
data structures; hashing; strings
|
One day Kefa the parrot was walking down the street as he was on the way home from the restaurant when he saw something glittering by the road. As he came nearer he understood that it was a watch. He decided to take it to the pawnbroker to earn some money. The pawnbroker said that each watch contains a serial number represented by a string of digits from 0 to 9, and the more quality checks this number passes, the higher is the value of the watch. The check is defined by three positive integers l, r and d. The watches pass a check if a substring of the serial number from l to r has period d. Sometimes the pawnbroker gets distracted and Kefa changes in some substring of the serial number all digits to c in order to increase profit from the watch. The seller has a lot of things to do to begin with and with Kefa messing about, he gave you a task: to write a program that determines the value of the watch.Let us remind you that number x is called a period of string s (1 β€ x β€ |s|), if si = si + x for all i from 1 to |s| - x.
|
The first line of the input contains three positive integers n, m and k (1 β€ n β€ 105, 1 β€ m + k β€ 105) β the length of the serial number, the number of change made by Kefa and the number of quality checks.The second line contains a serial number consisting of n digits.Then m + k lines follow, containing either checks or changes. The changes are given as 1 l r c (1 β€ l β€ r β€ n, 0 β€ c β€ 9). That means that Kefa changed all the digits from the l-th to the r-th to be c. The checks are given as 2 l r d (1 β€ l β€ r β€ n, 1 β€ d β€ r - l + 1).
|
For each check on a single line print ""YES"" if the watch passed it, otherwise print ""NO"".
|
In the first sample test two checks will be made. In the first one substring ""12"" is checked on whether or not it has period 1, so the answer is ""NO"". In the second one substring ""88"", is checked on whether or not it has period 1, and it has this period, so the answer is ""YES"".In the second statement test three checks will be made. The first check processes substring ""3493"", which doesn't have period 2. Before the second check the string looks as ""334334"", so the answer to it is ""YES"". And finally, the third check processes substring ""8334"", which does not have period 1.
|
Input: 3 1 21122 2 3 11 1 3 82 1 2 1 | Output: NOYES
|
Expert
| 3 | 1,034 | 538 | 93 | 5 |
1,142 |
E
|
1142E
|
E. Pink Floyd
| 3,200 |
graphs; interactive
|
This is an interactive task.Scientists are about to invent a new optimization for the Floyd-Warshall algorithm, which will allow it to work in linear time. There is only one part of the optimization still unfinished.It is well known that the Floyd-Warshall algorithm takes a graph with \(n\) nodes and exactly one edge between each pair of nodes. The scientists have this graph, what is more, they have directed each edge in one of the two possible directions.To optimize the algorithm, exactly \(m\) edges are colored in pink color and all the rest are colored in green. You know the direction of all \(m\) pink edges, but the direction of green edges is unknown to you. In one query you can ask the scientists about the direction of exactly one green edge, however, you can perform at most \(2 \cdot n\) such queries.Your task is to find the node from which every other node can be reached by a path consisting of edges of same color. Be aware that the scientists may have lied that they had fixed the direction of all edges beforehand, so their answers may depend on your queries.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\,000\), \(0 \le m \le 100\,000\)) β the number of nodes and the number of pink edges.The next \(m\) lines describe the pink edges, the \(i\)-th of these lines contains two integers \(u_i\), \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)) β the start and the end of the \(i\)-th pink edge. It is guaranteed, that all unordered pairs \((u_i, v_i)\) are distinct.
|
When you found the answer, print ""!"" and the number of the node from which every other node can be reached by a single-colored path.
|
In the example above the answer for the query ""? 1 3"" is 0, so the edge is directed from 3 to 1. The answer for the query ""? 4 2"" is 1, so the edge is directed from 4 to 2. The answer for the query ""? 3 2"" is 1, so the edge is directed from 3 to 2. So there are green paths from node 3 to nodes 1 and 2 and there is a pink path from node 3 to node 4.
|
Input: 4 2 1 2 3 4 0 1 1 | Output: ? 1 3 ? 4 2 ? 3 2 ! 3
|
Master
| 2 | 1,083 | 436 | 134 | 11 |
926 |
H
|
926H
|
H. Endless Roses Most Beautiful
| 2,200 |
Arkady decided to buy roses for his girlfriend.A flower shop has white, orange and red roses, and the total amount of them is n. Arkady thinks that red roses are not good together with white roses, so he won't buy a bouquet containing both red and white roses. Also, Arkady won't buy a bouquet where all roses have the same color. Arkady wants to buy exactly k roses. For each rose in the shop he knows its beauty and color: the beauty of the i-th rose is bi, and its color is ci ('W' for a white rose, 'O' for an orange rose and 'R' for a red rose). Compute the maximum possible total beauty of a bouquet of k roses satisfying the constraints above or determine that it is not possible to make such a bouquet.
|
The first line contains two integers n and k (1 β€ k β€ n β€ 200 000) β the number of roses in the show and the number of roses Arkady wants to buy.The second line contains a sequence of integers b1, b2, ..., bn (1 β€ bi β€ 10 000), where bi equals the beauty of the i-th rose.The third line contains a string c of length n, consisting of uppercase English letters 'W', 'O' and 'R', where ci denotes the color of the i-th rose: 'W' denotes white, 'O' β orange, 'R' β red.
|
Print the maximum possible total beauty of a bouquet of k roses that satisfies the constraints above. If it is not possible to make a single such bouquet, print -1.
|
In the first example Arkady wants to buy 3 roses. He can, for example, buy both red roses (their indices are 1 and 2, and their total beauty is 7) and the only orange rose (its index is 3, its beauty is 4). This way the total beauty of the bouquet is 11. In the second example Arkady can not buy a bouquet because all roses have the same color.
|
Input: 5 34 3 4 1 6RROWW | Output: 11
|
Hard
| 0 | 710 | 466 | 164 | 9 |
|
486 |
E
|
486E
|
E. LIS of Sequence
| 2,200 |
data structures; dp; greedy; hashing; math
|
The next ""Data Structures and Algorithms"" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.Nam created a sequence a consisting of n (1 β€ n β€ 105) elements a1, a2, ..., an (1 β€ ai β€ 105). A subsequence ai1, ai2, ..., aik where 1 β€ i1 < i2 < ... < ik β€ n is called increasing if ai1 < ai2 < ai3 < ... < aik. An increasing subsequence is called longest if it has maximum length among all increasing subsequences. Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 β€ i β€ n), into three groups: group of all i such that ai belongs to no longest increasing subsequences. group of all i such that ai belongs to at least one but not every longest increasing subsequence. group of all i such that ai belongs to every longest increasing subsequence. Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job.
|
The first line contains the single integer n (1 β€ n β€ 105) denoting the number of elements of sequence a.The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 105).
|
Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to.
|
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.
|
Input: 14 | Output: 3
|
Hard
| 5 | 1,075 | 188 | 147 | 4 |
163 |
A
|
163A
|
A. Substring and Subsequence
| 1,700 |
dp
|
One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of ""x y"" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string s or different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences.The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2... s|s|.A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 β€ a β€ b β€ |s|). For example, ""code"" and ""force"" are substrings or ""codeforces"", while ""coders"" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a β c or b β d. For example, if s=""codeforces"", s[2...2] and s[6...6] are different, though their content is the same.A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 β€ p1 < p2 < ... < p|y| β€ |s|). For example, ""coders"" is a subsequence of ""codeforces"". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequences p and q are different.
|
The input consists of two lines. The first of them contains s (1 β€ |s| β€ 5000), and the second one contains t (1 β€ |t| β€ 5000). Both strings consist of lowercase Latin letters.
|
Print a single number β the number of different pairs ""x y"" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7).
|
Let's write down all pairs ""x y"" that form the answer in the first sample: ""s[1...1] t[1]"", ""s[2...2] t[1]"", ""s[1...1] t[2]"",""s[2...2] t[2]"", ""s[1...2] t[1 2]"".
|
Input: aaaa | Output: 5
|
Medium
| 1 | 1,371 | 176 | 246 | 1 |
201 |
C
|
201C
|
C. Fragile Bridges
| 2,000 |
dp
|
You are playing a video game and you have just reached the bonus level, where the only possible goal is to score as many points as possible. Being a perfectionist, you've decided that you won't leave this level until you've gained the maximum possible number of points there.The bonus level consists of n small platforms placed in a line and numbered from 1 to n from left to right and (n - 1) bridges connecting adjacent platforms. The bridges between the platforms are very fragile, and for each bridge the number of times one can pass this bridge from one of its ends to the other before it collapses forever is known in advance.The player's actions are as follows. First, he selects one of the platforms to be the starting position for his hero. After that the player can freely move the hero across the platforms moving by the undestroyed bridges. As soon as the hero finds himself on a platform with no undestroyed bridge attached to it, the level is automatically ended. The number of points scored by the player at the end of the level is calculated as the number of transitions made by the hero between the platforms. Note that if the hero started moving by a certain bridge, he has to continue moving in the same direction until he is on a platform.Find how many points you need to score to be sure that nobody will beat your record, and move to the next level with a quiet heart.
|
The first line contains a single integer n (2 β€ n β€ 105) β the number of platforms on the bonus level. The second line contains (n - 1) integers ai (1 β€ ai β€ 109, 1 β€ i < n) β the number of transitions from one end to the other that the bridge between platforms i and i + 1 can bear.
|
Print a single integer β the maximum number of points a player can get on the bonus level.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
One possibility of getting 5 points in the sample is starting from platform 3 and consequently moving to platforms 4, 3, 2, 1 and 2. After that the only undestroyed bridge is the bridge between platforms 4 and 5, but this bridge is too far from platform 2 where the hero is located now.
|
Input: 52 1 2 1 | Output: 5
|
Hard
| 1 | 1,390 | 283 | 237 | 2 |
178 |
B3
|
178B3
|
B3. Greedy Merchants
| 1,800 |
In ABBYY a wonderful Smart Beaver lives. This time, he began to study history. When he read about the Roman Empire, he became interested in the life of merchants.The Roman Empire consisted of n cities numbered from 1 to n. It also had m bidirectional roads numbered from 1 to m. Each road connected two different cities. Any two cities were connected by no more than one road.We say that there is a path between cities c1 and c2 if there exists a finite sequence of cities t1, t2, ..., tp (p β₯ 1) such that: t1 = c1 tp = c2 for any i (1 β€ i < p), cities ti and ti + 1 are connected by a road We know that there existed a path between any two cities in the Roman Empire.In the Empire k merchants lived numbered from 1 to k. For each merchant we know a pair of numbers si and li, where si is the number of the city where this merchant's warehouse is, and li is the number of the city where his shop is. The shop and the warehouse could be located in different cities, so the merchants had to deliver goods from the warehouse to the shop.Let's call a road important for the merchant if its destruction threatens to ruin the merchant, that is, without this road there is no path from the merchant's warehouse to his shop. Merchants in the Roman Empire are very greedy, so each merchant pays a tax (1 dinar) only for those roads which are important for him. In other words, each merchant pays di dinars of tax, where di (di β₯ 0) is the number of roads important for the i-th merchant.The tax collection day came in the Empire. The Smart Beaver from ABBYY is very curious by nature, so he decided to count how many dinars each merchant had paid that day. And now he needs your help.
|
The first input line contains two integers n and m, separated by a space, n is the number of cities, and m is the number of roads in the empire.The following m lines contain pairs of integers ai, bi (1 β€ ai, bi β€ n, ai β bi), separated by a space β the numbers of cities connected by the i-th road. It is guaranteed that any two cities are connected by no more than one road and that there exists a path between any two cities in the Roman Empire.The next line contains a single integer k β the number of merchants in the empire.The following k lines contain pairs of integers si, li (1 β€ si, li β€ n), separated by a space, β si is the number of the city in which the warehouse of the i-th merchant is located, and li is the number of the city in which the shop of the i-th merchant is located.The input limitations for getting 20 points are: 1 β€ n β€ 200 1 β€ m β€ 200 1 β€ k β€ 200 The input limitations for getting 50 points are: 1 β€ n β€ 2000 1 β€ m β€ 2000 1 β€ k β€ 2000 The input limitations for getting 100 points are: 1 β€ n β€ 105 1 β€ m β€ 105 1 β€ k β€ 105
|
Print exactly k lines, the i-th line should contain a single integer di β the number of dinars that the i-th merchant paid.
|
The given sample is illustrated in the figure below. Let's describe the result for the first merchant. The merchant's warehouse is located in city 1 and his shop is in city 5. Let us note that if either road, (1, 2) or (2, 3) is destroyed, there won't be any path between cities 1 and 5 anymore. If any other road is destroyed, the path will be preserved. That's why for the given merchant the answer is 2.
|
Input: 7 81 22 33 44 55 65 73 54 741 52 42 64 7 | Output: 2120
|
Medium
| 0 | 1,676 | 1,052 | 123 | 1 |
|
983 |
C
|
983C
|
C. Elevator
| 2,400 |
dp; graphs; shortest paths
|
You work in a big office. It is a 9 floor building with an elevator that can accommodate up to 4 people. It is your responsibility to manage this elevator.Today you are late, so there are queues on some floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you know the order in which people came to the elevator.According to the company's rules, if an employee comes to the elevator earlier than another one, he has to enter the elevator earlier too (even if these employees stay on different floors). Note that the employees are allowed to leave the elevator in arbitrary order.The elevator has two commands: Go up or down one floor. The movement takes 1 second. Open the doors on the current floor. During this operation all the employees who have reached their destination get out of the elevator. Then all the employees on the floor get in the elevator in the order they are queued up while it doesn't contradict the company's rules and there is enough space in the elevator. Each employee spends 1 second to get inside and outside the elevator. Initially the elevator is empty and is located on the floor 1.You are interested what is the minimum possible time you need to spend to deliver all the employees to their destination. It is not necessary to return the elevator to the floor 1.
|
The first line contains an integer n (1 β€ n β€ 2000) β the number of employees.The i-th of the next n lines contains two integers ai and bi (1 β€ ai, bi β€ 9, ai β bi) β the floor on which an employee initially is, and the floor he wants to reach.The employees are given in the order they came to the elevator.
|
Print a single integer β the minimal possible time in seconds.
|
Explaination for the first sample t = 0 t = 2 t = 3 t = 5 t = 6 t = 7 t = 9 t = 10
|
Input: 23 55 3 | Output: 10
|
Expert
| 3 | 1,351 | 307 | 62 | 9 |
1,354 |
C1
|
1354C1
|
C1. Simple Polygon Embedding
| 1,400 |
binary search; geometry; math; ternary search
|
The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, \(n\) is always even, and in C2, \(n\) is always odd.You are given a regular polygon with \(2 \cdot n\) vertices (it's convex and has equal sides and equal angles) and all its sides have length \(1\). Let's name it as \(2n\)-gon.Your task is to find the square of the minimum size such that you can embed \(2n\)-gon in the square. Embedding \(2n\)-gon in the square means that you need to place \(2n\)-gon in the square in such way that each point which lies inside or on a border of \(2n\)-gon should also lie inside or on a border of the square.You can rotate \(2n\)-gon and/or the square.
|
The first line contains a single integer \(T\) (\(1 \le T \le 200\)) β the number of test cases.Next \(T\) lines contain descriptions of test cases β one per line. Each line contains single even integer \(n\) (\(2 \le n \le 200\)). Don't forget you need to embed \(2n\)-gon, not an \(n\)-gon.
|
Print \(T\) real numbers β one per test case. For each test case, print the minimum length of a side of the square \(2n\)-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed \(10^{-6}\).
|
Input: 3 2 4 200 | Output: 1.000000000 2.414213562 127.321336469
|
Easy
| 4 | 709 | 292 | 246 | 13 |
|
1,599 |
A
|
1599A
|
A. Weights
| 2,600 |
constructive algorithms; greedy; two pointers
|
You are given an array \(A\) of length \(N\) weights of masses \(A_1\), \(A_2\)...\(A_N\). No two weights have the same mass. You can put every weight on one side of the balance (left or right). You don't have to put weights in order \(A_1\),...,\(A_N\). There is also a string \(S\) consisting of characters ""L"" and ""R"", meaning that after putting the \(i-th\) weight (not \(A_i\), but \(i-th\) weight of your choice) left or right side of the balance should be heavier. Find the order of putting the weights on the balance such that rules of string \(S\) are satisfied.
|
The first line contains one integer \(N\) (\(1 \leq N \leq 2*10^5\)) - the length of the array \(A\) The second line contains \(N\) distinct integers: \(A_1\), \(A_2\),...,\(A_N\) (\(1 \leq A_i \leq 10^9\)) - the weights given The third line contains string \(S\) of length \(N\) consisting only of letters ""L"" and ""R"" - string determining which side of the balance should be heavier after putting the \(i-th\) weight of your choice
|
The output contains \(N\) lines. In every line, you should print one integer and one letter - integer representing the weight you are putting on the balance in that move and the letter representing the side of the balance where you are putting the weight. If there is no solution, print \(-1\).
|
Explanation for the test case: after the 1st weight: 3 L (left side is heavier)after the 2nd weight: 2 R (left side is heavier)after the 3rd weight: 8 R (right side is heavier)after the 4th weight: 13 L (left side is heavier)after the 5th weight: 7 L (left side is heavier)So, the rules given by string \(S\) are fulfilled and our order of putting the weights is correct.
|
Input: 5 3 8 2 13 7 LLRLL | Output: 3 L 2 R 8 R 13 L 7 L
|
Expert
| 3 | 575 | 436 | 294 | 15 |
1,651 |
F
|
1651F
|
F. Tower Defense
| 3,000 |
binary search; brute force; data structures
|
Monocarp is playing a tower defense game. A level in the game can be represented as an OX axis, where each lattice point from \(1\) to \(n\) contains a tower in it.The tower in the \(i\)-th point has \(c_i\) mana capacity and \(r_i\) mana regeneration rate. In the beginning, before the \(0\)-th second, each tower has full mana. If, at the end of some second, the \(i\)-th tower has \(x\) mana, then it becomes \(\mathit{min}(x + r_i, c_i)\) mana for the next second.There are \(q\) monsters spawning on a level. The \(j\)-th monster spawns at point \(1\) at the beginning of \(t_j\)-th second, and it has \(h_j\) health. Every monster is moving \(1\) point per second in the direction of increasing coordinate.When a monster passes the tower, the tower deals \(\mathit{min}(H, M)\) damage to it, where \(H\) is the current health of the monster and \(M\) is the current mana amount of the tower. This amount gets subtracted from both monster's health and tower's mana.Unfortunately, sometimes some monsters can pass all \(n\) towers and remain alive. Monocarp wants to know what will be the total health of the monsters after they pass all towers.
|
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of towers.The \(i\)-th of the next \(n\) lines contains two integers \(c_i\) and \(r_i\) (\(1 \le r_i \le c_i \le 10^9\)) β the mana capacity and the mana regeneration rate of the \(i\)-th tower.The next line contains a single integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of monsters.The \(j\)-th of the next \(q\) lines contains two integers \(t_j\) and \(h_j\) (\(0 \le t_j \le 2 \cdot 10^5\); \(1 \le h_j \le 10^{12}\)) β the time the \(j\)-th monster spawns and its health.The monsters are listed in the increasing order of their spawn time, so \(t_j < t_{j+1}\) for all \(1 \le j \le q-1\).
|
Print a single integer β the total health of all monsters after they pass all towers.
|
Input: 3 5 1 7 4 4 2 4 0 14 1 10 3 16 10 16 | Output: 4
|
Master
| 3 | 1,149 | 699 | 85 | 16 |
|
1,257 |
A
|
1257A
|
A. Two Rival Students
| 800 |
greedy; math
|
You are the gym teacher in the school.There are \(n\) students in the row. And there are two rivalling students among them. The first one is in position \(a\), the second in position \(b\). Positions are numbered from \(1\) to \(n\) from left to right.Since they are rivals, you want to maximize the distance between them. If students are in positions \(p\) and \(s\) respectively, then distance between them is \(|p - s|\). You can do the following operation at most \(x\) times: choose two adjacent (neighbouring) students and swap them.Calculate the maximum distance between two rivalling students after at most \(x\) swaps.
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.The only line of each test case contains four integers \(n\), \(x\), \(a\) and \(b\) (\(2 \le n \le 100\), \(0 \le x \le 100\), \(1 \le a, b \le n\), \(a \neq b\)) β the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.
|
For each test case print one integer β the maximum distance between two rivaling students which you can obtain.
|
In the first test case you can swap students in positions \(3\) and \(4\). And then the distance between the rivals is equal to \(|4 - 2| = 2\).In the second test case you don't have to swap students. In the third test case you can't swap students.
|
Input: 3 5 1 3 2 100 33 100 1 6 0 2 3 | Output: 2 99 1
|
Beginner
| 2 | 627 | 395 | 111 | 12 |
2,018 |
B
|
2018B
|
B. Speedbreaker
| 1,900 |
binary search; data structures; dp; greedy; implementation; two pointers
|
Djjaner - Speedbreakerβ There are \(n\) cities in a row, numbered \(1, 2, \ldots, n\) left to right. At time \(1\), you conquer exactly one city, called the starting city. At time \(2, 3, \ldots, n\), you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each \(i\), you conquer city \(i\) at a time no later than \(a_i\). A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of cities.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the deadlines for conquering the cities.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer: the number of starting cities that allow you to win.
|
In the first test case, cities \(2\), \(3\), and \(4\) are good starting cities.In the second test case, there are no good starting cities.In the third test case, the only good starting city is city \(5\).
|
Input: 366 3 3 3 5 565 6 4 1 4 598 6 4 2 1 3 5 7 9 | Output: 3 0 1
|
Hard
| 6 | 480 | 520 | 97 | 20 |
859 |
C
|
859C
|
C. Pie Rules
| 1,500 |
dp; games
|
You may have heard of the pie rule before. It states that if two people wish to fairly share a slice of pie, one person should cut the slice in half, and the other person should choose who gets which slice. Alice and Bob have many slices of pie, and rather than cutting the slices in half, each individual slice will be eaten by just one person.The way Alice and Bob decide who eats each slice is as follows. First, the order in which the pies are to be handed out is decided. There is a special token called the ""decider"" token, initially held by Bob. Until all the pie is handed out, whoever has the decider token will give the next slice of pie to one of the participants, and the decider token to the other participant. They continue until no slices of pie are left.All of the slices are of excellent quality, so each participant obviously wants to maximize the total amount of pie they get to eat. Assuming both players make their decisions optimally, how much pie will each participant receive?
|
Input will begin with an integer N (1 β€ N β€ 50), the number of slices of pie. Following this is a line with N integers indicating the sizes of the slices (each between 1 and 100000, inclusive), in the order in which they must be handed out.
|
Print two integers. First, the sum of the sizes of slices eaten by Alice, then the sum of the sizes of the slices eaten by Bob, assuming both players make their decisions optimally.
|
In the first example, Bob takes the size 141 slice for himself and gives the decider token to Alice. Then Alice gives the size 592 slice to Bob and keeps the decider token for herself, so that she can then give the size 653 slice to herself.
|
Input: 3141 592 653 | Output: 653 733
|
Medium
| 2 | 1,002 | 240 | 181 | 8 |
1,695 |
D2
|
1695D2
|
D2. Tree Queries (Hard Version)
| 2,300 |
constructive algorithms; dfs and similar; dp; greedy; trees
|
The only difference between this problem and D1 is the bound on the size of the tree.You are given an unrooted tree with \(n\) vertices. There is some hidden vertex \(x\) in that tree that you are trying to find.To do this, you may ask \(k\) queries \(v_1, v_2, \ldots, v_k\) where the \(v_i\) are vertices in the tree. After you are finished asking all of the queries, you are given \(k\) numbers \(d_1, d_2, \ldots, d_k\), where \(d_i\) is the number of edges on the shortest path between \(v_i\) and \(x\). Note that you know which distance corresponds to which query.What is the minimum \(k\) such that there exists some queries \(v_1, v_2, \ldots, v_k\) that let you always uniquely identify \(x\) (no matter what \(x\) is).Note that you don't actually need to output these queries.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2\cdot10^5\)) β the number of vertices in the tree.Each of the next \(n-1\) lines contains two integers \(x\) and \(y\) (\(1 \le x, y \le n\)), meaning there is an edges between vertices \(x\) and \(y\) in the tree.It is guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case print a single nonnegative integer, the minimum number of queries you need, on its own line.
|
In the first test case, there is only one vertex, so you don't need any queries.In the second test case, you can ask a single query about the node \(1\). Then, if \(x = 1\), you will get \(0\), otherwise you will get \(1\).
|
Input: 3121 2102 42 15 73 108 66 11 34 79 6 | Output: 0 1 2
|
Expert
| 5 | 787 | 592 | 111 | 16 |
1,984 |
D
|
1984D
|
D. ""a"" String Problem
| 2,000 |
brute force; hashing; implementation; math; string suffix structures; strings
|
You are given a string \(s\) consisting of lowercase Latin characters. Count the number of nonempty strings \(t \neq\) ""\(\texttt{a}\)"" such that it is possible to partition\(^{\dagger}\) \(s\) into some substrings satisfying the following conditions: each substring either equals \(t\) or ""\(\texttt{a}\)"", and at least one substring equals \(t\). \(^{\dagger}\) A partition of a string \(s\) is an ordered sequence of some \(k\) strings \(t_1, t_2, \ldots, t_k\) (called substrings) such that \(t_1 + t_2 + \ldots + t_k = s\), where \(+\) represents the concatenation operation.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The only line of each test case contains a string \(s\) consisting of lowercase Latin characters (\(2 \leq |s| \leq 2 \cdot 10^5\)).The sum of \(|s|\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, output a single integer β the number of nonempty strings \(t \neq\) ""\(\texttt{a}\)"" that satisfy all constraints.
|
In the first test case, \(t\) can be ""\(\texttt{aa}\)"", ""\(\texttt{aaa}\)"", ""\(\texttt{aaaa}\)"", or the full string.In the second test case, \(t\) can be ""\(\texttt{b}\)"", ""\(\texttt{bab}\)"", ""\(\texttt{ba}\)"", or the full string.In the third test case, the only such \(t\) is the full string.
|
Input: 8aaaaababacabacbaaabaaabitsetababbaaaabbbyearnineteeneightyfour | Output: 4 4 1 16 1 2 3 1
|
Hard
| 6 | 584 | 303 | 136 | 19 |
1,614 |
A
|
1614A
|
A. Divan and a Store
| 800 |
brute force; constructive algorithms; greedy
|
Businessman Divan loves chocolate! Today he came to a store to buy some chocolate. Like all businessmen, Divan knows the value of money, so he will not buy too expensive chocolate. At the same time, too cheap chocolate tastes bad, so he will not buy it as well.The store he came to has \(n\) different chocolate bars, and the price of the \(i\)-th chocolate bar is \(a_i\) dollars. Divan considers a chocolate bar too expensive if it costs strictly more than \(r\) dollars. Similarly, he considers a bar of chocolate to be too cheap if it costs strictly less than \(l\) dollars. Divan will not buy too cheap or too expensive bars.Divan is not going to spend all his money on chocolate bars, so he will spend at most \(k\) dollars on chocolates.Please determine the maximum number of chocolate bars Divan can buy.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The description of each test case consists of two lines. The first line contains integers \(n\), \(l\), \(r\), \(k\) (\(1 \le n \le 100\), \(1 \le l \le r \le 10^9\), \(1 \le k \le 10^9\)) β the lowest acceptable price of a chocolate, the highest acceptable price of a chocolate and Divan's total budget, respectively.The second line contains a sequence \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) integers β the prices of chocolate bars in the store.
|
For each test case print a single integer β the maximum number of chocolate bars Divan can buy.
|
In the first example Divan can buy chocolate bars \(1\) and \(3\) and spend \(100\) dollars on them.In the second example Divan can buy chocolate bars \(3\) and \(4\) and spend \(7\) dollars on them.In the third example Divan can buy chocolate bars \(3\), \(4\), and \(5\) for \(12\) dollars.In the fourth example Divan cannot buy any chocolate bar because each of them is either too cheap or too expensive.In the fifth example Divan cannot buy any chocolate bar because he considers the first bar too cheap, and has no budget for the second or third.In the sixth example Divan can buy all the chocolate bars in the shop.
|
Input: 8 3 1 100 100 50 100 50 6 3 5 10 1 2 3 4 5 6 6 3 5 21 1 2 3 4 5 6 10 50 69 100 20 30 40 77 1 1 12 4 70 10000 3 50 80 30 20 60 70 10 2 7 100 2 2 2 2 2 7 7 7 7 7 4 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1 1 1 1 1 | Output: 2 2 3 0 0 10 1 1
|
Beginner
| 3 | 812 | 614 | 95 | 16 |
292 |
B
|
292B
|
B. Network Topology
| 1,200 |
graphs; implementation
|
This problem uses a simplified network topology model, please read the problem statement carefully and use it as a formal document as you develop the solution.Polycarpus continues working as a system administrator in a large corporation. The computer network of this corporation consists of n computers, some of them are connected by a cable. The computers are indexed by integers from 1 to n. It's known that any two computers connected by cable directly or through other computersPolycarpus decided to find out the network's topology. A network topology is the way of describing the network configuration, the scheme that shows the location and the connections of network devices.Polycarpus knows three main network topologies: bus, ring and star. A bus is the topology that represents a shared cable with all computers connected with it. In the ring topology the cable connects each computer only with two other ones. A star is the topology where all computers of a network are connected to the single central node.Let's represent each of these network topologies as a connected non-directed graph. A bus is a connected graph that is the only path, that is, the graph where all nodes are connected with two other ones except for some two nodes that are the beginning and the end of the path. A ring is a connected graph, where all nodes are connected with two other ones. A star is a connected graph, where a single central node is singled out and connected with all other nodes. For clarifications, see the picture. (1) β bus, (2) β ring, (3) β star You've got a connected non-directed graph that characterizes the computer network in Polycarpus' corporation. Help him find out, which topology type the given network is. If that is impossible to do, say that the network's topology is unknown.
|
The first line contains two space-separated integers n and m (4 β€ n β€ 105; 3 β€ m β€ 105) β the number of nodes and edges in the graph, correspondingly. Next m lines contain the description of the graph's edges. The i-th line contains a space-separated pair of integers xi, yi (1 β€ xi, yi β€ n) β the numbers of nodes that are connected by the i-the edge.It is guaranteed that the given graph is connected. There is at most one edge between any two nodes. No edge connects a node with itself.
|
In a single line print the network topology name of the given graph. If the answer is the bus, print ""bus topology"" (without the quotes), if the answer is the ring, print ""ring topology"" (without the quotes), if the answer is the star, print ""star topology"" (without the quotes). If no answer fits, print ""unknown topology"" (without the quotes).
|
Input: 4 31 22 33 4 | Output: bus topology
|
Easy
| 2 | 1,797 | 489 | 353 | 2 |
|
132 |
B
|
132B
|
B. Piet
| 2,100 |
implementation
|
Piet is one of the most known visual esoteric programming languages. The programs in Piet are constructed from colorful blocks of pixels and interpreted using pretty complicated rules. In this problem we will use a subset of Piet language with simplified rules.The program will be a rectangular image consisting of colored and black pixels. The color of each pixel will be given by an integer number between 0 and 9, inclusive, with 0 denoting black. A block of pixels is defined as a rectangle of pixels of the same color (not black). It is guaranteed that all connected groups of colored pixels of the same color will form rectangular blocks. Groups of black pixels can form arbitrary shapes.The program is interpreted using movement of instruction pointer (IP) which consists of three parts: current block pointer (BP); note that there is no concept of current pixel within the block; direction pointer (DP) which can point left, right, up or down; block chooser (CP) which can point to the left or to the right from the direction given by DP; in absolute values CP can differ from DP by 90 degrees counterclockwise or clockwise, respectively.Initially BP points to the block which contains the top-left corner of the program, DP points to the right, and CP points to the left (see the orange square on the image below).One step of program interpretation changes the state of IP in a following way. The interpreter finds the furthest edge of the current color block in the direction of the DP. From all pixels that form this edge, the interpreter selects the furthest one in the direction of CP. After this, BP attempts to move from this pixel into the next one in the direction of DP. If the next pixel belongs to a colored block, this block becomes the current one, and two other parts of IP stay the same. It the next pixel is black or outside of the program, BP stays the same but two other parts of IP change. If CP was pointing to the left, now it points to the right, and DP stays the same. If CP was pointing to the right, now it points to the left, and DP is rotated 90 degrees clockwise.This way BP will never point to a black block (it is guaranteed that top-left pixel of the program will not be black).You are given a Piet program. You have to figure out which block of the program will be current after n steps.
|
The first line of the input contains two integer numbers m (1 β€ m β€ 50) and n (1 β€ n β€ 5Β·107). Next m lines contain the rows of the program. All the lines have the same length between 1 and 50 pixels, and consist of characters 0-9. The first character of the first line will not be equal to 0.
|
Output the color of the block which will be current after n steps of program interpretation.
|
In the first example IP changes in the following way. After step 1 block 2 becomes current one and stays it after two more steps. After step 4 BP moves to block 3, after step 7 β to block 4, and finally after step 10 BP returns to block 1. The sequence of states of IP is shown on the image: the arrows are traversed clockwise, the main arrow shows direction of DP, the side one β the direction of CP.
|
Input: 2 101243 | Output: 1
|
Hard
| 1 | 2,328 | 293 | 92 | 1 |
1,913 |
D
|
1913D
|
D. Array Collapse
| 2,100 |
data structures; divide and conquer; dp; trees
|
You are given an array \([p_1, p_2, \dots, p_n]\), where all elements are distinct.You can perform several (possibly zero) operations with it. In one operation, you can choose a contiguous subsegment of \(p\) and remove all elements from that subsegment, except for the minimum element on that subsegment. For example, if \(p = [3, 1, 4, 7, 5, 2, 6]\) and you choose the subsegment from the \(3\)-rd element to the \(6\)-th element, the resulting array is \([3, 1, 2, 6]\).An array \(a\) is called reachable if it can be obtained from \(p\) using several (maybe zero) aforementioned operations. Calculate the number of reachable arrays, and print it modulo \(998244353\).
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of two lines. The first line contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)). The second line contains \(n\) distinct integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le 10^9\)).Additional constraint on the input: the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, print one integer β the number of reachable arrays, taken modulo \(998244353\).
|
Input: 322 142 4 1 3510 2 6 3 4 | Output: 2 6 12
|
Hard
| 4 | 671 | 422 | 99 | 19 |
|
1,111 |
C
|
1111C
|
C. Creative Snap
| 1,700 |
binary search; brute force; divide and conquer; math
|
Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base.Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of \(2\). Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following: if the current length is at least \(2\), divide the base into \(2\) equal halves and destroy them separately, or burn the current base. If it contains no avenger in it, it takes \(A\) amount of power, otherwise it takes his \(B \cdot n_a \cdot l\) amount of power, where \(n_a\) is the number of avengers and \(l\) is the length of the current base. Output the minimum power needed by Thanos to destroy the avengers' base.
|
The first line contains four integers \(n\), \(k\), \(A\) and \(B\) (\(1 \leq n \leq 30\), \(1 \leq k \leq 10^5\), \(1 \leq A,B \leq 10^4\)), where \(2^n\) is the length of the base, \(k\) is the number of avengers and \(A\) and \(B\) are the constants explained in the question.The second line contains \(k\) integers \(a_{1}, a_{2}, a_{3}, \ldots, a_{k}\) (\(1 \leq a_{i} \leq 2^n\)), where \(a_{i}\) represents the position of avenger in the base.
|
Output one integer β the minimum power needed to destroy the avengers base.
|
Consider the first example.One option for Thanos is to burn the whole base \(1-4\) with power \(2 \cdot 2 \cdot 4 = 16\).Otherwise he can divide the base into two parts \(1-2\) and \(3-4\).For base \(1-2\), he can either burn it with power \(2 \cdot 1 \cdot 2 = 4\) or divide it into \(2\) parts \(1-1\) and \(2-2\).For base \(1-1\), he can burn it with power \(2 \cdot 1 \cdot 1 = 2\). For \(2-2\), he can destroy it with power \(1\), as there are no avengers. So, the total power for destroying \(1-2\) is \(2 + 1 = 3\), which is less than \(4\). Similarly, he needs \(3\) power to destroy \(3-4\). The total minimum power needed is \(6\).
|
Input: 2 2 1 2 1 3 | Output: 6
|
Medium
| 4 | 848 | 450 | 75 | 11 |
1,833 |
C
|
1833C
|
C. Vlad Building Beautiful Array
| 800 |
greedy; math
|
Vlad was given an array \(a\) of \(n\) positive integers. Now he wants to build a beautiful array \(b\) of length \(n\) from it.Vlad considers an array beautiful if all the numbers in it are positive and have the same parity. That is, all numbers in the beautiful array are greater than zero and are either all even or all odd.To build the array \(b\), Vlad can assign each \(b_i\) either the value \(a_i\) or \(a_i - a_j\), where any \(j\) from \(1\) to \(n\) can be chosen.To avoid trying to do the impossible, Vlad asks you to determine whether it is possible to build a beautiful array \(b\) of length \(n\) using his array \(a\).
|
The first line of input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follow the descriptions of the test cases.The first line of each case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\).The second line of each case contains \(n\) positive integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all cases does not exceed \(2 \cdot 10^5\).
|
Output \(t\) strings, each of which is the answer to the corresponding test case. As the answer, output ""YES"" if Vlad can build a beautiful array \(b\), and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
|
Input: 752 6 8 4 351 4 7 6 942 6 4 1075 29 13 9 10000001 11 352 1 2 4 252 4 5 4 342 5 5 4 | Output: NO YES YES YES YES NO NO
|
Beginner
| 2 | 634 | 503 | 319 | 18 |
|
1,002 |
B1
|
1002B1
|
B1. Distinguish zero state and W state
| 1,300 |
*special
|
You are given N qubits (2 β€ N β€ 8) which are guaranteed to be in one of the two states: state, or state. Your task is to perform necessary operations and measurements to figure out which state it was and to return 0 if it was state or 1 if it was W state. The state of the qubits after the operations does not matter.You have to implement an operation which takes an array of N qubits as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[]) : Int { body { // your code here } }}
|
Easy
| 1 | 620 | 0 | 0 | 10 |
||||
2,115 |
B
|
2115B
|
B. Gellyfish and Camellia Japonica
| 2,100 |
brute force; constructive algorithms; dfs and similar; dp; graphs; greedy; trees
|
Gellyfish has an array of \(n\) integers \(c_1, c_2, \ldots, c_n\). In the beginning, \(c = [a_1, a_2, \ldots, a_n]\).Gellyfish will make \(q\) modifications to \(c\).For \(i = 1,2,\ldots,q\), Gellyfish is given three integers \(x_i\), \(y_i\), and \(z_i\) between \(1\) and \(n\). Then Gellyfish will set \(c_{z_i} := \min(c_{x_i}, c_{y_i})\).After the \(q\) modifications, \(c = [b_1, b_2, \ldots, b_n]\).Now Flower knows the value of \(b\) and the value of the integers \(x_i\), \(y_i\), and \(z_i\) for all \(1 \leq i \leq q\), but she doesn't know the value of \(a\).Flower wants to find any possible value of the array \(a\) or report that no such \(a\) exists.If there are multiple possible values of the array \(a\), you may output any of them.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(q\) (\(1 \leq n, q \leq 3 \cdot 10^5\)) β the size of the array and the number of modifications.The second line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \leq b_i \leq 10^9\)) β the value of the array \(c\) after the \(q\) modifications.The following \(q\) lines each contain three integers \(x_i\), \(y_i\), and \(z_i\) (\(1 \leq x_i, y_i, z_i \leq n\)) β describing the \(i\)-th modification.It is guaranteed that the sum of \(n\) and the sum of \(q\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, if \(a\) exists, output \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\)) in a single line. Otherwise, output ""-1"" in a single line.If there are multiple solutions, print any of them.
|
In the first test case, based on the given sequence of modifications, we know that \(b_1 = a_1\) and \(b_2 = \min(a_1, a_2)\). Therefore, it is necessary that \(b_2 \leq b_1\). However, for the given \(b\), we have \(b_1<b_2\). Therefore, there is no solution.In the second test case, we can see that the given \(c\) becomes \(b\) from \(a\) after the given modifications, and \(c\) is not changed at each modification.
|
Input: 32 11 22 1 23 21 2 32 3 21 2 16 41 2 2 3 4 55 6 64 5 53 4 42 3 3 | Output: -1 1 2 3 1 2 3 4 5 5
|
Hard
| 7 | 752 | 767 | 223 | 21 |
1,393 |
A
|
1393A
|
A. Rainbow Dash, Fluttershy and Chess Coloring
| 800 |
greedy; math
|
One evening Rainbow Dash and Fluttershy have come up with a game. Since the ponies are friends, they have decided not to compete in the game but to pursue a common goal. The game starts on a square flat grid, which initially has the outline borders built up. Rainbow Dash and Fluttershy have flat square blocks with size \(1\times1\), Rainbow Dash has an infinite amount of light blue blocks, Fluttershy has an infinite amount of yellow blocks. The blocks are placed according to the following rule: each newly placed block must touch the built on the previous turns figure by a side (note that the outline borders of the grid are built initially). At each turn, one pony can place any number of blocks of her color according to the game rules.Rainbow and Fluttershy have found out that they can build patterns on the grid of the game that way. They have decided to start with something simple, so they made up their mind to place the blocks to form a chess coloring. Rainbow Dash is well-known for her speed, so she is interested in the minimum number of turns she and Fluttershy need to do to get a chess coloring, covering the whole grid with blocks. Please help her find that number!Since the ponies can play many times on different boards, Rainbow Dash asks you to find the minimum numbers of turns for several grids of the games.The chess coloring in two colors is the one in which each square is neighbor by side only with squares of different colors.
|
The first line contains a single integer \(T\) (\(1 \le T \le 100\)): the number of grids of the games. Each of the next \(T\) lines contains a single integer \(n\) (\(1 \le n \le 10^9\)): the size of the side of the grid of the game.
|
For each grid of the game print the minimum number of turns required to build a chess coloring pattern out of blocks on it.
|
For \(3\times3\) grid ponies can make two following moves:
|
Input: 2 3 4 | Output: 2 3
|
Beginner
| 2 | 1,458 | 234 | 123 | 13 |
1,519 |
B
|
1519B
|
B. The Cake Is a Lie
| 800 |
dp; math
|
There is a \(n \times m\) grid. You are standing at cell \((1, 1)\) and your goal is to finish at cell \((n, m)\).You can move to the neighboring cells to the right or down. In other words, suppose you are standing at cell \((x, y)\). You can: move right to the cell \((x, y + 1)\) β it costs \(x\) burles; move down to the cell \((x + 1, y)\) β it costs \(y\) burles. Can you reach cell \((n, m)\) spending exactly \(k\) burles?
|
The first line contains the single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.The first and only line of each test case contains three integers \(n\), \(m\), and \(k\) (\(1 \le n, m \le 100\); \(0 \le k \le 10^4\)) β the sizes of grid and the exact amount of money you need to spend.
|
For each test case, if you can reach cell \((n, m)\) spending exactly \(k\) burles, print YES. Otherwise, print NO.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).
|
In the first test case, you are already in the final cell, so you spend \(0\) burles.In the second, third and fourth test cases, there are two paths from \((1, 1)\) to \((2, 2)\): \((1, 1)\) \(\rightarrow\) \((1, 2)\) \(\rightarrow\) \((2, 2)\) or \((1, 1)\) \(\rightarrow\) \((2, 1)\) \(\rightarrow\) \((2, 2)\). Both costs \(1 + 2 = 3\) burles, so it's the only amount of money you can spend.In the fifth test case, there is the only way from \((1, 1)\) to \((1, 4)\) and it costs \(1 + 1 + 1 = 3\) burles.
|
Input: 6 1 1 0 2 2 2 2 2 3 2 2 4 1 4 3 100 100 10000 | Output: YES NO YES NO YES NO
|
Beginner
| 2 | 429 | 303 | 254 | 15 |
188 |
A
|
188A
|
A. Hexagonal Numbers
| 1,100 |
*special
|
Hexagonal numbers are figurate numbers which can be calculated using the formula hn = 2n2 - n. You are given n; calculate n-th hexagonal number.
|
The only line of input contains an integer n (1 β€ n β€ 100).
|
Output the n-th hexagonal number.
|
Input: 3 | Output: 15
|
Easy
| 1 | 144 | 59 | 33 | 1 |
|
602 |
A
|
602A
|
A. Two Bases
| 1,100 |
brute force; implementation
|
After seeing the ""ALL YOUR BASE ARE BELONG TO US"" meme for the first time, numbers X and Y realised that they have different bases, which complicated their relations.You're given a number X represented in base bx and a number Y represented in base by. Compare those two numbers.
|
The first line of the input contains two space-separated integers n and bx (1 β€ n β€ 10, 2 β€ bx β€ 40), where n is the number of digits in the bx-based representation of X. The second line contains n space-separated integers x1, x2, ..., xn (0 β€ xi < bx) β the digits of X. They are given in the order from the most significant digit to the least significant one.The following two lines describe Y in the same way: the third line contains two space-separated integers m and by (1 β€ m β€ 10, 2 β€ by β€ 40, bx β by), where m is the number of digits in the by-based representation of Y, and the fourth line contains m space-separated integers y1, y2, ..., ym (0 β€ yi < by) β the digits of Y.There will be no leading zeroes. Both X and Y will be positive. All digits of both numbers are given in the standard decimal numeral system.
|
Output a single character (quotes for clarity): '<' if X < Y '>' if X > Y '=' if X = Y
|
In the first sample, X = 1011112 = 4710 = Y.In the second sample, X = 1023 = 215 and Y = 245 = 1123, thus X < Y.In the third sample, and Y = 48031509. We may notice that X starts with much larger digits and bx is much larger than by, so X is clearly larger than Y.
|
Input: 6 21 0 1 1 1 12 104 7 | Output: =
|
Easy
| 2 | 280 | 824 | 86 | 6 |
877 |
F
|
877F
|
F. Ann and Books
| 2,300 |
data structures; flows; hashing
|
In Ann's favorite book shop are as many as n books on math and economics. Books are numbered from 1 to n. Each of them contains non-negative number of problems.Today there is a sale: any subsegment of a segment from l to r can be bought at a fixed price. Ann decided that she wants to buy such non-empty subsegment that the sale operates on it and the number of math problems is greater than the number of economics problems exactly by k. Note that k may be positive, negative or zero.Unfortunately, Ann is not sure on which segment the sale operates, but she has q assumptions. For each of them she wants to know the number of options to buy a subsegment satisfying the condition (because the time she spends on choosing depends on that).Currently Ann is too busy solving other problems, she asks you for help. For each her assumption determine the number of subsegments of the given segment such that the number of math problems is greaten than the number of economics problems on that subsegment exactly by k.
|
The first line contains two integers n and k (1 β€ n β€ 100 000, - 109 β€ k β€ 109) β the number of books and the needed difference between the number of math problems and the number of economics problems.The second line contains n integers t1, t2, ..., tn (1 β€ ti β€ 2), where ti is 1 if the i-th book is on math or 2 if the i-th is on economics.The third line contains n integers a1, a2, ..., an (0 β€ ai β€ 109), where ai is the number of problems in the i-th book.The fourth line contains a single integer q (1 β€ q β€ 100 000) β the number of assumptions.Each of the next q lines contains two integers li and ri (1 β€ li β€ ri β€ n) describing the i-th Ann's assumption.
|
Print q lines, in the i-th of them print the number of subsegments for the i-th Ann's assumption.
|
In the first sample Ann can buy subsegments [1;1], [2;2], [3;3], [2;4] if they fall into the sales segment, because the number of math problems is greater by 1 on them that the number of economics problems. So we should count for each assumption the number of these subsegments that are subsegments of the given segment.Segments [1;1] and [2;2] are subsegments of [1;2].Segments [1;1], [2;2] and [3;3] are subsegments of [1;3].Segments [1;1], [2;2], [3;3], [2;4] are subsegments of [1;4].Segment [3;3] is subsegment of [3;4].
|
Input: 4 11 1 1 21 1 1 141 21 31 43 4 | Output: 2341
|
Expert
| 3 | 1,012 | 663 | 97 | 8 |
1,631 |
A
|
1631A
|
A. Min Max Swap
| 800 |
greedy
|
You are given two arrays \(a\) and \(b\) of \(n\) positive integers each. You can apply the following operation to them any number of times: Select an index \(i\) (\(1\leq i\leq n\)) and swap \(a_i\) with \(b_i\) (i. e. \(a_i\) becomes \(b_i\) and vice versa). Find the minimum possible value of \(\max(a_1, a_2, \ldots, a_n) \cdot \max(b_1, b_2, \ldots, b_n)\) you can get after applying such operation any number of times (possibly zero).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1\le n\le 100\)) β the length of the arrays.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10\,000\)) where \(a_i\) is the \(i\)-th element of the array \(a\).The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 10\,000\)) where \(b_i\) is the \(i\)-th element of the array \(b\).
|
For each test case, print a single integer, the minimum possible value of \(\max(a_1, a_2, \ldots, a_n) \cdot \max(b_1, b_2, \ldots, b_n)\) you can get after applying such operation any number of times.
|
In the first test, you can apply the operations at indices \(2\) and \(6\), then \(a = [1, 4, 6, 5, 1, 5]\) and \(b = [3, 2, 3, 2, 2, 2]\), \(\max(1, 4, 6, 5, 1, 5) \cdot \max(3, 2, 3, 2, 2, 2) = 6 \cdot 3 = 18\).In the second test, no matter how you apply the operations, \(a = [3, 3, 3]\) and \(b = [3, 3, 3]\) will always hold, so the answer is \(\max(3, 3, 3) \cdot \max(3, 3, 3) = 3 \cdot 3 = 9\).In the third test, you can apply the operation at index \(1\), then \(a = [2, 2]\), \(b = [1, 1]\), so the answer is \(\max(2, 2) \cdot \max(1, 1) = 2 \cdot 1 = 2\).
|
Input: 361 2 6 5 1 23 4 3 2 2 533 3 33 3 321 22 1 | Output: 18 9 2
|
Beginner
| 1 | 440 | 623 | 202 | 16 |
656 |
G
|
656G
|
G. You're a Professional
| 1,900 |
*special
|
A simple recommendation system would recommend a user things liked by a certain number of their friends. In this problem you will implement part of such a system.You are given user's friends' opinions about a list of items. You are also given a threshold T β the minimal number of ""likes"" necessary for an item to be recommended to the user.Output the number of items in the list liked by at least T of user's friends.
|
The first line of the input will contain three space-separated integers: the number of friends F (1 β€ F β€ 10), the number of items I (1 β€ I β€ 10) and the threshold T (1 β€ T β€ F).The following F lines of input contain user's friends' opinions. j-th character of i-th line is 'Y' if i-th friend likes j-th item, and 'N' otherwise.
|
Output an integer β the number of items liked by at least T of user's friends.
|
Input: 3 3 2YYYNNNYNY | Output: 2
|
Hard
| 1 | 420 | 328 | 78 | 6 |
|
90 |
B
|
90B
|
B. African Crossword
| 1,100 |
implementation; strings
|
An African crossword is a rectangular table n Γ m in size. Each cell of the table contains exactly one letter. This table (it is also referred to as grid) contains some encrypted word that needs to be decoded.To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously.When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem.You are suggested to solve an African crossword and print the word encrypted there.
|
The first line contains two integers n and m (1 β€ n, m β€ 100). Next n lines contain m lowercase Latin letters each. That is the crossword grid.
|
Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter.
|
Input: 3 3cbabcdcbc | Output: abcd
|
Easy
| 2 | 919 | 143 | 108 | 0 |
|
1,264 |
A
|
1264A
|
A. Beautiful Regional Contest
| 1,500 |
greedy; implementation
|
So the Beautiful Regional Contest (BeRC) has come to an end! \(n\) students took part in the contest. The final standings are already known: the participant in the \(i\)-th place solved \(p_i\) problems. Since the participants are primarily sorted by the number of solved problems, then \(p_1 \ge p_2 \ge \dots \ge p_n\).Help the jury distribute the gold, silver and bronze medals. Let their numbers be \(g\), \(s\) and \(b\), respectively. Here is a list of requirements from the rules, which all must be satisfied: for each of the three types of medals, at least one medal must be awarded (that is, \(g>0\), \(s>0\) and \(b>0\)); the number of gold medals must be strictly less than the number of silver and the number of bronze (that is, \(g<s\) and \(g<b\), but there are no requirements between \(s\) and \(b\)); each gold medalist must solve strictly more problems than any awarded with a silver medal; each silver medalist must solve strictly more problems than any awarded a bronze medal; each bronze medalist must solve strictly more problems than any participant not awarded a medal; the total number of medalists \(g+s+b\) should not exceed half of all participants (for example, if \(n=21\), then you can award a maximum of \(10\) participants, and if \(n=26\), then you can award a maximum of \(13\) participants). The jury wants to reward with medals the total maximal number participants (i.e. to maximize \(g+s+b\)) so that all of the items listed above are fulfilled. Help the jury find such a way to award medals.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases in the input. Then \(t\) test cases follow.The first line of a test case contains an integer \(n\) (\(1 \le n \le 4\cdot10^5\)) β the number of BeRC participants. The second line of a test case contains integers \(p_1, p_2, \dots, p_n\) (\(0 \le p_i \le 10^6\)), where \(p_i\) is equal to the number of problems solved by the \(i\)-th participant from the final standings. The values \(p_i\) are sorted in non-increasing order, i.e. \(p_1 \ge p_2 \ge \dots \ge p_n\).The sum of \(n\) over all test cases in the input does not exceed \(4\cdot10^5\).
|
Print \(t\) lines, the \(j\)-th line should contain the answer to the \(j\)-th test case.The answer consists of three non-negative integers \(g, s, b\). Print \(g=s=b=0\) if there is no way to reward participants with medals so that all requirements from the statement are satisfied at the same time. Otherwise, print three positive numbers \(g, s, b\) β the possible number of gold, silver and bronze medals, respectively. The sum of \(g+s+b\) should be the maximum possible. If there are several answers, print any of them.
|
In the first test case, it is possible to reward \(1\) gold, \(2\) silver and \(3\) bronze medals. In this case, the participant solved \(5\) tasks will be rewarded with the gold medal, participants solved \(4\) tasks will be rewarded with silver medals, participants solved \(2\) or \(3\) tasks will be rewarded with bronze medals. Participants solved exactly \(1\) task won't be rewarded. It's easy to see, that in this case, all conditions are satisfied and it is possible to reward participants in this way. It is impossible to give more than \(6\) medals because the number of medals should not exceed half of the number of participants. The answer \(1\), \(3\), \(2\) is also correct in this test case.In the second and third test cases, it is impossible to reward medals, because at least one medal of each type should be given, but the number of medals should not exceed half of the number of participants.
|
Input: 5 12 5 4 4 3 2 2 1 1 1 1 1 1 4 4 3 2 1 1 1000000 20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 32 64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11 | Output: 1 2 3 0 0 0 0 0 0 2 5 3 2 6 6
|
Medium
| 2 | 1,531 | 653 | 525 | 12 |
1,659 |
B
|
1659B
|
B. Bit Flipping
| 1,300 |
bitmasks; constructive algorithms; greedy; strings
|
You are given a binary string of length \(n\). You have exactly \(k\) moves. In one move, you must select a single bit. The state of all bits except that bit will get flipped (\(0\) becomes \(1\), \(1\) becomes \(0\)). You need to output the lexicographically largest string that you can get after using all \(k\) moves. Also, output the number of times you will select each bit. If there are multiple ways to do this, you may output any of them.A binary string \(a\) is lexicographically larger than a binary string \(b\) of the same length, if and only if the following holds: in the first position where \(a\) and \(b\) differ, the string \(a\) contains a \(1\), and the string \(b\) contains a \(0\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case has two lines. The first line has two integers \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5\); \(0 \leq k \leq 10^9\)).The second line has a binary string of length \(n\), each character is either \(0\) or \(1\).The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output two lines.The first line should contain the lexicographically largest string you can obtain.The second line should contain \(n\) integers \(f_1, f_2, \ldots, f_n\), where \(f_i\) is the number of times the \(i\)-th bit is selected. The sum of all the integers must be equal to \(k\).
|
Here is the explanation for the first testcase. Each step shows how the binary string changes in a move. Choose bit \(1\): \(\color{red}{\underline{1}00001} \rightarrow \color{red}{\underline{1}}\color{blue}{11110}\). Choose bit \(4\): \(\color{red}{111\underline{1}10} \rightarrow \color{blue}{000}\color{red}{\underline{1}}\color{blue}{01}\). Choose bit \(4\): \(\color{red}{000\underline{1}01} \rightarrow \color{blue}{111}\color{red}{\underline{1}}\color{blue}{10}\). The final string is \(111110\) and this is the lexicographically largest string we can get.
|
Input: 66 31000016 41000116 00000006 11110016 111011006 12001110 | Output: 111110 1 0 0 2 0 0 111110 0 1 1 1 0 1 000000 0 0 0 0 0 0 100110 1 0 0 0 0 0 111111 1 2 1 3 0 4 111110 1 1 4 2 0 4
|
Easy
| 4 | 704 | 395 | 310 | 16 |
622 |
B
|
622B
|
B. The Time
| 900 |
implementation
|
You are given the current time in 24-hour format hh:mm. Find and print the time after a minutes.Note that you should find only the time after a minutes, see the examples to clarify the problem statement.You can read more about 24-hour format here https://en.wikipedia.org/wiki/24-hour_clock.
|
The first line contains the current time in the format hh:mm (0 β€ hh < 24, 0 β€ mm < 60). The hours and the minutes are given with two digits (the hours or the minutes less than 10 are given with the leading zeroes).The second line contains integer a (0 β€ a β€ 104) β the number of the minutes passed.
|
The only line should contain the time after a minutes in the format described in the input. Note that you should print exactly two digits for the hours and the minutes (add leading zeroes to the numbers if needed).See the examples to check the input/output format.
|
Input: 23:5910 | Output: 00:09
|
Beginner
| 1 | 291 | 299 | 264 | 6 |
|
2,028 |
D
|
2028D
|
D. Alice's Adventures in Cards
| 2,000 |
constructive algorithms; data structures; dp; graphs; greedy; implementation; ternary search
|
Alice is playing cards with the Queen of Hearts, King of Hearts, and Jack of Hearts. There are \(n\) different types of cards in their card game. Alice currently has a card of type \(1\) and needs a card of type \(n\) to escape Wonderland. The other players have one of each kind of card.In this card game, Alice can trade cards with the three other players. Each player has different preferences for the \(n\) types of cards, which can be described by permutations\(^{\text{β}}\) \(q\), \(k\), and \(j\) for the Queen, King, and Jack, respectively. A player values card \(a\) more than card \(b\) if for their permutation \(p\), \(p_a > p_b\). Then, this player is willing to trade card \(b\) to Alice in exchange for card \(a\). Alice's preferences are straightforward: she values card \(a\) more than card \(b\) if \(a > b\), and she will also only trade according to these preferences.Determine if Alice can trade up from card \(1\) to card \(n\) subject to these preferences, and if it is possible, give a possible set of trades to do it.\(^{\text{β}}\)A permutation of length \(n\) 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).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(2\le n\le 2\cdot 10^5\)) β the number of card types.The next three lines contain the preferences of the Queen, King, and Jack respectively. Each of these lines contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1\le p_i\le n\)) β a permutation corresponding to the player's preferences.The sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, on the first line output a single string ""YES"" or ""NO"" (without the quotes) denoting whether Alice can trade up to card \(n\).If the first line was ""YES"", then on the next line output \(k\) β the number of trades Alice will make. On the next \(k\) lines output space separated a character \(c\in \{\texttt{q}, \texttt{k}, \texttt{j}\}\) and integer \(x\), denoting that Alice trades with player \(c\) to get card \(x\). It must be the case that on the \(k\)'th line, \(x = n\). If there are multiple solutions, print any of them.You can output this answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses. The same goes for the character \(c\) denoting the player in the trade (\(\texttt{Q}, \texttt{K}, \texttt{J}\) will all be accepted alongside their lowercase variants).
|
In the first testcase, Alice can trade with the King to get card \(2\). She can then trade with the Queen to get card \(3\).In the second testcase, even though Alice can trade with the Queen to get card \(3\), with the King to get card \(2\), and then with the Jack to get card \(4\), this is not a valid solution since it doesn't respect Alice's preferences. We can show that there is no way for Alice to get to card \(4\).
|
Input: 231 3 22 1 31 2 342 3 1 41 2 3 41 4 2 3 | Output: YES 2 k 2 q 3 NO
|
Hard
| 7 | 1,382 | 580 | 886 | 20 |
1,197 |
A
|
1197A
|
A. DIY Wooden Ladder
| 900 |
greedy; math; sortings
|
Let's denote a \(k\)-step ladder as the following structure: exactly \(k + 2\) wooden planks, of which two planks of length at least \(k+1\) β the base of the ladder; \(k\) planks of length at least \(1\) β the steps of the ladder; Note that neither the base planks, nor the steps planks are required to be equal.For example, ladders \(1\) and \(3\) are correct \(2\)-step ladders and ladder \(2\) is a correct \(1\)-step ladder. On the first picture the lengths of planks are \([3, 3]\) for the base and \([1]\) for the step. On the second picture lengths are \([3, 3]\) for the base and \([2]\) for the step. On the third picture lengths are \([3, 4]\) for the base and \([2, 3]\) for the steps. You have \(n\) planks. The length of the \(i\)-th planks is \(a_i\). You don't have a saw, so you can't cut the planks you have. Though you have a hammer and nails, so you can assemble the improvised ""ladder"" from the planks.The question is: what is the maximum number \(k\) such that you can choose some subset of the given planks and assemble a \(k\)-step ladder using them?
|
The first line contains a single integer \(T\) (\(1 \le T \le 100\)) β the number of queries. The queries are independent.Each query consists of two lines. The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)) β the number of planks you have.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)) β the lengths of the corresponding planks.It's guaranteed that the total number of planks from all queries doesn't exceed \(10^5\).
|
Print \(T\) integers β one per query. The \(i\)-th integer is the maximum number \(k\), such that you can choose some subset of the planks given in the \(i\)-th query and assemble a \(k\)-step ladder using them.Print \(0\) if you can't make even \(1\)-step ladder from the given set of planks.
|
Examples for the queries \(1-3\) are shown at the image in the legend section.The Russian meme to express the quality of the ladders:
|
Input: 4 4 1 3 1 3 3 3 3 2 5 2 3 3 4 2 3 1 1 2 | Output: 2 1 2 0
|
Beginner
| 3 | 1,076 | 479 | 293 | 11 |
1,326 |
F2
|
1326F2
|
F2. Wise Men (Hard Version)
| 3,200 |
bitmasks; dp; math
|
This is the hard version of the problem. The difference is constraints on the number of wise men and the time limit. You can make hacks only if all versions of this task are solved.\(n\) wise men live in a beautiful city. Some of them know each other.For each of the \(n!\) possible permutations \(p_1, p_2, \ldots, p_n\) of the wise men, let's generate a binary string of length \(n-1\): for each \(1 \leq i < n\) set \(s_i=1\) if \(p_i\) and \(p_{i+1}\) know each other, and \(s_i=0\) otherwise. For all possible \(2^{n-1}\) binary strings, find the number of permutations that produce this binary string.
|
The first line of input contains one integer \(n\) (\(2 \leq n \leq 18)\) β the number of wise men in the city.The next \(n\) lines contain a binary string of length \(n\) each, such that the \(j\)-th character of the \(i\)-th string is equal to '1' if wise man \(i\) knows wise man \(j\), and equals '0' otherwise.It is guaranteed that if the \(i\)-th man knows the \(j\)-th man, then the \(j\)-th man knows \(i\)-th man and no man knows himself.
|
Print \(2^{n-1}\) space-separated integers. For each \(0 \leq x < 2^{n-1}\): Let's consider a string \(s\) of length \(n-1\), such that \(s_i = \lfloor \frac{x}{2^{i-1}} \rfloor \bmod 2\) for all \(1 \leq i \leq n - 1\). The \((x+1)\)-th number should be equal to the required answer for \(s\).
|
In the first test, each wise man knows each other, so every permutation will produce the string \(11\).In the second test: If \(p = \{1, 2, 3, 4\}\), the produced string is \(101\), because wise men \(1\) and \(2\) know each other, \(2\) and \(3\) don't know each other, and \(3\) and \(4\) know each other; If \(p = \{4, 1, 2, 3\}\), the produced string is \(110\), because wise men \(1\) and \(4\) know each other, \(1\) and \(2\) know each other and \(2\), and \(3\) don't know each other; If \(p = \{1, 3, 2, 4\}\), the produced string is \(000\), because wise men \(1\) and \(3\) don't know each other, \(3\) and \(2\) don't know each other, and \(2\) and \(4\) don't know each other.
|
Input: 3 011 101 110 | Output: 0 0 0 6
|
Master
| 3 | 607 | 447 | 294 | 13 |
38 |
G
|
38G
|
G. Queue
| 2,300 |
data structures
|
On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya.Every man is characterized by two numbers: ai, which is the importance of his current task (the greater the number is, the more important the task is) and number ci, which is a picture of his conscience. Numbers ai form the permutation of numbers from 1 to n.Let the queue consist of n - 1 people at the moment. Let's look at the way the person who came number n behaves. First, he stands at the end of the queue and the does the following: if importance of the task ai of the man in front of him is less than an, they swap their places (it looks like this: the man number n asks the one before him: ""Erm... Excuse me please but it's very important for me... could you please let me move up the queue?""), then he again poses the question to the man in front of him and so on. But in case when ai is greater than an, moving up the queue stops. However, the man number n can perform the operation no more than cn times.In our task let us suppose that by the moment when the man number n joins the queue, the process of swaps between n - 1 will have stopped. If the swap is possible it necessarily takes place.Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops.
|
The first input line contains an integer n which is the number of people who has joined the queue (1 β€ n β€ 105). In the next n lines descriptions of the people are given in order of their coming β space-separated integers ai and ci (1 β€ ai β€ n, 0 β€ ci β€ n). Every description is located on s single line. All the ai's are different.
|
Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space.
|
Input: 21 02 1 | Output: 2 1
|
Expert
| 1 | 1,572 | 332 | 412 | 0 |
|
1,910 |
E
|
1910E
|
E. Maximum Sum Subarrays
| 2,100 |
*special; dp
|
You are given two integer arrays \(a\) and \(b\), both of length \(n\).You can perform the following operation any number of times (possibly zero): swap \(a_i\) and \(b_i\).Let \(f(c)\) be the maximum sum of a contiguous subarray of the array \(c\) (including the empty subsegment, which sum is \(0\)).Your task is to calculate the maximum possible value of \(f(a) + f(b)\), using the aforementioned operation any number of times.
|
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 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)).The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(-10^9 \le b_i \le 10^9\)).The sum of \(n\) over all test case doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the maximum possible value of \(f(a) + f(b)\), using the aforementioned operation any number of times.
|
Input: 332 -1 3-4 0 164 2 -6 1 6 -4-6 -2 -3 7 -3 22-2 -50 -1 | Output: 6 21 0
|
Hard
| 2 | 430 | 448 | 147 | 19 |
|
2,018 |
F2
|
2018F2
|
F2. Speedbreaker Counting (Medium Version)
| 3,000 |
dp; greedy; math
|
NightHawk22 - Isolationβ This is the medium version of the problem. In the three versions, the constraints on \(n\) and the time limit are different. You can make hacks only if all the versions of the problem are solved.This is the statement of Problem D1B: There are \(n\) cities in a row, numbered \(1, 2, \ldots, n\) left to right. At time \(1\), you conquer exactly one city, called the starting city. At time \(2, 3, \ldots, n\), you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each \(i\), you conquer city \(i\) at a time no later than \(a_i\). A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win? For each \(0 \leq k \leq n\), count the number of arrays of positive integers \(a_1, a_2, \ldots, a_n\) such that \(1 \leq a_i \leq n\) for each \(1 \leq i \leq n\); the answer to Problem D1B is \(k\). The answer can be very large, so you have to calculate it modulo a given prime \(p\).
|
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 only line of each test case contains two integers \(n\), \(p\) (\(1 \le n \le 500\), \(10^8 \leq p \leq 10^9\), \(p\) is prime) β the number of cities and the modulo.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(500\).
|
For each test case, output \(n+1\) integers: the \(i\)-th integer should be the number of arrays that satisfy the conditions for \(k = i-1\).
|
In the first test case, arrays with \(1\) good starting city: \([1]\). In the second test case, arrays with \(0\) good starting cities: \([1, 1]\); arrays with \(1\) good starting city: \([1, 2]\), \([2, 1]\); arrays with \(2\) good starting cities: \([2, 2]\). In the third test case, arrays with \(0\) good starting cities: \([1, 1, 1]\), \([1, 1, 2]\), \([1, 1, 3]\), \([1, 2, 1]\), \([1, 2, 2]\), \([1, 3, 1]\), \([1, 3, 2]\), \([2, 1, 1]\), \([2, 1, 2]\), \([2, 2, 1]\), \([2, 2, 2]\), \([2, 3, 1]\), \([2, 3, 2]\), \([3, 1, 1]\); arrays with \(1\) good starting city: \([1, 2, 3]\), \([1, 3, 3]\), \([2, 1, 3]\), \([3, 1, 2]\), \([3, 1, 3]\), \([3, 2, 1]\), \([3, 3, 1]\); arrays with \(2\) good starting cities: \([2, 2, 3]\), \([2, 3, 3]\), \([3, 2, 2]\), \([3, 3, 2]\); arrays with \(3\) good starting cities: \([3, 2, 3]\), \([3, 3, 3]\).
|
Input: 111 9982443532 9982443533 9982443534 9982443535 9982443536 9982443537 9982443538 9982443539 99824435310 10227585710 999662017 | Output: 0 1 1 2 1 14 7 4 2 183 34 19 16 4 2624 209 112 120 48 12 42605 1546 793 992 468 216 36 785910 13327 6556 9190 4672 2880 864 144 16382863 130922 61939 94992 50100 36960 14256 4608 576 382823936 1441729 657784 1086596 583344 488700 216000 96480 23040 2880 20300780 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 944100756 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400
|
Master
| 3 | 1,002 | 413 | 141 | 20 |
1,693 |
D
|
1693D
|
D. Decinc Dividing
| 2,800 |
brute force; data structures; divide and conquer; dp; greedy
|
Let's call an array \(a\) of \(m\) integers \(a_1, a_2, \ldots, a_m\) Decinc if \(a\) can be made increasing by removing a decreasing subsequence (possibly empty) from it. For example, if \(a = [3, 2, 4, 1, 5]\), we can remove the decreasing subsequence \([a_1, a_4]\) from \(a\) and obtain \(a = [2, 4, 5]\), which is increasing.You are given a permutation \(p\) of numbers from \(1\) to \(n\). Find the number of pairs of integers \((l, r)\) with \(1 \le l \le r \le n\) such that \(p[l \ldots r]\) (the subarray of \(p\) from \(l\) to \(r\)) is a Decinc array.
|
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the size of \(p\).The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\), all \(p_i\) are distinct) β elements of the permutation.
|
Output the number of pairs of integers \((l, r)\) such that \(p[l \ldots r]\) (the subarray of \(p\) from \(l\) to \(r\)) is a Decinc array. \((1 \le l \le r \le n)\)
|
In the first sample, all subarrays are Decinc.In the second sample, all subarrays except \(p[1 \ldots 6]\) and \(p[2 \ldots 6]\) are Decinc.
|
Input: 3 2 3 1 | Output: 6
|
Master
| 5 | 563 | 242 | 166 | 16 |
2,028 |
B
|
2028B
|
B. Alice's Adventures in Permuting
| 1,400 |
binary search; implementation; math
|
Alice mixed up the words transmutation and permutation! She has an array \(a\) specified via three integers \(n\), \(b\), \(c\): the array \(a\) has length \(n\) and is given via \(a_i = b\cdot (i - 1) + c\) for \(1\le i\le n\). For example, if \(n=3\), \(b=2\), and \(c=1\), then \(a=[2 \cdot 0 + 1, 2 \cdot 1 + 1, 2 \cdot 2 + 1] = [1, 3, 5]\).Now, Alice really enjoys permutations of \([0, \ldots, n-1]\)\(^{\text{β}}\) and would like to transform \(a\) into a permutation. In one operation, Alice replaces the maximum element of \(a\) with the \(\operatorname{MEX}\)\(^{\text{β }}\) of \(a\). If there are multiple maximum elements in \(a\), Alice chooses the leftmost one to replace.Can you help Alice figure out how many operations she has to do for \(a\) to become a permutation for the first time? If it is impossible, you should report it.\(^{\text{β}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(0\) to \(n-1\) in arbitrary order. Please note, this is slightly different from the usual definition of a permutation. For example, \([1,2,0,4,3]\) is a permutation, but \([0,1,1]\) is not a permutation (\(1\) appears twice in the array), and \([0,2,3]\) is also not a permutation (\(n=3\) but there is \(3\) in the array).\(^{\text{β }}\)The \(\operatorname{MEX}\) of an array is the smallest non-negative integer that does not belong to the array. For example, the \(\operatorname{MEX}\) of \([0, 3, 1, 3]\) is \(2\) and the \(\operatorname{MEX}\) of \([5]\) is \(0\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The only line of each test case contains three integers \(n\), \(b\), \(c\) (\(1\le n\le 10^{18}\); \(0\le b\), \(c\le 10^{18}\)) β the parameters of the array.
|
For each test case, if the array can never become a permutation, output \(-1\). Otherwise, output the minimum number of operations for the array to become a permutation.
|
In the first test case, the array is already \([0, 1, \ldots, 9]\), so no operations are required.In the third test case, the starting array is \([1, 3, 5, \ldots, 199]\). After the first operation, the \(199\) gets transformed into a \(0\). In the second operation, the \(197\) gets transformed into a \(2\). If we continue this, it will take exactly \(50\) operations to get the array \([0, 1, 2, 3, \ldots, 99]\).In the fourth test case, two operations are needed: \([1,1,1] \to [0,1,1] \to [0,2,1]\).In the fifth test case, the process is \([0,0,0] \to [1,0,0] \to [2,0,0] \to [1,0,0] \to [2,0,0]\). This process repeats forever, so the array is never a permutation and the answer is \(-1\).
|
Input: 710 1 01 2 3100 2 13 0 13 0 01000000000000000000 0 01000000000000000000 1000000000000000000 1000000000000000000 | Output: 0 1 50 2 -1 -1 1000000000000000000
|
Easy
| 3 | 1,517 | 321 | 169 | 20 |
97 |
C
|
97C
|
C. Winning Strategy
| 2,400 |
binary search; graphs; math; shortest paths
|
One university has just found out about a sport programming contest called ACM ICPC v2.0. This contest doesn't differ much from the well-known ACM ICPC, for example, the participants are not allowed to take part in the finals more than two times. However, there is one notable difference: the teams in the contest should consist of exactly n participants.Having taken part in several ACM ICPC v2.0 finals and having not won any medals, the students and the university governors realized that it's high time they changed something about the preparation process. Specifically, as the first innovation it was decided to change the teams' formation process. Having spent considerable amount of time on studying the statistics of other universities' performance, they managed to receive some interesting information: the dependence between the probability of winning a medal and the number of team members that participated in the finals in the past. More formally, we know n + 1 real numbers p0 β€ p1 β€ ... β€ pn, where pi is the probability of getting a medal on the finals if the team has i participants of previous finals, and other n - i participants arrived to the finals for the first time.Despite such useful data, the university governors are unable to determine such team forming tactics that would provide the maximum probability of winning a medal at ACM ICPC v2.0 finals on average (we are supposed to want to provide such result to the far future and we are also supposed to have an endless supply of students). And how about you, can you offer such optimal tactic? At the first stage the university governors want to know the value of maximum average probability.More formally, suppose that the university sends a team to the k-th world finals. The team has ak participants of previous finals (0 β€ ak β€ n). Since each person can participate in the finals no more than twice, the following condition must be true: . Your task is to choose sequence so that the limit Ξ¨ exists and it's value is maximal:As is an infinite sequence, you should only print the maximum value of the Ξ¨ limit.
|
The first line contains an integer n (3 β€ n β€ 100), n is the number of team participants. The second line contains n + 1 real numbers with no more than 6 digits after decimal point pi (0 β€ i β€ n, 0 β€ pi β€ 1) β the probability of that the team will win a medal if it contains i participants who has already been on the finals. Also the condition pi β€ pi + 1 should be fulfilled for all 0 β€ i β€ n - 1.
|
Print the only real number β the expected average number of medals won per year if the optimal strategy is used. The result may have absolute or relative error 10 - 6.
|
In the second test, no matter what participants the team contains, it is doomed to be successful.
|
Input: 30.115590 0.384031 0.443128 0.562356 | Output: 0.4286122500
|
Expert
| 4 | 2,091 | 399 | 167 | 0 |
671 |
A
|
671A
|
A. Recycling Bottles
| 1,800 |
dp; geometry; greedy; implementation
|
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can take bottles from the ground and put them into a recycling bin.We can think Central Perk as coordinate plane. There are n bottles on the ground, the i-th bottle is located at position (xi, yi). Both Adil and Bera can carry only one bottle at once each. For both Adil and Bera the process looks as follows: Choose to stop or to continue to collect bottles. If the choice was to continue then choose some bottle and walk towards it. Pick this bottle and walk to the recycling bin. Go to step 1. Adil and Bera may move independently. They are allowed to pick bottles simultaneously, all bottles may be picked by any of the two, it's allowed that one of them stays still while the other one continues to pick bottles.They want to organize the process such that the total distance they walk (the sum of distance walked by Adil and distance walked by Bera) is minimum possible. Of course, at the end all bottles should lie in the recycling bin.
|
First line of the input contains six integers ax, ay, bx, by, tx and ty (0 β€ ax, ay, bx, by, tx, ty β€ 109) β initial positions of Adil, Bera and recycling bin respectively.The second line contains a single integer n (1 β€ n β€ 100 000) β the number of bottles on the ground.Then follow n lines, each of them contains two integers xi and yi (0 β€ xi, yi β€ 109) β position of the i-th bottle.It's guaranteed that positions of Adil, Bera, recycling bin and all bottles are distinct.
|
Print one real number β the minimum possible total distance Adil and Bera need to walk in order to put all bottles into recycling bin. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .
|
Consider the first sample.Adil will use the following path: .Bera will use the following path: .Adil's path will be units long, while Bera's path will be units long.
|
Input: 3 1 1 2 0 031 12 12 3 | Output: 11.084259940083
|
Medium
| 4 | 1,038 | 476 | 366 | 6 |
839 |
A
|
839A
|
A. Arya and Bran
| 900 |
implementation
|
Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies.At first, Arya and Bran have 0 Candies. There are n days, at the i-th day, Arya finds ai candies in a box, that is given by the Many-Faced God. Every day she can give Bran at most 8 of her candies. If she don't give him the candies at the same day, they are saved for her and she can give them to him later.Your task is to find the minimum number of days Arya needs to give Bran k candies before the end of the n-th day. Formally, you need to output the minimum day index to the end of which k candies will be given out (the days are indexed from 1 to n).Print -1 if she can't give him k candies during n given days.
|
The first line contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 10000).The second line contains n integers a1, a2, a3, ..., an (1 β€ ai β€ 100).
|
If it is impossible for Arya to give Bran k candies within n days, print -1.Otherwise print a single integer β the minimum number of days Arya needs to give Bran k candies before the end of the n-th day.
|
In the first sample, Arya can give Bran 3 candies in 2 days.In the second sample, Arya can give Bran 17 candies in 3 days, because she can give him at most 8 candies per day.In the third sample, Arya can't give Bran 9 candies, because she can give him at most 8 candies per day and she must give him the candies within 1 day.
|
Input: 2 31 2 | Output: 2
|
Beginner
| 1 | 741 | 145 | 203 | 8 |
64 |
E
|
64E
|
E. Prime Segment
| 1,800 |
*special; brute force; math; number theory
|
Positive integer number x is called prime, if it has exactly two positive integer divisors. For example, 2, 3, 17, 97 are primes, but 1, 10, 120 are not.You are given an integer number n, find the shortest segment [a, b], which contains n (i.e. a β€ n β€ b) and a, b are primes.
|
The only given line contains an integer number n (2 β€ n β€ 10000).
|
Print the space separated pair of the required numbers a, b.
|
Input: 10 | Output: 7 11
|
Medium
| 4 | 276 | 65 | 60 | 0 |
|
1,446 |
B
|
1446B
|
B. Catching Cheaters
| 1,800 |
dp; strings
|
You are given two strings \(A\) and \(B\) representing essays of two students who are suspected cheaters. For any two strings \(C\), \(D\) we define their similarity score \(S(C,D)\) as \(4\cdot LCS(C,D) - |C| - |D|\), where \(LCS(C,D)\) denotes the length of the Longest Common Subsequence of strings \(C\) and \(D\). You believe that only some part of the essays could have been copied, therefore you're interested in their substrings.Calculate the maximal similarity score over all pairs of substrings. More formally, output maximal \(S(C, D)\) over all pairs \((C, D)\), where \(C\) is some substring of \(A\), and \(D\) is some substring of \(B\). If \(X\) is a string, \(|X|\) denotes its length.A string \(a\) is a substring of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.A string \(a\) is a subsequence of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) characters. Pay attention to the difference between the substring and subsequence, as they both appear in the problem statement. You may wish to read the Wikipedia page about the Longest Common Subsequence problem.
|
The first line contains two positive integers \(n\) and \(m\) (\(1 \leq n, m \leq 5000\)) β lengths of the two strings \(A\) and \(B\). The second line contains a string consisting of \(n\) lowercase Latin letters β string \(A\).The third line contains a string consisting of \(m\) lowercase Latin letters β string \(B\).
|
Output maximal \(S(C, D)\) over all pairs \((C, D)\), where \(C\) is some substring of \(A\), and \(D\) is some substring of \(B\).
|
For the first case:abb from the first string and abab from the second string have LCS equal to abb.The result is \(S(abb, abab) = (4 \cdot |abb|\)) - \(|abb|\) - \(|abab|\) = \(4 \cdot 3 - 3 - 4 = 5\).
|
Input: 4 5 abba babab | Output: 5
|
Medium
| 2 | 1,270 | 321 | 131 | 14 |
1,776 |
C
|
1776C
|
C. Library game
| 2,500 |
games; greedy; interactive; sortings
|
Alessia and Bernardo are discovering the world of competitive programming through the books of their university library.The library consists of \(m\) sections numbered from \(1\) to \(m\). Each section contains only books dedicated to a particular subject and different sections correspond to different subjects. In order to prevent the students from wandering in the library, the university has established a system of passes. Each pass has a length \(y\) associated to it and allows access to an interval of \(y\) consecutive sections in the library. During a visit, the student must choose exactly one book from one of these sections and leave the library. Each pass can be used only once.At the moment Alessia and Bernardo have \(n\) passes of lengths \(x_1, \, x_2, \, \dots, \, x_n\). They have different opinions on the best way to improve: Alessia thinks that it is important to study many different topics, while Bernardo believes that it is important to study deeply at least one topic. So, Alessia wants to use the \(n\) passes to get \(n\) books on distinct topics, while Bernardo would like to get at least two books on the same topic.They have reached the following agreement: for each of the following \(n\) days, Alessia will choose a pass of length \(y\) among those which are still available and an interval of \(y\) sections in the library, and Bernardo will go into the library and will take exactly one book from one of those sections.Can Bernardo manage to get at least two books on the same subject, or will Alessia be able to avoid it?You should decide whether you want to be Alessia or Bernardo, and you have to fulfill the goal of your chosen character. The judge will impersonate the other character. Note that, even if at some moment Bernardo has already taken two books on the same subject, the interaction should go on until the end of the \(n\) days.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(n \le m \le 5000\)) β the number of passes and the number of sections.The second line contains \(n\) integers \(x_1, \, x_2, \, \dots, \, x_n\) (\(1 \le x_i \le m\)) β the lengths of the passes available.
|
In the first sample, it can be shown that Alessia can accomplish her goal. An example of interaction (after reading the input) follows:$$$$$$ \begin{array}{|c|c|c|} \hline \textbf{Contestant} & \textbf{Judge} & \textbf{Explanation} \\ \hline \texttt{Alessia} & & \text{The program will act as Alessia} \\ \hline 3 \quad 11 & & \text{Choose $y = 3$ and $a = 11$} \\ \hline & 13 & \text{Judge selects $b = 13$} \\ \hline 10 \quad 2 & & \text{Choose $y = 10$ and $a = 2$} \\ \hline & 9 & \text{Judge selects $b = 9$} \\ \hline 7 \quad 1 & & \text{Choose $y = 7$ and $a = 1$} \\ \hline & 4 & \text{Judge selects $b = 4$} \\ \hline 2 \quad 10 & & \text{Choose $y = 2$ and $a = 10$} \\ \hline & 10 & \text{Judge selects $b = 10$} \\ \hline 3 \quad 6 & & \text{Choose $y = 3$ and $a = 6$} \\ \hline & 7 & \text{Judge selects $b = 7$} \\ \hline \end{array} $$$\(The program of the contestant wins because all the books chosen by Bernardo pertain to different topics. The actions performed by the contestant and the judge in this example of interaction may be non-optimal.In the second sample, it can be shown that Bernardo can manage to fulfil his goal. An example of interaction (after reading the input) follows:\)$$$ \begin{array}{|c|c|c|} \hline \textbf{Contestant} & \textbf{Judge} & \textbf{Explanation} \\ \hline \texttt{Bernardo} & & \text{The program will act as Bernardo} \\ \hline & 4 \quad 1 & \text{Judge chooses $y = 4$ and $a = 1$} \\ \hline 4 & & \text{Select $b = 4$} \\ \hline & 1 \quad 10 & \text{Judge chooses $y = 1$ and $a = 10$} \\ \hline 10 & & \text{Select $b = 10$} \\ \hline & 6 \quad 3 & \text{Judge chooses $y = 6$ and $a = 3$} \\ \hline 4 & & \text{Select $b = 4$} \\ \hline & 4 \quad 5 & \text{Judge chooses $y = 4$ and $a = 5$} \\ \hline 8 & & \text{Select $b = 8$} \\ \hline \end{array} $$$\(The program of the contestant wins because Bernardo has selected two books on topic number \)4$$$. The actions performed by the contestant and the judge in this example of interaction may be non-optimal.
|
Input: 5 14 3 7 2 3 10 | Output: -
|
Expert
| 4 | 1,881 | 281 | 0 | 17 |
|
1,357 |
C1
|
1357C1
|
C1. Prepare superposition of basis states with 0s
| 0 |
*special
|
You are given \(N\) qubits in the state \(|0...0 \rangle\). Your task is to prepare an equal superposition of all basis states that have one or more \(0\) in them.For example, for \(N = 2\) the required state would be \(\frac{1}{\sqrt{3}} \big( |00 \rangle + |01 \rangle + |10 \rangle)\).You are not allowed to use any gates except the Pauli gates (X, Y and Z), the Hadamard gate and the controlled versions of those (you are allowed to use multiple qubits as controls in the controlled versions of gates). However, you are allowed to use measurements.You have to implement an operation which takes an array of \(N\) qubits as an input and has no output. The ""output"" of your solution is the state in which it left the input qubits.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Intrinsic; operation Solve (qs : Qubit[]) : Unit { // your code here }}
|
Beginner
| 1 | 895 | 0 | 0 | 13 |
||||
1,720 |
E
|
1720E
|
E. Misha and Paintings
| 2,700 |
constructive algorithms; data structures; greedy; implementation; math
|
Misha has a square \(n \times n\) matrix, where the number in row \(i\) and column \(j\) is equal to \(a_{i, j}\). Misha wants to modify the matrix to contain exactly \(k\) distinct integers. To achieve this goal, Misha can perform the following operation zero or more times: choose any square submatrix of the matrix (you choose \((x_1,y_1)\), \((x_2,y_2)\), such that \(x_1 \leq x_2\), \(y_1 \leq y_2\), \(x_2 - x_1 = y_2 - y_1\), then submatrix is a set of cells with coordinates \((x, y)\), such that \(x_1 \leq x \leq x_2\), \(y_1 \leq y \leq y_2\)), choose an integer \(k\), where \(1 \leq k \leq n^2\), replace all integers in the submatrix with \(k\). Please find the minimum number of operations that Misha needs to achieve his goal.
|
The first input line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 500, 1 \leq k \leq n^2\)) β the size of the matrix and the desired amount of distinct elements in the matrix.Then \(n\) lines follows. The \(i\)-th of them contains \(n\) integers \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, n}\) (\(1 \leq a_{i,j} \leq n^2\)) β the elements of the \(i\)-th row of the matrix.
|
Output one integer β the minimum number of operations required.
|
In the first test case the answer is \(1\), because one can change the value in the bottom right corner of the matrix to \(1\). The resulting matrix can be found below: 111112341 In the second test case the answer is \(2\). First, one can change the entire matrix to contain only \(1\)s, and the change the value of any single cell to \(2\). One of the possible resulting matrices is displayed below: 111111112
|
Input: 3 4 1 1 1 1 1 2 3 4 5 | Output: 1
|
Master
| 5 | 742 | 376 | 63 | 17 |
652 |
A
|
652A
|
A. Gabriel and Caterpillar
| 1,400 |
implementation; math
|
The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.
|
The first line contains two integers h1, h2 (1 β€ h1 < h2 β€ 105) β the heights of the position of the caterpillar and the apple in centimeters.The second line contains two integers a, b (1 β€ a, b β€ 105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.
|
Print the only integer k β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.If the caterpillar can't get the apple print the only integer - 1.
|
In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.
|
Input: 10 302 1 | Output: 1
|
Easy
| 2 | 826 | 297 | 198 | 6 |
321 |
D
|
321D
|
D. Ciel and Flipboard
| 2,900 |
dp; greedy; math
|
Fox Ciel has a board with n rows and n columns, there is one integer in each cell.It's known that n is an odd number, so let's introduce . Fox Ciel can do the following operation many times: she choose a sub-board with size x rows and x columns, then all numbers in it will be multiplied by -1.Return the maximal sum of numbers in the board that she can get by these operations.
|
The first line contains an integer n, (1 β€ n β€ 33, and n is an odd integer) β the size of the board.Each of the next n lines contains n integers β the numbers in the board. Each number doesn't exceed 1000 by its absolute value.
|
Output a single integer: the maximal sum of numbers in the board that can be accomplished.
|
In the first test, we can apply this operation twice: first on the top left 2 Γ 2 sub-board, then on the bottom right 2 Γ 2 sub-board. Then all numbers will become positive.
|
Input: 3-1 -1 1-1 1 -11 -1 -1 | Output: 9
|
Master
| 3 | 378 | 227 | 90 | 3 |
1,497 |
A
|
1497A
|
A. Meximization
| 800 |
brute force; data structures; greedy; sortings
|
You are given an integer \(n\) and an array \(a_1, a_2, \ldots, a_n\). You should reorder the elements of the array \(a\) in such way that the sum of \(\textbf{MEX}\) on prefixes (\(i\)-th prefix is \(a_1, a_2, \ldots, a_i\)) is maximized.Formally, you should find an array \(b_1, b_2, \ldots, b_n\), such that the sets of elements of arrays \(a\) and \(b\) are equal (it is equivalent to array \(b\) can be found as an array \(a\) with some reordering of its elements) and \(\sum\limits_{i=1}^{n} \textbf{MEX}(b_1, b_2, \ldots, b_i)\) is maximized.\(\textbf{MEX}\) of a set of nonnegative integers is the minimal nonnegative integer such that it is not in the set.For example, \(\textbf{MEX}(\{1, 2, 3\}) = 0\), \(\textbf{MEX}(\{0, 1, 2, 4, 5\}) = 3\).
|
The first line contains a single integer \(t\) \((1 \le t \le 100)\) β the number of test cases.The first line of each test case contains a single integer \(n\) \((1 \le n \le 100)\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((0 \le a_i \le 100)\).
|
For each test case print an array \(b_1, b_2, \ldots, b_n\) β the optimal reordering of \(a_1, a_2, \ldots, a_n\), so the sum of \(\textbf{MEX}\) on its prefixes is maximized.If there exist multiple optimal answers you can find any.
|
In the first test case in the answer \(\textbf{MEX}\) for prefixes will be: \(\textbf{MEX}(\{0\}) = 1\) \(\textbf{MEX}(\{0, 1\}) = 2\) \(\textbf{MEX}(\{0, 1, 2\}) = 3\) \(\textbf{MEX}(\{0, 1, 2, 3\}) = 4\) \(\textbf{MEX}(\{0, 1, 2, 3, 4\}) = 5\) \(\textbf{MEX}(\{0, 1, 2, 3, 4, 7\}) = 5\) \(\textbf{MEX}(\{0, 1, 2, 3, 4, 7, 3\}) = 5\) The sum of \(\textbf{MEX} = 1 + 2 + 3 + 4 + 5 + 5 + 5 = 25\). It can be proven, that it is a maximum possible sum of \(\textbf{MEX}\) on prefixes.
|
Input: 3 7 4 2 0 1 3 3 7 5 2 2 8 6 9 1 0 | Output: 0 1 2 3 4 7 3 2 6 8 9 2 0
|
Beginner
| 4 | 753 | 291 | 232 | 14 |
2,123 |
G
|
2123G
|
G. Modular Sorting
| 2,100 |
brute force; data structures; greedy; math; number theory; sortings
|
You are given an integer \(m\) (\(2\leq m\leq 5\cdot 10^5\)) and an array \(a\) consisting of nonnegative integers smaller than \(m\).Answer queries of the following form: \(1\) \(i\) \(x\): assign \(a_i := x\) \(2\) \(k\): in one operation, you may choose an element \(a_i\) and assign \(a_i := (a_i + k) \pmod m\)\(^{\text{β}}\) β determine if there exists some sequence of (possibly zero) operations to make \(a\) nondecreasing\(^{\text{β }}\). Note that instances of query \(2\) are independent; that is, no actual operations are taking place. Instances of query \(1\) are persistent.\(^{\text{β}}\)\(a\pmod m\) is defined as the unique integer \(b\) such that \(0\leq b<m\) and \(a-b\) is an integer multiple of \(m\).\(^{\text{β }}\)An array \(a\) of size \(n\) is called nondecreasing if and only if \(a_i\leq a_{i+1}\) for all \(1\leq i<n\).
|
The first line contains an integer \(t\) (\(1\leq t\leq 10^4\)) β the number of test cases.The first line of each test case contains three integers, \(n\), \(m\), and \(q\) (\(2\leq n \leq 10^5\), \(2\leq m\leq 5\cdot10^5\), \(1\leq q\leq 10^5\)) β the size of the array \(a\), the integer \(m\), and the number of queries.The second line of each test case contains \(n\) integers, \(a_1,a_2,\dots,a_n\) (\(0\leq a_i < m\)).Then follows \(q\) lines. Each line is of one of the following forms: \(1\) \(i\) \(x\) (\(1\leq i\leq n\), \(0\leq x<m\)) \(2\) \(k\) (\(1\leq k<m\)) It is guaranteed that the sum of \(n\) and the sum of \(q\) over all test cases each do not exceed \(10^5\).
|
For each instance of query \(2\), output on a single line ""YES"" if there exists some sequence of (possibly zero) operations to make \(a\) nondecreasing, and ""NO"" otherwise. You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first sample, the array is initially: 4522410By applying the operation twice on \(a_1\), twice on \(a_2\), once on \(a_5\), twice on \(a_6\), and once on \(a_7\), the array becomes: 0122234 which is in nondecreasing order.After the second query, the array becomes: 4525410 and it can be shown that it is impossible to sort this with operations of the form \(a_i := (a_i+4)\pmod 6\), and it is also impossible to sort this with operations of the form \(a_i := (a_i+3)\pmod 6\).
|
Input: 27 6 64 5 2 2 4 1 02 41 4 52 42 31 7 22 38 8 30 1 2 3 4 5 6 72 41 3 42 4 | Output: YES NO NO YES YES NO
|
Hard
| 6 | 847 | 683 | 338 | 21 |
492 |
C
|
492C
|
C. Vanya and Exams
| 1,400 |
greedy; sortings
|
Vanya wants to pass n exams and get the academic scholarship. He will get the scholarship if the average grade mark for all the exams is at least avg. The exam grade cannot exceed r. Vanya has passed the exams and got grade ai for the i-th exam. To increase the grade for the i-th exam by 1 point, Vanya must write bi essays. He can raise the exam grade multiple times.What is the minimum number of essays that Vanya needs to write to get scholarship?
|
The first line contains three integers n, r, avg (1 β€ n β€ 105, 1 β€ r β€ 109, 1 β€ avg β€ min(r, 106)) β the number of exams, the maximum grade and the required grade point average, respectively.Each of the following n lines contains space-separated integers ai and bi (1 β€ ai β€ r, 1 β€ bi β€ 106).
|
In the first line print the minimum number of essays.
|
In the first sample Vanya can write 2 essays for the 3rd exam to raise his grade by 2 points and 2 essays for the 4th exam to raise his grade by 1 point.In the second sample, Vanya doesn't need to write any essays as his general point average already is above average.
|
Input: 5 5 45 24 73 13 22 5 | Output: 4
|
Easy
| 2 | 451 | 292 | 53 | 4 |
1,864 |
G
|
1864G
|
G. Magic Square
| 3,100 |
combinatorics; constructive algorithms; implementation
|
Aquamoon has a Rubik's Square which can be seen as an \(n \times n\) matrix, the elements of the matrix constitute a permutation of numbers \(1, \ldots, n^2\).Aquamoon can perform two operations on the matrix: Row shift, i.e. shift an entire row of the matrix several positions (at least \(1\) and at most \(n-1\)) to the right. The elements that come out of the right border of the matrix are moved to the beginning of the row. For example, shifting a row \(\begin{pmatrix} a & b & c \end{pmatrix}\) by \(2\) positions would result in \(\begin{pmatrix} b & c & a \end{pmatrix}\); Column shift, i.e. shift an entire column of the matrix several positions (at least \(1\) and at most \(n-1\)) downwards. The elements that come out of the lower border of the matrix are moved to the beginning of the column. For example, shifting a column \(\begin{pmatrix} a \\ b \\ c \end{pmatrix}\) by \(2\) positions would result in \(\begin{pmatrix} b\\c\\a \end{pmatrix}\). The rows are numbered from \(1\) to \(n\) from top to bottom, the columns are numbered from \(1\) to \(n\) from left to right. The cell at the intersection of the \(x\)-th row and the \(y\)-th column is denoted as \((x, y)\).Aquamoon can perform several (possibly, zero) operations, but she has to obey the following restrictions: each row and each column can be shifted at most once; each integer of the matrix can be moved at most twice; the offsets of any two integers moved twice cannot be the same. Formally, if integers \(a\) and \(b\) have been moved twice, assuming \(a\) has changed its position from \((x_1,y_1)\) to \((x_2,y_2)\), and \(b\) has changed its position from \((x_3,y_3)\) to \((x_4,y_4)\), then \(x_2-x_1 \not\equiv x_4-x_3 \pmod{n}\) or \(y_2-y_1 \not\equiv y_4-y_3 \pmod{n}\).Aquamoon wonders in how many ways she can transform the Rubik's Square from the given initial state to a given target state. Two ways are considered different if the sequences of applied operations are different. Since the answer can be very large, print the result modulo \(998\,244\,353\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^4\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(3\le n \le 500\)).The \(i\)-th of the following \(n\) lines contains \(n\) integers \(a_{i1}, \ldots, a_{in}\), representing the \(i\)-th row of the initial matrix (\(1 \le a_{ij} \le n^2\)).The \(i\)-th of the following \(n\) lines contains \(n\) integers \(b_{i1}, \ldots, b_{in}\), representing the \(i\)-th row of the target matrix (\(1 \le b_{ij} \le n^2\)).It is guaranteed that both the elements of the initial matrix and the elements of the target matrix constitute a permutation of numbers \(1, \ldots, n^2\).It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(250\,000\).
|
For each test case, if it is possible to convert the initial state to the target state respecting all the restrictions, output one integer β the number of ways to do so, modulo \(998\,244\,353\).If there is no solution, print a single integer \(0\).
|
In the first test case, the only way to transform the initial matrix to the target one is to shift the second row by \(1\) position to the right, and then shift the first column by \(1\) position downwards.In the second test case, it can be shown that there is no correct way to transform the matrix, thus, the answer is \(0\).
|
Input: 431 2 34 5 67 8 97 2 31 4 56 8 931 2 34 5 67 8 93 2 16 5 49 7 831 2 34 5 67 8 97 8 12 3 45 6 931 2 34 5 67 8 93 8 45 1 97 6 2 | Output: 1 0 0 4
|
Master
| 3 | 2,054 | 838 | 249 | 18 |
2,044 |
C
|
2044C
|
C. Hard Problem
| 800 |
greedy; math
|
Ball is the teacher in Paperfold University. The seats of his classroom are arranged in \(2\) rows with \(m\) seats each.Ball is teaching \(a + b + c\) monkeys, and he wants to assign as many monkeys to a seat as possible. Ball knows that \(a\) of them only want to sit in row \(1\), \(b\) of them only want to sit in row \(2\), and \(c\) of them have no preference. Only one monkey may sit in each seat, and each monkey's preference must be followed if it is seated.What is the maximum number of monkeys that Ball can seat?
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.Each test case contains four integers \(m\), \(a\), \(b\), and \(c\) (\(1 \leq m, a, b, c \leq 10^8\)).
|
For each test case, output the maximum number of monkeys you can seat.
|
In the second test case, \(6\) monkeys want to sit in the front row, but only \(3\) seats are available. The monkeys that have no preference and the monkeys who prefer sitting in the second row can sit in the second row together. Thus, the answer is \(3+2=5\).
|
Input: 510 5 5 103 6 1 115 14 12 41 1 1 1420 6 9 69 | Output: 20 5 30 2 84
|
Beginner
| 2 | 524 | 196 | 70 | 20 |
1,500 |
F
|
1500F
|
F. Cupboards Jumps
| 3,500 |
dp
|
In the house where Krosh used to live, he had \(n\) cupboards standing in a line, the \(i\)-th cupboard had the height of \(h_i\). Krosh moved recently, but he wasn't able to move the cupboards with him. Now he wants to buy \(n\) new cupboards so that they look as similar to old ones as possible.Krosh does not remember the exact heights of the cupboards, but for every three consecutive cupboards he remembers the height difference between the tallest and the shortest of them. In other words, if the cupboards' heights were \(h_1, h_2, \ldots, h_n\), then Krosh remembers the values \(w_i = \max(h_{i}, h_{i + 1}, h_{i + 2}) - \min(h_{i}, h_{i + 1}, h_{i + 2})\) for all \(1 \leq i \leq n - 2\).Krosh wants to buy such \(n\) cupboards that all the values \(w_i\) remain the same. Help him determine the required cupboards' heights, or determine that he remembers something incorrectly and there is no suitable sequence of heights.
|
The first line contains two integers \(n\) and \(C\) (\(3 \leq n \leq 10^6\), \(0 \leq C \leq 10^{12}\)) β the number of cupboards and the limit on possible \(w_i\).The second line contains \(n - 2\) integers \(w_1, w_2, \ldots, w_{n - 2}\) (\(0 \leq w_i \leq C\)) β the values defined in the statement.
|
If there is no suitable sequence of \(n\) cupboards, print ""NO"".Otherwise print ""YES"" in the first line, then in the second line print \(n\) integers \(h'_1, h'_2, \ldots, h'_n\) (\(0 \le h'_i \le 10^{18}\)) β the heights of the cupboards to buy, from left to right.We can show that if there is a solution, there is also a solution satisfying the constraints on heights.If there are multiple answers, print any.
|
Consider the first example: \(w_1 = \max(4, 8, 8) - \min(4, 8, 8) = 8 - 4 = 4\) \(w_2 = \max(8, 8, 16) - \min(8, 8, 16) = 16 - 8 = 8\) \(w_3 = \max(8, 16, 20) - \min(8, 16, 20) = 20 - 8 = 12\) \(w_4 = \max(16, 20, 4) - \min(16, 20, 4) = 20 - 4 = 16\) \(w_5 = \max(20, 4, 0) - \min(20, 4, 0) = 20 - 0 = 20\) There are other possible solutions, for example, the following: \(0, 1, 4, 9, 16, 25, 36\).
|
Input: 7 20 4 8 12 16 20 | Output: YES 4 8 8 16 20 4 0
|
Master
| 1 | 933 | 303 | 415 | 15 |
1,154 |
G
|
1154G
|
G. Minimum Possible LCM
| 2,200 |
brute force; greedy; math; number theory
|
You are given an array \(a\) consisting of \(n\) integers \(a_1, a_2, \dots, a_n\).Your problem is to find such pair of indices \(i, j\) (\(1 \le i < j \le n\)) that \(lcm(a_i, a_j)\) is minimum possible.\(lcm(x, y)\) is the least common multiple of \(x\) and \(y\) (minimum positive number such that both \(x\) and \(y\) are divisors of this number).
|
The first line of the input contains one integer \(n\) (\(2 \le n \le 10^6\)) β the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^7\)), where \(a_i\) is the \(i\)-th element of \(a\).
|
Print two integers \(i\) and \(j\) (\(1 \le i < j \le n\)) such that the value of \(lcm(a_i, a_j)\) is minimum among all valid pairs \(i, j\). If there are multiple answers, you can print any.
|
Input: 5 2 4 8 3 6 | Output: 1 2
|
Hard
| 4 | 351 | 263 | 192 | 11 |
|
1,977 |
D
|
1977D
|
D. XORificator
| 2,300 |
bitmasks; brute force; greedy; hashing
|
You are given a binary (consisting only of 0s and 1s) \(n \times m\) matrix. You are also given a XORificator, using which you can invert all the values in a chosen row (i.e. replace 0 with 1 and 1 with 0).A column in the matrix is considered special if it contains exactly one 1. Your task is to find the maximum number of columns that can be made special at the same time, and the set of rows the XORificator should be used on to achieve that.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 3 \cdot 10^5\), \(n \cdot m \leq 3 \cdot 10^5\)).Each of the following \(n\) lines of the test case contains a binary string of length \(m\).It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, output two lines.In the first line, output the maximum number of special columns that is possible to get simultaneously.In the second line, output a binary string of length \(n\), where the \(i\)-th character is 0, if you don't use the XORificator on the \(i\)-th row, and 1, if you use the XORificator on the \(i\)-th row.If there are multiple valid XORificator configurations that achieve the optimal answer, you can output any of them.
|
In the first test case, you can use the XORificator on the second row to make the columns \(2\), \(3\), and \(4\) special.In the second test case, the only column is already special, so you don't need to use the XORificator.
|
Input: 53 41010011001001 111 102 500101101103 3101111000 | Output: 3 010 1 0 1 1 3 00 2 010
|
Expert
| 4 | 445 | 521 | 458 | 19 |
1,061 |
F
|
1061F
|
F. Lost Root
| 2,400 |
interactive; probabilities
|
The graph is called tree if it is connected and has no cycles. Suppose the tree is rooted at some vertex. Then tree is called to be perfect \(k\)-ary tree if each vertex is either a leaf (has no children) or has exactly \(k\) children. Also, in perfect \(k\)-ary tree all leafs must have same depth.For example, the picture below illustrates perfect binary tree with \(15\) vertices:There is a perfect \(k\)-ary tree with \(n\) nodes. The nodes are labeled with distinct integers from \(1\) to \(n\), however you don't know how nodes are labelled. Still, you want to find the label of the root of the tree.You are allowed to make at most \(60 \cdot n\) queries of the following type: ""? \(a\) \(b\) \(c\)"", the query returns ""Yes"" if node with label \(b\) lies on the path from \(a\) to \(c\) and ""No"" otherwise. Both \(a\) and \(c\) are considered to be lying on the path from \(a\) to \(c\).When you are ready to report the root of the tree, print ""! \(s\)"", where \(s\) is the label of the root of the tree. It is possible to report the root only once and this query is not counted towards limit of \(60 \cdot n\) queries.
|
The tree in the example is as follows:The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity).The hack corresponding to the example would look like:3 22 3 1
|
Input: 3 2NoYes | Output: ? 1 3 2? 1 2 3! 2
|
Expert
| 2 | 1,133 | 0 | 0 | 10 |
||
595 |
B
|
595B
|
B. Pasha and Phone
| 1,600 |
binary search; math
|
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly n digits.Also Pasha has a number k and two sequences of length n / k (n is divisible by k) a1, a2, ..., an / k and b1, b2, ..., bn / k. Let's split the phone number into blocks of length k. The first block will be formed by digits from the phone number that are on positions 1, 2,..., k, the second block will be formed by digits from the phone number that are on positions k + 1, k + 2, ..., 2Β·k and so on. Pasha considers a phone number good, if the i-th block doesn't start from the digit bi and is divisible by ai if represented as an integer. To represent the block of length k as an integer, let's write it out as a sequence c1, c2,...,ck. Then the integer is calculated as the result of the expression c1Β·10k - 1 + c2Β·10k - 2 + ... + ck.Pasha asks you to calculate the number of good phone numbers of length n, for the given k, ai and bi. As this number can be too big, print it modulo 109 + 7.
|
The first line of the input contains two integers n and k (1 β€ n β€ 100 000, 1 β€ k β€ min(n, 9)) β the length of all phone numbers and the length of each block, respectively. It is guaranteed that n is divisible by k.The second line of the input contains n / k space-separated positive integers β sequence a1, a2, ..., an / k (1 β€ ai < 10k).The third line of the input contains n / k space-separated positive integers β sequence b1, b2, ..., bn / k (0 β€ bi β€ 9).
|
Print a single integer β the number of good phone numbers of length n modulo 109 + 7.
|
In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
|
Input: 6 238 56 497 3 4 | Output: 8
|
Medium
| 2 | 1,037 | 460 | 85 | 5 |
1,917 |
F
|
1917F
|
F. Construct Tree
| 2,500 |
bitmasks; constructive algorithms; dp; trees
|
You are given an array of integers \(l_1, l_2, \dots, l_n\) and an integer \(d\). Is it possible to construct a tree satisfying the following three conditions? The tree contains \(n + 1\) nodes. The length of the \(i\)-th edge is equal to \(l_i\). The (weighted) diameter of the tree is equal to \(d\).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 250\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(d\) (\(2 \leq n \leq 2000, 1 \leq d \leq 2000\)).The second line of each test case contains \(n\) integers \(l_1, l_2, \dots, l_n\) (\(1 \leq l_i \leq d\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\).
|
For each test case, output \(\texttt{Yes}\) if it is possible to construct a tree that satisfies all the conditions, and \(\texttt{No}\) otherwise.You can print the letters in any case (upper or lower).
|
Below, you are given the illustrations of trees for the first and third test cases. One of the diameters is highlighted by coloring its edges in red.
|
Input: 34 101 2 3 44 71 4 3 46 182 4 3 7 6 7 | Output: Yes No Yes
|
Expert
| 4 | 302 | 488 | 202 | 19 |
1,375 |
H
|
1375H
|
H. Set Merging
| 3,300 |
constructive algorithms; divide and conquer
|
You are given a permutation \(a_1, a_2, \dots, a_n\) of numbers from \(1\) to \(n\). Also, you have \(n\) sets \(S_1,S_2,\dots, S_n\), where \(S_i=\{a_i\}\). Lastly, you have a variable \(cnt\), representing the current number of sets. Initially, \(cnt = n\).We define two kinds of functions on sets:\(f(S)=\min\limits_{u\in S} u\);\(g(S)=\max\limits_{u\in S} u\).You can obtain a new set by merging two sets \(A\) and \(B\), if they satisfy \(g(A)<f(B)\) (Notice that the old sets do not disappear).Formally, you can perform the following sequence of operations:\(cnt\gets cnt+1\);\(S_{cnt}=S_u\cup S_v\), you are free to choose \(u\) and \(v\) for which \(1\le u, v < cnt\) and which satisfy \(g(S_u)<f(S_v)\).You are required to obtain some specific sets.There are \(q\) requirements, each of which contains two integers \(l_i\),\(r_i\), which means that there must exist a set \(S_{k_i}\) (\(k_i\) is the ID of the set, you should determine it) which equals \(\{a_u\mid l_i\leq u\leq r_i\}\), which is, the set consisting of all \(a_i\) with indices between \(l_i\) and \(r_i\).In the end you must ensure that \(cnt\leq 2.2\times 10^6\). Note that you don't have to minimize \(cnt\). It is guaranteed that a solution under given constraints exists.
|
The first line contains two integers \(n,q\) \((1\leq n \leq 2^{12},1 \leq q \leq 2^{16})\) β the length of the permutation and the number of needed sets correspondently.The next line consists of \(n\) integers \(a_1,a_2,\cdots, a_n\) (\(1\leq a_i\leq n\), \(a_i\) are pairwise distinct) β given permutation.\(i\)-th of the next \(q\) lines contains two integers \(l_i,r_i\) \((1\leq l_i\leq r_i\leq n)\), describing a requirement of the \(i\)-th set.
|
It is guaranteed that a solution under given constraints exists.The first line should contain one integer \(cnt_E\) \((n\leq cnt_E\leq 2.2\times 10^6)\), representing the number of sets after all operations.\(cnt_E-n\) lines must follow, each line should contain two integers \(u\), \(v\) (\(1\leq u, v\leq cnt'\), where \(cnt'\) is the value of \(cnt\) before this operation), meaning that you choose \(S_u\), \(S_v\) and perform a merging operation. In an operation, \(g(S_u)<f(S_v)\) must be satisfied.The last line should contain \(q\) integers \(k_1,k_2,\cdots,k_q\) \((1\leq k_i\leq cnt_E)\), representing that set \(S_{k_i}\) is the \(i\)th required set.Please notice the large amount of output.
|
In the first sample:We have \(S_1=\{1\},S_2=\{3\},S_3=\{2\}\) initially.In the first operation, because \(g(S_3)=2<f(S_2)=3\), we can merge \(S_3,S_2\) into \(S_4=\{2,3\}\).In the second operation, because \(g(S_1)=1<f(S_3)=2\), we can merge \(S_1,S_3\) into \(S_5=\{1,2\}\).In the third operation, because \(g(S_5)=2<f(S_2)=3\), we can merge \(S_5,S_2\) into \(S_6=\{1,2,3\}\).For the first requirement, \(S_4=\{2,3\}=\{a_2,a_3\}\), satisfies it, thus \(k_1=4\).For the second requirement, \(S_6=\{1,2,3\}=\{a_1,a_2,a_3\}\), satisfies it, thus \(k_2=6\)Notice that unused sets, identical sets, outputting the same set multiple times, and using sets that are present initially are all allowed.
|
Input: 3 2 1 3 2 2 3 1 3 | Output: 6 3 2 1 3 5 2 4 6
|
Master
| 2 | 1,252 | 451 | 702 | 13 |
1,799 |
E
|
1799E
|
E. City Union
| 2,300 |
constructive algorithms; dfs and similar; dsu; geometry; greedy; implementation; math
|
You are given \(n \times m\) grid. Some cells are filled and some are empty.A city is a maximal (by inclusion) set of filled cells such that it is possible to get from any cell in the set to any other cell in the set by moving to adjacent (by side) cells, without moving into any cells not in the set. In other words, a city is a connected component of filled cells with edges between adjacent (by side) cells.Initially, there are two cities on the grid. You want to change some empty cells into filled cells so that both of the following are satisfied: There is one city on the resulting grid. The shortest path between any two filled cells, achievable only by moving onto filled cells, is equal to the Manhattan distance between them. The Manhattan distance between two cells \((a, b)\) and \((c, d)\) is equal to \(|a - c| + |b - d|\).Find a way to add filled cells that satisfies these conditions and minimizes the total number of filled cells.
|
Input consists of multiple test cases. The first line contains a single integer \(t\), the number of test cases (\(1 \le t \le 5000\)).The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 50\), \(nm \geq 3\)).The next \(n\) lines describe the grid. The \(i\)-th line contains a string \(s_i\) of length \(m\). \(s_{i,j}\) is '#' if the cell in position \((i, j)\) is filled, and '.' if it is empty.It is guaranteed that there are exactly two cities in the initial grid.It is guaranteed that the sum of \(n\cdot m\) over all test cases does not exceed \(25\,000\).
|
For each test case, output \(n\) lines, each containing a string of length \(m\), describing the grid you create in the same format as the input.If there are multiple possible answers with the minimum number of filled cells print any.
|
In the first test case, we can add a single filled cell between the two cities to connect them. We can verify that the second condition is satisfied.In the second test case, we can also connect the cities with a single filled cell, while satisfying the second condition. In the third test case, note that if we filled the 3 cells in the top left, the cities would be connected, but the second condition would not be satisfied for cells \((4, 2)\) and \((2, 4)\).
|
Input: 111 3#.#2 2.##.4 4..##...##...##..6 6.##...##..............##.....#...###6 5.#..#.#..#.#..#.#.##.#...##...5 5######...##.#.##...######4 4.##.##.##.##.##.5 5..###....#.....#....#....5 6.##...##....#....#....##...##.6 5..##....##....##....##....##..5 4..##..#...#.#...#... | Output: ### .# ## ..## ..## ###. ##.. .##... ###... ..#... ..#### ...### ...### .#### .#### .#### .#### .#... ##... ##### ##### ##### ##### ##### .##. #### #### .##. ..### ..### ..#.. ###.. #.... .##... ###... ###### ...### ...##. ..##. ..### ..### ###.. ###.. .##.. ..## ..#. ..#. ###. #...
|
Expert
| 7 | 948 | 599 | 234 | 17 |
1,510 |
J
|
1510J
|
J. Japanese Game
| 2,700 |
constructive algorithms; math
|
Joseph really likes the culture of Japan. Last year he learned Japanese traditional clothes and visual arts and now he is trying to find out the secret of the Japanese game called Nonogram.In the one-dimensional version of the game, there is a row of \(n\) empty cells, some of which are to be filled with a pen. There is a description of a solution called a profile β a sequence of positive integers denoting the lengths of consecutive sets of filled cells. For example, the profile of \([4, 3, 1]\) means that there are sets of four, three, and one filled cell, in that order, with at least one empty cell between successive sets. A suitable solution for \(n = 12\) and \(p = [4, 3, 1]\). A wrong solution: the first four filled cells should be consecutive. A wrong solution: there should be at least one empty cell before the last filled cell. Joseph found out that for some numbers \(n\) and profiles \(p\) there are lots of ways to fill the cells to satisfy the profile. Now he is in the process of solving a nonogram consisting of \(n\) cells and a profile \(p\). He has already created a mask of \(p\) β he has filled all the cells that must be filled in every solution of the nonogram. The mask for \(n = 12\) and \(p = [4, 3, 1]\): all the filled cells above are filled in every solution. After a break, he lost the source profile \(p\). He only has \(n\) and the mask \(m\). Help Joseph find any profile \(p'\) with the mask \(m\) or say that there is no such profile and Joseph has made a mistake.
|
The only line contains a string \(m\) β the mask of the source profile \(p\). The length of \(m\) is \(n\) (\(1 \le n \le 100\,000\)). The string \(m\) consists of symbols # and _ β denoting filled and empty cells respectively.
|
If there is no profile with the mask \(m\), output the number \(-1\). Otherwise, on the first line, output an integer \(k\) β the number of integers in the profile \(p'\). On the second line, output \(k\) integers of the profile \(p'\).
|
Input: __#_____ | Output: 2 3 2
|
Master
| 2 | 1,508 | 227 | 236 | 15 |
|
581 |
C
|
581C
|
C. Developing Skills
| 1,400 |
implementation; math; sortings
|
Petya loves computer games. Finally a game that he's been waiting for so long came out!The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is calculated as the sum of the values ββof for all i from 1 to n. The expression β xβ denotes the result of rounding the number x down to the nearest integer.At the beginning of the game Petya got k improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if a4 = 46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused.Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
|
The first line of the input contains two positive integers n and k (1 β€ n β€ 105, 0 β€ k β€ 107) β the number of skills of the character and the number of units of improvements at Petya's disposal.The second line of the input contains a sequence of n integers ai (0 β€ ai β€ 100), where ai characterizes the level of the i-th skill of the character.
|
The first line of the output should contain a single non-negative integer β the maximum total rating of the character that Petya can get using k or less improvement units.
|
In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to lfloor frac{100}{10} rfloor + lfloor frac{100}{10} rfloor = 10 + 10 = 20.In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is .In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
|
Input: 2 47 9 | Output: 2
|
Easy
| 3 | 1,097 | 344 | 171 | 5 |
2,119 |
B
|
2119B
|
B. Line Segments
| 1,200 |
geometry; greedy; math
|
PIKASONIC - Lost My Mind (feat.nakotanmaru) You are given two points \((p_x,p_y)\) and \((q_x,q_y)\) on a Euclidean plane.You start at the starting point \((p_x,p_y)\) and will perform \(n\) operations. In the \(i\)-th operation, you must choose any point such that the Euclidean distance\(^{\text{β}}\) between your current position and the point is exactly \(a_i\), and then move to that point.Determine whether it is possible to reach the terminal point \((q_x,q_y)\) after performing all operations.\(^{\text{β}}\)The Euclidean distance between \((x_1, y_1)\) and \((x_2, y_2)\) is \(\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}\)
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1\leq n \leq 10^3\)) β the length of the sequence \(a\).The second line of each test case contains four integers \(p_x,p_y,q_x,q_y\) (\(1\leq p_x,p_y,q_x,q_y\leq 10^7\)) β the coordinates of the starting point and terminal point.The third line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 10^4\)) β the distance to move in each operation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output ""Yes"" if it is possible to reach the terminal point \((q_x,q_y)\) after all operations; otherwise, output ""No"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
Here is a picture that shows a possible movement of the first test case. The coordinates of point \(r_1\) are \((3,1+\sqrt{5})\). The first test case. Here is a picture that shows a possible movement of the second test case. The coordinates of point \(r_1\) are \((1+\sqrt{3},0)\), and the coordinates of point \(r_2\) are \(\left(-\frac{(\sqrt{3}+4)(3\sqrt{3(149-24\sqrt{3})}-7\sqrt{3}-38)}{104},-\frac{\sqrt{3(1331-764\sqrt{3})}+12\sqrt{3}-27}{104}\right)\). The second test case. For the third test case, it can be shown that there is no set of moves that satisfies all requirements.Here is a picture that shows a possible movement of the fourth test case. The fourth test case.
|
Input: 521 1 5 13 331 1 3 32 3 42100 100 100 1004 515 1 1 45210000000 10000000 10000000 1000000010000 10000 | Output: Yes Yes No Yes Yes
|
Easy
| 3 | 626 | 696 | 303 | 21 |
855 |
F
|
855F
|
F. Nagini
| 3,100 |
binary search; data structures
|
Nagini, being a horcrux You-know-who created with the murder of Bertha Jorkins, has accumulated its army of snakes and is launching an attack on Hogwarts school. Hogwarts' entrance can be imagined as a straight line (x-axis) from 1 to 105. Nagini is launching various snakes at the Hogwarts entrance. Each snake lands parallel to the entrance, covering a segment at a distance k from x = l to x = r. Formally, each snake can be imagined as being a line segment between points (l, k) and (r, k). Note that k can be both positive and negative, but not 0.Let, at some x-coordinate x = i, there be snakes at point (i, y1) and point (i, y2), such that y1 > 0 and y2 < 0. Then, if for any point (i, y3) containing a snake such that y3 > 0, y1 β€ y3 holds and for any point (i, y4) containing a snake such that y4 < 0, |y2| β€ |y4| holds, then the danger value at coordinate x = i is y1 + |y2|. If no such y1 and y2 exist, danger value is 0. Harry wants to calculate the danger value of various segments of the Hogwarts entrance. Danger value for a segment [l, r) of the entrance can be calculated by taking the sum of danger values for each integer x-coordinate present in the segment.Formally, you have to implement two types of queries: 1 l r k: a snake is added parallel to entrance from x = l to x = r at y-coordinate y = k (l inclusive, r exclusive). 2 l r: you have to calculate the danger value of segment l to r (l inclusive, r exclusive).
|
First line of input contains a single integer q (1 β€ q β€ 5Β·104) denoting the number of queries.Next q lines each describe a query. Each query description first contains the query type typei (1 β€ typei β€ 2). This is followed by further description of the query. In case of the type being 1, it is followed by integers li, ri and ki (, - 109 β€ ki β€ 109, k β 0). Otherwise, it just contains two integers, li and ri (1 β€ li < ri β€ 105).
|
Output the answer for each query of type 2 in a separate line.
|
In the first sample case, the danger value for x-coordinates 1 is 0 as there is no y2 satisfying the above condition for x = 1.Danger values for x-coordinates 2 and 3 is 10 + | - 7| = 17.Danger values for x-coordinates 4 to 9 is again 0 as there is no y2 satisfying the above condition for these coordinates.Thus, total danger value is 17 + 17 = 34.
|
Input: 31 1 10 101 2 4 -72 1 10 | Output: 34
|
Master
| 2 | 1,439 | 432 | 62 | 8 |
1,153 |
C
|
1153C
|
C. Serval and Parenthesis Sequence
| 1,700 |
greedy; strings
|
Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School.In his favorite math class, the teacher taught him the following interesting definitions.A parenthesis sequence is a string, containing only characters ""("" and "")"".A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters ""1"" and ""+"" between the original characters of the sequence. For example, parenthesis sequences ""()()"", ""(())"" are correct (the resulting expressions are: ""(1+1)+(1+1)"", ""((1+1)+1)""), while "")("" and "")"" are not. Note that the empty string is a correct parenthesis sequence by definition.We define that \(|s|\) as the length of string \(s\). A strict prefix \(s[1\dots l]\) \((1\leq l< |s|)\) of a string \(s = s_1s_2\dots s_{|s|}\) is string \(s_1s_2\dots s_l\). Note that the empty string and the whole string are not strict prefixes of any string by the definition.Having learned these definitions, he comes up with a new problem. He writes down a string \(s\) containing only characters ""("", "")"" and ""?"". And what he is going to do, is to replace each of the ""?"" in \(s\) independently by one of ""("" and "")"" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence.After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable.
|
The first line contains a single integer \(|s|\) (\(1\leq |s|\leq 3 \cdot 10^5\)), the length of the string.The second line contains a string \(s\), containing only ""("", "")"" and ""?"".
|
A single line contains a string representing the answer.If there are many solutions, any of them is acceptable.If there is no answer, print a single line containing "":("" (without the quotes).
|
It can be proved that there is no solution for the second sample, so print "":("".
|
Input: 6 (????? | Output: (()())
|
Medium
| 2 | 1,606 | 188 | 193 | 11 |
1,949 |
J
|
1949J
|
J. Amanda the Amoeba
| 2,600 |
graphs; implementation; trees; two pointers
|
This problem has an attachment. You can use it to simulate and visualize the movements of the amoeba.Amoeba Amanda lives inside a rectangular grid of square pixels. Her body occupies some of these pixels. Other pixels may be either free or blocked. Amanda moves across the grid using the so-called amoeboid movement. In each step of such a movement, her body first shrinks by one pixel (one pixel of the body is removed and becomes free), and then grows at a different place (one previously-free pixel is added to the body).To prevent structural damage, Amanda's body always occupies a connected region of pixels, which means that any pair of pixels forming the body can be connected by a sequence of adjacent pixels without ever leaving the body. Two pixels are considered adjacent if they share a common side (each pixel has at most 4 neighbours). The body remains connected even during the movement, including the moment after removing a pixel and before adding another one.Your task is to help Amanda find her way around. Given her initial position and desired final position, suggest a sequence of valid moves leading from the former to the latter. Illustration of sample \(1\): The filled shape is the initial position, the dotted region is the final position.
|
The first line contains two integers \(r\) and \(c\) (\(1\le r,c \le 50\)) β the size of the rectangular grid in pixels.The next \(r\) lines contain \(c\) characters each, describing the initial position of Amanda. Each of those characters is either a dot \(\texttt{.}\) denoting a free pixel, an asterisk \(\texttt{*}\) denoting Amanda's body, or an \(\texttt{X}\) denoting a blocked pixel which may never be occupied.The next line is empty.The next \(r\) lines describe the desired final position in the same format as the initial position.It is guaranteed that: The number of pixels forming Amanda's body is the same in both positions, and it is at least 2. The body of Amanda is connected in the initial position. The body of Amanda is connected in the final position. The blocked pixels do not change between the descriptions of the initial and final position, their placement is exactly the same in both positions.
|
Print \(\texttt{YES}\) if it is possible for Amanda to go from the initial position to the final one. Otherwise, print \(\texttt{NO}\).If it is possible, on the next line print one integer \(m\) (\(0\le m\le 10\,000\)) β the number of moves to execute.The following \(m\) lines must contain four integer coordinates each: \(i_1\), \(j_1\), \(i_2\), \(j_2\) (\(1\le i_1,i_2\le r\), \(1\le j_1,j_2\le c\)). These four coordinates specify one move, meaning that the pixel at \(i_1\)-th row and \(j_1\)-th column is first removed from the body. Then, \((i_2,j_2)\) must designate a different location where one pixel is added.The sequence should consist only of valid moves and after the last move, Amanda's body should occupy the desired final position.If there are multiple solutions, print any of them.Under the assumptions of this problem, it can be proven that if it is possible for Amanda to go from the initial position to the desired final one, then it is possible to do it with at most \(10\,000\) moves.
|
In the first sample, Amanda executes 5 moves to reach the final position, as shown in the figure below.
|
Input: 5 8.******.**.X**..*******.**.X**...******..******....X****.*******...X****.******. | Output: YES 5 3 1 3 8 2 1 2 8 4 1 4 8 2 2 4 7 4 2 2 7
|
Expert
| 4 | 1,266 | 920 | 1,009 | 19 |
1,809 |
F
|
1809F
|
F. Traveling in Berland
| 2,500 |
binary search; data structures; graphs; greedy; implementation
|
There are \(n\) cities in Berland, arranged in a circle and numbered from \(1\) to \(n\) in clockwise order.You want to travel all over Berland, starting in some city, visiting all the other cities and returning to the starting city. Unfortunately, you can only drive along the Berland Ring Highway, which connects all \(n\) cities. The road was designed by a very titled and respectable minister, so it is one-directional β it can only be traversed clockwise, only from the city \(i\) to the city \((i \bmod n) + 1\) (i.e. from \(1\) to \(2\), from \(2\) in \(3\), ..., from \(n\) to \(1\)).The fuel tank of your car holds up to \(k\) liters of fuel. To drive from the \(i\)-th city to the next one, \(a_i\) liters of fuel are needed (and are consumed in the process).Every city has a fuel station; a liter of fuel in the \(i\)-th city costs \(b_i\) burles. Refueling between cities is not allowed; if fuel has run out between cities, then your journey is considered incomplete.For each city, calculate the minimum cost of the journey if you start and finish it in that city.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(3 \le n \le 2 \cdot 10^5\); \(1 \le k \le 10^9\)) β the number of cities and the volume of fuel tank, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le k\)).The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 2\)).The sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print \(n\) integers, where the \(i\)-th of them is equal to the minimum cost of the journey if you start and finish in the \(i\)-th city.
|
Input: 43 53 4 41 2 25 71 3 2 5 12 1 1 1 24 31 2 1 32 2 2 23 22 2 21 2 1 | Output: 17 19 17 13 12 12 12 14 14 14 14 14 8 8 8
|
Expert
| 5 | 1,076 | 528 | 158 | 18 |
|
282 |
D
|
282D
|
D. Yet Another Number Game
| 2,100 |
dp; games
|
Since most contestants do not read this part, I have to repeat that Bitlandians are quite weird. They have their own jobs, their own working method, their own lives, their own sausages and their own games!Since you are so curious about Bitland, I'll give you the chance of peeking at one of these games.BitLGM and BitAryo are playing yet another of their crazy-looking genius-needed Bitlandish games. They've got a sequence of n non-negative integers a1, a2, ..., an. The players make moves in turns. BitLGM moves first. Each player can and must do one of the two following actions in his turn: Take one of the integers (we'll denote it as ai). Choose integer x (1 β€ x β€ ai). And then decrease ai by x, that is, apply assignment: ai = ai - x. Choose integer x . And then decrease all ai by x, that is, apply assignment: ai = ai - x, for all i. The player who cannot make a move loses.You're given the initial sequence a1, a2, ..., an. Determine who wins, if both players plays optimally well and if BitLGM and BitAryo start playing the described game in this sequence.
|
The first line contains an integer n (1 β€ n β€ 3).The next line contains n integers a1, a2, ..., an (0 β€ ai < 300).
|
Write the name of the winner (provided that both players play optimally well). Either ""BitLGM"" or ""BitAryo"" (without the quotes).
|
Input: 21 1 | Output: BitLGM
|
Hard
| 2 | 1,068 | 114 | 133 | 2 |
|
1,859 |
C
|
1859C
|
C. Another Permutation Problem
| 1,200 |
brute force; dp; greedy; math
|
Andrey is just starting to come up with problems, and it's difficult for him. That's why he came up with a strange problem about permutations\(^{\dagger}\) and asks you to solve it. Can you do it?Let's call the cost of a permutation \(p\) of length \(n\) the value of the expression: \((\sum_{i = 1}^{n} p_i \cdot i) - (\max_{j = 1}^{n} p_j \cdot j)\). Find the maximum cost among all permutations of length \(n\).\(^{\dagger}\)A permutation of length \(n\) 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).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 30\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer \(n\) (\(2 \le n \le 250\)) β the length of the permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(500\).
|
For each test case, output a single integer β the maximum cost among all permutations of length \(n\).
|
In the first test case, the permutation with the maximum cost is \([2, 1]\). The cost is equal to \(2 \cdot 1 + 1 \cdot 2 - \max (2 \cdot 1, 1 \cdot 2)= 2 + 2 - 2 = 2\).In the second test case, the permutation with the maximum cost is \([1, 2, 4, 3]\). The cost is equal to \(1 \cdot 1 + 2 \cdot 2 + 4 \cdot 3 + 3 \cdot 4 - 4 \cdot 3 = 17\).
|
Input: 52431020 | Output: 2 17 7 303 2529
|
Easy
| 4 | 752 | 382 | 102 | 18 |
796 |
C
|
796C
|
C. Bank Hacking
| 1,900 |
constructive algorithms; data structures; dp; trees
|
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength ai.Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met: Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. The strength of bank x is less than or equal to the strength of Inzane's computer. Determine the minimum strength of the computer Inzane needs to hack all the banks.
|
The first line contains one integer n (1 β€ n β€ 3Β·105) β the total number of banks.The second line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the strengths of the banks.Each of the next n - 1 lines contains two integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β meaning that there is a wire directly connecting banks ui and vi.It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
|
Print one integer β the minimum strength of the computer Inzane needs to accomplish the goal.
|
In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1, 2, 3, 4, 5]. He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ]. He hacks bank 2, then strengths of the banks become [3, - , - , - , - ]. He completes his goal by hacking bank 1. In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93.
|
Input: 51 2 3 4 51 22 33 44 5 | Output: 5
|
Hard
| 4 | 1,469 | 487 | 93 | 7 |
2,123 |
B
|
2123B
|
B. Tournament
| 800 |
greedy
|
You are given an array of integers \(a_1,a_2,\dots,a_n\). A tournament is held with \(n\) players. Player \(i\) has strength \(a_i\).While more than \(k\) players remain, Two remaining players are chosen at random; Then the chosen player with the lower strength is eliminated. If the chosen players have the same strength, one is eliminated at random. Given integers \(j\) and \(k\) (\(1 \leq j,k \leq n\)), determine if there is any way for player \(j\) to be one of the last \(k\) remaining players.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains three integers \(n\), \(j\), and \(k\) (\(2\leq n \leq 2\cdot 10^5\), \(1\leq j, k\leq n\)).The second line of each test case contains \(n\) integers, \(a_1,a_2,\dots,a_n\) (\(1\leq a_i\leq n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output on a single line ""YES"" if player \(j\) can be one of the last \(k\) remaining players, and ""NO"" otherwise. You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first sample, suppose that players \(2\) and \(5\) are chosen. Then player \(2\) defeats player \(5\). Now, the remaining player strengths are 3244 Next, suppose that players \(3\) and \(4\) are chosen. Then player \(3\) might defeat player \(4\). Now, the remaining player strengths are 324 Player \(2\) is one of the last three players remaining.In the third sample, it can be shown that there is no way for player \(1\) to be the last player remaining.
|
Input: 35 2 33 2 4 4 15 4 15 3 4 5 26 1 11 2 3 4 5 6 | Output: YES YES NO
|
Beginner
| 1 | 501 | 421 | 299 | 21 |
2,087 |
E
|
2087E
|
E. Color the Arrows
| 0 |
*special; *special; dp; dp
|
There are \(n\) arrows drawn in a row on a strip of paper, numbered from \(1\) to \(n\). Each arrow points either to the left or to the right. Initially, all arrows are painted blue.In one operation, you can repaint a blue arrow into red. For the first operation, you can choose any arrow. For each subsequent operation, you are only allowed to choose such an arrow that the arrow repainted on the previous operation points in its direction. That is, if the arrow repainted in the previous operation is a left arrow, then, for the current operation, you have to choose an arrow at a smaller index than the previous one. Similarly, if the previous arrow was a right arrow, the current one has to be at a greater index than it. Note that the arrows don't have to be adjacent.Each arrow has an integer reward for repainting it into red (which can be negative, positive, or zero).The final score is the sum of the rewards for the repainted arrows. What is the maximum score that can be achieved if you are allowed to perform any number of operations (including zero)?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.In the first line of each test case, there is a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of arrows.In the second line, there is a string consisting of \(n\) characters '<' and/or '>' (ASCII codes 60 and 62, respectively).In the third line, there are \(n\) integers \(c_1, c_2, \dots, c_n\) (\(-10^9 \le c_i \le 10^9\)) β the reward for repainting each arrow.An additional constraint on the input: the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, print a single integer β the maximum score that can be achieved if you are allowed to perform any number of operations (including zero).
|
In the first test case, you can first repaint the arrow at index \(2\). This arrow points to the right, so in the next operation, you have to choose an arrow to the right of it. Let's repaint the arrow at index \(3\). This arrow also points to the right, so no more arrows can be repainted. The answer is \(c_2 + c_3 = 4 + 6 = 10\).In the second test case, you can repaint arrows \(3\), then \(1\).In the third test case, it is most optimal to not repaint any arrows. This will yield an answer of \(0\), while repainting at least one arrow would lead to a negative answer.In the fourth test case, you can repaint the arrows in the following order: \(3, 7, 1, 5\).In the fifth test case, you can repaint the arrows in the following order: \(4, 3, 2, 1, 5\).
|
Input: 53<>>5 4 65<><>>5 -2 4 -3 72>>-1 -28>>>><<<<1 -1 1 -1 1 -1 1 -15><<<>-1 100 100 100 100 | Output: 10 9 0 4 399
|
Beginner
| 4 | 1,063 | 588 | 156 | 20 |
1,505 |
F
|
1505F
|
F. Math
| 2,200 |
*special; math
|
*The two images are equivalent, feel free to use either one.
|
The input contains a single integer \(a\) (\(-100 \le a \le 100\)).
|
Output the result β an integer number.
|
Input: 1 | Output: 1
|
Hard
| 2 | 60 | 67 | 38 | 15 |
|
1,794 |
B
|
1794B
|
B. Not Dividing
| 900 |
constructive algorithms; greedy; math
|
You are given an array of \(n\) positive integers \(a_1, a_2, \ldots, a_n\). In one operation, you can choose any number of the array and add \(1\) to it. Make at most \(2n\) operations so that the array satisfies the following property: \(a_{i+1}\) is not divisible by \(a_i\), for each \(i = 1, 2, \ldots, n-1\). You do not need to minimize the number of operations.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1\le n\le 10^4\)) β the length of the given array. The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\le a_i\leq 10^9\)) β the given array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5\cdot 10^4\).
|
For each test case, print the answer on a separate line. In the only line, print \(n\) integers β the resulting array \(a\) after applying at most \(2n\) operations. We can show that an answer always exists under the given constraints. If there are multiple answers, print any of them.
|
In the first test case, the array \([4, 5, 6, 7]\) can be achieved by applying \(2\) operations to the first element, \(1\) operation to the second element, \(3\) operations to the third element, and \(1\) operation to the last element. The total number of operations performed is \(7\), which is less than the allowed \(8\) operations in this case.In the second test case, the array \([3, 2, 3]\) can be achieved by applying two operations to the first element. Another possible resulting array could be \([2, 3, 5]\), because the total number of operations does not need to be minimum.In the third test case, not applying any operations results in an array that satisfies the statement's property. Observe that it is not mandatory to make operations.
|
Input: 342 4 3 631 2 324 2 | Output: 4 5 6 7 3 2 3 4 2
|
Beginner
| 3 | 368 | 488 | 285 | 17 |
2,005 |
E1
|
2005E1
|
E1. Subtangle Game (Easy Version)
| 2,100 |
dp; games; greedy; implementation
|
This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved.Tsovak and Narek are playing a game. They have an array \(a\) and a matrix \(b\) of integers with \(n\) rows and \(m\) columns, numbered from \(1\). The cell in the \(i\)-th row and the \(j\)-th column is \((i, j)\).They are looking for the elements of \(a\) in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of \(a\) (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell \((r, c)\). The next player has to choose his cell in the submatrix starting at \((r + 1, c + 1)\) and ending in \((n, m)\) (the submatrix can be empty if \(r=n\) or \(c=m\)). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses.Your task is to determine the winner if the players play optimally.
|
The first line of the input contains \(t\) (\(1 \le t \le 300\)) β the number of test cases.The first line of each test case contains three integers \(l\), \(n\), and \(m\) (\(1 \le l, n, m \le 300\)) β the size of the array and the sizes of the matrix.The second line contains \(l\) integers \(a_1, a_2, a_3, \ldots a_l\) (\(1 \le a_i \le \min(7, n \cdot m)\)) β the elements of the array \(a\).The \(i\)-th of the last \(n\) lines contains \(m\) integers \(b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}\) (\(1 \le b_{i,j} \le \min(7, n \cdot m)\)) β representing the \(i\)-th row of the matrix.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(10^5\).It is guaranteed that the sum of \(l\) over all test cases does not exceed \(300\).
|
You should output \(t\) lines, the \(i\)-th of them containing a character representing the answer of the \(i\)-th test case: ""T"" if Tsovak wins or ""N"", otherwise (without quotes).
|
In the first example, Tsovak starts by looking for \(1\). There is only one occurrence of \(1\) at \((1,1)\), so he chooses it. Then Narek needs to look for \(2\) in the submatrix of \((2, 2)\), which consists of just the last two elements: \(5\) and \(2\). He chooses \(2\), and then Tsovak loses since the array has ended.In the second example, Tsovak needs to choose \(1\). There is a \(1\) at the cell \((n,m)\), so he chooses that cell. Then, since the submatrix of \((n + 1, m + 1)\) is empty, Narek cannot find \(2\), so he loses.
|
Input: 32 2 31 21 3 54 5 22 2 41 21 1 3 24 2 5 12 4 21 23 45 55 55 5 | Output: N T N
|
Hard
| 4 | 1,079 | 767 | 184 | 20 |
526 |
A
|
526A
|
A. King of Thieves
| 1,300 |
brute force; implementation
|
In this problem you will meet the simplified model of game King of Thieves.In a new ZeptoLab game called ""King of Thieves"" your aim is to reach a chest with gold by controlling your character, avoiding traps and obstacles on your way. An interesting feature of the game is that you can design your own levels that will be available to other players. Let's consider the following simple design of a level.A dungeon consists of n segments located at a same vertical level, each segment is either a platform that character can stand on, or a pit with a trap that makes player lose if he falls into it. All segments have the same length, platforms on the scheme of the level are represented as '*' and pits are represented as '.'. One of things that affects speedrun characteristics of the level is a possibility to perform a series of consecutive jumps of the same length. More formally, when the character is on the platform number i1, he can make a sequence of jumps through the platforms i1 < i2 < ... < ik, if i2 - i1 = i3 - i2 = ... = ik - ik - 1. Of course, all segments i1, i2, ... ik should be exactly the platforms, not pits. Let's call a level to be good if you can perform a sequence of four jumps of the same length or in the other words there must be a sequence i1, i2, ..., i5, consisting of five platforms so that the intervals between consecutive platforms are of the same length. Given the scheme of the level, check if it is good.
|
The first line contains integer n (1 β€ n β€ 100) β the number of segments on the level.Next line contains the scheme of the level represented as a string of n characters '*' and '.'.
|
If the level is good, print the word ""yes"" (without the quotes), otherwise print the word ""no"" (without the quotes).
|
In the first sample test you may perform a sequence of jumps through platforms 2, 5, 8, 11, 14.
|
Input: 16.**.*..*.***.**. | Output: yes
|
Easy
| 2 | 1,447 | 181 | 120 | 5 |
1,795 |
D
|
1795D
|
D. Triangle Coloring
| 1,600 |
combinatorics; math
|
You are given an undirected graph consisting of \(n\) vertices and \(n\) edges, where \(n\) is divisible by \(6\). Each edge has a weight, which is a positive (greater than zero) integer.The graph has the following structure: it is split into \(\frac{n}{3}\) triples of vertices, the first triple consisting of vertices \(1, 2, 3\), the second triple consisting of vertices \(4, 5, 6\), and so on. Every pair of vertices from the same triple is connected by an edge. There are no edges between vertices from different triples.You have to paint the vertices of this graph into two colors, red and blue. Each vertex should have exactly one color, there should be exactly \(\frac{n}{2}\) red vertices and \(\frac{n}{2}\) blue vertices. The coloring is called valid if it meets these constraints.The weight of the coloring is the sum of weights of edges connecting two vertices with different colors.Let \(W\) be the maximum possible weight of a valid coloring. Calculate the number of valid colorings with weight \(W\), and print it modulo \(998244353\).
|
The first line contains one integer \(n\) (\(6 \le n \le 3 \cdot 10^5\), \(n\) is divisible by \(6\)).The second line contains \(n\) integers \(w_1, w_2, \dots, w_n\) (\(1 \le w_i \le 1000\)) β the weights of the edges. Edge \(1\) connects vertices \(1\) and \(2\), edge \(2\) connects vertices \(1\) and \(3\), edge \(3\) connects vertices \(2\) and \(3\), edge \(4\) connects vertices \(4\) and \(5\), edge \(5\) connects vertices \(4\) and \(6\), edge \(6\) connects vertices \(5\) and \(6\), and so on.
|
Print one integer β the number of valid colorings with maximum possible weight, taken modulo \(998244353\).
|
The following picture describes the graph from the first example test. The maximum possible weight of a valid coloring of this graph is \(31\).
|
Input: 12 1 3 3 7 8 5 2 2 2 2 4 2 | Output: 36
|
Medium
| 2 | 1,051 | 506 | 107 | 17 |
53 |
C
|
53C
|
C. Little Frog
| 1,200 |
constructive algorithms
|
Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are n mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that he makes a route plan, to decide the order in which to jump on the mounds. Vasya can pick any mound as the first one. He thinks it boring to jump two times at the same distance. That's why he wants any two jumps on his route to have different lengths. Help Vasya the Frog and make the plan for him.
|
The single line contains a number n (1 β€ n β€ 104) which is the number of mounds.
|
Print n integers pi (1 β€ pi β€ n) which are the frog's route plan. All the pi's should be mutually different. All the |piβpi + 1|'s should be mutually different (1 β€ i β€ n - 1). If there are several solutions, output any.
|
Input: 2 | Output: 1 2
|
Easy
| 1 | 618 | 80 | 220 | 0 |
|
1,250 |
M
|
1250M
|
M. SmartGarden
| 2,500 |
constructive algorithms; divide and conquer
|
Berland Gardeners United Inc. hired you for the project called ""SmartGarden"". The main feature of this project is automatic garden watering.Formally the garden can be represented as a square of \(n \times n\) cells with rows numbered \(1\) to \(n\) from top to bottom and columns numbered \(1\) to \(n\) from left to right. Each cell of the garden contains either a plant or a slab. It's known that slabs are located on the main diagonal of the matrix representing the garden, and in the cells that are below the main diagonal and share a side with at least one cell of the main diagonal. All the remaining cells of the garden are filled with plants. Example of the garden for \(n=5\). During implementation of the project you created a smart robot that takes a list of commands as an input, which are processed one by one. Each command contains: a list of horizontal lines (rows in the matrix representing the garden); a list of vertical lines (columns in the matrix representing the garden). While executing each command robot waters only cells in the intersection of specified rows and specified columns. So, if you specify \(r\) rows and \(c\) columns, then exactly \(r \cdot c\) cells will be watered.In the demo for the customer you have tuned robot in such a way that it waters all the garden. To do that you prepared a single command containing all \(n\) rows and all \(n\) columns.Unfortunately, 5 hours before the demo for your customer it turned out that the CEO of Berland Gardeners United Inc. was going to take part in it. Moreover, most probably he will be standing on a garden slab during the demo!Now you need to create a list of commands for the robot so that it waters all the plants and doesn't water any cell containing a slab. Since it's only a beta version of ""SmartGarden"", the total number of commands shouldn't exceed \(50\).Create a program that, for a given size of the garden, will find a list of no more than \(50\) commands that allow the robot to water all the plants in the garden without watering the slabs. It is allowed to water a plant several times.
|
The first and the only line of the input contains a single integer \(n\) (\(2 \le n \le 5000\)), where \(n\) is the size of the garden.
|
In the first line print the total number of commands for the robot \(k\) (\(1 \le k \le 50\)). In the next \(2 \cdot k\) lines print all the commands. Each command should be specified by \(2\) lines. The first line of each command should describe rows in the command and the second line should describe columns in the command. Each of these \(2\) lines should have the following format: the first number of the line should specify the total number of items \(x\) in the appropriate list; then \(x\) distinct numbers follow, where each number is in the range \(1 \dots n\) and describes a chosen row for the first line and a chosen column for the second line. If there are multiple ways to water the garden, print any of them.
|
Input: 2 | Output: 2 1 1 1 2 1 1 1 2
|
Expert
| 2 | 2,091 | 135 | 725 | 12 |
|
1,812 |
E
|
1812E
|
E. Not a Geometry Problem
| 0 |
*special; *special; constructive algorithms; geometry; math
|
The only line of input contains three integers \(x\), \(y\), \(z\) (\(-1000 \le x, y, z \le 1000\)).
|
Output one real number β the answer.Your answer is considered correct if its absolute or relative error does not exceed \(10^6\). Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a-b|}{\max(1,|b|)} \le 10^6\).
|
Input: 1 1 1 | Output: 1.7320508075688772
|
Beginner
| 5 | 0 | 100 | 276 | 18 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.