[贪心+优先队列] hdu5360多校联合 第六场 Hiking

http://acm.hdu.edu.cn/showproblem.php?pid=5360

贪心的思路还是比较好想的,每次选择cur(已经邀请成功的人数)所在的区间中右端点最小的(因为右端点大的在后面可以邀请成功的几率大),然后很自然的想到可以用一个优先队列来维护这些区间,只要每次把左端点小于等于cur的区间放到优先队列中即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<string>
#include<iostream>
#include<queue>
using namespace std;
int n;
struct node
{
    int l,r,id;
    friend bool operator < (node a, node b)
    {
        return a.r>b.r;
    }
}
a[100010];
int used[100010];
bool cmp(node a, node b)
{
    return a.l<b.l;
}
int main()
{
    int T;
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%d",&a[i].l);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i].r);
            a[i].id=i+1;
        }
        sort(a,a+n,cmp);
        //  for(int i = 0;i<n;i++)
        //      printf("i = %d, L = %d, R = %d\n",i,a[i].l,a[i].r);
        priority_queue<node> que;

        vector<int> people;
        memset(used,0,sizeof used);

        int ans=0;
        int tot=0;
        while(tot<n || que.size())
        {
            while(tot<n && a[tot].l<=ans)
            {
                que.push(a[tot]);
                tot++;
            }
            while(que.size() && que.top().r < ans) que.pop();
            if(que.empty()) break;
            ans++;
            people.push_back(que.top().id);
            used[que.top().id]=1;
            que.pop();
        }
        printf("%d\n",ans);

        int num=1;
        for(int i=0; i<people.size(); i++)
        {
            printf("%d%c",people[i],num==n?'\n':' ');
            num++;
        }
        for(int i=1; i<=n; i++)
        {
            if(used[i]==0)
            {
                printf("%d%c",i,num==n?'\n':' ');
                num++;
            }
        }
    }
    return 0;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值