Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

A. Two Subsequences: Codeforces Round #751 (Div. 2)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Codeforces Round #751 (Div.

2)

A. Two Subsequences Black is gifted with a Divine array a consisting of n


(1 ≤ n ≤ 2000 )
integers. Each position in a has an
initial value. After shouting a curse
2 seconds, 256 megabytes over the array, it becomes
angry and starts an unstoppable
transformation.
You are given a string s . You need to find two non-empty
strings a and b
such that the following conditions are satisfied: The transformation consists of infinite steps. Array a
changes at the i -th
step in the following way: for every
position j , aj becomes equal to the
1. Strings a and b are both subsequences of s . number of
occurrences of aj in a before starting this step.
2. For each index i , character si of string s
must belong to exactly
one
Here is an example to help you understand the process better:
of strings a or b.
3. String a is lexicographically minimum
possible; string b may be any
Initial
array:

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 .

x is a prefix of y, but x ≠ y ; The transformation steps continue forever.


in the first position where x and y differ, the
string x has a letter that
You have to process q queries: in each query, Black is
curious to know the
appears earlier in the alphabet
than the corresponding letter in y.
value of ax after the k -th step
of transformation.
Input Input
Each test contains multiple test cases. The first line contains
the number Each test contains multiple test cases. The first line contains
the number
of test cases t (1 ≤ t ≤ 1000 ).
Description of the test cases follows. of test cases t (1 ≤ t ≤ 1000 ).
Description of the test cases follows.
The first and only line of each test case contains one string
s ( The first line of each test case contains an integer n
(1 ≤ n ≤ 2000 ) —
2 ≤ |s| ≤ 100 where |s| means the length
of s ). String s consists of
the size of the array a.
lowercase Latin letters.
The second line of each test case contains n integers
a1 , a2 , … , an (
Output 1 ≤ ai ≤ n ) — the initial
values of array a .
For each test case, print the strings a and b that
satisfy the given
conditions. If there are multiple answers, print any. The third line of each test case contains a single integer
q (
1 ≤ q ≤ 100 000 ) — the number of queries.

input Next q lines contain the information about queries — one


query per line.
3
The i -th line contains two integers
x i and ki (1 ≤ x i ≤ n;
fc
0 ≤ k i ≤ 10 ), meaning that Black is asking for the value of
ax after the
9

aaaa
i

k i -th step of transformation.


k i = 0 means that Black is interested in
thebrightboiler

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

answer to each query


is unique.
In the second test case, a
is the only character in the string.

In the third test case, it can be proven that b is the lexicographically


smallest subsequence of s . The second string can be of two
variants; one
of them is given here.

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

It's guaranteed that the sum of n over all test cases


doesn't exceed
1 1

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 .

output Print them in increasing order.

2
input
3
5

3
4

1
4 4 4 4

2
4

13 7 25 19

The first test case was described ih the statement. It can be


seen that: 6

3 5 3 1 7 1

1. k1 = 0 (initial array): a3 = 1; 1

2. k2 = 1 (after the 1 -st step): a1 = 2 ; 5

3. k3 = 2 (after the 2 -nd step): a2 = 3; 0 0 0 0 0

4. k4 = 1 (after the 1 -st step): a6 = 3.


output
For the second test case, 1 2 4

1 2

Initial
array: 1 1 1

1 2 3 4 5

After the
1 -st step: 2 2

In the first test case:


After the
2 -nd step: 2 2

... ... If k = 1 , we can make four elimination operations


with sets of indices

, ,
{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

a1 − x = 0 and a3 − x = 0 , and after the second operation —


C. Array Elimination a2 − x = 0
and a4 − x = 0 .
If k = 3 , it's impossible to make all ai equal
to 0 . After performing
2 seconds, 512 megabytes
the first operation, we'll get
three elements equal to 0 and one equal
You are given array a1 , a2 , … , an , consisting of
non-negative integers. to 4 . After
that, all elimination operations won't change anything, since
at
least one chosen element will always be equal to 0 .
Let's define operation of "elimination" with integer
parameter k (
If k = 4 , we can make one operation with set {1, 2, 3, 4}, because
1 ≤ k ≤ n) as follows:
x = a1  & a2  & a3  & a4 = 4 .

Choose k distinct array indices 1 ≤ i1 < i2 < … < ik ≤ n .


In the second test case, if k = 2 then we can make the
following
Calculate x = ai  & ai  &  …  & ai , where & denotes the bitwise
1 2 k
elimination operations:
AND operation (notes section
contains formal definition).
Subtract x from each of ai 1
, ai , … , ai
2 k
; all other elements remain Operation with indices {1, 3} : x = a1  & a3 = 13 & 25 = 9 .
untouched. a1 − x = 13 − 9 = 4 and a3 − x = 25 − 9 = 16 . Array a will

become equal to [4, 7, 16, 19] .


Find all possible values of k , such that it's possible to
make all elements
Operation with indices {3, 4} : x = a3  & a4 = 16 & 19 = 16.
of array a equal to 0 using a finite
number of elimination operations with
a3 − x = 16 − 16 = 0 and a4 − x = 19 − 16 = 3 . Array a will
parameter k . It can be
proven that exists at least one possible k for any
array a. become equal to [4, 7, 0, 3] .
Operation with indices {2, 4} : x = a2  & a4 = 7 & 3 = 3.
Note that you firstly choose
k and only after that perform elimination a2 − x = 7 − 3 = 4 and a4 − x = 3 − 3 = 0. Array a will
operations with
value k you've chosen initially.
become
equal to [4, 4, 0, 0] .
Input Operation with indices {1, 2} : x = a1  & a2 = 4 & 4 = 4 .
Each test contains multiple test cases. The first line contains
the number a1 − x = 4 − 4 = 0 and a2 − x = 4 − 4 = 0 . Array a will
of test cases t (1 ).
Description of the test cases follows.
4
≤ t ≤ 10 become
equal to [0, 0, 0, 0] .
Formal definition of bitwise AND: input
Let's define bitwise AND (&) as follows. Suppose we
have two non- 10

negative integers x and y, let's look at


their binary representations 0 1 2 3 5 5 6 7 8 5

9 8 7 1 5 4 3 2 0 0

(possibly, with leading zeroes):


x k … x 2 x 1 x 0 and yk … y2 y1 y0 . Here,
x i is the i -th bit of number x, and yi
is the i -th bit of number y. Let output
r = x & y is a result of operation & on number x and
y. Then binary
3

representation of r will be r k … r 2 r 1 r 0 , where: 9 4 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 .

The third line contains n integers b1 , b2 , … , bn (0 ≤ bi ≤ n − i ), Input


where bi is the
distance Gorf will slip down if he takes a break on i meters Each test contains multiple test cases. The first line contains the number
below ground level. of test cases t (1 ≤ t ≤ 104 ). Description of the test cases follows.

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

k  — the minimum possible number of jumps.


The second line of each test case contains n integers a1 , a2 , … , an (
Then print the sequence d1 , d2 , … , dk where
dj is the depth Gorf'll 1 ≤ ai ≤ 10 ).
9

reach after the j -th jump,


but before he'll slip down during the break.
Ground level is equal
to 0 . The third line of each test case contains m integers b1 , b2 , … , bm (
1 ≤ bi ≤ 10 ).
9

If there are multiple answers, print any of them.


It is guaranteed that the sum of n for all tests cases in one input doesn't
input exceed 10 . The sum of m for all tests cases doesn't exceed 10 as well.
6 6

3
Output
0 2 2

For each test case, print one integer — the minimum possible number of
1 1 0

inversions in the resulting array c.


output
2
input
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

example test cases (elements of a are underscored). 2 6

3 5

In the first test case, c = [1, 1, 2, 2, 3, 3, 4] . 5 7

– – –
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

F. Difficult Mountain input


2 seconds, 512 megabytes 3 3

2 4

6 4

A group of n alpinists has just reached the foot of the


mountain. The initial
4 6

difficulty of climbing this mountain can be


described as an integer d .
output
Each alpinist can be described by two integers s and
a, where s is his
2

skill of climbing mountains and


a is his neatness.

An alpinist of skill level s is able to climb a mountain of


difficulty p only if input
p ≤ s. As an alpinist climbs a
mountain, they affect the path and thus
5 0

may change mountain


difficulty. Specifically, if an alpinist of neatness a 1 5

climbs a mountain of difficulty p the difficulty of this


mountain becomes 4 8

max(p, a). 2 7

7 6

Alpinists will climb the mountain one by one. And before the
start, they 3 2

wonder, what is the maximum number of alpinists who


will be able to climb output
the mountain if they choose the right order.
As you are the only person in
3

the group who does programming, you


are to answer the question.

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 .

Input In the second example, alpinist 1 is not able to climb


because of the initial
The first line contains two integers n and d (1 ≤ n ≤ 500 000; difficulty of the mountain, while alpinists
2 and 3 can go up in any order.
0 ≤ d ≤ 10 ) — the number of
alpinists and the initial difficulty of the
9

In the third example, the mountain can be climbed by alpinists


5 , 3 and 4
mountain. in this particular order. There is no
other way to achieve optimal answer.
Each of the next n lines contains two integers si
and ai (
0 ≤ si , ai ≤ 10 ) that define the
skill of climbing and the neatness of the
9

i -th alpinist.

Codeforces (c) Copyright 2010-2021 Mike Mirzayanov


The only programming contests Web 2.0 platform

You might also like