1. 程式人生 > >[USACO17FEB] Why Did the Cow Cross the Road I P (樹狀數組求逆序對 易錯題)

[USACO17FEB] Why Did the Cow Cross the Road I P (樹狀數組求逆序對 易錯題)

-h 特殊性 另一個 %d .org class data 操作 efi

題目大意:給你兩個序列,可以序列進行若幹次旋轉操作(兩個都可以轉),對兩個序列相同權值的地方連邊,求最少的交點數

記錄某個值在第一個序列的位置,再記錄第二個序列中某個值 在第一個序列出現的位置 ,求逆序對數量即可

本以為是一道逆序對水題,結果被卡了20分。看了題解才恍然大悟,實際上,序列可以旋轉 ≠ 序列成環,由於逆序對的特殊性(並不適用於環),故不能把一個序列單獨旋轉看成它們的相對移動,正著旋轉一個序列≠反著旋轉另一個序列(更詳細證明可以看洛谷)

所以我們要對兩個序列再反著進行一次同樣的操作

#include <bits/stdc++.h>
#define N 200100
#define
ll long long using namespace std; ll ans,ret1,ret2,s[N]; int n,a[N],b[N],hx[N],c[N]; void update(int x,ll w) {for(int i=x;i<=n;i+=(i&(-i))) {s[i]+=w;}} ll query(int x) {ll ans=0; for(int i=x;i>0;i-=(i&(-i))) {ans+=s[i];} return ans;} int main() { //freopen("testdata.in","r",stdin);
scanf("%d",&n); for(int i=1;i<=n;i++) {scanf("%d",&a[i]);} for(int i=1;i<=n;i++) {scanf("%d",&b[i]);} for(int i=1;i<=n;i++) {hx[a[i]]=i;} for(int i=1;i<=n;i++) {c[i]=hx[b[i]];} for(int i=1;i<=n;i++) {ans+=query(n)-query(c[i]);update(c[i],(ll)1);}ret1=ans;
for(int i=1;i<=n;i++) {ans+=query(n)-query(c[i])-query(c[i]-1);ret1=min(ret1,ans);} memset(hx,0,sizeof(hx));memset(c,0,sizeof(c));memset(s,0,sizeof(s));ans=0; for(int i=1;i<=n;i++) {hx[b[i]]=i;} for(int i=1;i<=n;i++) {c[i]=hx[a[i]];} for(int i=1;i<=n;i++) {ans+=query(n)-query(c[i]);update(c[i],(ll)1);}ret2=ans; for(int i=1;i<=n;i++) {ans+=query(n)-query(c[i])-query(c[i]-1);ret2=min(ret2,ans);} printf("%lld",min(ret1,ret2)); return 0; }

[USACO17FEB] Why Did the Cow Cross the Road I P (樹狀數組求逆序對 易錯題)