zoukankan      html  css  js  c++  java
  • Codeforces 670D. Magic Powder

    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 of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.

    The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), 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 ≤ 1000), 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.

    这题还有大一点数据的版本,不过都是一样的

    二分答案,每一次判断是O(n)的很好写

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    #define SIZE 100005
    #define MAXS 2000000005
    LL n,k;
    LL a[SIZE],b[SIZE];
    
    
    bool ok(LL now){
      LL remain = k,oks = 1;
      for (int i=1;i<=n;i++){
        if (a[i] * now > b[i]){
          remain -= (a[i] * now - b[i]);
        }
        if (remain < 0){
          oks = 0; break;
        }
      }
    
      return oks;
    }
    
    LL search(LL l,LL r){
      // cout << l << " " << r << endl;
      if (l == r){
        return l;
      }
      if (l+1 == r){
        if (ok(r)){
          return r;
        }
        return l;
      }
    
      LL mid = l + (r - l) / 2;
      if (ok(mid)){
        return search(mid,r);
      }
      else
        return search(l,mid);
    }
    
    int main()
    {
      // freopen("test.in","r",stdin);
      ios::sync_with_stdio(false);
    
      cin >> n >> k;
    
      for (int i=1;i<=n;i++){
        cin >> a[i];
      }
      for (int i=1;i<=n;i++){
        cin >> b[i];
      }
    
      LL ans = search(0,MAXS);
      cout << ans;
      return 0;
    }
    View Code
  • 相关阅读:
    通过PHP类的反射来实现依赖注入
    hyperf 基础篇
    composer install 遇到 lock 文件 warning
    laravel carbon 怎么计算两个时间的差?
    laravel 属性类型转换$casts
    Java Web安全之代码审计
    【代码审计】————2、aspx代码审计-2
    【代码审计】————1、aspx代码审计-1
    【逻辑漏洞技巧拓展】————9、业务逻辑漏洞探索之上传漏洞
    【逻辑漏洞技巧拓展】————8、业务逻辑漏洞探索之敏感信息泄露
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7469407.html
Copyright © 2011-2022 走看看