• HDU 5493 Queue


    Queue

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 68    Accepted Submission(s): 40

    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 $(Tleq 1000)$.
    Each test case starts with a line containing an integer N indicating the number of people in the queue $(1leq Nleq 100000)$. Each of the next N lines consists of two integers hi and ki as described above $(1leq h_ileq 10^9,0leq k_ileq N−1)$. Note that the order of the given $h_i and k_i$ is randomly shuffled.
    The sum of N over all test cases will not exceed $10^6$

    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

     解题:贪心占位,按高度由低到高排序后,贪心占位就是了

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int INF = 0x3f3f3f3f;
     4 const int maxn = 100100;
     5 struct INFO {
     6     int height,k;
     7     bool operator<(const INFO &rhs)const {
     8         return height < rhs.height;
     9     }
    10 } info[maxn];
    11 struct node {
    12     int lt,rt,val;
    13 } tree[maxn<<2];
    14 int ret[maxn];
    15 inline void pushup(int v) {
    16     tree[v].val = tree[v<<1].val + tree[v<<1|1].val;
    17 }
    18 void build(int lt,int rt,int v) {
    19     tree[v].lt = lt;
    20     tree[v].rt = rt;
    21     if(lt == rt) {
    22         tree[v].val = 1;
    23         return;
    24     }
    25     int mid = (lt + rt)>>1;
    26     build(lt,mid,v<<1);
    27     build(mid + 1,rt,v<<1|1);
    28     pushup(v);
    29 }
    30 void update(int pos,int v) {
    31     if(tree[v].lt == tree[v].rt) {
    32         tree[v].val = 0;
    33         return;
    34     }
    35     if(pos <= tree[v<<1].rt) update(pos,v<<1);
    36     else update(pos,v<<1|1);
    37     pushup(v);
    38 }
    39 int query(int cnt,int v,bool ok) {
    40     if(cnt > tree[v].val) return INF;
    41     if(tree[v].lt == tree[v].rt) return tree[v].lt;
    42     if(ok) {
    43         if(tree[v<<1].val >= cnt) return query(cnt,v<<1,ok);
    44         else return query(cnt - tree[v<<1].val,v<<1|1,ok);
    45     }
    46     if(tree[v<<1|1].val >= cnt) return query(cnt,v<<1|1,ok);
    47     else return query(cnt - tree[v<<1|1].val,v<<1,ok);
    48 }
    49 int main() {
    50     int kase,n,cs = 1;
    51     scanf("%d",&kase);
    52     while(kase--) {
    53         scanf("%d",&n);
    54         for(int i = 0; i < n; ++i)
    55             scanf("%d%d",&info[i].height,&info[i].k);
    56         sort(info,info + n);
    57         bool flag = true;
    58         build(1,n,1);
    59         for(int i = 0; i < n && flag; ++i){
    60             int L = query(info[i].k + 1,1,true);
    61             int R = query(info[i].k + 1,1,false);
    62             if(min(L,R) == INF) {
    63                 flag = false;
    64                 break;
    65             }
    66             update(min(L,R),1);
    67             ret[min(L,R)] = info[i].height;
    68         }
    69         printf("Case #%d:",cs++);
    70         if(flag){
    71             for(int i = 1; i <= n; ++i)
    72                 printf(" %d",ret[i]);
    73             puts("");
    74         }else puts(" impossible");
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    GO 语言使用copy 拷贝切片的问题
    ggplot 局部放大
    R语言hist重叠图作法
    illumina SNP 芯片转基因型矩阵
    Shell 变量嵌套
    JVM指令
    VUE—CLI学习
    Gradle 项目打开自动下载Zip问题及相关配置
    MySQL8服务无法启动,服务没有报告任何错误
    SpringMVC相关
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4842723.html
Copyright © 2020-2023  润新知