Luogu p2107 小Z的AK计划 题解

题目链接

正解:贪心+堆优化

读入的距离不一定是有序的,读进来先要以距离为关键字排序,对于本身距离大于有的总时间的,或者是本身时间大于总时间的,都处理掉。然后用大根堆来存AK的机房用的时间(不要存距离)。当遇到再往里塞一个时间要超限时,就把你前面用的最大的去掉,直到能再塞,再加上本身的距离(用while来实现)。
下面是一段丑陋的AC代码:

#include<bits/stdc++.h>
using namespace std;
priority_queue<int> q;
int n,total,ans,anss;
long long m,sum;
inline int read(){
       int s=0,f=1;char c=getchar();
       while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
       while(isdigit(c)){s=s*10+c-'0';c=getchar();}
       return s*f;
}
inline void write(int x){
       if(x<0){x=-x;putchar('-');}
       if(x>9)write(x/10);
       putchar(x%10+'0');
}
inline long long readl(){
       long long s=0,f=1;char c=getchar();
       while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
       while(isdigit(c)){s=s*10+c-'0';c=getchar();}
       return s*f;
}
struct node{
    long long value;
    long long t;
}a[100010];
bool cmp(node a,node b){return a.t<b.t;}
int main(){
    n=read();cin>>m;
    for(int i=1;i<=n;i++){
        long long x,y;
        cin>>x>>y;
        if(y<=m&&x<=m){
            a[++total].t=x;
            a[total].value=y;
        }
    }
    sort(a+1,a+total+1,cmp);
    for(int i=1;i<=total;i++){
        sum+=a[i].value+a[i].t-a[i-1].t;
        q.push(a[i].value);
        ans++;
        while(sum>m){
            sum-=q.top();
            q.pop();
            ans--;
        }
        anss=max(anss,ans);
    }
    cout<<anss<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值