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.

      就是说几个人在排队,有人插队,然后问最后的队列是啥样。

      很经典的问题(可是我没想出来。T T)。

      从后往前枚举,建树。 首先最后一个人在哪就是哪,那么他前面的人如果原来在的位置在最后一个人插入的位置之后,那么就要向后移一个位置。

      所以说就是到了某个人时(倒序枚举),假设他要插在3这个位置,那么第三个空的地方(已经有人的不计数)就是他的位置。

      代码如下:

    #include<iostream>
    #include<cstdio>
    
    using namespace std;
    
    int BIT[200002*4];
    int N;
    int num[200002];
    int val[200002];
    int ans[200002];
    
    void build_tree(int L,int R,int po)
    {
        BIT[po]=R-L+1;
    
        if(L==R) return;
    
        int M=(L+R)/2;
    
        build_tree(L,M,po*2);
        build_tree(M+1,R,po*2+1);
    }
    
    int query_update(int qn,int L,int R,int po)
    {
        --BIT[po];
        if(L==R) return L;
    
        int M=(L+R)/2;
    
        if(BIT[po*2]>=qn)
            return query_update(qn,L,M,po*2);
        else
            return query_update(qn-BIT[po*2],M+1,R,po*2+1);
    }
    
    int main()
    {
        int temp;
    
        while(~scanf("%d",&N))
        {
            for(int i=N;i>=1;--i)
                scanf("%d %d",&num[i],&val[i]);
    
            build_tree(1,N,1);
    
            for(int i=1;i<=N;++i)
            {
                temp=query_update(num[i]+1,1,N,1);
                ans[temp]=val[i];
            }
    
            for(int i=1;i<=N;++i)
                printf("%d ",ans[i]);
            printf("
    ");
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    直播平台的相关技术(转载)
    各种排序算法分析总结(待整理))
    算法:60.第k个排列
    三种随机化算法:舍伍德算法 拉斯维加斯算法 蒙特卡洛算法
    随机化算法之随机数
    caffe调试
    Euclideanloss_layer层解析
    布线问题(分支限界法)
    最大堆和最小堆
    机器学习知识框架
  • 原文地址:https://www.cnblogs.com/whywhy/p/4189395.html
Copyright © 2011-2022 走看看