hdu 5493 Queue 贪心+线段树

本文探讨了在特定约束条件下重建排队序列的问题,利用输入数据中的身高和相对位置信息,通过算法实现序列还原。

Queue

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


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<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

struct Node{
    int n,k;
};
int comp(Node a,Node b){
    return a.n < b.n;
}
#define maxn 400007
int res[maxn];
int n;
Node point[maxn];

int lc[maxn],rc[maxn],num[maxn];
int cnt = 0;

void build(int u,int l,int r){
    if(l == r) return ;
    int mid = (l+r)/2;
    lc[u] = cnt++;
    rc[u] = cnt++;
    build(lc[u],l,mid);
    build(rc[u],mid+1,r);
}

int query(int u,int l,int r,int k){
    if(l == r){
        num[u] = 1;
        return l;
    }
    int y = num[lc[u]];

    int ans = 0;
    int mid =(l+r)/2;
    int x = mid-l+1-y;
    if(x >= k)
        ans = query(lc[u],l,mid,k);
    else
        ans = query(rc[u],mid+1,r,k-x);
    num[u] = num[lc[u]]+num[rc[u]];
    return ans;
}

int work(){
    for(int i = 0;i < n; i++)
        scanf("%d%d",&point[i].n,&point[i].k);
    sort(point,point+n,comp);
    for(int i = 0;i < n; i++){
        int x = n - i - 1;
        if(point[i].k > x) return -1;
        point[i].k = min(point[i].k,x-point[i].k);
    }
    cnt = 1;
    memset(num,0,sizeof(num));
    build(0,0,n-1);
    for(int i = 0;i < n; i++){
        int p = query(0,0,n-1,point[i].k+1);
        res[p] = point[i].n;
    }
    return 0;
}

int main(){
    int t,tt=1;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        int ans = work();
        printf("Case #%d:",tt++);
        if(ans == -1) printf(" impossible\n");
        else {
            for(int i = 0;i < n; i++)
                printf(" %d",res[i]);
            printf("\n");
        }
    }
    return 0;
}




标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值