poj 2299 Ultra-QuickSort(树状数组+离散化)
来源:互联网 发布:淘宝摄影相机推荐 编辑:程序博客网 时间:2024/06/07 03:31
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 62342 Accepted: 23200
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
Source
Waterloo local 2005.02.05
思路:其实就是求这些数每个数的逆序数的和。先将这些数据离散化,每一个数对应一个编号是连续的整数。要使这些数升序排序,就要求这些编号的逆序数之和。但树状数组每次查询都是查比它小的,不能做到逆序,所以我们可以将这些数据以降序来排。再用树状数组求的就是他们的逆序和。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX_N 500100using namespace std;int n,tree[MAX_N],sum[MAX_N];struct node{ int x,id;}p[MAX_N];int lowbit(int i){ return i&(-i);}void update(int i,int x){ while(i<=MAX_N) { tree[i]+=x; i+=lowbit(i); }}int query(int n){ int sum=0; while(n>0) { sum+=tree[n]; n-=lowbit(n); } return sum;}bool cmp(node a,node b){ return a.x>b.x;}int main(){ while(~scanf("%d",&n)&&n) { memset(tree,0,sizeof(tree)); memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++) { scanf("%d",&p[i].x); p[i].id=i; } sort(p+1,p+1+n,cmp); long long sum=0; for(int i=1;i<=n;i++) { update(p[i].id,1); sum+=query(p[i].id)-1; } printf("%lld\n",sum); } 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(树状数组+离散化)
- Python之什么是dict
- MySQL服务器的远程链接设置
- HDU6025 Coprime Sequence (前缀后缀处理)
- 利用CRgn判断点是否在图形内部和图形是否在图形内部
- 使用IntelliJ IDEA 配置Maven(入门)
- poj 2299 Ultra-QuickSort(树状数组+离散化)
- Unity读写XML文件
- 区间dp小练
- SpringCloud(二):服务发现与服务注册
- HBase Coprocessor编程案例
- Servlet处理Form表单
- js转换时间戳与日期字符串
- Springboot+React+antd开发问题记(一)
- 快速傅里叶变换(FFT)的原理、实现及代码解析(附C#源码)