• hdu 5493 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 (T≤1000).
    Each test case starts with a line containing an integer N indicating the number of people in the queue (1≤N≤100000). Each of the next N lines consists of two integers hi and ki as described above (1≤hi≤109,0≤ki≤N−1). 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

    题意:有n个人,每个人的身高和左边或右边比自己高的人的个数num[i],输出符合给出的条件且字典序最小的从左到右队列里每个人的身高。 
    题解:人有100000个,可以从线段树方法考虑,把问题转化为把每个人前面能留多少个空位给高个子的人,可以先按身高从小到大排个序,考虑第i个人前面留的位置肯定是num[i]或n-i-num[i]中的较小值,这样才能让字典序最小,一旦有n - i - num[i]的值小于0说明无解。

      1 #pragma comment(linker, "/STACK:1024000000,1024000000")
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<cmath>
      6 #include<math.h>
      7 #include<algorithm>
      8 #include<queue>
      9 #include<set>
     10 #include<bitset>
     11 #include<map>
     12 #include<vector>
     13 #include<stdlib.h>
     14 #include <stack>
     15 using namespace std;
     16 #define PI acos(-1.0)
     17 #define max(a,b) (a) > (b) ? (a) : (b)
     18 #define min(a,b) (a) < (b) ? (a) : (b)
     19 #define ll long long
     20 #define eps 1e-10
     21 #define MOD 1000000007
     22 #define N 100006
     23 #define inf 1e12
     24 struct Node{
     25    int h,num;
     26 }node[N];
     27 int n,s[N<<3],res[N];
     28 
     29 bool cmp(Node a,Node b){
     30    return a.h<b.h;
     31 }
     32 
     33 void pushup(int k){
     34    s[k]=s[k*2]+s[k*2+1];
     35 }
     36 
     37 void build(int k,int left,int right){
     38    if(left==right){
     39       s[k]=1;
     40       return;
     41    }
     42    int mid=(left+right)/2;
     43    build(k*2,left,mid);
     44    build(k*2+1,mid+1,right);
     45    pushup(k);
     46 }
     47 
     48 void modify(int k,int left,int right,int pos,int val){
     49     if(left==right){
     50        res[left]=val;
     51        s[k]=0;
     52        return;
     53     }
     54     int mid=(left+right)/2;
     55     if(pos<=s[k*2]){
     56        modify(k*2,left,mid,pos,val);
     57     }else{
     58        modify(k*2+1,mid+1,right,pos-s[k*2],val);
     59     }
     60     pushup(k);
     61 }
     62 
     63 int main()
     64 {
     65    int ac=0;
     66    int t;
     67    scanf("%d",&t);
     68    while(t--){
     69        scanf("%d",&n);
     70        for(int i=1;i<=n;i++){
     71            scanf("%d%d",&node[i].h,&node[i].num);
     72        }
     73        sort(node+1,node+1+n,cmp);
     74        build(1,1,n);//建树
     75 
     76        int flag=1;
     77        for(int i=1;i<=n;i++){
     78            int m=n-i;
     79            int tmp=m-node[i].num;
     80            if(tmp<0){
     81                flag=0;
     82                break;
     83            }
     84            if(node[i].num<tmp){
     85                modify(1,1,n,node[i].num+1,node[i].h);
     86            }else{
     87                modify(1,1,n,tmp+1,node[i].h);
     88            }
     89        }
     90        printf("Case #%d: ",++ac);
     91        if(flag==0){
     92            printf("impossible
    ");
     93        }
     94        else{
     95            printf("%d",res[1]);
     96            for(int i=2;i<=n;i++){
     97               printf(" %d",res[i]);
     98            }
     99            printf("
    ");
    100        }
    101    }
    102     return 0;
    103 }
    View Code
  • 相关阅读:
    JSTL之迭代标签库
    java中的IO流
    浅谈代理模式
    TreeSet集合
    网络编程之UDP协议
    Java中的多线程
    Java中的面向对象
    JavaScript 函数表达式
    JavaScript 数据属性和访问器属性
    JavaScript 正则表达式语法
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4851529.html
Copyright © 2020-2023  润新知