您的位置:首页 > 其它

CodeForces - 670D2 Magic Powder - 2(二分)

2017-07-21 12:13 302 查看
点击打开题目链接

D2. Magic Powder - 2

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

The term of this problem is the same as the previous one, the only exception — increased restrictions.

Input

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.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Examples

input
1 1000000000
1
1000000000


output
2000000000


input
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1 1 1 1 1


output
0


input
3 1
2 1 4
11 3 16


output
4


input
4 3
4 3 5 6
11 12 14 20


output
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 二分