littlelee Admin
Tổng số bài gửi : 415 Join date : 20/12/2009 Age : 29 Đến từ : Nghĩa địa
| Tiêu đề: lồi lõm Wed 14 Jul 2010, 15:15 | |
| - Code:
-
program loi_lom; const fi='loilom.inp'; fo='loilom.out'; var l,k,a:array[1..100] of integer; loi,lom:array[1..100] of integer; i,j,t,n,m,h,li,li1,li2,lm,lm1,lm2:integer; f:text;
procedure nhap; begin assign(f,fi); reset(f); readln(f,n); for i:=1 to n do read(f,a[i]); close(f); end;
procedure timloi; begin li:=0; for t:=2 to n-1 do begin for i:=1 to n do begin l[i]:=1; k[i]:=0; end; for i:=2 to t do for j:=1 to i-1 do if (a[j]<a[i])and(l[j]+1>l[i]) then begin l[i]:=l[j]+1; k[i]:=j; end; li1:=l[t]; h:=k[t];
l[t]:=1; for i:=n-1 downto t do for j:=n downto i+1 do if (a[i]>a[j])and(l[j]+1>l[i]) then begin l[i]:=l[j]+1; k[i]:=j; end; li2:=l[t];
if (li1>1)and(li2>1)and(li<li1+li2-1) then begin loi[li1]:=a[t]; i:=h; j:=li1-1; while i>0 do begin loi[j]:=a[i]; dec(j); i:=k[i]; end; j:=li1+1; i:=k[t]; while i>0 do begin loi[j]:=a[i]; inc(j); i:=k[i]; end; li:=li1+li2-1; end;
end; end;
procedure timlom; begin lm:=0; for t:=2 to n-1 do begin for i:=1 to n do begin l[i]:=1; k[i]:=0; end; for i:=2 to t do for j:=1 to i-1 do if (a[j]>a[i])and(l[j]+1>l[i]) then begin l[i]:=l[j]+1; k[i]:=j; end; lm1:=l[t]; h:=k[t];
l[t]:=1; for i:=n-1 downto t do for j:=n downto i+1 do if (a[i]<a[j])and(l[j]+1>l[i]) then begin l[i]:=l[j]+1; k[i]:=j; end; lm2:=l[t];
if (lm1>1)and(lm2>1)and(lm<lm1+lm2-1) then begin lom[lm1]:=a[t]; i:=h; j:=lm1-1; while i>0 do begin lom[j]:=a[i]; dec(j); i:=k[i]; end; j:=lm1+1; i:=k[t]; while i>0 do begin lom[j]:=a[i]; inc(j); i:=k[i]; end; lm:=lm1+lm2-1; end;
end; end;
procedure xuat; begin assign(f,fo); rewrite(f);
if li>lm then begin writeln(f,'loi'); writeln(f,li); for i:=1 to li do write(f,loi[i],' '); end else begin writeln(f,'lom'); writeln(f,lm); for i:=1 to lm do write(f,lom[i],' '); end;
close(f); end;
begin nhap; timloi; timlom; xuat; end. | |
|