Title Description
Given an envelope, a maximum of only N stamps can be pasted, calculated in the case of a given K (N+K≤40) stamp (assuming that all stamps are sufficient), how to design the face value of the stamp, the maximum value max, so that each postage value between the 1~max can be obtained.
For example, n=3,k=2, if the face value is 1 points, 4 points, then the 1 cent between the points of each postage value can be obtained (of course, 8, 9 and 12 points), if the face value of 1 points, 3 points, the 1 points between the value of each postage can be obtained. It can be verified that when n=3,k=2, 7 points is the maximum number of consecutive postage that can be obtained, so max=7, the face value is 1 points, 3 points respectively.
input/output format
Input format:
2 integers representing the n,k.
Output format:
2 lines. The first line is a number of numbers that represent the selected face value, sorted from small to large.
The second line, output "Max=s", S represents the maximum face value.
input/Output sampleInput Sample # #:
3 2
Sample # # of output:
1 3max=7
--------------------------------------------------------
Just go straight to the solution.
Seems to be the first thought of recursion, gray brother pushed half a class ...
There are too many uncertainties, and it is the optimization problem, the search
It must be that DFS enumerates the face value of each stamp, but there's no obvious upper bound.
found that if the current continuous value of up to Mxi, then the next stamp can be the largest mxi+1, and then large will be in the mxi+1 position vacancy, meaningless
So the question is how to find the current maximum number of consecutive values in each DFS----backpack DP F[i] indicates the minimum stamp count to reach the value of I, recursion, once >n break immediately
WARN: The beginning of the tle, even a point, the result is the value of the maximum value of V set too large, changed to 1E3 on the past, next time must first run the maximum value may be how much
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intn= $, v= -, inf=1e7;intn,k,f[v],ans=0, a[n],b[n];voidDfsintNow ) { for(intI=0; i<v;i++) F[i]=inf; f[0]=0; intMxi=0; for(mxi=1;; mxi++){ for(intI=1; i<now&&a[i]<=mxi;i++) F[mxi]=min (f[mxi],f[mxi-a[i]]+1); if(f[mxi]>n) Break; } Mxi--; if(mxi>ans) {ans=Mxi; for(intI=1; i<=n;i++) b[i]=A[i]; } if(now==k+1)return; for(inti=a[now-1]+1; i<=mxi+1; i++) {A[now]=i; DFS ( now+1); }}intMain () {CIN>>n>>K; a[1]=1; DFS (2); for(intI=1; i<=k;i++) cout<<b[i]<<" "; cout<<"\ n"; cout<<"max="<<ans;}
NOIP1999 Stamp denomination design [search | DP]