A complete backpack problem.
My second question is my backpack training. Follow the steps described in section 9 of the backpack.
I would like to give you some money, and then some bonds can be purchased. Different bonds have different profits, and the maximum profit is within the specified period of time.
Bonds are bought without restrictions, and the profits obtained from (full backpacks) can be bought (larger backpacks)
Bonds can be selected every year, that is, they must be restarted every year (once a year)
Finally, we can find out how much money you have. This question refers to a multiple of 1000. However, the cost is not necessarily, and the profit is not necessarily.
Only bonds can be divided into 1000 at input. At the beginning of each year, the cost is exceeded 1000, but % 1000 is also saved.
02 self-cultivation of a backpack:
F [0... V] limit 0
For I want 1 to n
For v sort CI to m
F [v] ← max (F [v]; F [V-CI] + WI)
The pseudocode is similar to that of the 01 backpack.
C [] indicates the cost, and W [] indicates the value. M indicates the size of the backpack.
01 The second process of the backpack is the opposite, from m to 0; in order to ensure that you are considering "selecting the I-item"
The policy is based on a sub-result f [I-1; m-CI] That has never been selected for the I-th item.
A full backpack may be selected multiple times for a certain item.
Int DP [1000001];
Int C [100001], V [100001];
Memset (DP, 0, sizeof (DP ));
For (INT I = 0; I <n; I ++)
{
For (Int J = C [I]; j <= m; j ++)
DP [J] = max (DP [J], DP [J-C [I] + V [I]);
}
Step 2: complete the backpack. But I have to understand it carefully.
Code:
#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[100001];int c[11],v[11];int main(){ int t,m,n,year; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&year); scanf("%d",&m); for(int i=0; i<m; i++) { scanf("%d%d",&c[i],&v[i]); c[i]/=1000; } memset(dp,0,sizeof(dp)); while(year--) { int tmp=n%1000; n/=1000; for(int i=0; i<m; i++) { if(c[i]>n)continue; for(int j=c[i];j<=n;j++) { dp[j]=max(dp[j],dp[j-c[i]]+v[i]); } } int ans=0; for(int i=0; i<=n; i++) ans=max(ans,dp[i]); n=n*1000+tmp+ans; } printf("%d\n",n); }}