【POJ】poj 2828

poj 2828 buy tickets http://poj.org/problem?id=2828 更多线段树膜拜大牛的http://www.notonlysuccess.com/index.php/segment-tree/ #include #define LL(i) (i<<1) #define RR(i) (i<<1|1) #define N 200000 /* 今天做seg_tree专题,看到这题 阅读详情

 

Description

Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…

The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.

It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!

People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

Input

There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.

There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

Sample Input

4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492

Sample Output

77 33 69 51
31492 20523 3890 19243

Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

 

 

 

线段树为题,题目大意:人们一个一个的来排队并插队,按人到来的顺序给出每个人插队的位置(插在第几个人后面),并告知每个人的id号,输出最终队伍的情况。

思路: 建立线段树,每一个节点表示这个区间上的空位置还有多少,将每个人的id和价值输入之后,和每个人的价值,从后往前计算,因为前面的排完队之后后面的还可能插队,导致计算量增大,最后一个人的位置肯定是他的id加一,继续确定倒数第二个人的位置,递推下去就可以得到答案:

代码:

 

/******************************************************
    二叉树问题,节点上的值记录的使这个区间剩余的空
位置,
******************************************************/
#include <iostream>
#include <cstdio>
#include<cstring>
#define MAX 200005
using namespace std;
int tree[MAX << 2];///记录这个区间还有多少空位置
int ans[MAX];///记录价值的顺序
int ticke[MAX];///
int val[MAX];///记录价值
void build ( int p,int l, int r )///建树和普通的二叉树一样
{
    if ( l == r ){ tree[p] = 1; return ; }///注意叶节点只有一个空位
    int mid = ( l + r ) >> 1;
    build ( p * 2, l , mid );
    build ( p * 2 + 1, mid + 1, r );
    tree[p] = tree[ 2 * p ] + tree [ 2 * p + 1 ];
}
void que ( int p , int l, int r, int pos, int val )
{
    tree[p]--;///要在这个区间放入一个值,那么这个区间的空位置就减去一个
    if ( l == r ) { ans[l] = val; return ;}
    int mid = ( l + r ) >> 1;
    if ( pos <= tree[2*p] )///如果左边的空位置少于需要的就放在右边
    {
        que(2*p,l,mid,pos,val);
    }
    else que(2*p+1,mid+1,r,pos-tree[2*p],val);
    //tree[p] = tree[2*p]+tree[2*p+1];
}
int main()
{
    //freopen("in.txt","r",stdin);
    int test;
    while ( cin >> test )
    {
        for ( int i = 0; i < test; i++ )
        {
            scanf( "%d %d",&ticke[i],&val[i]);
        }
        build(1,0,test-1);
        for ( int i = test-1; i >= 0; i-- )///注意要从后往前计算,因为前面的插入之后后面的会影响前面的,后面的位置是确定的
        {
            //cout << tree[i] << endl;
            que( 1, 0, test - 1,ticke[i]+1,val[i]);
        }
        for ( int i = 0; i < test; i++ )
        {
            //cout << tree[i] << endl;
            printf("%d%c",ans[i],i < test-1?' ':'\n');
        }
    }
}


还有一中费时间的做法,就是从后往前方,然后就是放前一个,如果放这个的时候他前面没有插队进入的他的位置就是id+1,如果插入的过的话就是最后插队进来的所以他的id要自增,代码如下:

 

 

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct ticke
{
    int pos;
    int val;
}t[200005];
int vis[200005]={0};
int p[200005];
int test;
int main()
{
    //freopen("in.txt","r",stdin);
    while ( scanf("%d",&test) != EOF )
    {
        for ( int i = 0; i < test; i++ )
        {
            scanf( "%d %d",&t[i].pos,&t[i].val);
        }
        for ( int i = test-1;i >=0; i-- )
        {
            for ( int j = 1; j < test; j++ )
            {
                if ( j <= t[i].pos +1 && vis[j]==1)
                    ///如果j放过,并且j的位置小于i的位置(j是后面插队进入的),那么i的位置就自增
                    t[i].pos++;
                if ( j > t[i].pos + 1) break;
            }
            vis[t[i].pos+1] = 1;///i就放过了
            p[t[i].pos] = t[i].val;///p记录val的正确顺序;
        }
        for ( int i = 0; i < test; i++ )
        {
            printf("%d%c",p[i],i < test-1?' ':'\n');
            vis[i] = 0;
        }
    }

}

 

RK3568—基于GM8775C的MIPI转双通道LVDS屏幕调试 Rockchip RK3568 原生显示接口不支持双通道LVDS屏幕的数据输出,因此需要借助显示转换芯片才能实现双通道LVDS屏幕的驱动。本文介绍使用GM8775C芯片方案,在 RK3568 平台实现单路 MIPI DSI 信号输出来驱动双通道LVDS显示屏。 关于GM8775C GM8775C 型 DSI 转双通道 LVDS 发送器产品主要实现将 MIPI DSI 转单/双通道 LVDS功能,MIPI 支持 1/2/3/4 通道可选,每通道最高支持 1Gbps 速率,最大支持 4Gbps 速率。LV. 阅读详情

相关推荐

VOS3000线路对接、路由配置与路由分析操作教程

通过本教程,你应该已经掌握了VOS3000的线路对接、路由配置和路由分析的基本操作。添加备用路由:为常用前缀添加备用路由,确保在主路由失效时呼叫仍能正常进行。进入线路管理:点击“系统管理” -> “线路管理” -> “添加线路”。查看日志:在“系统日志”中查看路由选择情况,确保路由配置正确。失败原因:查看失败呼叫的原因(如“无应答”、“线路忙”等)。线路选择:选择之前添加的线路(如“供应商A”)。保存并启用:填写完毕后,点击“保存”并启用路由。路由名称:自定义一个名称(如“路由A”)。

AI_wx_3307623172的博客 2972

POJ 2828

POJ 2828 (单点修改 区间查询) 题目链接:POJ 2828 题意: n个人,每个人不断地插入到队伍中。 输入a b表示号码为b的插入到第a个后面。 分析: 需要逆序处理插入队伍的信息。每个区间记录着每个区间能存放的人的数量。逆序插入时,若左子树还能放人,则放到左子树那,否则放到 右子树那。当前区间减1,并且将val的值放入到最终的答案中。 逆序处理的原因:如下面的

小酷Miki的专栏 510

CEH Practical 实战考试真题与答案

CEH Practical 是 EC-Council 推出的Certified Ethical Hacker(CEH)认证项目中的一项高级动手实践考试。它不同于传统的理论考试,侧重于在真实环境中检验考生的实操能力。

sgafdgdagd的博客 968

poj2828

POJ2828 Buy Tickets http://poj.org/problem?id=2828 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue… The Lunar...

北区以北的博客 267

Delta并联机械手-Codesys实现以及正解逆解研究

由于Codesys的Robotics模块有Delta并联机械手的运动模型,因此借助Codesys的机械手运动模型,研究自己的正解逆解算法。

weixin_42386991的博客 5565

PCIE6.0协议学习小记-Flit_Mode

PCIE6.0学习

code_aJack的博客 4638

POJ 2828 BIT

题意 传送门 POJ 2828 题解 最终队列里有 NNN 个人,考虑倒着插入序列,当前处理的人的位置,必然是当前序列的第 posi+1pos_{i}+1posi​+1 个位置;不断求出当前处理人的位置(求解位置一般要 O(logn)O(logn)O(logn) 二分位置再 O(logn) 求前缀和;这里用按区间长度降序枚举的方法 O(logn)O(logn)O(logn) 求前缀和为 xxx 时的位置),将其删除即可。 #include <cstdio> #include <cstrin

neweryyy的博客 235

SAP 更改物料基本计量单位

创建订单单位后,新创建的采购申请、采购合同、采购信息记录均会使用新的订单单位

matinal 當冬夜漸暖 。公众号:matinal 6161

poj2828-Buy Tickets

题目链接http://vjudge.net/problem/POJ-2828 解题思路 好吧。。。感觉跟Lost Cow那道题好像。。。 然后稀里糊涂地过了。。。3500+ms(时限4s(⊙﹏⊙)b) 线段树。。。单点 代码 #include<stdio.h> #include<string.h> #define MAX_SIZE...

anyi4391的博客 172

通信算法之325:无人机云哨协议及发射机接收机仿真算法

【代码】通信算法之325:无人机云哨物理层协议及发射机接收机仿真代码。

leegang12的专栏 362

Allegro格式PCB转换成Pads操作指导

变量:AEX_BIN_ROOT 值:D: MentorGraphics>PADSVX1.2>SDD_HOME>translators>win32bin (值:软件按装的位置,可以是9.5版本,也可以是VX1.0等不同的版本)。变量:HOME 值:C:Users>win7>AppData>Roaming>SPB_Data (这个值是allegro快捷键存放位置器,也可以随意放置在那个盘,我们可以采用默认的路径)。第4步:打开 PADSLayout软件,然后选择导入文件,选择刚刚处理的 allegro文件。

weixin_54787054的博客 3717

POJ2828

一道线段树的好题..

Dizzz的博客 756

STM32实战开发(65):STM32摄像头图像采集开发教程

我们将会详细介绍如何使用STM32进行摄像头控制、图像数据采集以及如何将采集到的图像数据传输到其他设备进行处理。通过这篇博客,您将能够掌握STM32与摄像头模块的图像采集技术,应用于各种图像处理场景。为了确保STM32能够正确接收图像数据,我们需要配置STM32的GPIO引脚,并通过外部中断捕获这些信号。为了实现更高效的图像采集,可以通过DMA进行数据传输,将图像数据直接从GPIO引脚存储到内存中。通过DMA传输,我们可以将采集到的图像数据直接存储在内存中的一个缓冲区,然后进行后续的图像处理。

2201_76125393的博客 2537

poj2828Buy Tickets

链接:http://poj.org/problem?id=2828 题意:给定n个人的信息依次插入队列,pos[i]第i个人插入到pos[i]的位置,val[i]为第i个人的价值。输出插入所有人之后的价值序列。 分析:因为是像插队一样插入队列中,那么越后面的人优先级越高,那么我们就倒着从最后一个人插入,这样的话我们插入位置后就不需要变动了,每次只要找当前这个人插入的位置为第pos[i]个空位即

Fsss 474

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

poj 2828 Buy Tickets

题目链接:http://poj.org/problem?id=2828 这个题目主要是考虑从后往前填,这样就方便操作了。 其中peo[rt]表示以rt为根的左右子树中总共存在的空位数,其实每个元素的pos[i]就可以表示前面有pos[i]个空位,这样就可以进行插入了。 代码

凌风 1236

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

poj2828 线段树

    poj2828      对方法不理解透彻!照搬是没有用的,要彻底理解。一些特殊情况没出什么问题。但一当到大量数据的测试时,WA了!哎,幸亏慢慢找还能找出来,不然我今晚就耗在这睡觉都睡不好了。嘿嘿!     好了,最近这段时间对并查集,线段树,树状数组的学习就先告一段落了。总的来说这段时间的成效还是不错的哦。明天开始我要进入关于搜索的学习,加油吧,孩子。继续学习,继续刷分去吧!呼呼~ ...

weixin_33800593的博客 255

POJ2828 插队

POJ2828 Buy Tickets 题意:人们一个一个的来排队并插队,按人到来的顺序给出每个人插队的位置(插在第几个人后面),并告知每个人的id号,输出最终队伍的情况 分析:从最后来的一个人开始向来得更早的人遍历,这样pos(插在第几个人后面)的意义就变成了,前面有多少个空位。线段树上每个节点中存储的是当前时刻,该区间有多少空位。具体分析了一下第一个样例,如图 #include<cstdi

Daniel 447

Buy Tickets POJ - 2828

Buy Tickets POJ - 2828 Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue… The Lunar New Year was approaching, but unluckily th...

Willen_的博客 387

Poj2828 Buy Tickets(逆序思维+线段树)

POJ2828 题意 nnn个人,给你nnn次操作 第iii次操作表示编号pospospos的人插队到队伍第xxx个人后面了 问最后队伍的编号顺序 乍一看以为是链表裸题,但是发现查找第xxx个人会非常慢 结果题解十分巧妙 倒序考虑,因为最后面插入的人的位置是永远固定的 那么倒序考虑插入到第xxx个人后面说明前面要留刚好xxx个空位 这样我们把空位的值设为111,有人设置为000 那么倒序每次查找第x+1x+1x+1个空位就好了嘛~ 这样直接上线段树也很简单了(权值线段树) 如果去掉头文件等只有20行 #i

jziwjxjd的博客 734

poj 2828

poj 2828从结束状态推向起始状态可以发现,这个过程中,只要给之前的状态留下足够的空位,就可以实现无冲突的放置方案

Dash 396

【POJ 2828】Buy Tickets

【题目链接】 http://poj.org/problem?id=2828 【算法】 离线用线段树维护序列即可 【代码】 #include <algorithm> #include <bitset> #include <cctype> #include...

106

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

题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每个人站入的位置,以及这个人的权值,现在按队列的顺序输出每个人的权值。 解题思路:第K大元素,很巧妙,将人入队的顺序倒过来看,就是纯第K大问题,然后用树状数组还是线段树就都可以做了。 C++ 线段树#include #include #include using namespace std;

不慌不忙、不急不躁 1519

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

题目:poj 2828 Buy Tickets 题意:有n个人排队,每个人有一个价值和要插的位置,然后当要插的位置上有人时所有的人向后移动一位当这个插入到这儿,如果没有直接插进去。 分析:分析发现直接插入移动的话花时间太多,我们可不可以用逆向思维。从后往前来,因为最后一个位置是肯定能确定的,而其他的则插入空的第某个位置。 比如第一组样例: 4 0 77 1 51 1 3

大神养成中..... 1086

poj 2828 分类: poj 2015-0...

poj 2828 从结束状态推向起始状态 可以发现,这个过程中, 只要给之前的状态留下足够的空位, 就可以实现无冲突的放置方案 #include<cstdio> #include<cstdlib> #include<cstring> using namespace std; const int MAXN = ...

aa288288的博客 112

【ICPC-435】poj 2828 Buy Tickets

点击打开poj 2828 思路: 树状数组/线段树单点更新 分析: 1 题目给定n个人的位置pos和id,要我们求出最后n个人的位置 2 我们先来考虑朴素的算法,假设现在进来一个人那么我们把它放到pos的位置,那么pos之后的所有的人都要向后移动一位,那么n个人的话最坏的情况是O(n^2),很显然时间效率上面是不行的 3 由于正向的插入不行,那么我们考虑反向插入的情况(就像逆向的并查集)...

藏经阁 | 玄苦 1663
上一篇: 【POJ】poj 3264 ( 线段树)
下一篇: 【HDU】hdu 2795
三生石1103
博客等级 码龄12年 16粉丝 87原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值