Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

用RMQ过的,要优化啊!!!+贴pascal代码

Posted by oierfc at 2011-07-27 08:20:34 on Problem 2823
其实只要算trunc(ln(p)/ln(2))的长度就行了,所以可以不断滚动数组。空间就不会爆掉了。
var n,k,i,j,t,p:longint;
    a,f:Array [0..1000001] of longint;

function max(a,b:longint):longint;
begin
  if a>b then exit(a) else exit(b);
end;

function min(a,b:longint):longint;
begin
  if a>b then exit(b) else exit(a);
end;

begin
  readln(n,p);
  for i:=1 to n do
    begin
      read(a[i]);
      f[i]:=a[i];
    end;

  t:=trunc(ln(p)/ln(2));
  for i:=1 to t do
    for j:=1 to n do
      if j+1 shl (i-1)<=n then
        f[j]:=min(f[j],f[j+1 shl (i-1)]);

  for i:=1 to n-p+1 do write(min(f[i],f[i+p-1 shl t]),' ');
  writeln;

  for i:=1 to n do f[i]:=a[i];
  for i:=1 to t do
    for j:=1 to n do
      if j+1 shl (i-1)<=n then
        f[j]:=max(f[j],f[j+1 shl (i-1)]);

  for i:=1 to n-p+1 do write(max(f[i],f[i+p-1 shl t]),' ');
  writeln;
end.

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator