The term of this problem is the same as the previous one, the only exception — increased restrictions.
The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.
The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
1 1000000000 1 1000000000
2000000000
10 1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1 1 1 1 1 1 1 1 1 1
0
3 1 2 1 4 11 3 16
4
4 3 4 3 5 6 11 12 14 20
3
题目大意:
有n种原料和k课魔法粉末,第二行为做一个饼干需要的每种原料数,第三行为每种原料总共有多少克。魔法粉末可以任意加到原料上,求最多可以做多少饼干。做的二分题还是少,当时没想到二分饼干数,一直超时样例。
思路:
二分饼干数求解。
附上AC代码:
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=100000+5;
ll n,k;
ll maxd;
ll sum;
struct nodes{
ll need,have;
}node[maxn];
int main(){
ios::sync_with_stdio(false);
while(cin>>n>>k){
sum=0;
for(int i=1;i<=n;i++)
cin>>node[i].need;
for(int i=1;i<=n;i++){
cin>>node[i].have;
maxd=max((node[i].have+k)/node[i].need,maxd);
}
ll l=0,r=maxd;
while(l<=r){
ll mid=(l+r)/2;
ll tmp=k;
ll ans=0;
for(int i=1;i<=n;i++){
if(tmp<0){
ans=1;
break;
}
if(node[i].have/node[i].need>=mid)continue;
else if(node[i].have/node[i].need<mid){
tmp-=mid*node[i].need-node[i].have;
if(tmp<0){
ans=1;
break;
}
}
}
if(ans==1){//tmp<0,魔法粉末不够用
r=mid-1;
}
else if(ans==0){
sum=max(sum,mid);
l=mid+1;
}
}
cout<<sum<<endl;
}
return 0;
}