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 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
随机推荐
- java内存模型优化建议
八.Java编程建议 根据GC的工作原理,我们可以通过一些技巧和方式,让GC运行更加有效率,更加符合应用程序的要求.一些关于程序设计的几点建议: 1)最基本的建议就是尽早释放无用对象的引用.大多数程序 ...
- MyBatis笔记——初次环境配置
简单介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBati ...
- JS解析json数据
JS解析json数据(如何将json字符串转化为数组) 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN&q ...
- 无图片,用css border实现尖三角
<!DOCTYPE HTML> <html> <head> <meta charset="gbk" /> <style typ ...
- Android处理Bitmap的一些方法
http://www.it165.net/pro/html/201305/5795.html # 文件与Bitmap间的方法 1. 从文件载入Bitmap 01./** 02.* @brief 从文件 ...
- 【Tech】YCSB-0.1.3安装使用
1. 下载YCSB 0.1.3: wget https://github.com/brianfrankcooper/YCSB/archive/0.1.3.tar.gz 如果提示“wget:命令没找到” ...
- GitHub 教程 in Ubuntu
Follow these steps to configure github if you are the first time to use Github 1. Sign up a username ...
- Linux操作系统基础(四)保护模式内存管理(2)【转】
转自:http://blog.csdn.net/rosetta/article/details/8570681 Linux操作系统基础(四)保护模式内存管理(2) 转载请注明出处:http://blo ...
- 如何修改linux时间? 校正linux系统的时间
第一步:通过xshell远程连接到linux系统 第二步:输入 tzselect 第三步:选择所在的州,中国人请选择 5 ,亚洲 第四步:选择你所在的国家,中国人请选择9,中国 第五步:选择一个时区, ...
- jpa+spring配置多数据源
property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test?useU ...