Вы находитесь на странице: 1из 118

,

2008


. , INTSPEI . . . . . .
. . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A. ? . . . . . . . . . . . . . . . . . . . . . . . . . .
B. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
D. . . . . . . . . . . . . . . . . . . . . . . . . . .
E. . . . . . . . . . . . . . . . . . . . . . . . .
F. . . . . . . . . . . . . . . . . . . .
G. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
H. ! . . . . . . . . . . . . . . . . . . . . . . .
I. . . . . . . . . . . . . . . . . . . . . . . .
J. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
K. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A. Rope pulling . . . . . . . . . . . . . . . . . . . . . . . . . . .
B. Cassettes rent . . . . . . . . . . . . . . . . . . . . . . . . . .
C. Minimal segment . . . . . . . . . . . . . . . . . . . . . . . .
D. Convex graphic . . . . . . . . . . . . . . . . . . . . . . . . .
E. Sticks for quadrilateral . . . . . . . . . . . . . . . . . . . . .
F. Bubble sorting . . . . . . . . . . . . . . . . . . . . . . . . . .
G. Ways of pair-matching . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
B. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
D. . . . . . . . . . . . . . . . . . . . . . . . . . .
E. . . . . . . . . . . . . . . . . . . . . .
F. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
G. . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2

4
5
5
6
16
16
18
21
25
27
29
32
35
37
40
42
51
51
51
51
53
55
56
58
59
61
63
63
63
64
65
67
68
69
70
72
72
72


, , 14-20 2008.

A. .
B. . . . . . . . . . . .
C. . . . . . . .
D. . .
E. . . . . . . . . . .
F. -2 . . . . . . . . .
G. .
H. . . . . . . . . .
I. . . . . . . . . .
J. . .
K. . . . . . . . . . . .
.
... . . . . . . . . . . . . .
. . . . . . . . .
A. Brackets . . . . . . . . .
B. Car Wash . . . . . . . .
C. Painting Cottages . . . .
D. Painting Cottages . . . .
E. Painting Cottages . . . .
F. Minima . . . . . . . . .
G. Move to Front . . . . . .
H. TV Show . . . . . . . .
I. Hard Test . . . . . . . . .
J. Travel Agency . . . . . .


, , 14-20 2008.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

72
74
76
77
79
83
86
88
91
94
96
99
99
100
100
102
104
106
107
109
111
113
115
117

,
INTSPEI

(14.02.2008 .)
Vladimir
L.
Pavlov
Todays
and
Tomorrows
ALM
Solutions(International Software & Productivity Engineering Institute)
vlpavlov@intspei.com
Abstract
Today new methodologies appear almost as often as new programming
languages used to arise 20 years ago. Agile Unified Process (AUP),
Design-Driven Development (D3), Dynamic Systems Development Method
(DSDM), Test-Driven Development (TDD). . . more and more new software
development methodologies, approaches and philosophies become available
every quarter. This great diversity is an indication that software engineering
is not mature yet, it evolves rapidly and we will see even more changes soon.
What is the fundamental trend in the industry? What kind of methodologies
will be widely used in 5 years? In 10 years? And, the most important question
- how should a software development company pick the most appropriate
methodology to get a real competitive advantage?
The keywords in all the answers to these questions are: productivity
engineering. Moores Law did not double the productivity for human
beings. To accomplish that will require a change, not in computers, but
in the way human beings use them. The focus of IT now shifts towards
human productivity engineering. New software development methodologies
will revolutionarily optimize the workflows for the first element in the pair
human + computer.
This presentation will explain how the described trend may impact both
domestic and international companies, how to identify the right strategy to
benefit from it and how to mitigate associated risks.


, , 14-20 2008.

(15.02.08 .)
.
...
, 9 1979 . 1986 1994
88
. 1994

. , , - 11- ( ) .
, , ,
. ,
,
.
-
1502 , .
.
1996
2001 . 1999

. ( )

(2000.), .

( (), , ) ACM- (2001 .), .

, , 14-20 2008.

. ,
ACM- , ,
5- .
2001 -
,
,
ACM- . ,

, . ,
. .
2007
.
,
.

. .

, . , ,
, . , .
[., .
: ]. , ,
, .
, , . , 6


, , 14-20 2008.

. ,
, .
1. : P L Q
M . .
. ,
. ,
. ,
, L + M ( , ).
, .
-
,
( )
. O(LM), n P
Q. .
, . : , ,
, . , N N
. L+M . (brute force)
O(LM) ,
, O((L + M )log(L + M )).
O((L + M )log(L + M ))- [Kundu, S., (1987) A
new O(n log n) algorithm for computing the intersection of convex polygons.
Pattern Recognition.20:419-424].

.
. [Shamos M.I., Hoey D. (1976) Geometric intersection
problems Proc. Seventeenth Annual IEEE Symposium on Foundations of
Computer Science. October 1976, pp 208-215] , ,

, , 14-20 2008.

. O (
,
). .
.
O ( ). ,
.
P Q , .
, .
[ORourke J., Chien C.-B., Olson T., Naddor D. (1982) A
new linear algorithm for intersecting convex polygons. Comput. Graph Image
Processing 19:384-391] , , , . . , P Q 6= ,
P = P Q. P Q.
, , P, Q
P .
, .
,
P Q. P Q,
.
P Q. ,
,
( ). L + M
,
. , .
, L + M
P .
[ Toussaint
G. T. (1985) A simple linear algorithm for intersecting convex polygons].
,
8


, , 14-20 2008.

. , , ..
, .
P Q,
, P Q. ,
P Q.
,
. .
2. Ax + By + Cz + D = 0 (A,
B, C, D - ). , R[1:8],
- . ,
- R. , . (
,
).
. R d. ,
.
, ,
x, y z . R
d + x, d + y d + z . , x y
z . .

, , d + x + y , d + x + z , x + y + z , d + x + y + z . ,
, , R
d, d + x, d + y , d + z , d + x + y , d + x + z , d + y + z , d + x + y + z ,
d > 0 0 x y z . , .
:
R
{}
d := R[1]
x := R[2] d
y := R[3] d
temp := d + x + y
s:=

temp = R[4]:;

, , 14-20 2008.

temp = R[5] : R[5] := R[4]; R[4] := temp


s :=

z := R[5] d
R[6] 6= d + x + z R[7] 6= d + y + z R[8] 6= d + x + y + z
s :=

, , 4 .
R[i] A(A,B,C)
2 +B 2 +C 2 ( - i=1,
- i=2 ..).
(i=1) .
.
.
p
2
R x + y 2 ,

.
(
Xa = b) , . .
, X =
(x1 , x2 , x3 ), Y = (y1 , y2 , y3 ) Z = (z1 , z2 , z3 ). x,y z.
n = A(A,B,C)
2 +B 2 +C 2 - .
X, Y, Z
: (X, n) = x, (Y, n) = y , (Z, n) = z , (X, Y ) = (X, Z) = (Y, Z) = 0,
(X, X) = (Y, Y ) = (Z, Z) = a2 , a - .
, . X, Y, Z, x, y, z a, : (X 0 , n) = x0 , (Y 0 , n) = y 0 ,
(Z 0 , n) = z 0 , (X 0 , Y 0 ) = (X 0 , Z 0 ) = (Y 0 , Z 0 ) = 0, (X 0 , X 0 ) = (Y 0 , Y 0 ) =
(Z 0 , Z 0 ) = 1, , . , X 0 ,Y 0 Z 0
. , , H.
10


, , 14-20 2008.

p = (x0 , y 0 , z 0 ). , Hn = p.
, p - n, H. , ( ), n p
, 1. , (x0 )2 + (y 0 )2 + (z 0 )2 = 1
x2 + y 2 + z 2 = a2 . .. .
-,
Hn = p. n = p, . , , , .
p-n .
pn
, t - . (, t) = 0, = |pn|
Ht = t 2(, t). ,
,
. . kn , k - .
D
, k = d + A2 +B
2 +C 2 .

a := sqrt(x2 + y 2 + z 2 )
p[1] := x/a; p[2] := y/a; p[3] := x/a
temp := sqrt(A2 + B 2 + C 2 )
n[1] := A/temp; n[2] := B/temp; n[3] := C/temp
D: = D/temp
tr := 0
i 1 3

temp := p[i] n[i]


tr := tr + temp2
[i] := temp

tr := sqrt(tr)
H := ((1, 0, 0), (0, 1, 0), (0, 0, 1))
tr 6= 0
i 1 3
[i] := [i]/tr
i 1 3
j 1 3
H[i, j] := H[i, j] 2 [i] [j]

i 1 3

, , 14-20 2008.

11

j 1 3H[i,j]:=H[i,j]*a
temp := d + D_
i 1 3

[1, i] := temp [i]


[2, i] := [1, i] + H[1, i]
[3, i] := [1, i] + H[2, i]
[4, i] := [2, i] + H[2, i]
[5, i] := [1, i] + H[3, i]
[6, i] := [2, i] + H[3, i]
[7, i] := [3, i] + H[3, i]
[8, i] := [4, i] + H[3, i]

.
a = 0, 0 .
, H, d.
.
p.
3. N , (xi , yi ).
( i- -
vi ), . ( - ) ,
.
. , .
1. ,
. , T ,
t(t T ) (
, ). ,
T , t(t T ). ,
( )
,
- 0 (
), - (, , ,
12


, , 14-20 2008.

- ,
).

T.
. , i-
T (xi , yi )
vi T , . , i-
. , ,
.
- ,
. ( -
, - )
() , , . ,
i , ( -
() (i+1)-
) , i+1 .
, , ,
, ,
.
, ,
N . , .
, 2 .
2N. , ,
,
.
. ( , ),
. ,
, .. 2 N (N 1)/2 N 2 .
O(N 3 (log T
log )), T - ,
- .
,
- , ,

, , 14-20 2008.

13

.
,
.
2. T ( ) ( ) (x,y) ( ),
. -
. , ,
:
)
(p
2
2
(x xi ) + (y yi )
T (x, y) = max
1iN
vi
, . ,
. ,
- , T
i.
, (,
) - ( , O(N log N ),
- ,
). , -
, . , ,
.

(.. ). ,
( )
. ,
- - .
( ) , T(x,y)
. ( ,
) . T ,
T(x,y) . , ,
.
- , ( -
vi ).
14


, , 14-20 2008.

, ,
N , O(N) T(x,y), ,
.
3. :
:

f (x, y, T ) = T min
:

gi (x, y, T ) = (x xi )2 + (y yi )2 vi 2 T 2 0
, , , . , , gi .
, ,
NP-.
, ,
:
q
gi (x, y, T ) = (x xi )2 + (y yi )2 vi T 0
, .
. n+3 n 3
x,y,T. , , .
,
, ,
. ,
O(N 4 ), .
. . .

, , 14-20 2008.

15

. , .
, ,
.
, ,
.


A. ?
:
:
:
:

a.in
a.out
1
256

, , .
. . .
, , ,
. , a
b ( ) ,
.
, , ,
n, ,
. , ,
n, .
, x y, k , x = ky ,

. n .
a, b,
n ( 101 8).
Divisibles, a b n,
16


, , 14-20 2008.

Indivisibles, , Equal,
.
a.in
1 4 2
1 2 3
10 10 5

a.out
Equal
Indivisibles
Divisibles

A. ?
, .
, , a b ,
a b, , ,
. a b.
, ,
n, -n. n
.
a b a b ( b a + 1)
, y. ,
d(b, n) d(a 1, n), d(x, y) - y m x.
, m = 0 ( m = 1), , d(x, y) ( ) x
- y , x + 1 m 1,
.
n = 0 , ,
, 0 0.
,
.
n. 1, n = 1,
Divisibles. n = 2 a b:
, , , ,
,
. n = 3 , Indivisibles
, a b 3.
, a b 1 3, Equal,
Divisibles a=b. n > 3 n = 0,

, , 14-20 2008.

17

n = 3, Equal
, 1.

O(1).
, , ,
. ,
[, , ] x div y = floor(x/y)
( floor(x) - , x), x mod y = x - (x
div y) y ( x div 0 , , x mod 0 x). 0 y
, .
x div y = trunc(x/y) ( trunc(x) - , , floor(x)
x ceil(x) ),
, , div.
. ,
, ,
.

B.
:
:
:
:

b.in
b.out
1
256

, . , -, 10
, 20
. 69 , . . .
N , -
683-1. . , K , 18


, , 14-20 2008.

.
. ,
.
, N .
N K
(0 N 10100000 , 2 K 1010000 ).

.
b.in
5 8
1 2 6

b.out
5
14

B.
.
, . K ,
. . (
, ),
. , , N = 36 K = 6 (
) 36
, 6 , ,
6 , . , - 43
.
,
, , .
K , , ,
O(logK N ) .
, ( ). ,
, O(logB N logB K ), B -
, .

, , 14-20 2008.

19

, O(logK N logB N logB K )


= O((logB N )2 ), .
.
, N N .
. . (K 1) ,
, .
,
. , N 1 K 1 ,
N + (N 1) div (K 1).
N = 0 ( div
, K, , div
, K = 2 ),
.
.
O(logB N logB K )
( ).
B, , . , B
10, .
B = 10000, ,
longint (
). -
, (L + 1) ( (uL , uL1 , . . . , u0 )B ) L-
((uL , uL1 , . . ., u0 )B ).
 L1
. [, q = min B 1, uL B+u
vL
, .2], q
q 2 q q , vL1
B/2 ( ,
B div (vL1 + 1)).

20


, , 14-20 2008.

C.
:
:
:
:

c.in
c.out
1
256

, ,
. . .
, , .
. ,
, .
.

, , , , - .
.
M
N (2 M ,N 1000000).
x y (0 < x < M , 0 < y < N ).
vx vy , (1000000
vx ,vy 1000000, vx2 + vy2 6= 0, ..
). - .
: - 0
(0,0), - 1 (M,0),
- 2 (0,N), 3 (M,N). , (
), ( ,
, - ),
(, ,
).
, ,
1, , .

, , 14-20 2008.

21

c.in
3
1
3
4
1
2

7
3
6
3
2
1

c.out
3

-1

C.
,
, .
, ( c
(kx M, ky N ,(kx + 1)M ,(ky + 1)N )). (kx M ,ky N ), kx ky - .
0 kx ky , 1 - kx
ky , 2 - kx ky , , , 3 -
kx ky . ,
(x,y) (vx ,vy ). , ,
(kx M ,ky N ), kx ky - , .
(x + vx t, y + vy t), t 0. ,
:

x + vx t = kx M ,
y + vy t = ky N ,
kx ky - , t - (
) .
,
t. (
, ).
vy , vx , t:

kx M vy ky N vx = x vy y vx (*)
t. :
22


, , 14-20 2008.

( ) (*), (*) ( t ,
t ).

ax + by = c. c d=(a,b),
( x y
(a,b)). c d,
() ( (x , y ).

x = x + r (b/d),
y = y r (a/d),
r - .
:
(x0 ,y0 )
ax + by = (a, b). , x = x0 (c/d), y = y 0 (c/d).
, (*), , t. , x 0 < x < M ,
x + vx t = kx M , vx > 0 - kx
(*), 1,
vx < 0 - kx (*), 1 ( vx = 0,
kx
y, (
), , (*) ).
, , ,
x = x0 (c/d) 64-
( x0 c 1012) ( b/d

- x = x + r(b/d)
,
).
. .

a b (
).

, , 14-20 2008.

23

m1 = a; m2 = b;
while (m2 )
{
q = m1 /m2 ;
m1 = q m2 ;
m1 m2 ;
}
m1 (a,b), m2 .
x1 , y1
x2 , y2 , m1 m2
a x1 + by1 = m1 ax2 + by2 = m 2:

x1 = 1; y1 = 0; m1 = a;
x2 = 0; y2 = 1; m2 = b;
while (m2 )
{
q = m1 /m2 ;
(x, y, m)1 = q (x, y, m)2 ;
(x, y, m)1 (x, y, m)2 ;
}
, m1 (a,b), (x1 , y1 ) ax + by =
(a, b), ,
, a b 0,
|x1 | < b/(a, b), |y1 | < a/(a, b) ( , , ).
, m2 , - x2 , y2 b/(a, b)
(a/(a, b)), r .
( , ) a b N O(log N ).
, a b - .

24


, , 14-20 2008.

D.
:
:
:
:

d.in
d.out
1
256

, .
-,
.
. . .

: . ,

. , -
. ,
. - .
, .
, ,
, .

x1 , y1 , x2 , y2 , x3 , y3 , - x4 , y4 , x5 , y5 , x6 , y6 .
xi , yi - , 108 . , .
( , , - 0-).


, , 14-20 2008.

25

d.in
-1 -1 1 0 -1 1
0 -2 3 0 0 2
0 0 2 0 2 2
0 0 2 2 0 2
2 -1 3 0 2 1
-1 -1 1 0 -1 1

d.out
3
2
0

D.
, .

0 6.
, ( )
,
. , - ,
. ,
.

, z
, (
z ).
X A1 A2 A3
.
,
, Ai Ai+1
Ai X i.
AB CD ,
AB CD, CD - AB.
AB CD
, CD CA CD CB
.
, -
:
OA + t1 AB = OC + t2 CD
- AB ( 0 t1 1 ), - CD.
26


, , 14-20 2008.

, OA :
t1 AB t2 CD = AC .
CD,
t1 AB CD = AC CD,
AB t2 AB CD = AC AB .
t1 t2 ,
0 1 ( ,
). ,
(108 ) 1016 ,
t 1016 .
.

E.
:
:
:
:

e.in
e.out
1
256

. -
, ,
, 1883 - . . .

. , .

( ). ,
.
. , , , ,
.
,
, .
, ( )
N
2N 1 .

, , 14-20 2008.

27

. , . ,
,
. ,
,
.
N M
(0 < N 63, 0 M 2N 1).
1 N.
- M ( ,
M ). , .
- . ,
.
e.in
3 2

4 14

e.out
1 1 1
3
1
2
0 3 1
4 3 2
1

E.
.
, N src dest, aux
.
dest, N-1 aux,
, N , aux dest.
Hanoi(N,src,dest,aux)
{
28


, , 14-20 2008.

if (N)
{
Hanoi(N-1,src,aux,dest);// 1 2N 1 1
N src dest // 2N 1
Hanoi(N-1,aux,dest,src); // 2N 1 +1 2N 1
}
}
N.
. , M 2N 1 ,
, N-1 .
M 2N 1 , N , M 2N 1 N-1
.
.
- O(N).

F.
:
:
:
:

f.in
f.out
1
256

. ,
-
,
. . .
,
.
,
( , )
, ,

, , 14-20 2008.

29

- , ( ).
- , . -
.
N (0 <
N 200000). 3 s1 , s2 , s3 (0
s1 , s2 , s3 N, s1 + s2 + s3 = N ) -
.
( ). ,

1 N. -.
.
,
, - 1000000.
f.in
3
1 1 1
3
1
2
4
0 3 1
4 3 2
1

f.out
1
2

2
1

F.
-
2003 ( ( ,
-) - Tobias Thierer Adrian Kuegel).
: .

30


, , 14-20 2008.

-
, ( N) , - -
N
, . (
, ) .
, CEOI, -
O(2N ), O(N 2 )
O(N ). , (
), .
N 1.
, ,
N 2 1 .
,
N 2 1 ( ,
N 1, , ) 2N 2
( (N 1)- 2N 2 1 N 2 1). ,
2N 2 , N 2 1
.
.
, i- ,
i. , 2, ( N-2 ).
, ,
.


, , 14-20 2008.

31

G.
:
:
:
:

g.in
g.out
1
256

. , .

. . .
,

,
,
. , , . ,
- , . ( N ),
,
.
, , ,
.
1, K, K 2 , . . . , K M ,

.

K, M, N (2 K 100, 0 M 100, 0 N 263 1, N K M ).
- , ,
, .
, , - .

32


, , 14-20 2008.

g.in
10 3 123
2 6 61

g.out
6 0
100 10 10 1 1 1
1 2
64
1 2

G.
, - , , .
1.
, .
,
,
.
2.
K-1.
K (
i
K ), K i+1 (
, ,
(i = M ), ,
).
3. M, , ,
N K M , ,
K.
, . , K i (
i > M ). ( ) .
K M +1 . , ,
. , K M +1 . ( K M +1 N )
K M . ,
K M 1 K-1

, , 14-20 2008.

33

. , ,
, K M 1. , K M
, ceil(((K M +1 N ) (K M 1))/K M ) ceil((K M +1 K M
K M + 1)/K M )=ceil(K 2 + K M ) = K 1.
. K i (i > M ) K M (K 1)
K M K M , .
KM .
, , M
.
4. , K/2 , ,
,
K/2.
, K i c > K/2 ( c (K + 1)/2) , .
K i+1 (K c) K i . ,
(1 + (K c)) =
2c (1 + K) 2 (K + 1)/2 (1 + K) = 0. ,
.
, K i
K i+1 K/2. (, ),
K i+2 , K/21 K i+1 K/2 K i ,
. K i+1 , K i - , K/2 1 K i+1 K/2 K i ,
1.
5. , , 4,
N .
. , 1, K, 1 ( ) N
K. N K K/2,
.
K/2, ,
,
34


, , 14-20 2008.

K. (
K),
K ..
, , N
64- (263 1), , - . , K = 2, 263 1,
263 1. ,
64- , ,
, ( ).

O(logK N ).

H. !
:
:
:
:

h.in
h.out
1
256

, , .
, , . . .
K .

( , , ).
, , .
, - ,
. , ,
, . ( , )
. ,

, , 14-20 2008.

35

,
, .
( ),
. - ,
-, .
, ( ,
).
( ) .
.
, , N .
K N (2 K 1000, 0 N 100000).
K -
( ,
). 30000.
- .

H. !
, K 1 . ,
K , K ,
,
. , ,
i- K 2 + i .
. . ,
, , ,
- , , .
a1 , a2 , a3 .. .
36


, , 14-20 2008.

, . -
, .

- , ,
.
( ),
. .
. . . K- K 2 , .
,
, (K 2)- K- (
). K 3, ,
,
, , .
- (K 1)- . ,
N ( ) .
- ,
, (K 1)-.
(K 3)- (
(K 1)- K-). , (K 4)-
..
, , , (
K = 2), - (
)
.
O(NK).

I.
:
:
:
:

i.in
i.out
2
256

, . 58,65 2/3
, , 14-20 2008.

37

. , , , . . .
,
, . , ,
,

. ,
. .

,
M N , - .
(
).
T1 T2
.
(i1 , j1 ), -
(i2 , j2 ).
.
- . ,
, ,
T2 T1 .
- (i2 , j2 ),
.
,
(i2 , j2 ).
M,
N (1 M, N 1000), - T1 T2 (0 T1 < T2
1000000), . i1 , j1 , i2 , j2 (1 i1 , i2 M, 1 j1 , j2 N ) -
(,
). M N , (, , ,
38


, , 14-20 2008.

-). 0
1000000.
i.in
4 3
20 30
2 2 4 3
19 17 23
10 28 25
11 25 29
12 13 10
2 3
10 20
1 1 1 3
15 3 15
17 14 0

i.out
15
ENHWWHHHHHSSSEE

-1

,
, 1,
.
. ,
. -
N ( ), S ( ),
W ( ), E ( ) H ( 1 ).
(i, j ) (i 1, j ), - (i + 1, j ),
- (i, j 1), - (i, j + 1).
.

I.
, ,
. . ,
- (, ) ,
.
l
Tb . Te k,
:

, , 14-20 2008.

39

k l+1
T1 Tb + k T2
T1 + 1 Te + k T2
+1 ,
Te + k .
O(V 2 ),
V - . () V = M N
, .
( )
O(E log V ), E - .
E 4V ,
.

J.
:
:
:
:

j.in
j.out
1
256

. , . , .
. . .
, -,
. -
,
- ,
.
, .
,
,
. .

40


, , 14-20 2008.

, , - , .
x1b , y1b , z1b
x1e , y1e , z1e ,
x2b , y2b , z2b , x2e , y2e , z2e . ,
100.
- -
106 .

j.in
1 1 -1 1 -1 1
-1 1 1 -1 -1 -1
1 2 3 4 5 6
0 0 0 4 6 8

j.out
2.000000
0.000000

J.
, ,
( , ,
,
).
. AB CD. X1 AB X2 CD.
AX1 AB
AX1 = t1 AB , 0 t1 1 (
AX1 AB). CX2 = t2 CD, 0 t2 1.
X1 X2
X1 X2 = X1 A + AC + CX2 = t1 AB + AC + t2 CD.
f (t1 , t2 ) X1 X2 ( ):
f (t1 , t2 ) = t21 AB 2 +t22 CD2 +AC 2 2t1 t2 AB CD2t1 AB AC +2t2 AC CD

0 t1 , t2 1.

, , 14-20 2008.

41

( (), , ,
0 t1 , t2 1),
(t1 = 0, t1 = 1, t2 = 0 t2 = 1).
f (t1 , t2 ) ,
t1 t2 .
- , ( ).
, 0 t1 , t2 1. ,
.
( t1 t2 ) , .
- O(1).

K.
:
:
:
:

k.in
k.out
1
256

, ,
Windows. ,
-, , .

, ,
. . .

42


, , 14-20 2008.


. -
N N ( )
( , ..). . ,
, :
1) Li, i 1 N 1 -
i .
, L2 .


, , 14-20 2008.

43

Ri, U i, Di,
i , .
2) V i, i 1 N/2. i = N/2,
, N/2 N/2 + 1. i < N/2,
, 2i 2i+1,
, i i + 1, -
N/2 + i N/2 + i + 1 ( N ) (N + 1)/2 + i (
N ).
, V 2 .

44


, , 14-20 2008.

Hi ( , V
).
3) P angle, angle 270, 180, 90, 0, 90, 180
270 -
.
, P 90 .


, , 14-20 2008.

45

4) T - ().
, , ,
.
, . , ,
,
. ,
, .
N (1 N
10 ) M
(0 M 105 ). M
.
9

, 46


, , 14-20 2008.

, . ,
1.
k.in
6
L
3
P
T
P

1
2
3
90

k.out
2
1

90

K.
, 0 (
,
).
N N (, 1 N 2 ).
, ,
, .
O(LM N 2 ), L - . ,
. , ,
. , , .
.
M , p 1 N 2 ,
p.
O((L + M )N 2 ) , - .
, -
L, , pL+1 = e, e - . (
-1) . N 2
O(N 2 ). ( ) O(log N ) .

, , 14-20 2008.

47

O(M N 2 ) .
O((M + log N )N 2 ).
,
. , . , , Lk ,
(i, j ), (i, j p) ( N ).
( Zn2 ),
3 3
Zn

1 0
0
px kxx kxy
py kyx kyy

(1, i, j)T .
() :

48


, , 14-20 2008.

1 0 0
0 1 0
Lp
1
p 0
1 0 0

0 1 0
Rp
p 0 1
1 0 0
p 1 0
Up
1
0 0
1 0 0

p 1 0
Dp
0 0 1

1
0 0
0
1 0
Vp
2p 1 0 1
1
0 0
2p 1 1 0
Hp
0 0 1
1 0 0

0 1 0
P0

0 0 1
1 0 0
P 90; P 270 1 0 1
0 1 0
1 0 0
P 180; P 180 0 1 0
0 0 1
1 0 0

0 0 1
P 270; P 90
1 1 0
1 0 0

0 0 1
T
0 1 0
. ,
, , O(M) .
(L + 1) ,
, , 14-20 2008.

49

, L ,
O(L) . O(M + L).
, L 2N,
.


kxx kxy
,
kyx kyy
( , 90,180, 270,
) Zn2
 0, 





1 0
1 0
1 0
1 0
8:
,
,
,
,
0
1
0
1
0
1
0
1

 
 
 

0 1
0 1
0 1
0 1
,
,
,
.
1 0
1 0
1 0
1 0
L, .
, O(M + log N ).
- N = 1 N = 2
, 1 1(modN ).

50


, , 14-20 2008.

(16.02.08 .).
.
...
6 , - . ,
.
:

(2004, 2006)
. 2005
(2002-2006) (- ).
2006 ( ).
(2003, 2007)
. ( 2003 :
, 30 72, 2007 :
, 26
88.)


A. Rope pulling
:
:
:
:

a.in
a.out
1
256

Some programmers want to compete in rope pulling. They want to


hold some rounds, such that in each round all programmers divide into 2

, , 14-20 2008.

51

equal teams (we suppose, that the quantity of programmers is even). The
programmers are interested in process - not in result. So the division for each
round doesnt depend on previous rounds. Programmers are very competitive,
so each of them wants, that he will be in the opposite team with any other
programmer for in at least one round. Programmers are very busy (or lazy
:) ), so they want to hold as less as possible rounds. What is the minimal
number of rounds, for which exist the splittings, satisfying all conditions?


Input file contains only an integer n(2 n 250), which is always even.


In first line you must output the minimal number of rounds. Then each
line describes the scheduling for each round. Per each round you must output
exactly n/2 distinct numbers, which correspond to programmers in the first
team. You havent to output the second team, because it is deduced from the
first. Programmers (as real programmers) are numbered from 0 to n 1. You
must split their numbers by single spaces. You can write any schedule, which
satisfies the condition.

a.in
2
4

a.out
1
0
2
0
0
3
0
0
1

1
2
1 2
1 3
5 2

A. Rope pulling
, ,
,
. - k ,
n 2k , k = dlog2 ne.
. n - ,
j
52


, , 14-20 2008.

, j 0. n - ,
, .
, (2k n)/2,
2k 1. ,
. ,
. -
.
- O(u), u - .

B. Cassettes rent
:
:
:
:

b.in
b.out
1
256

Suppose, you want to watch some films, taking them in a rent center. Each
film has its own price pi and rent cost ri . It means, that you must leave the
amount of money pi to take the cassette. After watching you can return it
and youll get for ri less, that you left in the center (namely, you get back
the amount pi ri ). Initially you have an amount m. You cant take only that
cassettes, for which you can cover the price pi . You appreciate each film in
some points from 1 to 5. So you want to watch the films with the greatest
sum of points. If you see the film repeatedly, you wouldnt take points for it
again, so you ought to take each cassette no more than once. You can take
the cassettes in arbitrary order. What maximal sum of points can you get?


Input file begins with m(1 m 2 109 ) - your amount of money. After
a space goes n(1 n 1000) - the quantity of cassettes. Then, in each of n
lines there are three numbers pi , ri , si - the price, rent cost and satisfaction
points of ith cassette. We always have 1 ri pi 2 109 and 1 si 5.
All numbers in the input are integer.


You must output the maximal sum of points, you can get.


, , 14-20 2008.

53

b.in
10 5
10 1 1
10 2 2
10 3 3
10 4 4
1 1 1
10 5
1 1 1
3 1 2
6 3 3
10 4 4
1 1 1

b.out
5

11

B. Cassettes rent
-
. ,
pi ri , ,
, .
, ,
, pi ri .
, -
.

: , 1
5k , k . k 1 ( ),
.
- O(kn2 ), n - , k -
.
. pi ri .
: m - ( ), pi - , i ,
, sri - , . pi ri -
, .
, .

54


, , 14-20 2008.

C. Minimal segment
:
:
:
:

c.in
c.out
1
256

The task is to find the minimal length of segment in a sequence, such that
the sum of the numbers in the segment is not less than some m.


Input file begins with m (1 m 2 109 ) - the minimal required sum
for the segment. After a space goes n(1 n 100000) - the length of the
sequence. Then, beginning from the new line, there are the numbers of the
sequence a1 , a2 , . . . , an , divided by single spaces. All numbers in the input are
integer, and |a1 | + |a2 | + . . . + |an | 2 109 .


You must output the minimal length of the needed segment. If it doesnt
exist, output n + 1.

c.in
5 6
1 3-1 2 1-3
2 5
1-1 1-1 1

c.out
4
6

C. Minimal segment
a1 , a2 , . . . , an b0 , b1 , . . . , bn , b0 = 0, bk = a1 + a2 + . . . + ak (1 k n).
i, j , bj bi m, i < j , ji . , j
i, k Z(i < k < j): bi < bk .
, bk bi (i < k < j), k i . bi i !
i j , O(log(n)). bi :
bj c ,

, , 14-20 2008.

55

bj , bj . : 1 , 1 .
- O(n).
- O(n log(n)).

D. Convex graphic
:
:
:
:

d.in
d.out
1
256

Beavis likes to make experiments. He wants to verify his claim, that his
strength depends on the quantity of drunk beer as convex-to-up function.
Function f is convex-to-up, if for each triple x1 , x2 , x3 , such that x1 < x2 < x3
and f is definite on x1 , x2 , x3 , we have:
(f (x2 ) f (x1 ))/(x2 x1 ) (f (x3 ) f (x1 ))/(x3 x1 ).
In other words, the point (x2 , f (x2 )) lies not under the line, connecting
(x1 , f (x1 )) and (x3 , f (x3 )).
Maybe, Beaviss claim is rather fool. Maybe, the drunk beer makes his
measurements not very precise... :) But Beavis wants to present his claim as
true. In connecting with this, he wants to throw out some of the results in
such a way, that the remained results make a convex-to-up function. To make
his results more true-like, he wants to leave as much results as possible. The
task is to determine, what is the maximal quantity of results, that Beavis can
leave.


Input file begins with n(1 n 1000) - the number of points. Then follow
n lines, each of which contains numbers xi , yi , (0 xi , yi 30000) separated
by a space - the quantity of beer and the strength in ith experiment. All
numbers are integer and all xi s are distinct.


You must output the maximal quantity of points, which could be left to
make the function convex-to-up.

56


, , 14-20 2008.

d.in
1
1000 1000
4
1 1
2 4
3 9
4 16
4
1 1
2 2
3 2
4 4
4
100 1000
500 100
200 2000
300 2500

d.out
1
2

D. Convex graphic
xi , : ,
(xj , yj ) (xi , yi ), 1 j < i. i, j
i.
n 1000.
. ,
(xi , yi ) . ( )
(xj , yj ) (xi , yi )
(xk , yk ) (xi , yi ), , k < i (xk , yk ) (xi , yi )
(xj , yj ) (xi , yi ) .
( ),
(xi , yi ) (xh , yh ) (xj , yj ) (xi , yi ) (xk , yk ) (xi , yi ), . (, ), i
O(n log(n)).

, , 14-20 2008.

57

(xj , yj ) (xi , yi ) O(log(n)).


- O(n2 log(n)).

E. Sticks for quadrilateral


:
:
:
:

e.in
e.out
1
256

You have sticks with lengths 1, 2, . . . , n. You can take any four distinct of
them to construct a quadrilateral. Of course, not every four sticks fit for this.
You must count the number of ways, in which you can take 4 distincts sticks,
that form a non-degenerate quadrilateral.


Input contains an integer n(4 n 1000000).


You must output the number of ways to choose sticks, that form a
quadrilateral. Choosings, which are different only by order of sticks, are
assumed the same.

e.in
4
5
6

e.out
1
5
14

E. Sticks for quadrilateral


n.
. . (a, b, c, d) , , n. , , :
a n + 1, b a, c b, d c, d 0, b + c + d a.
58


, , 14-20 2008.

4 !!! , n d, d -
, . , n,
.
.
n (, 1 100), ,
4 ,
k - . 4
:
. , k = 6.
,
n. , .

F. Bubble sorting
:
:
:
:

f.in
f.out
1
256

Almost all programmers know the method of bubble sorting. If we have


a sequence a1 , a2 , . . . , an , we make some bubble passes. A bubble pass is a
procedure, that regards 1st and 2nd elements, and swaps them (i. e. change
their positions with each other), if the 1st is greater, then it regards the
elements, which occured at 2nd and 3rd after previous swapping, and swaps
them, if the left elements is greater than the right, and so on, ending at
(n 1)th and nth elements. We repeat this procedure for several times, until
the elements become sorted by non decreasing. (We may sort in non increasing
in the same way, but in current problem we mean namely the non decreasing
order.)
Bubble sorting is a very quick method for near sorted data, but for some
other data it is very slow. Lets suppose, that we test the bubble sorting
method in the next way. We take some sequence a1 , a2 , . . . , an and make k
bubble passes with it. Then we output the data for further analysis. The task
in current problem is to predict the result after k bubble passes.

, , 14-20 2008.

59


Input file begins with k(0 k 100000) - the number of bubble passes.
After a space goes n(1 n 100000) - the length of the sequence.
Then, beginning from the new line, there are the numbers of the sequence
a1 , a2 , . . . , an , divided by single spaces (for every i from 1 to n: |ai | 2 109 ).
All numbers in the input are integer.


You must output the minimal the sequence after k bubble passes in the
single line. Numbers must be splitted by single spaces.

f.in
2 5
3 2 5 4 1
4 1
-5
0 5
5 3 4 2 0

f.out
2 3 1 4 5
-5
5 3 4 2 0

F. Bubble sorting
. . , .
ai a1 , a2 , . . . , an j , aj > ai j < i. ,
1,
- -
, .
0, .
, k
( 0, k ), .
O(n log(n)) . .
- O(n log(n)).
60


, , 14-20 2008.

G. Ways of pair-matching
:
:
:
:

g.in
g.out
1
256

There are n boys and n girls, some of who are friends, and some are not.
Teacher wants to arrange the children into pairs in such a way, that in each
pair must be a boy and a girl, which are friends. In how many ways is it
possible to do?


Input file begins with n(1 n 14) - the number of boys and girls. Then
follows the n n matrix, which decribes the friendship between children.
More precisely, the j th element in ith line is equal to 1, if ith boy and j th girl
are friends, and is equal to 0 in opposite case. Numbers in lines are splitted
by single spaces.


You must output the number of pair-matching, in which boy and girls are
friends in any pair.

g.in
2
1
1
3
1
0
0
3
1
1
1

g.out
1

0
1
0
1 1
0 1
0 1
0
1 1
1 1
1 1

G. Ways of pair-matching
||aij || . Sn a1(1) . . . an(n) , Sn -

, , 14-20 2008.

61

(1, 2, . . . , n). , .
- #P -,
N P - .
, . - (Ryser):

P erm||aij || = S{1,2,...,n} (1)n|S| 1in (jS aij )


.

a1j(1) . . . anj(n)
:

P erm||aij || = S1,2,...,n,|S|=[n/2] P erm(||aij ||1i[n/2],jS )P erm(||aij ||[n/2]+1in,j S


/ )
- O(2n n2 ), O(2n n). - O(2n [n/2]!).

62


, , 14-20 2008.

(17.02.08 .)
.

A.
:
:
:
:

a.in
a.out
1
256

... -
,
, , , ,
.
. , !
, -
100 , , -
, ...
24 .
,
. .


,
.

2008
N .


25 pi -
2008 . N M. M
ai bi ( ai
bi ).


, , 14-20 2008.

63


N , 2 .

0 pi 47,
0 N 1000000000(109),
0 M 600,
1 ai , bi 25,
ai 6= bi

a.in
1 1 1 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2
2 2 2

a.out
46.33

1 2
1 4
2 4

B.
:
:
:
:

b.in
b.out
1
256

,
. ,
.
,
, .
,

.
- - John N .
64


, , 14-20 2008.

M , . KJ
, . KJ
,
.
min (N, M) .


N, M - . M
N ai j - ,
i- j- .


- .

1 N, M 100,
1 ai j 1000000(106)

b.in
3 2
1 2 3
7 4 9

b.out
1111

C.
:
:
:
:

c.in
c.out
1
256

. .
John

, , 14-20 2008.

65

. , ,
, .
. ,
, t, . . ,
.
. K -.
.
,
, . . ,
K .


N, M, K - ,
. N vi -
i- . M
ai bi ( ai bi ).


- , 2 .

1 N 100,
0 M 10000,
0 K 1000000000(109),
1 vi 1000000000(109),
1 ai , bi N
66


, , 14-20 2008.

c.in

c.out

2 1 1
2 5
1 2

2.50

D.
:
:
:
:

d.in
d.out
1
256


. , , - . , ,

.
, N M . N = 3 M = 2
, :
, , , , .
.


N, M.

1 N, M 10000

d.in
3 2

, , 14-20 2008.

d.out
5
67

E.
:
:
:
:

e.in
e.out
1
256

, John
, , , ,
, ,
. KJ
?
.
.
, ,
,
, .
, .
. . Z,
X
Y, X + X-Y = Z.
1, 2, 4, 5, 10, ...
KJ
N- .


, N-

1 N 1000000000(109)
68


, , 14-20 2008.

e.in

e.out

13

F.
:
:
:
:

f.in
f.out
1
256

, N N,
. - ,
. KJ , , ,
. , -
.
, .
,
, .
.
K,
K , . , ,
- . .


. N, M -
. M
xi , yi - ,
i-


K. K
- .

, , 14-20 2008.

69

1 N , M 100000(105),
1 x i , yi N

f.in
3
1
2
1
2
2

f.out

5
2
3
2
3
3

3
1 3 5

G.
:
:
:
:

g.in
g.out
1
256

- .

. .
KJ N , . , . ,
,
.

(a 2b) + 1 (a + 1) 2b , a b -
. [1 .. N].

, .. .


N - . N, 0 1 (
)
70


, , 14-20 2008.

1 N 1000000(106)

g.in
7
0100111


, , 14-20 2008.

g.out
2

71

(18.02.08 .)
.
...
. -
.

IOI 1997 1998.


ACM 2000 2001.
TCCC Marathon Match 2007.
.
.


A.
:
:
:
:

a.in
a.out
1
256

.
, ,
. , ,
.
, . ,
.
, , ,
. .
. - ,
.
72


, , 14-20 2008.

- , . -
( ), .
, - , . ,
,
.



N(2 N 100) . N
, xi ,ai , bi
(10000 xi 10000, 10000 ai bi 10000) i- (xi , ai ) (xi , bi ).
. ,
Xi .


- . JV
( ),
. ,
.
, .
,
. .

a.in
2
112
2 3 4
3
112
2 3 4
4 1 2


, , 14-20 2008.

a.out
0
1.5
3.5
1.2490457723982544
2 3 2

73

A.
- .
, , , .
.
. [v1 ][v2 ] - ,
v1 , v2 . - ,
v1 . O(N3), .

B.
:
:
:
:

b.in
b.out
1
256


N 1 . N (
). ,
. , ,
( )
. ( , ).
1 (..
) .
.
, ( , ), . 1
.
, ,
. , .


N (2 N 100).
N ai (1 ai 100000)
( , ).
, .

74


, , 14-20 2008.


,
.

b.in
3 3 2 2
3 2 2 3

b.out
1
2

B.
, . ,
;
.
, , . , O(log n) -
.
.

,
( n/2). ,

, 5, 6, 7, 8, (
n/4). ,
, -
; log n, k-
2k . ,
, ,
,
,
, ,
. - ,
log n ,
.
. , , [1].
, , [2] [3], . , ,

, , 14-20 2008.

75

, [2d ], [2d + 1], ...,


d = log n,. . , (
) ,
2d .
[i] = min [2 i], [2 i + 1], j j - 1, j j + 1
. , a[1] .
,
, . , ; , , .

C.
:
:
:
:

c.in
c.out
1
256

.
, (
, , ),
. , ,
(- ...). ,
,
, , , ,
, .
-
, ,
, . . ,
.


- N (2 N
100000) - . JV
76


, , 14-20 2008.

- xi , yi (109
xi 109 , 109 yi 109 )- , i- .
. x-
- .



- .

c.in
4
1
0
1
0

c.out
4.0

0
1
1
0

C.
. ,
, , .
- , - X. ,
, X - ,
. .
, , 2( - + - ).
, .

D.
:
:
:
:

d.in
d.out
2
256

- . , .
, ,
.
, , , , , - .

, , 14-20 2008.

77

, - , . , ,
, .
, .
, .
, ,
. , 10%
.
, ,
, .


N - , N (1 N 10000). JV ,
,
. - 20 . - ,
, 26
.
.
- , L (1 L 100000).


,
, , 10% .
, . , N0
SOLUTION.

78


, , 14-20 2008.

d.in
4
Doom awsd
Warcraft ahpmsbf
Civilization rmic
Quake awsdzc
adwdqdsaawwzdswaawws
1
Doom awsd
programmacrobeginend

d.out
Doom
Quake

NO SOLUTION

D.
, .
. , ,
.
O(N L) , O(1) ( ,
boolean 1 26, ,
). .
, , ,
, ,
(
), ,
O(N 26 + L)

E.
:
:
:
:

e.in
e.out
1
256

N . , ,
. , - , (
, )
( ).

, , 14-20 2008.

79

, . ,
. , , ,
, ,
, . ,
, .
.
, , ,
. ,
- ,
. ( ,
) -
. , ,
.
,
( ...)
. , .


3 N, (1 N
1000, 0 100000, 1 109 ) - , .
.
b (1 a, b N ) - , .
.
.


- .

e.in
3
1
3
2
1
80

1 2
2
0 1000000
1 2
2

e.out
0
3
1


, , 14-20 2008.

E.
G,
,
, .
. ,
,
. G , ai ,
2 ,..., k . G ,
k - . , , k - ,
. ,
ai , 2 ,..., k .
f ({ai , 2 ,..., k }). k,

f ({a1 , 2 ,..., k }) = a1 a2 ...ak (a1 + a2 + ... + ak )k2


k = 1 . , (1)
k k = k0 + 1.
(k0 + 1) .
S {1, . . . , k0 } -B
, (k0 + 1) .

(ak0 +1 )|s|

ai

iS

. S ,

S P
f ({ai |1 i k0 , i
/ s} { ai })
iS

. :
P
Q
f ({a1 , 2 ,..., k0 +1 }) =
(ak0 +1 )|s|
ai f ({ai |1 i k0 , i
/
S{1,...,k0 }
|S|6=0

iS
/

S P
s} { ai })
iS
/


, , 14-20 2008.

81

, |S|, :
P
Q
f ({a1 , 2 ,..., k0 +1 }) =
((ak0 +1 )|s|
ai f ({ai |1 i k0 , i
/
S{1,...,k0 }|S|6=0
iS
/
S P
s} { ai })) =
iS
/

k0
P

((ak0 +1 )l

k0
P

((ak0 +1 )l

S P
ai f ({ai |1 i k0 , i
/ s} { ai }))) =

k0
Q

iS
/

S{1,...,k0 }|S|=l i=1

l=1
k0
P

S{1,...,k0 }|S|=l iS
/

l=1

k0
Q

((ak0 +1 )

P
P

k0
P
ai ( ai )k0 l1 )) =

iS

i=l

k0
P
( ai )k0 l1

i=1

l=1

ai )

S{1,...,k0 }|S|=l iS

i=l

ai

S{1,...,k0 }|S|=l iS

1, 2, . . . , ak0 Ckl 0 l ,
l/k

i Ck0 l = Ckl1
.
0 1
P

ai =

S{1,...,k0 }|S|=l iS

Ckl1
0 1

k0
P

ai

i=1

f ({a1 , 2 ,..., k0 +1 }) =

k0
P
l=1

kQ
0 +1
i=1
kQ
0 +1
i=1

ai

(Ckl1
(ak+1 )
0 1

kP
0 1

k0
P

i=l

i=1

(Ckl 0 1 (ak+1 )l (

kP
0 +1

ai (

i=1

k0
Q
i=1

ai (

k0
P

ai )k0 1 ) =

i=1

)k0 l1 ) =

ai )k0 1

- .
(1) k = k0 + 1, , .
k.
82


, , 14-20 2008.

, (1),
G. ,
, .

F. -2
:
:
:
:

f.in
f.out
1
256

, , . ,
. ,
. , ,
. , , 1. ,
. , ,
. , ,
, .
,
.


, .
, .
1.
N (1 N 100000, 0
100000). -
b (1 a, b N ) - .
.



.
. ,
.


, , 14-20 2008.

83

f.in
2 0
2 1
2 1
3 2
2 1
2 3

f.out
2
1 2
1
2
2
2 3

F. -2
, , -
. .
, ,
, .
, .
- , N - - : + 1, + 2,..., N . :
1.
. head[num] -
, next [num] - . inl [num] -
.
void insert(int num, int f, int s) {
next[num][inl[num]] = head[num][f];
head[num][f] = inl[num];
end[num][inl[num]] = s;
inl[num]++;
}
...
for(i=0; i < m; i++) {
scanf(%d%d, &a, &b);
a, b;
84


, , 14-20 2008.

insert(0, a, b);
insert(1, b, a);
}
2. ,
. - ,
. ,
, .
int dfs(int v, int num) { int r = 1, e; mark[num][v] = 1;
for( e = head[num][v]; e != -1; e = next[num][e] )
if( ! mark[num] [end[num][e]] )
r += dfs(end[num][e], num);
return r;
}
...
a = dfs(0, 0)-l;
b = dfs(0, 0-1);
3. :
printf(%d\n, n-a-b);
for(i = 0; i < n-a-b; i ++)
printf(%,d, b+i+1);
:
#include <stdio.h>
#include <memory.h>
#define maxn 100000
int n,m;
int head[2][maxn], next[2][maxn], end[2][maxn],inl[2];
int mark[2][maxn];
void insert(int num, int f, int s){
next[num][inl[num]] = head[num][f];
head[num][f] = inl[num];
end[num][inl[num]] = s; inl[num]++;
}
int dfs(int v, int num)
int r = 1, e;

, , 14-20 2008.

85

mark[num][v] = 1;
for( e = head[num][v]; e != -1; e = next[num][e])
if(!mark[num][end[num][e]])
r += dfs(end[num][e],num);
return r; }
int main(void) {
int i, a, b;
freopen(roads2.in, r, stdin);
freopen(roads2.out, w, stdout);
memset(head, -1, sizeof(head));
scanf(%d%d, &n, &m);
for( i = 0; i < m; i++ ) {
scanf (%d%d, &a, &b);
a, b;
insert(0, a, b);
insert(1, b, a);
}
a = dfs(O, 0)-l;
b = dfs(O, 1)-1;
printf(%d\n, n-a-b);
for( i = 0; i < n-a-b; i++ )
printf(%d, b+i+1);
return 0;
}

G.
:
:
:
:

g.in
g.out
1
256

, ,
. n , . 1 n.
, n
,
.

86


, , 14-20 2008.

, , . k .
: k
.

. , .
, m.


n, k m
(1 n 200000), (1 k 106 ) (2 m
109 ).
n ai (0 ai 109 ), ai - ,
i.


- ,
, m.

g.in
3
1
5
1

1
2
2
2

1000
3
29
3 4 5

g.out
6
1

G.
0.
,
:

{0, mod n, 2p mod n, mod n, ..., (n 1) mod n}


n, n ,

, , 14-20 2008.

87

, , .
: n = k , {0, , 2, , ..., (n 1)},
{0, , 2, , ..., (k 1)}
, n .
200,000 100,000, ,
.
:
1. n.
sq = ((int)sqrt(n 1.0)) + 1;
f or(i = 2; i <= sq; i + +) {
if (not_prime[i])continue;
f or(j = i i; j <= n; j+ = i)not_prime[j] = 1;
}
}
2. .
f or(number_of _prime = 0, i = 2; i <= n; i + +)
if (!not_prime[i])
prime[number_of _prime + +] = i;
3. m.
f or(i = 0; i < n; i + +)
sum+ = a[i];
sum% = m;
4. n.

H.
:
:
:
:

h.in
h.out
1
256

,
.
,
.
88


, , 14-20 2008.

, ,
. , , , ,
-,
.
, , , ,
- , - ,
-
. ,
.
, , , ,
,
.
- , , .


, - .
d m s c - , ,
. 0 d 90, 0 m 59, 0 s 59, - N
S ; 90
.
d m s c - , ,
. 0 d 180, 0 m 59, 0 s 59, - W
;
180 .
,
.



. .
, .


, , 14-20 2008.

89

20
20
12
23
50
50

0 0 S
0 0 S
34 56
45 53
0 0 N
0 0 N

h.in
10 0 0
110 0 0 W
N 14 24 45 W
N 15 21 34 W
90 0 0 E
30 0 0 W

h.out
20 0 0 S 10 0 0 E
23 45 53 N 15 21 34 W
67 14 22 N 30 0 0 E

H.
1
. A(xi , yi , zi ) - , B(x2 , y2 , z2 ) - .
, , .
, .
, , . .
.
,
ax + by + cz = 0, = 1 z2 z1 y2 , b = x2 z1 x1 z2 , = x1 y2
x2 y1 . S.
Oz, ,
, S.
,
. , .
, , , ,
.

L(x, y, z) = z /2(x2 + y 2 + z 2 1) + (ax + by + cz).


(, , z) L ,
:

90


, , 14-20 2008.

+ = 0
+ b = 0
1 z + = 0

2 + 2 + z 2 = 1

+ by + cz = 0
, z ,

= a2 +bc2 +c2
( , , 2 +
b2 + 2 > 0.) ,

a +b
2 = (a2 +b
2 +c2 )2

, z. ,
z = (c + 1)/
. ,
. ,
, , .

I.
:
:
:
:

i.in
i.out
1
256

. -
, .
m n ,
R .
, (dx, dy),

, , 14-20 2008.

91

L, ,
.
, : ,
.


m, n, , R,
L, (5 m 1000, 5 n 1000, 1 100, 1 R 20, 1 L 1000,
1 ). dx dy, 100, ,
. , , .
.
i yi (0 i m, 0 yi n).
, .


, , :
BOARD, .
TOUCH, .
STOP, ( L ).
, .

92


, , 14-20 2008.

i.in
15 15 2 1 10 1
1 1
2 2
11 11
15 15 2 1 11 1
1 1
2 2
11 11
15 15 2 1 10 1
1 -1
2 2
11 11
15 15 2 1 10 1
1 0
2 2
6 4

i.out
STOP

TOUCH

BOARD

TOUCH

I.
. (dx, dy). , .
, ,
, , . .
, , , L , STOP. BOARD
TOUCH , .
, , . , ,
= r, = m r, = r = n r, a
.
, .
,
. a x + b = ,
= dy , b = dx, = a x0 + b 0 , = (XQ , 0 ) -
. d

, , 14-20 2008.

93

p
2
2
)c|
= (x1 , y1 ) |(ax +by
,

v
=
dx2 + dy 2 - v
. , 2 ,
. , . t,
,
(dx, dy) (1 X0 , y1 y0 ),
v. , ,
D,
. ,
ABCD
p
p
2
2
, |CD| = |BD| |BC| = (2 )2 (d v)2 . , t |CD|/v .
, -, L/v
,
L.

J.
:
:
:
:

j.in
j.out
2
256

(n+1)(n+1), (0, 0) (n, n) ( n=2):


0
0 (0,0)
1 (0,1)
2 (0,2)

1
(1,0)
(1,1)
(1,2)

2
(2,0)
(2,1)
(2,2)

-10
10. ,
( ) ,
, .. Cxy = (Cx1,y + Cx+1,y + Cx,y+1 +
Cx,y1 )/4.
,
1
2
5
6
94

2
3
4
5

5
4
3
2

6
5
2
1

, , 14-20 2008.

. ,
(, ) .


n (2 n 50).
, (0, 0). , n = 2
: (0,0) (1,0) (2,0) (2,1) (2,2) (1,2) (0,2)
(0,1). / .


,
n + 1 . .
, .
.
.

j.in
3
1 2 5 6
5 2
1 2 5 6

5 2

1.00000
5.00000
2.00000
4.00000
5.00000
3.00000
6.00000
2.00000

j.out
2.00000
6.00000
3.00000
5.00000
4.00000
2.00000
5.00000
1.00000

J.
,
. , , .
, ,
,
.

, , 14-20 2008.

95

, .
; , , .
, ,
- - ; ,
, , . ,
,
.
, ,
,
.
,
,
. ,
- ,
.

, .
, ; , , .

K.
:
:
:
:

k.in
k.out
1
256

.
,
. N, 1 1,
, .
.
96


, , 14-20 2008.

,
;
, .
. - ,
. - -
, ,
. , -
, ,
. .
, . -, ,
, . , ,
, -
, , ,
. -,
, , .
,
,
, ,
.


M N(l M
100,l N 100) - . N
, ; i- j-.
, (i, j). :
- .
V - .
N - .
- -.
.
. () - .
,
( ) .


L - S
- - .
L - .

, , 14-20 2008.

97

( - , S - , W - )
, .
S ,
. ,
- 1.

k.in
4 3
.T.N
NVNP
.NVN
6 1
T.P..V
4 3
.. .
NNNN
. .V.
3 4
T.V
.VN
PPP

k.out
5 2
WSSEE

-1
9 4
EESWWWSEE

2 0
SE

K.
.
[, ] - , . - ,
. , (
). , ,
. .

98


, , 14-20 2008.

(19.02.08 .)
.
...

.
.
, - . NEERC,
, -
,
- .
A.
(www.lksh.ru).
(www.amse.ru). NEERC, ,
, 30 .
:

2 NEERC 2000;
4 ( ) 2000;
3 ( ) 2001;
6 TopCoder Open 2006;
4 TopCoder Collegiate Challenge 2006;
3 Google Code Jam 2006;
:

NEERC 2001, 2003, 2004, 2007 ;



, , 14-20 2008.

99

2 NEERC 2005, 2006;


2004 ;
2003, 2005, 2007 .


A. Brackets
:
:
:
:

a.in
a.out
2
256

Regular brackets sequence is the sequence of 2n characters each of which


is either ((opening bracket), or ) (closing bracket), such that each prefix
contains no more closing brackets than opening ones, and the whole sequence
contains n opening and n closing brackets. For example, , ((())), ()()()(),
(0)0 are the regular brackets sequences, but ())(() is not, because its prefix
()) contains more closing than opening brackets, neither is ((, because it
contains more opening brackets than closing.
For each opening bracket you can find the corresponding closing bracket the one following it, such that there is a regular brackets sequence between
them.
The generalization of the regular brackets sequence if the regular brackets
sequence with types of brackets. Let each bracket have its type - an integer
number from 1 to k . The sequence of opening and closing brackets with types
is regular if it is a regular brackets sequence and the corresponding closing
bracket for each opening bracket has the same type as the opening bracket
itself. For example, (1 (2 )2 )1 (1 )1 is the regular brackets sequence with two
types of brackets, but (1 (2 )1 )2 (1 )1 is not.
If you introduce some order on 2k typed brackets, you can consider lexicographical order on all regular brackets sequences of length 2n. You are given
n, k , the order on typed brackets and the regular brackets sequence. Find the
next regular brackets sequence in the lexicographical order.


The first line of the input file contains t the number of tests (1 t
10000). The description of t test cases follows.
The first line of each description contains two integer numbers: n and
(1 n 100000, 1 10000). The second line contains 2k integer numbers
100


, , 14-20 2008.

the permutation of the set {-k ,-(k - 1), , -2, -1,1,2, , k }. It lists the
brackets from the least to the greatest in the given order. Positive number i
means the opening bracket of the i-th type, negative number i means the
closing bracket of the i-th type. The third line contains 2n integer numbers
from to (except 0) and describes the regular brackets sequence.
The sum of n and the sum of in all test cases dont exceed 100 000.


For each test case output 2n integer numbers the next regular brackets
sequence after the one from the input file in lexicographical order. If the
sequence in the input file is the last one, output the first one.

a.in
2
3
1
1
3
1
1

2
-1 2
2 -2
2
-1 2
2 -2

a.out
1 2 -2 -1 2 -2
1 2 -2 2 -2 -1

-2
-1 1 -1
-2
-1 2 -2

A. Brackets
.
, ,
, . .
.
.
.

, ,
. ,
( ).

, , 14-20 2008.

101

,
,
,
( ). ,
, . , ,
.
, .
,
. , , : , , .
, ,
( ,
, , ).

B. Car Wash
:
:
:
:

b.in
b.out
2
256

Ben is the owner of a car wash. Ben offers car washing and dry-cleaning
of the car compartment. His wash is located in the capital of England, and he
often serves clients from government. Recently Ben has received an order of
washing and dry-cleaning of n cars. And this order must be executed as fast
as possible!
After preliminary investigation, Ben found out that the i-th car can be
washed in ai , minutes and cleaned in bi , minutes. Washing and cleaning are
performed at different buildings, so they cannot be performed simul-taneously
for the same car. The order of the two operations for a car is irrelevant, neither
is the order of processing the cars. Bens best workers Tom and Jerry were
called to perform the order. Tom will wash the cars, and Jerry will dry-clean
their compartments. Each worker can work with one car at a moment, and
due to quality requirements, it is not allowed to switch from one car to another

102


, , 14-20 2008.

until the one is ready (this condition is independent for each worker, different
workers can process cars in any order).
Help Ben to find out how the cars should be processed by the workers, so
that all cars were finished as soon as possible.


The first line of the input file contains n (1 < n < 10 000). The following
n lines contain two integer numbers each: ai , and bi (1 ai ,bi 105 ).


The first line of the output file must contain t the number of minutes
after the process has started when all cars will be washed and dry-cleaned.
You must minimize this number.
The following n lines must contain two integer numbers each the number
of minutes after the start of the process when the washing and the dry-cleaning
of the corresponding car must start, respectively.
If there are several optimal solutions, output any one.

b.in

b.out

6
10 6
7 9
3 8
1 2
12 7
6 6

39
11 26
4 17
0 7
3 15
27 0
21 32

B. Car Wash
, , n
n
P
P

ai
bi maxni=1 (ai + bi ). ,
i = 1

i = 1

, .
P (ai + bi ) i, , ai
bj , i-
j6=1
P
, , ,bi
aj ,
j6=1


, , 14-20 2008.

103

i- .
. n
n
P
P
bi ( ). ,
ai

i = 1

i = 1

n
P

ai .

i = 1

, .
: , , , , ,
. ,
, , ,
. , (
i ). ,
b2 > a1 , b2 + b3 > a1 + a2 . , ,
, - , . ,
( ) b3 > a2 , b3 + b1 > a2 + a3 .
, , , , b1 > a3 , b1 + b2 > a3 + a1 .
(
, ai bi ).
.
. .
:
ai + bi aj + bj , ,
ai + aj bi + bj . ,
n
n
P
P
,
ai
bi ,

maxni=1 (ai + bi )

n
P

i = 1

i = 1

ai . ,

i = 1

, .

C. Painting Cottages
:
:
:
:

c.in
c.out
2c
256

The new cottage settlement is organized near the capital of Flatland. The
104


, , 14-20 2008.

construction company that is building the settlement has decided to paint


some cottages pink and others light blue. However, they cannot decide
which cottages must be painted which color.
The director of the company claims that the painting is nice if there is at
least one pink cottage, at least one light blue cottage, and it is possible to
draw a straight line in such a way that pink cottages are at one side of the
line, and light blue cottages are at the other side of the line (and no cottage
is on the line itself). The main architect objects that there are several possible
nice paintings.
Help them to find out how many different nice paintings are there.


The first line of the input file contains n the number of the cottages
(1 n 300). The following n lines contain the coordinates of the cottages
each line contains two integer numbers xi and yi (104 xi , yi 104).


Output one integer number the number of different nice paintings of the
cottages.

c.in
4
0
1
1
0

c.out
12

0
0
1
1

The possible nice paintings are shown on the following picture.


, , 14-20 2008.

105

C. Painting Cottages
, . ,
, .
, . , ,
,
. ,
, . ,
, ,
. .
, (
). ,
, ,
, .
x 2 2/2 = 2x.

D. Painting Cottages
:
:
:
:

d.in
d.out
2
256

A group of students from Cooking University living in the campus decided


that each day of the semester one of them will prepare the dinner for the whole
company. The semester lasts for n days.
In sake of fairness they decided that each of the students must prepare the
dinner at least once during the semester. Now they wonder how many ways
are there to plan the semester to decide for each day which student would
make a dinner that day. Help them to find that out.


The input file contains two integer numbers and n (1 k n 100).

106


, , 14-20 2008.


Output one number the number of ways.

d.in
2 3

d.out
6

There are six ways: (1,1,2), (1,2,1), (2,1,1), (1,2,2), (2,1,2), (2,2,1).

D. Painting Cottages
. a[i][j] i, j
. [0][0] = 1, i > 0 a[i][j] = ja[i -1][j] + (k - j
+ l)a[i - l][j - 1]. ,
i , (j ),
, .
, , ,
.

E. Painting Cottages
:
:
:
:

e.in
e.out
2
256

Jim is writing financial software. He specializes in various financial


instruments. The last financial instrument he is writing is history analysis
of arbitrage with highly volatile stock.
The idea of the analysis is the following: given a quote of the stock for n
consecutive time periods, select a subsequence of the quotes such that any
two selected quotes that are adjacent in the selected subsequence are at least
points apart. For example, if the quotes are 1014, 1024, 1034, 1045, 1030,
998 and k = 15, the valid subsequence is 1014, 1034, 998.
The selected subsequence must be as long as possible. In the example above
the selected subsequence is not optimal, selecting 1014, 1045, 1030, 998 is


, , 14-20 2008.

107

better. Given a sequence of quotes, find the longest valid subsequence that
can be selected.


The first line of the input file contains n(1 n 100000) the number
of quotes, and k(1 k 109). The second line contains n integer numbers
the given sequence of quotes (all quotes are between 1 and 109, inclusive).


The first line of the output file must contain l the length of the optimal
subsequence. The second line must contain l integer numbers the elements
of the subsequence.

e.in
6 15
1014 1024 1034 1045 1030
998

e.out
4
1014 1045 1030 998

E. Painting Cottages
[1], [2], , [n].
, , (n2 ). , l[i] , - . l[i] =
max{l[j]|j < i, a[j] a[i] k a[j] a[i] + k} + 1.
0.
, l[i].
.
, .
, ,
. l[i]
O(log n) . a[j], A[i]-k.

108


, , 14-20 2008.

, a[i], a[i] ,
.
, a[i], a[i] .
l[i] , , .
O(n log n).

F. Minima
:
:
:
:

f.in
f.out
3
256

You are given an array x[1 n] and a number m. For all i from 1 to n m
+ 1 find the minimum among x[i], x[i + 1],. . . , x[i + m - 1] and return the
sum of those minima.


The first line of the input file contains three integer numbers: n, m and k
(1 n 30 000 000, 1 m n, 2 k min(n, 1000)). The second line of
the input file contains three integer numbers: a, b and (231 a,b,c 231 1). The third line of the input file contains integer numbers: x[1],x[2],...,x[k]
(231 x[i] 231 1).

f.in

f.out

10 3 2
1 1 0
0 1

33

1000000 15 5
283471207 23947205 3
17625384 939393931 1838388
912740247 290470294

-1879262596173354


, , 14-20 2008.

109

The rest of the array is calculated using the following formula: x[i] =
f(a*x[i - 2] + b*x[i - 1] + c). Here f(y) returns such number 231 z 231
- 1 that - z is divisible by 232 .


Print one integer number the sum of minima of all subarrays of length
m of the given array.

F. Minima
:
, :
, .
,
(1).
. .
(1) :
, i
.

,
,
.
,
(1).
.
. .
: p1 ,
p2 p1 -ro, p3
p2 -, . .
.
,
, . ,
, , , . ,
110


, , 14-20 2008.

, . ,
(1), ,
,
(1).

G. Move to Front
:
:
:
:

g.in
g.out
2
256

Move-to-Front is a method of transforming sequences of positive integer


numbers, that is used in some compression algorithms, such as BurrowsWheeler transform.
Initially all positive integer numbers are organized as an ordered list L
in their natural order. Consider a sequence a1 , a2 , . . . , an of positive integer
numbers. It is encoded as a sequence b1 , b2 , . . . , bn in the following way. Let
the part of the sequence from a1 to ai1 be encoded. The position of ai in
the current list L is considered. It is assigned to bi , and ai is moved to the
beginning of the list L. For example, the sequence 3, 3, 3, 2, 2, 2, 2, 2, 3, 1,
3, 3, 2 is encoded as 3, 1, 1, 3, 1, 1, 1, 1, 2, 3, 2, 1, 3.
You are given a sequence a1 , a2 , . . . , an , you must encode it using Moveto-Front method, and output the resulting sequence b1 , b2 , , bn .


The first line of the input file contains integer number n(1 n 100000).
The second line contains n integer numbers ai , ranging from 1 to 109 .


Output n integer numbers the sequence b1 , b2 , . . . , bn .

g.in
13 3 3 3 2 2 2 2 2 3 1 3 3
2


, , 14-20 2008.

g.out
3 1 1 3 1 1 1 1 2 3 2 1 3

111

G. Move to Front
.
: ,
.
.
x, . x + a a ,
, . , , .
,
. .
. , , ( ), . , (, -).
( ),
.
. . ,
. (0 1) .
.
, ( ). 0 (
) (
), 1.
, .

112


, , 14-20 2008.

H. TV Show
:
:
:
:

h.in
h.out
2
256

Recently various TV shows started to gain popularity among young people


who like to test their luck. One such show is running on ZhTV channel in
China.
The show proceeds as follows. There is a special game board which has
the form of a rectangle with n rows and n columns. The cells on the main
diagonal of the board contain one hieroglyph each. There are also m phrases
each of which contains n hieroglyphs. For each i there are at most two phrases
such that their i-th hieroglyph is equal to the hieroglyph written in the i-th
row of the board.
The player has to put phrases to the rows of the board in such way
that if the phrase is put to the i-th row, the i-th hieroglyph of the phrase
must coincide with the hieroglyph written in the corresponding cell. Also
hieroglyphs in each column must be distinct.
For each phrase put to the board the player gets some small prize, and she
gets a super prize if she puts a phrase to every row of the board. Help the
player to determine whether she can win the super prize.


We will denote hieroglyphs with integer numbers from 1 to 109.
The first line of the input file contains n the number of rows on the
board (1 n 200). The next line contains n integer numbers hieroglyphs
written on the diagonal.
The third line contains an integer number m(n m 2n) followed
by m lines. Each line contains n integer numbers hieroglyphs of the
corresponding phrase. For each i there are at most two phrases such that
their i-th hieroglyph is equal to the hieroglyph written in the i-th row of the
board.


If the super prize can be won, print YES at the first line of the output

, , 14-20 2008.

113

file. The second line must contain n integer numbers the phrases to put to
the board, from top to bottom. Phrases are numbered from 1 to m in order
they are given in the input file.
If the super prize cannot be won, print NO at the first line of the output
file.

h.in
4
1
7
1
1
2
3
2
3
5
4
1
7
1
1
2
3
2
3
5

2 3 4
5
5
3
2
2
3
4

2
4
3
4
2
3
5

h.out
YES
2 5 6 7

5
1
1
1
2
3
4
NO

2 3 4
5
3
3
2
2
3
4

2
4
3
4
2
3
5

5
1
1
1
2
3
4

H. TV Show
,
2-.
, . ,
-
.
, ,
114


, , 14-20 2008.

, ,
.
, , .
.
xi , xi = 0 , xi = 1 . ,
,
.
xi xj ( , , xi x
j , . .)
,
2-,
.
, , 2-SAT Google
Wikipedia.

I. Hard Test
:
:
:
:

i.in
i.out
2
256

Andrew is having a hard time preparing his 239-th contest for


Petrozavodsk. This time the solution to the problem is based on Dijkstra
algorithm and Andrew wants to prepare the hard test for the algorithm.
The Dijkstra algorithm is used to find the shortest path from a source
vertex to all other vertices in a graph. The algorithm acts as follows. Let G
be a weight directed graph with vertex set V , edge set E and weight function
w : E R+ . Let all vertices be reachable from vertex s. The algorithm uses a
set of vertices U , first initialized as empty. Each vertex is labeled with either
an integer number, or with +. Initially all vertices are labeled with +,
and the vertex s is labeled with 0. Denote the label of vertex v as d[v].
A step of the algorithm is the following: the vertex with the minimal label
that doesnt belong to U is selected. Let this vertex be u. The vertex u is added
to the set U , and each edge uv 6 E is relaxed. The relaxation replaces d[v]
with min(d[v], d[u] +w(uv)). The algorithm is over when all vertices belong

, , 14-20 2008.

115

to U . If the label of the vertex v has changed, the relaxation is said to be


active.
Now Andrew would like to create a graph with n vertices and m edges,
such that the Dijkstra algorithm makes as many active relaxations as possible.
Help him to create such graph. To avoid nondeterminism, each time when
selecting a vertex with minimal label among vertices that are not in U there
must be exactly one vertex with the minimal label.


The first line of the input file contains two integer numbers: n and m
the number of vertices and the number of edges in the graph Andrew would
like to create (4 n 1000, n 1 m n2/5 ).


Output m lines the edges of the graph. Each line must contain three
integer numbers: the beginning of the edge, the end of the edge and the weight
of the edge. All weights must be non-negative and must not exceed 106 . All
vertices must be reachable from vertex 1. If Dijkstra algorithm is run with s
= 1 there must be maximal possible number of active relaxations among all
graphs with n vertices and m edges. There must be no loops and no parallel
edges.

i.in
4 3

5 5

i.out
1
1
1
1
1
2
3
1

2
3
4
2
3
4
4
5

0
1
2
0
1
4
2
2

I. Hard Test
. , : i j , j > i
j i 1. ,
.
116


, , 14-20 2008.

n(n 1)/2 m .

J. Travel Agency
:
:
:
:

j.in
j.out
2
256

Anthony is working in the intergalaxy travel agency. He often meets the


requests to find a path from one planet to another using available spaceship
routes. Unfortunately, spaceships have only limited number of routes, so
passengers usually have to make connections at intermediate planets.
Anthony noticed that some planets are used as intermediate more often
than other. He decided to make an investigation for each planet A he would
like to know how many pairs of distinct planets (B, C ) are there, such that
any path from planet to planet visits planet A. Help him!


The first line of the input file contains two integer numbers: n and m
the number of planets and the number of spaceship routes, respectively
(2 n 20000, 1 m 200000). The following m lines describe spaceship
routes. Each route is bidirectional, and is described with the numbers of
planets it connects. It is possible to get from any planet to any other.


Output n integer numbers for each planet A output the number of pairs
of distinct planets such that any path from one to another goes through A.


, , 14-20 2008.

117

j.in
7
1
1
1
1
1
1
2
4
6

9
2
3
4
5
6
7
3
5
7

j.out
18
6
6
6
6
6
6

J. Travel Agency
. , , , P
s1 , s2 , . . . , sk ,
S0 = n Si 1,
i
P
Si , Sj .
0i<jk

, ((S0 + S1 + + SK )2 (S02 + S12 +


2
+ SK
))/2. ,
(n).

. (., , ) - .
, , n 1,

( , , , , ,
).

118


, , 14-20 2008.

Оценить