Ultra-QuickSort(逆序数)







 

                                                                                                                                               Ultra-QuickSort

Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 62759 Accepted: 23385

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

题意是求一组数的逆序数,首先搞清楚逆序数是什么,即一组数中逆序对的个数,如 3,4,5,1中逆序对为(3,1),(4,1),所以逆序数为2。

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    const int N=1001000;
    long long c[N],reflect[N], n;
    struct node
    {
        long long val;
        int pos;
    }nd[N];
    bool cmp(node p1,node p2)
    {
        if(p1.val!=p2.val)
            return p1.val<p2.val;
        else return p1.pos<p2.pos;
    }
    long long lowbit(long long x)
    {
        return x&(-x);
    }
   void updata(int x)
    {
        while(x<=n)
        {
            c[x]+=1;
            x+=lowbit(x);
        }
    }
    int getsum(int x)
    {
        int ans=0;
        while(x>0)
        {
            ans+=c[x];
            x-=lowbit(x);
        }
        return ans;
    }
    int main()
    {
           while(~scanf("%d",&n)&&n){
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&nd[i].val);
                nd[i].pos=i;
            }
            sort(nd+1,nd+n+1,cmp);

            for(int i=1;i<=n;i++) c[i]=0;
            long long ans=0;
            for(int i=1;i<=n;i++)
            {
                updata(nd[i].pos);
                ans+=i-getsum(nd[i].pos);
            }
            printf("%lld\n",ans);
        }
  }


但是由上面我们知道c[]中放的是输入变量,此题数值较大,直接开数组肯定不行,但n的值较小,于是我们把它离散化。
假如现在有一些数:1234 98756 123456 99999 56782,由于1234是第一小的数,所以reflct[1]=1;依此,有reflect[5]=2,reflect[2]=3,reflect[4]=4,reflect[3]=5;这 样转化后并不影响原来数据的相对大小关系,且把c[]范围缩小了。
于是得到代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=500005;
int c[N],reflect[N], n;
struct node
{
    int val;
    int pos;
}nd[N];
bool cmp(node p1,node p2)
{
    return p1.val<p2.val;
}
int lowbit(int x)
{
    return x&(-x);
}
int updata(int x)
{
    while(x<=n)
    {
        c[x]+=1;
        x+=lowbit(x);
    }
}
int getsum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&nd[i].val);
            nd[i].pos=i;
        }
        sort(nd+1,nd+n+1,cmp);
        for(int i=1;i<=n;i++)
        reflect[nd[i].pos]=i;
        for(int i=1;i<=n;i++) c[i]=0;
        long long ans=0;
        for(int i=1;i<=n;i++)
        {
            updata(nd[i].pos);
            ans+=i-getsum(nd[i].pos);
        }
        printf("%lld\n",ans);
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值