Usaco-section 3.1-stamps

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.