Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

Posted 微弱的世界

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对相关的知识,希望对你有一定的参考价值。

Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 52306   Accepted: 19194

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

Source

 
题意:给定n个数,只能交换相邻的两个元素,至少交换几次,成为递增序列。
题解:
明显要求序列的逆序对数目。。。
对于样例:
5
9 1 0 5 4
我们将其排序:
0 1 4 5 9
在每个位置上初始放为1.
1 1 1 1 1
然后,从原序列开始遍历。
先到9,我们把其排好序的位置拿出,即为5。
然后统计位置5之前有多少1。
1 1 1 1 1
————  > 4个  ans+=4
然后把5号位置放为0。
1 1 1 1 0
继续操作即可。。。
这个树状数组维护即可。。。
注意开long long和原序列排序后要去重。。。
技术分享
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define MAXN 500010
 4 #define LL long long
 5 int n,BIT[MAXN],a[MAXN],aa[MAXN];
 6 int read()
 7 {
 8     int s=0,fh=1;char ch=getchar();
 9     while(ch<0||ch>9){if(ch==-)fh=-1;ch=getchar();}
10     while(ch>=0&&ch<=9){s=s*10+(ch-0);ch=getchar();}
11     return s*fh;
12 }
13 int Lowbit(int o){return o&(-o);}
14 void Update(int o,int o1)
15 {
16     while(o<=n)
17     {
18         BIT[o]+=o1;
19         o+=Lowbit(o);
20     }
21 }
22 int Sum(int o)
23 {
24     int sum=0;
25     while(o>0)
26     {
27         sum+=BIT[o];
28         o-=Lowbit(o);
29     }
30     return sum;
31 }
32 int main()
33 {
34     int tot,i,wz;
35     LL ans;
36     while(1)
37     {
38         n=read();if(n==0)break;
39         for(i=1;i<=n;i++){a[i]=read();aa[i]=a[i];}
40         memset(BIT,0,sizeof(BIT));
41         sort(a+1,a+n+1);
42         tot=unique(a+1,a+n+1)-(a+1);
43         for(i=1;i<=tot;i++)Update(i,1);
44         ans=0;
45         for(i=1;i<=n;i++)
46         {
47             wz=lower_bound(a+1,a+tot+1,aa[i])-a;
48             ans+=(LL)Sum(wz-1);
49             if(BIT[wz]!=0)Update(wz,-1);
50         }
51         printf("%lld\n",ans);
52     }
53     fclose(stdin);
54     fclose(stdout);
55     return 0;
56 }
View Code

 

 

以上是关于Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对的主要内容,如果未能解决你的问题,请参考以下文章

POJ——T 2299 Ultra-QuickSort

POJ 2299.Ultra-QuickSort

poj2299(归并排序求逆序对)

POJ 2299 Ultra-QuickSort

POJ2299 Ultra-QuickSort

poj2299 Ultra-QuickSort