zoukankan      html  css  js  c++  java
  • 【poj2828】Buy Tickets 线段树 插队问题

    【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 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 Posiand 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 Inpu

    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.

    题解

         线段树的妙用,加深理解吧.....

    代码

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <ctime>
     5 #include <iostream>
     6 #include <algorithm>
     7 #include <set>
     8 #include <vector>
     9 #include <queue>
    10 #include <typeinfo>
    11 #include <map>
    12 #include <stack>
    13 typedef long long ll;
    14 #define inf 0x7fffffff
    15 using namespace std;
    16 inline ll read()
    17 {
    18     ll x=0,f=1;
    19     char ch=getchar();
    20     while(ch<'0'||ch>'9')
    21     {
    22         if(ch=='-')f=-1;
    23         ch=getchar();
    24     }
    25     while(ch>='0'&&ch<='9')
    26     {
    27         x=x*10+ch-'0';
    28         ch=getchar();
    29     }
    30     return x*f;
    31 }
    32 
    33 //**************************************************************************************
    34 struct ss
    35 {
    36     int l,r,x;
    37 } tr[200001*5];
    38 int n;
    39 int a[200001];
    40 void build(int k,int s,int t)
    41 {
    42     tr[k].l=s;
    43     tr[k].r=t;
    44     if(s==t)
    45     {
    46         tr[k].x=1;
    47         return;
    48     }
    49     int mid=(s+t)>>1;
    50     build(k<<1,s,mid);
    51     build(k<<1|1,mid+1,t);
    52     tr[k].x=tr[k<<1].x+tr[k<<1|1].x;
    53 }
    54 void update(int k,int x,int y)
    55 {
    56     if(tr[k].l==tr[k].r)
    57     {
    58         tr[k].x=0;
    59         a[tr[k].l]=y;
    60         return;
    61     }
    62     if(tr[k<<1].x>=x)update(k<<1,x,y);//能往左下放尽量放,同时你要明白这是个插队问题^-^!!
    63     else update(k<<1|1,x-tr[k<<1].x,y);
    64     tr[k].x=tr[k<<1].x+tr[k<<1|1].x;
    65 }
    66 int main()
    67 {
    68     int x[200001];
    69     int y[200001];
    70     while(scanf("%d",&n)!=EOF)
    71     {
    72         build(1,1,200001);
    73         for(int i=1; i<=n; i++)
    74         {
    75             scanf("%d%d",&x[i],&y[i]);
    76             x[i]++;
    77         }
    78         for(int i=n; i>=1; i--)
    79         {
    80             update(1,x[i],y[i]);
    81         }
    82         for(int i=1; i<n; i++)
    83         {
    84             printf("%d ",a[i]);
    85         }
    86         printf("%d
    ",a[n]);
    87     }
    88 
    89 
    90     return 0;
    91 }
  • 相关阅读:
    利用Sentinel实现Redis主从切换
    Docker应用一:docker介绍
    利用RAP搭建可视化接口管理平台
    Lucene初步搜索
    Lucene索引的初步创建
    kettle不能正常自动获取字段
    配置hive元数据库mysql时候出现 Unable to find the JDBC database jar on host : master
    Lost connection to MySQL server at ‘reading initial communication packet', system error: 0 mysql远程连接问题
    Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)解决方法
    Sqoop import加载HBase案例详解
  • 原文地址:https://www.cnblogs.com/zxhl/p/4704282.html
Copyright © 2011-2022 走看看