POJ 2828 Buy Tickets(线段树·插队)】的更多相关文章

题意  n个人排队  每一个人都有个属性值  依次输入n个pos[i]  val[i]  表示第i个人直接插到当前第pos[i]个人后面  他的属性值为val[i]  要求最后依次输出队中各个人的属性值 从头到尾看的话  队列是动态的   无法操作  可是反过来看时  pos[i]就能够表示第i个人前面有多少个空位了  然后想到了用线段树做就简单了  线段树维护相应区间还有多少个空位  每次把i放到前面刚好有pos[i]个空位的位置即可了  详细看代码 #include <cstdio> #d…
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 10478   Accepted: 5079 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a lo…
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容易就确定了,那最后第二个人的位置也可以推(与最后一个人的位置无关)...依次就都可以确定所有的人了. 用前缀和的思想,要是这个人的位置确定了,那么就标记这个人位置的值为0,然后回溯更新,跟求逆序对个数的思想比较类似. 线段树: #include <iostream> #include <cs…
题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了,点的更新.. 思想是 从后向前插入,用num存储 每一段剩余的位置. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <algorit…
Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 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 wa…
题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以用线段树维护一下每个区间剩余多少位置可选 对于一个pos 如果左儿子的剩余超过当前位置,就递归进左子树 反之就相当于留出了左儿子剩余的位置,递归进右子树,当前位置变成pos-左儿子剩余位置 请注意是在后面插入 #include<cstdio> #include<algorithm> #…
https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的数就往后推一位.最后输出每个位置的ans. 思路:根据题 目可知,最后插入的位置的数才是最终不变的数,所以可以从最后的输入作第1个放入,依此类推,倒插入.在插入时也有一定的技术,首先创建一棵空线段树时,每个节点记录当前范围内有多少个空位置.在插入时,要注意,一个数放入之后那么这个位置就不用管了,那么…
题目地址: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 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 d…
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 ha…
POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位置Pos_i和他的Val_i,求出最后的队列的val顺序. 分析:  也是一道非常巧妙的题目.  刚開始天真的以为sort一下即可了.wa了一发后发现我错了...  原来能够非常巧妙的用线段树做.因为某个人想要插入posi位置,插入后他就在posi位置上了,可是可能其它人会插到他前面来,他的位置就会…
题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入队的顺序倒过来看,就是纯第K大问题,然后用树状数组还是线段树就都能够做了. C++ 线段树 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int ma…
题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位置上有人时全部的人向后移动一位当这个插入到这儿,假设没有直接插进去. 分析:分析发现直接插入移动的话花时间太多.我们可不能够用逆向思维. 从后往前来.由于最后一个位置是肯定能确定的,而其它的则插入空的第某个位置. 比方第一组例子: 4 0 77 1 51 1 33 2 69 開始时候位置都为空 编号0 1 2…
题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt << 1 #define rson m+1, r, rt << 1 | 1 + ; int pos[MAX_N], val[MAX_N]; ]; int que[MAX_N]; int id; void build(int l, int r, int rt) { sum[rt] = r…
题链: http://poj.org/problem?id=2828 题解: 线段树. 逆向考虑这个过程.最后的序列S共有n个元素. 先看最后一个人,如果他插入到第i位,那么他最终的位置就是当前序列S的第i号位置,然后把这个位置去掉,得到新序列S'. 再按上面的操作确定倒数第二个人的位置: 如果他插入到第j位,那么他就在新序列S'的第j个位置. ......以此类推,可以确定出所有人的位置. 可以用线段树去确定位置. 代码: #include<cstdio> #include<cstri…
题目链接: 传送门 Buy Tickets Time Limit: 4000MS     Memory Limit: 65536K 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 t…
http://poj.org/problem?id=2828 题意 排队买票,依次给出当前人要插队的位置,每个人有个编号,然后问你最后整个的序列是什么? 分析 最后一个人的要插入的位置是确定的,所以逆序遍历,线段树结点存储的是当前区域的空位置数量.我们就可以倒着来插,最后一个固定后,如果倒数第二个插入的位置小于当前的空格数,那么就往前插到序号上,否则往后插,往后的话序号需要减去当前这个数左边的空位数.因为左右都是从0位置开始标记的,因此结构体里需要维持节点左右边的空位个数,(当前插队序号小于左边…
Buy Tickets Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2795 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…
题目链接 题意:有N个人排队,给出各个人想插队的位置和标识,要求输出最后的序列. 分析:因为之前的序列会因为插队而变化,如果直接算时间复杂度很高,所以可以用 线段树逆序插入,把序列都插到最后一层,len记录该区间里还剩余多少位置,插入的时候就插到剩余的第几个位置, 比如1,2已经插入了,如果再想插入第3个位置,则实际上插入的是5. 因为是逆序的,所以在3之前除了现在的1,2 还有之前的1,2. #include <iostream> #include <cstdio> #inclu…
Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16273   Accepted: 8098 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 wa…
Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15533   Accepted: 7759 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 wa…
题目大意 一些小朋友在排队,每次来一个人,第i个人会插到第x个人的后面.权值为y.保证x∈[0,i-1]. 按照最后的队伍顺序,依次输出每个人的权值. 解题分析 好气吖.本来是在做splay练习,然后发现这道题用splay写T掉了,可能是我的splay常数太大了吧.要不要考虑去学一下自顶向下建树的splay,据说会快一点. 可以倒着考虑问题.如果倒着安排小朋友的队伍的话,就不用考虑插队的问题了.如果第i个人插到了第x个人的后面. 用线段树写的话,记录一下空格的数量,每次找个第x+1个空格的位置插…
Buy Tickets   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 the…
题目链接 有N次操作,每次都是将第i个数放置在第pos个数的后面,并且这个数的值是val. 这个线段树的思维确实很好,我们可以发现,后面放进去的数,一定是强制位置的,而前面放的数,会随着后面的数进入而改变位置,所以我们可以尝试着先放后面的数,再处理前面的数,最后一个数一定是放在(pos[N] + 1)这个位置上的,而再后面的数呢,如果本来是放在pos[N]前面的,可能还是在pos[N]的前面,跟什么有关呢?可以多举几组例子,不难发现,假如这个数原先要放在第pos(i)的位置后面,现在呢在pos(…
[poj2828]Buy Tickets 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…
Buy Tickets 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…
Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22097   Accepted: 10834 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 w…
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 ha…
线段树 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, h…
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 ha…
Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16607   Accepted: 8275 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 wa…