题目链接: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;
}