POJ 2299 Ultra-QuickSort (树状数组)

题目原文:http://poj.org/problem?id=2299

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.

解题思路:本题要求求出逆序对的个数,因数据量较大所以采用树状数组的方法。先将所有数据离散化,然后出现一个数字,则在对应的数字位置加一。然后对每个数字统计在他前面有多少个小于等于他的数,也就可以的得到有多少个比他大的数了。

本题还有另一种做法,是使用归并排序,归并排序过程种每次合并时都可以快速计算出一组逆序对。

AC代码:

树状数组做法

/*
    @Author: wchhlbt
    @Date:   2017/3/1
*/
//#include <bits/stdc++.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <limits>
#include <climits>
#include <cstdio>


#define Fori(x) for(int i=0;i<x;i++)
#define Forj(x) for(int j=0;j<x;j++)
#define maxn 500005
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<double, double> P;

const double eps =1e-8;
const int mod = 1000000007;
const double PI = acos(-1.0);

int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
//BIT
int bit[maxn];
int n;
int sum(int i)
{
    int 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 a[maxn];
int b[maxn];
void solve(){
    ll ans = 0;
    memset(bit,0,sizeof bit);
    for(int i = 0; i<n; i++){
        ans += i - sum(a[i]);
        add(a[i],1);
    }
    cout << ans << endl;
}
int main()
{
    //cin>>n;
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(4);
    while(cin>>n && n){
        for(int i = 0; i<n; i++){
            cin>>a[i];
            b[i] = a[i];
        }
        sort(b,b+n);
        int size = unique(b,b+n)-b;
        for(int i = 0; i<n; i++)
            a[i] = lower_bound(b,b+size,a[i]) - b + 1;
        solve();
    }
    return 0;
}

归并排序做法

#include <iostream>

using namespace std;
#define maxn 500005
typedef long long ll;
int n;
int a[maxn],tmp[maxn];
ll ans;
void Merge(int l,int m,int r)
{
    int i = l;
    int j = m + 1;
    int k = l;
    while(i <= m && j <= r)
    {
        if(a[i] > a[j])
        {
            tmp[k++] = a[j++];
            ans += m - i + 1;
            //ans++;
        }
        else
        {
            tmp[k++] = a[i++];
        }
    }
    while(i <= m) tmp[k++] = a[i++];
    while(j <= r) tmp[k++] = a[j++];
    for(int i=l;i<=r;i++)
        a[i] = tmp[i];
}

void Merge_sort(int l,int r)
{
    if(l < r)
    {
        int m = (l + r) >> 1;
        Merge_sort(l,m);
        Merge_sort(m+1,r);
        Merge(l,m,r);
    }
}

int main()
{
    while(cin>>n && n)
    {
        ans = 0;
        for(int i = 0;i<n;i++)
        {
            cin>>a[i];
        }
        Merge_sort(0,n-1);
        cout << ans << endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值