zoukankan      html  css  js  c++  java
  • 线段树 --- (区间维护+逆推)

    Buy Tickets

    Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

     

    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 ≤ iN). 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.

    【题目大意】;有n个人买火车票,期初没有人排队,然后陆续的来人,来一个人就给他分配一个数字,而且会有人插队,要你输出最后的队伍顺序。
    【题目分析】:这是一个经典的逆推问题。需要注意到,最后一个插入的值位置的固定的,也就是最后一个插入的值可以在它想要插入的位置,所以,先保存所以整数对,之后在倒序插入序列中。那到底应该用什么数据结构实现呢?这里我们发现,可以用线段树来解决这一问题,每一个区间添加一个域 num,表示该区间还能放置的位置数,或者说,表示该区间还能放置的最大的序号值(当然,这是相对于最左边的区间而已的,所以,右区间的话,中间还有一步维护操作。
    source code:
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #define MAX 200010
    using namespace std;
    struct Node
    {
        int p,v;
    } node[MAX];
    struct TNode
    {
        int l,r;
        int c;
    } tree[4*MAX];
    int N;
    int  ans[MAX];
    void build(int l,int r,int x)
    {
        tree[x].c=r-l+1;     //该区间的大小,开始写反了,wa了两次
        tree[x].l=l;
        tree[x].r=r;
        if(l==r)        //到底了,返回
            return;
        int mid=(l+r)>>1;
        build(l,mid,2*x);
        build(mid+1,r,2*x+1);
    }
    
    void Insert(int x,int pos,int val)    //下标  位置(position)  价值(value)
    {
        tree[x].c--;     //该区间的空格数减少1
        if(tree[x].l==tree[x].r)       //直到找到了一个合适的空格来存放该位置
        {
            ans[tree[x].l]=val;     //将价值映射到ans中
            return;
        }
        if(tree[2*x].c>=pos)     //该区间的剩余空格数大于位置的坐标,往前探寻
           Insert(2*x,pos,val);
        else                     //该区间剩余空格数小于位置的坐标,往后找空格
            Insert(2*x+1,pos-tree[2*x].c,val);
    }
    
    int main()
    {
        while(~scanf("%d",&N))
        {
            build(1,N,1);
            int i;
            for(i=1;i<=N;i++)
            {
                scanf("%d%d",&node[i].p,&node[i].v);
                node[i].p++;        //输入是从0开始的,加1变为从1开始
            }
            for(i=N;i>=1;i--)     //重点:逆序更新
            {
                Insert(1,node[i].p,node[i].v);
            }
            for(i=1;i<N;i++)
                printf("%d ",ans[i]);
            printf("%d
    ",ans[N]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    3. 安装 glance(控制节点)— OpenStack Queens 三节点部署
    2. 安装 keystone(控制节点)— OpenStack Queens 三节点部署
    1. 环境准备 — OpenStack Queens 三节点部署
    使用kubeadm搭建一个完整的kubernetes集群
    LeetCode–数组中重复的数字
    Java面试题——随机题(一),看看哪些你会哪些你不会
    jQuery DOM操作
    jQuery 选择器
    jQuery--基本语法
    jQuery--简介和使用
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3705444.html
Copyright © 2011-2022 走看看