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 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.

分析:

二分答案即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+9;
ll a[N],b[N];
int n,k;
bool ok(ll x)
{
    ll num=k;
    for(int i=0;i<n;i++){
        ll t=b[i]-x*a[i];
        if(t<0){
            num+=t;
            if(num<0)return 0;
        }
    }
    return 1;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)scanf("%I64d",&a[i]);
    for(int i=0;i<n;i++)scanf("%I64d",&b[i]);
    ll l=0,r=2e9+9;
    while(l<r){
        int m=l+(r-l+1)/2;
        if(ok(m))l=m;
        else r=m-1;
    }
    printf("%d\n",l);
}
时间: 2024-08-19 09:15:48

Codeforces 670D2 Magic Powder - 2 二分答案的相关文章

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 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 (模拟)

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 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 Apollinar

Codeforces 551C GukiZ hates Boxes 二分答案

题目链接 题意: 一共有n个空地(是一个数轴,从x=1 到 x=n),每个空地上有a[i]块石头 有m个学生 目标是删除所有石头 一开始所有学生都站在 x=0的地方 每秒钟每个学生都可以在原地删除一块石头,或者向 → 移动一格距离 问:删除所有石头的最短时间 案例解析: 3 2 1 0 2 第一个学生第一秒向→走,第二秒删a[1]的一块石头 第二个学生一直走到头,删掉a[3] ,所以第二个学生花费是 1+1+1+2 = 5 两个学生可以同时运动. 思路: 二分答案,设答案为x,即需要x秒来搬完石

CodeForces 343C Read Time 【二分答案】+【贪心】

<题目链接> 题目大意: 一条水平的磁道上有n个磁头和m个待扫描的点,磁头可以左右互不干扰的移动去扫描点,每秒移动一个单位(也可以停留在原地),求这些磁头扫描完这些所有的点最少需要要花多少时间. 解题分析: 本题用二分答案和贪心求解,先二分出这些磁头扫描完所有的点所需的时间,然后用贪心策略去模拟每个磁头扫描这些点.对这些磁头从左向右分析,假设二分出的总时间为mid,在该条件下,每个磁头扫描点的最优情况毫无疑问是,在保证扫描到当前 最左边未被扫描到的点的情况下,向右扫描尽可能远的距离(对于这些磁

Codeforces 825D Suitable Replacement - 贪心 - 二分答案

You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters. Suitability of string s is calculated by following metric: Any two letters can be swapped positions, these operations can be performed arbi

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

Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

Digital collectible card games have become very popular recently. So Vova decided to try one of these. Vova has n cards in his collection. Each of these cards is characterised by its power pi, magic number ci and level li. Vova wants to build a deck