Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:单调队列O(N^2) 63ms ACIn Reply To:单调队列O(N^2) 63ms AC Posted by:byplane747 at 2011-08-03 11:28:32 贴个单调队列的代码 写的有些麻烦了 两个n*n的循环就够了 为了美观 就写了4个循环 program dsqwwe; var q:array[0..1000] of longint; g1,g2,f1,f2,a:array[0..250,0..250] of longint; open,closed,i,j,n,b,k,x,y:longint; begin assign(input,'poj2019.in'); reset(input); assign(output,'poj2019.out'); rewrite(output); readln(n,b,k); for i:=1 to n do for j:=1 to n do read(a[i,j]); //max//////////////////////////// for i:=1 to n do begin fillchar(q,sizeof(q),0); closed:=1; open:=1; q[1]:=1; for j:=1 to n do begin while (closed<open) and (j-q[closed]>b-1) do inc(closed); g1[i,j]:=a[i,q[closed]]; if j<n then while (closed<=open) and (a[i,j+1]>=a[i,q[open]]) do dec(open); inc(open); q[open]:=j+1; end; end; for j:=1 to n do begin fillchar(q,sizeof(q),0); closed:=1; open:=1; q[1]:=1; for i:=1 to n do begin while (closed<open) and (i-q[closed]>b-1) do inc(closed); f1[i,j]:=g1[q[closed],j]; if i<n then while (closed<=open) and (g1[i+1,j]>=g1[q[open],j]) do dec(open); inc(open); q[open]:=i+1; end; end; //min//////////////////////////////// for i:=1 to n do begin fillchar(q,sizeof(q),0); closed:=1; open:=1; q[1]:=1; for j:=1 to n do begin while (closed<open) and (j-q[closed]>b-1) do inc(closed); g2[i,j]:=a[i,q[closed]]; if j<n then while (closed<=open) and (a[i,j+1]<=a[i,q[open]]) do dec(open); inc(open); q[open]:=j+1; end; end; for j:=1 to n do begin fillchar(q,sizeof(q),0); closed:=1; open:=1; q[1]:=1; for i:=1 to n do begin while (closed<open) and (i-q[closed]>b-1) do inc(closed); f2[i,j]:=g2[q[closed],j]; if i<n then while (closed<=open) and (g2[i+1,j]<=g2[q[open],j]) do dec(open); inc(open); q[open]:=i+1; end; end; //ans///////////////////////////////////// for i:=1 to k do begin readln(x,y); writeln(f1[x+b-1,y+b-1]-f2[x+b-1,y+b-1]); end; close(input); close(output); end. Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator