poj 2299 Ultra-QuickSort (树状数组解法)
来源:互联网 发布:房地产大数据 编辑:程序博客网 时间:2024/06/10 04:26
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 32746 Accepted: 11691
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
ps:刚学了下树状数组 所以这题用树状数组做的 不过这题正规方法应该用归并排序做的
各位亲们 想了解 树状数组的可以看一下 不想的就直接pass哈
#include<cstdio>#include <cstring>#include <algorithm>#define maxn 500005using namespace std;int n;__int64 ans;int a[maxn];struct Tnode{ int num; int val;}node[maxn];int lowbit(int v){ return v&(-v);}void update(int v){ for(int i=v; i<=maxn-5; i+=lowbit(i)) { a[i]++; }}int getsum(int v){ int sum=0; for(int i=v; i>0; i-=lowbit(i)) { sum+=a[i]; } return sum;}bool cmp1(const Tnode &x,const Tnode &y){ return x.val<y.val;}bool cmp2(const Tnode &x,const Tnode &y){ return x.num<y.num;}int main(){ int i,preval,temp; while(scanf("%d",&n),n) { for(i=1; i<=n; i++) { scanf("%d",&node[i].val); node[i].num=i; } sort(node+1,node+n+1,cmp1); preval=-1; for(i=1; i<=n; i++) // 此循环用来离散化 a[i] 将其范围缩小到maxn内 { // 注意这里不能简单地 node[i].val=i; temp=node[i].val; if(node[i].val==preval) { node[i].val=node[i-1].val; } else node[i].val=i; preval=temp; } sort(node+1,node+n+1,cmp2); memset(a,0,sizeof(a)); ans=0; for(i=1; i<=n; i++) { ans+=getsum(maxn-5)-getsum(node[i].val); update(node[i].val); } printf("%I64d\n",ans); // 坑爹 原来逆序数可以很大很大 要用__int64保存 不然会WA } return 0;}
- 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(树状数组)
- 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 (树状数组)
- error C2440: “初始化”: 无法从“char *”转换为“int”
- lazyload 错误 :未定义的变量:checkbrowse
- tomcat配置说明
- Notes about Porting suspend-to-disk function to android
- UIView的drawRect方法
- poj 2299 Ultra-QuickSort (树状数组解法)
- 【386w.com】一键启动电脑应用程序不再是梦想
- java传集合到oracle的存储过程中
- 关于java List的一个问题解决 - the type list is not generic
- regex.h - 正则表达式
- File类的简单使用
- MYSQL常用操作命令
- scanf易错点解析
- listview中图片的异步加载,双缓存