Codeforces 670D2:Magic Powder - 2(二分)

来源:互联网 发布:js bytearray 遍历 编辑:程序博客网 时间:2024/05/17 00:51
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 100000000011000000000
output
2000000000
input
10 11000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 10000000001 1 1 1 1 1 1 1 1 1
output
0
input
3 12 1 411 3 16
output
4
input
4 34 3 5 611 12 14 20
output
3
题目大意:制作一个蛋糕需要n种材料,然后你有k克魔法粉,每克魔法粉可以代替任意一克的材料,ai代表制作一个蛋糕需要第i种材料多少克,bi代表你拥有第i个材料多少克,问做可以做多少个蛋糕。
解题思路:二分,将饼干数放到judge里面,看是否能生产mid个饼干。
#include <stdio.h>#include <algorithm>using namespace std;#define LL __int64LL need[100010];LL have[100010];LL n,k;bool judge(LL x){LL sheng=k;for(LL i=0;i<n;i++){if(x*need[i]>have[i])//需要魔法粉的 ,(如果不需要魔法粉,说明该材料肯定能弄出x个) {if((have[i]+sheng)/need[i]>=x)//剩下的魔法粉能够给该材料提供足够的材料 {sheng=sheng-(x*need[i]-have[i]);}else{return false;//剩下的不够变了 }}}return true;}int main(){scanf("%I64d%I64d",&n,&k);LL l=0,r=0;for(int i=0;i<n;i++){scanf("%I64d",&need[i]);}for(int i=0;i<n;i++){scanf("%I64d",&have[i]);r=max(r,(have[i]+k)/need[i]);}LL mid; LL ans;while(l<=r){mid=(l+r)/2;if(judge(mid)){ans=mid;l=mid+1;}else{r=mid-1;}}printf("%I64d\n",ans);return 0;}



0 0
原创粉丝点击