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
  output
  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恰好背包花费的更多相关文章

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

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

  3. codeforces #260 DIV 2 C题Boredom(DP)

    题目地址:http://codeforces.com/contest/456/problem/C 脑残了. .DP仅仅DP到了n. . 应该DP到10w+的. . 代码例如以下: #include & ...

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

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

  5. cf366C Dima and Salad (dp)

    是一个01分数规划的形式,只不过已经帮你二分好了. 把b乘过去,再减回来,找和等于0的a的最大值就行了 #include<bits/stdc++.h> #define pa pair< ...

  6. CodeForces-366C Dima and Salad 对01背包的理解 多个背包问题

    题目链接:https://cn.vjudge.net/problem/CodeForces-366C 题意 给出n个水果和一个常数k,其中每个水果都有两种性质ai, bi(美味度,卡路里量). 要保证 ...

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

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

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

  9. Codeforces #344 Div.2

    Codeforces #344 Div.2 Interview 题目描述:求两个序列的子序列或操作的和的最大值 solution 签到题 时间复杂度:\(O(n^2)\) Print Check 题目 ...

随机推荐

  1. 灵活使用Maven Profile

    项目中一直应用Maven的profile特性解决不同环境的部署问题.最近在尝试解决本地调试环境的时候碰到一些问题,顺便仔细研究了一下.因为项目仍然在用普通SpringMVC架构,没有切换到Spring ...

  2. 【iOS】UILabel 常用属性设置

    UILabel 的一些常用属性,示例代码如下: // 字体大小 label.font = [UIFont systemFontOfSize:14.0]; label.font = [UIFont fo ...

  3. 使用log4net记录ABP日志

    demo地址:ABP.WindowsService 该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇. 参考:https://aspnet ...

  4. Django REST framework的使用简单介绍

    官方文档:https://www.django-rest-framework.org/ GitHub源码:https://github.com/encode/django-rest-framework ...

  5. 经典SQL(sqlServer)

    一.基础 .说明:创建新表create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) .分组: ...

  6. MySQL操作命令梳理(2)

    一.表操作 在mysql运维操作中会经常使用到alter这个修改表的命令,alter tables允许修改一个现有表的结构,比如增加或删除列.创造或消去索引.改变现有列的类型.或重新命名列或表本身,也 ...

  7. http客户端-性能比较系列-第一篇-单线程

    系列文章: 单线程性能测试:https://www.cnblogs.com/victor2302/p/11077208.html 多线程性能测试:https://www.cnblogs.com/vic ...

  8. Docker部署网站之后映射域名

    Docker中部署tomcat相信大家也都知道,不知道的可以google 或者bing 一下.这里主要是为了记录在我们启动容器之后,tomcat需要直接定位到网站信息,而不是打开域名之后,还得加个bl ...

  9. JVM面试十问

    1. JVM运行时划分哪几个区域?哪些区域是线程共享的?哪些区域是线程独占的? JVM运行时一共划分:程序计数器.虚拟机栈.堆.本地方法栈.方法区. 线程共享的数据区域:堆.方法区. 线程独享的数据区 ...

  10. 3.php基础(控制语句,函数,数组遍历)

    if条件判断语句 结构一:只判断true,不管false 结构二:既判断true,也判断false(二选一) 结构三:多条件判断 switch多分支结构 Switch语法结构说明: l Switch的 ...