【poj 2828】 Buy Tickets(线段树/树状数组)

【poj 2828】 Buy Tickets(线段树/树状数组)


Buy Tickets
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 16922 Accepted: 8376

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


按顺序把n个人插入队列 给出每个人插入时的位置 正着不好想 可以倒着做 从第n到第1个人遍历

对于第i个人的位置id 其实就是找当前队列第id个空位置

例如第一组样例

4
0 77
1 51
1 33
2 69

给出下标从0开始 不好做 所以把下标统一后移一位

69->3

33->2

当51->2时 位置1可用 位置2.3被分配了 所以给51位置4

77->1


用线段树很容易写 找到从头开始的第k个空位置


代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int bit[800800];
int ans[200200];

//初始化n个空位
void init(int site,int l,int r)
{
    if(l == r)
    {
        bit[site] = 1;
        return;
    }
    int mid = (l+r)>>1;

    init(site<<1,l,mid);
    init(site<<1|1,mid+1,r);
    bit[site] = bit[site<<1]+bit[site<<1|1];
}

//找第data个空位
int Search(int site,int l,int r,int data)
{
//    printf("%d %d %d\n",l,r,data);
    //第data个空位肯定在当前遍历的子树中 剩余空位--
    bit[site]--;
    //l == r说明遍历到叶子 也就是找到了第data个空位
    if(l == r) return l;
    
    int mid = (l+r)>>1;
    //左树的位置不够 在右树中找第(data-(左树空位置))个空位置
    if(bit[site<<1] < data)
    {
       return Search(site<<1|1,mid+1,r,data-bit[site<<1]);
    }
    //否则只直接在左树找第data个空位置
    else
    {
       return Search(site<<1,l,mid,data);
    }
}

pair <int,int> p[200200];
int main()
{
    int n;

    while(~scanf("%d",&n))
    {
        init(1,1,n);

        for(int i = 0; i < n; ++i)
        {
            scanf("%d%d",&p[i].first,&p[i].second);
        }

        for(int i = n-1; i >= 0; --i)
        {
//            int tmp = Search(1,1,n,p[i].first+1);
//            puts("-------");
//            printf("%d\n",tmp);
            ans[Search(1,1,n,p[i].first+1)] = p[i].second;
        }

        for(int i = 1; i <= n; ++i)
        {
            if(i != 1) putchar(' ');
            printf("%d",ans[i]);
        }
        puts("");
    }

    return 0;
}


树状数组两种做法 不过核心都跟线段树一样 找第k个空位

第一种就是二分 找第一个id 满足 前id项和==k

跟线段树的思想类似

直接上代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int bit[200200];
int n;

int Lowbit(int x)
{
    return x&(-x);
}

void Add(int x,int data)
{
    while(x <= n)
    {
        bit[x] += data;
        x += Lowbit(x);
    }
}

int Sum(int x)
{
    int ans = 0;
    while(x > 0)
    {
        ans += bit[x];
        x -= Lowbit(x);
    }
    return ans;
}

pair <int,int> p[200200];
int ans[200200];

int main()
{
    int l,r,c;

    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n; ++i)
        {
            scanf("%d%d",&p[i].first,&p[i].second);
            p[i].first++;
        }
        for(int i = 1; i <= n; ++i) Add(i,1);


        for(int i = n-1; i >= 0; --i)
        {
            l = 1, r = n;
            c = -1;

            while(l <= r)
            {
                int mid = (l+r)>>1;
                int tmp = Sum(mid);
                if(tmp >= p[i].first)
                {
                    if(tmp == p[i].first) c = mid;
                    r = mid-1;
                }
                else l = mid+1;
            }

            ans[c] = p[i].second;
            Add(c,-1);
        }

        for(int i = 1; i <= n; ++i)
        {
            if(i != 1) putchar(' ');
            printf("%d",ans[i]);
        }
        puts("");
    }

    return 0;
}

第二种方法比较优美 按位找 主要是findk函数,节省了很大一段时间


代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int bit[200200];
int n;

int Lowbit(int x)
{
    return x&(-x);
}

void Add(int x,int data)
{
    while(x <= n)
    {
        bit[x] += data;
        x += Lowbit(x);
    }
}

int Sum(int x)
{
    int ans = 0;
    while(x > 0)
    {
        ans += bit[x];
        x -= Lowbit(x);
    }
    return ans;
}

int findk(int data)
{
    int id = 0;

    for(int i = 17; i >= 0; --i)
    {
        id ^= 1<<i;
        if(id <= n && data > bit[id]) data -= bit[id];
        else id ^= 1<<i;
    }
    return id+1;
}

pair <int,int> p[200200];
int ans[200200];

int main()
{
    int l,r,c;

    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n; ++i)
        {
            scanf("%d%d",&p[i].first,&p[i].second);
            p[i].first++;
        }
        for(int i = 1; i <= n; ++i) Add(i,1);


        for(int i = n-1; i >= 0; --i)
        {
            c = findk(p[i].first);
            ans[c] = p[i].second;
            Add(c,-1);
        }

        for(int i = 1; i <= n; ++i)
        {
            if(i != 1) putchar(' ');
            printf("%d",ans[i]);
        }
        puts("");
    }

    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值