https://ac.nowcoder.com/acm/contest/911/I
思路:二分答案;
#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define IT set<node>::iterator
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long LL;
typedef long long ll;
const int maxn=3e5+5;
ll a[maxn];
ll b[maxn];
ll c[maxn];
int n;
bool check(int x)//x枚
{
ll res=0;
for(int i=1;i<=n;i++)
c[i]=a[i];
c[1]-=x;
if(c[1]<=0)
return false;
for(int i=2;i<=n;i++)
{
if(c[i]>=c[1])
{
if(c[i]>=b[i]&&b[i]>=c[1])
{
int temp=(c[i]-b[i])+1;
if(temp%b[i]==0)
{
res+=temp/b[i];
c[i]-=temp/b[i]*b[i];
}
else res+=temp/b[i]+1,c[i]-=(temp/b[i]+1)*b[i];
if(c[i]>=c[1])
res++;//比b[i]小 再清空
}
else if(c[i]>=b[i]&&b[i]<c[1])//比c[1]小就好
{
int temp=(c[i]-c[1])+1;
if(temp%b[i]==0)
{
res+=temp/b[i];
c[i]-=temp/b[i]*b[i];
}
else res+=temp/b[i]+1,c[i]-=(temp/b[i]+1)*b[i];
}
else if(c[i]<b[i])
res++;
}
}
//cout<<x<<' '<<res<<' ';
return res<=x;
}
int main()
{
#ifdef ONLINE_JUDGE
FAST_IO;
#endif // ONLINE_JUDGE
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=2;i<=n;i++)
{
cin>>b[i];
}
int left=0,right=a[1];
int ans=-1;
while(left<=right)
{
int mid=(left+right)/2;
if(check(mid))
{
ans=mid;
right=mid-1;
}
else left=mid+1;
}
// if(ans==-1)
// {
// for(int i=0;i<=a[1]/2;i++)
// {
// if(check(i))
// {
// ans=i;
// break;
// }
// }
// }
cout<<ans<<endl;
}