At first see this problem think should use DP do, first think of two-dimensional, (to deal with the item I, the first m backpack can be loaded maximum number of items) dp[m][i]=dp[m-1][i]+ the last backpack used to load the first to n items can get the maximum number of items, the back due to 0,1 The backpack is not ripe, plus this DP equation thinking is not clear, and then did not write.
Later, I want to use three-dimensional DP to do, wrote a dp[i][m][v] that the number of processing to the first m backpack, the last capacity is V, the maximum number of items can be loaded. And because the last dimension only indicates the status of the last backpack, and the status of some of the previous backpack is not considered, so it does not feel. Behind, see a person on the net wrote a three-dimensional DP, and then think about, in fact, you can consider using two Max to find the maximum value to represent, a max to indicate the volume of the last backpack, another max to indicate only the first m-1 backpack when the maximum number of items obtained. The specific code is as follows:
1#include <cmath>2#include <cstring>3#include <cstdlib>4#include <iostream>5#include <algorithm>6#include <memory>7#include <string>8#include <queue>9#include <map>Ten#include <stdio.h> One#include <vector> A#include <stack> -#include <fstream> - using namespacestd; the - intMain () - { - intdp[ -][ -][ -]; + intn,t,m,a[ -]; - while(cin>>n>>t>>m) + { A for(intI=1; i<=n;i++) atCin>>A[i]; -Memset (DP,0,sizeof(DP)); - for(intI=1; i<=m;++i)//First I backpack - { - for(intj=1; j<=n;++j)//Top J Items - { in for(intk=0; k<=t;++k)//capacity is K - { to if(k-a[j]>=0)//put + { -Dp[i][j][k]=max (Max (dp[i][j-1][k],dp[i][j-1][k-a[j]]+1), dp[i-1][j][t]);//add to current backpack the * } $ Else //DiscardPanax NotoginsengDp[i][j][k]=max (Max (dp[i][j-1][k],0), dp[i-1][j][t]); - //cout<<i << "a backpack. "<<j<<" item. "<<k<< capacity" <<dp[i][j][k]<<endl; the } + } A // the } +cout<<dp[m][n][t]<<Endl; - } $ return 0; $}
View Code
2016-4-25 Perfect World-Internship-Xiao Meng's package