CF Dima and Salad 01背包
1 second
256 megabytes
standard input
standard output
Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something.
Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal k. In other words, , where aj is the taste of the j-th chosen fruit and bj is its calories.
Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem — now the happiness of a young couple is in your hands!
Inna loves Dima very much so she wants to make the salad from at least one fruit.
The first line of the input contains two integers n, k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10). The second line of the input contains n integersa1, a2, ..., an (1 ≤ ai ≤ 100) — the fruits' tastes. The third line of the input contains n integers b1, b2, ..., bn (1 ≤ bi ≤ 100) — the fruits' calories. Fruit number i has taste ai and calories bi.
If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer — the maximum possible sum of the taste values of the chosen fruits.
3 2
10 8 1
2 7 1
18
5 3
4 4 4 4 4
2 2 2 2 2
-1
In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition fulfills, that's exactly what Inna wants.
In the second test sample we cannot choose the fruits so as to follow Inna's principle.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int INF =; int dp1[];
int dp2[];
int a[];
int b[];
int Max(int x,int y)
{
return x>y? x:y;
}
void solve(int n,int m)
{
int i,j,k,hxl=-;
for(i=;i<=n;i++)
{
k=a[i]-b[i]*m;
if(k>)
{
for(j=;j>=k;j--)
{
dp1[j]=Max(dp1[j],dp1[j-k]+a[i]);
}
}
else
{
for(j=;j>=-k;j--)
{
dp2[j]=Max(dp2[j],dp2[j+k]+a[i]);
}
}
}
for(j=;j>=;j--)
{
k=dp1[j]+dp2[j];
if(k>hxl)
hxl=k;
}
if(hxl==)printf("-1\n");
else printf("%d\n",hxl);
}
int main()
{
int n,m;
int i;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
scanf("%d",&b[i]);
for(i=;i>=;i--)
{
dp1[i]=-INF;
dp2[i]=-INF;
}
dp1[]=;
dp2[]=;
solve(n,m);
}
return ;
}
CF Dima and Salad 01背包的更多相关文章
- CF#214 C. Dima and Salad 01背包变形
C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...
- CodeForces - 366C Dima and Salad (01背包)
题意:n件东西,有属性a和属性b.要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\).在这个条件下,问\(\sum a_j\)最大是多少. 分析:可以将其转化为0 ...
- Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Dima and Salad(完全背包)
Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces 366C Dima and Salad:背包dp
题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...
- CodeForces-366C Dima and Salad 对01背包的理解 多个背包问题
题目链接:https://cn.vjudge.net/problem/CodeForces-366C 题意 给出n个水果和一个常数k,其中每个水果都有两种性质ai, bi(美味度,卡路里量). 要保证 ...
- Codeforces Round #214 (Div. 2) C. Dima and Salad 背包
C. Dima and Salad Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to ...
- codeforces-214(Div. 2)-C. Dima and Salad+DP恰好背包花费
codeforces-214(Div. 2)-C. Dima and Salad 题意:有不同的沙拉,对应不同的颜值和卡路里,现在要求取出总颜值尽可能高的沙拉,同时要满足 解法:首先要把除法变成乘法, ...
- Codefroces 366 C Dima and Salad(dp)
Dima and Salad 题意:一共有n种水果,每种水果都有一个ai, bi,现求一个最大的ai总和,使得ai之和/对应的bi之和的值等于K. 题解:将bi转换成偏移量,只要偏移到起点位置,就代表 ...
随机推荐
- 【bzoj5084】 hashit(广义SAM+set)
题面 传送门 题解 后缀平衡树是个啥啊我不会啊-- 那么我们来考虑一下\(SAM\)的做法好了 不难发现它的本义是要我们维护一棵\(trie\)树,并求出\(trie\)树上每一个节点到根的这段串的不 ...
- linux密码修改实验
1.在单用户模式下进行引导 在不同的运行级别中,一个重要的运行级别就是单用户模式(运行级别1),该模式中,只有一个系统管理员使用特定的机器,而且尽可能少地运行系统服务,其中包含登录.单用户模式对少数管 ...
- jqury属性操作,特殊效果
一. 常用属性操作 1.html() 取出或设置html内容 // 取出html内容 var $htm = $('#div1').html(); // 设置html内容 $('#div1').html ...
- h5聊天工具的开发过程及思路
这个产品的主要技术栈有,网易nim即时通信,vue-cli,muse-ui 1.在拿到这个需求时,脑袋里空的,什么想法都没有,完全懵逼,进了网易云通信的官网api查看,由于我做的是客户端的,所以重点看 ...
- weex 编译vue成js
cd 项目 1.开发 npm run web 开发过程中可以直接使用浏览器运行 npm run ios 2.打包 npm run build 会在 dist 文件夹 中生成js文件, 即可拖入ios ...
- Shell检查程序是否正常,并显示出程序启动时间、执行时间
项目中多个实例,而每个实例均启动一个程序,本脚本将多个程序统一进行监控,并支持文件配置 #!/bin/sh bin=$(cd ``;pwd) cd ${bin} ### 定义检查函数 chkProgr ...
- PZ73H-PZ73X刀闸阀厂家,PZ73H-PZ73X刀闸阀价格 - 专题栏目 - 无极资讯网
无极资讯网 首页 最新资讯 最新图集 最新标签 搜索 PZ73H-PZ73X刀闸阀 无极资讯网精心为您挑选了(PZ73H-PZ73X刀闸阀)信息,其中包含了(PZ73H-PZ73X刀闸阀)厂家,( ...
- Oracle DMP
通过DMP对Oracle数据库进行导入导出 打开“开始”--->输入cmd,打开cmd命令窗口,输入以下命令即可 1导出 (1)将数据库ORACLE完全导出,用户名system密码manager ...
- 04-oracle时间函数
add_months(sysdate,x)x月之后的日期:last_day(sysdate)指定日期所在月份的最后一天:next_day(sysdate,'星期x')当前日期后的下一个星期x: mon ...
- [Android]Caused by: java.lang.IllegalArgumentException: Service not registered.md
Caused by: java.lang.IllegalArgumentException: Service not registered: org.diql.aidldemo.MainActivit ...