码迷,mamicode.com
首页 > 编程语言 > 详细

Ultra-QuickSort (poj 2299 归并排序 || 树状数组 求逆序对)

时间:2015-04-01 09:37:44      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:ultra-quicksort   poj 2299   归并排序求逆序对   

Language:
Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 45751   Accepted: 16615

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


题意:求n个数的逆序对。

代码:

归并排序:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 501005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

ll a[maxn];
ll b[maxn];
ll n,ans;

void Merge(ll a[],ll l,ll mid,ll r)
{
    int i,j,k=l;
//    int *b=new int[r+1];  //不要动态申请,会超时的
    FRE(i,l,r) b[i]=a[i];
    i=l;j=mid+1;
    while (i<=mid&&j<=r)
    {
        if (b[i]<=b[j])
            a[k++]=b[i++];
        else
        {
            a[k++]=b[j++];
            ans+=(mid-i+1);
        }
    }
    while (i<=mid) a[k++]=b[i++];
    while (j<=r) a[k++]=b[j++];
//    delete []b;
}

void Merge_sort(ll a[],ll l,ll r)
{
    if (l>=r) return ;
    ll mid=(l+r)>>1;
    Merge_sort(a,l,mid);
    Merge_sort(a,mid+1,r);
    Merge(a,l,mid,r);
}

int main()
{
    int i,j;
    while (scanf("%lld",&n),n)
    {
        ans=0;
        FRE(i,1,n)
            scanf("%lld",&a[i]);
        Merge_sort(a,1,n);
        pf("%lld\n",ans);
    }
    return 0;
}


树状数组:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 501005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef __int64 ll;
using namespace std;

struct Node
{
    int val;
    int pos;
}node[maxn];

int n;
ll ans;
int bit[maxn];

int cmp(Node a,Node b)
{
    return a.val>b.val;
}

ll sum(int i)
{
    ll s=0;
    while (i>0)
    {
        s+=bit[i];
        i-=i&-i;
    }
    return s;
}

void add(int i,int x)
{
    while (i<=n)
    {
        bit[i]+=x;
        i+=i&-i;
    }
}

int main()
{
    int i,j;
    while (scanf("%d",&n),n)
    {
        FRE(i,0,n+10) bit[i]=0;
        FRE(i,1,n)
        {
            scanf("%d",&node[i].val);
            node[i].val;
            node[i].pos=i;
        }
        sort(node+1,node+n+1,cmp);
        ans=0;
        FRE(i,1,n)
        {
            ans+=sum(node[i].pos-1);
            add(node[i].pos,1);
        }
        pf("%I64d\n",ans);
    }
    return 0;
}



Ultra-QuickSort (poj 2299 归并排序 || 树状数组 求逆序对)

标签:ultra-quicksort   poj 2299   归并排序求逆序对   

原文地址:http://blog.csdn.net/u014422052/article/details/44787845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!