poj 2299 Ultra-QuickSort(归并排序)||(树状数组+离散化)

115 篇文章 0 订阅
11 篇文章 0 订阅

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+10
LL 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 500010
typedef 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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值