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;
}

时间: 2024-08-02 09:27:44

Codeforces 670D. Magic Powder的相关文章

CodeForces - 670D2 Magic Powder - 2 (二分&amp;模拟)

CodeForces - 670D2 Magic Powder - 2 Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description The term of this problem is the same as the previous one, the only exception - increased restrictions. Input Th

CodeForces - 670D1 Magic Powder - 1 (模拟)

CodeForces - 670D1 Magic Powder - 1 Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description This problem is given in two versions that differ only by constraints. If you can solve this problem in large c

Codeforces 670D2 Magic Powder - 2 二分答案

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 Apollinar

codeforces 670D2 - Magic Powder - 2

和前面的那道题一样,就只改了数组的大小和数据类型. #include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; //int a[MAX], b[MAX], c[MAX]; typedef __int64 ll; struct NODE { ll a, b, c; }node[MAX]; ll sum[MAX]; bool comp(NODE a, NODE b) { return a.c < b.c; }

codeforces 670D1 - Magic Powder - 1

题目的意思,就是给出制作一种食物的每种材料所需的量,然后再给出每种材料目前总共的数量,问最多可以制作多少个这样的食物. 贪心.首先求出每种材料是总共有多少个这样的材料,然后由小到大排序,然后再用一个数组存那个后一个大的量的材料减去前面所有小的量的差,因为比如,有3种材料,每种材料的量分别是1, 2,4, 就需要sum[1] = 1, sum[2] = 3,然后就是贪心的计算了. #include <bits/stdc++.h> using namespace std; const int MA

Magic Powder - 1 CodeForces - 670D1

This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you

Magic Powder - 2 (CF 670_D)

http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous one, the only exception — increased restrictions. Input The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the nu

[递推+矩阵快速幂]Codeforces 1117D - Magic Gems

传送门:Educational Codeforces Round 60 – D 题意: 给定N,M(n <1e18,m <= 100) 一个magic gem可以分裂成M个普通的gem,现在需要N个gem,可以选择一定的magic gem,指定每一个分裂或不分裂,问一共有多少种方案 两种分裂方案不同当且仅当magic gem的数量不同,或者分裂的magic gem的索引不同. 思路: 1.首先从dp的角度出发 设F(i)为最终需要i个gem的方案数,容易得到递推式: (总方案数 = 最右边的m

【模拟】Codeforces 710C Magic Odd Square

题目链接: http://codeforces.com/problemset/problem/710/C 题目大意: 构造一个N*N的幻方.任意可行解. 幻方就是每一行,每一列,两条对角线的和都相等. 题目思路: [模拟] 分为奇幻方.单偶幻方和双偶幻方三种构造. 具体分类可以查看百度.幻方的N种构造方法 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algori