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

1

(14.02.08 .) ................................................................................. 1
Vladimir L. Pavlov Todays and Tomorrows ALM Solutions
(International Software & Productivity Engineering Institute) ..................... 1
Abstract..................................................................................................... 1
(15.02.08 .). ................................................................................. 1
. .................................................................... 1
............................................................................................ 1
. ..................................................................... 2
. ..................................................................................12
(16.02.08 .). ................................................................................. 1
. ...................................................................... 1
............................................................................................ 1
. ................................................................................... 1
(17.02.08 .). ........................................................................... 1
. ....................................................................... 1
(18.02.08 .) ................................................................................... 1
. ........................................................................ 1
........................................................................................... 1
. ................................................................................... 1
(19.02.08 .). ................................................................................ 1
. ...................................................................... 1
............................................................................................ 1
.................................................................................... 2

2
, ,14-21 2008 .

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


, ,14-21 2008 .


(15.02.08 .).
.

, 9
1979 . 1986 1994
88


.
1994

.
,

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

1502 ,
.
.
1996
2001 . 1999

.
( )

(2000.),

.

4
, ,14-21 2008 .


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


.
2007

.

,


.
.
.

, .
, ,
, . ,
.

[., .
: ]. ,
,
,
.


, ,14-21 2008 .


, ,
. ,
.
, , .
1. : P L Q
M . .
. ,

. ,
. ,
, L+M (

).
, .

,

( )
.
O(LM), n P
Q.
.
,
.
:
, ,
, . ,
N-

N
.

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

.

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

4
, ,14-21 2008 .


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

. P Q
,
.
, .
[ORourke J., Chien C.-B., Olson T., Naddor D. (1982) A
new linear algorithm for intersecting convex polygons. Comput. Graph Image
Processing 19:384-391] , ,
,
. . ,

PQ,

P*=PQ.
P Q.
, , P,
Q P*.

,
. ,

P Q.
P Q,
.

P Q. ,
,
(
). L+M
,
.
, .
, L+M
P*.
[Toussaint
G. T. (1985) A simple linear algorithm for intersecting convex polygons].
,

.
, , ..


, ,14-21 2008 .

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

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

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

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

4
, ,14-21 2008 .


s :=" "

, ,
4 .

R[i ]

( A, B, C )

A2 + B 2 + C 2

i=1, i=2 ..).


(i=1) .

.

.
R

x2 + y2


.

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

, . ,
X,Y Z
. ,
, H.
p=(x,y,z). , Hn=p. , p
n, H.
, (
), n p
, 1. , x2+y2+z2=1 x2+y2+z2=a2. ..
.


, ,14-21 2008 .


, Hn=p.
n=p, . ,
, , .
p-n
. (,t)=0,

pn
pn

, t .

Ht=t-2(,t). ,
, .
.
kn, k .
, k = d +

D
A + B2 + C 2
2

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

temp:=p[i]-n[i]
tr:=tr+temp2
[i]:=temp

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

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

[1,i ]:=temp*[i]

4
, ,14-21 2008 .


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

.
a=0, 0 .
, H,
d. .

p.
3. N , (xi,yi).
( i-
vi), .
( - ) ,
.
. ,
.
1. ,
. ,
T ,
t (tT) (
, ).
, T ,
t (tT).
, ( )

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

T.
. , i-
T (xi,yi)


, ,14-21 2008 .


viT, .
, i-
. , ,
.
,
. (
, )

() ,
, .
, i , (
() (i+1)-
) ,
i+1 . ,
, ,
, ,
.
, ,
N . ,
.
,
2 .
2N. , ,
,
.
.
( , ),
.
,
, .. 2*N(N-1)/2 ~ N2.
O(N3 * (log T
log )), T ,
.
,
- , ,
.
,
.
2. T (
) ( ) (x,y) (
), .

. , ,

4
, ,14-21 2008 .



:
( x x ) 2 + ( y y ) 2
i
i
T ( x, y ) = max

1i N
v
i

,
. ,
. ,
,
T i.
,
(, )
( ,
O(N log N), ,
).
,
,
. , , .

(.. ). ,
( )
. ,
.
( ) , T(x,y)
.
(
, )
. T
, T(x,y) . ,
, .
,
( - vi).
, ,
N ,
O(N) T(x,y),
,

.
3. :
:
f(x,y,T)=T min
:
gi(x,y,T)=(x-xi)2+(y-yi)2-vi2T20.


, ,14-21 2008 .

10


, ,
,
. ,
, gi .

, ,
NP-.
, ,
:
gi(x,y,T)=

( x xi ) 2 + ( y y i ) 2 vi T

0.

,
.
.
n+3 n
3 x,y,T. ,
, .
,
, ,

. ,
O(N4), .
. .
.
.
, .
, ,
.
,
, .

4
, ,14-21 2008 .

.
A. ?
: divisible.in
: divisible.out
: 1
, ,
.





,
,
,

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


, ,14-21 2008 .

12


"Divisibles, a b
n, "Indivisibles", ,
"Equal", .
divisible.in
142
123
10 10 5

divisible.out
Equal
Indivisibles
Divisibles

, .
, , a b ,
ab, , ,
. a b.
, ,
n, n. n
.
ab
a b ( ba+1) ,
y. , d(b,n)d(a1,n),
d(x,y) y
m x.
, m=0 ( m=1), ,
d(x,y) ( ) x
y , x+1 m1,
.
n=0 , ,
, 0
0.
,
.
n. 1, n=1,
Divisibles. n=2 a b:
, , , ,
,
. n=3 , Indivisibles
, a b 3. ,
a b 1 3, Equal, Divisibles
a=b. n>3 n=0,
n=3, Equal ,
1.

O(1).

4
, ,14-21 2008 .



, ,
, . ,
[, ,
] x div y = floor(x/y) ( floor(x) ,
x), x mod y = x (x div y) y ( x div 0 ,
, x mod 0 x).
0 y ,
. x div y =
trunc(x/y) ( trunc(x) ,
, floor(x) x ceil(x)
), ,
, div.
. ,
,
,
.
B.
: parts.in
: parts.out
: 1
, .
,
,
10 ,
20
.
69
,

N ,

-
683-1.


, ,14-21 2008 .

14


. , K ,
.
. ,

. ,
N .
N K
(0N10100000, 2K1010000).

.
parts.in
58
12 6

parts.out
5
14

.
,
. K ,
.
. (
,
), . , , N=36
K=6 (
)
36 , 6 ,
, 6 ,
. ,
43 .
,
, ,
.
K , , ,
O(logKN) .
,
( ).
, , O(logBNlogBK), B
, .

O(logKNlogBNlogBK)=O((logBN)2),
.
.
, N N .

4
, ,14-21 2008 .


.
. (K1) ,
, .
,
. , N1 K1
, N +
(N1) div (K1).
N=0 ( div
, K, , div
, K=2 ),
.
.
O(logBNlogBK)
( ).

B, ,
. , B
10, .
B=10000, ,
longint (
).
,
(L+1)- ( (uL,uL1,,u0)B) L-
((vL1,,v0)B).

~ = min B 1, u L B + u L 1 . [,
q

vL

, .2], q
q~ 2 q q~ , vL1
B/2 ( ,
B div (vL1+1) ).
C.
: billiard.in
: billiard.out
: 1


, ,


, ,14-21 2008 .

16


,


,
.

.


, ,
.

.
, ,
, , -
.
.
M
N (2M,N1000000).
x y (0<x<M, 0<y<N). vx vy
,
1000000
(
vx,vy1000000,
vx2+vy20, ..
). .
: 0
(0,0), 1
(M,0), 2 (0,N),
3 (M,N). ,
( ),
( ,
,
), (,
, ).
, ,
1, , .
billiard.in
37
13
36
43
12
21

billiard.out
3

-1

4
, ,14-21 2008 .

,

, .
,
( c
(kxM,kyN,(kx+1)M,(ky+1)N)).
(kxM,kyN), kx ky .
0 kx ky, 1 kx
ky, 2 kx ky, , , 3 kx
ky. ,
(x,y) (vx,vy). ,
,
(kxM,kyN), kx ky , .
(x+vxt,y+vyt), t0. ,
:
x+vxt=kxM,
y+vyt=kyN,
kx ky , t (
) .
,
t. (
, ).
vy ,
vx, t:
kxMvy kyNvx = xvy yvx (*)
t.
:
( ) (*),
(*)
( t ,
t ).

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

x = x* + r (b/d),
y = y* r (a/d),
r .


, ,14-21 2008 .

18


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

a b (
).
m1=a; m2=b;
while (m2)
{
q = m1 / m2;
m1 -= q*m2;
m1 m2;
}
m1 (a,b), m2
.
x1, y1 x2,
y2 , m1 m2
a x1 + b y1 = m1 a x2 + b y2 = m2:
x1=1; y1=0; m1=a;
x2=0; y2=1; m2=b;
while (m2)
{
q = m1 / m2;
(x,y,m)1 -= q*(x,y,m)2;
(x,y,m)1 (x,y,m)2;
}

4
, ,14-21 2008 .


, m1
(a,b), (x1,y1) a x + b y =
(a,b), ,
, a b 0,
|x1|<b/(a,b), |y1|<a/(a,b) ( ,
, ).
, m2 ,
x2, y2
b/(a,b) ( a/(a,b)), r
.
( ,
) a b N O(log
N). , a b
.
D.
: triangle.in
: triangle.out
: 1
,
.
-,

.


:
.


,
. ,


, ,14-21 2008 .

20


. ,
.
.
,
. , ,
, .
x1, y1,
x2, y2, x3, y3, x4, y4, x5, y5, x6, y6.
xi, yi ,
108. , .
( ,
, 0-).
triangle.in
-1 -1 1 0 -1 1
0 -2 3 0 0 2
002022
002202
2 -1 3 0 2 1
-1 -1 1 0 -1 1

triangle.out
3
2
0

, .

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

, z
,
( z ).
X A1A2A3
.
,
, AiAi+1
AiX i.

4
, ,14-21 2008 .


AB CD ,
AB CD, CD AB.
AB CD ,
CD CA CD CB .
,

:
OA + t1 AB = OC + t2 CD
AB ( 0t11
), CD.
, OA :
t1 AB t2 CD = AC.
CD,
t1 ABCD = ACCD,
AB
t2 ABCD = ACAB.
t1 t2,
0 1 ( ,
). ,
(108) 1016,
t 1016.
.
E.
: hanoi.in
: hanoi.out
: 1
.
-
,
,
,
1883


.
,

.


, ,14-21 2008 .

22



( ).
,
. .
, , , ,
.
,
, .
, ( )
N
2N1 .

. ,
. ,
,
.
,
, .

N M
(0<N63, 0M2N1). 1
N.

M (
, M ).
,
.
. ,
.
hanoi.in
32

4 14

hanoi.out
111
3
1
2
031
432
1

4
, ,14-21 2008 .

.
,

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

.
O(N).
F.
: highhanoi.in
: highhanoi.out
: 2
. ,


,


, ,14-21 2008 .

24

,

(
,
)

,
,
-
,

(

).

-
,

.


N (0<N200000). 3 s1, s2, s3
(0s1,s2,s3N, s1+s2+s3=N) .

( ). ,

1 N. .
.
,
,
1000000.

4
, ,14-21 2008 .


highhanoi.in
3
111
3
1
2
4
031

highhanoi.out
1
2

2
1

432
1

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

: .

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

O(2N), O(N2)
O(N). ,
(
), .
N1.
, ,
N2 1 .
, N2 1
( , N1, ,
) 2N2
( (N1)- 2N21
N2 1). , 2N2
,
N2 1
.


, ,14-21 2008 .

26


.
, i-
,
i. ,
2, ( N2
). , ,
.
G.
: purchase.in
: purchase.out
: 1

. ,
.


,


,

, .
, - ,
. ( N ),
,

. ,
,
, .

4
, ,14-21 2008 .


1, K, K2, , KM,

.
K, M, N
(2K100, 0M100, 0N263-1, NKM).

, ,
,
. ,
, .
purchase.in
10 3 123

purchase.out
60
100 10 10 1 1 1

2 6 61

12
64
12

,
, ,
.
1.
, .
,
,
.
2.
K1.
K (
Ki), Ki+1 (
, ,
(i=M), ,
).
3. M, ,
,
NKM ,
, K.
,
. ,
Ki (


, ,14-21 2008 .

28


i>M). ( ) .
KM+1. ,
,
. ,
KM+1. ( KM+1N)
KM. ,
KM1 K1 .
, , ,
KM1. , KM ,

ceil(((KM+1N)(KM1))/KM)ceil((KM+1KMKM+1)/KM)=ceil(K2+KM)=K1.
.
Ki (i>M) KM (K1)
KM KM,
.
KM.
, , M
.
4. ,
K/2 , ,
,
K/2.
,
Ki >K/2 ( (K+1)/2) ,
.
Ki+1 (K-c) Ki.
,
(1+(Kc))=2c(1+K)2(K+1)/2(1+K)=0. ,
.
, Ki Ki+1
K/2. (, ),
Ki+2, K/21
Ki+1 K/2 Ki,
. Ki+1
, Ki , K/21
Ki+1 K/2 Ki,
1.
5. , , 4,
N .
. ,
1, K, 1 (
)
N K. N K K/2,

4
, ,14-21 2008 .


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

O(logKN).
H. !
: birthday.in
: birthday.out
: 1
, ,

.

,
- ,

K .

(

,
,
).


, ,14-21 2008 .

30


,
. , -
, .

,
, ,
.
( , ) . ,
,

,
.
( ), . -
, ,
.
,
( ,
).
( )
.
.
,
, N .
birthday.in
42
0000

birthday.out
5

37
567


,
.
3 ,
.
,
2 (
),

.
4 , ,
.
5,
.
,
7
.

4
, ,14-21 2008 .


K
N (2K1000, 0N100000).
K
( ,
).
30000.

.

,
K1 .
, K , K ,
,

. ,
, i- K2+i .

.
. ,
, , ,
, , .
a1, a2, a3 .. .
, . -
, .

,
,
.
( ),
. .
K- K2 ,
.
,
, (K2)- K- (
). K3, ,
,
, , .
(K1)- . , N
( ) .
- ,
, (K1)-.


, ,14-21 2008 .

32


(K3)- (
(K1)- K-). , (K4)-
..
, ,
, (
K=2), - (
)
.

O(NK).
I.
: astronaut.in
: astronaut.out
: 2
,
.

58,65
2/3
. ,
,
,

,
,
.
,
,
,

.
,

.
.

,
MN ,

4
, ,14-21 2008 .


.
(
).
T1 T2
.
(i1,j1), (i2,j2).

.
. ,
, ,
T2 T1.
(i2,j2),
.
,
(i2,j2).

M, N (1M,N1000), T1 T2 (0T1<T21000000),
.
i1, j1, i2, j2 (1i1,i2M, 1j1,j2N)
(,
). M N ,
(,
, ,
). 0 1000000.
astronaut.in
43
20 30
2243
19 17 23
10 28 25
11 25 29
12 13 10
23
10 20
1113
15 3 15
17 14 0

astronaut.out
15
ENHWWHHHHHSSSEE

-1

,
,
1,
.
. ,


, ,14-21 2008 .

34


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


, ,
.
.
, (,
)
,
.
l
Tb.
Te k,
:
kl+1
T1Tb+kT2
T1+1Te+kT2
+1 ,
Te+k.
O(V2),
V . () V=MN
, .

( )
O(E log V), E .
E ~ 4V,
.
J.
: tunnel.in
: tunnel.out
: 1
. ,
.

4
, ,14-21 2008 .


,
.


,

,
.
,

,
.

,


,


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


10-6.
tunnel.in
1 1 -1 1 -1 1
-1 1 1 -1 -1 -1
123456
000468

tunnel.out
2.000000
0.000000


, ,14-21 2008 .

36

, ,
( , ,
,
).
.
AB CD. X1 AB X2
CD. AX1 AB
AX1 = t1 AB, 0t11 ( AX1
AB). CX2 = t2 CD, 0t21.
X1X2
X1X2 = X1A + AC + CX2 = t1 AB + AC + t2 CD.
f(t1,t2) X1X2 (
):
f(t1,t2) = t12 AB2 + t22 CD2 + AC2 2t1t2 ABCD 2t1 ABAC + 2t2 ACCD

0 t1,t21.
( (),
, ,
0 t1,t21),
(t1=0, t1=1, t2=0 t2=1).
f(t1,t2) ,
t1 t2.
,
( ).
,
0 t1,t21. ,
.
( t1 t2)
,
.
O(1).
K.
: ciphering.in
: ciphering.out
: 1

4
, ,14-21 2008 .


, ,

Windows.
,

, , .


,
,


.
NN ( )
(
, ..).
.
, , :
1) L i, i 1 N-1
i .
, L 2
.


, ,14-21 2008 .

38

W i

W i

c h o o

r W i

c h o

r o

a m

r o

g r

a m r o

m i

n g

m i

n g

2 m i

0 0

0 0

0 0

R i, U i, D i,
i ,
.
2) V i, i 1 N/2. i=N/2,
,
N/2 N/2+1. i<N/2,
, 2i 2i+1,
,
i i+1,
N/2+i N/2+i +1 ( N)
(N+1)/2+i ( N).
, V 2
.

4
, ,14-21 2008 .

W i

e r

i W r

S c h

o o

h c S

o o

o g r

a m

g o

r m a

m i

n g

0 0 8

g n

i m 2

8 0 0

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


, ,14-21 2008 .

40

W i

o P m 2

S c h o o

o g

a m

n g

m i

0 0 8

n c

o g n 8

r m 0

4) T
().
, ,
,
.
,
. , ,
,
. ,
, .
N (1N109)
M
(0 M105). M
.
,
,
. ,
1.

4
, ,14-21 2008 .


ciphering.in
61
L2
33
P 90
T
P 90

ciphering.out
2
1

, 0 (
,
).
NN
(, 1 N2).
, ,
,
. O(LMN2),
L . ,
. ,
, . ,
, .
.
M , p 1 N2,

p.
O((L+M)N2) , - .
,
L, , pL+1=e, e .
(
1)

. N2
O(N2). ( )
O(log N) .
O(MN2) .
O((M+log N)N2).
,
. ,
. , , L k ,
(i,j), (i,jp) (
N).
( Zn2),


, ,14-21 2008 .

42



33 Zn

px
p
y

0
k xx
k yx

k xy .
k yy


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

Rp

Up

Dp

0
p

0
p

0 0

1 0
0 1

0 0

1 0
0 1
1 0 0

p 1 0
0 0 1

1 0 0

p 1 0
0 0 1

Vp

1 0 0

0 1 0
2 p 0 1

Hp

0 0
1

2 p 1 0
0
0 1

4
, ,14-21 2008 .

P0

P 90
P -270

P 180
P -180

1 0 0

0 1 0
0 0 1

1 0 0

1 0 1
0 1 0

1
0
0

1
0

0 0 1

P 270
P -90

1 0 0

0 0 1
1 1 0

1 0 0

0 0 1
0 1 0

. ,

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

k xx
k
yx

k xy

k yy

( ,
) Zn2 0, 90, 180, 270,


, ,14-21 2008 .

44

1 0 , 1 0 ,

0 1 0 1
0 1 0 1

,
.
1 0 1 0

8:

1 0 ,

0 1

0 1 0 1

,
,
1 0 1 0

1 0

,
0 1


L,
.
, O(M+log N).
N=1 N=2
, 11 (mod N).

4
, ,14-21 2008 .


(16.02.08 .).
.

, .
,

.
.
(2004, 2006)

. 2005
.
(2002-2006)

( ). 2006
(

).
(2003, 2007)
. ( 2003 :
, 30 72, 2007 :
, 26 88.)
.
.
(17.02.08 .).
.
.

4
, ,14-21 2008 .


(18.02.08 .)
.

.



.
IOI
1997 1998.

ACM
2000 2001.
TCCC Marathon
Match 2007.

.

.

.
.
:
:
:
:

exam.in
exam.out
1
256


. , ,

4
, ,14-21 2008 .


. , ,

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

.

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


N(2N100). .
N , x i, a i, b i
(-10000 x i 10000, -10000 a i b i 10000) i-
(xi,ai) (xi,bi).

. , Xi .


.
JV ( ),

. ,
.


, ,14-21 2008 .


, .
,
.
.

exam.in

exam.out

2
112
234

0
1.5
3.5

3
112
234
4 1 2

1.2490457723982544
232



. , ,
, .
.
. [v1][v2]
, v1,
v2. ,
v1. O(N3),
.
.
:
:
:
:

firs.in
firs.out
1
256


N 1 .

4
, ,14-21 2008 .


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

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

,
.

firs.in

firs.out

3322

3223


, ,14-21 2008 .


,
. ,
;
.
, ,
.
, O(logn)
.
.

,
(
n/2). ,
, 5, 6, 7, 8,
( n/4).
, ,
;
logn, k-
2 k . , ,
,
,
, ,
, .
- ,
logn ,
.
.
, ,
[1]. ,
, [2] [3], .
, , ,
[2d], [2d+1], ..., d, = logn,.

4
, ,14-21 2008 .


( ) ,

2d.
[i] = min{[2 i],
[2 i + 1]}, j
j 1, j j + 1
. , [1]

,
,
. ,
;
,
, .

.
:
:
:
:

eucal ypt. in
eucalypt. out
1
256

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

,


, ,14-21 2008 .


,
, .
. ,
.

N (2 N
100000) . JV

x i, y i (-10 9 x i 10 9 , -10 9 yi 10 9 ) ,
- . .
- - .


.

eucalypt.in

eucalypt.out
4.0

4
10
01
11
00

. ,
, , .
, X.
, , X ,
.
. ,
, 2( + ). ,
.

4
, ,14-21 2008 .


D.
:
:
:
:

games.in
games.out
2
256

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

N ,
N (1 N 10000). JV
, ,
.
20 .
, ,
26 .
.


, ,14-21 2008 .



, L (1 L
100 000).

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

games.in

games.out

4
Doom awsd
Warcraft ahpmsbf
Civilization rmic
Quake awsdzc
adwdqdsaawwzdswaawws

Doom
Quake

1
Doom awsd
programmacrobeginend

NO SOLUTION


, .
.
,
,
. O(N * L) ,
O(1) (
, boolean 1 26,
, ).
. , ,
,

4
, ,14-21 2008 .


, ,
(
), , O(N * 26 +L)
.
: roads.in
: roads.out
: 1
: 256
N . , ,
.
, - ,
( , )
(
).
, .
, .
, , , ,
,
, . ,
, .
.
,
, ,
.
, - ,
. (
, )
.
, , .

,
(


, ,14-21 2008 .

10


...) .
,
.

3 N , ( 1
N 1 0 0 0 , 0 1 0 0 0 0 0 , 1 109)
, .
.
(1 a , b N) ,
.
. .

.

roads.in

roads.out

3 1 2
12
3 0 1000000

2 1 2
12


G,
,
,
. -
.
,
, . G
, oi, 2,..., .

4
, ,14-21 2008 .


G , \ .
, , \ ,
. ,
oi, 2,...,
.
f({ai, 2,..., }). ,

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


= 1 . , (1)
^ k =k0 + 1.
(k0 + 1)-
. S

{1,

, k0} - B

,
(k0+1)- .

(a k 0+1 ) |S | * ai
iS

. S
,

f ({ai | 1 i k 0 , i S } { ai })
iS

. / :

f ({a1 , a 2 ,..., ak 0 +1}) =

(a

S {1,..., k 0 }
S

) * ai * f ({ai | 1 i k 0 , i S}{ ai })
| s|

k 0 +1

iS


, ,14-21 2008 .

iS

12


, \S \,
:

f ({a1 , a 2 ,..., ak 0 +1}) =

k0

= ((a k0 +1 ) l
l =1

k0

= ((a k0 +1 ) l

((a

S {1,..., k 0 }
S

( a

S {1,..., k 0 }
|S | =l

) * ai * f ({ai | 1 i k 0 , i S}{ ai })) =


| s|

k 0 +1

iS

iS

iS

* f ({ai | 1 i k 0 , i S }{ ai }))) =
iS

k0

k0

( ai * ai * ( ai ) k 0l 1 )) =

l =1

S {1,..., k 0 }
|S | =l

iS

k0

k0

k0

l =1

i =1

i =1

i =1

i =l

= ((a k0 +1 ) l * ai * ( ai ) k 0l 1 *

a )

S {1,..., k 0 } iS
|S | =l

S {1,..., k 0 } iS
|S | =l

ak 0

C kl 0 * l / k 0 = C kl 011 .

C kl 0 * l

1,2,,

k0

ai = C kl 011 ai

S {1,..., k 0 } iS
|S | =l

i =1

4
, ,14-21 2008 .



k0

k0

k0

l =1

i =1

i =1

f ({a1 , a 2 ,..., a k0 +1 }) = (C kl 011 * (a k +1 ) * ai * ( ai ) k0 1 ) =

k 0 +1

k 0 1

ai * (C
i =1

l =0

k0

l
k 0 1

* (a k +1 ) * ( ai )
l

i =1

k 0 l 1

k 0 +1

k 0 +1

i =1

i =1

) = ai * ( ai ) k0 1

- .
(1) k=k0+1, , .
k.
, (1),
G.
, , .
F. -2
:
:
:
:

roads2. in
roads2. out
1
256

, ,
. ,

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


, ,14-21 2008 .

14


, ,
.
,
.

, .
,
. 1.
N (1 N 100 000, 0
100 000).
(1 a , b N)
. .


.
. ,
.

roads2.in

roads2.out

20

2
12

21
21

1
2

32
21
23

2
23

-2
,
,

4
, ,14-21 2008 .

. ,
,
, . ,
.

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

2.
, . - ,
. ,
, .


, ,14-21 2008 .

16


int dfs(int v, int num) { int r = 1, e; mark[num][v] = 1;

for( e = head[num][v]; e != -1; e = next[num][e] )


if( ! mark[num] [end[num][e]] )
r += dfs(end[num][e], num);
return r;
}
...
a = dfs(0, 0)-l;
b = dfs(0, 0-1);

3. :

printf("%d\n", n-a-b);
for(i = 0; i < n-a-b; i ++)
printf("%,d", b+i+1);
:

#include <stdio.h>
#include <memory.h>
#define maxn 100000
int n, m;
int head[2][maxn], next[2][maxn], end[2][maxn],inl[2];
int mark[2][maxn];
void insert(int num, int f, int s){
next[num][inl[num]] = head[num][f];
head[num][f] = inl[num];
end[num][inl[num]] = s;
inl[num]++;
}
int dfs(int v, int num)
int r = 1, e;

4
, ,14-21 2008 .


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


, ,14-21 2008 .

18


.
1 . ,
,

.
, ,
. .
:


.

.
,
.
,
.

,

(1 n 200000), (1 10 6 ) (2 m
9
10 ).
a i (0 a i 10 9 ), a i -
, i.


, , .

mustrack.in

mustrack.out

4
, ,14-21 2008 .


3 1 1000
123

5 2 29
12345


0.
,
:
{0, mod , 2p mod , mod ,..., ( 1) mod n}
,
,
,
, .
: = ,
{0,, 2, ,..., ( 1)},
{0,, 2, ,..., ( 1)}
, .
200,000 100,000, ,
.
:
1. .
sq = ((int) sqrt(n*1.0) ) + 1;
for( i = 2; i <= sq; i++ ) {
if( not_prime[i] ) continue;
for( j = i*i; j <= n; j += i ) not_prime[j] = 1;
}


, ,14-21 2008 .

20


}
2. .
for( number_of_prime = 0 ,

i = 2 ;

i<=n;

i ++)

if( !not_prime[i] )
prime[number_of_prime++] = i;
3. .
for( i = 0; i < n; i++)
s u m + = a[ i] ;
sum %= m;
4. .
.
: plane.in
: plane.out
: 1
: 256
,
.
,
.
, ,
. ,
,
, , , .
, , ,
, ,
,
. ,

4
, ,14-21 2008 .



.
,
, , ,
,
.
- , ,
.


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


.
. ,
.


, ,14-21 2008 .

22

plane.in

plane.out

20 0 0 S 10 0 0
2000S11000 W

2000S1000 E

123456N142445W
234553N152134W

234553N152134W

5000 N9000 E
5000 N3000 W

671422N3000 E


1
. A(xi,yi, zi) , (2, y2, z2) ~ .

, , .
,
. , ,
.
.

. ,
+ by + cz = 0, = 1 z 2 z 1y 2, b = x 2z1
x 1 z 2, = x 1y 2 x2 y 1- S.
Oz,
, ,
S.
,
. , .
,
, , ,
.

L(x,y,z) = z-/2(x 2 + y 2 + z 2 -l) + (ax + by + cz).

4
, ,14-21 2008 .


(, , z) L
, :

- + = 0
- + b = 0
1 - z + = 0
2 + 2 + z2 =

+ by + cz = 0
, z
,
c

=
-

a2 + b 2 + c2

( , ,
2 + 2 + 2 > 0.)
,
a2 + b 2
2

=(a2 + b 2 + c2)2
, z. ,
z = (c + 1)/
.
, . ,
, ,
.


, ,14-21 2008 .

24


I.
: pool.in
: pool.out
: 1
: 256

.
,
.

, R

,
(dx, dy), L,
,
.
,
: ,
.

,
, , R, L, (5 1 000, 5 1000, 1 100, 1 R 20,
1 L 1000, 1 ).
dx dy, 100,
, . ,
,
.
.
i y i (0 i , 0 y i ).
,
.

4
, ,14-21 2008 .


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

pool.in

pool.out

151521101
11
22
1111

STOP

151521111
11
22
1111

TOUCH

151521101
1-1
22
1111

BOARD

151521101
10
22
64

TOUCH


.
(dx, dy). ,


, ,14-21 2008 .

26


. , ,
, ,
.
. , ,
, L , STOP.
BOARD TOUCH
, .
, , . ,
, = r , = - r, = r =
r, a .
,
.
, .
a*x+b* = , = dy, b = - dx, = a*xo+b*, = (XQ, )
. d
= (x 1 , y 1 )

, v =

.
, 2 , .
,
. t,
, (dx, dy)
(1

X0,

y1 - y0), v.

, , D,
. , ABCD
,

|CD|

. ,
t

. ,

,
,
L.
J.

4
, ,14-21 2008 .


: square.in
: square.out
: 2
: 256
( + 1) ( + 1),
(0, 0) (, ) (
= 2):

(0,0)

(1,0)

(2,0)

(0,1)

(1,1)

(2,1)

(0,2)

(1,2)

(2,2)

10
10. - ,
(
) ,
, ..
= (Cx-1,y + Cx+1,y + Cx,y+1 + Cx,y-1)/4.
,
1

5 6

4 5

3 2

2 1

. ,

(,
) .

(2 50).

, (0, 0).


, ,14-21 2008 .

28


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


, +1 .
.
, .
.
.

square.in

square.out

3
1256
5 2
1256 52

1.00000 2.00000 5.00000 6.00000


2.00000 3.00000 4.00000 5.00000
5.00000 4.00000 3.00000 2.00000
6.00000 5.00000 2.00000 1.00000


,

. ,
, .
, ,

,
. ,

.

; ,

, ,

4
, ,14-21 2008 .


- ; ,
, ,
. ,

, .
, ,
,

,

,
. ,
,
.

, .
,
; ,
,
.
.
:
:
:
:

tank. in
t an k. o u t
1
256


.
,
.


, ,14-21 2008 .

30


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

-.
,

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

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

4
, ,14-21 2008 .


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

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

.
[, ] ,


, . ,
( ).

, ,
. .

, ,14-21 2008 .

32

tank.in

tank.out

43
.T.N
NVNP
.NVN
61
T.P..V

52
WSSEE

43
...
NNNN
.V.

94
EESWWWSEE

34
T.V
.VN
PPP

20
SE

-1

4
, ,14-21 2008 .


(19.02.08 .).
.

.

.

.
NEERC,

,

- .

(www.lksh.ru)

(www.amse.ru).
NEERC, , , 30
.
:
- 2 NEERC 2000;
- 4 ( ) 2000;
- 3 ( ) 2001;
- 6 TopCoder Open 2006;

4
, ,14-21 2008 .


- 4 TopCoder Collegiate Challenge 2006;
- 3 Google Code Jam 2006;
:
- NEERC 2001, 2003, 2004, 2007 ;
- 2 NEERC 2005, 2006;
- 2004 ;
- 2003, 2005, 2007 .

A. Brackets
:
:
:
:

brackets.in
brackets.out
2 seconds
256 megabytes

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


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


, ,14-21 2008 .


The first line of the input file contains t the number of tests (1 t
10000). The description of t test cases follows.
The first line of each description contains two integer numbers: n and (1
n 100 000, 1 10 000). The second line contains 2k integer numbers
the permutation of the set {-k,-(k - 1),..., -2, -1,1,2,..., k}. It lists the brackets
from the least to the greatest in the given order. Positive number i means the
opening bracket of the i-th type, negative number -i means the closing bracket
of the i-th type. The third line contains 2n integer numbers from - to (except
0) and describes the regular brackets sequence.
The sum of n and the sum of in all test cases don't exceed 100 000.

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

brackets.in

brackets.out

2
32
1 -1 2 -2
1 2 -2 -1 1 -1
32
1 -1 2 -2
1 2 -2 -1 2 -2

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

.
,
, ,
.
.
.
.

.
,
, .
,

4
, ,14-21 2008 .



(
).

, ,

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

, . ,
, :
,
,
.
, ,
(

, ,
).

. Car Wash
:
:
:
:

carwash. in
carwash. out
2 seconds
256 megabytes

Ben is the owner of a car wash. Ben offers car washing and dry-cleaning
of the car compartment. His wash is located in the capital of England, and
he often serves clients from government. Recently Ben has received an
order of washing and dry-cleaning of n cars. And this order must be
executed as fast as possible!
After preliminary investigation, Ben found out that the i-th car can
be washed in a i, minutes and cleaned in b i, minutes. Washing and
cleaning are performed at different buildings, so they cannot be
performed simultaneously for the same car. The order of the two
operations for a car is irrelevant, neither is the order of processing the


, ,14-21 2008 .


cars. Ben's best workers Tom and Jerry were called to perform the order.
Tom will wash the cars, and Jerry will dry-clean their compartments. Each
worker can work with one car at a moment, and due to quality
requirements, it is not allowed to switch from one car to another until
the one is ready (this condition is independent for each worker, different
workers can process cars in any order).
Help Ben to find out how the cars should be processed by the
workers, so that all cars were finished as soon as possible.

The first line of the input file contains n (1 < n < 10 000). The following
n lines contain two integer numbers each: a i, and b i (1 a i,b i 10 5 ).

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

carwash.in
6
10 6
79
38
12
12 7
66

carwash.out
39
11 26
4 17
07
3 15
27 0
21 32

, ,

i =1

i =1

ai , bi

max in=1 (ai + bi ) .

, .

4
, ,14-21 2008 .

ai b j

ai + bi

i, ,

j i

i- , , ,

bi a j

j i

, i- .
.
n

a
i =1

b
i =1

). , ,
n

a
i =1

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

( a1 + b1 i).
, b2>a1, b2+b3>a1+a2.
,
, , ,
.
, (
) b3>a2, b3+b1>a2+a3. ,
, , ,
b1>a3, b1+b2>a3+a1.
(
, ai bi).
.
.
.
:

ai + bi

a j + bj ,

ai + a j

bi + b j

, ,


, ,14-21 2008 .

ai
i =1

b , max
i =1

n
i =1

(ai + bi )

a
i =1

. ,

, .

C. Painting Cottages
:
:
:
:

cottages.in
cottages.out
2 seconds
256 megabytes

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

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

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

cottages.in

cottages.out

4
, ,14-21 2008 .


4
00
10
11
01

12

T
he
poss

ible nice paintings are shown on the following picture.

, .
,
, .
, .
, ,
,
. ,
,
. ,
, ,
. .
,
(
). ,
,
, ,
. x*2*2/2=2x.
D. Dinner Problem
:
:
:
:

dinner.in
dinner.out
2 seconds
256 megabytes


, ,14-21 2008 .


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

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

Output one number the number of ways.

dinner.in

dinner.out

23

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

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

. Financial Software
:
:
:
:

financial.in
financial.out
2 seconds
256 megabytes

4
, ,14-21 2008 .


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

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

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

financial.in

financial.out

6 15
1014 1024 1034 1045 1030 998

4
1014 1045 1030 998

[1], [2],... ,
[].
, , (2).
, l[i]
, - .
l[i] = max{l[j]|j < i,a[j] a[i] - a[j] a[i] + } + 1.
0.
,
l[i].
.
, .


, ,14-21 2008 .

10



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

A[i]-k.

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

F. Minima
:
:
:
:

minima. in
minima.out
3 seconds
256 megabytes

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

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

minima.in

minima.out

4
, ,14-21 2008 .


10 3 2
110
01

33

1000000 15 5
-1879262596173354
283471207 23947205 3
17625384
939393931
1838388
912740247 290470294
The rest of the array is calculated using the following formula: x[i] = f(a *
x[i - 2] + b * x[i - 1] + c). Here f(y) returns such number -231 z 231 - 1 that
- z is divisible by 232.

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

:
, :
, .
,

(1).
.
. (1)
: , i-
.

,
,
.
,
(1).
.
. .
: 1
, 2 1-ro, 3
2-, . .
.
,
,
. ,
, ,


, ,14-21 2008 .

12


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

G. Move to Front
:
:
:
:

mtf. in
mtf.out
2 seconds
256 megabytes

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


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

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

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

mtf.in

mtf.out

13 3 3 3 2 2 2 2 2 3 1 3 3 2

3113111123213

4
, ,14-21 2008 .

.
: ,
.
.
x, .
+ ,
, .
, , .
,
. .
. ,
,
( ), .
,
(, ).
( ),
.
. .
,
. (0 1) .

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

. TV Show
:
:
:
:

show. in
show.out
2 seconds
256 megabytes

Recently various TV shows started to gain popularity among young people


who like to test their luck. One such show is running on ZhTV channel in
China.


, ,14-21 2008 .

14


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

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

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

show.in

show.out

4
, ,14-21 2008 .


4
1234
7
1525
1541
2331
3241
2222
3333
5454

YES
2567

4
1234
7
1525
1341
2331
3241
2222
3333
5454

NO

,

2-.
,
. ,
-
.

, ,
, ,
.
, , .


, ,14-21 2008 .

16


.
xi, xi = 0
, xi = 1 . ,
,
.
xixj ( , , xi

x j , . .)

,
2-,
.
, , 2-SAT Google
Wikipedia.

I. Hard Test
:
:
:
:

test.in
test.out
2 seconds
256 megabytes

Andrew is having a hard time preparing his 239-th contest for Petrozavodsk.
This time the solution to the problem is based on Dijkstra algorithm and
Andrew wants to prepare the hard test for the algorithm.
The Dijkstra algorithm is used to find the shortest path from a source vertex
to all other vertices in a graph. The algorithm acts as follows. Let G be a weight
directed graph with vertex set V, edge set E and weight function w: ER +. Let
all vertices be reachable from vertex s. The algorithm uses a set of vertices U,
first initialized as empty. Each vertex is labeled with either an integer number,
or with +. Initially all vertices are labeled with +, and the vertex s is labeled
with 0. Denote the label of vertex v as d[v].
A step of the algorithm is the following: the vertex with the minimal label
that doesn't belong to U is selected. Let this vertex be u. The vertex is added
to the set U, and each edge uv 6 E is relaxed. The relaxation replaces d[v]
with min(d[v], d[u] +w(uv)). The algorithm is over when all vertices belong to
U. If the label of the vertex v has changed, the relaxation is said to be active.
Now Andrew would like to create a graph with n vertices and m edges, such
that the Dijkstra algorithm makes as many active relaxations as possible. Help
him to create such graph. To avoid nondeterminism, each time when selecting a
vertex with minimal label among vertices that are not in U there must be exactly
one vertex with the minimal label.

4
, ,14-21 2008 .


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

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

test.in

test.out

43

120
131
142

55

120
131
244
342
152

. ,
: i j, j
> i j i 1. ,
.

n(n 1)/2 m .

J. Travel Agency
:
:

travel.in
travel.out


, ,14-21 2008 .

18


:
:

2 seconds
256 megabytes

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


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

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

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

travel.in

travel.out

79
12
13
14
15
16
17
23
45
67

18
6
6
6
6
6
6

. , ,
, s1, s2, , s k,

4
, ,14-21 2008 .

s s

0i < j k

(s

2
0

+ s + ... + s
2
1

s0 = n si 1 ,

2
k ))/2.

((S0+S1++SK)2

(n).

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

( , , ,
, ,
).


, ,14-21 2008 .

20

Оценить