题目链接:http://codeforces.com/contest/670/problem/D2

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 can write solution to the simplified version only.

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  — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an , 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 , 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.

题意:有n中材料,每种材料有b克,他想做饼干,做1个饼干需要每种材料ai克,现在有k克魔法粉,这k克魔法粉可以变成任意一种材料,求最终最多做多少个饼干;

有两道题,一个所有数据范围是10^3,这个的话我们可以直接暴力解决;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 2011
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f int a[N], b[N], n, k; int main()
{
while(scanf("%d %d", &n, &k)!=EOF)
{
met(a, ); met(b, ); for(int i=; i<=n; i++)
scanf("%d", &a[i]); int Min = INF, sum = ; for(int i=; i<=n; i++)
{
scanf("%d", &b[i]);
Min = min(Min, b[i]/a[i]);
}
for(int i=; i<=n; i++)
{
b[i] = b[i] - a[i]*Min;
if(b[i]<a[i])
sum += a[i]-b[i];
}
while(sum<=k)
{
Min ++;
k -= sum;
sum = ;
for(int i=; i<=n; i++)
{
b[i] -= a[i];
if(b[i]<)b[i] = ;
if(b[i]<a[i])
sum+=a[i]-b[i];
}
}
printf("%d\n", Min);
}
return ;
}

另一个所有数据范围变成10^9时我们可以想到,他最多做不超过2*(10^9)个饼干,所以我们可以二分搜索答案,一直到找到符合题意de饼干个数为止;

注意中间过程会爆int的所以用long long

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std;
typedef long long LL;
#define N 200100
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
const int MAXN = *(1e9)+; LL a[N], b[N], k;
int n; LL Search(LL L, LL R)
{
while(L <= R)
{
LL x = (L+R)/; LL sum = ; for(int i=; i<=n; i++)
{
if( b[i] < a[i]*x )
sum += (a[i]*x) - b[i];
if(sum > k)///防止爆long long;
break;
}
if(sum == k)///当刚好满足题意时,返回;
return x;
else if(sum < k)
L = x + ;
else
R = x -;
}
return L-;
} int main()
{
while(scanf("%d %I64d", &n, &k)!=EOF)
{
met(a, ); met(b, ); for(int i=; i<=n; i++)
scanf("%I64d", &a[i]); for(int i=; i<=n; i++)
scanf("%I64d", &b[i]); LL ans = Search(, MAXN-);
printf("%I64d\n", ans);
}
return ;
}

D2 Magic Powder -1/- 2---cf#350D2(二分)的更多相关文章

  1. Codefroces D2. Magic Powder - 2(二分)

    http://codeforces.com/problemset/problem/670/D2 http://codeforces.com/problemset/problem/670/D1 time ...

  2. Magic Powder - 2 (CF 670_D)

    http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...

  3. Codeforces Round #350 (Div. 2) D2. Magic Powder - 2

    题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...

  4. CodeForces 670D2 Magic Powder 二分

    D2. Magic Powder - 2 The term of this problem is the same as the previous one, the only exception — ...

  5. codeforces 350 div2 D Magic Powder - 2 二分

    D2. Magic Powder - 2 time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #350 (Div. 2)_D2 - Magic Powder - 2

    D2. Magic Powder - 2 time limit per test 1 second memory limit per test 256 megabytes input standard ...

  7. Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分

    D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...

  8. Codeforces 670D1. Magic Powder - 1 暴力

    D1. Magic Powder - 1 time limit per test: 1 second memory limit per test: 256 megabytes input: stand ...

  9. CodeForces 670D2 Magic Powder - 2 (二分)

    题意:今天我们要来造房子.造这个房子需要n种原料,每造一个房子需要第i种原料ai个.现在你有第i种原料bi个.此外,你还有一种特殊的原料k个, 每个特殊原料可以当作任意一个其它原料使用.那么问题来了, ...

随机推荐

  1. CentOS查看登录用户以及踢出用户

    查看登录用户,使用w命令 [root@lnmp ~]# w 18:51:18 up 35 min,  2 users,  load average: 0.00, 0.00, 0.00 USER     ...

  2. asp.net mvc webconfig配置文件操作

    读取web.config数据,可以不用编译.如发布后,非常有用web.config文件<configuration> <appSettings> <add key=&qu ...

  3. c++ ++i and i++

    ++i 在 i 存储的值上增加一并向使用它的表达式 ``返回" 新的, 增加后的值; 而 i++ 对 i 增加一, 但返回原来的是未增加的值. 至于++i和i++有什么区别,举个例子 1.a ...

  4. c++ list erase()

    STL中的容器按存储方式分为两类,一类是按以数组形式存储的容器(如:vector .deque):另一类是以不连续的节点形式存储的容器(如:list.set.map).在使用erase方法来删除元素时 ...

  5. Oracle收购Apiary来加强其API集成云

        Oracle宣布计划于1月19日收购Apiary,一家专注于API设计和协作的API管理公司.Apiary最为人所知的是API flow,其API管理平台.     Oracle并没有宣布计划 ...

  6. css之导航菜单的制作

    通过设置<a>的背景改变样式,通过a:hover改变交互效果,改变文字颜色color 纵向 <!DOCTYPE html> <html> <head lang ...

  7. nuget修改配置文件

    https://www.cnblogs.com/seejoy/p/8093837.html 然后将文件解压到需要打包的工程解决方案根目录下. 然后修改nuget文件夹下的 UploadNupkg.ex ...

  8. 关于截取字符串substr和substring两者的区别

    https://blog.csdn.net/adley_function/article/details/52130762 substr和substring两个都是截取字符串的. 两者有相同点,如果只 ...

  9. Effective C++ Item 15 Provide access to raw resources in resource-managing classes

    In last two item, I talk about resource-managing using RAII, now comes to the practical part. Often, ...

  10. Python 入门(八)切片

    对list进行切片 取一个list的部分元素是非常常见的操作.比如,一个list如下: >>> L = ['Adam', 'Lisa', 'Bart', 'Paul'] 取前3个元素 ...