贪心——HDU 5360

  • 题目链接:
    http://acm.hdu.edu.cn/showproblem.php?pid=5360

  • 题意: 现在要邀请N个人,每个人接受邀请的条件为,在邀请他时,已经接受邀请的人数需要在 [Li,Ri] 之间,求一个邀请顺序使得最后接受邀请的人数最多

  • 分析:我们肯定需要按L排序,然后每次选择L小于等于当前受邀人数的人加入一个按R从小到大排序的优先队列,然后从这个队列中选则R最小的发出邀请,并标记为已邀请,受邀人数+1。最后将没有受邀的人排在最后即可

  • AC代码:

/*************************************************************************
    > File Name: test.cpp
    > Author: Akira 
    > Mail: qaq.febr2.qaq@gmail.com 
 ************************************************************************/

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <ctime>
#include <climits>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;

#define MaxN 100001
#define MaxM MaxN*10
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
const int mod = 1E9+7;
#define bug cout<<88888888<<endl;
int N;
struct Node
{
    int L,R,id;
    bool operator<(const Node & a) const 
    {
        if(a.L == L) return a.R>R;
        else return a.L>L;
    }
}node[MaxN];
struct Right
{
    int x;
    int pos;
    Right(int a = 0, int b = 0){x = a, pos = b;}
    bool operator<(const Right & a) const
    {
        return a.x < x;
    }
};
priority_queue<Right> Q;
int vis[MaxN];
int main()
{
    //std::ios::sync_with_stdio(false);
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &N);
        for(int i=1;i<=N;i++)scanf("%d", &node[i].L);
        for(int i=1;i<=N;i++)
        {
            scanf("%d", &node[i].R);
            node[i].id = i;
        }
        sort(node+1,node+1+N);
        CLR(vis);
        int now = 0;
        int _begin = 1;
        vector<int>ans;
        while(now<N)
        {
            for(;_begin<=N;_begin++)
            {
                if(node[_begin].L<=now)
                {
                    Q.push(Right(node[_begin].R,node[_begin].id));
                }
                else break;
            }
            if(!Q.empty())
            {
                Right tmp = Q.top();Q.pop();
                //cout <<now <<"--"<< tmp.x << endl;
                if(tmp.x>=now)
                {
                    //bug;
                    vis[tmp.pos] = 1;
                    ans.push_back(tmp.pos);
                    now++;
                }
            }
            else break;
        }
        printf("%d\n", ans.size());
        for(int i=1;i<=N;i++)
        {
            if(!vis[i]) ans.push_back(i);
        }
        for(int i=0;i<ans.size();i++)
            printf("%d%c", ans[i], i==ans.size()-1 ? '\n' : ' ');
    }
    //system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值