hdu 6092 Rikka with Subset(多重背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6092
#include <cstdio> #include <iostream> #include <cstring> using namespace std; const int maxn = 1e4 + 5; int b[maxn] , dp[maxn] , n , m; int main() { int t; scanf("%d", &t); while(t--) { scanf("%d%d" , &n , &m); for (int i = 0; i <= m; i++) { scanf("%d" , &b[i]); dp[i] = 0; } dp[0] = 1; int count = 0; for (int i = 1; i <= m; i++) { while (dp[i] < b[i]) { count++; if(count != n) printf("%d " , i); else printf("%d\n" , i); for (int j = m ; j >= i ; j--) { dp[j] += dp[j - i]; } } } } return 0; }
题解:就是一道多重背包,每次有优先拿最小的来更新看代码就很好理解了。