首页 > 其他 > 详细

HDUOJ---3743Frosh Week(BIT+离散化)

时间:2014-04-09 11:44:21      阅读:336      评论:0      收藏:0      [点我收藏+]

Frosh Week

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1545    Accepted Submission(s): 497


Problem Description
During Frosh Week, students play various fun games to get to know each other and compete against other teams. In one such game, all the frosh on a team stand in a line, and are then asked to arrange themselves according to some criterion, such as their height, their birth date, or their student number. This rearrangement of the line must be accomplished only by successively swapping pairs of consecutive students. The team that finishes fastest wins. Thus, in order to win, you would like to minimize the number of swaps required.
 

 

Input
The first line of input contains one positive integer n, the number of students on the team, which will be no more than one million. The following n lines each contain one integer, the student number of each student on the team. No student number will appear more than once. 
 

 

Output
Output a line containing the minimum number of swaps required to arrange the students in increasing order by student number. 
 

 

Sample Input
3 3 1 2
 

 

Sample Output
2
 

 

Source
 
bubuko.com,布布扣
 1 /*
 2  树状数组求逆序数
 3 */
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<stdlib.h>
 7 #define maxn 1000000
 8 int nn;
 9 __int64 tol;
10 int aa[maxn+5];
11 
12 struct node
13 {
14     int id;
15     int val;
16 }stu[maxn+5];
17 //低位操作
18 int lowbit(int x)
19 {
20     return x&(-x);
21 }
22 
23 void ope(int x)
24 {
25     while(x<=nn)
26     {
27         aa[x]++;
28         x+=lowbit(x);
29     }
30 }
31 
32 __int64 sum(int x)
33 {
34      __int64 ans=0;
35     while(x>0)
36     {
37      ans+=aa[x];
38      x-=lowbit(x);
39     }
40     return ans;
41 }
42 int cmp(void const *a,void const *b)
43 {
44     return (*(struct node *)a).val - (*(struct node *)b).val;
45 }
46 int main()
47 {
48     int i,val;
49     while( scanf("%d",&nn)!=EOF)
50     {
51         tol=0;
52         memset(aa,0,sizeof(int)*(nn+5));
53         for(i=0;i<nn;i++)
54         {
55             scanf("%d",&stu[i].val);
56             stu[i].id=i+1;
57         }
58         qsort(stu,nn,sizeof(struct node),cmp);
59         for(i=0;i<nn;i++)
60         {
61             tol+=sum(nn)-sum(stu[i].id);
62             ope(stu[i].id);
63         }
64         printf("%I64d\n",tol);
65     }
66     return 0;
67 }
bubuko.com,布布扣

 

HDUOJ---3743Frosh Week(BIT+离散化),布布扣,bubuko.com

HDUOJ---3743Frosh Week(BIT+离散化)

原文:http://www.cnblogs.com/gongxijun/p/3653204.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!