其他分享
首页 > 其他分享> > poj2828--Buy Tickets

poj2828--Buy Tickets

作者:互联网

题目描述

Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…

The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

题目大意

有N个人排队,每一个人都有一个val来对应,每一个后来人都会插入当前队伍的某一个位置pos。要求把队伍最后的状态输出。

解法

其实想到正解其实很简单,因为前面的每一个人都可能受到后面的人的插入而改变,那么我们可以确定这个序列之中,我们最先确定一定是最后的人,那么我们就解决了第一个问题。

但是我们还是不清楚这个线段树是要怎么建。但是仔细一想,如果我们要确定当前这个人的位置,那么我们必须要知道他前面一共有多少空格,这就是我们需要维护的东西。

来看一张图片,来自著名的hzwer大大的博客(p≧w≦q)!!!

这张图片解释了样例1,那么我们来详细说明一下。

再重申一下我们要维护的是区间内的空出来的位置。如果当前这个左区间内可以放入我们的这个人,那么我们就把这个人这个区间内,往下递归,不行那么放到有区间内,其实也就是通过当前这个区间的空格的个数来确定这个人是否可以放到哪个地方。这样不存在问题,是因为越后面的越不会受到影响,那么后面的都排好了,那么这个人就不会受到影响。然后每一次处理完一个人,那么就将这个区间的空格减少一个。

非常完美的做法。

ac代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<ctype.h>
#define N 200005
#define lson nod<<1
#define rson nod<<1|1
using namespace std;
struct Segment_tree{
    int tree[N<<2],ans[N];
    void init(){memset(tree,-1,sizeof(tree));}
    void pushup(int nod){tree[nod]=tree[lson]+tree[rson];}//pushup函数
    void build(int l,int r,int nod){//构建各个人前空格的线段树
        if(l==r){tree[nod]=1;return;}
        int mid=l+r>>1;
        build(l,mid,lson); build(mid+1,r,rson);
        pushup(nod);
    }
    void update(int l,int r,int k,int v,int nod){//单点更新
        if(l==r){ans[l]=v;tree[nod]=0;return;}//走到根节点就把这个人放到这个位置
        int mid=l+r>>1;
        if(tree[lson]>=k) update(l,mid,k,v,lson);//如果左边足够,那么就放到左边
        else update(mid+1,r,k-tree[lson],v,rson);//左边不够,放到右边去
        pushup(nod);//更新维护线段树
    }
}T;
int r(){
    int w=0,x=0;char ch=0;
    while(!isdigit(ch))w|=ch=='-',ch=getchar();
    while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return w?-x:x;
}
int x[N],v[N],n;
int main(){
    while(~scanf("%d",&n)){
        T.init(); T.build(1,n,1);
        for(int i=1;i<=n;i++) x[i]=r(),v[i]=r(),x[i]++;//说明当前人需要排到当前这个序列的x[i]的位置
        for(int i=n;i>=1;i--) T.update(1,n,x[i],v[i],1);
        for(int i=1;i<=n;i++) printf("%d ",T.ans[i]);
        puts("");
    }
    return 0;
}

标签:Tickets,queue,Buy,int,mid,poj2828,nod,ch,lson
来源: https://www.cnblogs.com/chhokmah/p/10386255.html