Taxed Editor 可反悔贪心

题目链接:https://ac.nowcoder.com/acm/contest/18453/J

题意

有n本书,每本书给定页数以及需要在第几天前读完,最多可以不读m本书,求满足要求的最小读书速度(每天最少读多少页)

思路

将书按截止日期排序
二分找答案
对于每一个二分,维护一个优先级队列,记录当前可以读的书的花费
按截止日期遍历每一本书
若可以读,则加入优先级队列,累加答案;
若不可以读,查看此前能读的书中,花费最大的书(堆顶的书),若其花费比当前书大,那么肯定可以用当前书替换堆顶的书。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5+5;
struct node{
    int l,d;
    bool operator < (const node& n) const{
        return d<n.d;
    }
}nd[maxn];
int n,m;
bool check(int x){
    priority_queue<int> pq;
    int now=0,ans=0;
    for(int i=1;i<=n;i++){
        if(now+nd[i].l<=nd[i].d*x){
            now+=nd[i].l;
            pq.push(nd[i].l);
        }
        else{

            if(!pq.empty()&&pq.top()>nd[i].l){
                now-=pq.top();
                pq.pop();
                now+=nd[i].l;
                pq.push(nd[i].l);
            }
            ans++;
        }
    }
    return ans<=m;
}
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>nd[i].l>>nd[i].d;
    sort(nd+1,nd+n+1);
    int l=0,r=1e15,ans;
        while(l<=r){
        int mid=(l+r)/2;
//        cout<<l<<' '<<r<<' '<<mid<<endl;
        if(check(mid))r=mid-1;
        else l=mid+1;
    }

    cout<<l;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

General.song

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值