Dima and Salad(完全背包)
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 integers a1, 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 numberi 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.
题解:
做水果色拉,有n种水果,水果有两个属性:味道和卡路里,选一定水果,让求使得味道和除以卡路里和为k的时候的味道和的最大值;
刚开始我的想法是弄一个完全背包,可以得到多种组合;判断bag[j] / j == k即可;但是当味道和一定的时候可能存在多个卡路里和值,必然会对下面的数据造成影响;
看了大神的,是转化了下:c =a -b*k;可以转化成两个背包;正数存一个,负数存一个,以免造成当味道和一定的时候可能存在多个卡路里和值的情况;
ac
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void getdata(ref int x) {
x = ;
int t;
while (true) {
t = Console.Read();
if (t >= '' && t <= '') break;
}
while (true) {
if (t < '' || t > '') break;
x = x * + t - '';
t = Console.Read();
}
}
static int max(int a, int b) {
return a > b ? a : b;
}
static int min(int a, int b)
{
return a < b ? a : b;
}
static void Main(string[] args)
{
int[] a = new int[];
int[] b = new int[];
int[] bag1 = new int[];
int[] bag2 = new int[];
int n = , k = ;
getdata(ref n);
getdata(ref k);
//Console.Read();
for (int i = ; i < ; i++) {
bag1[i] = -;
bag2[i] = -;
}
bag1[] = ;
bag2[] = ;
int ans = , V = ;
for (int i = ; i < n; i++)
{
getdata(ref a[i]);
}
for (int i = ; i < n; i++) {
getdata(ref b[i]);
} for (int i = ; i < n; i++) {
int w = a[i] - b[i] * k;
if (w < )
{
w = -w;
for (int j = V; j >= w; j--)
{
bag1[j] = max(bag1[j], bag1[j - w] + a[i]);
}
}
else {
for (int j = V; j >= w; j--) {
bag2[j] = max(bag2[j], bag2[j - w] + a[i]);
}
}
}
for (int i = ; i >= ;i-- )
{
ans = max(ans, bag1[i] + bag2[i]);
}
if (ans == )
Console.WriteLine(-);
else
Console.WriteLine(ans);
// while (true) ;
}
}
}
Dima and Salad(完全背包)的更多相关文章
- 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 ...
- CF#214 C. Dima and Salad 01背包变形
C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...
- CF Dima and Salad 01背包
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- CodeForces - 366C Dima and Salad (01背包)
题意:n件东西,有属性a和属性b.要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\).在这个条件下,问\(\sum a_j\)最大是多少. 分析:可以将其转化为0 ...
- Codeforces 366C Dima and Salad:背包dp
题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...
- 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转换成偏移量,只要偏移到起点位置,就代表 ...
- codeforces 366C Dima and Salad 【限制性01背包】
<题目链接> 题目大意: 在一个水果篮里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里 ...
随机推荐
- Unity扩展让枚举视图中变成多选框
如图: 定义属性描述特性(因为没有描述的数据,让绘制类去绘制所以为空) using UnityEngine; using System.Collections; public class EnumFl ...
- Animate.css 教程
animate.css 是一个有趣,酷炫的,跨浏览器的动画库,你可以将它用于你的项目中.不管是主页,滑动切换,又或者是其它方面,你都可以通过它来制作出惊人的效果. 基本用法 引入CSS文件 这个对你来 ...
- web前端的学习误区
web前端的学习误区 网页制作是计算机专业同学在大学期间都会接触到的一门课程,而学习网页制作所用的第一个集成开发环境(IDE)想必大多是Dreamweaver,这种所见即所得的“吊炸天”IDE为我们 ...
- ARCGIS接口详细说明
ArcGIS接口详细说明 目录 ArcGIS接口详细说明... 1 1. IField接口(esriGeoDatabase)... 2 2. IFieldEdit接口(esriGe ...
- c++如何生成随机数
一.使用rand()函数 头文件<stdlib.h> (1) 如果你只要产生随机数而不需要设定范围的话,你只要用rand()就可以了:rand()会返回一随机数值, 范围在0至RAND_ ...
- 获取设备的UUID
很多时候需要获取设备的UUID,比如在蓝牙交互时,需要获取服务和特征的UUID,那么如何获取设备的UUID呢?请见如下代码: // // ViewController.m // 获取UUID // / ...
- 实现两个select list box间item的移动和过滤
<head> <title> </title> <!--Standard jQuery --> <script type="text/j ...
- ORA-01034/ORA-27101解决
sql> shutdown immediate 后就无法进行任何操作了,重新通过sqlplus不能登录,提示ORA-01034和ORA-27101错误 解决,以下全部在cmd中: 1. 启动or ...
- kettle新建资源库出错
之前在本地测试新建kettle资源库很顺利,但是在把本地数据迁移到服务器的过程中出现了问题,多次新建资源库失败,提示插入数据错误. 解决办法: 将要执行的sql语句复制出来,单独在Navicat下执行 ...
- AdapterView及其子类之一:基本原理(ListView、ListActivity类型)
参考<疯狂android讲义>2.5节 1.AdapterView一般用于显示列表项,其内容由Adapter提供.调用Adapter的setAdapter(Adapter)方法设置Adap ...