NOIP2013提高组复赛 火柴排队 解题报告

NOIP2013提高组复赛 火柴排队 解题报告_第1张图片
 
 

 

NOIP2013提高组复赛 火柴排队 解题报告_第2张图片

const

c=99999997;

var

mid,key,a,b:array[1..100000]of longint;

u:array[1..100000]of longint;

n,m,i,j,k:longint;ans:int64;

procedure sort1(l,r: longint);

var

i,j,x,y: longint;

begin

i:=l;

j:=r;

x:=a[(l+r) div 2];

repeat

while a[i]

inc(i);

while x

dec(j);

if not(i>j) then

begin

y:=a[i];

a[i]:=a[j];

a[j]:=y;

y:=mid[i];

mid[i]:=mid[j];

mid[j]:=y;

inc(i);

j:=j-1;

end;

until i>j;

if l

sort1(l,j);

if i

sort1(i,r);

end;

procedure sort2(l,r: longint);

var

i,j,x,y: longint;

begin

i:=l;

j:=r;

x:=b[(l+r) div 2];

repeat

while b[i]

inc(i);

while x

dec(j);

if not(i>j) then

begin

y:=b[i];

b[i]:=b[j];

b[j]:=y;

y:=mid[i];

mid[i]:=mid[j];

mid[j]:=y;

inc(i);

j:=j-1;

end;

until i>j;

if l

sort2(l,j);

if i

sort2(i,r);

end;

procedure merge(i,j:longint);

var

k,p1,p2,l:longint;

begin

if i>=j then exit;

l:=(i+j)div 2;

merge(i,l);merge(l+1,j);

p1:=i;p2:=l+1;

for k:=i to j do

if ((p1<=l)and(a[p1]j) then

begin

u[k]:=a[p1];

inc(p1);

end

else

begin

u[k]:=a[p2];

ans:=(ans+l-p1+1) mod c;

inc(p2);

end;

for k:=i to j do a[k]:=u[k];

end;

begin

readln(n);

for i:=1 to n do read(a[i]);

readln;

for j:=1 to n do read(b[j]);

//读入,不解释

for i:=1 to n do mid[i]:=i;

sort1(1,n);

for i:=1 to n do a[mid[i]]:=i;

//a离散化成1..n的形式

for i:=1 to n do mid[i]:=i;

sort2(1,n);

for i:=1 to n do b[mid[i]]:=i;

//b离散化成1..n的形式

for i:=1 to n do key[b[i]]:=i;

for i:=1 to n do a[i]:=key[a[i]];

//a置为a中元素的目的地

ans:=0;

merge(1,n);

//归并秒杀

writeln(ans mod c);

end.

 

代码:

var
t,n,i,j:longint;
ans:int64;
a:array[1..200000,1..4]of longint;
procedure mergot(l,x,r:longint);
var
b:array[1..200000]of longint;
i,j,p:longint;
begin
i:=l;j:=x+1;p:=l;
while p<=r do begin
if (i<=x)and((j>r)or(a[i,4]<=a[j,4]))then begin
b[p]:=a[i,4];inc(i);end else begin
b[p]:=a[j,4];inc(j);ans:=ans+x-i+1;end;inc(p);end;
for i:=l to r do a[i,4]:=b[i];
end;
procedure msort(l,r:longint);
begin
if l>=r then exit;
msort(l,(l+r)shr 1);
msort((l+r)shr 1+1,r);
mergot(l,(l+r)shr 1,r);
end;
procedure swap(var x,y:longint);
var t:longint;
begin
t:=x;x:=y;y:=t;
end;
procedure qsort(l,r,x,y:longint);
var
i,j,mid,t:longint;
begin
i:=l;j:=r;mid:=a[(l+r)shr 1,x];
repeat
while a[i,x]mid do dec(j);
if i<=j then begin swap(a[i,x],a[j,x]);
swap(a[i,x+1+y],a[j,x+1+y]);inc(i);dec(j);end;
until i>j;
if i
if l
end;
begin
read(n);
for i:=1 to n do begin read(a[i,1]);a[i,2]:=i;end;
for i:=1 to n do begin read(a[i,3]);a[i,4]:=i;end;
qsort(1,n,1,0);qsort(1,n,3,0);qsort(1,n,2,1);
ans:=0;
msort(1,n);
writeln(ans mod 99999997);
end.

你可能感兴趣的:(NOIP解题报告)