Codeforces 366C Dima and Salad
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的更多相关文章
- Codeforces 366C Dima and Salad:背包dp
题目链接:http://codeforces.com/problemset/problem/366/C 题意: 有n个物品,每个物品有两个属性a[i]和b[i]. 给定k,让你选出一些物品,使得 ∑ ...
- codeforces 366C Dima and Salad 【限制性01背包】
<题目链接> 题目大意: 在一个水果篮里有n种水果,并且这些水果每一种都有一个美味度和一个卡路里的属性, 小明要从这些水果中选出来一些做一个水果沙拉, 并且要求他的水果沙拉的美味度是卡路里 ...
- CodeForces - 366C Dima and Salad (01背包)
题意:n件东西,有属性a和属性b.要选取若干件东西,使得\(\frac{\sum a_j}{\sum b_j} = k\).在这个条件下,问\(\sum a_j\)最大是多少. 分析:可以将其转化为0 ...
- 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 ...
- Dima and Salad(完全背包)
Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 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-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转换成偏移量,只要偏移到起点位置,就代表 ...
随机推荐
- 360网站卫士常用前端公共库CDN服务
360网站卫士常用前端公共库CDN服务 360网站卫士常用前端公共库CDN服务
- Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法
抛错: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document ...
- Android 体系结构
Anroid是在Linux基础开发出的一个移动设备开发平台.它自上而下包含四个部分: Application(应用程序) Applicaton Framework(应用程序框架) Libraries& ...
- 使用MutationObserver对象封装一个监听DOM生成的函数
(function(win){ 'use strict'; var listeners = []; var doc = win.document; var MutationObserver = win ...
- Js获取元素样式值(getComputedStyle¤tStyle)兼容性解决方案
因为:style(document.getElementById(id).style.XXX)只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的. 一般js获取内部样式和外部样式使用 ...
- 【编程范式】汇编解释swap方法
先要熟悉一些汇编的基本知识: 1.SP是什么? SP是堆栈寄存器,在调用子程序时,都会用到,保存原来程序的环境使用,如各个寄存器的内容,最重要的是,调用返回时程序的运行指令地址,这是由调用时将返回地址 ...
- float position的測试案例
依据<a target=_blank href="http://blog.csdn.net/goodshot/article/details/44348525">htt ...
- android设备之间屏幕共享
近期公司在开发一款android的设备把屏幕投射到手机上.同一时候手机还能够触控.键盘操作.这样.就达到了屏幕共享的目的. 思考了一下.主要思路: 1.将截图所获取的位图用ffmpeg编码成视频流. ...
- Linux gdb调试入门
没有使用过gdb调试过程序的觉得gdb是个很神奇的东东,如果你使用它调试一次保证你想忘记它都难,下面看看它的庐山真面目吧! GDB概述 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具. ...
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...