CF1132D Stressful Training

一、题目

点此看题

二、解法

显然这个 x x 可以二分,关键在于如何检查。

我们肯定是去救最危险的,怎么定义这个危险呢?用 a [ i ] / b [ i ] a[i]/b[i] ,也就是能承受的天数。

#include <cstdio>
#include <queue>
using namespace std;
#define int long long
const int M = 200005;
int read()
{
	int x=0,f=1;char c;
	while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
	while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
	return x*f;
}
int n,k,ans,a[M],b[M];
struct node
{
	int a,b,c;
	bool operator < (const node &r) const
	{
		return c>r.c;
	}
};
bool check(int x)
{
	priority_queue<node> q;
	for(int i=1;i<=n;i++)
		q.push(node{a[i],b[i],a[i]/b[i]});
	for(int i=0;i<k;i++)
	{
		node t=q.top();q.pop();
		if(t.a/t.b<i) return 0;
		if((t.a+x)/t.b<=k)
			q.push(node{t.a+x,t.b,(t.a+x)/t.b});
		if(q.empty()) return 1;
	}
	return 1;
}
void dich(int l,int r)
{
	if(l>r) return ;
	int mid=(l+r)>>1;
	if(check(mid))
	{
		ans=mid;
		dich(l,mid-1);
	}
	else dich(mid+1,r);
}
signed main()
{
	ans=-1;
	n=read();k=read();
	for(int i=1;i<=n;i++)
		a[i]=read();
	for(int i=1;i<=n;i++)
		b[i]=read();
	dich(0,2e12);
	printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/C202044zxy/article/details/107564014