poj 2299 Ultra-QuickSort(归并排序)||(树状数组+离散化)
来源:互联网 发布:js获取所有a标签 编辑:程序博客网 时间:2024/06/03 09:21
Ultra-QuickSort
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.
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
方法一:归并排序
归并排序博主貌似只懂得这一个套路。。。
代码:
#include<stdio.h>#define LL long long int#define maxn 500000+10LL s[maxn],temp[maxn];LL cont;void merge_array(LL a[],int st,int mid,int ed){ int i=st,j=mid+1,k=st; while(i<=mid&&j<=ed) { if(a[i]<=a[j]) temp[k++]=a[i++]; else { cont+=j-k; temp[k++]=a[j++]; } } while(i<=mid) temp[k++]=a[i++]; while(j<=ed) temp[k++]=a[j++]; for(i=st;i<=ed; i++) a[i]=temp[i];}void merge_sort(LL a[],int st,int ed){ if(st<ed) { int mid=(st+ed)>>1; merge_sort(a,st,mid); merge_sort(a,mid+1,ed); merge_array(a,st,mid,ed); }}int main(){ int n; while(~scanf("%d",&n),n) { for(int i=0; i<n; i++) scanf("%d",&s[i]); cont=0; merge_sort(s,0,n-1); printf("%lld\n",cont); } return 0;}
方法二:树状数组
因为正在树状数组入门,所以这里主要说一下树状数组
借鉴大神的说法:
由于999999999这个数字实在是大,数组也开不了这么大,而我们也可以发现最多只有500000个数据,所以我们可以离散化一下
怎么对这个输入的数组进行离散操作?
这里用一个结构体
struct Node
{
int val,pos;
}p[510000];和一个数组a[510000];
其中val就是原输入的值,pos是下标;
然后对结构体按val从小到大排序;
此时,val和结构体的下标就是一个一一对应关系,
而且满足原来的大小关系;
比如 9 1 0 5 4 ——- 离散后a数组
就是 5 2 1 4 3;
离散之后,怎么使用离散后的结果数组来进行树状数组操作,计算出逆序数?
如果数据不是很大, 可以一个个插入到树状数组中,
每插入一个数之前, 统计小于它的数的个数,
对应的大于它的数的个数为 i-1- getsum( num[i] ),
其中 i-1 为前面已经插入的数的个数,
getsum( num[i] )为小于num[i] 的数的个数,
i-1- getsum( num[i] ) 即比 a[i] 大的个数, 即逆序的个数
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define maxn 500010typedef long long LL;struct node{ int x,pos;}q[maxn];int a[maxn],num[maxn];int n;int lowbit(int x){ return x&-x;}bool cmp(node s,node b){ return s.x<b.x;}void add(int i,int x){ while(i<=n) { a[i]+=x; i+=lowbit(i); }}int getsum(int i){ int sum=0; while(i>0) { sum+=a[i]; i-=lowbit(i); } return sum;}int main(){ while(~scanf("%d",&n),n) { memset(a,0,sizeof(a));//注意清零 for(int i=1;i<=n;++i) { scanf("%d",&q[i].x); q[i].pos=i;//记录原始的位置 } sort(q+1,q+n+1,cmp); for(int i=1;i<=n;++i) num[q[i].pos]=i;//离散化 LL ans=0; for(int i=1;i<=n;++i) { ans+=(i-1)-getsum(num[i]);//前面有i-1个数,getsum(num[i])为小于num[i]的数 add(num[i],1);//加入数组中 } printf("%lld\n",ans); } return 0;}
- POJ 2299 Ultra QuickSort <树状数组+离散化 / 归并排序>
- poj 2299 Ultra-QuickSort(归并排序)||(树状数组+离散化)
- POJ 2299 Ultra-QuickSort(归并排序,树状数组,离散化)
- 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(树状数组+离散化 或 归并排序求逆序)
- poj2299 Ultra-QuickSort(归并排序 || 离散化+树状数组)
- 【Linux学习笔记】九、文本编辑器Vim
- MyBatis初识
- LeetCode 461 Hamming Distance
- Linux——死锁
- fullcalendar在dayclick中获取当前点击日期中的events
- poj 2299 Ultra-QuickSort(归并排序)||(树状数组+离散化)
- ACM- Color the ball
- win10下运行.sh文件
- java线程中run和start的区别
- 局部权重线性回归(Locally weighted linear regression)
- CodeForces
- git 使用——关于使用指令hexo d 出现一大串waring以及无法成功部署到github的解决办法
- javascript基本概念的联系与区别
- Doctype作用?DTD是啥?标准模式与兼容模式各有什么区别-