题目链接:Codeforces 366C Dima and Salad
C. Dima and Salad
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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 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个物品,有两个属性,选j个物品,满足j个物品第一个属性的总和是第二个属性总和的k倍的时候,第一个属性最大是多少。
分析:我们把公式变一下形状

,就是如此公式,所以,我们可以以a[i]-kb[i]为体积,a[i]为权值,然后01背包max(dp[i][0])为结果,但注意到会有负值然后这里就有两个方法:
1.两个DP,一个记录正的,一个记录负的
2.背包体积容量增加一倍
java一只不过 ,但改成C++就过了
/扩容背包
#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=200000+5;//最大元素个数
#define ll long long
int dp[106][N];
int main()
{
int n;
scanf("%d",&n);
int m;
scanf("%d",&m);
int a[105];
int v[105];
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
int b;
scanf("%d",&b);
v[i]=a[i]-m*b;
}
int M=n*1000;//物品的体积最大为1000,n个物品
memset(dp,-127,sizeof(dp));
dp[0][M]=0;
for(int i=1;i<=n;i++)
for(int j=2*M;j>=v[i];j--)
{
dp[i][j]=max(dp[i-1][j],dp[i-1][j-v[i]]+a[i]);
}
if(dp[n][M]<=0)
{
cout<<-1<<endl;
}
else
{
cout<<dp[n][M]<<endl;
}
return 0;
}
//两个DP
#include<stdio.h>
#include<string>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N=500000+5;//最大元素个数
#define ll long long
int dp1[N],dp2[N];
int main()
{
int n;
scanf("%d",&n);
int m;
scanf("%d",&m);
int a[105];
int v[105];
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
int b;
scanf("%d",&b);
v[i]=a[i]-m*b;
}
memset(dp1,-INF,sizeof(dp1));
memset(dp2,-INF,sizeof(dp2));
dp1[0]=0;
dp2[0]=0;
for(int i=1;i<=n;i++)
{
if(v[i]>=0)
{
for(int j=N-1;j>=v[i];j--)
dp1[j]=max(dp1[j],dp1[j-v[i]]+a[i]);
}
else
{
for(int j=N-1;j>=-v[i];j--)
dp2[j]=max(dp2[j],dp2[j+v[i]]+a[i]);
}
}
int ans=0;
for(int j=N-1;j>=0;j--)
{
ans=max(ans, dp1[j]+dp2[j]);
}
if(ans==0)
{
cout<<-1<<endl;
}
else
{
cout<<ans<<endl;
}
return 0;
}
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static final long mod=1000000007;
static final int N=101000;
public static void main(String args[])throws IOException {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
int []a=new int[105];
int []v=new int[105];
int[]dp1=new int[N];
int[]dp2=new int[N];
for(int i=1;i<=n;i++)
{
a[i]=in.nextInt();
}
for(int i=1;i<=n;i++)
{
int b=in.nextInt();
v[i]=a[i]-m*b;
}
int M=n*1000;//物品的体积最大为1000,n个物品
for(int j=0;j<N;j++)
{
dp1[j]=-0x3f3f3f3f;
dp2[j]=-0x3f3f3f3f;
}
dp1[0]=0;
dp2[0]=0;
for(int i=1;i<=n;i++)
{
if(v[i]>=0)
for(int j=N-1;j>=v[i];j--)
dp1[j]=Math.max(dp1[j],dp1[j-v[i]]+a[i]);
if(v[i]<=0)
for(int j=N-1;j>=-v[i];j--)
dp2[j]=Math.max(dp2[j],dp2[j+v[i]]+a[i]);
}
int ans=0;
for(int j=N-1;j>=0;j--)
{
ans=Math.max(ans, dp1[j]+dp2[j]);
}
if(ans==0)
{
System.out.println(-1);
}
else
{
System.out.println(ans);
}
}
}