Uva-7423-Assigning Workstations(贪心+优先队列)

链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5445


资源占用问题,x,y,y+m分别表示开始时间,结束时间,离开时间,开始->结束过程中占用资源1,在结束->离开时间内如有新的进程进入则可以继续使用资源而不占用新的资源,求:n-最少资源占用


按照x值排序,将y丢入小堆Q,若堆不空,且x-top(Q)>=m,即x不必占用新资源,堆顶出,y进;

由于m固定,则可以贪心的取最小y并替代,复杂度O(nlog(n))



#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <string>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 400007
#define MAXM 4500000
const long long mod=1000000007;
int a[MAXN],b[MAXN];
struct node
{
    long long x,y,z;
} nod[MAXN];
bool cmp(node a,node b)
{
    if(a.x!=b.x)return a.x<b.x;
    return a.y<b.y;
}
int main()
{
    int n;
    long long m;
    while(scanf("%d%I64d",&n,&m)!=EOF)
    {
        int cnt=0;
        long long x,y;
        for(int i=0; i<n; ++i)
        {
            scanf("%I64d%I64d",&x,&y);
            nod[cnt].x=x;
            nod[cnt].y=x+y;
            nod[cnt++].z=x+y+m;
        }
        sort(nod,nod+cnt,cmp);
        priority_queue<int, vector<int>, greater<int> > q;
        while(!q.empty())q.pop();
        int ans=0;
        for(int i=0; i<cnt; ++i)
        {
            if(q.empty())
            {
                q.push(nod[i].y);
            }
            else
            {
                int t=q.top();
                while(nod[i].x-t>m)
                {
                    q.pop();
                    if(q.empty())break;
                    t=q.top();
                }
                if(!q.empty())
                {
                    int o=q.top();
                    if(nod[i].x>=o)
                    {
                        q.pop();
                        q.push(nod[i].y);
                        ans++;
                    }
                    else
                        q.push(nod[i].y);
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

/*
3 5
1 5
6 3
14 6
5 10
2 6
1 2
17 7
3 9
15 6
*/


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值