题目链接:https://codeforces.com/contest/1132/problem/D
题意:有n台电脑,t=0时刻各个电脑的电量为a[ i ] (1=< i <=n) ,各台电脑每分钟的耗电量为b[ i ] (1<= i <=n) ,先需要选择一个每分钟充电量为ans的充电器,每个时刻只能给最多一台电脑充电,充电时间均为整数,求最小的ans使得在k分钟内的每一分钟开始的时刻每一台电脑的电量都大于等于零 (红色字体部分表示实际上只用保证前k-1分钟内电量大于等于零)
解法:二分+优先队列
先不管充电器每分钟冲多少电,给你一个能每分钟冲x的充电器,显然从贪心的角度,在任意一个时刻,你应该给电最先用完的
那台电脑充电,然后对于x 二分就行了;复杂度O(m*log n*log r);
附代码:
#include<bits/stdc++.h>
using namespace std;
#define per(i,a,b) for(int i=a;i<=b;i++)
#define typemax(type) numeric_limits<type>::max()
typedef long long ll;
const int maxn=200005;
ll a[maxn],b[maxn];
int n,m;
struct node{
ll po,de,rate;
node(){};
node(ll x,ll y,ll z){po=x;de=y;rate=z;}
bool operator < (const node &other) const {
return rate>other.rate;
}
};
bool check(ll k)
{
priority_queue<node> que;
per(i,1,n) que.push(node(a[i],b[i],a[i]/b[i]));
per(i,1,m){
node cur=que.top();que.pop();
if(cur.rate<i-1) return false; //第i分钟开始的时候电量为负
if(cur.rate>=m) return true; //最早用完电的都大于总时间m,直接返回true,不用再冲了
que.push(node(cur.po+k,cur.de,(cur.po+k)/cur.de));//直接在初始电量加上k
}
return true;
}
int main()
{
scanf("%d %d",&n,&m);
per(i,1,n) scanf("%lld",&a[i]);
per(i,1,n){
scanf("%lld",&b[i]);
}
ll l=0,r=1e15;
while(l<r){
ll mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
printf("%lld\n",r==(ll)1e15?(ll)-1:r);
}