I spent two hours a out of the feeling is not the same AH, study DP suggested that everyone more suffering a little, think about how the state is transferred.
But this problem I first understand wrong test instructions, otherwise will not waste so long time. The first thought is the knapsack problem, later found that the requirements to sing as much as possible, in this premise as far as possible to leave the KTV, I happen to be reversed.
So it's not hard to get a recursive equation: Because each track can only be sung once, so this makes the recursion orderly ~ then we set CNT[I][J] to indicate a number of songs before singing, and the total time does not exceed the maximum number of J songs.
Then cnt[i][j] = Max (Cnt[i-1][j],cnt[i-1][j-a[i]] + 1); Because you want to make the total time as long as possible, so when cnt[i][j] = = Cnt[i-1][j-a[i]] + 1 o'clock, d[i][j] = max (D[i][j],d[i-1][j-a[i]] + a[i]);
See the code for details:
#include <bits/stdc++.h>using namespace Std;const int maxn = 55;int T,n,t,case = 0,d[maxn][180*maxn + 678],a[maxn],c NT[MAXN][180*MAXN + 678];int Main () {scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&t); for (int i=1;i<=n;i++) scanf ("%d", &a[i]); for (int i=1;i<=n;i++) {for (int j=0;j<=t;j++) {Cnt[i][j] = (i = = 1? 0:cnt[i-1][j]); D[I][J] = (i = = 1? 0:d[i-1][j]); if (J > a[i]) {if (Cnt[i][j] < Cnt[i-1][j-a[i]] + 1) {Cnt[i][j] = cnt[i-1] [J-a[i]] + 1; D[I][J] = D[i-1][j-a[i]] + a[i]; } else if (cnt[i][j] = = Cnt[i-1][j-a[i]] + 1) {D[i][j] = max (d[i][j],d[i-1][j-a[ I]] + a[i]); }}}} printf ("Case%d:%d%d\n", ++case,cnt[n][t]+1,d[n][t]+678); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
12563-jin Ge Jin Qu Hao (DP)