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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
424 |
A
|
424A
|
A. Squats
| 900 |
implementation
|
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.For another exercise, Pasha needs exactly hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
|
The first line contains integer n (2 β€ n β€ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
|
In the first line, print a single integer β the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
|
Input: 4xxXx | Output: 1XxXx
|
Beginner
| 1 | 414 | 263 | 256 | 4 |
|
432 |
E
|
432E
|
E. Square Tiling
| 2,300 |
constructive algorithms; greedy
|
You have an n Γ m rectangle table, its cells are not initially painted. Your task is to paint all cells of the table. The resulting picture should be a tiling of the table with squares. More formally: each cell must be painted some color (the colors are marked by uppercase Latin letters); we will assume that two cells of the table are connected if they are of the same color and share a side; each connected region of the table must form a square. Given n and m, find lexicographically minimum coloring of the table that meets the described properties.
|
The first line contains two integers, n and m (1 β€ n, m β€ 100).
|
Print lexicographically minimum coloring of the table that meets the described conditions. One coloring (let's call it X) is considered lexicographically less than the other one (let's call it Y), if: consider all the table cells from left to right and from top to bottom (first, the first cell in the first row, then the second cell in the first row and so on); let's find in this order the first cell that has distinct colors in two colorings; the letter that marks the color of the cell in X, goes alphabetically before the letter that marks the color of the cell in Y.
|
Input: 1 3 | Output: ABA
|
Expert
| 2 | 554 | 63 | 572 | 4 |
|
2,021 |
C1
|
2021C1
|
C1. Adjust The Presentation (Easy Version)
| 1,300 |
constructive algorithms; greedy
|
This is the easy version of the problem. In the two versions, the constraints on \(q\) and the time limit are different. In this version, \(q=0\). You can make hacks only if all the versions of the problem are solved.A team consisting of \(n\) members, numbered from \(1\) to \(n\), is set to present a slide show at a large meeting. The slide show contains \(m\) slides.There is an array \(a\) of length \(n\). Initially, the members are standing in a line in the order of \(a_1, a_2, \ldots, a_n\) from front to back. The slide show will be presented in order from slide \(1\) to slide \(m\). Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is \([\color{red}{3},1,2,4]\). After member \(3\) presents the current slide, you can change the line of members into either \([\color{red}{3},1,2,4]\), \([1,\color{red}{3},2,4]\), \([1,2,\color{red}{3},4]\) or \([1,2,4,\color{red}{3}]\).There is also an array \(b\) of length \(m\). The slide show is considered good if it is possible to make member \(b_i\) present slide \(i\) for all \(i\) from \(1\) to \(m\) under these constraints.However, your annoying boss wants to make \(q\) updates to the array \(b\). In the \(i\)-th update, he will choose a slide \(s_i\) and a member \(t_i\) and set \(b_{s_i} := t_i\). Note that these updates are persistent, that is changes made to the array \(b\) will apply when processing future updates.For each of the \(q+1\) states of array \(b\), the initial state and after each of the \(q\) updates, determine if the slideshow is good.
|
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 three integers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 2 \cdot 10^5\); \(q=0\)) β the number of members, the number of sections and the number of updates.The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le n\)) β the initial order of the members from front to back. It is guaranteed that each integer from \(1\) to \(n\) appears exactly once in \(a\).The third line of each test case contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(1 \le b_i \le n\)) β the members who should present each section.It is guaranteed that the sum of \(n\) and the sum of \(m\) over all test cases do not exceed \(2 \cdot 10^5\) respectively.
|
For each test case, output \(q+1\) lines corresponding to the \(q+1\) states of the array \(b\). Output ""YA"" if the slide show is good, and ""TIDAK"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yA"", ""Ya"", ""ya"", and ""YA"" will be recognized as positive responses.
|
For the first test case, you do not need to move the members as both slides are presented by member \(1\), who is already at the front of the line.For the second test case, the following is a possible way to move members so that the presentation is good: \([1,2,3]\), do not move member \(1\). \([1,2,3]\), move member \(1\) after member \(3\). \([2,3,1]\), move member \(2\) after member \(3\). \([3,2,1]\), do not move member \(3\). \([3,2,1]\), move member \(3\) after member \(1\). \([2,1,3]\), do not move member \(2\).
|
Input: 34 2 01 2 3 41 13 6 01 2 31 1 2 3 3 24 6 03 1 4 23 1 1 2 3 4 | Output: YA YA TIDAK
|
Easy
| 2 | 1,750 | 876 | 319 | 20 |
1,009 |
D
|
1009D
|
D. Relatively Prime Graph
| 1,700 |
brute force; constructive algorithms; graphs; greedy; math
|
Let's call an undirected graph \(G = (V, E)\) relatively prime if and only if for each edge \((v, u) \in E\) \(GCD(v, u) = 1\) (the greatest common divisor of \(v\) and \(u\) is \(1\)). If there is no edge between some pair of vertices \(v\) and \(u\) then the value of \(GCD(v, u)\) doesn't matter. The vertices are numbered from \(1\) to \(|V|\).Construct a relatively prime graph with \(n\) vertices and \(m\) edges such that it is connected and it contains neither self-loops nor multiple edges.If there exists no valid graph with the given number of vertices and edges then output ""Impossible"".If there are multiple answers then print any of them.
|
The only line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^5\)) β the number of vertices and the number of edges.
|
If there exists no valid graph with the given number of vertices and edges then output ""Impossible"".Otherwise print the answer in the following format:The first line should contain the word ""Possible"".The \(i\)-th of the next \(m\) lines should contain the \(i\)-th edge \((v_i, u_i)\) of the resulting graph (\(1 \le v_i, u_i \le n, v_i \neq u_i\)). For each pair \((v, u)\) there can be no more pairs \((v, u)\) or \((u, v)\). The vertices are numbered from \(1\) to \(n\).If there are multiple answers then print any of them.
|
Here is the representation of the graph from the first example:
|
Input: 5 6 | Output: Possible2 53 25 13 44 15 4
|
Medium
| 5 | 654 | 127 | 532 | 10 |
1,305 |
H
|
1305H
|
H. Kuroni the Private Tutor
| 3,500 |
binary search; greedy
|
As a professional private tutor, Kuroni has to gather statistics of an exam. Kuroni has appointed you to complete this important task. You must not disappoint him.The exam consists of \(n\) questions, and \(m\) students have taken the exam. Each question was worth \(1\) point. Question \(i\) was solved by at least \(l_i\) and at most \(r_i\) students. Additionally, you know that the total score of all students is \(t\).Furthermore, you took a glance at the final ranklist of the quiz. The students were ranked from \(1\) to \(m\), where rank \(1\) has the highest score and rank \(m\) has the lowest score. Ties were broken arbitrarily.You know that the student at rank \(p_i\) had a score of \(s_i\) for \(1 \le i \le q\).You wonder if there could have been a huge tie for first place. Help Kuroni determine the maximum number of students who could have gotten as many points as the student with rank \(1\), and the maximum possible score for rank \(1\) achieving this maximum number of students.
|
The first line of input contains two integers (\(1 \le n, m \le 10^{5}\)), denoting the number of questions of the exam and the number of students respectively.The next \(n\) lines contain two integers each, with the \(i\)-th line containing \(l_{i}\) and \(r_{i}\) (\(0 \le l_{i} \le r_{i} \le m\)).The next line contains a single integer \(q\) (\(0 \le q \le m\)). The next \(q\) lines contain two integers each, denoting \(p_{i}\) and \(s_{i}\) (\(1 \le p_{i} \le m\), \(0 \le s_{i} \le n\)). It is guaranteed that all \(p_{i}\) are distinct and if \(p_{i} \le p_{j}\), then \(s_{i} \ge s_{j}\).The last line contains a single integer \(t\) (\(0 \le t \le nm\)), denoting the total score of all students.
|
Output two integers: the maximum number of students who could have gotten as many points as the student with rank \(1\), and the maximum possible score for rank \(1\) achieving this maximum number of students. If there is no valid arrangement that fits the given data, output \(-1\) \(-1\).
|
For the first sample, here is one possible arrangement that fits the data:Students \(1\) and \(2\) both solved problems \(1\) and \(2\).Student \(3\) solved problems \(2\) and \(3\).Student \(4\) solved problem \(4\).The total score of all students is \(T = 7\). Note that the scores of the students are \(2\), \(2\), \(2\) and \(1\) respectively, which satisfies the condition that the student at rank \(4\) gets exactly \(1\) point. Finally, \(3\) students tied for first with a maximum score of \(2\), and it can be proven that we cannot do better with any other arrangement.
|
Input: 5 4 2 4 2 3 1 1 0 1 0 0 1 4 1 7 | Output: 3 2
|
Master
| 2 | 1,001 | 707 | 290 | 13 |
1,541 |
B
|
1541B
|
B. Pleasant Pairs
| 1,200 |
brute force; implementation; math; number theory
|
You are given an array \(a_1, a_2, \dots, a_n\) consisting of \(n\) distinct integers. Count the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_i \cdot a_j = i + j\).
|
The first line contains one integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Then \(t\) cases follow.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 10^5\)) β the length of array \(a\).The second line of each test case contains \(n\) space separated integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 2 \cdot n\)) β the array \(a\). It is guaranteed that all elements are distinct.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_i \cdot a_j = i + j\).
|
For the first test case, the only pair that satisfies the constraints is \((1, 2)\), as \(a_1 \cdot a_2 = 1 + 2 = 3\)For the second test case, the only pair that satisfies the constraints is \((2, 3)\).For the third test case, the pairs that satisfy the constraints are \((1, 2)\), \((1, 5)\), and \((2, 3)\).
|
Input: 3 2 3 1 3 6 1 5 5 3 1 5 9 2 | Output: 1 1 3
|
Easy
| 4 | 185 | 523 | 119 | 15 |
1,455 |
A
|
1455A
|
A. Strange Functions
| 800 |
math; number theory
|
Let's define a function \(f(x)\) (\(x\) is a positive integer) as follows: write all digits of the decimal representation of \(x\) backwards, then get rid of the leading zeroes. For example, \(f(321) = 123\), \(f(120) = 21\), \(f(1000000) = 1\), \(f(111) = 111\).Let's define another function \(g(x) = \dfrac{x}{f(f(x))}\) (\(x\) is a positive integer as well).Your task is the following: for the given positive integer \(n\), calculate the number of different values of \(g(x)\) among all numbers \(x\) such that \(1 \le x \le n\).
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Each test case consists of one line containing one integer \(n\) (\(1 \le n < 10^{100}\)). This integer is given without leading zeroes.
|
For each test case, print one integer β the number of different values of the function \(g(x)\), if \(x\) can be any integer from \([1, n]\).
|
Explanations for the two first test cases of the example: if \(n = 4\), then for every integer \(x\) such that \(1 \le x \le n\), \(\dfrac{x}{f(f(x))} = 1\); if \(n = 37\), then for some integers \(x\) such that \(1 \le x \le n\), \(\dfrac{x}{f(f(x))} = 1\) (for example, if \(x = 23\), \(f(f(x)) = 23\),\(\dfrac{x}{f(f(x))} = 1\)); and for other values of \(x\), \(\dfrac{x}{f(f(x))} = 10\) (for example, if \(x = 30\), \(f(f(x)) = 3\), \(\dfrac{x}{f(f(x))} = 10\)). So, there are two different values of \(g(x)\).
|
Input: 5 4 37 998244353 1000000007 12345678901337426966631415 | Output: 1 2 9 10 26
|
Beginner
| 2 | 532 | 227 | 141 | 14 |
392 |
C
|
392C
|
C. Yet Another Number Sequence
| 0 |
combinatorics; math; matrices
|
Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recurrence relation: F1 = 1, F2 = 2, Fi = Fi - 1 + Fi - 2 (i > 2).We'll define a new number sequence Ai(k) by the formula: Ai(k) = Fi Γ ik (i β₯ 1).In this problem, your task is to calculate the following sum: A1(k) + A2(k) + ... + An(k). The answer can be very large, so print it modulo 1000000007 (109 + 7).
|
The first line contains two space-separated integers n, k (1 β€ n β€ 1017; 1 β€ k β€ 40).
|
Print a single integer β the sum of the first n elements of the sequence Ai(k) modulo 1000000007 (109 + 7).
|
Input: 1 1 | Output: 1
|
Beginner
| 3 | 391 | 85 | 107 | 3 |
|
195 |
A
|
195A
|
A. Let's Watch Football
| 1,000 |
binary search; brute force; math
|
Valeric and Valerko missed the last Euro football game, so they decided to watch the game's key moments on the Net. They want to start watching as soon as possible but the connection speed is too low. If they turn on the video right now, it will ""hang up"" as the size of data to watch per second will be more than the size of downloaded data per second.The guys want to watch the whole video without any pauses, so they have to wait some integer number of seconds for a part of the video to download. After this number of seconds passes, they can start watching. Waiting for the whole video to download isn't necessary as the video can download after the guys started to watch.Let's suppose that video's length is c seconds and Valeric and Valerko wait t seconds before the watching. Then for any moment of time t0, t β€ t0 β€ c + t, the following condition must fulfill: the size of data received in t0 seconds is not less than the size of data needed to watch t0 - t seconds of the video.Of course, the guys want to wait as little as possible, so your task is to find the minimum integer number of seconds to wait before turning the video on. The guys must watch the video without pauses.
|
The first line contains three space-separated integers a, b and c (1 β€ a, b, c β€ 1000, a > b). The first number (a) denotes the size of data needed to watch one second of the video. The second number (b) denotes the size of data Valeric and Valerko can download from the Net per second. The third number (c) denotes the video's length in seconds.
|
Print a single number β the minimum integer number of seconds that Valeric and Valerko must wait to watch football without pauses.
|
In the first sample video's length is 1 second and it is necessary 4 units of data for watching 1 second of video, so guys should download 4 Β· 1 = 4 units of data to watch the whole video. The most optimal way is to wait 3 seconds till 3 units of data will be downloaded and then start watching. While guys will be watching video 1 second, one unit of data will be downloaded and Valerik and Valerko will have 4 units of data by the end of watching. Also every moment till the end of video guys will have more data then necessary for watching.In the second sample guys need 2 Β· 10 = 20 units of data, so they have to wait 5 seconds and after that they will have 20 units before the second second ends. However, if guys wait 4 seconds, they will be able to watch first second of video without pauses, but they will download 18 units of data by the end of second second and it is less then necessary.
|
Input: 4 1 1 | Output: 3
|
Beginner
| 3 | 1,190 | 346 | 130 | 1 |
1,784 |
D
|
1784D
|
D. Wooden Spoon
| 2,400 |
combinatorics; dp
|
\(2^n\) people, numbered with distinct integers from \(1\) to \(2^n\), are playing in a single elimination tournament. The bracket of the tournament is a full binary tree of height \(n\) with \(2^n\) leaves.When two players meet each other in a match, a player with the smaller number always wins. The winner of the tournament is the player who wins all \(n\) their matches.A virtual consolation prize ""Wooden Spoon"" is awarded to a player who satisfies the following \(n\) conditions: they lost their first match; the player who beat them lost their second match; the player who beat that player lost their third match; \(\ldots\); the player who beat the player from the previous condition lost the final match of the tournament. It can be shown that there is always exactly one player who satisfies these conditions.Consider all possible \((2^n)!\) arrangements of players into the tournament bracket. For each player, find the number of these arrangements in which they will be awarded the ""Wooden Spoon"", and print these numbers modulo \(998\,244\,353\).
|
The only line contains a single integer \(n\) (\(1 \le n \le 20\)) β the size of the tournament.There are \(20\) tests in the problem: in the first test, \(n = 1\); in the second test, \(n = 2\); \(\ldots\); in the \(20\)-th test, \(n = 20\).
|
Print \(2^n\) integers β the number of arrangements in which the ""Wooden Spoon"" is awarded to players \(1, 2, \ldots, 2^n\), modulo \(998\,244\,353\).
|
In the first example, the ""Wooden Spoon"" is always awarded to player \(2\).In the second example, there are \(8\) arrangements where players \(1\) and \(4\) meet each other in the first match, and in these cases, the ""Wooden Spoon"" is awarded to player \(3\). In the remaining \(16\) arrangements, the ""Wooden Spoon"" is awarded to player \(4\).
|
Input: 1 | Output: 0 2
|
Expert
| 2 | 1,063 | 242 | 152 | 17 |
2,087 |
D
|
2087D
|
D. Uppercase or Lowercase?
| 0 |
*special; *special; interactive
|
This is an interactive problem.In a certain database, there are \(n\) handles stored in the form of a numbered list. You are interested in one specific handle \(h\), or more precisely, in its position in this list.You can ask the database for the position \(i\) from the list, and it will return the handle at the \(i\)-th position.All handles are non-empty strings consisting of at most \(20\) lowercase Latin letters, except the first one that can be either a lowercase or an uppercase Latin letter.You know that all handles are sorted in lexicographical order, but the problem is that it is unknown whether uppercase letters are considered less than lowercase ones. In other words, you do not know if it is true that all handles starting with uppercase letters come first, followed by those starting with lowercase letters β or vice versa, that all handles starting with lowercase letters come first, followed by those starting with uppercase letters.Lexicographical order is the order of standard string comparison, formally defined as follows: if strings \(s\) and \(t\) differ in some positions, the earliest of which is position \(i\), then string \(s\) comes before string \(t\) in lexicographical order if and only if \(s_i < t_i\) (for example, for the strings cats and current, the first position with different characters is \(2\), and the character a is less than the character u, so cats is lexicographically less than current); if strings \(s\) and \(t\) have no positions with different characters, then the shorter one comes first. For example, the string cat comes before the string cats. Due to restricted access to the database, you can make no more than \(10\) queries to it. Determine the position of the handle you are interested in, knowing that it is definitely in the list.
|
The first line contains the number \(n\) and the string \(h\) (\(1 \le n \le 500\)) β the total number of handles in the list and the handle you are searching for.It is guaranteed that all handles are non-empty strings of no more than \(20\) characters, consisting only of lowercase Latin letters, except the first letter that may be an uppercase letter.It is also guaranteed that all handles are sorted, unique, and the given handle is in the list.
|
Note that spaces in examples are added only for improved readability. When checking solutions, the interactor will not output any empty lines.
|
Input: 4 adedalic Bleddest Neon adedalic awoo | Output: ? 1 ? 2 ? 3 ? 4 ! 3
|
Beginner
| 3 | 1,799 | 449 | 0 | 20 |
|
1,423 |
M
|
1423M
|
M. Milutin's Plums
| 2,800 |
interactive
|
As you all know, the plum harvesting season is on! Little Milutin had his plums planted in an orchard that can be represented as an \(n\) by \(m\) matrix. While he was harvesting, he wrote the heights of all trees in a matrix of dimensions \(n\) by \(m\).At night, when he has spare time, he likes to perform various statistics on his trees. This time, he is curious to find out the height of his lowest tree. So far, he has discovered some interesting properties of his orchard. There is one particular property that he thinks is useful for finding the tree with the smallest heigh.Formally, let \(L(i)\) be the leftmost tree with the smallest height in the \(i\)-th row of his orchard. He knows that \(L(i) \le L(i+1)\) for all \(1 \le i \le n - 1\). Moreover, if he takes a submatrix induced by any subset of rows and any subset of columns, \(L(i) \le L(i+1)\) will hold for all \(1 \le i \le n'-1\), where \(n'\) is the number of rows in that submatrix.Since the season is at its peak and he is short on time, he asks you to help him find the plum tree with minimal height.
|
This problem is interactive.The first line of input will contain two integers \(n\) and \(m\), representing the number of rows and the number of columns in Milutin's orchard. It is guaranteed that \(1 \le n, m \le 10^6\).The following lines will contain the answers to your queries.
|
Once you know have found the minimum value \(r\), you should print ! \(r\) to the standard output.
|
Input: 5 5 13 15 10 9 15 15 17 12 11 17 10 12 7 6 12 17 19 14 13 19 16 18 13 12 18 | Output:
|
Master
| 1 | 1,077 | 282 | 98 | 14 |
|
2,109 |
C1
|
2109C1
|
C1. Hacking Numbers (Easy Version)
| 1,500 |
bitmasks; constructive algorithms; interactive; math; number theory
|
This is the easy version of the problem. In this version, you can send at most \(\mathbf{7}\) commands. You can make hacks only if all versions of the problem are solved.This is an interactive problem.Welcome, Duelists! In this interactive challenge, there is an unknown integer \(x\) (\(1 \le x \le 10^9\)). You must make it equal to a given integer in the input \(n\). By harnessing the power of ""Mathmech"" monsters, you can send a command to do one of the following: CommandConstraintResultCaseUpdateJury's response""add \(y\)""\(-10^{18} \le y \le 10^{18}\)\(\mathrm{res} = x + y\)\(\text{if } 1 \le \mathrm{res} \le 10^{18}\)\(x \leftarrow \mathrm{res}\)""1""\(\mathrm{else}\)\(x \leftarrow x\)""0""""mul \(y\)""\(1 \le y \le 10^{18}\)\(\mathrm{res} = x \cdot y\)\(\text{if } 1 \le \mathrm{res} \le 10^{18}\)\(x \leftarrow \mathrm{res}\)""1""\(\mathrm{else}\)\(x \leftarrow x\)""0""""div \(y\)""\(1 \le y \le 10^{18}\)\(\mathrm{res} = x/y\)\(\text{if } y\) divides \(x\)\(x \leftarrow \mathrm{res}\)""1""\(\mathrm{else}\)\(x \leftarrow x\)""0""""digit""β\(\mathrm{res} = S(x)\)\(^{\text{β}}\)β\(x \leftarrow \mathrm{res}\)""1"" You have to make \(x\) equal to \(n\) using at most \(\mathbf{7}\) commands.\(^{\text{β}}\)\(S(n)\) is a function that returns the sum of all the individual digits of a non-negative integer \(n\). For example, \(S(123) = 1 + 2 + 3 = 6\)
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). The description of the test cases follows. The first and only line of each test case contains one integer \(n\) (\(1 \le n \le 10^9\)).
|
SolutionJuryExplanation\(\texttt{2}\)There are 2 test cases.\(\texttt{100}\)In the first test case, the unknown integer \(x = 9\) and we have to make it equal to \(n = 100\).\(\texttt{add -10}\)\(\texttt{0}\)The answer to ""add -10"" is ""0"". This means that the addition command was not successful as \(x + y = 9 + (-10) \le 0\), and \(x\) remains \(9\) after the command\(\texttt{add 1}\)\(\texttt{1}\)The answer to ""add 1"" is ""1"". This means that the addition command was successful as \(x + y = 9 + 1 = 10\), and \(x\) changes to \(10\) after the command.\(\texttt{mul 10}\)\(\texttt{1}\)The answer to ""mul 10"" is ""1"". This means that the multiplication command was successful as \(x \cdot y = 10 \cdot 10 = 100\), and \(x\) changes to \(100\) after the command.\(\texttt{!}\)\(\texttt{1}\)The answer to ""!"" is ""1"". This means you have determined that \(x\) equals \(n\).\(\texttt{5}\)In the second test case, the unknown integer \(x = 1234\) and we have to make it equal to \(n = 5\).\(\texttt{digit}\)\(\texttt{1}\)The answer to ""digit"" is ""1"". This means that \(x\) turned into the sum of its digits \(1 + 2 + 3 + 4 = 10\), and \(x\) changes to \(10\) after the command.\(\texttt{div 2}\)\(\texttt{1}\)The answer to ""div 2"" is ""1"". This means that the division command was successful as \(y = 2\) is a divisor of \(x = 10\), and \(x\) changes to \(\frac{x}{y} = \frac{10}{2} = 5\) after the command.\(\texttt{!}\)\(\texttt{1}\)The answer to ""!"" is ""1"". This means you have determined that \(x\) equals \(n\). Note that the empty lines in the example input and output are for the sake of clarity, and do not occur in the real interaction.
|
Input: 2 100 0 1 1 1 5 1 1 1 | Output: add -10 add 1 mul 10 ! digit div 2 !
|
Medium
| 5 | 1,371 | 254 | 0 | 21 |
|
1,717 |
F
|
1717F
|
F. Madoka and The First Session
| 2,500 |
constructive algorithms; flows; graph matchings; graphs; implementation
|
Oh no, on the first exam Madoka got this hard problem:Given integer \(n\) and \(m\) pairs of integers (\(v_i, u_i\)). Also there is an array \(b_1, b_2, \ldots, b_n\), initially filled with zeros.Then for each index \(i\), where \(1 \leq i \leq m\), perform either \(b_{v_i} := b_{v_i} - 1\) and \(b_{u_i} := b_{u_i} + 1\), or \(b_{v_i} := b_{v_i} + 1\) and \(b_{u_i} := b_{u_i} - 1\). Note that exactly one of these operations should be performed for every \(i\).Also there is an array \(s\) of length \(n\) consisting of \(0\) and \(1\). And there is an array \(a_1, a_2, \ldots, a_n\), where it is guaranteed, that if \(s_i = 0\) holds, then \(a_i = 0\).Help Madoka and determine whenever it is possible to perform operations in such way that for every \(i\), where \(s_i = 1\) it holds that \(a_i = b_i\). If it possible you should also provide Madoka with a way to perform operations.
|
The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 10000, 1 \leq m \leq 10000\)) β the length of the array \(a\) and the number of pair of integers.The second line contains \(n\) integers \(s_1, s_2, \ldots s_n\) (\(0 \le s_i \le 1\)) β the elements of the array \(s\).The third line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(|a_i| \leq m\)) β the elements of the array \(a\). It is guaranteed that if \(s_i = 0\) holds, then \(a_i = 0\).\(i\)-th of the following \(m\) lines contains two integers \(v_i\) and \(u_i\) (\(1 \leq v_i, u_i \leq n, v_i \ne u_i\)) β the indexes of the elements of the array \(b\) to which the operation is performed. It is also guaranteed that there are no two indices \(i\) and \(j\), where \(1 \le i < j \le m\), such that \((v_i, u_i) = (v_j, u_j)\) or \((v_i, u_i) = (u_j, v_j)\).
|
In the first line print ""YES"" if it is possible to perform operations in the required way, and ""NO"" otherwise.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer).In case you printed ""YES"", print \(m\) pairs of integers. If for pair \((v_i, u_i)\) we should perform \(b_{v_i} := b_{v_i} - 1\) and \(b_{u_i} := b_{u_i} + 1\), print \((v_i, u_i)\). Otherwise print \((u_i, v_i)\). If there are multiple ways to get the correct answer, you can print any of them.You can print pairs in any order.
|
In the first example, the array \(b\) will change as follows: \([0,0,0,0,0] \rightarrow [-1,0,0,1,0] \rightarrow [-2,0,0,1,1] \rightarrow [-2,0,1,0,1] \rightarrow [-2,0,2,0,0] \rightarrow [-2,0,2,1,-1]\). \(a_i = b_i\) for all indices \(i\) from \(1\) to \(5\).In the second example, it is enough for us that \(b_2 = 1\) at the end, since only \(s_2 = 1\).In the third example, the operations cannot be performed as required.
|
Input: 5 5 1 1 1 1 1 -2 0 2 1 -1 1 5 1 4 3 5 3 4 4 5 | Output: YES 1 5 1 4 5 3 4 3 5 4
|
Expert
| 5 | 889 | 843 | 575 | 17 |
1,799 |
D2
|
1799D2
|
D2. Hot Start Up (hard version)
| 2,100 |
data structures; dp
|
This is a hard version of the problem. The constraints of \(t\), \(n\), \(k\) are the only difference between versions.You have a device with two CPUs. You also have \(k\) programs, numbered \(1\) through \(k\), that you can run on the CPUs. The \(i\)-th program (\(1 \le i \le k\)) takes \(cold_i\) seconds to run on some CPU. However, if the last program we ran on this CPU was also program \(i\), it only takes \(hot_i\) seconds (\(hot_i \le cold_i\)). Note that this only applies if we run program \(i\) multiple times consecutively β if we run program \(i\), then some different program, then program \(i\) again, it will take \(cold_i\) seconds the second time.You are given a sequence \(a_1, a_2, \ldots, a_n\) of length \(n\), consisting of integers from \(1\) to \(k\). You need to use your device to run programs \(a_1, a_2, \ldots, a_n\) in sequence. For all \(2 \le i \le n\), you cannot start running program \(a_i\) until program \(a_{i - 1}\) has completed.Find the minimum amount of time needed to run all programs \(a_1, a_2, \ldots, a_n\) in sequence.
|
Input consists of multiple test cases. The first line contains a single integer \(t\), the number of test cases (\(1 \le t \le 10^5\)).The first line of each test case contains \(n\) and \(k\) (\(1 \le n, k \le 3 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le k\)).The third line of each test case contains \(k\) integers \(cold_1, cold_2, \ldots, cold_k\) (\(1 \le cold_i \le 10^9\)).The fourth line of each test case contains \(k\) integers \(hot_1, hot_2, \ldots, hot_k\) (\(1 \le hot_i \le cold_i\)).It is guaranteed the sum of \(n\) and the sum of \(k\) over all test cases do not exceed \(3 \cdot 10^5\).
|
For each test case, print the minimum time needed to run all programs in the given order.
|
In the first test case, we can do the following: Run program \(a_1 = 1\) on CPU \(1\). It takes \(cold_1 = 3\) seconds to run. Run program \(a_2 = 2\) on CPU \(2\). It takes \(cold_2 = 2\) seconds to run. Run program \(a_3 = 2\) on CPU \(2\). The last program run on this CPU was also program \(2\), so it takes \(hot_2 = 1\) second to run. In total, we need \(3 + 2 + 1 = 6\) seconds to run them all. We can show this is optimal.In the second test case, we can use do the following: Run program \(a_1 = 1\) on CPU \(1\). It takes \(cold_1 = 5\) seconds to run. Run program \(a_2 = 2\) on CPU \(2\). It takes \(cold_2 = 3\) seconds to run. Run program \(a_3 = 1\) on CPU \(1\). The last program run on this CPU was also program \(1\), so it takes \(hot_1 = 2\) seconds to run. Run program \(a_4 = 2\) on CPU \(2\). The last program run on this CPU was also program \(2\), so it takes \(hot_2 = 1\) second to run. In total, we need \(5 + 3 + 2 + 1 = 11\) seconds. We can show this is optimal.
|
Input: 93 21 2 23 22 14 21 2 1 25 32 14 31 2 3 1100 100 1001 1 15 22 1 2 1 165 4554 75 31 3 2 1 22 2 21 1 15 11 1 1 1 110000000009999999995 61 6 1 4 13 6 4 1 4 51 1 1 1 4 11 334 5 61 2 38 33 3 3 1 2 3 2 110 10 810 10 5 | Output: 6 11 301 225 8 4999999996 11 6 63
|
Hard
| 2 | 1,069 | 678 | 89 | 17 |
568 |
D
|
568D
|
D. Sign Posts
| 2,800 |
brute force; geometry; math
|
One Khanate had a lot of roads and very little wood. Riding along the roads was inconvenient, because the roads did not have road signs indicating the direction to important cities.The Han decided that it's time to fix the issue, and ordered to put signs on every road. The Minister of Transport has to do that, but he has only k signs. Help the minister to solve his problem, otherwise the poor guy can lose not only his position, but also his head.More formally, every road in the Khanate is a line on the Oxy plane, given by an equation of the form Ax + By + C = 0 (A and B are not equal to 0 at the same time). You are required to determine whether you can put signs in at most k points so that each road had at least one sign installed.
|
The input starts with two positive integers n, k (1 β€ n β€ 105, 1 β€ k β€ 5)Next n lines contain three integers each, Ai, Bi, Ci, the coefficients of the equation that determines the road (|Ai|, |Bi|, |Ci| β€ 105, Ai2 + Bi2 β 0).It is guaranteed that no two roads coincide.
|
If there is no solution, print ""NO"" in the single line (without the quotes).Otherwise, print in the first line ""YES"" (without the quotes).In the second line print a single number m (m β€ k) β the number of used signs. In the next m lines print the descriptions of their locations.Description of a location of one sign is two integers v, u. If u and v are two distinct integers between 1 and n, we assume that sign is at the point of intersection of roads number v and u. If u = - 1, and v is an integer between 1 and n, then the sign is on the road number v in the point not lying on any other road. In any other case the description of a sign will be assumed invalid and your answer will be considered incorrect. In case if v = u, or if v and u are the numbers of two non-intersecting roads, your answer will also be considered incorrect.The roads are numbered starting from 1 in the order in which they follow in the input.
|
Note that you do not have to minimize m, but it shouldn't be more than k.In the first test all three roads intersect at point (0,0).In the second test all three roads form a triangle and there is no way to place one sign so that it would stand on all three roads at once.
|
Input: 3 11 0 00 -1 07 -93 0 | Output: YES11 2
|
Master
| 3 | 741 | 269 | 928 | 5 |
301 |
D
|
301D
|
D. Yaroslav and Divisors
| 2,200 |
data structures
|
Yaroslav has an array p = p1, p2, ..., pn (1 β€ pi β€ n), consisting of n distinct integers. Also, he has m queries: Query number i is represented as a pair of integers li, ri (1 β€ li β€ ri β€ n). The answer to the query li, ri is the number of pairs of integers q, w (li β€ q, w β€ ri) such that pq is the divisor of pw. Help Yaroslav, answer all his queries.
|
The first line contains the integers n and m (1 β€ n, m β€ 2Β·105). The second line contains n distinct integers p1, p2, ..., pn (1 β€ pi β€ n). The following m lines contain Yaroslav's queries. The i-th line contains integers li, ri (1 β€ li β€ ri β€ n).
|
Print m integers β the answers to Yaroslav's queries in the order they appear in the input.Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Input: 1 111 1 | Output: 1
|
Hard
| 1 | 354 | 247 | 238 | 3 |
|
837 |
D
|
837D
|
D. Round Subset
| 2,100 |
dp; math
|
Let's call the roundness of the number the number of zeros to which it ends.You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.
|
The first line contains two integer numbers n and k (1 β€ n β€ 200, 1 β€ k β€ n).The second line contains n space-separated integer numbers a1, a2, ..., an (1 β€ ai β€ 1018).
|
Print maximal roundness of product of the chosen subset of length k.
|
In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] β product 80, roundness 1, [50, 20] β product 1000, roundness 3.In the second example subset [15, 16, 25] has product 6000, roundness 3.In the third example all subsets has product with roundness 0.
|
Input: 3 250 4 20 | Output: 3
|
Hard
| 2 | 243 | 168 | 68 | 8 |
996 |
B
|
996B
|
B. World Cup
| 1,300 |
binary search; math
|
Allen wants to enter a fan zone that occupies a round square and has \(n\) entrances.There already is a queue of \(a_i\) people in front of the \(i\)-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute.Allen uses the following strategy to enter the fan zone: Initially he stands in the end of the queue in front of the first entrance. Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance). Determine the entrance through which Allen will finally enter the fan zone.
|
The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)) β the number of entrances.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β the number of people in queues. These numbers do not include Allen.
|
Print a single integer β the number of entrance that Allen will use.
|
In the first example the number of people (not including Allen) changes as follows: \([\textbf{2}, 3, 2, 0] \to [1, \textbf{2}, 1, 0] \to [0, 1, \textbf{0}, 0]\). The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance.In the second example the number of people (not including Allen) changes as follows: \([\textbf{10}, 10] \to [9, \textbf{9}] \to [\textbf{8}, 8] \to [7, \textbf{7}] \to [\textbf{6}, 6] \to \\ [5, \textbf{5}] \to [\textbf{4}, 4] \to [3, \textbf{3}] \to [\textbf{2}, 2] \to [1, \textbf{1}] \to [\textbf{0}, 0]\).In the third example the number of people (not including Allen) changes as follows: \([\textbf{5}, 2, 6, 5, 7, 4] \to [4, \textbf{1}, 5, 4, 6, 3] \to [3, 0, \textbf{4}, 3, 5, 2] \to \\ [2, 0, 3, \textbf{2}, 4, 1] \to [1, 0, 2, 1, \textbf{3}, 0] \to [0, 0, 1, 0, 2, \textbf{0}]\).
|
Input: 42 3 2 0 | Output: 3
|
Easy
| 2 | 708 | 256 | 68 | 9 |
1,200 |
E
|
1200E
|
E. Compress Words
| 2,000 |
brute force; hashing; implementation; string suffix structures; strings
|
Amugae has a sentence consisting of \(n\) words. He want to compress this sentence into one word. Amugae doesn't like repetitions, so when he merges two words into one word, he removes the longest prefix of the second word that coincides with a suffix of the first word. For example, he merges ""sample"" and ""please"" into ""samplease"".Amugae will merge his sentence left to right (i.e. first merge the first two words, then merge the result with the third word and so on). Write a program that prints the compressed word after the merging process ends.
|
The first line contains an integer \(n\) (\(1 \le n \le 10^5\)), the number of the words in Amugae's sentence.The second line contains \(n\) words separated by single space. Each words is non-empty and consists of uppercase and lowercase English letters and digits ('A', 'B', ..., 'Z', 'a', 'b', ..., 'z', '0', '1', ..., '9'). The total length of the words does not exceed \(10^6\).
|
In the only line output the compressed word after the merging process ends as described in the problem.
|
Input: 5 I want to order pizza | Output: Iwantorderpizza
|
Hard
| 5 | 556 | 382 | 103 | 12 |
|
1,305 |
B
|
1305B
|
B. Kuroni and Simple Strings
| 1,200 |
constructive algorithms; greedy; strings; two pointers
|
Now that Kuroni has reached 10 years old, he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence!We say that a string formed by \(n\) characters '(' or ')' is simple if its length \(n\) is even and positive, its first \(\frac{n}{2}\) characters are '(', and its last \(\frac{n}{2}\) characters are ')'. For example, the strings () and (()) are simple, while the strings )( and ()() are not simple.Kuroni will be given a string formed by characters '(' and ')' (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn't have to be continuous. For example, he can apply the operation to the string ')()(()))', to choose a subsequence of bold characters, as it forms a simple string '(())', delete these bold characters from the string and to get '))()'. Kuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty.Since the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead?A sequence of characters \(a\) is a subsequence of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) characters.
|
The only line of input contains a string \(s\) (\(1 \le |s| \le 1000\)) formed by characters '(' and ')', where \(|s|\) is the length of \(s\).
|
In the first line, print an integer \(k\) β the minimum number of operations you have to apply. Then, print \(2k\) lines describing the operations in the following format:For each operation, print a line containing an integer \(m\) β the number of characters in the subsequence you will remove.Then, print a line containing \(m\) integers \(1 \le a_1 < a_2 < \dots < a_m\) β the indices of the characters you will remove. All integers must be less than or equal to the length of the current string, and the corresponding subsequence must form a simple string.If there are multiple valid sequences of operations with the smallest \(k\), you may print any of them.
|
In the first sample, the string is '(()(('. The operation described corresponds to deleting the bolded subsequence. The resulting string is '(((', and no more operations can be performed on it. Another valid answer is choosing indices \(2\) and \(3\), which results in the same final string.In the second sample, it is already impossible to perform any operations.
|
Input: (()(( | Output: 1 2 1 3
|
Easy
| 4 | 1,679 | 143 | 662 | 13 |
2,041 |
H
|
2041H
|
H. Sheet Music
| 2,300 |
combinatorics; dp; math
|
Image generated by ChatGPT 4o. Alice likes singing. As a singing enthusiast, Alice has listened to countless songs and has tried singing them many times. However, occasionally, some songs make Alice feel bored. After some research, Alice believes that this is because even though the songs she chose are all different, due to her instinctive preference, they all turn out to be musically similar to one another.To thoroughly analyze this, Alice decided to study the sheet music of the songs. For convenience, Alice represented a song of length \(n\) as an integer sequence \(a_1, a_2, \ldots, a_n\), where \(a_i\) is the pitch of the \(i\)-th note. Then she defined the musical equivalence between songs. Two songs \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\) of length \(n\) are musically equivalent if for all \(1\leq i<n\), both \(a_i, a_{i+1}\) and \(b_{i}, b_{i+1}\) have the same pitch relationship. More specifically, \(a_i, a_{i+1}\) and \(b_i, b_{i+1}\) have the same pitch relationship if either \(a_i < a_{i + 1}\) and \(b_i < b_{i + 1}\), \(a_i = a_{i + 1}\) and \(b_i = b_{i + 1}\), or \(a_i > a_{i + 1}\) and \(b_i > b_{i + 1}\). For example, \(1, 2, 3, 3, 2\) and \(5, 9, 13, 13, 1\) are musically equivalent, while \(1, 2, 3, 2, 1\) and \(1, 2, 2, 2, 1\) are not. Having practiced consistently for a long time, Alice is able to sing any note in the range of \([1, k]\). She wants to know how many different songs of length \(n\) within her range there are, if we treat musically equivalent songs as the same one. Can you help her calculate the number?Since the answer might be large, print the answer modulo \(998244353\).
|
The only line contains two integers \(n, k\). \(1\leq n\leq 10^6\) \(1\leq k \leq 10^9\)
|
Output the number of different songs modulo \(998244353\).
|
Input: 3 2 | Output: 7
|
Expert
| 3 | 1,649 | 88 | 58 | 20 |
|
162 |
B
|
162B
|
B. Binary notation
| 1,800 |
*special
|
You are given a positive integer n. Output its binary notation.
|
The only line of input data contains an integer n (1 β€ n β€ 106).
|
Output the binary notation of n (without any leading zeros).
|
In the first example 5 = 1 * 22 + 0 * 21 + 1 * 20.
|
Input: 5 | Output: 101
|
Medium
| 1 | 63 | 64 | 60 | 1 |
606 |
B
|
606B
|
B. Testing Robots
| 1,600 |
implementation
|
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x Γ y, after that a mine will be installed into one of the squares of the field. It is supposed to conduct exactly xΒ·y tests, each time a mine is installed into a square that has never been used before. The starting cell of the robot always remains the same.After placing the objects on the field the robot will have to run a sequence of commands given by string s, consisting only of characters 'L', 'R', 'U', 'D'. These commands tell the robot to move one square to the left, to the right, up or down, or stay idle if moving in the given direction is impossible. As soon as the robot fulfills all the sequence of commands, it will blow up due to a bug in the code. But if at some moment of time the robot is at the same square with the mine, it will also blow up, but not due to a bug in the code.Moving to the left decreases coordinate y, and moving to the right increases it. Similarly, moving up decreases the x coordinate, and moving down increases it.The tests can go on for very long, so your task is to predict their results. For each k from 0 to length(s) your task is to find in how many tests the robot will run exactly k commands before it blows up.
|
The first line of the input contains four integers x, y, x0, y0 (1 β€ x, y β€ 500, 1 β€ x0 β€ x, 1 β€ y0 β€ y) β the sizes of the field and the starting coordinates of the robot. The coordinate axis X is directed downwards and axis Y is directed to the right.The second line contains a sequence of commands s, which should be fulfilled by the robot. It has length from 1 to 100 000 characters and only consists of characters 'L', 'R', 'U', 'D'.
|
Print the sequence consisting of (length(s) + 1) numbers. On the k-th position, starting with zero, print the number of tests where the robot will run exactly k commands before it blows up.
|
In the first sample, if we exclude the probable impact of the mines, the robot's route will look like that: .
|
Input: 3 4 2 2UURDRDRL | Output: 1 1 0 1 1 1 1 0 6
|
Medium
| 1 | 1,448 | 438 | 189 | 6 |
1,163 |
B2
|
1163B2
|
B2. Cat Party (Hard Edition)
| 1,600 |
data structures; implementation
|
This problem is same as the previous one, but has larger constraints.Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time.For each of the \(n\) days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the \(i\)-th day has a ribbon with color \(u_i\). Shiro wants to know the largest number \(x\), such that if we consider the streak of the first \(x\) days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining \(x - 1\) will have the same number of occurrences.For example, consider the following sequence of \(u_i\): \([2, 2, 1, 1, 5, 4, 4, 5]\). Then \(x = 7\) makes a streak, since if we remove the leftmost \(u_i = 5\), each ribbon color will appear exactly twice in the prefix of \(x - 1\) days. Note that \(x = 8\) doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the total number of days.The second line contains \(n\) integers \(u_1, u_2, \ldots, u_n\) (\(1 \leq u_i \leq 10^5\)) β the colors of the ribbons the cats wear.
|
Print a single integer \(x\) β the largest possible streak of days.
|
In the first example, we can choose the longest streak of \(13\) days, since upon removing the last day out of the streak, all of the remaining colors \(1\), \(2\), \(3\), and \(4\) will have the same number of occurrences of \(3\). Note that the streak can also be \(10\) days (by removing the \(10\)-th day from this streak) but we are interested in the longest streak.In the fourth example, if we take the streak of the first \(6\) days, we can remove the third day from this streak then all of the remaining colors \(1\), \(2\), \(3\), \(4\) and \(5\) will occur exactly once.
|
Input: 13 1 1 1 2 2 2 3 3 3 4 4 4 5 | Output: 13
|
Medium
| 2 | 1,170 | 234 | 67 | 11 |
1,839 |
D
|
1839D
|
D. Ball Sorting
| 2,100 |
data structures; dp; sortings
|
There are \(n\) colorful balls arranged in a row. The balls are painted in \(n\) distinct colors, denoted by numbers from \(1\) to \(n\). The \(i\)-th ball from the left is painted in color \(c_i\). You want to reorder the balls so that the \(i\)-th ball from the left has color \(i\). Additionally, you have \(k \ge 1\) balls of color \(0\) that you can use in the reordering process.Due to the strange properties of the balls, they can be reordered only by performing the following operations: Place a ball of color \(0\) anywhere in the sequence (between any two consecutive balls, before the leftmost ball or after the rightmost ball) while keeping the relative order of other balls. You can perform this operation no more than \(k\) times, because you have only \(k\) balls of color \(0\). Choose any ball of non-zero color such that at least one of the balls adjacent to him has color \(0\), and move that ball (of non-zero color) anywhere in the sequence (between any two consecutive balls, before the leftmost ball or after the rightmost ball) while keeping the relative order of other balls. You can perform this operation as many times as you want, but for each operation you should pay \(1\) coin. You can perform these operations in any order. After the last operation, all balls of color \(0\) magically disappear, leaving a sequence of \(n\) balls of non-zero colors.What is the minimum amount of coins you should spend on the operations of the second type, so that the \(i\)-th ball from the left has color \(i\) for all \(i\) from \(1\) to \(n\) after the disappearance of all balls of color zero? It can be shown that under the constraints of the problem, it is always possible to reorder the balls in the required way. Solve the problem for all \(k\) from \(1\) to \(n\).
|
The first line contains integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The descriptions of the test cases follow.The first line contains one integer \(n\) (\(1 \le n \le 500\)) β the number of balls.The second line contains \(n\) distinct integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le n\)) β the colors of balls from left to right.It is guaranteed that sum of \(n\) over all test cases doesn't exceed \(500\).
|
For each test case, output \(n\) integers: the \(i\)-th (\(1 \le i \le n\)) of them should be equal to the minimum amount of coins you need to spend in order to reorder balls in the required way for \(k = i\).
|
In the first test case there are \(n = 6\) balls. The colors of the balls from left to right are \([\, 2, 3, 1, 4, 6, 5 \,]\). Let's suppose \(k = 1\). One of the ways to reorder the balls in the required way for \(3\) coins:\([\, 2, 3, 1, 4, 6, 5 \,]\) \(\xrightarrow{\, 1 \,}\) \([\, 2, 3, 1, 4, \color{red}{0}, 6, 5 \,]\) \(\xrightarrow{\, 2 \,}\) \([\, 2, 3, \color{blue}{4}, 1, 0, 6, 5 \,]\) \(\xrightarrow{\, 2 \,}\) \([\, \color{blue}{1}, 2, 3, 4, 0, 6, 5 \,]\) \(\xrightarrow{\, 2\,}\) \([\, 1, 2, 3, 4, 0, 5, \color{blue}{6} \,]\)The number above the arrow is the operation type. Balls inserted on the operations of the first type are highlighted red; balls moved on the operations of second type are highlighted blue.It can be shown that for \(k = 1\) it is impossible to rearrange balls in correct order for less than \(3\) coins. Let's suppose \(k = 2\). One of the ways to reorder the balls in the required way for \(2\) coins:\([\, 2, 3, 1, 4, 6, 5 \,]\) \(\xrightarrow{\, 1 \,}\) \([\, 2, 3, 1, 4, 6, \color{red}{0}, 5\,]\) \(\xrightarrow{\, 2 \,}\) \([\, 2, 3, 1, 4, 0, 5, \color{blue}{6}\,]\) \(\xrightarrow{\, 1 \,}\) \([\, 2, 3, \color{red}{0}, 1, 4, 0, 5, 6 \,]\) \(\xrightarrow{\, 2 \,}\) \([\, \color{blue}{1}, 2, 3, 0, 4, 0, 5, 6\,]\)Note that this sequence of operations is also correct for \(k\) greater than \(2\).It can be shown that for \(k\) from \(2\) to \(6\) it is impossible to rearrange balls in correct order for less than \(2\) coins.In the second test case the balls are already placed in the correct order, so answers for all \(k\) are equal to \(0\).
|
Input: 362 3 1 4 6 531 2 3117 3 4 6 8 9 10 2 5 11 1 | Output: 3 2 2 2 2 2 0 0 0 10 5 4 4 4 4 4 4 4 4 4
|
Hard
| 3 | 1,789 | 432 | 209 | 18 |
2,071 |
B
|
2071B
|
B. Perfecto
| 1,100 |
brute force; constructive algorithms; greedy; math
|
A permutation \(p\) of length \(n\)\(^{\text{β}}\) is perfect if, for each index \(i\) (\(1 \le i \le n\)), it satisfies the following: The sum of the first \(i\) elements \(p_1 + p_2 + \ldots + p_i\) is not a perfect square\(^{\text{β }}\). You would like things to be perfect. Given a positive integer \(n\), find a perfect permutation of length \(n\), or print \(-1\) if none exists.\(^{\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). \(^{\text{β }}\)A perfect square is an integer that is the square of an integer, e.g., \(9=3^2\) is a perfect square, but \(8\) and \(14\) are not.
|
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 and only line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case: If no solution exists, print a single integer \(-1\). Otherwise, print \(n\) integers \(p_1,p_2,\ldots,p_n\) β the perfect permutation you find. If there are multiple solutions, print any of them.
|
In the first test case, there is only one permutation with length \(n = 1\) that is \(p = [1]\), which is not perfect: \(p_1 = 1 = x^2\) for \(x = 1\). In the second test case, one possible perfect permutation with length \(n = 4\) is \(p = [2, 4, 1, 3]\): \(p_1 = 2 \neq x^2\); \(p_1 + p_2 = 2 + 4 = 6 \neq x^2\); \(p_1 + p_2 + p_3 = 2 + 4 + 1 = 7 \neq x^2\); \(p_1 + p_2 + p_3 + p_4 = 2 + 4 + 1 + 3 = 10 \neq x^2\). In the third test case, one possible perfect permutation with length \(n = 5\) is \(p = [5, 1, 4, 3, 2]\): \(p_1 = 5 \neq x^2\); \(p_1 + p_2 = 5 + 1 = 6 \neq x^2\); \(p_1 + p_2 + p_3 = 5 + 1 + 4 = 10 \neq x^2\); \(p_1 + p_2 + p_3 + p_4 = 5 + 1 + 4 + 3 = 13 \neq x^2\); \(p_1 + p_2 + p_3 + p_4 + p_5 = 5 + 1 + 4 + 3 + 2 = 15 \neq x^2\).
|
Input: 3 1 4 5 | Output: -1 2 4 1 3 5 1 4 3 2
|
Easy
| 4 | 871 | 351 | 216 | 20 |
1,765 |
I
|
1765I
|
I. Infinite Chess
| 2,800 |
implementation; shortest paths
|
The black king lives on a chess board with an infinite number of columns (files) and \(8\) rows (ranks). The columns are numbered with all integer numbers (including negative). The rows are numbered from \(1\) to \(8\).Initially, the black king is located on the starting square \((x_s, y_s)\), and he needs to reach some target square \((x_t, y_t)\). Unfortunately, there are also white pieces on the board, and they threaten the black king. After negotiations, the white pieces agreed to let the black king pass to the target square on the following conditions: each turn, the black king makes a move according to the movement rules; the black king cannot move to a square occupied by a white piece; the black king cannot move to a square which is under attack by any white piece. A square is under attack if a white piece can reach it in one move according to the movement rules; the white pieces never move. Help the black king find the minimum number of moves needed to reach the target square while not violating the conditions. The black king cannot leave the board at any time.The black king moves according to the movement rules below. Even though the white pieces never move, squares which they can reach in one move are considered to be under attack, so the black king cannot move into those squares.Below are the movement rules. Note that the pieces (except for the knight) cannot jump over other pieces. a king moves exactly one square horizontally, vertically, or diagonally. a rook moves any number of vacant squares horizontally or vertically. a bishop moves any number of vacant squares diagonally. a queen moves any number of vacant squares horizontally, vertically, or diagonally. a knight moves to one of the nearest squares not on the same rank, file, or diagonal (this can be thought of as moving two squares horizontally then one square vertically, or moving one square horizontally then two squares vertically β i. e. in an ""L"" pattern). Knights are not blocked by other pieces, they can simply jump over them. There are no pawns on the board. King and knight possible moves, respectively. Dotted line shows that knight can jump over other pieces. Queen, bishop, and rook possible moves, respectively.
|
The first line contains two integers \(x_s\) and \(y_s\) (\(1 \le x_s \le 10^8\); \(1 \le y_s \le 8\)) β the starting coordinates of the black king.The second line contains two integers \(x_t\) and \(y_t\) (\(1 \le x_t \le 10^8\); \(1 \le y_t \le 8\)) β the coordinates of the target square for the black king.The third line contains one integer \(n\) (\(0 \le n \le 2000\)) β the number of white pieces on the board.Then \(n\) lines follow, the \(i\)-th line contains one character \(t_i\) and two integers \(x_i\) and \(y_i\) (\(1 \le x_i \le 10^8\); \(1 \le y_i \le 8\)) β the type and the coordinates of the \(i\)-th white piece. The types of pieces are represented by the following uppercase Latin letters: K β king Q β queen R β rook B β bishop N β knight There can be any number of white pieces of any type listed above on the board, for example, \(3\) white kings or \(4\) white queens. There are no pawns on the board.Additional constrains on the input: no square is occupied by more than one white piece; the starting square for the black king is different from the square he wants to reach, and neither of these two squares is occupied or is under attack by any white piece.
|
Print one integer β the minimum number of moves needed for the black king to reach the target square while not violating the conditions, or \(-1\) if it is impossible.
|
The image below demonstrates the solution for the second example. Here, the letters K, R, s, and t represent the white king, the white rook, the starting square, and the target square, respectively. Bold crosses mark the squares which are under attack by the white pieces. Bold dots show the shortest path for the black king.
|
Input: 1 8 7 8 2 N 4 8 B 4 6 | Output: 10
|
Master
| 2 | 2,227 | 1,185 | 167 | 17 |
1,399 |
C
|
1399C
|
C. Boats Competition
| 1,200 |
brute force; greedy; two pointers
|
There are \(n\) people who want to participate in a boat competition. The weight of the \(i\)-th participant is \(w_i\). Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight.So, if there are \(k\) teams \((a_1, b_1)\), \((a_2, b_2)\), \(\dots\), \((a_k, b_k)\), where \(a_i\) is the weight of the first participant of the \(i\)-th team and \(b_i\) is the weight of the second participant of the \(i\)-th team, then the condition \(a_1 + b_1 = a_2 + b_2 = \dots = a_k + b_k = s\), where \(s\) is the total weight of each team, should be satisfied.Your task is to choose such \(s\) that the number of teams people can create is the maximum possible. Note that each participant can be in no more than one team.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 50\)) β the number of participants. The second line of the test case contains \(n\) integers \(w_1, w_2, \dots, w_n\) (\(1 \le w_i \le n\)), where \(w_i\) is the weight of the \(i\)-th participant.
|
For each test case, print one integer \(k\): the maximum number of teams people can compose with the total weight \(s\), if you choose \(s\) optimally.
|
In the first test case of the example, we can reach the optimal answer for \(s=6\). Then the first boat is used by participants \(1\) and \(5\) and the second boat is used by participants \(2\) and \(4\) (indices are the same as weights).In the second test case of the example, we can reach the optimal answer for \(s=12\). Then first \(6\) participants can form \(3\) pairs.In the third test case of the example, we can reach the optimal answer for \(s=3\). The answer is \(4\) because we have \(4\) participants with weight \(1\) and \(4\) participants with weight \(2\).In the fourth test case of the example, we can reach the optimal answer for \(s=4\) or \(s=6\).In the fifth test case of the example, we can reach the optimal answer for \(s=3\). Note that participant with weight \(3\) can't use the boat because there is no suitable pair for him in the list.
|
Input: 5 5 1 2 3 4 5 8 6 6 6 6 6 6 8 8 8 1 2 2 1 2 1 1 2 3 1 3 3 6 1 1 3 4 2 2 | Output: 2 3 4 1 2
|
Easy
| 3 | 864 | 406 | 151 | 13 |
1,263 |
F
|
1263F
|
F. Economic Difficulties
| 2,400 |
data structures; dfs and similar; dp; flows; graphs; trees
|
An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea!Each grid (main and reserve) has a head node (its number is \(1\)). Every other node gets electricity from the head node. Each node can be reached from the head node by a unique path. Also, both grids have exactly \(n\) nodes, which do not spread electricity further.In other words, every grid is a rooted directed tree on \(n\) leaves with a root in the node, which number is \(1\). Each tree has independent enumeration and nodes from one grid are not connected with nodes of another grid.Also, the palace has \(n\) electrical devices. Each device is connected with one node of the main grid and with one node of the reserve grid. Devices connect only with nodes, from which electricity is not spread further (these nodes are the tree's leaves). Each grid's leaf is connected with exactly one device. In this example the main grid contains \(6\) nodes (the top tree) and the reserve grid contains \(4\) nodes (the lower tree). There are \(3\) devices with numbers colored in blue. It is guaranteed that the whole grid (two grids and \(n\) devices) can be shown in this way (like in the picture above): main grid is a top tree, whose wires are directed 'from the top to the down', reserve grid is a lower tree, whose wires are directed 'from the down to the top', devices β horizontal row between two grids, which are numbered from \(1\) to \(n\) from the left to the right, wires between nodes do not intersect. Formally, for each tree exists a depth-first search from the node with number \(1\), that visits leaves in order of connection to devices \(1, 2, \dots, n\) (firstly, the node, that is connected to the device \(1\), then the node, that is connected to the device \(2\), etc.).Businessman wants to sell (remove) maximal amount of wires so that each device will be powered from at least one grid (main or reserve). In other words, for each device should exist at least one path to the head node (in the main grid or the reserve grid), which contains only nodes from one grid.
|
The first line contains an integer \(n\) (\(1 \le n \le 1000\)) β the number of devices in the palace.The next line contains an integer \(a\) (\(1 + n \le a \le 1000 + n\)) β the amount of nodes in the main grid.Next line contains \(a - 1\) integers \(p_i\) (\(1 \le p_i \le a\)). Each integer \(p_i\) means that the main grid contains a wire from \(p_i\)-th node to \((i + 1)\)-th.The next line contains \(n\) integers \(x_i\) (\(1 \le x_i \le a\)) β the number of a node in the main grid that is connected to the \(i\)-th device.The next line contains an integer \(b\) (\(1 + n \le b \le 1000 + n\)) β the amount of nodes in the reserve grid.Next line contains \(b - 1\) integers \(q_i\) (\(1 \le q_i \le b\)). Each integer \(q_i\) means that the reserve grid contains a wire from \(q_i\)-th node to \((i + 1)\)-th.The next line contains \(n\) integers \(y_i\) (\(1 \le y_i \le b\)) β the number of a node in the reserve grid that is connected to the \(i\)-th device.It is guaranteed that each grid is a tree, which has exactly \(n\) leaves and each leaf is connected with one device. Also, it is guaranteed, that for each tree exists a depth-first search from the node \(1\), that visits leaves in order of connection to devices.
|
Print a single integer β the maximal amount of wires that can be cut so that each device is powered.
|
For the first example, the picture below shows one of the possible solutions (wires that can be removed are marked in red): The second and the third examples can be seen below:
|
Input: 3 6 4 1 1 4 2 6 5 3 4 1 1 1 3 4 2 | Output: 5
|
Expert
| 6 | 2,158 | 1,232 | 100 | 12 |
1,183 |
B
|
1183B
|
B. Equalize Prices
| 900 |
math
|
There are \(n\) products in the shop. The price of the \(i\)-th product is \(a_i\). The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly.In fact, the owner of the shop can change the price of some product \(i\) in such a way that the difference between the old price of this product \(a_i\) and the new price \(b_i\) is at most \(k\). In other words, the condition \(|a_i - b_i| \le k\) should be satisfied (\(|x|\) is the absolute value of \(x\)).He can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price \(b_i\) of each product \(i\) should be positive (i.e. \(b_i > 0\) should be satisfied for all \(i\) from \(1\) to \(n\)).Your task is to find out the maximum possible equal price \(B\) of all productts with the restriction that for all products the condiion \(|a_i - B| \le k\) should be satisfied (where \(a_i\) is the old price of the product and \(B\) is the same new price of all products) or report that it is impossible to find such price \(B\).Note that the chosen price \(B\) should be integer.You should answer \(q\) independent queries.
|
The first line of the input contains one integer \(q\) (\(1 \le q \le 100\)) β the number of queries. Each query is presented by two lines.The first line of the query contains two integers \(n\) and \(k\) (\(1 \le n \le 100, 1 \le k \le 10^8\)) β the number of products and the value \(k\). The second line of the query contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^8\)), where \(a_i\) is the price of the \(i\)-th product.
|
Print \(q\) integers, where the \(i\)-th integer is the answer \(B\) on the \(i\)-th query.If it is impossible to equalize prices of all given products with restriction that for all products the condition \(|a_i - B| \le k\) should be satisfied (where \(a_i\) is the old price of the product and \(B\) is the new equal price of all products), print -1. Otherwise print the maximum possible equal price of all products.
|
In the first example query you can choose the price \(B=2\). It is easy to see that the difference between each old price and each new price \(B=2\) is no more than \(1\).In the second example query you can choose the price \(B=6\) and then all the differences between old and new price \(B=6\) will be no more than \(2\).In the third example query you cannot choose any suitable price \(B\). For any value \(B\) at least one condition out of two will be violated: \(|1-B| \le 2\), \(|6-B| \le 2\).In the fourth example query all values \(B\) between \(1\) and \(7\) are valid. But the maximum is \(7\), so it's the answer.
|
Input: 4 5 1 1 1 2 3 1 4 2 6 4 8 5 2 2 1 6 3 5 5 2 5 | Output: 2 6 -1 7
|
Beginner
| 1 | 1,186 | 446 | 418 | 11 |
196 |
A
|
196A
|
A. Lexicographically Maximum Subsequence
| 1,100 |
greedy; strings
|
You've got string s, consisting of only lowercase English letters. Find its lexicographically maximum subsequence.We'll call a non-empty string s[p1p2... pk] = sp1sp2... spk(1 β€ p1 < p2 < ... < pk β€ |s|) a subsequence of string s = s1s2... s|s|.String x = x1x2... x|x| is lexicographically larger than string y = y1y2... y|y|, if either |x| > |y| and x1 = y1, x2 = y2, ... , x|y| = y|y|, or exists such number r (r < |x|, r < |y|), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 > yr + 1. Characters in lines are compared like their ASCII codes.
|
The single line contains a non-empty string s, consisting only of lowercase English letters. The string's length doesn't exceed 105.
|
Print the lexicographically maximum subsequence of string s.
|
Let's look at samples and see what the sought subsequences look like (they are marked with uppercase bold letters).The first sample: aBaBBAThe second sample: abbCbCCaCbbCBaaBA
|
Input: ababba | Output: bbba
|
Easy
| 2 | 546 | 132 | 60 | 1 |
1,062 |
E
|
1062E
|
E. Company
| 2,300 |
binary search; data structures; dfs and similar; greedy; trees
|
The company \(X\) has \(n\) employees numbered from \(1\) through \(n\). Each employee \(u\) has a direct boss \(p_u\) (\(1 \le p_u \le n\)), except for the employee \(1\) who has no boss. It is guaranteed, that values \(p_i\) form a tree. Employee \(u\) is said to be in charge of employee \(v\) if \(u\) is the direct boss of \(v\) or there is an employee \(w\) such that \(w\) is in charge of \(v\) and \(u\) is the direct boss of \(w\). Also, any employee is considered to be in charge of himself.In addition, for each employee \(u\) we define it's level \(lv(u)\) as follow: \(lv(1)=0\) \(lv(u)=lv(p_u)+1\) for \(u \neq 1\) In the near future, there are \(q\) possible plans for the company to operate. The \(i\)-th plan consists of two integers \(l_i\) and \(r_i\), meaning that all the employees in the range \([l_i, r_i]\), and only they, are involved in this plan. To operate the plan smoothly, there must be a project manager who is an employee in charge of all the involved employees. To be precise, if an employee \(u\) is chosen as the project manager for the \(i\)-th plan then for every employee \(v \in [l_i, r_i]\), \(u\) must be in charge of \(v\). Note, that \(u\) is not necessary in the range \([l_i, r_i]\). Also, \(u\) is always chosen in such a way that \(lv(u)\) is as large as possible (the higher the level is, the lower the salary that the company has to pay the employee).Before any plan is operated, the company has JATC take a look at their plans. After a glance, he tells the company that for every plan, it's possible to reduce the number of the involved employees exactly by one without affecting the plan. Being greedy, the company asks JATC which employee they should kick out of the plan so that the level of the project manager required is as large as possible. JATC has already figured out the answer and challenges you to do the same.
|
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 100\,000\), \(1 \le q \le 100\,000\)) β the number of employees and the number of plans, respectively.The second line contains \(n-1\) integers \(p_2, p_3, \dots, p_n\) (\(1 \le p_i \le n\)) meaning \(p_i\) is the direct boss of employee \(i\).It is guaranteed, that values \(p_i\) form a directed tree with the root of \(1\).Each of the following \(q\) lines contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i<r_i \le n\)) β the range of the employees, involved in the corresponding plan.
|
Print \(q\) lines, each containing two integers β the number of the employee which should be kicked from the corresponding plan and the maximum possible level of the project manager in that case.If there are more than one way to choose that employee, print any of them.
|
In the example: In the first query, we can choose whether \(4\) or \(5\) or \(6\) and the project manager will be \(3\).In the second query, if we choose any employee other than the employee \(8\), the project manager will be \(1\). If we choose \(8\), the project manager will be \(3\). Since \(lv(3)=1 > lv(1)=0\), choosing \(8\) is the best strategy.In the third query, no matter how we choose the employee, the project manager will always be \(1\).In the fourth query, if we choose \(9\) or \(10\) then the project manager will be \(3\). If we choose \(11\) then the project manager will be \(7\). Since \(lv(7)=3>lv(3)=1\), we choose \(11\) as the answer.
|
Input: 11 51 1 3 3 3 4 2 7 7 64 64 81 119 118 11 | Output: 4 18 11 011 38 1
|
Expert
| 5 | 1,874 | 561 | 269 | 10 |
241 |
F
|
241F
|
F. Race
| 2,300 |
brute force; implementation
|
The Old City is a rectangular city represented as an m Γ n grid of blocks. This city contains many buildings, straight two-way streets and junctions. Each junction and each building is exactly one block. All the streets have width of one block and are either vertical or horizontal. There is a junction on both sides of each street. We call two blocks adjacent if and only if they share a common side. No two blocks of different streets are adjacent and no two junctions are adjacent. There is an annual festival and as a part of it, The Old Peykan follows a special path in the city. This path starts from a block in a street, continues with many junctions and ends in a block of some street. For each street block, we know how much time it takes for the Old Peykan to go from this block to an adjacent block. Also the Old Peykan can go from each junction to its adjacent street blocks in one minute. Of course Old Peykan can't go to building blocks.We know the initial position of the Old Peykan and the sequence of junctions that it passes to reach its destination. After passing all the junctions and reaching the destination, it will stay there forever. Your task is to find out where will the Old Peykan be k minutes after it starts moving. Consider that The Old Peykan always follows the shortest path that passes through the given sequence of junctions and reaches the destination.Note that the Old Peykan may visit some blocks more than once.
|
The first line of input contains three integers m, n and k (3 β€ m, n β€ 100, 1 β€ k β€ 100000). Next m lines are representing the city's map. Each of them containts n characters, each character is a block: Character ""#"" represents a building. Digits ""1"", ""2"", ..., ""9"" represent a block of an street and this digit means the number of minutes it takes for the Old Peykan to pass this block. Characters ""a"", ""b"", ..., ""z"" means that this block is a junction and this character is it's name. All the junction names are unique. Consider that all blocks have the coordinates: the j-th in the i-th line have coordinates (i, j) (1 β€ i β€ m, 1 β€ j β€ n).The (m + 2)th line contains two integers rs and cs (1 β€ rs β€ m, 1 β€ cs β€ n), string s and another two integers re and ce (1 β€ re β€ m, 1 β€ ce β€ n). The path starts from block (rs, cs), continues through junctions in the order that is specified by s and will end in block (re, ce). Length of s is between 1 and 1000.It's guaranteed that string s denotes a correct path from the start position to the end position and string s doesn't contain two consecutive equal letters. Also start position (rs, cs) and the end position (re, ce) are street blocks.
|
In a single line print two integers rf and cf β (rf, cf) being the position of the Old Peykan after exactly k minutes.
|
Input: 3 10 12###########z1a1111b###########2 3 ab 2 8 | Output: 2 8
|
Expert
| 2 | 1,451 | 1,204 | 118 | 2 |
|
1,834 |
C
|
1834C
|
C. Game with Reversing
| 1,200 |
games; greedy; math; strings
|
Alice and Bob are playing a game. They have two strings \(S\) and \(T\) of the same length \(n\) consisting of lowercase latin letters. Players take turns alternately, with Alice going first.On her turn, Alice chooses an integer \(i\) from \(1\) to \(n\), one of the strings \(S\) or \(T\), and any lowercase latin letter \(c\), and replaces the \(i\)-th symbol in the chosen string with the character \(c\).On his turn, Bob chooses one of the strings \(S\) or \(T\), and reverses it. More formally, Bob makes the replacement \(S := \operatorname{rev}(S)\) or \(T := \operatorname{rev}(T)\), where \(\operatorname{rev}(P) = P_n P_{n-1} \ldots P_1\).The game lasts until the strings \(S\) and \(T\) are equal. As soon as the strings become equal, the game ends instantly.Define the duration of the game as the total number of moves made by both players during the game. For example, if Alice made \(2\) moves in total, and Bob made \(1\) move, then the duration of this game is \(3\).Alice's goal is to minimize the duration of the game, and Bob's goal is to maximize the duration of the game.What will be the duration of the game, if both players play optimally? It can be shown that the game will end in a finite number of turns.
|
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 10^5\)) β the length of the strings \(S\) and \(T\).The second line of each test case contains a string \(S\) of length \(n\) consisting of lowercase latin letters.The third line of each test case contains a string \(T\) of length \(n\) consisting of lowercase latin letters.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output a single number on a separate line β the duration of the described game, if both players play optimally.
|
In the first test case, in her turn, Alice can replace the third symbol of the string \(S\) with x. After that, both strings will become equal to ""abxde"" and the game will end after one move. Since Alice's goal is to finish the game in as few moves as possible, this move will be one of her optimal first moves, and the final answer will be \(1\).In the second test case, in her turn, Alice can replace the fifth symbol of the string \(T\) with h. After this move, \(S =\) ""hello"", \(T =\) ""olleh"". Then Bob makes his turn. In his turn, he must reverse one of the strings. If Bob chooses the string \(S\), then after his turn both strings will be equal to ""olleh"", and if he chooses the string \(T\), then after his turn both strings will be equal to ""hello"". Thus, after the presented first move of Alice, the game will definitely end in \(2\) moves. It can be shown that there is no strategy for Alice to finish the game in less than \(2\) moves, with both players playing optimally. The final answer is \(2\).In the third test case, in her first move, Alice can replace the second symbol of the string \(S\) with c. After this move, \(S =\) ""ac"", \(T =\) ""cd"". Then Bob makes his turn. If Bob reverses the string \(S\), then after his turn \(S =\) ""ca"", \(T =\) ""cd"". Then it is easy to see that in this case Alice can definitely finish the game on the \(3\)-rd move, by replacing the second symbol of the string \(T\) with a, after which both strings will become equal to ""ca"". If Bob reverses the string \(T\), then after his turn \(S =\) ""ac"", \(T =\) ""dc"". In this case, Alice can also definitely finish the game on the \(3\)rd move, by replacing the first symbol of the string \(S\) with d, after which both strings will become equal to ""dc"". Thus, Alice can definitely finish the game in \(3\) moves regardless of Bob's moves. It can be shown that the game cannot end in less than \(3\) moves, with both players playing optimally.In the fifth test case, the strings \(S\) and \(T\) are equal, so the game will end without starting, in \(0\) moves.
|
Input: 75abcdeabxde5helloolleo2abcd7aaaaaaaabbbbba1qq6yoyoyooyoyoy8abcdefghhguedfbh | Output: 1 2 3 9 0 2 6
|
Easy
| 4 | 1,230 | 600 | 131 | 18 |
76 |
C
|
76C
|
C. Mutation
| 2,700 |
bitmasks; dp; math
|
Scientists of planet Olympia are conducting an experiment in mutation of primitive organisms. Genome of organism from this planet can be represented as a string of the first K capital English letters. For each pair of types of genes they assigned ai, j β a risk of disease occurence in the organism provided that genes of these types are adjacent in the genome, where i β the 1-based index of the first gene and j β the index of the second gene. The gene 'A' has index 1, 'B' has index 2 and so on. For example, a3, 2 stands for the risk of 'CB' fragment. Risk of disease occurence in the organism is equal to the sum of risks for each pair of adjacent genes in the genome.Scientists have already obtained a base organism. Some new organisms can be obtained by mutation of this organism. Mutation involves removal of all genes of some particular types. Such removal increases the total risk of disease occurence additionally. For each type of genes scientists determined ti β the increasement of the total risk of disease occurence provided by removal of all genes having type with index i. For example, t4 stands for the value of additional total risk increasement in case of removing all the 'D' genes.Scientists want to find a number of different organisms that can be obtained from the given one which have the total risk of disease occurence not greater than T. They can use only the process of mutation described above. Two organisms are considered different if strings representing their genomes are different. Genome should contain at least one gene.
|
The first line of the input contains three integer numbers N (1 β€ N β€ 200 000) β length of the genome of base organism, K (1 β€ K β€ 22) β the maximal index of gene type in the genome and T (1 β€ T β€ 2Β·109) β maximal allowable risk of disease occurence. The second line contains the genome of the given organism. It is a string of the first K capital English letters having length N.The third line contains K numbers t1, t2, ..., tK, where ti is additional risk value of disease occurence provided by removing of all genes of the i-th type.The following K lines contain the elements of the given matrix ai, j. The i-th line contains K numbers. The j-th number of the i-th line stands for a risk of disease occurence for the pair of genes, first of which corresponds to the i-th letter and second of which corresponds to the j-th letter. The given matrix is not necessarily symmetrical.All the numbers in the input are integer, non-negative and all of them except T are not greater than 109. It is guaranteed that the maximal possible risk of organism that can be obtained from the given organism is strictly smaller than 231.
|
Output the number of organisms that can be obtained from the base one and which have the total risk of disease occurence not greater than T.
|
Explanation: one can obtain the following organisms (risks are stated in brackets): BACAC (11), ACAC (10), BAA (5), B (6), AA (4).
|
Input: 5 3 13BACAC4 1 21 2 32 3 43 4 10 | Output: 5
|
Master
| 3 | 1,558 | 1,122 | 140 | 0 |
2 |
B
|
2B
|
B. The least round way
| 2,000 |
dp; math
|
There is a square matrix n Γ n, consisting of non-negative integer numbers. You should find such a way on it that starts in the upper left cell of the matrix; each following cell is to the right or down from the current cell; the way ends in the bottom right cell. Moreover, if we multiply together all the numbers along the way, the result should be the least ""round"". In other words, it should end in the least possible number of zeros.
|
The first line contains an integer number n (2 β€ n β€ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).
|
In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.
|
Input: 31 2 34 5 67 8 9 | Output: 0DDRR
|
Hard
| 2 | 440 | 189 | 114 | 0 |
|
145 |
D
|
145D
|
D. Lucky Pair
| 2,900 |
combinatorics; data structures; implementation
|
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya has an array a of n integers. The numbers in the array are numbered starting from 1. Unfortunately, Petya has been misbehaving and so, his parents don't allow him play with arrays that have many lucky numbers. It is guaranteed that no more than 1000 elements in the array a are lucky numbers. Petya needs to find the number of pairs of non-intersecting segments [l1;r1] and [l2;r2] (1 β€ l1 β€ r1 < l2 β€ r2 β€ n, all four numbers are integers) such that there's no such lucky number that occurs simultaneously in the subarray a[l1..r1] and in the subarray a[l2..r2]. Help Petya count the number of such pairs.
|
The first line contains an integer n (2 β€ n β€ 105) β the size of the array a. The second line contains n space-separated integers ai (1 β€ ai β€ 109) β array a. It is guaranteed that no more than 1000 elements in the array a are lucky numbers.
|
On the single line print the only number β the answer to the problem.Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator.
|
The subarray a[l..r] is an array that consists of elements al, al + 1, ..., ar.In the first sample there are 9 possible pairs that satisfy the condition: [1, 1] and [2, 2], [1, 1] and [2, 3], [1, 1] and [2, 4], [1, 1] and [3, 3], [1, 1] and [3, 4], [1, 1] and [4, 4], [1, 2] and [3, 3], [2, 2] and [3, 3], [3, 3] and [4, 4].In the second sample there is only one pair of segments β [1;1] and [2;2] and it satisfies the condition.
|
Input: 41 4 2 4 | Output: 9
|
Master
| 3 | 832 | 241 | 221 | 1 |
1,791 |
G1
|
1791G1
|
G1. Teleporters (Easy Version)
| 1,100 |
greedy; sortings
|
The only difference between the easy and hard versions are the locations you can teleport to.Consider the points \(0, 1, \dots, n\) on the number line. There is a teleporter located on each of the points \(1, 2, \dots, n\). At point \(i\), you can do the following: Move left one unit: it costs \(1\) coin. Move right one unit: it costs \(1\) coin. Use a teleporter at point \(i\), if it exists: it costs \(a_i\) coins. As a result, you teleport to point \(0\). Once you use a teleporter, you can't use it again. You have \(c\) coins, and you start at point \(0\). What's the most number of teleporters you can use?
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(c\) (\(1 \leq n \leq 2\cdot10^5\); \(1 \leq c \leq 10^9\)) β the length of the array and the number of coins you have respectively.The following line contains \(n\) space-separated integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the costs to use the teleporters.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, output the maximum number of teleporters you can use.
|
In the first test case, you can move one unit to the right, use the teleporter at index \(1\) and teleport to point \(0\), move two units to the right and use the teleporter at index \(2\). You are left with \(6-1-1-2-1 = 1\) coins you don't have enough coins to use another teleporter. You have used two teleporters, so the answer is two.In the second test case, you go four units to the right and use the teleporter to go to \(0\), then go six units right and use the teleporter at index \(6\) to go to \(0\). The total cost will be \(4+6+6+4 = 20\). You are left with \(12\) coins, but it is not enough to reach any other teleporter and use it so the answer is \(2\).In the third test case, you don't have enough coins to use any teleporter, so the answer is zero.
|
Input: 105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000 | Output: 2 2 0 1 2 2 1 1 1 2
|
Easy
| 2 | 615 | 613 | 73 | 17 |
93 |
D
|
93D
|
D. Flags
| 2,500 |
dp; math; matrices
|
When Igor K. was a freshman, his professor strictly urged him, as well as all other freshmen, to solve programming Olympiads. One day a problem called ""Flags"" from a website called Timmy's Online Judge caught his attention. In the problem one had to find the number of three-colored flags that would satisfy the condition... actually, it doesn't matter. Igor K. quickly found the formula and got the so passionately desired Accepted.However, the professor wasn't very much impressed. He decided that the problem represented on Timmy's Online Judge was very dull and simple: it only had three possible colors of flag stripes and only two limitations. He suggested a complicated task to Igor K. and the fellow failed to solve it. Of course, we won't tell anybody that the professor couldn't solve it as well.And how about you? Can you solve the problem?The flags consist of one or several parallel stripes of similar width. The stripes can be one of the following colors: white, black, red or yellow. You should find the number of different flags with the number of stripes from L to R, if: a flag cannot have adjacent stripes of one color; a flag cannot have adjacent white and yellow stripes; a flag cannot have adjacent red and black stripes; a flag cannot have the combination of black, white and red stripes following one after another in this or reverse order; symmetrical flags (as, for example, a WB and a BW flag, where W and B stand for the white and black colors) are considered the same.
|
The only line contains two integers L and R (1 β€ L β€ R β€ 109). They are the lower and upper borders of the number of stripes on the flag.
|
Print a single number β the number of different flags that would satisfy the condition of the problem and would have from L to R stripes, modulo 1000000007.
|
In the first test the following flags exist (they are listed in the lexicographical order, the letters B, R, W, Y stand for Black, Red, White and Yellow correspondingly):3 stripes: BWB, BYB, BYR, RWR, RYR, WBW, WBY, WRW, WRY, YBY, YRY (overall 11 flags).4 stripes: BWBW, BWBY, BYBW, BYBY, BYRW, BYRY, RWRW, RWRY, RYBW, RYBY, RYRW, RYRY (12 flags).That's why the answer to test 1 is equal to 11 + 12 = 23.
|
Input: 3 4 | Output: 23
|
Expert
| 3 | 1,499 | 137 | 156 | 0 |
834 |
A
|
834A
|
A. The Useless Toy
| 900 |
implementation
|
Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadays β caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption.Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in.Slastyona managed to have spinner rotating for exactly n seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this.
|
There are two characters in the first string β the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space.In the second strings, a single number n is given (0 β€ n β€ 109) β the duration of the rotation.It is guaranteed that the ending position of a spinner is a result of a n second spin in any of the directions, assuming the given starting position.
|
Output cw, if the direction is clockwise, ccw β if counter-clockwise, and undefined otherwise.
|
Input: ^ >1 | Output: cw
|
Beginner
| 1 | 1,149 | 577 | 94 | 8 |
|
1,958 |
A
|
1958A
|
A. 1-3-5
| 1,100 |
*special; dp
|
In Berland, coins of worth \(1\), \(3\) and \(5\) burles are commonly used (burles are local currency).Eva has to pay exactly \(n\) burles in a shop. She has an infinite amount of coins of all three types. However, she doesn't like to pay using coins worth \(1\) burle β she thinks they are the most convenient to use.Help Eva to calculate the minimum number of coins worth \(1\) burle she has to use, if she has to pay exactly \(n\) burles. Note that she can spend any number of coins worth \(3\) and/or \(5\) burles.
|
The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Each test case consists of one line, containing one integer \(n\) (\(1 \le n \le 100\)).
|
For each test case, print one integer β the minimum number of \(1\)-burle coins Eva has to use.
|
In the first test case, Eva should use \(1\) coin worth \(1\) burle, and \(2\) coins worth \(3\) burles.In the second test case, Eva should use \(1\) coin worth \(3\) burles and \(1\) coin worth \(5\) burles.In the third test case, Eva should use \(14\) coins worth \(3\) burles.In the fourth test case, Eva should use \(2\) coins worth \(1\) burle.In the fifth test case, Eva should use \(2\) coins worth \(3\) burles and \(1\) coin worth \(5\) burles.
|
Input: 57842211 | Output: 1 0 0 2 0
|
Easy
| 2 | 518 | 179 | 95 | 19 |
1,187 |
E
|
1187E
|
E. Tree Painting
| 2,100 |
dfs and similar; dp; trees
|
You are given a tree (an undirected connected acyclic graph) consisting of \(n\) vertices. You are playing a game on this tree.Initially all vertices are white. On the first turn of the game you choose one vertex and paint it black. Then on each turn you choose a white vertex adjacent (connected by an edge) to any black vertex and paint it black.Each time when you choose a vertex (even during the first turn), you gain the number of points equal to the size of the connected component consisting only of white vertices that contains the chosen vertex. The game ends when all vertices are painted black.Let's see the following example:Vertices \(1\) and \(4\) are painted black already. If you choose the vertex \(2\), you will gain \(4\) points for the connected component consisting of vertices \(2, 3, 5\) and \(6\). If you choose the vertex \(9\), you will gain \(3\) points for the connected component consisting of vertices \(7, 8\) and \(9\).Your task is to maximize the number of points you gain.
|
The first line contains an integer \(n\) β the number of vertices in the tree (\(2 \le n \le 2 \cdot 10^5\)).Each of the next \(n - 1\) lines describes an edge of the tree. Edge \(i\) is denoted by two integers \(u_i\) and \(v_i\), the indices of vertices it connects (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)).It is guaranteed that the given edges form a tree.
|
Print one integer β the maximum number of points you gain if you will play optimally.
|
The first example tree is shown in the problem statement.
|
Input: 9 1 2 2 3 2 5 2 6 1 4 4 9 9 7 9 8 | Output: 36
|
Hard
| 3 | 1,006 | 362 | 85 | 11 |
1,272 |
D
|
1272D
|
D. Remove One Element
| 1,500 |
brute force; dp
|
You are given an array \(a\) consisting of \(n\) integers.You can remove at most one element from this array. Thus, the final length of the array is \(n-1\) or \(n\).Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.Recall that the contiguous subarray \(a\) with indices from \(l\) to \(r\) is \(a[l \dots r] = a_l, a_{l + 1}, \dots, a_r\). The subarray \(a[l \dots r]\) is called strictly increasing if \(a_l < a_{l+1} < \dots < a_r\).
|
The first line of the input contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β 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^9\)), where \(a_i\) is the \(i\)-th element of \(a\).
|
Print one integer β the maximum possible length of the strictly increasing contiguous subarray of the array \(a\) after removing at most one element.
|
In the first example, you can delete \(a_3=5\). Then the resulting array will be equal to \([1, 2, 3, 4]\) and the length of its largest increasing subarray will be equal to \(4\).
|
Input: 5 1 2 5 3 4 | Output: 4
|
Medium
| 2 | 510 | 271 | 149 | 12 |
796 |
B
|
796B
|
B. Find The Bone
| 1,300 |
implementation
|
Zane the wizard is going to perform a magic show shuffling the cups.There are n cups, numbered from 1 to n, placed along the x-axis on a table that has m holes on it. More precisely, cup i is on the table at the position x = i.The problematic bone is initially at the position x = 1. Zane will confuse the audience by swapping the cups k times, the i-th time of which involves the cups at the positions x = ui and x = vi. If the bone happens to be at the position where there is a hole at any time, it will fall into the hole onto the ground and will not be affected by future swapping operations.Do not forget that Zane is a wizard. When he swaps the cups, he does not move them ordinarily. Instead, he teleports the cups (along with the bone, if it is inside) to the intended positions. Therefore, for example, when he swaps the cup at x = 4 and the one at x = 6, they will not be at the position x = 5 at any moment during the operation. Zaneβs puppy, Inzane, is in trouble. Zane is away on his vacation, and Inzane cannot find his beloved bone, as it would be too exhausting to try opening all the cups. Inzane knows that the Codeforces community has successfully helped Zane, so he wants to see if it could help him solve his problem too. Help Inzane determine the final position of the bone.
|
The first line contains three integers n, m, and k (2 β€ n β€ 106, 1 β€ m β€ n, 1 β€ k β€ 3Β·105) β the number of cups, the number of holes on the table, and the number of swapping operations, respectively.The second line contains m distinct integers h1, h2, ..., hm (1 β€ hi β€ n) β the positions along the x-axis where there is a hole on the table.Each of the next k lines contains two integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the positions of the cups to be swapped.
|
Print one integer β the final position along the x-axis of the bone.
|
In the first sample, after the operations, the bone becomes at x = 2, x = 5, x = 7, and x = 1, respectively.In the second sample, after the first operation, the bone becomes at x = 2, and falls into the hole onto the ground.
|
Input: 7 3 43 4 61 22 55 77 1 | Output: 1
|
Easy
| 1 | 1,297 | 466 | 68 | 7 |
321 |
E
|
321E
|
E. Ciel and Gondolas
| 2,600 |
data structures; divide and conquer; dp
|
Fox Ciel is in the Amusement Park. And now she is in a queue in front of the Ferris wheel. There are n people (or foxes more precisely) in the queue: we use first people to refer one at the head of the queue, and n-th people to refer the last one in the queue.There will be k gondolas, and the way we allocate gondolas looks like this: When the first gondolas come, the q1 people in head of the queue go into the gondolas. Then when the second gondolas come, the q2 people in head of the remain queue go into the gondolas. ... The remain qk people go into the last (k-th) gondolas. Note that q1, q2, ..., qk must be positive. You can get from the statement that and qi > 0.You know, people don't want to stay with strangers in the gondolas, so your task is to find an optimal allocation way (that is find an optimal sequence q) to make people happy. For every pair of people i and j, there exists a value uij denotes a level of unfamiliar. You can assume uij = uji for all i, j (1 β€ i, j β€ n) and uii = 0 for all i (1 β€ i β€ n). Then an unfamiliar value of a gondolas is the sum of the levels of unfamiliar between any pair of people that is into the gondolas.A total unfamiliar value is the sum of unfamiliar values for all gondolas. Help Fox Ciel to find the minimal possible total unfamiliar value for some optimal allocation.
|
The first line contains two integers n and k (1 β€ n β€ 4000 and 1 β€ k β€ min(n, 800)) β the number of people in the queue and the number of gondolas. Each of the following n lines contains n integers β matrix u, (0 β€ uij β€ 9, uij = uji and uii = 0).Please, use fast input methods (for example, please use BufferedReader instead of Scanner for Java).
|
Print an integer β the minimal possible total unfamiliar value.
|
In the first example, we can allocate people like this: {1, 2} goes into a gondolas, {3, 4, 5} goes into another gondolas.In the second example, an optimal solution is : {1, 2, 3} | {4, 5, 6} | {7, 8}.
|
Input: 5 20 0 1 1 10 0 1 1 11 1 0 0 01 1 0 0 01 1 0 0 0 | Output: 0
|
Expert
| 3 | 1,328 | 347 | 63 | 3 |
1,873 |
C
|
1873C
|
C. Target Practice
| 800 |
implementation; math
|
A \(10 \times 10\) target is made out of five ""rings"" as shown. Each ring has a different point value: the outermost ring β 1 point, the next ring β 2 points, ..., the center ring β 5 points. Vlad fired several arrows at the target. Help him determine how many points he got.
|
The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.Each test case consists of 10 lines, each containing 10 characters. Each character in the grid is either \(\texttt{X}\) (representing an arrow) or \(\texttt{.}\) (representing no arrow).
|
For each test case, output a single integer β the total number of points of the arrows.
|
In the first test case, there are three arrows on the outer ring worth 1 point each, two arrows on the ring worth 3 points each, and two arrows on the ring worth 4 points each. The total score is \(3 \times 1 + 2 \times 3 + 2 \times 4 = 17\). In the second test case, there aren't any arrows, so the score is \(0\).
|
Input: 4X..........................X.......X..........X......................X..X..........................X................................................................................................................................................X.......................................................XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | Output: 17 0 5 220
|
Beginner
| 2 | 277 | 341 | 87 | 18 |
304 |
B
|
304B
|
B. Calendar
| 1,300 |
brute force; implementation
|
Calendars in widespread use today include the Gregorian calendar, which is the de facto international standard, and is used almost everywhere in the world for civil purposes. The Gregorian reform modified the Julian calendar's scheme of leap years as follows: Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year. In this problem, you have been given two dates and your task is to calculate how many days are between them. Note, that leap years have unusual number of days in February.Look at the sample to understand what borders are included in the aswer.
|
The first two lines contain two dates, each date is in the format yyyy:mm:dd (1900 β€ yyyy β€ 2038 and yyyy:mm:dd is a legal date).
|
Print a single integer β the answer to the problem.
|
Input: 1900:01:012038:12:31 | Output: 50768
|
Easy
| 2 | 769 | 129 | 51 | 3 |
|
373 |
A
|
373A
|
A. Collecting Beats is Fun
| 900 |
implementation
|
Cucumber boy is fan of Kyubeat, a famous music game.Kyubeat has 16 panels for playing arranged in 4 Γ 4 table. When a panel lights up, he has to press that panel.Each panel has a timing to press (the preffered time when a player should press it), and Cucumber boy is able to press at most k panels in a time with his one hand. Cucumber boy is trying to press all panels in perfect timing, that is he wants to press each panel exactly in its preffered time. If he cannot press the panels with his two hands in perfect timing, his challenge to press all the panels in perfect timing will fail.You are given one scene of Kyubeat's panel from the music Cucumber boy is trying. Tell him is he able to press all the panels in perfect timing.
|
The first line contains a single integer k (1 β€ k β€ 5) β the number of panels Cucumber boy can press with his one hand.Next 4 lines contain 4 characters each (digits from 1 to 9, or period) β table of panels. If a digit i was written on the panel, it means the boy has to press that panel in time i. If period was written on the panel, he doesn't have to press that panel.
|
Output ""YES"" (without quotes), if he is able to press all the panels in perfect timing. If not, output ""NO"" (without quotes).
|
In the third sample boy cannot press all panels in perfect timing. He can press all the panels in timing in time 1, but he cannot press the panels in time 2 in timing with his two hands.
|
Input: 1.135124734685789 | Output: YES
|
Beginner
| 1 | 735 | 372 | 129 | 3 |
1,200 |
C
|
1200C
|
C. Round Corridor
| 1,400 |
math; number theory
|
Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by \(n\) sectors, and the outer area is equally divided by \(m\) sectors. A wall exists between each pair of sectors of same area (inner or outer), but there is no wall between the inner area and the outer area. A wall always exists at the 12 o'clock position. The inner area's sectors are denoted as \((1,1), (1,2), \dots, (1,n)\) in clockwise direction. The outer area's sectors are denoted as \((2,1), (2,2), \dots, (2,m)\) in the same manner. For a clear understanding, see the example image above.Amugae wants to know if he can move from one sector to another sector. He has \(q\) questions.For each question, check if he can move between two given sectors.
|
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 10^{18}\), \(1 \le q \le 10^4\)) β the number of sectors in the inner area, the number of sectors in the outer area and the number of questions.Each of the next \(q\) lines contains four integers \(s_x\), \(s_y\), \(e_x\), \(e_y\) (\(1 \le s_x, e_x \le 2\); if \(s_x = 1\), then \(1 \le s_y \le n\), otherwise \(1 \le s_y \le m\); constraints on \(e_y\) are similar). Amague wants to know if it is possible to move from sector \((s_x, s_y)\) to sector \((e_x, e_y)\).
|
For each question, print ""YES"" if Amugae can move from \((s_x, s_y)\) to \((e_x, e_y)\), and ""NO"" otherwise.You can print each letter in any case (upper or lower).
|
Example is shown on the picture in the statement.
|
Input: 4 6 3 1 1 2 3 2 6 1 2 2 6 2 4 | Output: YES NO YES
|
Easy
| 2 | 774 | 547 | 167 | 12 |
754 |
C
|
754C
|
C. Vladik and chat
| 2,200 |
brute force; constructive algorithms; dp; implementation; strings
|
Recently Vladik discovered a new entertainment β coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.At first, he need to download t chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages.Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages.He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats!
|
The first line contains single integer t (1 β€ t β€ 10) β the number of chats. The t chats follow. Each chat is given in the following format.The first line of each chat description contains single integer n (1 β€ n β€ 100) β the number of users in the chat.The next line contains n space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters.The next line contains single integer m (1 β€ m β€ 100) β the number of messages in the chat. The next m line contain the messages in the following formats, one per line: <username>:<text> β the format of a message with known sender. The username should appear in the list of usernames of the chat. <?>:<text> β the format of a message with unknown sender. The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters.We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text ""Vasya, masha13 and Kate!"" can mention users ""Vasya"", ""masha13"", ""and"" and ""Kate"", but not ""masha"".It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender.
|
Print the information about the t chats in the following format:If it is not possible to recover senders, print single line ""Impossible"" for this chat. Otherwise print m messages in the following format:<username>:<text>If there are multiple answers, print any of them.
|
Input: 12Vladik netman2?: Hello, Vladik!?: Hi | Output: netman: Hello, Vladik!Vladik: Hi
|
Hard
| 5 | 916 | 1,759 | 271 | 7 |
|
1,131 |
C
|
1131C
|
C. Birthday
| 1,200 |
binary search; greedy; sortings
|
Cowboy Vlad has a birthday today! There are \(n\) children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing next to each other, and it will be difficult for them to hold hands. Therefore, children want to stand in a circle so that the maximum difference between the growth of two neighboring children would be minimal possible.Formally, let's number children from \(1\) to \(n\) in a circle order, that is, for every \(i\) child with number \(i\) will stand next to the child with number \(i+1\), also the child with number \(1\) stands next to the child with number \(n\). Then we will call the discomfort of the circle the maximum absolute difference of heights of the children, who stand next to each other.Please help children to find out how they should reorder themselves, so that the resulting discomfort is smallest possible.
|
The first line contains a single integer \(n\) (\(2 \leq n \leq 100\)) β the number of the children who came to the cowboy Vlad's birthday.The second line contains integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) denoting heights of every child.
|
Print exactly \(n\) integers β heights of the children in the order in which they should stand in a circle. You can start printing a circle with any child.If there are multiple possible answers, print any of them.
|
In the first example, the discomfort of the circle is equal to \(1\), since the corresponding absolute differences are \(1\), \(1\), \(1\) and \(0\). Note, that sequences \([2, 3, 2, 1, 1]\) and \([3, 2, 1, 1, 2]\) form the same circles and differ only by the selection of the starting point.In the second example, the discomfort of the circle is equal to \(20\), since the absolute difference of \(10\) and \(30\) is equal to \(20\).
|
Input: 5 2 1 1 3 2 | Output: 1 2 3 2 1
|
Easy
| 3 | 1,048 | 258 | 213 | 11 |
797 |
D
|
797D
|
D. Broken BST
| 2,100 |
data structures; dfs and similar
|
Let T be arbitrary binary tree β tree, every vertex of which has no more than two children. Given tree is rooted, so there exists only one vertex which doesn't have a parent β it's the root of a tree. Every vertex has an integer number written on it. Following algorithm is run on every value from the tree T: Set pointer to the root of a tree. Return success if the value in the current vertex is equal to the number you are looking for Go to the left child of the vertex if the value in the current vertex is greater than the number you are looking for Go to the right child of the vertex if the value in the current vertex is less than the number you are looking for Return fail if you try to go to the vertex that doesn't exist Here is the pseudo-code of the described algorithm: bool find(TreeNode t, int x) { if (t == null) return false; if (t.value == x) return true; if (x < t.value) return find(t.left, x); else return find(t.right, x);}find(root, x);The described algorithm works correctly if the tree is binary search tree (i.e. for each node the values of left subtree are less than the value in the node, the values of right subtree are greater than the value in the node). But it can return invalid result if tree is not a binary search tree.Since the given tree is not necessarily a binary search tree, not all numbers can be found this way. Your task is to calculate, how many times the search will fail being running on every value from the tree.If the tree has multiple vertices with the same values on them then you should run algorithm on every one of them separately.
|
First line contains integer number n (1 β€ n β€ 105) β number of vertices in the tree.Each of the next n lines contains 3 numbers v, l, r (0 β€ v β€ 109) β value on current vertex, index of the left child of the vertex and index of the right child of the vertex, respectively. If some child doesn't exist then number - 1 is set instead. Note that different vertices of the tree may contain the same values.
|
Print number of times when search algorithm will fail.
|
In the example the root of the tree in vertex 2. Search of numbers 5 and 15 will return fail because on the first step algorithm will choose the subtree which doesn't contain numbers you are looking for.
|
Input: 315 -1 -110 1 35 -1 -1 | Output: 2
|
Hard
| 2 | 1,588 | 402 | 54 | 7 |
1,882 |
D
|
1882D
|
D. Tree XOR
| 1,900 |
bitmasks; dfs and similar; dp; greedy; trees
|
You are given a tree with \(n\) vertices labeled from \(1\) to \(n\). An integer \(a_{i}\) is written on vertex \(i\) for \(i = 1, 2, \ldots, n\). You want to make all \(a_{i}\) equal by performing some (possibly, zero) spells.Suppose you root the tree at some vertex. On each spell, you can select any vertex \(v\) and any non-negative integer \(c\). Then for all vertices \(i\) in the subtree\(^{\dagger}\) of \(v\), replace \(a_{i}\) with \(a_{i} \oplus c\). The cost of this spell is \(s \cdot c\), where \(s\) is the number of vertices in the subtree. Here \(\oplus\) denotes the bitwise XOR operation.Let \(m_r\) be the minimum possible total cost required to make all \(a_i\) equal, if vertex \(r\) is chosen as the root of the tree. Find \(m_{1}, m_{2}, \ldots, m_{n}\).\(^{\dagger}\) Suppose vertex \(r\) is chosen as the root of the tree. Then vertex \(i\) belongs to the subtree of \(v\) if the simple path from \(i\) to \(r\) contains \(v\).
|
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 second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < 2^{20}\)).Each of the next \(n-1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\)), denoting that there is an edge connecting two vertices \(u\) and \(v\).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 \cdot 10^{5}\).
|
For each test case, print \(m_1, m_2, \ldots, m_n\) on a new line.
|
In the first test case, to find \(m_1\) we root the tree at vertex \(1\). In the first spell, choose \(v=2\) and \(c=1\). After performing the spell, \(a\) will become \([3, 3, 0, 1]\). The cost of this spell is \(3\). In the second spell, choose \(v=3\) and \(c=3\). After performing the spell, \(a\) will become \([3, 3, 3, 1]\). The cost of this spell is \(3\). In the third spell, choose \(v=4\) and \(c=2\). After performing the spell, \(a\) will become \([3, 3, 3, 3]\). The cost of this spell is \(2\). Now all the values in array \(a\) are equal, and the total cost is \(3 + 3 + 2 = 8\).The values \(m_2\), \(m_3\), \(m_4\) can be found analogously.In the second test case, the goal is already achieved because there is only one vertex.
|
Input: 243 2 1 01 22 32 41100 | Output: 8 6 12 10 0
|
Hard
| 5 | 953 | 678 | 66 | 18 |
1,725 |
J
|
1725J
|
J. Journey
| 2,500 |
dp; trees
|
One day, Pak Chanek who is already bored of being alone at home decided to go traveling. While looking for an appropriate place, he found that Londonesia has an interesting structure.According to the information gathered by Pak Chanek, there are \(N\) cities numbered from \(1\) to \(N\). The cities are connected to each other with \(N-1\) two-directional roads, with the \(i\)-th road connecting cities \(U_i\) and \(V_i\), and taking a time of \(W_i\) hours to be traversed. In other words, Londonesia's structure forms a tree.Pak Chanek wants to go on a journey in Londonesia starting and ending in any city (not necessarily the same city) such that each city is visited at least once with the least time possible. In order for the journey to end quicker, Pak Chanek also carries an instant teleportation device for moving from one city to any city that can only be used at most once. Help Pak Chanek for finding the minimum time needed.Notes: Pak Chanek only needs to visit each city at least once. Pak Chanek does not have to traverse each road. In the journey, a city or a road can be visited more than once.
|
The first line contains a single integer \(N\) (\(1 \le N \le 10^5\)) β the number of cities in Londonesia.The \(i\)-th of the next \(N-1\) lines contains three integers \(U_i\), \(V_i\), and \(W_i\) (\(1 \le U_i, V_i \le N\), \(1 \le W_i \le 10^9\)) β a two-directional road that connects cities \(U_i\) and \(V_i\) that takes \(W_i\) hours to be traversed. The roads in Londonesia form a tree.
|
Output one integer, which represents the minimum time in hours that is needed to visit each city at least once.
|
In the first example, the journey that has the minimum time is \(2 β 1 \xrightarrow{\text{teleport}} 4 β 3\).In the second example, the journey that has the minimum time is \(3 β 1 β 4 β 1 β 2 \xrightarrow{\text{teleport}} 5\).
|
Input: 4 1 2 4 2 3 5 3 4 4 | Output: 8
|
Expert
| 2 | 1,115 | 395 | 111 | 17 |
209 |
B
|
209B
|
B. Pixels
| 2,100 |
constructive algorithms; math
|
Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors x and y (x β y) meet in a violent fight, then the pixel that survives the fight immediately changes its color to z (z β x; z β y). Pixels of the same color are friends, so they don't fight.The King of Flatland knows that his land will be peaceful and prosperous when the pixels are of the same color. For each of the three colors you know the number of pixels of this color that inhabit Flatland. Help the king and determine whether fights can bring peace and prosperity to the country and if it is possible, find the minimum number of fights needed to make the land peaceful and prosperous.
|
The first line contains three space-separated integers a, b and c (0 β€ a, b, c β€ 231; a + b + c > 0) β the number of red, green and blue pixels, correspondingly.
|
Print a single number β the minimum number of pixel fights before the country becomes peaceful and prosperous. If making the country peaceful and prosperous is impossible, print -1.
|
In the first test sample the country needs only one fight to achieve peace and prosperity. Besides, it can be any fight whatsoever. For example, let's assume that the green and the blue pixels fight, then the surviving pixel will be red. As a result, after the fight there are two red pixels. There won't be other pixels.In the second sample the following sequence of fights is possible: red and blue, green and red, red and blue. As a result, after all fights there is one green pixel left.
|
Input: 1 1 1 | Output: 1
|
Hard
| 2 | 854 | 161 | 181 | 2 |
1,703 |
F
|
1703F
|
F. Yet Another Problem About Pairs Satisfying an Inequality
| 1,300 |
binary search; data structures; dp; greedy; sortings
|
You are given an array \(a_1, a_2, \dots a_n\). Count the number of pairs of indices \(1 \leq i, j \leq n\) such that \(a_i < i < a_j < j\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \leq a_i \leq 10^9\)) β the elements of the array.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the number of pairs of indices satisfying the condition in the statement.Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
|
For the first test cases the pairs are \((i, j)\) = \(\{(2, 4), (2, 8), (3, 8)\}\). The pair \((2, 4)\) is true because \(a_2 = 1\), \(a_4 = 3\) and \(1 < 2 < 3 < 4\). The pair \((2, 8)\) is true because \(a_2 = 1\), \(a_8 = 4\) and \(1 < 2 < 4 < 8\). The pair \((3, 8)\) is true because \(a_3 = 2\), \(a_8 = 4\) and \(2 < 3 < 4 < 8\).
|
Input: 581 1 2 3 8 2 1 421 2100 2 1 6 3 4 1 2 8 321 100000000030 1000000000 2 | Output: 3 0 10 0 1
|
Easy
| 5 | 140 | 443 | 305 | 17 |
620 |
D
|
620D
|
D. Professor GukiZ and Two Arrays
| 2,200 |
binary search; two pointers
|
Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is [5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5].Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.
|
The first line contains integer n (1 β€ n β€ 2000) β the number of elements in the array a.The second line contains n integers ai ( - 109 β€ ai β€ 109) β the elements of the array a.The third line contains integer m (1 β€ m β€ 2000) β the number of elements in the array b.The fourth line contains m integers bj ( - 109 β€ bj β€ 109) β the elements of the array b.
|
In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.The second line should contain the number of swaps k (0 β€ k β€ 2).Each of the next k lines should contain two integers xp, yp (1 β€ xp β€ n, 1 β€ yp β€ m) β the index of the element in the array a and the index of the element in the array b in the p-th swap.If there are several optimal solutions print any of them. Print the swaps in order the professor did them.
|
Input: 55 4 3 2 141 1 1 1 | Output: 121 14 2
|
Hard
| 2 | 807 | 356 | 459 | 6 |
|
159 |
A
|
159A
|
A. Friends or Not
| 1,400 |
*special; greedy; implementation
|
Polycarpus has a hobby β he develops an unusual social network. His work is almost completed, and there is only one more module to implement β the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user A sent user B a message at time t1, and user B sent user A a message at time t2. If 0 < t2 - t1 β€ d, then user B's message was an answer to user A's one. Users A and B are considered to be friends if A answered at least one B's message or B answered at least one A's message.You are given the log of messages in chronological order and a number d. Find all pairs of users who will be considered to be friends.
|
The first line of the input contains two integers n and d (1 β€ n, d β€ 1000). The next n lines contain the messages log. The i-th line contains one line of the log formatted as ""Ai Bi ti"" (without the quotes), which means that user Ai sent a message to user Bi at time ti (1 β€ i β€ n). Ai and Bi are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and ti is an integer (0 β€ ti β€ 10000). It is guaranteed that the lines are given in non-decreasing order of ti's and that no user sent a message to himself. The elements in the lines are separated by single spaces.
|
In the first line print integer k β the number of pairs of friends. In the next k lines print pairs of friends as ""Ai Bi"" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.
|
In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
|
Input: 4 1vasya petya 1petya vasya 2anya ivan 2ivan anya 4 | Output: 1petya vasya
|
Easy
| 3 | 734 | 607 | 253 | 1 |
958 |
F2
|
958F2
|
F2. Lightsabers (medium)
| 1,800 |
binary search; two pointers
|
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. Heidi has n Jedi Knights standing in front of her, each one with a lightsaber of one of m possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly k1 knights with lightsabers of the first color, k2 knights with lightsabers of the second color, ..., km knights with lightsabers of the m-th color.However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights.
|
The first line of the input contains n (1 β€ n β€ 2Β·105) and m (1 β€ m β€ n). The second line contains n integers in the range {1, 2, ..., m} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains m integers k1, k2, ..., km (with ) β the desired counts of Jedi Knights with lightsabers of each color from 1 to m.
|
Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output - 1.
|
Input: 8 33 3 1 2 2 1 1 33 1 1 | Output: 1
|
Medium
| 2 | 1,201 | 348 | 228 | 9 |
|
2,037 |
D
|
2037D
|
D. Sharky Surfing
| 1,300 |
data structures; greedy; two pointers
|
Mualani loves surfing on her sharky surfboard!Mualani's surf path can be modeled by a number line. She starts at position \(1\), and the path ends at position \(L\). When she is at position \(x\) with a jump power of \(k\), she can jump to any integer position in the interval \([x, x+k]\). Initially, her jump power is \(1\). However, her surf path isn't completely smooth. There are \(n\) hurdles on her path. Each hurdle is represented by an interval \([l, r]\), meaning she cannot jump to any position in the interval \([l, r]\). There are also \(m\) power-ups at certain positions on the path. Power-up \(i\) is located at position \(x_i\) and has a value of \(v_i\). When Mualani is at position \(x_i\), she has the option to collect the power-up to increase her jump power by \(v_i\). There may be multiple power-ups at the same position. When she is at a position with some power-ups, she may choose to take or ignore each individual power-up. No power-up is in the interval of any hurdle.What is the minimum number of power-ups she must collect to reach position \(L\) to finish the path? If it is not possible to finish the surf path, output \(-1\).
|
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 \(L\) (\(1 \leq n, m \leq 2 \cdot 10^5, 3 \leq L \leq 10^9\)) β the number of hurdles, the number of power-ups, and the position of the end.The following \(n\) lines contain two integers \(l_i\) and \(r_i\) (\(2 \leq l_i \leq r_i \leq L-1\)) β the bounds of the interval for the \(i\)'th hurdle. It is guaranteed that \(r_i + 1 < l_{i+1}\) for all \(1 \leq i < n\) (i.e. all hurdles are non-overlapping, sorted by increasing positions, and the end point of a previous hurdle is not consecutive with the start point of the next hurdle).The following \(m\) lines contain two integers \(x_i\) and \(v_i\) (\(1 \leq x_i, v_i \leq L\)) β the position and the value for the \(i\)'th power-up. There may be multiple power-ups with the same \(x\). It is guaranteed that \(x_i \leq x_{i+1}\) for all \(1 \leq i < m\) (i.e. the power-ups are sorted by non-decreasing position) and no power-up is in the interval of any hurdle.It is guaranteed the sum of \(n\) and the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the minimum number of power-ups she must collect to reach position \(L\). If it is not possible, output \(-1\).
|
In the first test case, she can collect power-ups \(1\), \(2\), \(3\), and \(5\) to clear all hurdles.In the second test case, she cannot jump over the first hurdle.In the fourth test case, by collecting both power-ups, she can jump over the hurdle.
|
Input: 42 5 507 1430 402 23 13 518 222 324 3 504 615 1820 2634 381 28 210 21 4 1710 141 61 21 216 91 2 105 92 32 2 | Output: 4 -1 1 2
|
Easy
| 3 | 1,159 | 1,192 | 138 | 20 |
1,129 |
B
|
1129B
|
B. Wrong Answer
| 2,000 |
constructive algorithms
|
Consider the following problem: given an array \(a\) containing \(n\) integers (indexed from \(0\) to \(n-1\)), find \(\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i\). In this problem, \(1 \leq n \leq 2\,000\) and \(|a_i| \leq 10^6\).In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:function find_answer(n, a) # Assumes n is an integer between 1 and 2000, inclusive # Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1] res = 0 cur = 0 k = -1 for i = 0 to i = n-1 cur = cur + a[i] if cur < 0 cur = 0 k = i res = max(res, (i-k)*cur) return resAlso, as you can see, Alice's idea is not entirely correct. For example, suppose \(n = 4\) and \(a = [6, -8, 7, -42]\). Then, find_answer(n, a) would return \(7\), but the correct answer is \(3 \cdot (6-8+7) = 15\).You told Alice that her solution is incorrect, but she did not believe what you said.Given an integer \(k\), you are to find any sequence \(a\) of \(n\) integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly \(k\). Note that although the choice of \(n\) and the content of the sequence is yours, you must still follow the constraints earlier given: that \(1 \leq n \leq 2\,000\) and that the absolute value of each element does not exceed \(10^6\). If there is no such sequence, determine so.
|
The first and only line contains one integer \(k\) (\(1 \leq k \leq 10^9\)).
|
If there is no sought sequence, print ""-1"".Otherwise, in the first line, print one integer \(n\) (\(1 \leq n \leq 2\,000\)), denoting the number of elements in the sequence.Then, in the second line, print \(n\) space-separated integers: \(a_0, a_1, \ldots, a_{n-1}\) (\(|a_i| \leq 10^6\)).
|
The first sample corresponds to the example given in the problem statement.In the second sample, one answer is \(n = 7\) with \(a = [30, -12, -99, 123, -2, 245, -300]\), in which case find_answer(n, a) returns \(1098\), while the correct answer is \(1710\).
|
Input: 8 | Output: 4 6 -8 7 -42
|
Hard
| 1 | 1,468 | 76 | 291 | 11 |
1,974 |
C
|
1974C
|
C. Beautiful Triple Pairs
| 1,400 |
combinatorics; data structures
|
Polycarp was given an array \(a\) of \(n\) integers. He really likes triples of numbers, so for each \(j\) (\(1 \le j \le n - 2\)) he wrote down a triple of elements \([a_j, a_{j + 1}, a_{j + 2}]\).Polycarp considers a pair of triples \(b\) and \(c\) beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied: \(b_1 \ne c_1\) and \(b_2 = c_2\) and \(b_3 = c_3\); \(b_1 = c_1\) and \(b_2 \ne c_2\) and \(b_3 = c_3\); \(b_1 = c_1\) and \(b_2 = c_2\) and \(b_3 \ne c_3\). Find the number of beautiful pairs of triples among the written triples \([a_j, a_{j + 1}, a_{j + 2}]\).
|
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\) (\(3 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)) β the elements of the array.It is guaranteed that the sum of the values of \(n\) for all test cases in the test does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the number of beautiful pairs of triples among the pairs of the form \([a_j, a_{j + 1}, a_{j + 2}]\).Note that the answer may not fit into 32-bit data types.
|
In the first example, \(a = [3, 2, 2, 2, 3]\), Polycarp will write the following triples: \([3, 2, 2]\); \([2, 2, 2]\); \([2, 2, 3]\). The beautiful pairs are triple \(1\) with triple \(2\) and triple \(2\) with triple \(3\).In the third example, \(a = [1, 2, 3, 2, 2, 3, 4, 2]\), Polycarp will write the following triples: \([1, 2, 3]\); \([2, 3, 2]\); \([3, 2, 2]\); \([2, 2, 3]\); \([2, 3, 4]\); \([3, 4, 2]\); The beautiful pairs are triple \(1\) with triple \(4\), triple \(2\) with triple \(5\), and triple \(3\) with triple \(6\).
|
Input: 853 2 2 2 351 2 1 2 181 2 3 2 2 3 4 242 1 1 182 1 1 2 1 1 1 172 1 1 1 1 1 162 1 1 1 1 152 1 1 1 1 | Output: 2 0 3 1 8 4 3 2
|
Easy
| 2 | 619 | 478 | 203 | 19 |
690 |
C2
|
690C2
|
C2. Brain Network (medium)
| 1,500 |
dfs and similar; graphs; trees
|
Further research on zombie thought processes yielded interesting results. As we know from the previous problem, the nervous system of a zombie consists of n brains and m brain connectors joining some pairs of brains together. It was observed that the intellectual abilities of a zombie depend mainly on the topology of its nervous system. More precisely, we define the distance between two brains u and v (1 β€ u, v β€ n) as the minimum number of brain connectors used when transmitting a thought between these two brains. The brain latency of a zombie is defined to be the maximum distance between any two of its brains. Researchers conjecture that the brain latency is the crucial parameter which determines how smart a given zombie is. Help them test this conjecture by writing a program to compute brain latencies of nervous systems.In this problem you may assume that any nervous system given in the input is valid, i.e., it satisfies conditions (1) and (2) from the easy version.
|
The first line of the input contains two space-separated integers n and m (1 β€ n, m β€ 100000) denoting the number of brains (which are conveniently numbered from 1 to n) and the number of brain connectors in the nervous system, respectively. In the next m lines, descriptions of brain connectors follow. Every connector is given as a pair of brains a b it connects (1 β€ a, b β€ n and a β b).
|
Print one number β the brain latency.
|
Input: 4 31 21 31 4 | Output: 2
|
Medium
| 3 | 983 | 390 | 37 | 6 |
|
193 |
B
|
193B
|
B. Xor
| 2,000 |
brute force
|
John Doe has four arrays: a, b, k, and p. Each array consists of n integers. Elements of all arrays are indexed starting from 1. Array p is a permutation of integers 1 to n.John invented a game for his friends and himself. Initially a player is given array a. The player must consecutively execute exactly u operations on a. You are permitted to execute the following operations: Operation 1: For each change ai into . Expression means applying the operation of a bitwise xor to numbers x and y. The given operation exists in all modern programming languages, for example, in language C++ and Java it is marked as ""^"", in Pascal β as ""xor"". Operation 2: For each change ai into api + r. When this operation is executed, all changes are made at the same time. After all u operations are applied, the number of points the player gets is determined by the formula . John wants to find out what maximum number of points a player can win in his game. Help him.
|
The first line contains space-separated integers n, u and r (1 β€ n, u β€ 30, 0 β€ r β€ 100) β the number of elements in each array, the number of operations and the number that describes one of the operations. Each of the next four lines contains n space-separated integers β arrays a, b, k, p. The first line has array a, the second line has array b, the third line has array k and the fourth one has array p. It is guaranteed that elements of arrays a and b are positive and do not exceed 104 (1 β€ ai, bi β€ 104), elements of array k do not exceed 104 in the absolute value (|k| β€ 104) and p is a permutation of numbers from 1 to n.
|
On a single line print number s β the maximum number of points that a player can win in John's game.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In the first sample you should first apply the operation of the first type, then the operation of the second type.
|
Input: 3 2 17 7 78 8 81 2 31 3 2 | Output: 96
|
Hard
| 1 | 959 | 630 | 247 | 1 |
1,010 |
B
|
1010B
|
B. Rocket
| 1,800 |
binary search; interactive
|
This is an interactive problem.Natasha is going to fly to Mars. Finally, Natasha sat in the rocket. She flies, flies... but gets bored. She wishes to arrive to Mars already! So she decides to find something to occupy herself. She couldn't think of anything better to do than to calculate the distance to the red planet.Let's define \(x\) as the distance to Mars. Unfortunately, Natasha does not know \(x\). But it is known that \(1 \le x \le m\), where Natasha knows the number \(m\). Besides, \(x\) and \(m\) are positive integers.Natasha can ask the rocket questions. Every question is an integer \(y\) (\(1 \le y \le m\)). The correct answer to the question is \(-1\), if \(x<y\), \(0\), if \(x=y\), and \(1\), if \(x>y\). But the rocket is broken β it does not always answer correctly. Precisely: let the correct answer to the current question be equal to \(t\), then, if the rocket answers this question correctly, then it will answer \(t\), otherwise it will answer \(-t\).In addition, the rocket has a sequence \(p\) of length \(n\). Each element of the sequence is either \(0\) or \(1\). The rocket processes this sequence in the cyclic order, that is \(1\)-st element, \(2\)-nd, \(3\)-rd, \(\ldots\), \((n-1)\)-th, \(n\)-th, \(1\)-st, \(2\)-nd, \(3\)-rd, \(\ldots\), \((n-1)\)-th, \(n\)-th, \(\ldots\). If the current element is \(1\), the rocket answers correctly, if \(0\) β lies. Natasha doesn't know the sequence \(p\), but she knows its length β \(n\).You can ask the rocket no more than \(60\) questions.Help Natasha find the distance to Mars. Assume, that the distance to Mars does not change while Natasha is asking questions.Your solution will not be accepted, if it does not receive an answer \(0\) from the rocket (even if the distance to Mars is uniquely determined by the already received rocket's answers).
|
The first line contains two integers \(m\) and \(n\) (\(1 \le m \le 10^9\), \(1 \le n \le 30\)) β the maximum distance to Mars and the number of elements in the sequence \(p\).
|
In the example, hacking would look like this:5 2 31 0This means that the current distance to Mars is equal to \(3\), Natasha knows that it does not exceed \(5\), and the rocket answers in order: correctly, incorrectly, correctly, incorrectly ...Really:on the first query (\(1\)) the correct answer is \(1\), the rocket answered correctly: \(1\);on the second query (\(2\)) the correct answer is \(1\), the rocket answered incorrectly: \(-1\);on the third query (\(4\)) the correct answer is \(-1\), the rocket answered correctly: \(-1\);on the fourth query (\(5\)) the correct answer is \(-1\), the rocket answered incorrectly: \(1\);on the fifth query (\(3\)) the correct and incorrect answer is \(0\).
|
Input: 5 21-1-110 | Output: 12453
|
Medium
| 2 | 1,829 | 176 | 0 | 10 |
|
702 |
F
|
702F
|
F. T-Shirts
| 2,800 |
data structures
|
The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all n types of t-shirts go on sale. The t-shirt of the i-th type has two integer parameters β ci and qi, where ci β is the price of the i-th type t-shirt, qi β is the quality of the i-th type t-shirt. It should be assumed that the unlimited number of t-shirts of each type goes on sale in the shop, but in general the quality is not concerned with the price. As predicted, k customers will come to the shop within the next month, the j-th customer will get ready to spend up to bj on buying t-shirts. All customers have the same strategy. First of all, the customer wants to buy the maximum possible number of the highest quality t-shirts, then to buy the maximum possible number of the highest quality t-shirts from residuary t-shirts and so on. At the same time among several same quality t-shirts the customer will buy one that is cheaper. The customers don't like the same t-shirts, so each customer will not buy more than one t-shirt of one type. Determine the number of t-shirts which each customer will buy, if they use the described strategy. All customers act independently from each other, and the purchase of one does not affect the purchase of another.
|
The first line contains the positive integer n (1 β€ n β€ 2Β·105) β the number of t-shirt types. Each of the following n lines contains two integers ci and qi (1 β€ ci, qi β€ 109) β the price and the quality of the i-th type t-shirt.The next line contains the positive integer k (1 β€ k β€ 2Β·105) β the number of the customers. The next line contains k positive integers b1, b2, ..., bk (1 β€ bj β€ 109), where the j-th number is equal to the sum, which the j-th customer gets ready to spend on t-shirts.
|
The first line of the input data should contain the sequence of k integers, where the i-th number should be equal to the number of t-shirts, which the i-th customer will buy.
|
In the first example the first customer will buy the t-shirt of the second type, then the t-shirt of the first type. He will spend 10 and will not be able to buy the t-shirt of the third type because it costs 4, and the customer will owe only 3. The second customer will buy all three t-shirts (at first, the t-shirt of the second type, then the t-shirt of the first type, and then the t-shirt of the third type). He will spend all money on it.
|
Input: 37 53 54 3213 14 | Output: 2 3
|
Master
| 1 | 1,260 | 495 | 174 | 7 |
1,932 |
E
|
1932E
|
E. Final Countdown
| 1,600 |
implementation; math; number theory
|
You are in a nuclear laboratory that is about to explode and destroy the Earth. You must save the Earth before the final countdown reaches zero. The countdown consists of \(n\) (\(1 \le n \le 4 \cdot 10^5\)) mechanical indicators, each showing one decimal digit. You noticed that when the countdown changes its state from \(x\) to \(x-1\), it doesn't happen in one move. Instead, each change of a single digit takes one second. So, for example, if the countdown shows 42, then it will change to 41 in one second, because only one digit is changed, but if the countdown shows 2300, then it will change to 2299 in three seconds, because the three last digits are changed.Find out how much time is left before the countdown reaches zero.
|
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1\le n\le 4\cdot 10^5\)). The second line contains a string of \(n\) digits, the current state of the countdown. It is guaranteed that at least one digit is not zero.The sum of \(n\) for all tests does not exceed \(4\cdot 10^5\).
|
For each test case, print a single integer without leading zeroes, the number of seconds left before the countdown reaches zero. Note that this number may be huge.
|
In the first example, there are four changes that take 2 seconds: 40 to 39, 30 to 29, 20 to 19, and 10 to 09, other changes take 1 second each. So the total time is \(2\cdot 4 + 1\cdot(42-4) = 46\).
|
Input: 52425123452994000527456480697259671309012631002 | Output: 46 13715 108 5 507200774732968121125145546
|
Medium
| 3 | 734 | 451 | 163 | 19 |
1,822 |
E
|
1822E
|
E. Making Anti-Palindromes
| 1,600 |
greedy; math; strings
|
You are given a string \(s\), consisting of lowercase English letters. In one operation, you are allowed to swap any two characters of the string \(s\).A string \(s\) of length \(n\) is called an anti-palindrome, if \(s[i] \ne s[n - i + 1]\) for every \(i\) (\(1 \le i \le n\)). For example, the strings ""codeforces"", ""string"" are anti-palindromes, but the strings ""abacaba"", ""abc"", ""test"" are not.Determine the minimum number of operations required to make the string \(s\) an anti-palindrome, or output \(-1\), if this is not possible.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.Each test case consists of two lines. The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the string \(s\).The second line contains the string \(s\), consisting of \(n\) lowercase English letters.The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of operations required to make the string \(s\) an anti-palindrome, or \(-1\) if this is not possible.
|
In the first test case, the string ""codeforces"" is already an anti-palindrome, so the answer is \(0\).In the second test case, it can be shown that the string ""abc"" cannot be transformed into an anti-palindrome by performing the allowed operations, so the answer is \(-1\).In the third test case, it is enough to swap the second and the fifth characters of the string ""taarrrataa"", and the new string ""trararataa"" will be an anti-palindrome, so the answer is \(1\).
|
Input: 1010codeforces3abc10taarrrataa10dcbdbdcccc4wwww12cabbaccabaac10aadaaaaddc14aacdaaaacadcdc6abccba12dcbcaebacccd | Output: 0 -1 1 1 -1 3 -1 2 2 2
|
Medium
| 3 | 547 | 448 | 167 | 18 |
154 |
B
|
154B
|
B. Colliders
| 1,600 |
math; number theory
|
By 2312 there were n Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number from 1 to n. However, scientists did not know what activating several colliders simultaneously could cause, so the colliders were deactivated.In 2312 there was a startling discovery: a collider's activity is safe if and only if all numbers of activated colliders are pairwise relatively prime to each other (two numbers are relatively prime if their greatest common divisor equals 1)! If two colliders with relatively nonprime numbers are activated, it will cause a global collapse.Upon learning this, physicists rushed to turn the colliders on and off and carry out all sorts of experiments. To make sure than the scientists' quickness doesn't end with big trouble, the Large Hadron Colliders' Large Remote Control was created. You are commissioned to write the software for the remote (well, you do not expect anybody to operate it manually, do you?).Initially, all colliders are deactivated. Your program receives multiple requests of the form ""activate/deactivate the i-th collider"". The program should handle requests in the order of receiving them. The program should print the processed results in the format described below.To the request of ""+ i"" (that is, to activate the i-th collider), the program should print exactly one of the following responses: ""Success"" if the activation was successful. ""Already on"", if the i-th collider was already activated before the request. ""Conflict with j"", if there is a conflict with the j-th collider (that is, the j-th collider is on, and numbers i and j are not relatively prime). In this case, the i-th collider shouldn't be activated. If a conflict occurs with several colliders simultaneously, you should print the number of any of them. The request of ""- i"" (that is, to deactivate the i-th collider), should receive one of the following responses from the program: ""Success"", if the deactivation was successful. ""Already off"", if the i-th collider was already deactivated before the request. You don't need to print quotes in the output of the responses to the requests.
|
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the number of colliders and the number of requests, correspondingly.Next m lines contain numbers of requests, one per line, in the form of either ""+ i"" (without the quotes) β activate the i-th collider, or ""- i"" (without the quotes) β deactivate the i-th collider (1 β€ i β€ n).
|
Print m lines β the results of executing requests in the above given format. The requests should be processed in the order, in which they are given in the input. Don't forget that the responses to the requests should be printed without quotes.
|
Note that in the sample the colliders don't turn on after the second and ninth requests. The ninth request could also receive response ""Conflict with 3"".
|
Input: 10 10+ 6+ 10+ 5- 10- 5- 6+ 10+ 3+ 6+ 3 | Output: SuccessConflict with 6SuccessAlready offSuccessSuccessSuccessSuccessConflict with 10Already on
|
Medium
| 2 | 2,177 | 360 | 243 | 1 |
2,068 |
F
|
2068F
|
F. Mascot Naming
| 1,900 |
brute force; greedy; implementation; strings
|
When organizing a big event, organizers often handle side tasks outside their expertise. For example, the chief judge of EUC 2025 must find a name for the event's official mascot while satisfying certain constraints: The name must include specific words as subsequences\(^{\texttt{*}}\), such as the event name and location. You are given the list \(s_1,\, s_2,\, \ldots,\, s_n\) of the \(n\) required words. The name must not contain as a subsequence\(^{\texttt{*}}\) the name \(t\) of last year's mascot. Please help the chief judge find a valid mascot name or determine that none exists.\(^{\texttt{*}}\) A string \(x\) is a subsequence of a string \(y\) if \(x\) can be obtained from \(y\) by erasing some characters (at any positions) while keeping the remaining characters in the same order. For example, \(\texttt{abc}\) is a subsequence of \(\texttt{axbycz}\) but not of \(\texttt{acbxyz}\).
|
The first line contains an integer \(n\) (\(1\le n\le 200\,000\)) β the number of words that shall appear as subsequences.The \(i\)-th of the following \(n\) lines contains the string \(s_i\) (\(1\le |s_i| \le 200\,000\), \(s_i\) consists of lowercase English letters) β the \(i\)-th word in the list of words that shall appear as subsequences. The total length of these \(n\) words is at most \(200\,000\), i.e., \(|s_1| + |s_2| + \cdots + |s_n| \le 200\,000\).The last line contains the string \(t\) (\(1\le |t| \le 200\,000\), \(t\) consists of lowercase English letters) β the name of last year's mascot.
|
Print \(\texttt{YES}\) if there is a valid name for the mascot. Otherwise, print \(\texttt{NO}\).If there is a valid name, on the next line print a valid name. The string you print must have length at most \(1\,000\,000\) and must consist of lowercase English letters. One can prove that if a valid name for the mascot exists, then there is one satisfying these additional constraints.If there are multiple solutions, print any of them.
|
In the first sample, the words that must appear as subsequences are \(\texttt{porto}\) and \(\texttt{euc}\), while the word that must not appear as a subsequence is \(\texttt{prague}\). There are many valid names for the mascot, e.g., \(\texttt{poretuco}\) or \(\texttt{xxxpppeortoucyyyy}\).If \(\texttt{poretuco}\) is chosen as the name of the mascot, observe that \(\texttt{porto}\) and \(\texttt{euc}\) are subsequences (highlighted in \(\texttt{POReTucO}\) and \(\texttt{porEtUCo}\), respectively), while \(\texttt{prague}\) is not, as desired.The string \(\texttt{poretuc}\) is not a valid mascot name because it does not contain the word \(\texttt{porto}\) as a subsequence. Also the string \(\texttt{poretucoague}\) is not a valid mascot name because it contains \(\texttt{prague}\) as a subsequence.
|
Input: 2portoeucprague | Output: YES poretuco
|
Hard
| 4 | 899 | 608 | 436 | 20 |
63 |
D
|
63D
|
D. Dividing Island
| 1,900 |
constructive algorithms
|
A revolution took place on the Buka Island. New government replaced the old one. The new government includes n parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island.The island can be conventionally represented as two rectangles a Γ b and c Γ d unit squares in size correspondingly. The rectangles are located close to each other. At that, one of the sides with the length of a and one of the sides with the length of c lie on one line. You can see this in more details on the picture. The i-th party is entitled to a part of the island equal to xi unit squares. Every such part should fully cover several squares of the island (it is not allowed to cover the squares partially) and be a connected figure. A ""connected figure"" presupposes that from any square of this party one can move to any other square of the same party moving through edge-adjacent squares also belonging to that party.Your task is to divide the island between parties.
|
The first line contains 5 space-separated integers β a, b, c, d and n (1 β€ a, b, c, d β€ 50, b β d, 1 β€ n β€ 26). The second line contains n space-separated numbers. The i-th of them is equal to number xi (1 β€ xi β€ a Γ b + c Γ d). It is guaranteed that .
|
If dividing the island between parties in the required manner is impossible, print ""NO"" (without the quotes). Otherwise, print ""YES"" (also without the quotes) and, starting from the next line, print max(b, d) lines each containing a + c characters. To mark what square should belong to what party, use lowercase Latin letters. For the party that is first in order in the input data, use ""a"", for the second one use ""b"" and so on. Use ""."" for the squares that belong to the sea. The first symbol of the second line of the output data should correspond to the square that belongs to the rectangle a Γ b. The last symbol of the second line should correspond to the square that belongs to the rectangle c Γ d.If there are several solutions output any.
|
Input: 3 4 2 2 35 8 3 | Output: YESaaabbaabbbcbb..ccb..
|
Hard
| 1 | 1,041 | 252 | 757 | 0 |
|
74 |
A
|
74A
|
A. Room Leader
| 1,000 |
implementation
|
Let us remind you part of the rules of Codeforces. The given rules slightly simplified, use the problem statement as a formal document.In the beginning of the round the contestants are divided into rooms. Each room contains exactly n participants. During the contest the participants are suggested to solve five problems, A, B, C, D and E. For each of these problem, depending on when the given problem was solved and whether it was solved at all, the participants receive some points. Besides, a contestant can perform hacks on other contestants. For each successful hack a contestant earns 100 points, for each unsuccessful hack a contestant loses 50 points. The number of points for every contestant is represented by the sum of points he has received from all his problems, including hacks.You are suggested to determine the leader for some room; the leader is a participant who has maximum points.
|
The first line contains an integer n, which is the number of contestants in the room (1 β€ n β€ 50). The next n lines contain the participants of a given room. The i-th line has the format of ""handlei plusi minusi ai bi ci di ei"" β it is the handle of a contestant, the number of successful hacks, the number of unsuccessful hacks and the number of points he has received from problems A, B, C, D, E correspondingly. The handle of each participant consists of Latin letters, digits and underscores and has the length from 1 to 20 characters. There are the following limitations imposed upon the numbers: 0 β€ plusi, minusi β€ 50; 150 β€ ai β€ 500 or ai = 0, if problem A is not solved; 300 β€ bi β€ 1000 or bi = 0, if problem B is not solved; 450 β€ ci β€ 1500 or ci = 0, if problem C is not solved; 600 β€ di β€ 2000 or di = 0, if problem D is not solved; 750 β€ ei β€ 2500 or ei = 0, if problem E is not solved. All the numbers are integer. All the participants have different handles. It is guaranteed that there is exactly one leader in the room (i.e. there are no two participants with the maximal number of points).
|
Print on the single line the handle of the room leader.
|
The number of points that each participant from the example earns, are as follows: Petr β 3860 tourist β 4140 Egor β 4030 c00lH4x0R β - 350 some_participant β 2220 Thus, the leader of the room is tourist.
|
Input: 5Petr 3 1 490 920 1000 1200 0tourist 2 0 490 950 1100 1400 0Egor 7 0 480 900 950 0 1000c00lH4x0R 0 10 150 0 0 0 0some_participant 2 1 450 720 900 0 0 | Output: tourist
|
Beginner
| 1 | 902 | 1,109 | 55 | 0 |
1,470 |
B
|
1470B
|
B. Strange Definition
| 1,900 |
bitmasks; graphs; hashing; math; number theory
|
Let us call two integers \(x\) and \(y\) adjacent if \(\frac{lcm(x, y)}{gcd(x, y)}\) is a perfect square. For example, \(3\) and \(12\) are adjacent, but \(6\) and \(9\) are not.Here \(gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\), and \(lcm(x, y)\) denotes the least common multiple (LCM) of integers \(x\) and \(y\).You are given an array \(a\) of length \(n\). Each second the following happens: each element \(a_i\) of the array is replaced by the product of all elements of the array (including itself), that are adjacent to the current value. Let \(d_i\) be the number of adjacent elements to \(a_i\) (including \(a_i\) itself). The beauty of the array is defined as \(\max_{1 \le i \le n} d_i\). You are given \(q\) queries: each query is described by an integer \(w\), and you have to output the beauty of the array after \(w\) seconds.
|
The first input line contains a single integer \(t\) (\(1 \le t \le 10^5)\) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the length of the array.The following line contains \(n\) integers \(a_1, \ldots, a_n\) (\(1 \le a_i \le 10^6\)) β array elements. The next line contain a single integer \(q\) (\(1 \le q \le 3 \cdot 10^5\)) β the number of queries.The following \(q\) lines contain a single integer \(w\) each (\(0 \le w \le 10^{18}\)) β the queries themselves.It is guaranteed that the sum of values \(n\) over all test cases does not exceed \(3 \cdot 10^5\), and the sum of values \(q\) over all test cases does not exceed \(3 \cdot 10^5\)
|
For each query output a single integer β the beauty of the array at the corresponding moment.
|
In the first test case, the initial array contains elements \([6, 8, 4, 2]\). Element \(a_4=2\) in this array is adjacent to \(a_4=2\) (since \(\frac{lcm(2, 2)}{gcd(2, 2)}=\frac{2}{2}=1=1^2\)) and \(a_2=8\) (since \(\frac{lcm(8,2)}{gcd(8, 2)}=\frac{8}{2}=4=2^2\)). Hence, \(d_4=2\), and this is the maximal possible value \(d_i\) in this array.In the second test case, the initial array contains elements \([12, 3, 20, 5, 80, 1]\). The elements adjacent to \(12\) are \(\{12, 3\}\), the elements adjacent to \(3\) are \(\{12, 3\}\), the elements adjacent to \(20\) are \(\{20, 5, 80\}\), the elements adjacent to \(5\) are \(\{20, 5, 80\}\), the elements adjacent to \(80\) are \(\{20, 5, 80\}\), the elements adjacent to \(1\) are \(\{1\}\). After one second, the array is transformed into \([36, 36, 8000, 8000, 8000, 1]\).
|
Input: 2 4 6 8 4 2 1 0 6 12 3 20 5 80 1 1 1 | Output: 2 3
|
Hard
| 5 | 880 | 725 | 93 | 14 |
1,011 |
A
|
1011A
|
A. Stages
| 900 |
greedy; implementation; sortings
|
Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string β concatenation of letters, which correspond to the stages.There are \(n\) stages available. The rocket must contain exactly \(k\) of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter 'c' can't go letters 'a', 'b', 'c' and 'd', but can go letters 'e', 'f', ..., 'z'.For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage 'a 'weighs one ton,' b 'weighs two tons, and' z' β \(26\) tons.Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once.
|
The first line of input contains two integers β \(n\) and \(k\) (\(1 \le k \le n \le 50\)) β the number of available stages and the number of stages to use in the rocket.The second line contains string \(s\), which consists of exactly \(n\) lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once.
|
Print a single integer β the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.
|
In the first example, the following rockets satisfy the condition: ""adx"" (weight is \(1+4+24=29\)); ""ady"" (weight is \(1+4+25=30\)); ""bdx"" (weight is \(2+4+24=30\)); ""bdy"" (weight is \(2+4+25=31\)).Rocket ""adx"" has the minimal weight, so the answer is \(29\).In the second example, target rocket is ""belo"". Its weight is \(2+5+12+15=34\).In the third example, \(n=k=2\), so the rocket must have both stages: 'a' and 'b'. This rocket doesn't satisfy the condition, because these letters are adjacent in the alphabet. Answer is -1.
|
Input: 5 3xyabd | Output: 29
|
Beginner
| 3 | 1,138 | 374 | 118 | 10 |
447 |
A
|
447A
|
A. DZY Loves Hash
| 800 |
implementation
|
DZY has a hash table with p buckets, numbered from 0 to p - 1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x) = x mod p. Operation a mod b denotes taking a remainder after division a by b.However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a ""conflict"" happens. Suppose the first conflict happens right after the i-th insertion, you should output i. If no conflict happens, just output -1.
|
The first line contains two integers, p and n (2 β€ p, n β€ 300). Then n lines follow. The i-th of them contains an integer xi (0 β€ xi β€ 109).
|
Output a single integer β the answer to the problem.
|
Input: 10 5021534153 | Output: 4
|
Beginner
| 1 | 657 | 140 | 52 | 4 |
|
1,443 |
E
|
1443E
|
E. Long Permutation
| 2,400 |
brute force; math; two pointers
|
A permutation is a sequence of integers from \(1\) to \(n\) of length \(n\) containing each number exactly once. For example, \([1]\), \([4, 3, 5, 1, 2]\), \([3, 2, 1]\) β are permutations, and \([1, 1]\), \([4, 3, 1]\), \([2, 3, 4]\) β no.Permutation \(a\) is lexicographically smaller than permutation \(b\) (they have the same length \(n\)), if in the first index \(i\) in which they differ, \(a[i] < b[i]\). For example, the permutation \([1, 3, 2, 4]\) is lexicographically smaller than the permutation \([1, 3, 4, 2]\), because the first two elements are equal, and the third element in the first permutation is smaller than in the second.The next permutation for a permutation \(a\) of length \(n\) β is the lexicographically smallest permutation \(b\) of length \(n\) that lexicographically larger than \(a\). For example: for permutation \([2, 1, 4, 3]\) the next permutation is \([2, 3, 1, 4]\); for permutation \([1, 2, 3]\) the next permutation is \([1, 3, 2]\); for permutation \([2, 1]\) next permutation does not exist. You are given the number \(n\) β the length of the initial permutation. The initial permutation has the form \(a = [1, 2, \ldots, n]\). In other words, \(a[i] = i\) (\(1 \le i \le n\)).You need to process \(q\) queries of two types: \(1\) \(l\) \(r\): query for the sum of all elements on the segment \([l, r]\). More formally, you need to find \(a[l] + a[l + 1] + \ldots + a[r]\). \(2\) \(x\): \(x\) times replace the current permutation with the next permutation. For example, if \(x=2\) and the current permutation has the form \([1, 3, 4, 2]\), then we should perform such a chain of replacements \([1, 3, 4, 2] \rightarrow [1, 4, 2, 3] \rightarrow [1, 4, 3, 2]\). For each query of the \(1\)-st type output the required sum.
|
The first line contains two integers \(n\) (\(2 \le n \le 2 \cdot 10^5\)) and \(q\) (\(1 \le q \le 2 \cdot 10^5\)), where \(n\) β the length of the initial permutation, and \(q\) β the number of queries.The next \(q\) lines contain a single query of the \(1\)-st or \(2\)-nd type. The \(1\)-st type query consists of three integers \(1\), \(l\) and \(r\) \((1 \le l \le r \le n)\), the \(2\)-nd type query consists of two integers \(2\) and \(x\) \((1 \le x \le 10^5)\).It is guaranteed that all requests of the \(2\)-nd type are possible to process.
|
For each query of the \(1\)-st type, output on a separate line one integer β the required sum.
|
Initially, the permutation has the form \([1, 2, 3, 4]\). Queries processing is as follows: \(2 + 3 + 4 = 9\); \([1, 2, 3, 4] \rightarrow [1, 2, 4, 3] \rightarrow [1, 3, 2, 4] \rightarrow [1, 3, 4, 2]\); \(1 + 3 = 4\); \(4 + 2 = 6\)
|
Input: 4 4 1 2 4 2 3 1 1 2 1 3 4 | Output: 9 4 6
|
Expert
| 3 | 1,764 | 550 | 94 | 14 |
309 |
D
|
309D
|
D. Tennis Rackets
| 2,700 |
brute force; geometry
|
Professional sport is more than hard work. It also is the equipment, designed by top engineers. As an example, let's take tennis. Not only should you be in great shape, you also need an excellent racket! In this problem your task is to contribute to the development of tennis and to help to design a revolutionary new concept of a racket!The concept is a triangular racket. Ant it should be not just any triangle, but a regular one. As soon as you've chosen the shape, you need to stretch the net. By the time you came the rocket had n holes drilled on each of its sides. The holes divide each side into equal n + 1 parts. At that, the m closest to each apex holes on each side are made for better ventilation only and you cannot stretch the net through them. The next revolutionary idea as to stretch the net as obtuse triangles through the holes, so that for each triangle all apexes lay on different sides. Moreover, you need the net to be stretched along every possible obtuse triangle. That's where we need your help β help us to count the number of triangles the net is going to consist of.Two triangles are considered to be different if their pictures on the fixed at some position racket are different.
|
The first and the only input line contains two integers n, m .
|
Print a single number β the answer to the problem.
|
For the following picture n = 8, m = 2. White circles are the holes for ventilation, red circles β holes for net stretching. One of the possible obtuse triangles is painted red.
|
Input: 3 0 | Output: 9
|
Master
| 2 | 1,210 | 62 | 50 | 3 |
1,207 |
F
|
1207F
|
F. Remainder Problem
| 2,100 |
brute force; data structures; implementation
|
You are given an array \(a\) consisting of \(500000\) integers (numbered from \(1\) to \(500000\)). Initially all elements of \(a\) are zero.You have to process two types of queries to this array: \(1\) \(x\) \(y\) β increase \(a_x\) by \(y\); \(2\) \(x\) \(y\) β compute \(\sum\limits_{i \in R(x, y)} a_i\), where \(R(x, y)\) is the set of all integers from \(1\) to \(500000\) which have remainder \(y\) modulo \(x\). Can you process all the queries?
|
The first line contains one integer \(q\) (\(1 \le q \le 500000\)) β the number of queries.Then \(q\) lines follow, each describing a query. The \(i\)-th line contains three integers \(t_i\), \(x_i\) and \(y_i\) (\(1 \le t_i \le 2\)). If \(t_i = 1\), then it is a query of the first type, \(1 \le x_i \le 500000\), and \(-1000 \le y_i \le 1000\). If \(t_i = 2\), then it it a query of the second type, \(1 \le x_i \le 500000\), and \(0 \le y_i < x_i\).It is guaranteed that there will be at least one query of type \(2\).
|
For each query of type \(2\) print one integer β the answer to it.
|
Input: 5 1 3 4 2 3 0 2 4 3 1 4 -4 2 1 0 | Output: 4 4 0
|
Hard
| 3 | 452 | 521 | 66 | 12 |
|
650 |
C
|
650C
|
C. Table Compression
| 2,200 |
dfs and similar; dp; dsu; graphs; greedy
|
Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis.Petya decided to compress tables. He is given a table a consisting of n rows and m columns that is filled with positive integers. He wants to build the table a' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row i of the initial table ai, j < ai, k, then in the resulting table a'i, j < a'i, k, and if ai, j = ai, k then a'i, j = a'i, k. Similarly, if in some column j of the initial table ai, j < ap, j then in compressed table a'i, j < a'p, j and if ai, j = ap, j then a'i, j = a'p, j. Because large values require more space to store them, the maximum value in a' should be as small as possible.Petya is good in theory, however, he needs your help to implement the algorithm.
|
The first line of the input contains two integers n and m (, the number of rows and the number of columns of the table respectively.Each of the following n rows contain m integers ai, j (1 β€ ai, j β€ 109) that are the values in the table.
|
Output the compressed table in form of n lines each containing m integers.If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them.
|
In the first sample test, despite the fact a1, 2 β a21, they are not located in the same row or column so they may become equal after the compression.
|
Input: 2 21 23 4 | Output: 1 22 3
|
Hard
| 5 | 1,006 | 237 | 217 | 6 |
180 |
F
|
180F
|
F. Mathematical Analysis Rocks!
| 1,200 |
constructive algorithms; implementation; math
|
Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to n. Each student i (1 β€ i β€ n) has a best friend p[i] (1 β€ p[i] β€ n). In fact, each student is a best friend of exactly one student. In other words, all p[i] are different. It is possible that the group also has some really ""special individuals"" for who i = p[i].Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm: on the first day of revising each student studies his own Mathematical Analysis notes, in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend. Thus, on the second day the student p[i] (1 β€ i β€ n) studies the i-th student's notes, on the third day the notes go to student p[p[i]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study.You are given two sequences that describe the situation on the third and fourth days of revising: a1, a2, ..., an, where ai means the student who gets the i-th student's notebook on the third day of revising; b1, b2, ..., bn, where bi means the student who gets the i-th student's notebook on the fourth day of revising. You do not know array p, that is you do not know who is the best friend to who. Write a program that finds p by the given sequences a and b.
|
The first line contains integer n (1 β€ n β€ 105) β the number of students in the group. The second line contains sequence of different integers a1, a2, ..., an (1 β€ ai β€ n). The third line contains the sequence of different integers b1, b2, ..., bn (1 β€ bi β€ n).
|
Print sequence n of different integers p[1], p[2], ..., p[n] (1 β€ p[i] β€ n). It is guaranteed that the solution exists and that it is unique.
|
Input: 42 1 4 33 4 2 1 | Output: 4 3 1 2
|
Easy
| 3 | 1,584 | 261 | 141 | 1 |
|
260 |
B
|
260B
|
B. Ancient Prophesy
| 1,600 |
brute force; implementation; strings
|
A recently found Ancient Prophesy is believed to contain the exact Apocalypse date. The prophesy is a string that only consists of digits and characters ""-"".We'll say that some date is mentioned in the Prophesy if there is a substring in the Prophesy that is the date's record in the format ""dd-mm-yyyy"". We'll say that the number of the date's occurrences is the number of such substrings in the Prophesy. For example, the Prophesy ""0012-10-2012-10-2012"" mentions date 12-10-2012 twice (first time as ""0012-10-2012-10-2012"", second time as ""0012-10-2012-10-2012"").The date of the Apocalypse is such correct date that the number of times it is mentioned in the Prophesy is strictly larger than that of any other correct date.A date is correct if the year lies in the range from 2013 to 2015, the month is from 1 to 12, and the number of the day is strictly more than a zero and doesn't exceed the number of days in the current month. Note that a date is written in the format ""dd-mm-yyyy"", that means that leading zeroes may be added to the numbers of the months or days if needed. In other words, date ""1-1-2013"" isn't recorded in the format ""dd-mm-yyyy"", and date ""01-01-2013"" is recorded in it.Notice, that any year between 2013 and 2015 is not a leap year.
|
The first line contains the Prophesy: a non-empty string that only consists of digits and characters ""-"". The length of the Prophesy doesn't exceed 105 characters.
|
In a single line print the date of the Apocalypse. It is guaranteed that such date exists and is unique.
|
Input: 777-444---21-12-2013-12-2013-12-2013---444-777 | Output: 13-12-2013
|
Medium
| 3 | 1,278 | 165 | 104 | 2 |
|
1,903 |
F
|
1903F
|
F. Babysitting
| 2,500 |
2-sat; binary search; data structures; graphs; trees
|
Theofanis wants to play video games, however he should also take care of his sister. Since Theofanis is a CS major, he found a way to do both. He will install some cameras in his house in order to make sure his sister is okay.His house is an undirected graph with \(n\) nodes and \(m\) edges. His sister likes to play at the edges of the graph, so he has to install a camera to at least one endpoint of every edge of the graph. Theofanis wants to find a vertex cover that maximizes the minimum difference between indices of the chosen nodes.More formally, let \(a_1, a_2, \ldots, a_k\) be a vertex cover of the graph. Let the minimum difference between indices of the chosen nodes be the minimum \(\lvert a_i - a_j \rvert\) (where \(i \neq j\)) out of the nodes that you chose. If \(k = 1\) then we assume that the minimum difference between indices of the chosen nodes is \(n\). Can you find the maximum possible minimum difference between indices of the chosen nodes over all vertex covers?
|
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 \(m\) (\(1 \le n \le 10^{5}, 1 \le m \le 2 \cdot 10^{5}\)) β the number of nodes and the number of edges. The \(i\)-th of the following \(m\) lines in the test case contains two positive integers \(u_i\) and \(v_i\) (\(1 \le u_i,v_i \le n\)), meaning that there exists an edge between them in the graph. It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^{5}\).It is guaranteed that the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^{5}\).
|
For each test case, print the maximum minimum difference between indices of the chosen nodes over all vertex covers.
|
In the first test case, we can install cameras at nodes \(1\), \(3\), and \(7\), so the answer is \(2\). In the second test case, we can install only one camera at node \(1\), so the answer is \(3\).
|
Input: 37 61 21 31 41 62 35 73 31 21 31 12 41 21 22 11 1 | Output: 2 3 2
|
Expert
| 5 | 992 | 646 | 116 | 19 |
926 |
E
|
926E
|
E. Merge Equal Elements
| 1,900 |
constructive algorithms; data structures
|
You are given a sequence of positive integers a1, a2, ..., an. While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to x, delete both and insert a single integer x + 1 on their place. This way the number of elements in the sequence is decreased by 1 on each step. You stop performing the operation when there is no pair of equal consecutive elements.For example, if the initial sequence is [5, 2, 1, 1, 2, 2], then after the first operation you get [5, 2, 2, 2, 2], after the second β [5, 3, 2, 2], after the third β [5, 3, 3], and finally after the fourth you get [5, 4]. After that there are no equal consecutive elements left in the sequence, so you stop the process.Determine the final sequence after you stop performing the operation.
|
The first line contains a single integer n (2 β€ n β€ 2Β·105) β the number of elements in the sequence.The second line contains the sequence of integers a1, a2, ..., an (1 β€ ai β€ 109).
|
In the first line print a single integer k β the number of elements in the sequence after you stop performing the operation. In the second line print k integers β the sequence after you stop performing the operation.
|
The first example is described in the statements.In the second example the initial sequence is [1000000000, 1000000000, 1000000000, 1000000000]. After the first operation the sequence is equal to [1000000001, 1000000000, 1000000000]. After the second operation the sequence is [1000000001, 1000000001]. After the third operation the sequence is [1000000002].In the third example there are no two equal consecutive elements initially, so the sequence does not change.
|
Input: 65 2 1 1 2 2 | Output: 25 4
|
Hard
| 2 | 909 | 181 | 216 | 9 |
1,267 |
K
|
1267K
|
K. Key Storage
| 2,100 |
combinatorics; math
|
Karl is developing a key storage service. Each user has a positive integer key.Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions. For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders \([1, 2, 1]\) and has the fingerprint multiset \(\{1, 1, 2\}\).Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \(\{0, 0, 0, 0, 2, 3, 3, 4\}\). Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
|
The first line contains an integer \(t\) (\(1 \le t \le 50\,000\)) β the number of commonly used keys to examine. Each of the next \(t\) lines contains one integer \(k_i\) (\(1 \le k_i \le 10^{18}\)) β the key itself.
|
For each of the keys print one integer β the number of other keys that have the same fingerprint.
|
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders \([1, 1, 2]\). So both numbers have the fingerprint multiset \(\{1, 1, 2\}\).
|
Input: 3 1 11 123456 | Output: 0 1 127
|
Hard
| 2 | 1,496 | 217 | 97 | 12 |
1,647 |
A
|
1647A
|
A. Madoka and Math Dad
| 800 |
implementation; math
|
Madoka finally found the administrator password for her computer. Her father is a well-known popularizer of mathematics, so the password is the answer to the following problem.Find the maximum decimal number without zeroes and with no equal digits in a row, such that the sum of its digits is \(n\).Madoka is too tired of math to solve it herself, so help her to solve this problem!
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Description of the test cases follows.The only line of each test case contains an integer \(n\) (\(1 \le n \le 1000\)) β the required sum of the digits.
|
For each test case print the maximum number you can obtain.
|
The only numbers with the sum of digits equal to \(2\) without zeros are \(2\) and \(11\). But the last one has two ones in a row, so it's not valid. That's why the answer is \(2\).The only numbers with the sum of digits equal to \(3\) without zeros are \(111\), \(12\), \(21\), and \(3\). The first one has \(2\) ones in a row, so it's not valid. So the maximum valid number is \(21\).The only numbers with the sum of digits equals to \(4\) without zeros are \(1111\), \(211\), \(121\), \(112\), \(13\), \(31\), \(22\), and \(4\). Numbers \(1111\), \(211\), \(112\), \(22\) aren't valid, because they have some identical digits in a row. So the maximum valid number is \(121\).
|
Input: 512345 | Output: 1 2 21 121 212
|
Beginner
| 2 | 382 | 290 | 59 | 16 |
1,250 |
G
|
1250G
|
G. Discarding Game
| 2,300 |
dp; greedy; two pointers
|
Eulampius has created a game with the following rules: there are two players in the game: a human and a computer; the game lasts for no more than \(n\) rounds. Initially both players have \(0\) points. In the \(j\)-th round the human gains \(a_j\) points, and the computer gains \(b_j\) points. The points are gained simultaneously; the game ends when one of the players gets \(k\) or more points. This player loses the game. If both players get \(k\) or more points simultaneously, both lose; if both players have less than \(k\) points after \(n\) rounds, the game ends in a tie; after each round the human can push the ""Reset"" button. If the human had \(x\) points, and the computer had \(y\) points before the button is pushed (of course, \(x < k\) and \(y < k\)), then after the button is pushed the human will have \(x' = max(0, \, x - y)\) points, and the computer will have \(y' = max(0, \, y - x)\) points. E. g. the push of ""Reset"" button transforms the state \((x=3, \, y=5)\) into the state \((x'=0, \, y'=2)\), and the state \((x=8, \, y=2)\) into the state \((x'=6, \, y'=0)\).Eulampius asked his friend Polycarpus to test the game. Polycarpus has quickly revealed that amounts of points gained by the human and the computer in each of \(n\) rounds are generated before the game and stored in a file. In other words, the pushes of the ""Reset"" button do not influence the values \(a_j\) and \(b_j\), so sequences \(a\) and \(b\) are fixed and known in advance.Polycarpus wants to make a plan for the game. He would like to win the game pushing the ""Reset"" button as few times as possible. Your task is to determine this minimal number of pushes or determine that Polycarpus cannot win.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases. Then the test cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(2 \le k \le 10^9\)) β the maximum possible number of rounds in the game and the number of points, after reaching which a player loses, respectively.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_j < k)\), where \(a_j\) is the amount of points the human gains in the \(j\)-th round.The third line of each test case contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_j < k\)), where \(b_j\) is the amount of points the computer gains in the \(j\)-th round.The sum of \(n\) over all test cases in the input does not exceed \(2 \cdot 10^5\).
|
Print the answers for all test cases in the order they appear in the input.If Polycarpus cannot win the game, then simply print one line ""-1"" (without quotes). In this case, you should not output anything else for that test case. Otherwise, the first line of the test case answer should contain one integer \(d\) β the minimum possible number of ""Reset"" button pushes, required to win the game. The next line should contain \(d\) distinct integers \(r_1, r_2, \dots, r_d\) (\(1 \le r_i < n\)) β the numbers of rounds, at the end of which Polycarpus has to press the ""Reset"" button, in arbitrary order. If \(d=0\) then either leave the second line of the test case answer empty, or do not print the second line at all.If there are several possible solutions, print any of them.
|
In the second test case, if the human pushes the ""Reset"" button after the second and the fourth rounds, the game goes as follows: after the first round the human has \(5\) points, the computer β \(4\) points; after the second round the human has \(7\) points, the computer β \(10\) points; the human pushes the ""Reset"" button and now he has \(0\) points and the computer β \(3\) points; after the third round the human has \(8\) points, the computer β \(6\) points; after the fourth round the human has \(10\) points, the computer β \(9\) points; the human pushes ""Reset"" button again, after it he has \(1\) point, the computer β \(0\) points; after the fifth round the human has \(5\) points, the computer β \(5\) points; after the sixth round the human has \(11\) points, the computer β \(6\) points; after the seventh round the human has \(12\) points, the computer β \(13\) points; after the eighth round the human has \(14\) points, the computer β \(17\) points; the human wins, as the computer has \(k\) or more points and the human β strictly less than \(k\) points.
|
Input: 3 4 17 1 3 5 7 3 5 7 9 11 17 5 2 8 2 4 6 1 2 7 2 5 4 6 3 3 5 1 7 4 2 5 3 6 17 6 1 2 7 2 5 1 7 4 2 5 3 | Output: 0 2 2 4 -1
|
Expert
| 3 | 1,706 | 831 | 782 | 12 |
95 |
D
|
95D
|
D. Horse Races
| 2,500 |
dp; math
|
Petya likes horse racing very much. Horses numbered from l to r take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of nearly lucky horses' numbers. A nearly lucky number is an integer number that has at least two lucky digits the distance between which does not exceed k. Petya learned from some of his mates from Lviv that lucky digits are digits 4 and 7. The distance between the digits is the absolute difference between their positions in the number of a horse. For example, if k = 2, then numbers 412395497, 404, 4070400000070004007 are nearly lucky and numbers 4, 4123954997, 4007000040070004007 are not.Petya prepared t intervals [li, ri] and invented number k, common for all of them. Your task is to find how many nearly happy numbers there are in each of these segments. Since the answers can be quite large, output them modulo 1000000007 (109 + 7).
|
The first line contains two integers t and k (1 β€ t, k β€ 1000) β the number of segments and the distance between the numbers correspondingly. Next t lines contain pairs of integers li and ri (1 β€ l β€ r β€ 101000). All numbers are given without the leading zeroes. Numbers in each line are separated by exactly one space character.
|
Output t lines. In each line print one integer β the answer for the corresponding segment modulo 1000000007 (109 + 7).
|
In the first sample, the four nearly lucky numbers are 44, 47, 74, 77.In the second sample, only 74 and 77 are in the given segment.
|
Input: 1 21 100 | Output: 4
|
Expert
| 2 | 940 | 329 | 118 | 0 |
1,201 |
C
|
1201C
|
C. Maximum Median
| 1,400 |
binary search; greedy; math; sortings
|
You are given an array \(a\) of \(n\) integers, where \(n\) is odd. You can make the following operation with it: Choose one of the elements of the array (for example \(a_i\)) and increase it by \(1\) (that is, replace it with \(a_i + 1\)). You want to make the median of the array the largest possible using at most \(k\) operations.The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array \([1, 5, 2, 3, 5]\) is \(3\).
|
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\), \(n\) is odd, \(1 \le k \le 10^9\)) β the number of elements in the array and the largest number of operations you can make.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)).
|
Print a single integer β the maximum possible median after the operations.
|
In the first example, you can increase the second element twice. Than array will be \([1, 5, 5]\) and it's median is \(5\).In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is \(3\).In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be \([5, 1, 2, 5, 3, 5, 5]\) and the median will be \(5\).
|
Input: 3 2 1 3 5 | Output: 5
|
Easy
| 4 | 507 | 299 | 74 | 12 |
1,608 |
F
|
1608F
|
F. MEX counting
| 3,200 |
combinatorics; dp; implementation
|
For an array \(c\) of nonnegative integers, \(MEX(c)\) denotes the smallest nonnegative integer that doesn't appear in it. For example, \(MEX([0, 1, 3]) = 2\), \(MEX([42]) = 0\).You are given integers \(n, k\), and an array \([b_1, b_2, \ldots, b_n]\).Find the number of arrays \([a_1, a_2, \ldots, a_n]\), for which the following conditions hold:\(0 \le a_i \le n\) for each \(i\) for each \(i\) from \(1\) to \(n\).\(|MEX([a_1, a_2, \ldots, a_i]) - b_i| \le k\) for each \(i\) from \(1\) to \(n\).As this number can be very big, output it modulo \(998\,244\,353\).
|
The first line of the input contains two integers \(n, k\) (\(1 \le n \le 2000\), \(0 \le k \le 50\)).The second line of the input contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(-k \le b_i \le n+k\)) β elements of the array \(b\).
|
Output a single integer β the number of arrays which satisfy the conditions from the statement, modulo \(998\,244\,353\).
|
Input: 4 0 0 0 0 0 | Output: 256
|
Master
| 3 | 566 | 236 | 121 | 16 |
|
1,144 |
D
|
1144D
|
D. Equalize Them All
| 1,400 |
constructive algorithms; greedy
|
You are given an array \(a\) consisting of \(n\) integers. You can perform the following operations arbitrary number of times (possibly, zero): Choose a pair of indices \((i, j)\) such that \(|i-j|=1\) (indices \(i\) and \(j\) are adjacent) and set \(a_i := a_i + |a_i - a_j|\); Choose a pair of indices \((i, j)\) such that \(|i-j|=1\) (indices \(i\) and \(j\) are adjacent) and set \(a_i := a_i - |a_i - a_j|\). The value \(|x|\) means the absolute value of \(x\). For example, \(|4| = 4\), \(|-3| = 3\).Your task is to find the minimum number of operations required to obtain the array of equal elements and print the order of operations to do it.It is guaranteed that you always can obtain the array of equal elements using such operations.Note that after each operation each element of the current array should not exceed \(10^{18}\) by absolute value.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)-th element of \(a\).
|
In the first line print one integer \(k\) β the minimum number of operations required to obtain the array of equal elements.In the next \(k\) lines print operations itself. The \(p\)-th operation should be printed as a triple of integers \((t_p, i_p, j_p)\), where \(t_p\) is either \(1\) or \(2\) (\(1\) means that you perform the operation of the first type, and \(2\) means that you perform the operation of the second type), and \(i_p\) and \(j_p\) are indices of adjacent elements of the array such that \(1 \le i_p, j_p \le n\), \(|i_p - j_p| = 1\). See the examples for better understanding.Note that after each operation each element of the current array should not exceed \(10^{18}\) by absolute value.If there are many possible answers, you can print any.
|
Input: 5 2 4 6 6 6 | Output: 2 1 2 3 1 1 2
|
Easy
| 2 | 857 | 279 | 765 | 11 |
|
1,954 |
D
|
1954D
|
D. Colored Balls
| 1,800 |
combinatorics; dp; math; sortings
|
There are balls of \(n\) different colors; the number of balls of the \(i\)-th color is \(a_i\).The balls can be combined into groups. Each group should contain at most \(2\) balls, and no more than \(1\) ball of each color.Consider all \(2^n\) sets of colors. For a set of colors, let's denote its value as the minimum number of groups the balls of those colors can be distributed into. For example, if there are three colors with \(3\), \(1\) and \(7\) balls respectively, they can be combined into \(7\) groups (and not less than \(7\)), so the value of that set of colors is \(7\).Your task is to calculate the sum of values over all \(2^n\) possible sets of colors. Since the answer may be too large, print it modulo \(998\,244\,353\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 5000\)) β the number of colors.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 5000\)) β the number of balls of the \(i\)-th color.Additional constraint on input: the total number of balls doesn't exceed \(5000\).
|
Print a single integer β the sum of values of all \(2^n\) sets of colors, taken modulo \(998\,244\,353\).
|
Consider the first example. There are \(8\) sets of colors: for the empty set, its value is \(0\); for the set \(\{1\}\), its value is \(1\); for the set \(\{2\}\), its value is \(1\); for the set \(\{3\}\), its value is \(2\); for the set \(\{1,2\}\), its value is \(1\); for the set \(\{1,3\}\), its value is \(2\); for the set \(\{2,3\}\), its value is \(2\); for the set \(\{1,2,3\}\), its value is \(2\). So, the sum of values over all \(2^n\) sets of colors is \(11\).
|
Input: 31 1 2 | Output: 11
|
Medium
| 4 | 740 | 309 | 105 | 19 |
1,417 |
B
|
1417B
|
B. Two Arrays
| 1,100 |
greedy; math; sortings
|
RedDreamer has an array \(a\) consisting of \(n\) non-negative integers, and an unlucky integer \(T\).Let's denote the misfortune of array \(b\) having length \(m\) as \(f(b)\) β the number of pairs of integers \((i, j)\) such that \(1 \le i < j \le m\) and \(b_i + b_j = T\). RedDreamer has to paint each element of \(a\) into one of two colors, white and black (for each element, the color is chosen independently), and then create two arrays \(c\) and \(d\) so that all white elements belong to \(c\), and all black elements belong to \(d\) (it is possible that one of these two arrays becomes empty). RedDreamer wants to paint the elements in such a way that \(f(c) + f(d)\) is minimum possible.For example: if \(n = 6\), \(T = 7\) and \(a = [1, 2, 3, 4, 5, 6]\), it is possible to paint the \(1\)-st, the \(4\)-th and the \(5\)-th elements white, and all other elements black. So \(c = [1, 4, 5]\), \(d = [2, 3, 6]\), and \(f(c) + f(d) = 0 + 0 = 0\); if \(n = 3\), \(T = 6\) and \(a = [3, 3, 3]\), it is possible to paint the \(1\)-st element white, and all other elements black. So \(c = [3]\), \(d = [3, 3]\), and \(f(c) + f(d) = 0 + 1 = 1\). Help RedDreamer to paint the array optimally!
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(T\) (\(1 \le n \le 10^5\), \(0 \le T \le 10^9\)) β the number of elements in the array and the unlucky integer, respectively. The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(0 \le a_i \le 10^9\)) β the elements of the array. The sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case print \(n\) integers: \(p_1\), \(p_2\), ..., \(p_n\) (each \(p_i\) is either \(0\) or \(1\)) denoting the colors. If \(p_i\) is \(0\), then \(a_i\) is white and belongs to the array \(c\), otherwise it is black and belongs to the array \(d\).If there are multiple answers that minimize the value of \(f(c) + f(d)\), print any of them.
|
Input: 2 6 7 1 2 3 4 5 6 3 6 3 3 3 | Output: 1 0 0 1 1 0 1 0 0
|
Easy
| 3 | 1,195 | 502 | 353 | 14 |
|
1,615 |
F
|
1615F
|
F. LEGOndary Grandmaster
| 2,800 |
combinatorics; dp; math
|
After getting bored by playing with crayons, you decided to switch to Legos! Today, you're working with a long strip, with height \(1\) and length \(n\), some positions of which are occupied by \(1\) by \(1\) Lego pieces.In one second, you can either remove two adjacent Lego pieces from the strip (if both are present), or add two Lego pieces to adjacent positions (if both are absent). You can only add or remove Lego's at two adjacent positions at the same time, as otherwise your chubby fingers run into precision issues.You want to know exactly how much time you'll spend playing with Legos. You value efficiency, so given some starting state and some ending state, you'll always spend the least number of seconds to transform the starting state into the ending state. If it's impossible to transform the starting state into the ending state, you just skip it (so you spend \(0\) seconds).The issue is that, for some positions, you don't remember whether there were Legos there or not (in either the starting state, the ending state, or both). Over all pairs of (starting state, ending state) that are consistent with your memory, find the total amount of time it will take to transform the starting state to the ending state. Print this value modulo \(1\,000\,000\,007\) (\(10^9 + 7\)).
|
The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. Then \(t\) cases follow.The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 2000\)) β the size of the Lego strip.The second line of each test case contains a string \(s\) of length \(n\), consisting of the characters 0, 1, and ? β your memory of the starting state: 1 represents a position that definitely has a Lego piece, 0 represents a position that definitely does not have a Lego piece, and ? represents a position that you don't remember. The third line of each test case contains a string \(t\) of length \(n\), consisting of the characters 0, 1, and ? β your memory of the ending state. It follows a similar format to the starting state.It's guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2000\).
|
For each test case, output a single integer β the answer to the problem modulo \(1\,000\,000\,007\) (\(10^9 + 7\)).
|
For the first test case, \(00\) is the only possible starting state, and \(11\) is the only possible ending state. It takes exactly one operation to change \(00\) to \(11\).For the second test case, some of the possible starting and ending state pairs are: \((000, 011)\) β takes \(1\) operation. \((001, 100)\) β takes \(2\) operations. \((010, 000)\) β takes \(0\) operations, as it's impossible to achieve the ending state.
|
Input: 6 2 00 11 3 ??? ??? 3 ??1 0?0 4 ??0? ??11 5 ????? 0??1? 10 ?01??01?1? ??100?1??? | Output: 1 16 1 14 101 1674
|
Master
| 3 | 1,292 | 847 | 115 | 16 |
1,998 |
A
|
1998A
|
A. Find K Distinct Points with Fixed Center
| 800 |
constructive algorithms; implementation; math
|
I couldn't think of a good title for this problem, so I decided to learn from LeetCode.β Sun Tzu, The Art of WarYou are given three integers \(x_c\), \(y_c\), and \(k\) (\(-100 \leq x_c, y_c \leq 100\), \(1 \leq k \leq 1000\)). You need to find \(k\) distinct points (\(x_1, y_1\)), (\(x_2, y_2\)), \(\ldots\), (\(x_k, y_k\)), having integer coordinates, on the 2D coordinate plane such that: their center\(^{\text{β}}\) is (\(x_c, y_c\)) \(-10^9 \leq x_i, y_i \leq 10^9\) for all \(i\) from \(1\) to \(k\) It can be proven that at least one set of \(k\) distinct points always exists that satisfies these conditions.\(^{\text{β}}\)The center of \(k\) points (\(x_1, y_1\)), (\(x_2, y_2\)), \(\ldots\), (\(x_k, y_k\)) is \(\left( \frac{x_1 + x_2 + \ldots + x_k}{k}, \frac{y_1 + y_2 + \ldots + y_k}{k} \right)\).
|
The first line contains \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.Each test case contains three integers \(x_c\), \(y_c\), and \(k\) (\(-100 \leq x_c, y_c \leq 100\), \(1 \leq k \leq 1000\)) β the coordinates of the center and the number of distinct points you must output.It is guaranteed that the sum of \(k\) over all test cases does not exceed \(1000\).
|
For each test case, output \(k\) lines, the \(i\)-th line containing two space separated integers, \(x_i\) and \(y_i\), (\(-10^9 \leq x_i, y_i \leq 10^9\)) β denoting the position of the \(i\)-th point.If there are multiple answers, print any of them. It can be shown that a solution always exists under the given constraints.
|
For the first test case, \(\left( \frac{10}{1}, \frac{10}{1} \right) = (10, 10)\).For the second test case, \(\left( \frac{-1 + 5 - 4}{3}, \frac{-1 -1 + 2}{3} \right) = (0, 0)\).
|
Input: 410 10 10 0 3-5 -8 84 -5 3 | Output: 10 10 -1 -1 5 -1 -4 2 -6 -7 -5 -7 -4 -7 -4 -8 -4 -9 -5 -9 -6 -9 -6 -8 1000 -1000 -996 995 8 -10
|
Beginner
| 3 | 811 | 372 | 326 | 19 |
553 |
D
|
553D
|
D. Nudist Beach
| 2,300 |
binary search; graphs; greedy
|
Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers.There are n cities, labeled from 1 to n, and m bidirectional roads between them. Currently, there are Life Fibers in every city. In addition, there are k cities that are fortresses of the Life Fibers that cannot be captured under any circumstances. So, the Nudist Beach can capture an arbitrary non-empty subset of cities with no fortresses.After the operation, Nudist Beach will have to defend the captured cities from counterattack. If they capture a city and it is connected to many Life Fiber controlled cities, it will be easily defeated. So, Nudist Beach would like to capture a set of cities such that for each captured city the ratio of Nudist Beach controlled neighbors among all neighbors of that city is as high as possible. More formally, they would like to capture a non-empty set of cities S with no fortresses of Life Fibers. The strength of a city is defined as (number of neighbors of x in S) / (total number of neighbors of x). Here, two cities are called neighbors if they are connnected with a road. The goal is to maximize the strength of the weakest city in S.Given a description of the graph, and the cities with fortresses, find a non-empty subset that maximizes the strength of the weakest city.
|
The first line of input contains three integers n, m, k (2 β€ n β€ 100 000, 1 β€ m β€ 100 000, 1 β€ k β€ n - 1).The second line of input contains k integers, representing the cities with fortresses. These cities will all be distinct. The next m lines contain the roads. The i-th of these lines will have 2 integers ai, bi (1 β€ ai, bi β€ n, ai β bi). Every city will have at least one road adjacent to it.There is no more than one road between each pair of the cities.
|
The first line should contain an integer r, denoting the size of an optimum set (1 β€ r β€ n - k). The second line should contain r integers, denoting the cities in the set. Cities may follow in an arbitrary order. This line should not contain any of the cities with fortresses.If there are multiple possible answers, print any of them.
|
The first example case achieves a strength of 1/2. No other subset is strictly better.The second example case achieves a strength of 1. Note that the subset doesn't necessarily have to be connected.
|
Input: 9 8 43 9 6 81 21 31 41 52 62 72 82 9 | Output: 31 4 5
|
Expert
| 3 | 1,413 | 460 | 334 | 5 |
1,163 |
F
|
1163F
|
F. Indecisive Taxi Fee
| 3,000 |
data structures; graphs; shortest paths
|
In the city of Capypaland where Kuro and Shiro resides, there are \(n\) towns numbered from \(1\) to \(n\) and there are \(m\) bidirectional roads numbered from \(1\) to \(m\) connecting them. The \(i\)-th road connects towns \(u_i\) and \(v_i\). Since traveling between the towns is quite difficult, the taxi industry is really popular here. To survive the harsh competition, each taxi company has to find a distinctive trait for their customers.Kuro is the owner of a taxi company. He has decided to introduce a new fee model for his taxi brand, where the fee for each ride is not calculated based on the trip length, but on the sum of the prices of the roads traveled. The price for each of the \(m\) roads has been decided by Kuro himself.As of now, the price for the road \(i\) is \(w_i\) and hence the fee for a taxi ride traveling through roads \(e_1, e_2, \ldots, e_k\) is \(\sum_{i=1}^k w_{e_i}\).However, Kuro himself is an indecisive person, so he has drafted \(q\) plans to change the road price. Each of the plans will be based on the original prices \(w_i\), except for a single road \(t_j\), the price of which is changed to \(x_j\). Note, that the plans are independent of each other.Shiro is a regular customer of the Kuro's taxi brand since she uses the taxi to travel from town \(1\) to town \(n\) every day. Since she's so a regular customer, Kuro decided to show her all his \(q\) plans before publishing them to the public. Now, Shiro wants to know the lowest fee she must pay to travel from the town \(1\) to the town \(n\) for each Kuro's plan.
|
The first line contains three integers \(n\), \(m\) and \(q\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m, q \le 2 \cdot 10^5\)) β the number of towns, the number of roads, and the number of plans that Kuro has drafted respectively.The \(i\)-th of the next \(m\) contains three integers \(u_i\), \(v_i\) and \(w_i\) (\(1 \le u_i, v_i \le n\), \(1 \le w_i \le 10^9\), \(u_i \ne v_i\)) β two endpoints and the original price of the \(i\)-th road.It is guaranteed, that there is at least one way to travel from town \(1\) to town \(n\) using these \(m\) bidirectional roads.Each of the next \(q\) lines contains two integers \(t_j\) and \(x_j\) (\(1 \leq t_j \leq m, 1 \leq x_j \leq 10^9\)) β the index of the road Kuro has planned to change and its new price respectively.
|
Print \(q\) integers β the lowest fee Shiro must pay to get from town \(1\) to town \(n\) in each of those \(q\) plans.
|
In the first example, the original overview of Capypaland looks like this, where the number next to each road denotes the original prices of the roads, The overview of the first plan, The lowest fee Shiro must pay in this plan is \(4\), which corresponds to the path \(1 \rightarrow 4\).The overview of the second plan, The lowest fee Shiro must pay in this plan is \(2\), which corresponds to the path \(1 \rightarrow 3 \rightarrow 4\).The overview of the third plan, The lowest fee Shiro must pay in this plan is \(5\), which corresponds to the path \(1 \rightarrow 2 \rightarrow 4\).
|
Input: 4 5 6 1 2 2 2 4 3 1 4 7 1 3 1 3 4 5 3 4 5 1 3 8 1 4 2 1 3 1 | Output: 4 2 5 6 3 1
|
Master
| 3 | 1,568 | 767 | 119 | 11 |
499 |
B
|
499B
|
B. Lecture
| 1,000 |
implementation; strings
|
You have a new professor of graph theory and he speaks very quickly. You come up with the following plan to keep up with his lecture and make notes.You know two languages, and the professor is giving the lecture in the first one. The words in both languages consist of lowercase English characters, each language consists of several words. For each language, all words are distinct, i.e. they are spelled differently. Moreover, the words of these languages have a one-to-one correspondence, that is, for each word in each language, there exists exactly one word in the other language having has the same meaning.You can write down every word the professor says in either the first language or the second language. Of course, during the lecture you write down each word in the language in which the word is shorter. In case of equal lengths of the corresponding words you prefer the word of the first language.You are given the text of the lecture the professor is going to read. Find out how the lecture will be recorded in your notes.
|
The first line contains two integers, n and m (1 β€ n β€ 3000, 1 β€ m β€ 3000) β the number of words in the professor's lecture and the number of words in each of these languages.The following m lines contain the words. The i-th line contains two strings ai, bi meaning that the word ai belongs to the first language, the word bi belongs to the second language, and these two words have the same meaning. It is guaranteed that no word occurs in both languages, and each word occurs in its language exactly once.The next line contains n space-separated strings c1, c2, ..., cn β the text of the lecture. It is guaranteed that each of the strings ci belongs to the set of strings {a1, a2, ... am}.All the strings in the input are non-empty, each consisting of no more than 10 lowercase English letters.
|
Output exactly n words: how you will record the lecture in your notebook. Output the words of the lecture in the same order as in the input.
|
Input: 4 3codeforces codesecrofcontest roundletter messagecodeforces contest letter contest | Output: codeforces round letter round
|
Beginner
| 2 | 1,035 | 796 | 140 | 4 |
|
396 |
E
|
396E
|
E. On Iteration of One Well-Known Function
| 0 |
math
|
Of course, many of you can calculate Ο(n) β the number of positive integers that are less than or equal to n, that are coprime with n. But what if we need to calculate Ο(Ο(...Ο(n))), where function Ο is taken k times and n is given in the canonical decomposition into prime factors? You are given n and k, calculate the value of Ο(Ο(...Ο(n))). Print the result in the canonical decomposition into prime factors.
|
The first line contains integer m (1 β€ m β€ 105) β the number of distinct prime divisors in the canonical representaion of n.Each of the next m lines contains a pair of space-separated integers pi, ai (2 β€ pi β€ 106; 1 β€ ai β€ 1017) β another prime divisor of number n and its power in the canonical representation. The sum of all ai doesn't exceed 1017. Prime divisors in the input follow in the strictly increasing order.The last line contains integer k (1 β€ k β€ 1018).
|
In the first line, print integer w β the number of distinct prime divisors of number Ο(Ο(...Ο(n))), where function Ο is taken k times.Each of the next w lines must contain two space-separated integers qi, bi (bi β₯ 1) β another prime divisor and its power in the canonical representaion of the result. Numbers qi must go in the strictly increasing order.
|
You can read about canonical representation of a positive integer here: http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic.You can read about function Ο(n) here: http://en.wikipedia.org/wiki/Euler's_totient_function.
|
Input: 17 11 | Output: 22 13 1
|
Beginner
| 1 | 411 | 468 | 353 | 3 |
1,850 |
E
|
1850E
|
E. Cardboard for Pictures
| 1,100 |
binary search; geometry; implementation; math
|
Mircea has \(n\) pictures. The \(i\)-th picture is a square with a side length of \(s_i\) centimeters. He mounted each picture on a square piece of cardboard so that each picture has a border of \(w\) centimeters of cardboard on all sides. In total, he used \(c\) square centimeters of cardboard. Given the picture sizes and the value \(c\), can you find the value of \(w\)? A picture of the first test case. Here \(c = 50 = 5^2 + 4^2 + 3^2\), so \(w=1\) is the answer. Please note that the piece of cardboard goes behind each picture, not just the border.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains two positive integers \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) and \(c\) (\(1 \leq c \leq 10^{18}\)) β the number of paintings, and the amount of used square centimeters of cardboard.The second line of each test case contains \(n\) space-separated integers \(s_i\) (\(1 \leq s_i \leq 10^4\)) β the sizes of the paintings.The sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).Additional constraint on the input: Such an integer \(w\) exists for each test case.Please note, that some of the input for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
|
For each test case, output a single integer β the value of \(w\) (\(w \geq 1\)) which was used to use exactly \(c\) squared centimeters of cardboard.
|
The first test case is explained in the statement.For the second test case, the chosen \(w\) was \(2\), thus the only cardboard covers an area of \(c = (2 \cdot 2 + 6)^2 = 10^2 = 100\) squared centimeters.For the third test case, the chosen \(w\) was \(4\), which obtains the covered area \(c = (2 \cdot 4 + 2)^2 \times 5 = 10^2 \times 5 = 100 \times 5 = 500\) squared centimeters.
|
Input: 10 3 50 3 2 1 1 100 6 5 500 2 2 2 2 2 2 365 3 4 2 469077255466389 10000 2023 10 635472106413848880 9181 4243 7777 1859 2017 4397 14 9390 2245 7225 7 176345687772781240 9202 9407 9229 6257 7743 5738 7966 14 865563946464579627 3654 5483 1657 7571 1639 9815 122 9468 3079 2666 5498 4540 7861 5384 19 977162053008871403 9169 9520 9209 9013 9300 9843 9933 9454 9960 9167 9964 9701 9251 9404 9462 9277 9661 9164 9161 18 886531871815571953 2609 10 5098 9591 949 8485 6385 4586 1064 5412 6564 8460 2245 6552 5089 8353 3803 3764 | Output: 1 2 4 5 7654321 126040443 79356352 124321725 113385729 110961227
|
Easy
| 4 | 556 | 806 | 149 | 18 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.