cf 670D2 Magic Powder - 2 二分裸题

1 篇文章 0 订阅

D2. Magic Powder - 2
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

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.

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种原料,对于原料i需要a[i],现在已有b[i]的i原料,同时Apollinar还有k的万用原料,万用原料能变成任意一种原料。现在让你求出最多能制作多少饼干
思路:由于数据量较大,暴力肯定行不通,二分答案+判断可行性,注意细节即可

代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
int main()
{
    long long mid,left=0,right=2000000000;
    int i,j,k,m,n;
    int a[100005],b[100005];
    scanf("%d%d",&n,&k);
    for(i=1;i<=n;i++)scanf("%d",&a[i]);
    for(i=1;i<=n;i++)scanf("%d",&b[i]);
    int flag=1;
    long long ans=0; 
    while(left<=right)
    {   long long k1=k;
        mid=(left+right)/2;
        int flag1=1;
        for(i=1;i<=n;i++)
        {  
           if((a[i]*mid>b[i]))
          {if(k1-(a[i]*mid-b[i])>=0)k1=k1-a[i]*mid+b[i];
           else
           {
            flag1=0;
            break;
            }
          }

        }
        if(flag1==1)
        { ans=max(ans,mid);
          left=mid+1;
         }
        else right=mid-1;
    }
    printf("%I64d\n",ans);
    return 0;
}

突然想起来上学期的迎新赛也有一道二分题,跟这个差不多,当时不知道怎么的就水过去了,ac之后都不知道是二分……
又想起来了郏老大的01分数规划……下次有机会应该再做几个01规划的题

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值