码迷,mamicode.com
首页 > 其他好文 > 详细

火柴排队

时间:2019-09-08 20:15:07      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:while   归并排序   str   nbsp   clu   family   pre   namespace   std   

【TIMEGate】

https://www.luogu.org/problem/P1966

【解题思路】

归并排序

【code】

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 using namespace std;
 5 const int mod=99999997;
 6 int i,n,ans,mid;
 7 int c[100005],d[100005];
 8 struct node{
 9        int h;
10        int id;
11 }a[100005],b[100005];
12 bool cmp(node a,node b){
13      return a.h<b.h;
14 }
15 void mergesort(int left,int right){
16     if(right<=left)return;
17     int mid=(left+right)/2;
18     int i=left,j=mid+1,cnt=0;
19     mergesort(left,mid);
20     mergesort(mid+1,right);
21     while(i<=mid&&j<=right){
22         if(c[i]<=c[j])d[++cnt]=c[i++];
23         else {
24             d[++cnt]=c[j++];
25             ans+=mid-i+1;
26             ans%=mod;
27         }
28     }
29     if(i>mid)
30         while(j<=right)
31             d[++cnt]=c[j++];
32     else 
33         while(i<=mid)
34             d[++cnt]=c[i++];
35     for(i=left,j=1;j<=cnt;i++,j++)c[i]=d[j];
36 }
37 int main(){
38     //freopen("match.in","r",stdin);
39     //freopen("match.out","w",stdout);
40     scanf("%d",&n);
41     for (i=1;i<=n;i++){
42         scanf("%d",&a[i].h);
43         a[i].id=i;
44     }
45     for (i=1;i<=n;i++){
46         scanf("%d",&b[i].h);
47         b[i].id=i;
48     }
49     sort(a+1,a+n+1,cmp);
50     sort(b+1,b+n+1,cmp);
51     for (i=1;i<=n;i++)
52         c[b[i].id]=a[i].id;
53     mergesort(1,n);           
54     printf("%d\n",ans);
55     return 0;
56 }

 

火柴排队

标签:while   归并排序   str   nbsp   clu   family   pre   namespace   std   

原文地址:https://www.cnblogs.com/66dzb/p/11487950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!