Contest Onesided
Contest Onesided
Contest Session
This problem set contains 12 problems; pages are numbered from 1 to 17.
This problem set is used in simultaneous contests with the following participating countries:
Antigua y Barbuda, Argentina, Bolivia, Brasil, Chile, Colombia, Costa Rica, Cuba, El Salvador, Guatemala,
México, Perú, Puerto Rico, República Dominicana, Trinidad y Tobago, Uruguay and Venezuela
v1.0
General information
Unless otherwise stated, the following conditions hold for all problems.
Program name
1. Your solution must be called codename.c, codename.cpp, codename.java, codename.kt, code-
name.py3, where codename is the capital letter which identifies the problem.
Input
1. The input must be read from standard input.
2. The input consists of a single test case, which is described using a number of lines that depends
on the problem. No extra data appear in the input.
3. When a line of data contains several values, they are separated by single spaces. No other spaces
appear in the input. There are no empty lines.
4. The English alphabet is used. There are no letters with tildes, accents, diaereses or other diacrit-
ical marks (ñ, Ã, é, Ì, ô, Ü, ç, etcetera).
5. Every line, including the last one, has the usual end-of-line mark.
Output
1. The output must be written to standard output.
2. The result of the test case must appear in the output using a number of lines that depends on
the problem. No extra data should appear in the output.
3. When a line of results contains several values, they must be separated by single spaces. No other
spaces should appear in the output. There should be no empty lines.
4. The English alphabet must be used. There should be no letters with tildes, accents, diaereses or
other diacritical marks (ñ, Ã, é, Ì, ô, Ü, ç, etcetera).
5. Every line, including the last one, must have the usual end-of-line mark.
Problem A – Append and Panic!
Today is Gabriel’s first day at his new job, and he has been given his first task. He needs to read
a string made up of uppercase letters from a file, sort the letters in the string alphabetically,
filter out repeated letters, and then write the result back to the original file. For instance,
sorting the string “ICPC” would produce “CCIP”, which would become “CIP” after removing
repeated letters. Easy, right?
However, Gabriel made a tiny mistake. Instead of overwriting the file with the filtered string,
he accidentally appended it to the file. Now, the file is corrupted, containing the original string
followed by the sorted, duplicate-free version of it, and Gabriel is in a bit of a panic.
Given the content of the corrupted file, can you determine the length of the original string?
Gabriel is confident that with this information, he will be able to complete his assigned task.
Input
The input consists of a single line that contains a string S made up of uppercase letters
(2 ≤ |S| ≤ 2000), which is the concatenation of the original (uncorrupted) string t and the
sorted, duplicate-free version of t.
Output
Output a single line with an integer indicating the length of t.
ICPCCIP 4
ABEDCCCABCDE 7
ZZ 1
8 9 7 1
6
13 7 6 4
12 11 1 10
14 3 5
3 2
8 2
4 5
As an example, consider the illustration above, which corresponds to the first sample. The
roads highlighted with dotted lines (14, 9 and 12) indicate the chosen competition track. This
track forms a closed loop, visiting three different cities (4, 7 and 8). Note that it remains
possible to travel between any pair of cities using the remaining roads. Additionally, another
possible track is shown with dashed lines, illustrating that the solution is not unique.
Input
The first line contains two integers N (4 ≤ N ≤ 2 · 105 ) and M (6 ≤ M ≤ 3 · 105 ), indicating
respectively the number of cities and the number of two-way roads. Each city is identified by
a distinct integer from 1 to N , and each road is identified by a distinct integer from 1 to M .
The i-th of the next M lines describes road i with two integers U and V (1 ≤ U, V ≤ N and
U 6= V ), indicating that the endpoints of the road are cities U and V .
It is guaranteed that there is at most one road between each pair of cities, each city is an
endpoint of at least three roads, and it is possible to travel between any two cities using one
or more roads.
Output
If a valid track exists, output two lines. The first line must contain the number of roads in the
track, and the second line must contain the identifiers of these roads listed in the order they
appear in the track. If there are multiple solutions, output any of them.
If no valid track exists, output a line with the character “*” (asterisk) instead.
8 14 3
2 6 14 9 12
2 5
2 3
2 1
6 5
6 7
6 1
3 4
7 8
1 5
3 7
4 8
3 8
4 7
(1) R R R B B
R R B G R B
(2)
(3) B G R
R R R B B
(4)
∼ ∼ + +
As an example, for R = 3 and B = 2, consider the picture above. At the top of the picture is
Cindy’s original (3, 2)-sequence (1). Below that is the sequence S after Grinch’s attack (2). A
possible contiguous subsequence with L = 3 and U = 5 follows (3). Finally, a way to transform
the subsequence into a (3, 2)-sequence using four operations is shown, replacing the first two
balls with red balls and adding two blue balls at the end (4). Four is the minimum number of
operations required for this contiguous subsequence.
Input
The first line contains two integers R and B (1 ≤ R, B ≤ 106 ), indicating respectively the
number of red balls and the number of blue balls in Cindy’s (R, B)-sequence.
The second line contains a string S (1 ≤ |S| ≤ 5 · 105 ) describing Grinch’s sequence. Each
character in S is one of the uppercase letters “R”, “B” or “G”, indicating respectively a red,
blue or green ball.
Output
For each contiguous subsequence described in the input, output a line with an integer in-
dicating the minimum number of operations needed to transform the subsequence into an
(R, B)-sequence. Output the results in the same order that the corresponding subsequences
appear in the input.
3 2 4
RRBGRB 3
3 2
3 5
1 5
1 3
2 3 3
RRGRBR 3
6 3
1 3 5
1 2 3
1 4 4
3 3
3 5
5 6
5 6 9
GRBBBB 8
4 9
4 5 10
1 4
5 6
2 2
1 1 1
RB 0
2
1 1
1 2
• Ai,j = 1 if i 6= j and models i and j cannot both be chosen because they share either the
text color or the background color, and
• Ai,j = 0 otherwise.
The organizers need your help to determine if it is possible to select enough T-shirt models for
the event while respecting the diversity rule. Using the vendor’s matrix, can you determine
the maximum number of models that can be selected?
Input
The first line contains an integer N (1 ≤ N ≤ 1000) indicating the number of T-shirt models.
Each of the next N lines contains a binary string of length N . In the i-th string, the j-th
character indicates the value of Ai,j (i = 1, 2, . . . , N and j = 1, 2, . . . , N ).
It is guaranteed that at least one set of T-shirt models corresponding to the input matrix exists.
Output
Output a single line with an integer indicating the maximum number of T-shirt models that
can be chosen without violating the diversity rule.
3 1
011
101
110
3 2
010
101
010
Input
The first line contains an integer N (1 ≤ N ≤ 105 ) indicating the number of stations. Each
station is identified by a distinct integer from 1 to N .
The second line contains N integers A1 , A2 , . . . , AN (0 ≤ Ai ≤ N for i = 1, 2, . . . , N ), represent-
ing the planned itinerary, where Ai = 0 indicates an unreadable station. It is guaranteed that
all readable stations are unique, that is, Ai 6= Aj or Ai = Aj = 0 for each i 6= j. Additionally,
there is at least one unreadable station, meaning there exists i such that Ai = 0.
Output
Output a single line with N integers describing any itinerary that visits each station exactly
once, in a single ascent followed by a single descent. Besides, the itinerary must match the
input itinerary in all but the unreadable stations.
If such an itinerary does not exist, output a line with the character “*” (asterisk) instead.
5 3 4 5 2 1
3 0 5 0 0
5 *
4 2 0 0 1
5 5 4 3 2 1
0 0 0 0 0
Input
The input consists of a single line that contains two integers K and N (1 ≤ K ≤ N ≤ 106 ),
indicating respectively the number of people and the number of toilets.
Output
Output a single line with a string of length N if the K people can choose toilets in a way that
prevents an additional person from finding an available toilet with the required privacy. In
this case the i-th character of the string must be the uppercase letter “X” if the i-th toilet is
chosen, and the character “-” (hyphen) otherwise. Toilets are chosen as it is described in the
statement. If there are multiple solutions, output any of them.
If toilets cannot be chosen as requested, output a line with the character “*” (asterisk) instead.
1 5 *
2 5 -X-X-
3 5 X-X-X
4 5 *
5 5 *
2 5 -X--X
• The Crown Village is Still a Mystery: The race will conclude at the Crown Village,
but no one knows yet which village it will be. The suspense is building with speculations,
keeping everyone guessing and adding excitement to the event.
• The Racers: Each leaf village (a village with only one connecting road) will send one
runner to participate. These runners will start from their respective leaf villages and
race toward the Crown Village along a shortest path. Once a runner reaches the Crown
Village, the race ends for them, but other runners continue their journey toward the
Crown Village until they arrive.
• Claiming Villages: To make the race more appealing for every village, the first runner
to reach a village claims it. If multiple runners arrive at a village at the same time, the
runner starting from the leaf village with the smaller identifier claims the village. This
way, even if a runner doesn’t reach the Crown Village first, they can still claim more
villages along the way, keeping the race competitive and unpredictable.
With these new rules in place, bookmakers across the kingdom are analyzing the odds for
different runners. While the order in which the runners reach the Crown Village is easy to
determine, what really interests the bookmakers is how many villages each runner can claim
during their journey.
To assist the King’s bookmaker, you are tasked with answering queries about various race
scenarios. For each query, you’ll be given two numbers: the identifiers of a leaf village S and
a village T , hypothetically chosen as the Crown Village. Based on this, you need to determine
how many villages will be claimed by a runner starting at S if the race ends at T .
Input
The first line contains an integer N (2 ≤ N ≤ 105 ) indicating the number of villages in the
kingdom. Each village is identified by a distinct integer from 1 to N .
Each of the next N − 1 lines contains three integers U , V and L (1 ≤ U, V ≤ N , U 6= V and
1 ≤ L ≤ 104 ), representing that there is a two-way road connecting villages U and V , with
length L.
The next line contains an integer Q (1 ≤ Q ≤ 105 ) denoting the number of queries.
Each of the next Q lines describes a query with two integers S and T (1 ≤ S, T ≤ N ), indicating
respectively the leaf village where a specific runners starts, and the hypothetical Crown Village
(the finish line for this particular query).
It is guaranteed that the villages and roads form a tree, and that S is a leaf in the tree for each
query.
8 3
6 3 5 5
6 7 1 1
6 5 1 1
5 4 4 1
7 8 1 2
8 1 1 1
8 2 1 2
8
3 1
1 5
4 5
1 1
2 5
2 1
3 5
4 1
Explanation of sample 1:
The picture below shows the villages that each runner will claim if the end of the race is
at village 1 (left) and 5 (right).
where Bi is the base execution time we already defined, and C(i) is the multiset of functions
that function i directly calls. If function i calls function j multiple times, then T (j) appears
multiple times in this sum. If function i has no call instructions, then C(i) is empty, and so
T (i) = Bi .
Prometheus wants to assess the impact of several code adjustments on the performance of his
program. Tired of programming, he has hired you to implement a tool that accepts a list of
events, where each event is either an update or a query.
• An update sets Bi to a given value V . Notice that while this event may modify the base
execution time of function i, the multiset C(i) of its called functions remains the same.
• A query calculates the current total execution time T (i) of function i, taking into account
all updates that have occurred up to that point in the event list.
As a first approach, Prometheus does not want the result of each query but a single summary
result. Suppose there are q queries, numbered sequentially from 1 to q in the list of events.
Then, the summary result is calculated as:
q
!
X
k · T (ik ) mod (109 + 7) ,
k=1
where T (ik ) is the total execution time of the function specified in the k-th query, taking into
account all updates up to that query.
Input
The first line contains an integer N (1 ≤ N ≤ 8000) indicating the number of functions. Each
function is identified by a distinct integer from 1 to N .
The next line contains N integers B1 , B2 , . . . , BN (0 ≤ Bi ≤ 109 ), representing that the base
execution time of function i is Bi .
The next line contains an integer M (1 ≤ M ≤ 8000) denoting the number of call instructions
among all the functions.
Output
Output a single line with an integer indicating the summary result of all the queries.
3 770
10 20 100
3
1 2
2 3
1 3
3
Q 1
Q 2
Q 3
Explanation of sample 1:
Total execution times for each query are: 230, 120 and 100.
Sample input 2 Sample output 2
2 640
42 10
2
2 1
2 1
5
Q 2
Q 1
U 2 0
Q 1
Q 2
Explanation of sample 2:
Total execution times for each query are: 94, 42, 42 and 84.
Input
The input consists of a single line that contains two integers N (1 ≤ N ≤ 2 · 105 ) and K
(1 ≤ K ≤ min(N !, 4 · 1018 )).
Output
Output a single line with N integers, describing the K-th permutation of the integers from 1
to N , considering permutations sorted according to the university’s criteria.
4 10 1 4 3 2
5 120 5 4 3 2 1
16 12345678901234 2 13 8 10 3 15 16 5 11 12 1 9 7 6 14 4
Eager to show off their flat-world knowledge, the citizens want to calculate how much of the
road is covered by their shadows. However, with shadows potentially overlapping, they need
your help to figure it out. Given the positions of the flatlanders and their heights, can you
calculate the total length of the road covered by their shadows?
Input
The first line contains two integers θ (10 ≤ θ ≤ 80) and N (1 ≤ N ≤ 105 ), indicating
respectively the angle of the light beams and the number of flatlanders on the road. The angle
is measured in degrees, clockwise from the ground to the light beams.
Each of the next N lines contains two integers X (0 ≤ X ≤ 3 · 105 ) and H (1 ≤ H ≤ 1000),
indicating that a flatlander of height H is located at position X along the road. It is guaranteed
that no two flatlanders share the same location.
Output
Output a single line with the total length of the road covered by the shadows of all flatlanders.
The output must have an absolute or relative error of at most 10−4 .
45 3 300
50 150
0 100
100 200
60 3 215.47
50 150
0 100
100 200
30 3 446.41016
50 150
0 100
100 200
Input
The input consists of a single line that contains an integer K and a binary string S (2 ≤ K ≤
|S| ≤ 105 ).
Output
Output a single line with an integer indicating the minimum number of operations needed to
transform S into a kool string, followed by a kool string that can be obtained after applying
that number of operations to S. If there are multiple solutions, output any of them.
2 00 1 01
2 10 0 10
3 1111100 1 1101100
3 00001111 2 01001101
Input
The first line contains two integers N (2 ≤ N ≤ 500) and T (1 ≤ T ≤ 105 ), indicating
respectively the size of the Latin square and the number of transformations.
The next T lines describe the transformations, in the order they are applied, one transformation
per line. Each of these lines contains a character X and two integers I and J (1 ≤ I, J ≤ N
and I 6= J). The character X is either the uppercase letter “R” or the uppercase letter “C”
indicating respectively that rows I and J, or columns I and J are swapped.
Output
If there exists an original matrix that, after applying all transformations, is identical to the
final matrix, output N lines with N integers each, representing any such matrix.
If no such matrix exists, output a line with the character “*” (asterisk) instead.
4 4 1 2 3 4
R 1 2 2 1 4 3
C 2 1 3 4 1 2
R 3 4 4 3 2 1
C 3 4
4 3 *
R 1 2
R 1 2
R 2 1