Ultraviolet A 12563 Jin Ge Jin Qu hao (DP), 12563dp
Link: http://uva.onlinejudge.org/external/125/12563.pdf
Train of Thought: DP, with the idea of 01 backpack, each record the maximum number of songs at each time, and finally find the answer to satisfy the maximum number of songs, the maximum number of times
# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int N = 55; int T, n, t, sing [N], dp [11005]; int main () {int cas = 0; scanf ("% d", & T); while (T --) {scanf ("% d", & n, & t); for (int I = 0; I <n; I ++) scanf ("% d ", & sing [I]); sing [n ++] = 678; memset (dp,-1, sizeof (dp); dp [0] = 0; for (int I = 0; I <n; I ++) {for (int j = min (t-1, 10000); j> = 0; j --) {if (dp [j]! =-1) dp [j + sing [I] = max (dp [j + sing [I], dp [j] + 1 );}} int ans1 = 0, ans2; for (int I = 11000; I> = 0; I --) {if (ans1 <dp [I]) {ans1 = dp [I]; ans2 = I ;}} printf ("Case % d: % d \ n", ++ cas, ans1, ans2);} return 0 ;}