luoguP1966 火柴排队(NOIP2013)(归并排序)

luogu P1966 火柴排队 题目

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<ctime>
#include<queue>
#include<stack>
#define il inline
#define rg register
#define lst long long
#define N 100050
#define mod 99999997
using namespace std;

int n;
lst ans;
struct MARCH{
    int high,num;
}a[N],b[N];
int c[N],d[N];

il int read()
{
    rg int s=0,m=1;rg char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')m=-1,ch=getchar();
    while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+ch-'0',ch=getchar();
    return s*m;
}

il int cmp(rg const MARCH &x,rg const MARCH &y)
{
    return x.high<y.high;
}

void Merge_sort(rg int l,rg int r)
{
    if(l>=r)return;
    rg int mid=(l+r)>>1;
    Merge_sort(l,mid),Merge_sort(mid+1,r);
    rg int ll=l,rr=mid+1,kk=l;
    while(ll<=mid&&rr<=r)
    {
        if(c[ll]>c[rr])
        {
            d[kk++]=c[rr++];
            ans=(ans+mid-ll+1)%mod;
        }
        else d[kk++]=c[ll++];
    }
    while(ll<=mid)d[kk++]=c[ll++];
    while(rr<=r)  d[kk++]=c[rr++];
    for(rg int i=l;i<=r;++i)c[i]=d[i];
}

int main()
{
    n=read();
    for(rg int i=1;i<=n;++i)a[i].high=read(),a[i].num=i;
    for(rg int j=1;j<=n;++j)b[j].high=read(),b[j].num=j;
    sort(a+1,a+n+1,cmp);
    sort(b+1,b+n+1,cmp);
    for(rg int i=1;i<=n;++i)
        c[a[i].num]=b[i].num;
    Merge_sort(1,n);
    printf("%lld\n",ans);
    return 0;
}

 

posted @ 2018-05-29 20:44  Eternal风度  阅读(108)  评论(0编辑  收藏  举报
/*自定义地址栏logo*/