poj 2299 Ultra-QuickSort (树状数组+离散化)
2015-08-04 21:13
435 查看
Ultra-QuickSort
Description
![](http://poj.org/images/2299_1.jpg)
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
Source
Waterloo local 2005.02.05
这道题可以总结的地方不少。
1:对于一组乱序数列,每次只能交换相邻元素,达到有序交换的次数就是原数列中你逆序对的个数。
cf上好像总喜欢出这个题。。。我印象中就出现三次了。。。。。
[b]2:原始数组a[i]和树状数组的t[i]的对应问题(???存在疑问。。。应该只是这道题,而不是一般规律!)[/b]
这道题n是500000,如果直接开数组是可以开得下的,不需要离散化。但是树状数组的下标对应的是原始数组的值!也就是t[i]的下表最大可能为999,999,999 ! 显然存不下,需要离散化。
[b]3:学习了离散化的又一种写法。
[/b]
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 48257 | Accepted: 17610 |
![](http://poj.org/images/2299_1.jpg)
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
Source
Waterloo local 2005.02.05
这道题可以总结的地方不少。
1:对于一组乱序数列,每次只能交换相邻元素,达到有序交换的次数就是原数列中你逆序对的个数。
cf上好像总喜欢出这个题。。。我印象中就出现三次了。。。。。
[b]2:原始数组a[i]和树状数组的t[i]的对应问题(???存在疑问。。。应该只是这道题,而不是一般规律!)[/b]
这道题n是500000,如果直接开数组是可以开得下的,不需要离散化。但是树状数组的下标对应的是原始数组的值!也就是t[i]的下表最大可能为999,999,999 ! 显然存不下,需要离散化。
[b]3:学习了离散化的又一种写法。
[/b]
/************************************************************************* > File Name: code/poj/2299.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年08月04日 星期二 12时27分32秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #define y0 abc111qqz #define y1 hust111qqz #define yn hez111qqz #define j1 cute111qqz #define tm crazy111qqz #define lr dying111qqz using namespace std; #define REP(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef unsigned long long ULL; const int inf = 0x7fffffff; const int N=5E5+7; int n; int t ; int ref ; struct Q { int val,id; }q ; bool cmp(Q a,Q b) { if (a.val<b.val) return true; return false; } int lowbit( int x) { return x&(-x); } void update ( int x,int c) { for ( int i = x ; i < N ; i = i + lowbit(i)) { t[i] = t[i] + c; } } LL Sum( int x) { LL res = 0; for ( int i = x; i >= 1 ; i = i - lowbit(i)) { res = res + t[i]; } return res; } int main() { while (~scanf("%d",&n)&&n) { memset(t,0,sizeof(t)); for ( int i = 1 ; i <= n ; i++ ) { scanf("%d",&q[i].val); //离散化的时候相对大小不能打乱 q[i].id = i; } sort(q+1,q+n+1,cmp); for ( int i = 1 ; i <= n ; i++ ) { ref[q[i].id] = i; } LL ans = 0; for ( int i = 1 ; i <= n ; i++ ) { update(ref[i],1); ans = ans + i-Sum(ref[i]); // cout<<"ans:"<<ans<<endl; } cout<<ans<<endl; } return 0; }
相关文章推荐
- HD1047 Integer Inquiry 【大数问题】
- iOS UI04_Target-Action
- BIM建筑信息模型(Building Information Modeling)
- UI 03 UIViewController (视图控制器)的7个方法与视图的跳转
- Disque: antirez开发的开源分布式内存作业队列
- UI Overview
- 27.UIStepper和UISlider
- 通过UIView来模拟 实现 Button的点击效果
- ios使用storyboard中的segue从后往前进行传值
- 搜索 - hdu5288 OO’s Sequence
- JSP中页面跳转response.sendRedirect()和request.getRequestDispatcher()的区别
- 手势识别器(UIImageView)
- Android的Handler、MessageQueue、Runnable和Looper的联系(源码选自21版本)
- iOS UI04_Delegate
- UISlider 和 UISegement
- 26.手势识别器(UIGestureRecognizer)
- String、StringBuffer与StringBuilder之间区别
- 音频播放,gif图的播放,毛玻璃效果 UISlider,UIStepper,UISegmentedControl
- LeetCode#60 Permutation Sequence
- Android 更新UI的两种方法——handler和runOnUiThread()