POJ 2299 Ultra-QuickSort(树状数组+离散化处理)
来源:互联网 发布:自制手机app软件 编辑:程序博客网 时间:2024/05/17 05:15
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总体思路:
由于a[i]的范围太大,故不可能以a[i]的值为下标创建树状数组,需要进行离散化处理。离散化处理的结果要求和原有数据的次序保持一致,这样我们就可以用离散化处理后的结果来代替原有的数据进行相关计算。离散化处理的主要步骤如下:
struct Node{ int val; int pos;};Node node[MAXN];int reflect[MAXN];sort(node, node + MAXN, cmp)//按实际情况进行处理reflect[node[i].pos] = i;//val存放原数组的元素,pos存放原始位置,即node[i].pos = i。//把这些结构体按照val的大小排序。//reflect数组存放离散化后的值,即reflect[node[i].pos] = i。//这样从头到尾读入reflect数组中的元素,即可以保持原来的大小关系,又可以节省大部分空间。
处理好后reflect数组的次序和原有数据的次序保持一致,因此可以代替原有的数据进行相关计算。
代码如下:
#include<iostream>#include<vector>#include<string>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 500010;int a[MAXN];int n;int lowbit(int t){ return t&(-t);}void update(int t, int d){ while (t <= n) { a[t] += d; t += lowbit(t); }}long long getsum(int t){ long long sum(0); while (t > 0) { sum += a[t]; t -= lowbit(t); } return sum;}struct Node{ int val; int pos;};bool cmp(Node& lhs, Node& rhs){ return lhs.val < rhs.val;}Node node[MAXN];int reflect[MAXN];int main(){#ifdef ONLINE_JUDGE#else freopen("D:\\in.txt", "r", stdin); freopen("D:\\out.txt", "w", stdout);#endif while (scanf("%d", &n) != EOF) { if (n == 0) break; memset(a, 0, sizeof(a)); for (int i = 1; i <=n; i++) { scanf("%d", &node[i].val); node[i].pos = i; } sort(node+1, node + n+1, cmp);//注意排序的范围 for (int i = 1; i <= n; i++) { //reflect[i] = node[i].pos; reflect[node[i].pos] = i;//离散化处理,原有序列的排名 } long long ans(0); //现在总共有j个数,getsum(a[j])为比a[j]小的数的个数加1, //则比a[j]大的数的个数为j-getsum(a[j]); for (int i = 1; i <= n; i++) { update(reflect[i], 1); //ans += (i - getsum(a[i])); //这个地方要注意理解 ans += (i - getsum(reflect[i])); } printf("%lld\n", ans); } return 0;}
0 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 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(树状数组+离散化)
- [极客范]智能腕带的虚假繁荣
- js弹出窗口总结6种弹窗方法
- 淘宝骗子卖家:hutianyou88888
- 【逆向跟踪】OllyDbg的条件断点字符串和文件读写
- 指定的服务未安装 unable to open the service 'tomcat7'
- POJ 2299 Ultra-QuickSort(树状数组+离散化处理)
- ArcGIS for Android 10.2.2 在IDEA安装配置
- 腾讯超预期的净利难掩收入增长乏力
- 冒泡排序
- 【Android学习笔记】Edittext阻止软键盘自动弹出与取消焦点
- spring整合hessain 访问远程服务
- 记一次阿里实习生电话面试
- 快速在CentOS下搭建LAMP(Apache、MySQL、PHP)环境
- 腾讯面试题:海量数据中查找一个数