题意  n个人排队  每一个人都有个属性值  依次输入n个pos[i]  val[i]  表示第i个人直接插到当前第pos[i]个人后面  他的属性值为val[i]  要求最后依次输出队中各个人的属性值

从头到尾看的话  队列是动态的   无法操作  可是反过来看时  pos[i]就能够表示第i个人前面有多少个空位了  然后想到了用线段树做就简单了  线段树维护相应区间还有多少个空位  每次把i放到前面刚好有pos[i]个空位的位置即可了  详细看代码

#include <cstdio>
#define lc p<<1, s, mid
#define rc p<<1|1, mid+1, e
#define mid ((s+e)>>1) using namespace std;
const int N = 2e5 + 5;
int tot[N * 4], ans[N];
//tot维护相应区间还能放多少人
int pos[N], val[N];
//pos[i] 保存第i个人进队时前面有多少人
//val[i] 保存第i个人的val void pushup(int p)
{
tot[p] = tot[p << 1] + tot[p << 1 | 1];
} void build(int p, int s, int e)
{
//tot维护相应区间还能放多少人
if(s == e)
{
tot[p] = 1;
return;
}
build(lc);
build(rc);
pushup(p);
} //第i个人插入
void update(int p, int s, int e, int i)
{
if(s == e)
{
tot[p] = 0;
ans[e] = val[i];
return;
}
if(tot[p << 1] > pos[i])
update(lc, i); //左区间的空位足够
else
{
pos[i] -= tot[p << 1];
update(rc, i);
}
pushup(p);
} int main()
{
int n;
while(~scanf("%d", &n))
{
build(1, 1, n);
for(int i = 1; i <= n; ++i)
scanf("%d%d", &pos[i], &val[i]); for(int i = n; i > 0; --i) //倒着更新
update(1, 1, n, i); for(int i = 1; i < n; ++i)
printf("%d ", ans[i]); printf("%d\n", ans[n]);
} return 0;
}
//Last modified : 2015-07-13 11:13

POJ 2828 Buy Tickets(线段树&#183;插队)的更多相关文章

  1. poj 2828 Buy Tickets (线段树(排队插入后输出序列))

    http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissio ...

  2. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  3. poj 2828 Buy Tickets (线段树)

    题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...

  4. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  5. POJ 2828 Buy Tickets | 线段树的喵用

    题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...

  6. POJ 2828 Buy Tickets(线段树单点)

    https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...

  7. poj 2828 Buy Tickets 【买票插队找位置 输出最后的位置序列+线段树】

    题目地址:http://poj.org/problem?id=2828 Sample Input 4 0 77 1 51 1 33 2 69 4 0 20523 1 19243 1 3890 0 31 ...

  8. POJ - 2828 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(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

随机推荐

  1. mysql linux查看配置文件my.cnf位置

    原文:mysql linux查看配置文件my.cnf位置 命令: mysql --help | grep 'Default options' -A 1

  2. Git:与eclipse搭配使用

    Git:与eclipse搭配使用 1)工程初始化为本地库 工程 ——>右键 ——>Team ——Share Project 在该目录下创建了本地库 这里可以设置用户签名 2)Eclipse ...

  3. ZOJ 3640

    很简单的概率题了 设dp[x]为能力值 为x时出去的期望 天数 #include <iostream> #include <cstdio> #include <cmath ...

  4. [React] Understanding setState in componentDidMount to Measure Elements Without Transient UI State

    In this lesson we'll explore using setState to synchronously update in componentDidMount. This allow ...

  5. 为QML创建C++插件(下载)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明出处 https://blog.csdn.net/MatchYang/article/details/54564462 1. 为QML创建C++插件的 ...

  6. yolo环境配置

    主要配置参考官网https://pjreddie.com/darknet/yolo/ 为了能够可视化,另安装cuda+opencv cuda版本为9.0 opencv版本为3.1.0 先安装cuda再 ...

  7. gpg 的使用

    GPG入门教程 GpG使用指南 1. 安装 源码编译安装:源码下载地址 ./configure make make install 直接安装编译好的二进制文件 # Debian / Ubuntu 环境 ...

  8. Scalable, Distributed Systems Using Akka, Spring Boot, DDD, and Java--转

    原文地址:https://dzone.com/articles/scalable-distributed-systems-using-akka-spring-boot-ddd-and-java Whe ...

  9. Python学习之基本概念

    1.Python是一种解释型语言.Python解释器通过“一次执行一条语句”的方式执行程序的. 2.Python用空白来组织程序,不像R等用大括号. 3.# 是Python的注释符号. 4.变量是按引 ...

  10. 如何在Windows下安装Linux子系统(Ubuntu,openSUSU,SUSU Linux Server)

    注意:只有win10才能安装,安装的linux没有图形界面. 1.首先在win10设置 --> 更新与安装 --> 针对开发人员 ,选择开发人员模式. 2.win10 Cortana -- ...