Acm Icpc2015 Finalround
Acm Icpc2015 Finalround
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
1 second
512 megabytes
Input
The input consists of multiple (at most 5) test cases. For each test case:
The rst line contains 2 integers N , P (1 N 1000, 0 P 104 ).
The second line contains N integers g1 , g2 , . . . , gN (1 gi 106 ).
The third line contains N integers q1 , q2 , . . . , qN (1 qi 3): qi = 1 means the i-th minion eats
only chocolate gelato; qi = 2 means he eats only vanilla one; and qi = 3 means he accepts both.
Each of the following P lines contains 2 integers ai , bi , which denotes minion numbered ai and bi dislike
each other (1 ai , bi N ).
Output
For each test case, print one integer which denotes the minimum total bananas served.
Example
stdin
2 1
10 10
2 2
1 2
3 2
3 3 2
2 1 3
1 3
2 3
3 0
1 1 1
3 3 3
stdout
10
2
0
Page 1 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
1 second
512 megabytes
Input
The positive integer N (1 N 104 ) is given on the rst line of the standard input. There are N
nonnegative integers on the second line w1 , w2 , . . . , wN separated by a single space (0 wi 104 for
1 i N and wi = wi+1 for 1 i < N ).
Output
On a line of the standard output, print by modulo 20151011 the number of satised sequences.
Example
stdin
2
3 1
stdout
3
Page 2 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
1 second
512 megabytes
Cups and Balls is a classic performance of magic, with various adaptations around the world. The most
widely performed version is:
There are 3 identical cups set down in a line on a table. Initally, a ball is concealed under the
middle cup. The magician will quickly and magically swap the cups for a number of times.
After that, the audience will guess where the ball is.
Here is the Minions version: There are N identical cups set down in a
line on a table. Under each cup is a ball labelled with a natural number
in range [1, N ]. These N numbers are distinct; and initially the cups
are arranged from the left-hand side to the right-hand side such that
the numbers form an ascending sequence.
The magician will perform T turns, which can be described as a triple
i j k:
First, reverse all cups from position i to position j in the line.
Then, the audience should tell the ball concealed under the cup
at position k is labelled with what number.
You are invited by Dave the smartest minion in town to play the audience role.
Input
The rst line of input contains 2 integers N , T (1 N 105 , 1 T 50 000). Next T lines, each
contains a triple i j k (1 i < j N , 1 k N ) as described above. Numbers on the same line are
separated by spaces.
Output
Print T lines, i-th line contains the corresponding answer for i-th turn in input.
Example
stdin
5
1
4
2
3
3 3
5 4
4 1
stdout
1
5
3
Explanation:
Initial sequence: 1 2 3 4 5
After the rst turn: 3 2 1 4 5
After the second turn: 3 2 1 5 4
After the third turn: 3 5 1 2 4
Page 3 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
1 second
512 megabytes
Input
First line of the input contains one integer N (1 N 2 105 ). Then N lines follow; ith of these lines
contains three integers ai , bi , ci (109 ai , bi 109 , 1 ci 109 ).
Output
Print the minimum total energy units Hiro should use to build Worm Tunnels in order to be able to move
between any two points in the universe. If this is impossible, print -1 instead.
Example
stdin
3
0 1 1
0 0 1
1 -1 2
5
10 2 5
0 1 20
1 0 11
0 -1 2
-1 0 3
stdout
-1
10
Page 4 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
1 second
512 megabytes
In the magic land of Oz, wizards are taught a list of spells since they were
young. The Oz spells are strings, which consist only of letters o and z.
Furthermore, for all Oz strings, the following property is true:
If we denote by nO the number of os and by nZ the number of zs in the
string, then for every substring of the Oz string, it should be true that
|nZ nO| 3 for this substring. For example, the string ozozzo is an Oz
string; while the string ozzzzzo is not an Oz string because it contains
the substring zzzzz and for this substring |nZ nO| = 5, which is greater
than 3.
If we sort the Oz strings by length and then lexicographically we will obtain a sorted list of the Oz strings.
The K th Oz string is the one that is in the K th position, starting from one, in this list. The rst 10
strings of this list are: {o, z, oo, oz, zo, zz, ooo, ooz, ozo, ozz, ...}. As the list is too long for a novice wizard
like Quinlan, can you help him to recall the K th Oz string?
Input
On the rst and only line of the standard input is a number K (1 K 1014 ) the position of the Oz
string that Quinlan wants to recall.
Output
On the rst and only line of the output, print the K th Oz string.
Example
stdin
8
stdout
ooz
Page 5 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
1 second
512 megabytes
Input
The input consists of multiple notes (at most 5) that Hiro wants to investigate. For each note:
The rst line contains 3 integers N, Rmin , Rmax (1 n 50, 0 Rmin + Rmax 50).
Each of the following (Rmin +Rmax ) lines contains 3 integers si , ei , hi (1 si ei N , 1 hi N ).
Output
For each note, print N integers p(1), p(2), . . . , p(N ) separated by a single space, which denote the smallest
lexicographical sequence; or Impossible if records contain contradiction.
Page 6 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
Example
stdin
8
3
3
3
1
2
1
2
1
5
5
2
3
3
3
3
1
2
2
2
1
1
3
3
stdout
Impossible
2 1 3
Page 7 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
2 seconds
512 megabytes
Anywhere Door is one of the most famous gadgets of Doraemon. When you want
to go to somewhere, just tell the door, then open and go through it. The location
behind the door is your expected destination. This magic door, however, has
certain limitation. Namely, it cannot be used if it has been exposed to strong
electromagnetic waves like solar winds. Or the door has only one instance and is
always kept inside Doraemons pocket; thus there is no way for Nobita to use the
door when Doraemon is not beside him.
Being aware of such limitation, Doraemon is about to upgrade his door to the
next version. In version 2.0, the Anywhere Door is a portable and reective
device. Suppose that the Earth map can be modelled as a 2D plane, where places
are points with integer coordinates. The Anywhere Door device provides a list of
N actuators placed at N dierent coordinates (xi , yi ) (1 i N ). And it functions with the following
behavior: when one stays at (u, v) and decides to use the actuator at (xi , yi ), then he or she will be
magicially moved to a destination at (2xi u, 2yi v).
Doraemon is not so good at implementation, you are trusted to write a program to verify his device.
Herein, Nobita will give you a list of test cases, each of which is asked to check if there is a path from
his home at (xS , yS ) to a destination (e.g., Shizukas house) at (xE , yE ).
Good luck to Doraemon, and wish his device early released!
Input
The rst line contains one integer T (1 T 105 ) the number of test cases in the input. Description
of each test is as following:
Each test consists of N + 3 lines.
The rst line contains one integer N (1 N 105 ) - the number of actuators.
The following N +2 lines contain two integers x, y (103 x, y 103 ) coordinates of N actuators,
Nobitas home (xS , yS ) and someones house (xE , yE ) respectively.
It is guaranteed that the sum of N in all the tests does not exceed 105 .
Output
For each test case, if Nobita can get to the destination print YES, otherwise print NO.
Page 8 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
Example
stdin
2
2
1 1
1 3
1 1
1 3
3
-1 1
1 1
1 -1
0 0
0 4
stdout
NO
YES
Explanation: For the second test case, Nobita starts at (0, 0) and uses actuator (1, 1) to move to
(2, 2). Then from (2, 2), he uses actuator (1, 1) to reach someones house at (0, 4).
Page 9 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
1 second
512 megabytes
Thomas is a brave young man who has made an amazing escape from the
hand of WICKED - an organization which uses human as the experimental
subject. However, one of his best friends named Minho was caught in the
last ght. As a result, Thomas decided to ght back. With a short
inspirational speech, he surprisingly became the head of the Right Arm an armed group formed by people who escaped from WICKED.
The Right Arm decided to battle from the inside, which means that they
need access to all the security doors. Since all the people in the Right
Arm used to be in WICKED, they can remember some of the access codes
in there. However, the security system works in a unusual way:
Step 1: enter the access code L.
Step 2: press the OK button. L will be displayed on the screen.
Step 3: press the OK button again. The system calculates the sum of the current number displayed
on the screen and the access code L. It means that after the second time, the result will be L + L
Continue pressing the OK button until we get a number which consists of equal digits only.
Since time is not on their side, Thomas wants to make sure if they could reach a valid number from a
particular access code.
Input
A single line containing a positive integer L (1 L 999) - the access code.
Output
If it is impossible to reach a valid number from the given access code, print I dont know! (without
quotes). Otherwise, print a pair of two integers digit and length denoted the digit and the length of the
valid number.
Example
stdin
74
50
stdout
2 3
I dont know!
Explanation: 74 + 74 + 74 = 222.
Page 10 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
3 seconds
512 megabytes
1vN
dist(i, v)}
i=1
where dist(i, v) denotes the distance between the two personality islands i and v.
Input
First line of the input contains one integer N (1 N 2 105 ). Then N 1 lines follow, ith of them
contains two integers pi+1 and di+1 predecessor of a island i + 1 and the distance between (i + 1)th
island and its predecessor (1 pi N , 1 di 2 105 , the graph represented by pi is a tree).
Output
Print N lines. In the ith line, print the answer when k = i.
Example
stdin
5
1
1
2
4
1
2
3
1
stdout
0
1
3
7
11
Page 11 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
stdin
stdout
1 second
512 megabytes
Input
The rst line contains an integer T (1 T 30) - the number of test cases.
The rst line of each test case contains two integers N , M (2 M, N 105 ) - the number of ninjas
and the number of tests respectively. Then there are M lines following.
Each line contains three integers res, A, B (0 res 4, 1 A, B N , A = B) denotes the result
of the test following the format described above.
Output
For each test case, print the minimal number of tests Naruto still needs to perform. If there is some error
in the database, print "-1" (without quotes).
Page 12 of 13
ACM ICPC 2015-2016, Ho Chi Minh City University of Science Training Camp
Team Selection Final Round, HCMUS October 11, 2015
Example
stdin
2
5
1
4
0
2
6
1
1
0
4
1
2
3
4
3
1
3
2
stdout
0
-1
2
3
4
5
3
1
4
Explanation: In the rst case, Naruto can gure out that 1 is Wind (because thats him), 2 is
Water, 3 is Wind, 4 is Wind and 5 is Lightning, so he will not need any more information. In the second
case, there is an error in the rst two tests.
Page 13 of 13