poj2828--Buy Tickets

题意 :总共n个人,一个一个的来排队,每个人都有一个要求,要求排到第几个人后面(当然肯定是最后面来的人的要求先满足),每个人有一个对应的val,按顺序输出n的人的val。

用线段树来维护区间剩余的位置数量,,当然必须从最后一个人向前来更新线段树,每次更新之后就把该位置的剩余数量置为0(因为后面的人的要求肯定最先满足嘛)

代码如下:

#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5+10;
int seg[maxn<<2],ans[maxn],Pos[maxn],val[maxn],n;
void build(int l,int r,int pos)
{
    if (l == r)
    {
        seg[pos] = 1;
        return;
    }
    int mid = (l + r) >> 1;
    build(l,mid,pos<<1);
    build(mid+1,r,pos<<1|1);
    seg[pos] = seg[pos<<1] + seg[pos<<1|1];
}
int update(int l,int r,int pos,int x)
{
    if (l == r)
    {
        seg[pos] = 0;
        return l;
    }
    int mid = (l + r) >> 1;
    int ans;
    if (seg[pos<<1] >= x)
        ans = update(l,mid,pos<<1,x);
    else
        ans = update(mid+1,r,pos<<1|1,x-seg[pos<<1]);
    seg[pos] = seg[pos<<1|1] + seg[pos<<1];
    return ans;
}
int main(void)
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif
    while (~scanf ("%d",&n))
	{
	  for (int i = 0; i < n; i++)
      {
          scanf ("%d%d",Pos+i,val+i);
      }

      build(1,n,1);
      for (int i = n - 1; i >= 0; i--)
      {
          int tmp = update(1,n,1,Pos[i]+1);
          ans[tmp-1] = val[i];
      }
      printf("%d",ans[0]);
      for (int i = 1; i < n; i++)
      {
          printf(" %d",ans[i]);
      }
      printf("\n");
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/oneshot/p/3982721.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值