DP has always been a pain point. If you don't talk much about it, you will be stuck here this summer vacation.
This summer's first DP question, 01 backpack problem.
The meaning of the question is that the item has weight and value, but the weight you can afford is limited. Ask the maximum value you can bring.
Although I don't know what the problem is, I open a large array and directly 1A.
I think that my DP is a big problem. It is really sad to tell my students the question Tiao.
Just a few days to show up on the DP talent. By the way, I post my own understanding. I am also going to tell my students this way. If there is a mistake, please pass by and correct me.
01 self-cultivation of backpacks:
N items, m size package, maximum value is W.
Items I have VI weight and CI value.
N = 4, M = 6.
V C
1: 1 4
2: 2 6
3: 3 12
4: 2 7
Int DP [10001], C [10001], V [10001];
// The value of a backpack, the value of an item, and the weight of an item.
Memset (DP, 0, sizeof (DP ));
// Initialize the backpack. After all, there is no value when there is no content.
// There are other cases where the initialization is-INF, only DP [0] = 0. This is the requirement: the maximum value that exactly fills up.
For (INT I = 0; I <n; I ++)
{
// Start from the first item
For (Int J = m; j-V [I]> = 0; j --) // J-V [I]> = 0 to prevent array out-of-bounds, after all, the backpack capacity cannot be negative.
{
// Subtract 1 from the backpack each time to see if this item can be put in.
// Printf ("% d: max (% d, % d)", J, DP [J], DP [J-C [I] + V [I]);
DP [J] = max (DP [J], DP [J-V [I] + C [I]);
// When the size of the backpack is J, you can add the value to it.
// Value of DP [J] When capacity is J
// DP [J-C [I] + C [I] (capacity J minus current item Weight = remaining capacity) value + current item Value
// The greater the value, the better. Max is used.
// When J-V [I] <0 indicates that the backpack is no longer available.
}
}
Capacity: max (previous value, value after the current item is placed)
6: max () 5: max () 4: max () 3: max () 2: max () 1: max)
6: max (4, 10) 5: max (4, 10) 4: max (4, 10) 3: max (4, 10) 2: max (4, 6)
6: max (10, 22) 5: max (10, 18) 4: max (10, 16) 3: max (10, 12)
6: max () 5: max () 4: max () 3: max () 2: max (6, 7)
The last size of the backpack is 0 ~ 6. Take the maximum value of all values.
M: W0 1 2 3 4 5 6
At this time, the maximum value of the backpack is W, which is 23.
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<queue>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<cmath>#define INF 0x7fffffff#define eps 1e-6using namespace std;int dp[1000001];int c[100001],v[100001];int n,m;int main(){ while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<n;i++) scanf("%d%d",&c[i],&v[i]); memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++) { for(int j=m;j-c[i]>=0;j--) { dp[j]=max(dp[j],dp[j-c[i]]+v[i]); } } int ans=0; for(int i=0;i<=m;i++) ans=max(ans,dp[i]); printf("%d\n",ans); }}
Poj 3624 charm bracelet