标签:链接 ref 并且 lld int names ble chef ace
原题链接:Squirrel and chestnut
题意:有\(n\)只松鼠在\(m\)棵树下,每一棵树会在\(t_i\)的时间掉下一颗果子,随后每隔\(p_i\)的时间会再落下一颗,现在\(n\)只松鼠想要尽可能快地获得\(k\)个果实,并且松鼠只能移动一次,求最少时间。
题解:这一道题,很明显能够想到二分答案,判断也不难,所以我就直接放上代码了:
#include <cstdio>
#include <algorithm>
using namespace std;
#define Maxn 100000
#define ll long long
ll a[Maxn+5],b[Maxn+5];
ll c[Maxn+5];
int n,m,k;
ll maxm(ll a,ll b){
return a>b?a:b;
}
bool cmp(ll a,ll b){
return a>b;
}
bool check(ll mid) {
for(int i=0;i<m;i++) {
if(mid<a[i]){
c[i]=0;
}
else{
c[i]=maxm(0,(mid-a[i]+b[i])/b[i]);
}
}
sort(c,c+m,cmp);
ll ans=0;
for(int i=0;i<min(n,m);i++){
ans+=c[i];
if(ans>=k){
return 1;
}
}
return 0;
}
int main(){
int t;
scanf("%d",&t);
ll l,r,mid;
while(t--){
scanf("%d%d%d",&m,&n,&k);
for(int i=0;i<m;i++){
scanf("%lld",&a[i]);
}
for(int i=0;i<m;i++){
scanf("%lld",&b[i]);
}
l=1,r=Maxn-1;
while(l<=r){
mid=(l+r)>>1;
if(check(mid)){
r=mid;
}
else{
l=mid+1;
}
}
printf("%lld\n",l);
}
return 0;
}
CodeChef Squirrel and chestnut 题解
标签:链接 ref 并且 lld int names ble chef ace
原文地址:https://www.cnblogs.com/withhope/p/11141270.html