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 ...
随机推荐
- django中使用Ajax
内容: 1.Ajax原理与基本使用 2.Ajax发送get请求 3.Ajax发送post请求 4.Ajax上传文件 5.Ajax设置csrf_token 6.django序列化 参考:https:// ...
- tcpdump抓sql语句
-A -n -i any |grep --color 'system_type' -n2 -- -E..,.@.@.f........ ...Ndh-....GP..:A.............. ...
- 网络层-IP地址
以下内容是IPv4 IP地址长度32位,Java里面一个int的长度,总共分为5类IP地址 1:分类编址 A类IP地址0开头: A类有31个位置可以变化,总数是2^31个, [(0 ...
- WINRAR 自解压脚本命令及变量
自解压脚本命令 Path=d:\ ;绝对路径 ;Path=.\在当前文件夹中创建 ;Path=在“Program Files”中创建 ;在当前文件夹创建,无语句 Setup=释放后运行 Presetu ...
- Spring boot Tomcat配置
来自: https://www.cnblogs.com/a8457013/p/7687764.html
- As3.0中的位图(Bitmap/BitmapData)编程
https://blog.csdn.net/wtuetnsrmh/article/details/12577929
- Mysql 知识(3)
1.如何登陆mysql数据库 mysql -u username -p 2.如何开启/关闭mysql服务 service mysql start/stop 3.查看mysql的状态 service m ...
- int和Integer区别
Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper cl ...
- Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?
1.什么是匿名内部类? 内部类,存在于另一个类内部的类,而匿名内部类,顾名思义,就是没有名字的内部类. 2.为什么需要匿名内部类? 每个inner class都能够各自继承某一实现类(implemen ...
- Centos 安装golang beego
刚开始用go和beego,真的还好多不懂~希望对看到的朋友有帮助~ 本人环境:centos 6.3x86_64 1.我在机器上创建了一个用户zww(useradd zww),登录zww(su zww) ...