codeforces-214(Div. 2)-C. Dima and Salad+DP恰好背包花费
codeforces-214(Div. 2)-C. Dima and Salad
题意:有不同的沙拉,对应不同的颜值和卡路里,现在要求取出总颜值尽可能高的沙拉,同时要满足

解法:首先要把除法变成乘法,就是每次把读进来的b[ i ] 乘以 K;
因为对于a [ i ] - b[ i ] * k有两种不同的可能(大于0和小于0),可以放在一个以25000为中心的,大小为50000的dp数组里;

比如对于样例1:
input
3 2
10 8 1
2 7 1
18
这里我们缩小一下数据

我一开始不理解,为什么dp[j - b[i]]为什么只能在非-1的情况下更新dp[j];后来画了图就明白了;
下面ac代码:
//learnt and created at 2018/2/7
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n,k;
int a[],b[];
int dp[],mid = ;
int main(){
scanf("%d%d",&n,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
for(int i=; i<=n; i++)
{
scanf("%d",&b[i]);
b[i] = a[i] - b[i]*k;
}
memset(dp,-,sizeof(dp)); //把dp数组初始化为-1;只把dp[25000]=0;
dp[mid]=;
for(int i=; i<=n; i++)
{
if(b[i]>=)
{
for(int j=; j>=b[i]; j--)
{
if(dp[j-b[i]] !=-)
dp[j] = max(dp[j-b[i]]+a[i],dp[j]);
}
}
else
{
for(int j=; j<=+b[i]; j++)
{
if(dp[j-b[i]]!=-)
dp[j] =max(dp[j-b[i]]+a[i],dp[j]);
}
}
}
if(dp[mid]==)puts("-1");
else printf("%d\n",dp[mid]); return ;
}
codeforces-214(Div. 2)-C. Dima and Salad+DP恰好背包花费的更多相关文章
- 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 ...
- 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 #260 DIV 2 C题Boredom(DP)
题目地址:http://codeforces.com/contest/456/problem/C 脑残了. .DP仅仅DP到了n. . 应该DP到10w+的. . 代码例如以下: #include & ...
- cf 366C C. Dima and Salad(01背包)
http://codeforces.com/contest/366/problem/C 题意:给出n个水果的两种属性a属性和b属性,然后挑选苹果,选择的苹果必须要满足这样一个条件:,现在给出n,k,要 ...
- cf366C Dima and Salad (dp)
是一个01分数规划的形式,只不过已经帮你二分好了. 把b乘过去,再减回来,找和等于0的a的最大值就行了 #include<bits/stdc++.h> #define pa pair< ...
- CodeForces-366C Dima and Salad 对01背包的理解 多个背包问题
题目链接:https://cn.vjudge.net/problem/CodeForces-366C 题意 给出n个水果和一个常数k,其中每个水果都有两种性质ai, bi(美味度,卡路里量). 要保证 ...
- Codeforces Round #214 (Div. 2) c题(dp)
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}{ ...
- Codeforces #344 Div.2
Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...
随机推荐
- form.elements[i]
原生js操作form的一些方法,来看下面写的这个demo,这个demo是展示了一下form.eleemnts[i]的意义和用法: <!DOCTYPE html> <html lang ...
- Nginx服务器安全加固tips整理
公司各业务网站大多用到Nginx,花了点时间整理了一下Nginx服务器安全加固的各类tips. 默认配置文件和Nginx端口 /usr/local/nginx/conf/-Nginx配置文件目录,/u ...
- 模拟器无Back、Menu等键
问题如图所示: 解决方法: 1. 打开Android Virtual Device (AVD) Manager --> 选择模拟器,并点击edit --> 勾选KeyBoard中的选项,并 ...
- 【译】Hello Kubernetes快速交互实验手册
原文:https://kubernetes.io/docs/tutorials 翻译:Edison Zhou 一.基本介绍 此交互实验可以让你不用搭建K8S环境就可以轻松地尝试管理一个简单的容器化应用 ...
- 基于zookeeper集群的云平台-配置中心的功能设计
最近准备找工作面试,就研究了下基于zookeeper集群的配置中心. 下面是自己设想的关于开源的基于zookeeper集群的云平台-配置中心的功能设计.大家觉得哪里有问题,请提出宝贵的意见和建议,谢谢 ...
- codeforces 327 A Ciel and Dancing
题目链接 给你一串只有0和1的数字,然后对某一区间的数翻转1次(0变1 1变0),只翻转一次而且不能不翻转,然后让你计算最多可能出现多少个1. 这里要注意很多细节 比如全为1,要求必须翻转,这时候我们 ...
- RocketMQ中NameServer的启动
在RocketMQ中,使用NamesrvStartup作为启动类 主函数作为其启动的入口: public static void main(String[] args) { main0(args); ...
- linux集群实施与部署-----Nginx
( 1 ) 配置基本环境 //安装虚拟工具 #cd /media/VMware\ Tools/ #cp VMwareTools--.tar.gz/tmp/ #cd /tmp/ #tar-xvzf VM ...
- ceph 初始化函数解析
global_pre_init 预初始化函数,解析ceph.conf配置文件, 初始化定义global_context 和 config的全局变量. 全局预初始化函数 CINIT_FLAG_UNPRI ...
- 【React踩坑记四】React项目中引入并使用js-xlsx上传插件(结合antdesign的上传组件)
最近有一个前端上传并解析excel/csv表格数据的需求. 于是在github上找到一个14K star的前端解析插件 github传送门 官方也有,奈何实在太过于浅薄.于是做了以下整理,避免道友们少 ...