poj 2299 Ultra-QuickSort 二分+线段树求逆序数
来源:互联网 发布:java游戏编程实例 编辑:程序博客网 时间:2024/05/11 02:14
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 47564 Accepted: 17354
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.
Ultra-QuickSort produces the output
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
59105431230
Sample Output
60
题意:给出一个序列,可以交换任意两个数,最终使得序列从小到大排序,求最少交换次数。
思路:一个数要交换的次数就是看他前面有几个数比它大,也就是它的逆序数。最终就是求所有数的逆序数之和。但是看题目条件n最大有50000,若每个逆序数都循环一遍,就是n*n肯定超时了,得改进。一个思路就是,先将数列排序,对于原始数列中每一个数,二分找出它在排序之后的位置p,然后插入到线段树中对应位置,那么每个数的逆序数就是查询[p,n]中有多少个数。
代码:
#include <cstdio>#include <algorithm>using namespace std;#define maxn 500000+10#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int n;int sum[maxn<<2];int b[maxn];int a[maxn];void pushUp(int rt){sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void build(int l,int r,int rt){if(l==r){sum[rt] = 0;return;}int m = (l+r) >> 1;build(lson);build(rson);pushUp(rt);}void update(int p,int l,int r,int rt){if(l==r){sum[rt] = 1;return;}int m = (l+r) >>1;if(p<=m) update(p,lson);else update(p,rson);pushUp(rt);}int query(int L,int R,int l,int r,int rt){if(L<=l && r<=R) return sum[rt];int m = (l+r) >> 1;int res = 0;if(L<=m) res += query(L,R,lson);if(R>m) res += query(L,R,rson);return res;}int main(){while(~scanf("%d",&n) && n){build(0,n-1,1);for(int i=0;i<n;i++){scanf("%d",&b[i]);a[i] = b[i];}sort(b,b+n);long long ans = 0;for(int i=0;i<n;i++){int p = a[i];int l=0,r=n,m;while(l<r){m = (l+r) >> 1;if(b[m] > p) r = m;else l = m+1;}int temp = query(r-1,n-1,0,n-1,1);//printf("@@@%d %d\n",r-1,temp);ans += temp;update(r-1,0,n-1,1);}printf("%lld\n",ans);}return 0;}
0 1
- poj 2299 Ultra-QuickSort 二分+线段树求逆序数
- poj 2299 Ultra-QuickSort 线段树求逆序数+离散化||归并排序求逆序数
- POJ 2299 Ultra-QuickSort (求逆序数)
- poj 2299 Ultra-QuickSort(线段树,离散化求逆序数)
- poj 2299 Ultra-QuickSort——归并排序求逆序数,线段树离散化
- POJ 2299 Ultra-QuickSort (树状数组求逆序数 || 线段树 +离散化)
- POJ 2299 Ultra-QuickSort——离散化+线段树求逆序数
- poj 2299 Ultra-QuickSort(归并排序求逆序数)
- poj 2299 Ultra-QuickSort(求逆序数,树状数组)
- poj 2299 Ultra-QuickSort 求逆序数 树状数组解法
- Ultra-QuickSort poj 2299--树状数组求逆序数
- Poj 2299 Ultra-QuickSort(归并排序求逆序数)
- poj 2299 Ultra-QuickSort :归并排序求逆序数
- POJ 2299 Ultra-QuickSort(归并排序求逆序数)
- POJ 2299 Ultra-QuickSort 分治法求逆序数
- poj 2299 Ultra-QuickSort 树状数组求逆序数
- poj 2299 Ultra-QuickSort(树状数组 / 求逆序数)
- poj 2299 Ultra-QuickSort 树状数组求逆序数
- Github上的andoird开源组件整理
- hdu1133 Buy the Ticket (卡兰特数应用+java大数)
- Snail—OC学习之可变数组NSMutableArray
- ORACLE系统函数
- WatchKit控件叠加达到类似addSubview的效果
- poj 2299 Ultra-QuickSort 二分+线段树求逆序数
- 黑马程序员——java要点笔记——正则表达式
- 【版本控制】Github for Windows客户端的使用
- 【UIKit】-4-UIActivityIndicatorView - 系统转菊花
- Android不让弹出键盘挡住View
- JAX-WS使用Handler实现简单的WebService权限验证
- Android 离线播放 (HLS)m3u8文件
- shell tr命令
- 浮动IP简介