Continuous postage

------ Plan

1. Problem Description

Assume that the country has issued n kinds of stamps with different denominations, and that at most m stamps can be pasted on each envelope. The continuous postage problem requires the optimal design of the stamp face value for the given n and m values, and the maximum continuous postage interval starting from postage 1 and increasing to 1 can be pasted on one envelope.

2. Solutions and basic ideas (recursion)

(1) Basic Ideas:

Search for all feasible solutions. When I <= n, the current node Z is the internal node in the solution space. At this node x [1: I-1] can be attached to the maximum continuous range of R-1. Therefore, at node Z, the possible value range of x is [x [I-1] + 1: r], thus, node Z has r-x [I-1] Son node. The algorithm recursively searches the corresponding subtree of each son node of the current node Z in depth-first traversal to find the maximum continuous postage interval.

(2) solution vector:

Use n tuples x [1: n] To represent n different stamp denominations and arrange them in ascending order. X [1] = 1 is the only choice. (The nominal value of the I stamp in Table x)

(3) Feasibility Constraint Functions:

Selected X [1: I-1], the maximum continuous postage interval is 1-r-1, the range of down X is X [I-1] + 1-r. (R-1 Table X [1: I-1] can reach the upper limit of the continuous interval)

(4) how to determine the value of R:

Calculating the maximum continuous postage interval of X [1: I] is frequently used in this algorithm, so it is necessary to find an efficient method. Considering that the complexity of direct recursion is too high, the minimum number of stamps required for posting a postage K is Y [k] with a stamp of no more than m x [1: I]. You can use y [k] to quickly release the R value. The specific algorithm is as follows:

For (j = 0; j <= x [I-2] * (S-1); j ++)

If (Y [J] <m)

For (k = 1; k <= m-y [J]; k ++)

If (Y [J] + k <Y [J + X [I-1] * k]) y [J + X [I-1] * k] = Y [J] + K;

While (Y [R] <maxnum) r ++;

During the initialization of Y [], all values are assigned to maxnum (Y [0] = 0), where the maxnum table is an integer large enough.

3. Core algorithms

How to obtain the value of the maximum continuous range R-1

For (j = 0; j <= x [I-2] * (S-1); j ++)

If (Y [J] <m)

For (k = 1; k <= m-y [J]; k ++)

If (Y [J] + k <Y [J + X [I-1] * k]) y [J + X [I-1] * k] = Y [J] + K;

While (Y [R] <maxnum) r ++;

During the initialization of Y [], all values are assigned to maxnum (Y [0] = 0), where the maxnum table is an integer large enough.

4. Preliminary Module

Initstamp (* s, NN, MM ){

// Assign an initial value to S.

// S indicates the pointer variable pointing to the stamp type, NN indicates the number of stamps, and mm indicates the maximum number of stamps.

}

Backtrack (* s, I, R ){

// Recursive Backtracking, find the maximum continuous range of 1-R-1

}

Output (s ){

// Output maxvalue and * bestx

}

Main (){

// Call the above module to solve the problem of continuous postage.

}

The call relationships between modules are as follows:

InitStamp (* S, nn, mm ){

•••

BackTrack (* S, 2, 1 );

•••

}

BackTrack (* S, I, r ){

•••

BackTrack (* S, I + 1, r + 1 );

•••

}

Main (){

•••

InitStamp (* S, nn, mm );

OutPut (S );

}

5. Data Structure

Typedef struct {

Int n, // Number of stamp faces

M, // Most stamps

Maxvalue; // The current optimal solution.

Int * bestx; // current Optimal Solution

Int * x; // current solution

Int * y; // the minimum number of stamps required for postage.

} Stamp;

6. Test Data

When n = 5 and m = 4, five stamps with a nominal value of (, 32) can be pasted with a maximum continuous postage interval of 1 to 70.