cf 670D2 Magic Powder - 2 二分裸题

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
#include
#include
#include
#include
#include
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规划的题

你可能感兴趣的:(acm,二分)