博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDUOJ---3743Frosh Week(BIT+离散化)
阅读量:5036 次
发布时间:2019-06-12

本文共 3059 字,大约阅读时间需要 10 分钟。

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
 
1 /* 2  树状数组求逆序数 3 */ 4 #include
5 #include
6 #include
7 #define maxn 1000000 8 int nn; 9 __int64 tol;10 int aa[maxn+5];11 12 struct node13 {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

 

运用归并排序求解:

递归版

1 #include
2 #include
3 #include
4 #define maxn 1000000 5 int aa[maxn+5]; 6 int cc[maxn+5]; 7 __int64 tol; 8 void merge(int low ,int mid ,int hight) 9 {10 int i,j,k;11 i=low;12 j=mid;13 k=0;14 while(i
aa[j])17 {18 cc[k++]=aa[j++];19 tol+=mid-i;20 }21 else22 cc[k++]=aa[i++];23 }24 for( ; i

 非递归版的归并排序

代码:

1 #include
2 #include
3 #include
4 #define maxn 1000000 5 int aa[maxn+5]; 6 int cc[maxn+5]; 7 __int64 tol; 8 void merge(int low ,int mid ,int hight) 9 {10 int i,j,k;11 i=low;12 j=mid;13 k=0;14 while(i
aa[j])17 {18 cc[k++]=aa[j++];19 tol+=mid-i;20 }21 else22 cc[k++]=aa[i++];23 }24 for( ; i

 

转载于:https://www.cnblogs.com/gongxijun/p/3653204.html

你可能感兴趣的文章
Linux内核OOM机制的详细分析
查看>>
Android TextView加上阴影效果
查看>>
Requests库的基本使用
查看>>
C#:System.Array简单使用
查看>>
C#inSSIDer强大的wifi无线热点信号扫描器源码
查看>>
「Foundation」集合
查看>>
算法时间复杂度
查看>>
二叉树的遍历 - 数据结构和算法46
查看>>
类模板 - C++快速入门45
查看>>
[转载]JDK的动态代理深入解析(Proxy,InvocationHandler)
查看>>
centos7 搭建vsftp服务器
查看>>
RijndaelManaged 加密
查看>>
Android 音量调节
查看>>
HTML&CSS基础学习笔记1.28-给网页添加一个css样式
查看>>
windows上面链接使用linux上面的docker daemon
查看>>
Redis事务
查看>>
Web框架和Django基础
查看>>
python中的逻辑操作符
查看>>
CSS兼容性常见问题总结
查看>>
HDU 1548 A strange lift (Dijkstra)
查看>>