P1966 火柴排队
进行映射,然后求逆序对即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<map> 9 #include<stack> 10 #include<cstring> 11 #define inf 2147483647 12 #define For(i,a,b) for(register int i=a;i<=b;i++) 13 #define p(a) putchar(a) 14 #define g() getchar() 15 #define mod 99999997 16 //by war 17 //2017.11.8 18 using namespace std; 19 int n; 20 int a[100010]; 21 int b[100010]; 22 int c[100010]; 23 int t[100010]; 24 int ans; 25 void in(int &x) 26 { 27 int y=1; 28 char c=g();x=0; 29 while(c<'0'||c>'9') 30 { 31 if(c=='-') 32 y=-1; 33 c=g(); 34 } 35 while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g(); 36 x*=y; 37 } 38 void o(int x) 39 { 40 if(x<0) 41 { 42 p('-'); 43 x=-x; 44 } 45 if(x>9)o(x/10); 46 p(x%10+'0'); 47 } 48 49 void modify(int k) 50 { 51 for(;k<=n;k+=(-k)&k) 52 t[k]++; 53 } 54 55 int get(int k) 56 { 57 int cnt=0; 58 for(;k>0;k-=(-k)&k) 59 cnt=(cnt+t[k])%mod; 60 return cnt; 61 } 62 63 int main() 64 { 65 in(n); 66 For(i,1,n) 67 in(a[i]),c[a[i]]=i; 68 For(i,1,n) 69 in(b[i]),b[i]=c[b[i]]; 70 For(i,1,n) 71 { 72 if(b[i]!=0) 73 { 74 modify(b[i]); 75 ans=(ans+i-get(b[i]))%mod; 76 } 77 else 78 { 79 modify(n); 80 ans=(ans+i-get(n))%mod; 81 } 82 } 83 o(ans%mod); 84 return 0; 85 }