luoguP3531 [POI2012]LIT-Letters

(https://www.luogu.org/problem/P3531)

注意编号

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int MAX = 1001001;

int n;
long long ans;
int p[MAX], num[MAX], t[MAX];
string a,b;
vector <int> v[27];

int add(int x, int k) {
    while(x <= n) {
        t[x] += k;
        x += x&(-x);
    }
}

int query(int x) {
    int res = 0;
    while(x) {
        res += t[x];
        x -= x&(-x);
    }
    return res;
}

int main() {
    scanf("%d",&n);
    cin>>a>>b;
    for(int i = 0; i < n; i++) v[b[i]-'A'+1].push_back(i+1);//对b编号
    for(int i = 0; i < n; i++) p[i+1] = v[a[i]-'A'+1][num[a[i]-'A'+1]++];//对a编号 
//  for(int i = 1; i <= n; i++) printf("%d ", p[i]);
//裸求逆序对(因为a中字母在b中的位置固定,对于相同的字母,一定是a中第一个位置的 放在 b中第一个位置, 第二个放第二个...)
    for(int i = 1; i <= n; i++) {
        add(p[i], 1);
        ans += (long long)i - query(p[i]);
    }
    printf("%lld\n",ans);
}

转载于:https://www.cnblogs.com/tyner/p/11544039.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值