Ultra-QuickSort(树状数组 + 离散化)
来源:互联网 发布:窗帘 知乎 编辑:程序博客网 时间:2024/06/05 16:52
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
解题思路:
题目大意是给一个数列,相邻两个进行交换,使之按从小到大排序,问最少交换几次。该题和之前的做的Janan是一个类型的,都是求逆序对。唯一难点就是数据特别大,数列中的元素值可以达到999999999,树状数组不可能开这么大。但由于数列最多有500000个数,所以可以进行离散化处理,把数列中的元素压缩到1-500000之间。离散化就是将输入的值与下标相对应,可以用结构体实现,然后对输入的值进行从小到大排序,再用一个数组去存储其下标的值。答案会超int范围,得用Int64存储。
AC代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 500005;__int64 c[maxn];struct node{ int a, b; // a存储输入的值,b存储其坐标}p[maxn];bool cmp(node v, node s){ return v.a < s.a;}int lowbit(int a){ return a & (-a);}void Update(int a){ while(a < maxn) { c[a] += 1; a += lowbit(a); }}__int64 Sum(int a){ __int64 sum = 0; while(a > 0) { sum += c[a]; a -= lowbit(a); } return sum;}int main(){ int n, a[maxn]; __int64 ans; while(scanf("%d", &n) && n) { ans = 0; memset(c, 0, sizeof(c)); for(int i = 1; i <= n; i++) { scanf("%d", &p[i].a); p[i].b = i; } sort(p + 1, p + n + 1, cmp); for(int i = 1; i <= n; i++) // 离散化处理 a[p[i].b] = i; for(int i = 1; i <= n; i++) { ans += i - Sum(a[i]) - 1; // 要-1,因为算的是输入该值之前的元素个数 Update(a[i]); } printf("%I64d\n", ans); } return 0;}
0 0
- Ultra-QuickSort(树状数组 + 离散化)
- Ultra-QuickSort 离散化+树状数组
- 树状数组的离散化 Ultra-QuickSort
- [树状数组][离散化]Ultra-QuickSort
- Ultra-QuickSort (树状数组离散化)
- poj2299 Ultra-QuickSort (树状数组+离散化)
- POJ Ultra-QuickSort (树状数组+离散化)
- POJ2299 Ultra-QuickSort 树状数组+离散化
- poj2299 Ultra-QuickSort 树状数组 + 离散化
- POJ2299-Ultra-QuickSort(树状数组+离散化)
- POJ-2299 Ultra-QuickSort(树状数组)(离散化)
- poj 2299 Ultra-QuickSort(离散化+树状数组)
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
- POJ 2299 Ultra-QuickSort(树状数组+离散化处理)
- Ultra-QuickSort 学习归并排序(树状数组+离散化)
- POJ 2299-Ultra-QuickSort (树状数组+离散化)
- POJ 2299 Ultra-QuickSort (离散化+树状数组)
- Ultra-QuickSort(离散化+树状数组求逆序数)
- com.genuitec.runtime.generic.jee60 is not defined
- apache 403错误 显示如下: You don't have permission to access / on this server. 解决办法
- 临界区,互斥量,信号量,事件的区别(线程同步)
- placeholder和json两种实现登录隐藏的方法
- freemaker的语法问题
- Ultra-QuickSort(树状数组 + 离散化)
- 文件工具
- CAS单点登陆实践
- latex多幅图片排列 2*2示例
- 哈希表
- 求每年最高气温
- 指针和数组——(个人笔记)
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- 学生成绩管理系统