HDU-5775

题意:给出一个数列,数字不会互相重复,求每个数字在冒泡排序时所到达的最右点到最左点的距离。


思路:思考一下会现其实这题就是在问你每个数字的右边有多少个比它小的数字。可以用线段树或者树状数组来解决,遍历一遍数列,每个数字都进行一次插入和查询操作。以线段树为例:

插入:将num[i]+1~n这个区间插入到线段树中,使这个区间中的节点的值加一。

查询:对于num[i]来说,num[i]~num[i]就是要查找的区间。

查询得到的值v就是这个数的左边比这个数小的数字的数量,因为数字不会重复且为1~n,所以右边的数量就是num[i]-1-v。


线段树:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;

struct node{
    int v,p;
    bool operator < (const node &a) const{
        return v<a.v;
    }
};

node value[100010];
node tree[400010];

int ABS(int a){
    return a>0?a:(a*-1);
}

void MakeTree(int now,int l,int r,int left,int right){
    if(left > right) return;
    if(left == l && right == r){
        tree[now].p += 1;
        return;
    }
    int middle = (l+r)/2;
    if(tree[now].p != 0){
        tree[now*2].p += tree[now].p;
        tree[now*2+1].p += tree[now].p;
        tree[now].p = 0;
    }
    if(left<=middle) MakeTree(now*2,l,middle,left,right<middle?right:middle);
    if(right>middle) MakeTree(now*2+1,middle+1,r,left>middle+1?left:middle+1,right);
    tree[now].v = tree[now*2].v+tree[now*2+1].v;
}

int Find(int now,int l,int r,int v){
    if(l == r) return tree[now].v+tree[now].p;
    int middle = (l+r)/2;
    if(v<=middle) return Find(now*2,l,middle,v)+tree[now].p;
    else return Find(now*2+1,middle+1,r,v)+tree[now].p;
}

int main(){
    int T,t,n,i,temp;
    scanf("%d",&t);
    T = t;
    while(t--){
        scanf("%d",&n);
        memset(tree,0,sizeof(tree));
        for(i=1;i<=n;i++){
            scanf("%d",&value[i].v);
            MakeTree(1,1,n,value[i].v+1,n);
            if(value[i].v<=i) value[i].p = ABS(value[i].v-i)+(value[i].v-1-Find(1,1,n,value[i].v));
            else value[i].p = (value[i].v-1-Find(1,1,n,value[i].v));
        }
        sort(value+1,value+n+1);
        cout<<"Case #"<<T-t<<": ";
        for(i=1;i<n;i++) printf("%d ",value[i].p);
        printf("%d\n",value[i].p);
    }
    return 0;
}



树状数组:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;

struct node{
    int v,p;
    bool operator < (const node &a) const{
        return v<a.v;
    }
};

int n;
node value[100010];
int tree[100010];

int ABS(int a){
    return a>0?a:(-a);
}

void Add(int a){
    while(a <= n){
        tree[a]++;
        a += a&(-a);
    }
}

int Find(int a){
    int sum = 0;
    while(a != 0){
        sum += tree[a];
        a -= a&(-a);
    }
    return sum;
}

int main(){
    int T,t,i;
    scanf("%d",&t);
    T = t;
    while(t--){
        scanf("%d",&n);
        memset(tree,0,sizeof(tree));
        for(i=1;i<=n;i++){
            scanf("%d",&value[i].v);
            Add(value[i].v+1);
            if(value[i].v<=i) value[i].p = ABS(value[i].v-i)+(value[i].v-1-Find(value[i].v));
            else value[i].p = (value[i].v-1-Find(value[i].v));
        }
        sort(value+1,value+n+1);
        cout<<"Case #"<<T-t<<": ";
        for(i=1;i<n;i++) printf("%d ",value[i].p);
        printf("%d\n",value[i].p);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值