题目:http://poj.org/problem?id=2828

这题可以倒序来做,因为越靠后的人实际上优先级越高;

用0和1表示这个位置上是否已经有人,0表示有,1表示没有,这样树状数组维护前缀和表示这个位置前面有多少个空位置;

每插入一个人,找到前面空位置恰好是他要求的个数的那个位置,就是他最终站的位置(若位置不空则表示后面的人之后插队到他前面了,所以他被挤到后面去);

找到位置后把该位置的值赋成0,表示这里也站了人,倒着处理即可。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int const MAXN=;
int n,val[MAXN],f[MAXN],x[MAXN],pos[MAXN];
int query(int x)
{
int s=;
for(;x;x-=(x&-x))
s+=f[x];
return s;
}
void add(int x,int w)
{
for(;x<=n;x+=(x&-x))
f[x]+=w;
}
int main()
{
while(scanf("%d",&n)==)
{
memset(pos,,sizeof pos);
for(int i=;i<=n;i++)add(i,);
for(int i=;i<=n;i++)
scanf("%d%d",&x[i],&val[i]);
for(int i=n;i;i--)
{
int l=,r=n,p;
while(l<=r)
{
int mid=((l+r)>>);
if(query(mid)>=x[i]+)p=mid,r=mid-;
else l=mid+;
}
pos[p]=i;
add(p,-);
}
for(int i=;i<=n;i++)
printf("%d ",val[pos[i]]);
printf("\n");
}
return ;
}

poj2828 Buy Tickets——倒序处理的更多相关文章

  1. [POJ2828] Buy Tickets(待续)

    [POJ2828] Buy Tickets(待续) 题目大意:多组测试,每组给出\(n\)条信息\((a,b)\),表示\(b\)前面有\(a\)个人,顺序靠后的信息优先级高 Solution.1 由 ...

  2. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

  3. poj-----(2828)Buy Tickets(线段树单点更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 Desc ...

  4. POJ2828 Buy Tickets 【线段树】+【单点更新】+【逆序】

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12296   Accepted: 6071 Desc ...

  5. poj2828 Buy Tickets (线段树 插队问题)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22097   Accepted: 10834 Des ...

  6. poj-2828 Buy Tickets(经典线段树)

    /* Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 10207 Accepted: 4919 Descr ...

  7. POJ2828 Buy Tickets [树状数组,二分答案]

    题目传送门 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22611   Accepted: 110 ...

  8. POJ2828 Buy Tickets 树状数组

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  9. poj-2828 Buy Tickets(线段树,排队问题,逆向思维)

    题目地址:POJ 2828 Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Yea ...

随机推荐

  1. mySql 主从复制linux配置

    总结: 主库(192.168.1.251): /etc/my.cnf.d/server.cnf [mysqld] log-bin=mysql-bin server-id=1 从库(192.168.1. ...

  2. [Algorithms] Tree Data Structure in JavaScript

    In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...

  3. 解决Gradle执行命令时报Could not determine the dependencies of task &#39;:compileReleaseJava&#39;.

    Could not determine the dependencies of task ':compileReleaseJava'. > failed to find target andro ...

  4. angular - 使用es6等一些功能

    app.module.ts var model = { user: 'Admin', items: [{ action: 'buy flowsers', done: false },{ action: ...

  5. 合并SO为单独交货单

    本场景为单步交货     为客户建立专用的route.     增加一个pull rule         在做订单的时候,为订单行选择 上面建立好的route,     连续建立了 2个 订单 SO ...

  6. ubuntu下调试ffmpeg程序出现undefined reference to pthread_once ,undefined reference to uncompress错误

    Ubuntu(版本16.04)下默认配置编译Ffmpeg(版本4.1.3configure 添加选项--enable-threads),将编译好的ffmpeg库添加到程序 中进行编译出现undefin ...

  7. 「零秒思考」是个神话,不过这款笔记术你值得拥有zz

    今天读完了赤羽雄二的<零秒思考>,作者是一位在麦肯锡公司工作了 14 年的资深顾问.依照作者的说法,「零秒思考」指的是: 瞬间便能认清现状, 瞬间便能整理问题, 瞬间便能考虑出解决办法, ...

  8. MySQL-获取某天的数据

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 近7天 DAY) <= date(时间字段名) 近30天 DAY) & ...

  9. 热烈庆祝UE4完全免费Free---GitHub的关联方式

    热烈庆祝UE4完全免费Free---GitHub的关联方式 时间:2015-03-03 18:38:52      阅读:3007      评论:0      收藏:0      [点我收藏+] 标 ...

  10. Scala随记

    使用Scala首先确保本地Java 8版本,然后按照官网所说,比较流行的方式(1) sbt; (2) IDE "The most popular way to get Scala is ei ...