【Codeforces】[670D2]Magic Powder - 2

Problem Description

Loading...

Problem Analysis

Problem summary: making one cake requires n kinds of ingredients, and you have k grams of magic powder; each gram of magic powder can substitute for any one gram of an ingredient. a_i is the amount of the i-th ingredient needed for one cake, b_i is the amount of the i-th ingredient you have. How many cakes can be made?

Check whether mid cakes can be made
If not, shrink the range of mid
and binary search this way

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<algorithm>
using namespace std;
__int64 a[100200],b[100200];
__int64 n,k;
bool judge(__int64 x) {
    __int64 t=k;
    for(int i=0; i<n; i++) {
        if(b[i]+t>=a[i]*x)
            t-=max((__int64)0,a[i]*x-b[i]);
        else
            return false;
    }
    return true;
}
int main() {
    while(scanf("%I64d %I64d",&n,&k)!=EOF) {
        for(int i=0; i<n; i++)
            scanf("%I64d",&a[i]);
        for(int i=0; i<n; i++)
            scanf("%I64d",&b[i]);
        __int64 l=0,r=2000000002,res;
        while(l<=r) {
            __int64 mid=(l+r)/2;
            if(judge(mid)) {
                res=mid;
                l=mid+1;
            } else
                r=mid-1;
        }
        printf("%I64d\n",res);
    }
    return 0;
}

Loading...