Because the requirements are continuous, so we must determine the nominal value of each stamp in sequence, and consider that the first possible face value has been obtained, then how can we judge that the value of i+1 is not available? We can know that this zip code of I+1 face value can be obtained from the stamp of 0-i face value and a certain desirable face value, but by the limitation of the subject, we must ensure that the number of cent constituting the i+1 face value stamp shall be less than K.
We define DP[I] to represent the minimum number of cent required to make a stamp with a face value of I, and then Dp[i]=min (dp[i-a[j]]+1), if dp[i]<=k indicates that I is a par value that can be obtained, otherwise it can only be continuous to i-1.
The code is as follows:
/*id:15674811lang:c++prog:stamps*/#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;#define MAXN 2002000#define INF 0x3f3f3f3fintdp[maxn],a[ -];intMain () {Freopen ("Stamps.in","R", stdin); Freopen ("Stamps.out","W", stdout);///freopen ("Lkl.txt", "R", stdin); intK,n; while(scanf("%d%d", &k,&n)!=eof) { for(intI=1; i<=n;i++)scanf("%d", &a[i]);intImemset(DP,0,sizeof(DP)); Sort (A +1, a+n+1); for(i=1;; i++) {intflag=0, Min=inf; for(intj=1; j<=n&&a[j]<=i;j++) {inttmp=dp[i-a[j]]+1;if(tmp<=k&&tmp<min) {min=tmp; flag=1; } }if(!flag) Break; Dp[i]=min; }printf("%d\n", I-1); }return 0;}
Usaco--3.1stamps+dp