CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊
嗯,,先来发水题纪念一下
D1. Magic Powder - 1
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 alln 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 (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), 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 (1 ≤ bi ≤ 1000), 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.
Examples
input
3 12 1 4
11 3 16
output
4
input
4 3
4 3 5 6
11 12 14 20
output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
1、CodeForces 670D1
2、链接:http://codeforces.com/problemset/problem/670/D1
3、总结:
题意,给出n种做一个饼干所要的材料数,n种现有材料数,k个可变化材料,求可做多少饼干。
可暴力,也可直接二分。
小数据直接暴力
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdio>
#define max(a,b) (a>b?a:b)
#define abs(a) ((a)>0?(a):-(a))
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
int main()
{
int n,k;
int a[],b[];
while(scanf("%d%d",&n,&k)!=EOF)
{
for(int i=;i<n;i++)scanf("%d",&a[i]);
for(int i=;i<n;i++)scanf("%d",&b[i]);
int aa,flag=;
int num=;
while(k>=) //k>=0,不要k>0
{
for(int i=;i<n;i++){ //找到个数最小点,标记
if(aa>b[i]/a[i]){
flag=i;
aa=b[i]/a[i];
}
}
//下面更新记录
int bb=a[flag]-b[flag]%a[flag];
if(k<bb)break;
else {
k-=bb;
b[flag]+=bb;
aa=b[flag]/a[flag];
} }
cout<<aa<<endl;
}
return ;
}
大数据二分
参考了http://blog.csdn.net/qiuxueming_csdn/article/details/51471935
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdio>
#define max(a,b) (a>b?a:b)
#define abs(a) ((a)>0?(a):-(a))
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
int n,k;
int a[],b[];
bool ok(LL mid)
{
LL kk=k;
for(int i=;i<n;i++){
if(a[i]*mid>b[i]){
kk-=(a[i]*mid-b[i]); }
if(kk<)return false; //mid太大,跳出; 不能放到上面if里
}
return true; //mid太小,使k有剩余
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
for(int i=;i<n;i++)
scanf("%d",&b[i]);
LL l=,r=INF;
LL num,mid;
while(l<=r)
{
mid=(l+r)>>;
if(ok(mid)){
l=mid+;
num=mid; //num要在这里赋值
}else {
r=mid-;
}
}
cout<<num<<endl;
}
return ;
}
CodeForces 670D1 暴力或二分的更多相关文章
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- Codeforces Round #404 (Div. 2) A,B,C,D,E 暴力,暴力,二分,范德蒙恒等式,树状数组+分块
题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory ...
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A B C D 暴力 水 二分 几何
A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #394 (Div. 2)A水 B暴力 C暴力 D二分 E dfs
A. Dasha and Stairs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 626E Simple Skewness(暴力枚举+二分)
E. Simple Skewness time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces 670D1. Magic Powder - 1 暴力
D1. Magic Powder - 1 time limit per test: 1 second memory limit per test: 256 megabytes input: stand ...
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
- Codeforces 799D. String Game 二分
D. String Game time limit per test:2 seconds memory limit per test:512 megabytes input:standard inpu ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
随机推荐
- Ubuntu / Win7 安装db2 v10.5
抓紧下载v10.5fp1_linuxx64_expc.tar.gz到~/Downloads/java_softcd java_softtar xf v10.5fp1_linuxx64_expc.tar ...
- Linux/centos下安装riak
必备的组件: gccgcc-c++glibc-develmakepam-devel 使用yum安装相关组件 sudo yum install gcc gcc-c++ glibc-devel make ...
- [Tips] JavaScript 使用hash 对象传参
转自Web 前端开发修炼之道. 在JavaScript 中funciton 包含多个参数的时候,我们想要实现可选参数的功能,传很多个null 其实是个很讨厌的事情,这个时候就可以使用这个技巧. 具体见 ...
- hdu 5033 单调栈 ****
看出来是单调栈维护斜率,但是不会写,2333,原来是和询问放在一起的 #include <iostream> #include <cstdio> #include <cs ...
- 1080P、720P、4CIF、CIF所需要的理论带宽
转自:http://blog.sina.com.cn/s/blog_64684bf30101hdl7.html 在视频监控系统中,对存储空间容量的大小需求是与画面质量的高低.及视频线路等都有很大关系. ...
- C语言判断文件是否存在(转)
int access(const char *filename, int amode); amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1. 这个函 ...
- MarkupExtension
目的 如果要在XAML里引用静态或动态对象实例,或在XAML中创建带有参数的类.这时,我们需要用到XAML扩展.XAML扩展常用来设定属性值.使用标识扩展,告诉 XAML 处理不要像通常那样将属性值 ...
- 在Android上用AChartEngine轻松绘制图表
本文由 伯乐在线 - LeonHover 翻译.未经许可,禁止转载!英文出处:jaxenter.欢迎加入翻译组. Android发布不久的2008年底,开发者们已经开始寻找制表.制图.绘图的工具库.当 ...
- Java 中新增的 foreach 的用法
JDK1.5加入的增强for和循环. foreach语句使用总结 增强for(part1:part2){part3}; part2中是一个数组对象,或者是带有泛性的集合. part1定义了一个局部 ...
- json入门(二)
背景 之前最早的时候,也见过类似于这样的字符串: {"list":[ {"ArticleId":7392749,"BlogId&q ...