Buy Tickets (插队问题)
发布日期:2021-07-14 01:03:46 浏览次数:46 分类:技术文章

本文共 3355 字,大约阅读时间需要 11 分钟。

传送门

描述

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.

输入

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.

输出

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.

样例

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

思路

  • 题意:pos[i]表示这个人现在要插入的位置,如果原来pos[i]有人,那么这个人就往pos[i]+1摞,如果pos[i]+1有人,那么原来pos[i]+1的人就往后摞……,每个人带有一个权值val[i],给出每个人想要插入的位置,依次输出最后每个人的权值
  • 逆向思考一波,最后一个人的位置一定是确定的,然后我们从后往前去安排每个人的位置,当安排pos[i]时如果pos[i]有人,那么他自己就往后面摞,直到有位置可以放为止
  • 线段树维护区间内能存放的空位数,对于要存放的pos,如果pos<=tree[d<<1],那么左子树区间一定可以放下它,否则就把pos-tree[d<<1]往右子树放(这里的pos和pos-tree[d<<1]分别对应区间的左数第pos、pos-tree[d<<1]个位置,最后节点的位置才是真正存放的位置)
  • 不需要结构体,存放区间空位数即可

Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include
#include
#include
using namespace std; #define LL long long #define INIT(a,b) memset(a,b,sizeof(a)) int pos[200007],val[200007]; int tree[800007]; int ans[200007]; int n,tem; void build(int l,int r,int d){
tree[d]=r-l+1; if(l==r)return; int mid=(l+r)/2; build(l,mid,d<<1); build(mid+1,r,d<<1|1); } int update(int l,int r,int p,int d){
tree[d]--; if(l==r) return l; int mid=(l+r)/2; if(p<=tree[d<<1]) return update(l,mid,p,d<<1); //位置小于左区间能存放的个数时就一定能在左边存下 else return update(mid+1,r,p-tree[d<<1],d<<1|1); } int main(){
while(~scanf("%d",&n)){
INIT(tree,0); for(int i=0;i
=0;i--){
tem=update(1,n,pos[i]+1,1); ans[tem]=val[i]; } for(int i=1;i<=n;i++) printf("%d%c",ans[i]," \n"[i==n]); } return 0; }

转载地址:https://blog.csdn.net/cpongo3/article/details/89334303 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:VS2017OpenGL的配置
下一篇:无题II(二分+匈牙利)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月03日 14时40分31秒