Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const
- finp = '';
- fout = '';
- maxn = 1000000 + 10;
- var
- fi,fo: text;
- a: array[0..maxn] of longint;
- next,previous,stack: array[0..maxn] of longint;
- n,kq,top: longint;
- procedure mofile;
- begin
- assign(fi,finp);
- reset(fi);
- assign(fo,fout);
- rewrite(fo);
- end;
- procedure dongfile;
- begin
- close(fi);
- close(fo);
- end;
- procedure push(i: longint);
- begin
- inc(top);
- stack[top] := i;
- end;
- procedure nhap;
- var
- i: longint;
- begin
- readln(fi,n);
- for i := 1 to n do
- begin
- read(fi,a[i]);
- next[i] := i;
- previous[i] := i;
- end;
- end;
- procedure tim;
- var
- i,dodaihientai: longint;
- begin
- kq := 0;
- a[0] := -1;
- a[n + 1] := -1;
- top := 0;
- for i := 1 to n + 1 do
- begin
- while (top > 0) and (a[i] < a[stack[top]]) do
- begin
- next[stack[top]] := i;
- dec(top);
- end;
- push(i);
- end;
- top := 0;
- for i := n downto 0 do
- begin
- while (top > 0) and (a[i] < a[stack[top]]) do
- begin
- previous[stack[top]] := i;
- dec(top);
- end;
- push(i);
- end;
- for i := 1 to n do
- begin
- dodaihientai := (next[i] - ord(i <> next[i])) - (previous[i] + ord(i <> next[i])) + 1;
- if (dodaihientai >= a[i]) and (a[i] > kq) then kq := a[i];
- end;
- end;
- procedure xuat;
- begin
- writeln(fo,kq);
- end;
- BEGIN
- mofile;
- nhap;
- tim;
- xuat;
- dongfile;
- END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement