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 <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <algorithm>
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+1<r)
    {
        long long mid=((long long)l+r)>>1;
        if (check(mid)) l=mid;
        else r=mid;
    }
    printf("%d",l);
}
int main()
{
    readdata();
    work();
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值