UESTC 电子科大专题训练 数据结构-E

UESTC 1583

题意:中文题

思路:预处理将要变成的序列映射成1 2 3...n,数状数组每次求每个数前比它本身大的数,因为最后要变成1 2 3 ....n的序列,所以每个数字(映射后的)应该要交换到它值的位置上(也就是1的位置是1,2的位置是2,n的位置是n),如果当前数字(映射后的)前面有比它大的数,那必然要通过交换,将比他大的数交换到它后面去

AC代码:

#include "iostream"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#define ll long long
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a) memset(a,0,sizeof(a))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
using namespace std;
const long long INF = 1e18+1LL;
const int inf = 1e9+1e8;
const int N=1e5+100;
const ll mod=1e9+7;

ll n,ans,C[N],a[N],b[N];
map<int, int> M;
int lowbit(int x){
    return (-x)&x;
}
void add(int x, int num){
    while(x<=n){
        C[x]+=num;
        x+=lowbit(x);
    }
}
ll sum(int x){
    int ret=0;
    while(x>0){
        ret+=C[x];
        x-=lowbit(x);
    }
    return ret;
}
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1; i<=n; ++i){
        cin>>a[i];
    }
    for(int i=1; i<=n; ++i){
        cin>>b[i];
        M[b[i]]=i;
    }
    for(int i=1; i<=n; ++i){
        add(M[a[i]],1);
        ans+=sum(n)-sum(M[a[i]]);
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/max88888888/p/7230746.html