poj 2299 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

Source

线段树 
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct ele
{
    __int64 x;
    __int64 num;
}a[502211];
__int64 c[502211];
bool cmp(ele x,ele y)
{
   return x.x<y.x;
}
struct Ele
{
    int left;
    int right;
    __int64 sum;
}p[2501122];
void build(int x,int y,int step)
{
    p[step].left=x;
    p[step].right=y;
    p[step].sum=0;
    if(p[step].right==p[step].left)
        return;
    else
    {
        int Mid=(p[step].right+p[step].left)/2;
        build(x,Mid,2*step);
        build(Mid+1,y,2*step+1);
    }
}
void update(int x,int step)
{
    if(p[step].right==p[step].left)
        p[step].sum=1;
    else
    {
        int Mid=(p[step].right+p[step].left)/2;
        if(x<=Mid)
            update(x,2*step);
        else
            update(x,2*step+1);
        p[step].sum=p[step*2].sum+p[step*2+1].sum;
    }
}
__int64 get(int x,int y,int step)
{
   if(p[step].right==y&&p[step].left==x)
        return p[step].sum;
    else
     {
         int Mid=(p[step].right+p[step].left)/2;
         if(y<=Mid)
            return get(x,y,2*step);
        else
        if(x>=Mid+1)
            return get(x,y,2*step+1);
        else
            return get(x,Mid,2*step)+get(Mid+1,y,2*step+1);
     }


}
int main()
{
    int n;
    int i;
    while(scanf("%d",&n),n)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%I64d",&c[i]);
            a[i].num=i;
            a[i].x=c[i];
        }
        sort(a+1,a+n+1,cmp);
        int num=1;
        __int64 temp=a[1].x;
        for(i=1;i<=n;i++)
        {
            if(a[i].x!=temp)
            {
                temp=a[i].x;
                num++;
            }
            c[a[i].num]=num;
        }
        build(1,num,1);
        __int64 sum=0;
        for(i=1;i<=n;i++)
        {
            if(c[i]+1<=num)
            {
                sum+=get(c[i]+1,num,1);
                //printf("%d\n",get(c[i]));
            }

            update(c[i],1);
        }
        printf("%I64d\n",sum);
    }
    return 0;
}
归并 排序  交换次数等于逆序数
#include<stdio.h>
int a[501122];
int b[501122];
int c[501122];
__int64 sum;
void Merge(int x,int y,int z)
{
    int i,j,k;
    for(i=x;i<=y;i++)
        b[i]=a[i];
    for(i=y+1;i<=z;i++)
        c[i]=a[i];
    i=x;
    j=y+1;
    k=x;
    while(i<=y&&j<=z)
    {
        if(b[i]<c[j])
        {
            a[k]=b[i];
            i++;
            k++;
        }
        else
        {
            sum+=(y-i+1);
            a[k]=c[j];
            j++;
            k++;
        }
    }
    while(i<=y)
    {
       a[k]=b[i];
       i++;
       k++;
    }
    while(j<=z)
    {
        a[k]=c[j];
        k++;
        j++;
    }
}
void Sort(int x,int y)
{
   if(x<y)
    {
       Sort(x,(x+y)/2);
       Sort((x+y)/2+1,y);
       Merge(x,(x+y)/2,y);
    }
}
int main()
{
    int n;
    int i;
    int Begin,End;
    while(scanf("%d",&n),n)
    {
        sum=0;
        for(i=1;i<=n;i++)
            scanf("%d",&a[i]);
        Begin=1;
        End=n;
        Sort(Begin,End);
        printf("%I64d\n",sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值