传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2789
逆序对,不多说
Code:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
int d[maxn];
int p[maxn];
deque<int>v[233];
char s1[maxn],s2[maxn];
int n;
#define lowbit(x) (x&-x)
int get(int x){
int ans=0;
while(x)ans+=d[x],x-=lowbit(x);
return ans;
}
void updata(int x,int f){
while(x<=n)d[x]+=f,x+=lowbit(x);
}
int main(){
scanf("%d",&n);
scanf("%s",s1+1);
scanf("%s",s2+1);
for(int i=1;i<=n;i++)
v[s1[i]-'A'].push_back(i);
for(int i=1;i<=n;i++){
p[i]=v[s2[i]-'A'].front();
v[s2[i]-'A'].pop_front();
}long long ans=0;
for(int i=1;i<=n;i++){
ans+=i-get(p[i])-1;
updata(p[i],1);
}cout<<ans<<endl;
return 0;
}