Codeforces Round #214 (Div. 2) c题(dp)
1 second
256 megabytes
standard input
standard output
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.
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 integersa1, 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.
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.
3 2 10 8 1 2 7 1
18
5 3 4 4 4 4 4 2 2 2 2 2
-1
In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition
fulfills, that's exactly what Inna wants.
In the second test sample we cannot choose the fruits so as to follow Inna's principle.
题意:就是给了你两个数组:a数组和b数组,然后让你从a数组中选取一个的子序列,子序列的和为sum1,然后除以相应b数组子序列的和sum2,sum1/sum2刚好为k,让你求出满足要求的a数组中sum1最大为多少,如果不存在这样的子序列,那么就输出-1,否则输出a数组中最大的sum1。
分析:对于这道题,我感悟还是比较深的,当时我想到用dp去做,并且抓住了两个数组加起来的和分别最多为10000,当时我没有把题目中的那个式子做变形,所以只想到了去记录a数组子序列相加之后的状态,但是如果a数组子序列的一个状态对应有多个b数组序列的状态呢?想到了这里,我就没法想下去了,因为我无法想到一个好的方法去解决这个问题,后来比完赛之后去看了下别人的代码,他们是把式子变形之后,把a[i]-b[i]*k作为状态进行dp的,这样a数组的序列和b数组的序列就绑定在一起了,就没必要去考虑我出现的问题了,而他的状态变化范围是:-10000-10000,出现了为负数的状态,无法用数组实现,于是干脆把所有状态都加上10000,于是状态的变化范围就变成:0-20000,那么现在就可以在此基础上进行dp了,还是比较简单的dp吧!
代码实现:
#include<stdio.h>
#include<string.h>
int n,k,a[],b[],dp[][]; void solve()
{
int i,j,p=,temp;
memset(dp,-,sizeof(dp));
dp[p][]=;
for(i=;i<=n;i++)
{
p=p^;
temp=a[i]-k*b[i];//变形之后的状态
for(j=;j<=;j++)
{
dp[p][j]=dp[p][j]>dp[-p][j]?dp[p][j]:dp[-p][j];//把上一状态移下来
if(!(j+temp>=&&j+temp<=))//不能超过状态的范围
continue;
if(dp[-p][j]!=-&&dp[p][j+temp]<dp[-p][j]+a[i])
dp[p][j+temp]=dp[-p][j]+a[i];
}
}
if(dp[p][]==)
printf("-1\n");
else
printf("%d\n",dp[p][]);
} int main()
{
int i,j;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
scanf("%d",&b[i]);
solve();
}
return ;
}
Codeforces Round #214 (Div. 2) c题(dp)的更多相关文章
- Codeforces Round #367 (Div. 2)---水题 | dp | 01字典树
A.Beru-taxi 水题:有一个人站在(sx,sy)的位置,有n辆出租车,正向这个人匀速赶来,每个出租车的位置是(xi, yi) 速度是 Vi;求人最少需要等的时间: 单间循环即可: #inclu ...
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Codeforces Round #713 (Div. 3)AB题
Codeforces Round #713 (Div. 3) Editorial 记录一下自己写的前二题本人比较菜 A. Spy Detected! You are given an array a ...
- Codeforces Round #552 (Div. 3) A题
题目网址:http://codeforces.com/contest/1154/problem/ 题目意思:就是给你四个数,这四个数是a+b,a+c,b+c,a+b+c,次序未知要反求出a,b,c,d ...
- Codeforces Round #412 Div. 2 补题 D. Dynamic Problem Scoring
D. Dynamic Problem Scoring time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #131 (Div. 2) B. Hometask dp
题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...
- Codeforces Round #579 (Div. 3) 套题 题解
A. Circle of Students 题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
随机推荐
- VBA高效删除不连续多行
最近在搞VBA,在感叹Excel功能强大的同时,对于新接触的一门编程语言也很烦恼.很多基础的语法都要靠网上搜索.现总结一些学习到的心得. VBA高效删除不连续多行 在一个拥有几万条数据的Excel中, ...
- Spring框架学习之第9节
aop编程 aop(aspect oriented programming)面向切面(方面)编程,是所有对象或者是一类对象编程,核心是(在不增加代码的基础上,还增加新功能) 汇编(伪机器指令 mov ...
- CentOS系统常用命令
1.进入目录命令 cd /usr/bin //进入etc目录 cd .. //切换到上一层目录 cd / //进入主目录 2.新建文件命令 比如进入某个目录后执行 vim test 2.查看运行的项目 ...
- JavaWeb项目开发案例精粹-第4章博客网站系统-001设计
1. 2. 3. # MySQL-Front 5.0 (Build 1.0) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL_MO ...
- 瞬态电压抑制二极管(TVS)选用原则
在选用瞬态电压抑制二极管(TVS)时,必须考虑电路的具体条件,一般应遵循以下原则: 一.大箝位电压Vc(MAX)不大于电路的最大允许安全电压. 二.最大反向工作电压(变位电压)VRWM不低于电路的最大 ...
- QQ群共享文件下载很慢解决办法
QQ群共享文件下载很慢解决办法.我们经常会不群里面共享文件,文件文件稍大,下载非常慢.家庭是20M的网速,一般正常下载能够达到2.5MB左右,而在QQ群实际下载网速却只有80KB左右.如果要下1G,就 ...
- php监听客户端连接状态
http://bbs.csdn.net/topics/390661022 http://www.poluoluo.com/jzxy/201207/169977.html http://zhidao.b ...
- 易犯的PHP小错误及相应分析
变量声明如果在一条语句中声明一个变量,如下所示:$var = 'value'; 编译器首先会求出语句右半部分的值,恰恰正是语句的这一部分常常会引发错误.如果使用的语法不正确,就会出现解析错误. 解析错 ...
- arcgis javascript dojo
一.为什么说ArcGIS API for JavaScript是构建于Dojo之上的? 1. 编写ArcGIS API for JavaScript的ESRI开发者使用Dojo来简化他们的开发过程,同 ...
- Maven+Spring+Mybatis+Security+Mysql简短的框架
一段时间想搞个框架做开发,但是网上好多代码建立的都太杂乱.有的开源的东西我感觉用不了.本人太笨,不懂怎么上传到github上,就写在博客里,留作记录.以后用的时候也方便. 1.首先让我们看一下项目结构 ...