POJ 2828 Buy Tickets(线段树)

题意是不断有人在插队,然后问你最后排列是什么样子。

思路很重要,想了很久还是没想出来,瞄了一眼题解看到个从后往前。想了一会顿时明白了。。因为首先最后一个人是肯定能得到他所要的位置的,倒数第二个人要占的位置假设是第2个是除去最后一人占的位置也就是说空位的第二个,然后以此类推,归纳来说就是当前人最后占的位置是所剩下空位的第(他要占的位置)个。

写起来很好写。

AC代码 :

//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<string.h>
#include<string>
#include<bitset>
using namespace std;
#define ll __int64
#define eps 1e-8
#define NMAX 200005
#define MOD 10007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}

int T[4*NMAX][2],pos,nct,n,a[NMAX][2],ans[NMAX];
void build(int l, int r, int rt)
{
    if(l == r)
    {
        T[rt][0] = 1;
        T[rt][1] = nct++;
        return;
    }
    int mid = (l+r)>>1;
    build(lson);
    build(rson);
    T[rt][0] = T[rt<<1][0]+T[rt<<1|1][0];
}

void findit(int x, int l, int r, int rt)
{
    if(l == r)
    {
        T[rt][0] = 0;
        pos = rt;
        return;
    }
    int mid = (l+r)>>1;
    if(T[rt<<1][0] >= x) findit(x,lson);
    else findit(x-T[rt<<1][0],rson);
    T[rt][0] = T[rt<<1][0]+T[rt<<1|1][0];
}

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o4.txt","w",stdout);
#endif // GLQ
    while(~scanf("%d",&n))
    {
        nct = 1;
        build(1,n,1);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d",&a[i][0],&a[i][1]);
            a[i][0]++;
        }
        for(int i = n-1; i >= 0; i--)
        {
            findit(a[i][0],1,n,1);
            int t = pos;
//            cout<<a[i][0]<<" "<<t<<" "<<T[t][1]<<endl;
            ans[T[t][1]] = a[i][1];
        }
        for(int i = 1; i <= n; i++) printf("%d%c",ans[i],(i==n)?'\n':' ');
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值