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个, 每个特殊原料可以当作任意一个其它原料使用.那么问题来了, ...
随机推荐
- 3D游戏与计算机图形学中的数学方法-点线面
<易传·系辞上传>:”易有太极,是生两仪,两仪生四象,四象生八卦.” 借用一下古代先人们的智慧引一下本文的主题-三维图形中的点线面,在三维几何中也有一句话可以和上面的话相对应:由点成线,由 ...
- otunnel : 一个和lcx差不多的端口转发的工具
项目地址 ooclab/otunnel 下载地址(内涵各大平台) http://dl.ooclab.com/otunnel/ otunnel 用法 前提: 1. 假设 server 的地址为 exam ...
- iOS开发- UILabel 自己主动换行 及 高度自适应
主要是今天看到论坛有人问这个问题.帮忙解决之后, 顺便Mark下. 非常easy, 代码写的非常清楚. 直接上代码. UILabel *label = [[UILabel alloc] initWit ...
- 移动端 touch
原文链接:http://caibaojian.com/mobile-touch-event.html 本文主要介绍 TouchEvent 相关的一些对象与属性如 Touch, TouchList, t ...
- Ubuntu 14.04 Server i386 安装 Oracle11g_11.2.0.3 RAC
文档地址:doc 文档地址:doc
- GIT+ Coding使用方法
1 进入码市 :https://coding.net 注册一个账户 2 创建一个项目: 3 本地window环境.安装git : https://git-scm.com/download/win ...
- [置顶] think in java interview-高级开发人员面试宝典代码示例
下载资源地址为: http://download.csdn.net/detail/lifetragedy/6379755 这是think in java interview中的代码示例,包括JAVA基 ...
- java基础---->多线程之Runnable(一)
java线程的创建有两种方式,这里我们通过简单的实例来学习一下.一切都明明白白,但我们仍匆匆错过,因为你相信命运,因为我怀疑生活. java中多线程的创建 一.通过继承Thread类来创建多线程 pu ...
- codereviw得到的一些经验
1.设置display为none的元素,它的背景图依然会被下载.所以最好是等到该元素需要显示时才给他加上相应的有背景图的class. 2.css中虽然ID选择器的优先级比较高,效率也比较高,但灵活性差 ...
- luogu P1379 八数码难题(A*算法入门详细讲解)
代码实现细节 #include<cstdio> #include<cstring> #include<iostream> using namespace std; ...