poj2299 (reverse request to merge sort)

Topic link: https: //vjudge.net/problem/POJ-2299

Meaning of the questions: Given a sequence, you can only swap two adjacent elements, and asked how many times in order to exchange the sequence in ascending order.

Ideas: seeking essentially reverse right. We seek to reverse a merge sort, this is a simple cdq partition.

#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;

typedef long long LL;
const int maxn=5e5+5;
int n,a[maxn],tmp[maxn];
LL ans;

void merge_sort(int l,int r){
    if(l==r) return;
    int mid=(l+r)>>1;
    int t1=l,t2=mid+1,cnt=l;
    merge_sort(l,mid);
    merge_sort(mid+1,r);
    while(t1<=mid||t2<=r){
        if(t2>r||(t1<=mid&&a[t1]<=a[t2]))
            tmp[cnt++]=a[t1++];
        else{
            ans+=(mid-t1+1);
            tmp[cnt++]=a[t2++];
        }
    }
    for(int i=l;i<=r;++i)
        a[i]=tmp[i];
}

int main(){
    while(scanf("%d",&n),n){
        ans=0;
        for(int i=1;i<=n;++i)
            scanf("%d",&a[i]);
        merge_sort(1,n);
        printf("%lld\n",ans);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11415341.html