Gives a s , and then give n Set of stamps, ask the group to get the maximum continuous postage.
For each set of stamps, find out the minimum number of stamps required when postage is I d[i] , the boundary is d [ 0 = 0 、 d [ i > s Time break. Similar to the knapsack problem of the method, the specific methods see Code.
#include <cstdio>#include <cstring>#include <algorithm>Using namespace Std;constintmaxn=1010;intd[maxn],ans[ -],num[ -],a[ -][ -];intMain () {int s; while(~SCANF ("%d",&s) &&s){intN;SCANF ("%d", &n); for(intI=1; i<=n;++i) {scanf ("%d", &num[i]); for(intj=1; j<=num[i];++j) scanf ("%d", &a[i][j]); memset (D,0x3f, sizeof (d)); d[0]=0; for(intj=1; j<maxn;++j) { for(intk=1; k<=num[i]&&j-a[i][k]>=0; ++k) D[j]=min (d[j],d[j-a[i][k]]+1);if(d[j]>s) {ans[i]=j-1; Break;} } }intbest=-1,POS=0; for(intI=1; i<=n;++i) {if(ans[i]>best) Best=ans[i],POS=i;Else if(ans[i]==best) {if(num[i]<num[POS])POS=i;Else if(num[i]==num[POS]) {bool Ok=false; for(intJ=num[i];j>0;--j)if(a[i][j]!=a[POS][j]) {ok=a[i][j]<a[POS][J]; Break; }if(OK)POS=i; } } }printf("Max coverage =%4d :", ans[POS]); for(intI=1; i<=num[POS];i++)printf("%3d", a[POS][i]);printf("\ n"); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVa 242-stamps and Envelope Size (DP)