poj 2299 Ultra-QuickSort(线段树/树状数组/归并 求逆序对)

Problem: 2299		User: shu_dayang

Memory: 7380K		Time: 500MS

Language: C++		Result: Accepted

Source Code
//树状数组 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> typedef long long LL; #define MAXN 500005 #define MID(l,r) ((l + r) >> 1) using namespace std; int c[MAXN],aa[MAXN]; int n; struct Node { int val; int order; }a[MAXN]; bool cmp(Node a, Node b) { return a.val < b.val; } int lowbit(int x) { return x &(-x); } void update(int x) { while(x <= n) { c[x] += 1; x += lowbit(x); } } int query(int x) { int sum = 0; while(x > 0) { sum += c[x]; x -= lowbit(x); } return sum; } int main() { LL ans; while(~scanf("%d",&n) && n != 0) { memset(c,0,sizeof(c)); for(int i = 1; i <= n; i++) { scanf("%d",&a[i].val); a[i].order = i; } sort(a+1,a+1+n,cmp); for(int i = 1; i <= n; i++) aa[a[i].order] = i; ans = 0; for(int i = 1; i <= n; i++) { update(aa[i]); ans += i - query(aa[i]); } printf("%I64d\n",ans); } return 0; }

 线段树:

Problem: 2299		User: shu_dayang

Memory: 9540K		Time: 922MS

Language: C++		Result: Accepted

//线段树

#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

typedef long long LL;

#define MAXN  500005

#define MID(l,r) ((l + r) >> 1)

#define lson l,m,rt<<1

#define rson m+1,r,rt<<1|1

using namespace std;



int sum[MAXN << 2],aa[MAXN];

int n;



struct Node

{

    int val;

    int order;

}a[MAXN];



bool cmp(Node a, Node b)

{

    return a.val < b.val;

}



void PushUp(int rt)

{

    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];

}



void Build(int l,int r ,int rt)

{

    sum[rt] = 0;

    if(l == r)

    {

        return;

    }

    int m = MID(l,r);

    Build(lson);

    Build(rson);

}



void Update(int p,int l,int r,int rt)

{

    if(l == r)

    {

        sum[rt]++;

        return;

    }

    int m = MID(l,r);

    if(p <= m)

        Update(p,lson);

    else

        Update(p,rson);

    PushUp(rt);

}



int Query(int L,int R,int l,int r,int rt)

{

    if(L<=l&&r <=R)

    {

        return sum[rt];

    }

    int res = 0;

    int m = MID(l,r);

    if(L<=m)

        res += Query(L,R,lson);

    if(R>m)

        res+=Query(L,R,rson);

    return res;

}



int main()

{

    LL ans;

    while(~scanf("%d",&n) && n != 0)

    {

        Build(1,n,1);

        for(int i = 1; i <= n; i++)

        {

            scanf("%d",&a[i].val);

            a[i].order = i;

        }

        sort(a+1,a+1+n,cmp);

        for(int i = 1; i <= n; i++)

            aa[a[i].order] = i;



        ans = 0;

        for(int i = 1; i <= n; i++)

        {

            Update(aa[i],1,n,1);

            ans += i - Query(1,aa[i],1,n,1);

        }

        printf("%I64d\n",ans);

    }

    return 0;

}

 归并:

Problem: 2299		User: shu_dayang

Memory: 3712K		Time: 1360MS

Language: C++		Result: Accepted

//归并

#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

#define MID(l,r) ((l+r) >> 1)

typedef long long LL;

#define MAXN  500005

using namespace std;



int num[MAXN];

LL ans;



void Merge(int low,int mid,int high)

{

    int *temp = new int[high - low + 1];

    int k = 0;

    int i = low,j = mid + 1;

    while(i <= mid && j <= high)

    {

        if(num[i] > num[j])

        {

            ans += mid - i + 1;

            temp[k++] = num[j++];

        }

        else

        {

            temp[k++] = num[i++];

        }

    }

    while(i<=mid)    temp[k++] = num[i++];

    while(j <= high)   temp[k++] = num[j++];



    for(int i = 0; i< k; i++)

        num[low+i] = temp[i];

    delete []temp;

}



void MergeSort(int low,int high)

{

    if(low < high)

    {

        int m = MID(low,high);

        MergeSort(low,m);

        MergeSort(m+1,high);

        Merge(low,m,high);

    }

}



int main()

{

    int n;

    while(~scanf("%d",&n) && n != 0)

    {

        ans = 0;

        for(int i = 0; i< n; i++)

            scanf("%d",&num[i]);

        MergeSort(0,n-1);

        printf("%I64d\n",ans);

    }

    return 0;

}

 

你可能感兴趣的:(Quicksort)