资源占用问题,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
*/