Solution to the problem of maximum continuous postage
Problem Description: suppose that the state issues n stamps of different denominations, and stipulates that only m stamps are allowed on each envelope. The continuous postage problem requires that for a given value of N and m, the optimal design of the face value of stamps is given, that is, the maximum continuous postage interval starting from postage 1 and increasing by 1 can be pasted on an envelope. For example, when n=5 and m=4, the maximum continuous postage range for five stamps with face value of (1,3,11,15,32) is 1-70.
Analysis:
For the continuous postage problem, n tuples x[1:n] are used to represent N Different Stamp denominations, and they are arranged from small to large. x[1]=1 is the only choice. At this time, the maximum continuous postage interval is [1:m].
The value range of x[2] is [2:m+1].
In general, if x[1:i-1] is selected, the maximum continuous postage interval is [1:r], then the value range of x[i] is [x[i-1]+1:r+1].
Algorithm design:
package com.bean.algorithmbasic;
public class StampsDemo2 {
/*
Continuous postage issues:
Suppose that the state issues n stamps of different denominations, and stipulates that only m stamps are allowed on each envelope.
The continuous postage problem requires that for a given value of n and m, the optimal design of stamp face value is given,
The maximum continuous postage interval starting from postage 1 and increasing by 1 can be pasted on one envelope.
For example:
When n=2,m=3, if the face value is 1 and 4 respectively, then the postage can be obtained from 1 to 6 plus 8, 9 and 12. But 8, 9, 12 are already discontinuous sums.
If the face value is 1,3, each postage value between 1 and 7 can be obtained, and 7 is the continuous maximum postage value.
Resolver, recursive call, backtrace
Input:
2
2 3
5 4
Output:
7
1 3
70
1 3 11 15 32
*/
static final int NUM=10;
static final int LEN=10000;
static int[] x = new int[NUM];
static int cnt = 0;//Current stamp type
static int[] r = new int[NUM];//For face value results
static int max = 0;//Maximum value
static int[][] C = new int[NUM][LEN]; //Record search node status
/*
* Calculate the maximum continuous postage interval for the current face value of stamps
* */
public static int findMax(int knd, int lim) {
int j = 1;
while (C[cnt - 1][j]!=0) {
if (j < x[cnt] || C[cnt - 1][j] <= C[cnt][j - x[cnt]] + 1)
C[cnt][j] = C[cnt - 1][j];
else
C[cnt][j] = C[cnt][j - x[cnt]] + 1;
j++;
}
while (true) {
int tmp = Integer.MAX_VALUE;
for (int i = 1; i <= cnt; i++) {
if (tmp > C[cnt][j - x[i]] + 1)
tmp = C[cnt][j - x[i]] + 1;
}
if (tmp == Integer.MAX_VALUE || tmp > lim)
break;
else
C[cnt][j] = tmp;
j++;
}
C[cnt][j] = 0;
return j - 1;
}
public static void dfs(int type,int limit) {
if (cnt == type) {
if (x[cnt] * limit < max)
return;
int tmp = findMax(type,limit);
if (tmp > max) {
max = tmp;//Update if it is larger than the upper limit of the maximum continuous postage interval recorded
for (int i = 1; i <= type; i++) //Record new stamp face value combinations
r[i] = x[i];
}
}
else {
int tmp = findMax(type,limit);
for (int i = tmp + 1; i >= x[cnt] + 1; i--) {
x[++cnt] = i;//Add the possible face value to the current face value combination
dfs(type,limit);
cnt--;
}
}
}
public static void main(String args[]){
int limit=3; //4 stamps allowed
int type=2; //There are five denominations
//From face value 1, there must be 1, otherwise many values cannot be constructed.
x[1] = 1;
//Because face value 1 is the first, cnt=1
cnt = 1;
dfs(type,limit);
System.out.println("MAX = "+max);
for (int i = 1; i <= type; i++) {
System.out.println(r[i]+" ");
}
}
}
(end)