题目链接: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. Onject.Instantiate实例

    该函数有两个函数原型: Object Instantiate(Object original,Vector3 position,Quaternion rotation); Onject Instant ...

  2. linux nfs

    linux(十四)之linux NFS服务管理 学到这里差不多就结束了linux的基础学习了,其实linux的内容并不难,我们要经常的反复的去操作它,多多和它去联络感情才能很好的掌握这个linux. ...

  3. sudo 之后 unable to resolve host的问题解决办法

    gedit /etc/hosts #127.0.0.1 localhost #127.0.0.1 Masterback或者其他 把后面的Masterback 或者其他改成新的主机名,应该是最近修改过主 ...

  4. LoadRunner 调用dll方法

    本文主要介绍简单DLL的编写方法及在LoadRunner中局部调用与全局调用DLL方法. 1.动态链接库的编写 在Visual C++6.0开发环境下,打开FileNewProject选项,可以选择W ...

  5. Jquery实现$.fn.extend和$.extend函数

    $.fn.extend(   {      method:function(){   }   })   $.extend(   {      method:function(){   }   })

  6. 这款Office密码破解工具,无坚不摧!

    你是否曾经陷入过这样的尴尬:因为忘记Word文档密码去找了一个Word密码破解工具,接着又忘记Excel文档密码去找了一个专门破击Excel的工具,那么如果忘记PowerPoint.Outlook.P ...

  7. Asp.net 程序优化js,css合并与压缩

    访问时将js和css压缩并且缓存在客户端,采用的是Yahoo.Yui.Compressor组件还完成的,从这里可下载 创建一个IHttpHandler来处理文件 ) }; )              ...

  8. win10取消开机密码

    WIN10开机以后点击一下[开始]然后面它的搜索栏处输入[netplwiz]就可以看到如下图所示提示.   进入到用户长户界面,把[要使用本计算机,用户必须输入用户名和密码]前面的那个勾勾取消了,再点 ...

  9. C# Smtp方式发送邮件

    //简单邮件传输协议类             System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();        ...

  10. swift - UIImageView 的使用

    1.创建 var imageView = UIImageView()//初始化 2.图片的显示及图片的改变 imageView = UIImageView(image: UIImage(named: ...