pku1882 Stamps (动态规划)

 题目大意是求n种面值的邮票能连续组合出的最大值。如面值1和3的邮票取5张可以组合出1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15但1-13中间是连续的,所以连续组合出的最大值为13

采用的算法为动态规划

 

#include <iostream> using namespace std; bool flag[1002]; int deno[10]; int S, N, denonum; int bestdeno[10]; int main() { int i, j, k, maxcover, mindenonum, minmaxdeno, curcover, end; while (cin>>S && S) { cin >> N; maxcover = -1; mindenonum = 10000; minmaxdeno = 10000; while (N--) { cin>>denonum; for (i=0; i<denonum; i++) cin >> deno[i]; memset(flag, 0, sizeof(flag)); end = 0; flag[0] = true; for (i=1; i<=S; i++) { for (j=end; j>=0; j--) //逆序更新 { if (flag[j]) { for (k=0; k<denonum; k++) { flag[j+deno[k]] = true; } } } end += deno[denonum-1]; } for (i=0; i<=end+1; i++) { if (!flag[i]) { curcover = i-1; break; } } if (curcover>maxcover || (curcover==maxcover && mindenonum>denonum) || (curcover==maxcover && mindenonum==denonum && minmaxdeno>deno[denonum-1])) { maxcover = curcover; mindenonum = denonum; minmaxdeno = deno[denonum-1]; memcpy(bestdeno, deno, sizeof(deno)); } } cout << "max coverage = " << maxcover << " :"; for (i=0; i<mindenonum; i++) cout << " " << bestdeno[i]; cout << endl; } return 0; }

你可能感兴趣的:(算法)