(POJ2828)Buy Tickets <树状数组 | 线段树> 找第k空位

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 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 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
这里写图片描述
Source

POJ Monthly–2006.05.28, Zhu, Zeyuan

题意:
有n个人排队买票,但是可以插队。售票员在第0号位置,买票的人依次排在右边。现在给你n个人的数据:pi,vi.其中pi表示第i个人排队时站在第pi个人后面,他的val值为vi。现在从小到达给出所有的pi,vi.问最后排队的人的vi值的顺序。

分析:
其实题目的意思很简单,就是将第i个人插在第pi个人后面,问最后的顺序。
若我们模拟这个过程,不管是数组还是链表都是O(n^2)的时间复杂度,会超时。
正向思维无法解决,那我们就逆向思维。
现在我们知道总共有 n 个人,且最后一个人要排在这 n 个人中的位置是 p[n],我们可以直接把队列的第 p[n] 个位子安排给第 n 个人

现在来考虑第 n-1 个人,他要排在只有 n-1 个人的第 p[n-1] 个位置上,怎么做呢?我们肯定是从队列的第 1 个开始数,遇到没有被人占领的位置就加 1,直到数到了 p[n-1],那么这个空位就安排给第 n-1 个人
对于第 n-2 个人,以及剩下的所有人都可以这样做
….
所以现在问题转化成了如何快速求出前p[n]个空位的位置。
关于这个问题我们有两种解决方法:树状数组 或 线段树

树状数组:
空位出值为1,有人处值为0.所以我们要求前缀和为p[n]的最小值。树状数组可以在O(logn)时间求出前缀和,然后我们只需要二分查找即可,时间也是O(logn).

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 200010;
int c[maxn];
int ans[maxn],p[maxn],v[maxn];
int n;

int sum(int x)
{
    int res = 0;
    while(x > 0)
    {
        res += c[x];
        x -= x&-x;
    }
    return res;
}

void add(int x,int d)
{
    while(x <= n)
    {
        c[x] += d;
        x += x&-x;
    }
}

int find_(int k)
{
    int low = 1, high = n, mid, sumt;
    while( low <= high)
    {
        mid = (low + high)/2;
        sumt = sum(mid);
        if(sumt < k) low = mid + 1;
        else high = mid -1;
    }
    return low;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++) add(i,1);
        for(int i=0;i<n;i++)
            scanf("%d%d",&p[i],&v[i]);
        for(int i=n-1;i>=0;i--)
        {
            int t = find_(p[i]+1);
            ans[t] = v[i];
            add(t,-1);
        }
        for(int i=1;i<n;i++)
            printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
    return 0;
}

线段树
维护区间空位数和。进行点修改即可。这里我们是在查询时就进行了更新操作

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 200010;
int p[maxn],v[maxn],ans[maxn];

struct node
{
    int id,st,en,val;
}tree[maxn<<2];

void build(int id,int L,int R)
{
    tree[id].st = L;
    tree[id].en = R;
    tree[id].val = R - L +1;
    if(L == R) return ;
    int mid = (L + R)>>1;
    build(id<<1, L, mid);
    build(id<<1|1, mid+1, R);
}

int query(int id,int pos)
{
    tree[id].val--;
    if(tree[id].st == tree[id].en) return tree[id].st;
    if(pos <= tree[id<<1].val) return query(id<<1, pos);
    else return query(id<<1|1, pos - tree[id<<1].val);  //注意
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        build(1,1,n);
        for(int i=0;i<n;i++) scanf("%d%d",&p[i],&v[i]);
        for(int i=n-1;i>=0;i--)
        {
            int t = query(1,p[i]+1);
            ans[t] = v[i];
        }
        for(int i=1;i<n;i++) printf("%d ",ans[i]);
        printf("%d\n",ans[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值