您的位置:首页 > 其它

codevs 3286 火柴排队

2016-02-15 15:03 274 查看
某年NOIP day1 压轴。

然而?首先我们展开这个式子,然后发现这是一个排序不等式的模型。

然后?!!双重离散化,首先离散化a,b,然后记录a数组每一个数应该到达的位置。

然后?我们的目的就变成了将a归并升序(降序)排列,求最少的操作数。

归并排序求逆序对。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 99999997
#define maxn 200005
using namespace std;
long long n,a[maxn],b[maxn],p[maxn],nowa[maxn],nowb[maxn],ans=0,tmp[maxn];
void merge(int left,int mid,int right)
{
int nowl=left,nowr=mid+1,cnt=left-1;
while ((nowl!=mid+1) && (nowr!=right+1))
{
if (a[nowl]>a[nowr])
{
ans=(ans+(mid-nowl+1))%mod;
tmp[++cnt]=a[nowr];
nowr++;
}
else
{
tmp[++cnt]=a[nowl];
nowl++;
}
}
while (nowl!=mid+1) {tmp[++cnt]=a[nowl];nowl++;}
while (nowr!=right+1) {tmp[++cnt]=a[nowr];nowr++;}
for (int i=left;i<=right;i++)
a[i]=tmp[i];
}
void merge_sort(int left,int right)
{
int mid=(left+right)>>1;
if (left<right)
{
merge_sort(left,mid);
merge_sort(mid+1,right);
merge(left,mid,right);
}
}
int main()
{
scanf("%lld",&n);
for (int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
nowa[i]=a[i];
}
for (int i=1;i<=n;i++)
{
scanf("%lld",&b[i]);
nowb[i]=b[i];
}
sort(nowa+1,nowa+n+1);
sort(nowb+1,nowb+n+1);
for (int i=1;i<=n;i++)
{
a[i]=lower_bound(nowa+1,nowa+n+1,a[i])-nowa;
b[i]=lower_bound(nowb+1,nowb+n+1,b[i])-nowb;
p[b[i]]=i;
}
for (int i=1;i<=n;i++)
a[i]=p[a[i]];
merge_sort(1,n);
printf("%lld\n",ans%mod);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: