-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjaponai.pas
148 lines (135 loc) · 2.97 KB
/
japonai.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{ MK 2002 }
{ XII Olimpiados uzdavinys JAPONAI }
program Japonai;
type
Tsk = -1 .. 1;
TG = array [0 .. 101, 0 .. 101] of Tsk;
Teil = array [1 .. 250] of integer;
Tmas = array [0 .. 101] of integer;
Taib = set of 0 .. 101;
var
sav, sve : integer;
g : Tg;
Griz : Tmas;
procedure nuskaitymas (var sav, sve : integer; var G : TG);
var
ck, ckx, cky, pg : Integer;
f : text;
begin
assign (f, 'japonai.dat');
reset (f);
readln (f, sav, sve);
pg := sav + sve + 2;
for ckx := 0 to pg do
for cky := 0 to pg do
g [ckx, cky] := 0;
for ck := 1 to sav do
g [0, ck] := 1;
for ck := sav + 1 to sav + sve do
g [ck, pg - 1] := 1;
for ck := 1 to sav do
begin
while not eoln (f) do
begin
read (f, pg);
g [ck, pg + sav] := 1
end;
readln (f)
end;
close (f)
end;
procedure pakelk (v, viso : integer; var h : Tmas; var sr : TG);
var
ck, mini : integer;
galima : boolean;
begin
galima := true;
mini := -1;
for ck := 0 to viso - 1 do
if (g [v, ck] > sr [v, ck]) then
if (h [ck] >= h [v]) and ((mini = -1) or (h [ck] <= h [mini]))
then mini := ck
else galima := false;
if galima then h [v] := h [mini] + 1
end;
function min (sk1, sk2 : integer) : integer;
begin
if sk1 < sk2 then min := sk1
else min := sk2
end;
procedure paleisk (v, viso : integer; var sr : TG; var bak, h : Tmas;
var aib : Taib; var uod : integer; var eil : Teil; var griz : Tmas);
var
pg, ck : integer;
begin
for ck := 0 to viso - 1 do
if (g [v, ck] > sr [v, ck]) and (h [ck] + 1 = h [v]) then
begin
pg := min (g [v, ck] - sr [v, ck], bak [v]);
bak [v] := bak [v] - pg;
bak [ck] := bak [ck] + pg;
sr [v, ck] := sr [v, ck] + pg;
sr [ck, v] := -sr [v, ck];
griz [v] := ck;
if not (ck in aib) and (ck <> 0) and (ck <> viso - 1) and (bak [ck] <> 0)
then
begin
aib := aib + [ck];
eil [uod] := ck;
uod := uod mod viso + 1
end
end;
end;
procedure srautai (var griz : Tmas);
var
sr : TG;
eil : Teil;
h, bak : Tmas;
ck, pg, viso,
gal, uod : integer;
aib : Taib;
begin
viso := sve + sav + 2;
for ck := 0 to viso - 1 do
begin
h [ck] := 0;
griz [ck] := 0;
bak [ck] := 0
end;
gal := 1;
h [0] := viso;
for ck := 1 to sav do
begin
bak [ck] := 1;
griz [ck] := 0;
eil [ck] := ck;
sr [0, ck] := 1;
sr [ck, 0] := -1
end;
aib := [1 .. sav];
uod := sav + 1;
while aib <> [] do
begin
pg := eil [gal];
gal := gal mod viso + 1;
aib := aib - [pg];
while bak [pg] <> 0 do
begin
pakelk (pg, viso, h, sr);
paleisk (pg, viso, sr, bak, h, aib, uod, eil, griz);
end
end;
end;
procedure grizk (var grizk : Tmas);
var
ck : integer;
begin
for ck := 1 to sav do
writeln (griz [ck])
end;
begin
nuskaitymas (sav, sve, g);
srautai (griz);
grizk (griz);
readln
end.