zoukankan      html  css  js  c++  java
  • HDU-- Buy Tickets

    告知每次要插到第 i 个位置上,问最后它们的顺序是什么。

    这一题,不是考线段树,是考如何想出用线段树...思维很巧妙,倒过来做的话就能确定此人所在的位置....

     

    线段树每个结点有一个remain,记录些线段还有多少个空位....开始时,叶结点的remain当然为1

                                         Buy Tickets

    Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 10   Accepted Submission(s) : 5
    Problem 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
    #include<stdio.h>
    #define maxn 500004 
    int a[maxn];
    int b[maxn];
    int c[maxn];
    struct node  
    {  
        int left,right;  
        int remain;  
    };  
    
    node tree[3*maxn];  
    void build(int left,int right,int i)  
    {  
        tree[i].left =left;  
        tree[i].right =right;  
        if(tree[i].left ==tree[i].right ) 
        {
            tree[i].remain=1;
            return ;  
        }
        build(left,(left+right)/2,2*i);  
        build((left+right)/2+1,right,2*i+1); 
        tree[i].remain=tree[2*i].remain+tree[2*i+1].remain;
    }  
    void insert(int p,int val,int k)
    {
          if(tree[k].left == tree[k].right)
             {
                       tree[k].remain--;
                       a[tree[k].left] = val;
                       return ;
             }
          if(p <= tree[k<<1].remain)  
                       insert(p,val,2*k);
             else
                       insert(p-tree[2*k].remain,val,2*k+1); //右子树
     
             tree[k].remain = tree[2*k].remain + tree[2*k+1].remain;   //更新remain
    }
    
    int main()
    {
           int i,n;
        while(~scanf("%d",&n))
        { 
          for(i=1;i<=n;i++)
               scanf("%d%d",&b[i],&c[i]);
          build(0,n-1,1);
        for(i=n;i>=1;i--)
        {
            insert(b[i]+1,c[i],1);
        }
        printf("%d",a[0]);
        for(i=1;i<n;i++)
            printf(" %d",a[i]);
        printf("
    ");
        }
        return 0;
    }
               
  • 相关阅读:
    nginx反向代理配置去除前缀
    centos6.x yum源站失效
    Shell if 条件判断
    使用Shell显示脚本使用帮助
    libmysqlclient.so.20 错误问题解决
    linux中ldconfig的使用介绍
    【源码解读】Vue与ASP.NET Core WebAPI的集成
    【Golang】快速复习指南QuickReview(十一)——数据库访问(MySql为例)
    【Golang】快速复习指南QuickReview(十)——goroutine池
    【Golang】快速复习指南QuickReview(九)——socket
  • 原文地址:https://www.cnblogs.com/cancangood/p/3400111.html
Copyright © 2011-2022 走看看