Codeforces 670D2 Magic Powder - 2 二分答案

本文介绍了一个有趣的算法问题——如何利用现有食材和魔法粉烘焙尽可能多的饼干。通过二分搜索法找到最大烘焙数量,文章包含完整的C++实现代码。

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.

Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.

Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.

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.

分析:
二分答案即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+9;
ll a[N],b[N];
int n,k;
bool ok(ll x)
{
    ll num=k;
    for(int i=0;i<n;i++){
        ll t=b[i]-x*a[i];
        if(t<0){
            num+=t;
            if(num<0)return 0;
        }
    }
    return 1;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)scanf("%I64d",&a[i]);
    for(int i=0;i<n;i++)scanf("%I64d",&b[i]);
    ll l=0,r=2e9+9;
    while(l<r){
        int m=l+(r-l+1)/2;
        if(ok(m))l=m;
        else r=m-1;
    }
    printf("%d\n",l);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值