A. Two Subsequences: Codeforces Round #751 (Div. 2)
A. Two Subsequences: Codeforces Round #751 (Div. 2)
A. Two Subsequences: Codeforces Round #751 (Div. 2)
2)
2 1 1 4 3 1 2
possible string.
After the
1 -st step:
2 3 3 1 1 3 2
Given string s , print any valid a and b.
After the
2 -nd step:
2 3 3 2 2 3 2
Reminder:
After the
3 -rd step:
4 3 3 4 4 3 4
A string a (b) is a subsequence of a string s
if a (b) can be obtained from
s by deletion of
several (possibly, zero) elements. For example, "dores", ... ...
"cf", and "for" are subsequences of
"codeforces",
while "decor"
and
"fork" are not. In the initial array, we had two 2 -s, three 1 -s, only
one 4 and only one 3 ,
so after the first step, each
element became equal to the number of its
A string x is lexicographically smaller than
a string y if and only if one of
occurrences in the
initial array: all twos changed to 2 , all ones changed to
the following holds:
3 , four changed to 1 and three changed to 1 .
aaaa
i
values of the
initial array.
output
It is guaranteed that the sum of n over all test cases
doesn't exceed 2000
c f
a aaa
and the sum of q over all test
cases doesn't exceed 100 000.
b therightboiler
Output
In the first test case, there are only two choices: either a = f and b = c For each test case, print q answers. The i -th of them
should be the value
or a = c and b = f. And a = c is lexicographically smaller
than a = f. of ax after the ki -th step
of transformation. It can be shown that the
i
B. Divine Array
2 seconds, 256 megabytes
input The first line of each test case contains one integer n
(
1 ≤ n ≤ 200 000 ) — the length of array a .
2
7
The second line of each test case contains n integers
a1 , a2 , … , an (
2 1 1 4 3 1 2
) — array
a itself.
30
0 ≤ ai < 2
4
3 0
2 2
200 000.
6 1
2
Output
1 1
For each test case, print all values k , such that it's
possible to make all
2
elements of a equal to 0 in a
finite number of elimination operations with
1 0
2 1000000000
the given parameter k .
2
input
3
5
3
4
1
4 4 4 4
2
4
13 7 25 19
3 5 3 1 7 1
1. k1 = 0 (initial array): a3 = 1; 1
1 2
Initial
array: 1 1 1
1 2 3 4 5
After the
1 -st step: 2 2
, ,
{1} {2} {3} {4} ,
. Since & of one element is equal to the
element
It can be seen that: itself, then for each operation x = ai , so ai − x = ai − ai = 0 .
If k = 2 , we can make two elimination operations with,
for example,
1. k1 = 0 (initial array): a1 = 1 ; sets of indices {1, 3} and {2, 4} :
x = a1 & a3 = a2 & a4
=
2. k 2 = 1000000000 a2 = 2 : ; 4 & 4 = 4 . For both operations x = 4 , so after the first operation
9 8 7 1 5 4 3 2 0 0
ri = {
1, if x i = 1 and yi = 1 In the first example, Gorf is on the bottom of the well and jump
to the
0, if x i = 0 or yi = 0 height 1 meter below ground level. After that he slip
down by meter and
stays on height 2 meters below ground
level. Now, from here, he can
reach ground level in one jump.
D. Frog Traveler
In the second example, Gorf can jump to one meter below ground
level,
2 seconds, 512 megabytes
but will slip down back to the bottom of the well. That's
why he can't reach
Frog Gorf is traveling through Swamp kingdom. Unfortunately,
after a poor ground level.
jump, he fell into a well of n meters depth.
Now Gorf is on the bottom of In the third example, Gorf can reach ground level only from the
height 5
the well and has a long way up. meters below the ground level. And Gorf can reach
this height using a
The surface of the well's walls vary in quality: somewhere they
are series of jumps 10 ⇒ 9 ⇢ 9 ⇒ 4 ⇢ 5 where
⇒ is the jump and ⇢
slippery, but somewhere have convenient ledges. In other
words, if Gorf is is
slipping during breaks.
on x meters below ground level, then in
one jump he can go up on any
integer distance from 0 to
ax meters inclusive. (Note that Gorf can't jump E. Optimal Insertion
down, only up).
3 seconds, 512 megabytes
Unfortunately, Gorf has to take a break after each jump
(including jump on
0 meters). And after jumping up to
position x meters below ground level, You are given two arrays of integers a1 , a2 , … , an and b1 , b2 , … , bm .
he'll slip exactly
bx meters down while resting.
You need to insert all elements of b into a in an arbitrary way. As a result
Calculate the minimum number of jumps Gorf needs to reach ground you will get an array c1 , c2 , … , cn+m of size n + m .
level.
Note that you are not allowed to change the order of elements in a, while
Input you can insert elements of b at arbitrary positions. They can be inserted at
The first line contains a single integer n (1 ≤ n ≤ 300 000 ) — the depth the beginning, between any elements of a, or at the end. Moreover,
of the well. elements of b can appear in the resulting array in any order.
The second line contains n integers a1 , a2 , … , an (0 ≤ ai ≤ i ), where What is the minimum possible number of inversions in the resulting array
ai is the maximum
height Gorf can jump from i meters below ground c ? Recall that an inversion is a pair of indices (i, j) such that i < j and
level. ci > cj .
Output The first line of each test case contains two integers n and m (
If Gorf can't reach ground level, print −1. Otherwise,
firstly print integer 1 ≤ n, m ≤ 10 ).
6
3
Output
0 2 2
For each test case, print one integer — the minimum possible number of
1 1 0
3 4
input 1 2 3
4 3 2 1
2
3 3
1 1
3 2 1
1 0
1 2 3
5 4
output 1 3 5 3 1
-1
4 3 6 1
output Output
Print one integer equal to the maximum number of alpinists who
can climb
0
4
the mountain if they choose the right order to do so.
6
input
Below is given the solution to get the optimal answer for each of the
3 2
3 5
– – –
In the second test case, c = [1, 2, 3, 2, 1, 3] . output
– – –
In the third test case, c = [1, 1, 3, 3, 5, 3, 1, 4, 6] .
– – – – – 2
2 4
6 4
max(p, a). 2 7
7 6
Alpinists will climb the mountain one by one. And before the
start, they 3 2
Note that after the order is chosen, each alpinist who can climb
the In the first example, alpinists 2 and 3 can climb the
mountain if they go in
mountain, must climb the mountain at that time. this order. There is no other way to
achieve the answer of 2 .
i -th alpinist.