Codeforces 366C Dima and Salad (背包变形,DP好题)
2017-03-07 23:09
525 查看
题目地址:点击打开链接
姿势水平太低啊。。。看到题是根本没思路
思路粘自:点击打开链接
题目要求a的和为b的和的k倍,就是说最后要达到的状态为ai1+ai2+ai3+ai4==kbi1+kbi2+kbi3+kbi4……
不妨设ai=k*bi+c,c=k*bi-ai; 这里的c表示如果选了这组,那么还差多少才能满足题意。很显然,最后得到的结果中,所有的c的和应该为0.
定义dp[i]表示还差 i 可以满足题意时 最大能达到的 a (taste值)。
dp[j]=max(dp[j] , dp[j-c[i]]+a[i]);
注意c可能为正也可能为负,为了不使某一个fruit被重复加两次,可以开二维数组来做,也可以先判断c的正负,再根据正负来写 j 的遍历顺序,这样只用一维数组。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e2+5;
const int maxm = 1e4+5;
const int INF = 0x3f3f3f3f;
int n, k, a[maxn], b[maxn], dp1[maxm], dp2[maxm];
int main(void)
{
while(cin >> n >> k)
{
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for(int i = 1; i <= n; i++)
scanf("%d", &b[i]);
memset(dp1, -INF, sizeof(dp1));
memset(dp2, -INF, sizeof(dp2));
dp1[0] = dp2[0] = 0;
for(int i = 1; i <= n; i++)
{
int c = a[i]-b[i]*k;
if(c >= 0)
{
for(int j = 10000; j-c >= 0; j--)
dp1[j] = max(dp1[j], dp1[j-c]+a[i]);
}
else
{
for(int j = 10000; j-(-c) >= 0; j--)
dp2[j] = max(dp2[j], dp2[j-(-c)]+a[i]);
}
}
int ans = 0;
for(int i = 0; i <= 10000; i++)
ans = max(ans, dp1[i]+dp2[i]);
printf("%d\n", ans ? ans : -1);
}
return 0;
}
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,
![](http://codeforces.com/predownloaded/10/ee/10eec4fa6028c5a0ba5b14ab6c44d4c5a72e6732.png)
,
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 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.
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.
Examples
input
output
input
output
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
![](http://codeforces.com/predownloaded/ca/95/ca95b29c276ef2433d77259192211c6776da6590.png)
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的和的k倍,就是说最后要达到的状态为ai1+ai2+ai3+ai4==kbi1+kbi2+kbi3+kbi4……
不妨设ai=k*bi+c,c=k*bi-ai; 这里的c表示如果选了这组,那么还差多少才能满足题意。很显然,最后得到的结果中,所有的c的和应该为0.
定义dp[i]表示还差 i 可以满足题意时 最大能达到的 a (taste值)。
dp[j]=max(dp[j] , dp[j-c[i]]+a[i]);
注意c可能为正也可能为负,为了不使某一个fruit被重复加两次,可以开二维数组来做,也可以先判断c的正负,再根据正负来写 j 的遍历顺序,这样只用一维数组。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e2+5;
const int maxm = 1e4+5;
const int INF = 0x3f3f3f3f;
int n, k, a[maxn], b[maxn], dp1[maxm], dp2[maxm];
int main(void)
{
while(cin >> n >> k)
{
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for(int i = 1; i <= n; i++)
scanf("%d", &b[i]);
memset(dp1, -INF, sizeof(dp1));
memset(dp2, -INF, sizeof(dp2));
dp1[0] = dp2[0] = 0;
for(int i = 1; i <= n; i++)
{
int c = a[i]-b[i]*k;
if(c >= 0)
{
for(int j = 10000; j-c >= 0; j--)
dp1[j] = max(dp1[j], dp1[j-c]+a[i]);
}
else
{
for(int j = 10000; j-(-c) >= 0; j--)
dp2[j] = max(dp2[j], dp2[j-(-c)]+a[i]);
}
}
int ans = 0;
for(int i = 0; i <= 10000; i++)
ans = max(ans, dp1[i]+dp2[i]);
printf("%d\n", ans ? ans : -1);
}
return 0;
}
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,
![](http://codeforces.com/predownloaded/10/ee/10eec4fa6028c5a0ba5b14ab6c44d4c5a72e6732.png)
,
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 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.
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.
Examples
input
3 2 10 8 1 2 7 1
output
18
input
5 3 4 4 4 4 4 2 2 2 2 2
output
-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
![](http://codeforces.com/predownloaded/ca/95/ca95b29c276ef2433d77259192211c6776da6590.png)
fulfills,
that's exactly what Inna wants.
In the second test sample we cannot choose the fruits so as to follow Inna's principle.
相关文章推荐
- Codeforces 366C Dima and Salad 背包DP
- Codeforces 366C Dima and Salad 【dp】
- CodeForces-366C Dima and Salad 对01背包的理解 多个背包问题
- CodeForces - 366C Dima and Salad (DP&01背包)
- Codeforces 366C Dima and Salad(dp)
- CodeForces 366C Dima and Salad
- codeforces 366C Dima and Salad (01背包)
- CodeForces - 366C Dima and Salad
- Codeforces 366C - Dima and Salad(DP)
- CodeForces - 366C Dima and Salad 01背包变形 好题!
- Codeforces 366C Dima and Salad
- Dima and Salad CodeForces - 366C 背包DP
- Codeforces 235B Let's Play Osu! (概率dp求期望+公式变形)
- poj 2184 Cow Exhibition(dp之01背包变形)
- codeforces 167B Wizards and Huge Prize (概率dp,类似背包)
- [杂题 背包DP] Codeforces 793E Tinkoff Challenge - Elimination Round E. Problem of offices
- codeforces 366C(dp)
- Codeforces 864E Fire(背包DP)
- codeforces 543A Writing Code(dp降维优化,完全背包思想)
- codeforces 366C C. Dima and Salad(dp)