Problem B: Binary Search Tree: Input
Problem B: Binary Search Tree: Input
Input
The input consist of several test cases. Each test case begins with a line containing the number N, 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.
PN
1 6 N 6 100 ; 0 6 pi 6 1 ; i=1 pi = 1
Output
For each test case write the minimum expected cost of searching a symbol in the BST. Print one line per
test case. Answers with a relative or absolute error less than 10-4 are considered correct.
@RedProgramacion 1 http://redprogramacioncompetitiva.org/
ICPC Latin American Regional – 2012 3
Problem B
Boxes and Stones
Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. Before
beginning the game they arbitrarily distribute the S stones among the boxes from 1 to B 1, leaving
box B empty. The game then proceeds by rounds. At each round, first Paul chooses a subset P of
the stones that are in the boxes; he may choose as many stones as he wants from as many boxes as he
wants, or he may choose no stones at all, in which case P is empty. Then, Carole decides what to do
next: she can either promote the subset P and discard the remaining stones (that is, those stones not
chosen by Paul in the first step); or she may discard the subset P and promote the remaining stones.
To promote a given subset means to take each stone in this subset and move it to the box with the
next number in sequence, so that if there was a stone in this subset inside box b, it is moved to box
b + 1. To discard a given subset means to remove every stone in this subset from its corresponding
box, so that those stones are not used in the game for the remaining rounds. The figure below shows
an example of the first two rounds of a game.
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
... ... ... ...
Paul chooses a set Carole decides to discard Paul chooses a set Carole decides to promote
and promote the others and discard the others
Round 1 Round 2
Paul and Carole play until at least one stone reaches box number B, in which case Paul wins the
game, or until there are no more stones left in the boxes, in which case Carole wins the game. Paul
is a very rational player, but Carole is a worthy rival because she is not only extremely good at this
game, but also quite lucky. We would like to know who is the best player, but before that we must first
understand how the outcome of a game depends on the initial distribution of the stones. In particular,
we would like to know in how many ways the S stones can initially be distributed among the first
B 1 boxes so that Carole can be certain that she can win the game if she plays optimally, even if
Paul never makes a mistake.
Input
Each test case is described using one line. The line contains two integers S (1 S 200) and B
(2 B 100), representing respectively the number of stones and the number of boxes in the game.
Output
For each test case output a line with an integer representing the number of ways in which the S stones
may be distributed among the first B 1 boxes so that Carole is certain that she can win the game.
Because this number can be very large, you are required to output the remainder of dividing it by
109 + 7.
Sample input Output for the sample input
2 3 2
8 4 0
42 42 498467348
Competitive Programming Network - 7th Activity August 23rd 2014
Problem C: Sumthing
Has it ever happened to you that, having worked on a problem for a long time, it starts to pop up in
your conscious mind when you least expect it? Just the other day I was singing that old song that goes
“Something in the way she moves. . . ”, but before I knew it, I replaced part of the lyrics with “Sum-thing in the
way she woos me. . . ”. The only explanation I have for this is that I had been working recently on a curious
mathematical problem concerning sums. It goes something like this:
Consider a list A with n positive integers, A1 , A2 , A3 , . . . , An . A function S is defined as follows, for
1 6 k 6 n:
X
n X
n X
n X
n
S(k) = 2k−1 ··· A i1 A i2 A i3 · · · A ik
i1 =1 i2 =i1 +1 i3 =i2 +1 ik =ik−1 +1
S(1) = 1 + 2 + 3 = 6
S(2) = 2 · ((1 · 2) + (1 · 3) + (2 · 3)) = 2(2 + 3 + 6) = 22
S(3) = 4 · (1 · 2 · 3) = 4 · 6 = 24
What the problem asks is, given the list A, find the sum:
X
n
Φ= S(k)
k=1
Input
Input starts with an integer T , the number of test cases. Each test case starts with an integer n in the first
line. The second line of each case contains n positive integers, separated by spaces, that form the set A.
T 6 10 ; 1 6 n 6 105 ; 1 6 Ai 6 109 for 1 6 i 6 n
Output
For each test case, print the value of Φ, modulo 1000000009 (109 + 9) on a single line.
@RedProgramacion 1 http://redprogramacioncompetitiva.org/
ACM ICPC2005 – South American Regional 6
Problem D
ICPC Strikes Again
Source file name: icpc.c, icpc.cpp, icpc.java or icpc.pas
• a single line containing five stars ***** indicating the beginning of the case
• for each employee i, one line with two integers i and s, separated by a blank, meaning
that i has a salary of s.
3 2 *****
100 2 2 1 200
2 3 1 2 2 200
40 0 1 *****
1 1 70
60 0 1 2 60
2
7 2
10 2 1
2 3 1
10 2 1
4 5 2
10 2 1
6 7 2
10 0 1
1
10 0 1
1
10 0 1
1
10 0 1
1
0 0
ICPC Latin American Regional – 2013
Problem E
Eleven
In this problem, we refer to the digits of a positive integer as the sequence of digits required to write
it in base 10 without leading zeros. For instance, the digits of N = 2090 are of course 2, 0, 9 and 0.
Let N be a positive integer. We call a positive integer M an eleven-multiple-anagram of N if and
only if (1) the digits of M are a permutation of the digits of N , and (2) M is a multiple of 11. You
are required to write a program that given N , calculates the number of its eleven-multiple-anagrams.
As an example, consider again N = 2090. The values that meet the first condition above are 2009,
2090, 2900, 9002, 9020 and 9200. Among those, only 2090 and 9020 satisfy the second condition, so
the answer for N = 2090 is 2.
Input
A single line that contains an integer N (1 N 10100 ).
Output
Output a line with an integer representing the number of eleven-multiple-anagrams of N . Because
this number can be very large, you are required to output the remainder of dividing it by 109 + 7.
Sample input 1 Sample output 1
2090 2
16510 12
201400000000000000000000000000 0
ICPC Latin American Regional – 2013
Problem G
Go up the Ultras
The topographic prominence of a peak is a measure of special interest to mountain climbers and can
be defined as follows: the prominence of a peak p with altitude h, relative to the sea level, is the
greatest d such that any path on the terrain from p to any strictly higher peak will pass through a
point of altitude h d. If there is no strictly higher peak, then the prominence is h itself. Those
peaks with topographic prominence greater than or equal to 150000 centimeters (precision is of great
importance to climbers!) have a special name: they are called “Ultras”.
You have to write a program that identifies all the Ultras that occur in a two dimensional profile
of a mountain range represented as a sequence of points. Note that the horizontal distance between
points is not important; all that you need is the altitude of each point. In the picture below, the Ultras
are the points 7, 12, 14, 20 and 23.
23
500000
450000
400000
12 14
350000 7 17
300000 11 20 24
250000
4 13 25
200000 16
6 15
150000
3 5 10 22
100000
8 19 21
50000
2 9 18
1 26
Input
The first line contains an integer N (3 N 105 ) representing the number of points in the profile.
The second line contains N integers Hi indicating the altitudes (in centimeters) of the points, in the
order in which they appear in the profile (0 Hi 106 for i = 1, 2, . . . , N ). Consecutive points have
di↵erent altitudes (Hi 6= Hi+1 for i = 1, 2, . . . , N 1), while the first and the last points are at sea
level (H1 = HN = 0). You may assume that the profile contains at least one Ultra.
Output
Output a line with the indices of all the Ultras in the mountain range, in the order in which they
appear in the profile.
Sample input 1 Sample output 1
5 4
0 10000 100000 884813 0
7 4 6
0 100000 0 200000 180000 200000 0
ICPC Latin American Regional – 2013
Problem J
Join two kingdoms
The kingdoms of Nlogonia and Quadradonia fought a long and terrible war that historians have come
to call Almost Completely Meaningless (ACM) because nobody can now remember why it started.
When the ACM war finally ended, the two kingdoms decided to strengthen their bonds in order
to avoid more bloodshed, and for this reason they consulted the International Consortium for the
Prevention of Conflicts (ICPC). The ICPC recommended building a single road to connect a city in
Nlogonia with a city in Quadradonia, thus allowing commercial and cultural exchange between the
two.
Nlogonia and Quadradonia have N and Q cities respectively. The road system of each kingdom
consists of a set of bidirectional roads that join pairs of di↵erent cities in the same kingdom, such that
there is a unique path (i.e. sequence of consecutive roads) that one can take to go from any city in a
kingdom to any other city in the same kingdom. The “size” of such a road system is defined as the
maximum number of roads that one must take in order to travel between any pair of cities.
Because the ICPC did not specify which two cities should be connected by the new road joining
the two kingdoms, the citizens are now worried that the size of the combined road system might be
too large. In order to prevent a second ACM war, you would like to convince them that this is not the
case, and to this end you need to calculate the expected size of the resulting road system assuming
that all possible roads between the two kingdoms are equally likely to be built.
Input
The first line contains two integers N and Q representing the number of cities in each of the two
kingdoms (1 N, Q 4 ⇥ 104 ). Cities in Nlogonia are identified with di↵erent integers from 1 to N ,
while cities in Quadradonia are identified with di↵erent integers from 1 to Q. Each of the next N 1
lines describes a road in Nlogonia with two distinct integers A and B indicating that the road connects
city A with city B (1 A, B N ). Each of the next Q 1 lines describes a road in Quadradonia with
two distinct integers C and D indicating that the road connects city C with city D (1 C, D Q).
The road system of each kingdom is such that there is exactly one path between each pair of cities in
the kingdom.
Output
Output a line with a rational number representing the expected size of the road system after the
two kingdoms have been joined, considering that all possible roads connecting them are equally likely
to be built. The result must be output as a rational number with exactly three digits after the decimal
point, rounded if necessary.
Sample input 1 Sample output 1
4 5 5.350
1 2
2 3
4 2
2 3
3 4
4 1
4 5
ICPC Latin American Regional – 2013
1 5 4.400
1 2
2 3
3 4
4 5
ACM ICPC2010 – Latin American Regional 15
Problem K
Kids’ Wishes
Problem code name: kids
Kevin is a kid. He has lunch at school together with many more kids. They use to go outdoors
and have lunch sitting on the ground. They love to form a big circle in which each kid has
exactly two neighbors, one on the left and one on the right. Sometimes the teacher has problems
arranging the circle because some kids wish to sit down next to other kids. Each kid may wish
to sit down next to at most two other kids, because each kid has just two neighbors in the
circle. The teacher wants to know whether it is possible to arrange the circle in such a way
that all kids’ wishes are satisfied. You clean up the place when the lunch ends. Since you want
to finish your work as early as possible, help the teacher in answering that question.
Input
Each test case is given using several lines. The first line contains two integers K and W
representing respectively the number of kids (3 ≤ K ≤ 109 ) and the number of wishes (0 ≤
W ≤ 105 ). Kids are identified with numbers between 1 and K. Each of the next W lines
describes a different wish using two distinct integers A and B (1 ≤ A, B ≤ K); these values
represent that kid A wishes to sit down next to kid B. Each kid has at most two wishes.
The last test case is followed by a line containing two zeros.
Output
For each test case output a single line containing an uppercase ‘Y’ if it is possible to arrange a
circle in such a way that all kids’ wishes are satisfied, or an uppercase ‘N’ otherwise.
4 3 N
2 3 Y
1 3 Y
2 1
1000000000 0
3 6
3 2
2 1
1 2
1 3
2 3
3 1
0 0