[POJ - 2299] Ultra-QuickSort 树状数组

POJ - 2299

Ultra-QuickSort

Time Limit: 7000MS        Memory Limit: 65536K
Total Submissions: 87481        Accepted: 32735

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

题解

观察一下过程就基本明白了
9 1 0 5 4

数组:9 一个元素不需要交换

数组:9 1 ——> 1 9 交换了1次

数组:1 9 0 ——> 0 1 9 交换了2次

数组:0 1 9 5 ——> 0 1 5 9 交换了1次

数组:0 1 5 9 4 ——> 0 1 4 5 9 交换了2次

数组:0 1 4 5 9 排序完成,总次数为1+2+1+2 = 6

就是每次放进来一个元素,然后排序,记录它移动了几次
如果你对冒泡排序的原理理解比较透彻的话应该很容易就能明白,这些移动次数的总和就是冒泡排序交换的总次数。
冒泡排序的时间复杂度为O(n2),如果你暴力统计上述过程的总次数的话时间复杂度也是O(n2),并没有得到优化
我们看这个变化的过程:x < y < z
x x z z z z y ——> x x y z z z z
这个y往前移动了四次,我们遍历数组得到这个答案需要O(n),但是如果我们考虑:加入y前有6个元素,前2个元素x不需要移动,我们只需把后4个元素z后移就行了。这样这个四次就可以算出来了6-2(就是加入最后一个元素前的元素个数-不需要移动的元素个数);

加入最后一个元素前的元素个数已经有了,你加入第i个的时候前面就有i-1个。

现在要算的就是不需要移动的元素个数,如果我们知道了排完后的结果(用快排之类的算法O(nlogn)就能实现),用一个新的数组b[i]表示位置i有无元素。这样你没加入一个元素就把他移动到它该到的位置,并作标记b[i] = 1;这样下次如果你想知道要移动到位置index有几个不需要移动,就只需要统计index前有几个1就行了。这个统计的过程能通过树状数组实现,修改查询复杂度都是O(logn)。
放第i个元素要移动到index的次数就是 i - 1 - getsum(index)

更新就是update(index,1)就行了。

代码实现

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <cmath>
#define endl "\n"
#define LL long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define debug(x); cout<<x<<endl;
#define lowbit(x) ((x)&(-x))
using namespace std;

/***
 * Author Informathons :
 * @author: 六月陌
***/

const int maxn=5e5+10;
const int mod=1e9+7;
struct node
{
    int num; //存数字
    int id;   //存初始位置
}a[maxn];
int c[maxn];  // 树状数组
int Index[maxn];  // 存排序后的位置(相当于离散化)
int n;
LL ans = 0;  //最终结果
bool cmp(node x,node y)
{
    return x.num < y.num;
}

void update(int x,int k)  //更新树状数组,在位置x加k
{
    while(x<=n)
    {
        c[x] += k;
        x += lowbit(x);
    }
}

int getsum(int x)   //查询从头到x的总和,也就是1的个数
{
    int res = 0;
    while(x>0)
    {
        res += c[x];
        x -= lowbit(x);
    }
    return res;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n == 0) break;
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].num);
            a[i].id = i;
        }
        sort(a+1,a+n+1,cmp);  // 快排排个序就有了最终位置
        //↓↓↓ 记录每个元素的初始位置对应的最终位置 ↓↓↓
        Index[a[1].id] = 1;  
        for(int i=2;i<=n;i++)
        {
            if(a[i].num == a[i-1].num) Index[a[i].id] == Index[a[i-1].id];
            else Index[a[i].id] = i;
        }
        ans = 0;
        for(int i=1;i<=n;i++)
        {
            ans += i - 1 - getsum(Index[i]); // 放第i个元素的时候有i-1个,且getsum(Index[i])个元素不用移动 所以就有 i - 1 - getsum(Index[i])个需要移动
            update(Index[i],1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值