Dima and Salad

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.

Input

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 number i has taste ai and calories bi.

Output

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.

样例

input1

3 2
10 8 1
2 7 1

output1

18

input2

5 3
4 4 4 4 4
2 2 2 2 2

output2

-1

人话:

n个物品,k为倍数。每个物品有两个属性(ai和bi),求在满足所取物品的a属性和是b属性和的k倍的前提下,问a属性的最大值是多少

按照题意,就是要让我们选出一些组aibi,使的:

\(\frac{a_1+a_2+...+a_j}{b_1+b_2+...+b_j}=k\),然后移项,得\(a_1+a_2+...+a_j=k(b_1+b_2+...+b_j)\)

得\((a_1-b_1k)+(a_2-b_2k)+...+(a_j-b_jk)=0\),很像0/1分数规划,对不对

但是这题分类是背包

观察公式,发现这其实是一个容量为0的01背包,

我们可以把\(a_i-b_ik\)看作一个物品的体积,\(a_i\)看作价值,,然后一个标准的01背包模板

那么,背包的容量?容量为0怎么枚举呢?

先来想一想,\((a_i-b_ik)\)是不是有可能为负数?那么怎么办呢?

可以考虑开两个背包,容量分别为V和-V,那么加起来就抵消为0,正容量的背包处理正体积的,负容量的背包处理负体积的

很好,到这一步,套个模板就可以交啦!

Code:

#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#define int long long
#define reg register
using namespace std;
const int MaxN=101;
const int MaxX=10000;
template <class t> inline void rd(t &s)
{
s=0;
reg char c=getchar();
while(!isdigit(c))
c=getchar();
while(isdigit(c))
s=(s<<3)+(s<<1)+(c^48),c=getchar();
return;
}
int w[MaxN],c[MaxN];
int f[MaxX+1],g[MaxX+1];
int n,k;
inline void work()
{
memset(f,0xc3,sizeof f);f[0]=0;
memset(g,0xc3,sizeof g);g[0]=0;
reg int W,C;
for(int i=1;i<=n;++i)
rd(w[i]);
for(int i=1;i<=n;++i)
rd(c[i]);
for(int i=1;i<=n;++i)
{
W=w[i],C=c[i];
w[i]=W-k*C;c[i]=W;
}
for(int i=1;i<=n;++i)
{
// printf("wi: %d ci: %d\n",w[i],c[i]);
if(w[i]>=0)
for(int j=MaxX;j>=w[i];--j)
f[j]=max(f[j],f[j-w[i]]+c[i]);
else
for(int j=MaxX;j>=-w[i];--j)
g[j]=max(g[j],g[j+w[i]]+c[i]);
// for(int j=1;j<=n;++j)
// printf("fi: %d %d ",f[i],g[i]);puts("");
}
reg int ans=-1;
for(int i=0;i<=MaxX;++i)
ans=max(ans,f[i]+g[i]);
printf("%lld\n",!ans?-1:ans);
return;
}
signed main(void)
{
while(cin>>n>>k)
work();
return 0;
}

<背包>solution_CF366C_Dima and Salad的更多相关文章

  1. Dima and Salad(完全背包)

    Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. 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 ...

  3. 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 ...

  4. CF#214 C. Dima and Salad 01背包变形

    C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...

  5. codeforces 366C Dima and Salad 【限制性01背包】

    <题目链接> 题目大意: 在一个水果篮里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里 ...

  6. cf 366C C. Dima and Salad(01背包)

    http://codeforces.com/contest/366/problem/C 题意:给出n个水果的两种属性a属性和b属性,然后挑选苹果,选择的苹果必须要满足这样一个条件:,现在给出n,k,要 ...

  7. CodeForces - 366C Dima and Salad (01背包)

    题意:n件东西,有属性a和属性b.要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\).在这个条件下,问\(\sum a_j\)最大是多少. 分析:可以将其转化为0 ...

  8. CF Dima and Salad 01背包

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Codeforces 366C Dima and Salad:背包dp

    题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...

随机推荐

  1. DEVOPS技术实践_11:Jenkins集成Sonar

    前言 前面已经有介绍sonar的安装,简单应用,下面在简答的研究一下sonar和jenkins集成的简单使用,对于sonar的安装不做介绍 一 sonar的简单介绍 持续检查避免了低质量的代码,比如S ...

  2. MyBatis使用mapper动态代理实现DAO接口

    工具: mysql 5.5.62   IDEA 参考自:https://www.cnblogs.com/best/p/5688040.html 遇到的问题: 无法读取src/main/java下配置文 ...

  3. 洛谷$P1935$ [国家集训队]圈地计划 网络流

    正解:最小割 解题报告: 传送门 就文理分科模型嘛$QwQ$?所以就,跑个最小割呗,然后就做完辣?仔细想想细节发现并麻油那么简单嗷$QwQ$ 先考虑如果没有这个$k\cdot C_{i,j}$的贡献就 ...

  4. win10纯净版安装及其常用软件集锦(2020新年湘岳阳万江波整理)

    win10纯净版安装及其常用软件集锦 1.安装win10纯净版:链接:https://pan.baidu.com/s/1L9yl-LNxxDQbEN_TGswzcA 提取码:u0pt 2.安装WPS2 ...

  5. 使用ASP.NET Core 3.x 构建 RESTful API - 4.3 HTTP 方法的安全性和幂等性

    什么样的HTTP方法是安全的? 如果一个方法不会该表资源的表述,那么这个方法就被认为是安全的. 例如 HTTP GET 和 HTTP HEAD 就被认为是安全的,但需要注意的是,这并不意味着执行GET ...

  6. 一文MyBatis-Plus快速入门

    目录 一.依赖及配置 1.在idea中创建一个SpringBoot项目,在pom.xml中添需要的依赖 2.配置数据库连接 3.在启动类中添加注解 @MapperScan 扫描Mapper接口包 4. ...

  7. spring boot介绍

    spring boot简介 1.spring boot是spring家族中的一个全新的框架,它用来简化spring应用程序的创建和开发过程,也可以说spring boot能简化我们之前采用ssm框架进 ...

  8. Spring循环依赖的解决

    ## Spring循环依赖的解决 ### 什么是循环依赖 循环依赖,是依赖关系形成了一个圆环.比如:A对象有一个属性B,那么这时候我们称之为A依赖B,如果这时候B对象里面有一个属性A.那么这时候A和B ...

  9. MySQL快速回顾:更新和删除操作

    前提要述:参考书籍<MySQL必知必会> 6.1 更新数据 为了更新(修改)表中的数据,可使用UPDATE语句.可采用两种方式使用UPDATE: 更新表中特定的行: 更新表中所有的行. U ...

  10. 【一起学源码-微服务】Hystrix 源码二:Hystrix核心流程:Hystix非降级逻辑流程梳理

    说明 原创不易,如若转载 请标明来源! 欢迎关注本人微信公众号:壹枝花算不算浪漫 更多内容也可查看本人博客:一枝花算不算浪漫 前言 前情回顾 上一讲我们讲了配置了feign.hystrix.enabl ...