D2 Magic Powder -1/- 2---cf#350D2(二分)
题目链接: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.
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.
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(二分)的更多相关文章
- Codefroces D2. Magic Powder - 2(二分)
http://codeforces.com/problemset/problem/670/D2 http://codeforces.com/problemset/problem/670/D1 time ...
- Magic Powder - 2 (CF 670_D)
http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...
- Codeforces Round #350 (Div. 2) D2. Magic Powder - 2
题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...
- CodeForces 670D2 Magic Powder 二分
D2. Magic Powder - 2 The term of this problem is the same as the previous one, the only exception — ...
- 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 ...
- 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 ...
- Codeforces Round #350 (Div. 2) D1. Magic Powder - 1 二分
D1. Magic Powder - 1 题目连接: http://www.codeforces.com/contest/670/problem/D1 Description This problem ...
- Codeforces 670D1. Magic Powder - 1 暴力
D1. Magic Powder - 1 time limit per test: 1 second memory limit per test: 256 megabytes input: stand ...
- CodeForces 670D2 Magic Powder - 2 (二分)
题意:今天我们要来造房子.造这个房子需要n种原料,每造一个房子需要第i种原料ai个.现在你有第i种原料bi个.此外,你还有一种特殊的原料k个, 每个特殊原料可以当作任意一个其它原料使用.那么问题来了, ...
随机推荐
- vc 找到一个或多个多重定义的符号
vc 找到一个或多个多重定义的符号, 这个问题还是不能很好的解决. 最根本的是: 把所有有关定义的部分都放在.cpp文件中,对应的.h文件中只放声明.这样在#include ""的 ...
- oracle中LAG()和LEAD()以及over (PARTITION BY)
LAG()和LEAD()统计函数可以在一次查询中取出同一字段的前N行的数据和后N行的值.这种操作可以使用对相同表的表连接来实现,不过使用LAG和 LEAD有更高的效率.以下整理的LAG()和LEAD( ...
- oracle解决多表关联分组查询问题
做了一个功能需要分组查询,同时查询A表分组查询的ID需要关联B表的数据,本来想两个表关联查询,但是报group by 语法不正确.所以做了以下修改. select count(*), cindexid ...
- java socket通信-传输文件图片--传输图片
ClientTcpSend.java client发送类 package com.yjf.test; import java.io.DataOutputStream; import java.io ...
- 工作流JBPM_day02:3-预定义的活动1_4-预定义的活动2+在图片上高亮显示正在执行的上活动
工作流JBPM_day02:3-预定义的活动1 工作流JBPM_day02:4-预定义的活动2+在图片上高亮显示正在执行的上活动 活动 Activity 预先定义好的活动 Start开始活动 End结 ...
- 系统启动日志:/var/log/boot.log
/var/log/boot.log — 记录系统启动时的日志信息,如果系统启动之后有什么异常可以查看该文件信息 [root@localhost ~]# cat /var/log/boot.log # ...
- Python 数据类型:列表
一.列表介绍 1. 列表可以存储一系列的值,使用中括号来定义,每个元素之间用逗号隔开,形如 ['a', 'b', 'c', 'd']2. 列表与元组的区别是:列表中的元素是可变的,元组中的元素是不可变 ...
- Windows 下安装 Python3
可以使用两种方式安装 Python3,一种是直接去官网下载安装包,然后进行安装即可:另一种是通过安装 Anaconda 来安装 Python3, Anaconda 提供了 Python 的科学计算环境 ...
- Mybatis中的foreach
<delete id="deleteByParam"> DELETE FROM YZ_SECURITIES_CURRENCY WHERE ID IN <forea ...
- 控制input框的内容输入为数字
<script> function toNum(v) { return v.replace(/[^\d.]/g, '').replace(/^\./g, "").rep ...