P1966 火柴排队
emmmmm不难吧。。
主要就是在最开始要把题目抽象化,转为求逆序对个数,那之后就很简单了,离散化+归并排序求逆序对,取模输出就结束了
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<stack> 7 #include<deque> 8 #include<algorithm> 9 #define ll long long 10 using namespace std; 11 const int oo=0x3f3f3f3f; 12 const int N=1000005; 13 const int mod=99999997; 14 15 int n,ans; 16 int f[N],f1[N]; 17 struct node{ 18 int x,y; 19 }a[N]; 20 struct node1{ 21 int x,y; 22 }b[N]; 23 24 bool cmp1(node a,node b){return a.x<b.x;} 25 bool cmp2(node1 a,node1 b){return a.x<b.x;} 26 27 ll get(){ 28 char zy=getchar(); 29 ll z=1,y=0; 30 while(zy>'9'||zy<'0'){ 31 if(zy=='-') z=-1; 32 zy=getchar(); 33 } 34 while(zy>='0'&&zy<='9'){ 35 y=(y<<1)+(y<<3)+zy-'0'; 36 zy=getchar(); 37 } 38 return z*y; 39 } 40 41 void merge_sort(int l,int r){ 42 if(l>=r) return; 43 int mid=(l+r)>>1; 44 merge_sort(l,mid); 45 merge_sort(mid+1,r); 46 int p=l,q=mid+1,g=l; 47 while(p<=mid||q<=r){ 48 if(q>r||(f[p]<=f[q]&&p<=mid)) f1[g++]=f[p++]; 49 else{ 50 ans+=mid-p+1; 51 ans%=mod; 52 f1[g++]=f[q++]; 53 } 54 } 55 for(int i=l;i<=r;i++){ 56 f[i]=f1[i]; 57 } 58 } 59 60 int main(){ 61 //freopen("P1966火柴排队.in","r",stdin); 62 //freopen("P1966火柴排队.out","w",stdout); 63 n=get(); 64 for(int i=1;i<=n;i++){ 65 a[i].x=get();a[i].y=i; 66 } 67 sort(a+1,a+1+n,cmp1); 68 for(int i=1;i<=n;i++){ 69 b[i].x=get();b[i].y=i; 70 } 71 sort(b+1,b+1+n,cmp2); 72 for(int i=1;i<=n;i++){ 73 f[a[i].y]=b[i].y; 74 } 75 for(int i=1;i<=n;i++){ 76 // printf("%d ",f[i]); 77 } 78 merge_sort(1,n); 79 printf("%d\n",ans%mod); 80 return 0; 81 }