zoukankan      html  css  js  c++  java
  • [POJ 2828]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 Posi and Valiin 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 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.

    题解

    最后一个插入的人位置肯定不会再改变,所以我们可以倒着来读入每个人,比如第$i$个人插入到$pos$位置,那么他前面必须留$pos$个空位才行,维护空位个数可以用线段树,单点修改维护。但这里给出树状数组的版本。我们每次倒着读入后,对于这个人,二分其在序列中的位置,树状数组记录选中空位了的个数,显然$mid-count(mid)$就是$mid$之前(包括$mid$)的空位个数。复杂度$O(nlog_2 ^2 n)$。

     1 //It is made by Awson on 2017.9.19
     2 #include <map>
     3 #include <set>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <queue>
     7 #include <stack>
     8 #include <cstdio>
     9 #include <string>
    10 #include <vector>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Max(a, b) ((a) > (b) ? (a) : (b))
    17 #define Min(a, b) ((a) < (b) ? (a) : (b))
    18 #define Abs(a) ((a) < 0 ? (-(a)) : (a))
    19 #define lowbit(x) ((x)&(-(x)))
    20 using namespace std;
    21 const int N = 200000;
    22 
    23 int a[N+5], b[N+5];
    24 int n, posi, vali;
    25 int c[N+5];
    26 int pos[N+5];
    27 
    28 void add(int x) {
    29     for (; x <= n; x += lowbit(x)) c[x]++;
    30 }
    31 int count(int x) {
    32     int sum = 0;
    33     for (; x; x -= lowbit(x)) sum += c[x];
    34     return sum;
    35 }
    36 void work() {
    37     memset(c, 0, sizeof(c));
    38     for (int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]);
    39     for (int i = n; i >= 1; i--) {
    40         posi = a[i], vali = b[i];
    41         int L = 1, R = n, ans = n;
    42         while (L <= R) {
    43             int mid = (L+R)>>1;
    44             if (mid-count(mid)-1 >= posi) ans = mid, R = mid-1;
    45             else L = mid+1;
    46         }
    47         pos[ans] = vali;
    48         add(ans);
    49     }
    50     for (int i = 1; i <= n; i++)
    51         printf("%d ", pos[i]);
    52     putchar('
    ');
    53 }
    54 
    55 int main() {
    56     freopen("tickets.in", "r", stdin);
    57     freopen("tickets.out", "w", stdout);
    58     while (~scanf("%d", &n))
    59         work();
    60     return 0;
    61 }
  • 相关阅读:
    【亲测有效】安装npm慢的解决方案
    设置redis开机自动启动
    win10开启redis失败解决方案
    ajax跨域问题
    python进程不能并行的原因,进程阻塞
    python多进程并行代码
    python多进程间通信
    orangepi自启动打开一个终端并且运行脚本
    lxterminal命令打开新窗口并执行python脚本
    orangepi获取cpu温度
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/7553128.html
Copyright © 2011-2022 走看看