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

AcWing 260. 买票 (树状数组+二分)打卡

时间:2019-08-09 20:02:21      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:namespace   amp   html   turn   blog   mem   define   fine   一个   

题目:https://www.acwing.com/problem/content/description/262/

题意:给定一个队伍,每个人过来的时候可以插队,每个人会输入一个插入到哪个位置,但是是按顺序的,所以前面的人选的位置有可能会被后面的人插队抢走,然后问最后的排列是多少

思路:仔细想想其实这题就是AcWing 244. 谜一样的牛 ,因为每个人都会选位置,但是只有最后的人选位置不会被抢走,这个时候我们肯定要从前往后,然后前一个人只用考虑后面的人是否抢了他的位置即可

 

 

#include<bits/stdc++.h>
#define maxn 200005
#define mod 1000000007
using namespace std;
typedef long long ll;
ll n,a[maxn],c[maxn],e[maxn],ans[maxn];
ll lowbit(ll x){
    return x&(-x);
}
void add(ll x,ll y){
    while(x<=n){
        c[x]+=y;
        x+=lowbit(x);
    }
}
ll query(ll x){
    ll sum=0;
    while(x){
        sum+=c[x];
        x-=lowbit(x);
    }
    return sum;
}
int main(){
    while(scanf("%lld",&n)!=EOF)
    {
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++){
            add(i,1);
        }
        for(int i=1;i<=n;i++) scanf("%lld%lld",&a[i],&e[i]);
        for(int i=1;i<=n;i++) a[i]++; 
        for(int i=n;i>=1;i--){
            ll l=1,r=n;
            while(l<r){
                ll mid=(l+r)/2;
                if(query(mid)>=a[i]){
                    r=mid;
                } 
                else l=mid+1;
            } 
            ans[r]=e[i];
            add(r,-1);
        }
        for(int i=1;i<=n;i++){
            printf("%lld ",ans[i]);
        }
        printf("\n");
    }
}
/*
4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492

77 33 69 51
31492 20523 3890 19243
*/
 

 

AcWing 260. 买票 (树状数组+二分)打卡

标签:namespace   amp   html   turn   blog   mem   define   fine   一个   

原文地址:https://www.cnblogs.com/Lis-/p/11329025.html

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