Ultra-QuickSort
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 61660 | Accepted: 22897 |
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
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6 0给定n个数,要求这些数构成的逆序对的个数。除了用归并排序来求逆序对个数,还可以使用树状数组来求解
树状数组求解的思路:开一个能大小为这些数的最大值的树状数组,并全部置0。从头到尾读入这些数
每读入一个数就更新树状数组,查看它前面比它小的已出现过的有多少个数sum,然后用当前位置减去该sum
就可以得到当前数导致的逆序对数了。把所有的加起来就是总的逆序对数
题目中的数都是独一无二的,这些数最大值不超过999999999,但n最大只是500000
如果采用上面的思想,必然会导致空间的巨大浪费,而且由于内存的限制,我们也不可能开辟这么大的数组
因此可以采用一种称为“离散化”的方式,把原始的数映射为1-n一共n个数,这样就只需要500000个int类型的空间
离散化的方式:
详见:http://blog.csdn.net/greatjames/article/details/75338977 假设输入的数组是9 1 0 5 4, 离散后的结果a[] = {5,2,1,4,3}; 在离散结果中间结果的基础上,那么其计算逆序数的过程是这么一个过程。 1.输入5, 调用add(5, 1),把第5位设置为1 1 2 3 4 5 0 0 0 0 1 计算1-5上比5小的数字存在么? 这里用到了树状数组的sum(5) = 1操作, 现在用输入的下标1 -sum(5) = 0 就可以得到对于5的逆序数为0。 2. 输入2, 调用add(2, 1),把第2位设置为1 1 2 3 4 5 0 1 0 0 1 计算1-2上比2小的数字存在么? 这里用到了树状数组的sum(2) = 1操作, 现在用输入的下标2 - sum(2) = 1 就可以得到对于2的逆序数为1。 3. 输入1, 调用add(1, 1),把第1位设置为1 1 2 3 4 5 1 1 0 0 1 计算1-1上比1小的数字存在么? 这里用到了树状数组的sum(1) = 1操作, 现在用输入的下标 3 -sum(1) = 2 就可以得到对于1的逆序数为2。 4. 输入4, 调用add(4, 1),把第5位设置为1 1 2 3 4 5 1 1 0 1 1 计算1-4上比4小的数字存在么? 这里用到了树状数组的sum(4) = 3操作, 现在用输入的下标4 - sum(4) = 1 就可以得到对于4的逆序数为1。 5. 输入3, 调用add(3, 1),把第3位设置为1 1 2 3 4 5 1 1 1 1 1 计算1-3上比3小的数字存在么? 这里用到了树状数组的sum(3) = 3操作, 现在用输入的下标5 - sum(3) = 2 就可以得到对于3的逆序数为2。 6. 0+1+2+1+2 = 6 这就是最后的逆序数 ///AC代码#include <iostream> #include <set> #include <map> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <bitset> #include <string> #include <vector> #include <iomanip> #include <cstring> #include <algorithm> #include <functional> #define PI acos(-1) #define eps 1e-8 #define inf 0x3f3f3f3f #define debug(x) cout<<"---"<<x<<"---"<<endl typedef long long ll; using namespace std; const int N = 5e5 + 10; struct node { int val; int pos; } p[N]; int n, a[N], c[N]; bool cmp(const node&a, const node& b) { return a.val < b.val; } int lowbit(int x) { return x & (-x); } ///单点修改+区间求和 inline int sum(int x) { int res = 0; while (x) { res += c[x], x -= lowbit(x); } return res; } inline void add(int x) ///单点修改 { while (x <= n) { c[x] += 1, x += lowbit(x); } } inline int query(int x, int y)///区间求和 { return sum(y) - sum(x - 1); } int main() { while (scanf("%d", &n) != EOF && n) { for (int i = 1; i <= n; i++) { scanf("%d", &p[i].val); p[i].pos = i; } sort(p + 1, p + n + 1, cmp); ///排序 for (int i = 1; i <= n; i++) { a[p[i].pos] = i; ///离散化 } ll ans = 0; for (int i = 1; i <= n; i++)///初始化树状数组 { c[i] = 0; } for (int i = 1; i <= n; i++) { add(a[i]); ans += i - sum(a[i]); } printf("%lld\n", ans); } return 0; }