Advertisement
icedragon41

KPLANK

Aug 29th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.31 KB | None | 0 0
  1. const
  2.     finp = '';
  3.     fout = '';
  4.     maxn = 1000000 + 10;
  5.  
  6. var
  7.     fi,fo: text;
  8.     a: array[0..maxn] of longint;
  9.     next,previous,stack: array[0..maxn] of longint;
  10.     n,kq,top: longint;
  11.  
  12. procedure mofile;
  13. begin
  14.     assign(fi,finp);
  15.     reset(fi);
  16.     assign(fo,fout);
  17.     rewrite(fo);
  18. end;
  19.  
  20. procedure dongfile;
  21. begin
  22.     close(fi);
  23.     close(fo);
  24. end;
  25.  
  26. procedure push(i: longint);
  27. begin
  28.     inc(top);
  29.     stack[top] := i;
  30. end;
  31.  
  32. procedure nhap;
  33. var
  34.     i: longint;
  35. begin
  36.     readln(fi,n);
  37.     for i := 1 to n do
  38.     begin
  39.         read(fi,a[i]);
  40.         next[i] := i;
  41.         previous[i] := i;
  42.     end;
  43. end;
  44.  
  45. procedure tim;
  46. var
  47.     i,dodaihientai: longint;
  48. begin
  49.     kq := 0;
  50.     a[0] := -1;
  51.     a[n + 1] := -1;
  52.    
  53.     top := 0;
  54.     for i := 1 to n + 1 do
  55.     begin
  56.         while (top > 0) and (a[i] < a[stack[top]]) do
  57.         begin
  58.             next[stack[top]] := i;
  59.             dec(top);
  60.         end;
  61.         push(i);
  62.     end;
  63.    
  64.     top := 0;
  65.     for i := n downto 0 do
  66.     begin
  67.         while (top > 0) and (a[i] < a[stack[top]]) do
  68.         begin
  69.             previous[stack[top]] := i;
  70.             dec(top);
  71.         end;
  72.         push(i);
  73.     end;
  74.    
  75.     for i := 1 to n do
  76.     begin
  77.         dodaihientai := (next[i] - ord(i <> next[i])) - (previous[i] + ord(i <> next[i])) + 1;
  78.         if (dodaihientai >= a[i]) and (a[i] > kq) then kq := a[i];
  79.     end;
  80. end;
  81.  
  82. procedure xuat;
  83. begin
  84.     writeln(fo,kq);
  85. end;
  86.  
  87. BEGIN
  88.     mofile;
  89.     nhap;
  90.     tim;
  91.     xuat;
  92.     dongfile;
  93. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement