分析
题意简单:给定一个标准,让你求另一个序列的逆序对数
序列都是字符串形式,字符串哈希即可,这里我选择map(反正也能过)
然后用树状数组经典求法即可
#include <iostream> #include <cstdio> #include <map> #include <cstring> #include <string> #define lowbit(x) x&-x using namespace std; typedef long long ll; const int N=1e5+10; ll t[N],ans; int a[N]; map<string,int> h; string c; int n,cnt; void Add(int x) { for (;x<N;x+=lowbit(x)) t[x]++; } ll Sum(int x) { ll ans=0; for (;x;x-=lowbit(x)) ans+=t[x]; return ans; } int main() { scanf("%d",&n); for (int i=1;i<=n;i++) { cin>>c; h[c]=++cnt; } for (int i=1;i<=n;i++) { cin>>c; a[i]=h[c]; } for (int i=n;i;i--) ans+=Sum(a[i]),Add(a[i]); printf("%lld",ans); return 0; }