Codeforces Round #350 (Div. 2) D Magic Powder (二分)

题意:

做一个东西需要n种材料,第i种材料需要 a[i] 个,现在每个材料有 b[i] 个,现在有k个魔法材料,这种材料能一比一的转化为任意一种材料,问最多做多少个

思路:

二分个数即可,复杂度o( nlogn ),记得在判断的时候小心爆long long

错误及反思:

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn  = 100010;
long long ori[maxn];
long long hav[maxn];
int n,k;
bool judge(long long x)
{
    long long now=1ll*k;
    for(int i=0;i<n;i++)
    {
        if(hav[i]/ori[i]>=x)
            continue;
        else
        now-=1ll*x*ori[i]-hav[i];
        if(now<0) return false;
    }
    return true;
}
int main(){
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
        scanf("%I64d",&ori[i]);
    for(int i=0;i<n;i++)
        scanf("%I64d",&hav[i]);
    long long l=0;
    long long r=2000000000;
    while(l<=r)
    {
        long long m=(l+r)/2;
        if(judge(m)) l=m+1;
        else r=m-1;
    }
    printf("%I64d\n",r);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值