Charm Bracelet
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di
Output
* Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints
Sample Input
4 6
1 4
2 6
3 12
2 7
Sample Output
23
题目大意:
有一个背包,承重为m,现在有n件物品,每件物品的重量为wi,对应价值为vi,每件物品只能选择装与不装,不能分割物品,求此背包能装的物品的最大价值。
【二维数组】:下标i正序,j的下标正序倒序无所谓,结果一样
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=3500,M=13000;
int dp[N][M];
int v[N],w[N];
int main()
{
int n,m;
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
for (int i=1; i<=n; i++)
scanf("%d%d",&w[i],&v[i]);
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++) //j表示背包容量从1增加到m
{
if (j>=w[i])
dp[i][j]=max(dp[i-1][j],dp[i-1][j-w[i]]+v[i]); //如果要用i-1,i必须从1开始
else
dp[i][j]=dp[i-1][j];
}
}
// for (int i=1;i<=n;i++)
// {
// for (int j=1;j<=m;j++) //j表示背包容量从1增加到m
// {
// printf("%-3d",dp[i][j]);
// }
// printf("\n");
// }
printf("%d\n",dp[n][m]);
}
return 0;
}
/*
4 6
1 4
2 6
3 12
2 7
*/
图解:
i\j 1 2 3 4 5 6
1 4 4 4 4 4 4
2 4 6 10 10 10 10
3 4 6 12 16 18 22
4 4 7 12 16 19 23
【二维数组】:下标i倒序,j的下标正序倒序无所谓,结果一样
#include<cstdio>
#include<cstring>
#include<algorithm>
//#include<time.h>
using namespace std;
const int N=3500,M=13000;
int dp[N][M];
int v[N],w[N];
int main()
{
int n,m;
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
for (int i=n-1; i>=0; i--)
scanf("%d%d",&w[i],&v[i]);
for (int i=n-1; i>=0; i--)
{
for (int j=m; j>=1; j--)
{
if (j>=w[i])
dp[i][j]=max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]);
else
dp[i][j]=dp[i+1][j];
}
}
// for (int i=0;i<n;i++)
// {
// for (int j=1;j<=m;j++)
// {
// printf("%-3d",dp[i][j]);
// }
// printf("\n");
// }
printf("%d\n",dp[0][m]);
// printf("Time used=%.8f\n",(double)clock()/CLOCKS_PER_SEC);//测试运行时间用的
}
return 0;
}
/*
4 6
1 4
2 6
3 12
2 7
*/
图解:
i\j 1 2 3 4 5 6
0 4 7 12 16 19 23
1 4 6 12 16 18 22
2 4 6 10 10 10 10
3 4 4 4 4 4 4
【一维数组】:缩少了空间复杂度
//AC
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=13000,N=3500;
int dp[M],v[N],w[N];
int main()
{
int n,m;
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
for (int i=1;i<=n;i++)
scanf("%d%d",&w[i],&v[i]);
for (int i=1;i<=n;i++)
for (int j=m;j>=w[i];j--) //一维数组j必须要从大到小,因为前面的还要用,不能被覆盖
dp[j]=max(dp[j],dp[j-w[i]]+v[i]);
// for (int j=1;j<=m;j++)
// printf("%-3d",dp[j]);
// printf("\n");
printf("%d\n",dp[m]); //必须是dp[m],数组必须要包括m
}
return 0;
}
/*
4 6
1 4
2 6
3 12
2 7
*/
图解:
j: 1 2 3 4 5 6
4 7 12 16 19 23