Sei sulla pagina 1di 13

Petrozavodsk Winter Training Camp 2013

Jagiellonian University Contest, Friday, January 25, 2013

Problem A. AB-poems
Input file: abpoems.in
Output file: standard output

A modern recursive AB-poem is constructed from a given verse W and two key phrases Pa and Pb , all of
them being words over the alphabet {a, b}. The first verse of the poem is W1 = W , and the n-th verse
is constructed from the n − 1-th by replacing every a in Wn−1 with Pa , and every b with Pb .
You are organizing an annual reading of an AB-poem, one verse every year. Try to estimate how the
amount of time needed for future readings will change – in other words, compute the limit:

|Wn |
lim
n→∞ |Wn−1 |

where |Wn | denotes the total number of letters in the n-th verse of the poem.

Input
The first line of standard input contains a single integer T – the number of test cases. The test cases
follow.
A test case contains three lines, each line containing a word over {a, b}. The three words in these lines
are Pa , Pb and W , respectively. Words are nonempty and do not exceed 1000 characters.

Output
For each test case, your program should output a single line containing a single real number: the limit
computed by the program. The allowed absolute error is 10−9 . If the limit does not exist, write a single
minus (-) instead.

Examples
abpoems.in standard output
2 1.61803398875
ab -
a
ab
bb
aaa
baa

Page 1 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem B. Business Card Juggling


Input file: business.in
Output file: standard output

Lord Bradley has n friends. Each of them has their own name, titles and history but, to be frank, Bradley
has never cared to remember all this information. He simply numbers his friends 1, 2, . . . , n. For every
friend, Bradley keeps his business card: all n cards (in some random order) form a stack, which is always
in his wallet. Whenever a friend comes to visit him, and his proper titles must be quickly recalled, Lord
Bradley takes out his stack and moves cards (one by one) from the top to the bottom, until the visitor’s
card is the front one. Then the stack is put back into Bradley’s wallet.
Lord Bradley is a distinguished traveller and explorer, but he doesn’t like unnecessary work. Given a
sequence of his friends’ visits, determine for every visit how many cards he has to shuffle to find the
correct one.

Input
The first line of standard input contains a single integer T – the number of test cases. The test cases
follow.
The first line of each test case contains two integers n and m (1 ≤ n ≤ 1 000 000, 1 ≤ m ≤ 1 000 000),
denoting respectively the number of Bradley’s friends and the number of visits. The second line contains
the starting order of the cards in Bradley’s stack, from the top to the bottom – a permutation of
{1, 2, . . . , n}. The third, final line contains m space-separated numbers from the set {1, 2, . . . , n} –
numbers of visiting friends in the order of arrival.

Output
For each test case, your program should write a single line containing m integers – for every visit, output
the number of cards that Bradley had to move before that visit.

Examples
business.in standard output
2 0 1 1 1 1 1 1 1
4 8 3 2
1 2 3 4
1 2 3 4 1 2 3 4
4 2
4 3 2 1
1 3

Page 2 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem C. Corridor
Input file: corridor.in
Output file: standard output

The museum is closing right now and you still have one corridor to see. Under pressure of time, you
decide not to miss the opportunity.
The corridor does not contain any exhibits – it’s the corridor itself that is the exhibit. If you look at it
from above, it has two opposing walls, each forming a polygonal chain (a polygonal chain is a sequence of
vertices connected by line segments). Looking at the plan of the corridor, you notice that in each chain
the vertices’ x coordinates are in (strictly) increasing order. Moreover, the first and the last vertices in
that order are the only points where the two walls coincide – that’s where the entrance and the exit are
located.

In order to escape the guards who are going to be chasing you in a short while, calculate the length of
the shortest path from the entrance to the exit.

Input
The first line of input is an integer T – the number of test cases. T test cases follow.
The first line of a test case contains a single integer n1 – the number of vertices in the upper polygonal
chain. Each of the next n1 lines contains two integers xi , yi (|xi |, |yi | ≤ 109 ) – the coordinates of the i-th
vertex of the chain. The vertices are given in increasing order of x coordinates. After the description of
the upper chain there is a line with a single integer n2 , followed by an analogous description of the lower
wall. You may assume that n1 + n2 ≤ 200 000. It is guaranteed that the upper wall is higher than the
lower one along their full length except endpoints.

Output
For each test case, output the length of the shortest path linking the ends of the chains and situated
between the two polygonal chains. Your answer should be accurate to within an absolute error of 10−3
or a relative error of 10−9 .

Examples
corridor.in standard output
1 7.301
5
0 0
2 2
4 -1
5 1
6 0
5
0 0
1 -1
2 1
4 -2
6 0

Page 3 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem D. Division
Input file: division.in
Output file: standard output

Two mining companies found together a good spot for gold mining. Now, tough negotiations are coming:
the plot of land needs to be split between the companies.
The terrain is square-shaped, and has been divided into n × n unit squares. Every small square is either
empty, or contains one or two deposits of gold. Your goal is to split the plot into two connected parts,
not necessarily identical or of the same area.
The simplest propositions are what the companies like best. Therefore, the border between their areas
has to start in northwest corner, end in southeast corner, and must go along the unit squares’ borders.
Furthermore, the length of the border must not exceed 2n. Of course, the two parts must contain exactly
the same number of gold deposits.

Input
The first line of standard input contains a single integer T – the number of test cases. The test cases
follow.
The first line of a test case contains a single integer n, the length of the side of the plot (1 ≤ n ≤ 1000).
The next n lines contain the description of the plot: n rows containing n integers each. These are the
numbers of gold deposits in the unit squares, equal to either 0, 1 or 2. The first row is the northernmost
one, and every row is printed from its western end to the eastern one.

Output
For each test case, output:

• either a single word IMPOSSIBLE, if there is no viable proposition of the land division,

• or the description of the border, if a suitable proposition exists. Print a sequence of at most 2n
letters N, E, S or W, describing the border from the northwest to the southeast corner. The letters
mean that the border goes north, east, south or west, respectively.

If there are multiple solutions, output any one of them.

Examples
division.in standard output
1 SEESEESSSE
5
2 0 1 0 2
0 0 2 0 0
0 0 2 1 0
1 1 0 0 0
0 2 0 0 0

Page 4 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem E. Explosion
Input file: explosion.in
Output file: standard output

You are the designer of a conveyor belt for uranium fuel rods. Several L-shaped rods will be attached to
the belt. Every rod consists of a horizontal and a vertical arm, of not necessarily equal lengths.

The coordinates of the point where a rod is attached to the belt are (k, k), where k denotes the rod’s
number. This point is the left end of the horizontal arm and the upper end of the vertical arm.
You don’t have to be reminded that if you put too much uranium in the same place, bad things happen
(and remember who is to blame). This brings up the question of the maximum set of rods such that
every two rods share a point.

Input
The first line of input contains a single integer T – the numer of test cases. The test cases follow.
Each test case begins with a single integer n (1 ≤ n ≤ 100 000) in a separate line. The next n lines contain
descriptions of rods. The i-th line of the description contains two nonnegative integers vi , hi ≤ 100 000
which denote the lengths of the vertical and the horizontal arm of the rod number i, respectively.

Output
For each test case, output one integer on a separate line: the number of elements in the maximum set of
rods such that every two elements of this set have a common point. An end of a rod is also considered a
part of the rod.

Examples
explosion.in standard output
1 4
8
1 2
2 6
1 5
4 0
3 3
2 2
5 2
2 1

The example is depicted on the graphics above. The maximum set consists of rods number 2, 3, 5 and 7
(numbers are 1-based).

Page 5 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem F. Factoradic Numbers


Input file: factoradic.in
Output file: standard output

In the factoradic number system, numbers are written similarly as in the standard decimal system, but
the positions of digits have different meanings:
The first (from the right) digit is always 0 or 1, meaning 0 × 1! or 1 × 1!, respectively. The second digit
may be 0, 1 or 2, meaning 0 × 2!, 1 × 2! or 2 × 2!, and so on: the digit ci at i-th position belongs to the
set {0, 1, . . . , i} and has value ci × i!.
For example, the factoradic number 1310 is 1 · 4! + 3 · 3! + 1 · 2! + 0 · 1! = 44 in decimal system. It can be
easily proven that every number has a unique factoradic representation.
Given two nonnegative numbers in factoradic system, output their product in the same system. No
trailing zeros are allowed, only the number zero is written simply as 0.

Input
The first line of standard input contains a single integer T – the number of test cases. The test cases
follow.
A test case contains four lines: the first two of them describe the first factor, the last two – the other
factor. Each factor is described by its length d (1 ≤ d ≤ 2000) on the first line, and d space-separated
integers denoting its digits on the second line. The digits follow the format described in the statement.

Output
For each test case, your program should write a single line containing the factoradic representation of
the product – output only its digits, from left to right, space-separated.

Examples
factoradic.in standard output
1 5 1 0 0 1
4
1 0 0 1
4
1 0 0 1

Page 6 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem G. Garden
Input file: garden.in
Output file: standard output

There are n precious trees in the garden (for simplicity’s sake, we will treat them as points on a standard
Euclidean plane). Build a square fence around the trees such that:

• all the trees are inside the square,

• there is at least one tree on every side of the square (a point in the corner counts for both sides).
This way, no one can accuse you of claiming too much land.

You do not care for the area of the garden, or the amount of fence needed – any such square will do.
Only the trees are important, after all!

Input
The first line of standard input contains a single integer T – the number of test cases. The test cases
follow.
First line of a test case contains the number of trees n (4 ≤ n ≤ 100 000). Then n lines follow – for
j = 1, 2, . . . , n, j-th of these lines contains two integer numbers xj , yj – coordinates of the j-th tree. The
coordinates’ absolute values do not exceed 109 . No two trees coincide.

Output
For each test case, your program should output four lines, each containing a pair of real numbers – the
coordinates of the vertices of the square found by your program. The allowed absolute error is 10−6 .

Examples
garden.in standard output
1 0.500000 2.500000
4 -1.000000 1.000000
0 0 0.500000 -0.500000
0 2 2.000000 1.000000
1 2
1 0

Page 7 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem H. Hunt
Input file: hunt.in
Output file: standard output

Lord Bradley is on a quest to hunt a yeti, a representative of species considered extinct for millennia,
but only recently spotted in the Himalaya mountains. As an unmatched hunter and a lover of all things
valuable, he wishes to capture the yeti and add its skin to his extensive collection.
The yeti lives in a cave system beneath the mountains. In a questionably legal manner, Lord Bradley
was able to acquire a map of the system, which consists of some caves and tunnels of various lengths
joining them. Strangely, the system appears to be connected: every two caves are connected by a path of
tunnels. Given his exceptional physical prowess, the veteran voyager plans to push the yeti into a dead
end and engage it in melee. To achieve that, he asks you to destroy some of the tunnels so that every
two caves are connected by a unique path (the cave graph should become a tree).
Bradley does not wish to track the animal for too long – choose the destroyed tunnels such that the
length of the path between the two most distant caves is minimal possible.

Input
In the first line of input there is an integer T – the number of test cases. T test cases follow.
The first line of a test case contains two positive integers: n ≤ 400 and m ≤ 5000 – the number of caves
and the number of tunnels between them, respectively. Each of the next m lines contains three integers
a, b and d (1 ≤ a, b ≤ n, 1 ≤ d ≤ 106 ), which denote a tunnel linking caves a and b, whose length is d.

Output
For each test case, output one line containing the minimum longest distance between two caves after
destroying the tunnels (it is quite obvious that exactly m − n + 1 of them must be buried).

Examples
hunt.in standard output
2 6
4 4 5
1 2 2
2 3 2
3 4 2
4 1 2
4 5
1 2 2
2 3 2
3 4 2
4 1 2
1 3 3

Page 8 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem I. Invasion
Input file: invasion.in
Output file: standard output

It has been hundreds of centuries since our first space flight and the farthest settlements humanity has
founded are not strange new worlds, but a few rusting space stations. We never listened to those aware of
our unpreparedness and the passage of time made the improbable a certainty: Earth has been conquered.
Our planet’s states will be divided among two alien empires as soon as an agreement on their shares is
made.
Today’s Earth is a sphere, whose surface is divided into states. Every state is an area with connected
borders and connected interior, whose borders do not self-intersect. No more than three borders can meet
at one point. After our world is divided, each empire’s territory must also be an area with connected
borders and connected interior, whose borders do not self-intersect.

The aliens are facing the following problem: given a division of our planet’s surface, calculate the absolute
value of the difference between numbers of states in each empire’s territory. In order to gain their trust,
human leaders have decided to help them.

Input
The first line of input contains a single integer T , the number of test cases. T test cases follow.
A test case is a description of the world map, followed by multiple queries made by the aliens.
Let a special point be a point where three borders meet. The first line of the description of the world map
contains an integer 4 ≤ n ≤ 100 000, after which there are n lines each of which contains numbers xi , yi ,
zi , si,1 , si,2 , si,3 (1 ≤ i ≤ n) that describe the special points. The real numbers xi , yi , zi are coordinates of
the i-th special point. All special points belong to the surface of the same sphere whose center is located
in (0, 0, 0) and whose radius is 1 ≤ r ≤ 1000. The coordinates are given with absolute error of 10−6 .
You can assume that the accuracy of the input is enough for the answer to be unambiguous. The integer
numbers si,1 , si,2 , si,3 denote the special points that are connected to the i-th special point by a border.
The border is always the minor arc of a great circle. No border connects two diametrally opposite points.
After the world map, the description of the queries follows. The first line of the description contains a
single integer 1 ≤ m ≤ 200 000 denoting the number of queries. Each of the next m lines is a description
of a division of Earth’s surface between two empires. The description of the i-th division begins with the
integer ki , followed by ki identifiers of special points that, in that order, form the border of the division.
No other special point is at the border of the division. The border satisfies the conditions mentioned
earlier.

Page 9 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Output
For each test case, output m numbers on a separate line. The i-th number should be the absolute value
of the difference between numbers of states in each empire’s territory according to the i-th division.

Examples
invasion.in standard output
1 2 0
4
1 1 1 2 3 4
-1 1 1 1 3 4
1 -1 1 1 2 4
1 1 -1 1 2 3
2
3 1 2 3
4 1 2 3 4

Page 10 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem J. Jousting
Input file: jousting.in
Output file: standard output

Once upon a time, there was a great jousting tournament. There were n knights competing (n = 2k for
some integer k), whose names are now forgotten – modern historians number them 1, 2, . . . , n.
The tournament was held in a knock-out manner. In the first round the knight 1 beat knight 2, knight
3 beat 4, 5 beat 6 and so on. In the second round 1 won the match with 3, 5 won the match with 7 etc.
This continued until the end of the tournament: in every following round, every number was matched
with one of its closest numbers (there is exactly one way to do that), and the lower number won. The
tournament was (of course) won by knight 1, who was crowned the champion.
Assume that every competitor had an (unknown, but distinct) level of jousting skill and the better always
won a duel. Obviously, the champion was the most skilled one. Your task is to find all knights that could
have had the second-highest skill.

Input
The first line of standard input contains a single integer T – the number of test cases. The test cases
follow.
A test case contains one positive integer n ≤ 263 – the number of players in the tournament.

Output
For each test case, your program should write a single line containing first the number of possible silver
medalists and then their numbers listed in increasing order. Separate all the integers with spaces.

Examples
jousting.in standard output
2 1 2
2 2 2 3
4

Page 11 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem K. Kaleidoscope
Input file: kaleidoscope.in
Output file: standard output

You are given a square board divided into n × n unit squares, each of which is painted with some color.
The integer n is odd. For every k ≤ n, a k × k sub-square of this board is kaleidoscopic if:

• k is odd,

• the vertical line going through the square’s middle is its axis of symmetry (the square’s left and
right half are painted symmetrically),

• the horizontal line going though the square’s middle is also its axis of symmetry.

Compute the number of sub-squares of the board that are kaleidoscopic.

Input
The first line of standard input contains a single integer T – the number of test cases. The test cases
follow.
The first line of a test case contains a single integer n (1 ≤ n ≤ 4000) – the board side length. The next n
lines describe the rows of the board: each of them contains a word of length n over the English alphabet
(only lowercase letters). The letters denote the colors of the consecutive unit squares.

Output
For each test case, print a single integer on a separate line – the total number of kaleidoscopic sub-squares
of the board.

Examples
kaleidoscope.in standard output
1 56
7
bbbbbbb
abbbbbb
aabbbbb
aababbb
abbbbbb
aabaabb
aaaaaab

Page 12 of 13
Petrozavodsk Winter Training Camp 2013
Jagiellonian University Contest, Friday, January 25, 2013

Problem L. Lord Bradley on the North Pole


Input file: lord.in
Output file: standard output

A polar dawn is rising. Lord Bradley is standing precisely on the North Pole, facing the direction of his
distant home at Greenwich. From throughout the whole Earth, angry polar bears are coming to seek
revenge on him. Lord Bradley has only his good, faithful rifle. He never misses, and can take a shot (or
any number of shots) in negligible time. The rifle has exceptional range – it can hit a target all the way
up to the South Pole.
The only problem is changing the shot direction: to keep his perfect concentration, Lord Bradley always
turns at the speed of one arcsecond (100 = 3600
1
× 1◦ ) per one “time” second. Will he manage to shoot all
the bears, or will he become their breakfast?
Suppose that Earth is perfectly spherical, its circumference is exactly 40 320 km, and all the bears are
running straight at Bradley, with constant speed of 28 km/h. A bear can be shot even at point-blank
range (at the very moment of entering the North Pole), however, shot direction in such a case should still
be the direction the bear is approaching from. Given the initial geographic coordinates of all the bears,
determine whether Lord Bradley survives this peril.

Input
The first line of standard input contains a single integer T – the number of test cases. The test cases
follow.
The first line of each test case contains a single integer n – the number of bears (1 ≤ n ≤ 1 000). In the
next n lines, the bears’ coordinates are given (latitude and longitude, using * for degrees, ’ for minutes
and " for seconds, as in the Examples section). None of the degrees, minutes and seconds can be omitted.
All the numbers are nonnegative integers. As usual, N stands for nortern hemisphere and S for southern
one, E for bears to the East of Greenwich and W for bears to the West.

Output
For each test case, your program should write a single line containing a single word: ALIVE if Bradley
can defeat the bears, or EATEN otherwise.

Examples
lord.in standard output
3 EATEN
2 ALIVE
87*0’0"N 4*0’0"E ALIVE
88*0’0"N 6*0’0"W
2
86*0’0"N 4*0’0"E
87*59’59"N 6*0’0"W
1
89*59’59"N 0*0’4"W

Page 13 of 13

Potrebbero piacerti anche