C. Dima and Salad
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line of the input contains two integers nk (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.

Output

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.

Sample test(s)
input
  1. 3 2
  2. 10 8 1
  3. 2 7 1
output
  1. 18
input
  1. 5 3
  2. 4 4 4 4 4
  3. 2 2 2 2 2
output
  1. -1
Note

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。

思路:

其实这题就是背包变形,将 ai-k*bi 看做重量,将 ai 看做价值,总容量为 0 ,进行背包,最后满足条件的情况为背包恰好装满的情况,答案即为dp[0]。

因为 ai-k*bi 可以为负数,所以要将原点平移,可以预算一下, 原点到10000就够了,剩下的就是01背包的问题了,还需注意一点的是 重量为正和负的要分开考虑,WA了我好久才想明白。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <map>
  8. #include <stack>
  9. #include <vector>
  10. #include <set>
  11. #include <queue>
  12. #pragma comment (linker,"/STACK:102400000,102400000")
  13. #define maxn 1005
  14. #define MAXN 300005
  15. #define mod 1000000007
  16. #define INF 0x3f3f3f3f
  17. #define pi acos(-1.0)
  18. #define eps 1e-6
  19. typedef long long ll;
  20. using namespace std;
  21.  
  22. int n,m,ans,k,flag,cnt;
  23. int a[maxn],b[maxn];
  24. int w[maxn];
  25. int dp[MAXN];
  26.  
  27. bool solve()
  28. {
  29. int i,j;
  30. memset(dp,-INF,sizeof(dp));
  31. dp[10000]=0;
  32. for(i=1; i<=n; i++)
  33. {
  34. if(w[i]>=0)
  35. {
  36. for(j=20000; j>=w[i]; j--)
  37. {
  38. if(dp[j-w[i]]!=-INF) dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
  39. }
  40. }
  41. else
  42. {
  43. for(j=0;j<20000;j++)
  44. {
  45. if(dp[j-w[i]]!=-INF) dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
  46. }
  47. }
  48. }
  49. ans=dp[10000];
  50. if(ans==0) return false ;
  51. return true ;
  52. }
  53. int main()
  54. {
  55. int i,j;
  56. while(~scanf("%d%d",&n,&k))
  57. {
  58. for(i=1; i<=n; i++)
  59. {
  60. scanf("%d",&a[i]);
  61. }
  62. for(i=1; i<=n; i++)
  63. {
  64. scanf("%d",&b[i]);
  65. }
  66. for(i=1; i<=n; i++)
  67. {
  68. w[i]=a[i]-k*b[i];
  69. }
  70. if(solve()) printf("%d\n",ans);
  71. else printf("-1\n");
  72. }
  73. return 0;
  74. }

Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)的更多相关文章

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

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

  3. Codeforces Round #167 (Div. 2) D. Dima and Two Sequences 排列组合

    题目链接: http://codeforces.com/problemset/problem/272/D D. Dima and Two Sequences time limit per test2 ...

  4. Codeforces Round #324 (Div. 2) D. Dima and Lisa 哥德巴赫猜想

    D. Dima and Lisa Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/584/probl ...

  5. Codeforces Round #208 (Div. 2) 358D Dima and Hares

    题目链接:http://codeforces.com/problemset/problem/358/D 开始题意理解错,整个就跪了= = 题目大意:从1到n的位置取数,取数的得到值与周围的数有没有取过 ...

  6. Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)

                                                     D. Dima and Lisa Dima loves representing an odd num ...

  7. Codeforces Round #208 (Div. 2) A.Dima and Continuous Line

    #include <iostream> #include <algorithm> #include <vector> using namespace std; in ...

  8. Codeforces Round #208 (Div. 2) B Dima and Text Messages

    #include <iostream> #include <algorithm> #include <string> using namespace std; in ...

  9. Codeforces Round #553 (Div. 2)B. Dima and a Bad XOR 思维构造+异或警告

    题意: 给出一个矩阵n(<=500)*m(<=500)每一行任选一个数 异或在一起 求一个 异或在一起不为0 的每行的取值列号 思路: 异或的性质  交换律 x1^x2^x3==x3^x2 ...

随机推荐

  1. Android中的TextView实现多行显示省略号

    今天遇到一个问题就是TextView显示内容的时候,多行显示的时候,显示省略号的问题,刚开始没有找到一个好的办法,只找到一个自定义TextView组件的方法,然而今天在贴吧中找到一个更好,更简便的方法 ...

  2. chrome 浏览器帐号登录不来,如何解决自己的书签

    装系统前把 该目录下  C:\Users\Administrator\AppData\Local\Google\Chrome\User Data\Default    的  Bookmarks 复制出 ...

  3. Redis中的value包含中文显示的问题?

    linux 系统 redis不识别中文  如何显示中文 在Redis中存储的value值是中文“马拉斯加”Shell下get获取后展示的结果为:\xc2\xed\xc0\xad\xcb\xb9\xbc ...

  4. 使用zabbix监控nginx

    在zabbix agentd客户端上,查看nginx是否加载了--with-http_stub_status_module.因为zabbix监控nginx是根据 nginx的Stub Status模块 ...

  5. Two-phase Termination

    本文参阅[http://ifeve.com/java-two-phase-termination/] Two-phase Termination模式简介 停止线程是一个目标简单而实现却不那么简单的任务 ...

  6. C语言--union关键字(转载)

    union维护足够的空间来放置多个数据成员中的“一种”,而不是为每一个数据成员配置空间.在union中,所有的数据成员共用一个空间,同一时间只能存储其中一个数据成员,所有的数据成员具有相同的起始地址.

  7. AddSelf

    今天看     C语言深度深度解剖  第58页 看到了这么一段代码,就敲进了dev 谁知居然出现了个死循环.但是我不知道为什么. 贴出来,等有空了再请教别人好好分析,或者是网络上的高人指点一下 //d ...

  8. Net::OpenSSH 使用例子

    [root@dr-mysql01 mojo]# cat a1.pl use Net::OpenSSH; my $host = '121.4xx.xx1.41'; my $user = 'root'; ...

  9. 【行业干货】2013中国零售商排名 - 课程公告板 - 京东内部论坛 - Powered by Discuz!

    [行业干货]2013中国零售商排名 - 课程公告板 - 京东内部论坛 - Powered by Discuz! [行业干货]2013中国零售商排名 [复制链接]     bjpanzhoulan   ...

  10. 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法

    图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...