poj Buy Tickets
题目链接:http://poj.org/problem?id=2828
类似的题目:http://www.cnblogs.com/lovychen/p/3674048.html
测试数据:
in:
4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492
out:
77 33 69 51
31492 20523 3890 19243
题目分析:直接倒着进行插入,然后按照从前向后寻找空位进行插入:
【题解】:
线段树节点中保存这一段中的空位数,然后倒序对pos插入:
例如: 0 77
1 51
1 33
2 69
先取: 2 69 —— —— —69— —— (需要前面有3个空位才能插入)
然后取: 1 33 —— —33— —69— —— (需要前面有2个空位才能插入)
然后取: 1 51 —— —33— —69— —51— (需要前面有2个空位才能插入) 前面只有1个空位 故插入后面空格
然后取: 0 77 —77— —33— —69— —51— (需要前面有1个空位才能插入)
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<cmath>
using namespace std;
const int N = ;
int T,tot;
int pre[N],vis[N];
struct TT
{
int x,y;
}a[N];
int lowbit(int x)
{
return x&(-x);
}
void init()
{
for(int i=;i<=T;i++)
vis[i] =lowbit(i);
}
int sum(int x)
{
int ans = ;
while(x>)
{
ans = ans+vis[x];
x = x-lowbit(x);
}
return ans;
}
int update(int x)
{
while(x<=T)
{
vis[x] = vis[x] - ;
x = x +lowbit(x);
}
}
void add(int x,int y)
{
int l=,r=T,mid;
while(l<r)
{
//printf("l === %d %d %d\n",l,r,mid);
mid = (l+r)/;
if(sum(mid)>=x) r = mid;
else l = mid+;
}
//printf("l = %d\n",l);
update(l);
pre[l] = y;
}
int main()
{
int aa,bb;
while(~scanf("%d",&T))
{
init();
for(int i=;i<=T;i++)
{
scanf("%d %d",&aa,&bb);
a[i].x = aa+;
a[i].y = bb;
}
for(int i=T;i>;i--)
add(a[i].x,a[i].y);
for(int i=;i<=T;i++)
{
if(i==) printf("%d",pre[i]);
else printf(" %d",pre[i]);
}
printf("\n");
}
return ;
}
poj Buy Tickets的更多相关文章
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- POJ 2828 Buy Tickets(排队问题,线段树应用)
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意: 排队买票时候插队. 给出一些数对,分别代表某个人的想要插入的位 ...
- poj 2828 Buy Tickets(树状数组 | 线段树)
题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...
- poj 2828 Buy Tickets 树状数组
Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...
- poj 2828 Buy Tickets (线段树 单节点 查询位置更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 15533 Accepted: 7759 Desc ...
- poj 2828 Buy Tickets 【线段树点更新】
题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位 ...
- 线段树(单点更新) POJ 2828 Buy tickets
题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...
- Buy Tickets POJ - 2828 思维+线段树
Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...
- H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分
H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...
随机推荐
- Unity3d通用工具类之解压缩文件
今天,我们来写写c#是如何通过代码解压缩文件的. 在游戏的项目中呢,常常我们需要运用到解压缩的技术.比如,当游戏需要更新的时候,我们会从服务器中下载更新的压缩文件包. 这时候我们就需要解压文件,然后覆 ...
- openresty记录响应body乱码问题
问题背景 最近新上了一个功能,openresty通过syslog记录请求日志,然后由logstash推送至ES.测试上线时未发现这个问题,在日常查看日志的过程中,发现logstash推送有错误日志,错 ...
- [java] 模拟QPS
在WEB服务器端,每日的访问量巨大.在非生产环境需要对服务器进行压力测试,一般使用后台线程和Sleep方式来模拟线上的压力.这里使用ScheduledExecutorService实现一种简单的QPS ...
- ylbtech-LanguageSamples-OperatorOverLoading(运算符重载)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-OperatorOverLoading(运算符重载) 1.A,示例(Sample) 返回 ...
- ylbtech-LanguageSamples-Indexers_2(索引器)
ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Indexers_2(索引器) 1.A,示例(Sample) 返回顶部 Indexers ...
- Struts基本概念
内容源自: Struts2基本概念 一.struts2体系结构: 1.Web浏览器请求一个资源.2.过滤器Dispatcher查找方法,确定适当的Action.3.拦截器自动对请求应用通用功能,如验证 ...
- NHibernate中几个集合的选择
NHibernate是从Hibernate移植过来的基于NET平台的一个ORM框架,同时跟这框架一起的还有一个开源库,叫做Iesi.Collections,这个库扩展了NET平台下面的几个集合,所谓集 ...
- poj 2236 Wireless Network 【并查集】
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 16832 Accepted: 706 ...
- unicode 编码在线转换工具
字符串 unideo的16进制值
- poj 1274 The Perfact Stall
click here ~~ ***The Perfect Stall*** Description Farmer John completed his new barn just last week, ...