Stamps
Given a set of N stamp values (e.g., {1 cent, 3 cents}) and a upper limit K to the number of stamps that can fit in an en Velope, calculate the largest unbroken list of postages from 1 cent to M cents so can be created.
For example, consider stamps whose values is limited to 1 cent and 3 cents; You can use on most 5 stamps. It's easy-to-see-assemble postage of 1 through 5 cents (just use that many 1 cent stamps), and successive values AR En ' t much harder:
- 6 = 3 + 3
- 7 = 3 + 3 + 1
- 8 = 3 + 3 + 1 + 1
- 9 = 3 + 3 + 3
- 10 = 3 + 3 + 3 + 1
- 11 = 3 + 3 + 3 + 1 + 1
- 12 = 3 + 3 + 3 + 3
- 13 = 3 + 3 + 3 + 3 + 1.
However, there is no-the-cents of postage with 5 or fewer stamps of value 1 and 3 cents. Thus, for this set of stamp values and a limit of k=5, the answer is m=13.
The most difficult test case is problem has a time limit of 3 seconds.
Program Name:stampsinput FORMAT
Line 1: |
The integers k and N. K (1 <= k <=) is the total number of stamps, can be used. N (1 <= n <=) is the number of stamp values. |
Lines 2..end: |
n integers, "per line", listing all of the N stamp values, and each of the which would be is at most 10000. |
SAMPLE INPUT (file stamps.in)
5 21 3
OUTPUT FORMAT
Line 1: |
One integer, the number of contiguous postage values starting at 1 cent so can be formed using no more than K stamps fro M the set. |
SAMPLE OUTPUT (file stamps.out)
13
A simple DP, thought is memory.
The code is as follows:
/*id:yizeng21prob:stampslang:c++*/#include<stdio.h>#include<string.h>using namespacestd;intdp[2000005];intMaxx;intMaxintIintj) { returnI>j?i:j;}intMinintIintj) { returnI<j?i:j;}inta[10000];intMain () {Freopen ("stamps.in","R", stdin); Freopen ("Stamps.out","W", stdout); intM,n; scanf ("%d%d",&m,&N); for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); Maxx=Max (maxx,a[i]); } memset (DP,Ten,sizeof(DP)); dp[0]=0; for(intj=1; j<=n;j++) for(inti=a[j];i<=maxx*m;i++) {Dp[i]=min (dp[i],dp[i-a[j]]+1); } inttot=0; while(dp[tot]<=m) tot++; printf ("%d\n", tot-1);}
Usaco-section 3.1-stamps