Usaco--3.1stamps+dp

Source: Internet
Author: User

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

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.