ACM International Collegiate Programming Contest 2016: Latin American Regional Contests
ACM International Collegiate Programming Contest 2016: Latin American Regional Contests
Contest Session
This problem set contains 11 problems; pages are numbered from 1 to 13.
This problem set is used in simultaneous contests hosted in the following countries:
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 or codename.java, codename.py2,
codename.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 (n, A, e, I, o, U, c, 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 (n, A, e, I, o, U, c, etcetera).
5. Every line, including the last one, must have the usual end-of-line mark.
6. To output real numbers, round them to the closest rational with the required number of digits
after the decimal point. Test case is such that there are no ties when rounding as specified.
Development team
The following persons helped to develop the problem set by creating and
improving statements, solutions, test cases and input and output checkers:
Four friends are playing table tennis. Each of them has a skill level which is represented by an
integer number: the higher the number, the better the player is.
The four friends want to form two teams of two players each. For the game to be more exciting,
they want the skill level of the teams to be as close as possible. The skill level of a team is the sum of
the skill levels of the players in that team.
Although they are very good table tennis players, these friends are not so good at other things, like
Math or Computing. Can you help them find the smallest possible difference between the teams skill
levels?
Input
The input consists of a single line that contains four integers A, B, C and D, representing the skill
levels of the four players (0 A B C D 104 ).
Output
Output a line with an integer representing the smallest difference between the skill levels for both
teams.
Sample input 1 Sample output 1
4 7 10 20 7
Doctor Emmet is working on a safer device to travel in time. He gathered N different and rare
pieces of metal. Each piece may be compatible with some other different pieces. He has a complete list
with M distinct pairs of compatible metals. Any pair of metals that is not on the list is incompatible.
In order for the device to work, he must choose a set of metals such that each of them is compatible
with at least A others in that set. However, in order to preserve some balance, they must also be
incompatible with at least B others in that set.
More metals mean more energy and a safer device. This is why Doctor Emmet needs your help, he
wants to know the size of the largest set he can choose that meets these criteria.
Input
The first line contains four integers N , M , A and B, representing respectively how many different
pieces of metal exist (1 N 105 ), how many compatibilities there are (1 M 105 ) and the variables
A and B described in the problem statement (0 A, B < N ). The different metals are conveniently
numbered from 1 to N . Each of the following M lines contains two integers X and Y corresponding to
a pair of compatible metals (1 X, Y N with X 6= Y ). There are no repeated pairs in the input.
Output
Output a line with one integer representing the size of the largest set of metals satisfying the
requirements specified in the problem statement.
Sample input 1 Sample output 1
3 1 1 0 2
1 2
A set of points in the plane is self-rotating if there is a point P , the center, and an angle , expressed
in degrees, where 0 < < 360, such that the rotation of the plane, with center P and angle , maps
every point in the set to some point also in the set.
You are given a set of N distinct points, all having integer coordinates. Find the number of distinct
subsets of size 1, 2, . . . , N that are self-rotating. Two subsets are considered distinct if one contains a
point that the other does not contain.
Input
The first line of the input contains one integer N representing the number of points in the input set
(1 N 1000). Each of the following N lines describes a different point of the set, and contains two
integers X and Y giving its coordinates in a Cartesian coordinate system (109 X, Y 109 ). All
points in the input set are distinct.
Output
Output a single line containing N integers S1 , S2 , . . . , SN . For i = 1, 2, . . . , N the integer Si must
be the number of subsets of i points of the input set that are self-rotating. Since these numbers can be
very big, output them modulo 109 + 7.
Sample input 1 Sample output 1
3 3 3 0
1 1
2 2
1 0
Alex registered in an online dating system to search for the perfect partner. The system requires
each of its members to fill a form specifying how much they enjoy N different activities, grading them
on a scale from 0 to 100. To present this information to potential dates, the system creates a profile
featuring a special kind of polygon called radial diagram.
A radial diagram for N activities is drawn by marking N points on the plane. Starting from the
vertical direction, the i-th point in clockwise order represents the i-th activity specified by the member,
and is a distance Si away from the center of the diagram, where Si is the score given by the member
for the corresponding activity. The angle sustained at the center of the diagram from each pair of
consecutive points is always the same, and the polygon is formed by drawing the segments whose
endpoints are consecutive points. Note that for the purposes of the radial diagram, the first and last
points are considered to be consecutive.
For example, if N = 6 Alex might specify the following activities: singing with score S1 = 10,
running with score S2 = 60, listening to music with score S3 = 70, traveling with score S4 = 70, eating
out with score S5 = 80, and visiting museums with score S6 = 80. Then the corresponding radial
diagram would be as shown in the figure below.
singing
80
70
60
50
visiting museums 40 running
30
20
10
0
traveling
The area of a radial diagram depends on the order in which the different activities are specified,
and Alex suspects that a profile depicting a radial diagram with greater area might be more successful.
For example, the radial diagram in the following figure features the same activities and scores as the
example above, but has a greater area.
singing
80
70
60
50
traveling 40 running
30
20
10
0
eating out
Alex has asked you to write a program to find the maximum possible area of a radial diagram given
a list of activities graded with scores between 0 and 100.
Input
The first line contains an integer N representing the number of activities (3 N 105 ). The
second line contains N integers S1 , S2 , . . . , SN representing the scores given by Alex to each activity
(0 Si 100 for i = 1, 2, . . . , N ).
ICPC Latin American Regional 2016 5
Output
Output a line with a rational number representing the maximum possible area of a radial diagram
featuring the scores given in the input. The result must be output as a rational number with exactly 3
digits after the decimal point, rounded if necessary.
Sample input 1 Sample output 1
6 10002.593
10 60 70 70 80 80
Scientists at the Interstellar Consortium of Planets and Constellations (ICPC) are studying the
composition of many celestial objects by analyzing their emission spectrum. The emission spectrum of
a celestial object is the spectrum of frequencies of electromagnetic radiation emitted due to its atomic
energy transitions, along with the intensity of the emitted radiation. In other words, it corresponds to
the intensity of each color for the light radiated by the object.
According to the postulates of quantum mechanics, the emission spectrum of a celestial object is
always discrete. Therefore, the ICPC can store the emission spectrum of an object as a sequence of
integers where each position in the sequence corresponds to the intensity of a specific wavelength. In this
representation of the spectrum, larger numbers correspond to higher emitted intensities, and contiguous
positions in the sequence correspond to contiguous wavelengths in the spectrum.
The emission spectrum of a celestial object is the result of very complex physical processes, and
may thus vary through its lifetime. Notably, due to complex atomic reactions not yet fully understood,
the intensity of two contiguous wavelengths can be swapped at a given time.
The ICPC is studying very closely the emission spectrum of some celestial object at particular
wavelengths. However, scientists are having trouble obtaining useful data from their observations.
Particularly, given a range of wavelengths and an integer K, they are interested in knowing the intensity
of the wavelength which has the K-th smallest intensity in that range. Given a list of observational
events mixing information requested by the scientists and wavelength intensity swaps in the spectrum,
your task is to help the scientists by answering their queries.
Input
The first line contains two integers N and Q, representing the number of measured wavelengths and
the number of events, respectively (1 N, Q 105 ). The second line contains N integers I1 , I2 , . . . , IN ,
representing Ii the initial intensity of the i-th wavelength (0 Ii 109 for i = 1, 2, . . . , N ). Each of the
following Q lines corresponds to an event, and starts with a character representing the event type. If
the event corresponds to a query from the ICPC scientists, the character is a Q; if it corresponds to an
atomic swap reaction it is an S. Query events have three integers A, B and K after the Q character,
representing that the scientists are interested in the K-th smallest intensity in the range from A to B,
inclusive (1 A B N and 1 K B A + 1). Intensity swapping events have a single integer
W after the S character, representing that the intensities for wavelengths at positions W and W + 1
in the spectrum are swapped (1 W N 1).
Output
Output one line for each query event in the input, containing a single integer number representing
the intensity of the wavelength with the K-th smallest intensity in the range of the spectrum from A
to B, inclusive (where A, B and K are the parameters specified in the corresponding query). Queries
should be answered in the same order in which they appear in the input.
Sample input 1 Sample output 1
10 4 6
1 2 6 7 5 8 9 3 0 4 5
Q 1 10 7 6
Q 2 5 2
S 5
Q 2 5 2
To discourage birds such as crows and sparrows from feeding on his crops a farmer needed to put
some scarecrows in his corn field. His nephew really likes robots, and suggested that he should use a
robot scarecrow instead: A single robot scarecrow can better protect the whole corn field and will last
way more than ten traditional ones!, he said.
Since the farmer thinks his nephew is a smart boy, he took his advice and bought a robot scarecrow.
The robot moves along a pathway that surrounds the corn field. In the pathway there are N unmanned
charging stations, numbered sequentially in clockwise order starting from 1. The figure below shows an
example with eight charging stations.
2
1 3
8
Corn field
4
7 5
6
The robot begins every day at station number 1, and is issued a sequence of commands that are
to be performed in order during the day. These commands are generated based on advanced machine
learning algorithms that work on data collected by sensors spread through the corn field, ensuring an
optimal coverage of the crop. Each command results in the robot moving to another charging station
next to the one it is currently at, either in clockwise or counter-clockwise direction.
Despite the promises of optimal coverage by the robot, at the end of a certain day the farmer found
part of his crop devastated. To figure out what might have happened the farmer wants to know how
many times the robot was at the charging station closest to the devastated area. Given the number of
the station closest to the devastated area and the sequence of commands for a single day, can you help
the farmer find this number?
Input
The first line contains three integers N , C and S representing respectively the number of posts
(2 N 100), the number of commands (1 C 1000) and the charging station closest to the
devastated area (1 S N ). The second line contains C integers X1 , X2 , . . . , XC , representing the
sequence of commands received by the robot scarecrow. For i = 1, 2, . . . , C, if Xi is 1 then the i-th
command means move to the next charging station in clockwise order, whereas if Xi is 1 then the
i-th command means move to the next charging station in counter-clockwise order. The robot always
starts at station number 1.
Output
Output a line with an integer representing the number of times the robot was at station number S
during the day.
Sample input 1 Sample output 1
8 8 3 2
1 -1 1 1 1 -1 1 1
Adam and Carol are having a great time playing the Game of Matchings. The game is played on
a string S composed of |S| lowercase English letters, s1 s2 . . . s|S| . The goal is to find all matchings of
a special kind of pattern P in S. The pattern has length N and is defined by a sequence of integers
between 1 and 26.
We consider a contiguous substring si si+1 . . . si+N 1 starting at position i of S a matching of pattern
P if there is a mapping from the numbers in P to lowercase English letters such that the pattern is
mapped to si si+1 ...si+N 1 but no two distinct numbers are mapped to the same letter.
For instance, if S is awawww and P is [10, 21, 10], the matchings of P are the substrings of S of
length three starting at positions 1 and 2: awa and waw. Note that www is not an occurence
because pattern numbers 10 and 21 would both map to w.
Adam and Carol lost the answer sheet and are not sure if they are finding all occurrences for some
of the strings in the game. Given S and P can you find the number of matchings for them?
Input
The first line contains a non-empty string S of at most 5 105 characters. Each character of S is
a lowercase English letter from a to z. The second line contains an integer N representing the size
of the pattern (1 N |S|). The third line contains N integers P1 , P2 , . . . , PN denoting the pattern
(1 Pi 26 for i = 1, 2, . . . , N ).
Output
Output a line with one integer representing the number of matchings of P found in S.
Sample input 1 Sample output 1
awawww 2
3
10 21 10
You are planning to spend your holidays touring Europe, staying each night in a different city for
N consecutive nights. You have already chosen the hotel you want to stay in for each city, so you know
the price Pi of the room youll be staying at during the i-th night of your holidays, for i = 1, . . . , N .
You will book your accommodation through a website that has a very convenient rewards program,
which works as follows. After staying for a night in a hotel you booked through this website you are
awarded one point, and at any time you can exchange K of these points in your account for a free night
in any hotel (which will however not give you another point).
For example, consider the case with N = 6 and K = 2 where the prices for the rooms are P1 = 10,
P2 = 3, P3 = 12, P4 = 15, P5 = 12 and P6 = 18. After paying for the first four nights you would have
four points in your account, which you could exchange to stay for free the remaining two nights, paying
a total of P1 + P2 + P3 + P4 = 40 for your accommodation. However, if after the first three nights
you use two of the three points you earned to stay the fourth night for free, then you can pay for the
fifth night and use the final two points to get the sixth one for free. In this case, the total cost of your
accommodation is P1 + P2 + P3 + P5 = 37, so this option is actually more convenient.
You want to make a program to find out what the minimum possible cost for your holidays accom-
modation is. You can safely assume that all hotels you want to stay always will have a room available
for you, and that the order of the cities you are going to visit cannot be altered.
Input
The first line of input contains two integers N and K, representing the total number of nights your
holidays will last, and the number of points you need in order to get a free night (1 N, K 105 ). The
second line contains N integers P1 , P2 , . . . , PN , representing the price of the rooms you will be staying
at during your holidays (1 Pi 104 for i = 1, 2, . . . , N ).
Output
Output a line with one integer representing the minimum cost of your accommodation for all of
your holidays.
Sample input 1 Sample output 1
6 2 37
10 3 12 15 12 18
The government is planning to provide internet to people in remote areas, in this case small towns
that developed on the side of a long and busy highway. There are N towns located side by side along
the highway, each taking up exactly one kilometer of highway. The towns are numbered consecutively
along the highway from 1 to N . To provide an internet connection, the government is going to place
access-point stations with satellite links, which will provide wired connections for the towns.
The stations are to be placed in one or more different towns, being B the cost to build each station.
Since the government wants to provide extremely good service, each house will be connected directly
to one of these stations. When connecting a house in town i, we must choose a station in town j for
connecting that house. The connection cost is then |i j| C, where C is the cost of a kilometer of
cable. Notice that the intra-town cable cost is small enough to be ignored, so in particular houses in a
town where a station is placed do not incur in any cabling cost when connected to that station.
Given N , B, C and the number of houses in each town, write a program to determine the minimum
total cost of providing an internet connection for every house in every town, including the cost of building
the stations and laying the cabling for each house. Because the government hasnt decided yet on the
final number of access-point stations to be built, you should calculate the minimum cost when there are
1, 2, . . . , N stations.
Input
The first line contains three integers N , B and C representing the number of towns, the cost of
building one access-point station and the cost of one kilometer of cable, respectively (1 N 6000,
1 B 109 and 1 C 100). The second line contains N integers H1 , H2 , . . . , HN , where Hi
represents the number of houses in the i-th town (1 Hi 109 for i = 1, 2, . . . , N ).
Output
Output a line with N integers representing the minimum total cost of providing an internet connec-
tion for every house in every town when building 1, 2, . . . , N access-point stations.
Sample input 1 Sample output 1
5 6 1 21 20 22 25 30
1 2 3 4 5
Hello contestant, I want to play a game. Your coach is in the contest room with a bomb about to
explode in his hands. This bomb will be set to detonate in T seconds, and if it detonates in the contest
room it is going to explode only your teams balloons.
I can tell you that the contest room is inside a building that contains N rooms in total. From each
room there is exactly one direct tunnel to another room, which can only be used in one direction. For
example if room A connects to room B, then you can walk from room A to room B, but not from room
B to room A, unless of course room B has a direct tunnel to room A.
The bomb has a special mechanism that detects if your coach stops moving, and if so it immediately
triggers the detonation taking all your teams balloons down. For that reason your coach will constantly
walk between the rooms, taking exactly one second to move through each tunnel. The only way for
your team to save its balloons is for your coach not to be in the contest room when the bomb detonates.
You dont have the building map in hand, all I can tell you is that the tunnels are chosen uniformly
at random. However, I will give you the possibility to set T , which must be an integer between 2 and
N inclusive. Your job is to choose T in such a way that it maximizes your balloons chance to survive
this riddle.
Let the game begin.
Input
The input consists of a single line that contains one integer N , representing how many rooms there
are in the building (2 N 109 ).
Output
Output a line with one integer representing the value of T that maximizes your balloons chance to
survive the riddle.
Sample input 1 Sample output 1
3 3
In the popular board game One Night Werewolf, players are distributed randomly in the roles of
villagers and Werewolves. The goal of the villagers is to decide together on one person to kill during the
night hopefully they will kill a Werewolf. Werewolves pose as villagers in the hope that the person
killed is a villager, not a Werewolf.
In the variation Uncertain Werewolf, only one Werewolf exists and the game consists of two phases.
During the first phase the players are still uncertain about who they should vote to kill, so each of them
chooses two other players as possible victims. After the first phase the Werewolf reveals himself, and
then in the second phase each player has to decide which one of their two initial choices they will vote
to kill. The Werewolf is the last one to decide between his two initial choices, doing so after all the
other players have decided already.
The Werewolf then loses the game if he has more votes than anyone else. If there is a draw, the
Werewolf wins.
You are given the votes of N players after the first phase of the game. You should answer how many
players could reveal themselves at this point as the Werewolf and still win the game if the other players
chose their votes optimally to kill the Werewolf.
Input
The first line contains an integer N (3 N 50), the number of players in the game. Each of the
following N lines contains two integers, ai and bi (1 ai , bi N , ai 6= bi ), the index of the players the
i-th player decided to kill in the first voting phase. No player will try to kill himself.
Output
Output a line with one integer representing the number of players that could win the game if they
were the Werewolf and everyone played optimally.
Sample input 1 Sample output 1
5 4
3 4
1 3
2 4
1 3
2 3