首页 > 代码库 > POJ 2299 Ultra-QuickSort

POJ 2299 Ultra-QuickSort

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



可以用归并排序,也可以用树状数组!

下面提供两个代码!

AC代码如下:


归并排序!!

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
long long a[500005],b[500005];
long long sum;

void GB(ll* A,int x,int y ,ll* T)
{
    int i;
    if(y-x>1)
    {
        int m=x+(y-x)/2;
        int l=x,r=m,t=x;
        GB(A,x,m,T);
        GB(A,m,y,T);
        while(l<m||r<y)
        {
            if(r>=y||(l<m&&A[l]<=A[r]))
                T[t++]=A[l++];
            else
            {
                T[t++]=A[r++];
                sum+=m-l;
            }
        }
        for(i=x;i<y;i++)
            A[i]=T[i];
    }
}

int main()
{
    int n;
    int i,j;
    while(~scanf("%d",&n)&&n)
    {
        memset(b,0,sizeof b);
        for(i=0;i<n;i++)
            scanf("%lld",&a[i]);
        sum=0;
        GB(a,0,n,b);
        //for(i=0;i<n;i++)
            //cout<<a[i]<<" ";
        //cout<<endl;
        cout<<sum<<endl;
    }
    return 0;
}

树状数组!!

#include<iostream>
#include<cstdio>
#include<cstring>
#define M 1000100
using namespace std;

long long c[M],num[M];
int l[M],n;

long long lowbit(long long a)
{
    return a&-a;
}

void add(long long a,int b)
{
    while (a<M)
    {
        c[a]+=b;
        a+=lowbit (a);
    }
}

long long sum(long long a)
{
    long long ans=0;
    while(a>0)
    {
        ans+=c[a];
        a-=lowbit(a);
    }
    return ans;
}

int main ()
{
    int i,j;
    int a,b;
    while(~scanf("%d",&n)&&n)
    {
        memset(c,0,sizeof c);
        memset(num,0,sizeof num);
        int tt=0;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&num[i]);
        }
        long long ans=0;
        for(i=n;i>=1;i--)
        {
            if(num[i]==0)
                {l[i]=0;tt++;}
            else
            {l[i]=sum(num[i]-1)+tt;
            add(num[i],1);}
            ans=ans+(long long)l[i];
        }
        printf("%lld\n",ans);
    }
    return 0;
}