Problems
Problems
Problems
(This set contains 11 problems; problem pages are numbered from 1 to 18)
A: Arctic Virus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
C: SquareDiff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
D: Drug Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
E: Spacebar Tokenizer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
F: Turnswitch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
G: Signal Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
H: Only1s0s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
I: Omens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
J: Lumina . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
K: Skyline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
0
General Information. Unless otherwise stated, the conditions stated below hold for all the problems. How-
ever, since some problems may have specific requirements, it is important to read the problem statements
carefully.
Input.
1. The input must be read from the standard input.
2. In most problems, the input can contain several test cases. Each test case is described using a number of
lines specific to the problem.
3. In most cases, when a line of input contains several values, they are separated by single blanks. No other
spaces appear in the input and there are no empty lines.
4. Every line, including the last one, has the usual end-of-line mark.
5. If no end condition is given, then the end of input is indicated by the end of the input stream. There is no
extra data after the test cases in the input.
Output.
1. The output must be written to the standard output.
2. The result of each test case must appear in the output using a number of lines, which depends on the
problem.
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. Every line, including the last one, must have the usual end-of-line mark.
5. After the output of all test cases, no extra data must be written to the output.
6. To output real numbers, if no particular instructions are given, round them to the closest rational with the
required number of digits after the decimal point. Ties are resolved rounding to the nearest upper value.
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 1
A: Arctic Virus
Source file name: arctic.c, arctic.cpp, arctic.java, or arctic.py
Author: Camilo Rocha
The year is 2045 and climate change has reached an irreversible tipping point. The polar ice caps are melting
at an alarming rate, causing global sea levels to rise and destabilizing ecosystems. Scientists around the world
have been racing to find solutions to slow the thawing of the poles. Then, one day, they discover something
extraordinary hidden deep within the ice --a network of ancient, highly advanced technology. These dormant
machines, left behind by an ancient civilization, have the power to control the Earth’s temperature.
Reactivating this technology requires solving a series of complex computational problems to restore balance to
the planet’s climate. Ultimately, these problems reduce to finding specific instances of an ancient virus that can
unlock the machines. Unfortunately, this virus no longer ‘‘walks’’ the Earth, so advanced in-vivo laboratory
experimentation is required to recreate instances of the arctic virus that can trigger the unlocking process.
The virus consists of a chain of bases --adenine (A), cytosine (C), guanine (G), and thymine (T )-- and evolves in
one of the following forms:
In its simple stage, the arctic virus takes one of two configurations: A and T . Any other form is considered a
mutation, which can occur in four ways:
• The notation φC means that the virus φ mutates by adding C to the end of its chain of bases.
• The notation Aφ means that the virus φ mutates by appending A to the beginning its chain of bases.
• The notation Aφ−1 means that the virus φ mutates by appending A to the beginning of its reversed chain of
bases.
• The notation Gφ−1C means that the virus φ mutates by appending G to the beginning and C to the end of
its reversed chain of bases.
For example, A, AT, GTAC, and ACGTCCGA are configurations of the virus, while TTG, CGGAT, and GAGAT
are not.
Your task is to help the scientists unlock the secrets of this ancient technology by writing code to identify
configurations of the arctic virus. In particular, you are tasked with designing and implementing an algorithm
to detect if a chain of bases created in the laboratory corresponds to the virus, in any of its configurations, or
not. This will allow the scientists to verify the results of in-vivo experimentation as quickly and efficiently as
possible. Failure means the complete melting of the poles and catastrophic consequences for humanity.
Input
The input consists of several test cases. Each testcase comprises exactly one line of the form n s, with
1 ≤ n ≤ 1 000, and s, with |s| = n, a sequence comprising only the characters A, C, G, and T representing the four
bases that can appear in the virus.
1 A simple
2 AT mutation
4 GTAC mutation
8 ACGTCCGA mutation
3 TTG doomed
5 CGGAT doomed
5 GAGAT doomed
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 3
A group of people want to cross a suspension bridge. Due to the weight of the individuals and the condition of
the bridge, no more than two people can cross at the same time. Additionally, it is nighttime and they have only
one lamp to light the way. Therefore, whenever two people cross the bridge, one person must return with the
lamp if there are still people pending to cross.
The challenge is further complicated by the fact that each person takes a different amount of time to cross the
bridge and when two people cross together, they must do so at the pace of the slower individual. What is the
shortest time required for the entire group to cross the bridge?
For example, suppose three people, A, B, and C, want to cross the bridge. Their crossing times are 1, 2, and
5 minutes, respectively. The minimum time required for the entire group to cross the bridge is 8 minutes.
For instance, A and B can cross first (2 minutes), A returns (1 minute), and then A and C cross together (5
minutes).
Input
The input consists of several test cases. For each test case, the first line contains an integer N (1 ≤ N ≤ 30),
representing the number of people in the group. The next N lines each contain an integer t (1 ≤ t ≤ 20), where
the k-th line (1 ≤ k ≤ N) represents the time (in minutes) required for the k-th person to cross the bridge. The
input ends with a line containing the value 0, which should not be processed.
1 10
10 20
2 8
3
20
3
1
2
5
0
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 4
C: SquareDiff
Source file name: sqdf.c, sqdf.cpp, sqdf.java, or sqdf.py
Author: Rodrigo Cardoso
Taking a break from her advanced number theory research, Alana began playing with square differences:
• 12 − 02 = 1
• 62 − 42 = 20
• 102 − 52 = 75
However, she has not found two integers x and y such that x2 − y2 = 2. It might be the case that such a pair does
not exist. This made her wonder if there are other numbers that are equally rare.
Alana wants to determine whether a given integer can be expressed as the difference of two squares of integers.
Can you help her by writing a program to solve this problem?
Input
The input consists of several test cases. Each test case is defined by a single line containing a positive integer N
(0 < N < 40 000), which is the number to test. The input ends with a line containing 0, which should not be
processed.
1 Y
2 N
3 Y
20 Y
0
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 5
D: Drug Test
Source file name: drugtest.c, drugtest.cpp, drugtest.java, or drugtest.py
Author: Rodrigo Cardoso
Active Customizable International Surveys (ACIS) is a company specialized in surveys to determine drug impact
and efficacy. They have designed an experiment to test X, a new drug to be used worldwide. The experiment is
based on the classical technique of using different concentrations of the drug to measure and compare its effects
on test individuals.
Packages of k ≥ 0 pills, called k-packages, are used to define different concentrations of the drug. Each
k-package is either filled with pills that contain the active ingredient of X (called active package) or filled with
placebo pills without X’s active ingredient (called placebo package). A package without pills (i.e., a 0-package)
is called empty and is considered a placebo package.
Experiments consider a maximum number of pills per package N and are designed with two conditions:
c1 Each test individual must receive exactly two packages, an active one and a placebo one; if their corresponding
sizes are a and p, then a + p must be a power of 2.
c2 For each size k, 0 ≤ k ≤ N, at least one individual must receive a k-package.
Your task is to determine if a given package size corresponds to an active or to a placebo package. Indeed, ACIS
researchers have proved that there is only one way to design the experiment fulfilling the given conditions.
Consider a scenario with N = 12, where packages 1, 2, 4, 5, 8, 9, 10 are active and packages 0, 3, 6, 7, 11, 12 are
placebo. The experiment may be accomplished by the following pairs of packages (active, placebo):
(1, 0), (1, 3), (1, 7), (2, 0), (2, 6), (4, 0), (4, 12), (5, 3), (5, 11), (8, 0), (9, 7), (10, 6).
Note that every pair (a, p) in the list is such that a is an active package, p is a placebo package, and a + p is a
power of 2. On the other hand, every number from 0 to 12, inclusive, appears in one pair at least. Hence, if the
queries are for packages of sizes 1, 3, 5, 7, 9, the answer is active for 1, 5, 9 and placebo for 3, 7.
Input
The input consists of several test cases. Each case is defined by two lines. The first line contains two blank-
separated integers N and Q, (0 < N < 10 000 and 0 < Q ≤ 100), the maximum number of pills per package and
the number of queries, respectively. The second line contains Q non-negative integer numbers q1 , q2 , . . . , qQ ,
(0 ≤ qk ≤ N and 0 < k ≤ Q). The input ends with a line containing two blank-separated 0 values.
4 3 PAA
0 4 2 APAPA
12 5
1 3 5 7 9
0 0
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 7
E: Spacebar Tokenizer
Source file name: spacebar.c, spacebar.cpp, spacebar.java, or spacebar.py
Author: Juan Corena
Spacebar Tokenizer is a startup developing a state-of-the-art tokenizer. Its unique design helps the fastest
programmers in the world avoid wasting time hitting the spacebar as they type.
To achieve this goal, the tokenizer uses a scoring system based on a large language model. This model assigns
an integer score to each valid token in the language and a score of 0 to any token that is not recognized.
When tokenizing a sentence, the tokenizer sums the scores of each token according to the model. An optimal
tokenization is one that yields the highest total score out of all possible ways to tokenize the given sentence.
For example: ilovespacebartokenizer could be tokenized in several ways including, but not limited to: ‘‘i love
space bar tokenizer’’, ‘‘i love spacebar tokenizer’’, and ‘‘ilovespace bartokenizer’’. If the language model
assigns a score of 5 to the token spacebar, 2 to the token bar, and 3 to the token tokenizer, then the second
tokenization yields the highest score among the 3 example tokenizations, with a total score of 8. Note that there
may be cases where multiple tokenizations result in the same maximum score.
The table below shows the scores for the sample tokenizations. The tokens contributing to the final score are in
bold:
Tokenization Score
i love space bar tokenizer 5
i love spacebar tokenizer 8
ilovespace bartokenizer 0
Your task is to create a program that outputs the score that a tokenizer should output, when it behaves optimally
as described.
Input
The input consists of several test cases. Each test case consists of a language model providing the score for each
token, and a series of sentences that need to be tokenized optimally according to the language model.
Each test case starts with a line containing two integers 1 ≤ m ≤ 1 000 and 1 ≤ s ≤ 100 separated by a single
space. Each of the next m lines contains, separated by a blank, a single token in undercase English alphabet
and a positive integer number denoting the score that token adds to the overall score of the tokenization. The
length of each token is at least 1 and at most 100 characters, the token can be assumed to be unique. Then s
lines follow, each contains a sequence ai for 1 ≤ i ≤ s, of undercase English characters to be tokenized, where
1 ≤ |ai | ≤ 1 500.
The input ends with two blank-separated 0s.
7 2 24
space 5 13
bar 7
spacebar 13
tokenizer 5
i 2
love 4
ipo 8
ilovespacebartokenizer
theonlytokenizeripo
0 0
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 9
F: Turnswitch
Source file name: turnswitch.c, turnswitch.cpp, turnswitch.java, or turnswitch.py
Author: Julián Badillo
The starship Century Hawk is in the middle of a loosing battle with a squad of space pirates, possibly after
realizing their business with Captain Hanna Lonely was actually, sort of a scam. There is no escape but to jump
to hyperspace. Unfortunately, the Century Hawk has been at the receiving end of heavy fire, and though Captain
Lonely is a skilled pilot, the space pirates managed to hit home before she could shield up.
"It must be the hyperdrive regulator Turnswitch, it’s not responding!" - yells the second in command.
"You, newbie, go to the hyperdrive and fix it up!" - Captain Lonely orders.
As the newbie - and the only crew member of the Century Hawk with any programming skills - your task is to
fix the Turnswitch, so the hyperdrive is back online.
The regulator Turnswitch is an N × N matrix of switches, each switch can be either in horizontal or vertical
position.
You discover that if you turn any switch -from horizontal to vertical or viceversa-, all the directly adjacent
swiches (up, down, left, right) will also turn. If a switch is on an edge of the matrix, or in a corner, the positions
outside the matrix are ignored.
For instance let’s consider the following 3 × 3 Turnswitch:
If you turn the switch at the very center, the Turnswitch will become:
Now, if you turn the switch of the bottom left corner, the Turnswitch will become:
The Century Hawk can jump to hyperdrive if the switches in the Turnswitch are either all horizontal, or all
vertical -it is a ship build under a very liberal set of standards. Your task is to find the least amount of switches
to turn in order to make the Turnswitch ready for the jump.
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 10
3 3
-|| 2
--- 3
|-| 3
3
||-
|-|
-||
4
-|-|
||-|
||-|
||--
4
|||-
|---
-|--
-|-|
0
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 11
G: Signal Coverage
Source file name: signal.c, signal.cpp, signal.java, or signal.py
Author: Rafael García
The Nlogonia government is in negotiations with a telecommunications provider to cover a region of the
country.
The commitment includes installing n antennas. Each antenna has a coverage radius (which defines a circular
area with its border included), a planned location on the map of Nlogonia, and can be active over a given time
range. More formally, the n antennas are numbered from 1 to n. The coverage radius of antenna i is ri , its
planned location is (xi , yi ), it goes into operation at 00:00 on day bi and goes out of operation at 00:00 on day
ei + 1.
For the benefit of both parties (the economy of Nlogonia and the profit for the supplier), two types of restrictions
have been imposed:
• Restriction imposed by Nlogonia: No point should be covered by more than one antenna at the same time.
• Restriction imposed by the supplier (pair restrictions): A list of antenna pairs will be provided. For every
listed pair, at least one of the antennas must be installed.
Your task is to determine a list of antennas to be installed that meet the restrictions outlined in the agreement, or
to conclude that it is impossible to do so.
For instance, consider the 4 antennas illustrated in the figure below. Antenna number 1 has a coverage radius
of 200 and is centered at the point (200, 200). The other antennas each have a coverage radius of 100 and are
located at (300, 200), (500, 200) and (500, 100), respectively. Assume that all four antennas share the same
activation time period. The pair restrictions are (1, 2), (3, 4) and (1, 4). In this case, it is possible to satisfy the
restrictions by activating antennas 2 and 4.
Input
The input consists of several lines. The first line contains the number of test cases.
Each case is described as follows:
The first line contains two integers: n, the number of antennas (2 ≤ n ≤ 10 000), and m, the number of pair
restrictions (1 ≤ m ≤ 10 000). In the next n lines, the i-th line contains five integers: ri , xi , yi , bi and ei
(1 ≤ ri , xi , yi ≤ 5 000 and 1 ≤ bi ≤ ei ≤ 5 000), decribing the i-th antenna. The next m lines describes the pair
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 12
restrictions: the j-th line contains two integers: u j , v j (1 ≤ u j , v j ≤ n and u j , v j ), describing the j-th pair
restriction.
3 01
2 1 0101
1000 500 500 100 200 Impossible
100 400 300 20 101
1 2
4 3
200 200 200 1 10
100 300 200 1 10
100 500 200 1 10
100 500 100 1 10
1 2
3 4
1 4
4 3
100 200 300 1 100
100 300 300 1 50
100 200 200 1 25
100 300 200 10 70
1 2
2 3
3 4
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 13
H: Only1s0s
Source file name: only1s0s.c, only1s0s.cpp, only1s0s.java, or only1s0s.py
Author: Rodrigo Cardoso
Alana is a very curious number theory researcher. Last week, she discovered a --perhaps-- useless property about
positive integers: for every such number N, there exists an integer M, a multiple of N, that consists only of the
digits 1 and 0 in decimal notation. This number M is called an only1s0s number.
For instance:
• If N = 4, then M = 10000 and M
N = 2500.
• If N = 14, then M = 10010 and M
N = 715.
Alana has proven that there are many possible solutions for any N. However, she is interested only in the
smallest possible M for a given N. Moreover, she wants to determine the value D such that M = N · D, where
M is the minimum only1s0s number.
Can you help her?
Input
The input consists of several test cases. Each test case is defined by one line containing a positive integer N
(0 < N < 105 ). The input ends with a line containing 0.
4 25
14 715
13 77
0
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 14
I: Omens
Source file name: omens.c, omens.cpp, omens.java, or omens.py
Author: Julián Badillo
Hanna Lonely, captain of the starship Century Hawk, and her crew, have arrived to the planet C0-3, carrying
precious (and dubiously legal) cargo for the Kingdom of Fomana.
The Fomanians are a very superstitious people. Once a year (that’s what they call their cicle around their star,
CO), they celebrate the Omens Day, which happens to be the next day Captain Lonely and her crew arrived to
C0-3. On this day, the Royal Oracle performs the divination ritual, by throwing a pair of Sacred Stones on the
Rectangle of Fortune.
The Rectangle of Fortune is an L × W rectangle, precisely drawn on the sands of the Sacred Beach. Once the
Royal Oracle has thrown the Sacred Stones, she proceeds to draw the Circle of Destiny, in which the stones are
in exactly oposite sides; the diameter of the circle is the distance between the two stones.
Fomanians believe it’s a good omen, when the resulting Circle of Destiny is completely contained within the
Rectangle of Fortune, clearly meaning their destiny in good fortune. On the other hand, a bad omen is when the
Circle of Destiny is partially outside the Rectangle of Fortune.
This is an example of good omen:
The nature of the Sacred Stones is to always fall within the Rectangle of Fortune, and you can assume they are
equally likely to fall anywhere within the rectangle.
The recently appointed Royal Oracle is quite progressive. She went to the Galactic University, and wants
Captain Lonely to help her calculate the probability a thrown resulting in a good omen, given the dimensions of
the Rectangle of Fortune. As the most junior member of the crew, you are tasked with this calculation.
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 15
Input
The input consists of several test cases. A case is defined with a line with two positive integer values L and W,
1 ≤ L, W ≤ 1 000, representing the dimensions of the Rectangle of Fortune.
L W
The end of the input is signaled with a line
0 0
that should be not processed.
1 1 0.5236
20 40 0.3927
40 30 0.4909
0 0
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 16
J: Lumina
Source file name: lumina.c, lumina.cpp, lumina.java, or lumina.py
Author: David Yepes
In the magical kingdom of Lumina, the streets are lit by luminous gems embedded in the ground. Each gem emits
a radiant aura that can activate nearby gems, creating a chain reaction that illuminates all the gems. However, a
powerful dark spell has extinguished all the gems in the kingdom.
The king of Lumina has tasked you with finding the most efficient way to reignite all the gems. Your goal is to
determine the minimum number of gems that need to be manually activated so that, through the chain reaction,
every gem becomes illuminated.
You are provided with a map of the kingdom, where each gem is represented as a point on the Cartesian plane.
Each gem has a radius that indicates the distance its aura can reach to activate other nearby gems. Two gems are
considered ‘‘nearby’’ if one gem lies within the radius of illumination of the other.
For example, suppose you have 3 gems with the following information:
• Gem 1: Coordinates (X: -1, Y: 0) with radius 4.
• Gem 2: Coordinates (X: 2, Y: 1) with radius 2.
• Gem 3: Coordinates (X: 4, Y: -2) with radius 1.
In this case:
• Gem 1 has a large aura with a radius of 4, covering a significant area.
• Gem 2’s aura, with a radius of 2, can also activate nearby gems but is smaller than Gem 1’s aura.
• Gem 3 has the smallest aura, with a radius of 1, affecting only a small area.
If you manually activate Gem 1, its large aura is enough to cover Gem 2, but Gem 3 is not covered by the aura.
Therefore, in this case, it is necessary to manually activate two gems.
Input
The input consists of multiple test cases. Each test case starts with an integer N (1 ≤ N ≤ 5 000) representing
the number of gems in that test case. Then, N lines follow, each containing three blank-separated integers X,
Y, and R, satisfying −109 ≤ X, Y ≤ 109 and 0 ≤ R ≤ 109 , where X is the x-coordinate of the gem, Y is the
y-coordinate of the gem, and R is the radius of the gem’s aura. The input terminates with a line containing a
single 0, which should not be processed.
4 1
0 0 5 2
1 1 1 2
-1 -1 1
2 2 1
4
0 0 2
1 1 1
-1 -1 1
2 2 1
3
-1 0 4
2 1 2
4 -2 1
0
2024 ACIS REDIS - XXXVIII Colombian Programming Contest - ICPC 18
K: Skyline
Source file name: skyline.c, skyline.cpp, skyline.java, or skyline.py
Author: Rodrigo Cardoso
Lucy Diamond is a young architect pursuing a Master’s degree in city development. As part of her research,
she is studying how different city skylines compare. To advance with her analysis, Lucy has devised a method
to estimate how the heights of buildings in a city are organized. Now she needs your assistance in writing a
computer program to perform the necessary calculations.
A skyline of N buildings can be represented by a sequence of N building heights, denoted as h1 , h2 , . . . , hN ,
where N ≥ 1. For any two buildings at positions i and j in the skyline, with 1 ≤ i < j ≤ N, Lucy defines a height
disorder whenever hi > h j . Let HD represent the total number of height disorders in the entire skyline and let
PHD represent the potential number of height disorders for a skyline of that size. Lucy’s measure for skyline
HD
organization is calculated as the ratio PHD when N ≥ 2. In the case where N = 1, this measure is defined as 0,
since no comparisons between building heights can be made.
As an example, consider a skyline of 4 buildings with heights 28, 30, −29, 28 (note that a negative height indicates
a depression in the landscape). In this case, there are 3 height disorders, corresponding to the pairs (1, 3), (2, 3),
and (2, 4). Since the potential number of height disorders for a skyline of 4 buildings is 6, Lucy’s measure for
this skyline is 36 .
Input
The input consists of several test cases. A case begins with a line containing a single integer positive number N,
0 < N < 100 000, the number of buildings in the skyline. Then, a line follows with N blank-separated integer
numbers, representing the heights of the skyline buildings. Each height h is in the range −20 < h < 300. The
end of input is signaled with a line containing 0.
Output
HD
For each test case, print one answer line with the decimal number corresponding to the value PHD rounded to 3
decimal places.
1 0.000
17 0.500
4
28 30 -29 28
0