POJ 2299 Ultra-QuickSort【树状数组】
来源:互联网 发布:网络暴力事件案例 编辑:程序博客网 时间:2024/05/14 21:21
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 64049 Accepted: 23919
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
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
Source
Waterloo local 2005.02.05
题意:给出一组数据,求其通过快排成有序的操作数。
快速排序的底部操作是选择排序。每进行一次交换,解决一个或多个逆序。
换句话说,整个逆序对的总量,就是答案。
本题有多种解法:(树状数组、线段树、快速排序、归并排序)
1.树状数组:1370ms
#include<iostream>#include<algorithm>#include<cstring>using namespace std;#define lowbit(x) (x&(-x))//求最低位数字const int maxn = 5e5 + 5;#define LL long long intstruct Data{ int id, w;}num[maxn];int n, a[maxn];bool cmp(Data a, Data b){ return a.w > b.w;}void add(int i){ while (i <= n) { a[i] += 1; i += lowbit(i); }}LL sum(int i){ LL ans = 0; while (i > 0) { ans += a[i]; i -= lowbit(i); } return ans;}int main(){ int i; while (cin >> n&&n) { memset(a, 0, sizeof(a)); for (int i = 0; i < n; i++) { num[i].id = i + 1; cin >> num[i].w; } sort(num, num + n, cmp); LL ans = 0; for (int i = 0; i < n; i++) { ans += sum(num[i].id - 1);//+逆序数 add(num[i].id); } cout << ans << endl; } return 0;}
- 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(树状数组)
- 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(树状数组)
- 含水最多的容器(Container With Most Water)
- 洛谷P1008 三连击
- TextView指定位置颜色变化
- BigDecimal
- [学习][poj1523]割点 SPF
- POJ 2299 Ultra-QuickSort【树状数组】
- 常用依赖
- 开发学习软件,求帮忙内测的小伙伴
- 日期插件layDate的使用
- 教你五招:防御互联网最可怕搜索Shodan
- 水仙花数C++
- cocos2d-js下的热更新机制
- Markdown学习
- 解决A generic error occurred in GDI+的问题