Magic Powder CodeForces - 670D2 二分

Magic Powder CodeForces - 670D2 二分

传送门:https://codeforces.com/contest/670/problem/D2

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n n n ingredients, and for each ingredient she knows the value a i a_i 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 b i b_i bi gram of the i − t h i-th ith ingredient. Also she has k k k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 1 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 of the input contains two positive integers n n n and k ( 1   ≤   n ,   k   ≤   1000 ) k (1 ≤ n, k ≤ 1000) k(1n,k1000) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a 1 ,   a 2 ,   . . . ,   a n ( 1   ≤   a i   ≤   1000 ) a_1, a_2, ..., a_n (1 ≤ a_i ≤ 1000) a1,a2,...,an(1ai1000), where the i − t h i-th ith number is equal to the number of grams of the i − t h i-th ith ingredient, needed to bake one cookie.

The third line contains the sequence b 1 ,   b 2 ,   . . . ,   b n ( 1   ≤   b i   ≤   1000 ) b_1, b_2, ..., b_n (1 ≤ b_i ≤ 1000) b1,b2,...,bn(1bi1000), where the i − t h i-th ith number is equal to the number of grams of the i − t h i-th ith 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.

题意:
Apollinaria 想要做一些饼干,已知做一个饼干需要 n n n种材料,每种材料需要 a i a_i ai克,我们现在拥有每种材料 b i b_i bi克,和 k k k克万能材料,万能材料可以补充任何一种材料,现问Apollinaria最多可以做多少饼干。

代码:

#include<iostream>
using namespace std ;
#define ll long long
const ll N = 1e5+9 ;
void updata( ll &a , ll b ){if(b>a)a=b;}
ll a[ N ] , b[ N ] , n , k ;
bool check( ll sum ){
    ll kk = k ; //存目前可用的万能材料还剩多少克
    for( int i = 1 ; i <= n ; i ++ ){
        ll ned = a[ i ]*sum ;//ned表示做sum个饼干,第i种材料一共需要多少克
        if( b[ i ] >= ned ) continue ; //第i种材料足够做sum个饼干,continue
        else if( b[ i ] + kk >= ned ){  kk -= (ned-b[i]) ; }//材料不足用万能材料补充
        else { return 0 ;  }
    }
    return 1 ;
}
int main(){
   cin >> n >> k ;
   for( int i = 1 ; i <= n ; i ++ ) cin >> a[ i ] ;
   for( int i = 1 ; i <= n ; i ++ ) cin >> b[ i ] ;
   ll l , r , mid , ans =-1 ;
    l = 0 , r = 3e9 ;
   while( l <= r ){
        mid = l + r >> 1 ;
        ll f = check( mid ) ;
        //cout << "mid=" << mid << " f=" << f << " l=" << l << " r=" << r << "\n" ;
        if( f ) { l = mid + 1 ; updata( ans , mid ) ; }
        else r = mid - 1 ;
   }
    cout << ans << "\n" ;
return 0 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值