解题思路
当我们走到第i个机房时,如果它可以做,就做(贪心)。
如果它不能做,我们把它前面选择的机房都看一看,如果有几个机房,它的编号为j1,j2……,找到最大的 t j tj tj,把 t j tj tj换成 t i ti ti。
PS:可以用堆来放选择的机房。(第一次发现堆这么好用,大爱C++)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue<long long>l;
long long n,m,ans,t,s;
struct c{
long long x,t;
}a[1000100];
bool cmp(const c&l,const c&r)
{
return l.x<r.x;
}
int main(){
freopen("plan.in","r",stdin);
freopen("plan.out","w",stdout);
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&a[i].x,&a[i].t);
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
t=t+a[i].x-a[i-1].x+a[i].t;
s++;
l.push(a[i].t);
if(!l.empty()&&t>m)
{
t-=l.top();
l.pop();
s--;
}
if(t>m)break;
ans=max(ans,s);
}
printf("%lld",ans);
}