In an ordinary three-game (five)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/dev_cao/article/details/102749452

Title Description

In two, three fight a fight game
given an envelope, allows a maximum of N pasted stamp, in the given calculation K (N + K≤15) kinds of stamps (assuming all the stamps are enough number), how design of denominations of stamps, the maximum value MAX can be obtained, so that each postage value between 1 and MAX, can be obtained.
For example, N = 3, K = 2 , if the denominations of 1 min, 4 min., Each postage value between 1 minute to 6 minutes can be obtained (of course, 8 points, 9 points and 12 points) ; If denominations of 1 minute, 3 minutes, then 1 minute to a postage value from each of the 7 points can be obtained. Can be verified when N = 3, K = 2, 7 consecutive points is the maximum value of postage can be obtained, so MAX = 7, denomination points - respectively 1 minute, 3 minutes.

Input Format

Two integer representing N, K.

Output Format

2 line. A first plurality of row numbers representing the selected denomination, from small to large.
The second line, the output "MAX = S", SS indicates the maximum nominal value.

SAMPLE INPUT

3 2

Sample Output

1 3
MAX=7

answer

First with dfs enumeration which k is selected from each species stamps
and then find the maximum of this embodiment may be constituted by a backpack

Code

#include<iostream>
#include<cstring>
using namespace std;
int a[17],n,k,ans[17],maxn;
int f[50000];
int dp(int step,int mx) {
	f[0]=0;
	for(int i=1; i<=a[step]*n; i++){
		f[i]=50000;
	}
	for(int i=1; i<=step; i++){
		for(int j=a[i]; j<=a[step]*n; j++){
			f[j]=min(f[j],f[j-a[i]]+1);
		}
	}
	for(int i=1; i<=a[step]*n; i++){
		if(f[i]>n){
			return i-1;
		}
	}
	return a[step]*n;
}
void dfs(int step,int mx) {
	if(step==k+1) {
		if(mx>maxn) {
			maxn=mx;
			for(int i=1; i<step; i++){
				ans[i]=a[i];
			}
		}
		return;
	}
	for(int i=a[step-1]+1; i<=mx+1; i++) {
		a[step]=i;
		int x=dp(step,mx);
		dfs(step+1,x);
	}
}
int main() {
	cin>>n>>k;
	dfs(1,0);
	for(int i=1; i<=k; i++){
		cout<<ans[i]<<" ";
	}
	cout<<endl<<"MAX="<<maxn;
	return 0;
}

Guess you like

Origin blog.csdn.net/dev_cao/article/details/102749452