• Queue


    Problem Description
    N people numbered from 1 to N are waiting in a bank for service. They all stand in a queue, but the queue never moves. It is lunch time now, so they decide to go out and have lunch first. When they get back, they don’t remember the exact order of the queue. Fortunately, there are some clues that may help.
    Every person has a unique height, and we denote the height of the i-th person as hi. The i-th person remembers that there were ki people who stand before him and are taller than him. Ideally, this is enough to determine the original order of the queue uniquely. However, as they were waiting for too long, some of them get dizzy and counted ki in a wrong direction. ki could be either the number of taller people before or after the i-th person.
    Can you help them to determine the original order of the queue?
     
    Input
    The first line of input contains a number T indicating the number of test cases (T1000).
    Each test case starts with a line containing an integer N indicating the number of people in the queue (1N100000). Each of the next N lines consists of two integers hi and ki as described above (1hi109,0kiN1). Note that the order of the given hi and ki is randomly shuffled.
    The sum of N over all test cases will not exceed 106
     
    Output
    For each test case, output a single line consisting of “Case #X: S”. X is the test case number starting from 1. S is people’s heights in the restored queue, separated by spaces. The solution may not be unique, so you only need to output the smallest one in lexicographical order. If it is impossible to restore the queue, you should output “impossible” instead.
     
    Sample Input
    3 3 10 1 20 1 30 0 3 10 0 20 1 30 0 3 10 0 20 0 30 1
     
    Sample Output
    Case #1: 20 10 30 Case #2: 10 20 30 Case #3: impossible
     
    Source

     线段树模拟+贪心

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int oo = 1e9;
    const double PI = acos(-1);
    const int N = 1e6;
    int n, Queue[N];
    struct node
    {
        int high, more;
        bool operator < (const node &a) const
        {
            return high < a.high;
        }
    } student[N];
    struct da
    {
        int left, right, sum;
    } as[N*4];
    void build(int left ,int right , int i)
    {
        as[i].left = left;
        as[i].right = right;
        as[i].sum = right - left + 1;
        int mid = (left + right) / 2;
        if(left == right) return ;
        build(left, mid, 2*i);
        build(mid+1, right, 2*i+1);
    }
    
    void update(int i, int id)
    {
        if(as[i].left == as[i].right && as[i].left == id)
        {
            as[i].sum = 0;
            return ;
        }
        if(id <= as[i*2].right) update(2*i, id);
        else update(2*i+1, id);
        as[i].sum = as[i*2].sum + as[i*2+1].sum;
    }
    
    int query(int i, int val, int sign)
    {
        if(val > as[i].sum) return oo;/**< 如果这个位置已经有人了 或者 这个人没有位置可站了 */
        
        if(as[i].left == as[i].right) return as[i].left;
    
        if(sign == 1)
        {
            if(val <= as[i*2].sum) return query(2*i, val, sign);
            else return query(2*i+1, val - as[i*2].sum, sign);
        }
    
        if(sign == 0)
        {
            if(as[i*2+1].sum >= val) return query(2*i+1, val, sign);
            else return query(2*i, val-as[i*2+1].sum, sign);
        }
    }
    
    int main()
    {
        int T, i, cas = 1, ok;
        scanf("%d", &T);
        while(T--)
        {
            ok = 0;
            memset(Queue, 0, sizeof(Queue));
    
            scanf("%d", &n);
    
            build(1, n, 1);
    
            for(i = 0; i < n; i++)
                scanf("%d %d", &student[i].high, &student[i].more);
    
            sort(student, student + n);/**< 按照身高排序 */
    
            for(i = 0; i < n; i++)
            {
                int l = query(1, student[i].more + 1, 1);/**< l,r 表示这个人可以插入的位置 */
                int r = query(1, student[i].more + 1, 0);
                int mini = min(l, r);/**<  找到最小的位置*/
                if(mini == oo)
                {
                    ok = 1;
                    break;
                }
                update(1, mini);
                Queue[mini] = student[i].high;
            }
    
            if(ok)
            {
                printf("Case #%d: impossible
    ", cas++);
                continue;
            }
            printf("Case #%d:", cas++);
            for(i = 1; i <= n; i++)
                printf(" %d", Queue[i]);
            printf("
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    【Django Admin皮肤】 Simpleui 自定义常用 持续更新中...
    【webSokect】基于django Channels的简单实现
    【webSocket】实现原理
    【webSocket】长轮询
    【前端实时时间】JS原生代码
    【数据结构的补全整理】规定在周一到周五中,赛选出8点到18点中的计划安排,空计划时刻补全空值
    JS面试经典知识(一)
    Python中 zipfile 出现乱码
    Linux shell 对数据进行归档
    Linux 函数的使用
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4844136.html
Copyright © 2020-2023  润新知