Codeforces Round #214 (Div. 2) C. Dima and Salad 背包
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 integers a1, 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
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.
题意:
给你n个水果,每个水果有两个属性ai,bi;
让你任选m个至少一个,使得
注意是恰好整除为k
无方案输出-1,否则输出方案中分子最大的 那个分子
题解:
背包模型
先把式子化简看看,假设现在是X/Y=k
那么新加入的第i个水果造成 影响着是这样的 : X = k*Y + k*bi - ai
那么久明显了,任意加入m个,我们使得后半段部分和为0即可,作DP
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long LL;
const int N=1e6+,mod=,inf=2e9+; int n,k,a[N],b[N];
int dp[][];
int main() {
scanf("%d%d",&n,&k);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= n; ++i) scanf("%d",&b[i]);
for(int i = ; i <= n; ++i) {
for(int j = ; j <= ; ++j) dp[i][j] = -inf;
}
dp[][] = ;
for(int i = ; i <= n; ++i) {
for(int j = k*b[i] - a[i]; j <= ; ++j) {
dp[i][j] = max(dp[i][j],dp[i-][j]);
dp[i][j] = max(dp[i][j], dp[i-][j - k*b[i] + a[i]] + a[i]);
}
}
if(dp[n][] == ) puts("-1");
else
cout<<dp[n][]<<endl;
return ;
}
Codeforces Round #214 (Div. 2) C. Dima and Salad 背包的更多相关文章
- 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题(dp)
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- 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 ...
- 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 ...
- Codeforces Round #208 (Div. 2) 358D Dima and Hares
题目链接:http://codeforces.com/problemset/problem/358/D 开始题意理解错,整个就跪了= = 题目大意:从1到n的位置取数,取数的得到值与周围的数有没有取过 ...
- Codeforces Round #324 (Div. 2)D. Dima and Lisa 数学(素数)
D. Dima and Lisa Dima loves representing an odd num ...
- Codeforces Round #208 (Div. 2) A.Dima and Continuous Line
#include <iostream> #include <algorithm> #include <vector> using namespace std; in ...
- Codeforces Round #208 (Div. 2) B Dima and Text Messages
#include <iostream> #include <algorithm> #include <string> using namespace std; in ...
- Codeforces Round #553 (Div. 2)B. Dima and a Bad XOR 思维构造+异或警告
题意: 给出一个矩阵n(<=500)*m(<=500)每一行任选一个数 异或在一起 求一个 异或在一起不为0 的每行的取值列号 思路: 异或的性质 交换律 x1^x2^x3==x3^x2 ...
随机推荐
- vue 组件用法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AC手动机 [原创]
题目背景 Monster_Qi 又双叒叕拿到了rank1! 在开心之余他决定帮助蒟蒻floatiy拿到合适的排名. 题目描述 已知考试有n道题,每道题有num个测试点,有m个人 b[x,i,j](01 ...
- Java 文件操作大集合
package com.sess.hny.sys.fileserver; import java.io.BufferedInputStream;import java.io.BufferedOutpu ...
- 77-CCI,Commodity Channel Index,商品通道指标.(2015.7.1)
CCI,Commodity Channel Index 商品通道指标 Channel Index,商品通道指标.(2015.7.1)" title="77-CCI,Commodit ...
- SQLSERVER DBCC命令大全
DBCC DROPCLEANBUFFERS:从缓冲池中删除所有缓存,清除缓冲区 在进行测试时,使用这个命令可以从SQLSERVER的数据缓存data cache(buffer)清除所有的测试数据,以保 ...
- 一个简单的模板了解css+div网页布局
直接附上最终效果图: index.html内容: <html> <!--20170730 soulsjie--> <head> <meta http-equi ...
- android开发里跳过的坑——adb connect连不上
user版本在系统init.rc里已经添加了setprop service.adb.tcp.port 5555 ,但是刷机以后,发现adb connect怎么都连不上,重启电脑,改变网络,巴拉巴拉,能 ...
- 【NOIP2017】
NOIP2015 省Rank136 NOIP2016 省Rank165 NOIP2017 求进省Rank前100
- 【Eclipse】eclipse中设置tomcat启动时候的JVM参数
主要通过以下的几个jvm参数来设置堆内存的: -Xmx512m 最大总堆内存,一般设置为物理内存的1/4 -Xms512m 初始总堆内存,一般将它设置的和最大堆内存一样大,这样就不需要根据当前堆使用情 ...
- 自定义日志工具LogUtil
package com.pingyijinren.test; import android.util.Log; /** * Created by Administrator on 2016/5/20 ...