poj 2828

       题意:有n个人按给出的次序去排队并且会插队,每个人会给pos和id两个数字,pos表示会在哪里插队,id是那个人的编号,要求输出最后队伍的编号序列。

       分析:正向模拟时由于不知道那个位置在将来会有人插队,因此必然会涉及到挪动队伍某个部分的情况,时间复杂度是o(n)的,因此我们可以想到逆向操作,最后一个人插队的位置是确定不变的,前面的位置会根据后面的而变。因此问题就变成了问pos前面是否有足够的空位。例如,pos为0的话,前面至少要有1个空位,注意,要插入的位置必须也是空的,也算进去,pos为1的话,前面至少要有2个空位。

                 用线段树实现插入的操作,每个节点记录的信息为其代表区间里空位的个数。这里要注意,左孩子空位足够时,直接访问左孩子,不够时,需要先执行pos-st[rt<<1],再访问右孩子。

               代码如下:

#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <iomanip>
#include <cmath>
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lrt rt << 1
#define rrt rt << 1|1
#define root 1,n,1
#define BitCount(x) __builtin_popcount(x)
#define BitCountll(x) __builtin_popcountll(x)
#define LeftPos(x) 32 - __builtin_clz(x) - 1
#define LeftPosll(x) 64 - __builtin_clzll(x) - 1
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const double eps = 1e-5;
const int MAXN = 300 + 10;
const int MOD = 1000007;
const double M=1e-8;
const int N=1e6+10;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
const int d[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int n,m,st[N],lazy[N],ans[N];
struct node
{
    int x,y;
};
node a[N];
void build(int l,int r,int rt)
{
    st[rt]=l-r+1;
    if (l==r) return ;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    st[rt]=st[lrt]+st[rrt];
}
void updata(int pos,int val,int l,int r,int rt)
{
    int i,j;
    if (l==r) {
        ans[rt]=val; //cout<<"->"<<rt<<" "<<val<<endl;
        st[rt]--;
        return ;
    }
    int mid=(l+r)>>1;
    if (pos<=st[rt<<1]) updata(pos,val,lson);
    else if (pos>st[rt<<1]) updata(pos-st[rt<<1],val,rson);
    st[rt]=st[rt<<1]+st[rt<<1|1];
}
void print(int l,int r,int rt)
{
    if (l==r) printf("%d ",ans[rt]);
    else {
        int mid=(l+r)>>1;
        print(lson);
        print(rson);
    }
}
int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        build(root);
        //for (i=1;i<2*n;i++) cout<<st[i]<<endl;
        for (i=0;i<n;i++) {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        for (i=n-1;i>=0;i--) {
            updata(a[i].x+1,a[i].y,root);
        }
        print(root);
        puts(" ");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值