Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Romania ACM Programming Contest Final 2010
May 22, 2010
A - Binary Trees
During the warm summer nights, Vasilica has difficulties sleeping. Vasilica is more passionate about
algorithms and data structures than domestic animals and, thus, he is counting binary trees in order to fall
asleep. He discovered that the set of binary trees is countable. More precisely, every tree T has a natural
number assigned to it (index(T)) and all the natural numbers starting from 1 are assigned to some tree.
We define the size of a tree T as the number of nodes it contains (size(T)). For two binary trees A and B, we
have that index(A)<index(B) if:
1. (size(A)<size(B)) OR
2. ((size(A)=size(B)) AND (index(left_subtree(A)) <
index(left_subtree(B)))) OR
3. ((size(A)=size(B)) AND (index(left_subtree(A)) =
index(left_subtree(B))) AND (index(right_subtree(A)) <
index(right_subtree(B))))
We denote by left_subtree(T)=the left subtree of the tree T, and by right_subtree(T)=the right
subtree of the tree T. The index of an empty tree is 0 and the index of the tree with just one nodes is 1. The
first few trees are shown in the following figure:
Using these rules, Vasilica wants to know how quickly he is going to fall asleep. He chose a tree and wants
to count all the binary trees until it reaches the chosen tree. More exactly, given a tree chosen by Vasilica,
find its index.
Input Data
The input contains multiple test cases. The first line of each test case contains the number N (1≤N≤100) of
nodes of a tree T. Then, N lines follow, each of them containing two numbers, separated by a single blank.
The ith of these lines contains the indices of the left son (the first number) and of the right son (the second
number) of the node i. The nodes of a tree are numbered from 1 to N. A value of 0 indicates the absence of
the corresponding son.
A test case with N=0 marks the end of input. This test case should not be processed.
Output Data
For each test case print a line containing the index of the corresponding tree.
Example
Standard Input
2
2
0
3
0
3
0
0
0
0
Standard Output
3
6
0
1
0
1
Romania ACM Programming Contest Final 2010
May 22, 2010
B - Sum of Modulos
Given the numbers N and K, compute the value S(N,K)=(1 mod K) + (2 mod K) + ... + (N
mod K). We denote by A mod B the remainder of the integer division of A at B. For instance, 20 mod
3=2 and 20 mod 4=0. Thus, S(N,K) is the sum of the values (i mod K) of all the natural numbers
1≤i≤N.
Input Data
The first line of input contains the number T of test cases. The next T lines describe the test cases. Each test
case is described on a single line, which contains the numbers N and K, separated by one blank
(1≤N≤1.000.000.000, 1≤K≤N+1).
Output Data
For each test case print a line containing the number S(N,K).
Example
Standard Input
6
3 3
3 4
1000 7
1000000 6789
1000000000 98765432
999999999 17
Standard Output
3
6
3003
3389189455
48849260210410000
7999999965
C - Maximum Modulo Sum
Consider a sequence of N natural numbers v1, v2, ..., vN, and a natural number K>0. The modulo
sum of a contiguous subsequence [i:j] (starting at the position i and ending at the position j) is
(vi+vi+1+...+vj-1+vj) mod K. To be more precise, the modulo sum of a contiguous subsequence is
equal to the sum of the numbers contained in the subsequence, modulo K.
For each position i (1≤i≤N) find the maximum modulo sum of a subsequence starting at the position i.
Input Data
The first line of input contains the number T of test cases which are given next. The first line of a test case
contains the numbers N (1≤N≤50.000) and K (1≤K≤1.000.000.000), separated by a blank. The second
line contains the numbers v1, ..., vN, in order, separated by blanks. We have that 0≤vi≤K-1 (1≤i≤N).
Output Data
For each test case print a line containing N numbers. The ith number on the line is the maximum modulo
sum of a contiguous subsequence starting at the position i. Two consecutive numbers on the same line must
be separated by a single blank space.
Example
Standard Input
3
5
1
6
3
6
9
7
2 3 4 5
4
0 2 1 3 2
10
9 9 8 7 6
Standard Output
6 5 5 4 5
3 3 3 2 3 2
9 9 9 8 7 6
2
Romania ACM Programming Contest Final 2010
May 22, 2010
D - XY
Consider two (multi)sets of natural numbers, X and Y (a multiset may contain the same number multiple
times) containing N numbers each. We want to choose a (multi)subset SX of X and a (multi)subset SY of Y,
such that the sum of their cardinalities is maximum and their area is less than or equal to S. The cardinality
of a (multi)subset is the number of elements it contains. The area of a pair of (multi)subsets (SX,SY) is
computed as follows. Let MX be the maximum element from SX, MY be the maximum element from SY,
SumX be the sum of the elements from SX and SumY be the sum of the elements from SY. The area of the
pair (SX,SY) is equal to max{MX,MY} x max{SumX,SumY}. One or both of the selected
(multi)subsets may be empty.
Determine the maximum sum of cardinalities of two subsets SX and SY, such that their area is less than or
equal to a given number S.
Input Data
The first line of input contains the number T of test cases which are given next. The first line of a test case
contains the numbers N (1≤N≤50.000) and S (1≤S≤1018), separated by a blank. The second line contains
the numbers x1, ..., xN, separated by blanks, representing the numbers from the set X. The second line
contains the numbers y1, ..., yN, separated by blanks, representing the numbers from the set Y. We have
that 1≤xi,yi≤100.000 (1≤i≤N).
Output Data
For each test case print a line containing the maximum sum of cardinalities of two subsets SX and SY
obeying the restrictions mentioned above.
Example
Standard Input
2
6
3
7
6
3
7
1000
4 7 2 1 9
10 4 2 6 5
100
4 7 2 1 9
10 4 2 6 5
Standard Output
Explanation
12
7
For the first test case we select the (multi)subsets:
SX={3, 4, 7, 2, 1, 9} and SY={7, 10, 4,
2, 6, 5} (i.e. we select the full sets X and Y). We
have: MX=9, MY=10, SumX=26 and SumY=34.
The area of (SX,SY) is 10x34=340.
For the second test case we select the (multi)subsets:
SX={3, 4, 2, 1} and SY={4, 2, 5}. We have:
MX=4, MY=5, SumX=10 and SumY=11.
The area of (SX,SY) is 5x11=55.
E - Project Scheduling
You are given two projects, A1 and A2, and you need to schedule them (find their starting times t1≥0
and t2≥t1). For project A1 you are given S different scenarios: for each scenario i (1≤i≤S) you are
given li (the length of time in minutes required to complete the project) and ni (the number of times the
scenario will occur). Project A2 takes 0 time to complete (it finishes as soon as it starts). Also, project A1
needs to be finished before project A2 can be started. You only need to fix the starting time for project A1
beforehand; you are allowed to observe the scenario, and then schedule the second project A2.
You are also given a baseline schedule, x1 and x2, for the starting times of the project A1, respectively A2.
It should be noted that the baseline schedule doesn't have to be feasible, i.e one can have x2<x1. For both
projects, you pay a penalty of G dollars for each minute a project starts before the baseline schedule, and H
dollars for each minute a project starts after the baseline schedule. You are interested in minimizing the sum
Cixni (1≤i≤S), where Ci is the cost incurred by the scenario i.
3
Romania ACM Programming Contest Final 2010
May 22, 2010
When computing the minimum sum, note that the project A1 must start at the same time moment t1≥0
(chosen by you) in every scenario, but the project A2 may start at different time moments in each scenario.
The choice of t1 influences the cost in every scenario.
Let’s consider an example with G = 2, H = 3, x1 = 1, x2 = 6. Also, there are S=2 scenarios for
project A1: one with length 5 and number of cases 2, and another with length 8 and number of cases 5. Then,
one possbile schedule would be to schedule A1 at time 0. Then, in scenario 1 it would finish at time 5, and
the optimal way to schedule A2 would be at time 6. The cost in dollars for this scenario would be (x10)*G+(6-x2)*H=2, multiplied by the number of cases (2), equaling 4. In scenario 2, project A1 would
be finished at time 8. One would schedule project A2 at time 8 paying (x1-0)*G+(8-x2)*H=8 dollars
to do so. Multiplying that by the number of cases (5) we get a cost of 40 for this scenario. Therefore, the
total cost of this schedule is 40 + 4 = 44 dollars. It should be noted that this is just an example, and
doesn’t necessarily represent the best possible schedule.
Input Data
The first line of input contains the number T of test cases which are described next. The first line of a test
case contains five integer numbers, separated by a blank: S, G, H, x1, x2
(1≤S,G,H,x1,x2≤10.000). The next S lines contain 2 numbers each, li and ni, which represent the
length of time project A1 takes in scenario i, respectively the number of cases in which scenario i occurs.
Output Data
For each testcase print a line containing one integer number, Q, which represents the minimum possible cost
in dollars for a schedule.
Example
Standard Input
2
4 2 3 3 10
5 3
4 10
8 7
1 1
3 0 10 40 20
10 2
20 3
30 4
Standard Output
21
400
F - Green Country
In Romania there are N cities, numbered from 1 to N, connected by M bidirectional roads. City 1 is the capital
of Romania. Each city i has a greenness level G(i). The N greenness values form a permutation of the
numbers 1, ..., N, and we have that G(1)=N.
The government wants to be environmentally friendly and mark some of the roads as dangerous in such a
way that the minimum greenness level GL of a city reachable from the capital using only the non-dangerous
roads is maximum. However, there are two constraints to this plan:
1. some of the roads are of national importance and cannot be marked as dangerous
2. the number of roads marked as dangerous having an endpoint at a city i and the other endpoint at a
city j with G(j)<G(i) cannot exceed the value D(i) (1≤i≤N)
Input Data
The first line of input contains the number T of test cases which are given next. The first line of a test case
4
Romania ACM Programming Contest Final 2010
May 22, 2010
contains the numbers N (2≤N≤50.000) and M (N-1≤M≤100.000). The next N lines contain two numbers
each, separated by a blank: the ith of these lines contains the values G(i) and D(i) (0≤D(i)≤N-1). The
next M lines contain three numbers each, separated by blanks: x, y, c. This means that there is a bidirectional
road between the cities x and y. If c=1 then this road is of national importance and cannot be marked as
dangerous ; if c=0 then this road can be marked as dangerous. There is a path between every pair of cities
using the existing roads.
There is at most one road between a pair of cities and there are no roads from a city to itself.
Output Data
For each test case you should print one line, containing the maximum possible value of GL.
Example
Standard Input
2
4
4
3
2
1
1
1
2
3
4
4
3
2
1
1
1
2
3
4
1
0
1
4
2
3
4
4
4
1
0
1
4
2
3
4
4
0
0
0
0
Standard Output
Explanation
2
1
For the first test case, the roads between the cities 1 and
2 and between the cities 3 and 4 are marked as
dangerous. The cities reachable from the capital are 1
and 3. GL=min{G(1),G(3)}=min{4,2}=2.
For the second test case, no road is marked as
dangerous. All the cities are reachable from the capital
and GL=1.
0
0
0
1
G - Minimum Number of Modulo Operations
Consider a sequence with N natural numbers: v1, v2, ..., vN. We want to compute the value
(v1+...+vN) mod P, where A mod B denotes the remainder of the integer division of A at B. However,
in order to compute the result, we can use only one old computer, which presents the following restrictions:
it can only perform the modulo operation on the sum of a contiguous subsequence of numbers vi,
vi+1, ..., vi-1, vj, if this sum is less than or equal to PxQ (i.e. only if vi+vi+1+...+vj-1+vj≤PxQ).
Thus, in order to compute the result, we will use the following strategy: As long as the sequence contains
more than one number, we select a contiguous subsequence vi, vi+1, ..., vi-1, vj, such that
vi+vi+1+...+vj-1+vj≤PxQ, we compute the result x=(vi+vi+1+...+vj-1+vj) mod P and then we
replace the subsequence by the number x in the original sequence (i.e. the j-i+1 numbers are replaced by
the number x). Note that we are allowed to select a subsequence containing only one element.
Let’s consider the following example: N=5, P=3, Q=10 and the sequence is 7, 9, 14, 6, 2. We
select the subsequence 9, 14, 6 first (9+14+6=29 ≤ 3x10=30), we compute the value (9+14+6)
mod 3=2 and we replace the subsequence by the computed value, 2. The original sequence becomes 7, 2,
2. Then, we select the subsequence 7, 2, 2, we compute the value (7+2+2) mod 3=2, and we replace
the subsequence by the value 2. The original sequence becomes 2, and we stop, because it contains only one
number. We performed two modulo operations.
5
Romania ACM Programming Contest Final 2010
May 22, 2010
Find the minimum number of modulo operations which need to be performed in order to obtain the final
result (i.e. a sequence with just one number).
Input Data
The first line of input contains the number T of test cases which are given next. The first line of a test case
contains three natural numbers, separated by blanks: N (2≤N≤30), P (2≤P≤1.000.000) and Q (2≤Q≤30).
The second line contains the natural numbers v1, ..., vN, in order, separated by blanks. We have that
0≤vi≤PxQ.
Output Data
For each test case you should print one line, containing the minimum number of modulo operations which
need to be performed.
Example
Standard Input
2
5 3 10
7 9 14 6 2
13 10 2
1 2 3 4 5 6 7 8 9 10 11 12 13
Standard Output
2
6
H - Maximum Salary
As the crisis deepens in Romania, even the ministers have to take cuts from their salaries. A salary of a
minister in Romania is always written in base 109 (nobody knows why), and it is very big (up to 400.000
digits in base 109). A digit in base 109 has exactly 9 normal digits, except, possibly, for the first (leftmost)
digit, from which we remove leading 0 (normal) digits.
Because of the crisis, each minister has to cut off some (base 109) digits from his salary, but he wants to
keep the maximum salary possible. You should help them (nobody knows why).
Input Data
The first line of input contains the number N of ministers (i.e. test cases). For each of the ministers (test
cases) there will be another 2 lines. The first line of a test case contains one number in base 109, the initial
salary of the minister. The next line contains one number K which represents the number of digits that should
be cut off from the minister salary.
Output Data
For each minister output the maximum salary that he can have after cutting K digits in base 109 from his
salary. Remember that you should remove the leading 0s from the first (leftmost) digit of each printed
number. If all the digits of the salary have to be removed, then the resulting salary is 0.
Example
Standard Input
Standard Output
3
9072308891
1
9072308891
0
9172300000179200000921146382549
2
72308891
9072308891
300000179200000921
6
Romania ACM Programming Contest Final 2010
May 22, 2010
The first salary is in fact 9 072308891 in base 109 so to remain with the best salary we will cut the first
digit (9) and remain with 072308891. After removing the leading zeros => 72308891.
The third salary is 9172 300000179 200000921 146382549 => we cut out the first and last digits
and remain with the middle 2 digits => 300000179200000921.
I - Amoebae
An amoeba of generation 1 is a living being having four cells: one central cell and three cells which are
connected directly to the central cell, called extreme cells. An amoeba of generation n is constructed from an
amoeba of generation n-1, by connecting some amoebae of generation 1 to it, as follows. For every two cells
a and b at distance 2 from each other in the amoeba of generation n-1, such that at least one of the cells a
and b is an extreme cell in the amoeba of generation n-1, we attach a generation 1 amoeba to them, such
that two of the extreme cells of the generation 1 amoeba are overlapped (coincide) with the cells a and b. No
amoeba of generation 1 will be connected to the same two cells a and b of the amoeba of generation n-1.
The extreme cells of the new amoeba (the amoeba of generation n) will be the cells which are connected to
exactly one cell. The central cell of the new amoeba will be the central cell of the starting amoeba.
The figure below shows the amoebae of generations 1, 2 and 3, the central cell being labeled with „C” and
the extreme cells with „E”.
From the rules above it results that an amoeba of generation n ( 2) will have 3 ∙ 2n -2 cells out of which 3 ∙
2n-2 are extreme cells.
Given the generation of the amoeba find the minimum distances from the extreme cells to the central cell.
Group the cells by these distances and count the number of cells in each group.
Input Data
The first line of input contains the number T of test cases which are described next. The next T lines contain
one number each: N (1≤N≤15), representing the generation of the amoeba.
Output Data
For each test case output g lines, where g represents the number of groups in which the extreme cells of the
amoeba can be grouped by minimal distance from the central cell. For each group output two numbers
separated by a single blank: the minimal distance of the extreme cells in the group from the central cell and
the number of extreme cells in the group. The groups should be output sorted ascendingly by the first number.
You should leave an empty line after the solution of each test case.
Example
Standard Input
Standard Output
3
1 3
7
Romania ACM Programming Contest Final 2010
May 22, 2010
1
2
3
3 3
3 6
J - Minimum Enclosing Circle
Given N points in the plane, find the minimum radius R of a circle containing all the points inside of it or on
its borders.
Input Data
The first line of input contains the number T of test cases which are given next. The first line of a test case
contains the number of points N (1≤N≤3). The next N-1 lines contain two integer numbers each, x and y,
separated by a blank space, representing the coordinates of a point. We have: -100≤x,y≤100. The
coordinates of the Nth point are always (0,0).
Output Data
For each test case print a line containing the minimum required radius R. You should always print exactly
one decimal digit after the decimal point. Round the printed number up or down, according to the 2nd digit
after the decimal point (up if the 2nd digit is 5 or greater; down, otherwise).
Example
Standard Input
3
1
2
3 5
3
1 2
-3 3
Standard Output
0.0
2.9
2.2
K - Multiversion Queue
We consider a queue which can have multiple versions. The only initial version is numbered with 0 and is an
empty queue. We will perform a sequence of INSERT and REMOVE operations on this queue.
An INSERT operation has the form INSERT(v) and performs the following actions:
1. let C be the index of the operation in the sequence of operations (C is indexed starting from 1; both
types of operations are indexed)
2. a new version vnew is created, which contains all the elements of the version v; vnew is the
smallest natural number which does not exist as a version
3. the element C+v is added at the tail of the queue version vnew
4. print at the standard output a line containing the number of elements in the queue version vnew
A REMOVE operation has the form REMOVE(v) and performs the following actions:
1. a new version vnew is created, which contains all the elements of the version v; vnew is the
smallest natural number which does not exist as a version
2. the element at the head of the queue version vnew is removed from the version vnew and printed to
the standard output on a single line ; if the version vnew does not contain any element, then -1 is
printed to the standard output on a single line
Input Data
8
Romania ACM Programming Contest Final 2010
May 22, 2010
The first line of input contains the number NOP (1≤NOP≤1.000.000) of operations in the sequence of
operations. Each of the next NOP lines describes one operation. An operation is described by two integer
numbers, separated by a blank: op and v (v≥0). If op=1 then the operation to be performed is INSERT(v).
If op=2 then the operation to be performed is REMOVE(v). The parameter v will always denote an existing
version.
This problem contains only one test case.
Output Data
Print the output of the sequence of operations.
Example
Standard Input
9
1
1
1
1
2
2
2
2
1
0
1
2
0
3
2
1
7
2
Standard Output
Explanation
1
2
3
1
1
1
1
-1
3
The sequence of operations is:
Insert(0), Insert(1), Insert(2),
Insert(0), Remove(3), Remove(2),
Remove(1), Remove(7), Insert(2).
The queues corresponding to each version are the
following (left-to-right corresponds to the head-totail order):
version 0: empty
version 1: 1
version 2: 1, 3
version 3: 1, 3, 5
version 4: 4
version 5: 3, 5
version 6: 3
version 7: empty
version 8: empty
version 9: 1, 3, 11
Hint
You may find it useful to know that normal queues can be implemented using stacks.
9