poj 2299

Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 40427 Accepted: 14559

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

Waterloo local 2005.02.05

这个题目是要求逆序数的,不是求最少几次交换使得数组有序。

怎么求逆序数呢,有两个nlog(n)的方法。

方法一:比如有个序列 9 5 2 4 6,从大到小排序,大小相同时按下标大的排在前面。

           然后初始化一个数组 0 0 0 0 0 则最大的9对应的下标为1,则在相应的数组上加1,变为 1 0 0 0 0当前数组前的数加起来就是逆序。此时逆序为0。

         依次类推数组变为1 0 0 0 1,逆序为0+1;数组变为1 1 0 0 1,逆序为0+1+1;数组变为1 1 0 1 1,逆序为0+1+1+2;数组变为1 1 1 1 1 ,逆序为0+1+1+2+2;所以最后逆序为6。然后在求前n项和的时候有树状数组,使时间降为log(n)。

方法二:归并排序(此方法不再赘述,具体参看我的博客中转载的一篇如何求数组的逆序)

AC代码,树状数组做的:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct Node{
    long long num;
    int index;
}a[500010];
int b[500010];
int n;
int cmp(Node t1,Node t2){
    if(t1.num==t2.num)
        return t1.index>t2.index;
    return t1.num>t2.num;
}
int lowbit(int x){
    return x&-x;
}
int sum(int x){
    int s=0;
    while(x){
        s+=b[x];
        x-=lowbit(x);
    }
    return s;
}
void add(int x){
    while(x<=n){
        b[x]++;
        x+=lowbit(x);
    }
}
int main(){
    while(cin>>n,n){
        long long result=0;
        for(int i=1;i<=n;i++){
            cin>>a[i].num;
            a[i].index=i;
        }
        sort(a+1,a+1+n,cmp);
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++){
            add(a[i].index);
            result+=sum(a[i].index);
        }
        cout<<result-n<<endl;
    }
    return 0;
} 

AC代码,归并排序做的:

#include<iostream>
#include<algorithm>
using namespace std;
long long a[500010];
long long tmp[500010];
long long result;
void merge_sort(int x,int y){
    if(x>=y)
        return ;
    int mid=(x+y)>>1;
    merge_sort(x,mid);
    merge_sort(mid+1,y);
    int i=x;
    int j=mid+1;
    int t=1;
    while(i<=mid && j<=y){
        if(a[i]<=a[j]){
            tmp[t++]=a[i++];
        }
        else{
            tmp[t++]=a[j++];
            result+=mid-i+1;
        }
    }
    while(i<=mid)
        tmp[t++]=a[i++];
    while(j<=y)
        tmp[t++]=a[j++];
    int tt=x;
    for(int k=1;k<t;k++){
        a[tt++]=tmp[k];
    }
}
int main(){
    int n;
    while(cin>>n,n){
        result=0;
        for(int i=0;i<n;i++)
            cin>>a[i];
        merge_sort(0,n-1);
        //for(int i=0;i<n;i++)
            //cout<<a[i]<<' ';
        cout<<result<<endl;
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值