contest_id
int32 1
2.13k
| index
stringclasses 62
values | problem_id
stringlengths 2
6
| title
stringlengths 0
67
| rating
int32 0
3.5k
| tags
stringlengths 0
139
| statement
stringlengths 0
6.96k
| input_spec
stringlengths 0
2.32k
| output_spec
stringlengths 0
1.52k
| note
stringlengths 0
5.06k
| sample_tests
stringlengths 0
1.02k
| difficulty_category
stringclasses 6
values | tag_count
int8 0
11
| statement_length
int32 0
6.96k
| input_spec_length
int16 0
2.32k
| output_spec_length
int16 0
1.52k
| contest_year
int16 0
21
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,933 |
F
|
1933F
|
F. Turtle Mission: Robot and the Earthquake
| 2,100 |
dfs and similar; dp; graphs; shortest paths
|
The world is a grid with \(n\) rows and \(m\) columns. The rows are numbered \(0, 1, \ldots, n-1\), while the columns are numbered \(0, 1, \ldots, m-1\). In this world, the columns are cyclic (i.e. the top and the bottom cells in each column are adjacent). The cell on the \(i\)-th row and the \(j\)-th column (\(0 \le i < n, 0 \le j < m\)) is denoted as \((i,j)\). At time \(0\), the cell \((i,j)\) (where \(0 \le i < n, 0 \le j < m\)) contains either a rock or nothing. The state of cell \((i,j)\) can be described using the integer \(a_{i,j}\): If \(a_{i,j} = 1\), there is a rock at \((i,j)\). If \(a_{i,j} = 0\), there is nothing at \((i,j)\). As a result of aftershocks from the earthquake, the columns follow tectonic plate movements: each column moves cyclically upwards at a velocity of \(1\) cell per unit of time. Formally, for some \(0 \le i < n, 0 \le j < m\), if \((i,j)\) contains a rock at the moment, it will move from \((i, j)\) to \((i - 1, j)\) (or to \((n - 1, j)\) if \(i=0\)). The robot called RT is initially positioned at \((0,0)\). It has to go to \((n-1,m-1)\) to carry out an earthquake rescue operation (to the bottom rightmost cell). The earthquake doesn't change the position of the robot, they only change the position of rocks in the world.Let RT's current position be \((x,y)\) (\(0 \le x < n, 0 \le y < m\)), it can perform the following operations: Go one cell cyclically upwards, i.e. from \((x,y)\) to \(((x+n-1) \bmod n, y)\) using \(1\) unit of time. Go one cell cyclically downwards, i.e. \((x,y)\) to \(((x+1) \bmod n, y)\) using \(1\) unit of time. Go one cell to the right, i.e. \((x,y)\) to \((x, y+1)\) using \(1\) unit of time. (RT may perform this operation only if \(y < m-1\).) Note that RT cannot go left using the operations nor can he stay at a position.Unfortunately, RT will explode upon colliding with a rock. As such, when RT is at \((x,y)\) and there is a rock at \(((x+1) \bmod n, y)\) or \(((x+2) \bmod n, y)\), RT cannot move down or it will be hit by the rock. Similarly, if \(y+1 < m\) and there is a rock at \(((x+1) \bmod n, y+1)\), RT cannot move right or it will be hit by the rock. However, it is worth noting that if there is a rock at \((x \bmod n, y+1)\) and \(((x+1) \bmod n, y)\), RT can still move right safely. Find the minimum amount of time RT needs to reach \((n-1,m-1)\) without colliding with any rocks. If it is impossible to do so, output \(-1\).
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.In each test case, the first line contains two integers \(n\), \(m\) (\(3 \le n, m \le 10^3\)) β the size of the planet's boundaries. Each of the next \(n\) lines contains \(m\) integers. The \((j+1)\)-th integer on the \((i+1)\)-th line (\(0 \le i < n, 0 \le j < m\)) is \(a_{i,j}\) (\(0 \le a_{i,j} \le 1\)), which denotes whether or not there is a rock at \((i,j)\) at time \(0\).Additionally, it is guaranteed that \(a_{0,0} = 0\), and \(a_{i, m-1} = 0\) for \(0 \le i < n\). In other words, there is no rock at RT's initial position as well as column \(m-1\).The sum of \(n \cdot m\) over all test cases does not exceed \(10^6\).
|
For each test case: If the destination can be reached without colliding with any rocks, output a single integer β the minimum amount of time RT needs to reach \((n-1,m-1)\). Otherwise, output \(-1\).
|
Visual explanation of the first test case in the example:
|
Input: 64 50 1 0 0 00 0 1 0 01 0 1 1 00 0 0 0 03 30 0 01 0 00 0 05 30 0 00 0 01 0 00 0 01 0 03 70 0 1 0 0 1 01 0 1 0 1 0 00 1 0 0 0 0 03 40 1 0 01 0 0 00 1 1 05 50 0 0 0 00 1 0 1 00 1 0 1 00 1 0 1 00 0 0 1 0 | Output: 7 3 3 8 -1 12
|
Hard
| 4 | 2,428 | 739 | 199 | 19 |
1,012 |
F
|
1012F
|
F. Passports
| 3,400 |
dp; implementation
|
Gleb is a famous competitive programming teacher from Innopolis. He is planning a trip to N programming camps in the nearest future. Each camp will be held in a different country. For each of them, Gleb needs to apply for a visa. For each of these trips Gleb knows three integers: the number of the first day of the trip si, the length of the trip in days leni, and the number of days ti this country's consulate will take to process a visa application and stick a visa in a passport. Gleb has P (1 β€ P β€ 2) valid passports and is able to decide which visa he wants to put in which passport.For each trip, Gleb will have a flight to that country early in the morning of the day si and will return back late in the evening of the day si + leni - 1.To apply for a visa on the day d, Gleb needs to be in Innopolis in the middle of this day. So he can't apply for a visa while he is on a trip, including the first and the last days. If a trip starts the next day after the end of the other one, Gleb can't apply for a visa between them as well. The earliest Gleb can apply for a visa is day 1.After applying for a visa of country i on day d, Gleb will get his passport back in the middle of the day d + ti. Consulates use delivery services, so Gleb can get his passport back even if he is not in Innopolis on this day. Gleb can apply for another visa on the same day he received his passport back, if he is in Innopolis this day. Gleb will not be able to start his trip on day si if he doesn't has a passport with a visa for the corresponding country in the morning of day si. In particular, the passport should not be in another country's consulate for visa processing.Help Gleb to decide which visas he needs to receive in which passport, and when he should apply for each visa.
|
In the first line of the input there are two integers N (1 β€ N β€ 22) and P (1 β€ P β€ 2)βthe number of trips and the number of passports Gleb has, respectively.The next N lines describe Gleb's trips. Each line contains three positive integers si, leni, ti (1 β€ si, leni, ti β€ 109)βthe first day of the trip, the length of the trip and number of days the consulate of this country needs to process a visa application. It is guaranteed that no two trips intersect.
|
If it is impossible to get all visas on time, just print ""NO"" (quotes for clarity). Otherwise, print ""YES"" and N lines describing trips. For each trip, first print number of the passport Gleb should put this country's visa in, and then print number of the day he should apply for it. Print trips in the same order as they appear in the input. Days are numbered from 1, starting with tomorrowβthe first day you can apply for a visa. Passports are numbered from 1 to P.If there are several possible answers, print any of them.
|
Examples with answer ""YES"" are depicted below.Each cell of the stripe represents a single day. Rectangles represent trips, each trip starts in the morning and ends in the evening. Rectangles with angled corners represent visa applications. Each application starts in the middle of a day and ends ti days after. The trip and the visa application for the same country have the same color.In examples with two passports, visa applications and trips depicted above the time stripe are made using the first passport, visa applications and trips depicted below the time stripe are made using the second passport.Example 1: Example 2: Example 3:
|
Input: 2 13 1 16 1 1 | Output: YES1 11 4
|
Master
| 2 | 1,776 | 460 | 528 | 10 |
42 |
E
|
42E
|
E. Baldman and the military
| 2,700 |
dfs and similar; graphs; trees
|
Baldman is a warp master. He possesses a unique ability β creating wormholes! Given two positions in space, Baldman can make a wormhole which makes it possible to move between them in both directions. Unfortunately, such operation isn't free for Baldman: each created wormhole makes him lose plenty of hair from his head.Because of such extraordinary abilities, Baldman has caught the military's attention. He has been charged with a special task. But first things first.The military base consists of several underground objects, some of which are connected with bidirectional tunnels. There necessarily exists a path through the tunnel system between each pair of objects. Additionally, exactly two objects are connected with surface. For the purposes of security, a patrol inspects the tunnel system every day: he enters one of the objects which are connected with surface, walks the base passing each tunnel at least once and leaves through one of the objects connected with surface. He can enter and leave either through the same object, or through different objects. The military management noticed that the patrol visits some of the tunnels multiple times and decided to optimize the process. Now they are faced with a problem: a system of wormholes needs to be made to allow of a patrolling which passes each tunnel exactly once. At the same time a patrol is allowed to pass each wormhole any number of times.This is where Baldman comes to operation: he is the one to plan and build the system of the wormholes. Unfortunately for him, because of strict confidentiality the military can't tell him the arrangement of tunnels. Instead, they insist that his system of portals solves the problem for any arrangement of tunnels which satisfies the given condition. Nevertheless, Baldman has some information: he knows which pairs of objects he can potentially connect and how much it would cost him (in hair). Moreover, tomorrow he will be told which objects (exactly two) are connected with surface. Of course, our hero decided not to waste any time and calculate the minimal cost of getting the job done for some pairs of objects (which he finds likely to be the ones connected with surface). Help Baldman!
|
First line of the input contains a single natural number n (2 β€ n β€ 100000) β the number of objects on the military base. The second line β one number m (1 β€ m β€ 200000) β the number of the wormholes Baldman can make. The following m lines describe the wormholes: each line contains three integer numbers a, b, c (1 β€ a, b β€ n, 1 β€ c β€ 100000) β the numbers of objects which can be connected and the number of hair Baldman has to spend to make this wormhole.The next line contains one natural number q (1 β€ q β€ 100000) β the number of queries. Finally, the last q lines contain a description of one query each β a pair of numbers of different objects ai, bi (1 β€ ai, bi β€ n, ai β bi). There could be more than one wormhole between a pair of objects.
|
Your program should output q lines, one for each query. The i-th line should contain a single integer number β the answer for i-th query: the minimum cost (in hair) of a system of wormholes allowing the optimal patrol for any system of tunnels (satisfying the given conditions) if ai and bi are the two objects connected with surface, or ""-1"" if such system of wormholes cannot be made.
|
Input: 211 2 311 2 | Output: 0
|
Master
| 3 | 2,210 | 749 | 388 | 0 |
|
1,411 |
F
|
1411F
|
F. The Thorny Path
| 3,000 |
greedy; math
|
According to a legend the Hanoi Temple holds a permutation of integers from \(1\) to \(n\). There are \(n\) stones of distinct colors lying in one line in front of the temple. Monks can perform the following operation on stones: choose a position \(i\) (\(1 \le i \le n\)) and cyclically shift stones at positions \(i\), \(p[i]\), \(p[p[i]]\), .... That is, a stone from position \(i\) will move to position \(p[i]\), a stone from position \(p[i]\) will move to position \(p[p[i]]\), and so on, a stone from position \(j\), such that \(p[j] = i\), will move to position \(i\).Each day the monks must obtain a new arrangement of stones using an arbitrary number of these operations. When all possible arrangements will have been obtained, the world will end. You are wondering, what if some elements of the permutation could be swapped just before the beginning? How many days would the world last?You want to get a permutation that will allow the world to last as long as possible, using the minimum number of exchanges of two elements of the permutation.Two arrangements of stones are considered different if there exists a position \(i\) such that the colors of the stones on that position are different in these arrangements.
|
Each test consists of multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^3\)). Description of the test cases follows.The first line of each test case contains \(n\) (\(3 \leq n \leq 10^6\)). The next line contains \(n\) integers \(p_1, \dots, p_n\) (\(1 \le p_i \le n\)). It is guaranteed that \(p\) is a permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each of the \(t\) test cases, print two integers on a new line: the largest possible number of days the world can last, modulo \(10^9 + 7\), and the minimum number of exchanges required for that.
|
Let's label the colors of the stones with letters. Explanations for the first two test cases of the first example: Using the permutation \([2, 3, 1]\), we can additionally obtain the arrangements CAB and BCA from ABC. This is already the maximum possible result. Using the permutation \([2, 1, 3]\), the only BAC can be obtained from ABC. As we saw in the previous case, two arrangements are not the maximum possible number of distinct arrangements for \(n = 3\). To get an optimal permutation, for example, we can swap \(1\) and \(3\), so we will get the permutation \([2, 3, 1]\).
|
Input: 3 3 2 3 1 3 2 1 3 3 1 2 3 | Output: 3 0 3 1 3 2
|
Master
| 2 | 1,228 | 446 | 199 | 14 |
1,950 |
E
|
1950E
|
E. Nearly Shortest Repeating Substring
| 1,500 |
brute force; implementation; number theory; strings
|
You are given a string \(s\) of length \(n\) consisting of lowercase Latin characters. Find the length of the shortest string \(k\) such that several (possibly one) copies of \(k\) can be concatenated together to form a string with the same length as \(s\) and, at most, one different character.More formally, find the length of the shortest string \(k\) such that \(c = \underbrace{k + \cdots + k}_{x\rm\ \text{times}}\) for some positive integer \(x\), strings \(s\) and \(c\) has the same length and \(c_i \neq s_i\) for at most one \(i\) (i.e. there exist \(0\) or \(1\) such positions).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2\cdot10^5\)) β the length of string \(s\).The second line of each test case contains the string \(s\), consisting of lowercase Latin characters.The sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, print the length of the shortest string \(k\) satisfying the constraints in the statement.
|
In the first test case, you can select \(k = \texttt{a}\) and \(k+k+k+k = \texttt{aaaa}\), which only differs from \(s\) in the second position.In the second test case, you cannot select \(k\) of length one or two. We can have \(k = \texttt{abba}\), which is equal to \(s\).
|
Input: 54abaa4abba13slavicgslavic8hshahaha20stormflamestornflame | Output: 1 4 13 2 10
|
Medium
| 4 | 591 | 394 | 110 | 19 |
1,178 |
F2
|
1178F2
|
F2. Long Colorful Strip
| 2,600 |
dp
|
This is the second subtask of problem F. The only differences between this and the first subtask are the constraints on the value of \(m\) and the time limit. It is sufficient to solve this subtask in order to hack it, but you need to solve both subtasks in order to hack the first one.There are \(n+1\) distinct colours in the universe, numbered \(0\) through \(n\). There is a strip of paper \(m\) centimetres long initially painted with colour \(0\). Alice took a brush and painted the strip using the following process. For each \(i\) from \(1\) to \(n\), in this order, she picks two integers \(0 \leq a_i < b_i \leq m\), such that the segment \([a_i, b_i]\) is currently painted with a single colour, and repaints it with colour \(i\). Alice chose the segments in such a way that each centimetre is now painted in some colour other than \(0\). Formally, the segment \([i-1, i]\) is painted with colour \(c_i\) (\(c_i \neq 0\)). Every colour other than \(0\) is visible on the strip.Count the number of different pairs of sequences \(\{a_i\}_{i=1}^n\), \(\{b_i\}_{i=1}^n\) that result in this configuration. Since this number may be large, output it modulo \(998244353\).
|
The first line contains a two integers \(n\), \(m\) (\(1 \leq n \leq 500\), \(n \leq m \leq 10^6\)) β the number of colours excluding the colour \(0\) and the length of the paper, respectively.The second line contains \(m\) space separated integers \(c_1, c_2, \ldots, c_m\) (\(1 \leq c_i \leq n\)) β the colour visible on the segment \([i-1, i]\) after the process ends. It is guaranteed that for all \(j\) between \(1\) and \(n\) there is an index \(k\) such that \(c_k = j\).
|
Output a single integer β the number of ways Alice can perform the painting, modulo \(998244353\).
|
In the first example, there are \(5\) ways, all depicted in the figure below. Here, \(0\) is white, \(1\) is red, \(2\) is green and \(3\) is blue.Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour \(2\).In the second example, Alice must first paint segment 0 3 with colour \(1\) and then segment 1 2 with colour \(2\).
|
Input: 3 3 1 2 3 | Output: 5
|
Expert
| 1 | 1,176 | 478 | 98 | 11 |
1,647 |
E
|
1647E
|
E. Madoka and the Sixth-graders
| 2,500 |
data structures; dfs and similar; greedy
|
After the most stunning success with the fifth-graders, Madoka has been trusted with teaching the sixth-graders.There's \(n\) single-place desks in her classroom. At the very beginning Madoka decided that the student number \(b_i\) (\(1 \le b_i \le n\)) will sit at the desk number \(i\). Also there's an infinite line of students with numbers \(n + 1, n + 2, n + 3, \ldots\) waiting at the door with the hope of being able to learn something from the Madoka herself. Pay attention that each student has his unique number.After each lesson, the following happens in sequence. The student sitting at the desk \(i\) moves to the desk \(p_i\). All students move simultaneously. If there is more than one student at a desk, the student with the lowest number keeps the place, and the others are removed from the class forever. For all empty desks in ascending order, the student from the lowest number from the outside line occupies the desk. Note that in the end there is exactly one student at each desk again. It is guaranteed that the numbers \(p\) are such that at least one student is removed after each lesson. Check out the explanation to the first example for a better understanding.After several (possibly, zero) lessons the desk \(i\) is occupied by student \(a_i\). Given the values \(a_1, a_2, \ldots, a_n\) and \(p_1, p_2, \ldots, p_n\), find the lexicographically smallest suitable initial seating permutation \(b_1, b_2, \ldots, b_n\).The permutation is an array of \(n\) different integers from \(1\) up to \(n\) in any order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not (\(2\) occurs twice). \([1,3,4]\) is not a permutation either (\(n=3\) but there's \(4\) in the array).For two different permutations \(a\) and \(b\) of the same length, \(a\) is lexicographically less than \(b\) if in the first position where \(a\) and \(b\) differ, the permutation \(a\) has a smaller element than the corresponding element in \(b\).
|
The first line of input data contains an integer \(n\) (\(2 \le n \le 10^5\)) β a number of desks in the classroom.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)) β desks where the students move. It is guaranteed that \(p\) has at least two equal elements.The third line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the final seating of the students. It is guaranteed that there is an initial permutation from which the seating \(a\) can be obtained.
|
In the only line print \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le n\)) β lexicographically minimum permutation describing the initial seating of the sixth-graders that can lead to the final seating \(a\).
|
The description of the first test is below: The first picture shows the starting permutation, which is the answer. Then the students sitting at desks \(1, 2\) are transferred to a \(5\) desk. Also, a \(1\) student moved from a \(5\) desk, and a student from a \(4\) disk is transferred to a \(3\) desk.Thus, after all these transfers permutation shown in the second image is obtained. Then, at the desk with the number \(5\), the student with the number \(3\) is expelled, and at the desk with the number \(3\), the student with the number \(5\) is expelled. (Since their numbers are not the smallest) Then new students with numbers \(6, 7\) sit at desks numbered \(2, 4\). And this permutation (after the end of the first lesson) is shown in the third image.The \(4\) image shows the seating arrangement, after the second lesson before all the extra ones were kicked out. And the fifth shows the final seating after \(2\) lesson.
|
Input: 5 5 5 3 3 1 1 8 2 9 4 | Output: 1 3 2 5 4
|
Expert
| 3 | 1,962 | 526 | 219 | 16 |
630 |
B
|
630B
|
B. Moore's Law
| 1,200 |
math
|
The city administration of IT City decided to fix up a symbol of scientific and technical progress in the city's main square, namely an indicator board that shows the effect of Moore's law in real time.Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately every 24 months. The implication of Moore's law is that computer performance as function of time increases exponentially as well.You are to prepare information that will change every second to display on the indicator board. Let's assume that every second the number of transistors increases exactly 1.000000011 times.
|
The only line of the input contains a pair of integers n (1000 β€ n β€ 10 000) and t (0 β€ t β€ 2 000 000 000) β the number of transistors in the initial time and the number of seconds passed since the initial time.
|
Output one number β the estimate of the number of transistors in a dence integrated circuit in t seconds since the initial time. The relative error of your answer should not be greater than 10 - 6.
|
Input: 1000 1000000 | Output: 1011.060722383550382782399454922040
|
Easy
| 1 | 634 | 211 | 197 | 6 |
|
1,637 |
D
|
1637D
|
D. Yet Another Minimization Problem
| 1,800 |
dp; greedy; math
|
You are given two arrays \(a\) and \(b\), both of length \(n\).You can perform the following operation any number of times (possibly zero): select an index \(i\) (\(1 \leq i \leq n\)) and swap \(a_i\) and \(b_i\).Let's define the cost of the array \(a\) as \(\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (a_i + a_j)^2\). Similarly, the cost of the array \(b\) is \(\sum_{i=1}^{n} \sum_{j=i + 1}^{n} (b_i + b_j)^2\).Your task is to minimize the total cost of two arrays.
|
Each test case consists of several test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 40\)) β the number of test cases. The following is a description of the input data sets.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 100\)) β the length of both arrays.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 100\)) β elements of the first array.The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \leq b_i \leq 100\)) β elements of the second array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(100\).
|
For each test case, print the minimum possible total cost.
|
In the second test case, in one of the optimal answers after all operations \(a = [2, 6, 4, 6]\), \(b = [3, 7, 6, 1]\).The cost of the array \(a\) equals to \((2 + 6)^2 + (2 + 4)^2 + (2 + 6)^2 + (6 + 4)^2 + (6 + 6)^2 + (4 + 6)^2 = 508\).The cost of the array \(b\) equals to \((3 + 7)^2 + (3 + 6)^2 + (3 + 1)^2 + (7 + 6)^2 + (7 + 1)^2 + (6 + 1)^2 = 479\).The total cost of two arrays equals to \(508 + 479 = 987\).
|
Input: 313643 6 6 62 7 4 146 7 2 42 5 3 5 | Output: 0 987 914
|
Medium
| 3 | 458 | 673 | 58 | 16 |
1,555 |
D
|
1555D
|
D. Say No to Palindromes
| 1,600 |
brute force; constructive algorithms; dp; strings
|
Let's call the string beautiful if it does not contain a substring of length at least \(2\), which is a palindrome. Recall that a palindrome is a string that reads the same way from the first character to the last and from the last character to the first. For example, the strings a, bab, acca, bcabcbacb are palindromes, but the strings ab, abbbaa, cccb are not.Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first \(3\) letters of the Latin alphabet (in lowercase).You are given a string \(s\) of length \(n\), each character of the string is one of the first \(3\) letters of the Latin alphabet (in lowercase).You have to answer \(m\) queries β calculate the cost of the substring of the string \(s\) from \(l_i\)-th to \(r_i\)-th position, inclusive.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the length of the string \(s\) and the number of queries.The second line contains the string \(s\), it consists of \(n\) characters, each character one of the first \(3\) Latin letters.The following \(m\) lines contain two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le n\)) β parameters of the \(i\)-th query.
|
For each query, print a single integer β the cost of the substring of the string \(s\) from \(l_i\)-th to \(r_i\)-th position, inclusive.
|
Consider the queries of the example test. in the first query, the substring is baa, which can be changed to bac in one operation; in the second query, the substring is baacb, which can be changed to cbacb in two operations; in the third query, the substring is cb, which can be left unchanged; in the fourth query, the substring is aa, which can be changed to ba in one operation.
|
Input: 5 4 baacb 1 3 1 5 4 5 2 3 | Output: 1 2 0 1
|
Medium
| 4 | 897 | 406 | 137 | 15 |
290 |
D
|
290D
|
D. Orange
| 1,400 |
*special; implementation
|
The first line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be a letter of English alphabet, lowercase or uppercase.The second line of the input is an integer between 0 and 26, inclusive.
|
Output the required string.
|
Input: AprilFool14 | Output: AprILFooL
|
Easy
| 2 | 0 | 234 | 27 | 2 |
||
859 |
E
|
859E
|
E. Desk Disorder
| 2,100 |
combinatorics; dfs and similar; dsu; graphs; trees
|
A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement.How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007 = 109 + 7.
|
Input will begin with a line containing N (1 β€ N β€ 100000), the number of engineers. N lines follow, each containing exactly two integers. The i-th line contains the number of the current desk of the i-th engineer and the number of the desk the i-th engineer wants to move to. Desks are numbered from 1 to 2Β·N. It is guaranteed that no two engineers sit at the same desk.
|
Print the number of possible assignments, modulo 1000000007 = 109 + 7.
|
These are the possible assignments for the first example: 1 5 3 7 1 2 3 7 5 2 3 7 1 5 7 3 1 2 7 3 5 2 7 3
|
Input: 41 55 23 77 3 | Output: 6
|
Hard
| 5 | 810 | 371 | 70 | 8 |
371 |
D
|
371D
|
D. Vessels
| 1,800 |
data structures; dsu; implementation; trees
|
There is a system of n vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to n, in the order from the highest to the lowest, the volume of the i-th vessel is ai liters. Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the i-th vessel goes to the (i + 1)-th one. The liquid that overflows from the n-th vessel spills on the floor.Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries: Add xi liters of water to the pi-th vessel; Print the number of liters of water in the ki-th vessel. When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
|
The first line contains integer n β the number of vessels (1 β€ n β€ 2Β·105). The second line contains n integers a1, a2, ..., an β the vessels' capacities (1 β€ ai β€ 109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer m β the number of queries (1 β€ m β€ 2Β·105). Each of the next m lines contains the description of one query. The query of the first type is represented as ""1 pi xi"", the query of the second type is represented as ""2 ki"" (1 β€ pi β€ n, 1 β€ xi β€ 109, 1 β€ ki β€ n).
|
For each query, print on a single line the number of liters of water in the corresponding vessel.
|
Input: 25 1061 1 42 11 2 51 1 42 12 2 | Output: 458
|
Medium
| 4 | 799 | 579 | 97 | 3 |
|
1,835 |
D
|
1835D
|
D. Doctor's Brown Hypothesis
| 2,900 |
dfs and similar; graphs; math; number theory
|
The rebels have been crushed in the most recent battle with the imperial forces, but there is a ray of new hope.Meanwhile, on one of the conquered planets, Luke was getting ready for an illegal street race (which should come as no surprise, given his family history). Luke arrived at the finish line with 88 miles per hour on his speedometer. After getting out of the car, he was greeted by a new reality. It turns out that the battle has not happened yet and will start in exactly \(k\) hours.The rebels have placed a single battleship on each of the \(n\) planets. \(m\) unidirectional wormholes connect the planets. Traversing each wormhole takes exactly one hour. Generals of the Imperium have planned the battle precisely, but their troops cannot dynamically adapt to changing circumstances. Because of this, it is enough for the rebels to move some ships around before the battle to confuse the enemy, secure victory and change the galaxy's fate.Owing to numerous strategical considerations, which we now omit, the rebels would like to choose two ships that will switch places so that both of them will be on the move for the whole time (exactly \(k\) hours). In other words, rebels look for two planets, \(x\) and \(y\), such that paths of length \(k\) exist from \(x\) to \(y\) and from \(y\) to \(x\).Because of the limited fuel supply, choosing one ship would also be acceptable. This ship should fly through the wormholes for \(k\) hours and then return to its initial planet.How many ways are there to choose the ships for completing the mission?
|
In the first line of input, there are three integer numbers \(n\), \(m\), and \(k\) (\(1 \leq n \leq 10^5\), \(0 \leq m \leq 2 \cdot 10^5\), \(n^3 \leq k \leq 10^{18}\)) denoting respectively the number of planets, wormholes and hours left until the battle starts.The following \(m\) lines contain two integers each, \(x\) and \(y\) (\(1 \leq x, y \leq n\), \(x \ne y\)), meaning that there is a wormhole from planet \(x\) to planet \(y\). It is guaranteed that there are no two identical wormholes, i. e. for every two wormholes, either \(x_1 \neq x_2\) or \(y_1 \neq y_2\).
|
In the first and only line, your program should output the number of possible ways of choosing a pair or a single warship for the mission.
|
In the first sample test, one can choose pairs of ships from the following planets: \(2\) and \(5\), \(3\) and \(5\), \(1\) and \(4\). Individual ships from planets \(6\) and \(7\) could also be chosen.In the second sample test, one can choose a pair of ships from the following planets: \(2\) and \(3\). Individual ships from planets \(1\), \(2\), \(3\), \(4\), and \(5\) could also be chosen.In the third sample test, there are no pairs of ships we can choose. Individual ships from planets \(2\) and \(3\) could also be chosen.
|
Input: 7 8 346 1 2 1 3 2 4 3 4 4 5 5 1 6 7 7 6 | Output: 5
|
Master
| 4 | 1,558 | 575 | 138 | 18 |
1,100 |
E
|
1100E
|
E. Andrew and Taxi
| 2,200 |
binary search; dfs and similar; graphs
|
Andrew prefers taxi to other means of transport, but recently most taxi drivers have been acting inappropriately. In order to earn more money, taxi drivers started to drive in circles. Roads in Andrew's city are one-way, and people are not necessary able to travel from one part to another, but it pales in comparison to insidious taxi drivers.The mayor of the city decided to change the direction of certain roads so that the taxi drivers wouldn't be able to increase the cost of the trip endlessly. More formally, if the taxi driver is on a certain crossroads, they wouldn't be able to reach it again if he performs a nonzero trip. Traffic controllers are needed in order to change the direction the road goes. For every road it is known how many traffic controllers are needed to change the direction of the road to the opposite one. It is allowed to change the directions of roads one by one, meaning that each traffic controller can participate in reversing two or more roads.You need to calculate the minimum number of traffic controllers that you need to hire to perform the task and the list of the roads that need to be reversed.
|
The first line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 100\,000\), \(1 \leq m \leq 100\,000\)) β the number of crossroads and the number of roads in the city, respectively.Each of the following \(m\) lines contain three integers \(u_{i}\), \(v_{i}\) and \(c_{i}\) (\(1 \leq u_{i}, v_{i} \leq n\), \(1 \leq c_{i} \leq 10^9\), \(u_{i} \ne v_{i}\)) β the crossroads the road starts at, the crossroads the road ends at and the number of traffic controllers required to reverse this road.
|
In the first line output two integers the minimal amount of traffic controllers required to complete the task and amount of roads \(k\) which should be reversed. \(k\) should not be minimized.In the next line output \(k\) integers separated by spaces β numbers of roads, the directions of which should be reversed. The roads are numerated from \(1\) in the order they are written in the input. If there are many solutions, print any of them.
|
There are two simple cycles in the first example: \(1 \rightarrow 5 \rightarrow 2 \rightarrow 1\) and \(2 \rightarrow 3 \rightarrow 4 \rightarrow 5 \rightarrow 2\). One traffic controller can only reverse the road \(2 \rightarrow 1\) and he can't destroy the second cycle by himself. Two traffic controllers can reverse roads \(2 \rightarrow 1\) and \(2 \rightarrow 3\) which would satisfy the condition.In the second example one traffic controller can't destroy the cycle \( 1 \rightarrow 3 \rightarrow 2 \rightarrow 1 \). With the help of three controllers we can, for example, reverse roads \(1 \rightarrow 3\) ,\( 2 \rightarrow 4\), \(1 \rightarrow 5\).
|
Input: 5 62 1 15 2 62 3 23 4 34 5 51 5 4 | Output: 2 21 3
|
Hard
| 3 | 1,138 | 498 | 441 | 11 |
551 |
B
|
551B
|
B. ZgukistringZ
| 1,800 |
brute force; constructive algorithms; implementation; strings
|
Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.GukiZ has strings a, b, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x is a string formed by consecutive segment of characters from x. Two substrings of string x overlap if there is position i in string x occupied by both of them.GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings k?
|
The first line contains string a, the second line contains string b, and the third line contains string c (1 β€ |a|, |b|, |c| β€ 105, where |s| denotes the length of string s).All three strings consist only of lowercase English letters. It is possible that b and c coincide.
|
Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.
|
In the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 β 2 (ab), 3 β 4 (ab), 5 β 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc.
|
Input: aaaab | Output: aaa
|
Medium
| 4 | 613 | 272 | 129 | 5 |
2,056 |
A
|
2056A
|
A. Shape Perimeter
| 800 |
constructive algorithms; math
|
There is an \(m\) by \(m\) square stamp on an infinite piece of paper. Initially, the bottom-left corner of the square stamp is aligned with the bottom-left corner of the paper. You are given two integer sequences \(x\) and \(y\), each of length \(n\). For each step \(i\) from \(1\) to \(n\), the following happens: Move the stamp \(x_i\) units to the right and \(y_i\) units upwards. Press the stamp onto the paper, leaving an \(m\) by \(m\) colored square at its current position. Note that the elements of sequences \(x\) and \(y\) have a special constraint: \(1\le x_i, y_i\le m - 1\).Note that you do not press the stamp at the bottom-left corner of the paper. Refer to the notes section for better understanding.It can be proven that after all the operations, the colored shape on the paper formed by the stamp is a single connected region. Find the perimeter of this colored shape.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(2 \le m \le 100\)) β the number of operations performed and the side length of the square stamp.The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le m - 1\)) β the distance that the stamp will be moved right and up during the \(i\)-th operation, respectively.Note that there are no constraints on the sum of \(n\) over all test cases.
|
For each test case, output a single integer representing the perimeter of the colored shape on the paper.
|
In the first example, the stamp has a side length of \(3\) and is pressed \(4\) times at coordinates \((1, 1)\), \((3, 3)\), \((5, 4)\), and \((6, 6)\). The piece of paper looks like that afterwards:Here, the square formed by the first press is colored blue, the second red, the third green, and the fourth purple. The combined shape, whose perimeter we need to calculate, looks like that:From the diagram, it can be seen that this shape has a perimeter of \(32\).
|
Input: 34 31 12 22 11 21 21 16 73 61 13 16 65 46 1 | Output: 32 8 96
|
Beginner
| 2 | 889 | 639 | 105 | 20 |
69 |
E
|
69E
|
E. Subsegments
| 1,800 |
data structures; implementation
|
Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.
|
The first line contains two positive integers n and k (1 β€ n β€ 105, 1 β€ k β€ n) β the number of array elements and the length of the segment. Then follow n lines: the i-th one contains a single number ai ( - 109 β€ ai β€ 109).
|
Print nβk + 1 numbers, one per line: on the i-th line print of the maximum number of those numbers from the subarray ai ai + 1 β¦ ai + k - 1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print ""Nothing"".
|
Input: 5 312233 | Output: 132
|
Medium
| 2 | 417 | 223 | 249 | 0 |
|
1,922 |
C
|
1922C
|
C. Closest Cities
| 1,300 |
greedy; implementation; math
|
There are \(n\) cities located on the number line, the \(i\)-th city is in the point \(a_i\). The coordinates of the cities are given in ascending order, so \(a_1 < a_2 < \dots < a_n\).The distance between two cities \(x\) and \(y\) is equal to \(|a_x - a_y|\).For each city \(i\), let's define the closest city \(j\) as the city such that the distance between \(i\) and \(j\) is not greater than the distance between \(i\) and each other city \(k\). For example, if the cities are located in points \([0, 8, 12, 15, 20]\), then: the closest city to the city \(1\) is the city \(2\); the closest city to the city \(2\) is the city \(3\); the closest city to the city \(3\) is the city \(4\); the closest city to the city \(4\) is the city \(3\); the closest city to the city \(5\) is the city \(4\). The cities are located in such a way that for every city, the closest city is unique. For example, it is impossible for the cities to be situated in points \([1, 2, 3]\), since this would mean that the city \(2\) has two closest cities (\(1\) and \(3\), both having distance \(1\)).You can travel between cities. Suppose you are currently in the city \(x\). Then you can perform one of the following actions: travel to any other city \(y\), paying \(|a_x - a_y|\) coins; travel to the city which is the closest to \(x\), paying \(1\) coin. You are given \(m\) queries. In each query, you will be given two cities, and you have to calculate the minimum number of coins you have to spend to travel from one city to the other city.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case is given in the following format: the first line contains one integer \(n\) (\(2 \le n \le 10^5\)); the second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_1 < a_2 < \dots < a_n \le 10^9\)); the third line contains one integer \(m\) (\(1 \le m \le 10^5\)); then \(m\) lines follow; the \(i\)-th of them contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)), denoting that in the \(i\)-th query, you have to calculate the minimum number of coins you have to spend to travel from the city \(x_i\) to the city \(y_i\). Additional constraints on the input: in every test case, for each city, the closest city is determined uniquely; the sum of \(n\) over all test cases does not exceed \(10^5\); the sum of \(m\) over all test cases does not exceed \(10^5\).
|
For each query, print one integer β the minimum number of coins you have to spend.
|
Let's consider the first two queries in the example from the statement: in the first query, you are initially in the city \(1\). You can travel to the closest city (which is the city \(2\)), paying \(1\) coin. Then you travel to the closest city (which is the city \(3\)) again, paying \(1\) coin. Then you travel to the closest city (which is the city \(4\)) again, paying \(1\) coin. In total, you spend \(3\) coins to get from the city \(1\) to the city \(4\); in the second query, you can use the same way to get from the city \(1\) to the city \(4\), and then spend \(5\) coins to travel from the city \(4\) to the city \(5\).
|
Input: 150 8 12 15 2051 41 53 43 25 1 | Output: 3 8 1 4 14
|
Easy
| 3 | 1,528 | 913 | 82 | 19 |
1,016 |
A
|
1016A
|
A. Death Note
| 900 |
greedy; implementation; math
|
You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: ""You have to write names in this notebook during \(n\) consecutive days. During the \(i\)-th day you have to write exactly \(a_i\) names."". You got scared (of course you got scared, who wouldn't get scared if he just receive a notebook which is named Death Note with a some strange rule written in it?).Of course, you decided to follow this rule. When you calmed down, you came up with a strategy how you will write names in the notebook. You have calculated that each page of the notebook can contain exactly \(m\) names. You will start writing names from the first page. You will write names on the current page as long as the limit on the number of names on this page is not exceeded. When the current page is over, you turn the page. Note that you always turn the page when it ends, it doesn't matter if it is the last day or not. If after some day the current page still can hold at least one name, during the next day you will continue writing the names from the current page.Now you are interested in the following question: how many times will you turn the page during each day? You are interested in the number of pages you will turn each day from \(1\) to \(n\).
|
The first line of the input contains two integers \(n\), \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le m \le 10^9\)) β the number of days you will write names in the notebook and the number of names which can be written on each page of the notebook.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) means the number of names you will write in the notebook during the \(i\)-th day.
|
Print exactly \(n\) integers \(t_1, t_2, \dots, t_n\), where \(t_i\) is the number of times you will turn the page during the \(i\)-th day.
|
In the first example pages of the Death Note will look like this \([1, 1, 1, 2, 2], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [3, 3, 3, 3]\). Each number of the array describes during which day name on the corresponding position will be written. It is easy to see that you should turn the first and the second page during the second day and the third page during the third day.
|
Input: 3 53 7 9 | Output: 0 2 1
|
Beginner
| 3 | 1,338 | 434 | 139 | 10 |
1,475 |
E
|
1475E
|
E. Advertising Agency
| 1,600 |
combinatorics; math; sortings
|
Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of \(n\) different bloggers. Blogger numbered \(i\) has \(a_i\) followers.Since Masha has a limited budget, she can only sign a contract with \(k\) different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.Help her, find the number of ways to select \(k\) bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).For example, if \(n=4\), \(k=3\), \(a=[1, 3, 1, 2]\), then Masha has two ways to select \(3\) bloggers with the maximum total number of followers: conclude contracts with bloggers with numbers \(1\), \(2\) and \(4\). In this case, the number of followers will be equal to \(a_1 + a_2 + a_4 = 6\). conclude contracts with bloggers with numbers \(2\), \(3\) and \(4\). In this case, the number of followers will be equal to \(a_2 + a_3 + a_4 = 6\). Since the answer can be quite large, output it modulo \(10^9+7\).
|
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 \(k\) (\(1 \le k \le n \le 1000\)) β the number of bloggers and how many of them you can sign a contract with.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots a_n\) (\(1 \le a_i \le n\)) β the number of followers of each blogger.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1000\).
|
For each test case, on a separate line output one integer β the number of ways to select \(k\) bloggers so that the total number of their followers is maximum possible.
|
The test case is explained in the statements.In the second test case, the following ways are valid: conclude contracts with bloggers with numbers \(1\) and \(2\). In this case, the number of followers will be equal to \(a_1 + a_2 = 2\); conclude contracts with bloggers with numbers \(1\) and \(3\). In this case, the number of followers will be equal to \(a_1 + a_3 = 2\); conclude contracts with bloggers with numbers \(1\) and \(4\). In this case, the number of followers will be equal to \(a_1 + a_4 = 2\); conclude contracts with bloggers with numbers \(2\) and \(3\). In this case, the number of followers will be equal to \(a_2 + a_3 = 2\); conclude contracts with bloggers with numbers \(2\) and \(4\). In this case, the number of followers will be equal to \(a_2 + a_4 = 2\); conclude contracts with bloggers with numbers \(3\) and \(4\). In this case, the number of followers will be equal to \(a_3 + a_4 = 2\). In the third test case, the following ways are valid: concludes a contract with a blogger with the number \(2\). In this case, the number of followers will be equal to \(a_2 = 2\).
|
Input: 3 4 3 1 3 1 2 4 2 1 1 1 1 2 1 1 2 | Output: 2 6 1
|
Medium
| 3 | 1,357 | 528 | 168 | 14 |
1,620 |
A
|
1620A
|
A. Equal or Not Equal
| 800 |
constructive algorithms; dsu; implementation
|
You had \(n\) positive integers \(a_1, a_2, \dots, a_n\) arranged in a circle. For each pair of neighboring numbers (\(a_1\) and \(a_2\), \(a_2\) and \(a_3\), ..., \(a_{n - 1}\) and \(a_n\), and \(a_n\) and \(a_1\)), you wrote down: are the numbers in the pair equal or not.Unfortunately, you've lost a piece of paper with the array \(a\). Moreover, you are afraid that even information about equality of neighboring elements may be inconsistent. So, you are wondering: is there any array \(a\) which is consistent with information you have about equality or non-equality of corresponding pairs?
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. Next \(t\) cases follow.The first and only line of each test case contains a non-empty string \(s\) consisting of characters E and/or N. The length of \(s\) is equal to the size of array \(n\) and \(2 \le n \le 50\). For each \(i\) from \(1\) to \(n\): if \(s_i =\) E then \(a_i\) is equal to \(a_{i + 1}\) (\(a_n = a_1\) for \(i = n\)); if \(s_i =\) N then \(a_i\) is not equal to \(a_{i + 1}\) (\(a_n \neq a_1\) for \(i = n\)).
|
For each test case, print YES if it's possible to choose array \(a\) that are consistent with information from \(s\) you know. Otherwise, print NO.It can be proved, that if there exists some array \(a\), then there exists an array \(a\) of positive integers with values less or equal to \(10^9\).
|
In the first test case, you can choose, for example, \(a_1 = a_2 = a_3 = 5\).In the second test case, there is no array \(a\), since, according to \(s_1\), \(a_1\) is equal to \(a_2\), but, according to \(s_2\), \(a_2\) is not equal to \(a_1\).In the third test case, you can, for example, choose array \(a = [20, 20, 4, 50, 50, 50, 20]\).In the fourth test case, you can, for example, choose \(a = [1, 3, 3, 7]\).
|
Input: 4 EEE EN ENNEENE NENN | Output: YES NO YES YES
|
Beginner
| 3 | 595 | 527 | 296 | 16 |
2,026 |
B
|
2026B
|
B. Black Cells
| 1,300 |
binary search; brute force; constructive algorithms; greedy
|
You are given a strip divided into cells, numbered from left to right from \(0\) to \(10^{18}\). Initially, all cells are white.You can perform the following operation: choose two white cells \(i\) and \(j\), such that \(i \ne j\) and \(|i - j| \le k\), and paint them black.A list \(a\) is given. All cells from this list must be painted black. Additionally, at most one cell that is not in this list can also be painted black. Your task is to determine the minimum value of \(k\) for which this is possible.
|
The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 < a_i < 10^{18}\); \(a_i < a_{i + 1}\)).Additional constraint on the input: the sum of \(n\) across all test cases does not exceed \(2000\).
|
For each test case, print a single integer β the minimum value of \(k\) for which it is possible to paint all the given cells black.
|
In the first example, with \(k=1\), it is possible to paint the cells \((1, 2)\).In the second example, with \(k=1\), it is possible to paint the cells \((7, 8)\).In the third example, with \(k=2\), it is possible to paint the cells \((2, 4)\) and \((8, 9)\).In the fourth example, with \(k=3\), it is possible to paint the cells \((0, 1)\), \((5, 8)\) and \((10, 13)\).
|
Input: 421 21732 4 951 5 8 10 13 | Output: 1 1 2 3
|
Easy
| 4 | 509 | 394 | 132 | 20 |
2,068 |
I
|
2068I
|
I. Pinball
| 3,500 |
graphs; shortest paths
|
You are playing a pinball-like game on a \(h \times w\) grid.The game begins with a small ball located at the center of a specific cell marked as \(\texttt{S}\). Each cell of the grid is either: A block-type wall (\(\texttt{#}\)) that prevents the ball from entering the cell, reflecting it instead. A thin oblique wall, either left-leaning (\(\texttt{\\}\)) or right-leaning (\(\texttt{/}\)), which reflects the ball according to its orientation. A free cell (\(\texttt{.}\)) where the ball can move freely. The goal is to make the ball escape the grid.At the start, you can nudge the ball in one of four directions: up (\(\texttt{U}\)), down (\(\texttt{D}\)), left (\(\texttt{L}\)), or right (\(\texttt{R}\)). The ball traverses a free cell in one second, it enters and exits a cell containing a thin oblique wall in one second, and it bounces off a block-type wall in no time (the block-type wall occupies all of its cell). Collisions between the ball and all walls, both block-type and oblique, are perfectly elastic, causing the ball to reflect upon contact.For example, the ball takes two seconds to enter a free cell, traverse it, bounce off an adjacent block-type wall, and traverse back the free cell until it exits.As the ball moves, you may destroy oblique walls at any time, permanently converting them into free cells. You may destroy multiple oblique walls throughout the game, at any given time. Determine whether it is possible for the ball to escape, and if so, find the minimum number of oblique walls that need to be destroyed, along with the precise time each chosen wall should be destroyed.
|
The first line contains two integers \(h\) and \(w\) (\(1\le h, w \le 1000\)) β the size of the grid.The next \(h\) lines describe the grid at the beginning of the game.The \(i\)-th of these lines contains \(w\) characters, describing the cells on the \(i\)-th row. A dot (\(\texttt{.}\)) denotes a free cell, a hash sign (\(\texttt{#}\)) denotes a block-type wall, a (\(\texttt{\\}\)) or (\(\texttt{/}\)) denotes a thin oblique wall, and an \(\texttt{S}\) denotes the starting position of the ball (the starting position is a free cell).It is guaranteed that all the \(h\cdot w\) characters describing the grid belong to the set \(\{\texttt{.},\, \texttt{#},\, \texttt{\\},\, \texttt{/},\, \texttt{S}\}\) and the character \(\texttt{S}\) appears exactly once.
|
Print \(\texttt{YES}\) if it is possible to make the ball escape the grid. Otherwise, print \(\texttt{NO}\).If it is possible, print the following extra information.On the second line, print a single character \(d \in \{ \texttt{U}, \texttt{D}, \texttt{L}, \texttt{R} \}\) β the direction of the starting nudge of the ball. On the third line, print \(k\) β the minimum number of oblique walls to be destroyed.On each of the following \(k\) lines, print three integers \(t_i\), \(r_i\), and \(c_i\) β the oblique wall in the cell on the \(r_i\)-th row from the top and on the \(c_i\)-th column from the left is destroyed \(t_i\) seconds after the ball starts moving. Note that the corresponding wall is destroyed just before \(t_i\) seconds have elapsed, essentially meaning that the corresponding cell acts as a free cell if the ball would have hit that wall exactly \(t_i\) seconds after the start.The operations must be printed in chronological order (i.e., \(t_i\le t_{i+1}\) for all \(1\le i\le k-1\)). The same wall cannot be destroyed multiple times (i.e., \((r_i,c_i)\not=(r_j,c_j)\) if \(i\not=j\)). Initially, there must be an oblique wall at the cell identified by \((r_i, c_i)\) for all \(1\le i\le k\).All \(t_i\) must satisfy \(0 \leq t_i \leq 10^7\). It can be proved that, if there exists a solution, there exists a solution where no \(t_i\) exceeds \(10^7\).
|
In the first sample, the minimum number of walls to be destroyed is 2. We describe the relevant moments of the solution given as sample output. At time \(t=0\), the ball is in its initial position and gets nudged towards the left direction. At time \(t=4.5\), the ball hits a block-type wall and reflects off of it. Right before \(t=7\), the wall at position \((3, 3)\) is destroyed. Right before \(t=8\), the wall at position \((1, 2)\) is destroyed. At time \(t = 10.5\), the ball finally escapes the grid. In the second sample, you can just nudge the ball towards the left or right direction, and the ball will eventually escape the grid.
|
Input: 4 6#\###.#./S###\\..####### | Output: YES L 2 7 3 3 8 1 2
|
Master
| 2 | 1,612 | 760 | 1,374 | 20 |
1,436 |
F
|
1436F
|
F. Sum Over Subsets
| 2,800 |
combinatorics; math; number theory
|
You are given a multiset \(S\). Over all pairs of subsets \(A\) and \(B\), such that: \(B \subset A\); \(|B| = |A| - 1\); greatest common divisor of all elements in \(A\) is equal to one; find the sum of \(\sum_{x \in A}{x} \cdot \sum_{x \in B}{x}\), modulo \(998\,244\,353\).
|
The first line contains one integer \(m\) (\(1 \le m \le 10^5\)): the number of different values in the multiset \(S\).Each of the next \(m\) lines contains two integers \(a_i\), \(freq_i\) (\(1 \le a_i \le 10^5, 1 \le freq_i \le 10^9\)). Element \(a_i\) appears in the multiset \(S\) \(freq_i\) times. All \(a_i\) are different.
|
Print the required sum, modulo \(998\,244\,353\).
|
A multiset is a set where elements are allowed to coincide. \(|X|\) is the cardinality of a set \(X\), the number of elements in it.\(A \subset B\): Set \(A\) is a subset of a set \(B\).In the first example \(B=\{1\}, A=\{1,2\}\) and \(B=\{2\}, A=\{1, 2\}\) have a product equal to \(1\cdot3 + 2\cdot3=9\). Other pairs of \(A\) and \(B\) don't satisfy the given constraints.
|
Input: 2 1 1 2 1 | Output: 9
|
Master
| 3 | 276 | 329 | 49 | 14 |
743 |
D
|
743D
|
D. Chloe and pleasant prizes
| 1,800 |
dfs and similar; dp; graphs; trees
|
Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai β pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.
|
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of gifts.The next line contains n integers a1, a2, ..., an ( - 109 β€ ai β€ 109) β the pleasantness of the gifts.The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.
|
If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer β the maximum possible sum of pleasantness they can get together.Otherwise print Impossible.
|
Input: 80 5 -1 4 3 2 6 51 22 42 51 33 66 76 8 | Output: 25
|
Medium
| 4 | 1,892 | 659 | 187 | 7 |
|
530 |
B
|
530B
|
B. String inside out
| 1,600 |
*special
|
You are given a string S of even length s1..s2n . Perform the following manipulations: divide it into two halves s1..sn and sn + 1..s2n reverse each of them sn..s1 and s2n..sn + 1 concatenate the resulting strings into sn..s1s2n..sn + 1Output the result of these manipulations.
|
The only line of the input contains a string of lowercase Latin letters. The length of the string is between 2 and 20, inclusive, and it is even.
|
Output the string which is the result of the described manipulations.
|
Input: codeforces | Output: fedocsecro
|
Medium
| 1 | 277 | 145 | 69 | 5 |
|
177 |
D2
|
177D2
|
D2. Encrypting Messages
| 1,500 |
data structures
|
The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help.A message is a sequence of n integers a1, a2, ..., an. Encryption uses a key which is a sequence of m integers b1, b2, ..., bm (m β€ n). All numbers from the message and from the key belong to the interval from 0 to c - 1, inclusive, and all the calculations are performed modulo c.Encryption is performed in n - m + 1 steps. On the first step we add to each number a1, a2, ..., am a corresponding number b1, b2, ..., bm. On the second step we add to each number a2, a3, ..., am + 1 (changed on the previous step) a corresponding number b1, b2, ..., bm. And so on: on step number i we add to each number ai, ai + 1, ..., ai + m - 1 a corresponding number b1, b2, ..., bm. The result of the encryption is the sequence a1, a2, ..., an after n - m + 1 steps.Help the Beaver to write a program that will encrypt messages in the described manner.
|
The first input line contains three integers n, m and c, separated by single spaces. The second input line contains n integers ai (0 β€ ai < c), separated by single spaces β the original message. The third input line contains m integers bi (0 β€ bi < c), separated by single spaces β the encryption key.The input limitations for getting 30 points are: 1 β€ m β€ n β€ 103 1 β€ c β€ 103 The input limitations for getting 100 points are: 1 β€ m β€ n β€ 105 1 β€ c β€ 103
|
Print n space-separated integers β the result of encrypting the original message.
|
In the first sample the encryption is performed in two steps: after the first step a = (0, 0, 0, 1) (remember that the calculations are performed modulo 2), after the second step a = (0, 1, 1, 0), and that is the answer.
|
Input: 4 3 21 1 1 11 1 1 | Output: 0 1 1 0
|
Medium
| 1 | 1,048 | 455 | 81 | 1 |
198 |
C
|
198C
|
C. Delivering Carcinogen
| 2,400 |
binary search; geometry
|
Qwerty the Ranger arrived to the Diatar system with a very important task. He should deliver a special carcinogen for scientific research to planet Persephone. This is urgent, so Qwerty has to get to the planet as soon as possible. A lost day may fail negotiations as nobody is going to pay for an overdue carcinogen.You can consider Qwerty's ship, the planet Persephone and the star Diatar points on a plane. Diatar is located in the origin of coordinate axes β at point (0, 0). Persephone goes round Diatar along a circular orbit with radius R in the counter-clockwise direction at constant linear speed vp (thus, for instance, a full circle around the star takes of time). At the initial moment of time Persephone is located at point (xp, yp).At the initial moment of time Qwerty's ship is at point (x, y). Qwerty can move in any direction at speed of at most v (v > vp). The star Diatar is hot (as all stars), so Qwerty can't get too close to it. The ship's metal sheathing melts at distance r (r < R) from the star.Find the minimum time Qwerty needs to get the carcinogen to planet Persephone.
|
The first line contains space-separated integers xp, yp and vp ( - 104 β€ xp, yp β€ 104, 1 β€ vp < 104) β Persephone's initial position and the speed at which it goes round Diatar.The second line contains space-separated integers x, y, v and r ( - 104 β€ x, y β€ 104, 1 < v β€ 104, 1 β€ r β€ 104) β The intial position of Qwerty's ship, its maximum speed and the minimum safe distance to star Diatar.It is guaranteed that r2 < x2 + y2, r2 < xp2 + yp2 and vp < v.
|
Print a single real number β the minimum possible delivery time. The answer will be considered valid if its absolute or relative error does not exceed 10 - 6.
|
Input: 10 0 1-10 0 2 8 | Output: 9.584544103
|
Expert
| 2 | 1,098 | 454 | 158 | 1 |
|
1,987 |
H
|
1987H
|
H. Fumo Temple
| 3,500 |
interactive
|
This temple only magnifies the mountain's power.BadelineThis is an interactive problem.You are given two positive integers \(n\) and \(m\) (\(\bf{n \le m}\)).The jury has hidden from you a rectangular matrix \(a\) with \(n\) rows and \(m\) columns, where \(a_{i,j} \in \{ -1, 0, 1 \}\) for all \(1 \le i \le n\) and \(1 \le j \le m\). The jury has also selected a cell \((i_0, j_0)\). Your goal is to find \((i_0,j_0)\).In one query, you give a cell \((i, j)\), then the jury will reply with an integer. If \((i, j) = (i_0, j_0)\), the jury will reply with \(0\). Else, let \(S\) be the sum of \(a_{x,y}\) over all \(x\) and \(y\) such that \(\min(i, i_0) \le x \le \max(i, i_0)\) and \(\min(j, j_0) \le y \le \max(j, j_0)\). Then, the jury will reply with \(|i - i_0| + |j - j_0| + |S|\). Find \((i_0, j_0)\) by making at most \(n + 225\) queries.Note: the grader is not adaptive: \(a\) and \((i_0,j_0)\) are fixed before any queries are made.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 50\)) β the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le m \le 5000\)) β the numbers of rows and the number of columns of the hidden matrix \(a\) respectively.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(25 \cdot 10^6\).
|
The hidden matrix in the first test case: \(1\)\(0\)\(1\)\(\color{red}{\textbf{0}}\)\(1\)\(0\)\(0\)\(1\)\(0\)\(-1\)\(-1\)\(-1\)The hidden matrix in the second test case: \(\color{red}{\textbf{0}}\)Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.
|
Input: 2 3 4 5 3 5 1 1 0 | Output: ? 1 1 ? 3 3 ? 3 2 ! 1 4 ? 1 1 ! 1 1
|
Master
| 1 | 944 | 475 | 0 | 19 |
|
708 |
D
|
708D
|
D. Incorrect Flow
| 2,900 |
flows
|
At the entrance examination for the magistracy of the MSU Cyber-Mechanics Department Sasha got the question about Ford-Fulkerson algorithm. He knew the topic perfectly as he worked with it many times on programming competition. As the task for the question he was given a network with partially build flow that he had to use in order to demonstrate the workflow of the algorithm. He quickly finished to write the text and took a look at the problem only to understand that the given network is incorrect!Suppose you are given a directed graph G(V, E) with two special nodes s and t called source and sink. We denote as n the number of nodes in the graph, i.e. n = |V| and m stands for the number of directed edges in the graph, i.e. m = |E|. For the purpose of this problem we always consider node 1 to be the source and node n to be the sink. In addition, for each edge of the graph e we define the capacity function c(e) and flow function f(e). Function f(e) represents the correct flow if the following conditions are satisfied: For each edge the flow is non-negative and does not exceed capacity c(e), i.e. 0 β€ f(e) β€ c(e). For each node , that is not source or sink (v β s and v β t) the sum of flows of all edges going in v is equal to the sum of the flows among all edges going out from v. In other words, there is no flow stuck in v. It was clear that as the exam was prepared last night and there are plenty of mistakes in the tasks. Sasha asked one of the professors to fix the network or give the correct task, but the reply was that the magistrate student should be able to fix the network himself. As the professor doesn't want the task to become easier, he asks Sasha to fix the network in a such way that the total number of changes is minimum possible. Sasha is not allowed to remove edges, add new ones or reverse the direction of existing edges. The only thing he is able to do is to change capacity function c(e) and flow function f(e). Moreover, all the values should remain non-negative integers. There is no requirement on the flow to be maximum in any sense.Find the minimum possible total change of the functions f(e) and c(e) that Sasha has to make in order to make the flow correct. The total change is defined as the sum of absolute differences, i.e. if new functions are f * (e) and c * (e), then the total change is .
|
The first line of the input contains two integers n and m (2 β€ n β€ 100, 0 β€ m β€ 100) β the number of nodes and edges in the graph respectively. Each of the following m lines contains the description of the edges, consisting of four integers ui, vi, ci and fi (1 β€ ui, vi β€ n, ui β vi, 0 β€ ci, fi β€ 1 000 000) β index of the node the edges starts from, the index of the node the edge goes to, current capacity and flow value.Node number 1 is the source, and node number n is the sink. It's guaranteed that no edge goes to the source, and no edges starts in the sink.Given graph contains no self-loops but may contain multiple edges.
|
Print one integer β the minimum total sum of changes that Sasha has to do in order to get the correct flow description.
|
In the first sample, the flow is initially correct. Note, that the flow is not maximum, but this is not required.In the second sample, the flow value of the only edge is greater than its capacity. There are two ways to fix this: either increase the capacity up to 2 or reduce the flow down to 1.In the third sample, there is only 1 unit of flow coming to vertex 2, but there are 2 units going out of it. One of the possible solutions is to reduce the value of the flow on the second edge by 1.In the fourth sample, there is isolated circulation of flow, but this description is correct by definition.
|
Input: 2 11 2 2 1 | Output: 0
|
Master
| 1 | 2,346 | 631 | 119 | 7 |
835 |
D
|
835D
|
D. Palindromic characteristics
| 1,900 |
brute force; dp; hashing; strings
|
Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th number is the total number of non-empty substrings of s which are k-palindromes.A string is 1-palindrome if and only if it reads the same backward as forward.A string is k-palindrome (k > 1) if and only if: Its left half equals to its right half. Its left and right halfs are non-empty (k - 1)-palindromes. The left half of string t is its prefix of length β|t| / 2β, and right half β the suffix of the same length. β|t| / 2β denotes the length of string t divided by 2, rounded down.Note that each substring is counted as many times as it appears in the string. For example, in the string ""aaa"" the substring ""a"" appears 3 times.
|
The first line contains the string s (1 β€ |s| β€ 5000) consisting of lowercase English letters.
|
Print |s| integers β palindromic characteristics of string s.
|
In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here.
|
Input: abba | Output: 6 1 0 0
|
Hard
| 4 | 732 | 94 | 61 | 8 |
1,857 |
C
|
1857C
|
C. Assembly via Minimums
| 1,200 |
greedy; sortings
|
Sasha has an array \(a\) of \(n\) integers. He got bored and for all \(i\), \(j\) (\(i < j\)), he wrote down the minimum value of \(a_i\) and \(a_j\). He obtained a new array \(b\) of size \(\frac{n\cdot (n-1)}{2}\).For example, if \(a=\) [\(2,3,5,1\)], he would write [\(\min(2, 3), \min(2, 5), \min(2, 1), \min(3, 5), \min(3, 1), min(5, 1)\)] \(=\) [\(2, 2, 1, 3, 1, 1\)].Then, he randomly shuffled all the elements of the array \(b\).Unfortunately, he forgot the array \(a\), and your task is to restore any possible array \(a\) from which the array \(b\) could have been obtained.The elements of array \(a\) should be in the range \([-10^9,10^9]\).
|
The first line contains a single integer \(t\) (\(1\le t\le 200\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(2\le n\le 10^3\)) β the length of array \(a\).The second line of each test case contains \(\frac{n\cdot (n-1)}{2}\) integers \(b_1,b_2,\dots,b_{\frac{n\cdot (n-1)}{2}}\) (\(β10^9\le b_i\le 10^9\)) β the elements of array \(b\).It is guaranteed that the sum of \(n\) over all tests does not exceed \(10^3\) and for each array \(b\) in the test, there exists an original array.
|
For each test case, output any possible array \(a\) of length \(n\).
|
In the first sample, Sasha chose the array \([1,3,3]\), then the array \(b\) will look like \([\min(a_1,a_2)=1, \min(a_1,a_3)=1, \min(a_2,a_3)=3]\), after shuffling its elements, the array can look like \([1,3,1]\).In the second sample, there is only one pair, so the array \([10,10]\) is suitable. Another suitable array could be \([15,10]\).
|
Input: 531 3 121047 5 3 5 3 352 2 2 2 2 2 2 2 2 253 0 0 -2 0 -2 0 0 -2 -2 | Output: 1 3 3 10 10 7 5 3 12 2 2 2 2 2 0 -2 0 3 5
|
Easy
| 2 | 652 | 537 | 68 | 18 |
520 |
C
|
520C
|
C. DNA Alignment
| 1,500 |
math; strings
|
Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences.Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in which the respective symbols of s and t are the same. Function h(s, t) can be used to define the function of Vasya distance Ο(s, t): where is obtained from string s, by applying left circular shift i times. For example, Ο(""AGC"", ""CGT"") = h(""AGC"", ""CGT"") + h(""AGC"", ""GTC"") + h(""AGC"", ""TCG"") + h(""GCA"", ""CGT"") + h(""GCA"", ""GTC"") + h(""GCA"", ""TCG"") + h(""CAG"", ""CGT"") + h(""CAG"", ""GTC"") + h(""CAG"", ""TCG"") = 1 + 1 + 0 + 0 + 1 + 1 + 1 + 0 + 1 = 6Vasya found a string s of length n on the Internet. Now he wants to count how many strings t there are such that the Vasya distance from the string s attains maximum possible value. Formally speaking, t must satisfy the equation: .Vasya could not try all possible strings to find an answer, so he needs your help. As the answer may be very large, count the number of such strings modulo 109 + 7.
|
The first line of the input contains a single integer n (1 β€ n β€ 105).The second line of the input contains a single string of length n, consisting of characters ""ACGT"".
|
Print a single number β the answer modulo 109 + 7.
|
Please note that if for two distinct strings t1 and t2 values Ο(s, t1) ΠΈ Ο(s, t2) are maximum among all possible t, then both strings must be taken into account in the answer even if one of them can be obtained by a circular shift of another one.In the first sample, there is Ο(""C"", ""C"") = 1, for the remaining strings t of length 1 the value of Ο(s, t) is 0.In the second sample, Ο(""AG"", ""AG"") = Ο(""AG"", ""GA"") = Ο(""AG"", ""AA"") = Ο(""AG"", ""GG"") = 4.In the third sample, Ο(""TTT"", ""TTT"") = 27
|
Input: 1C | Output: 1
|
Medium
| 2 | 1,186 | 171 | 50 | 5 |
2,068 |
D
|
2068D
|
D. Morse Code
| 3,100 |
dp; sortings; trees
|
Morse code is a classical way to communicate over long distances, but there are some drawbacks that increase the transmission time of long messages. In Morse code, each character in the alphabet is assigned a sequence of dots and dashes such that no sequence is a prefix of another. To transmit a string of characters, the sequences corresponding to each character are sent in order. A dash takes twice as long to transmit as a dot. Your alphabet has \(n\) characters, where the \(i\)-th character appears with frequency \(f_i\) in your language. Your task is to design a Morse code encoding scheme, assigning a sequence of dots and dashes to each character, that minimizes the expected transmission time for a single character. In other words, you want to minimize \(f_1t_1 + f_2t_2 + \cdots + f_nt_n\), where \(t_i\) is the time required to transmit the sequence of dots and dashes assigned to the \(i\)-th character.
|
The first line contains an integer \(n\) (\(2\le n\le 200\)) β the number of characters in the alphabet.The second line contains \(n\) real numbers \(f_1\), \(f_2\), \(\ldots\), \(f_n\) (\(0 < f_i < 1\)) β \(f_i\) is the frequency of the \(i\)-th character. All values \(f_1\), \(f_2\), \(\ldots\), \(f_n\) are given with exactly four digits after the decimal point. The sum of all frequencies is exactly 1.
|
Print \(n\) lines, each containing one string consisting of dots \(\texttt{.}\) and dashes \(\texttt{-}\). The \(i\)-th line corresponds to the sequence of dots and dashes that you assign to the \(i\)-th character.If there are multiple valid assignments with the minimum possible expected transmission time, any of them is considered correct.
|
In the first sample, the alphabet contains three letters, say \(a\), \(b\), and \(c\), with respective frequencies \(0.3\), \(0.6\), and \(0.1\). In the optimal assignment, we assign \(a\) to '\(\texttt{-.}\)', \(b\) to '\(\texttt{.}\)', and \(c\) to '\(\texttt{--}\)'. This gives an expected transmission time of \(0.3 \cdot 3 + 0.6 \cdot 1 + 0.1 \cdot 4 = 1.9\) time units per character, which is optimal.For comparison, the assignment \(a\to\) '\(\texttt{..}\)', \(b \to\) '\(\texttt{-}\)', \(c \to\) '\(\texttt{.-}\)' has an expected transmission time of \(0.3 \cdot 2 + 0.6 \cdot 2 + 0.1 \cdot 3 = 2.1\). The assignment \(a \to\) '\(\texttt{-}\)', \(b \to\) '\(\texttt{.}\)', \(c \to\) '\(\texttt{..}\)' has a lower expected transmission time, but is invalid since '\(\texttt{.}\)' is a prefix of '\(\texttt{..}\)'.
|
Input: 30.3000 0.6000 0.1000 | Output: -. . --
|
Master
| 3 | 919 | 407 | 342 | 20 |
2,059 |
E1
|
2059E1
|
E1. Stop Gaming (Easy Version)
| 2,500 |
brute force; constructive algorithms; greedy; hashing; strings
|
This is the easy version of the problem. The difference between the versions is that in this version you only need to find the minimum number of operations. You can hack only if you solved all versions of this problem.You are given \(n\) arrays, each of which has a length of \(m\). Let the \(j\)-th element of the \(i\)-th array be denoted as \(a_{i, j}\). It is guaranteed that all \(a_{i, j}\) are pairwise distinct. In one operation, you can do the following: Choose some integer \(i\) (\(1 \le i \le n\)) and an integer \(x\) (\(1 \le x \le 2 \cdot n \cdot m\)). For all integers \(k\) from \(i\) to \(n\) in increasing order, do the following: Add the element \(x\) to the beginning of the \(k\)-th array. Assign \(x\) the value of the last element in the \(k\)-th array. Remove the last element from the \(k\)-th array. In other words, you can insert an element at the beginning of any array, after which all elements in this and all following arrays are shifted by one to the right. The last element of the last array is removed.You are also given a description of the arrays that need to be obtained after all operations. That is, after performing the operations, the \(j\)-th element of the \(i\)-th array should be equal to \(b_{i, j}\). It is guaranteed that all \(b_{i, j}\) are pairwise distinct.Determine the minimum number of operations that need to be performed to obtain the desired arrays.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\)) β the number of arrays and the number of elements in each array.The \(i\)-th of the following \(n\) lines contains \(m\) integers \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}\) (\(1 \le a_{i, j} \le 2 \cdot n \cdot m\)) β the elements of the \(i\)-th original array. It is guaranteed that all \(a_{i, j}\) are pairwise distinct.The \(i\)-th of the following \(n\) lines contains \(m\) integers \(b_{i, 1}, b_{i, 2}, \ldots, b_{i, m}\) (\(1 \le b_{i, j} \le 2 \cdot n \cdot m\)) β the elements of the \(i\)-th final array. It is guaranteed that all \(b_{i, j}\) are pairwise distinct.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of operations that need to be performed.
|
In the first test case, the following sequence of \(3\) operations is suitable: Apply the operation to the first array with \(x = 1\). Then the element \(1\) will be added to the beginning of the first array, and the value of \(x\) will become \(6\). The last element will be removed, and the first array will look like \([1, 2]\). Next, the element \(x\) is added to the beginning of the second array, and the value of \(x\) becomes \(4\). The last element of the second array is removed, and both arrays look like \([1, 2]\) and \([6, 3]\) respectively after the first operation. Apply the operation to the second array with \(x = 8\). Then the first array remains unchanged, and both arrays will look like \([1, 2]\) and \([8, 6]\) respectively. Apply the operation to the second array with \(x = 7\), then both arrays will have the required appearance \([1, 2]\) and \([7, 8]\) respectively. In the second test case, the desired array can only be achieved in \(5\) operations.In the third test case, the following sequence of \(3\) operations is suitable: Apply the operation with \(x = 11\) to the first array. Apply the operation with \(x = 12\) to the second array. Apply the operation with \(x = 13\) to the third array.
|
Input: 42 22 63 41 27 81 55 4 1 2 35 4 3 2 13 31 2 34 5 67 8 911 1 212 3 413 5 64 41 2 3 45 6 7 89 10 11 1213 14 15 1617 1 2 34 18 5 67 19 8 209 21 22 10 | Output: 3 5 3 6
|
Expert
| 5 | 1,408 | 965 | 105 | 20 |
286 |
E
|
286E
|
E. Ladies' Shop
| 2,800 |
constructive algorithms; fft; math
|
A ladies' shop has recently opened in the city of Ultima Thule. To get ready for the opening, the shop bought n bags. Each bag is characterised by the total weight ai of the items you can put there. The weird thing is, you cannot use these bags to put a set of items with the total weight strictly less than ai. However the weights of the items that will be sold in the shop haven't yet been defined. That's what you should determine right now.Your task is to find the set of the items' weights p1, p2, ..., pk (1 β€ p1 < p2 < ... < pk), such that: Any bag will be used. That is, for any i (1 β€ i β€ n) there will be such set of items that their total weight will equal ai. We assume that there is the infinite number of items of any weight. You can put multiple items of the same weight in one bag. For any set of items that have total weight less than or equal to m, there is a bag into which you can put this set. Similarly, a set of items can contain multiple items of the same weight. Of all sets of the items' weights that satisfy points 1 and 2, find the set with the minimum number of weights. In other words, value k should be as small as possible. Find and print the required set.
|
The first line contains space-separated integers n and m (1 β€ n, m β€ 106). The second line contains n distinct space-separated integers a1, a2, ..., an (1 β€ a1 < a2 < ... < an β€ m) β the bags' weight limits.
|
In the first line print ""NO"" (without the quotes) if there isn't set pi, that would meet the conditions.Otherwise, in the first line print ""YES"" (without the quotes), in the second line print an integer k (showing how many numbers are in the suitable set with the minimum number of weights), in the third line print k space-separated integers p1, p2, ..., pk (1 β€ p1 < p2 < ... < pk). If there are multiple solutions, print any of them.
|
Input: 6 105 6 7 8 9 10 | Output: YES55 6 7 8 9
|
Master
| 3 | 1,188 | 207 | 440 | 2 |
|
919 |
B
|
919B
|
B. Perfect Number
| 1,100 |
binary search; brute force; dp; implementation; number theory
|
We consider a positive integer perfect, if and only if the sum of its digits is exactly \(10\). Given a positive integer \(k\), your task is to find the \(k\)-th smallest perfect positive integer.
|
A single line with a positive integer \(k\) (\(1 \leq k \leq 10\,000\)).
|
A single number, denoting the \(k\)-th smallest perfect integer.
|
The first perfect integer is \(19\) and the second one is \(28\).
|
Input: 1 | Output: 19
|
Easy
| 5 | 196 | 72 | 64 | 9 |
1,538 |
C
|
1538C
|
C. Number of Pairs
| 1,300 |
binary search; data structures; math; two pointers
|
You are given an array \(a\) of \(n\) integers. Find the number of pairs \((i, j)\) (\(1 \le i < j \le n\)) where the sum of \(a_i + a_j\) is greater than or equal to \(l\) and less than or equal to \(r\) (that is, \(l \le a_i + a_j \le r\)).For example, if \(n = 3\), \(a = [5, 1, 2]\), \(l = 4\) and \(r = 7\), then two pairs are suitable: \(i=1\) and \(j=2\) (\(4 \le 5 + 1 \le 7\)); \(i=1\) and \(j=3\) (\(4 \le 5 + 2 \le 7\)).
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains three integers \(n, l, r\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le l \le r \le 10^9\)) β the length of the array and the limits on the sum in the pair.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)).It is guaranteed that the sum of \(n\) overall test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the number of index pairs \((i, j)\) (\(i < j\)), such that \(l \le a_i + a_j \le r\).
|
Input: 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1 | Output: 2 7 0 1
|
Easy
| 4 | 431 | 468 | 132 | 15 |
|
40 |
D
|
40D
|
D. Interesting Sequence
| 2,600 |
math
|
Berland scientists noticed long ago that the world around them depends on Berland population. Due to persistent research in this area the scientists managed to find out that the Berland chronology starts from the moment when the first two people came to that land (it is considered to have happened in the first year). After one Berland year after the start of the chronology the population had already equaled 13 people (the second year). However, tracing the population number during the following years was an ultimately difficult task, still it was found out that if di β the number of people in Berland in the year of i, then either di = 12di - 2, or di = 13di - 1 - 12di - 2. Of course no one knows how many people are living in Berland at the moment, but now we can tell if there could possibly be a year in which the country population equaled A. That's what we ask you to determine. Also, if possible, you have to find out in which years it could be (from the beginning of Berland chronology). Let's suppose that it could be in the years of a1, a2, ..., ak. Then you have to define how many residents could be in the country during those years apart from the A variant. Look at the examples for further explanation.
|
The first line contains integer A (1 β€ A < 10300). It is guaranteed that the number doesn't contain leading zeros.
|
On the first output line print YES, if there could be a year in which the total population of the country equaled A, otherwise print NO. If the answer is YES, then you also have to print number k β the number of years in which the population could equal A. On the next line you have to output precisely k space-separated numbers β a1, a2, ..., ak. Those numbers have to be output in the increasing order.On the next line you should output number p β how many variants of the number of people could be in the years of a1, a2, ..., ak, apart from the A variant. On each of the next p lines you have to print one number β the sought number of residents. Those number also have to go in the increasing order. If any number (or both of them) k or p exceeds 1000, then you have to print 1000 instead of it and only the first 1000 possible answers in the increasing order.The numbers should have no leading zeros.
|
Input: 2 | Output: YES110
|
Expert
| 1 | 1,224 | 114 | 906 | 0 |
|
1,283 |
D
|
1283D
|
D. Christmas Trees
| 1,800 |
graphs; greedy; shortest paths
|
There are \(n\) Christmas trees on an infinite number line. The \(i\)-th tree grows at the position \(x_i\). All \(x_i\) are guaranteed to be distinct.Each integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything.There are \(m\) people who want to celebrate Christmas. Let \(y_1, y_2, \dots, y_m\) be the positions of people (note that all values \(x_1, x_2, \dots, x_n, y_1, y_2, \dots, y_m\) should be distinct and all \(y_j\) should be integer). You want to find such an arrangement of people that the value \(\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j|\) is the minimum possible (in other words, the sum of distances to the nearest Christmas tree for all people should be minimized).In other words, let \(d_j\) be the distance from the \(j\)-th human to the nearest Christmas tree (\(d_j = \min\limits_{i=1}^{n} |y_j - x_i|\)). Then you need to choose such positions \(y_1, y_2, \dots, y_m\) that \(\sum\limits_{j=1}^{m} d_j\) is the minimum possible.
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of Christmas trees and the number of people.The second line of the input contains \(n\) integers \(x_1, x_2, \dots, x_n\) (\(-10^9 \le x_i \le 10^9\)), where \(x_i\) is the position of the \(i\)-th Christmas tree. It is guaranteed that all \(x_i\) are distinct.
|
In the first line print one integer \(res\) β the minimum possible value of \(\sum\limits_{j=1}^{m}\min\limits_{i=1}^{n}|x_i - y_j|\) (in other words, the sum of distances to the nearest Christmas tree for all people).In the second line print \(m\) integers \(y_1, y_2, \dots, y_m\) (\(-2 \cdot 10^9 \le y_j \le 2 \cdot 10^9\)), where \(y_j\) is the position of the \(j\)-th human. All \(y_j\) should be distinct and all values \(x_1, x_2, \dots, x_n, y_1, y_2, \dots, y_m\) should be distinct.If there are multiple answers, print any of them.
|
Input: 2 6 1 5 | Output: 8 -1 2 6 4 0 3
|
Medium
| 3 | 1,055 | 374 | 543 | 12 |
|
585 |
E
|
585E
|
E. Present for Vitalik the Philatelist
| 2,900 |
combinatorics; math; number theory
|
Vitalik the philatelist has a birthday today!As he is a regular customer in a stamp store called 'Robin Bobin', the store management decided to make him a gift.Vitalik wants to buy one stamp and the store will give him a non-empty set of the remaining stamps, such that the greatest common divisor (GCD) of the price of the stamps they give to him is more than one. If the GCD of prices of the purchased stamp and prices of present stamps set will be equal to 1, then Vitalik will leave the store completely happy.The store management asks you to count the number of different situations in which Vitalik will leave the store completely happy. Since the required number of situations can be very large, you need to find the remainder of this number modulo 109 + 7. The situations are different if the stamps purchased by Vitalik are different, or if one of the present sets contains a stamp that the other present does not contain.
|
The first line of the input contains integer n (2 β€ n β€ 5Β·105) β the number of distinct stamps, available for sale in the 'Robin Bobin' store. The second line contains a sequence of integers a1, a2, ..., an (2 β€ ai β€ 107), where ai is the price of the i-th stamp.
|
Print a single integer β the remainder of the sought number of situations modulo 109 + 7.
|
In the first sample the following situations are possible: Vitalik buys the 1-st stamp, the store gives him the 2-nd stamp as a present; Vitalik buys the 3-rd stamp, the store gives him the 2-nd stamp as a present; Vitalik buys the 2-nd stamp, the store gives him the 1-st stamp as a present; Vitalik buys the 2-nd stamp, the store gives him the 3-rd stamp as a present; Vitalik buys the 2-nd stamp, the store gives him the 1-st and 3-rd stamps as a present.
|
Input: 32 3 2 | Output: 5
|
Master
| 3 | 931 | 263 | 89 | 5 |
1,324 |
B
|
1324B
|
B. Yet Another Palindrome Problem
| 1,100 |
brute force; strings
|
You are given an array \(a\) consisting of \(n\) integers.Your task is to determine if \(a\) has some subsequence of length at least \(3\) that is a palindrome.Recall that an array \(b\) is called a subsequence of the array \(a\) if \(b\) can be obtained by removing some (possibly, zero) elements from \(a\) (not necessarily consecutive) without changing the order of remaining elements. For example, \([2]\), \([1, 2, 1, 3]\) and \([2, 3]\) are subsequences of \([1, 2, 1, 3]\), but \([1, 1, 2]\) and \([4]\) are not.Also, recall that a palindrome is an array that reads the same backward as forward. In other words, the array \(a\) of length \(n\) is the palindrome if \(a_i = a_{n - i - 1}\) for all \(i\) from \(1\) to \(n\). For example, arrays \([1234]\), \([1, 2, 1]\), \([1, 3, 2, 2, 3, 1]\) and \([10, 100, 10]\) are palindromes, but arrays \([1, 2]\) and \([1, 2, 3, 1]\) are not.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β the number of test cases.Next \(2t\) lines describe test cases. The first line of the test case contains one integer \(n\) (\(3 \le n \le 5000\)) β the length of \(a\). The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)), where \(a_i\) is the \(i\)-th element of \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5000\) (\(\sum n \le 5000\)).
|
For each test case, print the answer β ""YES"" (without quotes) if \(a\) has some subsequence of length at least \(3\) that is a palindrome and ""NO"" otherwise.
|
In the first test case of the example, the array \(a\) has a subsequence \([1, 2, 1]\) which is a palindrome.In the second test case of the example, the array \(a\) has two subsequences of length \(3\) which are palindromes: \([2, 3, 2]\) and \([2, 2, 2]\).In the third test case of the example, the array \(a\) has no subsequences of length at least \(3\) which are palindromes.In the fourth test case of the example, the array \(a\) has one subsequence of length \(4\) which is a palindrome: \([1, 2, 2, 1]\) (and has two subsequences of length \(3\) which are palindromes: both are \([1, 2, 1]\)).In the fifth test case of the example, the array \(a\) has no subsequences of length at least \(3\) which are palindromes.
|
Input: 5 3 1 2 1 5 1 2 2 3 2 3 1 1 2 4 1 2 2 1 10 1 1 2 2 3 3 4 4 5 5 | Output: YES YES NO YES NO
|
Easy
| 2 | 939 | 506 | 161 | 13 |
2,055 |
E
|
2055E
|
E. Haystacks
| 2,800 |
brute force; constructive algorithms; data structures; greedy; sortings
|
On the next new moon, the universe will reset, beginning with Florida. It's up to Florida Man to stop it, but he first needs to find an important item.There are \(n\) haystacks labelled from \(1\) to \(n\), where haystack \(i\) contains \(a_i\) haybales. One of the haystacks has a needle hidden beneath it, but you do not know which one. Your task is to move the haybales so that each haystack is emptied at least once, allowing you to check if the needle is hidden under that particular haystack.However, the process is not that simple. Once a haystack \(i\) is emptied for the first time, it will be assigned a height limit and can no longer contain more than \(b_i\) haybales. More formally, a move is described as follows: Choose two haystacks \(i\) and \(j\). If haystack \(i\) has not been emptied before, or haystack \(i\) contains strictly less than \(b_i\) haybales, you may move exactly \(1\) haybale from haystack \(j\) to haystack \(i\). Note: Before a haystack is emptied, it has no height limit, and you can move as many haybales as you want onto that haystack.Compute the minimum number of moves required to ensure that each haystack is emptied at least once, or report that it is impossible.
|
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\) (\(2\le n\le 5\cdot 10^5\)) β the number of haystacks.The \(i\)-th of the next \(n\) lines contains two integers \(a_i\) and \(b_i\) (\(1\le a_i, b_i\le 10^9\)) β the initial number of haybales in the \(i\)-th haystack, and the height limit that it is assigned after it is emptied for the first time.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, print a single integer β the minimum number of moves required to ensure that each haystack is emptied at least once. If it is not possible to empty each haystack at least once, output -1.
|
In the first test case, we can do the following sequence of moves: Move \(3\) haybales from haystack \(1\) to haystack \(2\). Haystack \(1\) is now emptied, and is assigned a height limit of \(5\). Move \(5\) haybales from haystack \(2\) to haystack \(1\). Haystack \(2\) is now emptied, and is assigned a height limit of \(4\). The above sequence requires \(3 + 5 = 8\) moves. It is not possible to use less than \(8\) moves as the following sequence of moves is invalid: Move \(2\) haybales from haystack \(2\) to haystack \(1\). Haystack \(2\) is now emptied, and is assigned a height limit of \(4\). Move \(4\) haybales from haystack \(1\) to haystack \(2\). Haystack \(1\) now has \(1\) haybale, while haystack \(2\) has \(4\) haybales. Haystack \(1\) cannot be emptied as haystack \(2\) is already at its height limit of \(4\), so no more haybales can be moved from haystack \(1\) to haystack \(2\). In the second test case, the task is impossible. This is because the height limits of both haystacks are too small that once one of the haystacks is emptied, the other haystack cannot be emptied due to the small height limits.In the third test case, the following sequence of moves can be shown to be optimal: Move \(1\) haybale from haystack \(1\) to haystack \(3\). Haystack \(1\) is now emptied, and is assigned a height limit of \(3\). Move \(3\) haybales from haystack \(2\) to haystack \(1\). Move \(1\) haybale from haystack \(2\) to haystack \(3\). Haystack \(2\) is now emptied and is assigned a height limit of \(3\). Move \(3\) haybales from haystack \(3\) to haystack \(2\). Haystack \(3\) is now emptied, and is assigned a height limit of \(1\). The above sequence requires \(1 + 3 + 1 + 3 = 8\) moves.
|
Input: 723 52 4210 11 1031 34 31 135 42 41 1062 13 35 41 51 61 853 21 21 11 36 525 107 12 | Output: 8 -1 8 9 14 15 19
|
Master
| 5 | 1,208 | 619 | 207 | 20 |
1,746 |
C
|
1746C
|
C. Permutation Operations
| 1,300 |
constructive algorithms; greedy; implementation; math
|
You are given a permutation \(a\) of size \(n\) and you should perform \(n\) operations on it. In the \(i\)-th operation, you can choose a non-empty suffix of \(a\) and increase all of its elements by \(i\). How can we perform the operations to minimize the number of inversions in the final array?Note that you can perform operations on the same suffix any number of times you want.A permutation of size \(n\) is an array of size \(n\) such that each integer from \(1\) to \(n\) occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array \(a\) is a pair of indices \((i, j)\) such that \(i > j\) and \(a_{i} < a_{j}\).
|
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 size of the array.The second line contains \(n\) distinct integers \(a_{1}, a_{2}, \dots, a_{n}\) (\(1 \le a_i \le n\)), the initial permutation \(a\).It's guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print \(n\) integers \(x_{1}, x_{2}, \ldots, x_{n}\) (\(1 \le x_{i} \le n\) for each \(1 \le i \le n\)) indicating that the \(i\)-th operation must be applied to the suffix starting at index \(x_{i}\). If there are multiple answers, print any of them.
|
In the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index \(1\)). The final array \([11, 12, 13, 14]\) contains \(0\) inversions.In the second test case, \(a\) will be equal to \([2, 4, 3, 5, 6]\), \([2, 4, 3, 7, 8]\), \([2, 4, 6, 10, 11]\), \([2, 8, 10, 14, 15]\) and \([7, 13, 15, 19, 20]\) after the first, second, third, fourth, and fifth operations, respectively. So the final array \(a\) has zero inversions.
|
Input: 441 2 3 451 3 2 4 532 3 111 | Output: 1 1 1 1 1 4 3 2 1 1 3 3 1
|
Easy
| 4 | 717 | 497 | 271 | 17 |
1,011 |
B
|
1011B
|
B. Planning The Expedition
| 1,200 |
binary search; brute force; implementation
|
Natasha is planning an expedition to Mars for \(n\) people. One of the important tasks is to provide food for each participant.The warehouse has \(m\) daily food packages. Each package has some food type \(a_i\).Each participant must eat exactly one food package each day. Due to extreme loads, each participant must eat the same food type throughout the expedition. Different participants may eat different (or the same) types of food.Formally, for each participant \(j\) Natasha should select his food type \(b_j\) and each day \(j\)-th participant will eat one food package of type \(b_j\). The values \(b_j\) for different participants may be different.What is the maximum possible number of days the expedition can last, following the requirements above?
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(1 \le m \le 100\)) β the number of the expedition participants and the number of the daily food packages available.The second line contains sequence of integers \(a_1, a_2, \dots, a_m\) (\(1 \le a_i \le 100\)), where \(a_i\) is the type of \(i\)-th food package.
|
Print the single integer β the number of days the expedition can last. If it is not possible to plan the expedition for even one day, print 0.
|
In the first example, Natasha can assign type \(1\) food to the first participant, the same type \(1\) to the second, type \(5\) to the third and type \(2\) to the fourth. In this case, the expedition can last for \(2\) days, since each participant can get two food packages of his food type (there will be used \(4\) packages of type \(1\), two packages of type \(2\) and two packages of type \(5\)).In the second example, there are \(100\) participants and only \(1\) food package. In this case, the expedition can't last even \(1\) day.
|
Input: 4 101 5 2 1 1 1 2 5 7 2 | Output: 2
|
Easy
| 3 | 759 | 339 | 142 | 10 |
709 |
B
|
709B
|
B. Checkpoints
| 1,500 |
greedy; implementation; sortings
|
Vasya takes part in the orienteering competition. There are n checkpoints located along the line at coordinates x1, x2, ..., xn. Vasya starts at the point with coordinate a. His goal is to visit at least n - 1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.
|
The first line of the input contains two integers n and a (1 β€ n β€ 100 000, - 1 000 000 β€ a β€ 1 000 000) β the number of checkpoints and Vasya's starting position respectively.The second line contains n integers x1, x2, ..., xn ( - 1 000 000 β€ xi β€ 1 000 000) β coordinates of the checkpoints.
|
Print one integer β the minimum distance Vasya has to travel in order to visit at least n - 1 checkpoint.
|
In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7.In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point - 10.
|
Input: 3 101 7 12 | Output: 7
|
Medium
| 3 | 490 | 293 | 105 | 7 |
1,244 |
D
|
1244D
|
D. Paint the Tree
| 1,800 |
brute force; constructive algorithms; dp; graphs; implementation; trees
|
You are given a tree consisting of \(n\) vertices. A tree is an undirected connected acyclic graph. Example of a tree. You have to paint each vertex into one of three colors. For each vertex, you know the cost of painting it in every color.You have to paint the vertices so that any path consisting of exactly three distinct vertices does not contain any vertices with equal colors. In other words, let's consider all triples \((x, y, z)\) such that \(x \neq y, y \neq z, x \neq z\), \(x\) is connected by an edge with \(y\), and \(y\) is connected by an edge with \(z\). The colours of \(x\), \(y\) and \(z\) should be pairwise distinct. Let's call a painting which meets this condition good.You have to calculate the minimum cost of a good painting and find one of the optimal paintings. If there is no good painting, report about it.
|
The first line contains one integer \(n\) \((3 \le n \le 100\,000)\) β the number of vertices.The second line contains a sequence of integers \(c_{1, 1}, c_{1, 2}, \dots, c_{1, n}\) \((1 \le c_{1, i} \le 10^{9})\), where \(c_{1, i}\) is the cost of painting the \(i\)-th vertex into the first color.The third line contains a sequence of integers \(c_{2, 1}, c_{2, 2}, \dots, c_{2, n}\) \((1 \le c_{2, i} \le 10^{9})\), where \(c_{2, i}\) is the cost of painting the \(i\)-th vertex into the second color.The fourth line contains a sequence of integers \(c_{3, 1}, c_{3, 2}, \dots, c_{3, n}\) \((1 \le c_{3, i} \le 10^{9})\), where \(c_{3, i}\) is the cost of painting the \(i\)-th vertex into the third color.Then \((n - 1)\) lines follow, each containing two integers \(u_j\) and \(v_j\) \((1 \le u_j, v_j \le n, u_j \neq v_j)\) β the numbers of vertices connected by the \(j\)-th undirected edge. It is guaranteed that these edges denote a tree.
|
If there is no good painting, print \(-1\).Otherwise, print the minimum cost of a good painting in the first line. In the second line print \(n\) integers \(b_1, b_2, \dots, b_n\) \((1 \le b_i \le 3)\), where the \(i\)-th integer should denote the color of the \(i\)-th vertex. If there are multiple good paintings with minimum cost, print any of them.
|
All vertices should be painted in different colors in the first example. The optimal way to do it is to paint the first vertex into color \(1\), the second vertex β into color \(3\), and the third vertex β into color \(2\). The cost of this painting is \(3 + 2 + 1 = 6\).
|
Input: 3 3 2 3 4 3 2 3 1 3 1 2 2 3 | Output: 6 1 3 2
|
Medium
| 6 | 836 | 947 | 352 | 12 |
238 |
B
|
238B
|
B. Boring Partition
| 1,800 |
constructive algorithms
|
This problem is the most boring one you've ever seen. Given a sequence of integers a1, a2, ..., an and a non-negative integer h, our goal is to partition the sequence into two subsequences (not necessarily consist of continuous elements). Each element of the original sequence should be contained in exactly one of the result subsequences. Note, that one of the result subsequences can be empty.Let's define function f(ai, aj) on pairs of distinct elements (that is i β j) in the original sequence. If ai and aj are in the same subsequence in the current partition then f(ai, aj) = ai + aj otherwise f(ai, aj) = ai + aj + h. Consider all possible values of the function f for some partition. We'll call the goodness of this partiotion the difference between the maximum value of function f and the minimum value of function f.Your task is to find a partition of the given sequence a that have the minimal possible goodness among all possible partitions.
|
The first line of input contains integers n and h (2 β€ n β€ 105, 0 β€ h β€ 108). In the second line there is a list of n space-separated integers representing a1, a2, ..., an (0 β€ ai β€ 108).
|
The first line of output should contain the required minimum goodness. The second line describes the optimal partition. You should print n whitespace-separated integers in the second line. The i-th integer is 1 if ai is in the first subsequence otherwise it should be 2.If there are several possible correct answers you are allowed to print any of them.
|
In the first sample the values of f are as follows: f(1, 2) = 1 + 2 + 2 = 5, f(1, 3) = 1 + 3 + 2 = 6 and f(2, 3) = 2 + 3 = 5. So the difference between maximum and minimum values of f is 1.In the second sample the value of h is large, so it's better for one of the sub-sequences to be empty.
|
Input: 3 21 2 3 | Output: 11 2 2
|
Medium
| 1 | 953 | 187 | 353 | 2 |
1,361 |
D
|
1361D
|
D. Johnny and James
| 2,900 |
greedy; implementation; math; trees
|
James Bond, Johnny's favorite secret agent, has a new mission. There are \(n\) enemy bases, each of them is described by its coordinates so that we can think about them as points in the Cartesian plane. The bases can communicate with each other, sending a signal, which is the ray directed from the chosen point to the origin or in the opposite direction. The exception is the central base, which lies at the origin and can send a signal in any direction. When some two bases want to communicate, there are two possible scenarios. If they lie on the same line with the origin, one of them can send a signal directly to the other one. Otherwise, the signal is sent from the first base to the central, and then the central sends it to the second base. We denote the distance between two bases as the total Euclidean distance that a signal sent between them has to travel.Bond can damage all but some \(k\) bases, which he can choose arbitrarily. A damaged base can't send or receive the direct signal but still can pass it between two working bases. In particular, James can damage the central base, and the signal can still be sent between any two undamaged bases as before, so the distance between them remains the same. What is the maximal sum of the distances between all pairs of remaining bases that 007 can achieve by damaging exactly \(n - k\) of them?
|
The first line contains two integers \(n\) and \(k\) \((2 \leq k \leq n \leq 5 \cdot 10^5)\) β the total number of bases and number of bases that have to remain, respectively.Each of the next \(n\) lines contains two integers \(x\) and \(y\) \((-10^9 \leq x, y \leq 10^9)\), \(i\)-th line contains coordinates of the \(i\)-th base. You can assume that no two points coincide and that one of them is \((0, 0)\).
|
You should output one number β the maximal possible sum of distances between all pairs of some \(k\) from given bases. Your answer will be accepted if the absolute or relative error is less than \(10^{-6}\).
|
In the first example, in an optimal solution Bond doesn't destroy bases with indices \(4\) and \(6\) (marked in orange): The following picture represents an optimal solution for the second example. These bases are are not destroyed: \(2\), \(3\), \(4\), \(5\), \(6\) (marked in orange). An optimal solution for the third test is visible in the picture. Only bases \(3\), \(4\), \(5\) are destroyed. Again, the not destroyed bases are marked in orange.
|
Input: 6 2 0 0 1 1 2 2 3 3 0 1 0 2 | Output: 6.24264069
|
Master
| 4 | 1,358 | 410 | 207 | 13 |
1,205 |
E
|
1205E
|
E. Expected Value Again
| 3,100 |
combinatorics; strings
|
You are given integers \(n\), \(k\). Let's consider the alphabet consisting of \(k\) different elements.Let beauty \(f(s)\) of the string \(s\) be the number of indexes \(i\), \(1\le i<|s|\), for which prefix of \(s\) of length \(i\) equals to suffix of \(s\) of length \(i\). For example, beauty of the string \(abacaba\) equals \(2\), as for \(i = 1, 3\) prefix and suffix of length \(i\) are equal.Consider all words of length \(n\) in the given alphabet. Find the expected value of \(f(s)^2\) of a uniformly chosen at random word. We can show that it can be expressed as \(\frac{P}{Q}\), where \(P\) and \(Q\) are coprime and \(Q\) isn't divided by \(10^9 + 7\). Output \(P\cdot Q^{-1} \bmod 10^9 + 7\).
|
The first and the only line contains two integers \(n\), \(k\) (\(1\le n \le 10^5\), \(1\le k\le 10^9\)) β the length of a string and the size of alphabet respectively.
|
Output a single integer β \(P\times Q^{-1} \bmod 10^9 + 7\).
|
In the first example, there are \(9\) words of length \(2\) in alphabet of size \(3\) β \(aa\), \(ab\), \(ac\), \(ba\), \(bb\), \(bc\), \(ca\), \(cb\), \(cc\). \(3\) of them have beauty \(1\) and \(6\) of them have beauty \(0\), so the average value is \(\frac{1}{3}\).In the third example, there is only one such word, and it has beauty \(99\), so the average value is \(99^2\).
|
Input: 2 3 | Output: 333333336
|
Master
| 2 | 707 | 168 | 60 | 12 |
1,852 |
E
|
1852E
|
E. Rivalries
| 3,400 |
constructive algorithms; data structures; greedy
|
Ntarsis has an array \(a\) of length \(n\).The power of a subarray \(a_l \dots a_r\) (\(1 \leq l \leq r \leq n\)) is defined as: The largest value \(x\) such that \(a_l \dots a_r\) contains \(x\) and neither \(a_1 \dots a_{l-1}\) nor \(a_{r+1} \dots a_n\) contains \(x\). If no such \(x\) exists, the power is \(0\). Call an array \(b\) a rival to \(a\) if the following holds: The length of both \(a\) and \(b\) are equal to some \(n\). Over all \(l, r\) where \(1 \leq l \leq r \leq n\), the power of \(a_l \dots a_r\) equals the power of \(b_l \dots b_r\). The elements of \(b\) are positive. Ntarsis wants you to find a rival \(b\) to \(a\) such that the sum of \(b_i\) over \(1 \leq i \leq n\) is maximized. Help him with this task!
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)). The description of the test cases follows.The first line of each test case has a single integer \(n\) (\(1 \leq n \leq 10^5\)).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(n\) integers \(b_1, b_2, \ldots, b_n\) β a valid rival to \(a\) such that \(b_1 + b_2 + \cdots + b_n\) is maximal. If there exist multiple rivals with the maximum sum, output any of them.
|
For the first test case, one rival with the maximal sum is \([2, 4, 2, 3, 3]\).\([2, 4, 2, 3, 3]\) can be shown to be a rival to \([1, 4, 1, 3, 3]\).All possible subarrays of \(a\) and \(b\) and their corresponding powers are listed below: The power of \(a[1:1] = [1] = 0\), the power of \(b[1:1] = [2] = 0\). The power of \(a[1:2] = [1, 4] = 4\), the power of \(b[1:2] = [2, 4] = 4\). The power of \(a[1:3] = [1, 4, 1] = 4\), the power of \(b[1:3] = [2, 4, 2] = 4\). The power of \(a[1:4] = [1, 4, 1, 3] = 4\), the power of \(b[1:4] = [2, 4, 2, 3] = 4\). The power of \(a[1:5] = [1, 4, 1, 3, 3] = 4\), the power of \(b[1:5] = [2, 4, 2, 3, 3] = 4\). The power of \(a[2:2] = [4] = 4\), the power of \(b[2:2] = [4] = 4\). The power of \(a[2:3] = [4, 1] = 4\), the power of \(b[2:3] = [4, 2] = 4\). The power of \(a[2:4] = [4, 1, 3] = 4\), the power of \(b[2:4] = [4, 2, 3] = 4\). The power of \(a[2:5] = [4, 1, 3, 3] = 4\), the power of \(b[2:5] = [4, 2, 3, 3] = 4\). The power of \(a[3:3] = [1] = 0\), the power of \(b[3:3] = [2] = 0\). The power of \(a[3:4] = [1, 3] = 0\), the power of \(b[3:4] = [2, 3] = 0\). The power of \(a[3:5] = [1, 3, 3] = 3\), the power of \(b[3:5] = [2, 3, 3] = 3\). The power of \(a[4:4] = [3] = 0\), the power of \(b[4:4] = [3] = 0\). The power of \(a[4:5] = [3, 3] = 3\), the power of \(b[4:5] = [3, 3] = 3\). The power of \(a[5:5] = [3] = 0\), the power of \(b[5:5] = [3] = 0\). It can be shown there exists no rival with a greater sum than \(2 + 4 + 2 + 3 + 3 = 14\).
|
Input: 751 4 1 3 351 4 1 8 852 1 1 1 283 2 3 5 2 2 5 381 1 1 1 4 3 3 3101 9 5 9 8 1 5 8 9 1161 1 1 1 5 5 5 5 9 9 9 9 7 7 7 7 | Output: 2 4 2 3 3 3 4 3 8 8 2 1 2 1 2 4 2 4 5 5 2 5 4 1 2 2 1 4 3 2 3 7 9 5 9 8 9 5 8 9 7 1 8 8 1 5 8 8 5 9 9 9 9 7 8 8 7
|
Master
| 3 | 737 | 431 | 216 | 18 |
50 |
C
|
50C
|
C. Happy Farm 5
| 2,000 |
geometry
|
The Happy Farm 5 creators decided to invent the mechanism of cow grazing. The cows in the game are very slow and they move very slowly, it can even be considered that they stand still. However, carnivores should always be chased off them. For that a young player Vasya decided to make the shepherd run round the cows along one and the same closed path. It is very important that the cows stayed strictly inside the area limited by the path, as otherwise some cows will sooner or later be eaten. To be absolutely sure in the cows' safety, Vasya wants the path completion time to be minimum.The new game is launched for different devices, including mobile phones. That's why the developers decided to quit using the arithmetics with the floating decimal point and use only the arithmetics of integers. The cows and the shepherd in the game are represented as points on the plane with integer coordinates. The playing time is modeled by the turns. During every turn the shepherd can either stay where he stands or step in one of eight directions: horizontally, vertically, or diagonally. As the coordinates should always remain integer, then the length of a horizontal and vertical step is equal to 1, and the length of a diagonal step is equal to . The cows do not move. You have to minimize the number of moves the shepherd needs to run round the whole herd.
|
The first line contains an integer N which represents the number of cows in the herd (1 β€ N β€ 105). Each of the next N lines contains two integers Xi and Yi which represent the coordinates of one cow of (|Xi|, |Yi| β€ 106). Several cows can stand on one point.
|
Print the single number β the minimum number of moves in the sought path.
|
Picture for the example test: The coordinate grid is painted grey, the coordinates axes are painted black, the cows are painted red and the sought route is painted green.
|
Input: 41 15 15 31 3 | Output: 16
|
Hard
| 1 | 1,357 | 259 | 73 | 0 |
1,794 |
D
|
1794D
|
D. Counting Factorizations
| 1,900 |
combinatorics; divide and conquer; dp; math; number theory
|
The prime factorization of a positive integer \(m\) is the unique way to write it as \(\displaystyle m=p_1^{e_1}\cdot p_2^{e_2}\cdot \ldots \cdot p_k^{e_k}\), where \(p_1, p_2, \ldots, p_k\) are prime numbers, \(p_1 < p_2 < \ldots < p_k\) and \(e_1, e_2, \ldots, e_k\) are positive integers. For each positive integer \(m\), \(f(m)\) is defined as the multiset of all numbers in its prime factorization, that is \(f(m)=\{p_1,e_1,p_2,e_2,\ldots,p_k,e_k\}\). For example, \(f(24)=\{2,3,3,1\}\), \(f(5)=\{1,5\}\) and \(f(1)=\{\}\).You are given a list consisting of \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\). Count how many positive integers \(m\) satisfy that \(f(m)=\{a_1, a_2, \ldots, a_{2n}\}\). Since this value may be large, print it modulo \(998\,244\,353\).
|
The first line contains one integer \(n\) (\(1\le n \le 2022\)).The second line contains \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) (\(1\le a_i\le 10^6\)) β the given list.
|
Print one integer, the number of positive integers \(m\) satisfying \(f(m)=\{a_1, a_2, \ldots, a_{2n}\}\) modulo \(998\,244\,353\).
|
In the first sample, the two values of \(m\) such that \(f(m)=\{1,2,3,3\}\) are \(m=24\) and \(m=54\). Their prime factorizations are \(24=2^3\cdot 3^1\) and \(54=2^1\cdot 3^3\).In the second sample, the five values of \(m\) such that \(f(m)=\{2,2,3,5\}\) are \(200, 225, 288, 500\) and \(972\).In the third sample, there is no value of \(m\) such that \(f(m)=\{1,4\}\). Neither \(1^4\) nor \(4^1\) are prime factorizations because \(1\) and \(4\) are not primes.
|
Input: 2 1 3 2 3 | Output: 2
|
Hard
| 5 | 765 | 174 | 131 | 17 |
446 |
D
|
446D
|
D. DZY Loves Games
| 2,800 |
math; matrices; probabilities
|
Today DZY begins to play an old game. In this game, he is in a big maze with n rooms connected by m corridors (each corridor allows to move in both directions). You can assume that all the rooms are connected with corridors directly or indirectly.DZY has got lost in the maze. Currently he is in the first room and has k lives. He will act like the follows: Firstly he will randomly pick one of the corridors going from his current room. Each outgoing corridor has the same probability to be picked. Then he will go through the corridor and then the process repeats. There are some rooms which have traps in them. The first room definitely has no trap, the n-th room definitely has a trap. Each time DZY enters one of these rooms, he will lost one life. Now, DZY knows that if he enters the n-th room with exactly 2 lives, firstly he will lost one live, but then he will open a bonus round. He wants to know the probability for him to open the bonus round. Please, help him.
|
The first line contains three integers n, m, k (2 β€ n β€ 500; 1 β€ m β€ 105; 2 β€ k β€ 109).The second line contains n integers, each of them is either 0 or 1. If the i-th number is 1, then the i-th room has a trap, otherwise it has not a trap. Please note, that the number of rooms with a trap is no more than 101. It is guaranteed that the first room has no trap, and the n-th room has a trap.Then m lines follows. Each of them contains two integers ui, vi (1 β€ ui, vi β€ n; ui β vi), meaning that current corridor connects two rooms ui and vi. It is guaranteed that the corridor system is connected.
|
Print the only real number β the probability for DZY to open the bonus round. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
|
Input: 5 5 30 0 1 0 11 22 33 44 51 2 | Output: 0.25000000
|
Master
| 3 | 974 | 596 | 172 | 4 |
|
1,851 |
B
|
1851B
|
B. Parity Sort
| 800 |
greedy; sortings; two pointers
|
You have an array of integers \(a\) of length \(n\). You can apply the following operation to the given array: Swap two elements \(a_i\) and \(a_j\) such that \(i \neq j\), \(a_i\) and \(a_j\) are either both even or both odd. Determine whether it is possible to sort the array in non-decreasing order by performing the operation any number of times (possibly zero).For example, let \(a\) = [\(7, 10, 1, 3, 2\)]. Then we can perform \(3\) operations to sort the array: Swap \(a_3 = 1\) and \(a_1 = 7\), since \(1\) and \(7\) are odd. We get \(a\) = [\(1, 10, 7, 3, 2\)]; Swap \(a_2 = 10\) and \(a_5 = 2\), since \(10\) and \(2\) are even. We get \(a\) = [\(1, 2, 7, 3, 10\)]; Swap \(a_4 = 3\) and \(a_3 = 7\), since \(3\) and \(7\) are odd. We get \(a\) = [\(1, 2, 3, 7, 10\)].
|
The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of array \(a\).The second line of each test case contains exactly \(n\) positive integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output on a separate line: YES if the array can be sorted by applying the operation to it some number of times; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).
|
The first test case is explained in the problem statement.
|
Input: 657 10 1 3 2411 9 3 5511 3 15 3 2610 7 8 1 2 311056 6 4 1 6 | Output: YES YES NO NO YES NO
|
Beginner
| 3 | 777 | 519 | 269 | 18 |
538 |
D
|
538D
|
D. Weird Chess
| 1,800 |
brute force; constructive algorithms; implementation
|
Igor has been into chess for a long time and now he is sick of the game by the ordinary rules. He is going to think of new rules of the game and become world famous.Igor's chessboard is a square of size n Γ n cells. Igor decided that simple rules guarantee success, that's why his game will have only one type of pieces. Besides, all pieces in his game are of the same color. The possible moves of a piece are described by a set of shift vectors. The next passage contains a formal description of available moves.Let the rows of the board be numbered from top to bottom and the columns be numbered from left to right from 1 to n. Let's assign to each square a pair of integers (x, y) β the number of the corresponding column and row. Each of the possible moves of the piece is defined by a pair of integers (dx, dy); using this move, the piece moves from the field (x, y) to the field (x + dx, y + dy). You can perform the move if the cell (x + dx, y + dy) is within the boundaries of the board and doesn't contain another piece. Pieces that stand on the cells other than (x, y) and (x + dx, y + dy) are not important when considering the possibility of making the given move (for example, like when a knight moves in usual chess).Igor offers you to find out what moves his chess piece can make. He placed several pieces on the board and for each unoccupied square he told you whether it is attacked by any present piece (i.e. whether some of the pieces on the field can move to that cell). Restore a possible set of shift vectors of the piece, or else determine that Igor has made a mistake and such situation is impossible for any set of shift vectors.
|
The first line contains a single integer n (1 β€ n β€ 50).The next n lines contain n characters each describing the position offered by Igor. The j-th character of the i-th string can have the following values: o β in this case the field (i, j) is occupied by a piece and the field may or may not be attacked by some other piece; x β in this case field (i, j) is attacked by some piece; . β in this case field (i, j) isn't attacked by any piece.It is guaranteed that there is at least one piece on the board.
|
If there is a valid set of moves, in the first line print a single word 'YES' (without the quotes). Next, print the description of the set of moves of a piece in the form of a (2n - 1) Γ (2n - 1) board, the center of the board has a piece and symbols 'x' mark cells that are attacked by it, in a format similar to the input. See examples of the output for a full understanding of the format. If there are several possible answers, print any of them.If a valid set of moves does not exist, print a single word 'NO'.
|
In the first sample test the piece is a usual chess rook, and in the second sample test the piece is a usual chess knight.
|
Input: 5oxxxxx...xx...xx...xxxxxo | Output: YES....x........x........x........x....xxxxoxxxx....x........x........x........x....
|
Medium
| 3 | 1,654 | 506 | 514 | 5 |
991 |
B
|
991B
|
B. Getting an A
| 900 |
greedy; sortings
|
Translator's note: in Russia's most widespread grading system, there are four grades: 5, 4, 3, 2, the higher the better, roughly corresponding to A, B, C and F respectively in American grading system.The term is coming to an end and students start thinking about their grades. Today, a professor told his students that the grades for his course would be given out automatically β he would calculate the simple average (arithmetic mean) of all grades given out for lab works this term and round to the nearest integer. The rounding would be done in favour of the student β \(4.5\) would be rounded up to \(5\) (as in example 3), but \(4.4\) would be rounded down to \(4\).This does not bode well for Vasya who didn't think those lab works would influence anything, so he may receive a grade worse than \(5\) (maybe even the dreaded \(2\)). However, the professor allowed him to redo some of his works of Vasya's choosing to increase his average grade. Vasya wants to redo as as few lab works as possible in order to get \(5\) for the course. Of course, Vasya will get \(5\) for the lab works he chooses to redo.Help Vasya β calculate the minimum amount of lab works Vasya has to redo.
|
The first line contains a single integer \(n\) β the number of Vasya's grades (\(1 \leq n \leq 100\)).The second line contains \(n\) integers from \(2\) to \(5\) β Vasya's grades for his lab works.
|
Output a single integer β the minimum amount of lab works that Vasya has to redo. It can be shown that Vasya can always redo enough lab works to get a \(5\).
|
In the first sample, it is enough to redo two lab works to make two \(4\)s into \(5\)s.In the second sample, Vasya's average is already \(4.75\) so he doesn't have to redo anything to get a \(5\).In the second sample Vasya has to redo one lab work to get rid of one of the \(3\)s, that will make the average exactly \(4.5\) so the final grade would be \(5\).
|
Input: 34 4 4 | Output: 2
|
Beginner
| 2 | 1,183 | 197 | 157 | 9 |
1,944 |
B
|
1944B
|
B. Equal XOR
| 1,100 |
bitmasks; constructive algorithms
|
You are given an array \(a\) of length \(2n\), consisting of each integer from \(1\) to \(n\) exactly twice.You are also given an integer \(k\) (\(1 \leq k \leq \lfloor \frac{n}{2} \rfloor \)).You need to find two arrays \(l\) and \(r\) each of length \(\mathbf{2k}\) such that: \(l\) is a subset\(^\dagger\) of \([a_1, a_2, \ldots a_n]\) \(r\) is a subset of \([a_{n+1}, a_{n+2}, \ldots a_{2n}]\) bitwise XOR of elements of \(l\) is equal to the bitwise XOR of elements of \(r\); in other words, \(l_1 \oplus l_2 \oplus \ldots \oplus l_{2k} = r_1 \oplus r_2 \oplus \ldots \oplus r_{2k}\) It can be proved that at least one pair of \(l\) and \(r\) always exists. If there are multiple solutions, you may output any one of them.\(^\dagger\) A sequence \(x\) is a subset of a sequence \(y\) if \(x\) can be obtained by deleting several (possibly none or all) elements of \(y\) and rearranging the elements in any order. For example, \([3,1,2,1]\), \([1, 2, 3]\), \([1, 1]\) and \([3, 2]\) are subsets of \([1, 1, 2, 3]\) but \([4]\) and \([2, 2]\) are not subsets of \([1, 1, 2, 3]\).
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 5000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains \(2\) integers \(n\) and \(k\) (\(2 \le n \le 5 \cdot 10^4\), \(1 \leq k \leq \lfloor \frac{n}{2} \rfloor \)).The second line contains \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) (\(1 \le a_i \le n\)). It is guaranteed that every integer from \(1\) to \(n\) occurs exactly twice in \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^4\).
|
For each test case, output two lines. On the first line of output, output \(2k\) integers \(l_1, l_2, \ldots, l_{2k}\).On the second line of output, output \(2k\) integers \(r_1, r_2, \ldots r_{2k}\).If there are multiple solutions, you may output any one of them.
|
In the first test case, we choose \(l=[2,1]\) and \(r=[2,1]\). \([2, 1]\) is a subset of \([a_1, a_2]\) and \([2, 1]\) is a subset of \([a_3, a_4]\), and \(2 \oplus 1 = 2 \oplus 1 = 3\).In the second test case, \(6 \oplus 4 = 1 \oplus 3 = 2\).
|
Input: 42 11 2 2 16 16 4 2 1 2 3 1 6 3 5 5 44 11 2 3 4 1 2 3 46 25 1 3 3 5 1 2 6 4 6 4 2 | Output: 2 1 2 1 6 4 1 3 1 2 1 2 5 1 3 3 6 4 2 4
|
Easy
| 2 | 1,082 | 605 | 264 | 19 |
584 |
A
|
584A
|
A. Olesya and Rodion
| 1,000 |
math
|
Olesya loves numbers consisting of n digits, and Rodion only likes numbers that are divisible by t. Find some number that satisfies both of them.Your task is: given the n and t print an integer strictly larger than zero consisting of n digits that is divisible by t. If such number doesn't exist, print - 1.
|
The single line contains two numbers, n and t (1 β€ n β€ 100, 2 β€ t β€ 10) β the length of the number and the number it should be divisible by.
|
Print one such positive number without leading zeroes, β the answer to the problem, or - 1, if such number doesn't exist. If there are multiple possible answers, you are allowed to print any of them.
|
Input: 3 2 | Output: 712
|
Beginner
| 1 | 307 | 140 | 199 | 5 |
|
439 |
A
|
439A
|
A. Devu, the Singer and Churu, the Joker
| 900 |
greedy; implementation
|
Devu is a renowned classical singer. He is invited to many big functions/festivals. Recently he was invited to ""All World Classical Singing Festival"". Other than Devu, comedian Churu was also invited.Devu has provided organizers a list of the songs and required time for singing them. He will sing n songs, ith song will take ti minutes exactly. The Comedian, Churu will crack jokes. All his jokes are of 5 minutes exactly.People have mainly come to listen Devu. But you know that he needs rest of 10 minutes after each song. On the other hand, Churu being a very active person, doesn't need any rest.You as one of the organizers should make an optimal sΡhedule for the event. For some reasons you must follow the conditions: The duration of the event must be no more than d minutes; Devu must complete all his songs; With satisfying the two previous conditions the number of jokes cracked by Churu should be as many as possible. If it is not possible to find a way to conduct all the songs of the Devu, output -1. Otherwise find out maximum number of jokes that Churu can crack in the grand event.
|
The first line contains two space separated integers n, d (1 β€ n β€ 100; 1 β€ d β€ 10000). The second line contains n space-separated integers: t1, t2, ..., tn (1 β€ ti β€ 100).
|
If there is no way to conduct all the songs of Devu, output -1. Otherwise output the maximum number of jokes that Churu can crack in the grand event.
|
Consider the first example. The duration of the event is 30 minutes. There could be maximum 5 jokes in the following way: First Churu cracks a joke in 5 minutes. Then Devu performs the first song for 2 minutes. Then Churu cracks 2 jokes in 10 minutes. Now Devu performs second song for 2 minutes. Then Churu cracks 2 jokes in 10 minutes. Now finally Devu will perform his last song in 1 minutes. Total time spent is 5 + 2 + 10 + 2 + 10 + 1 = 30 minutes.Consider the second example. There is no way of organizing Devu's all songs. Hence the answer is -1.
|
Input: 3 302 2 1 | Output: 5
|
Beginner
| 2 | 1,100 | 172 | 149 | 4 |
144 |
A
|
144A
|
A. Arrival of the General
| 800 |
implementation
|
A Ministry for Defense sent a general to inspect the Super Secret Military Squad under the command of the Colonel SuperDuper. Having learned the news, the colonel ordered to all n squad soldiers to line up on the parade ground.By the military charter the soldiers should stand in the order of non-increasing of their height. But as there's virtually no time to do that, the soldiers lined up in the arbitrary order. However, the general is rather short-sighted and he thinks that the soldiers lined up correctly if the first soldier in the line has the maximum height and the last soldier has the minimum height. Please note that the way other solders are positioned does not matter, including the case when there are several soldiers whose height is maximum or minimum. Only the heights of the first and the last soldier are important.For example, the general considers the sequence of heights (4, 3, 4, 2, 1, 1) correct and the sequence (4, 3, 1, 2, 2) wrong.Within one second the colonel can swap any two neighboring soldiers. Help him count the minimum time needed to form a line-up which the general will consider correct.
|
The first input line contains the only integer n (2 β€ n β€ 100) which represents the number of soldiers in the line. The second line contains integers a1, a2, ..., an (1 β€ ai β€ 100) the values of the soldiers' heights in the order of soldiers' heights' increasing in the order from the beginning of the line to its end. The numbers are space-separated. Numbers a1, a2, ..., an are not necessarily different.
|
Print the only integer β the minimum number of seconds the colonel will need to form a line-up the general will like.
|
In the first sample the colonel will need to swap the first and second soldier and then the third and fourth soldier. That will take 2 seconds. The resulting position of the soldiers is (44, 33, 22, 11).In the second sample the colonel may swap the soldiers in the following sequence: (10, 10, 58, 31, 63, 40, 76) (10, 58, 10, 31, 63, 40, 76) (10, 58, 10, 31, 63, 76, 40) (10, 58, 10, 31, 76, 63, 40) (10, 58, 31, 10, 76, 63, 40) (10, 58, 31, 76, 10, 63, 40) (10, 58, 31, 76, 63, 10, 40) (10, 58, 76, 31, 63, 10, 40) (10, 76, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 10, 40) (76, 10, 58, 31, 63, 40, 10)
|
Input: 433 44 11 22 | Output: 2
|
Beginner
| 1 | 1,127 | 406 | 117 | 1 |
1,078 |
E
|
1078E
|
E. Negative Time Summation
| 3,400 |
constructive algorithms
|
Everyone knows that computers become faster and faster. Recently Berland scientists have built a machine that can move itself back in time!More specifically, it works as follows. It has an infinite grid and a robot which stands on one of the cells. Each cell of the grid can either be empty or contain 0 or 1. The machine also has a program which consists of instructions, which are being handled one by one. Each instruction is represented by exactly one symbol (letter or digit) and takes exactly one unit of time (say, second) to be performed, except the last type of operation (it's described below). Here they are: 0 or 1: the robot places this number into the cell he is currently at. If this cell wasn't empty before the operation, its previous number is replaced anyway. e: the robot erases the number into the cell he is at. l, r, u or d: the robot goes one cell to the left/right/up/down. s: the robot stays where he is for a unit of time. t: let \(x\) be \(0\), if the cell with the robot is empty, otherwise let \(x\) be one more than the digit in this cell (that is, \(x = 1\) if the digit in this cell is \(0\), and \(x = 2\) if the digit is \(1\)). Then the machine travels \(x\) seconds back in time. Note that this doesn't change the instructions order, but it changes the position of the robot and the numbers in the grid as they were \(x\) units of time ago. You can consider this instruction to be equivalent to a Ctrl-Z pressed \(x\) times. For example, let the board be completely empty, and the program be sr1t0. Let the robot initially be at \((0, 0)\). [now is the moment \(0\), the command is s]: we do nothing. [now is the moment \(1\), the command is r]: we are now at \((1, 0)\). [now is the moment \(2\), the command is 1]: we are at \((1, 0)\), and this cell contains \(1\). [now is the moment \(3\), the command is t]: we travel \(1 + 1 = 2\) moments back, that is, to the moment \(1\). [now is the moment \(1\), the command is 0]: we are again at \((0, 0)\), and the board is clear again, but after we follow this instruction, this cell has \(0\) in it. We've just rewritten the history. The consequences of the third instruction have never happened. Now Berland scientists want to use their machine in practice. For example, they want to be able to add two integers.Assume that the initial state of the machine is as follows: One positive integer is written in binary on the grid in such a way that its right bit is at the cell \((0, 1)\), from left to right from the highest bit to the lowest bit. The other positive integer is written in binary on the grid in such a way that its right bit is at the cell \((0, 0)\), from left to right from the highest bit to the lowest bit. All the other cells are empty. The robot is at \((0, 0)\). We consider this state to be always in the past; that is, if you manage to travel to any negative moment, the board was always as described above, and the robot was at \((0, 0)\) for eternity. You are asked to write a program after which The robot stands on a non-empty cell, If we read the number starting from the cell with the robot and moving to the right until the first empty cell, this will be \(a + b\) in binary, from the highest bit to the lowest bit. Note that there are no restrictions on other cells. In particular, there may be a digit just to the left to the robot after all instructions.In each test you are given up to \(1000\) pairs \((a, b)\), and your program must work for all these pairs. Also since the machine's memory is not very big, your program must consist of no more than \(10^5\) instructions.
|
The first line contains the only integer \(t\) (\(1\le t\le 1000\)) standing for the number of testcases. Each of the next \(t\) lines consists of two positive integers \(a\) and \(b\) (\(1\le a, b < 2^{30}\)) in decimal.
|
Output the only line consisting of no more than \(10^5\) symbols from 01eslrudt standing for your program.Note that formally you may output different programs for different tests.
|
Input: 2123456789 987654321555555555 555555555 | Output: 0l1l1l0l0l0l1l1l1l0l1l0l1l1l0l0l0l1l0l1l1l1l0l0l0l1l0l0l0l0l1l0lr
|
Master
| 1 | 3,595 | 221 | 179 | 10 |
|
1,550 |
C
|
1550C
|
C. Manhattan Subarrays
| 1,700 |
brute force; geometry; greedy; implementation
|
Suppose you have two points \(p = (x_p, y_p)\) and \(q = (x_q, y_q)\). Let's denote the Manhattan distance between them as \(d(p, q) = |x_p - x_q| + |y_p - y_q|\).Let's say that three points \(p\), \(q\), \(r\) form a bad triple if \(d(p, r) = d(p, q) + d(q, r)\).Let's say that an array \(b_1, b_2, \dots, b_m\) is good if it is impossible to choose three distinct indices \(i\), \(j\), \(k\) such that the points \((b_i, i)\), \((b_j, j)\) and \((b_k, k)\) form a bad triple.You are given an array \(a_1, a_2, \dots, a_n\). Calculate the number of good subarrays of \(a\). A subarray of the array \(a\) is the array \(a_l, a_{l + 1}, \dots, a_r\) for some \(1 \le l \le r \le n\).Note that, according to the definition, subarrays of length \(1\) and \(2\) are good.
|
The first line contains one integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).It's guaranteed that the sum of \(n\) doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print the number of good subarrays of array \(a\).
|
In the first test case, it can be proven that any subarray of \(a\) is good. For example, subarray \([a_2, a_3, a_4]\) is good since it contains only three elements and: \(d((a_2, 2), (a_4, 4)) = |4 - 3| + |2 - 4| = 3\) \(<\) \(d((a_2, 2), (a_3, 3)) + d((a_3, 3), (a_4, 4)) = 3 + 1 + 2 + 1 = 7\); \(d((a_2, 2), (a_3, 3))\) \(<\) \(d((a_2, 2), (a_4, 4)) + d((a_4, 4), (a_3, 3))\); \(d((a_3, 3), (a_4, 4))\) \(<\) \(d((a_3, 3), (a_2, 2)) + d((a_2, 2), (a_4, 4))\); In the second test case, for example, subarray \([a_1, a_2, a_3, a_4]\) is not good, since it contains a bad triple \((a_1, 1)\), \((a_2, 2)\), \((a_4, 4)\): \(d((a_1, 1), (a_4, 4)) = |6 - 9| + |1 - 4| = 6\); \(d((a_1, 1), (a_2, 2)) = |6 - 9| + |1 - 2| = 4\); \(d((a_2, 2), (a_4, 4)) = |9 - 9| + |2 - 4| = 2\); So, \(d((a_1, 1), (a_4, 4)) = d((a_1, 1), (a_2, 2)) + d((a_2, 2), (a_4, 4))\).
|
Input: 3 4 2 4 1 3 5 6 9 1 9 6 2 13 37 | Output: 10 12 3
|
Medium
| 4 | 767 | 389 | 70 | 15 |
1,088 |
C
|
1088C
|
C. Ehab and a 2-operation task
| 1,400 |
constructive algorithms; greedy; math
|
You're given an array \(a\) of length \(n\). You can perform the following operations on it: choose an index \(i\) \((1 \le i \le n)\), an integer \(x\) \((0 \le x \le 10^6)\), and replace \(a_j\) with \(a_j+x\) for all \((1 \le j \le i)\), which means add \(x\) to all the elements in the prefix ending at \(i\). choose an index \(i\) \((1 \le i \le n)\), an integer \(x\) \((1 \le x \le 10^6)\), and replace \(a_j\) with \(a_j \% x\) for all \((1 \le j \le i)\), which means replace every element in the prefix ending at \(i\) with the remainder after dividing it by \(x\). Can you make the array strictly increasing in no more than \(n+1\) operations?
|
The first line contains an integer \(n\) \((1 \le n \le 2000)\), the number of elements in the array \(a\).The second line contains \(n\) space-separated integers \(a_1\), \(a_2\), \(\dots\), \(a_n\) \((0 \le a_i \le 10^5)\), the elements of the array \(a\).
|
On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations.To print an adding operation, use the format ""\(1\) \(i\) \(x\)""; to print a modding operation, use the format ""\(2\) \(i\) \(x\)"". If \(i\) or \(x\) don't satisfy the limitations above, or you use more than \(n+1\) operations, you'll get wrong answer verdict.
|
In the first sample, the array is already increasing so we don't need any operations.In the second sample:In the first step: the array becomes \([8,6,3]\).In the second step: the array becomes \([0,2,3]\).
|
Input: 31 2 3 | Output: 0
|
Easy
| 3 | 654 | 258 | 386 | 10 |
2,038 |
I
|
2038I
|
I. Polyathlon
| 2,500 |
binary search; data structures; hashing; string suffix structures; strings
|
Berland is this year's host country of the International Collegiate Polyathlon Competition! Similar to biathlon being a competition of two sports, polyathlon is a competition of many sports. This year, there are \(m\) sports. Also, there are \(n\) participants in the event. The sports are numbered from \(1\) to \(m\), and the participants are numbered from \(1\) to \(n\).Some participants are skilled in multiple sports. You are given a binary matrix \(n \times m\) such that the \(j\)-th character of the \(i\)-th row is 1 if the \(i\)-th participant is skilled in the \(j\)-th sport, and 0, otherwise. It's also known that, for each pair of participants, there exists at least one sport such that one of them is skilled in that sport and the other one isn't.The order of sports in the competition is determined at the opening ceremony. Historically, it's done by the almighty Random Number Generator. A random number \(x\) from \(1\) to \(m\) is rolled. The competition then starts with the sport \(x\), then the sport \((x \bmod m + 1)\) is played, then the sport \(((x + 1) \bmod m + 1)\), and so on.Each sport is played as follows. If all remaining participants (all participants which are not eliminated yet) are not skilled in that sport, everyone goes through to the next sport. Otherwise, all skilled participants go through to the next sport, and all unskilled participants are eliminated from the competition. Once there is a single participant remaining in the competition, the competition ends, and that participant is declared the winner.As an organizer of the competition, you are curious of the possible outcomes of the competition beforehand (not that you are going to rig the random roll, how could you possibly think that...). For each sport \(x\), print the index of the winner if the competition starts with the sport \(x\).
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n, m \le 10^6\); \(n \le 2^m\); \(nm \le 2 \cdot 10^6\)) β the number of participants and the number of sports, respectively.The \(i\)-th of the next \(n\) lines contains a binary string consisting of exactly \(m\) characters 0 or 1 β the skillset of the \(i\)-th participant. If the \(j\)-th character is 1, the \(i\)-th participant is skilled in the \(j\)-th sport. If it's 0, the \(i\)-th participant is not skilled in the \(j\)-th sport.Additional constraint on the input: for each pair of participants, there exists at least one sport such that one of them is skilled in that sport and the other one isn't. In other words, all \(n\) binary strings are pairwise distinct.
|
Print \(m\) integers. For each \(x\) from \(1\) to \(m\), print the index of the winner if the competition starts with the sport \(x\).
|
Input: 3 5100100110010101 | Output: 3 2 3 1 3
|
Expert
| 5 | 1,848 | 737 | 135 | 20 |
|
1,165 |
E
|
1165E
|
E. Two Arrays and Sum of Functions
| 1,600 |
greedy; math; sortings
|
You are given two arrays \(a\) and \(b\), both of length \(n\).Let's define a function \(f(l, r) = \sum\limits_{l \le i \le r} a_i \cdot b_i\).Your task is to reorder the elements (choose an arbitrary order of elements) of the array \(b\) to minimize the value of \(\sum\limits_{1 \le l \le r \le n} f(l, r)\). Since the answer can be very large, you have to print it modulo \(998244353\). Note that you should minimize the answer but not its remainder.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\) and \(b\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)), where \(a_i\) is the \(i\)-th element of \(a\).The third line of the input contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_j \le 10^6\)), where \(b_j\) is the \(j\)-th element of \(b\).
|
Print one integer β the minimum possible value of \(\sum\limits_{1 \le l \le r \le n} f(l, r)\) after rearranging elements of \(b\), taken modulo \(998244353\). Note that you should minimize the answer but not its remainder.
|
Input: 5 1 8 7 2 4 9 7 2 9 3 | Output: 646
|
Medium
| 3 | 453 | 431 | 224 | 11 |
|
2,124 |
A
|
2124A
|
A. Deranged Deletions
| 800 |
greedy; sortings
|
Call an array \(b\) of length \(m\) a derangement if the following property holds: Let \(c\) be an array of length \(m\) such that \(c_i = b_i\) over all \(1 \leq i \leq m\). Sort \(c\) in non-decreasing order. If \(b_i\neq c_i\) over all \(1\leq i \leq m\), then \(b\) is a derangement. For example, If \(b = [4,8,3,1]\), then \(c = [1, 3, 4, 8]\) after getting sorted. Since \(b_i \neq c_i\) in all positions, \(b\) is a derangement. If \(b = [3,2,1]\), then \(c = [1, 2, 3]\) after getting sorted. Since \(b_2 = c_2\), \(b\) is not a derangement. You are given an array \(a\) of length \(n\). In one operation, you can delete an element from \(a\). The order of the remaining elements is preserved after each deletion.Output whether it is possible to perform some (possibly none) operations such that the remaining elements form a derangement. If it is possible, output any possible remaining array. The remaining array must be non-empty.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 100\)) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)) β denoting the array \(a\).
|
For each test case, on a new line, if it is possible to perform operations such that the remaining array is a derangement, output YES. Otherwise, output NO.You can output in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.If your response was positive, output two more lines in the following format: The first line should contain an integer \(k\) (\(1 \leq k \leq n\)), the number of elements that remain in the array. The second line should contain \(d_1, d_2 \ldots, d_k\), the elements that remain in the array. It must be possible to acquire array \(d\) after performing some operations on \(a\). Array \(d\) must be a derangement.
|
In the second test case, we can delete one \(5\) from the array so that it becomes \([4,5,2,4]\). It can be shown this array is a derangement. This is not the only solution β it can be shown that the original array \([4,5,5,2,4]\) is another valid solution.
|
Input: 332 2 354 5 5 2 411 | Output: NO YES 4 4 5 2 4 NO
|
Beginner
| 2 | 941 | 407 | 719 | 21 |
2,005 |
D
|
2005D
|
D. Alter the GCD
| 2,400 |
binary search; brute force; data structures; divide and conquer; implementation; number theory
|
You are given two arrays \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\).You must perform the following operation exactly once: choose any indices \(l\) and \(r\) such that \(1 \le l \le r \le n\); swap \(a_i\) and \(b_i\) for all \(i\) such that \(l \leq i \leq r\). Find the maximum possible value of \(\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)\) after performing the operation exactly once. Also find the number of distinct pairs \((l, r)\) which achieve the maximum value.
|
In the first line of the input, you are given a single integer \(t\) (\(1 \le t \le 10^5\)), the number of test cases. Then the description of each test case follows.In the first line of each test case, you are given a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)), representing the number of integers in each array.In the next line, you are given \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array \(a\).In the last line, you are given \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 10^9\)) β the elements of the array \(b\).The sum of values of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, output a line with two integers: the maximum value of \(\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)\) after performing the operation exactly once, and the number of ways.
|
In the first, third, and fourth test cases, there's no way to achieve a higher GCD than \(1\) in any of the arrays, so the answer is \(1 + 1 = 2\). Any pair \((l, r)\) achieves the same result; for example, in the first test case there are \(36\) such pairs.In the last test case, you must choose \(l = 1\), \(r = 2\) to maximize the answer. In this case, the GCD of the first array is \(5\), and the GCD of the second array is \(1\), so the answer is \(5 + 1 = 6\), and the number of ways is \(1\).
|
Input: 5811 4 16 17 3 24 25 88 10 4 21 17 18 25 2146 4 24 1315 3 1 14213 145 8820 17 15 11 21 10 3 79 9 4 20 14 9 13 1218 1315 20 | Output: 2 36 3 2 2 3 2 36 6 1
|
Expert
| 6 | 512 | 666 | 216 | 20 |
627 |
B
|
627B
|
B. Factory Repairs
| 1,700 |
data structures
|
A factory produces thimbles in bulk. Typically, it can produce up to a thimbles a day. However, some of the machinery is defective, so it can currently only produce b thimbles each day. The factory intends to choose a k-day period to do maintenance and construction; it cannot produce any thimbles during this time, but will be restored to its full production of a thimbles per day after the k days are complete.Initially, no orders are pending. The factory receives updates of the form di, ai, indicating that ai new orders have been placed for the di-th day. Each order requires a single thimble to be produced on precisely the specified day. The factory may opt to fill as many or as few of the orders in a single batch as it likes.As orders come in, the factory owner would like to know the maximum number of orders he will be able to fill if he starts repairs on a given day pi. Help the owner answer his questions.
|
The first line contains five integers n, k, a, b, and q (1 β€ k β€ n β€ 200 000, 1 β€ b < a β€ 10 000, 1 β€ q β€ 200 000) β the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively.The next q lines contain the descriptions of the queries. Each query is of one of the following two forms: 1 di ai (1 β€ di β€ n, 1 β€ ai β€ 10 000), representing an update of ai orders on day di, or 2 pi (1 β€ pi β€ n - k + 1), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day pi? It's guaranteed that the input will contain at least one query of the second type.
|
For each query of the second type, print a line containing a single integer β the maximum number of orders that the factory can fill over all n days.
|
Consider the first sample.We produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days.For the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that day, and 2 orders for day 5 since we are limited to our production capacity, for a total of 3 orders filled.For the third question, we are able to fill 1 order on day 1, 1 order on day 2, and 2 orders on day 5, for a total of 4 orders.
|
Input: 5 2 2 1 81 1 21 5 31 2 12 21 4 21 3 22 12 3 | Output: 364
|
Medium
| 1 | 920 | 676 | 149 | 6 |
575 |
F
|
575F
|
F. Bulbo
| 2,100 |
dp; greedy
|
Bananistan is a beautiful banana republic. Beautiful women in beautiful dresses. Beautiful statues of beautiful warlords. Beautiful stars in beautiful nights.In Bananistan people play this crazy game β Bulbo. Thereβs an array of bulbs and player at the position, which represents one of the bulbs. The distance between two neighboring bulbs is 1. Before each turn player can change his position with cost |posnew - posold|. After that, a contiguous set of bulbs lights-up and player pays the cost thatβs equal to the distance to the closest shining bulb. Then, all bulbs go dark again. The goal is to minimize your summed cost. I tell you, Bananistanians are spending their nights playing with bulbs.Banana day is approaching, and you are hired to play the most beautiful Bulbo game ever. A huge array of bulbs is installed, and you know your initial position and all the light-ups in advance. You need to play the ideal game and impress Bananistanians, and their families.
|
The first line contains number of turns n and initial position x. Next n lines contain two numbers lstart and lend, which represent that all bulbs from interval [lstart, lend] are shining this turn. 1 β€ n β€ 5000 1 β€ x β€ 109 1 β€ lstart β€ lend β€ 109
|
Output should contain a single number which represents the best result (minimum cost) that could be obtained by playing this Bulbo game.
|
Before 1. turn move to position 5Before 2. turn move to position 9Before 5. turn move to position 8
|
Input: 5 42 79 168 109 171 6 | Output: 8
|
Hard
| 2 | 973 | 247 | 136 | 5 |
2,113 |
B
|
2113B
|
B. Good Start
| 1,200 |
constructive algorithms; math
|
The roof is a rectangle of size \(w \times h\) with the bottom left corner at the point \((0, 0)\) on the plane. Your team needs to completely cover this roof with identical roofing sheets of size \(a \times b\), with the following conditions: The sheets cannot be rotated (not even by \(90^\circ\)). The sheets must not overlap (but they can touch at the edges). The sheets can extend beyond the boundaries of the rectangular roof. A novice from your team has already placed two such sheets on the roof in such a way that the sheets do not overlap and each of them partially covers the roof.Your task is to determine whether it is possible to completely tile the roof without removing either of the two already placed sheets.
|
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 four integers \(w\), \(h\), \(a\), and \(b\) (\(1 \le w, h, a, b \le 10^9\)) β the dimensions of the roof and the dimensions of the roofing sheets, respectively.The second line of each test case contains four integers \(x_1\), \(y_1\), \(x_2\), and \(y_2\) (\(-a + 1 \le x_1, x_2 \le w - 1, -b + 1 \le y_1, y_2 \le h - 1\)) β the coordinates of the bottom left corners of the already placed roofing sheets. It is guaranteed that these sheets do not overlap.
|
For each test case, output ""Yes"" (without quotes) if it is possible to completely tile the roof without removing either of the two already placed tiles, and ""No"" (without quotes) otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, it is possible to add \(8\) roofing sheets as follows: In the second test case, it is impossible to completely tile the roof:
|
Input: 76 5 2 3-1 -2 5 44 4 2 20 0 3 110 9 3 20 0 4 310 9 3 20 0 6 35 5 2 2-1 -1 4 -15 5 2 2-1 -1 2 37 8 2 40 0 0 5 | Output: Yes No No Yes No Yes No
|
Easy
| 2 | 726 | 661 | 354 | 21 |
1,131 |
B
|
1131B
|
B. Draw!
| 1,400 |
greedy; implementation
|
You still have partial information about the score during the historic football match. You are given a set of pairs \((a_i, b_i)\), indicating that at some point during the match the score was ""\(a_i\): \(b_i\)"". It is known that if the current score is Β«\(x\):\(y\)Β», then after the goal it will change to ""\(x+1\):\(y\)"" or ""\(x\):\(y+1\)"". What is the largest number of times a draw could appear on the scoreboard?The pairs ""\(a_i\):\(b_i\)"" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.
|
The first line contains a single integer \(n\) (\(1 \le n \le 10000\)) β the number of known moments in the match.Each of the next \(n\) lines contains integers \(a_i\) and \(b_i\) (\(0 \le a_i, b_i \le 10^9\)), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).All moments are given in chronological order, that is, sequences \(x_i\) and \(y_j\) are non-decreasing. The last score denotes the final result of the match.
|
Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.
|
In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4.
|
Input: 32 03 13 4 | Output: 2
|
Easy
| 2 | 609 | 509 | 148 | 11 |
461 |
E
|
461E
|
E. Appleman and a Game
| 3,000 |
binary search; shortest paths; strings
|
Appleman and Toastman like games. Today they play a game with strings with the following rules. Firstly Toastman tells Appleman two strings s and t both consisting only of letters 'A', 'B', 'C', 'D'. Then Appleman must build string s as quickly as possible. Initially he has empty string, and in one second he can append to end of the current string any contiguous substring of t.Now, Toastman and Appleman are beginning to play the game. Toastman has already told string t to Appleman, but he hasn't come up with string s yet. Toastman only thinks, that he should choose string s consisting of n characters. Of course, he wants to find the worst string for Appleman (such string, that Appleman will spend as much time as possible during the game). Tell Toastman, how much time will Appleman spend during the game if Toastman finds the worst string for him. You can assume that Appleman plays optimally, therefore he builds any string s in minimal possible time.
|
The first line contains an integer n (1 β€ n β€ 1018). The second line contains string t (1 β€ |t| β€ 105). String t consists of only letters 'A', 'B', 'C', 'D'. Each letter appears at least once in string t.
|
Print a single integer β the largest possible time Appleman needs.
|
In the first example, Toastman can choose s equal to ""AAAAA"".In the second example, Toastman can choose s equal to ""DADDA"".
|
Input: 5ABCCAD | Output: 5
|
Master
| 3 | 962 | 204 | 66 | 4 |
1,771 |
F
|
1771F
|
F. Hossam and Range Minimum Query
| 2,500 |
binary search; bitmasks; data structures; hashing; probabilities; strings; trees
|
Hossam gives you a sequence of integers \(a_1, \, a_2, \, \dots, \, a_n\) of length \(n\). Moreover, he will give you \(q\) queries of type \((l, \, r)\). For each query, consider the elements \(a_l, \, a_{l + 1}, \, \dots, \, a_r\). Hossam wants to know the smallest number in this sequence, such that it occurs in this sequence an odd number of times.You need to compute the answer for each query before process the next query.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)), the length of the sequence.The second line contains \(n\) integers \(a_1, \, a_2, \, \dots, \, a_n\) (\(1 \le a_i \le 10^9\)).The third line contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)), the number of queries.Each of the next \(q\) lines contains two integers \(a\) and \(b\) (\(0 \le a, \, b \le 2 \cdot 10^9\)), the numbers used to encode the queries. Let \(\mathrm{ans}_i\) be the answer on the \(i\)-th query, and \(\mathrm{ans}_0\) be zero. Then $$$\(l_i = a_i \oplus \mathrm{ans}_{i - 1},\)\( \)\(r_i = b_i \oplus \mathrm{ans}_{i - 1},\)\( where \)l_i, \, r_i\( are parameters of the \)i\(-th query and \)\oplus\( means the bitwise exclusive or operation. It is guaranteed that \)1 \le l \le r \le n$$$.
|
For each query, print the smallest number that occurs an odd number of times on the given segment of the sequence.If there is no such number, print \(0\).
|
In the example,$$$\(l_1 = 1, \, r_1 = 2,\)\( \)\(l_2 = 1, \, r_2 = 3,\)\( \)\(l_3 = 2, \, r_3 = 4,\)\( \)\(l_4 = 1, \, r_4 = 4,\)\( \)\(l_5 = 2, \, r_5 = 2,\)\( \)\(l_6 = 1, \, r_6 = 5.\)$$$
|
Input: 5 1 2 1 2 2 6 1 2 0 2 0 6 0 5 2 2 3 7 | Output: 1 2 1 0 2 2
|
Expert
| 7 | 429 | 809 | 154 | 17 |
2,044 |
G2
|
2044G2
|
G2. Medium Demon Problem (hard version)
| 1,900 |
dfs and similar; dp; dsu; graphs; implementation; trees
|
This is the hard version of the problem. The key difference between the two versions is highlighted in bold.A group of \(n\) spiders has come together to exchange plushies. Initially, each spider has \(1\) plushie. Every year, if spider \(i\) has at least one plushie, he will give exactly one plushie to spider \(r_i\). Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, each spider is allowed to have more than 1 plushie at any point in time.The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year \(1\) can never be stable.Find the first year in which the process becomes stable.
|
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 an integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β the number of spiders.The following line contains \(n\) integers \(r_1, r_2, \ldots, r_n\) (\(1 \leq r_i \leq n, r_i \neq i\)) β the recipient of the plushie of each spider.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output an integer on a new line, the first year in which the process becomes stable.
|
For the second test case: At year \(1\), the following array shows the number of plushies each spider has: \([1, 1, 1, 1, 1]\). Then, year \(1\)'s exchange happens. At year \(2\), the following array shows the number of plushies each spider has: \([1, 1, 1, 1, 1]\). Since this array is the same as the previous year, this year is stable. For the third test case: At year \(1\), the following array shows the number of plushies each spider has: \([1, 1, 1, 1, 1]\). Then, year \(1\)'s exchange happens. At year \(2\), the following array shows the number of plushies each spider has: \([1, 2, 1, 1, 0]\). Then, year \(2\)'s exchange happens. At year \(3\), the following array shows the number of plushies each spider has: \([1, 3, 0, 1, 0]\). Then, year \(3\)'s exchange happens. At year \(4\), the following array shows the number of plushies each spider has: \([1, 4, 0, 0, 0]\). Then, year \(4\)'s exchange happens. At year \(5\), the following array shows the number of plushies each spider has: \([1, 4, 0, 0, 0]\). Since this array is the same as the previous year, this year is stable.
|
Input: 522 152 3 4 5 152 1 4 2 354 1 1 5 4104 3 9 1 6 7 9 10 10 3 | Output: 2 2 5 5 5
|
Hard
| 6 | 788 | 452 | 104 | 20 |
1,835 |
B
|
1835B
|
B. Lottery
| 2,500 |
binary search; brute force; greedy; math; two pointers
|
\(n\) people indexed with integers from \(1\) to \(n\) came to take part in a lottery. Each received a ticket with an integer from \(0\) to \(m\).In a lottery, one integer called target is drawn uniformly from \(0\) to \(m\). \(k\) tickets (or less, if there are not enough participants) with the closest numbers to the target are declared the winners. In case of a draw, a ticket belonging to the person with a smaller index is declared a winner.Bytek decided to take part in the lottery. He knows the values on the tickets of all previous participants. He can pick whatever value he wants on his ticket, but unfortunately, as he is the last one to receive it, he is indexed with an integer \(n + 1\). Bytek wants to win the lottery. Thus, he wants to know what he should pick to maximize the chance of winning. He wants to know the smallest integer in case there are many such integers. Your task is to find it and calculate his chance of winning.
|
In the first line of the input, there are the integers \(n\), \(m\), and \(k\) (\(1 \leq n \leq 10^6\), \(0 \leq m \leq 10^{18}\), \(1 \leq k \leq 10^6\)).In the following line, there are \(n\) integers separated by a single space, denoting the numbers on tickets received by people participating in a lottery. These numbers are integers in the range from \(0\) to \(m\).
|
You should output two integers separated by a single space on the standard output. The first should be equal to the number of target values (from \(0\) to \(m\)), upon drawing which Baytek wins, given that he chooses his ticket optimally. The second should be equal to the integer Bytek should pick to maximize his chance of winning the lottery.
|
In the first example, Bytek wins for \(4\) target values (namely \(0, 1, 2, 3\)) if he chooses integer \(2\), which is the lowest optimal value. If he chooses \(3\), he also wins in four cases, but it is not the lowest value.
|
Input: 3 6 2 1 4 5 | Output: 4 2
|
Expert
| 5 | 949 | 371 | 345 | 18 |
1,497 |
B
|
1497B
|
B. M-arrays
| 1,200 |
constructive algorithms; greedy; math
|
You are given an array \(a_1, a_2, \ldots, a_n\) consisting of \(n\) positive integers and a positive integer \(m\).You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.Let's call an array \(m\)-divisible if for each two adjacent numbers in the array (two numbers on the positions \(i\) and \(i+1\) are called adjacent for each \(i\)) their sum is divisible by \(m\). An array of one element is \(m\)-divisible.Find the smallest number of \(m\)-divisible arrays that \(a_1, a_2, \ldots, a_n\) is possible to divide into.
|
The first line contains a single integer \(t\) \((1 \le t \le 1000)\) β the number of test cases.The first line of each test case contains two integers \(n\), \(m\) \((1 \le n \le 10^5, 1 \le m \le 10^5)\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((1 \le a_i \le 10^9)\).It is guaranteed that the sum of \(n\) and the sum of \(m\) over all test cases do not exceed \(10^5\).
|
For each test case print the answer to the problem.
|
In the first test case we can divide the elements as follows: \([4, 8]\). It is a \(4\)-divisible array because \(4+8\) is divisible by \(4\). \([2, 6, 2]\). It is a \(4\)-divisible array because \(2+6\) and \(6+2\) are divisible by \(4\). \([9]\). It is a \(4\)-divisible array because it consists of one element.
|
Input: 4 6 4 2 2 8 6 9 4 10 8 1 1 1 5 2 4 4 8 6 7 1 1 666 2 2 2 4 | Output: 3 6 1 1
|
Easy
| 3 | 583 | 418 | 51 | 14 |
1,252 |
E
|
1252E
|
E. Songwriter
| 2,200 |
greedy; two pointers
|
Andi is a mathematician, a computer scientist, and a songwriter. After spending so much time writing songs, he finally writes a catchy melody that he thought as his best creation. However, the singer who will sing the song/melody has a unique vocal range, thus, an adjustment may be needed.A melody is defined as a sequence of \(N\) notes which are represented by integers. Let \(A\) be the original melody written by Andi. Andi needs to adjust \(A\) into a new melody \(B\) such that for every \(i\) where \(1 \le i < N\): If \(A_i < A_{i+1}\), then \(B_i < B_{i+1}\). If \(A_i = A_{i+1}\), then \(B_i = B_{i+1}\). If \(A_i > A_{i+1}\), then \(B_i > B_{i+1}\). \(|B_i - B_{i+1}| \le K\), i.e. the difference between two successive notes is no larger than \(K\). Moreover, the singer also requires that all notes are within her vocal range, i.e. \(L \le B_i \le R\) for all \(1 \le i \le N\).Help Andi to determine whether such \(B\) exists, and find the lexicographically smallest \(B\) if it exists. A melody \(X\) is lexicographically smaller than melody \(Y\) if and only if there exists \(j\) (\(1 \le j \le N\)) such that \(X_i = Y_i\) for all \(i < j\) and \(X_{j} < Y_{j}\).For example, consider a melody \(A = \{1,3,5,6,7,8,9,10,3,7,8,9,10,11,12,12\}\) as shown in the following figure. The diagonal arrow up in the figure implies that \(A_i < A_{i+1}\), the straight right arrow implies that \(A_i = A_{i+1}\), and the diagonal arrow down implies that \(A_i > A_{i+1}\). Supposed we want to make a new melody with \(L = 1\), \(R = 8\), and \(K = 6\). The new melody \(B = \{1,2,3,4,5,6,7,8,2,3,4,5,6,7,8,8\}\) as shown in the figure satisfies all the requirements, and it is the lexicographically smallest possible.
|
Input begins with a line containing four integers: \(N\) \(L\) \(R\) \(K\) (\(1 \le N \le 100\,000\); \(1 \le L \le R \le 10^9\); \(1 \le K \le 10^9\)) representing the number of notes in the melody, the vocal range (\(L\) and \(R\)), and the maximum difference between two successive notes in the new melody, respectively. The next line contains \(N\) integers: \(A_i\) (\(1 \le A_i \le 10^9\)) representing the original melody.
|
Output in a line \(N\) integers (each separated by a single space) representing the lexicographically smallest melody satisfying all the requirements, or output -1 if there is no melody satisfying all the requirements. Note that it might be possible that the lexicographically smallest melody which satisfies all the requirements to be the same as the original melody.
|
Explanation for the sample input/output #1This is the example from the problem description.
|
Input: 16 1 8 6 1 3 5 6 7 8 9 10 3 7 8 9 10 11 12 12 | Output: 1 2 3 4 5 6 7 8 2 3 4 5 6 7 8 8
|
Hard
| 2 | 1,725 | 429 | 368 | 12 |
630 |
A
|
630A
|
A. Again Twenty Five!
| 800 |
number theory
|
The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. ""Do I give such a hard task?"" β the HR manager thought. ""Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot find the power using a calculator, but we need people who are able to think, not just follow the instructions.""Could you pass the interview in the machine vision company in IT City?
|
The only line of the input contains a single integer n (2 β€ n β€ 2Β·1018) β the power in which you need to raise number 5.
|
Output the last two digits of 5n without spaces between them.
|
Input: 2 | Output: 25
|
Beginner
| 1 | 484 | 120 | 61 | 6 |
|
1,220 |
G
|
1220G
|
G. Geolocation
| 3,400 |
geometry
|
You are working for the Gryzzl company, headquartered in Pawnee, Indiana.The new national park has been opened near Pawnee recently and you are to implement a geolocation system, so people won't get lost. The concept you developed is innovative and minimalistic. There will be \(n\) antennas located somewhere in the park. When someone would like to know their current location, their Gryzzl hologram phone will communicate with antennas and obtain distances from a user's current location to all antennas.Knowing those distances and antennas locations it should be easy to recover a user's location... Right? Well, almost. The only issue is that there is no way to distinguish antennas, so you don't know, which distance corresponds to each antenna. Your task is to find a user's location given as little as all antennas location and an unordered multiset of distances.
|
The first line of input contains a single integer \(n\) (\(2 \leq n \leq 10^5\)) which is the number of antennas.The following \(n\) lines contain coordinates of antennas, \(i\)-th line contain two integers \(x_i\) and \(y_i\) (\(0 \leq x_i,y_i \leq 10^8\)). It is guaranteed that no two antennas coincide.The next line of input contains integer \(m\) (\(1 \leq n \cdot m \leq 10^5\)), which is the number of queries to determine the location of the user.Following \(m\) lines contain \(n\) integers \(0 \leq d_1 \leq d_2 \leq \dots \leq d_n \leq 2 \cdot 10^{16}\) each. These integers form a multiset of squared distances from unknown user's location \((x;y)\) to antennas.For all test cases except the examples it is guaranteed that all user's locations \((x;y)\) were chosen uniformly at random, independently from each other among all possible integer locations having \(0 \leq x, y \leq 10^8\).
|
For each query output \(k\), the number of possible a user's locations matching the given input and then output the list of these locations in lexicographic order.It is guaranteed that the sum of all \(k\) over all points does not exceed \(10^6\).
|
As you see in the second example, although initially a user's location is picked to have non-negative coordinates, you have to output all possible integer locations.
|
Input: 3 0 0 0 1 1 0 1 1 1 2 | Output: 1 1 1
|
Master
| 1 | 870 | 899 | 247 | 12 |
338 |
D
|
338D
|
D. GCD Table
| 2,900 |
chinese remainder theorem; math; number theory
|
Consider a table G of size n Γ m such that G(i, j) = GCD(i, j) for all 1 β€ i β€ n, 1 β€ j β€ m. GCD(a, b) is the greatest common divisor of numbers a and b.You have a sequence of positive integer numbers a1, a2, ..., ak. We say that this sequence occurs in table G if it coincides with consecutive elements in some row, starting from some position. More formally, such numbers 1 β€ i β€ n and 1 β€ j β€ m - k + 1 should exist that G(i, j + l - 1) = al for all 1 β€ l β€ k.Determine if the sequence a occurs in table G.
|
The first line contains three space-separated integers n, m and k (1 β€ n, m β€ 1012; 1 β€ k β€ 10000). The second line contains k space-separated integers a1, a2, ..., ak (1 β€ ai β€ 1012).
|
Print a single word ""YES"", if the given sequence occurs in table G, otherwise print ""NO"".
|
Sample 1. The tenth row of table G starts from sequence {1, 2, 1, 2, 5, 2, 1, 2, 1, 10}. As you can see, elements from fifth to ninth coincide with sequence a.Sample 2. This time the width of table G equals 8. Sequence a doesn't occur there.
|
Input: 100 100 55 2 1 2 1 | Output: YES
|
Master
| 3 | 509 | 184 | 93 | 3 |
1,857 |
A
|
1857A
|
A. Array Coloring
| 800 |
greedy; math
|
You are given an array consisting of \(n\) integers. Your task is to determine whether it is possible to color all its elements in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored.For example, if the array is [\(1,2,4,3,2,3,5,4\)], we can color it as follows: [\(\color{blue}{1},\color{blue}{2},\color{red}{4},\color{blue}{3},\color{red}{2},\color{red}{3},\color{red}{5},\color{red}{4}\)], where the sum of the blue elements is \(6\) and the sum of the red elements is \(18\).
|
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case begins with a line containing an integer \(n\) (\(2 \le n \le 50\)) β the length of the array \(a\).The next line contains \(n\) integers \(a_1,a_2, \dots, a_n\) (\(1 \le a_i \le 50\)) β the elements of the array \(a\).
|
For each test case, output ""YES"" (without quotes) if it is possible to color the array in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored, and ""NO"" otherwise.You can output ""Yes"" and ""No"" in any case (for example, the strings ""yES"", ""yes"", and ""Yes"" will be recognized as correct answers).
|
The first sample is described in the statement.In the second sample, there are only two colorings \([\color{blue}{4},\color{red}{7}]\) and \([\color{red}{4},\color{blue}{7}]\) , but in both cases the parity of sums is different.In the third sample, you can color \([\color{blue}{3},\color{blue}{9},\color{red}{8}]\) and \(12\) and \(8\) are both even.
|
Input: 781 2 4 3 2 3 5 424 733 9 821 755 4 3 2 144 3 4 5250 48 | Output: YES NO YES YES NO YES YES
|
Beginner
| 2 | 566 | 325 | 394 | 18 |
1,836 |
B
|
1836B
|
B. Astrophysicists
| 1,100 |
greedy; math
|
In many, many years, far, far away, there will be a launch of the first flight to Mars. To celebrate the success, \(n\) astrophysicists working on the project will be given bonuses of a total value of \(k\) gold coins.You have to distribute the money among the astrophysicists, and to make it easier, you have to assign bonuses in silver coins. Each gold coin is worth \(g\) silver coins, so you have to distribute all \(k \cdot g\) silver coins among \(n\) people.Unfortunately, the company has some financial troubles right now. Therefore, instead of paying the number of silver coins written on the bonus, they decided to round this amount to the nearest integer number of gold coins.The rounding procedure is as follows. If an astrophysicist has bonus equal to \(x\) silver coins, and we denote \(r = x \bmod g\), then: If \(r \geq \lceil \frac{g}{2} \rceil\), the astrophysicist receives \(x + (g - r)\) silver coins; Otherwise, an astrophysicists receives \(x - r\) silver coins. Note that due to rounding, the total sum of actually paid money is not, in general, equal to \(k \cdot g\) silver coins. The operation \(a \bmod b\) denotes the remainder of the division of \(a\) by \(b\). Sum of values before rounding has to be equal to \(k \cdot g\) silver coins, but some workers can be assigned \(0\) silver coins.You aim to distribute the bonuses so that the company saves as many silver coins due to rounding as possible. Please note that there is always a distribution in which the company spends no more than \(k \cdot g\) silver coins.
|
In the first line of input, there is one integer \(t\) (\(1 \leq t \leq 10^4\)) denoting the number of test cases.Each of the following \(t\) lines describes one test case and contains three integers \(n\), \(k\), \(g\) (\(1 \le n \le 10^9\), \(0 \le k \le 10^9\), \(2 \le g \le 10^9\)) β respectively the number of astrophysicists in the company, total number of gold coins to assign and the number of silver coins that one gold coin corresponds to.
|
In a separate line for each test case, output a single integer β the maximum number of silver coins that could be saved due to rounding.
|
In the first test case, one of the optimal assignments could be the following: First person: \(x = 30\) silver coins: company pays \(0\), saves \(30\) silver coins, Second person: \(x = 140\) silver coins: company pays \(100\), saves \(40\) silver coins, Third person: \(x = 130\) silver coins: company pays \(100\), saves \(30\) silver coins. In the second test case, we could have the following assignment: First person: \(x = 8\) silver coins: company pays \(14\), spends extra \(6\) silver coins, Second person: \(x = 6\) silver coins: company pays \(0\), saves \(6\) silver coins. If the bonuses are assigned to \(7\) silver coins for both astrophysicists, then the company would have to pay an additional gold coin to cover the bonuses.
|
Input: 53 3 1002 1 1491 2 1336 16 673 8 22 | Output: 100 0 26 72 176
|
Easy
| 2 | 1,547 | 450 | 136 | 18 |
1,970 |
E3
|
1970E3
|
E3. Trails (Hard)
| 2,200 |
dp; matrices
|
Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are \(m\) cabins, numbered 1 to \(m\). Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin \(i\) is connected with \(s_i\) short trails and \(l_i\) long trails to the lake.Each day, Harry walks a trail from the cabin where he currently is to Lake Geneva, and then from there he walks a trail to any of the \(m\) cabins (including the one he started in). However, as he has to finish the hike in a day, at least one of the two trails has to be short.How many possible combinations of trails can Harry take if he starts in cabin 1 and walks for \(n\) days?Give the answer modulo \(10^9 + 7\).
|
The first line contains the integers \(m\) and \(n\).The second line contains \(m\) integers, \(s_1, \dots, s_m\), where \(s_i\) is the number of short trails between cabin \(i\) and Lake Geneva.The third and last line contains \(m\) integers, \(l_1, \dots, l_m\), where \(l_i\) is the number of long trails between cabin \(i\) and Lake Geneva.We have the following constraints:\(0 \le s_i, l_i \le 10^3\).\(1 \le m \le 10^5\).\(1 \le n \le 10^9\).
|
The number of possible combinations of trails, modulo \(10^9 + 7\).
|
Input: 3 21 0 10 1 1 | Output: 18
|
Hard
| 2 | 754 | 448 | 67 | 19 |
|
1,921 |
A
|
1921A
|
A. Square
| 800 |
greedy; math
|
A square of positive (strictly greater than \(0\)) area is located on the coordinate plane, with sides parallel to the coordinate axes. You are given the coordinates of its corners, in random order. Your task is to find the area of the square.
|
Each test consists of several testcases. The first line contains one integer \(t\) (\(1 \le t \le 100\)) β the number of testcases. The following is a description of the testcases.Each testcase contains four lines, each line contains two integers \(x_i, y_i\) (\(-1000\le x_i, y_i\le 1000\)), coordinates of the corners of the square. It is guaranteed that there is a square with sides parallel to the coordinate axes, with positive (strictly greater than \(0\)) area, with corners in given points.
|
For each test case, print a single integer, the area of the square.
|
Input: 31 24 51 54 2-1 11 -11 1-1 -145 1145 3917 1117 39 | Output: 9 4 784
|
Beginner
| 2 | 243 | 498 | 67 | 19 |
|
1,940 |
D
|
1940D
| 0 |
*special; constructive algorithms; data structures; sortings
|
Beginner
| 4 | 0 | 0 | 0 | 19 |
||||||
965 |
C
|
965C
|
C. Greedy Arkady
| 2,000 |
math
|
\(k\) people want to split \(n\) candies between them. Each candy should be given to exactly one of them or be thrown away.The people are numbered from \(1\) to \(k\), and Arkady is the first of them. To split the candies, Arkady will choose an integer \(x\) and then give the first \(x\) candies to himself, the next \(x\) candies to the second person, the next \(x\) candies to the third person and so on in a cycle. The leftover (the remainder that is not divisible by \(x\)) will be thrown away.Arkady can't choose \(x\) greater than \(M\) as it is considered greedy. Also, he can't choose such a small \(x\) that some person will receive candies more than \(D\) times, as it is considered a slow splitting.Please find what is the maximum number of candies Arkady can receive by choosing some valid \(x\).
|
The only line contains four integers \(n\), \(k\), \(M\) and \(D\) (\(2 \le n \le 10^{18}\), \(2 \le k \le n\), \(1 \le M \le n\), \(1 \le D \le \min{(n, 1000)}\), \(M \cdot D \cdot k \ge n\)) β the number of candies, the number of people, the maximum number of candies given to a person at once, the maximum number of times a person can receive candies.
|
Print a single integer β the maximum possible number of candies Arkady can give to himself.Note that it is always possible to choose some valid \(x\).
|
In the first example Arkady should choose \(x = 4\). He will give \(4\) candies to himself, \(4\) candies to the second person, \(4\) candies to the third person, then \(4\) candies to the fourth person and then again \(4\) candies to himself. No person is given candies more than \(2\) times, and Arkady receives \(8\) candies in total.Note that if Arkady chooses \(x = 5\), he will receive only \(5\) candies, and if he chooses \(x = 3\), he will receive only \(3 + 3 = 6\) candies as well as the second person, the third and the fourth persons will receive \(3\) candies, and \(2\) candies will be thrown away. He can't choose \(x = 1\) nor \(x = 2\) because in these cases he will receive candies more than \(2\) times.In the second example Arkady has to choose \(x = 4\), because any smaller value leads to him receiving candies more than \(1\) time.
|
Input: 20 4 5 2 | Output: 8
|
Hard
| 1 | 809 | 354 | 150 | 9 |
2,103 |
A
|
2103A
|
A. Common Multiple
| 800 |
brute force; greedy; implementation; math
|
You are given an array of integers \(a_1, a_2, \ldots, a_n\). An array \(x_1, x_2, \ldots, x_m\) is beautiful if there exists an array \(y_1, y_2, \ldots, y_m\) such that the elements of \(y\) are distinct (in other words, \(y_i\neq y_j\) for all \(1 \le i < j \le m\)), and the product of \(x_i\) and \(y_i\) is the same for all \(1 \le i \le m\) (in other words, \(x_i\cdot y_i = x_j\cdot y_j\) for all \(1 \le i < j \le m\)).Your task is to determine the maximum size of a subsequence\(^{\text{β}}\) of array \(a\) that is beautiful.\(^{\text{β}}\)A sequence \(b\) is a subsequence of a sequence \(a\) if \(b\) can be obtained from \(a\) by the deletion of several (possibly, zero or all) element from arbitrary positions.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the elements of array \(a\).Note that there are no constraints on the sum of \(n\) over all test cases.
|
For each test case, output the maximum size of a subsequence of array \(a\) that is beautiful.
|
In the first test case, the entire array \(a = [1, 2, 3]\) is already beautiful. A possible array \(y\) is \([6, 3, 2]\), which is valid since the elements of \(y\) are distinct, and \(1\cdot 6 = 2\cdot 3 = 3\cdot 2\).In the second test case, the subsequence \([3, 1, 4, 5]\) is beautiful. A possible array \(y\) is \([20, 60, 15, 12]\). It can be proven that the entire array \(a = [3, 1, 4, 1, 5]\) is not beautiful, so the maximum size of a subsequence of array \(a\) that is beautiful is \(4\).
|
Input: 331 2 353 1 4 1 511 | Output: 3 4 1
|
Beginner
| 4 | 725 | 491 | 94 | 21 |
1,548 |
E
|
1548E
|
E. Gregor and the Two Painters
| 3,400 |
data structures; divide and conquer; graphs; greedy; math
|
Two painters, Amin and Benj, are repainting Gregor's living room ceiling! The ceiling can be modeled as an \(n \times m\) grid.For each \(i\) between \(1\) and \(n\), inclusive, painter Amin applies \(a_i\) layers of paint to the entire \(i\)-th row. For each \(j\) between \(1\) and \(m\), inclusive, painter Benj applies \(b_j\) layers of paint to the entire \(j\)-th column. Therefore, the cell \((i,j)\) ends up with \(a_i+b_j\) layers of paint.Gregor considers the cell \((i,j)\) to be badly painted if \(a_i+b_j \le x\). Define a badly painted region to be a maximal connected component of badly painted cells, i. e. a connected component of badly painted cells such that all adjacent to the component cells are not badly painted. Two cells are considered adjacent if they share a side.Gregor is appalled by the state of the finished ceiling, and wants to know the number of badly painted regions.
|
The first line contains three integers \(n\), \(m\) and \(x\) (\(1 \le n,m \le 2\cdot 10^5\), \(1 \le x \le 2\cdot 10^5\)) β the dimensions of Gregor's ceiling, and the maximum number of paint layers in a badly painted cell.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 2\cdot 10^5\)), the number of paint layers Amin applies to each row.The third line contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(1 \le b_j \le 2\cdot 10^5\)), the number of paint layers Benj applies to each column.
|
Print a single integer, the number of badly painted regions.
|
The diagram below represents the first example. The numbers to the left of each row represent the list \(a\), and the numbers above each column represent the list \(b\). The numbers inside each cell represent the number of paint layers in that cell.The colored cells correspond to badly painted cells. The red and blue cells respectively form \(2\) badly painted regions.
|
Input: 3 4 11 9 8 5 10 6 7 2 | Output: 2
|
Master
| 5 | 903 | 528 | 60 | 15 |
300 |
A
|
300A
|
A. Array
| 1,100 |
brute force; constructive algorithms; implementation
|
Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero ( < 0). The product of all numbers in the second set is greater than zero ( > 0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array.
|
The first line of the input contains integer n (3 β€ n β€ 100). The second line contains n space-separated distinct integers a1, a2, ..., an (|ai| β€ 103) β the array elements.
|
In the first line print integer n1 (n1 > 0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set.In the next line print integer n2 (n2 > 0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set.In the next line print integer n3 (n3 > 0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set.The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.
|
Input: 3-1 2 0 | Output: 1 -11 21 0
|
Easy
| 3 | 449 | 173 | 615 | 3 |
|
862 |
E
|
862E
|
E. Mahmoud and Ehab and the function
| 2,100 |
binary search; data structures; sortings
|
Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array a of length n and array b of length m. He introduced a function f(j) which is defined for integers j, which satisfy 0 β€ j β€ m - n. Suppose, ci = ai - bi + j. Then f(j) = |c1 - c2 + c3 - c4... cn|. More formally, . Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid j. They found it a bit easy, so Dr. Evil made their task harder. He will give them q update queries. During each update they should add an integer xi to all elements in a in range [li;ri] i.e. they should add xi to ali, ali + 1, ... , ari and then they should calculate the minimum value of f(j) for all valid j.Please help Mahmoud and Ehab.
|
The first line contains three integers n, m and q (1 β€ n β€ m β€ 105, 1 β€ q β€ 105) β number of elements in a, number of elements in b and number of queries, respectively.The second line contains n integers a1, a2, ..., an. ( - 109 β€ ai β€ 109) β elements of a.The third line contains m integers b1, b2, ..., bm. ( - 109 β€ bi β€ 109) β elements of b.Then q lines follow describing the queries. Each of them contains three integers li ri xi (1 β€ li β€ ri β€ n, - 109 β€ x β€ 109) β range to be updated and added value.
|
The first line should contain the minimum value of the function f before any update.Then output q lines, the i-th of them should contain the minimum value of the function f after performing the i-th update .
|
For the first example before any updates it's optimal to choose j = 0, f(0) = |(1 - 1) - (2 - 2) + (3 - 3) - (4 - 4) + (5 - 5)| = |0| = 0.After the first update a becomes {11, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(11 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6) = |9| = 9.After the second update a becomes {2, 2, 3, 4, 5} and it's optimal to choose j = 1, f(1) = |(2 - 2) - (2 - 3) + (3 - 4) - (4 - 5) + (5 - 6)| = |0| = 0.After the third update a becomes {1, 1, 2, 3, 4} and it's optimal to choose j = 0, f(0) = |(1 - 1) - (1 - 2) + (2 - 3) - (3 - 4) + (4 - 5)| = |0| = 0.
|
Input: 5 6 31 2 3 4 51 2 3 4 5 61 1 101 1 -91 5 -1 | Output: 0900
|
Hard
| 3 | 730 | 508 | 207 | 8 |
1,213 |
D2
|
1213D2
|
D2. Equalizing by Division (hard version)
| 1,600 |
brute force; math; sortings
|
The only difference between easy and hard versions is the number of elements in the array.You are given an array \(a\) consisting of \(n\) integers. In one move you can choose any \(a_i\) and divide it by \(2\) rounding down (in other words, in one move you can set \(a_i := \lfloor\frac{a_i}{2}\rfloor\)).You can perform such an operation any (possibly, zero) number of times with any \(a_i\).Your task is to calculate the minimum possible number of operations required to obtain at least \(k\) equal numbers in the array.Don't forget that it is possible to have \(a_i = 0\) after some operations, thus the answer always exists.
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of elements in the array and the number of equal numbers required.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)-th element of \(a\).
|
Print one integer β the minimum possible number of operations required to obtain at least \(k\) equal numbers in the array.
|
Input: 5 3 1 2 2 4 5 | Output: 1
|
Medium
| 3 | 629 | 341 | 123 | 12 |
|
331 |
A1
|
331A1
|
A1. Oh Sweet Beaverette
| 1,400 |
brute force; implementation
|
β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai β some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?
|
The first line contains a single integer n β the initial number of trees in the woodland belt, 2 β€ n. The second line contains space-separated integers ai β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. to get 30 points, you need to solve the problem with constraints: n β€ 100 (subproblem A1); to get 100 points, you need to solve the problem with constraints: n β€ 3Β·105 (subproblems A1+A2).
|
In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.In the next line print k integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to n from left to right.If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
|
Input: 51 2 3 1 2 | Output: 8 11
|
Easy
| 2 | 1,133 | 444 | 440 | 3 |
|
1,624 |
C
|
1624C
|
C. Division by Two and Permutation
| 1,100 |
constructive algorithms; flows; graph matchings; greedy; math
|
You are given an array \(a\) consisting of \(n\) positive integers. You can perform operations on it.In one operation you can replace any element of the array \(a_i\) with \(\lfloor \frac{a_i}{2} \rfloor\), that is, by an integer part of dividing \(a_i\) by \(2\) (rounding down).See if you can apply the operation some number of times (possible \(0\)) to make the array \(a\) become a permutation of numbers from \(1\) to \(n\) βthat is, so that it contains all numbers from \(1\) to \(n\), each exactly once.For example, if \(a = [1, 8, 25, 2]\), \(n = 4\), then the answer is yes. You could do the following: Replace \(8\) with \(\lfloor \frac{8}{2} \rfloor = 4\), then \(a = [1, 4, 25, 2]\). Replace \(25\) with \(\lfloor \frac{25}{2} \rfloor = 12\), then \(a = [1, 4, 12, 2]\). Replace \(12\) with \(\lfloor \frac{12}{2} \rfloor = 6\), then \(a = [1, 4, 6, 2]\). Replace \(6\) with \(\lfloor \frac{6}{2} \rfloor = 3\), then \(a = [1, 4, 3, 2]\).
|
The first line of input data contains an integer \(t\) (\(1 \le t \le 10^4\)) βthe number of test cases.Each test case contains exactly two lines. The first one contains an integer \(n\) (\(1 \le n \le 50\)), the second one contains integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).
|
For each test case, output on a separate line: YES if you can make the array \(a\) become a permutation of numbers from \(1\) to \(n\), NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
|
The first test case is explained in the text of the problem statement.In the second test case, it is not possible to get a permutation.
|
Input: 641 8 25 221 199 8 3 4 2 7 1 5 638 2 1424 7 16 7522 6 22 4 22 | Output: YES NO YES NO NO YES
|
Easy
| 5 | 950 | 292 | 275 | 16 |
1,013 |
A
|
1013A
|
A. Piles With Stones
| 800 |
math
|
There is a beautiful garden of stones in Innopolis.Its most beautiful place is the \(n\) piles with stones numbered from \(1\) to \(n\).EJOI participants have visited this place twice. When they first visited it, the number of stones in piles was \(x_1, x_2, \ldots, x_n\), correspondingly. One of the participants wrote down this sequence in a notebook. They visited it again the following day, and the number of stones in piles was equal to \(y_1, y_2, \ldots, y_n\). One of the participants also wrote it down in a notebook.It is well known that every member of the EJOI jury during the night either sits in the room \(108\) or comes to the place with stones. Each jury member who comes there either takes one stone for himself or moves one stone from one pile to another. We can assume that there is an unlimited number of jury members. No one except the jury goes to the place with stones at night.Participants want to know whether their notes can be correct or they are sure to have made a mistake.
|
The first line of the input file contains a single integer \(n\), the number of piles with stones in the garden (\(1 \leq n \leq 50\)).The second line contains \(n\) integers separated by spaces \(x_1, x_2, \ldots, x_n\), the number of stones in piles recorded in the notebook when the participants came to the place with stones for the first time (\(0 \leq x_i \leq 1000\)).The third line contains \(n\) integers separated by spaces \(y_1, y_2, \ldots, y_n\), the number of stones in piles recorded in the notebook when the participants came to the place with stones for the second time (\(0 \leq y_i \leq 1000\)).
|
If the records can be consistent output ""Yes"", otherwise output ""No"" (quotes for clarity).
|
In the first example, the following could have happened during the night: one of the jury members moved one stone from the second pile to the first pile, and the other jury member moved one stone from the fourth pile to the third pile.In the second example, the jury took stones from the second and fourth piles.It can be proved that it is impossible for the jury members to move and took stones to convert the first array into the second array.
|
Input: 51 2 3 4 52 1 4 3 5 | Output: Yes
|
Beginner
| 1 | 1,004 | 615 | 94 | 10 |
1,290 |
F
|
1290F
|
F. Making Shapes
| 3,500 |
dp
|
You are given \(n\) pairwise non-collinear two-dimensional vectors. You can make shapes in the two-dimensional plane with these vectors in the following fashion: Start at the origin \((0, 0)\). Choose a vector and add the segment of the vector to the current point. For example, if your current point is at \((x, y)\) and you choose the vector \((u, v)\), draw a segment from your current point to the point at \((x + u, y + v)\) and set your current point to \((x + u, y + v)\). Repeat step 2 until you reach the origin again.You can reuse a vector as many times as you want.Count the number of different, non-degenerate (with an area greater than \(0\)) and convex shapes made from applying the steps, such that the shape can be contained within a \(m \times m\) square, and the vectors building the shape are in counter-clockwise fashion. Since this number can be too large, you should calculate it by modulo \(998244353\).Two shapes are considered the same if there exists some parallel translation of the first shape to another.A shape can be contained within a \(m \times m\) square if there exists some parallel translation of this shape so that every point \((u, v)\) inside or on the border of the shape satisfies \(0 \leq u, v \leq m\).
|
The first line contains two integers \(n\) and \(m\) β the number of vectors and the size of the square (\(1 \leq n \leq 5\), \(1 \leq m \leq 10^9\)).Each of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) β the \(x\)-coordinate and \(y\)-coordinate of the \(i\)-th vector (\(|x_i|, |y_i| \leq 4\), \((x_i, y_i) \neq (0, 0)\)).It is guaranteed, that no two vectors are parallel, so for any two indices \(i\) and \(j\) such that \(1 \leq i < j \leq n\), there is no real value \(k\) such that \(x_i \cdot k = x_j\) and \(y_i \cdot k = y_j\).
|
Output a single integer β the number of satisfiable shapes by modulo \(998244353\).
|
The shapes for the first sample are: The only shape for the second sample is: The only shape for the fourth sample is:
|
Input: 3 3 -1 0 1 1 0 -1 | Output: 3
|
Master
| 1 | 1,246 | 555 | 83 | 12 |
1,987 |
D
|
1987D
|
D. World is Mine
| 1,800 |
dp; games
|
Alice and Bob are playing a game. Initially, there are \(n\) cakes, with the \(i\)-th cake having a tastiness value of \(a_i\).Alice and Bob take turns eating them, with Alice starting first: In her turn, Alice chooses and eats any remaining cake whose tastiness is strictly greater than the maximum tastiness of any of the cakes she's eaten before that. Note that on the first turn, she can choose any cake. In his turn, Bob chooses any remaining cake and eats it. The game ends when the current player can't eat a suitable cake. Let \(x\) be the number of cakes that Alice ate. Then, Alice wants to maximize \(x\), while Bob wants to minimize \(x\).Find out how many cakes Alice will eat if both players play optimally.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5000\)) β the number of cakes.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β the tastiness values of the cakes.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5000\).
|
For each test case, output a single integer β the number of cakes Alice will eat if both players play optimally.
|
In the first test case, one possible sequence of turns is: Alice eats a cake with a tastiness value of \(1\). The remaining cakes are \([4, 2, 3]\). Bob eats a cake with a tastiness value of \(2\). The remaining cakes are \([4, 3]\). Alice eats a cake with a tastiness of \(3\). The remaining cakes are \([4]\). Bob eats a cake with a tastiness value of \(4\). The remaining cakes are \([]\). Since there are no more cakes left, the game ends. In the second test case, one possible sequence of turns is: Alice eats a cake with a tastiness value of \(1\). The remaining cakes are \([1, 1]\). Bob eats a cake with a tastiness value of \(1\). The remaining cakes are \([1]\). Since Alice has already eaten a cake with a tastiness value of \(1\), she cannot make a turn, so the game ends.
|
Input: 941 4 2 331 1 151 4 2 3 443 4 1 41184 3 2 5 6 8 3 476 1 1 3 5 3 1116 11 6 8 7 5 3 11 2 3 5172 6 5 3 9 1 6 2 5 6 3 2 3 9 6 1 6 | Output: 2 1 3 2 1 3 2 4 4
|
Medium
| 2 | 721 | 524 | 112 | 19 |
1,492 |
B
|
1492B
|
B. Card Deck
| 1,100 |
data structures; greedy; math
|
You have a deck of \(n\) cards, and you'd like to reorder it to a new one.Each card has a value between \(1\) and \(n\) equal to \(p_i\). All \(p_i\) are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. \(p_1\) stands for the bottom card, \(p_n\) is the top card. In each step you pick some integer \(k > 0\), take the top \(k\) cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)Let's define an order of a deck as \(\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}\).Given the original deck, output the deck with maximum possible order you can make using the operation above.
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains the single integer \(n\) (\(1 \le n \le 10^5\)) β the size of deck you have.The second line contains \(n\) integers \(p_1, p_2,\dots, p_n\) (\(1 \le p_i \le n\); \(p_i \neq p_j\) if \(i \neq j\)) β values of card in the deck from bottom to top.It's guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.If there are multiple answers, print any of them.
|
In the first test case, one of the optimal strategies is the next one: take \(1\) card from the top of \(p\) and move it to \(p'\): \(p\) becomes \([1, 2, 3]\), \(p'\) becomes \([4]\); take \(1\) card from the top of \(p\): \(p\) becomes \([1, 2]\), \(p'\) becomes \([4, 3]\); take \(1\) card from the top of \(p\): \(p\) becomes \([1]\), \(p'\) becomes \([4, 3, 2]\); take \(1\) card from the top of \(p\): \(p\) becomes empty, \(p'\) becomes \([4, 3, 2, 1]\). In result, \(p'\) has order equal to \(4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1\) \(=\) \(256 + 48 + 8 + 1 = 313\).In the second test case, one of the optimal strategies is: take \(4\) cards from the top of \(p\) and move it to \(p'\): \(p\) becomes \([1]\), \(p'\) becomes \([5, 2, 4, 3]\); take \(1\) card from the top of \(p\) and move it to \(p'\): \(p\) becomes empty, \(p'\) becomes \([5, 2, 4, 3, 1]\); In result, \(p'\) has order equal to \(5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1\) \(=\) \(3125 + 250 + 100 + 15 + 1 = 3491\).In the third test case, one of the optimal strategies is: take \(2\) cards from the top of \(p\) and move it to \(p'\): \(p\) becomes \([4, 2, 5, 3]\), \(p'\) becomes \([6, 1]\); take \(2\) cards from the top of \(p\) and move it to \(p'\): \(p\) becomes \([4, 2]\), \(p'\) becomes \([6, 1, 5, 3]\); take \(2\) cards from the top of \(p\) and move it to \(p'\): \(p\) becomes empty, \(p'\) becomes \([6, 1, 5, 3, 4, 2]\). In result, \(p'\) has order equal to \(6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2\) \(=\) \(46656 + 1296 + 1080 + 108 + 24 + 2 = 49166\).
|
Input: 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 | Output: 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1
|
Easy
| 3 | 761 | 465 | 165 | 14 |
1,700 |
B
|
1700B
|
B. Palindromic Numbers
| 1,100 |
constructive algorithms; implementation; math
|
During a daily walk Alina noticed a long number written on the ground. Now Alina wants to find some positive number of same length without leading zeroes, such that the sum of these two numbers is a palindrome. Recall that a number is called a palindrome, if it reads the same right to left and left to right. For example, numbers \(121, 66, 98989\) are palindromes, and \(103, 239, 1241\) are not palindromes.Alina understands that a valid number always exist. Help her find one!
|
The first line of input data contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. Next, descriptions of \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(2 \leq n \leq 100\,000\)) β the length of the number that is written on the ground.The second line of contains the positive \(n\)-digit integer without leading zeroes β the number itself.It is guaranteed that the sum of the values \(n\) over all test cases does not exceed \(100\,000\).
|
For each of \(t\) test cases print an answer β a positive \(n\)-digit integer without leading zeros, such that the sum of the input integer and this number is a palindrome.We can show that at least one number satisfying the constraints exists. If there are multiple solutions, you can output any of them.
|
In the first test case \(99 + 32 = 131\) is a palindrome. Note that another answer is \(12\), because \(99 + 12 = 111\) is also a palindrome.In the second test case \(1023 + 8646 = 9669\).In the third test case \(385 + 604 = 989\).
|
Input: 3 2 99 4 1023 3 385 | Output: 32 8646 604
|
Easy
| 3 | 480 | 507 | 304 | 17 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.