Codeforces 670D1. Magic Powder - 1 暴力
1 second
256 megabytes
standard input
standard output
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 (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.
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.
3 1
2 1 4
11 3 16
4
4 3
4 3 5 6
11 12 14 20
3
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.
题目链接:http://codeforces.com/problemset/problem/670/D1
题意:做一个饼干需要有n种材料,每种材料需要ai克。现在每种材料有bi克。还有k克神奇材料,可以代替那n种材料。1克神奇材料替换1克普通材料。
#include<bits/stdc++.h>
using namespace std;
struct ingredient
{
int a,b,num,sign;
} gg[];
int add[];
int cmp(ingredient x,ingredient y)
{
if(x.num!=y.num) return x.num<y.num;
else return x.sign<y.sign;
}
int main()
{
int i,j,n,k;
scanf("%d%d",&n,&k);
for(i=; i<=n; i++)
scanf("%d",&gg[i].a);
for(i=; i<=n; i++)
{
scanf("%d",&gg[i].b);
gg[i].num=gg[i].b/gg[i].a;
gg[i].sign=gg[i].b%gg[i].a;
}
sort(gg+,gg+n+,cmp);
gg[n+].num=gg[n].num+;
gg[n+].sign=;
int sum=,flag=;
add[]=;
for(i=; i<=n; i++)
{
sum+=gg[i].a;
flag+=gg[i].sign;
if(gg[i].num<gg[i+].num)
{
add[i]=add[i-]+sum*(gg[i+].num-gg[i].num)-flag;
if(add[i]>=k)
{
k-=add[i-];
cout<<gg[i].num+(k+flag)/sum<<endl;
break;
}
flag=;
}
else add[i]=add[i-];
}
if(i>n)
{
k-=add[n];
cout<<gg[n+].num+k/sum;
}
return ;
}
每次增加num就进行sort排序:
#include<bits/stdc++.h>
using namespace std;
int a[],b;
struct ingredient
{
int a,b,num;
} gg[];
int cmp(ingredient x,ingredient y)
{
return x.num<y.num;
}
int main()
{
int i,n,k;
scanf("%d%d",&n,&k);
for(i=; i<n; i++)
scanf("%d",&gg[i].a);
for(i=; i<n; i++)
{
scanf("%d",&gg[i].b);
gg[i].num=gg[i].b/gg[i].a;
}
sort(gg,gg+n,cmp);
while(k>)
{
int sign=(gg[].num+)*gg[].a;
if((sign-gg[].b)<=k)
{
k-=(sign-gg[].b);
gg[].b=sign;
gg[].num++;
}
else
{
gg[].b+=k;
k=;
}
sort(gg,gg+n,cmp);
}
cout<<gg[].num<<endl;
return ;
}
Codeforces 670D1. Magic Powder - 1 暴力的更多相关文章
- CodeForces 670D2 Magic Powder 二分
D2. Magic Powder - 2 The term of this problem is the same as the previous one, the only exception — ...
- Codeforces 632F - Magic Matrix(暴力 bitset or Prim 求最小生成树+最小瓶颈路)
题面传送门 开始挖老祖宗(ycx)留下来的东西.jpg 本来想水一道紫题作为 AC 的第 500 道紫题的,结果发现点开了道神题. 首先先讲一个我想出来的暴力做法.条件一和条件二直接扫一遍判断掉.先将 ...
- CodeForces 670D Magic Powder
二分. 二分一下答案,然后验证一下. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cst ...
- CodeForces 670D2 Magic Powder - 2 (二分)
题意:今天我们要来造房子.造这个房子需要n种原料,每造一个房子需要第i种原料ai个.现在你有第i种原料bi个.此外,你还有一种特殊的原料k个, 每个特殊原料可以当作任意一个其它原料使用.那么问题来了, ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- 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 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 350 div2 D Magic Powder - 2 二分
D2. Magic Powder - 2 time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Magic Powder - 2 (CF 670_D)
http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...
随机推荐
- Docker-compose 在up之后,各个子服务容器的hosts文件中不能找到父服务的域名
使用命令查看docker当前建立的网络: docker network ls 发现docker-compose up确实建立了网络xx_default 使用命令查看该网络详细信息: docker in ...
- Kafka 基本原理
Kafka 基本原理 来源:阿凡卢 , www.cnblogs.com/luxiaoxun/p/5492646.html 简介 Apache Kafka是分布式发布-订阅消息系统.它最初由Link ...
- Data Provider 中没有.net framework Data provider for Mysql 的解决方法
近来做的一个项目中,数据库用的是 MySql, 而在项目使用 Entity Data Model 来做数据服务层,可是在项目中添加 Data Entty Model 时,一般我们都会选择从数据库中直接 ...
- 代码生成器 CodeSmith 的使用(一)
由于在项目中经常要会用到数据库的 CRUD 操作(增.删.改.查),而且还使用的是orm 框架将数据库表名和表中的的字段映射成相应的类属性.如果把大量的时间用到手工输入数据库表中的字段,除了能练习打字 ...
- pure框架
内容: 1.介绍与入门 2.基础使用 参考资料: pure中文文档:https://www.purecss.cn/ pure实例:https://www.purecss.cn/layouts.html ...
- robot framework取出列表子元素
取出嵌套列表变量的子元素 ${list}型列表: ${list} = [["A1", "first"], ["A2", "seco ...
- clientdataset 用法
http://www.360doc.com/content/10/0709/01/2071424_37769962.shtml
- jemalloc for mysql
ptmalloc 是glibc的内存分配管理 tcmalloc 是google的内存分配管理模块 jemalloc 是BSD的提供的内存分配管理 三者jemalloc和tcmalloc的性能不分伯仲, ...
- Python 3 学习笔记(2)
输入输出 str() 函数人类可读,repr() 解释器可读 rjust() 靠右,ljust() 靠左,center() 居中.zfill() 填0. 名字空间 名字到对象的映射关系被称为名字空间. ...
- 不用登陆密码也能进路由器,适用于TP、磊科、腾达
结合wooyun提供的腾达COOKIE漏洞,结合自己的经验,成功进入腾达路由器破解其登陆密码和无线密码. 教程开始: 所用工具:WebCruiser 输入路由网关,出现登陆界面. 选择:COOKIE ...