Test instructions: give n the song of Love to sing, the remaining T time. Since KTV will not force you to pause your song at the end, you can finally order a song, that is, you can add 678 seconds to sing more time. Ask to be able to sing the songs as much as possible, the time to sing as long as possible. Output the highest number of singing and the longest singing time. The given n first will not exceed three minutes.
Solution: Obviously, Jin song is sure to sing, after all, long time. Although the range of T is 1e9, but at most only 50 first, each song 180 seconds, up to 9,000 seconds. More than 9000 can be directly output all the songs are sung.
Next consider the general situation, it is obvious that this is a 01 backpack, we have to ensure that singing songs as much as possible, so the value of each song as 1, and then do a 01 backpack, you can know how many songs can be sung. Plus the golden Melody, is dp[n][t-1] + 1.
But in the premise of singing as much as possible, we must also ensure that the singing time as long as possible. So we also open an array to save the singing time,
If the number of songs to be updated, then the singing time from the previous state plus the current song time.
If the number of songs is the same, then take two times to compare the maximum singing time update.
The code is as follows:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < vector> #include <string> #include <set> #include <map> #include <stack> #include <queue
> Using namespace std;
const int MAXN = 505;
const int MAXM = 10005;
const int INF = 0X3F3F3F3F;
int n, t;
int dp[55][10005], val[55], cnt[55][10005];
int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif int T, case = 1;
scanf ("%d", &t);
while (t--) {int sum = 0;
memset (DP, 0, sizeof (DP));
scanf ("%d%d", &n, &t);
for (int i = 1; I <= n; i++) {scanf ("%d", &val[i]);
Sum + = Val[i];
} if (Sum < T) {printf ("Case%d:%d%d\n", case++, n + 1, sum + 678);
Continue } for (int i = 1, i <= N; i++) {for (int j = t-1; J >= 0; j--) {Dp[i][j] = dp[
I-1][J]; cnT[I][J] = Cnt[i-1][j]; if (J >= Val[i]) {if (Dp[i-1][j] < Dp[i-1][j-val[i]] + 1) {Dp[i][j
] = Dp[i-1][j-val[i]] + 1; CNT[I][J] = Cnt[i-1][j-val[i]] + val[i]; Singing time length update} else if (dp[i-1][j] = = Dp[i-1][j-val[i]] + 1) {Cnt[i][j] =
Max (Cnt[i][j], Cnt[i-1][j-val[i]] + val[i]); If there is an equal number of songs, be sure to let the singing time as long as possible}}}} printf ("Case%d:%d
%d\n ", case++, Dp[n][t-1] + 1, cnt[n][t-1] + 678);
} return 0; }