0% found this document useful (0 votes)
74 views5 pages

A. Three Strings: Codeforces Round #619 (Div. 2)

The document describes two algorithm problems - making three strings equal by swapping characters, and minimizing the maximum difference between adjacent elements in an integer array by replacing missing values. Test cases and expected outputs are provided for both problems.

Uploaded by

Mystic life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views5 pages

A. Three Strings: Codeforces Round #619 (Div. 2)

The document describes two algorithm problems - making three strings equal by swapping characters, and minimizing the maximum difference between adjacent elements in an integer array by replacing missing values. Test cases and expected outputs are provided for both problems.

Uploaded by

Mystic life
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Codeforces Round #619 (Div.

2)

A. Three Strings In the second test case, you should swap $$$c_i$$$ with $$$a_i$$$ for
all possible $$$i$$$. After the swaps $$$a$$$ becomes "bca", $$$b$$$
1 second, 256 megabytes becomes "bca" and $$$c$$$ becomes "abc". Here the strings $$$a$$$
and $$$b$$$ are equal.
You are given three strings $$$a$$$, $$$b$$$ and $$$c$$$ of the same
length $$$n$$$. The strings consist of lowercase English letters only. The In the third test case, you should swap $$$c_1$$$ with $$$a_1$$$,
$$$i$$$-th letter of $$$a$$$ is $$$a_i$$$, the $$$i$$$-th letter of $$$c_2$$$ with $$$b_2$$$, $$$c_3$$$ with $$$b_3$$$ and $$$c_4$$$
$$$b$$$ is $$$b_i$$$, the $$$i$$$-th letter of $$$c$$$ is $$$c_i$$$. with $$$a_4$$$. Then string $$$a$$$ becomes "baba", string $$$b$$$
becomes "baba" and string $$$c$$$ becomes "abab". Here the strings
For every $$$i$$$ ($$$1 \leq i \leq n$$$) you must swap (i.e. exchange)
$$$a$$$ and $$$b$$$ are equal.
$$$c_i$$$ with either $$$a_i$$$ or $$$b_i$$$. So in total you'll perform
exactly $$$n$$$ swap operations, each of them either $$$c_i In the fourth test case, it is impossible to do the swaps so that string
\leftrightarrow a_i$$$ or $$$c_i \leftrightarrow b_i$$$ ($$$i$$$ iterates $$$a$$$ becomes exactly the same as string $$$b$$$.
over all integers between $$$1$$$ and $$$n$$$, inclusive).

For example, if $$$a$$$ is "code", $$$b$$$ is "true", and $$$c$$$ is B. Motarack's Birthday
"help", you can make $$$c$$$ equal to "crue" taking the $$$1$$$-st
2 seconds, 256 megabytes
and the $$$4$$$-th letters from $$$a$$$ and the others from $$$b$$$.
In this way $$$a$$$ becomes "hodp" and $$$b$$$ becomes "tele". Dark is going to attend Motarack's birthday. Dark decided that the gift he
Is it possible that after these swaps the string $$$a$$$ becomes exactly is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-
the same as the string $$$b$$$? negative integers.

Dark created that array $$$1000$$$ years ago, so some elements in that
Input
The input consists of multiple test cases. The first line contains a single array disappeared. Dark knows that Motarack hates to see an array that
integer $$$t$$$ ($$$1 \leq t \leq 100$$$)  — the number of test cases. has two adjacent elements with a high absolute difference between them.
The description of the test cases follows. He doesn't have much time so he wants to choose an integer $$$k$$$
($$$0 \leq k \leq 10^{9}$$$) and replaces all missing elements in the array
The first line of each test case contains a string of lowercase English $$$a$$$ with $$$k$$$.
letters $$$a$$$.
Let $$$m$$$ be the maximum absolute difference between all adjacent
The second line of each test case contains a string of lowercase English elements (i.e. the maximum value of $$$|a_i - a_{i+1}|$$$ for all $$$1 \leq
letters $$$b$$$. i \leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing
elements with $$$k$$$.
The third line of each test case contains a string of lowercase English
letters $$$c$$$. Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized.
Can you help him?
It is guaranteed that in each test case these three strings are non-empty
and have the same length, which is not exceeding $$$100$$$. Input
The input consists of multiple test cases. The first line contains a single
Output
integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$)  — the number of test cases.
Print $$$t$$$ lines with answers for all test cases. For each test case:
The description of the test cases follows.
If it is possible to make string $$$a$$$ equal to string $$$b$$$ print
The first line of each test case contains one integer $$$n$$$ ($$$2 \leq n
"YES" (without quotes), otherwise print "NO" (without quotes).
\leq 10^{5}$$$) — the size of the array $$$a$$$.
You can print either lowercase or uppercase letters in the answers.
The second line of each test case contains $$$n$$$ integers $$$a_1,
a_2, \ldots, a_n$$$ ($$$-1 \leq a_i \leq 10 ^ {9}$$$). If $$$a_i = -1$$$,
input
then the $$$i$$$-th integer is missing. It is guaranteed that at least one
4 integer is missing in every test case.
aaa
bbb It is guaranteed, that the sum of $$$n$$$ for all test cases does not
ccc
exceed $$$4 \cdot 10 ^ {5}$$$.
abc
bca
bca Output
aabb Print the answers for each test case in the following format:
bbaa
baba You should print two integers, the minimum possible value of $$$m$$$
imi and an integer $$$k$$$ ($$$0 \leq k \leq 10^{9}$$$) that makes the
mii maximum absolute difference between adjacent elements in the array
iim
$$$a$$$ equal to $$$m$$$.
output
Make sure that after replacing all the missing elements with $$$k$$$, the
NO maximum absolute difference between adjacent elements becomes
YES
YES $$$m$$$.
NO
If there is more than one possible $$$k$$$, you can print any of them.
In the first test case, it is impossible to do the swaps so that string
$$$a$$$ becomes exactly the same as string $$$b$$$.

/
input 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
7
5
string and the number of symbols equal to "1" in it.
-1 10 -1 12 -1
5 Output
-1 40 35 -1 35 For every test case print one integer number — the maximum value of
6 $$$f(s)$$$ over all strings $$$s$$$ of length $$$n$$$, which has exactly
-1 -1 9 -1 3 -1
$$$m$$$ symbols, equal to "1".
2
-1 -1
2 input
0 -1
4 5
1 -1 3 -1 3 1
7 3 2
1 -1 7 5 2 -1 5 3 3
4 0
output 5 2

1 11 output
5 35
3 6 4
0 42 5
0 0 6
1 2 0
3 4 12

In the first test case after replacing all missing elements with $$$11$$$ In the first test case, there exists only $$$3$$$ strings of length $$$3$$$,
the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference which has exactly $$$1$$$ symbol, equal to "1". These strings are:
between any adjacent elements is $$$1$$$. It is impossible to choose a $$$s_1 = $$$"100", $$$s_2 = $$$"010", $$$s_3 = $$$"001". The
value of $$$k$$$, such that the absolute difference between any adjacent values of $$$f$$$ for them are: $$$f(s_1) = 3, f(s_2) = 4, f(s_3) = 3$$$, so
element will be $$$\leq 0$$$. So, the answer is $$$1$$$. the maximum value is $$$4$$$ and the answer is $$$4$$$.

In the third test case after replacing all missing elements with $$$6$$$ In the second test case, the string $$$s$$$ with the maximum value is
the array becomes $$$[6, 6, 9, 6, 3, 6]$$$. "101".

$$$|a_1 - a_2| = |6 - 6| = 0$$$; In the third test case, the string $$$s$$$ with the maximum value is
"111".
$$$|a_2 - a_3| = |6 - 9| = 3$$$;
$$$|a_3 - a_4| = |9 - 6| = 3$$$; In the fourth test case, the only string $$$s$$$ of length $$$4$$$, which
$$$|a_4 - a_5| = |6 - 3| = 3$$$; has exactly $$$0$$$ symbols, equal to "1" is "0000" and the value of
$$$|a_5 - a_6| = |3 - 6| = 3$$$. $$$f$$$ for that string is $$$0$$$, so the answer is $$$0$$$.

So, the maximum difference between any adjacent elements is $$$3$$$. 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.

C. Ayoub's function
D. Time to Run
1 second, 256 megabytes
1 second, 256 megabytes
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 Bashar was practicing for the national programming contest. Because of
symbols "0" and "1"). The function $$$f(s)$$$ is equal to the number of sitting too much in front of the computer without doing physical
substrings in the string $$$s$$$ that contains at least one symbol, that is movements and eating a lot Bashar became much fatter. Bashar is going
equal to "1". to quit programming after the national contest and he is going to become
an actor (just like his father), so he should lose weight.
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 In order to lose weight, Bashar is going to run for $$$k$$$ kilometers.
Bashar is going to run in a place that looks like a grid of $$$n$$$ rows
the length of string $$$s$$$), such that at least one of the symbols
and $$$m$$$ columns. In this grid there are two one-way roads of one-
$$$s_l, s_{l+1}, \ldots, s_r$$$ is equal to "1".
kilometer length between each pair of adjacent by side cells, one road is
For example, if $$$s = $$$"01010" then $$$f(s) = 12$$$, because there going from the first cell to the second one, and the other road is going
are $$$12$$$ such pairs $$$(l, r)$$$: $$$(1, 2), (1, 3), (1, 4), (1, 5), (2, 2), from the second cell to the first one. So, there are exactly $$$(4 n m - 2n
(2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)$$$. - 2m)$$$ roads.

Ayoub also thinks that he is smarter than Mahmoud so he gave him two Let's take, for example, $$$n = 3$$$ and $$$m = 4$$$. In this case,
integers $$$n$$$ and $$$m$$$ and asked him this problem. For all there are $$$34$$$ roads. It is the picture of this case (arrows describe
binary strings $$$s$$$ of length $$$n$$$ which contains exactly roads):
$$$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?

Input
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.
/
If the answer is "YES", on the second line print an integer $$$a$$$ ($$$1
\leq a \leq 3000$$$) — the number of steps, then print $$$a$$$ lines
describing the steps.

To describe a step, print an integer $$$f$$$ ($$$1 \leq f \leq 10^{9}$$$)


and a string of moves $$$s$$$ of length at most $$$4$$$. Every
character in $$$s$$$ should be 'U', 'D', 'L' or 'R'.

Bashar will start from the top-left cell. Make sure to move exactly
$$$k$$$ moves without visiting the same road twice and without going
outside the grid. He can finish at any cell.

We can show that if it is possible to run exactly $$$k$$$ kilometers, then


it is possible to describe the path under such output constraints.

input
3 3 4

output
YES
Bashar wants to run by these rules: 2
2 R
2 L
He starts at the top-left cell in the grid;
In one move Bashar may go up (the symbol 'U'), down (the symbol
'D'), left (the symbol 'L') or right (the symbol 'R'). More formally, if he
input
stands in the cell in the row $$$i$$$ and in the column $$$j$$$, i.e. in 3 3 1000000000

the cell $$$(i, j)$$$ he will move to: output


NO
in the case 'U' to the cell $$$(i-1, j)$$$;
in the case 'D' to the cell $$$(i+1, j)$$$;
input
in the case 'L' to the cell $$$(i, j-1)$$$;
3 3 8
in the case 'R' to the cell $$$(i, j+1)$$$;
output
He wants to run exactly $$$k$$$ kilometers, so he wants to make YES
exactly $$$k$$$ moves; 3
2 R
Bashar can finish in any cell of the grid;
2 D
He can't go out of the grid so at any moment of the time he should 1 LLRR
be on some cell;
Bashar doesn't want to get bored while running so he must not visit input
the same road twice. But he can visit the same cell any number of 4 4 9
times.
output
Bashar asks you if it is possible to run by such rules. If it is possible, you YES
should tell him how should he run. 1
3 RLD
You should give him $$$a$$$ steps to do and since Bashar can't
remember too many steps, $$$a$$$ should not exceed $$$3000$$$. In input
every step, you should give him an integer $$$f$$$ and a string of moves
3 4 16
$$$s$$$ of length at most $$$4$$$ which means that he should repeat
the moves in the string $$$s$$$ for $$$f$$$ times. He will perform the output
steps in the order you print them. YES
8
For example, if the steps are $$$2$$$ RUD, $$$3$$$ UUL then the 3 R
moves he is going to move are RUD $$$+$$$ RUD $$$+$$$ UUL 3 L
1 D
$$$+$$$ UUL $$$+$$$ UUL $$$=$$$ RUDRUDUULUULUUL.
3 R
1 D
Can you help him and give him a correct sequence of moves such that
1 U
the total distance he will run is equal to $$$k$$$ kilometers or say, that it 3 L
is impossible? 1 D

Input The moves Bashar is going to move in the first example are: "RRLL".
The only line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$
($$$1 \leq n, m \leq 500$$$, $$$1 \leq k \leq 10 ^{9}$$$), which are the It is not possible to run $$$1000000000$$$ kilometers in the second
number of rows and the number of columns in the grid and the total example because the total length of the roads is smaller and Bashar can't
distance Bashar wants to run. run the same road twice.

Output The moves Bashar is going to move in the third example are:
If there is no possible way to run $$$k$$$ kilometers, print "NO" (without "RRDDLLRR".
quotes), otherwise print "YES" (without quotes) in the first line.

/
The moves Bashar is going to move in the fifth example are: For the next $$$n$$$ lines, every line will contain $$$m$$$ characters. In
"RRRLLLDRRRDULLLD". It is the picture of his run (the roads on this the $$$i$$$-th line the $$$j$$$-th character will contain the color of the
way are marked with red and numbered in the order of his running): cell at the $$$i$$$-th row and $$$j$$$-th column of the Adhami's picture.
The color of every cell will be one of these: {'G','Y','R','B'}.

For the next $$$q$$$ lines, the input will contain four integers $$$r_1$$$,
$$$c_1$$$, $$$r_2$$$ and $$$c_2$$$ $$$(1 \leq r_1 \leq r_2 \leq n, 1
\leq c_1 \leq c_2 \leq m)$$$. In that option, Adhami gave to Warawreh a
sub-rectangle of the picture with the upper-left corner in the cell $$$(r_1,
c_1)$$$ and with the bottom-right corner in the cell $$$(r_2, c_2)$$$.

Output
For every option print the maximum area of sub-square inside the given
sub-rectangle, which can be a NanoSoft Logo. If there are no such sub-
squares, print $$$0$$$.

input
5 5 5
RRGGB
RRGGY
YYBBG
YYBBR
RBBRG
1 1 5 5
2 2 5 5
2 2 3 3
E. Nanosoft 1 1 3 5
4 4 5 5
2 seconds, 512 megabytes
output
Warawreh created a great company called Nanosoft. The only thing that
16
Warawreh still has to do is to place a large picture containing its logo on 4
top of the company's building. 4
4
The logo of Nanosoft can be described as four squares of the same size 0
merged together into one large square. The top left square is colored with
red, the top right square is colored with green, the bottom left square is input
colored with yellow and the bottom right square is colored with blue.
6 10 5
An Example of some correct logos: RRRGGGRRGG
RRRGGGRRGG
RRRGGGYYBB
YYYBBBYYBB
YYYBBBRGRG
YYYBBBYBYB
1 1 6 10
1 3 3 10
2 2 6 6
1 7 6 10
2 1 5 10

output
An Example of some incorrect logos:
36
4
16
Warawreh went to Adhami's store in order to buy the needed picture. 16
Although Adhami's store is very large he has only one picture that can be 16
described as a grid of $$$n$$$ rows and $$$m$$$ columns. The color of
every cell in the picture will be green (the symbol 'G'), red (the symbol 'R'), input
yellow (the symbol 'Y') or blue (the symbol 'B'). 8 8 8
RRRRGGGG
Adhami gave Warawreh $$$q$$$ options, in every option he gave him a RRRRGGGG
sub-rectangle from that picture and told him that he can cut that sub- RRRRGGGG
rectangle for him. To choose the best option, Warawreh needs to know RRRRGGGG
YYYYBBBB
for every option the maximum area of sub-square inside the given sub- YYYYBBBB
rectangle that can be a Nanosoft logo. If there are no such sub-squares, YYYYBBBB
the answer is $$$0$$$. YYYYBBBB
1 1 8 8
Warawreh couldn't find the best option himself so he asked you for help, 5 2 5 7
can you help him? 3 1 8 6
2 3 5 8
1 2 6 8
Input 2 1 5 5
The first line of input contains three integers $$$n$$$, $$$m$$$ and 2 1 7 7
$$$q$$$ $$$(1 \leq n , m \leq 500, 1 \leq q \leq 3 \cdot 10^{5})$$$  — the 6 5 7 5
number of row, the number columns and the number of options.

/
output It is guaranteed that for every color between $$$1$$$ and $$$k$$$ there
is at least one city of that color.
64
0
16
Output
4 For every mission print the minimum possible time to reach city at the cell
16 $$$(r_2, c_2)$$$ starting from city at the cell $$$(r_1, c_1)$$$.
4
36
0
input
3 4 5
Picture for the first test: 1 2 1 3
4 4 5 5
1 2 1 3
2
1 1 3 4
2 2 2 2

output
The pictures from the left to the right corresponds to the options. The 2
border of the sub-rectangle in the option is marked with black, the border 0
of the sub-square with the maximal possible size, that can be cut is
marked with gray. input
4 4 8
1 2 2 8
F. Super Jaber 1 3 4 7
5 1 7 6
5 seconds, 256 megabytes 2 3 8 8
4
Jaber is a superhero in a large country that can be described as a grid 1 1 2 2
with $$$n$$$ rows and $$$m$$$ columns, where every cell in that grid 1 1 3 4
contains a different city. 1 1 2 4
1 1 4 4
Jaber gave every city in that country a specific color between $$$1$$$
output
and $$$k$$$. In one second he can go from the current city to any of the
cities adjacent by the side or to any city with the same color as the 2
3
current city color.
3
4
Jaber has to do $$$q$$$ missions. In every mission he will be in the city
at row $$$r_1$$$ and column $$$c_1$$$, and he should help someone
In the first example:
in the city at row $$$r_2$$$ and column $$$c_2$$$.

Jaber wants your help to tell him the minimum possible time to go from mission $$$1$$$: Jaber should go from the cell $$$(1,1)$$$ to the
the starting city to the finishing city for every mission. cell $$$(3,3)$$$ because they have the same colors, then from the
cell $$$(3,3)$$$ to the cell $$$(3,4)$$$ because they are adjacent by
Input side (two moves in total);
The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$
mission $$$2$$$: Jaber already starts in the finishing cell.
($$$1 \leq n, m \leq 1000$$$, $$$1 \leq k \leq min(40 , n \cdot m)$$$) —
the number of rows, columns and colors. In the second example:
Each of the next $$$n$$$ lines contains $$$m$$$ integers. In the
mission $$$1$$$: $$$(1,1)$$$ $$$\rightarrow$$$ $$$(1,2)$$$
$$$i$$$-th line, the $$$j$$$-th integer is $$$a_{ij}$$$ ($$$1 \leq a_{ij} \leq
k$$$), which is the color assigned to the city in the $$$i$$$-th row and $$$\rightarrow$$$ $$$(2,2)$$$;
$$$j$$$-th column. mission $$$2$$$: $$$(1,1)$$$ $$$\rightarrow$$$ $$$(3,2)$$$
$$$\rightarrow$$$ $$$(3,3)$$$ $$$\rightarrow$$$ $$$(3,4)$$$;
The next line contains one integer $$$q$$$ ($$$1 \leq q \leq 10^{5}$$$)
mission $$$3$$$: $$$(1,1)$$$ $$$\rightarrow$$$ $$$(3,2)$$$
 — the number of missions.
$$$\rightarrow$$$ $$$(3,3)$$$ $$$\rightarrow$$$ $$$(2,4)$$$;
For the next $$$q$$$ lines, every line contains four integers $$$r_1$$$, mission $$$4$$$: $$$(1,1)$$$ $$$\rightarrow$$$ $$$(1,2)$$$
$$$c_1$$$, $$$r_2$$$, $$$c_2$$$ ($$$1 \leq r_1 , r_2 \leq n$$$, $$$1 $$$\rightarrow$$$ $$$(1,3)$$$ $$$\rightarrow$$$ $$$(1,4)$$$
\leq c_1 , c_2 \leq m$$$)  — the coordinates of the starting and the $$$\rightarrow$$$ $$$(4,4)$$$.
finishing cities of the corresponding mission.

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov


The only programming contests Web 2.0 platform

You might also like