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

Re:各位的程序几乎都是错的

Posted by SCQ at 2013-04-27 21:15:10 on Problem 1743
In Reply To:各位的程序几乎都是错的 Posted by:Tune at 2013-03-28 08:48:38
有标记的地方须注意

program poj1743;
const
  maxn=20000+10;inf=1000000;
var
  x,y,sa,rank,h:array[0..2*maxn] of longint;
  num:array[0..maxn] of longint;
  n,t:longint;
procedure swap(var a,b:longint);
begin
  t:=a;a:=b;b:=t;
end;

procedure qsort(l,r:longint);
var
  i,j,xx,yy:longint;
begin
  i:=l;j:=r;
  xx:=x[(i+j)>>1];yy:=y[(i+j)>>1];
  repeat
    while (x[i]<xx)or((x[i]=xx)and(y[i]<yy)) do inc(i);
    while (x[j]>xx)or((x[j]=xx)and(y[j]>yy)) do dec(j);
    if i<=j then
    begin
      swap(x[i],x[j]);
      swap(y[i],y[j]);
      swap(sa[i],sa[j]);
      inc(i);dec(j);
    end;
  until i>j;
  if i<r then qsort(i,r);
  if l<j then qsort(l,j);
end;

function check2(x:longint):boolean;
var
  i,j,k:longint;
begin
  i:=1;
  while i<n do
  begin
    j:=inf;k:=-inf;
    while h[i+1]>=x do
    begin
      if j>sa[i] then j:=sa[i];
      if k<sa[i] then k:=sa[i];
      inc(i);
    end;
    if j>sa[i] then j:=sa[i];
    if k<sa[i] then k:=sa[i];
    if k-j>x then exit(true);//这里是>非>=
    inc(i);
  end;
  exit(false);
end;

procedure check;
var
  l,r,mid,ans:longint;
begin
  ans:=-1;l:=4;r:=n;//这里ans=-1时+1刚好0
  while l<r do      //把【最小长度为5】的限制去掉就是l=1;
  begin
    mid:=(l+r)>>1;
    if check2(mid) then
    begin
      l:=mid+1;ans:=mid;//这样二分不会有错,注意开始是l<r,不符时l:=mid+1;
    end else r:=mid;
  end;
  writeln(ans+1);
end;

procedure work;
var
  p,i,j,sum:longint;
begin
  p:=1;
  while p<n do
  begin
    for i:=1 to n do
    begin
      x[i]:=rank[i];
      y[i]:=rank[i+p];
      sa[i]:=i;
    end;
    qsort(1,n);
    sum:=1;
    rank[sa[1]]:=1;
    for i:=2 to n do
    begin
      if (x[i]<>x[i-1])or(y[i]<>y[i-1]) then inc(sum);
      rank[sa[i]]:=sum;
    end;
    if sum=n then break;
    p:=p<<1;
  end;
  p:=0;h[1]:=0;
  for i:=1 to n do
  begin
    if rank[i]=1 then continue;
    j:=sa[rank[i]-1];
    while num[i+p]=num[j+p] do inc(p);
    h[rank[i]]:=p;
    if p>0 then dec(p);
  end;
end;

procedure readdata;
var
  i:longint;
begin
  read(n);
  while n<>0 do
  begin
    for i:=1 to n do read(num[i]);
    for i:=1 to n-1 do
    begin
      num[i]:=num[i+1]-num[i];
      rank[i]:=num[i];
    end;
    rank[n]:=num[n];
    work;
    check;
    read(n);
  end;
end;

begin
  readdata;
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