From http://blog.csdn.net/shuangde800
Question Portal
QuestionThere are n songs, each with a length of Ti, which should be loaded into M CDs, the maximum length of time that each CD can store is T. Therefore, these songs must be stored in the CD in the order they are given. For example, if there are four songs, their durations are given in order: 1, 2, 3, 4. load a CD with a capacity of 10, which can be 1, 2, 3, 3, 4, etc., but cannot be 2, 1, or 3. How many songs can I store at most?
Ideas:Use F [I] [J] [k] to represent the first I song, use J CDs, and use k time for the J CDs. The most songs can be saved.
When the J-disc is used to enumerate the song I, there is a decision and status transfer:
1. choose not to save this song.
F [I] [J] [k] = max (F [I] [J] [K], F [I-1] [J] [k]);
2. When this song is the first song on the J-disc
F [I] [J] [k] = max (F [I] [J] [K], F [I-1] [J-1] [T] + 1 );
3. When it is not the first song of the J-disc
F [I] [J] [k] = max (F [I] [J] [K], f [I-1] [J] [k-T [I] + 1 );Code:
/**=========================================== * This is a solution for ACM/ICPC problem. * * @author: shuangde * @blog: http://blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com *============================================*/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>using namespace std;typedef long long int64;const int INF = 0x3f3f3f3f;const int MAXN = 1010;const int MOD = 10007;int n, t, m;int T[MAXN];int f[900][120][120];int main(){ int nCase; scanf("%d", &nCase); while(nCase--){ scanf("%d%d%d", &n, &t, &m); int idx = 0, x; for(int i=1; i<=n; ++i){ if(i==1) scanf("%d", &x); else scanf(", %d", &x); if(x <= t) T[++idx] = x; } n = idx; memset(f, 0, sizeof(f)); for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ for(int k=0; k<=t; ++k){ f[i][j][k] = f[i-1][j][k]; if(k >= T[i]){ f[i][j][k] = max(f[i][j][k], f[i-1][j][k-T[i]]+1); f[i][j][k] = max(f[i][j][k], f[i-1][j-1][t]+1); } } } } printf("%d\n", f[n][m][t]); if(nCase) puts(""); } return 0;}