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种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里 ...
随机推荐
- SVN强制填写日志
在F:\Repositories\版本库名\hooks下新建pre-commit.bat 内容如下: @echo off setlocal set SVN_BINDIR="C:\Progra ...
- FastJson的简单使用(alibaba)
原文章:http://blog.csdn.net/glarystar/article/details/6654494 原作者:张星的博客 maven配置: <dependency> < ...
- Bridging signals(二分 二分+stl dp)
欢迎参加——每周六晚的BestCoder(有米!) Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 6 ...
- 兼容各个浏览器的H.264播放: H.264+HTML5+FLOWPLAYER+WOWZA+RMTP
一.方案确定 计划做视频播放,要求可以播放H264编码的mp4文件,各个浏览器,各种终端都能播放. 首先查找可行性方案, http://www.cnblogs.com/sink_cup/archive ...
- Unix/Linux环境C编程新手教程(22) C/C++怎样获取程序的执行时间
1.问:知道程序执行时间我们能够做什么? 在<C++应用程序性能优化>一书中,假设大家读过相信大家一定对性能优化这一块很上心,文中总是对优化前后的时间对照很直观给我们一个感受. 那么我们怎 ...
- libaio under MIPS architecture /在mips架构下使用的libaio
First, you can find libaio source in http://libaio.sourcearchive.com/ Second,download the libaio_0.3 ...
- 提高你的Java代码质量吧:小心switch带来的空值异常
一.分析 使用枚举定义常量时,会有伴有大量的switch语句判断,目的是为每个枚举解释其行为. 我们知道,目前的Java的switch语句只能判断byte.short.char.int类型(JDK7 ...
- static静态属性和静态方法的原理与调用技巧
这篇文章主要介绍了php面向对象中static静态属性和静态方法的调用,实例分析了static静态属性和静态方法的原理与调用技巧,需要的朋友可以参考下 本文实例讲述了php中static静态属 ...
- mysql1主多从配置
mysql一主多从的配置: 其实1主多从的配置与一主一从配置非常相似,现在主要讲讲一主多从的大概配置方法. 一, 1,master端开启binlog日志,并且设置server id=1. 2.重启服务 ...
- nodejs 计算内存使用率
//计算内存使用率 function calcMem(){ let mem_total = os.totalmem(), mem_free = os.freemem(), mem_used = mem ...