POJ 2299 Ultra-QuickSort【树状数组】
2017-10-12 15:51
429 查看
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
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 int struct 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(树状数组+离散化)
- POJ2299 Ultra-QuickSort(树状数组求逆序数)
- poj 2299 Ultra-QuickSort(树状数组求逆序数)
- POJ 2299 Ultra-QuickSort【求逆序数:归并排序|树状数组】
- poj 2299 Ultra-QuickSort 【线段树 or 线段树+lazy or 树状数组 or 归并排序】 求逆序对
- poj-2299-Ultra-QuickSort-(树状数组and离散化)
- POJ 2299 Ultra-QuickSort(树状数组)
- POJ 2299 Ultra-QuickSort(树状数组水题)
- POJ 2299 Ultra-QuickSort(树状数组入门) 求逆序数