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

,

,
2013


. Akai . . . . . . . . . . . . . . . . . . . . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A. Automaton . . . . . . . . . . . . . . . . . . . . . . . . . . . .
B. Bims . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C. Cutting . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
D. Disclosure . . . . . . . . . . . . . . . . . . . . . . . . . . . .
E. Embedded circles . . . . . . . . . . . . . . . . . . . . . . . .
F. False figures . . . . . . . . . . . . . . . . . . . . . . . . . . .
G. Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . .
H. Hidden triangles . . . . . . . . . . . . . . . . . . . . . . . .
I. Interactive . . . . . . . . . . . . . . . . . . . . . . . . . . . .
J. Journey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
K. Knuth knows . . . . . . . . . . . . . . . . . . . . . . . . . .
L. Lake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
M. Match them up . . . . . . . . . . . . . . . . . . . . . . . . .
N. Need for sum thing . . . . . . . . . . . . . . . . . . . . . . .
O. Open air . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
P. Pseudo automaton . . . . . . . . . . . . . . . . . . . . . . . .
Q. Quiz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
R. Reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . .
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A. . . . . . . . . . . . . . . . . . . . . .
B. 1 . . . . . . . . . . . . . . .
C. . . . . .
D. . . . . . . . . . . . . . . . . . . . . .
E. . . . . . . . . . . . . . . . . . . . . . . . .
F. . . . . . . . . . . . . . . . . . . . . .
G. . . . . . . . . . . . . . . . . . . . . . . . .
H. . . . . . . . . . . . . . . . . . . . . .
I. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
J. . . . . . . . . . . . . . . . . . . .
K. . . . . . . . . . . . . . . . . . . .
L. . . . . . . . . . . . . . . . . . . . . . . . . .
M. . . . . . . . . . . . . . . . . . . . . . . .

, , 15-25 2013

8
8
9
13
13
15
17
19
20
22
24
26
28
30
31
33
34
36
38
40
41
44
45
45
45
50
50
51
52
53
55
57
58
61
62
63
65
66
69
3

. . . . . . . . . . . . . . . . . . 72
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
. STL (, , , ) . . . . 72
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
A. DOMA 2: Last Hit . . . . . . . . . . . . . . . . . . . . . . .
84
B. Last Effect 3: Danger . . . . . . . . . . . . . . . . . . . . . .
86
C. Fineage: Training . . . . . . . . . . . . . . . . . . . . . . . .
88
D. DOMA 2: Inventory . . . . . . . . . . . . . . . . . . . . . .
89
E. South Mark: 3.50 . . . . . . . . . . . . . . . . . . . . . . . .
92
F. HOLM 2: The Great Battle . . . . . . . . . . . . . . . . . . .
94
G. Failout Few Vegas: Slow Save . . . . . . . . . . . . . . . . .
97
H. Carmarandom TDC2013: New Trace . . . . . . . . . . . . .
99
I. X-Dom: Railway . . . . . . . . . . . . . . . . . . . . . . . . .
101
J. MindCraft: Heliport . . . . . . . . . . . . . . . . . . . . . . . 103 K.
Double Life: Amplifiers . . . . . . . . . . . . . . . . . . . . . 105 .
. . . . . 108 ... . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
115
A. High . . . . . . . . . . . . . . .
115
B. Junior . . . . . . . . . . . . . .
116
C. High . . . . . . . . . . . . . . . . . .
121
D. Junior . . . . . . . . . . . . . . . . .
122
E. High . . . . . . . . . . . . . . . . . . . . .
125
F. Junior . . . . . . . . . . . . . . . . . . . .
126
G. - High . . . . . . . . . . . . . . . . . . . .
128
H. - Junior . . . . . . . . . . . . . . . . . . .
129
I. High . . . . . . . . . . . . . . . . . . . . . . .
131
J. Junior . . . . . . . . . . . . . . . . . . . . . .
133
K. High . . . . . . . . . . . . . . . .
136
L. Junior . . . . . . . . . . . . . . .
136
M. High . . . . . . . . . . . . . .
138
N. Junior . . . . . . . . . . . . . .
138
O. High . . . . . . . . . . . . . . . . . . . . .
140
P. Junior . . . . . . . . . . . . . . . . . . . . .
141
Q. High . . . . . . . . . . . . . . . . . . .
142
R. Junior . . . . . . . . . . . . . . . . . .
143
S. High . . . . . . . . . . . . . . . . . . . .
146
T. Junior . . . . . . . . . . . . . . . . . . .
147
4


, , 15-25 2013

. . . . . . . . 149
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
149
. . . . . . 150
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
150
A. . . . . . . . . . . . . . . . . . . . . . . 150
B. . . . . . . . . . . . . . . . . . .
152
C. . . . . . . . . . . . . . . . . . . . . .
154
D. . . . . . . . . . . .
157
E. . . . . . . . . . . . . . . . . . . . . . . . . .
159
. . . . . . . . 162
. C . . . . . . . . . . . . . .
162
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
166
A. - . . . . . . . . . . . . . . . . . . . . . . . .
166
B. - . . . . . . . . . . . . . . . . . . . . . . . . . . . .
168
C. . . . . . . . . . . . . . . . . . . . .
169
D. - . . . . . . . . . . . . . . . . . . . . . . . . . .
171
E. - . . . . . . . . . . . . . . . . . . . .
173
F. . . . . . . . . . . . . . . . . . . . . . .
176
G. . . . . . . . . . . . . . . . . . . . . . . .
179
H. - . . . . . . . . . . . . . . . . . . . . . . . . . . .
181
I. - . . . . . . . . . . . . . . . . . . . . . . . . . . . .
183
J. . . . . . . . . . . . . . . . . . . . . . . .
185
K. . . . . . . . . . . . . . . . .
186
L. . . . . . . . . . . . . . . . . . . . . .
189
M. . . . . . . . . . . . . . . . . . . . . . . .
191
N. . . . . . . . . . . . . . .
192
. K . . . . . . . . . . .
195
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
195
. . . . . . . . . . . . . .
195
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
200
A. Chords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 B.
Cyclic suffixes . . . . . . . . . . . . . . . . . . . . . . . . . . 201 C. A Coloring
Game . . . . . . . . . . . . . . . . . . . . . . . . 203 D.
Hippopotamus . . . . . . . . . . . . . . . . . . . . . . . . . . 204 E. False RSA .
. . . . . . . . . . . . . . . . . . . . . . . . . . . 205 F.
Perspective . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
G. Circular Railway . . . . . . . . . . . . . . . . . . . . . . . .
208
H. SETI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
I. 2-3 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
210
. . . . . . . . 213
... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
213

, , 15-25 2013

. . . . . . . . . . . 214
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
A. . . . . . . . . . . . . . . . . . . . . . . . 216
B. . . . . . . . . . . . . . . . . . . . . . . . . . . 219
C. . . . . . . . . . . . . . . . . . . . . . . 221
D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
E. . . . . . . . . . . . . . . . . . . . . 233
F. Shortest Path . . . . . . . . . . . . . . . . . . . . . . . . . . 235
G. . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
H. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
I. . . . . . . . . . . . . . . . . . . . . . 239
J. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
K. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
. . . . . . . . . . . . . . . . . . . . . . 248
. . . . . . . . . . 250
...... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
A. . . . . . . . . . . . . . . . . . . . . . . . 250
B. . . . . . . . . . . . . . 253
C. . . . . . . . . . . . . . 255
D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
E. . . . . . . . . . . . . . . . . . . . . . 260
. . . . . . 263
...... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
. 20 . . . . 264
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
A. n . . . . . . . . . . . . . . . . . . . . 280
B. . . . . . . . . . . . . . . . . . . . . . . . . . 281
C. . . . . . . . . . . . . . . . . . . . . . . . . . . 283
D. . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
E. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
F. . . . . . . . . . . . . . . . . . . . . . 287
G. . . . . . . . . . . . . . . . . . . . . . . . . 288
H. . . . . . . . . . . . . . . . . . . . . . . 289
I. K . . . . . . . . . . . . . . . . . . . . 291
J. . . . . . . . . . . . . . . . . . . . . . . . . 292
K. . . . . . . . . . . . . . . . . . . . . 293
L. . . . . . . . . . . . . . . . . . . . . . . . . 294
M. . . . . . . . . . . . . . . . . 296
N. . . . . . . . . . . . . . . 297
O. . . . . . . . . . . . . . . 298
6


, , 15-25 2013

P. k- . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Q. . . . . . . . . . . . . . . . . . . . .
R. . . . . . . . . . . . . . . . . . . . . . . . . .


, , 15-25 2013

299
300
301

Akai

(16.02.2013 .)
Akai
...
, 28 1988 .

.
..
-.

-.
-.

:
Akai :
2009 6

SEERC, -.
2010 3 .

2010 4 SEERC
.
2011 1 II
- .
2011 2
SEERC .
2010 2012 1 SEERC
..
Russian Code Cup 2011 2012
. 3 Russian AI Cup 2012.


, , 15-25 2013

Akai

.
-.

N M,
1 1. , .
. ,

: , , , .
-
: (U), (D), (L), (R).
- .
,
-.
- .
, ,
- .

- . : S
= a1a2a3 : : : al, ai 2 fL; R; U; Dg ,
-.
- :
i, 0

(LR, RL, U D, DU). , S = a1a2 : : : ai 1LRai : : : al.

. i
.


ai

i,

0
S = a1a2 : : : ai 1LaiRai+1 : : : al.

ai.
,

. i
- i 1 i + 1.


, , 15-25 2013

Akai


- () ()

, ,
.

.
. , ,
.
,
. , ,
.
10


, , 15-25 2013

Akai

.
,
. , ,
.
. ( ).


-.

- , , ,
- .
:


, , 15-25 2013

11

Akai

(3; 2)
RRRRDDRU U RDDDLDLLLU LDDLU U U U U .
- [ 4; 6; 6; 4; 4; 6; 9; 10; 10; 10; 11; 11; 10; 8; 5;
4].
- , .
,
, ,
,
. ,
, .

, . ,
,
. [ 6; 9; 8; 5].
, , .
, .
-
. , [ 9; 8; 5; 6].
,
,
, .

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

12


, , 15-25 2013

Akai


A. Automaton
:
:
:
:

stdin
stdout
1

256

S. ,
- S (
).
N 2N .
-. 1.

abacaba


S, .


N K
. K ,
ai, bi ci, ai
bi ci. .
, .

1 jSj 105
1 K
2N
1 ai; bi N
a ci z

, , 15-25 2013

13

Akai

stdin
abacaba

stdout
8 10
1 2a
1 3b
1 5c
2 3b
2 5c
3 4a
4 5c
5 6a
6 7b
7 8a

A. Automaton
N S. N + 1 N ,
i i + 1 S[i].
N , ,
.
, , . , 2 N.
, 1 N,
: , ,
, .
.
, ( ), , ,
,
( 1). , 1 (
), ,
. , ,
.
N 1. ,
,
, 1,

14


, , 15-25 2013

Akai

.
, .
, , ,
- ( ,
, ). ,
-, ,
- , .
L R,
,
-, T .


- , ,
. ,
.

, O(N log(N)) ,
.

B. Bims
:
:
:
:

stdin
stdout
1

256

k- k- ,
i ;i2;:::;ik

(i1; i2; :::; ik) Cn 1

, ij 0, n = i1 + i2 + ::: + ik.
:
1- 2 3 ,
i;j;k

Cn .
, k- ,
p (p ). n-
(, n).
.
9
, 10 + 7.


, , 15-25 2013

15

Akai


k, p t. t
ni .


t , ni
ni- ,
9

10 + 7.

1
2
1
0

k
p
t
ni

10

10 + 3, p .
10

10

18

stdin
2 7 4
0
6
7
8
3 7 4
0
6
7
8

stdout
1
7
2
4
1
28
3
9

B. Bims
p . ,
, , , p.
p- . , , 0;0;0:::p:::;0;0;0
, Cp
, p

, .
i ;i2;:::ik

, C n 1
i ;i ;:::i 1
... Cn 1 2 k ( ,

i ;i 1;:::i
k,
Cn 1 2

i 1;i ;:::i

Cn 1

), ,
p-
k p.
16


, , 15-25 2013

Akai

,
p , , k-
p. :

answern = answern

mod p

answern=p

. -
(n mod p)-
(n mod p) k ,
C

k 1

(n mod p)+k 1

C. Cutting
:
:
:
:

stdin
stdout
1

256

S M, N .
S,
M, S.
- S, , .
,
S. -, .


S. N
. N ,
M. .


,
- S.

1
1
1

jSj 100
N 100
jMij
100


, , 15-25 2013

17

Akai

stdin

stdout

abacaba
4
aba
aca
a
b

aca,
abba. b,
aba. aba.

C. Cutting
.
r[i][j] , , -
S i j. d[i][j][k][l]
, , S i j
l k. :

d[i][j][k][l] = min fd[i][p

1][k][l

1] + r[p + 1][j]g

i p j

, m[k][l] = S[p], m[k][l] l- k-


.

[][j ] = minf

ri

p j

min

1f

[][p ] + [p + 1][]g ; 1

ri

k N

min

[][j ][k ][

di

len(k) k- .
3 P

()]gg

len k

O(jSj
len(i)) O(N ). , 1,
, .

, i len(k), i + len(k) > jSj.


3

N N .

18


, , 15-25 2013

Akai

D. Disclosure
:
:
:
:

stdin
stdout
1

512

N K .
,
- .
0
G G ,
G (u; v) ,
u v G.


N K. K , ai bi, ,
ai bi. , .


, ,
. .

1
0
1

N 50 000
K
50 000
ai; bi N

5
1
2
3
4
1
1

6
2
3
5
5
5
3

stdin


, , 15-25 2013

1
2
3
4

2
3
5
5

stdout

19

Akai

D. Disclosure
(u; v) ,
u v. .
. u S[u] ,
S

u. S[u] = S[vi], vi u. S[u], -


, .
.
S .

N=32 .
O(N K=32)

E. Embedded circles
:
:
:
:

stdin
stdout
1

256

faijg R C , 0
9. Q :
ik jk rk, 1
k Q
aij ,

(i

ik) + (j

jk)

rk .


(ik; jk) rk.
, .
:
,
.
, . ,
k l (k < l) ,
, t: k < t l, t
k. .


R C . R
C ( ).
Q . Q

20


, , 15-25 2013

Akai

: ik, jk, rk,


. 1.


,
.

1 R; C 2 000
0 aij
9
6
1 Q 10
1 + rk ik R rk 1 + rk jk
C rk

0 rk min(R; C) 1 2

stdin
6 6
123456
234567
345678
456789
567890
678901
10
1 1 0
3 3 2
3 2 1
2 2 0
4 2 0
1 3 0
2 3 0
4 3 0
5 5 1
5 5 0

stdout
141

141 = 1 + 65 + 20 + 3 + 5 + 3 + 4 + 6 + 25 + 9


, , 15-25 2013

21

Akai

E. Embedded triangles
O(r i), ri
-.
.
O(1) .
, ,
O(S), S .
f(R) , , , ,
, ,
R.
g(S) , ,


2 2
S. f(R) = 2r + f(r 1) + g( R r )
2
g(S) = 2r + f(r
1) + g(S
r )
r 2
. , f(R) 2 R g(S) 2S.
2
2 2
2
2
2
f(R) 2r
+ 2 (r 1) + 2( R r ) 2 R + (4 4 )r + 2 2 R g(S) 2r + 2 (r 1) +
2
2(S r ) 2S + (4 4 )r + 2 2S

F. False figures
:
:
:
:

stdin
stdout
2
256

.
,
- .
faijg 1 000 1 000 . Q
- :
ik jk rk, 1
k Q
aij ,
2

2
ik) + (j
jk)
rk .

(ik; jk) rk.

(i

22


, , 15-25 2013

Akai

k l (k < l) , k l
, t: k < t l,
k. .
, .


Q . Q
: ik, jk, rk,
.
1.


,
. , . , Ok.

1 Q 106
1 + rk ik 1 000 rk 1 + rk
jk 1 000 rk 0 rk < 500

stdin
6
10 10
5
10 10
4
5 10 0
10 5 0
10 15
0
15 10
0
2
6 6 5
11 11
5

stdout
Ok

F. False figures

, .
. ,
.

, , 15-25 2013

23

Akai

.
-, . -
,
. - , ,

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

O(ri)
- , O(S), S
-. -
, log .

G. Grouping
:
:
:
:

stdin
stdout
1

256

N faig. fbjg K (
), :
N

S=
S
. S.

min jai

i=1

1 j

b jj


N K. N
faig.


S
8
- 10 .
24


, , 15-25 2013

Akai

1
1
0

N
K
ai

5 000

N
400 000

stdin
5 3
1 5 7 10

stdout
5.0

14

fbig f1; 7; 14g.

G. Grouping
,
K b j. ,
.
bj .
[i; j]
b .
: d[i][j]
i j . :

d[i][j] = min d[p

1][j

1] + cost(p; i)

1 p i

cost(p; i) , p i.
3
O(N ). p[i][j] p,
d[i][j].
: p[i 1][j] p[i][j] p[i][j + 1]
, p[i][j] ,
- :
N K
XX

(p[i][j + 1] p[i 1][j]) =

i=1 j=1

K
X

N 1 K

p[i][j] +

=1 j=2

p[i][j] =

i=1 j=1

N 1

K
X

XX

p[N][j]

j=2


, , 15-25 2013

p[i][1]:

i=1

25

Akai

p[i][j] 1 i,
N K. O(N K).

H. Hidden triangles
:
:
:
:

stdin
stdout
4
512

N 1 N.

- , .
, .
,
.


N . N , . xi1; yi1; xi2; yi2; xi3; yi3 .
.
.


.
.

1 N
1 000

500

xij; yij

1 000, 1 i

N; 1

stdin

3
1 0 40 0 3
-2 1 5 -2 3 4
-2 2 4 1 2 4

26

2
2 3

stdout

, , 15-25 2013

Akai

H. Hidden triangles
, ,
.
2

O(N log(N)), O(N ),



O(N log(N)). -


2
O(N log(N)).
,
.
, ,
,
. ,
,
, . ,
,
, ,
-.
set.

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

,
- .


, , 15-25 2013

27

Akai

I. Interactive
:
:
:
:

stdin
stdout
1

256

http://codeforces.ru/blog/entry/4037
Alex_KPR: ... - , FFT ,
...

Kunyavsky: .
FFT?
Alex_KPR: , ,

Kunyavsky: : .
,
-
homo_sapiens:
( )

Kunyavsky: .
- .
homo_sapiens: ...
, .
N fx ig
fyig.
FFT, ,
:
N 1
X

P (z) =
=0
N 1

xiz

Q(z) =
=0

yiz

P (z) Q(z).
, , fxig
, fyig . N (xi; yi).
.
- - .

28


, , 15-25 2013

Akai

, FFT, , , (x i). (yi). ,


,
, , .

.
, ,
prewritten code.


N. N

fxig.
fyig.

FFT, 2N 1
.
, N
.
, , ,
.

1 N 50 000
1 xi 50 000
1 yi 50 000
, 50

stdin
6
4
1
6
4
1

1 2 36
6 3 45

5
2

1 2 36
6 3 45

5
2


, , 15-25 2013

4 25
58 56
0 1 2

stdout
20 34 54
37 10

71 72

2 34

29

Akai

I. Interactive
.
- O(N log(N)).

J. Journey
:
:
:
:

stdin
stdout
1

256

, J-Pax
A, B C. , ,
.
. ,
, .


A, B, C.


,
8
- 10 .

A; B; C

1 000

stdin
1 1 1

stdout
2.2360679774998

J. Journey
, .
- .
.
.
( ).
30


, , 15-25 2013

Akai

A , B 1; B2; B3; B4 -,
. , B 4 -
. , 1; 2; 3; 4; 6
A B4. 5.

5 ( ).
A1; A2; A3; A4
A 5.
5 .
,
d , 5
A1; A2; A3; A4 d.

K. Knuth knows
:
:
:
:

stdin
stdout
1

512

N K .
.
0
G G ,
G (u; v) ,
u v G.
, , ?

, , 15-25 2013

31

Akai


N K. K , ai bi, ,
ai bi. , .



-.

1
0
1

N 50 000
K
50 000
ai; bi N

stdin
5
1
2
3
4
1
1

6
2
3
5
5
5
3

stdout
7

K. Knuth knows
(u; v) , ,
u v. .
u S[u] ,
S

u. S[u] = S[v i] [ vi, vi


u.
S .
N=32 .
O(N K=32).

32


, , 15-25 2013

Akai

L. Lake
:
:
:
:

stdin
stdout
1

256

.
, - (x; y). ,
. -,
d.
, ,
. (x; y)
-.
.


x, y, d.

10 000 x; y 10 000 1
d 10 000

stdin
6 3 2

stdout
4

L. Lake
(x; y) T = k d+l; 0 l < d. l = 0,
k. l > 0. , k .
, k + 1 . k 1
(x; y). d
(x; y).
l, d, 2d + l.
, ,


, , 15-25 2013

33

Akai

d. ,
. , k + 1 .
,
- d,
- .

M. Match them up
:
:
:
:

stdin
stdout
1

256

.
, .
N , N
K .

- M, ,
M.
f(ui; vi)g , ui
, vi .
: u1, v1, u2, v2, ..., um, vm.


- .


N K
. K .
ai bi, ai
, bi .


m.
- m ui, vi , ui
, vi , i-
-.

1
0
34

N
K

10

10


, , 15-25 2013

Akai

ai; bi

3
1
1
3
2
2

5
2
3
2
3
1

stdin

3
1 3
2 1
3 2

stdout

M. Match them up
. .
, .
,
. ,
. ,
.
2, 3 ..
a. ,
. a
- , b
, b , , (a;
b). - b
(a; b), . ,
, .
a , . .
. , a.
, a , . a, .
. , -
a.
, a -
. ,


, , 15-25 2013

35

Akai

, .
, , .

-, .

N. Need for sum thing


:
:
:
:

stdin
stdout
1

256

N .
, ,
.. i- 2i 1 .
. ,
.
, .

a
a

31

21

32

11

22
33

:::

23

34

35

0 9.
Q
. :

ri, ci, ki
i-
- ki (ri; ci):

a
a

ri+ki 1; ci

ri+1; ci

:::

ri+ki 1; ci+1

c
ri;

ri+1; ci+1

:::
:::

ri+1; ci+2

:::

ri+ki 1; ci+2ki 3

ri+ki 1; ci+2ki 2

(ri; ci).

: (ri + 1; ci), (ri + 1; ci + 1), (ri + 1; ci + 2), ..


.
, :
A1 = 1
36


, , 15-25 2013

Akai

Ai = (1 234 567 Ai
ri = Ai mod N + 1

+ 7 654 321) mod 1 000 000 007, 2

ci = Ai

mod (2ri 1) + 1

ki = Ai

mod (N ri + 1)

iQ

+1


N Q
. N . i 2i 1 (
).


,
.

1
0

N
Q

10

5 10

stdin
3 5
1
234
56789

stdout
42

1
234
: 56789
3
: 678, 24.
: 8, 8.
: 6, 6.
: 1,
1. : 3, 3.

N. Need for sum thing


(i; j) : sl(i; j), sr(i; j) s(i; j) .


, , 15-25 2013

37

Akai

sl(i; j) , sr(i; j)
, s(i; j) .
, i; j; k, :
ans = sl(i; j) sl(i + k; j) + sr(i + 1; j + 2) sr(i + k; j + 2k) + s(i + k; j + 2k 1)
s(i + k; j).

O. Open air
:
:
:
:

stdin
stdout
1

256

,
0 N (N ). fa; bg . ,
(N + 1)(N + 2)

.

38


, , 15-25 2013

Akai

N + 1
, ,
, . , .


N.


N + 1 N + 2 : a1; b1; a2; b2; : : : ; ak; bk,
N+2

k=
2 , ai; bi . .
.
, .

100, N

stdin
2

stdout
1 2 0 1
1 1 0 2
0 0 2 2

O. Open air
N(N + 2)
S =
2 .
.
. :
i j p i pj . vi
i, vj j.
pi- si, pj- sj.
. pi- new_si = si vi + vj,
pj- new_sj = sj
vj + vi.

jnew_si Sj + jnew_sj Sj < jsi Sj + jsj Sj, ,


i j.

, , 15-25 2013

39

Akai


- . ,
.
,

. .

P. Pseudo automaton
:
:
:
:

stdin
stdout
1

256

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

abacaba


S, .


N K
. K ,
ai, bi ci, ai
bi ci. .
, .

40


, , 15-25 2013

Akai

1 jSj 5 000
1 ai; bi N
a ci

stdin

stdout

abacaba

8 10
1 2a
1 3b
1 5c
2 3b
2 5c
3 4a
4 5c
5 6a
6 7b
7 8a

P. Pseudo automaton
N S. ,
1 N. N + 1 26N
-: i i + 1 a
z, 1 i N. ,
S.

Q. Quiz
:
:
:
:

stdin
stdout
1

256

.
.
: N N,
1 1. ,
2
1 N 1. .
, ,

, , 15-25 2013

41

Akai


.
. .

-:

N+1

N+2

:::

N 1 N

:::

2N 1 2N

N
N+1N
N+2 ::: N
1

. :
1
5
9
13

2
6
10
14

3 4
7 8
11 12
15

, (
-, ), . ,
.


N . N N
2
. 1 N 1 , 0
2
. 0 N 1 .


, No.
Yes,
- ( ):
L , ,
.
R , ,
- .
U , ,
- .
D , ,
- .
2 500 000.
, .
42


, , 15-25 2013

Akai

2
0

N
aij

50

2
0
2
2
2
3

stdin
3
1
1
0

Yes
DRULDR

stdout

No

Q. Quiz
.
N 2 .
i 1 i- j 1 .
t = (i 1)N + j (i; j). j <
N, t j,
i, . j = N
i , i
. t j 1 i,
t 1. i
, .
N 2 ,
-. j, (N 1)N +j
(N 1; j), (N 2)N + j (N 1; j
+ 1). ,
. .
, ,
2
N 1 (N; N 1),
(N; N). , ,
.
O(N) ,
3
O(N ) .


, , 15-25 2013

43

Akai

R. Reduction
:
:
:
:

stdin
stdout
1

256

S M, N ,
L.
S, M,
S. S, , . ,
S. , .


S. N
. N ,
M. .


,
- S.

jSj

100 ; 1

100 ; 1

jMij

100 ; 1

jSj

stdin
abacabada
4
aba
aca
ada
abb

stdout
3

R. Reduction
, ,
jSj
. , L .
44


, , 15-25 2013

(17.02.2013 .)

...
, - ,
. 20052010 . -
Saratov SU #1, -
ACM ICPC. -

.
:
I
2005 .;

1 ACM ICPC,
( ) 2008 .;
ACM ICPC 2009, ACM ICPC 2010;

Google Code Jam 2008 2011, VK Cup 2012, Challenge


24 2012;
2 Russian Code Cup 2012;
12 ..
,
, III-.

.

G = (V; E),
(u; v) 2 E c(u; v) 0, -
. (u; v) 2= E c(u; v) =
0. : s t. G
f : V V ! R, :

, , 15-25 2013

45

1. ,

f(u; v) c(u; v), u; v V .

2. : f(u; v) = f(v; u) u; v V .
3. : P
f(u; v) = 0 u V , s t.
v2V

G ,
- (
) (
), - .

X
jfj =
f(s; v):
v2V


u v G, f, : cf (u; v) = c(u; v) f(u; v).
Gf = (V; Ef ), Ef = f(u; v) 2 V V : c f (u; v) > 0g, -
G, f.
Gf cf .
0

1. f G f
0
0
0
Gf . f + f G jf + f j = jfj + jf j.
13
0
f+f .
w s t Gf

cf (w) = minfcf (u; v) : (u; v) 2 wg:


p
8
<
cf (p); (u; v) 2 w;
fw(u; v) =
cf (p); (v; u) 2 w;
:
0; :

jfwj = cf (w) > 0, G: f := f + f w.

46


, , 15-25 2013

2. f
, .
:
w,
f, , .
F f -:
0
jfj < jF j. , f = F f
0
Gf jf j > 0. , -
.

-
begin
f := 0
while w
Gf do
f := f + fw
end
end
n = jV j, m = jEj. O(jf jm). -
-,
2
, O(nm ).
. 100200
.
3
- O(n ).

G s t,
(u; v) 2 E c(u; v) p(u; v). f
X
p(f) =
p(u; v)f(u; v):
u;v2V
f(u;v)>0

f
, .
(u; v) G (v; u) c(v; u) = 0 p(v; u) = p(u; v). ,
(
), . pf (u; v) = p(u; v).

, , 15-25 2013

47

. ,
- (u; v)
(v; u), u
v .
, .
0

3. f G, f
0
0
Gf . p(f + f ) = p(f) + p(f ).
4. f (..
- )
, Gf
(-).
,

- w, fw.
jfwj = 0 p(fw) = c(fw) ( ) < 0. -
1 3, jf + fwj = jfj p(f + fw) < p(f), ..
f .

F f
, . ,
()
Gf - .
begin

( -)
while w
Gf do
f := f + fw
end
end
-
2
O(nm). O(nm CP ), C =
max c(u; v), P = max p(u; v). ,
2

O(nm log n).



-. f
- G w s t
. , 0 cf (w) f + fw .

48


, , 15-25 2013

begin
f := 0
while Gf
do w := s t
f := f + fw
end
end

, d(n; m). O(d(n; m)jfj). 2
- d(n; m) = O(nm), O(n ),
O(m log n) O(n log n + m), .
, ,
, .
p(u; v) 0 ,
- .
- ( )
.
v V '(v),
. (u; v)
- p(u; v) + '(u) '(v). ,
u v
'(u) '(v), .
,
.
,
.
'(v) - s
v +1, v . ,
'(v) >
1, - .

'(u) + p(u; v)

'(v);

.
,
. while
-
d(v) - : '(v) := '(v) + d(v).

, , 15-25 2013

49


1. , , ,
. : (2005).
2. Ravindra Ahuja, Thomas Magnanti, James Orlin. Network flows (1993).


A.
:
:
:
:

stdin
stdout
2
64

n , 1 n.
1 , n . i
- j
i < j, j i.
- .


n (2 n 100)
.

stdin

stdout

A.
, ,
. i,
1 ! i i ! n min(i 1; n i).
1 n.
n 1

1+

min(i

1; n

i):

i=2

50


, , 15-25 2013

.
, ,
- . , i n=2
- , i > n=2 .
i n=2 i > n=2.
1 n .

B. 1
:
:
:
:

stdin
stdout
2
64

n m . 1
- , n .
.
1, .. 1, .
, ,
- .
, , 1.


n m (2 n 1000, 0 m 10000).
m x, y, p x y
p (1 x; y n, 1000 p 1000).
.



1. 1, N O.

stdin
6
1
1
2
1
3
3
4

7
2 -3
18
31
31
4 -2
12
6 10


, , 15-25 2013

stdout
6

51

B. 1
, -
, .
- (
O(nm)).

C.
-
:
:
:
:

stdin
stdout
2
64

- .


.
, n (2 n 100) . s, t c
, , , . -
c , :
, 1 000.
, .
,
0, .

52


, , 15-25 2013

stdin
4
1
1
1
2
2
3
0

stdout
25

45
2 20
3 10
35
4 10
4 20

C.
-
-.
:
1. .
.
2.
- .
, ,
.

D.
:
:
:
:

stdin
stdout
2
64

,
ACM 2004 .
, .
, -
, -
.
- n . , , -


, , 15-25 2013

53

, 1.
-
, m
. ,
( ),
- ,
- ,
. ,
- (
)
.
, ,
- .
-,
, .

, ,

. ,
.


n m
,
- . (2 n 100, 1 m 2000).
n 1 2; 3 : : : ; n
- ( 1000).
m .
, ,

(
, - 1000).


,
.

54


, , 15-25 2013

stdin

stdout

4 4
80 50
130
1 2 80 50
2 4 40 90
3 1 40 60
3 4 30 50

3000

80
(- 50 , 30
, 2400 ), 30 (
3 4, 110 , 20 , 600
). ,
.

D.
.
- 1, .
( )
- , ,
-.
. -
. -,

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

E.
:
:
:
:

stdin
stdout
2
64

k . , n ,


, , 15-25 2013

55

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


n, m k -
, (2 n 200, 1 m
2000, 1 k 100). m
, ,
6

( 10 ).
,
.


,
1. ( 5
), ,
-,
. k ,
- ,
. , ,
, .

stdin
5
1
1
1
2
2
3
3
5
56

8
2
3
4
5
3
5
4
4

2
1
1
3
5
1
1
1
1

stdout
3.00000
3 1 5 6
3 2 7 8


, , 15-25 2013

E.
k 1
n, .
, -.
.
-, -
.
k,
k . -
-, O(nmk).

F.
:
:
:
:

stdin
stdout
2
64

n.
n , , :
1. 1
;
2. , ,
- .
.


n (1 n 200).
n n .
1000.


.
n . i-
, i-
. ,
1 n.


, , 15-25 2013

57

stdin
5
7 10
10 6
7 7
5 1
5 10

5
6
7
4
6

3 7
10 9
59
77
5 6

stdout
23
4 3 1 2

F.
. ,
, . 1
,
. .
1
0 .
n .
.
3

O(n ). .

G.
:
:
:
:

stdin
stdout
2
64

,

.
, .

-. , ,
,
-.

58


, , 15-25 2013

. , , ,
,
.
.



,
- .
, , .

,
-, .
- .
,
- , ,

.
. , (Xi; Yi) (Pj; Qj)

Dij = jXi

Pjj + jYi

Qjj + 1 .


,
.
- N (1 N 100) M (1 M 100),
. N (
- 1 N), M (
1 M).
N .
Xi, Yi Bi, , Xi, Yi ( 1000 Xi; Yi 1000) , Bi (1 Bi 1000)
.
M . Pj, Qj Cj, ,
Pj, Qj ( 1000 Pj; Qj 1000) , Cj (1 Cj 1000)
.


, , 15-25 2013

59

N . -

. i- M E ij,
. Eij (0 Eij 10000) ,
i- j- .

, , , .


,
OPTIMAL.
SUBOPTIMAL, N
( ) ,
. ,
.

stdin
3 4
-3 3 5
-2 2 6
2 25
-1 1 3
1 14
-2 -2 7
0 -1 3
3 11
0 06
0 30
3 4
-3 3 5
-2 2 6
2 25
-1 1 3
1 14
-2 -2 7
0 -1 3
3 01
0 06
0 40

60

stdout
SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1

0
0
2
OPTIMAL

1
0
1


, , 15-25 2013

G.
.
-, .
-
- ,
. .
,
,
.
-.

H.
:
:
:
:

stdin
stdout
2
64


. , ,

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



N M (2N400),
M
, .
M
: X, Y L
(1 X; Y N; 1 L 10000), X Y ,
.


.
. , No
solution ( ).

, , 15-25 2013

61

stdin
6
1
3
3
1
4
4
5
4

8
2
2
4
3
2
5
6
6

1 3 4 5
1 2 4 6

1
1
1
2
2
1
1
2

stdout
6

H.
E k = 2.

, , .

I.
:
:
:
:

stdin
stdout
2c
64 M


- .
, - . , -
, .
- .


n (1 n 1000).

31
31
( 2 xi; yi 2 ). ( , , ).
.


, .

62


, , 15-25 2013

stdin
8
0010
2130
3040

stdout

1021
1 0 2 -1
2 -1 3 0 1 0 4 0
0030
0040

I.
,
- . v v1 v2.
(v1; v2).
(u; v) - : (u 2; v1)
(v2; u1). 1. ,
, , ,
- .
.

J.
:
:
:
:

stdin
stdout
2
64

,
. 1
x y, 1
x y.

- 1 n .

n,
m
(2 n 150; 0 m 2000),
n ,
m . m
(x; y) c
x, y, c (1 x; y n; 1 c 1000). t (1 t
500), t


, , 15-25 2013

63

. t .
"1 x y" "2 x y" .
.

.


t + 1 .
-
. t
-.

stdin
4
1
2
3
1
2
2
1
2

5
2 15
35
4 15
35
45

stdout
15
16
15

23
13

J.
-. :
. , (u; v)
,
. u 1, v
1. u , v
1
. , 1. ,
u v.

64


, , 15-25 2013

K.
:
:
:
:

stdin
stdout
2
64

G. ci, li.
,
li
fi
c i.

0.
,
, (
).
.


(1 n 200; 0 m 400), n
, m . m
f romi; toi; li; ci, f romi
, toi ,
li 5 , ci (1
f romi; toi n; 0 li ci 10 ). .
.
a b, b a
(.. ).


, NO.
YES. m
. m- ,
i- .


, , 15-25 2013

65

stdin
4
1
2
3
4
1
4
4
1
2
3
4
1
4

6
2
3
4
1
3
2
6
2
3
4
1
3
2

1
1
1
1
1
1
1
1
1
1
1
1

2
2
2
2
2
2
3
3
3
3
3
3

stdout
NO

YES
1
2
3
2
1
1

K.
. li ri ci.
: li .
. ( -). ? ,
( , ),
li. ,
, li fi ci.
, .

L.
:
:
:
:

stdin
stdout
2
64

n , m . -
66


, , 15-25 2013

.
,
- .
, ,
1, , , n.
Max Traffic
, , .
Max Traffic ,
, ,
.
,
- .
.
,
, -,
. - k
c, c=k.


n (2 n 100)
m (1 m 400). m
:
-, , .
7
10 .
, .
- . ,
, ..
.


, .
.
1 , .


, , 15-25 2013

67

stdin
6
1
1
2
2
3
3
5
4
4
1
1
2
2
3

8
2
3
4
5
4
5
6
6
5
2
3
3
4
4

stdout
4
3 4 5 6

3
3
2
2
2
2
3
3

3
1 2 3

2
2
1
2
2

L.
G = (V; E) C E, ,
t s EnC. -
.
. . [1]. ( ) .
, .
, w.
. k c1, c2,
. . . , ck.
k
1

ci

w:

i=1

:
k
X

(ci

w)

0:

i=1

,
c(u; v) w. ( )
68


, , 15-25 2013

. w . ,

c(u; v) w w.
. ,
-
( ).
- ,

. -.

M.
:
:
:
:

stdin
stdout
2
64

, .

. .
, n m
.
xi; yi xi yi. i- wi .
Snow
White. .
A,
B- .
, A B.

A
B, , , .

1 . ,
, .
,
- .
Snow White
-. , A
,
. ,
- .

, , 15-25 2013

69

,
- Snow White ,
.
A B, : ,
, .


n; m; A; B (2 n 100;
0 m 5000; 1 A; B n; A 6= B), n
, m . m
xi; yi; wi; ti (1 xi; yi n; xi 6= yi; 0 wi 100; 0 ti 1), xi; yi
, wi , ti (0 , , 1 ).
.


p . p
. , A, B, .
, , , 0.

stdin
4
1
2
2
1
3
2
1
3
1
3
1

70

7
2
1
4
3
4
3
4
3
3
2
2

1 4
3 1
100 0
1 0
1 0
4 0
2 1
2 0
1 2
2 0
3 0
1 0

stdout
6
1
1
1
1
1
1

3
4
4
2
2
2

4
4
3 4
3 4

3
1 3 2
1 3 2
1 2


, , 15-25 2013

M.
, wi. , V . :
S T ,
B A . (u; v)
V : (u; v) (S; v) (u; T ) -
c(u; v). .
(S; v) (u; T ), , ,
. -
,
V . .

, B A
. , . ,
F1, F2.
F1 F F2 -
. F2
,
B A.

-
V ( ).
,
- V ,
, .
, ( ,
)
.
. , u v ,
u v . , B A
, . - ,
. ,
B A .
: A B
B A.


, , 15-25 2013

71

(18.02.2013 .)

...
, .
:
ICL-2011
-

ACM
ICPC 2011
(5 -).
Google Code Jam 2011
onsite (20 ).
Russian Code Cup 2011
onsite (12 ).
Challenge 24 onsite
_NiN_ (8 ).
ACM ICPC 2012 (18 ).
Codeforces: 2391.
TopCoder: 2407.

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

72


, , 15-25 2013

STL-:
;
;
( );
.

:
;
.

- .

,
++ - ;
-- - ; * , .
- .

STL .

(-) ( ).
Random Access Iterator
Random Access Iterator - ,
N . += -=.
Random Access .
1. .
1 #include <algorithm>

STL- -
- ,
.
, -
(, )
:
1 bool compare_func(T a, T b)


, , 15-25 2013

73

T - , .
true,
( ), . false - .
, false.
() .

count
1 count(it1, it2, value);

value ,
it1, it2. - int.

:
1 a[5] = {1, 2, 8,
2 cout << count(a,
3 cout << count(a,
4 cout << count(a,

2, 2};
a + 5, 2); // 3
a +
5, 8); // 1
a +
4, 2); // 2

find
1 find(it1, it2, value);

, , value
-
,

it1,
it2.
- .
:
1 a[5] = {6, 2, 8, 1, 3};
2 find(a, a + 5, 2); // \&a[1]
3 find(a, a + 5, 4); // \&a[5] 4 find(a, a + 5,
1) - a; // = 3

min_element/max_element
1 min_element(it1, it2);
2 max_element(it1, it2);

, /
, , it1, it2.
- - .
:
1 a[5] = {6, 2, 8, 1, 3};
2 min_element(a, a + 5); // &a[3]
3 max_element(a, a + 5); // &a[2]
4 min_element(a, a + 5) - a; // = 3 5
max_element(a, a + 5) - a; // = 2 6
*min_element(a, a + 5); // = 1
7 *max_element(a, a + 5); // = 8

binary_search

74


, , 15-25 2013


1 binary_search(it1, it2, value);

, value , it1, it2. . - true, , false - .

:
1 a[5] = {1, 2, 8, 17, 239};
2 cout << binary_search(a,
3 cout << binary_search(a,

a+
5,
a + 5,

17);
19);

// true
// false

lower_bound/upper_bound
1 lower_bound(it1, it2, value);
2 upper_bound(it1, it2, value);

, /
[it1, it2),
value, - .
. :
1 a[5] = {1, 2, 2, 6, 8};
2 lower_bound(a, a + 5, 1); // &a[0] 3
upper_bound(a, a + 5, 1); // &a[1] 4
lower_bound(a, a + 5, 2); // &a[1] 5
upper_bound(a, a + 5, 2); // &a[3]

sort
1 sort(it1, it2);

it1,
it2. :
1 a[5] = {6, 2, 8, 1, 3};
2 sort(a, a + 5); // a[5] = {1, 2, 3, 6, 8}

stable_sort
sort,
-.
replace
1 replace(it1, it2, value1, value2);

value1 value2 ,
- it1, it2.
:
1 a[5] = {1, 2, 2, 6, 8};
2 replace(a, a + 5, 2, 4); // a[5] = {1, 4, 4, 6, 8}


, , 15-25 2013

75

reverse
1 reverse(it1, it2);

it1 it2
-.
:
1 a[5] = {6, 2, 8, 1, 3};
2 sort(a, a + 5); // a[5] = {1, 2, 3, 6, 8}

stable_sort
sort,
-.
replace
1 replace(it1, it2, value1, value2);

value1 value2 ,
- it1, it2.
:
1 a[5] = {1, 2, 2, 6, 8};
2 replace(a, a + 5, 2, 4); // a[5] = {1, 4, 4, 6, 8}

reverse
1 reverse(it1, it2);

it1 it2
-.
:
1 a[5] = {6, 2, 8, 1, 3};
2 reverse(a, a + 5); // a[5] = {3, 1, 8, 2, 6}

merge
1 merge(it11, it12, it21, it22, out_it)

,
it11, it12 ( ) it21, it22 (
), - ,

out_it.
-

,
-.
:
1 a[5] = {1, 2, 2, 6, 8};
2 b[3] = {2, 3, 9};
3 *c;
4 merge(a, a + 5, b, b + 3, c); // &c[8], c[8] = {1, 2, 2, 2, 3, 6, 8, 9}

76


, , 15-25 2013

unique
1 unique(it1, it2);

,
- it1 it2, .
- , .
:
1 a[5] = {1, 2, 2, 6, 6};
2 unique(a, a + 5); // &a[3], a[3] = {1, 2, 6}

random_shuffle
1 random_shuffle(it1, it2)

it1 it2.
:

1 a[5] = {6, 5, 1, 4, 3};


2 random_shuffle(a, a + 5); // a[5] = {1, 5, 6, 3, 4};

next_permutation
1 next_permutation(it1, it2)

, it1
it2 - ,

-.

true,

, false -
-
( ).
:
1 a[5]
= {6,
1, 1,
5, 3};
2 next_permutation(a, a + 5); // true, a[5] = {6, 1, 3, 1, 5}
3 a[5]
= {6,
5, 3, 1, 1};
4 next_permutation(a,
a + 5); // false, a[5] = {6, 5, 3, 1, 1}

prev_permutation
next_permutation,
- .
2. pair.
(
- ).
:
1 pair<type1, type2> var;


, , 15-25 2013

77

2 : var.first - var.second -
.
:
( );
;

:
, - ;

make_pair,
-.
:
1 i n t x, y;
2 double w;
3 ...
4 pair<i n t , i n t > coordinates = pair<i n t , i n t > (x, y);
5 pair<i n t , i n t > coordinates = make_pair(x, y);
6 pair<double, pair<i n t , i n t > > point = make_pair(w, make_pair(x, y));

3. .


- , ..
- ,
.
:
size() - (
); empty() - , ;
clear() - ;
(
- );

; .
vector
,
( ). -,
,
78


, , 15-25 2013

. , , .
1 #include <vector>

:
push_back(value) - value ;
pop_back() - ;

capacity() - ;
resize(size) - (size);
reserve(cap) -
( ). capacity().

:
begin() - , ;
end() - ,
-.
:
1 vector<i n t > v;
2 v.push_back(100); // - 100
3 v.push_back(500); // - 100 500
4 *v.begin(); // 100
5 v.size(); // 2

:
1 vector<i n t > v;
2 ...
3
4 f o r (i n t i = 0; i < v.size(); i++) {
5
i n t x = v[i];
6 ...
7 }

:
1 f o r (vector<i n t >::iterator it = v.begin(); it != v.end(); it++) {
2
i n t x = *it;
3 ...
4
5 }

.

, , 15-25 2013

79


1 vector<long long > v;
2 ...
3 sort(v.begin(), v.end());
4 i n t c = count(v.begin(), v.end(), 100ll);


1. N M .
.
1 vector<i n t > v[MAXN];
2 ...
3
4 cin >> n;
5 f o r (i n t i = 0; i < M; i++) {
6
i n t x, y;
7
cin >> x >> y;
8
v[x].push_back(y);
9
v[y].push_back(x);
10 }

2. . ,
.
1 vector<i n t > ans;
2
3 ...
4 ans.push_back(value);
5 ...
6 reverse(all(ans));
7 f o r (i n t i = 0; i < ans.size(); i++)
8
cout << ans[i] << endl;

3. . K
.
1 i n t f(i n t x) {
2
...
3
vector<i n t > tmp;
4
...
5
tmp.push_back(h[y]);
6
...
7
sort(tmp.begin(), tmp.end());
8 i n t sum = 0;
9 f o r (i n t i =
0; i < min(k, tmp.size()); i++)
10
sum += tmp[i];
11
return sum;
12 }

4.
.
80


, , 15-25 2013


1 vector<i n t > v;
2 ...
3 v.push_back(x);
4 ...
5
6 sort(v.begin(), v.end());
7 v.resize(unique(v.begin(), v.end()) - v.begin());
8 ...

queue
, ,
- .
1 #include <queue>

:
front() - ;
pop() - .
push(value) - value .

1.
1 queue<pair<i n t , i n t > > q;
2
3 void process(pair<i n t , i n t > p) {
4 i n t x = p.first;
5 i n t y = p.second;
6 ...
7}
8
9 ...
10
11 i n t main() {
12 ...
13 i n t x, y;
14 q.push(mp(x, y));
15 while (!q.empty()) {
16
process(q.front());
17
q.pop();
18 }
19 ...
20 }


, , 15-25 2013

81

priority_queue
, ,
- () .
1 #include <queue>

:
top() - () ;
pop() - ;

push(value) - value .

1.
1 vector<pair<i n t , i n t > > v[MAXN];
2 i n t ans[MAXN];
3
4 priority_queue<pair<i n t , i n t > > q;
5
6 void process(pair<i n t , i n t > p) {
7 i n t d = -p.first;
8 i n t x = p.second;
9
10
i f (ans[x] != d)
11
return;
12
13
f o r (i n t i = 0; i < v[i].size(); i++) {
14
i n t y = v[x][i].first;
15
int
nd = d + v[x][i].second;
16
17
if
(ans[y] > nd) {
18
q.push(make_pair(-nd, y));
19
ans[y] = nd;
20
}
21
}
22 }
23
24 i n t main() {
25 ...
26
i n t x;
27
q.push(mp(0, x));
28
while (!q.empty()) {
29
process(q.top());
30
q.pop();
31
}

82


, , 15-25 2013

set
, .
, , O(log(N)), N -
-.
1 #include <set>

:
insert(value) - value ;
erase(value) - value , , ;

erase(it) - ,
it;
find(value) - value ,
, end();
lower_bound(value) - lower_bound
begin(), end(), O(log(N));
upper_bound(value) - upper_bound
begin(), end(), O(log(N)).
:
begin() - , ;
end() - , .


1. set online- .
- N , ,
- ;
2. set .
, ;

3. set . - . N .
;
4. set . .

, , 15-25 2013

83

1. .
2. .
3. .
4. set.
5. lower_bound
- (lower_bound(s.begin(), s.end(), x)).
multiset
set,
. : erase(value) value
. erase(it) .

1. . N
M , .

1. C++,
http://www.cplusplus.com/reference/

STL

2. topcoder.com
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=standardTemplateLibrary
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=standardTemplateLibrary2


A. DOMA 2: Last Hit
:
:
:
:

stdin
stdout
1

256

- Defence of Mysterious Accepted 2.


, ,
84


, , 15-25 2013

, . ,
.
( ). ,
, ,
. , ,
-
. ,
, . !
, , ,
, T 1 ,
T2 . , - T ,
, T +
T1. . -
, ,
. ,
, ( 0).
0
, T2 .
, ,
(. 1).

H .
H1 , H 2
-.
, .
!
- ,
.


2 T1 T2 (1 T1; T2
100000) -
. 3
H1, H2 H (1 H1; H2; H 100000) ,
.


,
, .
( T1

, , 15-25 2013

85

). ,
.
-1.

stdin
1
1
1
1
1
1

1
11
1
12
2
4 10

stdout
-1
1
0
2
2

A. DOMA 2: Last Hit


, ,
- . H 100000, 99999
. - K 2
, .
-
(K2 1) T2, D2 K2 . 0
0
H = H D2 K2. , , H
0. T2 1 , .. T = K2 T2 1
( ). 0
, 0 , K1 = H 0=D1 .
T = (K1 1) T1 . T T ,
. ,
- ,
T , T T1, T 2 T1 .. K2
, -1.

B. Last Effect 3: Danger


:
:
:
:

stdin
stdout
1

256

Last Effect 3. ,
, .
. , - ,
86


, , 15-25 2013

3 .
-,
. .
.
,
- ,
. 3 ,
,
.
.


3 .

3
Xi; Yi
( 100000 Xi; Yi 100000) Ri
(1 Ri 100000).
.
- .



6
- 10
. , .
, - .

stdin

0 0 1
0 4 1
8 0 1

stdout
4.0000000 2.0000000

B. Last Effect 3: Danger


2 2
D R , D -
, R - .
(X1; Y1; R1), (X2; Y2; R2).
(X; Y ), . 2

(X X1) +(Y Y1) +R1 = (X X2) +(Y Y2) +R2 . ,


A X + B Y + C = 0, .. ( ).


, , 15-25 2013

87

(X2; Y2; R2), (X3; Y3; R3)


. ,
(
- ).

C. Fineage: Training
:
:
:
:

stdin
stdout
1

256

Fineage.
80- (, , 99-
). , 80-
. ,
- . ,
X1 , X 2
. .
N , - . ,
,
N 1 .
,
,
, .
Fineage - M,
, 0 M 1.
, ?
, T > 0,
i Ei = Ei+t (mod M), Ei
, i .


2 X1 X2 ,

9
(0 X1; X2 10 ). N
9
(1 N 10 ).
9
M Fineage (1 M 10 ).


88


, , 15-25 2013

1 2
2
3

stdin

stdout

C. Fineage: Training
X1 X2 M.
X1 = X2, , .
T, , T
N. T = N K. N X
= X1 (N 1) + X2 . T K X . , K
X M. K = M=gcd(M; X), K N - .

D. DOMA 2: Inventory
:
:
:
:

stdin
stdout
1

256

- Defence of Mysterious Accepted


2. , , , . .
, , . .
, BattleFury Claymore
Broadsword Perseverance. Perseverance, , RingOfHealth VoidStone. (, ), ,
, .
, . , ,
,
, .

, , 15-25 2013

89


N (1 N 100)
. N ,
. ,
. ,
:
<item> = <part1> + <part2> + ... + <partK>
<item> , <part1>, ..., <partK> - , <item>.
2 10. = +
.
, 50 ,
-. .
. ,
( ).
, ,
-
( ).
M (1 M 100) . M
( ).
- .
.


,
(, -
), 1.
- ,
2. , 0.
, -1.

90


, , 15-25 2013

stdin
6
BattleFury = Claymore + Broadsword + Perseverance
Claymore
Broadsword
Perseverance = RingOfHealth + VoidStone
RingOfHealth
VoidStone
1
BattleFury
1
Perseverance
6
BattleFury = Claymore + Broadsword + Perseverance
Claymore
Broadsword
Perseverance = RingOfHealth + VoidStone
RingOfHealth
VoidStone
1
RingOfHealth
1
Perseverance
6
BattleFury = Claymore + Broadsword + Perseverance
Claymore
Broadsword
Perseverance = RingOfHealth + VoidStone
RingOfHealth
VoidStone
2
Claymore
Broadsword
2
RingOfHealth
VoidStone
6
BattleFury = Claymore + Broadsword + Perseverance
Claymore
Broadsword
Perseverance = RingOfHealth + VoidStone
RingOfHealth
VoidStone
1
BattleFury
4
RingOfHealth
VoidStone
Claymore
Broadsword

stdout
1

-1

91

, , 15-25 2013

D. DOMA 2: Inventory
,
. ,
. ,
- .
. ,
. . .
10
.

100

E. South Mark: 3.50


:
:
:
:

stdin
stdout
1

256
CHEFS DAD: Ooh, it mustve been about
seven, eight years ago. Me and the little lady
was out on this boat, you see, all alone at
night, when all of a sudden this huge creature,
this giant crustacean from the paleolithic era,
comes out of the water.
CHEFS MOM: We was so scared, Lord have
mercy, I jumped up in the boat and I said
"Thomas, what on earth is that creature?!"
CHEFS DAD: It stood above us looking down
with these big red eyes,CHEFS MOM: Oh, it was so scary! CHEFS
DAD: -and I yelled. I said, "What do you
want from us, monster?!"And the monster
bent down and said, "...Uh I need about treefifty."
KYLE: Whats tree-fifty?
CHEFS DAD: Three dollars and fifty cents.
CHEFS MOM: Tree-fifty.

92


, , 15-25 2013

... South Mark.


, - 3 50 . ,
, 3.50, ?
- ? ,
3.50 , N ? ,
, N ,
, N ,
.


N
,
9
- (1 N 10 ).
M (1 M
30). M 9 N i
(1 N9i 10 ) Ki
(1 K i 10 ). ,
i > 1 Ni Ni 1 Ni > Ni 1.



- N, .

stdin
350
3
10 10
50 8
100 4

stdout
2

E. South Mark: 3.50


,
N. ,
M,
, M.

.
- - K.
, .

, , 15-25 2013

93


X, Y .
0
0
T = min(N =X; Y =K) , N - ,
. Y =K ,
( Y K),
Y %K , T = (Y =K) + 1, D = (K Y %K) X

- ,
(, D 0
). N
T X .
0
, D > 0 N > 0, K
, K .

F. HOLM 2: The Great Battle


:
:
:
:

stdin
stdout
1

256

Heroes of Light and Magic 2.


- .
()
. :
(Hi) ,
(Li Ri). L i Ri ,
-
, .
, .
-.
-
( Li Ri). , ,
.
. . ,
, .
,
, , -
.
. -
, ,
, -

94


, , 15-25 2013

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


N (2 N 10) , .
N .
namei 20,
.
- 3 :
Hi (1 Hi 300), L i, Ri (1 Li Ri 50).
.
M (1 M 1000) . M
:
namei deal D damage to namej
namei , namej ,
D (1 D 50000). ,
. ,
,
, (. 2).
( ).


.
- .
:
namei K

, , 15-25 2013

95

namei , K
.
. 5 .
1 1000 .
,
,
. ,
-. , .

stdin
3
Vampires 30
5 7
Minotaurs 35 5
10
Liches 25 8
10
5
Vampires deal
7 damage to Minotaurs
Minotaurs deal
9 damage to Vampires
Vampires deal
7 damage to Minotaurs
Liches deal
16 damage to Minotaurs
Vampires deal
6 damage to Minotaurs

stdout
2
Liches 2
Vampires 1
1
Minotaurs 1

2
Titans 300 20
Sprites 2 1
2
Sprites deal 1
Titans deal

1
Titans 300
1
Sprites 1

30
damage to Titans
9000 damage to Sprites

,
- ,
Heroes of Might and Magic II.

F. HOLM 2: The Great Battle


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

96


, , 15-25 2013

, , -
.
.
3 :
1. .
2. (, ,
, ).S
3. .
. ,
L R (, L = 1, R = 1000). -

, , .
DR, .
0
0
, , [L ; R ]
(
/ , ).
, X = DR=Hi .
0
0
L1 = L + X, R1 = R + X [L1;
R1] [L; R].

2 3,
-, [L; R].
[L; R] .

G. Failout Few Vegas: Slow Save


:
:
:
:

stdin
stdout
1

256

Failout Few Vegas. , ,


. ,
. , S
. , ...
. , -
-, (
, ).
3 N . ,
N . ,
,


, , 15-25 2013

97

, (
). ,
S. "" M S : S,
M . "" M S .
,
, .


S,
. S
- 2000 .
.
,
. 2000.
.



S, .

stdin
a
aa
aa
aa
aa
abc
cab
bca

stdout
2

G. Failout Few Vegas: Slow Save


- S,
Pi. . d[x][y][pref] ,
, x ,
x y (x 1)
98


, , 15-25 2013

, S pref. , i .
pref i.
, S x
( Ki). , prefi x ( pref i).
prefi 0, , , Ki = 0. , x -
S prefi, P [prefi], P [P [prefi]] ,
0. P Pi, P [i], P [P [i]], ... 2
S O(N ), N S. Ki prefi
i, d[x][y][pref], :
d[x][y][pref] = max(d[x+1][i][pref i]+Ki). , d[n][i][pref] = 0,
d[0][1][0].

H. Carmarandom TDC2013: New Trace


:
:
:
:

stdin
stdout
1

256

Carmarandom Total Destruction Contest 2013. -


, , ,
. . -
. , ,
.
. , , .
, N ,
.
, .
,

( , ).
N ,
N .


N
(10 N 100).

, , 15-25 2013

99


, Yes ( ). N + 1
, .
, N 1 -
( ),
. . .
0.5.
0 180 , .
10000 .

, No
( ).

stdin
11

stdout
No

H. Carmarandom TDC2013: New Trace


N- ,
1 .
, , N
. - N = 6:
-2 -1
-1 2
3 3
0 0
-3 3
1 2
2 -1
,
N.

100


, , 15-25 2013

I. X-Dom: Railway
:
:
:
:

stdin
stdout
3
256

X-Dom: The Distance Unknown. -


.
.
. ,
N
. . :
, , .
, -
, , ,
, . , .
,
.


N (2 N 75000)
. N Xi Yi, 100000,
.
OX.
Y = 0
. -,
OX. ,
OX.


6
10

.


, , 15-25 2013

101

stdin
3
0 1
-2 5
2 5

stdout
14.000000

1: ( 1; 3), (0; 5),


(1; 3), ( 7; 0), (0; 0) (7; 0) .
2: 1,
cin
. - cin
,
Time Limit Exceeded.

I. X-Dom: Railway
, , 2 . , -
, - . P O X
OP
: - P .
P OX . P
OX OP
. OP P ,
P
OX . OP
( P = (+1; 0). -
0
P OX , OP 0 OP .

OX . -
O(N), O(log(M axX)) .
,
.

102


, , 15-25 2013

J. MindCraft: Heliport
:
:
:
:

stdin
stdout
3
256

MindCraft. N M ,
.
, ,
.
( ),
.. 1. ,
H W . ,
H W ,
.
. ,
.


N M (1 N; M
500)
, .
H W (1 H 20; 1 W 200; H N; W M)
. N M
.
100000.


,
.

stdin
2
1
1
4
2
1
1
4

3
2
2
5
3
2
6
2

stdout
1

3
6
2
3
5


, , 15-25 2013

103


( ) .

, 1 2 2.
1,
cin .
cin , Time Limit Exceeded.

J. MindCraft: Heliport
, . ,
. X - . -
, ,
Y , X.
, .
- : K 1 X, Y S1 - K2 X, Y S2 - .
T , ,

T = S 1 K1 Y + K 2 Y
S2( ):
, K1, S1, K2, S2. , , M S1
, 1
M S2,
(Size(M S2) <= Size(M S1) <= Size(M S2) + 1) M S1 -
M S2. X
M S1. K1 M S1, S1 - M S1.
M S 2.
S1 M S2 S1 S2.
- . -

( ). H
X H (, M S 1, M S2).
X O(log(H W )). , -
O(H log(H W )).
O(W log(H W )). -

104


, , 15-25 2013

.. ,
O(N M) / O(N) .
O(N M H log(H W ) + N W log(H W )) = O(N M H log(H W )).

K. Double Life: Amplifiers


:
:
:
:

stdin
stdout
3
256

Double Life. . -. -,
, , N
. Ai, ,
-. Ai ,
Ai . Ai 0,
, 0
. Ai .
- |Ai| , ,
. ,
()
( ,
). -
, . ,
, .
. K ,
.
.
Ai 1 ( Ai,
). ,
. ,
- .
,
.


N
(1 N 50000).
, , 15-25 2013

105

Ai ( 100000 Ai 100000).
9
K (0 K 10 ).



-, .
, - (.
2). - ( ) 1.

3
1 0 1
2
3
10 10
3
3
0 0 0
2

stdin

-5

stdout

-200
0

K. Double Life: Amplifiers


, N
K, .
, .
, . ,
0 , 1 ,
( ,
-0). : K > 0 -
1 (, K 1).
4 .

1. .
(http://en.wikipedia.org/wiki/Karamata%27s_inequality). , -
. - ,
. , - . , -
A = a1; :::; an B = b1; :::; bn,
log(a1) + ::: + log(an) <= log(b1) + ::: + log(bn). ,
106


, , 15-25 2013

K
- , ,
- , K.
2. . X 0.
K < X, 0. K >= X,
X
1 (K X). ,
1.
3. , M, M <= K.
, , ,
.
- , .
2.
4. , M, M > K.
, .
, ,
K ( K ).
, - 1,
( ).
. .
-, .
-
- . K ,
,
, . , O(N)
. .
- f, [L; R)
-. : C
= (L + R)=2, X1 = f(L; C); X2 = f(C; R). X = (X 1

X2) , X.
f(0; N). ,
2
O(N log (N)).


, , 15-25 2013

107

(19.02.2013 .)

...

(
01.02.01

), -
,

,


( 11.193.01)
01.02.01, -


, II-III -
,
, -

, ,
, -
.

:
1-
(, 2000);

1- ACM-
(,
2001);
3 ;
.. DonNU United,
7- (2008), 4- (2009) 7- (2010) ACM SEERC,
2011 8- ( ) ACM ICPC.

108


, , 15-25 2013

SCH_Donetsk Erudit, 1- XII .


.. (2013)

.



(x1; x2; : : : ; xn), (
) -
(1)
c1x1 + : : : cnxn;

ai1x1 + ai2x2 + : : : ainxn?bi; i = 1; m

(2)

? =, , aij, bi, cj
.
(1) , ,
- (2), ,
, (1).
1.
- .
, ,

-. ,
,
. ,
, .
2. K
. ,
, .
, . -
n (2),
, .
,

, , 15-25 2013

109

.
m
-, n .
,
- .
( ) .
.


- ,
(1), (2) , b i
. ,
, .. - xj 0.
. ,
.
1. .
-, cj cj. ,
, ,
-.
2. .
- bi,
1 (
-), .
3. . xj

0xj bj, xj =
0
x j + bj, x j 0.
0
0
xj bj, xj = x j + bj x j 0.
,
0
00 x j ,
xj = x j x j, 0

00
x j 0; x j 0.
4. .
:

ai1x1 + ai2x2 + : : : ainxn

bi:

xn+1 :

ai1x1 + ai2x2 + : : : ainxn + xn+1 = bi;


x n+1
0. ,
110


, , 15-25 2013

xn+1 1.
. .


- .
3. , P1, P2, . . . , Pk , Pi - (2),

x1P1 + : : : + xkPk = P0;


X = (x1; : : : ; xk) .
4. x = (x1; : : : ; xn) ,

Pi, x i,
.
, m
- .
-
, i
xg[i], 1,
n
. Pg[i] R ,
-.

i
[]=
xj =
0 j 6= g[i] i:
(3)
b i , g i j;
3 .
fPg[i]g.
c a.
zj =
g[i] ij
1 im

P
5. j zj

cj, (3)

.
6. j
(3) .

z j > c j,

i:aij>0

= min bi=aij. ,

i aij ,
. ,
Pg[i] Pj ( j
aij), j-
, - xj
. ,
(3) - ,
(zj cj) , .

, , 15-25 2013

111

j, zj > cj, j
. j,
zj cj . ,
j j(zj cj),
.
,
.
7. , ,
6,
.
n !
m .
,
- . - . ,
- 2m + n.
,
- O(mn(m + n)) .

(2)
m, , w, .
(1)

, z j zj w +
(2)
zj . ,
w .
, ,
, ,
- .

.

, .
, .
. , ,
,
, . .
112


, , 15-25 2013

,
. m
- , n.
m n.
,
aij ( ) i- ,
j- . , , ,
. , .


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

,
-. , ,
1

1
2
3
4

7
2
5
3

2
2
2
3
2

3
5
3
4
1

4
1
4
4
6

7
1, , 1
. , 1 -
1,
1. 2 2,
3 3, 4 1. -,
3
3. .
1 7, 2 -
3, 3 5, 4
6. , -
2,

, , 15-25 2013

113

3. , , 3
-, 2 .
3 (
), -
, - (
), .
,
-: 1 ,
, maxi minj aij, ,
-, min j
maxi aij. ,
.
- ,

( ).
, max i minj aij < minj maxi aij.
, -
-.
, ,
.
-,
.

P

(p1; p2; : : : ; pm), pi 0 pi = 1. pi


i- .
p, q,
P

V (p; q) = a ijpiqj. ,
, .
. .
p q ,
p q V (p; q ) V (p ; q ) V (p ; q).
. V . ,
,
,

, V :
P

pi = 1;

Pi aijpi

p
114

V; j = 1; n;
i

; m:
=1


, , 15-25 2013

(4)

, ,
V ,
-. ,
V . ,
f(p; V ) = V ,

(4).

-.
-.
, ( ,
, ,
0
), - p i = pi=V


!
P
aijpi0

1;

min;

P pi = 1=V
i0 0

j = 1; n;

; m:

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


A. High
:
:
:
:

stdin
stdout
1

256

p. Z p = f0; 1; : : : ; p 1g
p.
p.

n

n 1

f(x) = x + an 1x

, , 15-25 2013

+ : : : a 1x + a 0;
115

n , x , ai 2 Zp . ,
2
, x +x+ 1 Z2 . ,
f(x) = p(x) q(x), p(x), q(x) - ,
f(x).

Z 2.

- n Zp. ,
m.

p, n, m , p .
1 p; n; m 109.


p, n, m.



- n Zp m.

stdin

stdout

2 2 10
3 4 100

1
18

B. Junior
:
:
:
:

stdin
stdout
1

256

p. Z p = f0; 1; : : : ; p 1g
p.
p.

n

n 1

f(x) = x + an 1x

+ : : : a 1x + a 0;

n , x , ai 2 Zp .
116


, , 15-25 2013

, , x +x+ 1 Z2 . ,
f(x) = p(x) q(x), p(x), q(x) - , f(x) .

Z 2.

- n Zp.

p, n , p .

p; n

10 ; 1

18

p < 10 .


p, n.



- n Zp.

stdin

2 2
3 4

1
18

stdout

B.
f(x).
,
f(x) = p1(x)p2(x). - ,

, :

f(x) = p1(x)p2(x) : : : pk(x):



- ,
.
, n
( R(n)) :

R(n) =

i1+2i2+:::

n 1

+(n 1)i

C(I(1); i1)C(I(2); i2) : : : C(I(n 1); in 1); ( )


=n


, , 15-25 2013

117

I(n) = p R(n) n ,
(n; k) n k,

C(n; k) = n + k 1!:
k
(*) n
-,
, - (
) - .
,
- (*) .
R . R(n; k)
,
n
k. R(n; 0) = 0 I(n) = p R(n; n
1). - ,
k.
:
dn=ke

R(n; k) =

C(I(k); i)R(n; k

1):

i=0

R(i; k) 1 i n, 0 k i 1
2
O(n log n). ,
Junior, p
n
18
p 10 , n 60.
High
- .
1. f(x) n
Zp. Zp ,
, f( )2 = 0.
F = Z p( )
n 1

c
+
c
+
c
+
:
:
:
c
,

ci 2 Z p
0
1
2
n 1
n
( ( )
f( ) = 0). :
a) F f(x) Zp,
f(x) n
p

; ;

p2

;:::;

) f(x)jx
118

pn

pn

x.

, , 15-25 2013

. a) ai Zp,
p
a i = ai. s 2 N

n

f(

ps

)=

i ps

ai ( )

i=0

i ps

(ai )

i=0

ps

= f( )

= 0:
p

, (a + b) = a + b (
p, p ).
, , (**) f(x).
p

, . , s = t , 0 s < t n 1.
p p
p p
r = t s , s+r s = ( r ) s = 0,
pr
=
; 0 < r < n:
n

,
pr p F
- x x, r < n.
, (**) .
n
) F p ,

pn
x x = 0. ,
p
f(x) G(x) = x n
x. G(x) f(x) F .
Zp, f(x)
G(x) Zp.
2. Zp( )
- Z p
g(x) m (mjn),
Zp h(x), deg h(x) 6nj.
. pmg(x) m nZp p
pn 1m,
m 1

1
g(x)jx
x
.

mjn
.

p
1jp
1

x
1jx
1.
p
p
p
, x m xjx n x g(x)jx n x.
p

x n x Zp( ), g(x)
.
g(x) m 2 Z p( ),
[Zp( ) : Zp] = m. ,
Zp( )
Zp( )
Zp, mjn.
3. n 2 N
n

p =

dI(d):
djn

. f(x) Zp
deg f(x) = n. 1 Zp( ),

, , 15-25 2013

119

f(x), f(x) n
.
2 g(x)
Zp d, djn, Zp( ) .
- 1 d g(x).

Zp( ),
, . Zp( ) dI(d) :
P

dj

dj

dI(d):
n

, Zp( ) p

x n x, , r(x). 2 deg r(x)jn :

dI(d):
djn

.
4. F (n) f(n) ,
:
X

f(d) = F (n);

djn

n :

(d)F ( d) = f(n):

djn

(n) , :
(n) =
8 ( 1)k 1 = : : : = k = 1;
n = 1;
< 1;
0
i > 1;
:
9
k n
, i
-.

120


, , 15-25 2013


- (.
., ., . , c.161).
n 3 4 f(n) = nI(n)
F (n) = p ,
n Zp:
n
1X
I(n) =
djn (d)pd :
n

d n, .
n. p
9

O( n). l ( , n 10 l
9). , ,
l
2
. O(log n)
n

pd
.
, n,
mn,
18
m. mn 10 , ,
O(log mn) .
, -p
l

O( n + 2 log n(log m + log n)).

C. High
:
:
:
:

stdin
stdout
1

256

.
, 0 1.
,

( 0 , 1 ).
,
. -,
, .


, , 15-25 2013

121

,
50. .


,
- ( ).


,
- ,
8
- 10 .

001
110
00
10

stdin

stdout
0.50000000
0.25000000

D. Junior
:
:
:
:

stdin
stdout
1

256

.
, 0 1.
,

( 0 , 1 ).
,
. -,
, .

,
10. .

122


, , 15-25 2013


,
- ( ).


,
- ,
8
- 10 .

stdin

stdout
0.50000000

001
110
00
10

0.25000000

D.
,
l. ,
l , . , , l
, l
. : p00:::0
, l ,
p11:::1 l
, - pa1a2:::al ,
al, al 1 ..
:
8 1; a a : : : a ;
p

a1a2:::al

0;
<1
>
>

1 2
l
a1a2 : : : al ;

(p

a2:::al0

+p

:::al1

) :

:
l

2 2 . , .
( ).
, l


, , 15-25 2013

123

1 X
P=

a1a2:::al

ai2f0;1g


- ,
.
, , -
,
l = max(l1; l2) .
a1a2 : : : al ( )
a1a2 : : : al (
) ( -

).
3l

O(2 ).
- ,
, . ,
(
), - .
,
- Junior ,
.
High . ,
,
. l 1 l2 , l1 + l2 + 1.
, 101 .
2
O(l ),
3

O(l ). O(l),
-
( -). ,
, . 0
( ), 1 ,
, parent[x] x (
, next[x][c] , -
x c, last[x] x, suf f[x]
, .. (
124


, , 15-25 2013

),
x, pr[x][c] x
c, .. , (
- x + c). pr[ 1][0]
= pr[ 1][1] = 0, p[0] = 1. (
), :

suf f[x] = pr[suf f[parent[x]]][last[x]];


pr[x][c] =
next[x][c] = 1;
next x][c];

pr[suf f[x]][c] :
:
px = 1 ppr[x][0] + ppr[x][1]
2
x, .
- ,
-. , p0.

- ( ,
(..
-),
2
O(l ). -
,
. :
p = Ap;
. 2
p = Ap, , k p = A k p.
2
k , A k
, , .
, p0.
3

O(kl ).

E. High
:
:
:
:

stdin
stdout
2
256

, N , .
, , 15-25 2013

125

,
C .
,

. M .
-, ,
, .

N, C, M .
18
1 N 10 , 1 C 100, 0

C(C + 1)=2.


N, C, M.
- M ,
.



9
10 + 7.

stdin
2
1
2
3
1

3 2
2
3
2 1
2

stdout
5
2

F. Junior
:
:
:
:

stdin
stdout
1

256

, N , . ,
C .
,
.
126


, , 15-25 2013

M . , , ,
.

N, C, M .
1 N 10000, 1 C 100, 0

C(C + 1)=2.


N, C, M.
- M ,
.



9
10 + 7.

2
1
2
3
1

3 2
2
3
2 1
2

stdin

stdout

F.
A
, .. Aij 1, i j , 0,
-.

. f(n; c) n,
c.
P

c=1

f(N; c). , f(1; c) = 1. ,

f, :
C

Xd

f(n; c) =

Acdf(n 1; d):

()

=1


, , 15-25 2013

127

, N C ,
O(C) ,
2
O(N C ).

Junior, High.
f(n; c) n -
fn. (*) :

fn = Afn 1;
, , :
N 1

fN = A

f 1:

,
3
O(log N C ),
3
, O(C ).

G. - High
:
:
:
:

stdin
stdout
1

256

N .
K
- . , ,
-.

-.

N, K .
9
1 N 100000, 0 K N=2. 10


N K. N ,
- .

128


, , 15-25 2013



,
- K .

stdin
5
0
3
2

stdout

2
3 4 79
1
7 5

13 3
5

H. - Junior
:
:
:
:

stdin
stdout
1

256

N .
K
- . , ,
-.

-.

N, K .
9
1 N 10000, 0 K N=2. 10


N K. N ,
- .



,
- K .


, , 15-25 2013

129

stdin
5
0
3
2

2
3 4 79
1
7 5

stdout
13 3
5

H. -
, k .

. .
.
- k- k
- k
. -
,
.
k+1 i N xi

1 i k xi. , .
k- ,
,
i i 1 i+1.
, k-, i
j (j < i 1). i 1 ,
, (i; j) (i; i 1) .
0
i 1 ( j ),
0
0
(i; j) (i 1; j ) (i; i 1) (j; j ).
0
. ( j > i
0
0
(i; j ) (i 1; j ),
0
. j 6= i + 1,
, i i
+ 1 ).

, ,
(i; i + 1). n 1.
k , ,
.
, - (i; i + 1)
(i 1; i) (i + 1; i + 2).

130


, , 15-25 2013

Junior, , f(n; k) k-
1, 2, . . . , n.
f(n; k) = min(f(n 1; k),

f(n; 0) = 0,
f(n 2; k 1) + (xn xn 1)).
-

k- O(N k).
High, . (i; i + 1), (i 1; i) (i + 1; i + 2)
.
, , (i; i + 1)
(i 1; i) (i + 1; i + 2),
.
,

(i 1; i + 2) w i+1;i+2 + wi 1;i wi;i+1,


, , .
,
.
, ( ) , c 1 . , ,
, O(log N).
. , ,
. -
.
, - (
) , (
).
O(log N),
- O(k log N).

I. High
:
:
:
:

stdin
stdout
1

256


, , 15-25 2013

131

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

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


-, .

N, hi .
5
1 N 10 , 0 hi 1000.


N. N
+ 1 hi, ,
i- (hN+1 ,
, ).


, , -.
N + 1
-
6

. 10 .
,
, -
. ,
N- , ..
132


, , 15-25 2013

stdin

stdout

2
0 6 3

4.000000
0.333333
0.000000
2.800000
0.400000
0.200000

3
2 3 4 2

0.666667
0.400000
0.000000

J. Junior
:
:
:
:

stdin
stdout
1

256

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

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


-, .

N, hi .

, , 15-25 2013

133

5000, 0

hi

1000.


N. N
+ 1 hi, ,
i- (hN+1 ,
, ).


, , -.
N + 1
-
6

. 10 .
,
, -
. ,
N- , ..

stdin
2
0 6 3
3
2 3 4 2

stdout
4.000000
0.333333
0.000000
2.800000
0.400000
0.200000

0.666667
0.400000
0.000000

J.

,
n
( 2 ).
. Gn , ,
,
( ), n.
.
k, ,
hk.
, Gk+1.
134


, , 15-25 2013

k i N

GN+1 , , , , VN+1
hN+1. Gk, k N 1,
- Vk
pk .
V1,
p1, (1 p1)p2, (1 p1)(1 p2)p3 .. -

(1

p1)(1 p2) : : : (1 pN ).
,
, G k
- (
-).
Gk, 2 (Nk + 1),

.
f(p) =
min (hip + Vi+1(1 p)), p 2 [0; 1].
h ip + Vi+1(1 p).
Gk+1 G k,
-
, .
, O(l), l
2
. - O(N )
, Junior.
High,
(set), ( ).
,

, , .
N N , N
,

. ,
O(N log N).


, , 15-25 2013

135

K. High
:
:
:
:

stdin
stdout
1

256

a b. x ,
x = x 1 + x2 + : : : + xn
- ( ) x i,
a b. ,
[A; B] .

a, b , 10000.
18
A, B , 10 .
A B.


a, b, A, B.


a b [A; B].

stdin
4 5 7 12
6 10 2030

stdout
4
6

L. Junior
:
:
:
:

stdin
stdout
1

256

a b. x
-, x = x 1 + x2
+ : : : + xn ( ) xi,
a b. ,
[A; B] .
136


, , 15-25 2013

a, b, A, B .
0 a; b 10000, 0 A B

10 .


a, b, A, B.


a b [A; B].

stdin
4 5 7 12
6 10 2030

stdout
4
6

L.
a b ,
xa+yb, x; y 0. , ,
d = gcd(a; b), xa+yb
a b. , a b
, d, A B, dA=de
bB=dc . a b
. (
Junior). , ab (
ab a b) . O(ab) ,
[0; ab).
[A; B].
High, a b , . , a b ,
, .
- a b ,
. a = b = 0,
0.


, , 15-25 2013

137

M. High
:
:
:
:

stdin
stdout
1

256

a b.
-, ,
,
. -,
p a b ,
2
i i + p(i) = j j.
a b .

a, b .
0 a 100, 0 b 100000, a b.


a b.


b a+ 1 , p(i) i a
b, p .
a b , 1.

19
35

stdin

stdout
826543917
-1

N. Junior
:
:
:
:

stdin
stdout
1

256

a b. , ,
138


, , 15-25 2013

, .
-, p
a b , i
2
i + p(i) = j j.
a b .

a, b .
0 a b 20.


a b.


b a+ 1 , p(i) i a
b, p .
a b , 1.

19
35

stdin

stdout
826543917
-1

N.
, ,
a b, ,
. , . p
2

O(b b) (-p
b ). ,
Junior. ,

.
High 0

. b , b + 1 + b
. 0

[a; b ], , b + 1 + j ! b j,


, , 15-25 2013

139

[a; b]. , a 100 b 200,


. , -
( )
, b 200, 500.
,
b.

O. High
:
:
:
:

stdin
stdout
1

256

n D = f0; 1; : : : ; b 1g a0, a1, . . . , al 1 ,


n D
(.. b0, b1, . . . , bn 1 2 D k 0 l 1 , bj = a(k+j) mod l
j = 0; n 1. .

n, b .
1 n 1000, 1 b

10, b

10 .


n b.


n
- b- ( ).

2 3
3 2

140

stdin

stdout
001102122
00010111


, , 15-25 2013

P. Junior
:
:
:
:

stdin
stdout
1

256

n D = f0; 1; : : : ; b 1g a0, a1, . . . , al 1 ,


n D
(.. b0, b1, . . . , bn 1 2 D k 0 l 1 , bj = a(k+j) mod l
j = 0; n 1). .

n, b .
1 n 1000, 1 b

10, b

10 .


n b.


n
- b- ( ).

stdin
2 3
3 2

stdout
001102122
00010111

P.
,
- n D,
(x1; : : : xn) (y1; : : : yn)
, yi+1 = xi i = 1; n 1. n + 1: (x1; : : : xn; yn) = (x1; y1; : : : yn).
, , . ,
. .

, , 15-25 2013

141

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

. Junior
, High
, ,
n

. O(b ).

Q. High
:
:
:
:

stdin
stdout
1

256

. -
( n 1 n2 ).
, . ,
, , . ,
, .
, -
.
, 1 , 0. , 0:5 . N .

A. Aij 1, i j, 0 . -
,
.

n 1; n 2; N

8.

Aij + Aji = 1 i 6= j, Aii = 0.

142


, , 15-25 2013


N. N
N , A.
n1 n1 ,
- .
.

10 .

stdin
3
0
0
0
2
1
3
0
0
1
3
3
3
0
0
1
3
3

1
0
0
3
1
1
0
0
1
1
1
0
0
1
2

stdout
0.00000000

1
1
0
2

0.50000000

0
1
0
2 3
2 3

0.66666667

0
1
0
2 3
2 3

R. Junior
:
:
:
:

stdin
stdout
1

256

. -
( n1 n2 ).

, , 15-25 2013

143

, . ,
, , . ,
, .
, -
.
, 1 , 0. , 0:5 . N .

A. Aij 1, i j, 0 . -
,
(
).

n 1; n 2; N

8.

Aij + Aji = 1 i 6= j, Aii = 0.


N. N
N , A.
n1 n1 ,
- .
.

10 .

144


, , 15-25 2013

stdin
3
0
0
0
2
1
3
0
0
1
3
3
3
0
0
1
3
3

1
0
0
3
1
1
0
0
1
1
1
0
0
1
2

1
1
0
2
0
1
0
2 3
2 3
0
1
0
2 3
2 3

stdout
0.00000000

0.66666667

0.66666667

R.
. S1 ( ) , , S2 ,
. , .
V (S1; S2) .

0
0
0
. (S1 ; S2 ), Si = Si,
0
i- , Si = Si n j, - i j .
, S1 S2 ,
, , . Junior
,
, . High
, ,

, , 15-25 2013

145

-.
V (;; ;) = 0:5, V (;; S) = 0, V (S; ;) = 1
0
0
0
S. V (S1 ; S2 ), Si
i- .
n +n
3
O(2 1 2 N ).

S. High
:
:
:
:

stdin
stdout
1

256

N , N .

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

N , 1

100000.


N. N ,
(0 ,
1 -).


N , ,
.
8
10 .

146


, , 15-25 2013

stdin

stdout
1.00000000 0.5000000
0.50000000
0.25000000 0.75000000
0.33333333 0.33333333

3
0 1 0
4
1 0 1 0

T. Junior
:
:
:
:

stdin
stdout
1

256

N , N .

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

N , 1

15.


N. N ,
(0 ,
1 -).


N , ,
.
8
10 .


, , 15-25 2013

147

stdin

stdout
1.00000000 0.5000000
0.50000000
0.25000000 0.75000000
0.33333333 0.33333333

3
0 1 0
4
1 0 1 0

T.
, Junior. p(i; a1a2 : : : an)

, i- ,
n ,
, a1 = 0; ,
a1 = 1; , , a 1 = 2
( ).
:
p(1; a1a2 : : : an) =
8 1=n; a1 = 1;
< 1;
a1 = 0;
0;
a1 = 2:
:

:
0
0
p(i; a1a2 : : : an) = 8
<n

n 1k (p(i

1; a2 : : : an) +
p(i 1; a2 : : : an 0)); a1 = 1;
0
a1
= 0;
: : : an ));

p(i 1; a2 : : : an);
k ( p(i

1; a20

a1

= 2:

k aj 2, a 2 : : : a n , 2 .
-
2 n
O(n 3 ), ,
n
3 a1a2 : : : an .

High, ,
p(i; a1a2 : : : an) = a1?1 : (n i + 1)
ak

n i+1

1P

k<i

ak

2. .
-, , O(n).

148


, , 15-25 2013

(20.02.2013 .)

...
,
, 5 , .
TopCoder: 2309
Codeforces: 2275

C 2012
- Codeforces.
VK Cup 2012.

,
- , 5 ,
-

- .
TopCoder: 1851
Codeforces: 2145

- VK Cup 2012
Russian Code Cup 2012.
:


2010 . 2013
- 6 .
ICL (. )
2010 . 2010 - 5 , 2011
- 3 , 2012 - 2 .

NEERC 2010 . 2011 7 .


, , 15-25 2013

149

Best Of 2011 snarknews.info Saratov SU #1 " 2011".

.
-
:
http://www.cs.princeton.edu/%7Esssix/papers/dto-journal.pdf.


A.
:
:
:
:

stdin
stdout
2
256

, n .
. m
.
1 n. ,
x y, (x; y).
(vi; ui). :

(vi; ui) .
,
-,
(vi; ui).
,
- +.
.

n m (1 n 10 ; 1 m 3 10 ).
m . i-
vi; ui (1 vi; ui n) (vi; ui).
150


, , 15-25 2013

, .
, . .


m
.

stdin
3
1
1
1
2
3
3
1
1
1
1
1
4
1
1
1
2
4
3

7
2
2
1
3
1
1
3
3
1
1
1
6
2
3
4
3
1
4

stdout
+
+
+
+
+
+
+
+
+

A.
online O(jV j jEj).
, ,
. ,
,
, . ,
. ,

, , 15-25 2013

151

. ,
(v; u),
, ,
, v.

B.
:
:
:
:

stdin
stdout
1

256

, n .
- .
. m .
1 n.
v cv.
, cv = v . ,
x y, (x; y).
(vi; ui). :
(vi; ui) .

. p1; p2; ; pn, (v; u) xv < xu, pxv = v, pxu = u.
.
9

n
6

1)

pi (10 + 3) i
i=1

1000000009 (10 + 9).



- . v
cv = j, pj = v.
.

n m (1 n 10 ; 1 m 50000).
m . i-
152


, , 15-25 2013

vi; ui (1

vi; ui

n; vi 6= ui)

(vi; ui).

, .
-, .

1000000009 (10 + 9).

3
3
2
3
5
5
5
5
5

3
1
1
2
4
1
2
3
4

stdin

8991020
8991020
7991018

stdout

350074640
276308329
265326315
264326313

, n ,
- n ,
1 n.
x1; x2; : : : ; xp y1; y2; : : : ; yq, p < q x 1 = y1; x2 = y2; : : : ; xp = yp,
r (r < p; r < q), x1 = y1; x2 = y2; : : : ; xr =

y x
r

r+1

<y .
r+1


:
2; 3; 1;
2; 3; 1;
3; 2; 1.


, , 15-25 2013

153

B.
online O(jV j jEj). , ,
. ,
,
( O(1)) - . :
i

1. x[i] = 1000003 , p[i] ,


- .
i=n

2. res =

x[i]

i 1

p[i]

i=1

3. j p,
res. , p[j] = val. , -
res: res = res
(x[j 1] p[j]); p[j] = val; res = res + (x[j 1] p[j]).

C.
:
:
:
:

stdin
stdout
1

256

, n ,
pj wj.
, n 1
( ,
, ), .
.
. P

wjCj.

. -
, .

154


, , 15-25 2013


n (1 n 50000)
. n p j (1 pj 1000)
. n w j (1 wj 1000)
.
n 1 vi ui (1 vi; ui n)
, ui -
vi. ,
.



.
- n t 1; t2; ; tn. ti
, i- .

stdin
3
1 3 2
1 6 4
1 2
1 3
4
3 4 2 1
2 3 3 2
2 1
3 4
2 3
7
1234567
7 6 5 43 2 1
1 2
1 3
2 4
2 5
3 6
3 7


, , 15-25 2013

stdout
49
0 3 1

64
7 0 4 6

210
0 1 3 6 10 15 21

155

C.
. i ti
wi. . ,
. . , P
, Ti
w
T
i i
i- .
. , . . n = 2, ,
(1; 2), t1w2, t2w1. , ,
t1

<

w1

t2

, (1; 2),
w2

(2; 1). ,t
n
t
:

<

, , w1

w2

. ,
t

i .

wi

. . t

i , ,
wi

. : , , , ,
, .
, p v, v.
, .
tpv + tv, w pv + wv. .
t w . -
, , ,
.
, .
. t

i ,

wi

set (TreeSet). , , , set ,


, set.

156


, , 15-25 2013

D.
:
:
:
:

stdin
stdout
1

256

. n , 1 n.
.
, -
x tx. .

a 1- b,
- a
b.
a k- (k > 1)
b, b 1-,
a (k 1)- 1- b.
.
,
( x , x, x > 0).
a k- b,
b k- a.
.
m vi, ki. vi,
ki ki-
v i.
p1; p2; ; pr (p1 < p2 < < pr) -
(tp1 ! (tp2 ! ( ! tpr ))).
. x ! y
- 30- .
.



5
n (1 n 10 ) . n
. i-
9
ti ri (0 ti 10 ; 0 ri n), ti
i, ri
i 0 i
- .

, , 15-25 2013

157

m (1 m 10 )
. m
. i- vi, ki
(1 vi; ki n).
, .


m , ,
- .
, .

stdin
6
1
1
0
3
4
5
4
1
1
2
3

0
1
1
2
2
2

stdout
1073741822
1073741823
1073741823
0

1
2
1
1

30-

30- , 30
.
10 ! 132 = 1073741813.
:

0 ! 0 = 1;
0 ! 1 = 1;
1 ! 0 = 0;
1 ! 1 = 1.

158


, , 15-25 2013

D.

O(n n log n). ,


.
.
1. dep(v)
.
2. dep(v),
.
3. v; k. ,

,
dep(v) + k.
4. .
-.
5.
dep(v) + k,
- -.
6. ,
( O(1), -

) O(n n).

E.
:
:
:
:

stdin
stdout
1

256
d

n . R .
d 1 ,
(0; 0; : : : ; 0). , .


n (1 n
10).


, , 15-25 2013

5000), d (1

159

n d+1 . d
, xj;1; xj;2; : : : ; xj;d.
yj (jyjj = 1). yj -
, .
100.
6 .


d n1; n2; : : : ; nd .
. P

sign(

i=1

xj;i

ni) yj. ,

.
, j

xj;i nij

i=1

10 jnj, jnj .

stdin
2 1
1 -1
-1 1
4 3
-99.749 12.71 -61.33 1
61.7
17.00 -4.0 -1
-29.94
79.192 64.56 1
49.320
-65.178 71.788

-1.000000000
-0.891651465
-0.179427280

stdout

0.415648308

-1

E.
. d- n .
, .
,
, . , .
.
- r. ,
, - . ,
, 1.
- : r ,
hr; xii . :

r0 = 0, xj : hr; xji
160

0 ri+1 = ri + xj

, , 15-25 2013


1 read(n)
2 f o r (i = 1; i <= n; i++)
3 read(x[i], sign[i])
4 x[i] *= sign[i]
5r= 0
6 run = 1
7 while (run)
8
run = 0
9 f o r (i = 1; i <= n; i++)
10
i f (r * x[i] <= 0)
11
r += x[i]
12 output(r)

.
, , . , u. , , juj = 1.
. hrk; ui. 8i : hxi; ui > 0, minhxi; ui
,
i

hrk; ui = hrk 1; ui + hxj; ui hrk 1; ui + , hrk; ui k .

-.
2
2 2
2
2
2
jr2kj = jrk2j juj (hrk; ui) = (k
2 ) . jrkj
= jrk 1 + xjj = jrk 1j + 2hrk 1; xji + jxjj .
hrk 1; xji

0, jrkj

jrk 1j + jxjj . max jxij ,

jrkj k . : (k ) jrkj k .
, xi, jxij = 1, = 1, = cos t , t 2
[0:: ) - .

(k

jrkj
2

, (k ) = k, k


, , 15-25 2013

161

(21.02.2013 .)

. C

(
) ,
,
. ,

http://ru.wikipedia.org/wiki/_. ,

, .
,
- .
, ,
- .
,
( ) .
.
1 s t r u c t node{
2
i n t l, r, par; //s[l..r-1] ,

3
map<char, i n t > next;
4
node(i n t l, i n t r, i n t par) : l(l), r(r), par(par){
5
}
6 };


, .
.
1 s t r u c t position{
2
i n t V, L;
3
position(i n t V, i n t L) : V(V), L(L) {}
4 };



-.
1 i n t leng(i n t v); // .
1 void split_edge(position pos); // .

162


, , 15-25 2013


1 void add_edge_to_parent(i n t l, i n t r, i n t parent); // .
1 position read_char(position pos, char c); // .

:
1 i n t leng(i n t v){
2
return t[v].r - t[v].l;
3}
1 i n t add_edge_to_parent(i n t l, i n t r, i n t parent){
2
3
4
5}

i n t nidx = szt++;
t[nidx] = node(l, r, parent);
return (t[parent].next[s[l]] = nidx);

1int
2
3
4
5
6
7
8
9
10
11 }

split_edge(position pos){
i n t v = pos.V, up = pos.L, down = leng(v) - up;
i f (up == 0) return v;
i f (down == 0) return t[v].par; // if
i n t mid = add_edge_to_parent(t[v].l, t[v].l + down, t[v].par);
t[v].l += down, t[v].par = mid;
t[mid].next[s[t[v].l]] = v;
return mid;

1 pt read_char(position pos, char c){ 2 i n t v =


pos.V, up = pos.L;
3
i f (up > 0)
4
return s[t[v].r] == c ? position(v, up - 1) : position(-1, -1);
5
else{
6
i n t nextv = t[v].next.count(c) ? t[v].next[c] : -1;
7
return nextv != -1 ? position(nextv, leng(nextv) - 1) : position(-1,
-1);
8
}
9}


s,
2
- t O(jsj ).
,
. .
1 void make_tree(){
2
node root(-1, -1, -1);
3
t[szt++] = root;
4
5
f o r (i n t i = 0; i < szs; ++i){
6
position pos(0, 0);


, , 15-25 2013

163


7
8

f o r (i n t j = i; j < szs; j++){


position npos = read_char(pos, s[j]);
i f (npos.V != -1)
pos = npos;
else{
i n t mid = split_edge(pos);

9
10
11
12
13
14
15
16
17
18
19
20 }

add_edge_to_parent(j, szs, mid);


break;
}
}
}

,
- . ,
1000000,
- .

.
, (v; u) ,

u
- u. (v; u) ,
- link(v) = u. ,
v dep(v).
1. ()
.
- .
2.
- .
,
L(L > 1) . ,
, x(jxj > L) y(jyj > L) ,
- L. ,
link(v) x1(jx1j = jxj 1), y1(jy1j =
jyj 1) , L 1
link(v), . ,
link(v), . link(v)
.
3. v
- dep(link(v)) dep(v) 1.
164


, , 15-25 2013

v .
x1; x2; ; xdep(v) (xdep(v) = v).
link(x1); link(x2); ; link(xdep(v)) , . , .
.


-? w
, w .
, ( k)
( 2), k dep(k) dep(w).
.
1
position fast_go_down(i n t v, i n t l, i n t r){
2
i f (l == r) return position(v, 0);
3
while(t r u e){
4
v = t[v].next[s[l]];
5
i f (leng(v)
>= r - l)
6
return position(v, leng(v) - r + l);
7
l += leng(v);
8
}
9
throw;
10 }
1 i n t link(i n t v){
2
3
4
5}

i f (t[v].link == -1)
t[v].link = split_edge(fast_go_down(link(t[v].par), t[v].l + i n t (t[v ].par == 0), t[v].r)); //
.
return t[v].link;

, link split_edge.
.
.

,
- . s =
s0s1s2 sjsj 1,
- s 0, s0s1, , s0s1 sjsj 1.

- .
( ), , .
.


, , 15-25 2013

165


1 position add_char_to_tree(position pos, i n t i){
2
while(t r u e){
3
position npos = read_char(pos, s[i]);
4
i f (npos.V != -1) return npos;
5
6
i n t mid = split_edge(pos);
7
8
add_edge_to_parent(i, szs, mid);
9
10
pos = position(link(mid), 0);
11
12
i f (mid == 0)
13
return pos;
14
}
15
throw;
16 }
1 void make_tree(){
2 node root(-1, -1, -1); root.link = 0; 3 t[szt++] = root;
4
5
6
7
8
9

position pos(0, 0);


f o r (i n t i = 0; i < szs; ++i){
pos = add_char_to_tree(pos, i);
}
}


A. -
:
:
:
:

stdin
stdout
1

256

k k k, .
, .
3

k
( ), :
2 ;
2 .


k (1 k 100)
.
166


, , 15-25 2013


, -1.
. k
k k . -
k k k
. k- . ,
.
w, b.
, .
.

stdin
1
2

stdout
-1
bb
ww
bb
ww

A. -
1. , - ,
- ,
.
2. .
3. , .
4. ,
,
.
, .
k .
5. .
:


, , 15-25 2013

167


wwwwwwww
wbbbbbbw
wbwwwwbw
wbwbbwbw
wbwbbwbw
wbwwwwbw
wbbbbbbw
wwwwwwww

6. n n,
n n n.
7. , (.. , ). -
.

B. -
:
:
:
:

stdin
stdout
1

256

n. y
. ,
(1) (2).
,
n
2 n
. ,
.
, .


n (1 n

50) .


,
n
n , 2 .
.

, .
-, .
168


, , 15-25 2013

stdin

stdout

1
2

2
12

B. -
1. , . .
n = 1 .
2. n a.
3. n + 1. n n + 1
n. n ,
n
- n + 1: 10 + a 2
n
10 + a. b c .
n+1

n+1

4. b c -
n + 1,
= cmod2
= 0.
n+1
n bmod2
n n
(b c)mod2
=
0
.

b
c
=
10
=
2
5
.

n+1
2 , ,
.
5. n,
. n = 1 .
n.
n , n + 1.

C.
:
:
:
:

stdin
stdout
1

256

n m . .
,
.


, , 15-25 2013

169

,

(- )
, .
,
- - .

- ,
- .

, .
- ,
.
? ,
-.



5
n m (1 n m 10 ) .
n m .



-, .

stdin
2 2
ab
ba
1 9
aaabbbccc
4 4
xyzx
yzxx
zxxy
xxyz
1 1
a
170

stdout
2
2
5

0

, , 15-25 2013

C.
1. , -
, ,
, .
2. , ,
- , .
- ,
, , ,
.
3.
, .

4. ,
, ,
.
5. ,
. , ,
x (x 2)=2 ,
.
6. x 2 (x 2)=2
+ 1, , 1,
.

D. -
:
:
:
:

stdin
stdout
1

256

-,
n , , (v; u)(v 6= u)
v u .

,

( ).


, , 15-25 2013

171


n (3 n 1000)
.


-1,
,
.
n , n ,
, a . ,
1 n.
av;u = 0, v u, a v;u = 1,
.
, :

av;u + au;v = 1 v; u (1
av;v = 0 v (1

v; u

n; v 6= u);

n).

stdin
3
4

stdout
0 1 0
0 0 1
1 0 0
-1

D. -
1. n 3.
n + 2 .
2. : s; t.
3. t s.
4. v : s v v t.
5. s
1. s t 2.

172


, , 15-25 2013

6. t 1.
s 2.
7. t s 1. t
2.
8. n = 3,
n 3,
.
9. n = 4, ,
.
10. n = 6. , ,
, n 6.

E. -
:
:
:
:

stdin
stdout
1

256

. -.

: a; b; c.
a, b, c.
a: a , b c
. , b
c .
b: b , a c
. , a
c .
c: c , a b
. , a
b .
, (, )
a, b c .
, ,
, ,

, , 15-25 2013

173

, . ,
.


: a; b; c (0
9

a; b; c 10 ; 1

a + b + c 10 ) ,



, .
, -1.
, , a, b,
c, ], [ . 1000 .
c answer:

character ::= a | b | c;
sequence ::= character | character sequence; block ::=
character | number [ sequence ];
answer ::= block | block answer | << >>;
10

number (1 number < 10 ) ,


. , ,
- , .

. character
- , number [ sequence ]
- number sequence.
- .

1 1 0
2 0 0
0 0 1
5 4 5

174

stdin

1
c
-1
0
13
4[abc]b

stdout


, , 15-25 2013

E. -
1.
.
2. , 001.
3. ,
,
(..
), .
4. ,
-.
5. ,
1, , ,
.
6. 001,
, .
7. Ec 00a(a 6= 1),
, .
8. 01a, ,
10a 1,
01a 1. a
010.
9. Ec aa + 1b, ,
a 01b + a.
10. abc.
a 2bc : a 1b
1c + 1, a 1 1b 1 + 1c + 1 1, a 2bc.
11. .
a, a + b. ,
b - .
12. , a + b 2,

aa + 1c, .


, , 15-25 2013

175

F.
:
:
:
:

stdin
stdout
1

256

n, k.
[f rom; to] idx,
[lef t; right] (f rom to; lef t right).
1 i n t get(i n t idx, i n t
left, i n t right, i n t from, i n t to) {
2
i n t ans = 1;
3
4
i f (left == from
&& right == to)
5
return ans;
6 i n t mid = (left + right) / 2; 7
i f (from <= mid)
ans += get(2 * idx + 1, left, mid, from, min(mid, to));
i f (to >= mid + 1)
ans += get(2 * idx + 2, mid + 1, right, max(from, mid + 1), to);

8
9
10
11
12
13
14 }

return ans;

,
. , .
[f rom; to] (0 f rom to n 1),
get(0; 0; n 1; f rom; to) k.
9

1000000009 (10 + 9).


: n (1
18

10 ), k (0

18

10 ).

1000000009 (10 + 9).

6 5
10 6
4 6

176

stdin

4
9
0

stdout


, , 15-25 2013

F.
1. , get(idx, l, r, from, to)
,
-.
2. O(log n),
k 10 log n, 0.
3.
z[L][V ][K], V , K , , L
, .
4. V l r.
5. L = 0, [f rom;
to], l f rom to r.
6. L = 1, [l; to],
l to r.

7. L = 2, [f rom; r],
l f rom r.
8. (L; V; K) :
9. k < 1, 0, V .
10. k = 1,
[l; r]. V .
l+ r

11. mid = 2 .
12. L = 1, [l; to].
13. to mid,
,
z[1][2 V + 1][K 1].
14. mid < to,
,
2 V + 1,
z[1][2 V + 2][K 2].


, , 15-25 2013

177

15. L = 2 .
16. L = 0, [f rom; to].
17. to mid,
, z[0][2 V +1][K 1].
18. mid < f rom,
, z[0][2 V +2][K 1].
19. , , . lk , lk 1 k 2

z[2][2 V + 1][lk] z[1][2 V + 2][k 1 lk].


20. , ,
- .
, .

21. O(log n). .


22. O(log n) .
n.
23. i x. x , i
x
x 1 x 1
+ 1 2 , 2 2 + 1.
24. i : x x + 1. x , i + 1
x
x
x+1
x+1
2 2 + 1, :
2
2 1.

25. O(log n) ,
O(log n).
26. O(k cdot log n)
2
O(k). O(k log n).
3

27. k 10 log n, O(log n).


2

28. O(log n).


- .

178


, , 15-25 2013

G.
:
:
:
:

stdin
stdout
5
512

p q, n , m : l1; r1; l2; r2 (l1 r1; l2 r2).


1 n, -
[l1; r1] ( ),
[l2; r2] ( ).

, n ,
- n ,
1 n.
v (1 v n) g1; g2; ; gn
i, gi = v.

n (1 n 10 ) .
n , : p1; p2; :::; pn (1 pi n)
.
q1; q2; ; qn .
5
m (1 m 2 10 )
.
m
-. i- : a; b;
c; d (1 a; b; c; d n). l1; r1; l2; r2 a;
b; c; d :
1. x. , 0,
.
2. f(z) = ((z 1 + x) mod n) + 1.
3. l1 = min(f(a); f(b)); r1 = max(f(a); f(b)); l2 = min(f(c); f(d)); r2 =
max(f(c); f(d)).



.

, , 15-25 2013

179

stdin
3
3
3
1
1
4
4
2
3
1
1
1

1 2
2 1

stdout
1

2 3 3
3 2 1
3 4 1

1
1
2

2 3 4
3 2 1
4 2 3

G.
1. , . p1; p2; :::; pn.
2. (l1; r1; l2; r2)
x, l1 x r1 x
[l2; r2].
3. (l1; r1; R)
x, l1 x r1 x
[1; R].

4. (l1; r1; l2; r2) (l1; r1; r2) (l1; r1;


l2 1).
5. , .
.
6. 1 n,
- . .
7. . i
- pi .
(l1; r1; i)
[l1; r1] .
180


, , 15-25 2013

8. ,
,
.

H. -
:
:
:
:

stdin
stdout
5
256

-.
n m. .

i j (xi;j; yi;j) (1 i n; 1 j m). -

, ,
k . t (1 t k) -
-
- ((t 1) mod n) + 1.
, .
, -, k.
-
? .


n; m; k (1 n; m
9

10 ; 1

k 10 ; n m 10 ) -.
n 2 m :
i xi;1; yi;1; xi;2; yi;2;
; xi;m; yi;m (jxi;jj; jyi;jj
9
10 ).


, -.


, , 15-25 2013

181

stdin
22 3
1 0 -1 0
0 1 0 -1
2 1 10
11
-1 -1
2 3 1000000000
1001320 -1000 123 1233
2233 -1232
-12300 -10 -1233 -15533
2233 -123

stdout
5

50

251779971234500000000000000000

H. -
1. ,
- .
i ,
.
2. . n m + 1 .
(0; 0). , .

3. .
.
4. ,
, , .

182


, , 15-25 2013

I. -
:
:
:
:

stdin
stdout
1

256

1 n,
1 n .
, .
a1, a2,
ak k .
.
, . -

. , . ,
n.
, . ,
, ,
-. , .


n k (1 n
6
10 ; 1 k 10 ) .

a1; a2; ; ak (1 ai < n; ai < ai+1) .
, n .
9



- .

5
1
5
2

2
3
2
3

stdin


, , 15-25 2013

stdout

183

I. -
1. n 1 ,
, .
2. , n 1,
.
3. n n 1 ,
, .
4. ,
.
5. , ,
i , i- .
6. . - 1.
1 , i-.
7.
, , , ,
, .
8. , a 1; a2; :::; am(ai
ai+1).
, ai ai+1 .
9. d1; d2; :::; dm, d1 == a1; di = ai
ai 1. .
, x ,
x .
10. . .
- .

184


, , 15-25 2013

J.
:
:
:
:

stdin
stdout
1

256

s. s
. ,
- .

s (1 jsj 10 ),
$, .


0 n 1
,
. ASCII-
.
n
. n 1 ,
, .
v : p; lf; rg,
p (0 p < n; p 6= v) . ,
p v, s[lf:::rg 1] (0 lf < rg jsj).


, , 15-25 2013

185

stdin

stdout

aaa$

7
0
0
2
2
4
4
3
0
0
10
0
0
2
2
4
4
0
7
7

b$
ababa$

34
01
34
12
34
24
12
02
56
01
56
13
56
36
13
56
36

s[l:::r] (0

r < jsj) s = s0s1 : : : sjsj 1 ( jsj

s) slsl+1 : : : sr.

J.
,
.

K.
:
:
:
:

stdin
stdout
1

256

s t. , 186


, , 15-25 2013

s t. ,
.

s (1 jsj 10 ),
$, .
5
t (1 jtj 10 ),
#, .


0 n 1
,
. ASCII-
.
n
. n 1 ,
, .
v : p; w;
lf; rg, p (0 p < n; p 6= v) , w (0 w
1) . w = 0,
, p v, s[lf:::rg 1] (0 lf < rg jsj).
w = 1, , p v, t[lf:::rg 1]
(0 lf < rg jtj).


, , 15-25 2013

187

stdin

stdout

ab$
ac#

8
0
123
0 0 2 3
0 0 0 1
3 0 1 3
3 1 1 3
0 0 1 3
0 1 1 3
14
0
145
0 0 3 4
0 0 0 1
3 0 3 4
3 1 2 5
3 0 1 2
6 1 4 5
6 0 2 4
0 0 1 2
9 1 4 5
9 0 2 3
11 0 3 4
11 1 2 5

aba$
baab#

s[l:::r] (0

r < jsj) s = s0s1 : : : sjsj 1 ( jsj

s) slsl+1 : : : sr.

K.
1. s + t.
2. t.
3. s,
s t.
4. ,
, ,
- $.
188


, , 15-25 2013

L.
:
:
:
:

stdin
stdout
1.5

256

s. q .
:
1. s.
- + c, c ,
s.
2. s.
? l, l
s, .
-, s
( jsj l + 1,
-).
, +,
, - .

q (1 q 2 10 )
.
q ,
- . , .
, . ,
c
a, b, c, d, e. , l
s.


.
.


, , 15-25 2013

189

stdin
13
+a
?1
+a
?1
?2
+a
?1
?2
?3
+b
?3
?1
?2
8
+a
+b
+a
+b
+a
?3
?2
?4

stdout
+
+
+
+
+
+

L.
online
-.
, .
.
- ,
.

190


, , 15-25 2013

M.
:
:
:
:

stdin
stdout
1

256

s, t k.
, s t . k-
.


s t (1 jsj; jtj 10 ).
18
k (1 k 10 ).
.


-1,
-.

stdin

aaa
abaa
3
aabbbabba
abbabbab
1
baab
bbaababb
3
bbbbaaba
abbabbabba
2


, , 15-25 2013

-1

stdout

a
aab
ab

191

M.
1. s + $ + t + #.
2. $,
s.
3. #
$, t.
4. ,
- s, t.
5. , ,
- .
6. k-
.

N.
:
:
:
:

stdin
stdout
1

256

. a
- b, a b, b
a.
s.
s, k , , s
, , .

-.

s (1 jsj 10 ) .
k (1 k min(100; jsj))
.
.

192


, , 15-25 2013



. , -1.

stdin
aba
1
aaa
1
abacaba
3

stdout
-1
3
11

N.
1. s.
2. -
- s.
3. ,
.
4.
- z[V ][K], V
, K ,
V .
5. V , ,
V
V .
k 1
V .
6.
- V .
7. .
- z2[V ][U][K], V U,
K ,
- V .

, , 15-25 2013

193

8. z2 U K,
- ,
U, 1 K z[U][U K] +
z2[V ][Y ][K U K], e Y U V .

194


, , 15-25 2013

(22.02.2013 .)
K
...
, 28
1985 .
11
1992 - 1993., No. 84
- 1993 - 1998 .,
33
.

1998 - 2002 .
2007 - , 2005 - 2007 . -
-
. .

2007-2009 .
Google -.
C 2012 .
:
Google CodeJam 2010;
x13 2006;
2012 8 TopCoder 3
Codeforces.

s
, :
1. s.
.
2. t0.

, , 15-25 2013

195

3.
. s ,
- s.


t0, , s.
, aba (*
- ):

,
.
t s. s, t.
endP os(t). s endP os (
t0 endP os
[ 1 : : : lengths(s) 1). .

u w s ( u w). , u
w, endP os(v) endP os(w) ,
endP os(w) endP os(u).
- w -.
, , ,
w,
.
, t0,
- ,
v t0,
, v .
. ,
s[0 : : : i]
s[0 : : : i + 1].
, .
196


, , 15-25 2013

t0 0, null
.
last, .
c :
cur , last + 1.
last ,
c.
c cur.
- p,
c q, 2 :
q p + 1, q
cur
.
clone c , p + 1,
,
q, , q.
q cur clone,
p ,
c q ,
clone.
c,
- cur t0.
last cur.
. s
c. (p; q) , p
q. .
cur , s
+ c (endP os(s + c) = length(s), , ,
). w s,
w + c s.
w s w +c
s. ,
w c ( w + c , , ), endP os w + c
length(s).
endP os(w + c) , , endP os(q)

, , 15-25 2013

197

( q , ,
w c). clone.
, . ,
(
2). , . ,
, ,
.
(p; q)
t0 p q ,
, t0
- .
,
. ,
2n 1, - 3n 3, n s.
,
(
, ). ,

last (q)
. length(s) .

1 p u b l i c c l a s s SuffixAutomaton {
2 p u b l i c f i n a l i n t [] length;
3 p u b l i c f i n a l i n t [] link;
4 p u b l i c f i n a l i n t [] first;
5 p u b l i c f i n a l i n t [] next;
6 p u b l i c f i n a l i n t [] to;
7 p u b l i c f i n a l i n t [] l a b e l ;
8 p u b l i c i n t size;
9 p u b l i c i n t last;
10
11
p u b l i c SuffixAutomaton(CharSequence s) {
12
i n t count = s.size();
13
length = new i n t [2 * count];
14
link = new i n t [2 * count];
15
first = new i n t [2 * count];
16
next = new i n t [4 * count];
17
l a b e l = new i n t [4 * count];
18
to = new i n t [4 * count];
19
Arrays.fill(first, -1);
20
link[0] = -1;
21
size = 1;
22
i n t edgeSize = 0;
23
last = 0;
24
f o r (i n t i = 0; i < s.length(); i++) {
25
i n t c = s.charAt(i);

198


, , 15-25 2013

K
26
i n t current = size++;
27
length[current] = length[last] + 1;
28
for
(i n t previous = last; ; previous = link[previous]) {
29
if
(previous == -1) {
30
link[current] = 0;
31
break;
32
}
33
i n t index = findEdge(previous, c);
34
i f (index != -1) {
35
i n t curLink = to[index];
36
i f (length[previous] + 1 == length[curLink])
37
link[current] = curLink;
38
else{
39
i n t clone = size++;
40
length[clone] = length[previous] + 1;
41
link[clone] = link[curLink];
42
i n t linkEdge = first[curLink];
43
while (linkEdge != -1) {
44
next[edgeSize] = first[clone];
45
first[clone] = edgeSize;
46
l a b e l [edgeSize] = l a b e l [linkEdge];
47
to[edgeSize++] = to[linkEdge];
48
linkEdge = next[linkEdge];
49
}
50
f o r (; previous != -1; previous = link[previous]) {
51
i n t edge = findEdge(previous, c);
52
i f (edge == -1 || to[edge] != curLink)
53
break;
54
to[edge] = clone;
55
}
56
link[current] = link[curLink] = clone;
57
}
58
break;
59
}
60
next[edgeSize] = first[previous];
61
first[previous] = edgeSize;
62
l a b e l [edgeSize] = c;
63
to[edgeSize++] = current;
64
}
65
last = current;
66
}
67
}
68
69 p u b l i c i n t findEdge(i n t vertex, i n t l a b e l ) {
70
i n t edge = first[vertex];
71
while (edge != -1) {
72
i f (t h i s .l a b e l [edge] == l a b e l )
73
return edge;
74
edge = next[edge];
75
}
76
return -1;
77
}
78 }


, , 15-25 2013

199


s. ,
s .

1 + ,
.

, t,
- s, ,
- ,
s. , ,
- .
, ,
.
,
, . ,
- t
s. ,
( -
) -
, .
, -,
(
)
-.


A. Chords
:
:
:
:

stdin
stdout
2s

256 Mb

Consider a circle. There are 2N different points marked on it and


numbered with integer numbers in range from 1 to N so that each number
from the interval has two points corresponding to it.
Points with the same numbers are connected by segments. Thus weve got
N chords. Futhermore, the chords are numbered as well: the chord number

200


, , 15-25 2013

i connects the two different points with number i. Apparently some of the
chords may intersect. Now it would be nice to find out for each single chord
the number of chords it intersects.


In the first line of the input file there is a single integer number N (1 N
10 ). In the next line there are 2N integers in range from 1 to N the
numbers assigned to the points in the order of traversal. Each number is
written exactly twice. All the numbers in the line are separated with spaces.
5


The output file is supposed to contain exactly N lines: on the i-th line
write the number of chords that i-th chord intersects.

stdin

5
1231425534

2
3
3
2
0

stdout

A. Chords
,
. ,
, .
,
- , .
, ,
. - ,
,
.

B. Cyclic suffixes
:
:
:
:

stdin
stdout
2s

256 Mb

Consider a string S = s1s2s3 : : : sn 1sn over an alphabet



, , 15-25 2013

. A cyclic
201

expansion of order m over a string S is the string s1s2s3 : : : sn 1sns1s2 : : :


of m characters; that is, we concatenate instances of S until the resulting
string has sufficient length, and then take the first m characters as the result.
~
Let cyclic string be the infinite cyclic expansion over a string
.
Take a look at the suffixes of a cyclic string

. Obviously, there are no

more than jSj different ones among them: the (n + 1)-th one is equal to the
first (the cyclic string itself), the (n + 2)-th is equal to the second, and so on.
However, there may be even less different suffixes. For example, if S = abab,
the first suffixes of

are:

S~ 1 =

ababababab : : :

=
=

ababababab : : :

S~ 2 =
S
~

S4

bababababa : : :
bababababa : : :

etc. Here, only two different suffixes can be found, while jSj = 4.
S
S
equal, the one with the lower index comes first. We are now interested in the
S
The following example shows the ordering for S = cabcab.
Let us order the first j j suffixes of

following problem: what is the position of our original string

lexicographically. If two suffixes are

after sorting?

(1)

(2)
(3)
(4)
(5)
(6)
~ ~

Here, the position of S = S1


Given a string

of length

abcabcabca : : :

~
S

abcabcabca : : :

S3

bcabcabcab : : :

S6

bcabcabcab : : :

~
S

cabcabcabc : : :

cabcabcabc : :
:

S2
5

is 5.
, find the number of

in the specified ordering.


On the first line of the input file, a string S (1 6 jSj 6 1 000 000) is given.
The input consists of lowercase Latin letters only.


Output the only number on
specified ordering of its first jSj
202

a line by itself the number of

suffixes.

in the


, , 15-25 2013

stdin
abracadabra
cabcab

stdout
3
5

C. A Coloring Game
:
:
:
:

stdin
stdout
2s

256 Mb

Two players play a graph coloring game. They make moves in turn, first
player moves first. Initially they take some undirected graph. At each move,
a player can color an uncolored vertex with either white or black color (each
player can use any color, possibly different at different turns). Its not
allowed to color two adjacent vertices with the same color. A player that
cant move loses.
After playing this game for some time, they decided to study it. For a
start, theyve decided to study very simple kind of graph a chain. A chain
consists of N vertices, v1, v2, . . . , vN , and N 1 edges, connecting v1 with
v2, v2 with v3, . . . , vN 1 with vN .
Given a position in this game, and assuming both players play optimally,
who will win?


The first line of the input file contains the integer N, 1 N 100 000. The
second line of the input file describes the current position. It contains
th
N digits without spaces. i digit describes the color of vertex vi: 0
uncolored, 1 black, 2 white. No two vertices of the same color are
adjacent.


On the only line of the output file, print FIRST (without quotes) if the
player moving first in that position wins the game, and SECOND (without
quotes) otherwise.


, , 15-25 2013

203

stdin

stdout

5
00100
4
1020

SECOND
FIRST

C. A Coloring Game
x0 : : : 0x x0 : : : 0y ( x; y
0 ). , 1,
0 . (0
) .
0 xor 0 1 xor 1, 1.
0 xor 1 1 xor 0.
,
0. , ,
,
, 2.

D. Hippopotamus
:
:
:
:

stdin
stdout
2s

256

You think that your roof looks unpretty. So you opt for a new one, consisting of
n consecutive long narrow boards. You have two types of boards: wooden ones
n
and iron ones, giving you an amazing total of 2 possible roofs.

But the safety should not be left aside. Having considered the weight
and the cruising speed of a falling hippopotamus, you decide to have at
least k iron boards among every m consecutive boards.
How many possibilities do you have?


The input file contains three integers, n, m and k, separated by spaces
and/or line breaks. 1 n 60, 1 m 15, 0 k m n.


Output the number of possibilities.
204


, , 15-25 2013

stdin

stdout

10 2
1
5 5 2
3 2 2

144
26
1

D. Hippopotamus

(- , m 1
). ,
. ,
0 1,
0 0.

E. False RSA
:
:
:
:

stdin
stdout
2s

256 Mb

Roman, Serge and Andrew has decided to upgrade the famous RSA
encryption algorithm. They think that the limitation that modulo n used in RSA
must be a product of two distinct primes is redundant. Instead they plan to use
k k
n which is the product of k-th powers of two distinct primes: n = p q .
However, Nick pointed out that besides all other mathematical problems,
the scheme may be easier to crack. That is it is difficult to factorize n
which is a product of two distinct primes, because it has exactly one nonk k
trivial factorization. On the other hand, in case n = p q there can be more
2 2
different non-trivial factorizations. For example, 100 = 2 5 has eight nontrivial factorizations: 100 = 2 50, 100 = 2 2 25, 100 = 2 2 5 5, 100 = 2 5 10,
100 = 4 25, 100 = 4 5 5, 100 = 5 20 and 100 = 10 10.
k k
Now Roman, Serge and Andrew wonder given n = p q , how many
different non-trivial factorizations of n are there?


18

The input
file contains one integer number n (6 n 10 , it is guaranteed
k k
that n = p q for different primes p and q and integer k > 0).


, , 15-25 2013

205


Output one integer number the number of different non-trivial
factorizations of n.

6
100

stdin

stdout

1
8

E. False RSA
, k. k.
k

, p < q, p <
p

18

k > 1, p <

(n), 10 .

(10 ),

. , k .
< 24. , , k log6 10

(a = p, b = q, c = p
, d = q ),
a b

p q , p
, c, p c, q ,
d. .

F. Perspective
:
:
:
:

stdin
stdout
2s

256 Mb

Breaking news! A Russian billionaire has bought a yet undisclosed NBA


team. Hes planning to invest huge effort and money into making that team
the best. And in fact hes been very specific about the expected result: the
first place.
Being his advisor, you need to determine whether its possible for your
team to finish first in its division or not.
More formally, the NBA regular season is organized as follows: all teams
play some games, in each game one team wins and one team loses.
Teams are grouped into divisions, some games are between the teams in
the same division, and some are between the teams in different divisions.

206


, , 15-25 2013

Given the current score and the total number of remaining games for
each team of your division, and the number of remaining games between
each pair of teams in your division, determine if its possible for your team
to score at least as much wins as any other team in your division.


The first line of the input file contains N (2 N 20) the number of teams
in your division. They are numbered from 1 to N, your team has number 1.
The second line of the input file contains
N integers w 1, w2, . . . , wN ,
th
where wi is the total number of games that i team has won to the moment.
The third line of the input file contains N integers
r 1, r2, . . . , rN , where ri is
th
the total number of remaining games for the i team (including the games
inside the division).
th
th
The next N lines contain N integers each. The j integer in the i line of
those contains aij the number of games remaining between teams i and j.
It is always true that aij = aji and aii = 0, for all i
ri.
j aij
All the numbers in the input file are non-negative and dont exceed 10 000.
P


On the only line of output, print YES (without quotes) if its possible for
the team 1 to score at least as much wins as any other team of its division,
and NO (without quotes) otherwise.

stdin
3
1
1
0
0
0
3
1
1
0
0
0

2
1
0
0
0
2
1
0
0
1

2
1
0
0
0
2
1
0
1
0


, , 15-25 2013

stdout
YES

NO

207

F. Perspective
, .
, . , , ,
, .
-
, ,

-,
, ,
, - .
, ,
- .

G. Circular Railway
:
:
:
:

stdin
stdout
2s

256 Mb

There are L stations along a circular railway, numbered 1 through L.


Trains travel in both directions, and take 1 minute to get from a station to
the neighbouring one (i.e., between 1st and 2nd, between 2nd and
3rd, . . . , between (L 1)-th and L-th and between L-th and 1-st).
There are n employees houses along the railway, and n offices, each
house or office located near a railway station. You are to establish a one-toone correspondence between houses and offices in such a way that total
travel time (sum of travel times of each employee) is minimized.


The first line of the input file contains two integer numbers, n and L (1 n
9
50000, 2 L 10 ). The second line contains n locations of the employees
houses, and the third line contains n locations of the offices. Each location
is an integer number between 1 and L. Some houses or offices or both can
be located at the same railway station.


Output the minimal total travel time followed by the description of the oneto-one correspondence. The description should be represented by n numbers

208


, , 15-25 2013

(one for each employee, ordered as in the input), denoting the 1-based
index of the office assigned to the corresponding employee.

stdin

stdout

3 15
1 2 10
11 12 13
4 12
2 5 8 11
6 9 12 3

9
2 3 1
4
4 1 2 3

G. Circular Railway
:
. , ,
,
, 2.
, ai ,
i i+1 (
, -, an ,
n 1),
a1 ( ai+1 ai = -, i + 1
). ,
a1, ai .
,
ai , ai -.
ai a1 , .

H. SETI
:
:
:
:

stdin
stdout
2 seconds
256 s

Amateur astronomers Tom and Bob try to find radio broadcasts of


extraterrestrial civilizations in the air. Recently they received some strange
signal and represented it as a word consisting of small letters of the English
alphabet. Now they wish to decode the signal. But they do not know what
to start with.

, , 15-25 2013

209

They think that the extraterrestrial message consists of words, but they
cannot identify them. Tom and Bob call a subword of the message a potential
word if it has at least two non-overlapping occurrences in the message.
For example, if the message is abacabacaba, abac is a potential
word, but acaba is not because two of its occurrences overlap.
Given a message m help Tom and Bob to find the number of potential
words in it.


Input file contains one string that consists of small letters of the English
alphabet. The length of the message doesnt exceed 10 000.


Output one integer number the number of potential words in a message.

stdin
abacabacaba

stdout
15

H. SETI
. ,

endP os.
,
.
.
.

I. 2-3 Trees
:
:
:
:

stdin
stdout
2s

256 Mb

2-3 tree is an elegant data structure invented by John Hopcroft. It is


designed to implement the same functionality as the binary search tree. 2-3
tree is an ordered rooted tree with the following properties:
the root and each internal vertex have either 2 or 3 children;
210


, , 15-25 2013

the distance from the root to any leaf of the tree is the same.
The only exception is the tree that contains exactly one vertex in this
case the root of the tree is the only vertex, and it is simultaneously a leaf,
i.e. has no children. The main idea of the described properties is that the
tree with l leaves has the height O(log l).
Given the number of leaves l there can be several valid 2-3 trees that
have l leaves. For example, the picture below shows the two possible 2-3
trees with exactly 6 leaves.

Given l find the number of different 2-3 trees that have l leaves. Since
this number can be quite large, output it modulo r.


Input file contains two integer numbers: l and r (1

5 000, 1

10 ).

, , 15-25 2013

211


Output one number the number of different 2-3 trees with exactly l
leaves modulo r.

6
7

stdin
1000000000
1000000000

2
3

stdout

I. 2-3 Trees
k l
p , 2 3 . 2-3 v l
p v p
2-3 p .

212


, , 15-25 2013

(23.02.2013 .)

...
, .
. Google (-
2008).
(, 2009) -
(, 2009-2010).

ACM ICPC, NEERC ACM ICPC. .


.
.

Google .
:

- (ACM ICPC Tokyo 2007);
TopCoder Open, TopCoder Collegiate Open,
Global Google Code Jam, Google Code Jam Europe;
(4- , )
- ACM-ICPC World Finals 2009,
, .


, , 15-25 2013

213

.

:
;
;
,
- .
, , ,

,
-.
( , ):
( ,
);
.
,
;
,
- .
,
. ,
:
;
;

.

- , .
-, P (W ),
:
P (A) P (A + e)
P (A) P (B)=>P (A + e)
P (B + e)
A B , e .
, .

,
-
.

s t, -.
,

214


, , 15-25 2013

. ,
:
, . d[v] s v e[v]
v t.
s t
,
u v.
w max(d[u]; e[v]),
w+max(d[u]; e[v]). .
.

-
. D[k][v] s -
v k . D[k + 1]
D[k]. , , N 1 ,
N . -
,
.
E[k][v] s v , k .
-
. N 1
W [v].
- W [v] k .
, W [v] D[k][v].
.

-
N W , ,
.
,
. ,
, - , N 1 ,
.


, , 15-25 2013

215


A.
:
:
:
:

stdin
stdout
1

256

N M .
.
.
.
1,
N.
,
. ,
. ,
.

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

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

, .
-
, , .

,
,
-, .
216


, , 15-25 2013


N, M K
(2 N
2 000, 1 M
100 000, 1 K
100 000). M
: Bi, Ei , i , Ti ,
(1 Ti 10 000).

K
, : Pi ,
, Vi , (1 Vi 10 000).
1 N.

.


,
, NO.

,
YES.
.
,
. .

.


, , 15-25 2013

217

stdin
4
1
1
2
3
2
2
2
3
4
4
1
2
2
1
2
2
3

4
3
2
3
4
1
2
1
4
1
3
2
3
4
2
1
2
5

stdout

5
6
2
2
1

YES
2
1 4

4
2
1
3

NO

A.
. (
).
, ,
. L,
R,
[L T; R], T
. , . , ,
( ,
).

218


, , 15-25 2013

B.
:
:
:
:

stdin
stdout
1

256

, , -
. .

, .
. , , -
. -
, . K .
,
, . , ,
K , ,
. 1,
N. ,
.
, , ,
1 ( ,
, ). -
, :
, , , , . .
, . ,

, .


N , M K (1 N 100; 0 K M 20000). M
: a b -,
, t
( , 1000).


, , 15-25 2013

219


,
. 1, .

5
1
2
1
3
4
1
1
1
2
2
3

4
2
4
4
5
6
2
3
4
3
4
4

2
1
1
1
1
2
1
1
2
1
1
1

stdin

-1

stdout

B.

. D[V ]
N.
N 0. V , V , .
, V .
U K + 1 (-
), D[V ] + W (V - , W U V ). , K
. D[U]
K D[V ] + 2 W D[V ] + W K + 1 . ,
U, K + 1,
.
- O(M logM).
N, O(N M
logM). -

220


, , 15-25 2013

D[V ] + 2 W D[V ] + W ,
O(M logM logK):

C.
:
:
:
:

stdin
stdout
1

256


,
-
.
, -
. :
1
.
p

2 .
.
, (, A
- , B C ).
, (
).
(, ;


). - , ,
, .


N (1
N 40). N X1, Y 1, X2, Y2, T ,
,
(, p
1 2 , ,
). ,
200, ,
7

10 .

, , 15-25 2013

221


,
, ,
, .
2- .
, .

stdin

stdout
0 0
1.00

1
0 0 111
5
0
1
0
0
2

0
0
0
0
2

0 0
3.25

011
0 1 10
101
111
111

3
1 1 1 2 10
1 2 2 2 10
1 1 2 2 50
16
0 0 011
-1 0 0 0 1
21101
00101

2 2
35.00

-2 1 3 1 1
03131
20111
0 1 1 1 1

-2 1 1 0 1
13221
20101
-1 1 1 2 1

-2 1 1 2 1
22211
21111
0 3 1 2 1

0 0
4.50

C.
, , . .
,
,
. ,
.
, ,

. -, ,
.

, . ,
, .
222


, , 15-25 2013

,
.

-.
- ,

.
, (
), .
?
,
.
, ! , :

,
,
.
. , . ,
1 ,
.


-. ,
.
-, , .

- , ,
. , ,
- :
1 f o r k:=1 t o N do


, , 15-25 2013

223


2
3
4

f o r i:=1 t o N do
f o r j:=1 t o N do
i f a[i,j]$<$a[i,k]+a[k,j] then a[i,j]:=a[i,k]+a[k,j];

.
A[i,j] ( A[i,j] -,
i j, ,
,
9

, 10 ). Ck[i,j] (k=0,. . . ,N). Ck[i,j] i j,


-
1 k. ,
1 k (
- ),
k+1 N. . ,
, .

.
C0[i,j] , . , C0[i,j]
A[i,j].
CN [i,j] ,
-
, .
C k 1[i,j],
Ck[i,j] : Ck[i,j]:=min (Ck 1[i,j], Ck
1[i,k]+Ck 1[k,j]). , - i
j,
1 k. :
k.
- 1 k-1.
Ck 1[i,j].
k.
: i
k,
1 k-1 ( C k
1[i,k]), k j
,
1 k (Ck 1[k,j]).
, Ck[i,j].
C0, C1, C2 ..
CN
224


, , 15-25 2013

.
, C k
- Ck 1,
N , ,
, , .
-,
(, ).
.

, ,
. -
,
, , ,
. .
L
T1 T2. T1 = T2+L T2 = T1+L,
, T1 T2
, . ,
T1 T2 L (, ).
T1 T2 L. ,
,
- . ,
,
.
- !
, , L/2. ,
.
T1 T2, .. -
, 0,
T.
T + (L-T)/2. . L 1
. T
, T ., ,
L-T 2 /, .. -
(L-T)/2. T = 0 L/2, T = L - L,
.

.


, , 15-25 2013

225

.
.
. ,
, . ,
. ,
, .
, 2.

.
, . ,
X/4, X . , 0.0, 0.25, 0.5 0.75,


. :
1 Program Matches;
2
3 Const
4
TaskID=f;
5
InFile=TaskID+.in;
6 OutFile=TaskID+.out;
7
8 Const
9
MaxN=42; { N }
10
MaxG=2*MaxN+1; { }
11
Infinity=MaxLongInt; { "" }
12
13 Var
14 N:Integer;
{
}
15 Match:Array[1..MaxN]Of Record
{
}
16
X1,Y1,X2,Y2:Integer;
{
}
17
Time:LongInt;
{
}
18 End;
{
}
19
20 NG:Integer;
21 Vertex:Array[1..MaxG]Of Record
22
X,Y:Integer;
23 End;
24 Edge,Distance:Array[1..MaxG,1..MaxG]Of LongInt; {
25
26 Res:Extended;
{
27 ResX,ResY:Integer; {
28
29 Procedure Load;
30 Var
31
I:Integer;

226

{
}
{
}
{ }
{
}
}
}
}


, , 15-25 2013


32 Begin
33
Assign(Input,InFile);
34
ReSet(Input);
35
Read(N);
36
For I:=1 To N Do
37
With Match[I] Do
38
Read(X1,Y1,X2,Y2,Time);
39
Close(Input);
40 End;
41
42
43
44 Function GetVertex(VX,VY:Integer):Integer;
45
{ , .
46
}
47 Var
48
I:Integer;
49 Begin
50
For I:=1 To NG Do
51
With Vertex[I] Do
52
I f (X=VX) And (Y=VY) Then Begin
53
GetVertex:=I;
54
Exit;
55
End;
56
57
Inc(NG); { }
58
With Vertex[NG] Do Begin
59
X:=VX;
60
Y:=VY;
61
For I:=1 To NG-1 Do Begin
62
Edge[I,NG]:=Infinity;
63
Edge[NG,I]:=Infinity;
64
End;
65
Edge[NG,NG]:=0;
66
End;
67
GetVertex:=NG;
68 End;
69
70 Procedure AddEdge(X1,Y1,X2,Y2:Integer; Time:Longint);
71
{ , }
72 Var
73
A,B:Integer;
74 Begin
75
A:=GetVertex(X1,Y1);
76
B:=GetVertex(X2,Y2);
77
Edge[A,B]:=Time;
78
Edge[B,A]:=Time;
79 End;
80
81 Procedure BuildGraph; { }
82 Var
83
I:Integer;
84 Begin
85
NG:=0;


, , 15-25 2013

227


86
For I:=1 To N Do
87
With Match[I] Do Begin
88
AddEdge(X1*2,Y1*2,X1+X2,Y1+Y2,Time);
89
AddEdge(X1+X2,Y1+Y2,X2*2,Y2*2,Time);
90
End;
91 End;
92
93 Procedure FindShortestPaths;
94 Var
95
K,I,J:Integer;
96 Begin
97
Distance:=Edge;
98
For K:=1 To NG Do
99
For I:=1 To NG Do I f Distance[I,K]<Infinity Then
100
For J:=1 To NG Do I f Distance[K,J]<Infinity Then
101
I f Distance[I,K]+Distance[K,J]<Distance[I,J] Then
102
Distance[I,J]:=Distance[I,K]+Distance[K,J];
103 End;
104
105
106
107 Function BurnAt(At:Integer):Extended;
108
{ , At }
109 Var
110
I,J:Integer;
111
Cur,ThisEdge:Extended;
112 Begin
113
Cur:=0;
114 For I:=1 To NG Do I f Distance[At,I]>Cur Then Cur:=Distance[At,I];
115 For I:=1 To NG Do
116
For J:=I+1 To NG Do I f Edge[I,J]<Infinity Then Begin
117
I f (Distance[At,I]<Distance[At,J]+Edge[I,J]) And
118
(Distance[At,J]<Distance[At,I]+Edge[I,J]) Then Begin
119
I f Distance[At,I]<Distance[At,J] Then
120
ThisEdge:=Distance[At,J]+(Edge[I,J]-(Distance[At,J]-Distance[At,I ]))/2
121
122

E l se
ThisEdge:=Distance[At,I]+(Edge[I,J]-(Distance[At,I]-Distance[At,J ]))/2;

123
I f ThisEdge>Cur Then Cur:=ThisEdge;
124
End;
125
End;
126
BurnAt:=Cur;
127 End;
128
129 Procedure Solve;
130 Var
131
I:Integer;
132
Cur:Extended;
133 Begin
134
Res:=Infinity;
135
For I:=1 To NG Do
136
With Vertex[I] Do
137
I f Not Odd(X) And Not Odd(Y) Then Begin

228


, , 15-25 2013


138
Cur:=BurnAt(I);
139
I f Cur<Res Then Begin
140
Res:=Cur;
141
ResX:=X Div 2;
142
ResY:=Y Div 2;
143
End;
144
End;
145 End;
146
147 Procedure Save;
148 Begin
149
Assign(Output,OutFile);
150
ReWrite(Output);
151
WriteLn(ResX, ,ResY);
152
WriteLn(Res/2:0:2);
153
Close(Output);
154 End;
155
156 Begin
157
Load;
158
BuildGraph;
159
FindShortestPaths;
160
Solve;
161
Save;
162 End.

D.
:
:
:
:

stdin
stdout
1

256


. ,
,
: ,
, - ,
, .
, . ,
- .
,
,
,
-.

, , 15-25 2013

229

,
. . ,
.
,
, .


N , (1 N 3000). N
Ai, Bi,
6
(0 < Ai < Bi < 10 ).



, .
,
-.
, .

stdin
5
1 10
10 12
1 10
1 10
23 24

stdout
5
5 10 12 23 24

D.
-
.
- ,
. :
,
.
- :

230


, , 15-25 2013

:
1 const
2
MaxN=3000;
3
Infinity=MaxLongInt; { }
4
5 type
6
TSegment = record
7
left, right : longint;
8 end;
9
10 var
11 n : longint;
12 segment : array [1..MaxN] Of TSegment;
13 point : array [1..MaxN*2] Of longint;

{ }
{ }
{, }

,
.
(N 3000) ,
- ,
.
.
- . :
[1,6], [3,7], [12,14], [14,16].

[1,6] . ,
5 6. ?
, .
! ,
, -.
,
, :
,
.


[3,7]. , , 1 2,

, , 15-25 2013

231


, . 6 ,
5 . :
1 PrevLast := right-1;
2 Last := right;
3 { right .}

[3,7]
, ,
.
1 i f left <= PrevLast then < >;

[12, 14]. ,
lef t > last.
.

[14, 16].
- , Last = lef t.
16.
14.
1
2

PrevLast := Last;
Last := right;

, . , ,
,
.
- , :
1 procedure solve;
2 var
3
Last, PrevLast : longint; { }
4
i : longint;
5 begin
6
res := 0; { }
7
Last := -Infinity;
8
PrevLast := -Infinity;
9 f o r i := 1 t o
N do
10
with segment[i] do
11
i f last <
left
then
{ }
12
begin

232


, , 15-25 2013


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 end;

inc(res);
point[res] := right-1;
inc(res);
point[res] := right;
PrevLast := right-1;
Last := right;
end
else
i f PrevLast < left then
begin
inc(res);
point[res] := right;
PrevLast := Last;
Last := right;
end;

{ }

E.
:
:
:
:

stdin
stdout
1c

256

, . , ,
,
. 6 : (N), (S), (W), (E), (U)
(D). .
,
.
N, S, W, E, U, D.
M.
,
. 1,
, , , ,
1. 1, . , ,
:


, , 15-25 2013

233


N
N
S
NUSDDUSE
W
UEWWD
E
U
U
D
WED
S(3)
:
1) 1 S;
2) N(2), U(2), S(2), D(2), D(2),
U(2), S(2), E(2).

- 2, ,
-: SNNUUSNUSDDUSEDWEDDWEDUUSNUSDDUSEE
- ,
.
- 34.



- N, S, W, E, U D .
100 ( ).
: N, S,
W, E, U, D,
, - 100.

, . ,
9

10 .

stdin
N
NUSDDUSE
UEWWD
U

stdout
34

WED
S3
234


, , 15-25 2013

E.

.
- L(C), L .
L(C + 1)
R(C). d[L][C] ,
L(). L ABC:::Z,
d[L][C+1] d[A][C] + d[B][C] + ... d[Z][C] + 1.

F. Shortest Path
:
:
:
:

stdin
stdout
1

256

s .
u s u


: n, m s -
,
(2 n 2 000, 1 m 5 000).
m .
- .
15
- , 10 .
. .


n - s u
u, *, -,
s u.


, , 15-25 2013

235

stdin
6
1
2
1
3
5
4
6

stdout

71
2 10
35
3 100
57
4 10
3 -18
1 -1

0
10
*

F. Shortest Path
-,
- .

G.
:
:
:
:

stdin
stdout
3
256

, . n
. .
( i j), ,
cij
i j.
: ,
- ? ,
?


n m
n(n 1)
), n , m (1 n 300; 0 m
2
. : m
i, j, cij cji (1 i n; 1 j n; i 6= j; 104 cij
4
4
4
10 ; 10 cji 10 ). ,
. .

236


, , 15-25 2013


,
- ,
, 0, .

stdin
4
1
1
2
3

4
2 -10 3
3 1 -10
4 -10 -1
4 0 -3

stdout
4

, a1,

a2, . . . , ak, a1 a2, a2 a3, . . . , ak 1 ak,


ak a1.
, . k, .
, .

G.
-.
,
. ,
. : .
- ,
. , .


, , 15-25 2013

237

H.
:

stdin

:
:
:

stdout
1
256

, , N . -

D( -).
, , .
.


N (1 N 1000).
S1 S2 (1 S1; S2 N; S1 6= S2)
, .
9
R (0 < R 10 ) . D (0
9
< D 4 10 ). N
G1 T1 G2 T2, :
G1 , (0 G1 90); T1
: N , S ;
G2 , (0 G2 180); T2
: E , W .


,
, 1, .

stdin
2 1 2 10000 31416
89 S 13.12 W
89 N 78.8 E
3 1 3 10000 15000
89.9 S 13.12 W
0.001
S 63.12 W
89 N 78.8 E

238

stdout
1
-1


, , 15-25 2013

H.
. , , D. ,
.
- .
.

I.
:
:
:
:

stdin
stdout
1

256

- . . , , ,
, . , ,
, , (-
, ,
). 1 N.
1 , ,
0 . ,
, E. ,
- ,
.


N (2 N 100) E (2 E N).
M (2 M 100), -
. M .
Ki (2 Ki N)
, , K i
, ,
9
( 0 10 ). -
.

, .


, , 15-25 2013

239


,
.
, 1.

stdin
5
4
2
2
4
3

stdout

3
15
2 10
50
12

20
210
4 15
417 3 20
340 4 45

2 35

I.

.
, . ,
.

J.
:
:
:
:

stdin
stdout
1

256

" - ,
, . .
.
.
( )
, . , ,
.
:
? - , .

240


, , 15-25 2013


N ,
- (2 N 30000). N ,
.
(
, ). , ,
, 32000.


,
-.

stdin
13
53135253135
2 5
4
1 1 1 1
4
1 2 3 1

stdout
6
1
3

J.
,
: Ai
N. Bi ,
- .

, Bi : Ai ( ).
. Ai N

, , 15-25 2013

241

Bi

K.

- Bi Ai
, N K. , K 1
N. -, K N ,
Ai Bi. .
. N = 12, K = 2. ,
- Ai
. Ai N=K = 6 ,
Bi .

,
- Bi .

:
1 j := 1;
2 while (j <= n-k) and (a[j] = a[j+k]) do inc(j, k);

.
(2 6= 3), 2 .


Bi 1 K.
Pascal:
1 res :=
0;
2 f o r k := 1
t o n do { }
3
i f n mod k=0 then
4
begin
5
Ok := t r u e;
6
f o r i := 1 t o k do
7
begin
8
j := i;
9
while (j <= n-k) and (a[j] = a[j+k]) do inc(j, k);

242


, , 15-25 2013


10
11
12
13
14
15
16
17

i f j <= n-k then


Ok := f a l s e ;
{
}
end;
i f Ok then
{ ,
begin
B }
res := k;
break;
end;
end;

.
. N = 12; K = 4. Ai N=K = 3
,
Bi.


-
,
- .
, ,
a = b b = c , a = c.

. Ai ( j) a[j]

a[j + k].

1
2 res :=
0;
3 f o r k := 1
t o n do { }
4
i f n mod k=0 then


, , 15-25 2013

243


begin
j := 1;
while (j <= n-k) and (a[j] = a[j+k]) do inc(j);
i f j > n-k then
begin { , B }
res :=
k;
break;
end;
end;

5
6
7
8
9
10
11
12
13

, nmodk = 0 . , 30000 50 , 50
Bi, 30000. 6
, N > 10
.
. Ai .


Ai.
Bi (,
!) , Ai,
- Ai.
.
-
- , -. - ,
, . , . , . :
, .: -, 2000.

K.
:
:
:
:

stdin
stdout
1

256

NxN
- ,
KxK S .

244


, , 15-25 2013


N, K,
2
S (1 N 100; 1 K N, 0 S K ).


.
- ,
. ,
.

stdin
3 2 1
4 2 2

stdout
0
0
0
1
0
1
0

0
1
0
0
1
0
1

0
0
0
0
1
0
1

1
0
1
0

K.
. , .
( ) - . :
K K, S , -
N N.
. . - K
K, S . - .
, .
, ,
. -
S , , .
2

S > K , - .
Pascal,
. gen k, -
s . true,
,
false :


, , 15-25 2013

245


1 const
2
MaxK=100;
3
4 type
5
Square = array [1..MaxK, 1..MaxK] o f byte;
6
{ KxK, }
7
8
9
10 function gen(k, s : word; var a : Square) : boolean;
11 var
12
i, j : i n t e g e r ;
13
CurS : word; { KxK }
14 Begin
15
CurS:=0;
16
fillchar(a, sizeof(a), 0); { }
17
f o r i := 1 t o k do
18
f o r j := 1 t o k do
19
i f CurS < s then {
}
20
begin
21
a[i, j] :=
1; { }
22
inc(CurS);
23
end
24
end;
25 i f CurS < s then gen := f a l s e e l s e gen := t r u e; 26 end;

. K K N N. ,
- . N = 7; K = 3; S = 4.
gen :

7*7:

246


, , 15-25 2013

,
3x3 , 4. ?
3x3 ,
. (1,1)
, ,
, , -.
(1,2)

,
. ?
, 4 -
-!
,
.
, .
, . ,
-, ! -,
.

, ,
, !
, N N.


, , 15-25 2013

247

N*N
. :
1 i f gen(k, s, a) then
{ }
2
f o r i := 1 t o n do
{ N*N}
3
begin{ }
4
f o r j := 1 t o n do
5
write(a[(i-1) mod k+1,(j-1) mod k+1], );
6
w r i t e l n;
7
end;


:
:
:
:

stdin
stdout
1

64

,
.
, f
. x
f(x). -
.


.
- 100000.
x f(x),
x. .


:
.

248


, , 15-25 2013

stdin
1
1
2
1 2
2
2 1

stdout
1
2
1

(. strongly connected),
.
s t ,
s t t s.


- .

249

, , 15-25 2013

(23.02.2013 .)

...
,
1989 . -
. 9-
, 11- - . -
. , .

:
,
- .
ACM ICPC (2012 2013
). Russian Code Cup 2012.
2011 Codeforces.


A.
:
:
:
:

stdin
stdout
1

256

.
4 4 16 .
, .

250


, , 15-25 2013

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


n (1 n 100).
n , . 4
4 .
, <, >.



, .
First,
Second. Draw.


, , 15-25 2013

251

stdin
3
<<<<
<<<<
<<<<
<<<<
<<<<

stdout
Second
First
Second

<><<
<<><
<<<<
<<>>
<<>>
>><<
>><<

A.
16- ,
0 , 1 . ,
-,
( ,
1 0).
. .
.
-
. .

First Second. Draw
.
16
3
O(2 16 + n).

252


, , 15-25 2013

B.
:
:
:
:

stdin
stdout
1c

256

.
, n m .
, . .
,

.
.
.
, .
( ).

( ).
:
, , ,
- .
,

- (
).

.
( )
, - .

.
, .


n m (3 n; m 50).
n m
- . :
. .

, , 15-25 2013

253

(). H (Harry) .
P (Philosophers stone)
. E (Exit) .
B (Blue fog) .
R (Red fog) .

. H,
P E . P
# R.



- IMPOSSIBLE .

, :

L (Left) .
R (Right) .
U (Up) .

D (Down) .
G (Get) .
T (Throw) .
.
.

254


, , 15-25 2013

stdin

stdout
RRGLLTRRDDLLGRR

55
#####
#H.P#
#B#R#
#..E#
#####
45
#####
#H#.#
#P#E#
#####

IMPOSSIBLE

B.
, : ,
. ,
- , .

- ,
, - .
.
IMPOSSIBLE.
2

O(n m ).

C.
:
:
:
:

stdin
stdout
1

256

a 1; a 2;

; an.
:
-

ai ! ai ai+1 ( i = n an ! an a1),
.

, , 15-25 2013

255

a 1; a 2;

; an

a1

a 2; a 2 a 3;

; an

a1:

n (2 n 10 ).

n . i- a i
9
(0 ai 10 ).

,
- .
Never
.

stdin
8
3 6 7 24
3
0 1 1
5
0 0 0 00

1 0 5

stdout
5
Never
0


-:
: 3, 6, 7, 2, 4, 1, 0, 5;
1: 5, 1, 5, 6, 5, 1, 5, 6;
2: 4, 4, 3, 3, 4, 4, 3, 3;
3: 0, 7, 0, 7, 0, 7, 0, 7;
4: 7, 7, 7, 7, 7, 7, 7, 7;
5: 0, 0, 0, 0, 0, 0, 0, 0.
256


, , 15-25 2013

C.
k

, 2 (k 0),
:

a1

a1+2k ; a2 a2+2k ;

; an

an+2k :

( , 1 n,
, ).
k
n = 2 . n (
- ) .
- 0 n.
m
n = 2 t ( t 2).
x m
m
x m
2 2 mod 2 t. : 2
1 mod t.
x = m + z (t). x
:

a1

a1+2m ; a2 a2+2m ;

; a2

an+2m :

, a i ai+2m ,

(
) Never. i a i =
ai+2
m m , n =
2 . 0 n.
, , ,
0 n. .
O(n log n).

D.
:
:
:
:

stdin
stdout
3
256

.
,
.
, . .

. .


, , 15-25 2013

257

,
. ,
.

- .
- ,
- .
.

,
. ,
, .

- ( )
-.

.
.
,
. . ,

,
.

.

n (1 n 10 ).
n . i- op
x1 y1 x2 y2. op , add del
. x1 y1 , x2 y2
. ,
(x 1 = x2 y1 = y2),
(x1 6= x2 y1 6= y2).
9
1 10 .


n . i-
i- .

258


, , 15-25 2013

stdin
7
add 3 1 3 6
add 2 2 6 2
add 5 1 5 6
add 1 4 7
del 5
35
add 3 5 5
add 5 2 5

4
5
5
7

stdout
0
1
2
4
3
5
6

D.
.
- ,
. -, x0 x.
,
x- x0.
,
- .
, ,
-. (,
- , ).
.
- ,
, .
,
- , ,
.
- O(n),
2 ,
- . , set C++,
O(n log n).
2
-: , (,
).

,
. -
.


, , 15-25 2013

259

(x; y 1) (x; y2) (y1 < y2),


(x; y1) +1, (x; y2 +1) -1. (x1; y) (x2; y) (x1 < x2)
(x1; 0) (x2; y).


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

E.
:
:
:
:

stdin
stdout
1

256
5

100 1 10 .
4
, .

5
1 10 .

, 4
- .

10 10 . 1 10 .
.


4 . i- i-
- .
-, ,
. .
.

260


, , 15-25 2013

stdin
1 2 34 5 67 8
11 12 13 14 15
18 19 20
21 22 23 24 25
28 29 30
31 32 33 34 35
38 39 40
41 42 43 44 45
48 49 50
51 52 53 54 55
58 59 60
61 62 63 64 65
68 69 70
71 72 73 74 75
78 79 80
81 82 83 84 85
88 89 90
91 92 93 94 95
98 99 100

stdout
9 10
16 17
26 27

1 100
3 20 30 50
5 1 2 3 4 90
2 35 65

36 37
46 47
56 57
66 67
76 77
86 87
96 97

,

-.

E.
,
-. 100000
- . .
, .
- .

6
. .
k (k 19-20)

(A1; B1); (A2; B2);

; (Ak; Bk), Ai = (ai1 ; ai2 ;


, , 15-25 2013

), Bi = (bi1 ; bi2 ;

),

261

a- b- , P

. , j aij =
j bij i.
j aij . .
.
.
X Y , .

0
0
. X Y .
,
0
. Ai, i 2 X . Ai
, .
0
Bi , i 2 X .
0
0
X Y .
P

262


, , 15-25 2013

(24.02.2013 .)

...
- 1989 . - 30. 4-6
. 9- .

2012-

-.
-

.
2008-2009 .

http://kruzhok.spbgu.ru/09e http://195.19.228.2/%7Esk1/mm/345/
-
.
2012- .
( 2011-)
- ( 2004-, )
:
IOI 2005 () 2006 ()
. TopCoder Open Algorithm Final 2008, 5-th place.
2008 (
-) 1- .
ACM
ICPC 2007 .
ACM Burunduchki.
2008 () 2009 () .


, , 15-25 2013

263

: Google Code Jam Topcoder Open.

. 20

1.
1.
.
c1 c2 ,
- p, 8i; j : c1[pi; pj] = c2[i; j].
2.
.
, (proot1 = root2).
1. . O(nlog n).
. , .
- ,
. - , .
:
-.
1. .
O(n log n), , O(n). . -
. :
(1; 2); (1; 3); (1; 4) (1; 2); (1; 3); (3; 4); (3; 5).
X

a0; a1; : : : ; ak = aip


i=0

2
: 1 + 1 p + 1 p = 1 + (1 + 1 p) p.
, , :
k

13 +

i=0

log ai

C++ long double.


, , a i, . . ,
264


, , 15-25 2013

. :
X

=0

ai + aip + 3

mod 2

64

2. . O(nlog n).

, .
-, - ,
, .
, .

3. . O(n).
1
n. 1. ,
- , ,
-,
. - , .
.
:
1
list[1]<--

2 f o r type = 1..n
3
f o r vi n list[type]
4
p = parent[v]
5
children[p].push_back(type)
6
i f children[p].size() = degree[p] then
7
h = hash(children[p])
8
t[p] = get_type(h)
9
list[t[p]].push_back(parent[v])


. , ,
, (
) ( ) .

4. -.
-? 1 n
1 n. .
() ,
, 1 n , .
, n, -


, , 15-25 2013

265

. O(1), , ,
, -.
5. O(n). .
, .
-.
-, ,
. ,
children[p].push_back(type)
,
pos[p] = go_down(pos[p], type)
, type ",
- ( type).

2. offline

a, q , .
- Offline . :
?
li " ri "
,
(li li+1; ri ri+1),
L++ R++.
?:
1 map <i n t , i n t > cnt;
2 i n t answer = 0, L = 0,
R = 0;
3 f o r (i n t i
= 0; i < q; i++) {
4
while (R <= r[i])
5
i f (cnt[a[R++]]++ == 0)
6
answer++;
7
while (L < l[i])
8
i f (--cnt[a[L++]] == 0)
9
answer--;
10
result[i] = answer;
11 }

O(q log n). 2q


t(n), t(n) L++
R++.
266


, , 15-25 2013

li ri
p

n . i- pl
b in c. ri
. R , L p

, ,
n.
: R p p
n n ( np, n), L p
q n (

q

n).
p
(q + n) n t(n), t(n) l--,
l++ r++.

3.
,
n m , (1; 1) (n;
m), (0; 1) (1; 0).
.
, O(nm):
1 i n t f[n + 1][m
+ 1];
2
f[][] <--- 0
3 f o r (i n t i
= 1; i
<= n; i++)
4 f o r (i n t
j = 1; j <= m; j++)
5
f[i][j] += a[i][j]
6
f[i + 1][j] = max(f[i + 1][j], f[i][j])
7
f[i][j + 1] = max(f[i][j + 1], f[i][j])

( ) f[n][m].

, i- f i + 1-
( ). :
1 i n t cc =
0, f[2][m + 1];
2 f[cc] <--0
3 f o r (i n t i =
1; i <= n; i++)
4
f[cc ^ 1] <-- 0
5
f o r (i n t j = 1; j <= m; j++)
6
f[cc][j] += a[i][j]
7
f[cc ^ 1][j] = max(f[cc ^ 1][j], f[cc][j])
8
f[cc][j + 1] = max(f[cc][j + 1], f[cc][j])
9
cc ^= 1

( ) f[cc1][m].


, , 15-25 2013

267


,
-, ( )
, p[n+1][m+1],
(i; j) -, .
.
.

: i=1, i=n/2, i=n. -
p[m+1],
(1; 1) (n; x) (n=2; p[x]). ,
: (1; 1) ! (n=2; p[m]) ! (n; m). , , , (y1; x1) (y2; x2)
O((y2 y1 + 1) (x2 x1 + 1)), (1; 1) (n=2; p[m])
( ), (n=2; p[m]) (n; m) ( ). : T (n; m) =
n

nm + T (n=2; x) + T (n=2; m x) = nm + 2n m + T n(n=4; x1) + T (n=4; x x1) +


T (n=4; x2) + T (n=4; (m x) x2) = = nm + 2 m + 4 m + ::: 2nm.
, x-
m. ,
.


: s n ci wi.
, .
- .
, O(ns):
1 i n t f[n
+ 1][s
+ 1];
2 f[][] <--- 0
3 f o r (i n t i
= 0; i < n; i++)
4 f o r (i n t j = 0; j <= s; j++)
5
f[i
+ 1][j]
= max(f[i
+ 1][j], f[i][j])
6
i f (j + w[i] <= s[i])
7
f[i + 1][j + w[i]] = max(f[i + 1][j + w[i]], f[i][j] + c[i])

,
(1; 0) (0; 1), (1; 0) (1; w[i]). .
, ,

268


, , 15-25 2013

,
, . (n=2; p[x]) ,
n=2 p[x] ,
, -
n=2 p[x],
n n=2 s p[x].

4.


+ for.

1. . , ti.
.
0 d
.
. ti.
2. . , ti
deadline di. .
0
deadline-.
. di.
3. . , mi ,
-, w i.
, .
.
mi

+ wi. :

wi
j6=i

+ mi
mj ) w i

wi + mi

mj = const )
.

4. . , ti
wi. i- C i,
wiCi.
1

(, s)
, . ,
.


, , 15-25 2013

269

. 0.
, .
.

wi

. :

ti

( ) ,
, . : w2t1 > w1t2.
5. . n , ,
.

.
-
bool less(string a, string b) { return a + b < b +
a; }
: . .
.
- . ,
n = 2 , (1; 2)
(2; 1) . ,
- . (4)
F (1; 2) = w 2t1 , .
(5) F (s; t) = st .
- F :
bool less(type a, type b) { return F(a, b) < F(b, a); }
, , ,
- .


.
1. . : n
, i- ai
, bi .

. ,
, .
. n = 2. F (1; 2) = a1 + max(a2; b1) + b2.
:
i < j , ai + max(aj; bi) + bj < aj + max(ai; bj) + bi
, ai + aj + bi + bj = const,
: i < j , min(aj; bi) > min(ai; bj).
270


, , 15-25 2013

2. . , .
,
.
. -
.
( )
(ai) (b i). ai 0, ai bi.
, ai 0.
(.. bi 0). ai.
ai bi < 0. -
,

+ bj) > min(bj; aj + bi). ,


ai + bj < bj aj + bi < bi, ai bi > i < j , ai + bj > aj + bi ,
. i < j , min(bi; ai
aj bj.

,
,
deadline-.
1. .
-. ,
.
. . , , -
, .
. . -
. Mij, i
, j , Mij


+1, .
, wi Mij, .
j : Mnj 6= +1.
2
O(sort) + O(n ).
2. . . s.
,
(.. , ),
. , . Bij


, , 15-25 2013

271

,
- i j,
1, , .
j : Bnj 6= 1.
O(sort) + O(ns). 1745.
3. deadline-.
deadline di ti,
.
di. -:
. = ( di) + ( Tij),
i , j
, Tij .


deadline- ,
O(n log n).
1.
: ,
- ,
di. : di
.
(.. deadline),
- . , t i < tmax,
tmax
. tmax, set<int>.
2.
: 8i : di ti ( ,
), ti ( ,
, ). :
ti,
ti. , . :
ti.
A = {,
}. , j A,
A di , .
O(log n), j, A
di
mi =

min

f i = di

fjj2.

tj,

j2

272


, , 15-25 2013

5.

1; 2; : : : n, .
(1; 2), (2; 3) (1; 3), (3; 2) . ,
n 2
n n . , , , :
,
. n 1 ,
n. , ,
n 2
n .
.


.
n-
n . . fn;d n
d ( ).

1. fn;d :
2d n+1
Xd

i<d

2a<n
X

a<n

f f
) + (f
f
F
)
(x +
a;d n a;d
n;d
a;d 1 n a;d
a=1
a=1
=1 P
n
(
)
i=1
Fn;d =
fn;i, x = D(fn=2;d; 2), n ,
. D n; k
n+k 1
k . D(n; k) =


k

, .
2.
n d. (d; n) .
d,
n. fn;d, gn;d;x,
. gn;d;x
n ,
(d; x). fn;d = gn;d;1 gn;d 1;1 (
n d + 1


, , 15-25 2013

273

n d,
, ).
3. gn;d;x.
ax<n

n;d ;x

=
0

a=0

n xa;d;x

D(f ; a)
x;d

(d0; x0) (d; x)


(d := d, x := x+1; if (x = maxN) {d++, x := 0})
4. : f1;1 = 1, g1;1;1 = 1.
,
1. ,
.
2. , g d (..
d ! d; d + 1). ,
) ) .
3. a > 0 fx;d = 0 , D(fx;d; a) = 0, , fx;d =
0, a , break a.
4. D-, C- ( n k) .
f+k
f+k
f+k
D(f; k + 1) =
= f+k k1
= D(f; k)
k+1
k+1
k+1
D(fx;d; a) O(1).
2

, O(n ) O(n log n)


, ( ax < n,
a x, , O(n log n)).
,

32
n 2 . ,
unsigned int,
.
1

32

'(2 ) 1

32

: a mod 2 = a 32 mod 2 = a
32
2 1 2
32
mod 2 = (a 16 ) a mod 2 = : : : -

231 1

10 . ,
?

274


, , 15-25 2013

, -

( ). -
k
2 a, a , . :
(k1; a1) (k2; a2) = (k1 +kn2; a1 a2), : (k1; a1)=(k2; a2) = (k1 k2; a1=a2).
k , .
f
,32 x;dk , fx;d
2 32. :

32
n k 2 , n 2 ? :
64
. :
32 2 ,
, 2 :-).

6.

1. . k n- .
- ,
, , n- ,
, - f(i) ,
i- .
-
i, x(i) = 0; x(i + 1) = 1.
2. . fn;k (n; k). f

2;k

= 0, f

n;k

= min

i=2::n

max(fi;k

; fn

i+1;k

). ,

O(n k).
3. 1. k log2 n, ,
2 k = min(k; dlog 2 ne).
O(n log n).
4. 2. fi;k fi+1;k, fi;k 1#, fn i+1;k", -
max(fi;k 1; fn i+1;k) (
2
fi;k 1 fn i+1;k). O(n log n).
5. 3. pn;k i,
. pn;k pn+1;k
: pn+1;k = pn;k ,
, pn+1;k 1.
O(n log n).


, , 15-25 2013

275

6. . 1 n; k 10

18 ,
1

,
, fn;1 = n 2, fn;2 n , fn;k n
- (
) ,
.
2

(Day1-G)
1. . n ,
k , n
- k .
2. . , n k
. O(1)
( k, -
k
2 , ), ,
O(1), .
fi;j , ,
i j . -
, j = 1. :

f = min (f
i;j

p;j 1

+s

p+1;i p=1:::i

sl;r [l : : : r]. ,

sl;r = 0.
O(nk)
2
O(n k).
3. . pn+1;k pn;k. ,
,
- (n; k). pn;k pn 1;k.
: pn;k 1 pn;k pn;k+1.
. , p n;k
fn;k, .
:

n;k 1

n;k

n+1;k

pn;k . -
pn+1;i = n. =
XX

i=2 j=1

(pj+1;i

pj;i

+ 1) = O(n )

, pi;j ,
276


, , 15-25 2013

, . .., .
2
, k
2
O(n ).
2
n . k = 2, ai

pn;k = n

.
j+1

k = 2: =1(pj+1;2 pj;1 +1) = j=1(

0+1)

= i, , ..

n 2
4 .

k=1

O(1)

command-post
1. . n ,
3 k ,
,
. , ,
, ,
, .
2. . : l, r, (l : : : r)
k . [i; i; k 1] i.
(i; i) , . :
f l;r;k
= max (fl;m;0 + f m;r;k 1 )
m=l+1::r

O(n k).
3. 1. k 1 k
1, k=2
k k=2. k O(log k),
3
O(n log k).
4. 2. m pl;r. ,
pl;r 1 pl;r pl+1;r. pl;r . k
P

l;r

(pl+1;r

pl;r 1). pi;j ,


2

, ,
O(n ).
2
O(n log k).

7.
: n
- . 1 + 2 2 +
1 - .


, , 15-25 2013

277

k
,

f =f
n;k

n k;k

+f

n;k 1

:
1

f[0] = 1

2
3
4

f o r (x = 1; x <= k;
f o r (i = 0; i + x
f[i + x] += f[i]

x++)
<= n; i++)

f =f
n;k

n k;k

+f

n k;k 1

:
1

f[0] = 1

2
3
4

f o r (x = 1; x <= k;
f o r (i = n - x; i
f[i + x] += f[i]

x++)
>= 0; i--)

k
,

f =f
n;k

n k;k

+f

n;k 1

: 1, ,
- .
-,

f =f
n;k

n k;k

+f

n k;k 1

, ?
, , . ,
90 ( ).
n
n . ,
. 90

.
k k
P
fk k, Fk k, fk = Fk
278

Fk 1, Fk =

fi


, , 15-25 2013

k
k . i- i. , n k(k+1) k
, ,
p ,
k
p n ).
O(n
2

8.
:
1. . mi
wi, .
.
2. deadline-. deadline di ti.
deadline-.
3. . -
H. li hi.
(

). li + hj H,
.
, ,
.

4. A. n
-. i- ,
ai . ,
i- ,
bi. ai, bi , , .
- ,
.
$ deadline-
P
mj
wi
mi +
mj
wi + mi
di = wi + mi, ti = mi ( ti > 0, mi > 0)
P


, , 15-25 2013

279

$
P

li + (S

hi S, .

hj) H (i , j ) (S H) + li

( )
mi = hi, wi = li + (S

hj

H) ( mi > 0, hi > 0)

$
.
, (bi 0).
ai .
A,

(bi < 0).


P

A + b a (i , j bj,
)j i
P
bj
A ai
wi = A
ai, mi =
bi ( mi > 0, bi < 0)

,
deadline- O(n log n)
set<int>, , ,
,
O(n log n).


A. n
:
:
:
:

stdin
stdout
0.5
256

, -!, ,
- .
! .
n , . ,
, i- a i ,
bi . ,

280


, , 15-25 2013

, ,
, .
, , a1 = 1, b1 = 10, a2 = 10, b2 =
20. ,
10 , ,
. ,
10 .


n (1 n
a1; a2; : : : an, b1; b2; : : : bn (1
9

10 ),
ai; bi

10 ).


n ,
- . ,
1.

stdin

stdout

2
1 10
10 20
2
10 10
10 10

-1

A. n
ai + bi.

B.
:
:
:
:

stdin
stdout
0.5
256

-
, . ,


, , 15-25 2013

281

,
.
,
. , ai, bi.
j k i- ,
bj < ai < bk, bk < ai < bj.

, - ,
.
, .

,
, ,
.


m n
, (1 m; n 100 000).
m ai (0
9
ai 10 ).9 n bi
(0 bi 10 ).

k , -
. k : d i,
ei;1, ei;2 , . , .
, bei;1 < adi < bei;2 .
, , ,
, .

stdin
4 6
2 3 4 5
1 3 2 25

stdout
2
1 1 2
3 4 5

B.
a b .
. , k, ,
282


, , 15-25 2013

i- (bi; bn k+i),
.

C.
:
:
:
:

stdin
stdout
0.5
256

N -
. ,

. ,
.
H. hi
li. , , , -
, h i + li .
, , .
. i j1; j2; : : : ; jk,
hj1 + hj2 + : : : + hjk + hi + li.

( h j1 + hj2 + : : :
+ hjk + hi + li H), .
.
,
, .


N
- , . N
: i- h i li .
H.


K ,
. K > 0, , . ,
. , .


, , 15-25 2013

283


1 (50 ) n 2 000
1 li; hi; H 10
9
n
100
000
1
2 (50 )
li; hi; H 10

2
10 4
5 2
20
6
6 7
3 1
8 5
8 5
4 2
10 5
30

stdin

stdout

4
1 4 2 5

C.
. . 8, 3, 4,
deadline-.

D.
:
:
:
:

stdin
stdout
0.5
256

.
- . . i-
,
ai ( ). i-
-, b i
( , bi < 0).
.

284


, , 15-25 2013


.
9
9
n a0 ( 10 a0 10 ). 9
9
n ai bi ( 10 ai; bi 10 ).


m ,
- . m
, .


1 (50 ) 1 n 10
5
1
n
10
2 (50 )

stdin

stdout

51
13
6 -5
6 -4
22
2 -1

4
1 4 3 5

D.
. . 8, 4.

E.
:
:
:
:

stdin
stdout
0.5
256

,
-. , .

.
, . , -

, , 15-25 2013

285

,
.

.
m ,
,
, .


n (1 n) . n
5
9
wi ci (1 wi 10 ; 1 ci 10 ), wi
i, ci .


1 (50 ) n 1250
2 (50 ) n 100 000

3
10 11
20 100
30 10
3
11 11
20 100
30 10

stdin

stdout

E.
. . 8, 1.

286


, , 15-25 2013

F.
:
:
:
:

stdin
stdout
1

256

n n 1 .
-
, .
.


n (2 n 300 000). n 1
1 n .


n 1 . i- i-


1 (50 ) n 3 000.
2 (50 ) n 300 000.

stdin
5
1
2
2
5

2
3
4
1

stdout
13
8
8
9

F.
. dfs-
v s1[v] v,
s2[v] v . dfs-
.
a b. s1[a],
t1[b] = n s1[a],
s2[a], t2[b] ,

, , 15-25 2013

287

: t2[a] = t2[b] + t1[b] + s2[b] (s2[a] + s1[a]). : t2[root] = 0.


(a; b) : s1[a] t2[b] + s2[a] t1[b] + s1[a] t1[b].

G.
:
:
:
:

stdin
stdout
0.5
256

, n .
- ,
.
.
. ,
, -
, p .


n
p (1 p n 1000). n 1 .
ai, bi (1 ai; bi n)
.


q
.
.
. .
, .


1 (50 ) 1 p n 200.
2 (50 ) 1 p n 1000.

288


, , 15-25 2013

stdin

stdout

11 6
1 2
1 3
1 4
2 6
2 7
1 5
2 8
4 9
4 10
4 11

2
3 6

G.
. v f[v,k]
v ,
- v k.
, .

v. , , :

for (l = 1; l <= left_size; l++) for (r = 1; r <=


right_size; r++)
f[v, l+r] = min(f[v, l+r], f[left, l] + f[right, r])
P

(left_size[v] right_size[v]). v=1::n

, O(n ).
http://codeforces.ru/blog/entry/6703#comment-122804

H.
:
:
:
:

stdin
stdout
0.2
256

n ,
,


, , 15-25 2013

289

,
- . ,
.
. n .

,
32
2 .


n.


n
32
2 .


1 (20 )
2 (20 )
3 (20 )
4 (20 )
5 (20 )

n
n
n
n
n

10.
18.
28.
300.
2000.

stdin
3
5
7

stdout
1
3
11

H.
. . 5.

290


, , 15-25 2013

I. K
:
:
:
:

stdin
stdout
1

256

a n q k
[l : : : r] .
: n = 7, a = [6; 1; 5; 2; 4; 3; 1], l = 2, r = 4, k = 2. [l : : : r]
= [1; 5; 2]. = [1; 2; 5]. 2 =
[1; 2].


n (1 n

100 000).
9

a (n 1 10 ).
q (1 q 100 000).
q li ri ki
1 li r i
n, 1 ki min(ri
li + 1; 10)


q (ki ) . .
.


1 (50 ) n; q 100 000
2 (50 ) n; q 30 000

li li+1; ri ri+1.
li ri .

stdin
7
6
4
1
2
3
5

1 5 24
7
4
5
7

3 1

7
2
1
2


, , 15-25 2013

1 1 2 3
1 2
2
1 3

stdout
45 6

291

I. K
. . 2.
L++, L--, R++, R-- multiset<int> p
O((n + q) n log n + qk)

J.
:
:
:
:

stdin
stdout
3
256

p n .
, (i; j): i < j, pi > pj.


n.
.
q.
q li, ri (1 li

ri

n).


q .

1 (50 ) 1 n; q 105
2 (50 ) 1 n; q 20 000

li li+1; ri ri+1.
li ri

stdin
5
5
3
1
1
3

292

4 3 12

stdout
3
9
2

3
5
5

, , 15-25 2013

J.
. . 2.
, [L::R]
. L++, L--, R++, R--,
O(log n).

K.
:
:
:
:

stdin
stdout
1

512

:
1 i n t l = m, r = n, result = n + 1;
2 while (l <= r) {
3
i n t c = f(l, r);
4
i f (enough(c)){
5
result =
c;
6
r = c - 1;
7
}else{
8
l = c + 1;
9
}
10 }

enough() true false. ,


. .. c < c0 false, c c 0
true. enough(x) x.
f,
.


m n (1

6 500) .



- f.


1 (50 ) n 2 000.
2 (50 ) n 6 500.


, , 15-25 2013

293

stdin

stdout

1 1
1 5

1
9


enough(4). , enough(5), enough(2).
( 4-2-?), enough(3),
enough(1).
: 4+5 = 9, 4+2+3 = 9, 4+2+1 = 7, = 9.

K.
3

O(n ).
f l;r =
min (max(fl;m 1 ; f m+1;r ) + m)
m=l+1:::r

, fl+1;r fl;r fl;r+1. fl;m 1 m, fm+1;r


m. p: fp+1;r fl;p 1. , ,

p, , (f l;m 1 + m)
[p : : : r). :
f l;r = min(f l;p 1 + p; min (f m+1;r + m))
m=l+1:::p 1

l. r . p l;r
:
while (p - 1 > l && f[l][p - 2] > f[p][r]) p--;
O(1), ,
, l + 1 p 1, .
2
O(n ).

L.
:
:
:
:

stdin
stdout
1.5

512

.
.

294


, , 15-25 2013

, . , n
. n (
).
, ,
k . -
, .
, k , ,
. ,
. , , . .


n k (3 k n 1000).
n ( )
- . .

. 0 2 .
. (0; 0).


k 1 n ,
- .
.
- , .
,
0 - .


1 (50 ) n 300
2 (50 ) n 1000


, , 15-25 2013

295

stdin

stdout

4 4
1.57
0
3.14
4.71
4 3
1.57
0
3.14
4.71

2 1 3 4

2 1 4

L.
. . 6, command-post.

M.
:
:
:
:

stdin
stdout
2
16

, .
- .


.
.
1 5000.



. , .
, .


1 (50 ) 1000.
2 (50 ) 5 000.

296


, , 15-25 2013

stdin
abacabadabacaba bccb dbdccdbd

stdout

M.
. . 3.

N.
:
:
:
:

stdin
stdout
0.5
256

n.
- .


1 (25 ) 1 n
2 (25 ) 1 n
3 (25 ) 1 n
4 (25 ) 1 n

104
105
10
3 10

1 ai
1 ai
1 ai
1 ai

10
10
100
3 10


n. n .

10 + 7.

stdin
3
1
35
1
1
1
1

stdout
5

1 2
2
2
2
2

3
3
3
3

45
45
45
45

6 7 8
6 7 8
6 7 8

9 10
9 10
9 10


, , 15-25 2013

941167856

297

N.
f[i] ,
i- . prev[i] ,
: a[prev[i]] = a[i]. ,
O(n) .
1
2
3
4

f[0] = 1
f o r (i = 1; i <= n; i++)
f[i] = f[prev[i]..i-1]
result = f[1..n]

O.
:
:
:
:

stdin
stdout
0.5
256

. . :
=b ,
1 i1 < i2 < : : : < ik a:n 1 j1 < j2 : : : < jk b:n, 8t : ait
jt
ait < ait+1 k .


n m 1 3 000 . n ,
. m ,
.
9

, 10 .


1 (50 ) n 400.
2 (50 ) n 3 000.

stdin
6 5
1 2 1 21
2 1 3 21
298

stdout
2


, , 15-25 2013

O.
fi;j , i a, j b.
:

1. (i; j) ! (i; j + 1) , j b.
2. if b[j] > a[i] : (i; j) ! (next(i; b j); j + 1) , - (next(i; b j); j)
. next(i; b j); j
i , bj, -
a. next, .
1 ai; bi 2n 1
2n a.

P. k-
:
:
:
:

stdin
stdout
1

16

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


n
- s . n
wi; ci; ki, ,
9
ki , wi ci. 1 wi; ci 10 .


,
.


, , 15-25 2013

299

1 (30 ) n 100
2 (35 ) n 100
3 (35 ) n 300

4
s 104

s 105
s 10

ki = 1
1 ki 109
1 ki 10

stdin

stdout

2 100
2 1 100
7 100 3

339

3 . 21, 300.
39 , 78,
39. .

P. k-
, ki = 1, O(ns) O(s)
. f 0, f1.
(wi; ci; ki) O(s).
wi wi: 0 : : : wi 1.
r f1[r]; f1[r+wi]; f1[r+2wi]; : : :

f1[r + twi] = min (f0[r + (t


j=0::k

k)wi] + kci)

,
O(1).

Q.
:
:
:
:

stdin
stdout
0.5
16

N K. ,
N
K .

300


, , 15-25 2013

1 (33 ) 1 N; K 500.
2 (33 ) 1 N; K 10 000.
3 (34 ) 1 N; K 50 000.


N K.

10 + 7.

10 3

stdin

stdout

10
- : 1 + 2 + 7, 1 + 3 + 6, 1 + 4 + 5, 2 + 3 + 5.

Q.
. . 7.

R.
:
:
:
:

stdin
stdout
0.3 (0.4 Java)
256

- n n.
- .
.
.
-, .
, .


n (2 n 1000).
. .
6
0 10 .

, , 15-25 2013

301


,
YES . NO.


1 (50 ) n 320
2 (50 ) n 1000

stdin
2
1
2
2
1
2

3
2
3
4

stdout
YES
0 1
2 1
NO

R.
.
:
- . ,
, . :
- , ,
-
1, 1 1,
, .
. ,
x, :
1 Check(x)
2

3
sum_col = 0
4
f o r (i = 1; i <= n; i++)
5
sum_col += i-
6
sum_row = 0
7
f o r (j = 1; j <= n; j++)
8
sum_row += min(row[j], i * x)
9
if
sum_row < sum_col
10
return false
11
return true

, , x,
. . i- . ,
302


, , 15-25 2013

.
. y,
x, y.
y - y 1.
2

O(n log M). (


)
2
O(n ).


, , 15-25 2013

303