归并排序
2015-08-03 16:15
176 查看
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 9 1 0 5 4 ,Ultra-QuickSort produces the output 0 1 4 5 9 .Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
Sample Output
题目大意:
给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列。
刚刚学了时间复杂度, 用归并排序Mergesort了,O(nlogn),省时,不会超时。
这里用归并排序并不是为了求交换次数,而是为了求序列的逆序数,而一个乱序序列的逆序数 = 在只允许相邻两个元素交换的条件下,得到有序序列的交换次数。
案例中的
9 1 0 5 4
要把它排列为升序0,1,4,5,9
而对于序列9 1 0 5 4
9后面却有4个比9小的元素,因此9的逆序数为4
1后面只有1个比1小的元素0,因此1的逆序数为1
0后面不存在比他小的元素,因此0的逆序数为0
5后面存在1个比他小的元素4, 因此5的逆序数为1
4是序列的最后元素,逆序数为0
因此序列9 1 0 5 4的逆序数 t=4+1+0+1+0 = 6 ,就是交换次数
(自己还不是很理解,所以拐到同学的解释来解释了........)
代码如下:
注意:保存逆序数时,必须要用long long型定义,否则会WA的。。。
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
题目大意:
给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列。
刚刚学了时间复杂度, 用归并排序Mergesort了,O(nlogn),省时,不会超时。
这里用归并排序并不是为了求交换次数,而是为了求序列的逆序数,而一个乱序序列的逆序数 = 在只允许相邻两个元素交换的条件下,得到有序序列的交换次数。
案例中的
9 1 0 5 4
要把它排列为升序0,1,4,5,9
而对于序列9 1 0 5 4
9后面却有4个比9小的元素,因此9的逆序数为4
1后面只有1个比1小的元素0,因此1的逆序数为1
0后面不存在比他小的元素,因此0的逆序数为0
5后面存在1个比他小的元素4, 因此5的逆序数为1
4是序列的最后元素,逆序数为0
因此序列9 1 0 5 4的逆序数 t=4+1+0+1+0 = 6 ,就是交换次数
(自己还不是很理解,所以拐到同学的解释来解释了........)
代码如下:
#include <stdio.h> int n,A[500005],T[500005],i; long long ans=0; void merge_sort(int* A,int x,int y,int*T){ if(y-x>1){ int m=x+(y-x)/2; int p=x,q=m,i=x; merge_sort(A,x,m,T); merge_sort(A,m,y,T); while(p<m||q<y){ if(q>=y||(p<m&&A[p]<=A[q])){ T[i++]=A[p++]; } else { T[i++]=A[q++]; ans+=m-p; } } for(i=x;i<y;i++) A[i]=T[i]; } } int main() { while(scanf("%d",&n)==1&&n){ ans=0; for(int i=0;i<n;i++) scanf("%d",&A[i]); merge_sort(A,0,n,T); printf("%lld\n",ans); } return 0; }
注意:保存逆序数时,必须要用long long型定义,否则会WA的。。。
相关文章推荐
- Main.storyboard绑定viewController
- 第11章 类的高级特性--final
- Linux 目录管理
- 英飞凌 DAVE™ 4.1.2 SDK 开发app学习笔记——什么是DAVE APP?
- hadoop资源
- iOS应用程序生命周期
- Handler功能
- Spring总结
- Ehcache集群
- 脏读、不可重复读 共享锁、悲观锁 和 事务五种隔离级别
- 文件及文件处理小结
- JAVA内存管理
- Use of undectared identifier '_xxxx'
- C语言:将结构体数组的成绩按照从小到大进行排序。
- php页面静态化方法小结
- BitmapFactory.decodeStream()获取bitmap返回null
- LeetCode -- 帕斯卡三角形
- IP获取城市位置信息 API
- 真机调试成功和打包安装后闪退
- iOS 将文本转换为语音