码迷,mamicode.com
首页 > 其他好文 > 详细

luogu 1966 火柴排队 离散化+逆序对

时间:2018-10-03 23:45:05      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:code   编号   define   fine   存在   ace   std   air   class   

题意:找到最小改变对数使a数组的第i大和b数组的第i大相等

则先将a,b,数组编号再排序,则数组显示的就是排名第i的数的编号

再关键一步:c[a[i].id]=b[i].id

实质上就是新建一个数组,按照现有a数组的排布,和b数组进行比较,看是否有逆序对存在,有则需要更换,故再求逆序对即可

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define dec(i,x,y) for(register int i=x;i>=y;i--)
#define LL long long
#define int long long
using namespace std;
const int mod=99999997;
const int N=2e6+50;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch==-)f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;}int n,ans,c[N],k[N];
pair<int,int> a[N],b[N];
inline void add(int x,int d){for(int i=x;i<=n;i+=i&(-i)) k[i]+=d;}
inline int query(int x){int ans=0;for(int i=x;i;i-=i&(-i)) ans+=k[i];return ans;}
signed main(){
    n=read();
    rep(i,1,n) a[i].first=read(),a[i].second=i;
    rep(i,1,n) b[i].first=read(),b[i].second=i;
    sort(a+1,a+1+n);sort(b+1,b+1+n);
    rep(i,1,n) c[a[i].second]=b[i].second;
    dec(i,n,1) ans=(ans+query(c[i]))%mod,add(c[i],1);
    printf("%lld\n",ans%mod);return 0;
}

 

luogu 1966 火柴排队 离散化+逆序对

标签:code   编号   define   fine   存在   ace   std   air   class   

原文地址:https://www.cnblogs.com/asdic/p/9738838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!