POJ 2828 Buy Tickets

Thinking: 

 About this question, we have to think reversely. We first get all the input and arrange the sequence from the last guy who enters the queue. Because the last guy will not be effected by previous ones. For example, if the N th guy jumped to position[N], then he should be the position[N]+1 guy at last. What about N-1 th guy? Suppose all the N positions are empty at the beginning and no one takes them, he will find the position[N--1]+1 th empty place to stand.(You can draw a picture.) It is the same to N-2,N-3...

Thus we can use segment tree to store how many empty space there are in the interval.


AC code:

 

#include<iostream>
#include<cstring>
#define maxn 200005
using namespace std;

int space[maxn * 3];
int N;
int final_arrange[maxn];
int posi[maxn], vale[maxn];
void build(int rt, int l,int r)
{
	space[rt] = r - l + 1;
	if (l == r)
		return;
	int mid = (l + r) >> 1;
	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
}

void fix(int rt, int l,int r, int pos, int val)
{
	space[rt]--;
	if (l == r)
	{
		final_arrange[l] = val;
		return;
	}
	int mid = (l + r )>> 1;

	if (pos <= space[rt<<1])
	{
		fix(rt << 1, l, mid, pos, val);
	}
	else fix(rt << 1 | 1, mid+1, r, pos - space[rt<<1], val);
}

int main()
{
	while (~scanf("%d", &N))
	{
		build(1, 1, N);
		for (int i = 1; i <= N; i++)
			scanf("%d%d", &posi[i], &vale[i]);
		for (int i = N; i >= 1; i--)
		{
			fix(1,1,N, posi[i]+1, vale[i]);
		}
		for (int i = 1; i <= N-1; i++)
		{
			printf("%d ", final_arrange[i]);
		}
		printf("%d\n", final_arrange[N]);


	}

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值