http://codeforces.com/problemset/problem/366/C

题意:在一个冰箱里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度之和是卡路里和的k倍,问小明是否可以做出这么一个水果沙拉,若不能输出-1,否则输出符合要求的最大的美味值之和。

思路:考虑这样DP:f[i][j]:做到第i个水果,平衡度为j,平衡度的表示:Σa[i]-K*Σb[i],这样就可以dp

f[i][j]=max(f[i-1][j],f[i-1][j-a[i]+K*b[i]])

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
int f[][],a[],b[],g[][];
int n,K;
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
int main(){
n=read();K=read();
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<=n;i++) b[i]=read();
for (int i=;i<=n;i++)
for (int j=;j<=;j++)
f[i][j]=-;
f[][]=; int cnt=;
for (int i=;i<=n;i++){
cnt++;
for (int j=-;j<=;j++){
f[i][j+]=std::max(f[i][j+],std::max(f[i-][j+],f[i-][j-a[i]+K*b[i]+]+a[i]));
}
}
int ans=-;
if (f[n][]) ans=f[n][];
printf("%d\n",ans);
return ;
}

Codeforces 366C Dima and Salad的更多相关文章

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

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

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

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

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

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

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

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

  6. Dima and Salad(完全背包)

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

  7. CF Dima and Salad 01背包

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

  8. codeforces-214(Div. 2)-C. Dima and Salad+DP恰好背包花费

    codeforces-214(Div. 2)-C. Dima and Salad 题意:有不同的沙拉,对应不同的颜值和卡路里,现在要求取出总颜值尽可能高的沙拉,同时要满足 解法:首先要把除法变成乘法, ...

  9. Codefroces 366 C Dima and Salad(dp)

    Dima and Salad 题意:一共有n种水果,每种水果都有一个ai, bi,现求一个最大的ai总和,使得ai之和/对应的bi之和的值等于K. 题解:将bi转换成偏移量,只要偏移到起点位置,就代表 ...

随机推荐

  1. Visual Studio 启动加速

    Who is locking my SQL database?|Deploy a database project with TFS Build Visual Studio 2012 running ...

  2. 关于volatile

    也许读者会注意到,端口寄存器的变量使用了volatile 修饰符,这是因为C语言在编译的时候会进行某种优化来提高效率,比如下面这段代码int i = 1;int a = i;int b = i;对于编 ...

  3. iptables 顺序

    -A INPUT -s 115.236.6.6/32 -p udp -m udp --dport 111 -j ACCEPT -A INPUT -s 10.175.197.98/32 -p udp - ...

  4. 基数---SQL Server 2008 Bible

    关系类型 主要实体的键 次要实体的键 一对一 主要实体-主键-单个元组 主要实体-主键-单个元组 一对多 主要实体-主键-单个元组 次要实体-外键-多个元组 多对多 多个元组 多个元组

  5. github atom 试用

    github的编辑器atom 1.0已经出来了,在https://atom.io/ 我之前在win上一直用notepad++写了两年脚本.最近改写lua了,项目组统一用的sublime text.su ...

  6. Linux基础知识笔记

    1.case的命令格式    #!/bin/sh echo "please input number 1 to 3" read number case $number in ) e ...

  7. Asp.Net HttpApplication请求管道与Session(二)

    Asp.Net 回话的创建与结束 LogHelper.LogHelper _log = new LogHelper.LogHelper(); /// <summary> /// 程序开始- ...

  8. (转)关于font-size:100%

    重设浏览器默认字体大小 h1,h2,h3,h4,h5,h6 {font-size:100%;font-weight:normal;} 假如你设置body{font-size:12px;} 但h1是不会 ...

  9. 返回ipv 地址

    //返回ipv 地址 public static string GetIP4Address() { string IP4Address = String.Empty; foreach (IPAddre ...

  10. ajaxFileUpload js判断类型

    function ajaxFileUpload() {    var File_box = document.getElementById('V_file');    var extend = Fil ...