题目链接: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的更多相关文章

  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(排队问题,线段树应用)

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

  3. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

  4. poj 2828 Buy Tickets 树状数组

    Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...

  5. poj 2828 Buy Tickets (线段树 单节点 查询位置更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15533   Accepted: 7759 Desc ...

  6. poj 2828 Buy Tickets 【线段树点更新】

    题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位 ...

  7. 线段树(单点更新) POJ 2828 Buy tickets

    题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...

  8. Buy Tickets POJ - 2828 思维+线段树

    Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...

  9. H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分

    H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...

随机推荐

  1. 剖析ASP.NET Core(Part 2)- AddMvc(译)

    原文:https://www.stevejgordon.co.uk/asp-net-core-mvc-anatomy-addmvccore发布于:2017年3月环境:ASP.NET Core 1.1 ...

  2. [转]sqlserver2008锁表语句详解

    本文转自:http://xue.uplook.cn/database/sqlserver/801760.html 锁定数据库的一个表 代码如下: SELECT * FROM table WITH (H ...

  3. 各种软核处理器二进制文件FPGA初始化文件生成程序

    不管是MIPS, Nios II, MicroBlaze, MSP430, 8051, OpenRISC, OpenSPARC, LEON2/LEON3等等软核处理器,在FPGA上实现的时候我们通常需 ...

  4. DX12

    CD3DX12_DESCRIPTOR_RANGE1 的baseShaderRegister 用来指定 t0 t1 b0 b1...的index t0 srv b0 constant buffer u0 ...

  5. 《jQuery基础》总结

    目前,互联网上最好的jQuery入门教材,是Rebecca Murphey写的<jQuery基础>(jQuery Fundamentals).这本书虽然是入门教材,但也足足有100多页.我 ...

  6. Geeks - Check whether a given graph is Bipartite or not 二分图检查

    检查一个图是否是二分图的算法 使用的是宽度搜索: 1 初始化一个颜色记录数组 2 利用queue宽度遍历图 3 从随意源点出发.染色0. 或1 4 遍历这点的邻接点.假设没有染色就染色与这个源点相反的 ...

  7. 转: Android中的签名机制

    转载请注明出处:http://www.blogjava.net/zh-weir/archive/2011/07/19/354663.html Android APK 签名比对 发布过Android应用 ...

  8. [Javascript] Automate the process of flattening deeply nested arrays using ES2019's flat method

    Among the features introduced to the language of JavaScript in ES2019 is Array.prototype.flat. In th ...

  9. vue - config(dev.env.js和prov.env.js)

    描述:配置产品模式.打包模式:开发还是打包,以最佳运行(不配置则有一个大大的Warning!!!) 官网:https://www.webpackjs.com/concepts/mode/

  10. windows下流媒体nginx-rmtp-module服务器搭建及java程序调用fmpeg将rtsp转rtmp直播流【转】

    https://github.com/illuspas/nginx-rtmp-win32 http://bashell.sinaapp.com/archives/build-nginx-rtmp-mo ...