poj 2229 Ultra-QuickSort (归并排序求逆序数对)
2015-08-08 16:08
141 查看
http://poj.org/problem?id=2299
Ultra-QuickSort
Description
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(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次?
解题思路:
根据冒泡排序的特点,我们可知,本题只需要统计每一个数的逆序数(如果有i<j,存在a[i] > a[j],则称a[i]与
a[j]为逆序数对),输出所有的数的逆序数的和用普通排序一定会超时,但是比较快的排序,像快排又无法统计
交换次数,这里就很好地体现了归并排序的优点。典型的利用归并排序求逆序数。
归并排序:比如现在有一个序列[l,r),我们可以把这个序列分成两个序列[l,mid),[mid,r),利用递归按照上
述方法逐步缩小序列,先使子序列有序,再使子序列区间有序,然后再把有序区间合并,很好滴体现了分治的思想。
Ultra-QuickSort
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 48444 | Accepted: 17684 |
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
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
题目描述:
给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次?
解题思路:
根据冒泡排序的特点,我们可知,本题只需要统计每一个数的逆序数(如果有i<j,存在a[i] > a[j],则称a[i]与
a[j]为逆序数对),输出所有的数的逆序数的和用普通排序一定会超时,但是比较快的排序,像快排又无法统计
交换次数,这里就很好地体现了归并排序的优点。典型的利用归并排序求逆序数。
归并排序:比如现在有一个序列[l,r),我们可以把这个序列分成两个序列[l,mid),[mid,r),利用递归按照上
述方法逐步缩小序列,先使子序列有序,再使子序列区间有序,然后再把有序区间合并,很好滴体现了分治的思想。
///#pragma comment (linker, "/STACK:102400000,102400000") #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; #define N 501005 #define INF 0xfffffff #define PI acos (-1.0) #define EPS 1e-8 long long cnt; int a , b ; void Merge (int l, int r); int main () { int n; while (scanf ("%d", &n), n) { memset (a, 0, sizeof (a)); memset (b, 0, sizeof (b)); for (int i=0; i<n; i++) scanf ("%d", &a[i]); cnt = 0;//一定要用int64,int32会溢出 Merge (0, n); printf ("%I64d\n", cnt); } return 0; } void Merge (int l, int r)//归并排序,参数分别是子区间的位置 { if (r - l <= 1) return; int mid = (l + r) / 2; Merge (l, mid); Merge (mid, r); int x = l, y = mid, i = l; while (x<mid || y<r)//对子序列进行排序,并且存到数组b里面 { if (y>=r || (x<mid && a[x] <= a[y])) b[i++] = a[x++]; else { if (x < mid) cnt += mid - x;//记录交换次数 b[i++] = a[y++]; } } for (int i=l; i<r; i++)//把排好序的子序列抄到a数组对应的位置 a[i] = b[i]; }
相关文章推荐
- Selenium2学习-033-WebUI自动化实战实例-031-页面快照截图应用之二 -- 区域截图
- hdu 1711Number Sequence (KMP——输出模式串第一次出现位置)
- UIButton
- android stduio 引入 .so
- UISegmentedControl方法与属性的总结
- UI08_UItableView(关于cell滑动条消失,隐藏cell线,解决cell的线不对应问题)
- 周赛-Equidistant String
- 周赛-Equidistant String 分类: 比赛 2015-08-08 15:44 6人阅读 评论(0) 收藏
- iOS基础-UIKit框架-触摸事件-事件传递
- Xib中UIScrollView添加约束步骤
- UILabel与UIFont的用法和属性的一些总结
- 蓝牙协议(bluetooth spec)
- UIActivityIndicatorView控件的属性和方法
- UIProgress控件的属性和方法
- UIPageControll 的属性和用法
- Ultra-QuickSort
- IOS中UIAlertView(警告框)常用方法总结
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
- IOS中UIActionSheet使用方法详解
- 手机开发之H5+规范的原生UI警示框