poj_2299 Ultra-QuickSort(归并排序/树状数组 求逆序对)
来源:互联网 发布:怎么查看手机淘宝等级 编辑:程序博客网 时间:2024/06/05 18:17
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 57323 Accepted: 21183
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
利用归并排序的性质求逆序对,要注意最后输出值较大 要用long long。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 500010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;int n;int a[maxn];int t[maxn];LL ans = 0;void Merge(int l, int m, int r){ int i = l, j = m+1, cot = l; while(i <= m && j <= r) { if(a[i] > a[j]) t[cot++] = a[j++], ans += m - i + 1; else t[cot++] = a[i++]; } while(i <= m) t[cot++] = a[i++]; while(j <= r) t[cot++] = a[j++]; for(i = l; i <= r; i++) a[i] = t[i];}void Msort(int l, int r){ if(l < r) { int m = l + r >> 1; Msort(l, m); Msort(m+1, r); Merge(l, m, r); }}int main(){ while(scanf("%d", &n) && n) { ans = 0; for(int i = 0; i < n; i++) scanf("%d", &a[i]); Msort(0, n-1); printf("%lld\n", ans); } return 0;}
也可以离散(也就是排序后用较小的数去代替原数)后用树状数组的性质做。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 500010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Node{ int v, pos;}a[maxn];int n;int aa[maxn]; //离散之后的数组int c[maxn]; //树状数组bool compare(Node a, Node b){ return a.v < b.v;}int lowbit(int i){ return i & (-i);}void update(int i, int val){ while(i <= n) { c[i] += val; i += lowbit(i); }}int Sum(int x){ int ans = 0; while(x > 0) { ans += c[x]; x -= lowbit(x); } return ans;}int main(){ while(scanf("%d", &n) && n) { for(int i = 1; i <= n; i++) { scanf("%d", &a[i].v); a[i].pos = i; } sort(a+1, a+n+1, compare); for(int i = 1; i <= n; i++) aa[a[i].pos] = i; memset(c, 0, sizeof(c)); LL ans = 0; for(int i = 1; i <= n; i++) { update(aa[i], 1); ans += i - Sum(aa[i]); } printf("%lld\n", ans); } return 0;}
0 0
- poj_2299 Ultra-QuickSort(归并排序/树状数组 求逆序对)
- Ultra-QuickSort (poj 2299 归并排序 || 树状数组 求逆序对)
- poj 2299 Ultra-QuickSort 求逆序对,树状数组和归并排序
- poj 2229 Ultra-QuickSort (归并排序求逆序数对|| 树状数组)
- POJ 2299 Ultra-QuickSort 求逆序对数(归并排序,树状数组)
- POJ_2299 Ultra-QuickSort【归并排序】
- poj 2299 Ultra-QuickSort(归并排序求逆序对)
- Ultra-QuickSort poj2299 (归并排序 求逆序数对)
- POJ 2299 Ultra-QuickSort 【归并排序求逆序数 OR 树状数组求逆序数】
- poj2299 Ultra-QuickSort&&NYOJ117 求逆序数 (树状数组求逆序对数+离散化)+(归并排序)
- poj 2299 Ultra-QuickSort 【线段树 or 线段树+lazy or 树状数组 or 归并排序】 求逆序对
- POJ2299 Ultra-QuickSort 归并排序和逆序数,树状数组
- POJ Ultra-QuickSort 逆序数 树状数组 归并排序
- POJ 2299 Ultra-QuickSort 【归并排序 || 树状数组求逆序对数】
- POJ 2299 Ultra-QuickSort(树状数组+离散化 或 归并排序求逆序)
- POJ 2299 Ultra-QuickSort【求逆序数:归并排序|树状数组】
- poj-2299-Ultra-QuickSort-归并排序求逆序数--或树状数组
- POJ 2299 Ultra-QuickSort(归并排序·树状数组·逆序对)
- hdu 1016Prime Ring Problem dfs经典题 素数环
- Android开发中的原生代码的网络访问和请求
- echarts(国产)基于html5-canvas的开源图表绘制组件
- ListView的优化
- C语言-- 二维数组变量指针取值的写法
- poj_2299 Ultra-QuickSort(归并排序/树状数组 求逆序对)
- 【栈】写一个带有getMin功能的栈
- 代码模板
- css页面初始化
- JAVA基础(11)——泛型
- javascript笔记(二)
- Maven的核心文件pom.xml解析
- Android自定义viewpager且高度自适应
- IRT模型