HDU 5360 Hiking

题意:有一个人要邀朋友去远足,但朋友有要求,就是除开邀请的那个人之外,已经邀到的人的数量要在他可接受的范围内(不小于l,不大于r)。朋友一旦接受邀请,就不会反悔。问最大能邀到多少人,并输出邀到这么多人的一组方案。

思路:在当前邀到的人的数量下,找到满足邀请条件的其他人中r值最小的人邀请,因为他最“紧急”。

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;


const int maxp=(1e+5)+5;
struct p
{
    int l,r,biaohao;


}people[maxp];


bool operator > (p a,p b)
{
        if(a.r==b.r)
            return a.l>a.l;
        return a.r>b.r;
}
int yao[maxp];
bool cmp(const p &a,const p &b)
{
    if(a.l==b.l){
        return a.r<b.r;
    }else{
        return a.l<b.l;
    }
}




priority_queue<p,vector<p>,greater<p> >que;
int main()
{
    int cnt;
    scanf("%d",&cnt);
    while(cnt)
    {
        cnt--;
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&people[i].l);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&people[i].r);
        }
        for(int i=0;i<n;i++)
            people[i].biaohao=i+1;
        sort(people,people+n,cmp);
        int pe=0;
        int cc=0;
        int i=0;
        while(i<n)
        {
             while(people[i].l<=pe)
             {
                 que.push(people[i]);
                 i++;
                 if(i>=n)break;
             }
             if(i<n&&que.empty())
            {
                for(int j=i;j<n;j++)
                {
                    yao[cc]=people[j].biaohao;
                    cc++;
                }
                break;
            }
             while(!que.empty())
             {
                p pp=que.top();
                que.pop();
                yao[cc]=pp.biaohao;
                cc++;
                if(pp.l<=pe&&pe<=pp.r)
                {
                    pe++;
                    if(i<n)
                        break;
                }
             }
        }
        printf("%d\n",pe);
        for(int i=0;i<n-1;i++)
            printf("%d ",yao[i]);
        printf("%d\n",yao[n-1]);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值