First of all, the test instructions to read first, rather than to see the 01 knapsack problem began to knock.
Main topic:
If you ask a Mac: "You in the KTV will sing the songs have those", the answer may be included Leo Ku "Jin song", generally speaking, KTV will not in time to be sung song to cut off, but will wait for it to put out, for example, there are 15 seconds to sing a song 2 minutes, then actually sang more than 105 seconds. But singing "The Divine Comedy" "Jin Song" is equivalent to singing for more than 663 seconds.
Now ask you to sing as many songs as possible, and sing the time as long as possible.
Ideas:
01 Backpack Thinking, each time the maximum number of songs, the last to find the answer first to meet the maximum number of songs, and then meet the most time.
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 55;const int M Ax_time = 100005;int song[n],dp[max_time];int n,t;int main () {int t,cas = 1;SCANF ("%d", &t), while (t--) {scanf ("%d%d", &N,&T); for (int i = 0; i < n; i++) {scanf ("%d", &song[i]);} Memset (Dp,-1,sizeof (DP));DP [0] = 0;for (int i = 0; i < n; i++) {for (int j = min (t-1,max_time); J >= Song[i]; j--) {i F (dp[j-song[i]]! =-1) dp[j] = max (dp[j],dp[j-song[i]]+1);}} int MAXN = 0, time;for (int i = max_time-1; I >= 0; i--) {if (Maxn < dp[i]+1) {MAXN = Dp[i]+1;time = i;}} printf ("Case%d:%d%d\n", Cas++,maxn,time + 678);} return 0;}
UVA-12563 Jin Ge jin Qu hao (01 backpack)