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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
814 |
D
|
814D
|
D. An overnight dance in discotheque
| 2,000 |
dfs and similar; dp; geometry; greedy; trees
|
The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it?The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area Ci described by a center (xi, yi) and a radius ri. No two ranges' borders have more than one common point, that is for every pair (i, j) (1 β€ i < j β€ n) either ranges Ci and Cj are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges.Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time. But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves β before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above. By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum.
|
The first line of input contains a positive integer n (1 β€ n β€ 1 000) β the number of dancers.The following n lines each describes a dancer: the i-th line among them contains three space-separated integers xi, yi and ri ( - 106 β€ xi, yi β€ 106, 1 β€ ri β€ 106), describing a circular movement range centered at (xi, yi) with radius ri.
|
Output one decimal number β the largest achievable sum of spaciousness over two halves of the night.The output is considered correct if it has a relative or absolute error of at most 10 - 9. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
|
The first sample corresponds to the illustrations in the legend.
|
Input: 52 1 60 4 12 -1 31 -2 14 -1 1 | Output: 138.23007676
|
Hard
| 5 | 1,557 | 332 | 289 | 8 |
1,503 |
A
|
1503A
|
A. Balance the Bits
| 1,600 |
constructive algorithms; greedy
|
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not.You are given a binary string \(s\) of length \(n\). Construct two balanced bracket sequences \(a\) and \(b\) of length \(n\) such that for all \(1\le i\le n\): if \(s_i=1\), then \(a_i=b_i\) if \(s_i=0\), then \(a_i\ne b_i\) If it is impossible, you should report about it.
|
The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(2\le n\le 2\cdot 10^5\), \(n\) is even).The next line contains a string \(s\) of length \(n\), consisting of characters 0 and 1.The sum of \(n\) across all test cases does not exceed \(2\cdot 10^5\).
|
If such two balanced bracked sequences exist, output ""YES"" on the first line, otherwise output ""NO"". You can print each letter in any case (upper or lower).If the answer is ""YES"", output the balanced bracket sequences \(a\) and \(b\) satisfying the conditions on the next two lines.If there are multiple solutions, you may print any.
|
In the first test case, \(a=\)""()()()"" and \(b=\)""((()))"". The characters are equal in positions \(1\), \(3\), \(4\), and \(6\), which are the exact same positions where \(s_i=1\).In the second test case, \(a=\)""()()((()))"" and \(b=\)""(())()()()"". The characters are equal in positions \(1\), \(4\), \(5\), \(7\), \(8\), \(10\), which are the exact same positions where \(s_i=1\).In the third test case, there is no solution.
|
Input: 3 6 101101 10 1001101101 4 1100 | Output: YES ()()() ((())) YES ()()((())) (())()()() NO
|
Medium
| 2 | 506 | 362 | 339 | 15 |
1,840 |
D
|
1840D
|
D. Wooden Toy Festival
| 1,400 |
binary search; greedy; sortings
|
In a small town, there is a workshop specializing in woodwork. Since the town is small, only three carvers work there.Soon, a wooden toy festival is planned in the town. The workshop employees want to prepare for it.They know that \(n\) people will come to the workshop with a request to make a wooden toy. People are different and may want different toys. For simplicity, let's denote the pattern of the toy that the \(i\)-th person wants as \(a_i\) (\(1 \le a_i \le 10^9\)).Each of the carvers can choose an integer pattern \(x\) (\(1 \le x \le 10^9\)) in advance, different carvers can choose different patterns. \(x\) is the integer. During the preparation for the festival, the carvers will perfectly work out the technique of making the toy of the chosen pattern, which will allow them to cut it out of wood instantly. To make a toy of pattern \(y\) for a carver who has chosen pattern \(x\), it will take \(|x - y|\) time, because the more the toy resembles the one he can make instantly, the faster the carver will cope with the work.On the day of the festival, when the next person comes to the workshop with a request to make a wooden toy, the carvers can choose who will take on the job. At the same time, the carvers are very skilled people and can work on orders for different people simultaneously.Since people don't like to wait, the carvers want to choose patterns for preparation in such a way that the maximum waiting time over all people is as small as possible.Output the best maximum waiting time that the carvers can achieve.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then follow the descriptions of the test cases.The first line of a test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of people who will come to the workshop.The second line of a test case contains \(n\) integers \(a_1, a_2, a_3, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the patterns of toys.The sum of all \(n\) values over all test cases does not exceed \(2 \cdot 10^5\).
|
Output \(t\) numbers, each of which is the answer to the corresponding test case β the best maximum waiting time that the carvers can achieve.
|
In the first example, the carvers can choose patterns \(1\), \(7\), \(9\) for preparation.In the second example, the carvers can choose patterns \(3\), \(30\), \(60\) for preparation.In the third example, the carvers can choose patterns \(14\), \(50\), \(85\) for preparation.
|
Input: 561 7 7 9 9 965 4 2 1 30 60914 19 37 59 1 4 4 98 731263 10 1 17 15 11 | Output: 0 2 13 0 1
|
Easy
| 3 | 1,547 | 511 | 142 | 18 |
1,677 |
A
|
1677A
|
A. Tokitsukaze and Strange Inequality
| 1,600 |
brute force; data structures; dp
|
Tokitsukaze has a permutation \(p\) of length \(n\). Recall that a permutation \(p\) of length \(n\) is a sequence \(p_1, p_2, \ldots, p_n\) consisting of \(n\) distinct integers, each of which from \(1\) to \(n\) (\(1 \leq p_i \leq n\)).She wants to know how many different indices tuples \([a,b,c,d]\) (\(1 \leq a < b < c < d \leq n\)) in this permutation satisfy the following two inequalities: \(p_a < p_c\) and \(p_b > p_d\). Note that two tuples \([a_1,b_1,c_1,d_1]\) and \([a_2,b_2,c_2,d_2]\) are considered to be different if \(a_1 \ne a_2\) or \(b_1 \ne b_2\) or \(c_1 \ne c_2\) or \(d_1 \ne d_2\).
|
The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. Each test case consists of two lines.The first line contains a single integer \(n\) (\(4 \leq n \leq 5000\)) β the length of permutation \(p\).The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)) β the permutation \(p\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5000\).
|
For each test case, print a single integer β the number of different \([a,b,c,d]\) tuples.
|
In the first test case, there are \(3\) different \([a,b,c,d]\) tuples.\(p_1 = 5\), \(p_2 = 3\), \(p_3 = 6\), \(p_4 = 1\), where \(p_1 < p_3\) and \(p_2 > p_4\) satisfies the inequality, so one of \([a,b,c,d]\) tuples is \([1,2,3,4]\).Similarly, other two tuples are \([1,2,3,6]\), \([2,3,5,6]\).
|
Input: 365 3 6 1 4 241 2 3 4105 1 6 2 8 3 4 10 9 7 | Output: 3 0 28
|
Medium
| 3 | 607 | 436 | 90 | 16 |
2,030 |
F
|
2030F
|
F. Orangutan Approved Subarrays
| 2,400 |
binary search; data structures; dp; greedy; implementation; two pointers
|
Suppose you have an array \(b\). Initially, you also have a set \(S\) that contains all distinct elements of \(b\). The array \(b\) is called orangutan-approved if it can be emptied by repeatedly performing the following operation: In one operation, select indices \(l\) and \(r\) (\(1 \leq l \leq r \leq |b|\)) such that \(v = b_l = b_{l+1} = \ldots = b_r\) and \(v\) is present in \(S\). Remove \(v\) from \(S\), and simultaneously remove all \(b_i\) such that \(l \leq i \leq r\). Then, reindex the elements \(b_{r+1}, b_{r+2}, \ldots\) as \(b_l, b_{l+1}, \ldots\) accordingly. You are given an array \(a\) of length \(n\) and \(q\) queries. Each query consists of two indices \(l\) and \(r\) (\(1 \le l \le r \le n\)), and you need to determine whether or not the subarray \(a_{l}, a_{l+1}, \ldots, a_r\) is orangutan-approved.
|
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains integers \(n\) and \(q\) (\(1 \leq n,q \leq 2 \cdot 10^5\)) β the size of \(a\) and the number of queries, respectively.The following line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)) β the elements of the array \(a\).The following \(q\) lines contain two integers \(l\) and \(r\) β the endpoints of the subarray for each query (\(1 \leq l \leq r \leq n\)).It is guaranteed that the sum of \(n\) and \(q\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each query, output ""YES"" (without quotes) if the subarray from \(l\) to \(r\) is orangutan-approved, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yES"", ""yes"" and ""Yes"" will be recognized as a positive response).
|
In the first query of the first testcase, the answer is YES. Initially, \(S=\{1,2\}\) and \(b=[1,2,2,1]\) Select \(l=2\) and \(r=3\). Since \(b_2=b_3=2\) is in \(S\), we may erase \(b_2\) and \(b_3\) from the array, as well as erasing \(2\) from \(S\). The set \(S\) becomes \(\{1\}\) and the array becomes \([1,1]\). Select \(l=1\) and \(r=2\). Since \(b_1=b_2=1\) is in \(S\), we may erase \(b_1\) and \(b_2\) from the array, as well as erasing \(1\) from \(S\). The set \(S\) becomes \(\{\}\) and the array becomes \([]\). Since the array is now empty, we can say the original array is orangutan-approved. In the first query of the second testcase, the answer is NO, because it can be shown that the subarray \([2,1,2,1]\) cannot become empty through any sequence of valid operations.
|
Input: 34 21 2 2 11 41 35 31 2 1 2 12 53 51 38 41 2 3 2 1 3 2 31 52 83 56 8 | Output: YES YES NO YES YES YES NO YES YES
|
Expert
| 6 | 831 | 612 | 285 | 20 |
1,311 |
C
|
1311C
|
C. Perform the Combo
| 1,300 |
brute force
|
You want to perform the combo on your opponent in one popular fighting game. The combo is the string \(s\) consisting of \(n\) lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in \(s\). I.e. if \(s=\)""abca"" then you have to press 'a', then 'b', 'c' and 'a' again.You know that you will spend \(m\) wrong tries to perform the combo and during the \(i\)-th try you will make a mistake right after \(p_i\)-th button (\(1 \le p_i < n\)) (i.e. you will press first \(p_i\) buttons right and start performing the combo from the beginning). It is guaranteed that during the \(m+1\)-th try you press all buttons right and finally perform the combo.I.e. if \(s=\)""abca"", \(m=2\) and \(p = [1, 3]\) then the sequence of pressed buttons will be 'a' (here you're making a mistake and start performing the combo from the beginning), 'a', 'b', 'c', (here you're making a mistake and start performing the combo from the beginning), 'a' (note that at this point you will not perform the combo because of the mistake), 'b', 'c', 'a'.Your task is to calculate for each button (letter) the number of times you'll press it.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)) β the length of \(s\) and the number of tries correspondingly.The second line of each test case contains the string \(s\) consisting of \(n\) lowercase Latin letters.The third line of each test case contains \(m\) integers \(p_1, p_2, \dots, p_m\) (\(1 \le p_i < n\)) β the number of characters pressed right during the \(i\)-th try.It is guaranteed that the sum of \(n\) and the sum of \(m\) both does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\), \(\sum m \le 2 \cdot 10^5\)).It is guaranteed that the answer for each letter does not exceed \(2 \cdot 10^9\).
|
For each test case, print the answer β \(26\) integers: the number of times you press the button 'a', the number of times you press the button 'b', \(\dots\), the number of times you press the button 'z'.
|
The first test case is described in the problem statement. Wrong tries are ""a"", ""abc"" and the final try is ""abca"". The number of times you press 'a' is \(4\), 'b' is \(2\) and 'c' is \(2\).In the second test case, there are five wrong tries: ""co"", ""codeforc"", ""cod"", ""co"", ""codeforce"" and the final try is ""codeforces"". The number of times you press 'c' is \(9\), 'd' is \(4\), 'e' is \(5\), 'f' is \(3\), 'o' is \(9\), 'r' is \(3\) and 's' is \(1\).
|
Input: 3 4 2 abca 1 3 10 5 codeforces 2 8 3 2 9 26 10 qwertyuioplkjhgfdsazxcvbnm 20 10 1 2 3 5 10 5 9 4 | Output: 4 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 9 4 5 3 0 0 0 0 0 0 0 0 9 0 0 3 1 0 0 0 0 0 0 0 2 1 1 2 9 2 2 2 5 2 2 2 1 1 5 4 11 8 2 7 5 1 10 1 5 2
|
Easy
| 1 | 1,208 | 838 | 204 | 13 |
685 |
A
|
685A
|
A. Robbers' watch
| 1,700 |
brute force; combinatorics; dp; math
|
Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation.Note that to display number 0 section of the watches is required to have at least one place.Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number.
|
The first line of the input contains two integers, given in the decimal notation, n and m (1 β€ n, m β€ 109) β the number of hours in one day and the number of minutes in one hour, respectively.
|
Print one integer in decimal notation β the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.
|
In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2).In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
|
Input: 2 3 | Output: 4
|
Medium
| 4 | 1,113 | 192 | 149 | 6 |
993 |
B
|
993B
|
B. Open Communication
| 1,900 |
bitmasks; brute force
|
Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you.Both participants communicated to each other a set of pairs of numbers, that includes the pair given to them. Each pair in the communicated sets comprises two different numbers.Determine if you can with certainty deduce the common number, or if you can determine with certainty that both participants know the number but you do not.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 12\)) β the number of pairs the first participant communicated to the second and vice versa.The second line contains \(n\) pairs of integers, each between \(1\) and \(9\), β pairs of numbers communicated from first participant to the second.The third line contains \(m\) pairs of integers, each between \(1\) and \(9\), β pairs of numbers communicated from the second participant to the first.All pairs within each set are distinct (in particular, if there is a pair \((1,2)\), there will be no pair \((2,1)\) within the same set), and no pair contains the same number twice.It is guaranteed that the two sets do not contradict the statements, in other words, there is pair from the first set and a pair from the second set that share exactly one number.
|
If you can deduce the shared number with certainty, print that number.If you can with certainty deduce that both participants know the shared number, but you do not know it, print \(0\).Otherwise print \(-1\).
|
In the first example the first participant communicated pairs \((1,2)\) and \((3,4)\), and the second communicated \((1,5)\), \((3,4)\). Since we know that the actual pairs they received share exactly one number, it can't be that they both have \((3,4)\). Thus, the first participant has \((1,2)\) and the second has \((1,5)\), and at this point you already know the shared number is \(1\).In the second example either the first participant has \((1,2)\) and the second has \((1,5)\), or the first has \((3,4)\) and the second has \((6,4)\). In the first case both of them know the shared number is \(1\), in the second case both of them know the shared number is \(4\). You don't have enough information to tell \(1\) and \(4\) apart.In the third case if the first participant was given \((1,2)\), they don't know what the shared number is, since from their perspective the second participant might have been given either \((1,3)\), in which case the shared number is \(1\), or \((2,3)\), in which case the shared number is \(2\). While the second participant does know the number with certainty, neither you nor the first participant do, so the output is \(-1\).
|
Input: 2 21 2 3 41 5 3 4 | Output: 1
|
Hard
| 2 | 599 | 825 | 209 | 9 |
621 |
C
|
621C
|
C. Wet Shark and Flowers
| 1,700 |
combinatorics; math; number theory; probabilities
|
There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from li to ri. Wet Shark has it's favourite prime number p, and he really likes it! If for any pair of neighbouring sharks i and j the product siΒ·sj is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.
|
The first line of the input contains two space-separated integers n and p (3 β€ n β€ 100 000, 2 β€ p β€ 109) β the number of sharks and Wet Shark's favourite prime number. It is guaranteed that p is prime.The i-th of the following n lines contains information about i-th shark β two space-separated integers li and ri (1 β€ li β€ ri β€ 109), the range of flowers shark i can produce. Remember that si is chosen equiprobably among all integers from li to ri, inclusive.
|
Print a single real number β the expected number of dollars that the sharks receive in total. You 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 .
|
A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (s0, s1, s2) each shark grows: (1, 420, 420420): note that s0Β·s1 = 420, s1Β·s2 = 176576400, and s2Β·s0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars. (1, 420, 420421): now, the product s2Β·s0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1 will receive 2000. The total is 4000. (1, 421, 420420): total is 4000 (1, 421, 420421): total is 0. (2, 420, 420420): total is 6000. (2, 420, 420421): total is 6000. (2, 421, 420420): total is 6000. (2, 421, 420421): total is 4000.The expected value is .In the second sample, no combination of quantities will garner the sharks any money.
|
Input: 3 21 2420 421420420 420421 | Output: 4500.0
|
Medium
| 4 | 667 | 461 | 326 | 6 |
1,356 |
B2
|
1356B2
|
B2. Decrement
| 0 |
*special
|
Implement a unitary operation on a register of \(N\) qubits that decrements the number written in the register modulo \(2^N\).Your operation should take a register of type LittleEndian - an array of qubits that encodes an unsigned integer in little-endian format, with the least significant bit written first (corresponding to the array element with index 0). The ""output"" of your solution is the state in which it left the input qubits.For example, if the qubits passed to your operation are in the state \(\frac{1}{\sqrt{2}}(|00\rangle + |01\rangle) = \frac{1}{\sqrt{2}}(|0\rangle + |2\rangle)\), they should be transformed to the state \(\frac{1}{\sqrt{2}}(|(0-1) \mod 2^2\rangle + |(2-1) \mod 2^2\rangle) = \frac{1}{\sqrt{2}}(|3\rangle + |1\rangle) = \frac{1}{\sqrt{2}}(|11\rangle + |10\rangle)\).Your code should have the following signature (note that your operation should have Adjoint and Controlled variants defined for it; is Adj+Ctl in the operation signature will generate them automatically based on your code):namespace Solution { open Microsoft.Quantum.Arithmetic; open Microsoft.Quantum.Intrinsic; operation Solve (register : LittleEndian) : Unit is Adj+Ctl { // your code here }}Your code is not allowed to use measurements or arbitrary rotation gates (so, for example, using the library operation IncrementByInteger will cause runtime error). This operation can be implemented using just the X gate and its controlled variants.
|
Beginner
| 1 | 1,447 | 0 | 0 | 13 |
||||
1,379 |
A
|
1379A
|
A. Acacius and String
| 1,500 |
brute force; implementation; strings
|
Acacius is studying strings theory. Today he came with the following problem.You are given a string \(s\) of length \(n\) consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string ""abacaba"" occurs as a substring in a resulting string exactly once?Each question mark should be replaced with exactly one lowercase English letter. For example, string ""a?b?c"" can be transformed into strings ""aabbc"" and ""azbzc"", but can't be transformed into strings ""aabc"", ""a?bbc"" and ""babbc"".Occurrence of a string \(t\) of length \(m\) in the string \(s\) of length \(n\) as a substring is a index \(i\) (\(1 \leq i \leq n - m + 1\)) such that string \(s[i..i+m-1]\) consisting of \(m\) consecutive symbols of \(s\) starting from \(i\)-th equals to string \(t\). For example string ""ababa"" has two occurrences of a string ""aba"" as a substring with \(i = 1\) and \(i = 3\), but there are no occurrences of a string ""aba"" in the string ""acba"" as a substring.Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string ""abacaba"" occurs as a substring in a resulting string exactly once.
|
First line of input contains an integer \(T\) (\(1 \leq T \leq 5000\)), number of test cases. \(T\) pairs of lines with test case descriptions follow.The first line of a test case description contains a single integer \(n\) (\(7 \leq n \leq 50\)), length of a string \(s\).The second line of a test case description contains string \(s\) of length \(n\) consisting of lowercase English letters and question marks.
|
For each test case output an answer for it.In case if there is no way to replace question marks in string \(s\) with a lowercase English letters in such a way that there is exactly one occurrence of a string ""abacaba"" in the resulting string as a substring output ""No"".Otherwise output ""Yes"" and in the next line output a resulting string consisting of \(n\) lowercase English letters. If there are multiple possible strings, output any.You may print every letter in ""Yes"" and ""No"" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
|
In first example there is exactly one occurrence of a string ""abacaba"" in the string ""abacaba"" as a substring.In second example seven question marks can be replaced with any seven lowercase English letters and with ""abacaba"" in particular.In sixth example there are two occurrences of a string ""abacaba"" as a substring.
|
Input: 6 7 abacaba 7 ??????? 11 aba?abacaba 11 abacaba?aba 15 asdf???f???qwer 11 abacabacaba | Output: Yes abacaba Yes abacaba Yes abadabacaba Yes abacabadaba No No
|
Medium
| 3 | 1,269 | 413 | 609 | 13 |
1,459 |
B
|
1459B
|
B. Move and Turn
| 1,300 |
dp; math
|
A robot is standing at the origin of the infinite two-dimensional plane. Each second the robot moves exactly \(1\) meter in one of the four cardinal directions: north, south, west, and east. For the first step the robot can choose any of the four directions, but then at the end of every second it has to turn 90 degrees left or right with respect to the direction it just moved in. For example, if the robot has just moved north or south, the next step it takes has to be either west or east, and vice versa.The robot makes exactly \(n\) steps from its starting position according to the rules above. How many different points can the robot arrive to at the end? The final orientation of the robot can be ignored.
|
The only line contains a single integer \(n\) (\(1 \leq n \leq 1000\)) β the number of steps the robot makes.
|
Print a single integer β the number of different possible locations after exactly \(n\) steps.
|
In the first sample case, the robot will end up 1 meter north, south, west, or east depending on its initial direction.In the second sample case, the robot will always end up \(\sqrt{2}\) meters north-west, north-east, south-west, or south-east.
|
Input: 1 | Output: 4
|
Easy
| 2 | 714 | 109 | 94 | 14 |
1,002 |
A4
|
1002A4
|
A4. Generate W state
| 1,900 |
*special
|
You are given N = 2k qubits (0 β€ k β€ 4) in zero state . Your task is to create a generalized W state on them. Generalized W state is an equal superposition of all basis states on N qubits that have Hamming weight equal to 1:For example, for N = 1, .You have to implement an operation which takes an array of N qubits as an input and has no output. The ""output"" of the operation is the state in which it leaves the qubits.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[]) : () { body { // your code here } }}
|
Hard
| 1 | 621 | 0 | 0 | 10 |
||||
1,904 |
C
|
1904C
|
C. Array Game
| 1,400 |
binary search; brute force; data structures; sortings; two pointers
|
You are given an array \(a\) of \(n\) positive integers. In one operation, you must pick some \((i, j)\) such that \(1\leq i < j\leq |a|\) and append \(|a_i - a_j|\) to the end of the \(a\) (i.e. increase \(n\) by \(1\) and set \(a_n\) to \(|a_i - a_j|\)). Your task is to minimize and print the minimum value of \(a\) after performing \(k\) operations.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2\le n\le 2\cdot 10^3\), \(1\le k\le 10^9\)) β the length of the array and the number of operations you should perform.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1\le a_i\le 10^{18}\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(4\cdot 10^6\).
|
For each test case, print a single integer β the smallest possible value of the minimum of array \(a\) after performing \(k\) operations.
|
In the first test case, after any \(k=2\) operations, the minimum value of \(a\) will be \(1\).In the second test case, an optimal strategy is to first pick \(i=1, j=2\) and append \(|a_1 - a_2| = 3\) to the end of \(a\), creating \(a=[7, 4, 15, 12, 3]\). Then, pick \(i=3, j=4\) and append \(|a_3 - a_4| = 3\) to the end of \(a\), creating \(a=[7, 4, 15, 12, 3, 3]\). In the final operation, pick \(i=5, j=6\) and append \(|a_5 - a_6| = 0\) to the end of \(a\). Then the minimum value of \(a\) will be \(0\).In the third test case, an optimal strategy is to first pick \(i=2, j=3\) to append \(|a_2 - a_3| = 3\) to the end of \(a\). Any second operation will still not make the minimum value of \(a\) be less than \(3\).
|
Input: 45 23 9 7 15 14 37 4 15 126 242 47 50 54 62 792 1500000000000000000 1000000000000000000 | Output: 1 0 3 500000000000000000
|
Easy
| 5 | 353 | 606 | 137 | 19 |
1,711 |
B
|
1711B
|
B. Party
| 1,300 |
brute force; graphs
|
A club plans to hold a party and will invite some of its \(n\) members. The \(n\) members are identified by the numbers \(1, 2, \dots, n\). If member \(i\) is not invited, the party will gain an unhappiness value of \(a_i\).There are \(m\) pairs of friends among the \(n\) members. As per tradition, if both people from a friend pair are invited, they will share a cake at the party. The total number of cakes eaten will be equal to the number of pairs of friends such that both members have been invited.However, the club's oven can only cook two cakes at a time. So, the club demands that the total number of cakes eaten is an even number.What is the minimum possible total unhappiness value of the party, respecting the constraint that the total number of cakes eaten is even?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n \leq 10^5\), \(0 \leq m \leq \min(10^5,\frac{n(n-1)}{2})\)) β the number of club members and pairs of friends.The second line of each test case contains \(n\) integers \(a_1,a_2, \dots,a_n\) (\(0 \leq a_i \leq 10^4\)) β the unhappiness value array.Each of the next \(m\) lines contains two integers \(x\) and \(y\) (\(1 \leq x,y \leq n\), \(x \neq y\)) indicating that \(x\) and \(y\) are friends. Each unordered pair \((x,y)\) appears at most once in each test case.It is guaranteed that both the sum of \(n\) and the sum of \(m\) over all test cases do not exceed \(10^5\).
|
For each test case, print a line containing a single integer β the minimum possible unhappiness value of a valid party.
|
In the first test case, all members can be invited. So the unhappiness value is \(0\).In the second test case, the following options are possible: invite \(1\) and \(2\) (\(0\) cakes eaten, unhappiness value equal to \(3\)); invite \(2\) and \(3\) (\(0\) cakes eaten, unhappiness value equal to \(2\)); invite only \(1\) (\(0\) cakes eaten, unhappiness value equal to \(4\)); invite only \(2\) (\(0\) cakes eaten, unhappiness value equal to \(5\)); invite only \(3\) (\(0\) cakes eaten, unhappiness value equal to \(3\)); invite nobody (\(0\) cakes eaten, unhappiness value equal to \(6\)). The minimum unhappiness value is achieved by inviting \(2\) and \(3\).In the third test case, inviting members \(3,4,5\) generates a valid party with the minimum possible unhappiness value.
|
Input: 41 013 12 1 31 35 51 2 3 4 51 21 31 41 52 35 51 1 1 1 11 22 33 44 55 1 | Output: 0 2 3 2
|
Easy
| 2 | 779 | 821 | 119 | 17 |
1,807 |
E
|
1807E
|
E. Interview
| 1,300 |
binary search; implementation; interactive
|
This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read the guide for participants.Before the last stage of the exam, the director conducted an interview. He gave Gon \(n\) piles of stones, the \(i\)-th pile having \(a_i\) stones.Each stone is identical and weighs \(1\) grams, except for one special stone that is part of an unknown pile and weighs \(2\) grams. A picture of the first test case. Pile \(2\) has the special stone. The piles have weights of \(1,3,3,4,5\), respectively. Gon can only ask the director questions of one kind: he can choose \(k\) piles, and the director will tell him the total weight of the piles chosen. More formally, Gon can choose an integer \(k\) (\(1 \leq k \leq n\)) and \(k\) unique piles \(p_1, p_2, \dots, p_k\) (\(1 \leq p_i \leq n\)), and the director will return the total weight \(m_{p_1} + m_{p_2} + \dots + m_{p_k}\), where \(m_i\) denotes the weight of pile \(i\). Gon is tasked with finding the pile that contains the special stone. However, the director is busy. Help Gon find this pile in at most \(\mathbf{30}\) queries.
|
The input data contains several test cases. The first line contains one integer \(t\) (\(1 \leq t \leq 1000\)) β 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 \leq n \leq 2 \cdot 10^5\)) β the number of piles.The second line of each test case contains \(n\) integers \(a_i\) (\(1 \leq a_i \leq 10^4\)) β the number of stones in each pile.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).After reading the input for each test case, proceed with the interaction as follows.
|
In the first test case, the stone with weight two is located in pile \(2\), as shown in the picture. We perform the following interaction: \(\texttt{? 4 1 2 3 4}\) β ask the total weight of piles \(1\), \(2\), \(3\), and \(4\). The total weight we receive back is \(1+3+3+4=11\). \(\texttt{? 2 2 3}\) β ask the total weight of piles \(2\) and \(3\). The total weight we receive back is \(3+3=6\). \(\texttt{? 1 2}\) β ask the total weight of pile \(2\). The total weight we receive back is \(3\). \(\texttt{! 2}\) β we have figured out that pile \(2\) contains the special stone, so we output it and move on to the next test case. In the second test case, the stone with weight two is located on index \(7\). We perform the following interaction: \(\texttt{? 4 2 3 5 6}\) β ask the total weight of piles \(2\), \(3\), \(5\), and \(6\). The total weight we receive back is \(2+3+3+4=12\). \(\texttt{? 2 1 4}\) β ask the total weight of piles \(1\) and \(4\). The total weight we receive back is \(1+5=6\). \(\texttt{! 7}\) β we have somehow figured out that pile \(7\) contains the special stone, so we output it and end the interaction.
|
Input: 2 5 1 2 3 4 5 11 6 3 7 1 2 3 5 3 4 2 12 6 | Output: ? 4 1 2 3 4 ? 2 2 3 ? 1 2 ! 2 ? 4 2 3 5 6 ? 2 1 4 ! 7
|
Easy
| 3 | 1,127 | 606 | 0 | 18 |
|
1,946 |
B
|
1946B
|
B. Maximum Sum
| 1,100 |
dp; greedy; math
|
You have an array \(a\) of \(n\) integers.You perform exactly \(k\) operations on it. In one operation, you select any contiguous subarray of the array \(a\) (possibly empty) and insert the sum of this subarray anywhere in the array.Your task is to find the maximum possible sum of the array after \(k\) such operations.As this number can be very large, output the answer modulo \(10^9 + 7\).Reminder: the remainder of a number \(x\) modulo \(p\) is the smallest non-negative \(y\) such that there exists an integer \(q\) and \(x = p \cdot q + y\).
|
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n, k \le 2 \cdot 10^5\)) β the length of the array \(a\) and the number of operations, respectively.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\)) β the array \(a\) itself.It is guaranteed that the sum of the values of \(n\) and \(k\) for all test cases does not exceed \(2 \cdot 10^5\).
|
For each test, output a single integer β the maximum sum of the array that can be obtained after \(k\) operations modulo \(10^9 + 7\).
|
In the first test case, it is advantageous to take an empty subarray of the array twice and insert the sum of the empty subarray (zero) anywhere, then the sum of the resulting array will be \((-4) + (-7) + 0 + 0 = -11\), modulo \(10^9 + 7\) this is \(999\,999\,996\).In the second test case, it is advantageous to take the sum of the entire array three times and place it anywhere in the array, then one of the possible sequences of actions: [\(2, 2, 8\)] \(\rightarrow\) [\(2, 2, 8, 12\)] \(\rightarrow\) [\(2, 2, 8, 12, 24\)] \(\rightarrow\) [\(2, 2, 8, 12, 24, 48\)], the sum of the final array is \(2 + 2 + 8 + 12 + 24 + 48 = 96\).In the fourth test case, it is advantageous to take a subarray of the array consisting of the first three numbers (i.e. consisting of the numbers \(4, -2\) and \(8\)) and insert its sum at the beginning of the array, thereby obtaining the array [\(10, 4, -2, 8, -12, 9\)], the sum of this array is \(17\).In the seventh test case, it will always be advantageous for us to take an empty subarray of the array. In this case, the sum of the resulting array will not differ from the sum of the original. The answer will be the sum of the original array, taken modulo β \(42\), because \((-6 \cdot (10^9 + 7) + 42 = -6\,000\,000\,000)\).
|
Input: 122 2-4 -73 32 2 81 775 14 -2 8 -12 97 48 14 -9 6 0 -1 37 1005 3 -8 12 -5 -9 36 1000-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -10000000002 11000000000 85 40 0 0 0 06 1048973 757292 58277 -38574 27475 9999847 1-1000 1000 -1000 1000 -1000 1000 -100010 10050408293874 -3498597 7374783 295774930 -48574034 26623784 498754833 -294875830 283045804 85938045 | Output: 999999996 96 896 17 351 716455332 42 2 0 897909241 0 416571966
|
Easy
| 3 | 548 | 620 | 134 | 19 |
1,490 |
F
|
1490F
|
F. Equalize the Array
| 1,500 |
binary search; data structures; greedy; math; sortings
|
Polycarp was gifted an array \(a\) of length \(n\). Polycarp considers an array beautiful if there exists a number \(C\), such that each number in the array occurs either zero or \(C\) times. Polycarp wants to remove some elements from the array \(a\) to make it beautiful.For example, if \(n=6\) and \(a = [1, 3, 2, 1, 4, 2]\), then the following options are possible to make the array \(a\) array beautiful: Polycarp removes elements at positions \(2\) and \(5\), array \(a\) becomes equal to \([1, 2, 1, 2]\); Polycarp removes elements at positions \(1\) and \(6\), array \(a\) becomes equal to \([3, 2, 1, 4]\); Polycarp removes elements at positions \(1, 2\) and \(6\), array \(a\) becomes equal to \([2, 1, 4]\); Help Polycarp determine the minimum number of elements to remove from the array \(a\) to make it beautiful.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case consists of one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output one integer β the minimum number of elements that Polycarp has to remove from the array \(a\) to make it beautiful.
|
Input: 3 6 1 3 2 1 4 2 4 100 100 4 100 8 1 2 3 3 3 2 6 6 | Output: 2 1 2
|
Medium
| 5 | 826 | 463 | 142 | 14 |
|
1,970 |
A2
|
1970A2
|
A2. Balanced Unshuffle (Medium)
| 2,400 |
brute force; constructive algorithms; trees
|
The differences with the easy version of this problem are highlighted in bold.A parentheses sequence is a string consisting of characters ""("" and "")"", for example ""(()(("".A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations into it, for example ""(()(()))"".The balance of a parentheses sequence is defined as the number of opening parentheses ""("" minus the number of closing parentheses "")"". For example, the balance of the sequence ""(()(("" is 3.A balanced parentheses sequence can also be defined as a parentheses sequence with balance 0 such that each of its prefixes has a non-negative balance.We define the balanced shuffle operation that takes a parentheses sequence and returns a parentheses sequence as follows: first, for every character of the input sequence, we compute the balance of the prefix of the sequence before that character and write those down in a table together with the positions of the characters in the input sequence, for example:Prefix balance01212321Position12345678Character(()(()))Then, we sort the columns of this table in increasing order of prefix balance, breaking ties in decreasing order of position. In the above example, we get:Prefix balance01112223Position18427536Character()(()())The last row of this table forms another parentheses sequence, in this case ""()(()())"". This sequence is called the result of applying the balanced shuffle operation to the input sequence, or in short just the balanced shuffle of the input sequence.Surprisingly, it turns out that the balanced shuffle of any balanced parentheses sequence is always another balanced parentheses sequence (we will omit the proof for brevity). Even more surprisingly, the balanced shuffles of two different balanced parentheses sequences are always different, therefore the balanced shuffle operation is a bijection on the set of balanced parentheses sequences of any given length (we will omit this proof, too).You are given a balanced parentheses sequence. Find its preimage: the balanced parentheses sequence the balanced shuffle of which is equal to the given sequence.
|
The only line of input contains a string \(s\) consisting only of characters ""("" and "")"". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding \(1\,000\).
|
Print the balanced parentheses sequence \(t\) such that the balanced shuffle of \(t\) is equal to \(s\). It is guaranteed that the answer always exists and is unique.
|
Input: ()(()()) | Output: (()(()))
|
Expert
| 3 | 2,184 | 209 | 166 | 19 |
|
1,801 |
B
|
1801B
|
B. Buying gifts
| 1,800 |
data structures; greedy; sortings
|
Little Sasha has two friends, whom he wants to please with gifts on the Eighth of March. To do this, he went to the largest shopping center in the city.There are \(n\) departments in the mall, each of which has exactly two stores. For convenience, we number the departments with integers from \(1\) to \(n\). It is known that gifts in the first store of the \(i\) department cost \(a_i\) rubles, and in the second store of the \(i\) department β \(b_i\) rubles.Entering the mall, Sasha will visit each of the \(n\) departments of the mall, and in each department, he will enter exactly one store. When Sasha gets into the \(i\)-th department, he will perform exactly one of two actions: Buy a gift for the first friend, spending \(a_i\) rubles on it. Buy a gift for the second friend, spending \(b_i\) rubles on it. Sasha is going to buy at least one gift for each friend. Moreover, he wants to pick up gifts in such a way that the price difference of the most expensive gifts bought for friends is as small as possible so that no one is offended.More formally: let \(m_1\) be the maximum price of a gift bought to the first friend, and \(m_2\) be the maximum price of a gift bought to the second friend. Sasha wants to choose gifts in such a way as to minimize the value of \(\lvert m_1 - m_2 \rvert\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1\,000\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 500\,000\)) β the number of departments in the mall.Each of the following \(n\) lines of each test case contains two integers \(a_i\) and \(b_i\) (\(0 \le a_i, b_i \le 10^9\)) β the prices of gifts in the first and second store of the \(i\) department, respectively.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(500\,000\).
|
Print one integer β the minimum price difference of the most expensive gifts bought to friends.
|
In the first test case, Sasha has two possible options: buy a gift for the first friend in the first department, and the second friend β in the second department, or vice versa. In the first case, \(m_1 = m_2 = 1\), and in the second case β \(m_1 = m_2 = 2\). In both cases, the answer is \(0\). In the second test case, you can buy gifts for the first friend in the \(2\), \(4\) and \(5\) departments, and for the second friend β in the \(1\) and \(3\) departments.So \(m_1 = \max(2, 4, 2) = 4\), \(m_2 = \max(5, 3) = 5\). The answer is \(\lvert 4 - 5 \rvert = 1\).
|
Input: 221 22 151 52 73 34 102 5 | Output: 0 1
|
Medium
| 3 | 1,303 | 597 | 95 | 18 |
605 |
E
|
605E
|
E. Intergalaxy Trips
| 2,700 |
probabilities; shortest paths
|
The scientists have recently discovered wormholes β objects in space that allow to travel very long distances between galaxies and star systems. The scientists know that there are n galaxies within reach. You are in the galaxy number 1 and you need to get to the galaxy number n. To get from galaxy i to galaxy j, you need to fly onto a wormhole (i, j) and in exactly one galaxy day you will find yourself in galaxy j. Unfortunately, the required wormhole is not always available. Every galaxy day they disappear and appear at random. However, the state of wormholes does not change within one galaxy day. A wormhole from galaxy i to galaxy j exists during each galaxy day taken separately with probability pij. You can always find out what wormholes exist at the given moment. At each moment you can either travel to another galaxy through one of wormholes that exist at this moment or you can simply wait for one galaxy day to see which wormholes will lead from your current position at the next day.Your task is to find the expected value of time needed to travel from galaxy 1 to galaxy n, if you act in the optimal way. It is guaranteed that this expected value exists.
|
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of galaxies within reach.Then follows a matrix of n rows and n columns. Each element pij represents the probability that there is a wormhole from galaxy i to galaxy j. All the probabilities are given in percents and are integers. It is guaranteed that all the elements on the main diagonal are equal to 100.
|
Print a single real value β the expected value of the time needed to travel from galaxy 1 to galaxy n if one acts in an optimal way. 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 second sample the wormhole from galaxy 1 to galaxy 2 appears every day with probability equal to 0.3. The expected value of days one needs to wait before this event occurs is .
|
Input: 3100 50 500 100 800 0 100 | Output: 1.750000000000000
|
Master
| 2 | 1,174 | 391 | 366 | 6 |
1,101 |
F
|
1101F
|
F. Trucks and Cities
| 2,400 |
binary search; dp
|
There are \(n\) cities along the road, which can be represented as a straight line. The \(i\)-th city is situated at the distance of \(a_i\) kilometers from the origin. All cities are situated in the same direction from the origin. There are \(m\) trucks travelling from one city to another. Each truck can be described by \(4\) integers: starting city \(s_i\), finishing city \(f_i\), fuel consumption \(c_i\) and number of possible refuelings \(r_i\). The \(i\)-th truck will spend \(c_i\) litres of fuel per one kilometer. When a truck arrives in some city, it can be refueled (but refueling is impossible in the middle of nowhere). The \(i\)-th truck can be refueled at most \(r_i\) times. Each refueling makes truck's gas-tank full. All trucks start with full gas-tank.All trucks will have gas-tanks of the same size \(V\) litres. You should find minimum possible \(V\) such that all trucks can reach their destinations without refueling more times than allowed.
|
First line contains two integers \(n\) and \(m\) (\(2 \le n \le 400\), \(1 \le m \le 250000\)) β the number of cities and trucks.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\), \(a_i < a_{i+1}\)) β positions of cities in the ascending order.Next \(m\) lines contains \(4\) integers each. The \(i\)-th line contains integers \(s_i\), \(f_i\), \(c_i\), \(r_i\) (\(1 \le s_i < f_i \le n\), \(1 \le c_i \le 10^9\), \(0 \le r_i \le n\)) β the description of the \(i\)-th truck.
|
Print the only integer β minimum possible size of gas-tanks \(V\) such that all trucks can reach their destinations.
|
Let's look at queries in details: the \(1\)-st truck must arrive at position \(7\) from \(2\) without refuelling, so it needs gas-tank of volume at least \(50\). the \(2\)-nd truck must arrive at position \(17\) from \(2\) and can be refueled at any city (if it is on the path between starting point and ending point), so it needs gas-tank of volume at least \(48\). the \(3\)-rd truck must arrive at position \(14\) from \(10\), there is no city between, so it needs gas-tank of volume at least \(52\). the \(4\)-th truck must arrive at position \(17\) from \(10\) and can be refueled only one time: it's optimal to refuel at \(5\)-th city (position \(14\)) so it needs gas-tank of volume at least \(40\). the \(5\)-th truck has the same description, so it also needs gas-tank of volume at least \(40\). the \(6\)-th truck must arrive at position \(14\) from \(2\) and can be refueled two times: first time in city \(2\) or \(3\) and second time in city \(4\) so it needs gas-tank of volume at least \(55\).
|
Input: 7 6 2 5 7 10 14 15 17 1 3 10 0 1 7 12 7 4 5 13 3 4 7 10 1 4 7 10 1 1 5 11 2 | Output: 55
|
Expert
| 2 | 967 | 514 | 116 | 11 |
691 |
B
|
691B
|
B. s-palindrome
| 1,600 |
implementation; strings
|
Let's call a string ""s-palindrome"" if it is symmetric about the middle of the string. For example, the string ""oHo"" is ""s-palindrome"", but the string ""aa"" is not. The string ""aa"" is not ""s-palindrome"", because the second half of it is not a mirror reflection of the first half. English alphabet You are given a string s. Check if the string is ""s-palindrome"".
|
The only line contains the string s (1 β€ |s| β€ 1000) which consists of only English letters.
|
Print ""TAK"" if the string s is ""s-palindrome"" and ""NIE"" otherwise.
|
Input: oXoxoXo | Output: TAK
|
Medium
| 2 | 373 | 92 | 72 | 6 |
|
711 |
D
|
711D
|
D. Directed Roads
| 1,900 |
combinatorics; dfs and similar; graphs; math
|
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1 to n. There are n directed roads in the Udayland. i-th of them goes from town i to some other town ai (ai β i). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town A to town B before the flip, it will go from town B to town A after.ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns A1, A2, ..., Ak (k > 1) such that for every 1 β€ i < k there is a road from town Ai to town Ai + 1 and another road from town Ak to town A1. In other words, the roads are confusing if some of them form a directed cycle of some towns.Now ZS the Coder wonders how many sets of roads (there are 2n variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing.Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.
|
The first line of the input contains single integer n (2 β€ n β€ 2Β·105) β the number of towns in Udayland.The next line contains n integers a1, a2, ..., an (1 β€ ai β€ n, ai β i), ai denotes a road going from town i to town ai.
|
Print a single integer β the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109 + 7.
|
Consider the first sample case. There are 3 towns and 3 roads. The towns are numbered from 1 to 3 and the roads are , , initially. Number the roads 1 to 3 in this order. The sets of roads that ZS the Coder can flip (to make them not confusing) are {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}. Note that the empty set is invalid because if no roads are flipped, then towns 1, 2, 3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 6 possible sets ZS the Coder can flip.The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.
|
Input: 32 3 1 | Output: 6
|
Hard
| 4 | 1,160 | 223 | 205 | 7 |
1,998 |
D
|
1998D
|
D. Determine Winning Islands in Race
| 2,100 |
data structures; dp; graphs; greedy; shortest paths
|
MOOOOOOOOOOOOOOOOOβ Bessie the Cow, The Art of Racing on IslandsTwo of Farmer John's cows, Bessie and Elsie, are planning to race on \(n\) islands. There are \(n - 1\) main bridges, connecting island \(i\) to island \(i + 1\) for all \(1 \leq i \leq n - 1\). Additionally, there are \(m\) alternative bridges. Elsie can use both main and alternative bridges, while Bessie can only use main bridges. All bridges are one way and can only be used to travel from an island with a lower index to an island with a higher index.Initially, Elsie starts on island \(1\), and Bessie starts on island \(s\). The cows alternate turns, with Bessie making the first turn. Suppose the cow is on island \(i\). During a cow's turn, if there are any bridges connecting island \(i\) to island \(j\), then the cow can move to island \(j\). Then, island \(i\) collapses, and all bridges connecting to island \(i\) also collapse. Also, note the following: If there are no bridges connecting island \(i\) to another island, then island \(i\) collapses, and this cow is eliminated from the race. If the other cow is also on island \(i\), then after this cow moves to another island, the island collapses, and the other cow is eliminated from the race. After an island or bridge collapses, no cows may use them. If a cow is eliminated, their turn is skipped for the rest of the race. The race ends once either cow reaches island \(n\). It can be shown that regardless of the cows' strategies, at least one cow reaches island \(n\). Bessie wins if and only if she reaches island \(n\) first.For each \(1 \leq s \leq n - 1\), determine whether Bessie wins if she starts the race on island \(s\). Assume both cows follow optimal strategies to ensure their own respective victories.
|
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains \(n\) and \(m\) (\(2 \leq n \leq 2 \cdot 10^5\), \(0 \leq m \leq 2 \cdot 10^5\)) β the number of islands and the number of alternative bridges.The next \(m\) lines of each test case contain \(u\) and \(v\) (\(1 \leq u < v \leq n\)) β the islands that the alternative bridge connects. It is guaranteed all alternative bridges are distinct, and they do not coincide with the main bridges.It is guaranteed that neither the sum of \(n\) nor the sum of \(m\) over all test cases exceeds \(2 \cdot 10^5\).
|
For each test case, output a binary string of length \(n - 1\) on a new line. The \(i\)'th character is \(1\) if it is possible for Bessie to win if she starts on island \(i\). Otherwise, it is \(0\).
|
In the first test case, there are no alternative bridges for Elsie to overtake Bessie and reach island \(n\) first, so Bessie will win on all islands because she always moves first.In the second case, Bessie will lose if she starts on island \(3\) because: Bessie's Turn: Take a main bridge from island \(3\) to island \(4\). Elsie's Turn: Take a main bridge from island \(1\) to island \(2\). Bessie's Turn: Take a main bridge from island \(4\) to island \(5\). Elsie's Turn: Take an alternative bridge from island \(2\) to island \(6\). Elsie reaches island \(n\) first.
|
Input: 56 06 12 66 11 510 41 31 62 73 815 32 84 98 15 | Output: 11111 11011 10011 100001111 11000111000111
|
Hard
| 5 | 1,753 | 623 | 200 | 19 |
1,916 |
A
|
1916A
|
A. 2023
| 800 |
constructive algorithms; implementation; math; number theory
|
In a sequence \(a\), whose product was equal to \(2023\), \(k\) numbers were removed, leaving a sequence \(b\) of length \(n\). Given the resulting sequence \(b\), find any suitable sequence \(a\) and output which \(k\) elements were removed from it, or state that such a sequence could not have existed.Notice that you are not guaranteed that such array exists.
|
Each test consists of several test cases. The first line 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 \(n\) (\(1 \le n, k \le 5\)) β the size of sequence \(b\) and the number of numbers removed from sequence \(a\).The second line contains \(n\) integers \(b_1,b_2, \ldots,b_n\) (\(1 \leq b_i \leq 2023\)) β the remaining sequence. The values of \(b_i\) might not be divisors of \(2023\).
|
For each test case, output ""YES"" if the sequence \(a\) exists, and in the following line output \(k\) non-negative integers that were removed from the sequence \(a\). If the sequence \(a\) does not exist, output ""NO"" in a single line.You can output the answer in any case (uppercase or lowercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive answers.
|
In third test case product is equal to \(289 \cdot 7 = 2023\).In fourth test case product is already equal to \(2023\).In seventh test case product is equal to \(7 \cdot 17 \cdot 17 = 2023\).
|
Input: 72 25 23 17 17 74 21 289 1 13 17 17 171 12891 120231 31 | Output: NO NO YES 7 1 YES 1 YES 7 YES 1 YES 7 17 17
|
Beginner
| 4 | 362 | 531 | 405 | 19 |
509 |
A
|
509A
|
A. Maximum in Table
| 800 |
brute force; implementation
|
An n Γ n table a is defined as follows: The first row and the first column contain ones, that is: ai, 1 = a1, i = 1 for all i = 1, 2, ..., n. Each of the remaining numbers in the table is equal to the sum of the number above it and the number to the left of it. In other words, the remaining elements are defined by the formula ai, j = ai - 1, j + ai, j - 1. These conditions define all the values in the table.You are given a number n. You need to determine the maximum value in the n Γ n table defined by the rules above.
|
The only line of input contains a positive integer n (1 β€ n β€ 10) β the number of rows and columns of the table.
|
Print a single line containing a positive integer m β the maximum value in the table.
|
In the second test the rows of the table look as follows: {1, 1, 1, 1, 1}, {1, 2, 3, 4, 5}, {1, 3, 6, 10, 15}, {1, 4, 10, 20, 35}, {1, 5, 15, 35, 70}.
|
Input: 1 | Output: 1
|
Beginner
| 2 | 523 | 112 | 85 | 5 |
1,029 |
C
|
1029C
|
C. Maximal Intersection
| 1,600 |
greedy; math; sortings
|
You are given \(n\) segments on a number line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.The intersection of a sequence of segments is such a maximal set of points (not necesserily having integer coordinates) that each point lies within every segment from the sequence. If the resulting set isn't empty, then it always forms some continuous segment. The length of the intersection is the length of the resulting segment or \(0\) in case the intersection is an empty set.For example, the intersection of segments \([1;5]\) and \([3;10]\) is \([3;5]\) (length \(2\)), the intersection of segments \([1;5]\) and \([5;7]\) is \([5;5]\) (length \(0\)) and the intersection of segments \([1;5]\) and \([6;6]\) is an empty set (length \(0\)).Your task is to remove exactly one segment from the given sequence in such a way that the intersection of the remaining \((n - 1)\) segments has the maximal possible length.
|
The first line contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β the number of segments in the sequence.Each of the next \(n\) lines contains two integers \(l_i\) and \(r_i\) (\(0 \le l_i \le r_i \le 10^9\)) β the description of the \(i\)-th segment.
|
Print a single integer β the maximal possible length of the intersection of \((n - 1)\) remaining segments after you remove exactly one segment from the sequence.
|
In the first example you should remove the segment \([3;3]\), the intersection will become \([2;3]\) (length \(1\)). Removing any other segment will result in the intersection \([3;3]\) (length \(0\)).In the second example you should remove the segment \([1;3]\) or segment \([2;6]\), the intersection will become \([2;4]\) (length \(2\)) or \([1;3]\) (length \(2\)), respectively. Removing any other segment will result in the intersection \([2;3]\) (length \(1\)).In the third example the intersection will become an empty set no matter the segment you remove.In the fourth example you will get the intersection \([3;10]\) (length \(7\)) if you remove the segment \([1;5]\) or the intersection \([1;5]\) (length \(4\)) if you remove the segment \([3;10]\).
|
Input: 41 32 60 43 3 | Output: 1
|
Medium
| 3 | 1,043 | 265 | 162 | 10 |
666 |
E
|
666E
|
E. Forensic Examination
| 3,100 |
data structures; string suffix structures
|
The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word.Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure.All the m leaflets that are brought by the spy are numbered from 1 to m. After that it's needed to get the answer to q queries of the following kind: ""In which leaflet in the segment of numbers [l, r] the substring of the Absolutely Inadmissible Swearword [pl, pr] occurs more often?"".The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him!
|
The first line contains the string s (1 β€ |s| β€ 5Β·105) β the Absolutely Inadmissible Swearword. The string s consists of only lowercase English letters.The second line contains the only integer m (1 β€ m β€ 5Β·104) β the number of texts of leaflets for expertise.Each of the next m lines contains the only string ti β the text of the i-th leaflet. The sum of lengths of all leaflet texts doesn't exceed 5Β·104. The text of the leaflets consists of only lowercase English letters.The next line contains integer q (1 β€ q β€ 5Β·105) β the number of queries for expertise.Finally, each of the last q lines contains four integers l, r, pl, pr (1 β€ l β€ r β€ m, 1 β€ pl β€ pr β€ |s|), where |s| is the length of the Absolutely Inadmissible Swearword.
|
Print q lines. The i-th of them should contain two integers β the number of the text with the most occurences and the number of occurences of the substring [pl, pr] of the string s. If there are several text numbers print the smallest one.
|
Input: suffixtree3suffixtreesareawesomecartesiantreeisworsethansegmenttreenyeeheeheee21 2 1 101 3 9 10 | Output: 1 13 4
|
Master
| 2 | 864 | 733 | 239 | 6 |
|
1,468 |
K
|
1468K
|
K. The Robot
| 1,600 |
brute force; implementation
|
There is a robot on a checkered field that is endless in all directions. Initially, the robot is located in the cell with coordinates \((0, 0)\). He will execute commands which are described by a string of capital Latin letters 'L', 'R', 'D', 'U'. When a command is executed, the robot simply moves in the corresponding direction: 'L': one cell to the left (the \(x\)-coordinate of the current cell decreases by \(1\)); 'R': one cell to the right (the \(x\)-coordinate of the current cell is increased by \(1\)); 'D': one cell down (the \(y\)-coordinate of the current cell decreases by \(1\)); 'U': one cell up (the \(y\)-coordinate of the current cell is increased by \(1\)). Your task is to put an obstacle in one cell of the field so that after executing the commands, the robot will return to the original cell of its path \((0, 0)\). Of course, an obstacle cannot be placed in the starting cell \((0, 0)\). It is guaranteed that if the obstacle is not placed, then the robot will not return to the starting cell.An obstacle affects the movement of the robot in the following way: if it tries to go in a certain direction, and there is an obstacle, then it simply remains in place (the obstacle also remains, that is, it does not disappear).Find any such cell of the field (other than \((0, 0)\)) that if you put an obstacle there, the robot will return to the cell \((0, 0)\) after the execution of all commands. If there is no solution, then report it.
|
The first line contains one integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.Each test case consists of a single line containing \(s\) β the sequence of commands, which are uppercase Latin letters 'L', 'R', 'D', 'U' only. The length of \(s\) is between \(1\) and \(5000\), inclusive. Additional constraint on \(s\): executing this sequence of commands leads the robot to some cell other than \((0, 0)\), if there are no obstacles.The sum of lengths of all \(s\) in a test doesn't exceed \(5000\).
|
For each test case print a single line: if there is a solution, print two integers \(x\) and \(y\) (\(-10^9 \le x,y \le 10^9\)) such that an obstacle in \((x, y)\) will force the robot to return back to the cell \((0, 0)\); otherwise, print two zeroes (i. e. 0 0). If there are multiple answers, you can print any of them.
|
Input: 4LRUUDLLLUUDDDUUUUU | Output: -1 0 1 2 0 0 0 1
|
Medium
| 2 | 1,459 | 510 | 322 | 14 |
|
450 |
B
|
450B
|
B. Jzzhu and Sequences
| 1,300 |
implementation; math
|
Jzzhu has invented a kind of sequences, they meet the following property:You are given x and y, please calculate fn modulo 1000000007 (109 + 7).
|
The first line contains two integers x and y (|x|, |y| β€ 109). The second line contains a single integer n (1 β€ n β€ 2Β·109).
|
Output a single integer representing fn modulo 1000000007 (109 + 7).
|
In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.In the second sample, f2 = - 1; - 1 modulo (109 + 7) equals (109 + 6).
|
Input: 2 33 | Output: 1
|
Easy
| 2 | 144 | 123 | 68 | 4 |
1,984 |
A
|
1984A
|
A. Strange Splitting
| 800 |
constructive algorithms
|
Define the range of a non-empty array to be the maximum value minus the minimum value. For example, the range of \([1,4,2]\) is \(4-1=3\).You are given an array \(a_1, a_2, \ldots, a_n\) of length \(n \geq 3\). It is guaranteed \(a\) is sorted.You have to color each element of \(a\) red or blue so that: the range of the red elements does not equal the range of the blue elements, and there is at least one element of each color. If there does not exist any such coloring, you should report it. If there are multiple valid colorings, you can print any of them.
|
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 an integer \(n\) (\(3 \leq n \leq 50\)) β the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)). It is guaranteed \(a_1 \leq a_2 \leq \ldots \leq a_{n - 1} \leq a_{n}\).
|
For each test case, if it is impossible to color \(a\) to satisfy all the constraints, output \(\texttt{NO}\).Otherwise, first output \(\texttt{YES}\).Then, output a string \(s\) of length \(n\). For \(1 \leq i \leq n\), if you color \(a_i\) red, \(s_i\) should be \(\texttt{R}\). For \(1 \leq i \leq n\), if you color \(a_i\) blue, \(s_i\) should be \(\texttt{B}\).
|
In the first test case, given the array \([1, 1, 2, 2]\), we can color the second element blue and the remaining elements red; then the range of the red elements \([1, 2, 2]\) is \(2-1=1\), and the range of the blue elements \([1]\) is \(1-1=0\).In the second test case, we can color the first, second, fourth and fifth elements \([1, 2, 4, 5]\) blue and the remaining elements \([3]\) red. The range of the red elements is \(3 - 3 = 0\) and the range of the blue elements is \(5 - 1 = 4\), which are different.In the third test case, it can be shown there is no way to color \(a = [3, 3, 3]\) to satisfy the constraints.
|
Input: 741 1 2 251 2 3 4 533 3 341 2 2 231 2 231 1 231 9 84 | Output: YES RBRR YES BBRBB NO YES RBBR YES RRB YES BRR YES BRB
|
Beginner
| 1 | 561 | 390 | 366 | 19 |
986 |
A
|
986A
|
A. Fair
| 1,600 |
graphs; greedy; number theory; shortest paths
|
Some company is going to hold a fair in Byteland. There are \(n\) towns in Byteland and \(m\) two-way roads between towns. Of course, you can reach any town from any other town using roads.There are \(k\) types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least \(s\) different types of goods. It costs \(d(u,v)\) coins to bring goods from town \(u\) to town \(v\) where \(d(u,v)\) is the length of the shortest path from \(u\) to \(v\). Length of a path is the number of roads in this path.The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of \(n\) towns.
|
There are \(4\) integers \(n\), \(m\), \(k\), \(s\) in the first line of input (\(1 \le n \le 10^{5}\), \(0 \le m \le 10^{5}\), \(1 \le s \le k \le min(n, 100)\)) β the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.In the next line there are \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_{i} \le k\)), where \(a_i\) is the type of goods produced in the \(i\)-th town. It is guaranteed that all integers between \(1\) and \(k\) occur at least once among integers \(a_{i}\).In the next \(m\) lines roads are described. Each road is described by two integers \(u\) \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) β the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.
|
Print \(n\) numbers, the \(i\)-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town \(i\). Separate numbers with spaces.
|
Let's look at the first sample.To hold a fair in town \(1\) you can bring goods from towns \(1\) (\(0\) coins), \(2\) (\(1\) coin) and \(4\) (\(1\) coin). Total numbers of coins is \(2\).Town \(2\): Goods from towns \(2\) (\(0\)), \(1\) (\(1\)), \(3\) (\(1\)). Sum equals \(2\).Town \(3\): Goods from towns \(3\) (\(0\)), \(2\) (\(1\)), \(4\) (\(1\)). Sum equals \(2\).Town \(4\): Goods from towns \(4\) (\(0\)), \(1\) (\(1\)), \(5\) (\(1\)). Sum equals \(2\).Town \(5\): Goods from towns \(5\) (\(0\)), \(4\) (\(1\)), \(3\) (\(2\)). Sum equals \(3\).
|
Input: 5 5 4 31 2 4 3 21 22 33 44 14 5 | Output: 2 2 2 2 3
|
Medium
| 4 | 730 | 895 | 169 | 9 |
2,003 |
C
|
2003C
|
C. Turtle and Good Pairs
| 1,200 |
constructive algorithms; greedy; sortings; strings
|
Turtle gives you a string \(s\), consisting of lowercase Latin letters.Turtle considers a pair of integers \((i, j)\) (\(1 \le i < j \le n\)) to be a pleasant pair if and only if there exists an integer \(k\) such that \(i \le k < j\) and both of the following two conditions hold: \(s_k \ne s_{k + 1}\); \(s_k \ne s_i\) or \(s_{k + 1} \ne s_j\). Besides, Turtle considers a pair of integers \((i, j)\) (\(1 \le i < j \le n\)) to be a good pair if and only if \(s_i = s_j\) or \((i, j)\) is a pleasant pair.Turtle wants to reorder the string \(s\) so that the number of good pairs is maximized. Please help him!
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the string.The second line of each test case contains a string \(s\) of length \(n\), consisting of lowercase Latin letters.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the string \(s\) after reordering so that the number of good pairs is maximized. If there are multiple answers, print any of them.
|
In the first test case, \((1, 3)\) is a good pair in the reordered string. It can be seen that we can't reorder the string so that the number of good pairs is greater than \(1\). bac and cab can also be the answer.In the second test case, \((1, 2)\), \((1, 4)\), \((1, 5)\), \((2, 4)\), \((2, 5)\), \((3, 5)\) are good pairs in the reordered string. efddd can also be the answer.
|
Input: 53abc5edddf6turtle8pppppppp10codeforces | Output: acb ddedf urtlet pppppppp codeforces
|
Easy
| 4 | 611 | 489 | 157 | 20 |
2,060 |
C
|
2060C
|
C. Game of Mathletes
| 900 |
games; greedy; sortings; two pointers
|
Alice and Bob are playing a game. There are \(n\) (\(n\) is even) integers written on a blackboard, represented by \(x_1, x_2, \ldots, x_n\). There is also a given integer \(k\) and an integer score that is initially \(0\). The game lasts for \(\frac{n}{2}\) turns, in which the following events happen sequentially: Alice selects an integer from the blackboard and erases it. Let's call Alice's chosen integer \(a\). Bob selects an integer from the blackboard and erases it. Let's call Bob's chosen integer \(b\). If \(a+b=k\), add \(1\) to score. Alice is playing to minimize the score while Bob is playing to maximize the score. Assuming both players use optimal strategies, what is the score after the game ends?
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The first line of each test case contains two integers \(n\) and \(k\) (\(2 \leq n \leq 2\cdot 10^5, 1 \leq k \leq 2\cdot n\), \(n\) is even).The second line of each test case contains \(n\) integers \(x_1,x_2,\ldots,x_n\) (\(1 \leq x_i \leq n\)) β the integers on the blackboard.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output the score if both players play optimally.
|
In the first test case, one way the game may go is as follows: Alice selects \(1\) and Bob selects \(3\). The score increases as \(1+3=4\). Now the two integers remaining on the blackboard are \(2\) and \(2\). Alice and Bob both select \(2\). The score increases as \(2+2=4\). The game ends as the blackboard now has no integers. In the third test case, it is impossible for the sum of Alice and Bob's selected integers to be \(1\), so we answer \(0\).Note that this is just an example of how the game may proceed for demonstration purposes. This may not be Alice or Bob's most optimal strategies.
|
Input: 44 41 2 3 28 151 2 3 4 5 6 7 86 11 1 1 1 1 116 93 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 | Output: 2 1 0 4
|
Beginner
| 4 | 716 | 465 | 68 | 20 |
848 |
B
|
848B
|
B. Rooter's Song
| 1,900 |
constructive algorithms; data structures; geometry; implementation; sortings; two pointers
|
Wherever the destination is, whoever we meet, let's render this song together.On a Cartesian coordinate plane lies a rectangular stage of size w Γ h, represented by a rectangle with corners (0, 0), (w, 0), (w, h) and (0, h). It can be seen that no collisions will happen before one enters the stage.On the sides of the stage stand n dancers. The i-th of them falls into one of the following groups: Vertical: stands at (xi, 0), moves in positive y direction (upwards); Horizontal: stands at (0, yi), moves in positive x direction (rightwards). According to choreography, the i-th dancer should stand still for the first ti milliseconds, and then start moving in the specified direction at 1 unit per millisecond, until another border is reached. It is guaranteed that no two dancers have the same group, position and waiting time at the same time.When two dancers collide (i.e. are on the same point at some time when both of them are moving), they immediately exchange their moving directions and go on. Dancers stop when a border of the stage is reached. Find out every dancer's stopping position.
|
The first line of input contains three space-separated positive integers n, w and h (1 β€ n β€ 100 000, 2 β€ w, h β€ 100 000) β the number of dancers and the width and height of the stage, respectively.The following n lines each describes a dancer: the i-th among them contains three space-separated integers gi, pi, and ti (1 β€ gi β€ 2, 1 β€ pi β€ 99 999, 0 β€ ti β€ 100 000), describing a dancer's group gi (gi = 1 β vertical, gi = 2 β horizontal), position, and waiting time. If gi = 1 then pi = xi; otherwise pi = yi. It's guaranteed that 1 β€ xi β€ w - 1 and 1 β€ yi β€ h - 1. It is guaranteed that no two dancers have the same group, position and waiting time at the same time.
|
Output n lines, the i-th of which contains two space-separated integers (xi, yi) β the stopping position of the i-th dancer in the input.
|
The first example corresponds to the initial setup in the legend, and the tracks of dancers are marked with different colours in the following figure. In the second example, no dancers collide.
|
Input: 8 10 81 1 101 4 131 7 11 8 22 2 02 5 142 6 02 6 1 | Output: 4 810 58 810 610 21 87 810 6
|
Hard
| 6 | 1,099 | 670 | 137 | 8 |
27 |
C
|
27C
|
C. Unordered Subsequence
| 1,900 |
constructive algorithms; greedy
|
The sequence is called ordered if it is non-decreasing or non-increasing. For example, sequnces [3, 1, 1, 0] and [1, 2, 3, 100] are ordered, but the sequence [1, 3, 3, 1] is not. You are given a sequence of numbers. You are to find it's shortest subsequence which is not ordered.A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of the input contains one integer n (1 β€ n β€ 105). The second line contains n space-separated integers β the given sequence. All numbers in this sequence do not exceed 106 by absolute value.
|
If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length k of the shortest such subsequence. Then output k integers from the range [1..n] β indexes of the elements of this subsequence. If there are several solutions, output any of them.
|
Input: 567 499 600 42 23 | Output: 31 3 5
|
Hard
| 2 | 438 | 205 | 285 | 0 |
|
1,513 |
B
|
1513B
|
B. AND Sequences
| 1,400 |
bitmasks; combinatorics; constructive algorithms; math
|
A sequence of \(n\) non-negative integers (\(n \ge 2\)) \(a_1, a_2, \dots, a_n\) is called good if for all \(i\) from \(1\) to \(n-1\) the following condition holds true: $$$\(a_1 \: \& \: a_2 \: \& \: \dots \: \& \: a_i = a_{i+1} \: \& \: a_{i+2} \: \& \: \dots \: \& \: a_n,\)\( where \)\&\( denotes the bitwise AND operation.You are given an array \)a\( of size \)n\( (\)n \geq 2\(). Find the number of permutations \)p\( of numbers ranging from \)1\( to \)n\(, for which the sequence \)a_{p_1}\(, \)a_{p_2}\(, ... ,\)a_{p_n}\( is good. Since this number can be large, output it modulo \)10^9+7$$$.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)), denoting the number of test cases.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the size of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β the elements of the array.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
Output \(t\) lines, where the \(i\)-th line contains the number of good permutations in the \(i\)-th test case modulo \(10^9 + 7\).
|
In the first test case, since all the numbers are equal, whatever permutation we take, the sequence is good. There are a total of \(6\) permutations possible with numbers from \(1\) to \(3\): \([1,2,3]\), \([1,3,2]\), \([2,1,3]\), \([2,3,1]\), \([3,1,2]\), \([3,2,1]\).In the second test case, it can be proved that no permutation exists for which the sequence is good.In the third test case, there are a total of \(36\) permutations for which the sequence is good. One of them is the permutation \([1,5,4,2,3]\) which results in the sequence \(s=[0,0,3,2,0]\). This is a good sequence because \( s_1 = s_2 \: \& \: s_3 \: \& \: s_4 \: \& \: s_5 = 0\), \( s_1 \: \& \: s_2 = s_3 \: \& \: s_4 \: \& \: s_5 = 0\), \( s_1 \: \& \: s_2 \: \& \: s_3 = s_4 \: \& \: s_5 = 0\), \( s_1 \: \& \: s_2 \: \& \: s_3 \: \& \: s_4 = s_5 = 0\).
|
Input: 4 3 1 1 1 5 1 2 3 4 5 5 0 2 0 3 0 4 1 3 5 1 | Output: 6 0 36 4
|
Easy
| 4 | 601 | 455 | 131 | 15 |
852 |
I
|
852I
|
I. Dating
| 2,300 |
brute force; dfs and similar; graphs; trees
|
This story is happening in a town named BubbleLand. There are n houses in BubbleLand. In each of these n houses lives a boy or a girl. People there really love numbers and everyone has their favorite number f. That means that the boy or girl that lives in the i-th house has favorite number equal to fi.The houses are numerated with numbers 1 to n.The houses are connected with n - 1 bidirectional roads and you can travel from any house to any other house in the town. There is exactly one path between every pair of houses.A new dating had agency opened their offices in this mysterious town and the citizens were very excited. They immediately sent q questions to the agency and each question was of the following format: a b β asking how many ways are there to choose a couple (boy and girl) that have the same favorite number and live in one of the houses on the unique path from house a to house b. Help the dating agency to answer the questions and grow their business.
|
The first line contains an integer n (1 β€ n β€ 105), the number of houses in the town.The second line contains n integers, where the i-th number is 1 if a boy lives in the i-th house or 0 if a girl lives in i-th house.The third line contains n integers, where the i-th number represents the favorite number fi (1 β€ fi β€ 109) of the girl or boy that lives in the i-th house.The next n - 1 lines contain information about the roads and the i-th line contains two integers ai and bi (1 β€ ai, bi β€ n) which means that there exists road between those two houses. It is guaranteed that it's possible to reach any house from any other.The following line contains an integer q (1 β€ q β€ 105), the number of queries.Each of the following q lines represents a question and consists of two integers a and b (1 β€ a, b β€ n).
|
For each of the q questions output a single number, the answer to the citizens question.
|
In the first question from house 1 to house 3, the potential couples are (1, 3) and (6, 3).In the second question from house 7 to house 5, the potential couples are (7, 6), (4, 2) and (4, 5).
|
Input: 71 0 0 1 0 1 09 2 9 2 2 9 92 61 24 26 53 67 421 37 5 | Output: 23
|
Expert
| 4 | 976 | 809 | 88 | 8 |
768 |
F
|
768F
|
F. Barrels and boxes
| 2,300 |
brute force; combinatorics; math; number theory; probabilities
|
Tarly has two different type of items, food boxes and wine barrels. There are f food boxes and w wine barrels. Tarly stores them in various stacks and each stack can consist of either food boxes or wine barrels but not both. The stacks are placed in a line such that no two stacks of food boxes are together and no two stacks of wine barrels are together.The height of a stack is defined as the number of items in the stack. Two stacks are considered different if either their heights are different or one of them contains food and other contains wine.Jon Snow doesn't like an arrangement if any stack of wine barrels has height less than or equal to h. What is the probability that Jon Snow will like the arrangement if all arrangement are equiprobably?Two arrangement of stacks are considered different if exists such i, that i-th stack of one arrangement is different from the i-th stack of the other arrangement.
|
The first line of input contains three integers f, w, h (0 β€ f, w, h β€ 105) β number of food boxes, number of wine barrels and h is as described above. It is guaranteed that he has at least one food box or at least one wine barrel.
|
Output the probability that Jon Snow will like the arrangement. The probability is of the form , then you need to output a single integer pΒ·q - 1 mod (109 + 7).
|
In the first example f = 1, w = 1 and h = 1, there are only two possible arrangement of stacks and Jon Snow doesn't like any of them.In the second example f = 1, w = 2 and h = 1, there are three arrangements. Jon Snow likes the (1) and (3) arrangement. So the probabilty is .
|
Input: 1 1 1 | Output: 0
|
Expert
| 5 | 916 | 231 | 160 | 7 |
1,446 |
A
|
1446A
|
A. Knapsack
| 1,300 |
constructive algorithms; greedy; sortings
|
You have a knapsack with the capacity of \(W\). There are also \(n\) items, the \(i\)-th one has weight \(w_i\). You want to put some of these items into the knapsack in such a way that their total weight \(C\) is at least half of its size, but (obviously) does not exceed it. Formally, \(C\) should satisfy: \(\lceil \frac{W}{2}\rceil \le C \le W\). Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible. If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.
|
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 integers \(n\) and \(W\) (\(1 \le n \le 200\,000\), \(1\le W \le 10^{18}\)). The second line of each test case contains \(n\) integers \(w_1, w_2, \dots, w_n\) (\(1 \le w_i \le 10^9\)) β weights of the items.The sum of \(n\) over all test cases does not exceed \(200\,000\).
|
For each test case, if there is no solution, print a single integer \(-1\). If there exists a solution consisting of \(m\) items, print \(m\) in the first line of the output and \(m\) integers \(j_1\), \(j_2\), ..., \(j_m\) (\(1 \le j_i \le n\), all \(j_i\) are distinct) in the second line of the output β indices of the items you would like to pack into the knapsack.If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.
|
In the first test case, you can take the item of weight \(3\) and fill the knapsack just right.In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is \(-1\).In the third test case, you fill the knapsack exactly in half.
|
Input: 3 1 3 3 6 2 19 8 19 69 9 4 7 12 1 1 1 17 1 1 1 | Output: 1 1 -1 6 1 2 3 5 6 7
|
Easy
| 3 | 639 | 473 | 539 | 14 |
1,891 |
E
|
1891E
|
E. Brukhovich and Exams
| 2,500 |
brute force; greedy; implementation; math; sortings
|
The boy Smilo is learning algorithms with a teacher named Brukhovich.Over the course of the year, Brukhovich will administer \(n\) exams. For each exam, its difficulty \(a_i\) is known, which is a non-negative integer.Smilo doesn't like when the greatest common divisor of the difficulties of two consecutive exams is equal to \(1\). Therefore, he considers the sadness of the academic year to be the number of such pairs of exams. More formally, the sadness is the number of indices \(i\) (\(1 \leq i \leq n - 1\)) such that \(gcd(a_i, a_{i+1}) = 1\), where \(gcd(x, y)\) is the greatest common divisor of integers \(x\) and \(y\).Brukhovich wants to minimize the sadness of the year of Smilo. To do this, he can set the difficulty of any exam to \(0\). However, Brukhovich doesn't want to make his students' lives too easy. Therefore, he will perform this action no more than \(k\) times.Help Smilo determine the minimum sadness that Brukhovich can achieve if he performs no more than \(k\) operations.As a reminder, the greatest common divisor (GCD) of two non-negative integers \(x\) and \(y\) is the maximum integer that is a divisor of both \(x\) and \(y\) and is denoted as \(gcd(x, y)\). In particular, \(gcd(x, 0) = gcd(0, x) = x\) for any non-negative integer \(x\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 10^5\)) β the total number of exams and the maximum number of exams that can be simplified, respectively. The second line of each test case contains \(n\) integers \(a_1, a_2, a_3, \ldots, a_n\) β the elements of array \(a\), which are the difficulties of the exams (\(0 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(10^5\).
|
For each test case, output the minimum possible sadness that can be achieved by performing no more than \(k\) operations.
|
In the first test case, a sadness of \(1\) can be achieved. To this, you can simplify the second and fourth exams. After this, there will be only one pair of adjacent exams with a greatest common divisor (GCD) equal to one, which is the first and second exams.In the second test case, a sadness of \(0\) can be achieved by simplifying the second and fourth exams.
|
Input: 95 21 3 5 7 95 23 5 7 9 118 217 15 10 1 1 5 14 85 31 1 1 1 15 51 1 1 1 119 71 1 2 3 4 5 5 6 6 7 8 9 10 1 1 1 2 3 115 62 1 1 1 1 2 1 1 2 1 1 1 2 1 25 21 1 1 1 25 21 0 1 0 1 | Output: 1 0 2 2 0 5 5 2 1
|
Expert
| 5 | 1,276 | 616 | 121 | 18 |
1,562 |
B
|
1562B
|
B. Scenes From a Memory
| 1,000 |
brute force; constructive algorithms; implementation; math; number theory
|
During the hypnosis session, Nicholas suddenly remembered a positive integer \(n\), which doesn't contain zeros in decimal notation. Soon, when he returned home, he got curious: what is the maximum number of digits that can be removed from the number so that the number becomes not prime, that is, either composite or equal to one?For some numbers doing so is impossible: for example, for number \(53\) it's impossible to delete some of its digits to obtain a not prime integer. However, for all \(n\) in the test cases of this problem, it's guaranteed that it's possible to delete some of their digits to obtain a not prime number.Note that you cannot remove all the digits from the number.A prime number is a number that has no divisors except one and itself. A composite is a number that has more than two divisors. \(1\) is neither a prime nor a composite number.
|
Each test contains multiple test cases.The first line contains one positive integer \(t\) (\(1 \le t \le 10^3\)), denoting the number of test cases. Description of the test cases follows.The first line of each test case contains one positive integer \(k\) (\(1 \le k \le 50\)) β the number of digits in the number.The second line of each test case contains a positive integer \(n\), which doesn't contain zeros in decimal notation (\(10^{k-1} \le n < 10^{k}\)). It is guaranteed that it is always possible to remove less than \(k\) digits to make the number not prime.It is guaranteed that the sum of \(k\) over all test cases does not exceed \(10^4\).
|
For every test case, print two numbers in two lines. In the first line print the number of digits, that you have left in the number. In the second line print the digits left after all delitions. If there are multiple solutions, print any.
|
In the first test case, you can't delete \(2\) digits from the number \(237\), as all the numbers \(2\), \(3\), and \(7\) are prime. However, you can delete \(1\) digit, obtaining a number \(27 = 3^3\).In the second test case, you can delete all digits except one, as \(4 = 2^2\) is a composite number.
|
Input: 7 3 237 5 44444 3 221 2 35 3 773 1 4 30 626221626221626221626221626221 | Output: 2 27 1 4 1 1 2 35 2 77 1 4 1 6
|
Beginner
| 5 | 867 | 652 | 238 | 15 |
448 |
E
|
448E
|
E. Divisors
| 2,200 |
brute force; dfs and similar; implementation; number theory
|
Bizon the Champion isn't just friendly, he also is a rigorous coder.Let's define function f(a), where a is a sequence of integers. Function f(a) returns the following sequence: first all divisors of a1 go in the increasing order, then all divisors of a2 go in the increasing order, and so on till the last element of sequence a. For example, f([2, 9, 1]) = [1, 2, 1, 3, 9, 1].Let's determine the sequence Xi, for integer i (i β₯ 0): X0 = [X] ([X] is a sequence consisting of a single number X), Xi = f(Xi - 1) (i > 0). For example, at X = 6 we get X0 = [6], X1 = [1, 2, 3, 6], X2 = [1, 1, 2, 1, 3, 1, 2, 3, 6].Given the numbers X and k, find the sequence Xk. As the answer can be rather large, find only the first 105 elements of this sequence.
|
A single line contains two space-separated integers β X (1 β€ X β€ 1012) and k (0 β€ k β€ 1018).
|
Print the elements of the sequence Xk in a single line, separated by a space. If the number of elements exceeds 105, then print only the first 105 elements.
|
Input: 6 1 | Output: 1 2 3 6
|
Hard
| 4 | 743 | 92 | 156 | 4 |
|
1,958 |
B
|
1958B
|
B. Clock in the Pool
| 1,400 |
*special; math
|
You are swimming in the pool, and you need to control the time of swimming.The pool has a clock that cycles between three different modes: showing water temperature, showing air temperature, and showing time. At the start of the \(0\)-th second it starts showing water temperature, at the start of the \(k\)-th second it switches to air temperature. At the start of the \(2k\)-th second, it switches to showing time.At the start of the \(3k\)-th second the clock starts showing water temperature again, at the start of the \(4k\)-th second β air temperature, and so on.You looked at the clock during the \(m\)-th second to check the time, but it may be that the clock is not showing time right now. How much time do you have to wait to see the time on the clock?Answer \(t\) independent test cases.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Next \(t\) cases follow.The first and only line of each test case contains two integers \(k\) and \(m\) (\(1 \le k \le 10^8; 1 \le m \le 10^9\)) β the length of the period of the clock and the moment of time you check the clock.
|
For each test case, print a single integer β the time you have to wait from the moment \(m\) until the moment the clock starts showing the time.You can assume that you are able to read the time instantly the moment it displays on the clock.
|
In the first test case, the clock will start showing time during the \(2\)-nd second, so you have to wait \(2 - 1 = 1\) second.In the second test case, the clock shows time from the \(10\)-th until the \(15\)-th second, so during the \(14\)-th second it shows time, and you don't have to wait.In the third test case, during the \(15\)-th second, the clock is already showing water temperature. So you have to wait till the \(25\)-th second, when the clock starts showing time again. You'll wait \(25 - 15 = 10\) seconds.In the fourth test case, the clock will start showing time at the start of the \(110\)-th second, so you'll wait \(110 - 110 = 0\) seconds.
|
Input: 51 15 145 1510 11099999999 1000000000 | Output: 1 0 10 0 99999989
|
Easy
| 2 | 798 | 326 | 240 | 19 |
1,080 |
A
|
1080A
|
A. Petya and Origami
| 800 |
math
|
Petya is having a party soon, and he has decided to invite his \(n\) friends.He wants to make invitations in the form of origami. For each invitation, he needs two red sheets, five green sheets, and eight blue sheets. The store sells an infinite number of notebooks of each color, but each notebook consists of only one color with \(k\) sheets. That is, each notebook contains \(k\) sheets of either red, green, or blue.Find the minimum number of notebooks that Petya needs to buy to invite all \(n\) of his friends.
|
The first line contains two integers \(n\) and \(k\) (\(1\leq n, k\leq 10^8\)) β the number of Petya's friends and the number of sheets in each notebook respectively.
|
Print one number β the minimum number of notebooks that Petya needs to buy.
|
In the first example, we need \(2\) red notebooks, \(3\) green notebooks, and \(5\) blue notebooks.In the second example, we need \(5\) red notebooks, \(13\) green notebooks, and \(20\) blue notebooks.
|
Input: 3 5 | Output: 10
|
Beginner
| 1 | 516 | 166 | 75 | 10 |
1,776 |
N
|
1776N
|
N. Count Permutations
| 3,500 |
math
|
You are given a string \(s\) with length \(n-1\) whose characters are either \(\texttt{<}\) or \(\texttt{>}\).Count the permutations \(p_1, \, p_2, \, \dots, \, p_n\) of \(1, \, 2, \, \dots, \, n\) such that, for all \(i = 1, \, 2, \, \dots, \, n - 1\), if \(s_i\) is \(\texttt{<}\) then \(p_i < p_{i+1}\) and if \(s_i\) is \(\texttt{>}\) then \(p_i > p_{i+1}\).Since this number can be very large, compute its logarithm in base \(2\).
|
The first line contains a single integer \(n\) (\(2 \le n \le 100\,000\)).The second line contains a string \(s\) of length \(n-1\); each character of \(s\) is either \(\texttt{<}\) or \(\texttt{>}\).
|
Print the logarithm in base \(2\) of the number of permutations satisfying the constraints described in the statement.Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\). Formally, let your answer be \(x\) and let the correct answer be \(y\). Your answer is accepted if and only if \(\frac{|x - y|}{\max{(1, |y|)}} \le 10^{-6}\).
|
In the first sample, there is only one valid permutation, that is \([2, 1]\). Since \(\log_2(1)=0\), the correct output is \(0\).In the second sample, there are \(2\) valid permutations, that are \([3, 1, 2]\) and \([2, 1, 3]\). Since \(\log_2(2)=1\), the correct output is \(1\).In the third sample, there are \(4\) valid permutations, that are \([1, 5, 4, 3, 2]\), \([2, 5, 4, 3, 1]\), \([3, 5, 4, 2, 1]\), \([4, 5, 3, 2, 1]\). Since \(\log_2(4)=2\), the correct output is \(2\).In the fourth sample, there are \(909\) valid permutations. Notice that \(\log_2(909)=9.828136484194\ldots\)
|
Input: 2 < | Output: 0.0000000000
|
Master
| 1 | 435 | 200 | 373 | 17 |
1,089 |
I
|
1089I
|
I. Interval-Free Permutations
| 2,600 |
combinatorics
|
Consider a permutation \(p_1, p_2, \dots p_n\) of integers from 1 to \(n\). We call a sub-segment \(p_l, p_{l+1}, \dots, p_{r-1}, p_{r}\) of the permutation an interval if it is a reordering of some set of consecutive integers. For example, the permutation \((6,7,1,8,5,3,2,4)\) has the intervals \((6,7)\), \((5,3,2,4)\), \((3,2)\), and others. Each permutation has some trivial intervals β the full permutation itself and every single element. We call a permutation interval-free if it does not have non-trivial intervals. In other words, interval-free permutation does not have intervals of length between 2 and \(n - 1\) inclusive. Your task is to count the number of interval-free permutations of length \(n\) modulo prime number \(p\).
|
In the first line of the input there are two integers \(t\) (\(1 \le t \le 400\)) and \(p\) (\(10^8 \le p \le 10^9\)) β the number of test cases to solve and the prime modulo. In each of the next \(t\) lines there is one integer \(n\) (\(1 \le n \le 400\)) β the length of the permutation.
|
For each of \(t\) test cases print a single integer β the number of interval-free permutations modulo \(p\).
|
For \(n = 1\) the only permutation is interval-free. For \(n = 4\) two interval-free permutations are \((2,4,1,3)\) and \((3,1,4,2)\). For \(n = 5\) β \((2,4,1,5,3)\), \((2,5,3,1,4)\), \((3,1,5,2,4)\), \((3,5,1,4,2)\), \((4,1,3,5,2)\), and \((4,2,5,1,3)\). We will not list all 28146 for \(n = 9\), but for example \((4,7,9,5,1,8,2,6,3)\), \((2,4,6,1,9,7,3,8,5)\), \((3,6,9,4,1,5,8,2,7)\), and \((8,4,9,1,3,6,2,7,5)\) are interval-free.The exact value for \(n = 20\) is 264111424634864638.
|
Input: 4 998244353 1 4 5 9 | Output: 1 2 6 28146
|
Expert
| 1 | 741 | 289 | 108 | 10 |
559 |
B
|
559B
|
B. Equivalent Strings
| 1,700 |
divide and conquer; hashing; sortings; strings
|
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: They are equal. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct: a1 is equivalent to b1, and a2 is equivalent to b2 a1 is equivalent to b2, and a2 is equivalent to b1 As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.Gerald has already completed this home task. Now it's your turn!
|
The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.
|
Print ""YES"" (without the quotes), if these two strings are equivalent, and ""NO"" (without the quotes) otherwise.
|
In the first sample you should split the first string into strings ""aa"" and ""ba"", the second one β into strings ""ab"" and ""aa"". ""aa"" is equivalent to ""aa""; ""ab"" is equivalent to ""ba"" as ""ab"" = ""a"" + ""b"", ""ba"" = ""b"" + ""a"".In the second sample the first string can be splitted into strings ""aa"" and ""bb"", that are equivalent only to themselves. That's why string ""aabb"" is equivalent only to itself and to string ""bbaa"".
|
Input: aabaabaa | Output: YES
|
Medium
| 4 | 620 | 197 | 115 | 5 |
1,777 |
E
|
1777E
|
E. Edge Reverse
| 2,200 |
binary search; dfs and similar; graphs; trees
|
You will be given a weighted directed graph of \(n\) nodes and \(m\) directed edges, where the \(i\)-th edge has a weight of \(w_i\) (\(1 \le i \le m\)).You need to reverse some edges of this graph so that there is at least one node in the graph from which every other node is reachable. The cost of these reversals is equal to the maximum weight of all reversed edges. If no edge reversal is required, assume the cost to be \(0\).It is guaranteed that no self-loop or duplicate edge exists.Find the minimum cost required for completing the task. If there is no solution, print a single integer \(-1\).
|
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.Each test case begins with a line containing two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)) β the number of nodes in the graph and the number of edges in the graph.The next \(m\) lines of each test case contain \(3\) integers each β \(u\), \(v\), \(w\) (\(1 \le u, v \le n\), \(1 \le w \le 10^9\)), indicating an edge from \(u\) to \(v\) with a weight of \(w\). It is guaranteed that no edge connects a vertex to itself, and no pair of edges share the same origin and destination simultaneously.It is guaranteed that the sum of \(n\) and the sum of \(m\) over all test cases do not exceed \(2 \cdot 10^5\).
|
For each test case, output the minimum cost. If there is no solution, print \(-1\).
|
In the first test case, an edge exists from \(1\) to \(2\), so all nodes are reachable (from \(1\)).In the second test case, no nodes are reachable from any node no matter what edges we reverse, so the answer is \(-1\).In the third test case, reversing the \(4\)-th or \(5\)-th edge allows all nodes to be reachable from \(1\). We choose the \(5\)-th edge here because its weight is smaller.
|
Input: 42 11 2 35 41 2 102 3 103 1 104 5 104 51 2 100002 3 200001 3 300004 2 5004 3 204 51 2 100002 3 200001 3 300004 2 54 3 20 | Output: 0 -1 20 5
|
Hard
| 4 | 602 | 810 | 83 | 17 |
580 |
D
|
580D
|
D. Kefa and Dishes
| 1,800 |
bitmasks; dp
|
When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were n dishes. Kefa knows that he needs exactly m dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible. Kefa knows that the i-th dish gives him ai units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself k rules of eating food of the following type β if he eats dish x exactly before dish y (there should be no other dishes between x and y), then his satisfaction level raises by c. Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task!
|
The first line of the input contains three space-separated numbers, n, m and k (1 β€ m β€ n β€ 18, 0 β€ k β€ n * (n - 1)) β the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules.The second line contains n space-separated numbers ai, (0 β€ ai β€ 109) β the satisfaction he gets from the i-th dish.Next k lines contain the rules. The i-th rule is described by the three numbers xi, yi and ci (1 β€ xi, yi β€ n, 0 β€ ci β€ 109). That means that if you eat dish xi right before dish yi, then the Kefa's satisfaction increases by ci. It is guaranteed that there are no such pairs of indexes i and j (1 β€ i < j β€ k), that xi = xj and yi = yj.
|
In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant.
|
In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule.In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5.
|
Input: 2 2 11 12 1 1 | Output: 3
|
Medium
| 2 | 728 | 689 | 111 | 5 |
364 |
E
|
364E
|
E. Empty Rectangles
| 3,000 |
divide and conquer; two pointers
|
You've got an n Γ m table (n rows and m columns), each cell of the table contains a ""0"" or a ""1"".Your task is to calculate the number of rectangles with the sides that are parallel to the sides of the table and go along the cell borders, such that the number one occurs exactly k times in the rectangle.
|
The first line contains three space-separated integers n, m and k (1 β€ n, m β€ 2500, 0 β€ k β€ 6) β the sizes of the table and the required number of numbers one.Next n lines each contains m characters ""0"" or ""1"". The i-th character of the j-th line corresponds to the character that is in the j-th row and the i-th column of the table.
|
Print a single number β the number of rectangles that contain exactly k numbers one.Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Input: 3 3 2101000101 | Output: 8
|
Master
| 2 | 307 | 337 | 233 | 3 |
|
545 |
B
|
545B
|
B. Equidistant String
| 1,100 |
greedy
|
Little Susie loves strings. Today she calculates distances between them. As Susie is a small girl after all, her strings contain only digits zero and one. She uses the definition of Hamming distance:We will define the distance between two strings s and t of the same length consisting of digits zero and one as the number of positions i, such that si isn't equal to ti. As besides everything else Susie loves symmetry, she wants to find for two strings s and t of length n such string p of length n, that the distance from p to s was equal to the distance from p to t.It's time for Susie to go to bed, help her find such string p or state that it is impossible.
|
The first line contains string s of length n. The second line contains string t of length n.The length of string n is within range from 1 to 105. It is guaranteed that both strings contain only digits zero and one.
|
Print a string of length n, consisting of digits zero and one, that meets the problem statement. If no such string exist, print on a single line ""impossible"" (without the quotes).If there are multiple possible answers, print any of them.
|
In the first sample different answers are possible, namely β 0010, 0011, 0110, 0111, 1000, 1001, 1100, 1101.
|
Input: 00011011 | Output: 0011
|
Easy
| 1 | 661 | 214 | 239 | 5 |
1,157 |
G
|
1157G
|
G. Inverse of Rows and Columns
| 2,200 |
brute force; constructive algorithms
|
You are given a binary matrix \(a\) of size \(n \times m\). A binary matrix is a matrix where each element is either \(0\) or \(1\).You may perform some (possibly zero) operations with this matrix. During each operation you can inverse the row of this matrix or a column of this matrix. Formally, inverting a row is changing all values in this row to the opposite (\(0\) to \(1\), \(1\) to \(0\)). Inverting a column is changing all values in this column to the opposite.Your task is to sort the initial matrix by some sequence of such operations. The matrix is considered sorted if the array \([a_{1, 1}, a_{1, 2}, \dots, a_{1, m}, a_{2, 1}, a_{2, 2}, \dots, a_{2, m}, \dots, a_{n, m - 1}, a_{n, m}]\) is sorted in non-descending order.
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 200\)) β the number of rows and the number of columns in the matrix.The next \(n\) lines contain \(m\) integers each. The \(j\)-th element in the \(i\)-th line is \(a_{i, j}\) (\(0 \le a_{i, j} \le 1\)) β the element of \(a\) at position \((i, j)\).
|
If it is impossible to obtain a sorted matrix, print ""NO"" in the first line.Otherwise print ""YES"" in the first line. In the second line print a string \(r\) of length \(n\). The \(i\)-th character \(r_i\) of this string should be '1' if the \(i\)-th row of the matrix is inverted and '0' otherwise. In the third line print a string \(c\) of length \(m\). The \(j\)-th character \(c_j\) of this string should be '1' if the \(j\)-th column of the matrix is inverted and '0' otherwise. If there are multiple answers, you can print any.
|
Input: 2 2 1 1 0 1 | Output: YES 00 10
|
Hard
| 2 | 737 | 333 | 536 | 11 |
|
575 |
D
|
575D
|
D. Tablecity
| 1,700 |
constructive algorithms; implementation
|
There was a big bank robbery in Tablecity. In order to catch the thief, the President called none other than Albert β Tablecityβs Chief of Police. Albert does not know where the thief is located, but he does know how he moves.Tablecity can be represented as 1000 Γ 2 grid, where every cell represents one district. Each district has its own unique name β(X, Y)β, where X and Y are the coordinates of the district in the grid. The thiefβs movement is as Every hour the thief will leave the district (X, Y) he is currently hiding in, and move to one of the districts: (X - 1, Y), (X + 1, Y), (X - 1, Y - 1), (X - 1, Y + 1), (X + 1, Y - 1), (X + 1, Y + 1) as long as it exists in Tablecity. Below is an example of thiefβs possible movements if he is located in district (7,1):Albert has enough people so that every hour he can pick any two districts in Tablecity and fully investigate them, making sure that if the thief is located in one of them, he will get caught. Albert promised the President that the thief will be caught in no more than 2015 hours and needs your help in order to achieve that.
|
There is no input for this problem.
|
The first line of output contains integer N β duration of police search in hours. Each of the following N lines contains exactly 4 integers Xi1, Yi1, Xi2, Yi2 separated by spaces, that represent 2 districts (Xi1, Yi1), (Xi2, Yi2) which got investigated during i-th hour. Output is given in chronological order (i-th line contains districts investigated during i-th hour) and should guarantee that the thief is caught in no more than 2015 hours, regardless of thiefβs initial position and movement. N β€ 2015 1 β€ X β€ 1000 1 β€ Y β€ 2
|
Let's consider the following output:25 1 50 28 1 80 2This output is not guaranteed to catch the thief and is not correct. It is given to you only to show the expected output format. There exists a combination of an initial position and a movement strategy such that the police will not catch the thief.Consider the following initial position and thiefβs movement:In the first hour, the thief is located in district (1,1). Police officers will search districts (5,1) and (50,2) and will not find him.At the start of the second hour, the thief moves to district (2,2). Police officers will search districts (8,1) and (80,2) and will not find him.Since there is no further investigation by the police, the thief escaped!
|
Input: Π ΡΡΠΎΠΉ Π·Π°Π΄Π°ΡΠ΅ Π½Π΅Ρ ΠΏΡΠΈΠΌΠ΅ΡΠΎΠ² Π²Π²ΠΎΠ΄Π°-Π²ΡΠ²ΠΎΠ΄Π°.This problem doesn't have sample input and output. | Output: Π‘ΠΌΠΎΡΡΠΈΡΠ΅ Π·Π°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅ Π½ΠΈΠΆΠ΅.See the note below.
|
Medium
| 2 | 1,097 | 35 | 529 | 5 |
291 |
B
|
291B
|
B. Command Line Arguments
| 1,300 |
*special; implementation; strings
|
The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document.In the Pindows operating system a strings are the lexemes of the command line β the first of them is understood as the name of the program to run and the following lexemes are its arguments. For example, as we execute the command "" run.exe one, two . "", we give four lexemes to the Pindows command line: ""run.exe"", ""one,"", ""two"", ""."". More formally, if we run a command that can be represented as string s (that has no quotes), then the command line lexemes are maximal by inclusion substrings of string s that contain no spaces.To send a string with spaces or an empty string as a command line lexeme, we can use double quotes. The block of characters that should be considered as one lexeme goes inside the quotes. Embedded quotes are prohibited β that is, for each occurrence of character """""" we should be able to say clearly that the quotes are opening or closing. For example, as we run the command """"run.exe o"" """" "" ne, "" two . "" "" "", we give six lexemes to the Pindows command line: ""run.exe o"", """" (an empty string), "" ne, "", ""two"", ""."", "" "" (a single space).It is guaranteed that each lexeme of the command line is either surrounded by spaces on both sides or touches the corresponding command border. One of its consequences is: the opening brackets are either the first character of the string or there is a space to the left of them.You have a string that consists of uppercase and lowercase English letters, digits, characters "".,?!"""" and spaces. It is guaranteed that this string is a correct OS Pindows command line string. Print all lexemes of this command line string. Consider the character """""" to be used only in order to denote a single block of characters into one command line lexeme. In particular, the consequence is that the given string has got an even number of such characters.
|
The single line contains a non-empty string s. String s consists of at most 105 characters. Each character is either an uppercase or a lowercase English letter, or a digit, or one of the "".,?!"""" signs, or a space.It is guaranteed that the given string is some correct command line string of the OS Pindows. It is guaranteed that the given command line string contains at least one lexeme.
|
In the first line print the first lexeme, in the second line print the second one and so on. To make the output clearer, print the ""<"" (less) character to the left of your lexemes and the "">"" (more) character to the right. Print the lexemes in the order in which they occur in the command.Please, follow the given output format strictly. For more clarifications on the output format see the test samples.
|
Input: ""RUn.exe O"" """" "" 2ne, "" two! . "" "" | Output: <RUn.exe O><>< 2ne, ><two!><.>< >
|
Easy
| 3 | 2,122 | 391 | 408 | 2 |
|
1,955 |
E
|
1955E
|
E. Long Inversions
| 1,700 |
brute force; greedy; implementation; sortings
|
A binary string \(s\) of length \(n\) is given. A binary string is a string consisting only of the characters '1' and '0'.You can choose an integer \(k\) (\(1 \le k \le n\)) and then apply the following operation any number of times: choose \(k\) consecutive characters of the string and invert them, i.e., replace all '0' with '1' and vice versa.Using these operations, you need to make all the characters in the string equal to '1'.For example, if \(n=5\), \(s=00100\), you can choose \(k=3\) and proceed as follows: choose the substring from the \(1\)-st to the \(3\)-rd character and obtain \(s=\color{blue}{110}00\); choose the substring from the \(3\)-rd to the \(5\)-th character and obtain \(s=11\color{blue}{111}\); Find the maximum value of \(k\) for which it is possible to make all the characters in the string equal to '1' using the described operations. Note that the number of operations required to achieve this is not important.
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 5000\)) β the length of the string \(s\).The second line of each test case contains a string \(s\) of length \(n\), consisting of the characters '1' and '0'.It is guaranteed that the sum of the values \(n^2\) over all test cases in the test does not exceed \(25 \cdot 10^6\).
|
For each test case, output the maximum integer \(k\) (\(1 \le k \le n\)) for which it is possible to obtain a string \(s\) consisting only of the characters '1' using the described operations.
|
Input: 5500100501000710111013000210 | Output: 3 2 4 3 1
|
Medium
| 4 | 945 | 440 | 192 | 19 |
|
1,550 |
E
|
1550E
|
E. Stringforces
| 2,500 |
binary search; bitmasks; brute force; dp; strings; two pointers
|
You are given a string \(s\) of length \(n\). Each character is either one of the first \(k\) lowercase Latin letters or a question mark.You are asked to replace every question mark with one of the first \(k\) lowercase Latin letters in such a way that the following value is maximized.Let \(f_i\) be the maximum length substring of string \(s\), which consists entirely of the \(i\)-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the \(i\)-th letter doesn't appear in a string, then \(f_i\) is equal to \(0\).The value of a string \(s\) is the minimum value among \(f_i\) for all \(i\) from \(1\) to \(k\).What is the maximum value the string can have?
|
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le k \le 17\)) β the length of the string and the number of first Latin letters used.The second line contains a string \(s\), consisting of \(n\) characters. Each character is either one of the first \(k\) lowercase Latin letters or a question mark.
|
Print a single integer β the maximum value of the string after every question mark is replaced with one of the first \(k\) lowercase Latin letters.
|
In the first example the question marks can be replaced in the following way: ""aaaababbbb"". \(f_1 = 4\), \(f_2 = 4\), thus the answer is \(4\). Replacing it like this is also possible: ""aaaabbbbbb"". That way \(f_1 = 4\), \(f_2 = 6\), however, the minimum of them is still \(4\).In the second example one of the possible strings is ""aabbccdda"".In the third example at least one letter won't appear in the string, thus, the minimum of values \(f_i\) is always \(0\).
|
Input: 10 2 a??ab????b | Output: 4
|
Expert
| 6 | 694 | 338 | 147 | 15 |
703 |
C
|
703C
|
C. Chris and Road
| 2,100 |
geometry; implementation
|
And while Mishka is enjoying her trip...Chris is a little brown bear. No one knows, where and when he met Mishka, but for a long time they are together (excluding her current trip). However, best friends are important too. John is Chris' best friend.Once walking with his friend, John gave Chris the following problem:At the infinite horizontal road of width w, bounded by lines y = 0 and y = w, there is a bus moving, presented as a convex polygon of n vertices. The bus moves continuously with a constant speed of v in a straight Ox line in direction of decreasing x coordinates, thus in time only x coordinates of its points are changing. Formally, after time t each of x coordinates of its points will be decreased by vt.There is a pedestrian in the point (0, 0), who can move only by a vertical pedestrian crossing, presented as a segment connecting points (0, 0) and (0, w) with any speed not exceeding u. Thus the pedestrian can move only in a straight line Oy in any direction with any speed not exceeding u and not leaving the road borders. The pedestrian can instantly change his speed, thus, for example, he can stop instantly.Please look at the sample note picture for better understanding.We consider the pedestrian is hit by the bus, if at any moment the point he is located in lies strictly inside the bus polygon (this means that if the point lies on the polygon vertex or on its edge, the pedestrian is not hit by the bus).You are given the bus position at the moment 0. Please help Chris determine minimum amount of time the pedestrian needs to cross the road and reach the point (0, w) and not to be hit by the bus.
|
The first line of the input contains four integers n, w, v, u (3 β€ n β€ 10 000, 1 β€ w β€ 109, 1 β€ v, u β€ 1000) β the number of the bus polygon vertices, road width, bus speed and pedestrian speed respectively.The next n lines describes polygon vertices in counter-clockwise order. i-th of them contains pair of integers xi and yi ( - 109 β€ xi β€ 109, 0 β€ yi β€ w) β coordinates of i-th polygon point. It is guaranteed that the polygon is non-degenerate.
|
Print the single real t β the time the pedestrian needs to croos the road and not to be hit by the bus. The answer is considered correct if its relative or absolute error doesn't exceed 10 - 6.
|
Following image describes initial position in the first sample case:
|
Input: 5 5 1 21 23 14 33 41 4 | Output: 5.0000000000
|
Hard
| 2 | 1,634 | 449 | 193 | 7 |
767 |
A
|
767A
|
A. Snacktower
| 1,100 |
data structures; implementation
|
According to an old legeng, a long time ago Ankh-Morpork residents did something wrong to miss Fortune, and she cursed them. She said that at some time n snacks of distinct sizes will fall on the city, and the residents should build a Snacktower of them by placing snacks one on another. Of course, big snacks should be at the bottom of the tower, while small snacks should be at the top.Years passed, and once different snacks started to fall onto the city, and the residents began to build the Snacktower. However, they faced some troubles. Each day exactly one snack fell onto the city, but their order was strange. So, at some days the residents weren't able to put the new stack on the top of the Snacktower: they had to wait until all the bigger snacks fell. Of course, in order to not to anger miss Fortune again, the residents placed each snack on the top of the tower immediately as they could do it.Write a program that models the behavior of Ankh-Morpork residents.
|
The first line contains single integer n (1 β€ n β€ 100 000) β the total number of snacks.The second line contains n integers, the i-th of them equals the size of the snack which fell on the i-th day. Sizes are distinct integers from 1 to n.
|
Print n lines. On the i-th of them print the sizes of the snacks which the residents placed on the top of the Snacktower on the i-th day in the order they will do that. If no snack is placed on some day, leave the corresponding line empty.
|
In the example a snack of size 3 fell on the first day, and the residents immediately placed it. On the second day a snack of size 1 fell, and the residents weren't able to place it because they were missing the snack of size 2. On the third day a snack of size 2 fell, and the residents immediately placed it. Right after that they placed the snack of size 1 which had fallen before.
|
Input: 33 1 2 | Output: 3 2 1
|
Easy
| 2 | 976 | 239 | 239 | 7 |
1,045 |
C
|
1045C
|
C. Hyperspace Highways
| 2,300 |
dfs and similar; graphs; trees
|
In an unspecified solar system, there are \(N\) planets. A space government company has recently hired space contractors to build \(M\) bidirectional Hyperspaceβ’ highways, each connecting two different planets. The primary objective, which was to make sure that every planet can be reached from any other planet taking only Hyperspaceβ’ highways, has been completely fulfilled. Unfortunately, lots of space contractors had friends and cousins in the Space Board of Directors of the company, so the company decided to do much more than just connecting all planets. In order to make spending enormous amounts of space money for Hyperspaceβ’ highways look neccessary, they decided to enforce a strict rule on the Hyperspaceβ’ highway network: whenever there is a way to travel through some planets and return to the starting point without travelling through any planet twice, every pair of planets on the itinerary should be directly connected by a Hyperspaceβ’ highway. In other words, the set of planets in every simple cycle induces a complete subgraph.You are designing a Hyperspaceβ’ navigational app, and the key technical problem you are facing is finding the minimal number of Hyperspaceβ’ highways one needs to use to travel from planet \(A\) to planet \(B\). As this problem is too easy for Bubble Cup, here is a harder task: your program needs to do it for \(Q\) pairs of planets.
|
The first line contains three positive integers \(N\) (\(1\leq N\leq 100\,000\)), \(M\) (\(1\leq M\leq 500\,000\)) and \(Q\) (\(1\leq Q\leq 200\,000\)), denoting the number of planets, the number of Hyperspaceβ’ highways, and the number of queries, respectively.Each of the following M lines contains a highway: highway \(i\) is given by two integers \(u_i\) and \(v_i\) (\(1 \leq u_i < v_i \leq N\)), meaning the planets \(u_i\) and \(v_i\) are connected by a Hyperspaceβ’ highway. It is guaranteed that the network of planets and Hyperspaceβ’ highways forms a simple connected graph.Each of the following \(Q\) lines contains a query: query \(j\) is given by two integers \(a_j\) and \(b_j\) \((1 \leq a_j < b_j \leq N )\), meaning we are interested in the minimal number of Hyperspaceβ’ highways one needs to take to travel from planet \(a_j\) to planet \(b_j\).
|
Output \(Q\) lines: the \(j\)-th line of output should contain the minimal number of Hyperspaceβ’ highways one needs to take to travel from planet \(a_j\) to planet \(b_j\).
|
The graph from the second sample:
|
Input: 5 7 21 21 31 42 32 43 41 51 42 5 | Output: 12
|
Expert
| 3 | 1,382 | 861 | 172 | 10 |
1,216 |
C
|
1216C
|
C. White Sheet
| 1,700 |
geometry; math
|
There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume that the bottom left corner of the table has coordinates \((0, 0)\), and coordinate axes are left and bottom sides of the table, then the bottom left corner of the white sheet has coordinates \((x_1, y_1)\), and the top right β \((x_2, y_2)\).After that two black sheets of paper are placed on the table. Sides of both black sheets are also parallel to the sides of the table. Coordinates of the bottom left corner of the first black sheet are \((x_3, y_3)\), and the top right β \((x_4, y_4)\). Coordinates of the bottom left corner of the second black sheet are \((x_5, y_5)\), and the top right β \((x_6, y_6)\). Example of three rectangles. Determine if some part of the white sheet can be seen from the above after the two black sheets are placed. The part of the white sheet can be seen if there is at least one point lying not strictly inside the white sheet and strictly outside of both black sheets.
|
The first line of the input contains four integers \(x_1, y_1, x_2, y_2\) \((0 \le x_1 < x_2 \le 10^{6}, 0 \le y_1 < y_2 \le 10^{6})\) β coordinates of the bottom left and the top right corners of the white sheet.The second line of the input contains four integers \(x_3, y_3, x_4, y_4\) \((0 \le x_3 < x_4 \le 10^{6}, 0 \le y_3 < y_4 \le 10^{6})\) β coordinates of the bottom left and the top right corners of the first black sheet.The third line of the input contains four integers \(x_5, y_5, x_6, y_6\) \((0 \le x_5 < x_6 \le 10^{6}, 0 \le y_5 < y_6 \le 10^{6})\) β coordinates of the bottom left and the top right corners of the second black sheet.The sides of each sheet of paper are parallel (perpendicular) to the coordinate axes.
|
If some part of the white sheet can be seen from the above after the two black sheets are placed, print ""YES"" (without quotes). Otherwise print ""NO"".
|
In the first example the white sheet is fully covered by black sheets.In the second example the part of the white sheet can be seen after two black sheets are placed. For example, the point \((6.5, 4.5)\) lies not strictly inside the white sheet and lies strictly outside of both black sheets.
|
Input: 2 2 4 4 1 1 3 5 3 1 5 5 | Output: NO
|
Medium
| 2 | 1,090 | 738 | 153 | 12 |
1,609 |
G
|
1609G
|
G. A Stroll Around the Matrix
| 3,000 |
data structures; greedy; math
|
William has two arrays of numbers \(a_1, a_2, \dots, a_n\) and \(b_1, b_2, \dots, b_m\). The arrays satisfy the conditions of being convex. Formally an array \(c\) of length \(k\) is considered convex if \(c_i - c_{i - 1} < c_{i + 1} - c_i\) for all \(i\) from \(2\) to \(k - 1\) and \(c_1 < c_2\).Throughout William's life he observed \(q\) changes of two types happening to the arrays: Add the arithmetic progression \(d, d \cdot 2, d \cdot 3, \dots, d \cdot k\) to the suffix of the array \(a\) of length \(k\). The array after the change looks like this: \([a_1, a_2, \dots, a_{n - k}, a_{n - k + 1} + d, a_{n - k + 2} + d \cdot 2, \dots, a_n + d \cdot k]\). The same operation, but for array \(b\). After each change a matrix \(d\) is created from arrays \(a\) and \(b\), of size \(n \times m\), where \(d_{i, j}=a_i + b_j\). William wants to get from cell (\(1, 1\)) to cell (\(n, m\)) of this matrix. From cell (\(x, y\)) he can only move to cells (\(x + 1, y\)) and (\(x, y + 1\)). The length of a path is calculated as the sum of numbers in cells visited by William, including the first and the last cells.After each change William wants you to help find out the minimal length of the path he could take.
|
The first line contains three integers \(n\), \(m\) and \(q\) (\(2 \le n \le 100, 2 \le m \le 10^5\), \(1 \le q \le 10^5\)), the sizes of the arrays and the number of changes.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{12}\)), the contents of array \(a\).The third line contains \(m\) integers \(b_1, b_2, \dots, b_m\) (\(1 \le b_i \le 10^{12}\)), the contents of array \(b\).Each of the next \(q\) lines contains three integers \(type\), \(k\) and \(d\) (\(1 \le type \le 2\), if \(type = 1\), then \(1 \le k \le n\) otherwise \(1 \le k \le m\), \(1 \le d \le 10^3\)).
|
After each change, output one integer, the minimum length of the path in the constructed matrix.
|
Input: 5 3 4 1 2 4 7 11 5 7 10 1 3 2 2 2 5 1 5 4 2 1 7 | Output: 98 128 219 229
|
Master
| 3 | 1,213 | 611 | 96 | 16 |
|
1,030 |
E
|
1030E
|
E. Vasya and Good Sequences
| 2,000 |
bitmasks; dp
|
Vasya has a sequence \(a\) consisting of \(n\) integers \(a_1, a_2, \dots, a_n\). Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number \(6\) \((\dots 00000000110_2)\) into \(3\) \((\dots 00000000011_2)\), \(12\) \((\dots 000000001100_2)\), \(1026\) \((\dots 10000000010_2)\) and many others. Vasya can use this operation any (possibly zero) number of times on any number from the sequence.Vasya names a sequence as good one, if, using operation mentioned above, he can obtain the sequence with bitwise exclusive or of all elements equal to \(0\).For the given sequence \(a_1, a_2, \ldots, a_n\) Vasya'd like to calculate number of integer pairs \((l, r)\) such that \(1 \le l \le r \le n\) and sequence \(a_l, a_{l + 1}, \dots, a_r\) is good.
|
The first line contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β length of the sequence.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{18}\)) β the sequence \(a\).
|
Print one integer β the number of pairs \((l, r)\) such that \(1 \le l \le r \le n\) and the sequence \(a_l, a_{l + 1}, \dots, a_r\) is good.
|
In the first example pairs \((2, 3)\) and \((1, 3)\) are valid. Pair \((2, 3)\) is valid since \(a_2 = 7 \rightarrow 11\), \(a_3 = 14 \rightarrow 11\) and \(11 \oplus 11 = 0\), where \(\oplus\) β bitwise exclusive or. Pair \((1, 3)\) is valid since \(a_1 = 6 \rightarrow 3\), \(a_2 = 7 \rightarrow 13\), \(a_3 = 14 \rightarrow 14\) and \(3 \oplus 13 \oplus 14 = 0\).In the second example pairs \((1, 2)\), \((2, 3)\), \((3, 4)\) and \((1, 4)\) are valid.
|
Input: 36 7 14 | Output: 2
|
Hard
| 2 | 864 | 217 | 141 | 10 |
255 |
C
|
255C
|
C. Almost Arithmetical Progression
| 1,500 |
brute force; dp
|
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: a1 = p, where p is some integer; ai = ai - 1 + ( - 1)i + 1Β·q (i > 1), where q is some integer. Right now Gena has a piece of paper with sequence b, consisting of n integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression.Sequence s1, s2, ..., sk is a subsequence of sequence b1, b2, ..., bn, if there is such increasing sequence of indexes i1, i2, ..., ik (1 β€ i1 < i2 < ... < ik β€ n), that bij = sj. In other words, sequence s can be obtained from b by crossing out some elements.
|
The first line contains integer n (1 β€ n β€ 4000). The next line contains n integers b1, b2, ..., bn (1 β€ bi β€ 106).
|
Print a single integer β the length of the required longest subsequence.
|
In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10, 20, 10.
|
Input: 23 5 | Output: 2
|
Medium
| 2 | 761 | 115 | 72 | 2 |
451 |
E
|
451E
|
E. Devu and Flowers
| 2,300 |
bitmasks; combinatorics; number theory
|
Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contains fi flowers. All flowers in a single box are of the same color (hence they are indistinguishable). Also, no two boxes have flowers of the same color.Now Devu wants to select exactly s flowers from the boxes to decorate his garden. Devu would like to know, in how many different ways can he select the flowers from each box? Since this number may be very large, he asks you to find the number modulo (109 + 7). Devu considers two ways different if there is at least one box from which different number of flowers are selected in these two ways.
|
The first line of input contains two space-separated integers n and s (1 β€ n β€ 20, 0 β€ s β€ 1014).The second line contains n space-separated integers f1, f2, ... fn (0 β€ fi β€ 1012).
|
Output a single integer β the number of ways in which Devu can select the flowers modulo (109 + 7).
|
Sample 1. There are two ways of selecting 3 flowers: {1, 2} and {0, 3}.Sample 2. There is only one way of selecting 4 flowers: {2, 2}.Sample 3. There are three ways of selecting 5 flowers: {1, 2, 2}, {0, 3, 2}, and {1, 3, 1}.
|
Input: 2 31 3 | Output: 2
|
Expert
| 3 | 644 | 180 | 99 | 4 |
1,073 |
A
|
1073A
|
A. Diverse Substring
| 1,000 |
implementation; strings
|
You are given a string \(s\), consisting of \(n\) lowercase Latin letters.A substring of string \(s\) is a continuous segment of letters from \(s\). For example, ""defor"" is a substring of ""codeforces"" and ""fors"" is not. The length of the substring is the number of letters in it.Let's call some string of length \(n\) diverse if and only if there is no letter to appear strictly more than \(\frac n 2\) times. For example, strings ""abc"" and ""iltlml"" are diverse and strings ""aab"" and ""zz"" are not.Your task is to find any diverse substring of string \(s\) or report that there is none. Note that it is not required to maximize or minimize the length of the resulting substring.
|
The first line contains a single integer \(n\) (\(1 \le n \le 1000\)) β the length of string \(s\).The second line is the string \(s\), consisting of exactly \(n\) lowercase Latin letters.
|
Print ""NO"" if there is no diverse substring in the string \(s\).Otherwise the first line should contain ""YES"". The second line should contain any diverse substring of string \(s\).
|
The first example has lots of correct answers. Please, restrain yourself from asking if some specific answer is correct for some specific test or not, these questions always lead to ""No comments"" answer.
|
Input: 10codeforces | Output: YEScode
|
Beginner
| 2 | 691 | 188 | 184 | 10 |
553 |
E
|
553E
|
E. Kyoya and Train
| 3,200 |
dp; fft; graphs; math; probabilities
|
Kyoya Ootori wants to take the train to get to school. There are n train stations and m one-way train lines going between various stations. Kyoya is currently at train station 1, and the school is at station n. To take a train, he must pay for a ticket, and the train also takes a certain amount of time. However, the trains are not perfect and take random amounts of time to arrive at their destination. If Kyoya arrives at school strictly after t time units, he will have to pay a fine of x.Each train line is described by a ticket price, and a probability distribution on the time the train takes. More formally, train line i has ticket cost ci, and a probability distribution pi, k which denotes the probability that this train will take k time units for all 1 β€ k β€ t. Amounts of time that each of the trains used by Kyouya takes are mutually independent random values (moreover, if Kyoya travels along the same train more than once, it is possible for the train to take different amounts of time and those amounts are also independent one from another). Kyoya wants to get to school by spending the least amount of money in expectation (for the ticket price plus possible fine for being late). Of course, Kyoya has an optimal plan for how to get to school, and every time he arrives at a train station, he may recalculate his plan based on how much time he has remaining. What is the expected cost that Kyoya will pay to get to school if he moves optimally?
|
The first line of input contains four integers n, m, t, x (2 β€ n β€ 50, 1 β€ m β€ 100, 1 β€ t β€ 20 000, 0 β€ x β€ 106).The next 2m lines contain the description of the trains. The 2i-th line will have 3 integers ai, bi, ci, representing a one way train from station ai to bi with ticket cost ci (1 β€ ai, bi β€ n, ai β bi, 0 β€ ci β€ 106). There will always be at least one path from any station to the school. The (2i + 1)-th line will contain t integers, pi, 1, pi, 2, ..., pi, t where pi, k / 100000 is the probability that this train will take k units of time to traverse (0 β€ pi, k β€ 100 000 for 1 β€ k β€ t, ). It is guaranteed that there is no more than one train between each pair of platforms in each of the directions.
|
Print a single real number that is equal to an optimal expected cost of getting to school. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
|
The optimal strategy in the first case is as follows:First, travel along first train line. With probability 1 / 2 Kyoya will take 1 time unit. Otherwise, Kyoya will take 3 time units.If the train takes 1 time unit, travel along the 4th train line. Kyoya will make it to school in time with probability 1 / 2. Otherwise, if the train takes 3 time units, travel along the 2nd train line. Kyoya will make it to school in time with probability 1 / 10.Since the cost of all train lines are zero, we can just look at the probability that Kyoya will incur the penalty. The probability that Kyoya will have to pay the penalty is 1 / 2 Γ 1 / 2 + 1 / 2 Γ 9 / 10 = 7 / 10. We can show that no other strategy is strictly better.The optimal strategy in the second case is to travel along 1 β 2 β 4 no matter what. Kyoya will incur the penalty with probability 3 / 4, and the cost of the trains is 200, thus the expected cost is 200.75.
|
Input: 4 4 5 11 2 050000 0 50000 0 02 3 010000 0 0 0 900003 4 0100000 0 0 0 02 4 00 0 0 50000 50000 | Output: 0.7000000000
|
Master
| 5 | 1,463 | 716 | 185 | 5 |
1,082 |
G
|
1082G
|
G. Petya and Graph
| 2,400 |
flows; graphs
|
Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of \(n\) vertices and \(m\) edges.The weight of the \(i\)-th vertex is \(a_i\).The weight of the \(i\)-th edge is \(w_i\).A subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. The weight of a subgraph is the sum of the weights of its edges, minus the sum of the weights of its vertices. You need to find the maximum weight of subgraph of given graph. The given graph does not contain loops and multiple edges.
|
The first line contains two numbers \(n\) and \(m\) (\(1 \le n \le 10^3, 0 \le m \le 10^3\)) - the number of vertices and edges in the graph, respectively.The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) - the weights of the vertices of the graph.The following \(m\) lines contain edges: the \(i\)-e edge is defined by a triple of integers \(v_i, u_i, w_i\) (\(1 \le v_i, u_i \le n, 1 \le w_i \le 10^9, v_i \neq u_i\)). This triple means that between the vertices \(v_i\) and \(u_i\) there is an edge of weight \(w_i\). It is guaranteed that the graph does not contain loops and multiple edges.
|
Print one integer β the maximum weight of the subgraph of the given graph.
|
In the first test example, the optimal subgraph consists of the vertices \({1, 3, 4}\) and has weight \(4 + 4 + 5 - (1 + 2 + 2) = 8\). In the second test case, the optimal subgraph is empty.
|
Input: 4 5 1 5 2 2 1 3 4 1 4 4 3 4 5 3 2 2 4 2 2 | Output: 8
|
Expert
| 2 | 652 | 633 | 74 | 10 |
1,742 |
C
|
1742C
|
C. Stripes
| 900 |
implementation
|
On an \(8 \times 8\) grid, some horizontal rows have been painted red, and some vertical columns have been painted blue, in some order. The stripes are drawn sequentially, one after the other. When the stripe is drawn, it repaints all the cells through which it passes.Determine which color was used last. The red stripe was painted after the blue one, so the answer is R.
|
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 4000\)) β the number of test cases. The description of test cases follows. There is an empty line before each test case.Each test case consists of \(8\) lines, each containing \(8\) characters. Each of these characters is either 'R', 'B', or '.', denoting a red square, a blue square, and an unpainted square, respectively.It is guaranteed that the given field is obtained from a colorless one by drawing horizontal red rows and vertical blue columns.At least one stripe is painted.
|
For each test case, output 'R' if a red stripe was painted last, and 'B' if a blue stripe was painted last (without quotes).
|
The first test case is pictured in the statement.In the second test case, the first blue column is painted first, then the first and last red rows, and finally the last blue column. Since a blue stripe is painted last, the answer is B.
|
Input: 4....B.......B.......B...RRRRRRRR....B.......B.......B.......B...RRRRRRRBB......BB......BB......BB......BB......BB......BRRRRRRRBRRRRRRBB.B.B..BBRRRRRRBB.B.B..BB.B.B..BBRRRRRRBB.B.B..BB.B.B..BB........................RRRRRRRR................................ | Output: R B B R
|
Beginner
| 1 | 372 | 559 | 124 | 17 |
1,299 |
D
|
1299D
|
D. Around the World
| 3,000 |
bitmasks; combinatorics; dfs and similar; dp; graphs; graphs; math; trees
|
Guy-Manuel and Thomas are planning \(144\) trips around the world.You are given a simple weighted undirected connected graph with \(n\) vertexes and \(m\) edges with the following restriction: there isn't any simple cycle (i. e. a cycle which doesn't pass through any vertex more than once) of length greater than \(3\) which passes through the vertex \(1\). The cost of a path (not necessarily simple) in this graph is defined as the XOR of weights of all edges in that path with each edge being counted as many times as the path passes through it.But the trips with cost \(0\) aren't exciting. You may choose any subset of edges incident to the vertex \(1\) and remove them. How many are there such subsets, that, when removed, there is not any nontrivial cycle with the cost equal to \(0\) which passes through the vertex \(1\) in the resulting graph? A cycle is called nontrivial if it passes through some edge odd number of times. As the answer can be very big, output it modulo \(10^9+7\).
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n,m \le 10^5\)) β the number of vertexes and edges in the graph. The \(i\)-th of the next \(m\) lines contains three integers \(a_i\), \(b_i\) and \(w_i\) (\(1 \le a_i, b_i \le n, a_i \neq b_i, 0 \le w_i < 32\)) β the endpoints of the \(i\)-th edge and its weight. It's guaranteed there aren't any multiple edges, the graph is connected and there isn't any simple cycle of length greater than \(3\) which passes through the vertex \(1\).
|
Output the answer modulo \(10^9+7\).
|
The pictures below represent the graphs from examples. In the first example, there aren't any nontrivial cycles with cost \(0\), so we can either remove or keep the only edge incident to the vertex \(1\). In the second example, if we don't remove the edge \(1-2\), then there is a cycle \(1-2-4-5-2-1\) with cost \(0\); also if we don't remove the edge \(1-3\), then there is a cycle \(1-3-2-4-5-2-3-1\) of cost \(0\). The only valid subset consists of both edges. In the third example, all subsets are valid except for those two in which both edges \(1-3\) and \(1-4\) are kept.
|
Input: 6 8 1 2 0 2 3 1 2 4 3 2 6 2 3 4 8 3 5 4 5 4 5 5 6 6 | Output: 2
|
Master
| 8 | 995 | 499 | 36 | 12 |
1,914 |
B
|
1914B
|
B. Preparing for the Contest
| 800 |
constructive algorithms; math
|
Monocarp is practicing for a big contest. He plans to solve \(n\) problems to make sure he's prepared. Each of these problems has a difficulty level: the first problem has a difficulty level of \(1\), the second problem has a difficulty level of \(2\), and so on, until the last (\(n\)-th) problem, which has a difficulty level of \(n\).Monocarp will choose some order in which he is going to solve all \(n\) problems. Whenever he solves a problem which is more difficult than the last problem he solved, he gets excited because he feels like he's progressing. He doesn't get excited when he solves the first problem in his chosen order.For example, if Monocarp solves the problems in the order \([3, \underline{5}, 4, 1, \underline{6}, 2]\), he gets excited twice (the corresponding problems are underlined).Monocarp wants to get excited exactly \(k\) times during his practicing session. Help him to choose the order in which he has to solve the problems!
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case consists of one line containing two integers \(n\) and \(k\) (\(2 \le n \le 50\); \(0 \le k \le n - 1\)).
|
For each test case, print \(n\) distinct integers from \(1\) to \(n\), denoting the order in which Monocarp should solve the problems. If there are multiple answers, print any of them.It can be shown that under the constraints of the problem, the answer always exists.
|
Input: 36 25 45 0 | Output: 3 5 4 1 6 2 1 2 3 4 5 5 4 3 2 1
|
Beginner
| 2 | 957 | 212 | 268 | 19 |
|
1,891 |
F
|
1891F
|
F. A Growing Tree
| 2,000 |
data structures; dfs and similar; trees
|
You are given a rooted tree with the root at vertex \(1\), initially consisting of a single vertex. Each vertex has a numerical value, initially set to \(0\). There are also \(q\) queries of two types: The first type: add a child vertex with the number \(sz + 1\) to vertex \(v\), where \(sz\) is the current size of the tree. The numerical value of the new vertex will be \(0\). The second type: add \(x\) to the numerical values of all vertices in the subtree of vertex \(v\). After all queries, output the numerical value of all of the vertices in the final tree.
|
The first line contains a single integer \(T\) (\(1 \leq T \leq 10^4\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer \(q\) (\(1 \leq q \leq 5 \cdot 10^5\)) β the number of queries.The following \(q\) lines can fall into two cases: The first type of query: The \(i\)-th line contains two integers \(t_i\) (\(t_i = 1\)), \(v_i\). You need to add a child with the number \(sz + 1\) to vertex \(v_i\), where \(sz\) is the current size of the tree. It is guaranteed that \(1 \leq v_i \leq sz\). The second type of query: The \(i\)-th line contains three integers \(t_i\) (\(t_i = 2\)), \(v_i\), \(x_i\) (\(-10^9 \leq x_i \leq 10^9\)). You need to add \(x_i\) to all numerical values of vertices in the subtree of \(v_i\). It is guaranteed that \(1 \leq v_i \leq sz\), where \(sz\) is the current size of the tree. It is guaranteed that the sum of \(q\) across all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, output the numerical value of each vertex of the final tree after all queries have been performed.
|
In the first case, the final tree with the assigned numerical values will look like this: The final tree with the assigned numerical values
|
Input: 392 1 31 12 2 11 12 3 21 32 1 41 32 3 252 1 11 12 1 -11 12 1 151 11 12 1 12 1 32 2 10 | Output: 7 5 8 6 2 1 0 1 4 14 4
|
Hard
| 3 | 566 | 987 | 118 | 18 |
1,468 |
G
|
1468G
|
G. Hobbits
| 2,500 |
binary search; geometry
|
The hobbits Frodo and Sam are carrying the One Ring to Mordor. In order not to be spotted by orcs, they decided to go through the mountains.The mountain relief can be represented as a polyline with \(n\) points \((x_i, y_i)\), numbered from \(1\) to \(n\) (\(x_i < x_{i + 1}\) for \(1 \le i \le n - 1\)). Hobbits start their journey at the point \((x_1, y_1)\) and should reach the point \((x_n, y_n)\) to complete their mission.The problem is that there is a tower with the Eye of Sauron, which watches them. The tower is located at the point \((x_n, y_n)\) and has the height \(H\), so the Eye is located at the point \((x_n, y_n + H)\). In order to complete the mission successfully, the hobbits have to wear cloaks all the time when the Sauron Eye can see them, i. e. when there is a direct line from the Eye to the hobbits which is not intersected by the relief.The hobbits are low, so their height can be considered negligibly small, but still positive, so when a direct line from the Sauron Eye to the hobbits only touches the relief, the Eye can see them. The Sauron Eye can't see hobbits when they are in the left position, but can see them when they are in the right position. The hobbits do not like to wear cloaks, so they wear them only when they can be spotted by the Eye. Your task is to calculate the total distance the hobbits have to walk while wearing cloaks.
|
The first line of the input contains two integers \(n\) and \(H\) (\(2 \le n \le 2 \cdot 10^5\); \(1 \le H \le 10^4\)) β the number of vertices in polyline and the tower height.The next \(n\) lines contain two integers \(x_i, y_i\) each (\(0 \le x_i \le 4 \cdot 10^5\); \(0 \le y_i \le 10^4\)) β the coordinates of the polyline vertices. It is guaranteed that \(x_i < x_{i + 1}\) for \(1 \le i \le n - 1\).
|
Print one real number β the total distance the hobbits have to walk while wearing cloaks. Your answer will be considered correct if its absolute or relative error does not exceed \(10^{-6}\) β formally, if your answer is \(a\), and the jury's answer is \(b\), your answer will be accepted if \(\dfrac{|a - b|}{\max(1, b)} \le 10^{-6}\).
|
Input: 6 10 10 40 20 10 25 30 30 15 50 15 65 30 | Output: 70.4034587602
|
Expert
| 2 | 1,378 | 406 | 336 | 14 |
|
976 |
E
|
976E
|
E. Well played!
| 2,100 |
greedy; sortings
|
Recently Max has got himself into popular CCG ""BrainStone"". As ""BrainStone"" is a pretty intellectual game, Max has to solve numerous hard problems during the gameplay. Here is one of them:Max owns n creatures, i-th of them can be described with two numbers β its health hpi and its damage dmgi. Max also has two types of spells in stock: Doubles health of the creature (hpi := hpiΒ·2); Assigns value of health of the creature to its damage (dmgi := hpi). Spell of first type can be used no more than a times in total, of the second type β no more than b times in total. Spell can be used on a certain creature multiple times. Spells can be used in arbitrary order. It isn't necessary to use all the spells.Max is really busy preparing for his final exams, so he asks you to determine what is the maximal total damage of all creatures he can achieve if he uses spells in most optimal way.
|
The first line contains three integers n, a, b (1 β€ n β€ 2Β·105, 0 β€ a β€ 20, 0 β€ b β€ 2Β·105) β the number of creatures, spells of the first type and spells of the second type, respectively.The i-th of the next n lines contain two number hpi and dmgi (1 β€ hpi, dmgi β€ 109) β description of the i-th creature.
|
Print single integer β maximum total damage creatures can deal.
|
In the first example Max should use the spell of the first type on the second creature, then the spell of the second type on the same creature. Then total damage will be equal to 15 + 6Β·2 = 27.In the second example Max should use the spell of the second type on the first creature, then the spell of the second type on the third creature. Total damage will be equal to 10 + 11 + 5 = 26.
|
Input: 2 1 110 156 1 | Output: 27
|
Hard
| 2 | 890 | 304 | 63 | 9 |
2,001 |
E2
|
2001E2
|
E2. Deterministic Heap (Hard Version)
| 2,900 |
combinatorics; dp; trees
|
This is the hard version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on \(n\) and \(t\). You can make hacks only if both versions of the problem are solved.Consider a perfect binary tree with size \(2^n - 1\), with nodes numbered from \(1\) to \(2^n-1\) and rooted at \(1\). For each vertex \(v\) (\(1 \le v \le 2^{n - 1} - 1\)), vertex \(2v\) is its left child and vertex \(2v + 1\) is its right child. Each node \(v\) also has a value \(a_v\) assigned to it.Define the operation \(\mathrm{pop}\) as follows: initialize variable \(v\) as \(1\); repeat the following process until vertex \(v\) is a leaf (i.e. until \(2^{n - 1} \le v \le 2^n - 1\)); among the children of \(v\), choose the one with the larger value on it and denote such vertex as \(x\); if the values on them are equal (i.e. \(a_{2v} = a_{2v + 1}\)), you can choose any of them; assign \(a_x\) to \(a_v\) (i.e. \(a_v := a_x\)); assign \(x\) to \(v\) (i.e. \(v := x\)); assign \(-1\) to \(a_v\) (i.e. \(a_v := -1\)). Then we say the \(\mathrm{pop}\) operation is deterministic if there is a unique way to do such operation. In other words, \(a_{2v} \neq a_{2v + 1}\) would hold whenever choosing between them.A binary tree is called a max-heap if for every vertex \(v\) (\(1 \le v \le 2^{n - 1} - 1\)), both \(a_v \ge a_{2v}\) and \(a_v \ge a_{2v + 1}\) hold.A max-heap is deterministic if the \(\mathrm{pop}\) operation is deterministic to the heap when we do it for the first and the second time.Initially, \(a_v := 0\) for every vertex \(v\) (\(1 \le v \le 2^n - 1\)), and your goal is to count the number of different deterministic max-heaps produced by applying the following operation \(\mathrm{add}\) exactly \(k\) times: Choose an integer \(v\) (\(1 \le v \le 2^n - 1\)) and, for every vertex \(x\) on the path between \(1\) and \(v\), add \(1\) to \(a_x\). Two heaps are considered different if there is a node which has different values in the heaps. Since the answer might be large, print it modulo \(p\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 50\)). The description of the test cases follows.The first line of each test case contains three integers \(n, k, p\) (\(2 \le n \le 100\), \(1 \le k \le 500\), \(10^8 \le p \le 10^9\), \(p\) is a prime).It is guaranteed that the sum of \(n\) does not exceed \(100\) and the sum of \(k\) over all test cases does not exceed \(500\).
|
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation \(\mathrm{add}\) exactly \(k\) times, modulo \(p\).
|
For the first testcase, if we choose \(v = 1\) and do the operation, we would have \(a = [1, 0, 0]\), and since \(a_2 = a_3\), we can choose either of them when doing the first \(\mathrm{pop}\) operation, so such heap is not a deterministic max-heap. And if we choose \(v = 2\), we would have \(a = [1, 1, 0]\), during the first \(\mathrm{pop}\), the following would happen: initialize \(v\) as \(1\) since \(a_{2v} > a_{2v + 1}\), choose \(2v\) as \(x\), then \(x = 2\) assign \(a_x\) to \(a_v\), then \(a = [1, 1, 0]\) assign \(x\) to \(v\), then \(v = 2\) since \(v\) is a leaf, assign \(-1\) to \(a_v\), then \(a = [1, -1, 0]\) And during the second \(\mathrm{pop}\), the following would happen: initialize \(v\) as \(1\) since \(a_{2v} < a_{2v + 1}\), choose \(2v + 1\) as \(x\), then \(x = 3\) assign \(a_x\) to \(a_v\), then \(a = [0, -1, 0]\) assign \(x\) to \(v\), then \(v = 3\) since \(v\) is a leaf, assign \(-1\) to \(a_v\), then \(a = [0, -1, -1]\) Since both the first and the second \(\mathrm{pop}\) operation are deterministic, this is a deterministic max-heap. Also, if we choose \(v = 3\), \(a\) would be a deterministic max-heap, so the answer is \(2\).
|
Input: 62 1 9982443533 2 9982448533 3 9982443533 4 1000000374 2 1000000394 3 100000037 | Output: 2 12 40 100 32 224
|
Master
| 3 | 2,068 | 442 | 213 | 20 |
946 |
B
|
946B
|
B. Weird Subtraction Process
| 1,100 |
math; number theory
|
You have two variables a and b. Consider the following sequence of actions performed with these variables: If a = 0 or b = 0, end the process. Otherwise, go to step 2; If a β₯ 2Β·b, then set the value of a to a - 2Β·b, and repeat step 1. Otherwise, go to step 3; If b β₯ 2Β·a, then set the value of b to b - 2Β·a, and repeat step 1. Otherwise, end the process.Initially the values of a and b are positive integers, and so the process will be finite.You have to determine the values of a and b after the process ends.
|
The only line of the input contains two integers n and m (1 β€ n, m β€ 1018). n is the initial value of variable a, and m is the initial value of variable b.
|
Print two integers β the values of a and b after the end of the process.
|
Explanations to the samples: a = 12, b = 5 a = 2, b = 5 a = 2, b = 1 a = 0, b = 1; a = 31, b = 12 a = 7, b = 12.
|
Input: 12 5 | Output: 0 1
|
Easy
| 2 | 510 | 155 | 72 | 9 |
98 |
E
|
98E
|
E. Help Shrek and Donkey
| 2,700 |
dp; games; math; probabilities
|
Shrek and the Donkey (as you can guess, they also live in the far away kingdom) decided to play a card game called YAGame. The rules are very simple: initially Shrek holds m cards and the Donkey holds n cards (the players do not see each other's cards), and one more card lies on the table face down so that both players cannot see it as well. Thus, at the beginning of the game there are overall m + n + 1 cards. Besides, the players know which cards the pack of cards consists of and their own cards (but they do not know which card lies on the table and which ones the other player has). The players move in turn and Shrek starts. During a move a player can: Try to guess which card is lying on the table. If he guesses correctly, the game ends and he wins. If his guess is wrong, the game also ends but this time the other player wins. Name any card from the pack. If the other player has such card, he must show it and put it aside (so that this card is no longer used in the game). If the other player doesn't have such card, he says about that. Recently Donkey started taking some yellow pills and winning over Shrek. Now Shrek wants to evaluate his chances to win if he too starts taking the pills.Help Shrek assuming the pills are good in quality and that both players using them start playing in the optimal manner.
|
The first line contains space-separated integers m and n (0 β€ m, n β€ 1000).
|
Print space-separated probabilities that Shrek wins and Donkey wins correspondingly; the absolute error should not exceed 10 - 9.
|
Input: 0 3 | Output: 0.25 0.75
|
Master
| 4 | 1,325 | 75 | 129 | 0 |
|
201 |
B
|
201B
|
B. Guess That Car!
| 1,800 |
math; ternary search
|
A widely known among some people Belarusian sport programmer Yura possesses lots of information about cars. That is why he has been invited to participate in a game show called ""Guess That Car!"".The game show takes place on a giant parking lot, which is 4n meters long from north to south and 4m meters wide from west to east. The lot has n + 1 dividing lines drawn from west to east and m + 1 dividing lines drawn from north to south, which divide the parking lot into nΒ·m 4 by 4 meter squares. There is a car parked strictly inside each square. The dividing lines are numbered from 0 to n from north to south and from 0 to m from west to east. Each square has coordinates (i, j) so that the square in the north-west corner has coordinates (1, 1) and the square in the south-east corner has coordinates (n, m). See the picture in the notes for clarifications.Before the game show the organizers offer Yura to occupy any of the (n + 1)Β·(m + 1) intersection points of the dividing lines. After that he can start guessing the cars. After Yura chooses a point, he will be prohibited to move along the parking lot before the end of the game show. As Yura is a car expert, he will always guess all cars he is offered, it's just a matter of time. Yura knows that to guess each car he needs to spend time equal to the square of the euclidean distance between his point and the center of the square with this car, multiplied by some coefficient characterizing the machine's ""rarity"" (the rarer the car is, the harder it is to guess it). More formally, guessing a car with ""rarity"" c placed in a square whose center is at distance d from Yura takes cΒ·d2 seconds. The time Yura spends on turning his head can be neglected.It just so happened that Yura knows the ""rarity"" of each car on the parking lot in advance. Help him choose his point so that the total time of guessing all cars is the smallest possible.
|
The first line contains two integers n and m (1 β€ n, m β€ 1000) β the sizes of the parking lot. Each of the next n lines contains m integers: the j-th number in the i-th line describes the ""rarity"" cij (0 β€ cij β€ 100000) of the car that is located in the square with coordinates (i, j).
|
In the first line print the minimum total time Yura needs to guess all offered cars. In the second line print two numbers li and lj (0 β€ li β€ n, 0 β€ lj β€ m) β the numbers of dividing lines that form a junction that Yura should choose to stand on at the beginning of the game show. If there are multiple optimal starting points, print the point with smaller li. If there are still multiple such points, print the point with smaller lj.Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In the first test case the total time of guessing all cars is equal to 3Β·8 + 3Β·8 + 4Β·8 + 9Β·8 + 5Β·40 + 1Β·40 = 392.The coordinate system of the field:
|
Input: 2 33 4 53 9 1 | Output: 3921 1
|
Medium
| 2 | 1,907 | 287 | 580 | 2 |
846 |
B
|
846B
|
B. Math Show
| 1,800 |
brute force; greedy
|
Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him tj minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.By solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all k of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is k + 1.Polycarp has M minutes of time. What is the maximum number of points he can earn?
|
The first line contains three integer numbers n, k and M (1 β€ n β€ 45, 1 β€ k β€ 45, 0 β€ M β€ 2Β·109).The second line contains k integer numbers, values tj (1 β€ tj β€ 1000000), where tj is the time in minutes required to solve j-th subtask of any task.
|
Print the maximum amount of points Polycarp can earn in M minutes.
|
In the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute.In the second example Polycarp can solve the first subtask of all five tasks and spend 5Β·1 = 5 minutes. Also he can solve the second subtasks of two tasks and spend 2Β·2 = 4 minutes. Thus, he earns 5 + 2 = 7 points in total.
|
Input: 3 4 111 2 3 4 | Output: 6
|
Medium
| 2 | 730 | 246 | 66 | 8 |
45 |
C
|
45C
|
C. Dancing Lessons
| 1,900 |
data structures
|
There are n people taking dancing lessons. Every person is characterized by his/her dancing skill ai. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of ai variable. Your task is to find out what pairs and in what order will start dancing.
|
The first line contains an integer n (1 β€ n β€ 2Β·105) β the number of people. The next line contains n symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains n space-separated integers ai (1 β€ ai β€ 107) β the dancing skill. People are specified from left to right in the order in which they lined up.
|
Print the resulting number of couples k. Then print k lines containing two numerals each β the numbers of people forming the couple. The people are numbered with integers from 1 to n from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
|
Input: 4BGBG4 2 4 3 | Output: 23 41 2
|
Hard
| 1 | 735 | 340 | 396 | 0 |
|
721 |
A
|
721A
|
A. One-dimensional Japanese Crossword
| 800 |
implementation
|
Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a Γ b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents how many groups of black squares there are in corresponding row or column, and the integers themselves represents the number of consecutive black squares in corresponding group (you can find more detailed explanation in Wikipedia https://en.wikipedia.org/wiki/Japanese_crossword).Adaltik decided that the general case of japanese crossword is too complicated and drew a row consisting of n squares (e.g. japanese crossword sized 1 Γ n), which he wants to encrypt in the same way as in japanese crossword. The example of encrypting of a single row of japanese crossword. Help Adaltik find the numbers encrypting the row he drew.
|
The first line of the input contains a single integer n (1 β€ n β€ 100) β the length of the row. The second line of the input contains a single string consisting of n characters 'B' or 'W', ('B' corresponds to black square, 'W' β to white square in the row that Adaltik drew).
|
The first line should contain a single integer k β the number of integers encrypting the row, e.g. the number of groups of black squares in the row.The second line should contain k integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right.
|
The last sample case correspond to the picture in the statement.
|
Input: 3BBW | Output: 12
|
Beginner
| 1 | 948 | 274 | 310 | 7 |
1,054 |
D
|
1054D
|
D. Changing Array
| 1,900 |
greedy; implementation
|
At a break Vanya came to the class and saw an array of \(n\) \(k\)-bit integers \(a_1, a_2, \ldots, a_n\) on the board. An integer \(x\) is called a \(k\)-bit integer if \(0 \leq x \leq 2^k - 1\). Of course, Vanya was not able to resist and started changing the numbers written on the board. To ensure that no one will note anything, Vanya allowed himself to make only one type of changes: choose an index of the array \(i\) (\(1 \leq i \leq n\)) and replace the number \(a_i\) with the number \(\overline{a_i}\). We define \(\overline{x}\) for a \(k\)-bit integer \(x\) as the \(k\)-bit integer such that all its \(k\) bits differ from the corresponding bits of \(x\). Vanya does not like the number \(0\). Therefore, he likes such segments \([l, r]\) (\(1 \leq l \leq r \leq n\)) such that \(a_l \oplus a_{l+1} \oplus \ldots \oplus a_r \neq 0\), where \(\oplus\) denotes the bitwise XOR operation. Determine the maximum number of segments he likes Vanya can get applying zero or more operations described above.
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \leq n \leq 200\,000\), \(1 \leq k \leq 30\)).The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 2^k - 1\)), separated by spaces β the array of \(k\)-bit integers.
|
Print one integer β the maximum possible number of segments with XOR not equal to \(0\) that can be obtained by making several (possibly \(0\)) operations described in the statement.
|
In the first example if Vasya does not perform any operations, he gets an array that has \(5\) segments that Vanya likes. If he performs the operation with \(i = 2\), he gets an array \([1, 0, 0]\), because \(\overline{3} = 0\) when \(k = 2\). This array has \(3\) segments that Vanya likes. Also, to get an array with \(5\) segments that Vanya likes, he can perform two operations with \(i = 3\) and with \(i = 2\). He then gets an array \([1, 0, 3]\). It can be proven that he can't obtain \(6\) or more segments that he likes.In the second example, to get \(19\) segments that Vanya likes, he can perform \(4\) operations with \(i = 3\), \(i = 4\), \(i = 5\), \(i = 6\) and get an array \([1, 4, 3, 0, 4, 3]\).
|
Input: 3 21 3 0 | Output: 5
|
Hard
| 2 | 1,013 | 266 | 182 | 10 |
42 |
B
|
42B
|
B. Game of chess unfinished
| 1,700 |
implementation
|
Once Volodya was at the museum and saw a regular chessboard as a museum piece. And there were only four chess pieces on it: two white rooks, a white king and a black king. ""Aha, blacks certainly didn't win!"", β Volodya said and was right for sure. And your task is to say whether whites had won or not.Pieces on the chessboard are guaranteed to represent a correct position (every piece occupies one cell, no two pieces occupy the same cell and kings cannot take each other). Thus, your task is only to decide whether whites mate blacks. We would remind you that it means that the black king can be taken by one of the opponent's pieces at the moment and also it cannot move to an unbeaten position. A rook moves vertically or horizontally by any number of free cells (assuming there are no other pieces on its path), a king β to the adjacent cells (either by corner or by side). Certainly, pieces cannot leave the board. The black king might be able to take opponent's rooks at his turn (see sample 3).
|
The input contains 4 space-separated piece positions: positions of the two rooks, the white king and the black king. Each position on 8 Γ 8 chessboard is denoted by two symbols β ('a' - 'h') and ('1' - '8') β which stand for horizontal and vertical coordinates of the cell occupied by the piece. It is guaranteed, that no two pieces occupy the same cell, and kings cannot take each other.
|
Output should contain one word: ""CHECKMATE"" if whites mate blacks, and ""OTHER"" otherwise.
|
Input: a6 b4 c8 a8 | Output: CHECKMATE
|
Medium
| 1 | 1,005 | 388 | 93 | 0 |
|
127 |
A
|
127A
|
A. Wasted Time
| 900 |
geometry
|
Mr. Scrooge, a very busy man, decided to count the time he wastes on all sorts of useless stuff to evaluate the lost profit. He has already counted the time he wastes sleeping and eating. And now Mr. Scrooge wants to count the time he has wasted signing papers.Mr. Scrooge's signature can be represented as a polyline A1A2... An. Scrooge signs like that: first it places a pen at the point A1, then draws a segment from point A1 to point A2, then he draws a segment from point A2 to point A3 and so on to point An, where he stops signing and takes the pen off the paper. At that the resulting line can intersect with itself and partially repeat itself but Scrooge pays no attention to it and never changes his signing style. As Scrooge makes the signature, he never takes the pen off the paper and his writing speed is constant β 50 millimeters per second.Scrooge signed exactly k papers throughout his life and all those signatures look the same.Find the total time Scrooge wasted signing the papers.
|
The first line contains two integers n and k (2 β€ n β€ 100, 1 β€ k β€ 1000). Each of the following n lines contains the coordinates of the polyline's endpoints. The i-th one contains coordinates of the point Ai β integers xi and yi, separated by a space.All points Ai are different. The absolute value of all coordinates does not exceed 20. The coordinates are measured in millimeters.
|
Print one real number β the total time Scrooges wastes on signing the papers in seconds. The absolute or relative error should not exceed 10 - 6.
|
Input: 2 10 010 0 | Output: 0.200000000
|
Beginner
| 1 | 1,001 | 382 | 145 | 1 |
|
1,578 |
C
|
1578C
|
C. Cactus Lady and her Cing
| 3,500 |
Cactus lady loves her cactuses very much. Especially she likes a small cactus named Cing. Cing can be seen as a connected undirected graph in which every vertex lies on at most one simple cycle. Intuitively, a cactus is a generalization of a tree where some cycles are allowed. Multiedges (multiple edges between a pair of vertices) and loops (edges that connect a vertex to itself) are not allowed.She bought a special grid for her special little cactus Cing. This grid can be represented as a graph consisting of two paths of length \(400\,000\), \(u_{(0, -200\,000)} - u_{(0, -199\,999)} - \ldots - u_{(0, 200\,000)}\) and \(u_{(1, -200\,000)} - u_{(1, -199\,999)} - \ldots - u_{(1, 200\,000)}\), connected together by \(400\,001\) edges \((u_{(0, i)}, u_{(1, i)})\) for each \(i\). In other words, a grid can be seen as a ladder. Cactus lady wants to know whether she can embed Cing into this grid, i.e., map each vertex of the cactus onto a separate vertex of the grid while each edge of the cactus will be mapped onto some edge of the grid.
|
The first line contains an integer \(t\) β the number of test cases.Each test case begins with a line containing two integers \(n\) and \(m\) β the number of vertices and the number of edges in a given cactus, respectively (\(1 \le n \le 200\,000\); \(0 \le m \le 250\,000\)).Each of the following \(m\) lines contains two integers \(v\) and \(u\), describing the edges of the cactus (\(1 \le v, u \le n, u \ne v\)).The total sum of all \(n\) in the input doesn't exceed \(200\,000\).
|
Print an answer for each test case in the same order the cases appear in the input.For each test case print ""No"" in the first line, if no layout exists.Otherwise print ""Yes"" in the first line, and the following \(n\) lines describing the layout. The \(i\)-th of these \(n\) lines should contain two integers \(x_i\) and \(y_i\), the location of the \(i\)-th vertex (\(0 \le x_i \le 1\); \(-200\,000 \le y_i \le 200\,000\)).
|
Empty lines between test cases are for clarity. In real test cases there are no empty lines.In these notes, we consider the embeddings for tests 2 and 4.We start with the embedding for test 2. Here goes the embedding for test 4.
|
Input: 5 4 3 1 2 2 3 3 4 8 7 1 2 3 2 2 4 4 5 4 6 6 7 6 8 5 4 1 2 1 3 1 4 1 5 8 9 1 2 2 3 3 4 1 4 4 5 5 6 6 7 7 8 5 8 10 10 1 2 2 3 3 4 4 5 5 6 6 1 3 7 4 8 1 9 6 10 | Output: Yes 0 0 0 1 1 1 1 2 Yes 0 3 1 3 1 4 1 2 0 2 1 1 0 1 1 0 No Yes 0 0 1 0 1 1 0 1 0 2 0 3 1 3 1 2 Yes 1 1 1 2 1 3 0 3 0 2 0 1 1 4 0 4 1 0 0 0
|
Master
| 0 | 1,046 | 484 | 427 | 15 |
|
1,695 |
E
|
1695E
|
E. Ambiguous Dominoes
| 2,700 |
constructive algorithms; dfs and similar; graphs
|
Polycarp and Monocarp are both solving the same puzzle with dominoes. They are given the same set of \(n\) dominoes, the \(i\)-th of which contains two numbers \(x_i\) and \(y_i\). They are also both given the same \(m\) by \(k\) grid of values \(a_{ij}\) such that \(m\cdot k = 2n\).The puzzle asks them to place the \(n\) dominoes on the grid in such a way that none of them overlap, and the values on each domino match the \(a_{ij}\) values that domino covers. Dominoes can be rotated arbitrarily before being placed on the grid, so the domino \((x_i, y_i)\) is equivalent to the domino \((y_i, x_i)\).They have both solved the puzzle, and compared their answers, but noticed that not only did their solutions not match, but none of the \(n\) dominoes were in the same location in both solutions! Formally, if two squares were covered by the same domino in Polycarp's solution, they were covered by different dominoes in Monocarp's solution. The diagram below shows one potential \(a\) grid, along with the two players' solutions. Polycarp and Monocarp remember the set of dominoes they started with, but they have lost the grid \(a\). Help them reconstruct one possible grid \(a\), along with both of their solutions, or determine that no such grid exists.
|
The first line contains a single integer \(n\) (\(1 \le n \le 3\cdot 10^5\)).The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le 2n\)).
|
If there is no solution, print a single integer \(-1\).Otherwise, print \(m\) and \(k\), the height and width of the puzzle grid, on the first line of output. These should satisfy \(m\cdot k = 2n\).The \(i\)-th of the next \(m\) lines should contain \(k\) integers, the \(j\)-th of which is \(a_{ij}\).The next \(m\) lines describe Polycarp's solution. Print \(m\) lines of \(k\) characters each. For each square, if it is covered by the upper half of a domino in Polycarp's solution, it should contain a ""U"". Similarly, if it is covered by the bottom, left, or right half of a domino, it should contain ""D"", ""L"", or ""R"", respectively.The next \(m\) lines should describe Monocarp's solution, in the same format as Polycarp's solution.If there are multiple answers, print any.
|
Extra blank lines are added to the output for clarity, but are not required.The third sample case corresponds to the image from the statement.
|
Input: 1 1 2 | Output: -1
|
Master
| 3 | 1,260 | 184 | 784 | 16 |
1,141 |
E
|
1141E
|
E. Superhero Battle
| 1,700 |
math
|
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly \(n\) minutes. After a round ends, the next round starts immediately. This is repeated over and over again.Each round has the same scenario. It is described by a sequence of \(n\) numbers: \(d_1, d_2, \dots, d_n\) (\(-10^6 \le d_i \le 10^6\)). The \(i\)-th element means that monster's hp (hit points) changes by the value \(d_i\) during the \(i\)-th minute of each round. Formally, if before the \(i\)-th minute of a round the monster's hp is \(h\), then after the \(i\)-th minute it changes to \(h := h + d_i\).The monster's initial hp is \(H\). It means that before the battle the monster has \(H\) hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to \(0\). Print -1 if the battle continues infinitely.
|
The first line contains two integers \(H\) and \(n\) (\(1 \le H \le 10^{12}\), \(1 \le n \le 2\cdot10^5\)). The second line contains the sequence of integers \(d_1, d_2, \dots, d_n\) (\(-10^6 \le d_i \le 10^6\)), where \(d_i\) is the value to change monster's hp in the \(i\)-th minute of a round.
|
Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer \(k\) such that \(k\) is the first minute after which the monster is dead.
|
Input: 1000 6 -100 -200 -300 125 77 -4 | Output: 9
|
Medium
| 1 | 864 | 297 | 198 | 11 |
|
1,301 |
C
|
1301C
|
C. Ayoub's function
| 1,700 |
binary search; combinatorics; greedy; math; strings
|
Ayoub thinks that he is a very smart person, so he created a function \(f(s)\), where \(s\) is a binary string (a string which contains only symbols ""0"" and ""1""). The function \(f(s)\) is equal to the number of substrings in the string \(s\) that contains at least one symbol, that is equal to ""1"".More formally, \(f(s)\) is equal to the number of pairs of integers \((l, r)\), such that \(1 \leq l \leq r \leq |s|\) (where \(|s|\) is equal to the length of string \(s\)), such that at least one of the symbols \(s_l, s_{l+1}, \ldots, s_r\) is equal to ""1"". For example, if \(s = \)""01010"" then \(f(s) = 12\), because there are \(12\) such pairs \((l, r)\): \((1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)\).Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers \(n\) and \(m\) and asked him this problem. For all binary strings \(s\) of length \(n\) which contains exactly \(m\) symbols equal to ""1"", find the maximum value of \(f(s)\).Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of the test cases follows.The only line for each test case contains two integers \(n\), \(m\) (\(1 \leq n \leq 10^{9}\), \(0 \leq m \leq n\)) β the length of the string and the number of symbols equal to ""1"" in it.
|
For every test case print one integer number β the maximum value of \(f(s)\) over all strings \(s\) of length \(n\), which has exactly \(m\) symbols, equal to ""1"".
|
In the first test case, there exists only \(3\) strings of length \(3\), which has exactly \(1\) symbol, equal to ""1"". These strings are: \(s_1 = \)""100"", \(s_2 = \)""010"", \(s_3 = \)""001"". The values of \(f\) for them are: \(f(s_1) = 3, f(s_2) = 4, f(s_3) = 3\), so the maximum value is \(4\) and the answer is \(4\).In the second test case, the string \(s\) with the maximum value is ""101"".In the third test case, the string \(s\) with the maximum value is ""111"".In the fourth test case, the only string \(s\) of length \(4\), which has exactly \(0\) symbols, equal to ""1"" is ""0000"" and the value of \(f\) for that string is \(0\), so the answer is \(0\).In the fifth test case, the string \(s\) with the maximum value is ""01010"" and it is described as an example in the problem statement.
|
Input: 5 3 1 3 2 3 3 4 0 5 2 | Output: 4 5 6 0 12
|
Medium
| 5 | 1,100 | 375 | 165 | 13 |
1,405 |
A
|
1405A
|
A. Permutation Forgery
| 800 |
constructive algorithms
|
A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).Let \(p\) be any permutation of length \(n\). We define the fingerprint \(F(p)\) of \(p\) as the sorted array of sums of adjacent elements in \(p\). More formally,$$$\(F(p)=\mathrm{sort}([p_1+p_2,p_2+p_3,\ldots,p_{n-1}+p_n]).\)\(For example, if \)n=4\( and \)p=[1,4,2,3],\( then the fingerprint is given by \)F(p)=\mathrm{sort}([1+4,4+2,2+3])=\mathrm{sort}([5,6,5])=[5,5,6]\(.You are given a permutation \)p\( of length \)n\(. Your task is to find a different permutation \)p'\( with the same fingerprint. Two permutations \)p\( and \)p'\( are considered different if there is some index \)i\( such that \)p_i \ne p'_i$$$.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 668\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2\le n\le 100\)) β the length of the permutation.The second line of each test case contains \(n\) integers \(p_1,\ldots,p_n\) (\(1\le p_i\le n\)). It is guaranteed that \(p\) is a permutation.
|
For each test case, output \(n\) integers \(p'_1,\ldots, p'_n\) β a permutation such that \(p'\ne p\) and \(F(p')=F(p)\).We can prove that for every permutation satisfying the input constraints, a solution exists.If there are multiple solutions, you may output any.
|
In the first test case, \(F(p)=\mathrm{sort}([1+2])=[3]\).And \(F(p')=\mathrm{sort}([2+1])=[3]\).In the second test case, \(F(p)=\mathrm{sort}([2+1,1+6,6+5,5+4,4+3])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]\).And \(F(p')=\mathrm{sort}([1+2,2+5,5+6,6+3,3+4])=\mathrm{sort}([3,7,11,9,7])=[3,7,7,9,11]\).In the third test case, \(F(p)=\mathrm{sort}([2+4,4+3,3+1,1+5])=\mathrm{sort}([6,7,4,6])=[4,6,6,7]\).And \(F(p')=\mathrm{sort}([3+1,1+5,5+2,2+4])=\mathrm{sort}([4,6,7,6])=[4,6,6,7]\).
|
Input: 3 2 1 2 6 2 1 6 5 4 3 5 2 4 3 1 5 | Output: 2 1 1 2 5 6 3 4 3 1 5 2 4
|
Beginner
| 1 | 945 | 416 | 265 | 14 |
906 |
D
|
906D
|
D. Power Tower
| 2,700 |
chinese remainder theorem; math; number theory
|
Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from k - 1 rocks, possesses power p and we want to add the rock charged with power wk then value of power of a new tower will be {wk}p. Rocks are added from the last to the first. That is for sequence w1, ..., wm value of power will beAfter tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo m. Priests have n rocks numbered from 1 to n. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered l, l + 1, ..., r.
|
First line of input contains two integers n (1 β€ n β€ 105) and m (1 β€ m β€ 109).Second line of input contains n integers wk (1 β€ wk β€ 109) which is the power of rocks that priests have.Third line of input contains single integer q (1 β€ q β€ 105) which is amount of queries from priests to you.kth of next q lines contains two integers lk and rk (1 β€ lk β€ rk β€ n).
|
Output q integers. k-th of them must be the amount of cumulative power the tower will have if is built from rocks lk, lk + 1, ..., rk.
|
327 = 7625597484987
|
Input: 6 10000000001 2 2 3 3 381 11 62 22 32 44 44 54 6 | Output: 1124256327597484987
|
Master
| 3 | 908 | 360 | 134 | 9 |
1,681 |
F
|
1681F
|
F. Unique Occurrences
| 2,300 |
data structures; dfs and similar; divide and conquer; dp; dsu; trees
|
You are given a tree, consisting of \(n\) vertices. Each edge has an integer value written on it.Let \(f(v, u)\) be the number of values that appear exactly once on the edges of a simple path between vertices \(v\) and \(u\).Calculate the sum of \(f(v, u)\) over all pairs of vertices \(v\) and \(u\) such that \(1 \le v < u \le n\).
|
The first line contains a single integer \(n\) (\(2 \le n \le 5 \cdot 10^5\)) β the number of vertices in the tree.Each of the next \(n-1\) lines contains three integers \(v, u\) and \(x\) (\(1 \le v, u, x \le n\)) β the description of an edge: the vertices it connects and the value written on it.The given edges form a tree.
|
Print a single integer β the sum of \(f(v, u)\) over all pairs of vertices \(v\) and \(u\) such that \(v < u\).
|
Input: 3 1 2 1 1 3 2 | Output: 4
|
Expert
| 6 | 333 | 326 | 111 | 16 |
|
295 |
E
|
295E
|
E. Yaroslav and Points
| 2,500 |
data structures
|
Yaroslav has n points that lie on the Ox axis. The coordinate of the first point is x1, the coordinate of the second point is x2, ..., the coordinate of the n-th point is β xn. Now Yaroslav wants to execute m queries, each of them is of one of the two following types: Move the pj-th point from position xpj to position xpj + dj. At that, it is guaranteed that after executing such query all coordinates of the points will be distinct. Count the sum of distances between all pairs of points that lie on the segment [lj, rj] (lj β€ rj). In other words, you should count the sum of: . Help Yaroslav.
|
The first line contains integer n β the number of points (1 β€ n β€ 105). The second line contains distinct integers x1, x2, ..., xn β the coordinates of points (|xi| β€ 109).The third line contains integer m β the number of queries (1 β€ m β€ 105). The next m lines contain the queries. The j-th line first contains integer tj (1 β€ tj β€ 2) β the query type. If tj = 1, then it is followed by two integers pj and dj (1 β€ pj β€ n, |dj| β€ 1000). If tj = 2, then it is followed by two integers lj and rj ( - 109 β€ lj β€ rj β€ 109).It is guaranteed that at any moment all the points have distinct coordinates.
|
For each type 2 query print the answer on a single line. Print the answers in the order, in which the queries follow in the input.Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
|
Input: 836 50 28 -75 40 -60 -95 -48202 -61 291 5 -531 1 4291 5 1302 -101 -712 -69 531 1 4041 5 5182 -101 532 50 8721 1 -2072 -99 -401 7 -3891 6 -1711 2 4641 7 -7071 1 -7301 1 5602 635 6441 7 -677 | Output: 17620406104616381560
|
Expert
| 1 | 596 | 597 | 277 | 2 |
|
1,540 |
E
|
1540E
|
E. Tasty Dishes
| 3,500 |
math; matrices
|
Note that the memory limit is unusual.There are \(n\) chefs numbered \(1, 2, \ldots, n\) that must prepare dishes for a king. Chef \(i\) has skill \(i\) and initially has a dish of tastiness \(a_i\) where \(|a_i| \leq i\). Each chef has a list of other chefs that he is allowed to copy from. To stop chefs from learning bad habits, the king makes sure that chef \(i\) can only copy from chefs of larger skill.There are a sequence of days that pass during which the chefs can work on their dish. During each day, there are two stages during which a chef can change the tastiness of their dish. At the beginning of each day, each chef can choose to work (or not work) on their own dish, thereby multiplying the tastiness of their dish of their skill (\(a_i := i \cdot a_i\)) (or doing nothing). After all chefs (who wanted) worked on their own dishes, each start observing the other chefs. In particular, for each chef \(j\) on chef \(i\)'s list, chef \(i\) can choose to copy (or not copy) \(j\)'s dish, thereby adding the tastiness of the \(j\)'s dish to \(i\)'s dish (\(a_i := a_i + a_j\)) (or doing nothing). It can be assumed that all copying occurs simultaneously. Namely, if chef \(i\) chooses to copy from chef \(j\) he will copy the tastiness of chef \(j\)'s dish at the end of stage \(1\).All chefs work to maximize the tastiness of their own dish in order to please the king.Finally, you are given \(q\) queries. Each query is one of two types. \(1\) \(k\) \(l\) \(r\) β find the sum of tastiness \(a_l, a_{l+1}, \ldots, a_{r}\) after the \(k\)-th day. Because this value can be large, find it modulo \(10^9 + 7\). \(2\) \(i\) \(x\) β the king adds \(x\) tastiness to the \(i\)-th chef's dish before the \(1\)-st day begins (\(a_i := a_i + x\)). Note that, because the king wants to see tastier dishes, he only adds positive tastiness (\(x > 0\)). Note that queries of type \(1\) are independent of each all other queries. Specifically, each query of type \(1\) is a scenario and does not change the initial tastiness \(a_i\) of any dish for future queries. Note that queries of type \(2\) are cumulative and only change the initial tastiness \(a_i\) of a dish. See notes for an example of queries.
|
The first line contains a single integer \(n\) (\(1 \le n \le 300\)) β the number of chefs.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-i \le a_i \le i\)).The next \(n\) lines each begin with a integer \(c_i\) (\(0 \le c_i < n\)), denoting the number of chefs the \(i\)-th chef can copy from. This number is followed by \(c_i\) distinct integers \(d\) (\(i < d \le n\)), signifying that chef \(i\) is allowed to copy from chef \(d\) during stage \(2\) of each day.The next line contains a single integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β the number of queries.Each of the next \(q\) lines contains a query of one of two types: \(1\) \(k\) \(l\) \(r\) (\(1 \le l \le r \le n\); \(1 \le k \le 1000\)); \(2\) \(i\) \(x\) (\(1 \le i \le n\); \(1 \le x \le 1000\)). It is guaranteed that there is at least one query of the first type.
|
For each query of the first type, print a single integer β the answer to the query.
|
Below is the set of chefs that each chef is allowed to copy from: \(1\): \(\{2, 3, 4, 5\}\) \(2\): \(\{3\}\) \(3\): \(\{4\}\) \(4\): \(\{5\}\) \(5\): \(\emptyset\) (no other chefs)Following is a description of the sample.For the first query of type \(1\), the initial tastiness values are \([1, 0, -2, -2, 4]\).The final result of the first day is shown below: \([1, 0, -2, -2, 20]\) (chef \(5\) works on his dish). \([21, 0, -2, 18, 20]\) (chef \(1\) and chef \(4\) copy from chef \(5\)). So, the answer for the \(1\)-st query is \(21 + 0 - 2 + 18 + 20 = 57\).For the \(5\)-th query (\(3\)-rd of type \(1\)). The initial tastiness values are now \([1, 0, 0, 1, 4]\).Day 1 \([1, 0, 0, 4, 20]\) (chefs \(4\) and \(5\) work on their dishes). \([25,0, 4, 24, 20]\) (chef \(1\) copies from chefs \(4\) and \(5\), chef \(3\) copies from chef \(4\), chef \(4\) copies from chef \(5\)). Day 2 \([25, 0, 12, 96, 100]\) (all chefs but chef \(2\) work on their dish). \([233, 12, 108, 196, 100]\) (chef \(1\) copies from chefs \(3\), \(4\) and \(5\), chef \(2\) from \(3\), chef \(3\) from \(4\), chef \(4\) from chef \(5\)).So, the answer for the \(5\)-th query is \(12+108+196=316\). It can be shown that, in each step we described, all chefs moved optimally.
|
Input: 5 1 0 -2 -2 4 4 2 3 4 5 1 3 1 4 1 5 0 7 1 1 1 5 2 4 3 1 1 1 5 2 3 2 1 2 2 4 2 5 1 1 981 4 5 | Output: 57 71 316 278497818
|
Master
| 2 | 2,207 | 859 | 83 | 15 |
1,436 |
A
|
1436A
|
A. Reorder
| 800 |
math
|
For a given array \(a\) consisting of \(n\) integers and a given integer \(m\) find if it is possible to reorder elements of the array \(a\) in such a way that \(\sum_{i=1}^{n}{\sum_{j=i}^{n}{\frac{a_j}{j}}}\) equals \(m\)? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, \(\frac{5}{2}=2.5\).
|
The first line contains a single integer \(t\) β the number of test cases (\(1 \le t \le 100\)). The test cases follow, each in two lines.The first line of a test case contains two integers \(n\) and \(m\) (\(1 \le n \le 100\), \(0 \le m \le 10^6\)). The second line contains integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^6\)) β the elements of the array.
|
For each test case print ""YES"", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and ""NO"" otherwise.
|
In the first test case one of the reorders could be \([1, 2, 5]\). The sum is equal to \((\frac{1}{1} + \frac{2}{2} + \frac{5}{3}) + (\frac{2}{2} + \frac{5}{3}) + (\frac{5}{3}) = 8\). The brackets denote the inner sum \(\sum_{j=i}^{n}{\frac{a_j}{j}}\), while the summation of brackets corresponds to the sum over \(i\).
|
Input: 2 3 8 2 5 1 4 4 0 1 2 3 | Output: YES NO
|
Beginner
| 1 | 377 | 364 | 170 | 14 |
1,886 |
B
|
1886B
|
B. Fear of the Dark
| 1,200 |
binary search; geometry; math
|
Monocarp tries to get home from work. He is currently at the point \(O = (0, 0)\) of a two-dimensional plane; his house is at the point \(P = (P_x, P_y)\).Unfortunately, it is late in the evening, so it is very dark. Monocarp is afraid of the darkness. He would like to go home along a path illuminated by something.Thankfully, there are two lanterns, located in the points \(A = (A_x, A_y)\) and \(B = (B_x, B_y)\). You can choose any non-negative number \(w\) and set the power of both lanterns to \(w\). If a lantern's power is set to \(w\), it illuminates a circle of radius \(w\) centered at the lantern location (including the borders of the circle).You have to choose the minimum non-negative value \(w\) for the power of the lanterns in such a way that there is a path from the point \(O\) to the point \(P\) which is completely illuminated. You may assume that the lanterns don't interfere with Monocarp's movement. The picture for the first two test cases
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of three lines: the first line contains two integers \(P_x\) and \(P_y\) (\(-10^3 \le P_x, P_y \le 10^3\)) β the location of Monocarp's house; the second line contains two integers \(A_x\) and \(A_y\) (\(-10^3 \le A_x, A_y \le 10^3\)) β the location of the first lantern; the third line contains two integers \(B_x\) and \(B_y\) (\(-10^3 \le B_x, B_y \le 10^3\)) β the location of the second lantern. Additional constraint on the input: in each test case, the points \(O\), \(P\), \(A\) and \(B\) are different from each other.
|
For each test case, print the answer on a separate line β one real number equal to the minimum value of \(w\) such that there is a completely illuminated path from the point \(O\) to the point \(P\).Your answer will be considered correct if its absolute or relative error does not exceed \(10^{-6}\) β formally, if your answer is \(a\), and the jury's answer is \(b\), your answer will be accepted if \(\dfrac{|a - b|}{\max(1, b)} \le 10^{-6}\).
|
Input: 23 31 0-1 63 3-1 -14 3 | Output: 3.6055512755 3.2015621187
|
Easy
| 3 | 965 | 656 | 445 | 18 |
|
372 |
C
|
372C
|
C. Watching Fireworks is Fun
| 2,100 |
data structures; dp; math
|
A festival will be held in a town's main street. There are n sections in the main street. The sections are numbered 1 through n from left to right. The distance between each adjacent sections is 1.In the festival m fireworks will be launched. The i-th (1 β€ i β€ m) launching is on time ti at section ai. If you are at section x (1 β€ x β€ n) at the time of i-th launching, you'll gain happiness value bi - |ai - x| (note that the happiness value might be a negative value).You can move up to d length units in a unit time interval, but it's prohibited to go out of the main street. Also you can be in an arbitrary section at initial time moment (time equals to 1), and want to maximize the sum of happiness that can be gained from watching fireworks. Find the maximum total happiness.Note that two or more fireworks can be launched at the same time.
|
The first line contains three integers n, m, d (1 β€ n β€ 150000; 1 β€ m β€ 300; 1 β€ d β€ n).Each of the next m lines contains integers ai, bi, ti (1 β€ ai β€ n; 1 β€ bi β€ 109; 1 β€ ti β€ 109). The i-th line contains description of the i-th launching.It is guaranteed that the condition ti β€ ti + 1 (1 β€ i < m) will be satisfied.
|
Print a single integer β the maximum sum of happiness that you can gain from watching all the fireworks.Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
Input: 50 3 149 1 126 1 46 1 10 | Output: -31
|
Hard
| 3 | 846 | 319 | 253 | 3 |
|
301 |
A
|
301A
|
A. Yaroslav and Sequence
| 1,800 |
constructive algorithms
|
Yaroslav has an array, consisting of (2Β·n - 1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them by -1.Yaroslav is now wondering: what maximum sum of array elements can be obtained if it is allowed to perform any number of described operations?Help Yaroslav.
|
The first line contains an integer n (2 β€ n β€ 100). The second line contains (2Β·n - 1) integers β the array elements. The array elements do not exceed 1000 in their absolute value.
|
In a single line print the answer to the problem β the maximum sum that Yaroslav can get.
|
In the first sample you do not need to change anything. The sum of elements equals 150.In the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100.
|
Input: 250 50 50 | Output: 150
|
Medium
| 1 | 410 | 180 | 89 | 3 |
883 |
K
|
883K
|
K. Road Widening
| 1,800 |
constructive algorithms; greedy; implementation
|
Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy!The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street.The street is split into n equal length parts from left to right, the i-th part is characterized by two integers: width of road si and width of lawn gi. For each of n parts the Mayor should decide the size of lawn to demolish. For the i-th part he can reduce lawn width by integer xi (0 β€ xi β€ gi). After it new road width of the i-th part will be equal to s'i = si + xi and new lawn width will be equal to g'i = gi - xi.On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each i (1 β€ i < n) the inequation |s'i + 1 - s'i| β€ 1 should hold. Initially this condition might not be true.You need to find the the total width of lawns the Mayor will destroy according to his plan.
|
The first line contains integer n (1 β€ n β€ 2Β·105) β number of parts of the street.Each of the following n lines contains two integers si, gi (1 β€ si β€ 106, 0 β€ gi β€ 106) β current width of road and width of the lawn on the i-th part of the street.
|
In the first line print the total width of lawns which will be removed.In the second line print n integers s'1, s'2, ..., s'n (si β€ s'i β€ si + gi) β new widths of the road starting from the first part and to the last.If there is no solution, print the only integer -1 in the first line.
|
Input: 34 54 54 10 | Output: 169 9 10
|
Medium
| 3 | 1,290 | 247 | 286 | 8 |
|
1,860 |
A
|
1860A
|
A. Not a Substring
| 900 |
constructive algorithms; strings
|
A bracket sequence is a string consisting of characters '(' and/or ')'. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example: bracket sequences ""()()"" and ""(())"" are regular (they can be transformed into ""(1)+(1)"" and ""((1+1)+1)"", respectively); bracket sequences "")("", ""("" and "")"" are not regular. You are given a bracket sequence \(s\); let's define its length as \(n\). Your task is to find a regular bracket sequence \(t\) of length \(2n\) such that \(s\) does not occur in \(t\) as a contiguous substring, or report that there is no such sequence.
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The only line of each test case contains a string \(s\) (\(2 \le |s| \le 50\)), consisting of characters ""("" and/or "")"".
|
For each test case, print the answer to it. If there is no required regular bracket sequence, print NO in a separate line. Otherwise, print YES in the first line, and the required regular bracket sequence \(t\) itself in the second line. If there are multiple answers β you may print any of them.
|
Input: 4)((()()))() | Output: YES (()) YES ()()() NO YES ()(()())
|
Beginner
| 2 | 720 | 221 | 296 | 18 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.