题目链接: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系统程序包管理器【rpm、yum】

    将编译好的文件打包成一个或有限的几个文件,可用于实现便捷的安装.卸载.升级.查询,校验等程序管理. centos常用的程序管理器有rpm和yum rpm: redhat package manager ...

  2. Oracle 12c安装详细步骤,带截图

    1,在官网上下载oracle的压缩文件,两个都要下载. 并两个同时选中解压在一个文件夹里面. 2,解压之后,如下图,点击setup.exe稍等一会儿 ,3,开始安装: 不选点击下一步,或者直接点击下一 ...

  3. NopCommerce的定时任务分析和应用

    NOP的定时任务也是群里听群友听说,我很少在WEB端做定时任务,所以尝鲜下,看看效果怎么样. 主要涉及到下面几个类和配置文件配置: web.config <configSections> ...

  4. tiny6410的linux操作系统实验开发

    ---恢复内容开始--- 1.前期由于2440 的4.3寸屏太小.后来修改程序准备在tiny6410增强版的S70屏上用.但是前期移植再用yaffs的文件系统,但是6410是(MLC)的磁盘,根本就不 ...

  5. socket小实例

    服务端 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  6. Gson、FastJson、json-lib对比与实例

    一.各个JSON技术的对比(本部分摘抄自http://www.cnblogs.com/kunpengit/p/4001680.html): 1.json-libjson-lib最开始的也是应用最广泛的 ...

  7. /etc/logrotate.conf

    /etc/logrotate.conf 是 Logrotate 工具的一个配置文件,这个工具用来自动切割系统日志,Logrotate 是基于 cron 来运行的,如下: [root@localhost ...

  8. 嵌入式系统UBOOT

    一个完整的嵌入式linux系统包含4部分内容:Bootloader.Parameters.Kernel.Root File System.3.4.5.6部分详细介绍了这4部分的内容,这是Linux底层 ...

  9. MUI 页面跳转(传值+接收)

    官方:做web app,一个无法避开的问题就是转场动画:web是基于链接构建的,从一个页面点击链接跳转到另一个页面, 如果通过有刷新的打开方式,用户要面对一个空白的页面等待: 如果通过无刷新的方式,用 ...

  10. vue-学习笔记(更新中...)

    vue学习笔记 2017-08-23  11:10:28 Vue实例: var vm = new Vue({ // 选项 }) 实例化Vue.Vue实例,构造函数Vue.创建一个Vue的根实例,Vue ...