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

A. Su X Three: Codeforces Round #607 (Div. 2)

Codeforces problem set in pdf

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)
50 views5 pages

A. Su X Three: Codeforces Round #607 (Div. 2)

Codeforces problem set in pdf

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 #607 (Div.

2)

A. Suffix Three 2 seconds, 256 megabytes

1 second, 256 megabytes Your friend Jeff Zebos has been trying to run his new online company, but
it's not going very well. He's not getting a lot of sales on his website
We just discovered a new data structure in our research group: a suffix which he decided to call Azamon. His big problem, you think, is that he's
three! not ranking high enough on the search engines. If only he could rename
his products to have better names than his competitors, then he'll be at
It's very useful for natural language processing. Given three languages
the top of the search results and will be a millionaire.
and three suffixes, a suffix three can determine which language a
sentence is written in. After doing some research, you find out that search engines only sort
their results lexicographically. If your friend could rename his products to
It's super simple, 100% accurate, and doesn't involve advanced machine
lexicographically smaller strings than his competitor's, then he'll be at the
learning algorithms.
top of the rankings!
Let us tell you how it works.
To make your strategy less obvious to his competitors, you decide to
If a sentence ends with "po" the language is Filipino. swap no more than two letters of the product names.
If a sentence ends with "desu" or "masu" the language is Japanese. Please help Jeff to find improved names for his products that are
If a sentence ends with "mnida" the language is Korean. lexicographically smaller than his competitor's!

Given this, we need you to implement a suffix three that can differentiate Given the string s representing Jeff's product name and the string c
Filipino, Japanese, and Korean. representing his competitor's product name, find a way to swap at most
one pair of characters in s (that is, find two distinct indices i and j and
Oh, did I say three suffixes? I meant four.
swap s and s ) such that the resulting new name becomes strictly
i j

Input lexicographically smaller than c , or determine that it is impossible.


The first line of input contains a single integer t (1 ≤ t ≤ 30) denoting the
Note: String a is strictly lexicographically smaller than string b if and only
number of test cases. The next lines contain descriptions of the test
if one of the following holds:
cases.

Each test case consists of a single line containing a single string denoting a is a proper prefix of b , that is, a is a prefix of b such that a ≠ b ;
the sentence. Spaces are represented as underscores (the symbol "_") There exists an integer 1 ≤ i ≤ min (|a|, |b|) such that a
i
< b
i
and
for ease of reading. The sentence has at least 1 and at most 1000 a
j
= b
j
for 1 ≤ j < i .
characters, and consists only of lowercase English letters and
underscores. The sentence has no leading or trailing underscores and no Input
two consecutive underscores. It is guaranteed that the sentence ends The first line of input contains a single integer t (1 ≤ t ≤ 1500 ) denoting
with one of the four suffixes mentioned above. the number of test cases. The next lines contain descriptions of the test
cases.
Output
For each test case, print a single line containing either "FILIPINO", Each test case consists of a single line containing two space-separated
"JAPANESE", or "KOREAN" (all in uppercase, without quotes), strings s and c (2 ≤ |s| ≤ 5000, 1 ≤ |c| ≤ 5000 ). The strings s and c consists
depending on the detected language. of uppercase English letters.

It is guaranteed that the sum of |s| in the input is at most 5000 and the
input sum of the |c| in the input is at most 5000 .
8
kamusta_po Output
genki_desu For each test case, output a single line containing a single string, which is
ohayou_gozaimasu
either
annyeong_hashimnida
hajime_no_ippo
bensamu_no_sentou_houhou_ga_okama_kenpo the new name which is obtained after swapping no more than one
ang_halaman_doon_ay_sarisari_singkamasu pair of characters that is strictly lexicographically smaller than c . In
si_roy_mustang_ay_namamasu
case there are many possible such strings, you can output any of
output them;
FILIPINO three dashes (the string "---" without quotes) if it is impossible.
JAPANESE
JAPANESE
KOREAN input
FILIPINO 3
FILIPINO AZAMON APPLE
JAPANESE AZAMON AAAAAAAAAAALIBABA
JAPANESE APPLE BANANA

The first sentence ends with "po", so it is written in Filipino. output


AMAZON
The second and third sentences end with "desu" and "masu", so they ---
are written in Japanese. APPLE

The fourth sentence ends with "mnida", so it is written in Korean. In the first test case, it is possible to swap the second and the fourth
letters of the string and the resulting string "AMAZON" is lexicographically
smaller than "APPLE".
B. Azamon Web Services /
It is impossible to improve the product's name in the second test case input
and satisfy all conditions.
4
In the third test case, it is possible not to swap a pair of characters. The 5
231
name "APPLE" is lexicographically smaller than "BANANA". Note that 7
there are other valid answers, e.g., "APPEL". 2323
6
333
C. Cut and Paste 24
133321333
2 seconds, 256 megabytes
output
We start with a string s consisting only of the digits 1 , 2 , or 3 . The length 25
of s is denoted by |s| . For each i from 1 to |s| , the i-th character of 𝑠 is 1438
1101
denoted by 𝑠𝑖 . 686531475
There is one cursor. The cursor's location ℓ is denoted by an integer in
{0, … , |𝑠|} , with the following meaning:
Let's illustrate what happens with the first test case. Initially, we have 𝑠 =

231. Initially, ℓ = 0 and 𝑐 = 𝜀 (the empty string). The following things


If ℓ = 0 , then the cursor is located before the first character of 𝑠. happen if we follow the procedure above:
If ℓ = |𝑠| , then the cursor is located right after the last character of 𝑠.
Step 1, Move once: we get ℓ = 1 .
If 0 < ℓ < |𝑠| , then the cursor is located between 𝑠ℓ and 𝑠ℓ .
Step 2, Cut once: we get 𝑠 2 and 𝑐 31.
+1
= =

We denote by 𝑠 the string to the left of the cursor and 𝑠


left right
the string to Step 3, Paste 𝑠ℓ = 2 times: we get 𝑠 = 23131.
the right of the cursor. Step 4: ℓ = 1 ≠ 𝑥 = 5 , so we return to step 1.

We also have a string 𝑐 , which we call our clipboard, which starts out as Step 1, Move once: we get ℓ = 2 .
empty. There are three types of actions: Step 2, Cut once: we get 𝑠 = 23 and 𝑐 =131.
Step 3, Paste 𝑠ℓ = 3 times: we get 𝑠 = 23131131131.
The Move action. Move the cursor one step to the right. This
Step 4: ℓ = 2 ≠ 𝑥 = 5 , so we return to step 1.
increments ℓ once.
The Cut action. Set 𝑐 ← 𝑠right , then set 𝑠 ← 𝑠left . Step 1, Move once: we get ℓ = 3 .
The Paste action. Append the value of 𝑐 to the end of the string 𝑠. Step 2, Cut once: we get 𝑠 = 231 and 𝑐 = 31131131.
Note that this doesn't modify 𝑐 . Step 3, Paste 𝑠ℓ = 1 time: we get 𝑠 = 23131131131.
Step 4: ℓ = 3 ≠ 𝑥 = 5 , so we return to step 1.
The cursor initially starts at ℓ = 0 . Then, we perform the following
procedure: Step 1, Move once: we get ℓ = 4 .
Step 2, Cut once: we get 𝑠 = 2313 and 𝑐 = 1131131.
1. Perform the Move action once.
Step 3, Paste 𝑠ℓ
3 times: we get 𝑠 =
=
2. Perform the Cut action once.
2313113113111311311131131.
3. Perform the Paste action 𝑠ℓ times.
Step 4: ℓ = 4 ≠ 𝑥 = 5 , so we return to step 1.
4. If ℓ = 𝑥 , stop. Otherwise, return to step 1.
Step 1, Move once: we get ℓ = 5 .
You're given the initial string 𝑠 and the integer 𝑥. What is the length of 𝑠
Step 2, Cut once: we get 𝑠 = 23131 and 𝑐 =
when the procedure stops? Since this value may be very large, only find it
9
13113111311311131131.
modulo 10 + 7 .
Step 3, Paste 𝑠 = 1 times: we get 𝑠 =

It is guaranteed that ℓ ≤ |𝑠| at any time. 2313113113111311311131131.


Step 4: ℓ = 5 = 𝑥 , so we stop.
Input
The first line of input contains a single integer 𝑡 (1 ≤ 𝑡 ≤ 1000 ) denoting At the end of the procedure, 𝑠 has length 25.
the number of test cases. The next lines contain descriptions of the test
cases.
D. Beingawesomeism
The first line of each test case contains a single integer 𝑥 (1 ≤ 𝑥 ≤ 10 ).
6

2 seconds, 256 megabytes


The second line of each test case consists of the initial string 𝑠 (
1 ≤ |𝑠| ≤ 500). It is guaranteed, that 𝑠 consists of the characters "1", "2",
You are an all-powerful being and you have created a rectangular world.
"3". In fact, your world is so bland that it could be represented by a 𝑟 × 𝑐 grid.
6
Each cell on the grid represents a country. Each country has a dominant
It is guaranteed that the sum of 𝑥 in a single file is at most 10 . It is religion. There are only two religions in your world. One of the religions is
guaranteed that in each test case before the procedure will stop it will be called Beingawesomeism, who do good for the sake of being good. The
true that ℓ ≤ |𝑠| at any time. other religion is called Pushingittoofarism, who do murders for the sake of
being bad.
Output
For each test case, output a single line containing a single integer Oh, and you are actually not really all-powerful. You just have one power,
which you can use infinitely many times! Your power involves missionary
9
denoting the answer for that test case modulo 10 + 7 .
groups. When a missionary group of a certain country, say 𝑎, passes by
another country 𝑏 , they change the dominant religion of country 𝑏 to the
dominant religion of country 𝑎.

In particular, a single use of your power is this:


/
You choose a horizontal 1 × 𝑥 subgrid or a vertical 𝑥 × 1 subgrid. input
That value of 𝑥 is up to you;
4
You choose a direction 𝑑. If you chose a horizontal subgrid, your 7 8
choices will either be NORTH or SOUTH. If you choose a vertical AAPAAAAA
PPPPAAAA
subgrid, your choices will either be EAST or WEST; PPPPAAAA
You choose the number 𝑠 of steps; APAAPPPP
APAPPAPP
You command each country in the subgrid to send a missionary
AAAAPPAP
group that will travel 𝑠 steps towards direction 𝑑. In each step, they AAAAPPAA
will visit (and in effect convert the dominant religion of) all 𝑠 countries 6 5
AAAAA
they pass through, as detailed above. AAAAA
The parameters 𝑥, 𝑑, 𝑠 must be chosen in such a way that any of the AAPAA
AAPAP
missionary groups won't leave the grid.
AAAPP
AAAPP
The following image illustrates one possible single usage of your power. 4 4
Here, A represents a country with dominant religion Beingawesomeism PPPP
and P represents a country with dominant religion Pushingittoofarism. PPPP
PPPP
Here, we've chosen a 1 × 4 subgrid, the direction NORTH, and 𝑠 = 2
PPPP
steps. 3 4
PPPP
PAAP
PPPP

output
2
1
MORTAL
4

In the first test case, it can be done in two usages, as follows:

Usage 1:
You are a being which believes in free will, for the most part. However,
you just really want to stop receiving murders that are attributed to your
name. Hence, you decide to use your powers and try to make
Beingawesomeism the dominant religion in every country.

What is the minimum number of usages of your power needed to convert


everyone to Beingawesomeism?

With god, nothing is impossible. But maybe you're not god? If it is


impossible to make Beingawesomeism the dominant religion in all
countries, you must also admit your mortality and say so.

Input Usage 2:
4
The first line of input contains a single integer 𝑡 (1 ≤ 𝑡 ≤ 2 ⋅ 10 ) denoting
the number of test cases.

The first line of each test case contains two space-separated integers 𝑟
and 𝑐 denoting the dimensions of the grid (1 ≤ 𝑟, 𝑐 ≤ 60 ). The next 𝑟 lines
each contains 𝑐 characters describing the dominant religions in the
countries. In particular, the 𝑗 -th character in the 𝑖-th line describes the
dominant religion in the country at the cell with row 𝑖 and column 𝑗 ,
where:

"A" means that the dominant religion is Beingawesomeism;


"P" means that the dominant religion is Pushingittoofarism. In the second test case, it can be done with just one usage of the power.

In the third test case, it is impossible to convert everyone to


It is guaranteed that the grid will only contain "A" or "P" characters. It is
6
Beingawesomeism, so the answer is "MORTAL".
guaranteed that the sum of the 𝑟 ⋅ 𝑐 in a single file is at most 3 ⋅ 10 .

Output E. Jeremy Bearimy


For each test case, output a single line containing the minimum number
of usages of your power needed to convert everyone to 3 seconds, 256 megabytes
Beingawesomeism, or the string "MORTAL" (without quotes) if it is
Welcome! Everything is fine.
impossible to do so.
You have arrived in The Medium Place, the place between The Good
Place and The Bad Place. You are assigned a task that will either make
people happier or torture them for eternity.

/
You have a list of 𝑘 pairs of people who have arrived in a new inhabited The first pair of people get assigned to houses 5 and 6 , giving us
neighborhood. You need to assign each of the 2𝑘 people into one of the ;
𝑓 (1) = 5

2𝑘 houses. Each person will be the resident of exactly one house, and
The second pair of people get assigned to houses 1 and 4 , giving us
each house will have exactly one resident.
𝑓 (2) = 6 ;
Of course, in the neighborhood, it is possible to visit friends. There are The third pair of people get assigned to houses 3 and 2 , giving us
2𝑘 ⎯ 1 roads, each of which connects two houses. It takes some time to 𝑓 (3) = 4 .
traverse a road. We will specify the amount of time it takes in the input.
The neighborhood is designed in such a way that from anyone's house, Note that the sum of the 𝑓 (𝑖) is 5 + 6 + 4 = 15 .
there is exactly one sequence of distinct roads you can take to any other
We also have a maximum sum equal to 𝐵 = 33 . One way this can be
house. In other words, the graph with the houses as vertices and the
achieved is with the following assignment:
roads as edges is a tree.

The truth is, these 𝑘 pairs of people are actually soulmates. We index The first pair of people get assigned to houses 1 and 4 , giving us
them from 1 to 𝑘. We denote by 𝑓 (𝑖) the amount of time it takes for the 𝑖- 𝑓 (1) = 6 ;
th pair of soulmates to go to each other's houses. The second pair of people get assigned to houses 6 and 2 , giving us
𝑓 (2) = 14;
As we have said before, you will need to assign each of the 2𝑘 people
The third pair of people get assigned to houses 3 and 5 , giving us
into one of the 2𝑘 houses. You have two missions, one from the entities
in The Good Place and one from the entities of The Bad Place. Here they 𝑓 (3) = 13 .
are:
Note that the sum of the 𝑓 (𝑖) is 6 + 14 + 13 = 33 .
The first mission, from The Good Place, is to assign the people into
the houses such that the sum of 𝑓 (𝑖) over all pairs 𝑖 is minimized. F. Miss Punyverse
Let's define this minimized sum as 𝐺. This makes sure that
soulmates can easily and efficiently visit each other; 4 seconds, 512 megabytes
The second mission, from The Bad Place, is to assign the people into The Oak has 𝑛 nesting places, numbered with integers from 1 to 𝑛.
the houses such that the sum of 𝑓 (𝑖) over all pairs 𝑖 is maximized. Nesting place 𝑖 is home to 𝑏𝑖 bees and 𝑤𝑖 wasps.
Let's define this maximized sum as 𝐵 . This makes sure that
Some nesting places are connected by branches. We call two nesting
soulmates will have a difficult time to visit each other.
places adjacent if there exists a branch between them. A simple path
What are the values of 𝐺 and 𝐵 ? from nesting place 𝑥 to 𝑦 is given by a sequence 𝑠 , … , 𝑠𝑝 of distinct
0

nesting places, where 𝑝 is a non-negative integer, 𝑠 = 𝑥 , 𝑠𝑝 = 𝑦 , and


Input
0

𝑠𝑖⎯1and 𝑠𝑖 are adjacent for each 𝑖 = 1, … , 𝑝. The branches of The Oak


The first line of input contains a single integer 𝑡 (1 ≤ 𝑡 ≤ 500 ) denoting the
are set up in such a way that for any two pairs of nesting places 𝑥 and 𝑦,
number of test cases. The next lines contain descriptions of the test
there exists a unique simple path from 𝑥 to 𝑦. Because of this, biologists
cases.
and computer scientists agree that The Oak is in fact, a tree.
The first line of each test case contains a single integer 𝑘 denoting the
A village is a nonempty set 𝑉 of nesting places such that for any two 𝑥
5
number of pairs of people (1 ≤ 𝑘 ≤ 10 ). The next 2𝑘 ⎯ 1 lines describe and 𝑦 in 𝑉 , there exists a simple path from 𝑥 to 𝑦 whose intermediate
the roads; the 𝑖-th of them contains three space-separated integers nesting places all lie in 𝑉 .
𝑎𝑖 , 𝑏𝑖 , 𝑡𝑖 which means that the 𝑖-th road connects the 𝑎𝑖 -th and 𝑏𝑖 -th
A set of villages  is called a partition if each of the 𝑛 nesting places is
houses with a road that takes 𝑡𝑖 units of time to traverse (1 ≤ 𝑎𝑖 , 𝑏𝑖 ≤ 2𝑘,
6
contained in exactly one of the villages in  . In other words, no two
𝑎𝑖 ≠ 𝑏𝑖 ,1 ≤ 𝑡𝑖 ≤ 10 ). It is guaranteed that the given roads define a tree villages in  share any common nesting place, and altogether, they
structure. contain all 𝑛 nesting places.

The Oak holds its annual Miss Punyverse beauty pageant. The two
5
It is guaranteed that the sum of the 𝑘 in a single file is at most 3 ⋅ 10 .
contestants this year are Ugly Wasp and Pretty Bee. The winner of the
Output beauty pageant is determined by voting, which we will now explain.
For each test case, output a single line containing two space-separated
Suppose  is a partition of the nesting places into 𝑚 villages 𝑉 , … , 𝑉𝑚 .
integers 𝐺 and 𝐵 .
1

There is a local election in each village. Each of the insects in this village
vote for their favorite contestant. If there are strictly more votes for Ugly
input
Wasp than Pretty Bee, then Ugly Wasp is said to win in that village.
2 Otherwise, Pretty Bee wins. Whoever wins in the most number of villages
3
1 2 3 wins.
3 2 4
2 4 3
As it always goes with these pageants, bees always vote for the bee
4 5 6 (which is Pretty Bee this year) and wasps always vote for the wasp (which
5 6 5 is Ugly Wasp this year). Unlike their general elections, no one abstains
2 from voting for Miss Punyverse as everyone takes it very seriously.
1 2 1
1 3 2 Mayor Waspacito, and his assistant Alexwasp, wants Ugly Wasp to win.
1 4 3
He has the power to choose how to partition The Oak into exactly 𝑚
output villages. If he chooses the partition optimally, determine the maximum
15 33 number of villages in which Ugly Wasp wins.
6 6
Input
The first line of input contains a single integer 𝑡 (1 ≤ 𝑡 ≤ 100 ) denoting the
For the sample test case, we have a minimum sum equal to 𝐺 = 15 . One
number of test cases. The next lines contain descriptions of the test
way this can be achieved is with the following assignment:
cases.
/
The first line of each test case contains two space-separated integers 𝑛 output
and 𝑚 (1 ≤ 𝑚 ≤ 𝑛 ≤ 3000 ). The second line contains 𝑛 space-separated
2
9
integers 𝑏 1
, 𝑏2 , … , 𝑏𝑛 (0 ≤ 𝑏𝑖 ≤ 10 ). The third line contains 𝑛 space- 0
9
separated integers 𝑤 , 𝑤 , … , 𝑤𝑛 (0 ≤ 𝑤𝑖 ≤ 10 ). The next 𝑛 ⎯ 1 lines
1 2
In the first test case, we need to partition the 𝑛 = 4 nesting places into
describe the pairs of adjacent nesting places. In particular, the 𝑖-th of
𝑚 = 3 villages. We can make Ugly Wasp win in 2 villages via the following
them contains two space-separated integers 𝑥𝑖 and 𝑦𝑖 (1 ≤ 𝑥𝑖 , 𝑦𝑖 ≤ 𝑛,
partition: {{1, 2}, {3}, {4}} . In this partition,
𝑥𝑖 ≠ 𝑦𝑖 ) denoting the numbers of two adjacent nesting places. It is

guaranteed that these pairs form a tree. Ugly Wasp wins in village {1, 2}, garnering 181 votes as opposed to
5 Pretty Bee's 170 ;
It is guaranteed that the sum of 𝑛 in a single file is at most 10 .
Ugly Wasp also wins in village {3}, garnering 111 votes as opposed
Output to Pretty Bee's 70;
For each test case, output a single line containing a single integer Ugly Wasp loses in the village {4}, garnering 0 votes as opposed to
denoting the maximum number of villages in which Ugly Wasp wins,
Pretty Bee's 50.
among all partitions of The Oak into 𝑚 villages.
Thus, Ugly Wasp wins in 2 villages, and it can be shown that this is the
input maximum possible number.
2
4 3 In the second test case, we need to partition the 𝑛 = 2 nesting places
10 160 70 50 into 𝑚 = 1 village. There is only one way to do this: {{1, 2}}. In this
70 111 111 0
partition's sole village, Ugly Wasp gets 563 votes, and Pretty Bee also
1 2
2 3 gets 563 votes. Ugly Wasp needs strictly more votes in order to win.
3 4 Therefore, Ugly Wasp doesn't win in any village.
2 1
143 420
214 349
2 1

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov


The only programming contests Web 2.0 platform

You might also like