0% found this document useful (0 votes)
87 views

CCPL2015-R6 ProblemSet

Here are the steps to solve this problem: 1. Initialize the graph with a vertex for each switch and the end vertex. The start switch has indegree 0. 2. Set the initial state of each switch (L/R). 3. Simulate dropping a ball starting from the start switch: - Get the current switch vertex - Check its state and follow the edge left or right accordingly - Flip the state of the current switch - Move to the next switch vertex along the path until reaching the end 4. Repeat step 3 for each ball dropped in the given sequence. 5. Output the final state of each switch after all balls have been dropped. Let me know if
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

CCPL2015-R6 ProblemSet

Here are the steps to solve this problem: 1. Initialize the graph with a vertex for each switch and the end vertex. The start switch has indegree 0. 2. Set the initial state of each switch (L/R). 3. Simulate dropping a ball starting from the start switch: - Get the current switch vertex - Check its state and follow the edge left or right accordingly - Flip the state of the current switch - Move to the next switch vertex along the path until reaching the end 4. Repeat step 3 for each ball dropped in the given sequence. 5. Output the final state of each switch after all balls have been dropped. Let me know if
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Colombian Collegiate Programming League

CCPL 2015

Round 6 – May 30

Problems
This set contains 12 problems; pages 1 to 14.
Problem setter: Edwin Niño.
(Borrowed from several sources online.)

A - Parades . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

B - Bricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

C - Cent Savings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

D - Digi Comp II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

E - Virus Synthesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

F - Mountainous Landscape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

G - Outwitting the Weighing Machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

H - Vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

I - Binary Search Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

J - Sums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

K - Knapsack Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

L - A Minimum Land Price . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

Official site http://programmingleague.org


Follow us on Twitter @CCPL2003

0
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

A - Parades
Source file name: parades.c, parades.cpp, or parades.java

In The City of Eternal Festivities, there are n street junctions and n − 1 bidirectional streets, each street connecting
two of the junctions. Between every two junctions there is exactly one (direct or indirect) path connecting them.
No junction is an endpoint for more than 10 streets.
Every 13th of September (the 256th day of the year), there are many festivities going on in The City. In particular,
the citizens want to organize m parades. The parade number i starts at junction ui and ends at vi , following the
unique path between the endpoints. As the mayor of The City, you are responsible for citizens’ safety. Therefore,
you decreed that no two parades are ever allowed to use the same street, though they can have common junctions,
or even common endpoints.
Without breaking the safety regulations, try to organize as many parades as possible.

Input

The first line of input contains the number of test cases T . The first line of each test case contains a single integer:
the number of junctions n (2 ≤ n ≤ 1000). Each of the next n − 1 lines contains two integers a, b (1 ≤ a , b ≤ n),
denoting that junctions a and b are connected by a street. Each junction has at most n 10 streets leaving it. The
next line contains a single integer: the number of planned parades m (0 ≤ m ≤ 2 ). Each of the next m lines
contains two integers ui , vi (1 ≤ ui , vi ≤ n), meaning that a parade is planned to start at junction ui and finish at
junction vi . No two parades share both endpoints.

The input must be read from standard input.

Output

For each test case, output one line containing the largest number of parades that can be organized with no street
used by more than one parade.

The output must be written to standard output.

Sample Input Sample Output

1 2
6
1 2
2 3
3 4
3 5
3 6
4
1 3
4 5
5 6
6 4

1
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

B - Bricks
Source file name: bricks.c, bricks.cpp, or bricks.java

You are given a sequence of white (W) and black (B) bricks. The goal is to partition it into some number of
non-empty, contiguous blocks, each one having the same ratio of white and black bricks.
Of course one can always “partition” the sequence into one single block (which is not very interesting). We
want, however, to have as many blocks as possible. Consider for example the following sequences and its
partitions:
• BWWW BB = BW + WW BB (ratio 1 : 1),
• WWW BBBWWWWWWWWW B = WWW B + BBWWWWWW + WWW B (ratio 3 : 1).
Note that both of these partitions are optimal with respect to the number of blocks.

Input

The first line of input contains the number of test cases T . Each test case starts with one line containing an integer
n (1 ≤ n ≤ 105 ) which is the length of the description of a sequence. Each of the following n lines consists of an
integer (1 ≤ k ≤ 109 ) and one of the characters W or B, meaning that k bricks of the given color follow next in
the sequence. It is guaranteed that the total length of the brick sequence does not exceed 109 .

The input must be read from standard input.

Output

For each test case, output a single line containing the largest possible number of blocks.

The output must be written to standard output.

Sample Input Sample Output

3 2
3 3
1 B 5
3 W
2 B
4
3 W
3 B
9 W
1 B
2
2 W
3 W

2
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

C - Cent Savings
Source file name: cent.c, cent.cpp, or cent.java

To host a regional contest like NWERC a lot of preparation is necessary: organizing rooms and computers,
making a good problem set, inviting contestants, designing T-shirts, booking hotel rooms and so on. I am
responsible for going shopping in the supermarket.
When I get to the cash register, I put all my n items on the conveyor belt and wait until all the other customers in
the queue in front of me are served. While waiting, I realize that this supermarket recently started to round the
total price of a purchase to the nearest multiple of 10 cents (with 5 cents being rounded upwards). For example,
94 cents are rounded to 90 cents, while 95 are rounded to 100.
It is possible to divide my purchase into groups and to pay for the parts separately. I managed to find d dividers
to divide my purchase in up to d + 1 groups. I wonder where to place the dividers to minimize the total cost of
my purchase. As I am running out of time, I do not want to rearrange items on the belt.

Input

The input consists of several test cases. Each test case consists of:
• one line with two integers n (1 ≤ n ≤ 2000) and d (1 ≤ d ≤ 20), the number of items and the number of
available dividers;
• one line with n integers p1 , . . . , pn (1 ≤ pi ≤ 10000 for 1 ≤ i ≤ n), the prices of the items in cents. The
prices are given in the same order as the items appear on the belt.

The input must be read from standard input.

Output

For each test case output the minimum amount of money needed to buy all the items, using up to d dividers.

The output must be written to standard output.

Sample Input Sample Output

5 1 190
13 21 55 60 42 0
5 2
1 1 1 1 1

3
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

D - Digi Comp II
Source file name: digi.c, digi.cpp, or digi.java

The Digi Comp II is a machine where balls enter from the top and find their way to the bottom via a certain
circuit defined by switches. Whenever a ball falls on a switch it either goes to the left or to the right depending
on the state of the switch and flips this state in the process. Abstractly it can be modelled by a directed graph
with a vertex of outdegree 2 for each switch and in addition a designated end vertex of outdegree 0. One of the
switch vertices is the start vertex, it has indegree 0. Each switch vertex has an internal state (L/R). A ball starts at
the start vertex and follows a path down to the end vertex, where at each switch vertex it will pick the left or
right outgoing edge based on the internal state of the switch vertex. The internal state of a vertex is flipped after
a ball passes through. A ball always goes down and therefore cannot get into a loop.
One can “program” this machine by specifying the graph structure, the initial states of each switch vertex and the
number of balls that enter. The result of the computation is the state of the switches at the end of the computation.
Interestingly one can program quite sophisticated algorithms such as addition, multiplication, division, and even
the stable marriage problem. However, it is not Turing complete.

Input

The input consists of several test cases. Each test case consists of:
• one line with two integers n (0 ≤ n ≤ 1018 ) and m (1 ≤ m ≤ 500000), the number of balls and the number
of switches of the graph;
• m lines describing switches 1 to m in order. Each line consists of a single character c (‘L’ or ‘R’) and two
integers L and R (0 ≤ L, R ≤ m), describing the initial state (c) of the switch and the destination vertex of
the left (L) and right (R) outgoing edges. L and R can be equal.
Vertex number 0 is the end vertex and vertex 1 is the start vertex. There are no loops in the graph, i.e., after going
through a switch a ball can never return to it.

The input must be read from standard input.

Output

For each test case, output one line with a string of length m consisting of the characters ‘L’ and ‘R’, describing
the final state of the switches (1 to m in order).

The output must be written to standard output.

Sample Input Sample Output

5 3 RLL
L 2 3
R 0 3
L 0 0

4
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

E - Virus Synthesis
Source file name: virus.c, virus.cpp, or virus.java

Viruses are usually bad for your health. How about fighting them with ... other viruses? In this problem, you
need to find out how to synthesize such good viruses. We have prepared for you a set of strings of the letters A,
G, T , and C. They correspond to the DNA nucleotide sequences of viruses that we want to synthesize, using the
following operations:
• Adding a nucleotide either to the beginning or the end of the existing sequence.
• Replicating the sequence, reversing the copied piece, and gluing it either to the beginning or to the end of
the original (so that e.g., AGTC can become AGTCCTGA or CTGAAGTC).
We’re concerned about efficiency, since we have very many such sequences, some of them very long. Find a way
to synthesize them in a minimum number of operations.

Input

The first line of input contains the number of test cases T . Each test case consists of a single line containing
a non-empty string. The string uses only the capital letters A, C, G, and T ; it is not longer than 100000
characters.

The input must be read from standard input.

Output

For each test case, output a single line containing the minimum total number of operations necessary to construct
the given sequence.

The output must be written to standard output.

Sample Input Sample Output

4 3
AAAA 8
AGCTTGCA 6
AAGGGGAAGGGGAA 18
AAACAGTCCTGACAAAAAAAAAAAAC

5
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

F - Mountainous Landscape
Source file name: mountain.c, mountain.cpp, or mountain.java

You travel through a scenic landscape consisting mostly of mountains —there are n landmarks (peaks and valleys)
on your path. You pause for breath and wonder: which mountain are you currently seeing on the horizon?

Formally: you are given a polygonal chain P1 P2 · · · Pn in the plane. The x coordinates of the points are in strictly
increasing order. For each segment Pi Pi+1 of this chain, find the smallest index j > i, for which any point of
~ i+1 ).
P j P j+1 is visible from Pi Pi+1 (lies strictly above the ray Pi P

Input

The first line of input contains the number of test cases T . The first line of each test case contains an integer
n (2 ≤ n ≤ 100000) —the number of vertices on the chain. Each of the following n lines contains integer
coordinates xi , yi of the vertex Pi (0 ≤ x1 < x2 < · · · < xn ≤ 109 ; 0 ≤ yi ≤ 109 ).

The input must be read from standard input.

Output

For each test case, output a single line containing n − 1 space-separated integers. These should be the smallest
indices of chain segments visible to the right, or 0 when no such segment exists

The output must be written to standard output.

6
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

Sample Input Sample Output

2 0 3 6 5 6 0 0
8 6 4 4 0 6 0
0 0
3 7
6 2
9 4
11 2
13 3
17 13
20 7
7
0 2
1 2
3 1
4 0
5 2
6 1
7 3

7
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

G - Outwitting the Weighing Machine


Source file name: machine.c, machine.cpp, or machine.java

Some school children discovered that by getting on a weighing machine in couples, and then exchanging places
—one at a time— they could get the correct weight of a whole party on the payment of but one cent. They found
that in couples they weighed (in pounds): 129, 125, 124, 123, 122, 121, 120, 118, 116, and 114. What was the
weight of each one of the five little girls if taken separately?
It proves that they must have been clever scholars or they never would have been able to work out the correct
answer to such an interesting puzzle question, which is liable to confuse older heads than theirs.
Given a list of 10 integers, representing the weighs of each couple formed from a group of 5 people, determine
the weights of each person.

Input

Input starts with a positive integer T (T ≤ 3000), that denotes the number of test cases. Each test case is
described by 10 integers W1 , W2 , . . . , W10 in a single line, with 100 ≤ W1 ≤ W2 ≤ · · · ≤ W10 ≤ 400.

The input must be read from standard input.

Output

For each test case, print the case number, followed by the 5 weights asked, separated by spaces. Print these
numbers in ascending order.

The output must be written to standard output.

Sample Input Sample Output

3 Case 1: 56 58 60 64 65
114 116 118 120 121 122 123 124 125 129 Case 2: 53 57 58 61 65
110 111 114 115 118 118 119 122 123 126 Case 3: 90 90 100 106 126
180 190 190 196 196 206 216 216 226 232

8
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

H - Vocabulary
Source file name: vocabulary.c, vocabulary.cpp, or vocabulary.java

According to a popular belief, computer programmers drink a lot of coffee and know only a few words. The
vocabulary of a typical programmer consists of just three words. Besides, he rarely knows how to spell them. To
help programmers with their spelling mistakes, we published a book titled The Dictionary of the Three Words
Every Typical Programmer Should Know.
You got a copy of the book but, soon after that, you spilled your coffee over it. Now, you cannot read some of the
characters. Fortunately, the three words were, as usually in dictionaries, distinct and printed in lexicographical
order.
Before you attempt to use that fact to recover the missing characters, you want to know in how many different
ways you can do it. Since you expect this number might be large, you want to know it modulo 109 + 9.

Input

The first line of input contains the number of test cases T . Each test case consists of three lines, each containing
a single nonempty word —in the order they appear in the dictionary. Words consist of small letters of the English
alphabet and question marks, the latter denoting missing characters. Each word is at most 1000000 characters
long.

The input must be read from standard input.

Output

For each test case, output one line containing the number of different ways you can substitute each question
mark with one of the 26 letters from a to z in such a way that the three words are distinct and in lexicographical
order. The number should be printed modulo 109 + 9.

The output must be written to standard output.

Sample Input Sample Output

3 42562
?heoret?cal 52
c?mputer 1
?cience
jagiellonian
?niversity
kra?ow
?
b
c

9
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

I - Binary Search Tree


Source file name: binary.c, binary.cpp, or binary.java

In Tobyland, dogs communicate with each other through barking. These barks are like symbols in a complex
language, and naturally different symbols have different probabilities of appearing. Toby enumerates the symbols
(barks) from 1 to N in the dog alphabetic order, so symbol 1 is lexicographically smaller than 2 and so on. Toby
also counts in many dog conversations the frequency of each symbol to estimate its probabilities.
Now Toby wants to put these symbols (barks) in a fast data structure (Toby is the only dog that studies computer
science), and he chose a binary search tree (BST). As in any BST, all nodes that are to the left of some node with
symbol X must be smaller lexicographically than X and nodes to the right must have symbols bigger than X.
Toby is incredibly concerned with efficiency; he defines the cost of searching a symbol as the number of nodes
that have to be visited from the root to the node with the desired symbol —if the searched symbol is at the root
the cost is 1. Now Toby wants to find the BST that minimizes the expected cost of searching a symbol.

Input

The input consist of several test cases. Each test case begins with a line containing the number N (1 ≤ N ≤ 100),
which is the number of symbols in the dog alphabet, the second line contains N numbers pi stating the probability
of receiving the symbol i. It is guaranteed that the sum of the probabilities is 1.

The input must be read from standard input.

Output

For each test case write the minimum expected cost of searching a symbol in the BST rounded up to 5 decimal
places.

The output must be written to standard output.

Sample Input Sample Output

3 1.66000
0.33 0.34 0.33 1.25000
3 1.77000
0.8 0.15 0.05
4
0.23 0.4 0.17 0.2

10
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

J - Sums
Source file name: sums.c, sums.cpp, or sums.java

Given an integer N, express it as the sum of at least two consecutive positive integers. For example:
• 10 = 1 + 2 + 3 + 4
• 24 = 7 + 8 + 9
If there are multiple solutions, output the one with the smallest possible number of summands.

Input

The first line of input contains the number of test cases T . Each test case consists of one line containing an
integer N (1 ≤ N ≤ 109 ).

The input must be read from standard input.

Output

For each test case, output a single line containing the equation in the format:
N = a + (a+1) + ... + b
as in the example. If there is no solution, output a single word IMPOSSIBLE instead.

The output must be written to standard output.

Sample Input Sample Output

3 IMPOSSIBLE
8 10 = 1 + 2 + 3 + 4
10 24 = 7 + 8 +9
24

11
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

K - Knapsack Connection
Source file name: knapsack.c, knapsack.cpp, or knapsack.java

Gerald’s job is to welcome the teams for this year’s NWERC at the airport in Linkoping. One of his duties is to
stand at the luggage carousel and collect all the knapsacks that the teams are bringing. Gerald is a lazy person,
so he just stands at the same position of the carousel and waits for bags to pass by so he can pick them up.
The baggage carousel consists of s luggage slots, numbered in ascending order from 0 to s − 1. Since the
baggage carousel is cyclic, luggage slots s − 1 and 0 also lie side by side. The carousel turns in such a way that if
Gerald stands in front of slot i at some point in time, he will stand in front of slot (i + 1) mod s one time unit
later.
In the beginning Gerald prepares a huge baggage cart at some position and stands there to wait for luggage.
When a knapsack arrives in front of Gerald, he needs t time units to take it and put it on the baggage cart. After
these t time units he is ready to pick up another knapsack. As long as there are remaining knapsacks on the
luggage carousel, Gerald always takes the next one to arrive at his position as soon as he is ready after putting
away the previous one.
Now Gerald wonders about the effect of his choice of position on the time it will take him to finish this task.
It is up to you to help Gerald calculate the minimum, maximum, and average time to pick up all knapsacks,
taken over all s possible slots, which can appear in front of Gerald after preparation. Time starts when he has
prepared the baggage cart at some slot of the baggage carousel and ends after he has put the last knapsack on the
cart.

Input

The input of several test cases. Each test case consists of:
• one line with three integers n (1 ≤ n ≤ 2000), s (1 ≤ s ≤ 107 ), and t (1 ≤ t ≤ 107 ), where n is the number
of knapsacks to pick up, s is the number of slots of the carousel, and t is the number of time units Gerald
needs to pick up a knapsack from the carousel and put it on the cart;
• one line with n integers k1 , · · · , kn (0 ≤ ki ≤ s − 1 for 1 ≤ i ≤ n), the slots of the knapsacks. There may
be several knapsacks stacked on top of each other in the same slot, but Gerald can still only pick up one
knapsack at a time.

The input must be read from standard input.

Output

For each test case, output three lines of output containing the minimum, maximum, and average time to pick
up all the luggage, over all s positions. The average time should be output as a reduced fraction in the form
p/q.

The output must be written to standard output.

12
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

Sample Input Sample Output

7 10 10000000 70000001
0 0 0 0 0 0 1 70000009
10 10 3 350000027/5
0 0 2 2 4 4 6 6 8 8 39
9 10000000 1 40
0 7 2 3 4 5 6 1 8 79/2
9
10000000
12500021249991/2500000

13
Colombian Collegiate Programming League - CCPL 2015 @CCPL2003

L - A Minimum Land Price


Source file name: land.c, land.cpp, or land.java

Manager of ACM-ICPC Thailand Contest Council is planning to buy lands in Phuket to build the office building
for national programming skill camp and programming contest that will be held on Phuket regularly in the future.
The land price in Phuket is becoming more expensive every year. The price increases exponentially by a yearly
factor. If the land i with initial cost Li is bought in year t, then its price will be

2 × (Li )t .

All land prices are different. ACM-ICPC can buy only one land per year. You have to help the manager to buy
the lands at lowest price within the budget of 5000000 millions baht.

Input

The first line of the input contains an integer T (1 ≤ T ), indicating the number of test cases. Each test case
contains an integer Li which is the cost of land i in million baht. There are less than 40 lands in each test case. A
line containing ‘0’ (zero) indicates the end of each test case.

The input must be read from standard input.

Output

For each test case, print out the minimum price for purchasing all lands. If the total price exceeds the budget of
5000000 baht, then output ‘Too expensive’.

The output must be written to standard output.

Sample Input Sample Output

3 134
7 17744
2 Too expensive
10
0
20
29
31
0
42
41
40
37
20
0

14

You might also like