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;
}
然而?首先我们展开这个式子,然后发现这是一个排序不等式的模型。
然后?!!双重离散化,首先离散化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;
}
相关文章推荐
- 在Mac OS的PHP环境下安装配置MemCache的全过程解析
- [Python]网络爬虫2
- git push error: failed to push some refs to 'ssh://git@ip:8850/Out/Afuyigou.
- 关闭Tomcat日志输出catalina.out
- Java中String类的isEmpty方法、null以及""的区别
- 如何从京东商品里获取需要的信息
- UIImagePickerController详解
- gz文件的解压和压缩
- Qt窗口句柄
- 关于IOS开发证书显示“此证书的签发者无效”解决方法
- 【题解】洛谷1579 哥德巴赫猜…
- iOS可执行文件瘦身方法
- MySQL引擎
- 【题解】洛谷2089 烤鸡
- 【题解】洛谷1426 小鱼会有危险吗
- 【题解】洛谷1618 三连击(升…
- 【题解】洛谷1478 陶陶摘苹果…
- 注意!最新.Net Framework与Exchange 的兼容性问题
- 【题解】洛谷1217 回文质数
- [CSS] CSS 书写规范、顺序