codeforces 670D2

D2. Magic Powder - 2
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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克魔法粉,每份魔法粉能能当一份任一材料,现在已知做一个蛋糕需要a1,a2…an份材料,你有b1,b2…bn份材料,问你最多能做出多少个蛋糕.
分析:二分答案,l=0,r=max((a[i]+k)/b[i])(可以更优化,但没必要)

#include 
#include 
#include 
#include 
#include 
using namespace std;
long long a[100010],k,maxi,n,b[100010];
void readdata()
{
    scanf("%d%d",&n,&k);
    for (long long i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for (long long i=1;i<=n;i++)
    {
        scanf("%d",&b[i]);
        maxi=max(maxi,(b[i]+k)/a[i]+1);
    }
}
bool check(long long x)
{
    long long z=k;
    for (long long i=1;i<=n;i++)
    {
        long long w=b[i]-a[i]*x;
        if (b[i]-a[i]*x<0)
        {
            z=z+w;
        }
        if (z<0) return false;
    }
    return true;
}
void work()
{
    long long l=0;
    long long r=maxi;
    while (l+1long long mid=((long long)l+r)>>1;
        if (check(mid)) l=mid;
        else r=mid;
    }
    printf("%d",l);
}
int main()
{
    readdata();
    work();
}

重要的东西放最后 :越界!!!(wa了别怪我)

你可能感兴趣的:(二分,制杖题)