Sie sind auf Seite 1von 12

Andrew Stankevich Contest 47

Petrozavodsk Winter Training Camp, February 8, 2015

Problem A. Ambitious Plan


Input le:
Output le:
Time limit:
Memory limit:

ambitious.in
ambitious.out
4 seconds
512 megabytes

Steven Sleepberg is going to produce the forty seventh sequel to his lm Battleeld Jupiter and is now
planning the main battle scene. The script of the scene says that n empire drones would attack m republic
forts. The attack proceeds as follows: a drone shoots the laser cannon towards one of the forts. The defense
in turn has t power towers, and one pair of towers creates a power shield. The power shield is a segment
connecting the two towers. If the shield intersects the segment which connects the shooting drone with
its target a spectacular explosion takes place.
Steven wants the scene to be as spectacular as possible. So he wants to lm all possible pairs of shoot
and shield that cause explosion. Help him to estimate the length of the scene by counting the number of
possible explosions.
Let us consider drones, forts and power towers as points on a plane. The line of defense coincides with
the line y = 0, so y-coordinates of all forts and towers are less than 0, and y-coordinates of all drones
are greater than 0. You are given coordinates of n drones, m forts and t towers. Find the number of sets
{D, F, T1 , T2 } such that D is drone, F is fort, T1 and T2 are towers, and segments DF and T1 T2 intersect.
No two points coincide, no three points are on the same line.

Input
The input le contains multiple test cases.
Each test case starts with n the number of drones (1 n 1500), n lines follow, each line contains
dxi , dyi coordinates of a drone. Integer m the number of forts (1 m 1500) follows, with m
more lines, each line contains f xi , f yi coordinates of a fort. Finally there goes t the number of towers
(2 t 1500) followed by t lines, each line contains txi , tyi coordinates of a tower.
All drones coordinates satisfy 109 dxi 109 , 0 < dyi 109 . All forts and towers coordinates satisfy
109 f xi , txi 109 , 109 f yi , tyi < 0.
Input is followed by a line containing a single zero.
The total number of drones in the input le is at most 1500. The total number of forts in the input le
is at most 1500. The total number of towers in the input le is at most 1500.

Output
For each test case print one integer: the number of possible pairs of shoot-shield that cause an explosion.

Example
ambitious.in
3
1 12
10 30
30 10
1
10 -10
4
2 -11
9 -1
11 -1
15 -14
0

ambitious.out
7

Page 1 of 15

Andrew Stankevich Contest 47


Petrozavodsk Winter Training Camp, February 8, 2015

Problem D. Decomposable Single Word Languages


Input le:
Output le:
Time limit:
Memory limit:

decomposable.in
decomposable.out
2 seconds
512 megabytes

Deterministic nite automaton (DFA) is an ordered set h, U, S, T, i where is the nite set called input
alphabet, in this problem = {a, b, . . . , z}, U is the nite set of states, S U is the initial state, T U
is the set of terminal states and : U U is the transition function.
The input of the automaton is the string over . Initially the automaton is in state s. Each step the
automaton reads the rst character c of the input string and changes its state to (u, c) where u is the
current state. Then the rst character of the input string is removed and the step repeats. If after its
input string is empty the automaton is in the terminal state, it accepts the initial string , in the other
case it rejects it. The set of all words accepted by an automaton A is denoted as L(A).
One can visualize DFA as a directed graph representing its states as vertices and its transitions as edges
marked with characters. Terminal states are shown as double circled vertices, the initial state is marked
by an arrow. The picture below on the left shows the automaton for a language (ab) of words that
consist of zero or more repeated words ab. The picture below on the right shows the automaton for a
language a ba ba that consist of a and b and contain two b-s. For sake of clarity edges marked by
represent all transitions not explicitly drawn.
a

a
b

b
*

b
*

A set X of words is called a regular language if it is equal to L(A) for some DFA A. The index of a regular
language X denoted as ind(X) is the minimal number of states in a DFA A such that L(A) = X. For
example, the two automatons shown on the picture above are indeed the minimal DFA-s for the described
languages, so ind((ab) ) = 3 and ind(a ba ba ) = 4.
It is well known that if X1 and X2 are two regular languages its intersection X1 X2 is also a
regular language. For example, the intersection of the two languages described above is the language
Y = (ab) (a ba ba ) = {abab} that contains a single word abab. Clearly a single word language is
regular, the automaton for Y is shown on the picture below.
a

b
*

It is easy to see that if W is a single word language W = {w}, and length of w is n, the index of W is
equal to n + 2.
Page 5 of 15

Andrew Stankevich Contest 47


Petrozavodsk Winter Training Camp, February 8, 2015
A regular language X is called decomposable if X can be represented as an intersection of two regular
languages X = X1 X2 and ind(X) > ind(X1 ) and ind(X) > ind(X2 ). For example, the single word
language Y = {abab} is decomposable.
Given a word w of length n nd whether the single word language W = {w} is decomposable and if it
is, nd two automatons A1 and A2 such that number of states in both A1 and A2 is less than n + 2 and
W = L(A1 ) L(A2 ).

Input
The input le contains multiple test cases.
Each test case consists of a word w on a line on itself, w consists of lowercase letters of the English
alphabet, length of w is between 1 and 50, inclusive.
There are at most 100 tests in one input le.

Output
For each test case rst print YES if the corresponding single-word language is decomposable, or NO if
it is not. If the language is decomposable, the description of two DFA-s must follow. Each DFA description
must start with k the number of states, 1 k n + 1, where n is the length of the input word. Let
states be numbered from 1 to k, the initial state is the state number 1. Then print t the number of
terminal states, 1 t k, followed by t integers from 1 to k terminal states. The following k lines
must contain 26 integers each: for a state u print (u, a), (u, b), . . . , (u, z).

Examples
decomposable.in
abab
a
decomposable.out
YES
3
1
1
2 3
3 1
3 3
4
1
3
2 1
3 2
4 3
4 4
NO

3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

4
4
4
4

Page 6 of 15

4
4
4
4

Andrew Stankevich Contest 45


Petrozavodsk Winter Training Camp, February 6, 2014

Problem B. Bayes' Law


Output le:

bayes.in
bayes.out

Time limit:

2 seconds

Memory limit:

512 mebibytes

Input le:

Bayes' Law is one of the central theorems of elementary probability theory. It allows to update probability

A and B . Let A be a positive outcome of an


B be a hypothesis. The probability P (A|B) is the probability that A is observed if B is
true. We have P (A|B) = P (A B)/P (B). If we observe A indeed, we can estimate the probability of B as
P (B|A) = P (A|B) P (B)/P (A). In this problem you are given an experiment and the required condence
, and you must nd the best hypothesis B , such that P (B|A) . Let us get into more details now.

estimations based on experiments. Consider random events


experiment and

x. The experiment consists


. The result of the experiment is considered positive if
a f () b. Given f as a piecewise-linear continuous function, a, b, and , you must nd such segment
[L, R] that 0 L < R x, the probability P (L R | a f () b) is at least , and segment length
R L is minimal possible.
Consider a real-valued random variable
of evaluation of a given function

distributed uniformly between 0 and

on the value of

Input
The input le contains multiple test cases.

n  the number of segments in the description of f (1 n 100 000).


3
The following line contains two real numbers: a and b (0 a < b 10 ). The following line contains real
3 .
number (0 < < 1). Each number is given with at most 3 digits after decimal point, b a 10

Each test case starts with an integer

f 's graph. These lines contain two integers


6
3
each: coordinates of points (x0 , y0 ), (x1 , y1 ), . . . , (xn , yn ), 0 = x0 < x1 < . . . < xn = x 10 , 0 yi 10 .
The graph of the function f consists of segments (x0 , y0 ) (x1 , y1 ), (x1 , y1 ) (x2 , y2 ), etc. It is guaranteed
3 .
that P (a f () b) is at least 10
After that

n+1

lines follow, they describe the break points of

The last test case is followed by a zero that should not be processed. The sum of
input le is at most

in all test cases in one

100 000.

Output
L and R. Your answer may have absolute or relative error of at
P (L R | a f () b) condition and R L minimization. Tests are

6 ) but
designed in such way that there are no segments [L , R ] such that R L < (R L)(1 10
P (L R | a f () b) 106 .
For each test case two real numbers:

most

106

in both

If there are several possible solutions, output any one.

Examples
6
3.0 5.0
0.9
0 2
2 5
5 0
7 2
8 1
13 6
15 0
0

bayes.in

bayes.out
1.0 13.813333333333333

Page 2 of 15

Andrew Stankevich Contest 45


Petrozavodsk Winter Training Camp, February 6, 2014

Problem K. Kabbalah for Two


Output le:

kabbalah.in
kabbalah.out

Time limit:

5 seconds

Memory limit:

512 mebibytes

Input le:

Kai and Kevin are kabbalists. However, they don't like traditional kabbalah with its pentagrams, etc.
They have developed their own version of kabbalah that has its rituals centered around circles.
Now they are preparing a place for their kabbalistic rituals. They are going to have rituals at the backyard
of Kevin's grandmother's house. The backyard has a form of a convex polygon with

vertices. Friends

need to place two circular mats to the yard for their rituals. The mats must t completely into the yard
without overlapping. Of course none of the friends would agree to have a smaller mat, so their radii must
be equal to each other.
To have as powerful rituals as possible, Kai and Kevin would like to have as big mats as possible. Help
them to nd out what maximal radius of the mats could be.

Input
The input le contains multiple test cases.
The rst line of each test case contains

The following

n  the number of vertices of the backyard polygon (3 n 200).

lines describe vertices of the polygon in counterclockwise order, each vertex is described

by its integer coordinates

xi

and

yi (104 xi , yi 104 ).

The given polygon is convex, no three vertices

are on the same line.


The sum of

in all test cases in one le doesn't exceed 200.

Output
For each test case rst output one oating point number  the maximal possible radius of two nonoverlapping circular mats of the same size that can be put to the backyard. The following two lines must
contain two oating point numbers each  coordinates of centers of mats.
Your answer must have absolute or relative error of at most

106 .

If there are several optimal solutions, output any one.

Examples
kabbalah.in
4
0 0
10 0
10 10
0 10
0

kabbalah.out
2.9289321881345248
2.9289321881345248 2.9289321881345248
7.0710678118654752 7.0710678118654752

Page 15 of 15

Notes

Andrew Stankevich Contest 36


Petrozavodsk, August 30, 2009

Problem G. Magic Potions

magic.in
magic.out

Input le:
Output le:
Time limit:

1 second

Memory limit:

256 megabytes

L'Ashyto is a famous magician. He is working in his laboratory on a new set of magic potions to sell on
the upcoming magic fair. He has bought several bottles of each of

dierent magic substances that can

be used to create magic potions. Each magic potion must be composed of two dierent magic substances.
To create a potion the magician mixes the contains of two bottles and pronounces the magic spell.
Of course, L'Ashyto would like to create as many potions as possible. But he would like the quality of
their ingredients be as good as possible. L'Ashyto has numbered all substances from 1 to
number 1 being the best, and substance number

n,

substance

being the worst. If there are several ways to create

the maximal possible number of potions, he would like to create as many potions from substances 1 and
2, as possible. If there are still several variants, he would like to maximize the number of
then

1+4

potions, etc,

1+n

potions,

2+3

1+3

potions,

potions, etc. Help him to nd out how many potions of which

ingredients to create.

Input
The rst line of the input le contains
The second line contains

 the number of substances L'Ashyto has (2

a1 , a2 , . . . , an  the number
9
substance, etc (1 ai 10 ).

integer numbers:

the number of bottles of the second

n 100 000).

of bottles of the rst substance,

Output
The rst line of the output le must contain
must create. The following

 the number of dierent types of potions L'Ashyto

lines must contain three integer numbers each:

i, j, cij

 the numbers of

substances to create a potion of, and how many such potions to create. For each description there must
be

i < j.

Output potion description in lexicographical order.

Examples
4
1 1 1 1

magic.in

2
1 2 1
3 4 1

Page 9 of 12

magic.out

Andrew Stankevich Contest 36


Petrozavodsk, August 30, 2009

Problem H. Restoring Permutation

restore.in
restore.out

Input le:
Output le:
Time limit:

1 second

Memory limit:

256 megabytes

A permutation

a1 , a2 , . . . , an

a1 , a2 , . . . , an
is said to have a

Let us call a permutation

descent

is said to have a

xed point

at position

A = a1 , a2 , . . . , a2n

of

2n

if

at position

if

ai > ai+1 .

A permutation

ai = i.

integer numbers from 1 to

2n alternatively xed

if

the following conditions are satised:

from 1 to

n descents, all of its


n 1 and a2n1 > a2n );

has exactly

has exactly

descents are at odd positions (i.e.

a2i1 > a2i < a2i+1

for all

xed points.

For example, the permutation

3, 2, 6, 4, 5, 1

is alternatively xed.

Consider the following transformation of alternatively xed permutation: remove all xed points
and turn the remaining vector to permutation by replacing each remaining number with the count
of remaining numbers not exceeding it. For example, transforming the above permutation yields

3, 2, 6, 4, 5, 1 3, 6, 1 2, 3, 1.
You are given the result of the transformation. Restore the original alternatively xed permutation.

Input
The rst line of the input le contains
(1

n 100 000).

The second line contains

 the number of elements in the transformed permutation

integer numbers  the permutation itself.

Output
If there is no solution, output

alternatively xed permutation of

at the rst line of the output le. In the other case output the original

2n

numbers. If there are several solutions, output any one.

Examples
3
2 3 1
1
1

restore.in

3 2 6 4 5 1
-1

Page 10 of 12

restore.out

Andrew Stankevich Contest 34  Northern Grand Prix


Petrozavodsk, Monday, February 2, 2009
Problem I. Tour
Output le:

tour.in
tour.out

Time limit:

3 seconds

Memory limit:

256 megabytes

Input le:

There are

cities in Flatland, some of them are connected by bidirectional roads. The system of roads is

organized in such way that there is exactly one way to get from any city to any other city by the roads.
Recently the king of Flatland has decided that he would like to travel around the country and visit all the
cities. He would like to start travelling from the capital of the country, travel along the roads and return
to the capital visiting each city exactly once.
The minister of transport of Flatland told the king that it is impossible to do so, because there are no
cycles in the road system of Flatland, in particular there is no way to travel from the capital back to the
capital without visiting a city more than once. But the king insisted that he would like to perform the
tour. So the minister ordered to build new roads in order to make such tour possible.
Help the minister to nd the minimal number of roads to build so that the king could perform his tour.

Input
The rst line of the input le contains

 the number of cities (3

n 100 000).

The following

n1

lines describe roads. Each road is described by two integer numbers  the numbers of cities it connects.
The cities are numbered from 1 to

n.

Output
Output one integer number  the minimal number of roads that must be built.

Examples
5
1
2
2
1

tour.in
2
3
4
5

In the given example roads

tour.out

3 4 and 4 5 can be built, the king's tour would then be 1 2 3 4 5 1.

Page 10 of 11

Andrew Stankevich Contest 34  Northern Grand Prix


Petrozavodsk, Monday, February 2, 2009
Problem H. Peaks
Output le:

peaks.in
peaks.out

Time limit:

2 seconds

Memory limit:

256 megabytes

Input le:

a1 , a2 , . . . , an of
< ai > ai+1 are satised

Permutation

integer numbers from 1 to

ai1

for exactly

dierent indices

is said to have

(we consider

peaks if inequalities

a0 = an+1 = 0

for the

purpose of the above inequalities).


For example, permutation
Given

n and k

3, 1, 4, 5, 2

has two peaks at

i=1

and

i = 4.

nd the number of permutations of numbers from 1 to

n with exactly k

peaks. Return this

number modulo 239.

Input
Input le contains two integer numbers:

and

k (1 n 1015 , 1 k 30).

Output
Output one integer number  the number of permutations of numbers from 1 to
modulo 239.

Examples
3 1
10 3

peaks.in

4
131

Page 9 of 11

peaks.out

with exactly

peaks,

Andrew Stankevich Contest 16 Welcome to Hel..., ough, Petrozavodsk!


Petrozavodsk Winter Training Sessions, January 28, 2006

Problem G. Word Square


Input le:
Output le:
Time limit:
Memory limit:

square.in
square.out
1 second
64 megabytes

Some sets of n words of length n have a nice property it is possible to arrange them in the n n grid
so that this set of words is read both vertically and horizontally.
An example of such set is { DATE, FIND, IDEA, NEXT }. You can arrange them in the following
way:
F
I
N
D

I
D
E
A

N
E
X
T

D
A
T
E

Note that you can nd each word exactly once as a horizonatal word, and as a vertical word. Such
squares are called word squares, the largest known word square is 10 10.
Another example of the 4 4 word square is:
C
R
A
B

R
A
R
E

A
R
T
S

B
E
S
T

You are given 2n words such that it is possible to construct two dierent n n word squares from them.
You task is to divide the words into two groups of n words and construct the words square from each
group.
You are guaranteed that all words are the English words (however, some of them are really rare ones,
human names, or some extremely special terms).

Input
The rst line of the input le contains n (2 n 10). The following 2n lines contain n-letter word each,
words consist of capital English letters.

Output
Output two n n word squares constructed from the given words. Separate squares by a blank line.

Examples
square.in
4
ARTS
BEST
CRAB
DATE
FIND
IDEA
NEXT
RARE

square.out
FIND
IDEA
NEXT
DATE
CRAB
RARE
ARTS
BEST

Page 8 of 13

Andrew Stankevich Contest 16 Welcome to Hel..., ough, Petrozavodsk!


Petrozavodsk Winter Training Sessions, January 28, 2006

Problem H. Subword
Input le:
Output le:
Time limit:
Memory limit:

subword.in
subword.out
1 second
64 megabytes

Let us introduce the free group with two non-commuting generators.


We will denote generators as a and b and their reverses as A and B respectively. The group can be
described as a set of words consisting of a, b, A and B, the operation of concatentation, and the generating
relations aA = Aa = Bb = bB = .
In other words, we say that two words are equivalent if one can be transformed to another by successively
performing the following operations:
remove a subword equal to Aa, aA, Bb or bB from any position;
add a subword equal to Aa, aA, Bb or bB to any position.
For example, words abAaBBabbA and aAaBabaAbA are equivalent:
abAaBBabbA abBBabbA aBabbA aAaBabbA aAaBabaAbA,
but words abAB and baBA are not.
Equivalence classes of words are exactly the elements of the group.
It is interesting to note that for each word and each word there is such word equivalent to that
contains as a subword. Your task in this problem is to nd the shortest such word.

Input
The rst line of the input le contains . The second line contains . Both words are not empty and
contain at most 2000 characters each.

Output
Output one word the shortest word equivalent to that contains as a subword. If there are several
solutions, print any one.

Examples
subword.in
abAaBBabbA
AaBaba

subword.out
aAaBabaAbA

Page 9 of 13

Andrew Stankevich Contest 16 Welcome to Hel..., ough, Petrozavodsk!


Petrozavodsk Winter Training Sessions, January 28, 2006

Problem B. Darts
Input le:
Output le:
Time limit:
Memory limit:

darts.in
darts.out
1 second
64 megabytes

The game of darts is played by two or more players. Players in turn throw three darts into the dartboard.
Initially each player has some number of points, usually 501. Each dart thrown at the dartboard scores
some points. This number is subtracted from the players points. The rst one to reach exactly zero is
the winner.
The dartboard looks as shown on a picture. There are 20 sectors ar5 20 1
ranged around the central circle, called the bull eye. This circle in turn
12
18
consists of the inner bull eye (or simply the bull eye), and outer bull
9
4
eye. Outer bull eyes score is 25, inner bull eye is double outer, which is
50. The outer sectors score the number of points they are marked with.
14
13
There are also two circular areas going around the board. The outer
11
6
circle area doubles the points of the sector, and the inner one triples
them.
8
10
There are additional rules for the last round when the player is to de16
15
crease his score to zero. He can do it by throwing from one to three
7
2
19 3 17
darts. First, the player must decrease it to exactly zero, going below
zero is not allowed. Second, the last dart in a game must be double
that is, it must be thrown into the double part of some segment or into
the bull eye (which is double outer bull eye).
For example, one correct way to nish the game from 50 points is by throwing darts to 18 and D16.
Finishing by playing D20, 10, or 20, T10 is illegal (the last throw in these cases is not double).
Another correct way to win in this case is Bull.
Given the number of points left, determine all the ways to nish the game correctly.

Input
The rst line of the input le contains n the number of points left (1 n 200).

Output
At the rst line of the output le print k the number of ways to correctly nish the game. After
that print k lines, each must contain one correct way to nish the game. Use sectors value to denote it,
preceed it with D for double area, T for triple area. Use 25 to denote the outer bull eye, Bull to
denote the bull eye itself.

Examples
darts.in
5

darts.out
7
1 D1 D1
1 2 D1
1 D2
D1 1 D1
T1 D1
2 1 D1
3 D1

Page 2 of 13

Das könnte Ihnen auch gefallen