code1047 Stamp Denomination Design

Source: Internet
Author: User

Dfs+dp

DFS enumeration for each case, each layer recursively determines the number of k i:i = a[k-1]+1 to A[k-1]*n+1

When you have finished enumerating a sequence, use check () to test the max that it can achieve

Use DP. Set Dp[i] to the minimum number of the face value of I

 for (int k=1; a[k]<=i&&k<=t;k++) {    if(dp[i-a[k]]<N) {        Dp[i]=min (dp[i], dp[i-a[k]]+1);}    }

Note Dp[i] Initially set to maximum value

The code is as follows:

#include <iostream>#include<cstring>#defineSize 41using namespacestd;intA[size];intdp[10005];intBest[size];intN,t;//N-Sheet t typeintnum=0;voidSaveintx) {num=x;  for(intI=1; i<=t;i++) {Best[i]=A[i]; }}intCheck () {//memset (Dp,0x3f,sizeof (DP));dp[0]=0; intans=0; BOOLok=true; //for (int i=1;i<=t;i++) cout<<a[i]<< ""; //cout<<endl;         for(intI=1; ok;i++) {OK=false; Dp[i]=0x3f3f3f3f;  for(intk=1; a[k]<=i&&k<=t;k++){            if(dp[i-a[k]]<N) {Dp[i]=min (Dp[i], dp[i-a[k]]+1); OK=true; }        }        if(OK) ans=i; //cout<<dp[i]<< ";    }    //cout<<endl<<endl;        returnans;}voidDfsintk) {if(k>t) {        intx=check (); if(x>num) Save (x); return; }         for(inti=a[k-1]+1; i<=n*a[k-1]+1; i++) {A[k]=i; DFS (k+1); }}intMain () {CIN>>n>>T; a[1]=1; Num=1; DFS (2);  for(intI=1; i<=t;i++) {cout<<best[i]<<' '; } cout<<endl<<"max="<<num<<Endl; cout<<F; return 0;} 

By the way:a[1] must be 1!

PS: You can see that I commented out the first line of the check memset, because most of the time, many of the elements behind the DP array are useless (max is not that big), so you just need to write a dp[i]=x03f3f3f3f in the loop. Memset makes the program slow and slow. (It's really slow ...) (You really don't need to try it too slow, because I've spent one hours trying it for you countless times ...) )

If you want to continue optimization, you can maintain the values of the DP and current MAX2 on the DFS side so that the DFS loop can be written like this:

i = a[k-1]+1 to Max2+1

If i>max2+1, then max2+1 this number will always be unable to make up, the result will not be more excellent

code1047 Stamp Denomination Design

Related Article

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.