题目链接
题目思路
肯定是二分
问题是如何check才是最优解
就是每次都是走相邻的位置
例如x没走完,那就一直循环走\(x,x+1\)这俩个位置
代码
#include<bits/stdc++.h>
#define fi first
#define se second
#define debug cout<<"I AM HERE"<<endl;
using namespace std;
typedef long long ll;
const int maxn=2e5+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-6;
int n;
ll m;
ll a[maxn];
ll b[maxn];
ll check(ll x){
for(int i=1;i<=n;i++){
b[i]=(x+a[i]-1)/a[i];
}
ll ans=0;
int now=0;
for(int i=1;i<=n;i++){
if(b[i]==0) continue;
ans+=i-now;
ans+=2*(b[i]-1);
b[i+1]-=(b[i]-1);
b[i+1]=max(b[i+1],0ll);
now=i;
}
return ans;
}
signed main(){
int _;scanf("%d",&_);
while(_--){
scanf("%d%lld",&n,&m);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
ll l=1,r=(1e18)/n,ans=0;
while(l<=r){
ll mid=(l+r)/2;
if(check(mid)<=m){
ans=mid;
l=mid+1;
}else{
r=mid-1;
}
}
printf("%lld\n",ans);
}
return 0;
}