火柴排队 2013年NOIP全国联赛提高组

题目描述 
Description

涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度。现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:
,其中 ai表示第一列火柴中第 i 个火柴的高度,bi表示第二列火柴中第 i 个火柴的高度。
每列火柴中相邻两根火柴的位置都可以交换,请你通过交换使得两列火柴之间的距离最小。请问得到这个最小的距离,最少需要交换多少次?如果这个数字太大,请输出这个最小交换次数对 99,999,997 取模的结果。

输入描述  Input Description

共三行,第一行包含一个整数 n,表示每盒中火柴的数目。
第二行有 n 个整数,每两个整数之间用一个空格隔开,表示第一列火柴的高度。
第三行有 n 个整数,每两个整数之间用一个空格隔开,表示第二列火柴的高度。

输出描述  Output Description

输出共一行,包含一个整数,表示最少交换次数对 99,999,997 取模的结果。

样例输入  Sample Input

[Sample 1]

2 3 1 4 
3 2 1 4
[Sample 2]

1 3 4 2 
1 7 2 4

样例输出  Sample Output

[Sample 1]
1
[Sample 2]
2

数据范围及提示 
Data Size & Hint
【样例1说明】
最小距离是 0,最少需要交换 1 次,比如:交换第 1 列的前 2 根火柴或者交换第 2 列的前 2 根火柴。
【样例2说明】
最小距离是 10,最少需要交换 2 次,比如:交换第 1 列的中间 2 根火柴的位置,再交换第 2 列中后 2 根火柴的位置。
【数据范围】
对于 10%的数据, 1 ≤ n ≤ 10; 
对于 30%的数据,1 ≤ n ≤ 100; 
对于 60%的数据,1 ≤ n ≤ 1,000; 
对于 100%的数据,1 ≤ n ≤ 100,000,0 ≤火柴高度≤ 2^31 - 1。

思路:
sigma(ai-bi)^2 最小
a1^2-2a1b1+b1^2+...an^2-2anbn+bn^2最小
x=simga(si^2)+simga(bi)^2
x-2(a1b1+a2b2+...+anbn)最小
a1b1+a2b2+...+anbn最大

排序不等式:顺序和>=乱序和>=逆序和

离散化

code:
var i,j,k:longint;
a,b,a1,b1:array[1..1000000]of longint;
n:longint;
mid,head,tail:longint;
ans:longint;
pos,r:array[1..1000000]of longint;
procedure qsort1(x,y:longint);
var head,tail,k:longint;
temp:longint;
begin head:=x; tail:=y;
k:=a1[(head+tail) div 2];
while head<tail do
begin while a1[head]<k do inc(head);
while k<a1[tail] do dec(tail);
if head<=tail
then begin temp:=a1[head];
a1[head]:=a1[tail];
a1[tail]:=temp;
inc(head);
dec(tail);
end;
end;
if head<y then qsort1(head,y);
if x<tail then qsort1(x,tail);
end;
procedure qsort2(x,y:longint);
var head,tail,k,temp:longint;
begin head:=x; tail:=y;
k:=b1[(head+tail) div 2];
while head<tail do
begin while b1[head]<k do inc(head);
while k<b1[tail] do dec(tail);
if head<=tail
then begin temp:=b1[head];
b1[head]:=b1[tail];
b1[tail]:=temp;
inc(head);
dec(tail);
end;
end;
if x<tail then qsort2(x,tail);
if head<y then qsort2(head,y);
end;
procedure msort(s,t:longint);
var m,i,j,k:longint;
begin if s=t then exit;
m:=(s+t) div 2;
msort(s,m);
msort(m+1,t);
i:=s; j:=m+1; k:=s;
while (i<=m)and(j<=t) do
begin if pos[b[i]]>pos[b[j]]
then begin r[k]:=b[i];
ans:=ans+t-j+1;
inc(i);
inc(k);
end
else begin r[k]:=b[j];
inc(j);
inc(k);
end;
end;
while (i<=m) do
begin r[k]:=b[i];
inc(i);
inc(k);
end;
while (j<=t) do
begin r[k]:=b[j];
inc(j);
inc(k);
end;
for i:=s to t do
b[i]:=r[i];
end;
begin readln(n);
for i:=1 to n do
begin read(a[i]);
a1[i]:=a[i];
end;
qsort1(1,n);
for i:=1 to n do
begin head:=1; tail:=n;
while head<tail do
begin mid:=(head+tail) div 2;
if a1[mid] >a[i]
then tail:=mid-1;
if a1[mid]<a[i]
then head:=mid;
if (a1[mid]=a[i])
then begin head:=mid;
break;
end;
if (a1[head]=a[i])
then break;
if (a1[tail]=a[i])
then begin head:=tail;
break;
end;
end;
a[i]:=head;
end;
for i:=1 to n do
begin read(b[i]);
b1[i]:=b[i];
end;
qsort2(1,n);
for i:=1 to n do
begin head:=1; tail:=n;
while head<tail do
begin mid:=(head+tail) div 2;
if b1[mid]>b[i]
then tail:=mid-1;
if b1[mid]<b[i]
then head:=mid;
if b1[head]=b[i]
then break;
if b1[mid]=b[i]
then begin head:=mid;
break;
end;
if b1[tail]=b[i]
then begin head:=tail;
break;
end;
end;
b[i]:=head;
end;
for i:=1 to n do
pos[a[i]]:=i;
ans:=0;
msort(1,n);
writeln(ans mod 99999997);
end.

转载于:https://www.cnblogs.com/spiderKK/p/4908522.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值