CodeForces - 670D2 Magic Powder - 2(二分)

本文介绍了一个关于原料配比与魔法粉末辅助烘焙的问题,通过二分查找算法确定最大可制作饼干数量的方法。该问题涉及到算法设计与实现,特别是二分搜索技巧的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开题目链接

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课魔法粉末,第二行为做一个饼干需要的每种原料数,第三行为每种原料总共有多少克。魔法粉末可以任意加到原料上,求最多可以做多少饼干。做的二分题还是少,当时没想到二分饼干数,一直超时样例。

思路:

二分饼干数求解。

附上AC代码:

#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;
const int maxn=100000+5;
ll n,k;
ll maxd;
ll sum;

struct nodes{
    ll need,have;
}node[maxn];

int main(){
    ios::sync_with_stdio(false);
    while(cin>>n>>k){
        sum=0;
        for(int i=1;i<=n;i++)
            cin>>node[i].need;
        for(int i=1;i<=n;i++){
            cin>>node[i].have;
            maxd=max((node[i].have+k)/node[i].need,maxd);
        }
        ll l=0,r=maxd;
        while(l<=r){
            ll mid=(l+r)/2;
            ll tmp=k;
            ll ans=0;
            for(int i=1;i<=n;i++){
                if(tmp<0){
                    ans=1;
                    break;
                }
                if(node[i].have/node[i].need>=mid)continue;
                else if(node[i].have/node[i].need<mid){
                    tmp-=mid*node[i].need-node[i].have;
                    if(tmp<0){
                        ans=1;
                        break;
                    }
                }
            }
            if(ans==1){//tmp<0,魔法粉末不够用
                r=mid-1;
            }
            else if(ans==0){
                sum=max(sum,mid);
                l=mid+1;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值