codeforces670D2 Magic Powder - 2 (二分)

题目链接 http://codeforces.com/problemset/problem/670/D2

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克魔法粉,每克魔法粉可以代替任意一克的材料,ai代表制作一个蛋糕需要第i种材料多少克,bi代表你拥有第i个材料多少克,问做多可以做多少个蛋糕。

思路:二分查找可以制作多少个蛋糕,假如可以制作,那么每一种材料都必须充足。


直接上代码:

#include   
using namespace std;  
__int64 a[111111],b[111111];  
const int maxn= 2 * 1e9 + 2;         //最多可以做这么多个蛋糕  
__int64 n,k;  
__int64 search(__int64 l,__int64 r)  
{    
    __int64 mid,sum;    
    while(l <= r)  
    {    
        mid = ( l + r) / 2 ,sum = 0;    
        for (int i = 0 ; i < n ; i++ )       //遍历每种材料是否满足  
        {    
            sum += a[i] * mid - b[i] > 0 ? a[i] * mid - b[i] : 0 ;    
            if( sum > k)  
                break;   
        }    
        if(sum == k)  
            return mid;    
        else if(sum < k)  
            l = mid + 1;    
        else   
            r = mid - 1;    
    }    
    return r;    
}   
int main()   
{  
      
    while (~scanf("%I64d %I64d",&n,&k))  
    {  
        for (int i = 0 ; i < n ; i++)  
        {  
            scanf("%I64d",&a[i]);  
        }  
        for (int i = 0 ; i < n ; i++ )  
        {  
            scanf("%I64d",&b[i]);  
        }  
        printf("%I64d\n",search(1,maxn));  
    }  
    return 0;  
}  


你可能感兴趣的:(CF)