I have submited a lot of code for this problem but i still got WA. Any idea? I have read all of the topic before.
[pascal]
(* @begin_of_source_code *)
program p105(input,output);
{$IFDEF ONLINE_JUDGE}
type tint = integer;
{$ELSE}
type tint = longint;
var input : text;
{$IFDEF FILE}
var output : text;
{$ENDIF}
{$ENDIF}
type
tdata = array[1..20001,1..102,1..4] of tint;
var
data : tdata;
i,j,k,l,m,cur,last,max,save,save2 : tint;
s : boolean;
procedure qsort(cur,l,r:tint);
var
i,j,x: tint;
begin
i:=l; j:=r; x:=data[cur,(l+r) DIV 2,2];
repeat
while x<data[cur,i,2] do inc(i);
while data[cur,j,2]<x do dec(j);
if i<=j then
begin
data[cur,102] := data[cur,i];
data[cur,i] := data[cur,j];
data[cur,j] := data[cur,102];
inc(i);
dec(j);
end;
until i>j;
if l<j then qsort(cur,l,j);
if i<r then qsort(cur,i,r);
end;
procedure qsort2(l,r:tint);
var
i,j,x: tint;
begin
i:=l; j:=r; x:=data[(l+r) DIV 2,1,1];
repeat
while data[i,1,1]<x do inc(i);
while x<data[j,1,1] do dec(j);
if (i<=j) then
begin
data[20001] := data
;
data := data[j];
data[j] := data[20001];
inc(i);
dec(j);
end
until i>j;
if l<j then qsort2(l,j);
if i<r then qsort2(i,r);
end;
begin
{$IFNDEF ONLINE_JUDGE}
assign(input,'p105.in');
reset(input);
{$IFDEF FILE}
assign(output,'p105.out');
rewrite(output);
{$ENDIF}
{$ENDIF}
cur := 0;
for l := 1 to 20000 do data[l,101,1] := 1;
while not eof(input) do
begin
read(input,i);
read(input,k);
readln(input,j);
if i <> j then
begin
s := false;
if cur <> 0 then for l := 1 to cur do if i = data[l,1,1] then
begin
s := true;
break;
end;
if not s then
begin
inc(cur);
l := cur;
end;
m := data[l,101,1];
data[l,m,1] := i;
data[l,m,2] := k;
data[l,m,3] := 1;
data[l,m,4] := j;
inc(data[l,101,1]);
s := false;
if cur <> 0 then for l := 1 to cur do if j = data[l,1,1] then
begin
s := true;
break;
end;
if not s then
begin
inc(cur);
l := cur;
end;
m := data[l,101,1];
data[l,m,1] := j;
data[l,m,2] := k;
data[l,m,3] := 2;
data[l,m,4] := i;
inc(data[l,101,1]);
end;
end;
for i := 1 to cur do qsort(i,1,data[i,101,1]-1);
qsort2(1,cur);
{for i := 1 to cur do
begin
for j := 1 to data[i,101,1]-1 do
begin
writeln(i,' ',j,' ',data[i,j,1],' ',data[i,j,2],' ',data[i,j,3],' ',data[i,j,4]);
end;
end;}
last := 0;
save := 0;
for i := 1 to cur do
begin
if data[i,1,3] = 1 then
begin
if data[i,1,2] > last then
begin
if save <> 0 then
begin
write(output,save,' ',save2,' ');
save := 0;
save2 := 0;
end;
write(output,data[i,1,1],' ',data[i,1,2],' ');
last := data[i,1,2];
end;
end
else if data[i,1,3] = 2 then
begin
if (data[i,1,2] = last) and (last <> 0) then
begin
max := 0;
for j := i downto 1 do for k := 1 to data[j,101,1] do if data[j,k,3] = 1 then if (data[j,k,4] > data[i,1,1]) and (data[j,k,2] > max) then max := data[j,k,2];
if max <> last then
begin
if max <> 0 then write(output,data[i,1,1],' ',max,' ')
else
begin
save := data[i,1,1];
save2 := max;
end;
last := max;
end;
end;
end;
end;
if save <> 0 then write(output,save,' ',save2);
writeln;
{$IFNDEF ONLINE_JUDGE}
{$IFDEF FILE}
close(output);
{$ENDIF}
close(input);
{$ENDIF}
end.
(* @end_of_source_code *)
[/pascal]
input
1 11 5
2 6 7
3 13 9
12 7 16
14 3 25
19 18 22
23 13 29
24 4 28
output
1 11 3 13 9 0 12 7 16 3 19 18 22 3 23 13 29 0
input
-5 5 1
1 6 2
1 2 7
3 12 10
4 3 5
7 5 20
8 15 12
11 12 13
16 14 19
21 2 22
23 1 30
26 10 28
26 12 27
32 5 36
34 3 38
34 3 40
37 2 42
38 2 44
45 1 46
output
-5 5 1 6 2 2 3 12 8 15 12 12 13 5 16 14 19 5 20 0 21 2 22 0 23 1 26 12 27 10 28 1 30 0 32 5 36 3 40 2 44 0 45 1 46 0
input
3 2 4
3 1 7
5 2 6
output
3 2 4 1 5 2 6 1 7 0
I don't know why WA. I hope to hear your idea as soon as possible. Thanks.
Best Regards.