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,170 |
G
|
1170G
|
G. Graph Decomposition
| 0 |
*special; graphs
|
You are given an undirected graph consisting of \(n\) vertices and \(m\) edges.Recall that a cycle is a path that starts and ends in the same vertex. A cycle in a graph is called simple if it contains each vertex (except the starting and ending one) no more than once (the starting and the ending one is contained always twice). Note that loops are considered to be simple cycles.In one move you can choose any simple cycle in this graph and erase the edges corresponding to this cycle (corresponding vertices remain in the graph). It is allowed to erase the loop or two copies of the same edge (take a look at examples).Your problem is to apply some sequence of moves to obtain the graph without edges. It is not necessary to minimize the number of cycles. If it is impossible, print ""NO"".
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of vertices and the number of edges in the graph.The next \(m\) lines contain edges of the graph. The \(i\)-th line contains the \(i\)-th edge \(x_i, y_i\) (\(1 \le x_i, y_i \le n\)), where \(x_i\) and \(y_i\) are vertices connected by the \(i\)-th edge. The graph can contain loops or multiple edges.
|
If it is impossible to decompose the given graph into simple cycles, print ""NO"" in the first line.Otherwise print ""YES"" in the first line. In the second line print \(k\) β the number of simple cycles in the graph decomposition.In the next \(k\) lines print cycles themselves. The \(j\)-th line should contain the \(j\)-th cycle. First, print \(c_j\) β the number of vertices in the \(j\)-th cycle. Then print the cycle as a sequence of vertices. All neighbouring (adjacent) vertices in the printed path should be connected by an edge that isn't contained in other cycles.
|
The picture corresponding to the first example:
|
Input: 6 9 1 2 2 3 1 3 2 4 2 5 4 5 3 5 3 6 5 6 | Output: YES 3 4 2 5 4 2 4 3 6 5 3 4 1 3 2 1
|
Beginner
| 2 | 792 | 414 | 575 | 11 |
285 |
A
|
285A
|
A. Slightly Decreasing Permutations
| 1,100 |
greedy; implementation
|
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.The decreasing coefficient of permutation p1, p2, ..., pn is the number of such i (1 β€ i < n), that pi > pi + 1.You have numbers n and k. Your task is to print the permutation of length n with decreasing coefficient k.
|
The single line contains two space-separated integers: n, k (1 β€ n β€ 105, 0 β€ k < n) β the permutation length and the decreasing coefficient.
|
In a single line print n space-separated integers: p1, p2, ..., pn β the permutation of length n with decreasing coefficient k. If there are several permutations that meet this condition, print any of them. It is guaranteed that the permutation with the sought parameters exists.
|
Input: 5 2 | Output: 1 5 2 4 3
|
Easy
| 2 | 482 | 141 | 279 | 2 |
|
1,296 |
E1
|
1296E1
|
E1. String Coloring (easy version)
| 1,800 |
constructive algorithms; dp; graphs; greedy; sortings
|
This is an easy version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.You are given a string \(s\) consisting of \(n\) lowercase Latin letters.You have to color all its characters one of the two colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in \(s\)).After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.The goal is to make the string sorted, i.e. all characters should be in alphabetical order.Your task is to say if it is possible to color the given string so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 200\)) β the length of \(s\).The second line of the input contains the string \(s\) consisting of exactly \(n\) lowercase Latin letters.
|
If it is impossible to color the given string so that after coloring it can become sorted by some sequence of swaps, print ""NO"" (without quotes) in the first line.Otherwise, print ""YES"" in the first line and any correct coloring in the second line (the coloring is the string consisting of \(n\) characters, the \(i\)-th character should be '0' if the \(i\)-th character is colored the first color and '1' otherwise).
|
Input: 9 abacbecfd | Output: YES 001010101
|
Medium
| 5 | 977 | 206 | 421 | 12 |
|
2,130 |
B
|
2130B
|
B. Pathless
| 1,100 |
constructive algorithms
|
There is an array \(a_1, a_2, \ldots, a_n\) consisting of values \(0\), \(1\), and \(2\), and an integer \(s\). It is guaranteed that \(a_1, a_2, \ldots, a_n\) contains at least one \(0\), one \(1\), and one \(2\).Alice wants to start from index \(1\) and perform steps of length \(1\) to the right or to the left, and reach index \(n\) at the end. While Alice moves, she calculates the sum of the values she is visiting, and she wants the sum to be exactly \(s\).Formally, Alice wants to find a sequence \([i_1, i_2, \ldots, i_m]\) of indices, such that: \(i_1 = 1\), \(i_m = n\). \(1 \leq i_j \leq n\) for all \(1 \le j \le m\). \(|i_{j} - i_{j+1}| = 1\) for all \(1 \leq j < m\). \(a_{i_1} + a_{i_2} + \ldots + a_{i_m} = s\). However, Bob wants to rearrange \(a_1, a_2, \ldots, a_n\) to prevent Alice from achieving her target. Determine whether it is possible to rearrange \(a_1, a_2, \ldots, a_n\) such that Alice cannot find her target sequence (even if Alice is smart enough). If it is possible, you also need to output the rearranged array \(a_1, a_2, \ldots, a_n\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows. The first line of each test case contains two integers \(n\) and \(s\) (\(3 \le n \le 50\), \(0 \le s \le 1000\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 2\)).It is guaranteed that \(a\) contains at least one \(0\), one \(1\), and one \(2\).
|
For each test case, if it is possible to rearrange \(a\) such that Alice cannot find her target sequence, output \(n\) integers β such rearrangement of \(a\). Otherwise, output \(-1\) instead.
|
In the first test case, any rearrangement of \(a\) can prevent Alice from achieving her target.In the second test case, regardless of rearranging \(a\), Alice can always find the sequence \([1,2,3]\) as her target sequence.In the third test case,there is no rearrangement of \(a\) that can prevent Alice from achieving her target. For example, for \(a=[0,2,1]\), Alice can find the sequence \([1,2,3,2,3]\) as her target sequence.
|
Input: 63 20 1 23 30 1 23 60 1 23 40 1 23 100 1 25 10002 0 1 1 2 | Output: 0 1 2 -1 -1 0 2 1 -1 -1
|
Easy
| 1 | 1,074 | 464 | 192 | 21 |
27 |
E
|
27E
|
E. Number With The Given Amount Of Divisors
| 2,000 |
brute force; dp; number theory
|
Given the number n, find the smallest positive integer which has exactly n divisors. It is guaranteed that for the given n the answer will not exceed 1018.
|
The first line of the input contains integer n (1 β€ n β€ 1000).
|
Output the smallest positive integer with exactly n divisors.
|
Input: 4 | Output: 6
|
Hard
| 3 | 155 | 62 | 61 | 0 |
|
2,068 |
K
|
2068K
|
K. Amusement Park Rides
| 3,000 |
flows; graphs
|
Ivan, Dmitrii, and Pjotr are celebrating Ivan's birthday at an amusement park with \(n\) attractions. The \(i\)-th attraction operates at minutes \(a_i, 2a_i, 3a_i, \dots\) (i.e., every \(a_i\) minutes).Each minute, the friends can either ride exactly one available attraction together or wait. Since the rides are very short, they can board another attraction the next minute. They may ride the attractions in any order.They want to experience each ride exactly once before heading off to enjoy the birthday cake. What is the earliest time by which they can finish all \(n\) attractions?
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 2000\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line contains an integer \(n\) (\(1\le n \le 2000\)) β the number of attractions. The second line contains \(n\) integers \(a_1,\, a_2,\,\ldots,\, a_n\) (\(1 \le a_i \le 10^9\)) β the values determining when the various attractions operate.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\).
|
For each test case, print the earliest time the three friends can finish all \(n\) attractions.
|
In the first test case, the three friends can ride the \(i\)-th attraction at the \(i\)-th minute. Since there are \(4\) attractions, they need \(4\) minutes to ride them all.In the third test case, the three friends can ride the attractions in order at minutes \(1,\, 2,\, 3,\, 4,\, 6,\, 8\) respectively. Therefore, they can ride all attractions in \(8\) minutes. One can show that it is impossible to finish all the attractions earlier.
|
Input: 341 2 3 441 1 1 161 2 1 2 2 2 | Output: 4 4 8
|
Master
| 2 | 588 | 512 | 95 | 20 |
1,147 |
C
|
1147C
|
C. Thanos Nim
| 2,000 |
games
|
Alice and Bob are playing a game with \(n\) piles of stones. It is guaranteed that \(n\) is an even number. The \(i\)-th pile has \(a_i\) stones.Alice and Bob will play a game alternating turns with Alice going first.On a player's turn, they must choose exactly \(\frac{n}{2}\) nonempty piles and independently remove a positive number of stones from each of the chosen piles. They can remove a different number of stones from the piles in a single turn. The first player unable to make a move loses (when there are less than \(\frac{n}{2}\) nonempty piles).Given the starting configuration, determine who will win the game.
|
The first line contains one integer \(n\) (\(2 \leq n \leq 50\)) β the number of piles. It is guaranteed that \(n\) is an even number.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 50\)) β the number of stones in the piles.
|
Print a single string ""Alice"" if Alice wins; otherwise, print ""Bob"" (without double quotes).
|
In the first example, each player can only remove stones from one pile (\(\frac{2}{2}=1\)). Alice loses, since Bob can copy whatever Alice does on the other pile, so Alice will run out of moves first.In the second example, Alice can remove \(2\) stones from the first pile and \(3\) stones from the third pile on her first move to guarantee a win.
|
Input: 2 8 8 | Output: Bob
|
Hard
| 1 | 624 | 261 | 96 | 11 |
1,984 |
H
|
1984H
|
H. Tower Capturing
| 3,300 |
combinatorics; dp; geometry
|
There are \(n\) towers at \(n\) distinct points \((x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)\), such that no three are collinear and no four are concyclic. Initially, you own towers \((x_1, y_1)\) and \((x_2, y_2)\), and you want to capture all of them. To do this, you can do the following operation any number of times: Pick two towers \(P\) and \(Q\) you own and one tower \(R\) you don't own, such that the circle through \(P\), \(Q\), and \(R\) contains all \(n\) towers inside of it. Afterwards, capture all towers in or on triangle \(\triangle PQR\), including \(R\) itself. An attack plan is a series of choices of \(R\) (\(R_1, R_2, \ldots, R_k\)) using the above operations after which you capture all towers. Note that two attack plans are considered different only if they differ in their choice of \(R\) in some operation; in particular, two attack plans using the same choices of \(R\) but different choices of \(P\) and \(Q\) are considered the same.Count the number of attack plans of minimal length. Note that it might not be possible to capture all towers, in which case the answer is \(0\). Since the answer may be large, output it modulo \(998\,244\,353\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 250\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(4 \leq n \leq 100\)) β the number of towers.The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(-10^4 \leq x_i, y_i \leq 10^4\)) β the location of the \(i\)-th tower. Initially, you own towers \((x_1, y_1)\) and \((x_2, y_2)\).All towers are at distinct locations, no three towers are collinear, and no four towers are concyclic.The sum of \(n\) over all test cases does not exceed \(1000\).
|
For each test case, output a single integer β the number of attack plans of minimal length after which you capture all towers, modulo \(998\,244\,353\).
|
In the first test case, there is only one possible attack plan of shortest length, shown below. Use the operation with \(P =\) tower \(1\), \(Q =\) tower \(2\), and \(R =\) tower \(5\). The circle through these three towers contains all towers inside of it, and as a result towers \(3\) and \(5\) are captured. Use the operation with \(P =\) tower \(5\), \(Q =\) tower \(1\), and \(R =\) tower \(4\). The circle through these three towers contains all towers inside of it, and as a result tower \(4\) is captured. In the second case, for example, you can never capture the tower at \((3, 10\,000)\).
|
Input: 351 12 53 34 25 461 13 31 22 13 1000019 8472 7-4 -3-3 63 1-5 21 -4-1 7 | Output: 1 0 10
|
Master
| 3 | 1,176 | 587 | 152 | 19 |
1,537 |
E2
|
1537E2
|
E2. Erase and Extend (Hard Version)
| 2,200 |
binary search; data structures; greedy; hashing; string suffix structures; strings; two pointers
|
This is the hard version of the problem. The only difference is the constraints on \(n\) and \(k\). You can make hacks only if all versions of the problem are solved.You have a string \(s\), and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: \(s:=s+s\), where \(+\) denotes concatenation. You can use each operation any number of times (possibly none).Your task is to find the lexicographically smallest string of length exactly \(k\) that can be obtained by doing these operations on string \(s\).A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a\ne b\); In the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).
|
The first line contains two integers \(n\), \(k\) (\(1 \leq n, k \leq 5\cdot 10^5\)) β the length of the original string \(s\) and the length of the desired string.The second line contains the string \(s\), consisting of \(n\) lowercase English letters.
|
Print the lexicographically smallest string of length \(k\) that can be obtained by doing the operations on string \(s\).
|
In the first test, it is optimal to make one duplication: ""dbcadabc"" \(\to\) ""dbcadabcdbcadabc"".In the second test it is optimal to delete the last \(3\) characters, then duplicate the string \(3\) times, then delete the last \(3\) characters to make the string have length \(k\).""abcd"" \(\to\) ""abc"" \(\to\) ""ab"" \(\to\) ""a"" \(\to\) ""aa"" \(\to\) ""aaaa"" \(\to\) ""aaaaaaaa"" \(\to\) ""aaaaaaa"" \(\to\) ""aaaaaa"" \(\to\) ""aaaaa"".
|
Input: 8 16 dbcadabc | Output: dbcadabcdbcadabc
|
Hard
| 7 | 865 | 253 | 121 | 15 |
429 |
C
|
429C
|
C. Guess the Tree
| 2,300 |
bitmasks; constructive algorithms; dp; greedy; trees
|
Iahub and Iahubina went to a picnic in a forest full of trees. Less than 5 minutes passed before Iahub remembered of trees from programming. Moreover, he invented a new problem and Iahubina has to solve it, otherwise Iahub won't give her the food. Iahub asks Iahubina: can you build a rooted tree, such that each internal node (a node with at least one son) has at least two sons; node i has ci nodes in its subtree? Iahubina has to guess the tree. Being a smart girl, she realized that it's possible no tree can follow Iahub's restrictions. In this way, Iahub will eat all the food. You need to help Iahubina: determine if there's at least one tree following Iahub's restrictions. The required tree must contain n nodes.
|
The first line of the input contains integer n (1 β€ n β€ 24). Next line contains n positive integers: the i-th number represents ci (1 β€ ci β€ n).
|
Output on the first line ""YES"" (without quotes) if there exist at least one tree following Iahub's restrictions, otherwise output ""NO"" (without quotes).
|
Input: 41 1 1 4 | Output: YES
|
Expert
| 5 | 721 | 144 | 156 | 4 |
|
407 |
B
|
407B
|
B. Long Path
| 1,600 |
dp; implementation
|
One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 β€ i β€ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number pi, where 1 β€ pi β€ i.In order not to get lost, Vasya decided to act as follows. Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1. Let's assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal. Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.
|
The first line contains integer n (1 β€ n β€ 103) β the number of rooms. The second line contains n integers pi (1 β€ pi β€ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.
|
Print a single number β the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109 + 7).
|
Input: 21 2 | Output: 4
|
Medium
| 2 | 1,014 | 238 | 160 | 4 |
|
444 |
D
|
444D
|
D. DZY Loves Strings
| 2,500 |
binary search; hashing; strings; two pointers
|
DZY loves strings, and he enjoys collecting them.In China, many people like to use strings containing their names' initials, for example: xyz, jcvb, dzy, dyh.Once DZY found a lucky string s. A lot of pairs of good friends came to DZY when they heard about the news. The first member of the i-th pair has name ai, the second one has name bi. Each pair wondered if there is a substring of the lucky string containing both of their names. If so, they want to find the one with minimum length, which can give them good luck and make their friendship last forever.Please help DZY for each pair find the minimum length of the substring of s that contains both ai and bi, or point out that such substring doesn't exist.A substring of s is a string slsl + 1... sr for some integers l, r (1 β€ l β€ r β€ |s|). The length of such the substring is (r - l + 1).A string p contains some another string q if there is a substring of p equal to q.
|
The first line contains a string s (1 β€ |s| β€ 50000).The second line contains a non-negative integer q (0 β€ q β€ 100000) β the number of pairs. Each of the next q lines describes a pair, the line contains two space-separated strings ai and bi (1 β€ |ai|, |bi| β€ 4).It is guaranteed that all the strings only consist of lowercase English letters.
|
For each pair, print a line containing a single integer β the minimum length of the required substring. If there is no such substring, output -1.
|
The shortest substrings in the first sample are: xyz, dyhduxyz.The shortest substrings in the second sample are: ca, abc and abd.The shortest substrings in the third sample are: baabca and abaa.
|
Input: xudyhduxyz3xyz xyzdyh xyzdzy xyz | Output: 38-1
|
Expert
| 4 | 928 | 343 | 145 | 4 |
53 |
B
|
53B
|
B. Blog Photo
| 1,700 |
binary search; implementation
|
One popular blog site edits the uploaded photos like this. It cuts a rectangular area out of them so that the ratio of height to width (i.e. the height / width quotient) can vary from 0.8 to 1.25 inclusively. Besides, at least one side of the cut area should have a size, equal to some power of number 2 (2x for some integer x). If those rules don't indicate the size of the cut are clearly, then the way with which the cut part possesses the largest area is chosen. Of course, both sides of the cut area should be integer. If there are several answers to this problem, you should choose the answer with the maximal height.
|
The first line contains a pair of integers h and w (1 β€ h, w β€ 109) which are the height and width of the uploaded photo in pixels.
|
Print two integers which are the height and width of the cut area.
|
Input: 2 1 | Output: 1 1
|
Medium
| 2 | 623 | 131 | 66 | 0 |
|
1,042 |
D
|
1042D
|
D. Petya and Array
| 1,800 |
data structures; divide and conquer; two pointers
|
Petya has an array \(a\) consisting of \(n\) integers. He has learned partial sums recently, and now he can calculate the sum of elements on any segment of the array really fast. The segment is a non-empty sequence of elements standing one next to another in the array.Now he wonders what is the number of segments in his array with the sum less than \(t\). Help Petya to calculate this number.More formally, you are required to calculate the number of pairs \(l, r\) (\(l \le r\)) such that \(a_l + a_{l+1} + \dots + a_{r-1} + a_r < t\).
|
The first line contains two integers \(n\) and \(t\) (\(1 \le n \le 200\,000, |t| \le 2\cdot10^{14}\)).The second line contains a sequence of integers \(a_1, a_2, \dots, a_n\) (\(|a_{i}| \le 10^{9}\)) β the description of Petya's array. Note that there might be negative, zero and positive elements.
|
Print the number of segments in Petya's array with the sum of elements less than \(t\).
|
In the first example the following segments have sum less than \(4\): \([2, 2]\), sum of elements is \(-1\) \([2, 3]\), sum of elements is \(2\) \([3, 3]\), sum of elements is \(3\) \([4, 5]\), sum of elements is \(3\) \([5, 5]\), sum of elements is \(-1\)
|
Input: 5 45 -1 3 4 -1 | Output: 5
|
Medium
| 3 | 538 | 299 | 87 | 10 |
1,584 |
C
|
1584C
|
C. Two Arrays
| 900 |
greedy; math; sortings
|
You are given two arrays of integers \(a_1, a_2, \ldots, a_n\) and \(b_1, b_2, \ldots, b_n\).Let's define a transformation of the array \(a\): Choose any non-negative integer \(k\) such that \(0 \le k \le n\). Choose \(k\) distinct array indices \(1 \le i_1 < i_2 < \ldots < i_k \le n\). Add \(1\) to each of \(a_{i_1}, a_{i_2}, \ldots, a_{i_k}\), all other elements of array \(a\) remain unchanged. Permute the elements of array \(a\) in any order. Is it possible to perform some transformation of the array \(a\) exactly once, so that the resulting array is equal to \(b\)?
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Descriptions of test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the size of arrays \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-100 \le a_i \le 100\)).The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(-100 \le b_i \le 100\)).
|
For each test case, print ""YES"" (without quotes) if it is possible to perform a transformation of the array \(a\), so that the resulting array is equal to \(b\). Print ""NO"" (without quotes) otherwise.You can print each letter in any case (upper or lower).
|
In the first test case, we can make the following transformation: Choose \(k = 2\). Choose \(i_1 = 1\), \(i_2 = 2\). Add \(1\) to \(a_1\) and \(a_2\). The resulting array is \([0, 2, 0]\). Swap the elements on the second and third positions. In the second test case there is no suitable transformation.In the third test case we choose \(k = 0\) and do not change the order of elements.
|
Input: 3 3 -1 1 0 0 0 2 1 0 2 5 1 2 3 4 5 1 2 3 4 5 | Output: YES NO YES
|
Beginner
| 3 | 575 | 476 | 259 | 15 |
1,037 |
C
|
1037C
|
C. Equalize
| 1,300 |
dp; greedy; strings
|
You are given two binary strings \(a\) and \(b\) of the same length. You can perform the following two operations on the string \(a\): Swap any two bits at indices \(i\) and \(j\) respectively (\(1 \le i, j \le n\)), the cost of this operation is \(|i - j|\), that is, the absolute difference between \(i\) and \(j\). Select any arbitrary index \(i\) (\(1 \le i \le n\)) and flip (change \(0\) to \(1\) or \(1\) to \(0\)) the bit at this index. The cost of this operation is \(1\). Find the minimum cost to make the string \(a\) equal to \(b\). It is not allowed to modify string \(b\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^6\)) β the length of the strings \(a\) and \(b\).The second and third lines contain strings \(a\) and \(b\) respectively.Both strings \(a\) and \(b\) have length \(n\) and contain only '0' and '1'.
|
Output the minimum cost to make the string \(a\) equal to \(b\).
|
In the first example, one of the optimal solutions is to flip index \(1\) and index \(3\), the string \(a\) changes in the following way: ""100"" \(\to\) ""000"" \(\to\) ""001"". The cost is \(1 + 1 = 2\).The other optimal solution is to swap bits and indices \(1\) and \(3\), the string \(a\) changes then ""100"" \(\to\) ""001"", the cost is also \(|1 - 3| = 2\).In the second example, the optimal solution is to swap bits at indices \(2\) and \(3\), the string \(a\) changes as ""0101"" \(\to\) ""0011"". The cost is \(|2 - 3| = 1\).
|
Input: 3100001 | Output: 2
|
Easy
| 3 | 586 | 262 | 64 | 10 |
1,809 |
E
|
1809E
|
E. Two Tanks
| 2,400 |
binary search; dp; implementation; math
|
There are two water tanks, the first one fits \(a\) liters of water, the second one fits \(b\) liters of water. The first tank has \(c\) (\(0 \le c \le a\)) liters of water initially, the second tank has \(d\) (\(0 \le d \le b\)) liters of water initially.You want to perform \(n\) operations on them. The \(i\)-th operation is specified by a single non-zero integer \(v_i\). If \(v_i > 0\), then you try to pour \(v_i\) liters of water from the first tank into the second one. If \(v_i < 0\), you try to pour \(-v_i\) liters of water from the second tank to the first one.When you try to pour \(x\) liters of water from the tank that has \(y\) liters currently available to the tank that can fit \(z\) more liters of water, the operation only moves \(\min(x, y, z)\) liters of water.For all pairs of the initial volumes of water \((c, d)\) such that \(0 \le c \le a\) and \(0 \le d \le b\), calculate the volume of water in the first tank after all operations are performed.
|
The first line contains three integers \(n, a\) and \(b\) (\(1 \le n \le 10^4\); \(1 \le a, b \le 1000\)) β the number of operations and the capacities of the tanks, respectively.The second line contains \(n\) integers \(v_1, v_2, \dots, v_n\) (\(-1000 \le v_i \le 1000\); \(v_i \neq 0\)) β the volume of water you try to pour in each operation.
|
For all pairs of the initial volumes of water \((c, d)\) such that \(0 \le c \le a\) and \(0 \le d \le b\), calculate the volume of water in the first tank after all operations are performed.Print \(a + 1\) lines, each line should contain \(b + 1\) integers. The \(j\)-th value in the \(i\)-th line should be equal to the answer for \(c = i - 1\) and \(d = j - 1\).
|
Consider \(c = 3\) and \(d = 2\) from the first example: The first operation tries to move \(2\) liters of water from the second tank to the first one, the second tank has \(2\) liters available, the first tank can fit \(1\) more liter. Thus, \(\min(2, 2, 1) = 1\) liter is moved, the first tank now contains \(4\) liters, the second tank now contains \(1\) liter. The second operation tries to move \(1\) liter of water from the first tank to the second one. \(\min(1, 4, 3) = 1\) liter is moved, the first tank now contains \(3\) liters, the second tank now contains \(2\) liter. The third operation tries to move \(2\) liter of water from the first tank to the second one. \(\min(2, 3, 2) = 2\) liters are moved, the first tank now contains \(1\) liter, the second tank now contains \(4\) liters. There's \(1\) liter of water in the first tank at the end. Thus, the third value in the fourth row is \(1\).
|
Input: 3 4 4 -2 1 2 | Output: 0 0 0 0 0 0 0 0 0 1 0 0 1 1 2 0 1 1 2 3 1 1 2 3 4
|
Expert
| 4 | 975 | 345 | 365 | 18 |
498 |
D
|
498D
|
D. Traffic Jams in the Land
| 2,400 |
data structures; dp; number theory
|
Some country consists of (n + 1) cities, located along a straight highway. Let's number the cities with consecutive integers from 1 to n + 1 in the order they occur along the highway. Thus, the cities are connected by n segments of the highway, the i-th segment connects cities number i and i + 1. Every segment of the highway is associated with a positive integer ai > 1 β the period of traffic jams appearance on it. In order to get from city x to city y (x < y), some drivers use the following tactics. Initially the driver is in city x and the current time t equals zero. Until the driver arrives in city y, he perfors the following actions: if the current time t is a multiple of ax, then the segment of the highway number x is now having traffic problems and the driver stays in the current city for one unit of time (formally speaking, we assign t = t + 1); if the current time t is not a multiple of ax, then the segment of the highway number x is now clear and that's why the driver uses one unit of time to move to city x + 1 (formally, we assign t = t + 1 and x = x + 1). You are developing a new traffic control system. You want to consecutively process q queries of two types: determine the final value of time t after the ride from city x to city y (x < y) assuming that we apply the tactics that is described above. Note that for each query t is being reset to 0. replace the period of traffic jams appearing on the segment number x by value y (formally, assign ax = y). Write a code that will effectively process the queries given above.
|
The first line contains a single integer n (1 β€ n β€ 105) β the number of highway segments that connect the n + 1 cities.The second line contains n integers a1, a2, ..., an (2 β€ ai β€ 6) β the periods of traffic jams appearance on segments of the highway.The next line contains a single integer q (1 β€ q β€ 105) β the number of queries to process.The next q lines contain the descriptions of the queries in the format c, x, y (c β the query type). If c is character 'A', then your task is to process a query of the first type. In this case the following constraints are satisfied: 1 β€ x < y β€ n + 1.If c is character 'C', then you need to process a query of the second type. In such case, the following constraints are satisfied: 1 β€ x β€ n, 2 β€ y β€ 6.
|
For each query of the first type output a single integer β the final value of time t after driving from city x to city y. Process the queries in the order in which they are given in the input.
|
Input: 102 5 3 2 3 5 3 4 2 410C 10 6A 2 6A 1 3C 3 4A 3 11A 4 9A 5 6C 7 3A 8 10A 2 5 | Output: 53146244
|
Expert
| 3 | 1,553 | 748 | 192 | 4 |
|
1,829 |
A
|
1829A
|
A. Love Story
| 800 |
implementation; strings
|
Timur loves codeforces. That's why he has a string \(s\) having length \(10\) made containing only lowercase Latin letters. Timur wants to know how many indices string \(s\) differs from the string ""codeforces"".For example string \(s =\) ""coolforsez"" differs from ""codeforces"" in \(4\) indices, shown in bold.Help Timur by finding the number of indices where string \(s\) differs from ""codeforces"".Note that you can't reorder the characters in the string \(s\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.Each test case is one line and contains the string \(s\), consisting of exactly \(10\) lowercase Latin characters.
|
For each test case, output a single integer β the number of indices where string \(s\) differs.
|
Input: 5coolforsezcadafurciecodeforcespaiuforcesforcescode | Output: 4 5 0 4 9
|
Beginner
| 2 | 469 | 213 | 95 | 18 |
|
551 |
E
|
551E
|
E. GukiZ and GukiZiana
| 2,500 |
binary search; data structures; implementation
|
Professor GukiZ was playing with arrays again and accidentally discovered new function, which he called GukiZiana. For given array a, indexed with integers from 1 to n, and number y, GukiZiana(a, y) represents maximum value of j - i, such that aj = ai = y. If there is no y as an element in a, then GukiZiana(a, y) is equal to - 1. GukiZ also prepared a problem for you. This time, you have two types of queries: First type has form 1 l r x and asks you to increase values of all ai such that l β€ i β€ r by the non-negative integer x. Second type has form 2 y and asks you to find value of GukiZiana(a, y). For each query of type 2, print the answer and make GukiZ happy!
|
The first line contains two integers n, q (1 β€ n β€ 5 * 105, 1 β€ q β€ 5 * 104), size of array a, and the number of queries. The second line contains n integers a1, a2, ... an (1 β€ ai β€ 109), forming an array a. Each of next q lines contain either four or two numbers, as described in statement:If line starts with 1, then the query looks like 1 l r x (1 β€ l β€ r β€ n, 0 β€ x β€ 109), first type query.If line starts with 2, then th query looks like 2 y (1 β€ y β€ 109), second type query.
|
For each query of type 2, print the value of GukiZiana(a, y), for y value for that query.
|
Input: 4 31 2 3 41 1 2 11 1 1 12 3 | Output: 2
|
Expert
| 3 | 670 | 481 | 89 | 5 |
|
1,742 |
E
|
1742E
|
E. Scuza
| 1,200 |
binary search; greedy; math
|
Timur has a stairway with \(n\) steps. The \(i\)-th step is \(a_i\) meters higher than its predecessor. The first step is \(a_1\) meters higher than the ground, and the ground starts at \(0\) meters. The stairs for the first test case. Timur has \(q\) questions, each denoted by an integer \(k_1, \dots, k_q\). For each question \(k_i\), you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length \(k_i\). Timur can only climb the \(j\)-th step if his legs are of length at least \(a_j\). In other words, \(k_i \geq a_j\) for each step \(j\) climbed.Note that you should answer each question independently.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The first line of each test case contains two integers \(n, q\) (\(1 \leq n, q \leq 2\cdot10^5\)) β the number of steps and the number of questions, respectively.The second line of each test case contains \(n\) integers (\(1 \leq a_i \leq 10^9\)) β the height of the steps.The third line of each test case contains \(q\) integers (\(0 \leq k_i \leq 10^9\)) β the numbers for each question.It is guaranteed that the sum of \(n\) does not exceed \(2\cdot10^5\), and the sum of \(q\) does not exceed \(2\cdot10^5\).
|
For each test case, output a single line containing \(q\) integers, the answer for each question.Please note, that the answer for some questions won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
|
Consider the first test case, pictured in the statement. If Timur's legs have length \(1\), then he can only climb stair \(1\), so the highest he can reach is \(1\) meter. If Timur's legs have length \(2\) or \(4\), then he can only climb stairs \(1\), \(2\), and \(3\), so the highest he can reach is \(1+2+1=4\) meters. If Timur's legs have length \(9\) or \(10\), then he can climb the whole staircase, so the highest he can reach is \(1+2+1+5=9\) meters. In the first question of the second test case, Timur has no legs, so he cannot go up even a single step. :(
|
Input: 34 51 2 1 51 2 4 9 102 21 10 13 11000000000 1000000000 10000000001000000000 | Output: 1 4 4 9 9 0 2 3000000000
|
Easy
| 3 | 659 | 610 | 282 | 17 |
313 |
B
|
313B
|
B. Ilya and Queries
| 1,100 |
dp; implementation
|
Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.You've got string s = s1s2... sn (n is the length of the string), consisting only of characters ""."" and ""#"" and m queries. Each query is described by a pair of integers li, ri (1 β€ li < ri β€ n). The answer to the query li, ri is the number of such integers i (li β€ i < ri), that si = si + 1.Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.
|
The first line contains string s of length n (2 β€ n β€ 105). It is guaranteed that the given string only consists of characters ""."" and ""#"".The next line contains integer m (1 β€ m β€ 105) β the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains integers li, ri (1 β€ li < ri β€ n).
|
Print m integers β the answers to the queries in the order in which they are given in the input.
|
Input: ......43 42 31 62 6 | Output: 1154
|
Easy
| 2 | 522 | 350 | 96 | 3 |
|
1,539 |
E
|
1539E
|
E. Game with Cards
| 2,500 |
binary search; constructive algorithms; data structures; dp; greedy; implementation
|
The Alice's computer is broken, so she can't play her favorite card game now. To help Alice, Bob wants to answer \(n\) her questions. Initially, Bob holds one card with number \(0\) in the left hand and one in the right hand. In the \(i\)-th question, Alice asks Bob to replace a card in the left or right hand with a card with number \(k_i\) (Bob chooses which of two cards he changes, Bob must replace exactly one card).After this action, Alice wants the numbers on the left and right cards to belong to given segments (segments for left and right cards can be different). Formally, let the number on the left card be \(x\), and on the right card be \(y\). Then after the \(i\)-th swap the following conditions must be satisfied: \(a_{l, i} \le x \le b_{l, i}\), and \(a_{r, i} \le y \le b_{r,i}\).Please determine if Bob can answer all requests. If it is possible, find a way to do it.
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 100\,000\), \(2 \le m \le 10^9\)) β the number of questions and the maximum possible value on the card.Then \(n\) queries are described. Every description contains 3 lines.The first line of the description of the \(i\)-th query contains a single integer \(k_i\) (\(0 \le k_i \le m\)) β the number on a new card.The second line of the description of the \(i\)-th query contains two integers \(a_{l, i}\) and \(b_{l, i}\) (\(0 \le a_{l, i} \le b_{l, i} \le m\)) β the minimum and maximum values of the card at the left hand after the replacement.The third line of the description of the \(i\)-th query contains two integers \(a_{r, i}\) and \(b_{r,i}\) (\(0 \le a_{r, i} \le b_{r,i} \le m\)) β the minimum and maximum values of the card at the right hand after the replacement.
|
At the first line, print ""Yes"", if Bob can answer all queries, and ""No"" otherwise.If Bob can answer all \(n\) queries, then at the second line print \(n\) numbers: a way to satisfy all requirements. If in \(i\)-th query Bob needs to replace the card in the left hand, print \(0\), otherwise print \(1\). If there are multiple answers, print any.
|
Input: 2 10 3 0 3 0 2 2 0 4 0 2 | Output: Yes 0 1
|
Expert
| 6 | 888 | 843 | 349 | 15 |
|
1,663 |
C
|
1663C
|
C. PΕja Verdon
| 0 |
*special; implementation; math
|
Aenar has an array \(a\) consisting of \(n\) integers.
|
The first line is the integer \(n\) (\(1 \le n \le 100\)).The second line contains \(n\) integers \(a_i\) (\(0 \le a_i \le 3979\)) β the contents of the array.
|
Output a single number.
|
Beginner
| 3 | 54 | 159 | 23 | 16 |
||
1,930 |
D2
|
1930D2
|
D2. Sum over all Substrings (Hard Version)
| 2,100 |
bitmasks; divide and conquer; dp; dsu; greedy; implementation; strings
|
This is the hard version of the problem. The only difference between the two versions is the constraint on \(t\) and \(n\). You can make hacks only if both versions of the problem are solved.For a binary\(^\dagger\) pattern \(p\) and a binary string \(q\), both of length \(m\), \(q\) is called \(p\)-good if for every \(i\) (\(1 \leq i \leq m\)), there exist indices \(l\) and \(r\) such that: \(1 \leq l \leq i \leq r \leq m\), and \(p_i\) is a mode\(^\ddagger\) of the string \(q_l q_{l+1} \ldots q_{r}\). For a pattern \(p\), let \(f(p)\) be the minimum possible number of \(\mathtt{1}\)s in a \(p\)-good binary string (of the same length as the pattern).You are given a binary string \(s\) of size \(n\). Find $$$\(\sum_{i=1}^{n} \sum_{j=i}^{n} f(s_i s_{i+1} \ldots s_j).\)\( In other words, you need to sum the values of \)f\( over all \)\frac{n(n+1)}{2}\( substrings of \)s\(.\)^\dagger\( A binary pattern is a string that only consists of characters \)\mathtt{0}\( and \)\mathtt{1}\(.\)^\ddagger\( Character \)c\( is a mode of string \)t\( of length \)m\( if the number of occurrences of \)c\( in \)t\( is at least \)\lceil \frac{m}{2} \rceil\(. For example, \)\mathtt{0}\( is a mode of \)\mathtt{010}\(, \)\mathtt{1}\( is not a mode of \)\mathtt{010}\(, and both \)\mathtt{0}\( and \)\mathtt{1}\( are modes of \)\mathtt{011010}$$$.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^5\)) β 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 10^6\)) β the length of the binary string \(s\).The second line of each test case contains a binary string \(s\) of length \(n\) consisting of only characters \(\mathtt{0}\) and \(\mathtt{1}\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output the sum of values of \(f\) over all substrings of \(s\).
|
In the first test case, the only \(\mathtt{1}\)-good string is \(\mathtt{1}\). Thus, \(f(\mathtt{1})=1\).In the second test case, \(f(\mathtt{10})=1\) because \(\mathtt{01}\) is \(\mathtt{10}\)-good, and \(\mathtt{00}\) is not \(\mathtt{10}\)-good. Thus, the answer is \(f(\mathtt{1})+f(\mathtt{10})+f(\mathtt{0}) = 1 + 1 + 0 = 2\).In the third test case, \(f\) equals to \(0\) for all \(1 \leq i \leq j \leq 5\). Thus, the answer is \(0\).
|
Input: 4112105000002011110110000000111111 | Output: 1 2 0 346
|
Hard
| 7 | 1,340 | 545 | 83 | 19 |
2,110 |
E
|
2110E
|
E. Melody
| 2,300 |
dfs and similar; graphs; implementation
|
In 2077, the robots that took over the world realized that human music wasn't that great, so they started composing their own.To write music, the robots have a special musical instrument capable of producing \(n\) different sounds. Each sound is characterized by its volume and pitch. A sequence of sounds is called music. Music is considered beautiful if any two consecutive sounds differ either only in volume or only in pitch. Music is considered boring if the volume or pitch of any three consecutive sounds is the same.You want to compose beautiful, non-boring music that contains each sound produced by your musical instrument exactly once.
|
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. In the first line of each test case, there is a number \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of sounds that the musical instrument can produce.Next, there are \(n\) lines, where the \(i\)-th line contains a pair of numbers \(v_i, p_i\) (\(1 \le v_i,\space p_i \le 10^9\)) β the volume and pitch of the \(i\)-th sound, respectively. It is guaranteed that among all \(n\) sounds, there are no duplicates, meaning for any \(i \neq j\), at least one of the conditions \(v_i \neq v_j\) or \(p_i \neq p_j\) holds.The sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, if it is possible to compose such music, output ""YES"", and on the next line, output \(n\) numbers β the indices of the sounds in the order that forms beautiful non-boring music. Otherwise, output ""NO"".You may output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer.
|
In the first test case, the music \((239,239)-(239,179)-(179,179)-(179,239)\) is suitable, contains all sounds, and all consecutive sounds differ either only in volume or only in pitch.In the second test case, it can be shown that there is no suitable music with the given sounds.
|
Input: 54179 239179 179239 179239 23931 12 13 115 751 11 22 12 299 9971 11 32 12 23 13 23 3 | Output: YES 4 3 2 1 NO YES 1 NO YES 3 4 6 7 2 1 5
|
Expert
| 3 | 646 | 753 | 392 | 21 |
758 |
D
|
758D
|
D. Ability To Convert
| 2,000 |
constructive algorithms; dp; greedy; math; strings
|
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter A he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475 = 1Β·162 + 13Β·161 + 11Β·160). Alexander lived calmly until he tried to convert the number back to the decimal number system.Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base n he will get the number k.
|
The first line contains the integer n (2 β€ n β€ 109). The second line contains the integer k (0 β€ k < 1060), it is guaranteed that the number k contains no more than 60 symbols. All digits in the second line are strictly less than n.Alexander guarantees that the answer exists and does not exceed 1018.The number k doesn't contain leading zeros.
|
Print the number x (0 β€ x β€ 1018) β the answer to the problem.
|
In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12Β·130 or 15 = 1Β·131 + 2Β·130.
|
Input: 1312 | Output: 12
|
Hard
| 5 | 635 | 344 | 62 | 7 |
489 |
D
|
489D
|
D. Unbearable Controversy of Being
| 1,700 |
brute force; combinatorics; dfs and similar; graphs
|
Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections a, b, c and d, such that there are two paths from a to c β one through b and the other one through d, he calls the group a ""damn rhombus"". Note that pairs (a, b), (b, c), (a, d), (d, c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below: Other roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a ""damn rhombus"" for him.Given that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of ""damn rhombi"" in the city.When rhombi are compared, the order of intersections b and d doesn't matter.
|
The first line of the input contains a pair of integers n, m (1 β€ n β€ 3000, 0 β€ m β€ 30000) β the number of intersections and roads, respectively. Next m lines list the roads, one per line. Each of the roads is given by a pair of integers ai, bi (1 β€ ai, bi β€ n;ai β bi) β the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.It is not guaranteed that you can get from any intersection to any other one.
|
Print the required number of ""damn rhombi"".
|
Input: 5 41 22 31 44 3 | Output: 1
|
Medium
| 4 | 1,095 | 533 | 45 | 4 |
|
1,038 |
C
|
1038C
|
C. Gambling
| 1,300 |
greedy; sortings
|
Two players A and B have a list of \(n\) integers each. They both want to maximize the subtraction between their score and their opponent's score. In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent's list (assuming his opponent's list is not empty).Note, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements \(\{1, 2, 2, 3\}\) in a list and you decided to choose \(2\) for the next turn, only a single instance of \(2\) will be deleted (and added to the score, if necessary). The player A starts the game and the game stops when both lists are empty. Find the difference between A's score and B's score at the end of the game, if both of the players are playing optimally.Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent's score, knowing that the opponent is doing the same.
|
The first line of input contains an integer \(n\) (\(1 \le n \le 100\,000\)) β the sizes of the list.The second line contains \(n\) integers \(a_i\) (\(1 \le a_i \le 10^6\)), describing the list of the player A, who starts the game.The third line contains \(n\) integers \(b_i\) (\(1 \le b_i \le 10^6\)), describing the list of the player B.
|
Output the difference between A's score and B's score (\(A-B\)) if both of them are playing optimally.
|
In the first example, the game could have gone as follows: A removes \(5\) from B's list. B removes \(4\) from A's list. A takes his \(1\). B takes his \(1\). Hence, A's score is \(1\), B's score is \(1\) and difference is \(0\).There is also another optimal way of playing: A removes \(5\) from B's list. B removes \(4\) from A's list. A removes \(1\) from B's list. B removes \(1\) from A's list. The difference in the scores is still \(0\).In the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be \(0\).
|
Input: 21 45 1 | Output: 0
|
Easy
| 2 | 1,246 | 341 | 102 | 10 |
2,070 |
C
|
2070C
|
C. Limited Repainting
| 1,500 |
binary search; greedy
|
You are given a strip, consisting of \(n\) cells, all cells are initially colored red.In one operation, you can choose a segment of consecutive cells and paint them blue. Before painting, the chosen cells can be either red or blue. Note that it is not possible to paint them red. You are allowed to perform at most \(k\) operations (possibly zero).For each cell, the desired color after all operations is specified: red or blue.It is clear that it is not always possible to satisfy all requirements within \(k\) operations. Therefore, for each cell, a penalty is also specified, which is applied if the cell ends up the wrong color after all operations. For the \(i\)-th cell, the penalty is equal to \(a_i\).The penalty of the final painting is calculated as the maximum penalty among all cells that are painted the wrong color. If there are no such cells, the painting penalty is equal to \(0\).What is the minimum penalty of the final painting that can be achieved?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5\); \(0 \le k \le n\)) β the length of the strip and the maximum number of operations.The second line contains a string \(s\), consisting of \(n\) characters 'R' and/or 'B'. 'R' means that the cell should be painted red. 'B' means that the cell should be painted blue.The third line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the penalty for each cell.The sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, print a single integer β the minimum penalty of the final painting.
|
In the first test case, you can paint the cells from \(1\) to \(3\). The painting will be BBBR. So, only cell \(2\) is painted the wrong color. Therefore, the penalty for it is the final penalty and equals \(3\).In the second test case, the painting BBBR will now result in a penalty of \(5\). However, if you paint the cells from \(1\) to \(1\), resulting in BRRR, then only cell \(3\) is painted the wrong color. The final penalty is \(3\).In the third test case, you can paint the cells from \(1\) to \(1\) and from \(3\) to \(3\). Then all cells will be the correct color, the penalty equals \(0\).
|
Input: 54 1BRBR9 3 5 44 1BRBR9 5 3 44 2BRBR9 3 5 410 2BRBRBBRRBR5 1 2 4 5 3 6 1 5 45 5RRRRR5 3 1 2 4 | Output: 3 3 0 4 0
|
Medium
| 2 | 968 | 650 | 87 | 20 |
1,310 |
F
|
1310F
|
F. Bad Cryptography
| 3,400 |
math; number theory
|
In modern cryptography much is tied to the algorithmic complexity of solving several problems. One of such problems is a discrete logarithm problem. It is formulated as follows: Let's fix a finite field and two it's elements \(a\) and \(b\). One need to fun such \(x\) that \(a^x = b\) or detect there is no such x. It is most likely that modern mankind cannot solve the problem of discrete logarithm for a sufficiently large field size. For example, for a field of residues modulo prime number, primes of 1024 or 2048 bits are considered to be safe. However, calculations with such large numbers can place a significant load on servers that perform cryptographic operations. For this reason, instead of a simple module residue field, more complex fields are often used. For such field no fast algorithms that use a field structure are known, smaller fields can be used and operations can be properly optimized. Developer Nikolai does not trust the generally accepted methods, so he wants to invent his own. Recently, he read about a very strange field β nimbers, and thinks it's a great fit for the purpose. The field of nimbers is defined on a set of integers from 0 to \(2^{2^k} - 1\) for some positive integer \(k\) . Bitwise exclusive or (\(\oplus\)) operation is used as addition. One of ways to define multiplication operation (\(\odot\)) is following properties: \(0 \odot a = a \odot 0 = 0\) \(1 \odot a = a \odot 1 = a\) \(a \odot b = b \odot a\) \(a \odot (b \odot c)= (a \odot b) \odot c\) \(a \odot (b \oplus c) = (a \odot b) \oplus (a \odot c)\) If \(a = 2^{2^n}\) for some integer \(n > 0\), and \(b < a\), then \(a \odot b = a \cdot b\). If \(a = 2^{2^n}\) for some integer \(n > 0\), then \(a \odot a = \frac{3}{2}\cdot a\). For example: \( 4 \odot 4 = 6\) \( 8 \odot 8 = 4 \odot 2 \odot 4 \odot 2 = 4 \odot 4 \odot 2 \odot 2 = 6 \odot 3 = (4 \oplus 2) \odot 3 = (4 \odot 3) \oplus (2 \odot (2 \oplus 1)) = (4 \odot 3) \oplus (2 \odot 2) \oplus (2 \odot 1) = 12 \oplus 3 \oplus 2 = 13.\) \(32 \odot 64 = (16 \odot 2) \odot (16 \odot 4) = (16 \odot 16) \odot (2 \odot 4) = 24 \odot 8 = (16 \oplus 8) \odot 8 = (16 \odot 8) \oplus (8 \odot 8) = 128 \oplus 13 = 141\) \(5 \odot 6 = (4 \oplus 1) \odot (4 \oplus 2) = (4\odot 4) \oplus (4 \odot 2) \oplus (4 \odot 1) \oplus (1 \odot 2) = 6 \oplus 8 \oplus 4 \oplus 2 = 8\) Formally, this algorithm can be described by following pseudo-code. multiply(a, b) { ans = 0 for p1 in bits(a) // numbers of bits of a equal to one for p2 in bits(b) // numbers of bits of b equal to one ans = ans xor multiply_powers_of_2(1 << p1, 1 << p2) return ans;}multiply_powers_of_2(a, b) { if (a == 1 or b == 1) return a * b n = maximal value, such 2^{2^{n}} <= max(a, b) power = 2^{2^{n}}; if (a >= power and b >= power) { return multiply(power * 3 / 2, multiply_powers_of_2(a / power, b / power)) } else if (a >= power) { return multiply_powers_of_2(a / power, b) * power } else { return multiply_powers_of_2(a, b / power) * power }}It can be shown, that this operations really forms a field. Moreover, than can make sense as game theory operations, but that's not related to problem much. With the help of appropriate caching and grouping of operations, it is possible to calculate the product quickly enough, which is important to improve speed of the cryptoalgorithm. More formal definitions as well as additional properties can be clarified in the wikipedia article at link. The authors of the task hope that the properties listed in the statement should be enough for the solution. Powering for such muliplication is defined in same way, formally \(a^{\odot k} = \underbrace{a \odot a \odot \cdots \odot a}_{k~\texttt{times}}\).You need to analyze the proposed scheme strength. For pairs of numbers \(a\) and \(b\) you need to find such \(x\), that \(a^{\odot x} = b\), or determine that it doesn't exist.
|
In the first line of input there is single integer \(t\) (\(1 \le t \le 100\)) β number of pairs, for which you need to find the discrete logarithm.In each of next \(t\) line there is a pair of integers \(a\) \(b\) (\(1 \le a, b < 2^{64}\)).
|
For each pair you should print one integer \(x\) (\(0 \le x < 2^{64}\)), such that \(a^{\odot x} = b\), or -1 if no such x exists. It can be shown, that if any such \(x\) exists, there is one inside given bounds. If there are several good values, you can output any of them.
|
Input: 7 2 2 1 1 2 3 8 10 8 2 321321321321 2 123214213213 4356903202345442785 | Output: 1 1 2 4 -1 6148914691236517205 68943624821423112
|
Master
| 2 | 3,854 | 241 | 274 | 13 |
|
1,571 |
I
|
1571I
|
I. Physical Examination
| 3,200 |
*special; binary search; data structures
|
Polycarp plans to undergo a full physical examination at his local clinic. There are \(n\) doctors, numbered from \(1\) to \(n\). The \(i\)-th doctor takes patients from minute \(L_i\) to minute \(R_i\), so Polycarp can visit him at any minute in this range. It takes each doctor exactly one minute to examine Polycarp's health.Polycarp wants to arrive at the clinic at some minute \(x\) and visit all \(n\) doctors in some order without waiting or visiting any doctor several times.More formally, he chooses an integer \(x\) and a permutation \(p_1, p_2, \dots, p_n\) (a sequence of \(n\) integers from \(1\) to \(n\) such that each integer appears exactly once), then proceeds to visit: doctor \(p_1\) at minute \(x\); doctor \(p_2\) at minute \(x+1\); ... doctor \(p_n\) at minute \(x+n-1\). The \(p_i\)-th doctor should be able to take patients at minute \(x+i-1\), so the following should hold: \(L[p_i] \le x + i - 1 \le R[p_i]\).Determine if it's possible for Polycarp to choose such a minute \(x\) and a permutation \(p\) that he'll be able to visit all \(n\) doctors in without waiting or visiting any doctor several times. If there are multiple answers, print any of them.
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of testcases.Then the descriptions of \(t\) testcases follow.The first line of the testcase contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of doctors.The second line of the testcase contains \(n\) integers \(L_1, L_2, \dots L_n\) (\(1 \le L_i \le 10^9\)).The third line of the testcase contains \(n\) integers \(R_1, R_2, \dots R_n\) (\(L_i \le R_i \le 10^9\)).The sum of \(n\) over all testcases doesn't exceed \(10^5\).
|
For each testcase print an answer.If there exists such a minute \(x\) and a permutation \(p\) that Polycarp is able to visit all \(n\) doctors without waiting or visiting any doctor several times, then print \(x\) in the first line and a permutation \(p\) in the second line. If there are multiple answers, print any of them.Otherwise, print \(-1\) in the only line.
|
In the third testcase it's impossible to visit all doctors, because Polycarp has to visit doctor \(2\) at minute \(2\) and doctor \(1\) at minute \(4\). However, that would require him to wait a minute between the visits, which is not allowed.In the fourth testcase all doctors take patients in the span of \(2\) minutes. However, since there are three of them, Polycarp can't visit them all.
|
Input: 5 3 2 3 1 3 3 2 8 6 6 5 4 9 4 3 6 7 6 10 6 9 6 6 8 2 4 2 4 2 3 2 2 2 3 3 3 1 5 10 | Output: 1 3 1 2 3 7 4 6 2 1 8 5 3 -1 -1 7 1
|
Master
| 3 | 1,182 | 524 | 366 | 15 |
1,915 |
G
|
1915G
|
G. Bicycles
| 1,800 |
graphs; greedy; implementation; shortest paths; sortings
|
All of Slavic's friends are planning to travel from the place where they live to a party using their bikes. And they all have a bike except Slavic. There are \(n\) cities through which they can travel. They all live in the city \(1\) and want to go to the party located in the city \(n\). The map of cities can be seen as an undirected graph with \(n\) nodes and \(m\) edges. Edge \(i\) connects cities \(u_i\) and \(v_i\) and has a length of \(w_i\).Slavic doesn't have a bike, but what he has is money. Every city has exactly one bike for sale. The bike in the \(i\)-th city has a slowness factor of \(s_{i}\). Once Slavic buys a bike, he can use it whenever to travel from the city he is currently in to any neighboring city, by taking \(w_i \cdot s_j\) time, considering he is traversing edge \(i\) using a bike \(j\) he owns.Slavic can buy as many bikes as he wants as money isn't a problem for him. Since Slavic hates traveling by bike, he wants to get from his place to the party in the shortest amount of time possible. And, since his informatics skills are quite rusty, he asks you for help.What's the shortest amount of time required for Slavic to travel from city \(1\) to city \(n\)? Slavic can't travel without a bike. It is guaranteed that it is possible for Slavic to travel from city \(1\) to any other city.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two space-separated integers \(n\) and \(m\) (\(2 \leq n \leq 1000\); \(n - 1 \leq m \leq 1000\)) β the number of cities and the number of roads, respectively.The \(i\)-th of the following \(m\) lines each contain three integers \(u_i\), \(v_i\), \(w_i\) (\(1 \le u_i, v_i \le n\), \(u_i \neq v_i\); \(1 \leq w_i \leq 10^5\)), denoting that there is a road between cities \(u_i\) and \(v_i\) of length \(w_i\). The same pair of cities can be connected by more than one road.The next line contains \(n\) integers \(s_1, \ldots, s_n\) (\(1 \leq s_i \leq 1000\)) β the slowness factor of each bike.The sum of \(n\) over all test cases does not exceed \(1000\), and the sum of \(m\) over all test cases does not exceed \(1000\).Additional constraint on the input: it is possible to travel from city \(1\) to any other city.
|
For each test case, output a single integer denoting the shortest amount of time Slavic can reach city \(n\) starting from city \(1\).
|
Input: 35 51 2 23 2 12 4 52 5 74 5 15 2 1 3 35 101 2 51 3 51 4 41 5 82 3 62 4 32 5 23 4 13 5 84 5 27 2 8 4 17 103 2 82 1 42 5 72 6 47 1 24 3 56 4 26 7 16 7 44 5 97 6 5 4 3 2 1 | Output: 19 36 14
|
Medium
| 5 | 1,324 | 1,002 | 134 | 19 |
|
1,089 |
F
|
1089F
|
F. Fractions
| 1,900 |
math
|
You are given a positive integer \(n\).Find a sequence of fractions \(\frac{a_i}{b_i}\), \(i = 1 \ldots k\) (where \(a_i\) and \(b_i\) are positive integers) for some \(k\) such that:$$$$$$ \begin{cases} \text{$b_i$ divides $n$, $1 < b_i < n$ for $i = 1 \ldots k$} \\ \text{$1 \le a_i < b_i$ for $i = 1 \ldots k$} \\ \text{$\sum\limits_{i=1}^k \frac{a_i}{b_i} = 1 - \frac{1}{n}$} \end{cases} $$$$$$
|
The input consists of a single integer \(n\) (\(2 \le n \le 10^9\)).
|
In the first line print ""YES"" if there exists such a sequence of fractions or ""NO"" otherwise.If there exists such a sequence, next lines should contain a description of the sequence in the following format.The second line should contain integer \(k\) (\(1 \le k \le 100\,000\)) β the number of elements in the sequence. It is guaranteed that if such a sequence exists, then there exists a sequence of length at most \(100\,000\).Next \(k\) lines should contain fractions of the sequence with two integers \(a_i\) and \(b_i\) on each line.
|
In the second example there is a sequence \(\frac{1}{2}, \frac{1}{3}\) such that \(\frac{1}{2} + \frac{1}{3} = 1 - \frac{1}{6}\).
|
Input: 2 | Output: NO
|
Hard
| 1 | 398 | 68 | 542 | 10 |
62 |
D
|
62D
|
D. Wormhouse
| 2,300 |
dfs and similar; graphs
|
Arnie the Worm has finished eating an apple house yet again and decided to move. He made up his mind on the plan, the way the rooms are located and how they are joined by corridors. He numbered all the rooms from 1 to n. All the corridors are bidirectional.Arnie wants the new house to look just like the previous one. That is, it should have exactly n rooms and, if a corridor from room i to room j existed in the old house, it should be built in the new one. We know that during the house constructing process Arnie starts to eat an apple starting from some room and only stops when he eats his way through all the corridors and returns to the starting room. It is also known that Arnie eats without stopping. That is, until Arnie finishes constructing the house, he is busy every moment of his time gnawing a new corridor. Arnie doesn't move along the already built corridors.However, gnawing out corridors in one and the same order any time you change a house is a very difficult activity. That's why Arnie, knowing the order in which the corridors were located in the previous house, wants to gnaw corridors in another order. It is represented as a list of rooms in the order in which they should be visited. The new list should be lexicographically smallest, but it also should be strictly lexicographically greater than the previous one. Help the worm.
|
The first line contains two integers n and m (3 β€ n β€ 100, 3 β€ m β€ 2000). It is the number of rooms and corridors in Arnie's house correspondingly. The next line contains m + 1 positive integers that do not exceed n. They are the description of Arnie's old path represented as a list of rooms he visited during the gnawing. It is guaranteed that the last number in the list coincides with the first one.The first room described in the list is the main entrance, that's why Arnie should begin gnawing from it.You may assume that there is no room which is connected to itself and there is at most one corridor between any pair of rooms. However, it is possible to find some isolated rooms which are disconnected from others.
|
Print m + 1 positive integers that do not exceed n. Those numbers are the description of the new path, according to which Arnie should gnaw out his new house. If it is impossible to find new path you should print out No solution. The first number in your answer should be equal to the last one. Also it should be equal to the main entrance.
|
Input: 3 31 2 3 1 | Output: 1 3 2 1
|
Expert
| 2 | 1,359 | 722 | 340 | 0 |
|
1,070 |
G
|
1070G
|
G. Monsters and Potions
| 2,300 |
brute force; dp; greedy; implementation
|
Polycarp is an introvert person. In fact he is so much of an introvert that he plays ""Monsters and Potions"" board game alone. The board of the game is a row of \(n\) cells. The cells are numbered from \(1\) to \(n\) from left to right. There are three types of cells: a cell containing a single monster, a cell containing a single potion or a blank cell (it contains neither a monster nor a potion).Polycarp has \(m\) tokens representing heroes fighting monsters, which are initially located in the blank cells \(s_1, s_2, \dots, s_m\). Polycarp's task is to choose a single cell (rally point) and one by one move all the heroes into this cell. A rally point can be a cell of any of three types.After Policarp selects a rally point, he picks a hero and orders him to move directly to the point. Once that hero reaches the point, Polycarp picks another hero and orders him also to go to the point. And so forth, until all the heroes reach the rally point cell. While going to the point, a hero can not deviate from the direct route or take a step back. A hero just moves cell by cell in the direction of the point until he reaches it. It is possible that multiple heroes are simultaneously in the same cell.Initially the \(i\)-th hero has \(h_i\) hit points (HP). Monsters also have HP, different monsters might have different HP. And potions also have HP, different potions might have different HP.If a hero steps into a cell which is blank (i.e. doesn't contain a monster/potion), hero's HP does not change.If a hero steps into a cell containing a monster, then the hero and the monster fight. If monster's HP is strictly higher than hero's HP, then the monster wins and Polycarp loses the whole game. If hero's HP is greater or equal to monster's HP, then the hero wins and monster's HP is subtracted from hero's HP. I.e. the hero survives if his HP drops to zero, but dies (and Polycarp looses) if his HP becomes negative due to a fight. If a hero wins a fight with a monster, then the monster disappears, and the cell becomes blank.If a hero steps into a cell containing a potion, then the hero drinks the potion immediately. As a result, potion's HP is added to hero's HP, the potion disappears, and the cell becomes blank.Obviously, Polycarp wants to win the game. It means that he must choose such rally point and the order in which heroes move, that every hero reaches the rally point and survives. I.e. Polycarp loses if a hero reaches rally point but is killed by a monster at the same time. Polycarp can use any of \(n\) cells as a rally point β initially it can contain a monster, a potion, or be a blank cell with or without a hero in it.Help Polycarp write a program to choose a rally point and the order in which heroes move.
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n \le 100\); \(1 \le m \le n\)) β length of the game board and the number of heroes on it.The following \(m\) lines describe heroes. Each line contains two integers \(s_i\) and \(h_i\) (\(1 \le s_i \le n\); \(1 \le h_i \le 10^6\)), where \(s_i\) is the initial position and \(h_i\) is the initial HP of the \(i\)-th hero. It is guaranteed that each cell \(s_i\) is blank. It is also guaranteed that all \(s_i\) are different. The following line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^6 \le a_j \le 10^6\)), where \(a_j\) describes the \(i\)-th cell of the game board: \(a_j=0\) means that the \(i\)-th cell is blank, \(a_j<0\) means that the \(i\)-th cell contains monster with positive HP of \(-a_j\), \(a_j>0\) means that the \(i\)-th cell contains potion with \(a_j\) HP.
|
On the first line of the output print the index of the rally point cell.On the second line print \(m\) integers β the order in which heroes should move to the rally point. Heroes are numbered from \(1\) to \(m\) in the order they are given in the input.If there are multiple solutions, print any of them.If it is impossible to find a rally point which can be reached by all heroes, print a single integer -1 in the output.
|
The picture illustrates the first example:
|
Input: 8 38 21 34 90 3 -5 0 -5 -4 -1 0 | Output: 63 1 2
|
Expert
| 4 | 2,742 | 866 | 422 | 10 |
1,582 |
F1
|
1582F1
|
F1. Korney Korneevich and XOR (easy version)
| 1,800 |
bitmasks; dp; greedy
|
This is an easier version of the problem with smaller constraints.Korney Korneevich dag up an array \(a\) of length \(n\). Korney Korneevich has recently read about the operation bitwise XOR, so he wished to experiment with it. For this purpose, he decided to find all integers \(x \ge 0\) such that there exists an increasing subsequence of the array \(a\), in which the bitwise XOR of numbers is equal to \(x\).It didn't take a long time for Korney Korneevich to find all such \(x\), and he wants to check his result. That's why he asked you to solve this problem!A sequence \(s\) is a subsequence of a sequence \(b\) if \(s\) can be obtained from \(b\) by deletion of several (possibly, zero or all) elements.A sequence \(s_1, s_2, \ldots , s_m\) is called increasing if \(s_1 < s_2 < \ldots < s_m\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 500\)) β the elements of the array \(a\).
|
In the first line print a single integer \(k\) β the number of found \(x\) values.In the second line print \(k\) integers in increasing order \(x_1, x_2, \ldots x_k\) (\(0 \le x_1 < \ldots < x_k\)) β found \(x\) values.
|
In the first test case: To get value \(x = 0\) it is possible to choose and empty subsequence To get value \(x = 2\) it is possible to choose a subsequence \([2]\) To get value \(x = 4\) it is possible to choose a subsequence \([4]\) To get value \(x = 6\) it is possible to choose a subsequence \([2, 4]\)
|
Input: 4 4 2 2 4 | Output: 4 0 2 4 6
|
Medium
| 3 | 803 | 194 | 219 | 15 |
1,495 |
A
|
1495A
|
A. Diamond Miner
| 1,200 |
geometry; greedy; math; sortings
|
Diamond Miner is a game that is similar to Gold Miner, but there are \(n\) miners instead of \(1\) in this game.The mining area can be described as a plane. The \(n\) miners can be regarded as \(n\) points on the y-axis. There are \(n\) diamond mines in the mining area. We can regard them as \(n\) points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point \((0, 0)\)). Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point \((a,b)\) uses his hook to mine a diamond mine at the point \((c,d)\), he will spend \(\sqrt{(a-c)^2+(b-d)^2}\) energy to mine it (the distance between these points). The miners can't move or help each other.The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1\le t\le 10\)) β 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 10^5\)) β the number of miners and mines.Each of the next \(2n\) lines contains two space-separated integers \(x\) (\(-10^8 \le x \le 10^8\)) and \(y\) (\(-10^8 \le y \le 10^8\)), which represent the point \((x,y)\) to describe a miner's or a diamond mine's position. Either \(x = 0\), meaning there is a miner at the point \((0, y)\), or \(y = 0\), meaning there is a diamond mine at the point \((x, 0)\). There can be multiple miners or diamond mines at the same point.It is guaranteed that no point is at the origin. It is guaranteed that the number of points on the x-axis is equal to \(n\) and the number of points on the y-axis is equal to \(n\).It's guaranteed that the sum of \(n\) for all test cases does not exceed \(10^5\).
|
For each test case, print a single real number β the minimal sum of energy that should be spent.Your answer is considered correct if its absolute or relative error does not exceed \(10^{-9}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}\).
|
In the first test case, the miners are at \((0,1)\) and \((0,-1)\), while the diamond mines are at \((1,0)\) and \((-2,0)\). If you arrange the miners to get the diamond mines in the way, shown in the picture, you can get the sum of the energy \(\sqrt2 + \sqrt5\).
|
Input: 3 2 0 1 1 0 0 -1 -2 0 4 1 0 3 0 -5 0 6 0 0 3 0 1 0 2 0 4 5 3 0 0 4 0 -3 4 0 2 0 1 0 -3 0 0 -10 0 -2 0 -10 | Output: 3.650281539872885 18.061819283610362 32.052255376143336
|
Easy
| 4 | 860 | 993 | 346 | 14 |
1,862 |
F
|
1862F
|
F. Magic Will Save the World
| 1,800 |
binary search; bitmasks; brute force; dp
|
A portal of dark forces has opened at the border of worlds, and now the whole world is under a terrible threat. To close the portal and save the world, you need to defeat \(n\) monsters that emerge from the portal one after another.Only the sorceress Vika can handle this. She possesses two magical powers β water magic and fire magic. In one second, Vika can generate \(w\) units of water mana and \(f\) units of fire mana. She will need mana to cast spells. Initially Vika have \(0\) units of water mana and \(0\) units of fire mana.Each of the \(n\) monsters that emerge from the portal has its own strength, expressed as a positive integer. To defeat the \(i\)-th monster with strength \(s_i\), Vika needs to cast a water spell or a fire spell of at least the same strength. In other words, Vika can spend at least \(s_i\) units of water mana on a water spell, or at least \(s_i\) units of fire mana on a fire spell.Vika can create and cast spells instantly. Vika can cast an unlimited number of spells every second as long she has enough mana for that.The sorceress wants to save the world as quickly as possible, so tell her how much time she will need.
|
Each test consists of several test cases. The first line of each test contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. This is followed by a description of the test cases.The first line of each test case contains two integers \(w\), \(f\) (\(1 \le w, f \le 10^9\)) β the amount of water and fire mana that Vika can generate per second.The second line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the number of monsters.The third line of each test case contains \(n\) integers \(s_1, s_2, s_3, \dots, s_n\) (\(1 \le s_i \le 10^4\)) β the strengths of the monsters.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(100\).
|
For each test case, output a single integer β the minimum time in seconds that Vika will need to defeat all the monsters.
|
In the first sample, after the first second, Vika can spend \(2\) units of fire mana to kill the first monster. Then she has \(2\) units of water mana and \(1\) unit of fire mana. After the third second, she will have \(6\) units of water mana and \(7\) units of fire mana at her disposal. This will be enough to immediately kill the second and third monsters.
|
Input: 42 332 6 737 58193190 90223 9713 4410 10 2 45 | Output: 3 2 1 5
|
Medium
| 4 | 1,159 | 708 | 121 | 18 |
1,488 |
J
|
1488J
|
J. Flower Shop
| 3,100 |
*special; data structures; fft; math
|
Your friend is running a flower shop. In order to be prepared for the next holidays (when, as usual, sales skyrocket) she asked you to write her a special program that will help to analyze the stocks she has.There are \(n\) different types of flowers she can order and each flower of the type \(i\) costs \(w_i\). The last holidays were a great success, she sold all flowers she had, so right now all her stocks are empty.From this point, she starts routine operations of ordering and selling flowers, while trying to analyze what she has at hand. All of this can be represented as \(m\) queries of three types: ""\(1\) \(i\) \(c\)"" β she bought \(c\) flowers of type \(i\); ""\(2\) \(i\) \(c\)"" β she disposed of \(c\) flowers of type \(i\); ""\(3\) \(l\) \(r\) \(k\)"" β how many variants of bouquets she can make using only flowers of types \(l, l + 1, \dots, r\) with the total cost no more than \(k\). For simplicity, you can think that a bouquet is a multiset of flowers, and two bouquets are different if they are different as multisets. The cost of a bouquet is the sum of all flowers it has. Help your friend and write the program that can process all these queries.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 1000\); \(1 \le m \le 1000\)) β the number of flower types and the number of queries.The second line contains \(n\) integers \(w_1, w_2, \dots, w_n\) (\(1 \le w_i \le 1000\)) β the cost of one flower of each type.The next \(m\) lines contains queries β one per line. Each query has one of three types: \(1\) \(i\) \(c\) (\(1 \le i \le n\); \(1 \le c \le 5000\)); \(2\) \(i\) \(c\) (\(1 \le i \le n\); \(1 \le c \le 5000\)). It's guaranteed that there are at least \(c\) flowers of type \(i\) at this moment; \(3\) \(l\) \(r\) \(k\) (\(1 \le l \le r \le n\); \(1 \le k \le 5000\)) It's guaranteed that the total cost of all flowers in stock after each query doesn't exceed \(5000\).
|
For each query of the third type, print how many variants of bouquets she can make using only flowers of types \(l, l + 1, \dots, r\) with the total cost no more than \(k\). Since the answer may be too large, print it modulo \(998\,244\,353\).
|
Input: 5 12 1 2 3 2 1 1 1 5 1 2 3 1 3 1 3 1 5 10 3 4 5 100 1 4 4 1 5 1 3 2 5 7 3 1 1 3 3 1 5 100 2 1 5 3 1 5 100 | Output: 40 0 28 3 479 79
|
Master
| 4 | 1,177 | 749 | 243 | 14 |
|
1,863 |
C
|
1863C
|
C. MEX Repetition
| 1,100 |
implementation; math
|
You are given an array \(a_1,a_2,\ldots, a_n\) of pairwise distinct integers from \(0\) to \(n\). Consider the following operation: consecutively for each \(i\) from \(1\) to \(n\) in this order, replace \(a_i\) with \(\operatorname{MEX}(a_1, a_2, \ldots, a_n)\). Here \(\operatorname{MEX}\) of a collection of integers \(c_1, c_2, \ldots, c_m\) is defined as the smallest non-negative integer \(x\) which does not occur in the collection \(c\). For example, \(\operatorname{MEX}(0, 2, 2, 1, 4) = 3\) and \(\operatorname{MEX}(1, 2) = 0\).Print the array after applying \(k\) such operations.
|
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 contains two integers \(n\) and \(k\) (\(1\le n\le 10^5\), \(1\le k\le 10^9\)).The second line contains \(n\) pairwise distinct integers \(a_1,a_2,\ldots, a_n\) (\(0\le a_i\le n\)) representing the elements of the array before applying the operations.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print all \(n\) elements of the array after applying \(k\) operations.
|
In the first test case, here is the entire process: On the first operation, the array changes from \([1]\) to \([0]\), since \(\operatorname{MEX}(1) = 0\). On the second operation, the array changes from \([0]\) to \([1]\), since \(\operatorname{MEX}(0) = 1\). Thus, the array becomes \([1]\) after two operations.In the second test case, the array changes as follows during one operation: \([{\mkern3mu\underline{\mkern-3mu {\bf 0}\mkern-3mu}\mkern3mu}, 1, 3] \rightarrow [2, {\mkern3mu\underline{\mkern-3mu {\bf 1}\mkern-3mu}\mkern3mu}, 3] \rightarrow [2, 0, {\mkern3mu\underline{\mkern-3mu {\bf 3}\mkern-3mu}\mkern3mu}] \rightarrow [2, 0, 1]\).In the third test case, the array changes as follows during one operation: \([{\mkern3mu\underline{\mkern-3mu {\bf 0}\mkern-3mu}\mkern3mu}, 2] \rightarrow [1, {\mkern3mu\underline{\mkern-3mu {\bf 2}\mkern-3mu}\mkern3mu}] \rightarrow [1, 0]\). And during the second operation: \([{\mkern3mu\underline{\mkern-3mu {\bf 1}\mkern-3mu}\mkern3mu}, 0] \rightarrow [2, {\mkern3mu\underline{\mkern-3mu {\bf 0}\mkern-3mu}\mkern3mu}] \rightarrow [2, 1]\).
|
Input: 51 213 10 1 32 20 25 51 2 3 4 510 1005 3 0 4 2 1 6 9 10 8 | Output: 1 2 0 1 2 1 2 3 4 5 0 7 5 3 0 4 2 1 6 9 10
|
Easy
| 2 | 591 | 529 | 90 | 18 |
995 |
D
|
995D
|
D. Game
| 2,500 |
math
|
Allen and Bessie are playing a simple number game. They both know a function \(f: \{0, 1\}^n \to \mathbb{R}\), i. e. the function takes \(n\) binary arguments and returns a real value. At the start of the game, the variables \(x_1, x_2, \dots, x_n\) are all set to \(-1\). Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an \(i\) such that \(x_i = -1\) and either setting \(x_i \to 0\) or \(x_i \to 1\).After \(n\) rounds all variables are set, and the game value resolves to \(f(x_1, x_2, \dots, x_n)\). Allen wants to maximize the game value, and Bessie wants to minimize it.Your goal is to help Allen and Bessie find the expected game value! They will play \(r+1\) times though, so between each game, exactly one value of \(f\) changes. In other words, between rounds \(i\) and \(i+1\) for \(1 \le i \le r\), \(f(z_1, \dots, z_n) \to g_i\) for some \((z_1, \dots, z_n) \in \{0, 1\}^n\). You are to find the expected game value in the beginning and after each change.
|
The first line contains two integers \(n\) and \(r\) (\(1 \le n \le 18\), \(0 \le r \le 2^{18}\)).The next line contains \(2^n\) integers \(c_0, c_1, \dots, c_{2^n-1}\) (\(0 \le c_i \le 10^9\)), denoting the initial values of \(f\). More specifically, \(f(x_0, x_1, \dots, x_{n-1}) = c_x\), if \(x = \overline{x_{n-1} \ldots x_0}\) in binary.Each of the next \(r\) lines contains two integers \(z\) and \(g\) (\(0 \le z \le 2^n - 1\), \(0 \le g \le 10^9\)). If \(z = \overline{z_{n-1} \dots z_0}\) in binary, then this means to set \(f(z_0, \dots, z_{n-1}) \to g\).
|
Print \(r+1\) lines, the \(i\)-th of which denotes the value of the game \(f\) during the \(i\)-th round. Your answer must have absolute or relative error within \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is considered correct if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}\).
|
Consider the second test case. If Allen goes first, he will set \(x_1 \to 1\), so the final value will be \(3\). If Bessie goes first, then she will set \(x_1 \to 0\) so the final value will be \(2\). Thus the answer is \(2.5\).In the third test case, the game value will always be \(1\) regardless of Allen and Bessie's play.
|
Input: 2 20 1 2 32 50 4 | Output: 1.5000002.2500003.250000
|
Expert
| 1 | 1,028 | 565 | 326 | 9 |
1,601 |
E
|
1601E
|
E. Phys Ed Online
| 2,900 |
data structures; dp; greedy
|
Students of one unknown college don't have PE courses. That's why \(q\) of them decided to visit a gym nearby by themselves. The gym is open for \(n\) days and has a ticket system. At the \(i\)-th day, the cost of one ticket is equal to \(a_i\). You are free to buy more than one ticket per day.You can activate a ticket purchased at day \(i\) either at day \(i\) or any day later. Each activated ticket is valid only for \(k\) days. In other words, if you activate ticket at day \(t\), it will be valid only at days \(t, t + 1, \dots, t + k - 1\). You know that the \(j\)-th student wants to visit the gym at each day from \(l_j\) to \(r_j\) inclusive. Each student will use the following strategy of visiting the gym at any day \(i\) (\(l_j \le i \le r_j\)): person comes to a desk selling tickets placed near the entrance and buy several tickets with cost \(a_i\) apiece (possibly, zero tickets); if the person has at least one activated and still valid ticket, they just go in. Otherwise, they activate one of tickets purchased today or earlier and go in. Note that each student will visit gym only starting \(l_j\), so each student has to buy at least one ticket at day \(l_j\).Help students to calculate the minimum amount of money they have to spend in order to go to the gym.
|
The first line contains three integers \(n\), \(q\) and \(k\) (\(1 \le n, q \le 300\,000\); \(1 \le k \le n\)) β the number of days, the number of students and the number of days each ticket is still valid. The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the cost of one ticket at the corresponding day.Each of the next \(q\) lines contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le n\)) β the segment of days the corresponding student want to visit the gym.
|
For each student, print the minimum possible amount of money they have to spend in order to go to the gym at desired days.
|
Let's see how each student have to spend their money: The first student should buy one ticket at day \(1\). The second student should buy one ticket at day \(3\) and two tickets at day \(4\). Note that student can keep purchased tickets for the next days. The third student should buy one ticket at day \(5\). The fourth student should buy one ticket at day \(7\). The fifth student should buy one ticket at day \(3\) and one at day \(4\).
|
Input: 7 5 2 2 15 6 3 7 5 6 1 2 3 7 5 5 7 7 3 5 | Output: 2 12 7 6 9
|
Master
| 3 | 1,283 | 518 | 122 | 16 |
232 |
E
|
232E
|
E. Quick Tortoise
| 3,000 |
bitmasks; divide and conquer; dp
|
John Doe has a field, which is a rectangular table of size n Γ m. We assume that the field rows are numbered from 1 to n from top to bottom, and the field columns are numbered from 1 to m from left to right. Then the cell of the field at the intersection of the x-th row and the y-th column has coordinates (x; y).We know that some cells of John's field are painted white, and some are painted black. Also, John has a tortoise, which can move along the white cells of the field. The tortoise can get from a white cell with coordinates (x; y) into cell (x + 1; y) or (x; y + 1), if the corresponding cell is painted white. In other words, the turtle can move only along the white cells of the field to the right or down. The turtle can not go out of the bounds of the field.In addition, John has q queries, each of them is characterized by four numbers x1, y1, x2, y2 (x1 β€ x2, y1 β€ y2). For each query John wants to know whether the tortoise can start from the point with coordinates (x1; y1), and reach the point with coordinates (x2; y2), moving only along the white squares of the field.
|
The first line contains two space-separated integers n and m (1 β€ n, m β€ 500) β the field sizes.Each of the next n lines contains m characters ""#"" and ""."": the j-th character of the i-th line equals ""#"", if the cell (i; j) is painted black and ""."", if it is painted white.The next line contains integer q (1 β€ q β€ 6Β·105) β the number of queries. Next q lines contain four space-separated integers x1, y1, x2 and y2 (1 β€ x1 β€ x2 β€ n, 1 β€ y1 β€ y2 β€ m) β the coordinates of the starting and the finishing cells. It is guaranteed that cells (x1; y1) and (x2; y2) are white.
|
For each of q queries print on a single line ""Yes"", if there is a way from cell (x1; y1) to cell (x2; y2), that meets the requirements, and ""No"" otherwise. Print the answers to the queries in the order, in which the queries are given in the input.
|
Input: 3 3....##.#.51 1 3 31 1 1 31 1 3 11 1 1 21 1 2 1 | Output: NoYesYesYesYes
|
Master
| 3 | 1,090 | 577 | 251 | 2 |
|
1,331 |
H
|
1331H
|
H. It's showtime
| 0 |
*special
|
You are given a mysterious language (codenamed ""UnknownX"") available in ""Custom Test"" tab. Find out what this language is, and use it to solve the following problem.You are given an integer \(input = 1000 * n + mod\) (\(1 \le n, mod \le 999\)). Calculate double factorial of \(n\) modulo \(mod\).
|
The input contains a single integer \(input\) (\(1001 \le input \le 999999\)). You are guaranteed that \(input \mod 1000 \neq 0\).
|
Output a single number.
|
In the first test case you need to calculate \(6!! \mod 100\); \(6!! = 6 * 4 * 2 = 48\).In the second test case you need to calculate \(9!! \mod 900\); \(9!! = 9 * 7 * 5 * 3 = 945\).In the third test case you need to calculate \(100!! \mod 2\); you can notice that \(100!!\) is a multiple of 100 and thus is divisible by 2.
|
Input: 6100 | Output: 48
|
Beginner
| 1 | 300 | 130 | 23 | 13 |
1,415 |
D
|
1415D
|
D. XOR-gun
| 2,000 |
bitmasks; brute force; constructive algorithms
|
Arkady owns a non-decreasing array \(a_1, a_2, \ldots, a_n\). You are jealous of its beauty and want to destroy this property. You have a so-called XOR-gun that you can use one or more times.In one step you can select two consecutive elements of the array, let's say \(x\) and \(y\), remove them from the array and insert the integer \(x \oplus y\) on their place, where \(\oplus\) denotes the bitwise XOR operation. Note that the length of the array decreases by one after the operation. You can't perform this operation when the length of the array reaches one.For example, if the array is \([2, 5, 6, 8]\), you can select \(5\) and \(6\) and replace them with \(5 \oplus 6 = 3\). The array becomes \([2, 3, 8]\).You want the array no longer be non-decreasing. What is the minimum number of steps needed? If the array stays non-decreasing no matter what you do, print \(-1\).
|
The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)) β the initial length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array. It is guaranteed that \(a_i \le a_{i + 1}\) for all \(1 \le i < n\).
|
Print a single integer β the minimum number of steps needed. If there is no solution, print \(-1\).
|
In the first example you can select \(2\) and \(5\) and the array becomes \([7, 6, 8]\).In the second example you can only obtain arrays \([1, 1]\), \([3, 3]\) and \([0]\) which are all non-decreasing.In the third example you can select \(1\) and \(2\) and the array becomes \([3, 4, 6, 20]\). Then you can, for example, select \(3\) and \(4\) and the array becomes \([7, 6, 20]\), which is no longer non-decreasing.
|
Input: 4 2 5 6 8 | Output: 1
|
Hard
| 3 | 877 | 292 | 99 | 14 |
1,463 |
E
|
1463E
|
E. Plan of Lectures
| 2,400 |
constructive algorithms; dfs and similar; dsu; graphs; implementation; sortings; trees
|
Ivan is a programming teacher. During the academic year, he plans to give \(n\) lectures on \(n\) different topics. Each topic should be used in exactly one lecture. Ivan wants to choose which topic will he explain during the \(1\)-st, \(2\)-nd, ..., \(n\)-th lecture β formally, he wants to choose some permutation of integers from \(1\) to \(n\) (let's call this permutation \(q\)). \(q_i\) is the index of the topic Ivan will explain during the \(i\)-th lecture.For each topic (except exactly one), there exists a prerequisite topic (for the topic \(i\), the prerequisite topic is \(p_i\)). Ivan cannot give a lecture on a topic before giving a lecture on its prerequisite topic. There exists at least one valid ordering of topics according to these prerequisite constraints.Ordering the topics correctly can help students understand the lectures better. Ivan has \(k\) special pairs of topics \((x_i, y_i)\) such that he knows that the students will understand the \(y_i\)-th topic better if the lecture on it is conducted right after the lecture on the \(x_i\)-th topic. Ivan wants to satisfy the constraints on every such pair, that is, for every \(i \in [1, k]\), there should exist some \(j \in [1, n - 1]\) such that \(q_j = x_i\) and \(q_{j + 1} = y_i\).Now Ivan wants to know if there exists an ordering of topics that satisfies all these constraints, and if at least one exists, find any of them.
|
The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 3 \cdot 10^5\), \(1 \le k \le n - 1\)) β the number of topics and the number of special pairs of topics, respectively.The second line contains \(n\) integers \(p_1\), \(p_2\), ..., \(p_n\) (\(0 \le p_i \le n\)), where \(p_i\) is the prerequisite topic for the topic \(i\) (or \(p_i = 0\) if the \(i\)-th topic has no prerequisite topics). Exactly one of these integers is \(0\). At least one ordering of topics such that for every \(i\) the \(p_i\)-th topic is placed before the \(i\)-th topic exists.Then \(k\) lines follow, the \(i\)-th line contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)) β the topics from the \(i\)-th special pair. All values of \(x_i\) are pairwise distinct; similarly, all valus of \(y_i\) are pairwise distinct.
|
If there is no ordering of topics meeting all the constraints, print \(0\).Otherwise, print \(n\) pairwise distinct integers \(q_1\), \(q_2\), ..., \(q_n\) (\(1 \le q_i \le n\)) β the ordering of topics meeting all of the constraints. If there are multiple answers, print any of them.
|
Input: 5 2 2 3 0 5 3 1 5 5 4 | Output: 3 2 1 5 4
|
Expert
| 7 | 1,408 | 842 | 284 | 14 |
|
98 |
A
|
98A
|
A. Help Victoria the Wise
| 1,700 |
brute force; implementation
|
Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that.The box's lock is constructed like that. The box itself is represented by an absolutely perfect black cube with the identical deepening on each face (those are some foreign nanotechnologies that the far away kingdom scientists haven't dreamt of). The box is accompanied by six gems whose form matches the deepenings in the box's faces. The box can only be opened after it is correctly decorated by the gems, that is, when each deepening contains exactly one gem. Two ways of decorating the box are considered the same if they can be obtained one from the other one by arbitrarily rotating the box (note that the box is represented by a perfect nanotechnological cube)Now Vasilisa the Wise wants to know by the given set of colors the following: in how many ways would she decorate the box in the worst case to open it? To answer this question it is useful to know that two gems of one color are indistinguishable from each other. Help Vasilisa to solve this challenging problem.
|
The first line contains exactly 6 characters without spaces from the set {R, O, Y, G, B, V} β they are the colors of gems with which the box should be decorated.
|
Print the required number of different ways to decorate the box.
|
Input: YYYYYY | Output: 1
|
Medium
| 2 | 1,267 | 161 | 64 | 0 |
|
656 |
B
|
656B
|
B. Scrambled
| 1,700 |
*special; implementation
|
Btoh yuo adn yuor roomatme lhoate wianshg disehs, btu stlil sdmoeboy msut peorrfm tihs cohre dialy. Oen dya yuo decdie to idourtcne smoe syestm. Yuor rmmotaoe sstgegus teh fooniwllg dael. Yuo argee on tow arayrs of ientgres M adn R, nmebur upmicnog dyas (induiclng teh cunrret oen) wtih sicsescuve irnegets (teh ceurrnt dya is zreo), adn yuo wsah teh diehss on dya D if adn olny if terhe etsixs an iednx i scuh taht D mod M[i] = R[i], otwsehrie yuor rmootmae deos it. Yuo lkie teh cncepot, btu yuor rmotaome's cuinnng simle meaks yuo ssecupt sthnoemig, so yuo itennd to vefriy teh fnerisas of teh aemnrgeet.Yuo aer geivn ayarrs M adn R. Cuaclatle teh pceanregte of dyas on wchih yuo edn up dnoig teh wisahng. Amsuse taht yuo hvae iiiftlneny mnay dyas aehad of yuo.
|
The first line of input contains a single integer N (1 β€ N β€ 16).The second and third lines of input contain N integers each, all between 0 and 16, inclusive, and represent arrays M and R, respectively. All M[i] are positive, for each i R[i] < M[i].
|
Output a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 4.
|
Input: 120 | Output: 0.500000
|
Medium
| 2 | 764 | 249 | 125 | 6 |
|
495 |
B
|
495B
|
B. Modular Equations
| 1,600 |
math; number theory
|
Last week, Hamed learned about a new type of equations in his math class called Modular Equations. Lets define i modulo j as the remainder of division of i by j and denote it by . A Modular Equation, as Hamed's teacher described, is an equation of the form in which a and b are two non-negative integers and x is a variable. We call a positive integer x for which a solution of our equation.Hamed didn't pay much attention to the class since he was watching a movie. He only managed to understand the definitions of these equations.Now he wants to write his math exercises but since he has no idea how to do that, he asked you for help. He has told you all he knows about Modular Equations and asked you to write a program which given two numbers a and b determines how many answers the Modular Equation has.
|
In the only line of the input two space-separated integers a and b (0 β€ a, b β€ 109) are given.
|
If there is an infinite number of answers to our equation, print ""infinity"" (without the quotes). Otherwise print the number of solutions of the Modular Equation .
|
In the first sample the answers of the Modular Equation are 8 and 16 since
|
Input: 21 5 | Output: 2
|
Medium
| 2 | 808 | 94 | 165 | 4 |
1,833 |
B
|
1833B
|
B. Restore the Weather
| 900 |
greedy; sortings
|
You are given an array \(a\) containing the weather forecast for Berlandia for the last \(n\) days. That is, \(a_i\) β is the estimated air temperature on day \(i\) (\(1 \le i \le n\)).You are also given an array \(b\) β the air temperature that was actually present on each of the days. However, all the values in array \(b\) are mixed up. Determine which day was which temperature, if you know that the weather never differs from the forecast by more than \(k\) degrees. In other words, if on day \(i\) the real air temperature was \(c\), then the equality \(|a_i - c| \le k\) is always true.For example, let an array \(a\) = [\(1, 3, 5, 3, 9\)] of length \(n = 5\) and \(k = 2\) be given and an array \(b\) = [\(2, 5, 11, 2, 4\)]. Then, so that the value of \(b_i\) corresponds to the air temperature on day \(i\), we can rearrange the elements of the array \(b\) so: [\(2, 2, 5, 4, 11\)]. Indeed: On the \(1\)st day, \(|a_1 - b_1| = |1 - 2| = 1\), \(1 \le 2 = k\) is satisfied; On the \(2\)nd day \(|a_2 - b_2| = |3 - 2| = 1\), \(1 \le 2 = k\) is satisfied; On the \(3\)rd day, \(|a_3 - b_3| = |5 - 5| = 0\), \(0 \le 2 = k\) is satisfied; On the \(4\)th day, \(|a_4 - b_4| = |3 - 4| = 1\), \(1 \le 2 = k\) is satisfied; On the \(5\)th day, \(|a_5 - b_5| = |9 - 11| = 2\), \(2 \le 2 = k\) is satisfied.
|
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 two integers \(n\) (\(1 \le n \le 10^5\)) and \(k\) (\(0 \le k \le10^9\)) β the number of days and the maximum difference between the expected and actual air temperature on each day.The second line of each test case contains exactly \(n\) integers β elements of array \(a\) (\(-10^9 \le a_i \le 10^9\)).The third line of each test case contains exactly \(n\) integers β elements of array \(b\) (\(-10^9 \le b_i \le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\), and that the elements of array \(b\) can always be rearranged so that the equality \(|a_i - b_i| \le k\) is true for all \(i\).
|
On a separate line for each test case, output exactly \(n\) numbers β the values of air temperature on each of the days in the correct order. If there is more than one answer β output any of them.
|
Input: 35 21 3 5 3 92 5 11 2 46 1-1 3 -2 0 -5 -1-4 0 -1 4 0 03 37 7 79 4 8 | Output: 2 2 5 4 11 0 4 -1 0 -4 0 8 4 9
|
Beginner
| 2 | 1,305 | 830 | 196 | 18 |
|
1,162 |
B
|
1162B
|
B. Double Matrix
| 1,400 |
brute force; greedy
|
You are given two \(n \times m\) matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix \(\begin{bmatrix} 9&10&11\\ 11&12&14\\ \end{bmatrix}\) is increasing because each individual row and column is strictly increasing. On the other hand, the matrix \(\begin{bmatrix} 1&1\\ 2&3\\ \end{bmatrix}\) is not increasing because the first row is not strictly increasing.Let a position in the \(i\)-th row (from top) and \(j\)-th column (from left) in a matrix be denoted as \((i, j)\). In one operation, you can choose any two numbers \(i\) and \(j\) and swap the number located in \((i, j)\) in the first matrix with the number in \((i, j)\) in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions.You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print ""Possible"", otherwise, print ""Impossible"".
|
The first line contains two integers \(n\) and \(m\) (\(1 \leq n,m \leq 50\)) β the dimensions of each matrix.Each of the next \(n\) lines contains \(m\) integers \(a_{i1}, a_{i2}, \ldots, a_{im}\) (\(1 \leq a_{ij} \leq 10^9\)) β the number located in position \((i, j)\) in the first matrix.Each of the next \(n\) lines contains \(m\) integers \(b_{i1}, b_{i2}, \ldots, b_{im}\) (\(1 \leq b_{ij} \leq 10^9\)) β the number located in position \((i, j)\) in the second matrix.
|
Print a string ""Impossible"" or ""Possible"".
|
The first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be \(\begin{bmatrix} 9&10\\ 11&12\\ \end{bmatrix}\) and \(\begin{bmatrix} 2&4\\ 3&5\\ \end{bmatrix}\).In the second example, we don't need to do any operations.In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
|
Input: 2 2 2 10 11 5 9 4 3 12 | Output: Possible
|
Easy
| 2 | 1,357 | 475 | 46 | 11 |
1,030 |
G
|
1030G
|
G. Linear Congruential Generator
| 2,900 |
number theory
|
You are given a tuple generator \(f^{(k)} = (f_1^{(k)}, f_2^{(k)}, \dots, f_n^{(k)})\), where \(f_i^{(k)} = (a_i \cdot f_i^{(k - 1)} + b_i) \bmod p_i\) and \(f^{(0)} = (x_1, x_2, \dots, x_n)\). Here \(x \bmod y\) denotes the remainder of \(x\) when divided by \(y\). All \(p_i\) are primes.One can see that with fixed sequences \(x_i\), \(y_i\), \(a_i\) the tuples \(f^{(k)}\) starting from some index will repeat tuples with smaller indices. Calculate the maximum number of different tuples (from all \(f^{(k)}\) for \(k \ge 0\)) that can be produced by this generator, if \(x_i\), \(a_i\), \(b_i\) are integers in the range \([0, p_i - 1]\) and can be chosen arbitrary. The answer can be large, so print the remainder it gives when divided by \(10^9 + 7\)
|
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in the tuple.The second line contains \(n\) space separated prime numbers β the modules \(p_1, p_2, \ldots, p_n\) (\(2 \le p_i \le 2 \cdot 10^6\)).
|
Print one integer β the maximum number of different tuples modulo \(10^9 + 7\).
|
In the first example we can choose next parameters: \(a = [1, 1, 1, 1]\), \(b = [1, 1, 1, 1]\), \(x = [0, 0, 0, 0]\), then \(f_i^{(k)} = k \bmod p_i\).In the second example we can choose next parameters: \(a = [1, 1, 2]\), \(b = [1, 1, 0]\), \(x = [0, 0, 1]\).
|
Input: 42 3 5 7 | Output: 210
|
Master
| 1 | 757 | 245 | 79 | 10 |
331 |
D3
|
331D3
|
D3. Escaping on Beaveractor
| 3,000 |
data structures; implementation; trees
|
Don't put up with what you're sick of! The Smart Beaver decided to escape from the campus of Beaver Science Academy (BSA). BSA is a b Γ b square on a plane. Each point x, y (0 β€ x, y β€ b) belongs to BSA. To make the path quick and funny, the Beaver constructed a Beaveractor, an effective and comfortable types of transport.The campus obeys traffic rules: there are n arrows, parallel to the coordinate axes. The arrows do not intersect and do not touch each other. When the Beaveractor reaches some arrow, it turns in the arrow's direction and moves on until it either reaches the next arrow or gets outside the campus. The Beaveractor covers exactly one unit of space per one unit of time. You can assume that there are no obstacles to the Beaveractor.The BSA scientists want to transport the brand new Beaveractor to the ""Academic Tractor"" research institute and send the Smart Beaver to do his postgraduate studies and sharpen pencils. They have q plans, representing the Beaveractor's initial position (xi, yi), the initial motion vector wi and the time ti that have passed after the escape started.Your task is for each of the q plans to determine the Smart Beaver's position after the given time.
|
The first line contains two integers: the number of traffic rules n and the size of the campus b, 0 β€ n, 1 β€ b. Next n lines contain the rules. Each line of the rules contains four space-separated integers x0, y0, x1, y1 β the beginning and the end of the arrow. It is guaranteed that all arrows are parallel to the coordinate axes and have no common points. All arrows are located inside the campus, that is, 0 β€ x0, y0, x1, y1 β€ b holds.Next line contains integer q β the number of plans the scientists have, 1 β€ q β€ 105. The i-th plan is represented by two integers, xi, yi are the Beaveractor's coordinates at the initial time, 0 β€ xi, yi β€ b, character wi, that takes value U, D, L, R and sets the initial direction up, down, to the left or to the right correspondingly (the Y axis is directed upwards), and ti β the time passed after the escape started, 0 β€ ti β€ 1015. to get 30 points you need to solve the problem with constraints n, b β€ 30 (subproblem D1); to get 60 points you need to solve the problem with constraints n, b β€ 1000 (subproblems D1+D2); to get 100 points you need to solve the problem with constraints n, b β€ 105 (subproblems D1+D2+D3).
|
Print q lines. Each line should contain two integers β the Beaveractor's coordinates at the final moment of time for each plan. If the Smart Beaver manages to leave the campus in time ti, print the coordinates of the last point in the campus he visited.
|
Input: 3 30 0 0 10 2 2 23 3 2 3120 0 L 00 0 L 10 0 L 20 0 L 30 0 L 40 0 L 50 0 L 62 0 U 22 0 U 33 0 U 51 3 D 21 3 R 2 | Output: 0 00 10 21 22 23 23 22 23 21 32 21 3
|
Master
| 3 | 1,205 | 1,162 | 253 | 3 |
|
1,850 |
F
|
1850F
|
F. We Were Both Children
| 1,300 |
brute force; implementation; math; number theory
|
Mihai and Slavic were looking at a group of \(n\) frogs, numbered from \(1\) to \(n\), all initially located at point \(0\). Frog \(i\) has a hop length of \(a_i\). Each second, frog \(i\) hops \(a_i\) units forward. Before any frogs start hopping, Slavic and Mihai can place exactly one trap in a coordinate in order to catch all frogs that will ever pass through the corresponding coordinate.However, the children can't go far away from their home so they can only place a trap in the first \(n\) points (that is, in a point with a coordinate between \(1\) and \(n\)) and the children can't place a trap in point \(0\) since they are scared of frogs.Can you help Slavic and Mihai find out what is the maximum number of frogs they can catch using a trap?
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of frogs, which equals the distance Slavic and Mihai can travel to place a trap.The second line of each test case contains \(n\) integers \(a_1, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the lengths of the hops of the corresponding frogs.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output a single integer β the maximum number of frogs Slavic and Mihai can catch using a trap.
|
In the first test case, the frogs will hop as follows: Frog 1: \(0 \to 1 \to 2 \to 3 \to \mathbf{\color{red}{4}} \to \cdots\) Frog 2: \(0 \to 2 \to \mathbf{\color{red}{4}} \to 6 \to 8 \to \cdots\) Frog 3: \(0 \to 3 \to 6 \to 9 \to 12 \to \cdots\) Frog 4: \(0 \to \mathbf{\color{red}{4}} \to 8 \to 12 \to 16 \to \cdots\) Frog 5: \(0 \to 5 \to 10 \to 15 \to 20 \to \cdots\) Therefore, if Slavic and Mihai put a trap at coordinate \(4\), they can catch three frogs: frogs 1, 2, and 4. It can be proven that they can't catch any more frogs.In the second test case, Slavic and Mihai can put a trap at coordinate \(2\) and catch all three frogs instantly.
|
Input: 751 2 3 4 532 2 263 1 3 4 9 1091 3 2 4 2 3 7 8 511087 11 6 8 12 4 4 8109 11 9 12 1 7 2 5 8 10 | Output: 3 3 3 5 0 4 4
|
Easy
| 4 | 755 | 590 | 113 | 18 |
747 |
D
|
747D
|
D. Winter Is Coming
| 1,800 |
dp; greedy; sortings
|
The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days.Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day.Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.
|
The first line contains two positive integers n and k (1 β€ n β€ 2Β·105, 0 β€ k β€ n) β the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total.The second line contains a sequence of n integers t1, t2, ..., tn ( - 20 β€ ti β€ 20) β the average air temperature in the i-th winter day.
|
Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.
|
In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
|
Input: 4 3-5 20 -3 0 | Output: 2
|
Medium
| 3 | 992 | 387 | 163 | 7 |
1,758 |
A
|
1758A
|
A. SSeeeeiinngg DDoouubbllee
| 800 |
constructive algorithms; strings
|
A palindrome is a string that reads the same backward as forward. For example, the strings \(\texttt{z}\), \(\texttt{aaa}\), \(\texttt{aba}\), and \(\texttt{abccba}\) are palindromes, but \(\texttt{codeforces}\) and \(\texttt{ab}\) are not.The double of a string \(s\) is obtained by writing each character twice. For example, the double of \(\texttt{seeing}\) is \(\texttt{sseeeeiinngg}\).Given a string \(s\), rearrange its double to form a palindrome. Output the rearranged string. It can be proven that such a rearrangement always exists.
|
The first line of input contains \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The only line of each test case contains a single string \(s\) (\(1 \leq |s| \leq 100\)) consisting only of lowercase English letters.Note that the sum of \(|s|\) over all test cases is not bounded.
|
For each test case, output a palindromic string of length \(2 \cdot |s|\) that is a rearrangement of the double of \(s\).
|
In the first test case, the double of \(\texttt{a}\) is \(\texttt{aa}\), which is already a palindrome.In the second test case, the double of \(\texttt{sururu}\) is \(\texttt{ssuurruurruu}\). If we move the first \(\texttt{s}\) to the end, we get \(\texttt{suurruurruus}\), which is a palindrome.In the third test case, the double of \(\texttt{errorgorn}\) is \(\texttt{eerrrroorrggoorrnn}\). We can rearrange the characters to form \(\texttt{rgnororerrerorongr}\), which is a palindrome.
|
Input: 4asururuerrorgornanutforajaroftuna | Output: aa suurruurruus rgnororerrerorongr aannuuttffoorraajjaarrooffttuunnaa
|
Beginner
| 2 | 542 | 289 | 121 | 17 |
1,203 |
C
|
1203C
|
C. Common Divisors
| 1,300 |
implementation; math
|
You are given an array \(a\) consisting of \(n\) integers.Your task is to say the number of such positive integers \(x\) such that \(x\) divides each number from the array. In other words, you have to find the number of common divisors of all elements in the array.For example, if the array \(a\) will be \([2, 4, 6, 2, 10]\), then \(1\) and \(2\) divide each number from the array (so the answer for this test is \(2\)).
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 4 \cdot 10^5\)) β the number of elements in \(a\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{12}\)), where \(a_i\) is the \(i\)-th element of \(a\).
|
Print one integer β the number of such positive integers \(x\) such that \(x\) divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array).
|
Input: 5 1 2 3 4 5 | Output: 1
|
Easy
| 2 | 421 | 274 | 211 | 12 |
|
417 |
E
|
417E
|
E. Square Table
| 2,400 |
constructive algorithms; math; probabilities
|
While resting on the ship after the ""Russian Code Cup"" a boy named Misha invented an interesting game. He promised to give his quadrocopter to whoever will be the first one to make a rectangular table of size n Γ m, consisting of positive integers such that the sum of the squares of numbers for each row and each column was also a square.Since checking the correctness of the table manually is difficult, Misha asks you to make each number in the table to not exceed 108.
|
The first line contains two integers n and m (1 β€ n, m β€ 100) β the size of the table.
|
Print the table that meets the condition: n lines containing m integers, separated by spaces. If there are multiple possible answers, you are allowed to print anyone. It is guaranteed that there exists at least one correct answer.
|
Input: 1 1 | Output: 1
|
Expert
| 3 | 474 | 86 | 230 | 4 |
|
1 |
B
|
1B
|
B. Spreadsheets
| 1,600 |
implementation; math
|
In the popular spreadsheets systems (for example, in Excel) the following numeration of columns is used. The first column has number A, the second β number B, etc. till column 26 that is marked by Z. Then there are two-letter numbers: column 27 has number AA, 28 β AB, column 52 is marked by AZ. After ZZ there follow three-letter numbers, etc.The rows are marked by integer numbers starting with 1. The cell name is the concatenation of the column and the row numbers. For example, BC23 is the name for the cell that is in column 55, row 23. Sometimes another numeration system is used: RXCY, where X and Y are integer numbers, showing the column and the row numbers respectfully. For instance, R23C55 is the cell from the previous example.Your task is to write a program that reads the given sequence of cell coordinates and produce each item written according to the rules of another numeration system.
|
The first line of the input contains integer number n (1 β€ n β€ 105), the number of coordinates in the test. Then there follow n lines, each of them contains coordinates. All the coordinates are correct, there are no cells with the column and/or the row numbers larger than 106 .
|
Write n lines, each line should contain a cell coordinates in the other numeration system.
|
Input: 2R23C55BC23 | Output: BC23R23C55
|
Medium
| 2 | 905 | 278 | 90 | 0 |
|
2,043 |
E
|
2043E
|
E. Matrix Transformation
| 2,300 |
bitmasks; brute force; data structures; dfs and similar; graphs; greedy; implementation
|
You are given two matrices \(A\) and \(B\) of size \(n \times m\), filled with integers between \(0\) and \(10^9\). You can perform the following operations on matrix \(A\) in any order and any number of times: &=: choose two integers \(i\) and \(x\) (\(1 \le i \le n\), \(x \ge 0\)) and replace each element in row \(i\) with the result of the bitwise AND operation between \(x\) and that element. Formally, for every \(j \in [1, m]\), the element \(A_{i,j}\) is replaced with \(A_{i,j} \text{ & } x\); |=: choose two integers \(j\) and \(x\) (\(1 \le j \le m\), \(x \ge 0\)) and replace each element in column \(j\) with the result of the bitwise OR operation between \(x\) and that element. Formally, for every \(i \in [1, n]\), the element \(A_{i,j}\) is replaced with \(A_{i,j} \text{ | } x\). The value of \(x\) may be chosen differently for different operations.Determine whether it is possible to transform matrix \(A\) into matrix \(B\) using the given operations any number of times (including zero).
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Then, \(t\) test cases follow.Each test case is given as follows: the first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^3\); \(n \cdot m \le 10^3\)) β the dimensions of the matrices \(A\) and \(B\); the following \(n\) lines describe the matrix \(A\), where the \(i\)-th line contains \(m\) integers \(A_{i,1}, A_{i,2}, \dots, A_{i,m}\) (\(0 \le A_{i,j} \le 10^9\)); the following \(n\) lines describe the matrix \(B\), where the \(i\)-th line contains \(m\) integers \(B_{i,1}, B_{i,2}, \dots, B_{i,m}\) (\(0 \le B_{i,j} \le 10^9\)).
|
For each test case, output Yes if it is possible to transform the matrix \(A\) into the matrix \(B\); otherwise, output No. Each letter can be output in any case, upper or lower.
|
Let's consider the second set of input data and show a sequence of operations that transforms matrix \(A\) into matrix \(B\):Initially, the matrix looks like this:\(\begin{bmatrix} 10&10\\ 42&42\\ \end{bmatrix}\)Apply an operation of the first type with parameters \(i = 1\) and \(x = 0\). As a result, we get the matrix:\(\begin{bmatrix} 0&0\\ 42&42\\ \end{bmatrix}\)Apply an operation of the first type with parameters \(i = 2\) and \(x = 0\). As a result, we get the matrix:\(\begin{bmatrix} 0&0\\ 0&0\\ \end{bmatrix}\)Apply an operation of the second type with parameters \(j = 1\) and \(x = 21\). As a result, we get the matrix:\(\begin{bmatrix} 21&0\\ 21&0\\ \end{bmatrix}\)Apply an operation of the second type with parameters \(j = 2\) and \(x = 21\). As a result, we get the matrix:\(\begin{bmatrix} 21&21\\ 21&21\\ \end{bmatrix}\)Thus, we have transformed matrix \(A\) into matrix \(B\).
|
Input: 41 112132 210 1042 4221 2121 212 274 1042 10621 8585 212 41 2 3 45 6 7 83 2 3 41 0 1 0 | Output: Yes Yes No Yes
|
Expert
| 7 | 1,010 | 651 | 178 | 20 |
1,899 |
E
|
1899E
|
E. Queue Sort
| 1,300 |
greedy; implementation; sortings
|
Vlad found an array \(a\) of \(n\) integers and decided to sort it in non-decreasing order.To do this, Vlad can apply the following operation any number of times: Extract the first element of the array and insert it at the end; Swap that element with the previous one until it becomes the first or until it becomes strictly greater than the previous one. Note that both actions are part of the operation, and for one operation, you must apply both actions.For example, if you apply the operation to the array [\(4, 3, 1, 2, 6, 4\)], it will change as follows: [\(\color{red}{4}, 3, 1, 2, 6, 4\)]; [\(3, 1, 2, 6, 4, \color{red}{4}\)]; [\(3, 1, 2, 6, \color{red}{4}, 4\)]; [\(3, 1, 2, \color{red}{4}, 6, 4\)].Vlad doesn't have time to perform all the operations, so he asks you to determine the minimum number of operations required to sort the array or report that it is impossible.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Descriptions of the test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, a_3, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array.It is guaranteed that the sum of \(n\) over all testcases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of operations needed to sort the array. If it is impossible to do so, output \(-1\) as the answer.
|
Input: 556 4 1 2 574 5 3 7 8 6 264 3 1 2 6 445 2 4 232 2 3 | Output: 2 6 -1 -1 0
|
Easy
| 3 | 881 | 503 | 163 | 18 |
|
78 |
B
|
78B
|
B. Easter Eggs
| 1,200 |
constructive algorithms; implementation
|
The Easter Rabbit laid n eggs in a circle and is about to paint them. Each egg should be painted one color out of 7: red, orange, yellow, green, blue, indigo or violet. Also, the following conditions should be satisfied: Each of the seven colors should be used to paint at least one egg. Any four eggs lying sequentially should be painted different colors. Help the Easter Rabbit paint the eggs in the required manner. We know that it is always possible.
|
The only line contains an integer n β the amount of eggs (7 β€ n β€ 100).
|
Print one line consisting of n characters. The i-th character should describe the color of the i-th egg in the order they lie in the circle. The colors should be represented as follows: ""R"" stands for red, ""O"" stands for orange, ""Y"" stands for yellow, ""G"" stands for green, ""B"" stands for blue, ""I"" stands for indigo, ""V"" stands for violet.If there are several answers, print any of them.
|
The way the eggs will be painted in the first sample is shown on the picture:
|
Input: 8 | Output: ROYGRBIV
|
Easy
| 2 | 454 | 71 | 402 | 0 |
1,628 |
F
|
1628F
|
F. Spaceship Crisis Management
| 3,500 |
binary search; data structures; geometry; sortings
|
NASA (Norwegian Astronaut Stuff Association) is developing a new steering system for spaceships. But in its current state, it wouldn't be very safe if the spaceship would end up in a bunch of space junk. To make the steering system safe, they need to answer the following:Given the target position \(t = (0, 0)\), a set of \(n\) pieces of space junk \(l\) described by line segments \(l_i = ((a_{ix}, a_{iy}), (b_{ix}, b_{iy}))\), and a starting position \(s = (s_x, s_y)\), is there a direction such that floating in that direction from the starting position would lead to the target position?When the spaceship hits a piece of space junk, what happens depends on the absolute difference in angle between the floating direction and the line segment, \(\theta\): If \(\theta < 45^{\circ}\), the spaceship slides along the piece of space junk in the direction that minimizes the change in angle, and when the spaceship slides off the end of the space junk, it continues floating in the direction it came in (before hitting the space junk). If \(\theta \ge 45^{\circ}\), the spaceship stops, because there is too much friction to slide along the space junk. You are only given the set of pieces of space junk once, and the target position is always \((0, 0)\), but there are \(q\) queries, each with a starting position \(s_j = (s_{jx}, s_{jy})\).Answer the above question for each query.
|
The first line contains the the integer \(n\) (\(1 \le n \le 1500\)).Then follows \(n\) lines, the \(i\)-th of which containing the \(4\) integers \(a_{ix}\), \(a_{iy}\), \(b_{ix}\), and \(b_{iy}\) (\(|a_{ix}|, |a_{iy}|, |b_{ix}|, |b_{iy}| \le 1000\)).Then follows a line containing the integer \(q\) (\(1 \le q \le 1000\)).Then follows \(q\) lines, the \(j\)-th of which containing the \(2\) integers \(s_{jx}\) and \(s_{jy}\) (\(|s_{jx}|, |s_{jy}| \le 1000\)).It is guaranteed that none of the segments in \(l\) cross or touch, that \(t\) is not on any segment in \(l\), that \(s_j\) is not on any segment in \(l\), and that \(s \neq t\).
|
For each query \(s_j\) print an answer. If there exists a direction such that floating from \(s_j\) in that direction, possibly sliding along some pieces of space junk, leads to \(t\), print ""YES"". Otherwise, print ""NO"" (case insensitive).
|
The blue cross represents the target location, and the other blue line segments represent the space junk.Green dots represent starting locations where the answer is yes, and red dots represent starting locations where the answer is no.The yellow lines are possible paths to the target location for the \(3\)rd and \(14\)th queries. The black line is a possible path from the starting location in the \(6\)th query, but it barely misses the target location.
|
Input: 3 0 1 2 4 1 3 -1 6 0 -1 1 -1 14 -2 10 -1 10 0 10 1 10 2 10 3 10 4 10 5 10 6 10 -1 -2 0 -2 1 -2 2 -2 3 -2 | Output: YES YES YES YES YES NO NO NO YES YES NO NO NO YES
|
Master
| 4 | 1,386 | 640 | 243 | 16 |
57 |
A
|
57A
|
A. Square Earth?
| 1,300 |
dfs and similar; greedy; implementation
|
Meg the Rabbit decided to do something nice, specifically β to determine the shortest distance between two points on the surface of our planet. But Meg... what can you say, she wants everything simple. So, she already regards our planet as a two-dimensional circle. No, wait, it's even worse β as a square of side n. Thus, the task has been reduced to finding the shortest path between two dots on a square (the path should go through the square sides). To simplify the task let us consider the vertices of the square to lie at points whose coordinates are: (0, 0), (n, 0), (0, n) and (n, n).
|
The single line contains 5 space-separated integers: n, x1, y1, x2, y2 (1 β€ n β€ 1000, 0 β€ x1, y1, x2, y2 β€ n) which correspondingly represent a side of the square, the coordinates of the first point and the coordinates of the second point. It is guaranteed that the points lie on the sides of the square.
|
You must print on a single line the shortest distance between the points.
|
Input: 2 0 0 1 0 | Output: 1
|
Easy
| 3 | 592 | 304 | 73 | 0 |
|
958 |
C1
|
958C1
|
C1. Encryption (easy)
| 1,200 |
brute force
|
Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted β the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one looks.Heidi is presented with a screen that shows her a sequence of integers A and a positive integer p. She knows that the encryption code is a single number S, which is defined as follows:Define the score of X to be the sum of the elements of X modulo p.Heidi is given a sequence A that consists of N integers, and also given an integer p. She needs to split A into 2 parts such that: Each part contains at least 1 element of A, and each part consists of contiguous elements of A. The two parts do not overlap. The total sum S of the scores of those two parts is maximized. This is the encryption code. Output the sum S, which is the encryption code.
|
The first line of the input contains two space-separated integer N and p (2 β€ N β€ 100 000, 2 β€ p β€ 10 000) β the number of elements in A, and the modulo for computing scores, respectively.The second line contains N space-separated integers which are the elements of A. Each integer is from the interval [1, 1 000 000].
|
Output the number S as described in the problem statement.
|
In the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of .In the second example, the score is maximized if the first part consists of the first three elements, and the second part consists of the rest. Then, the score is .
|
Input: 4 103 4 7 2 | Output: 16
|
Easy
| 1 | 970 | 318 | 58 | 9 |
2,125 |
B
|
2125B
|
B. Left and Down
| 900 |
math; number theory
|
There is a robot located in the cell \((a,b)\) of an infinite grid. Misha wants to move it to the cell \((0,0)\). To do this, he has fixed some integer \(k\).Misha can perform the following operation: choose two integers \(dx\) and \(dy\) (both from \(0\) to \(k\) inclusive) and move the robot \(dx\) cells to the left (in the direction of decreasing \(x\) coordinate) and \(dy\) cells down (in the direction of decreasing \(y\) coordinate). In other words, move the robot from \((x,y)\) to \((x - dx, y - dy)\).The cost of the operation is: \(1\), if the chosen pair \((dx,dy)\) is used for the first time; \(0\), if the pair \((dx,dy)\) has been chosen before. Note that if \(dx \ne dy\), the pairs \((dx, dy)\) and \((dy, dx)\) are considered different.Help Misha bring the robot to the cell \((0,0)\) with minimum total cost. Note that you don't have to minimize the number of operations.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each test case contains three integers \(a, b\), and \(k\) (\(1 \le a, b, k \le 10^{18}\)).
|
For each test case, output a single integer β the minimum total cost of operations required to move the robot to the cell \((0,0)\).
|
In the first test case, the operation \((3,5)\) can be applied once. The robot will immediately go to \((0,0)\), and the cost of the operation will be \(1\).In the second test case, the operations: \((1,1)\), \((0,1)\), and \((1,1)\) can be applied. After the first operation, the robot will be at cell \((1,2)\), after the second one β at \((1,1)\), and after the third one β at \((0,0)\). The cost of the first and second operations is \(1\), while the third is \(0\), as the pair \((1,1)\) has already been used in the first operation.In the third test case, the pair \((4,6)\) can be chosen three times in a row.In the fourth test case, the operations: \((4,2)\) and \((5,5)\) can be applied.
|
Input: 43 5 152 3 112 18 89 7 5 | Output: 1 2 1 2
|
Beginner
| 2 | 893 | 205 | 132 | 21 |
1,875 |
D
|
1875D
|
D. Jellyfish and Mex
| 1,600 |
dp
|
You are given an array of \(n\) nonnegative integers \(a_1, a_2, \dots, a_n\). Let \(m\) be a variable that is initialized to \(0\), Jellyfish will perform the following operation \(n\) times: select an index \(i\) (\(1 \leq i \leq |a|\)) and delete \(a_i\) from \(a\). add \(\operatorname{MEX}(a)^{\dagger}\) to \(m\). Now Jellyfish wants to know the minimum possible final value of \(m\) if he performs all the operations optimally.\(^{\dagger}\) The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of \([2,2,1]\) is \(0\), because \(0\) does not belong to the array. The MEX of \([3,1,0,1]\) is \(2\), because \(0\) and \(1\) belong to the array, but \(2\) does not. The MEX of \([0,3,1,2]\) is \(4\) because \(0\), \(1\), \(2\), and \(3\) belong to the array, but \(4\) does not.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 5000\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5000\)) β the size of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \leq a_i \leq 10^9\)) β the integers in the array.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 minimum value of \(m\) if the operations are performed optimally.
|
In the first test case, we delete elements from \(a\) in the following order: \([5,2,\color{red}{1},0,3,0,4,0] \to [5,2,0,3,\color{red}{0},4,0] \to [5,2,\color{red}{0},3,4,0] \to [5,2,3,4,\color{red}{0}] \to [5,2,\color{red}{3},4] \to [\color{red}{5},2,4] \to [\color{red}{2},4] \to [\color{red}{4}] \to [~]\). The value of \(m\) will be \(1+1+1+0+0+0+0+0=3\).
|
Input: 485 2 1 0 3 0 4 021 251 0 2 114514 080 1 2 0 1 2 0 3 | Output: 3 0 2 7
|
Medium
| 1 | 864 | 499 | 115 | 18 |
1,209 |
H
|
1209H
|
H. Moving Walkways
| 3,300 |
data structures; greedy; math
|
Airports often use moving walkways to help you walking big distances faster. Each such walkway has some speed that effectively increases your speed. You can stand on such a walkway and let it move you, or you could also walk and then your effective speed is your walking speed plus walkway's speed.Limak wants to get from point \(0\) to point \(L\) on a straight line. There are \(n\) disjoint walkways in between. The \(i\)-th walkway is described by two integers \(x_i\) and \(y_i\) and a real value \(s_i\). The \(i\)-th walkway starts at \(x_i\), ends at \(y_i\) and has speed \(s_i\). Every walkway is located inside the segment \([0, L]\) and no two walkways have positive intersection. However, they can touch by endpoints.Limak needs to decide how to distribute his energy. For example, it might make more sense to stand somewhere (or to walk slowly) to then have a lot of energy to walk faster.Limak's initial energy is \(0\) and it must never drop below that value. At any moment, he can walk with any speed \(v\) in the interval \([0, 2]\) and it will cost him \(v\) energy per second, but he continuously recovers energy with speed of \(1\) energy per second. So, when he walks with speed \(v\), his energy increases by \((1-v)\). Note that negative value would mean losing energy.In particular, he can walk with speed \(1\) and this won't change his energy at all, while walking with speed \(0.77\) effectively gives him \(0.23\) energy per second.Limak can choose his speed arbitrarily (any real value in interval \([0, 2]\)) at every moment of time (including the moments when he is located on non-integer positions). Everything is continuous (non-discrete).What is the fastest time Limak can get from \(0\) to \(L\)?
|
The first line contains integers \(n\) and \(L\) (\(1 \le n \le 200\,000\), \(1 \le L \le 10^9\)), the number of walkways and the distance to walk.Each of the next \(n\) lines contains integers \(x_i\), \(y_i\) and real value \(s_i\) (\(0 \le x_i < y_i \le L\), \(0.1 \le s_i \le 10.0\)). The value \(s_i\) is given with at most \(9\) digits after decimal point.It's guaranteed, that no two walkways have a positive intersection. The walkways are listed from left to right. That is, \(y_i \le x_{i + 1}\) for \(1 \le i \le n - 1\).
|
Print one real value, the fastest possible time to reach \(L\). Your answer will be considered correct if its absolute or relative error won't exceed \(10^{-9}\).
|
The drawings show the first two examples. In the first one, there is a walkway from \(0\) to \(2\) with speed \(2.0\) and Limak wants to get to point \(5\). The second example has a walkway from \(2\) to \(4\) with speed \(0.91\). In the first example, one of optimal strategies is as follows. Get from \(0\) to \(2\) by standing still on the walkway. It moves you with speed \(2\) so it takes \(1\) second and you save up \(1\) energy. Get from \(2\) to \(4\) by walking with max speed \(2\) for next \(1\) second. It takes \(1\) second again and the energy drops to \(0\). Get from \(4\) to \(5\) by walking with speed \(1\). It takes \(1\) second and the energy stays constant at the value \(0\). The total time is \(1 + 1 + 1 = 3\).
|
Input: 1 5 0 2 2.0 | Output: 3.000000000000
|
Master
| 3 | 1,732 | 531 | 162 | 12 |
906 |
A
|
906A
|
A. Shockers
| 1,600 |
implementation; strings
|
Valentin participates in a show called ""Shockers"". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.
|
The first line contains a single integer n (1 β€ n β€ 105) β the number of actions Valentin did.The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types: Valentin pronounced some word and didn't get an electric shock. This action is described by the string "". w"" (without quotes), in which ""."" is a dot (ASCII-code 46), and w is the word that Valentin said. Valentin pronounced some word and got an electric shock. This action is described by the string ""! w"" (without quotes), in which ""!"" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said. Valentin made a guess about the selected letter. This action is described by the string ""? s"" (without quotes), in which ""?"" is a question mark (ASCII-code 63), and s is the guess β a lowercase English letter. All words consist only of lowercase English letters. The total length of all words does not exceed 105.It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.
|
Output a single integer β the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.
|
In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word ""b"" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock. In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.
|
Input: 5! abc. ad. b! cd? c | Output: 1
|
Medium
| 2 | 780 | 1,419 | 165 | 9 |
1,566 |
F
|
1566F
|
F. Points Movement
| 2,600 |
data structures; dp; greedy; implementation; sortings
|
There are \(n\) points and \(m\) segments on the coordinate line. The initial coordinate of the \(i\)-th point is \(a_i\). The endpoints of the \(j\)-th segment are \(l_j\) and \(r_j\) β left and right endpoints, respectively.You can move the points. In one move you can move any point from its current coordinate \(x\) to the coordinate \(x - 1\) or the coordinate \(x + 1\). The cost of this move is \(1\).You should move the points in such a way that each segment is visited by at least one point. A point visits the segment \([l, r]\) if there is a moment when its coordinate was on the segment \([l, r]\) (including endpoints).You should find the minimal possible total cost of all moves such that all segments are visited.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of points and segments respectively.The next line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)) β the initial coordinates of the points.Each of the next \(m\) lines contains two integers \(l_j\), \(r_j\) (\(-10^9 \le l_j \le r_j \le 10^9\)) β the left and the right endpoints of the \(j\)-th segment.It's guaranteed that the sum of \(n\) and the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case print a single integer β the minimal total cost of all moves such that all segments are visited.
|
In the first test case the points can be moved as follows: Move the second point from the coordinate \(6\) to the coordinate \(5\). Move the third point from the coordinate \(14\) to the coordinate \(13\). Move the fourth point from the coordinate \(18\) to the coordinate \(17\). Move the third point from the coordinate \(13\) to the coordinate \(12\). Move the fourth point from the coordinate \(17\) to the coordinate \(16\). The total cost of moves is \(5\). It is easy to see, that all segments are visited by these movements. For example, the tenth segment (\([7, 13]\)) is visited after the second move by the third point.Here is the image that describes the first test case:
|
Input: 2 4 11 2 6 14 18 0 3 4 5 11 15 3 5 10 13 16 16 1 4 8 12 17 19 7 13 14 19 4 12 -9 -16 12 3 -20 -18 -14 -13 -10 -7 -3 -1 0 4 6 11 7 9 8 10 13 15 14 18 16 17 18 19 | Output: 5 22
|
Expert
| 5 | 728 | 751 | 115 | 15 |
427 |
A
|
427A
|
A. Police Recruits
| 800 |
implementation
|
The police department of your city has just started its journey. Initially, they donβt have any manpower. So, they started hiring new recruits in groups.Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.If there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated.Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.
|
The first line of input will contain an integer n (1 β€ n β€ 105), the number of events. The next line will contain n space-separated integers.If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time.
|
Print a single integer, the number of crimes which will go untreated.
|
Lets consider the second example: Firstly one person is hired. Then crime appears, the last hired person will investigate this crime. One more person is hired. One more crime appears, the last hired person will investigate this crime. Crime appears. There is no free policeman at the time, so this crime will go untreated. One more person is hired. One more person is hired. One more person is hired. The answer is one, as one crime (on step 5) will go untreated.
|
Input: 3-1 -1 1 | Output: 2
|
Beginner
| 1 | 526 | 348 | 69 | 4 |
2,048 |
H
|
2048H
|
H. Kevin and Strange Operation
| 3,100 |
data structures; dp
|
Kevin is exploring problems related to binary strings in Chinatown. When he was at a loss, a stranger approached him and introduced a peculiar operation: Suppose the current binary string is \( t \), with a length of \( \vert t \vert \). Choose an integer \( 1 \leq p \leq \vert t \vert \). For all \( 1 \leq i < p \), simultaneously perform the operation \( t_i = \max(t_i, t_{i+1}) \), and then delete \( t_p \). For example, suppose the current binary string is 01001, and you choose \( p = 4 \). Perform \( t_i = \max(t_i, t_{i+1}) \) for \(t_1\), \(t_2\), and \( t_3 \), transforming the string into 11001, then delete \( t_4 \), resulting in 1101.Kevin finds this strange operation quite interesting. Thus, he wants to ask you: Given a binary string \( s \), how many distinct non-empty binary strings can you obtain through any number of operations (possibly zero)?Since the answer may be very large, you only need to output the result modulo \(998\,244\,353\).
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1\le t \le 10^4\)) β the number of test cases.For each test case, the only line contains a binary string \( s \) (\( 1 \le \lvert s \rvert \le 10^6 \)).It is guaranteed that the sum of \(\lvert s \rvert\) over all test cases does not exceed \(10^6\).
|
For each test case, print a single integer in the only line of the output β the number of distinct non-empty binary strings you can obtain, modulo \(998\,244\,353\).
|
In the first test case, all the binary strings you can obtain are: 11001, 1001, 1101, 001, 101, 111, 01, 11, and 1. There are \( 9 \) in total.
|
Input: 211001000110111001100 | Output: 9 73
|
Master
| 2 | 968 | 340 | 165 | 20 |
1,822 |
C
|
1822C
|
C. Bun Lover
| 800 |
math
|
Tema loves cinnabon rolls β buns with cinnabon and chocolate in the shape of a ""snail"".Cinnabon rolls come in different sizes and are square when viewed from above. The most delicious part of a roll is the chocolate, which is poured in a thin layer over the cinnabon roll in the form of a spiral and around the bun, as in the following picture: Cinnabon rolls of sizes 4, 5, 6 For a cinnabon roll of size \(n\), the length of the outer side of the square is \(n\), and the length of the shortest vertical chocolate segment in the central part is one.Formally, the bun consists of two dough spirals separated by chocolate. A cinnabon roll of size \(n + 1\) is obtained from a cinnabon roll of size \(n\) by wrapping each of the dough spirals around the cinnabon roll for another layer.It is important that a cinnabon roll of size \(n\) is defined in a unique way.Tema is interested in how much chocolate is in his cinnabon roll of size \(n\). Since Tema has long stopped buying small cinnabon rolls, it is guaranteed that \(n \ge 4\).Answer this non-obvious question by calculating the total length of the chocolate layer.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.The following \(t\) lines describe the test cases.Each test case is described by a single integer \(n\) (\(4 \le n \le 10^9\)) β the size of the cinnabon roll.
|
Output \(t\) integers. The \(i\)-th of them should be equal to the total length of the chocolate layer in the \(i\)-th test case.
|
Input: 4456179179179 | Output: 26 37 50 32105178545472401
|
Beginner
| 1 | 1,123 | 269 | 129 | 18 |
|
1,184 |
E3
|
1184E3
|
E3. Daleks' Invasion (hard)
| 2,400 |
data structures; dsu; graphs; trees
|
With your help, Heidi has prepared a plan of trap placement and defence. Yet suddenly, the Doctor popped out of the TARDIS and told her that he had spied on the Daleks' preparations, and there is more of them than ever. Desperate times require desperate measures, so Heidi is going to risk meeting with the Daleks and she will consider placing a trap along any Corridor.This means she needs your help again in calculating \(E_{max}(c)\) β the largest \(e \le 10^9\) such that if we changed the energy requirement of \(c\) to \(e\), then the Daleks might use \(c\) in their invasion β but this time for all Time Corridors.
|
First line: number \(n\) of destinations, number \(m\) of corridors (\(2 \leq n \leq 10^5\), \(n - 1 \leq m \leq 10^6\)). The next \(m\) lines: destinations \(a\), \(b\) and energy \(e\) (\(1 \leq a, b \leq n\), \(a \neq b\), \(0 \leq e \leq 10^9\)).No pair \(\{a, b\}\) will repeat. The graph is guaranteed to be connected. It is not guaranteed that all energy requirements \(e\) are distinct, or that the minimum spanning tree is unique.
|
Output \(m\) lines, each containing one integer: \(E_{max}(c_i)\) for the \(i\)-th Corridor \(c_i\) from the input.
|
Input: 3 31 2 82 3 33 1 4 | Output: 488
|
Expert
| 4 | 621 | 439 | 115 | 11 |
|
330 |
B
|
330B
|
B. Road Construction
| 1,300 |
constructive algorithms; graphs
|
A country has n cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two roads. You are also given m pairs of cities β roads cannot be constructed between these pairs of cities.Your task is to construct the minimum number of roads that still satisfy the above conditions. The constraints will guarantee that this is always possible.
|
The first line consists of two integers n and m .Then m lines follow, each consisting of two integers ai and bi (1 β€ ai, bi β€ n, ai β bi), which means that it is not possible to construct a road connecting cities ai and bi. Consider the cities are numbered from 1 to n.It is guaranteed that every pair of cities will appear at most once in the input.
|
You should print an integer s: the minimum number of roads that should be constructed, in the first line. Then s lines should follow, each consisting of two integers ai and bi (1 β€ ai, bi β€ n, ai β bi), which means that a road should be constructed between cities ai and bi.If there are several solutions, you may print any of them.
|
This is one possible solution of the example: These are examples of wrong solutions: The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3). In addition, it also tries to construct a road between cities 1 and 3, while the input specifies that it is not allowed to construct a road between the pair. The above solution is wrong because you need to traverse at least 3 roads to go from city 1 to city 3, whereas in your country it must be possible to go from any city to another by traversing at most 2 roads. Finally, the above solution is wrong because it must be possible to go from any city to another, whereas it is not possible in this country to go from city 1 to 3, 2 to 3, and 4 to 3.
|
Input: 4 11 3 | Output: 31 24 22 3
|
Easy
| 2 | 578 | 350 | 332 | 3 |
1,485 |
B
|
1485B
|
B. Replace and Keep Sorted
| 1,200 |
dp; implementation; math
|
Given a positive integer \(k\), two arrays are called \(k\)-similar if: they are strictly increasing; they have the same length; all their elements are positive integers between \(1\) and \(k\) (inclusive); they differ in exactly one position. You are given an integer \(k\), a strictly increasing array \(a\) and \(q\) queries. For each query, you are given two integers \(l_i \leq r_i\). Your task is to find how many arrays \(b\) exist, such that \(b\) is \(k\)-similar to array \([a_{l_i},a_{l_i+1}\ldots,a_{r_i}]\).
|
The first line contains three integers \(n\), \(q\) and \(k\) (\(1\leq n, q \leq 10^5\), \(n\leq k \leq 10^9\)) β the length of array \(a\), the number of queries and number \(k\).The second line contains \(n\) integers \(a_1, a_2, \ldots,a_n\) (\(1 \leq a_i \leq k\)). This array is strictly increasing β \(a_1 < a_2 < \ldots < a_n\).Each of the following \(q\) lines contains two integers \(l_i\), \(r_i\) (\(1 \leq l_i \leq r_i \leq n\)).
|
Print \(q\) lines. The \(i\)-th of them should contain the answer to the \(i\)-th query.
|
In the first example:In the first query there are \(4\) arrays that are \(5\)-similar to \([2,4]\): \([1,4],[3,4],[2,3],[2,5]\).In the second query there are \(3\) arrays that are \(5\)-similar to \([4,5]\): \([1,5],[2,5],[3,5]\).
|
Input: 4 2 5 1 2 4 5 2 3 3 4 | Output: 4 3
|
Easy
| 3 | 520 | 441 | 88 | 14 |
1,851 |
G
|
1851G
|
G. Vlad and the Mountains
| 2,000 |
binary search; data structures; dsu; graphs; implementation; sortings; trees; two pointers
|
Vlad decided to go on a trip to the mountains. He plans to move between \(n\) mountains, some of which are connected by roads. The \(i\)-th mountain has a height of \(h_i\).If there is a road between mountains \(i\) and \(j\), Vlad can move from mountain \(i\) to mountain \(j\) by spending \(h_j - h_i\) units of energy. If his energy drops below zero during the transition, he will not be able to move from mountain \(i\) to mountain \(j\). Note that \(h_j - h_i\) can be negative and then the energy will be restored.Vlad wants to consider different route options, so he asks you to answer the following queries: is it possible to construct some route starting at mountain \(a\) and ending at mountain \(b\), given that he initially has \(e\) units of energy?
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The descriptions of the test cases follow.The first line of each test case contains two numbers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5)\)) β the number of mountains and the number of roads between them, respectively.The second line contains \(n\) integers \(h_1, h_2, h_3, \dots, h_n\) (\(1 \le h_i \le 10^9\)) β the heights of the mountains.The next \(m\) lines contain two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) β the numbers of the mountains connected by a road. It is guaranteed that no road appears twice.The next line contains an integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of queries.The following \(q\) lines contain three numbers \(a\), \(b\), and \(e\) (\(1 \le a, b \le n\), \(0 \le e \le 10^9\)) β the initial and final mountains of the route, and the amount of energy, respectively.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). The same guarantee applies to \(m\) and \(q\).
|
For each query, output ""YES"" if Vlad can construct a route from mountain \(a\) to mountain \(b\), and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).In the examples below, the answers for different test cases are separated by an empty line, which you do not need to output.
|
Input: 27 71 5 3 4 2 4 11 44 33 63 22 55 65 751 1 36 2 04 7 01 7 41 7 26 54 7 6 2 5 11 35 31 52 46 251 5 11 3 11 2 10006 2 66 2 5 | Output: YES NO YES YES NO YES NO NO YES NO
|
Hard
| 8 | 762 | 1,139 | 389 | 18 |
|
1,195 |
C
|
1195C
|
C. Basketball Exercise
| 1,400 |
dp
|
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. \(2 \cdot n\) students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly \(n\) people in each row). Students are numbered from \(1\) to \(n\) in each row in order from left to right. Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all \(2n\) students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.
|
The first line of the input contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of students in each row.The second line of the input contains \(n\) integers \(h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}\) (\(1 \le h_{1, i} \le 10^9\)), where \(h_{1, i}\) is the height of the \(i\)-th student in the first row.The third line of the input contains \(n\) integers \(h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}\) (\(1 \le h_{2, i} \le 10^9\)), where \(h_{2, i}\) is the height of the \(i\)-th student in the second row.
|
Print a single integer β the maximum possible total height of players in a team Demid can choose.
|
In the first example Demid can choose the following team as follows: In the second example Demid can choose the following team as follows:
|
Input: 5 9 3 5 7 3 5 8 1 4 5 | Output: 29
|
Easy
| 1 | 1,133 | 518 | 97 | 11 |
1,303 |
F
|
1303F
|
F. Number of Components
| 2,800 |
dsu; implementation
|
You are given a matrix \(n \times m\), initially filled with zeroes. We define \(a_{i, j}\) as the element in the \(i\)-th row and the \(j\)-th column of the matrix.Two cells of the matrix are connected if they share a side, and the elements in these cells are equal. Two cells of the matrix belong to the same connected component if there exists a sequence \(s_1\), \(s_2\), ..., \(s_k\) such that \(s_1\) is the first cell, \(s_k\) is the second cell, and for every \(i \in [1, k - 1]\), \(s_i\) and \(s_{i + 1}\) are connected.You are given \(q\) queries of the form \(x_i\) \(y_i\) \(c_i\) (\(i \in [1, q]\)). For every such query, you have to do the following: replace the element \(a_{x, y}\) with \(c\); count the number of connected components in the matrix. There is one additional constraint: for every \(i \in [1, q - 1]\), \(c_i \le c_{i + 1}\).
|
The first line contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 300\), \(1 \le q \le 2 \cdot 10^6\)) β the number of rows, the number of columns and the number of queries, respectively.Then \(q\) lines follow, each representing a query. The \(i\)-th line contains three integers \(x_i\), \(y_i\) and \(c_i\) (\(1 \le x_i \le n\), \(1 \le y_i \le m\), \(1 \le c_i \le \max(1000, \lceil \frac{2 \cdot 10^6}{nm} \rceil)\)). For every \(i \in [1, q - 1]\), \(c_i \le c_{i + 1}\).
|
Print \(q\) integers, the \(i\)-th of them should be equal to the number of components in the matrix after the first \(i\) queries are performed.
|
Input: 3 2 10 2 1 1 1 2 1 2 2 1 1 1 2 3 1 2 1 2 2 2 2 2 2 1 2 3 2 4 2 1 5 | Output: 2 4 3 3 4 4 4 2 2 4
|
Master
| 2 | 857 | 491 | 145 | 13 |
|
573 |
B
|
573B
|
B. Bear and Blocks
| 1,600 |
binary search; data structures; dp; math
|
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.Limak will repeat the following operation till everything is destroyed.Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.
|
The first line contains single integer n (1 β€ n β€ 105).The second line contains n space-separated integers h1, h2, ..., hn (1 β€ hi β€ 109) β sizes of towers.
|
Print the number of operations needed to destroy all towers.
|
The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
|
Input: 62 1 4 6 2 2 | Output: 3
|
Medium
| 4 | 694 | 156 | 60 | 5 |
1,209 |
D
|
1209D
|
D. Cow and Snacks
| 1,700 |
dfs and similar; dsu; graphs
|
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo!There are \(n\) snacks flavors, numbered with integers \(1, 2, \ldots, n\). Bessie has \(n\) snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows: First, Bessie will line up the guests in some way. Then in this order, guests will approach the snacks one by one. Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad. Help Bessie to minimize the number of sad guests by lining the guests in an optimal way.
|
The first line contains integers \(n\) and \(k\) (\(2 \le n \le 10^5\), \(1 \le k \le 10^5\)), the number of snacks and the number of guests. The \(i\)-th of the following \(k\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\), \(x_i \ne y_i\)), favorite snack flavors of the \(i\)-th guest.
|
Output one integer, the smallest possible number of sad guests.
|
In the first example, Bessie can order the guests like this: \(3, 1, 2, 4\). Guest \(3\) goes first and eats snacks \(1\) and \(4\). Then the guest \(1\) goes and eats the snack \(2\) only, because the snack \(1\) has already been eaten. Similarly, the guest \(2\) goes up and eats the snack \(3\) only. All the snacks are gone, so the guest \(4\) will be sad. In the second example, one optimal ordering is \(2, 1, 3, 5, 4\). All the guests will be satisfied.
|
Input: 5 4 1 2 4 3 1 4 3 4 | Output: 1
|
Medium
| 3 | 794 | 316 | 63 | 12 |
839 |
E
|
839E
|
E. Mother of Dragons
| 2,700 |
brute force; graphs; math; meet-in-the-middle
|
There are n castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more than one wall, no wall connects a castle to itself. Sir Jaime Lannister has discovered that Daenerys Targaryen is going to attack his kingdom soon. Therefore he wants to defend his kingdom. He has k liters of a strange liquid. He wants to distribute that liquid among the castles, so each castle may contain some liquid (possibly zero or non-integer number of liters). After that the stability of a wall is defined as follows: if the wall connects two castles a and b, and they contain x and y liters of that liquid, respectively, then the strength of that wall is xΒ·y.Your task is to print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.
|
The first line of the input contains two integers n and k (1 β€ n β€ 40, 1 β€ k β€ 1000).Then n lines follows. The i-th of these lines contains n integers ai, 1, ai, 2, ..., ai, n (). If castles i and j are connected by a wall, then ai, j = 1. Otherwise it is equal to 0.It is guaranteed that ai, j = aj, i and ai, i = 0 for all 1 β€ i, j β€ n.
|
Print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve.Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
|
In the first sample, we can assign 0.5, 0.5, 0 liters of liquid to castles 1, 2, 3, respectively, to get the maximum sum (0.25).In the second sample, we can assign 1.0, 1.0, 1.0, 1.0 liters of liquid to castles 1, 2, 3, 4, respectively, to get the maximum sum (4.0)
|
Input: 3 10 1 01 0 00 0 0 | Output: 0.250000000000
|
Master
| 4 | 801 | 338 | 328 | 8 |
747 |
E
|
747E
|
E. Comments
| 1,700 |
dfs and similar; expression parsing; implementation; strings
|
A rare article in the Internet is posted without a possibility to comment it. On a Polycarp's website each article has comments feed.Each comment on Polycarp's website is a non-empty string consisting of uppercase and lowercase letters of English alphabet. Comments have tree-like structure, that means each comment except root comments (comments of the highest level) has exactly one parent comment.When Polycarp wants to save comments to his hard drive he uses the following format. Each comment he writes in the following format: at first, the text of the comment is written; after that the number of comments is written, for which this comment is a parent comment (i. e. the number of the replies to this comments); after that the comments for which this comment is a parent comment are written (the writing of these comments uses the same algorithm). All elements in this format are separated by single comma. Similarly, the comments of the first level are separated by comma.For example, if the comments look like: then the first comment is written as ""hello,2,ok,0,bye,0"", the second is written as ""test,0"", the third comment is written as ""one,1,two,2,a,0,b,0"". The whole comments feed is written as: ""hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0"". For a given comments feed in the format specified above print the comments in a different format: at first, print a integer d β the maximum depth of nesting comments; after that print d lines, the i-th of them corresponds to nesting level i; for the i-th row print comments of nesting level i in the order of their appearance in the Policarp's comments feed, separated by space.
|
The first line contains non-empty comments feed in the described format. It consists of uppercase and lowercase letters of English alphabet, digits and commas. It is guaranteed that each comment is a non-empty string consisting of uppercase and lowercase English characters. Each of the number of comments is integer (consisting of at least one digit), and either equals 0 or does not contain leading zeros.The length of the whole string does not exceed 106. It is guaranteed that given structure of comments is valid.
|
Print comments in a format that is given in the statement. For each level of nesting, comments should be printed in the order they are given in the input.
|
The first example is explained in the statements.
|
Input: hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0,b,0 | Output: 3hello test one ok bye two a b
|
Medium
| 4 | 1,644 | 518 | 154 | 7 |
711 |
E
|
711E
|
E. ZS and The Birthday Paradox
| 2,300 |
math; number theory; probabilities
|
ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% chance that some two of them share the same birthday. ZS the Coder finds this very interesting, and decides to test this with the inhabitants of Udayland.In Udayland, there are 2n days in a year. ZS the Coder wants to interview k people from Udayland, each of them has birthday in one of 2n days (each day with equal probability). He is interested in the probability of at least two of them have the birthday at the same day. ZS the Coder knows that the answer can be written as an irreducible fraction . He wants to find the values of A and B (he does not like to deal with floating point numbers). Can you help him?
|
The first and only line of the input contains two integers n and k (1 β€ n β€ 1018, 2 β€ k β€ 1018), meaning that there are 2n days in a year and that ZS the Coder wants to interview exactly k people.
|
If the probability of at least two k people having the same birthday in 2n days long year equals (A β₯ 0, B β₯ 1, ), print the A and B in a single line.Since these numbers may be too large, print them modulo 106 + 3. Note that A and B must be coprime before their remainders modulo 106 + 3 are taken.
|
In the first sample case, there are 23 = 8 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly , so A = 1, B = 8.In the second sample case, there are only 21 = 2 days in Udayland, but there are 3 people, so it is guaranteed that two of them have the same birthday. Thus, the probability is 1 and A = B = 1.
|
Input: 3 2 | Output: 1 8
|
Expert
| 3 | 769 | 196 | 298 | 7 |
346 |
E
|
346E
|
E. Doodle Jump
| 3,000 |
math; number theory
|
In Doodle Jump the aim is to guide a four-legged creature called ""The Doodler"" up a never-ending series of platforms without falling. β Wikipedia. It is a very popular game and xiaodao likes it very much. One day when playing the game she wondered whether there exists a platform that the doodler couldn't reach due to the limits of its jumping ability. Consider the following problem.There are n platforms. The height of the x-th (1 β€ x β€ n) platform is aΒ·x mod p, where a and p are positive co-prime integers. The maximum possible height of a Doodler's jump is h. That is, it can jump from height h1 to height h2 (h1 < h2) if h2 - h1 β€ h. Initially, the Doodler is on the ground, the height of which is 0. The question is whether it can reach the highest platform or not.For example, when a = 7, n = 4, p = 12, h = 2, the heights of the platforms are 7, 2, 9, 4 as in the picture below. With the first jump the Doodler can jump to the platform at height 2, with the second one the Doodler can jump to the platform at height 4, but then it can't jump to any of the higher platforms. So, it can't reach the highest platform. User xiaodao thought about the problem for a long time but didn't solve it, so she asks you for help. Also, she has a lot of instances of the problem. Your task is solve all of these instances.
|
The first line contains an integer t (1 β€ t β€ 104) β the number of problem instances. Each of the next t lines contains four integers a, n, p and h (1 β€ a β€ 109, 1 β€ n < p β€ 109, 0 β€ h β€ 109). It's guaranteed that a and p are co-prime.
|
For each problem instance, if the Doodler can reach the highest platform, output ""YES"", otherwise output ""NO"".
|
Input: 37 4 12 27 1 9 47 4 12 3 | Output: NONOYES
|
Master
| 2 | 1,320 | 235 | 114 | 3 |
|
1,357 |
D5
|
1357D5
|
D5. Quantum Classification - Dataset 7
| 0 |
*special
|
This problem is identical to the problem D1 in every aspect except the training dataset. Please refer to that problem for the full problem statement.
|
Beginner
| 1 | 149 | 0 | 0 | 13 |
||||
1,257 |
E
|
1257E
|
E. The Contest
| 2,000 |
data structures; dp; greedy
|
A team of three programmers is going to play a contest. The contest consists of \(n\) problems, numbered from \(1\) to \(n\). Each problem is printed on a separate sheet of paper. The participants have decided to divide the problem statements into three parts: the first programmer took some prefix of the statements (some number of first paper sheets), the third contestant took some suffix of the statements (some number of last paper sheets), and the second contestant took all remaining problems. But something went wrong β the statements were printed in the wrong order, so the contestants have received the problems in some random order.The first contestant has received problems \(a_{1, 1}, a_{1, 2}, \dots, a_{1, k_1}\). The second one has received problems \(a_{2, 1}, a_{2, 2}, \dots, a_{2, k_2}\). The third one has received all remaining problems (\(a_{3, 1}, a_{3, 2}, \dots, a_{3, k_3}\)).The contestants don't want to play the contest before they redistribute the statements. They want to redistribute them so that the first contestant receives some prefix of the problemset, the third contestant receives some suffix of the problemset, and the second contestant receives all the remaining problems.During one move, some contestant may give one of their problems to other contestant. What is the minimum number of moves required to redistribute the problems?It is possible that after redistribution some participant (or even two of them) will not have any problems.
|
The first line contains three integers \(k_1, k_2\) and \(k_3\) (\(1 \le k_1, k_2, k_3 \le 2 \cdot 10^5, k_1 + k_2 + k_3 \le 2 \cdot 10^5\)) β the number of problems initially taken by the first, the second and the third participant, respectively.The second line contains \(k_1\) integers \(a_{1, 1}, a_{1, 2}, \dots, a_{1, k_1}\) β the problems initially taken by the first participant.The third line contains \(k_2\) integers \(a_{2, 1}, a_{2, 2}, \dots, a_{2, k_2}\) β the problems initially taken by the second participant.The fourth line contains \(k_3\) integers \(a_{3, 1}, a_{3, 2}, \dots, a_{3, k_3}\) β the problems initially taken by the third participant.It is guaranteed that no problem has been taken by two (or three) participants, and each integer \(a_{i, j}\) meets the condition \(1 \le a_{i, j} \le n\), where \(n = k_1 + k_2 + k_3\).
|
Print one integer β the minimum number of moves required to redistribute the problems so that the first participant gets the prefix of the problemset, the third participant gets the suffix of the problemset, and the second participant gets all of the remaining problems.
|
In the first example the third contestant should give the problem \(2\) to the first contestant, so the first contestant has \(3\) first problems, the third contestant has \(1\) last problem, and the second contestant has \(1\) remaining problem.In the second example the distribution of problems is already valid: the first contestant has \(3\) first problems, the third contestant has \(1\) last problem, and the second contestant has \(2\) remaining problems.The best course of action in the third example is to give all problems to the third contestant.The best course of action in the fourth example is to give all problems to the second contestant.
|
Input: 2 1 2 3 1 4 2 5 | Output: 1
|
Hard
| 3 | 1,480 | 853 | 270 | 12 |
1,404 |
B
|
1404B
|
B. Tree Tag
| 1,900 |
dfs and similar; dp; games; trees
|
Alice and Bob are playing a fun game of tree tag.The game is played on a tree of \(n\) vertices numbered from \(1\) to \(n\). Recall that a tree on \(n\) vertices is an undirected, connected graph with \(n-1\) edges.Initially, Alice is located at vertex \(a\), and Bob at vertex \(b\). They take turns alternately, and Alice makes the first move. In a move, Alice can jump to a vertex with distance at most \(da\) from the current vertex. And in a move, Bob can jump to a vertex with distance at most \(db\) from the current vertex. The distance between two vertices is defined as the number of edges on the unique simple path between them. In particular, either player is allowed to stay at the same vertex in a move. Note that when performing a move, a player only occupies the starting and ending vertices of their move, not the vertices between them.If after at most \(10^{100}\) moves, Alice and Bob occupy the same vertex, then Alice is declared the winner. Otherwise, Bob wins.Determine the winner if both players play optimally.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains five integers \(n,a,b,da,db\) (\(2\le n\le 10^5\), \(1\le a,b\le n\), \(a\ne b\), \(1\le da,db\le n-1\)) β the number of vertices, Alice's vertex, Bob's vertex, Alice's maximum jumping distance, and Bob's maximum jumping distance, respectively.The following \(n-1\) lines describe the edges of the tree. The \(i\)-th of these lines contains two integers \(u\), \(v\) (\(1\le u, v\le n, u\ne v\)), denoting an edge between vertices \(u\) and \(v\). It is guaranteed that these edges form a tree structure.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(10^5\).
|
For each test case, output a single line containing the winner of the game: ""Alice"" or ""Bob"".
|
In the first test case, Alice can win by moving to vertex \(1\). Then wherever Bob moves next, Alice will be able to move to the same vertex on the next move. In the second test case, Bob has the following strategy to win. Wherever Alice moves, Bob will always move to whichever of the two vertices \(1\) or \(6\) is farthest from Alice.
|
Input: 4 4 3 2 1 2 1 2 1 3 1 4 6 6 1 2 5 1 2 6 5 2 3 3 4 4 5 9 3 9 2 5 1 2 1 6 1 9 1 3 9 5 7 9 4 8 4 3 11 8 11 3 3 1 2 11 9 4 9 6 5 2 10 3 2 5 9 8 3 7 4 7 10 | Output: Alice Bob Alice Alice
|
Hard
| 4 | 1,036 | 789 | 97 | 14 |
1,366 |
E
|
1366E
|
E. Two Arrays
| 2,100 |
binary search; brute force; combinatorics; constructive algorithms; dp; two pointers
|
You are given two arrays \(a_1, a_2, \dots , a_n\) and \(b_1, b_2, \dots , b_m\). Array \(b\) is sorted in ascending order (\(b_i < b_{i + 1}\) for each \(i\) from \(1\) to \(m - 1\)).You have to divide the array \(a\) into \(m\) consecutive subarrays so that, for each \(i\) from \(1\) to \(m\), the minimum on the \(i\)-th subarray is equal to \(b_i\). Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of \(a\) compose the first subarray, the next several elements of \(a\) compose the second subarray, and so on.For example, if \(a = [12, 10, 20, 20, 25, 30]\) and \(b = [10, 20, 30]\) then there are two good partitions of array \(a\): \([12, 10, 20], [20, 25], [30]\); \([12, 10], [20, 20, 25], [30]\). You have to calculate the number of ways to divide the array \(a\). Since the number can be pretty large print it modulo 998244353.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the length of arrays \(a\) and \(b\) respectively.The second line contains \(n\) integers \(a_1, a_2, \dots , a_n\) (\(1 \le a_i \le 10^9\)) β the array \(a\).The third line contains \(m\) integers \(b_1, b_2, \dots , b_m\) (\(1 \le b_i \le 10^9; b_i < b_{i+1}\)) β the array \(b\).
|
In only line print one integer β the number of ways to divide the array \(a\) modulo 998244353.
|
Input: 6 3 12 10 20 20 25 30 10 20 30 | Output: 2
|
Hard
| 6 | 908 | 371 | 95 | 13 |
|
1,059 |
C
|
1059C
|
C. Sequence Transformation
| 1,600 |
constructive algorithms; math
|
Let's call the following process a transformation of a sequence of length \(n\).If the sequence is empty, the process ends. Otherwise, append the greatest common divisor (GCD) of all the elements of the sequence to the result and remove one arbitrary element from the sequence. Thus, when the process ends, we have a sequence of \(n\) integers: the greatest common divisors of all the elements in the sequence before each deletion.You are given an integer sequence \(1, 2, \dots, n\). Find the lexicographically maximum result of its transformation.A sequence \(a_1, a_2, \ldots, a_n\) is lexicographically larger than a sequence \(b_1, b_2, \ldots, b_n\), if there is an index \(i\) such that \(a_j = b_j\) for all \(j < i\), and \(a_i > b_i\).
|
The first and only line of input contains one integer \(n\) (\(1\le n\le 10^6\)).
|
Output \(n\) integers β the lexicographically maximum result of the transformation.
|
In the first sample the answer may be achieved this way: Append GCD\((1, 2, 3) = 1\), remove \(2\). Append GCD\((1, 3) = 1\), remove \(1\). Append GCD\((3) = 3\), remove \(3\). We get the sequence \([1, 1, 3]\) as the result.
|
Input: 3 | Output: 1 1 3
|
Medium
| 2 | 745 | 81 | 83 | 10 |
2,000 |
C
|
2000C
|
C. Numeric String Template
| 1,000 |
data structures; strings
|
Kristina has an array \(a\), called a template, consisting of \(n\) integers. She also has \(m\) strings, each consisting only of lowercase Latin letters. The strings are numbered from \(1\) to \(m\). She wants to check which strings match the template.A string \(s\) is considered to match the template if all of the following conditions are simultaneously satisfied: The length of the string \(s\) is equal to the number of elements in the array \(a\). The same numbers from \(a\) correspond to the same symbols from \(s\). So, if \(a_i = a_j\), then \(s_i = s_j\) for (\(1 \le i, j \le n\)). The same symbols from \(s\) correspond to the same numbers from \(a\). So, if \(s_i = s_j\), then \(a_i = a_j\) for (\(1 \le i, j \le n\)). In other words, there must be a one-to-one correspondence between the characters of the string and the elements of the array.For example, if \(a\) = [\(3, 5, 2, 1, 3\)], then the string ""abfda"" matches the template, while the string ""afbfa"" does not, since the character ""f"" corresponds to both numbers \(1\) and \(5\).
|
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The following descriptions are for the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in the array \(a\).The second line of each test case contains exactly \(n\) integers \(a_i\) (\(-10^9 \le a_i \le 10^9\)) β the elements of the array \(a\).The third line of each test case contains a single integer \(m\) (\(1 \le m \le 2 \cdot 10^5\)) β the number of strings to check for template matching.Following are \(m\) strings, each containing a non-empty string \(s_j\) (\(1 \le |s_j| \le 2 \cdot 10^5\)), consisting of lowercase Latin letters.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\), and that the sum of the lengths of all strings does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(m\) lines. On the \(i\)-th line (\(1 \le i \le m\)) output: ""YES"", if the string with index \(i\) matches the template; ""NO"" otherwise. You may output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
|
The first test case is explained in the problem statement.
|
Input: 353 5 2 1 32abfdaafbfa21 23ababcaa45 -3 5 -34aaaabcbcabacbcb | Output: YES NO YES NO NO NO YES NO YES
|
Beginner
| 2 | 1,060 | 905 | 313 | 20 |
1,765 |
D
|
1765D
|
D. Watch the Videos
| 1,700 |
binary search; constructive algorithms; two pointers
|
Monocarp wants to watch \(n\) videos. Each video is only one minute long, but its size may be arbitrary. The \(i\)-th video has the size \(a_i\) megabytes. All videos are published on the Internet. A video should be downloaded before it can be watched. Monocarp has poor Internet connection β it takes exactly \(1\) minute to download \(1\) megabyte of data, so it will require \(a_i\) minutes to download the \(i\)-th video.Monocarp's computer has a hard disk of \(m\) megabytes. The disk is used to store the downloaded videos. Once Monocarp starts the download of a video of size \(s\), the \(s\) megabytes are immediately reserved on a hard disk. If there are less than \(s\) megabytes left, the download cannot be started until the required space is freed. Each single video can be stored on the hard disk, since \(a_i \le m\) for all \(i\). Once the download is started, it cannot be interrupted. It is not allowed to run two or more downloads in parallel.Once a video is fully downloaded to the hard disk, Monocarp can watch it. Watching each video takes exactly \(1\) minute and does not occupy the Internet connection, so Monocarp can start downloading another video while watching the current one.When Monocarp finishes watching a video, he doesn't need it on the hard disk anymore, so he can delete the video, instantly freeing the space it occupied on a hard disk. Deleting a video takes negligible time.Monocarp wants to watch all \(n\) videos as quickly as possible. The order of watching does not matter, since Monocarp needs to watch all of them anyway. Please calculate the minimum possible time required for that.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 10^9\)) β the number of videos Monocarp wants to watch and the size of the hard disk, respectively. The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le m\)) β the sizes of the videos.
|
Print one integer β the minimum time required to watch all \(n\) videos.
|
Input: 5 6 1 2 3 4 5 | Output: 16
|
Medium
| 3 | 1,631 | 311 | 72 | 17 |
|
2,036 |
E
|
2036E
|
E. Reverse the Rivers
| 1,600 |
binary search; constructive algorithms; data structures; greedy
|
A conspiracy of ancient sages, who decided to redirect rivers for their own convenience, has put the world on the brink. But before implementing their grand plan, they decided to carefully think through their strategy β that's what sages do.There are \(n\) countries, each with exactly \(k\) regions. For the \(j\)-th region of the \(i\)-th country, they calculated the value \(a_{i,j}\), which reflects the amount of water in it.The sages intend to create channels between the \(j\)-th region of the \(i\)-th country and the \(j\)-th region of the \((i + 1)\)-th country for all \(1 \leq i \leq (n - 1)\) and for all \(1 \leq j \leq k\). Since all \(n\) countries are on a large slope, water flows towards the country with the highest number. According to the sages' predictions, after the channel system is created, the new value of the \(j\)-th region of the \(i\)-th country will be \(b_{i,j} = a_{1,j} | a_{2,j} | ... | a_{i,j}\), where \(|\) denotes the bitwise ""OR"" operation.After the redistribution of water, the sages aim to choose the most suitable country for living, so they will send you \(q\) queries for consideration.Each query will contain \(m\) requirements. Each requirement contains three parameters: the region number \(r\), the sign \(o\) (either ""\(<\)"" or ""\(>\)""), and the value \(c\). If \(o\) = ""\(<\)"", then in the \(r\)-th region of the country you choose, the new value must be strictly less than the limit \(c\), and if \(o\) = ""\(>\)"", it must be strictly greater.In other words, the chosen country \(i\) must satisfy all \(m\) requirements. If in the current requirement \(o\) = ""\(<\)"", then it must hold that \(b_{i,r} < c\), and if \(o\) = ""\(>\)"", then \(b_{i,r} > c\).In response to each query, you should output a single integer β the number of the suitable country. If there are multiple such countries, output the smallest one. If no such country exists, output \(-1\).
|
The first line contains three integers \(n\), \(k\), and \(q\) (\(1 \leq n, k, q \leq 10^5\)) β the number of countries, regions, and queries, respectively.Next, there are \(n\) lines, where the \(i\)-th line contains \(k\) integers \(a_{i,1}, a_{i,2}, \dots, a_{i,k}\) (\(1 \leq a_{i,j} \leq 10^9\)), where \(a_{i,j}\) is the value of the \(j\)-th region of the \(i\)-th country.Then, \(q\) queries are described.The first line of each query contains a single integer \(m\) (\(1 \leq m \leq 10^5\)) β the number of requirements.Then follow \(m\) lines, each containing an integer \(r\), a character \(o\), and an integer \(c\) (\(1 \leq r \leq k\), \(0 \leq c \leq 2 \cdot 10^9\)), where \(r\) and \(c\) are the region number and the value, and \(o\) is either ""\(<\)"" or ""\(>\)"" β the sign.It is guaranteed that \(n \cdot k\) does not exceed \(10^5\) and that the sum of \(m\) across all queries also does not exceed \(10^5\).
|
For each query, output a single integer on a new line β the smallest number of the suitable country, or \(-1\) if no such country exists.
|
In the example, the initial values of the regions are as follows: \(1\)\(3\)\(5\)\(9\)\(4\)\(6\)\(5\)\(3\)\(2\)\(1\)\(2\)\(7\) After creating the channels, the new values will look like this: \(1\)\(3\)\(5\)\(9\)\(1 | 4\)\(3 | 6\)\(5 | 5\)\(9 | 3\)\(1 | 4 | 2\)\(3 | 6 | 1\)\(5 | 5 | 2\)\(9 | 3 | 7\) \(\downarrow\) \(1\)\(3\)\(5\)\(9\)\(5\)\(7\)\(5\)\(11\)\(7\)\(7\)\(7\)\(15\) In the first query, it is necessary to output the minimum country number (i.e., row) where, after the redistribution of water in the first region (i.e., column), the new value will be greater than four and less than six, and in the second region it will be less than eight. Only the country with number \(2\) meets these requirements.In the second query, there are no countries that meet the specified requirements.In the third query, only the country with number \(3\) is suitable.In the fourth query, all three countries meet the conditions, so the answer is the smallest number \(1\).
|
Input: 3 4 41 3 5 94 6 5 32 1 2 731 > 42 < 81 < 621 < 82 > 813 > 524 > 81 < 8 | Output: 2 -1 3 1
|
Medium
| 4 | 1,925 | 932 | 137 | 20 |
785 |
B
|
785B
|
B. Anton and Classes
| 1,100 |
greedy; sortings
|
Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.Anton has n variants when he will attend chess classes, i-th variant is given by a period of time (l1, i, r1, i). Also he has m variants when he will attend programming classes, i-th variant is given by a period of time (l2, i, r2, i).Anton needs to choose exactly one of n possible periods of time when he will attend chess classes and exactly one of m possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.The distance between periods (l1, r1) and (l2, r2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |i - j|, where l1 β€ i β€ r1 and l2 β€ j β€ r2. In particular, when the periods intersect, the distance between them is 0.Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!
|
The first line of the input contains a single integer n (1 β€ n β€ 200 000) β the number of time periods when Anton can attend chess classes.Each of the following n lines of the input contains two integers l1, i and r1, i (1 β€ l1, i β€ r1, i β€ 109) β the i-th variant of a period of time when Anton can attend chess classes.The following line of the input contains a single integer m (1 β€ m β€ 200 000) β the number of time periods when Anton can attend programming classes.Each of the following m lines of the input contains two integers l2, i and r2, i (1 β€ l2, i β€ r2, i β€ 109) β the i-th variant of a period of time when Anton can attend programming classes.
|
Output one integer β the maximal possible distance between time periods.
|
In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3.In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.
|
Input: 31 52 62 322 46 8 | Output: 3
|
Easy
| 2 | 1,142 | 658 | 72 | 7 |
1,666 |
J
|
1666J
|
J. Job Lookup
| 2,100 |
constructive algorithms; dp; shortest paths; trees
|
Julia's \(n\) friends want to organize a startup in a new country they moved to. They assigned each other numbers from 1 to \(n\) according to the jobs they have, from the most front-end tasks to the most back-end ones. They also estimated a matrix \(c\), where \(c_{ij} = c_{ji}\) is the average number of messages per month between people doing jobs \(i\) and \(j\).Now they want to make a hierarchy tree. It will be a binary tree with each node containing one member of the team. Some member will be selected as a leader of the team and will be contained in the root node. In order for the leader to be able to easily reach any subordinate, for each node \(v\) of the tree, the following should apply: all members in its left subtree must have smaller numbers than \(v\), and all members in its right subtree must have larger numbers than \(v\).After the hierarchy tree is settled, people doing jobs \(i\) and \(j\) will be communicating via the shortest path in the tree between their nodes. Let's denote the length of this path as \(d_{ij}\). Thus, the cost of their communication is \(c_{ij} \cdot d_{ij}\).Your task is to find a hierarchy tree that minimizes the total cost of communication over all pairs: \(\sum_{1 \le i < j \le n} c_{ij} \cdot d_{ij}\).
|
The first line contains an integer \(n\) (\(1 \le n \le 200\)) β the number of team members organizing a startup.The next \(n\) lines contain \(n\) integers each, \(j\)-th number in \(i\)-th line is \(c_{ij}\) β the estimated number of messages per month between team members \(i\) and \(j\) (\(0 \le c_{ij} \le 10^9; c_{ij} = c_{ji}; c_{ii} = 0\)).
|
Output a description of a hierarchy tree that minimizes the total cost of communication. To do so, for each team member from 1 to \(n\) output the number of the member in its parent node, or 0 for the leader. If there are many optimal trees, output a description of any one of them.
|
The minimal possible total cost is \(566 \cdot 1+239 \cdot 1+30 \cdot 1+1 \cdot 2+1 \cdot 2=839\):
|
Input: 4 0 566 1 0 566 0 239 30 1 239 0 1 0 30 1 0 | Output: 2 4 2 0
|
Hard
| 4 | 1,263 | 349 | 282 | 16 |
2,073 |
C
|
2073C
| 2,700 |
Master
| 0 | 0 | 0 | 0 | 20 |
|||||||
1,255 |
C
|
1255C
|
C. League of Leesins
| 1,600 |
constructive algorithms; implementation
|
Bob is an avid fan of the video game ""League of Leesins"", and today he celebrates as the League of Leesins World Championship comes to an end! The tournament consisted of \(n\) (\(n \ge 5\)) teams around the world. Before the tournament starts, Bob has made a prediction of the rankings of each team, from \(1\)-st to \(n\)-th. After the final, he compared the prediction with the actual result and found out that the \(i\)-th team according to his prediction ended up at the \(p_i\)-th position (\(1 \le p_i \le n\), all \(p_i\) are unique). In other words, \(p\) is a permutation of \(1, 2, \dots, n\).As Bob's favorite League player is the famous ""3ga"", he decided to write down every \(3\) consecutive elements of the permutation \(p\). Formally, Bob created an array \(q\) of \(n-2\) triples, where \(q_i = (p_i, p_{i+1}, p_{i+2})\) for each \(1 \le i \le n-2\). Bob was very proud of his array, so he showed it to his friend Alice.After learning of Bob's array, Alice declared that she could retrieve the permutation \(p\) even if Bob rearranges the elements of \(q\) and the elements within each triple. Of course, Bob did not believe in such magic, so he did just the same as above to see Alice's respond.For example, if \(n = 5\) and \(p = [1, 4, 2, 3, 5]\), then the original array \(q\) will be \([(1, 4, 2), (4, 2, 3), (2, 3, 5)]\). Bob can then rearrange the numbers within each triple and the positions of the triples to get \([(4, 3, 2), (2, 3, 5), (4, 1, 2)]\). Note that \([(1, 4, 2), (4, 2, 2), (3, 3, 5)]\) is not a valid rearrangement of \(q\), as Bob is not allowed to swap numbers belong to different triples.As Alice's friend, you know for sure that Alice was just trying to show off, so you decided to save her some face by giving her any permutation \(p\) that is consistent with the array \(q\) she was given.
|
The first line contains a single integer \(n\) (\(5 \le n \le 10^5\)) β the size of permutation \(p\).The \(i\)-th of the next \(n-2\) lines contains \(3\) integers \(q_{i, 1}\), \(q_{i, 2}\), \(q_{i, 3}\) (\(1 \le q_{i, j} \le n\)) β the elements of the \(i\)-th triple of the rearranged (shuffled) array \(q_i\), in random order. Remember, that the numbers within each triple can be rearranged and also the positions of the triples can be rearranged.It is guaranteed that there is at least one permutation \(p\) that is consistent with the input.
|
Print \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) such that \(p\) is consistent with array \(q\). If there are multiple answers, print any.
|
Input: 5 4 3 2 2 3 5 4 1 2 | Output: 1 4 2 3 5
|
Medium
| 2 | 1,839 | 548 | 167 | 12 |
|
994 |
B
|
994B
|
B. Knights of a Polygonal Table
| 1,400 |
greedy; implementation; sortings
|
Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than \(k\) other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.Now each knight ponders: how many coins he can have if only he kills other knights?You should answer this question for each knight.
|
The first line contains two integers \(n\) and \(k\) \((1 \le n \le 10^5, 0 \le k \le \min(n-1,10))\) β the number of knights and the number \(k\) from the statement.The second line contains \(n\) integers \(p_1, p_2 ,\ldots,p_n\) \((1 \le p_i \le 10^9)\) β powers of the knights. All \(p_i\) are distinct.The third line contains \(n\) integers \(c_1, c_2 ,\ldots,c_n\) \((0 \le c_i \le 10^9)\) β the number of coins each knight has.
|
Print \(n\) integers β the maximum number of coins each knight can have it only he kills other knights.
|
Consider the first example. The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has. The second knight can kill the first knight and add his coin to his own two. The third knight is the strongest, but he can't kill more than \(k = 2\) other knights. It is optimal to kill the second and the fourth knights: \(2+11+33 = 46\). The fourth knight should kill the first and the second knights: \(33+1+2 = 36\). In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own.In the third example there is only one knight, so he can't kill anyone.
|
Input: 4 24 5 9 71 2 11 33 | Output: 1 3 46 36
|
Easy
| 3 | 578 | 433 | 103 | 9 |
2,000 |
D
|
2000D
|
D. Right Left Wrong
| 1,200 |
greedy; implementation; two pointers
|
Vlad found a strip of \(n\) cells, numbered from left to right from \(1\) to \(n\). In the \(i\)-th cell, there is a positive integer \(a_i\) and a letter \(s_i\), where all \(s_i\) are either 'L' or 'R'.Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations.In one operation, you can choose two indices \(l\) and \(r\) (\(1 \le l < r \le n\)) such that \(s_l\) = 'L' and \(s_r\) = 'R' and do the following: add \(a_l + a_{l + 1} + \dots + a_{r - 1} + a_r\) points to the current score; replace \(s_i\) with '.' for all \(l \le i \le r\), meaning you can no longer choose these indices. For example, consider the following strip: \(3\)\(5\)\(1\)\(4\)\(3\)\(2\)LRLLLR You can first choose \(l = 1\), \(r = 2\) and add \(3 + 5 = 8\) to your score. \(3\)\(5\)\(1\)\(4\)\(3\)\(2\)..LLLR Then choose \(l = 3\), \(r = 6\) and add \(1 + 4 + 3 + 2 = 10\) to your score. \(3\)\(5\)\(1\)\(4\)\(3\)\(2\)...... As a result, it is impossible to perform another operation, and the final score is \(18\).What is the maximum score that can be achieved?
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the strip.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)) β the numbers written on the strip.The third line of each test case contains a string \(s\) of \(n\) characters 'L' and 'R'.It is guaranteed that the sum of the values of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output one integer β the maximum possible number of points that can be scored.
|
Input: 463 5 1 4 3 2LRLLLR22 8LR23 9RL51 2 3 4 5LRLRR | Output: 18 10 0 22
|
Easy
| 3 | 1,097 | 549 | 98 | 20 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.