Description
It is known that a set of face values (for example, {1 point, 3 points}) of N stamps and an upper limit k indicate that K stamps can be attached to an envelope. Calculate the maximum continuous postage from 1 to M.
For example, suppose there are stamps of one or three points; you can paste up to five stamps. It is easy to post a postage of 1 to 5 points (just paste it with a one-point stamp), and the next postage is not hard:
6 = 3 + 3 7 = 3 + 3 + 1 8 = 3 + 3 + 1 + 1 9 = 3 + 3 + 3 10 = 3 + 3 + 3 + 1 11 = 3 + 3 + 3 + 1 + 1 12 = 3 + 3 + 3 + 3 13 = 3 + 3 + 3 + 3 + 1。
However, using five stamps with one or three points is impossible to post a postage of 14 points. Therefore, for the set of the two stamps and the maximum K = 5, the answer is M = 13.
[The maximum time limit for a point is 3 S]
Format
Program name: Stamps
Input Format:
(File stamps. In)
Row 1st: two integers, K and N. K (1 <= k <= 200) is the total number of available stamps. N (1 <= n <= 50) is the number of stamps.
Row 2nd .. end of the file: N integers, 15 in each line, list the faces of all N stamps. The faces of each stamp cannot exceed 10000.
Output Format:
(File stamps. out)
Row 1st: an integer that indicates the number of postage stamps attached to no more than K stamps in a continuous available set starting from 1.
Sample Input
5 21 3
Sample output
13
Analysis:
Simple DP question. It's a waste of time...
First, let's talk about the idea: first, let's set B [I] to indicate whether I can be expressed, then, B [I] = B [I-A [0] | B [I-A [1] | ...... B [I-A [J] calculates each I, and then determines the maximum number of consecutive times.
Later, we found that the question was limited to a maximum of K stamps. So I simply changed the equation: F [I] indicates the minimum number of stamps that I need. The formula for B [I] is the same, but a restriction must be added. f [I-A [J] <K. (j = 0 .. n) but this method has a defect and does not know where to calculate I. Of course, this question may be 200*10000 at most.
But now I can think of a more direct and space-saving method: 1. There is no need to record B [I. Because we want to start from 1 consecutively, so until we calculate this I, the previous number is OK. (That is, B [I] = 1). Therefore, if B [I] is used, any preceding number can be used. 2. The bounce condition does not need to be calculated to 200*10000. Once the calculated I cannot be pasted, it can jump out immediately. And output I-1. The reason is the same as above, because the question is to make the number from 1 consecutive to the maximum.
Code:
/* ID: 138_3531lang: C ++ task: stamps */# include <iostream> # include <fstream> # include <string> # include <cstring> # include <climits> using namespace STD; int f [2001000]; //Minimum number of stamps required for IInt main () {ifstream fin ("stamps. in "); ofstream fout (" stamps. out "); int I; int K, N; int OK; int min; int A [50]; memset (F, 0, sizeof (f )); f [0] = 1; Fin> K> N; for (I = 0; I <n; I ++) Fin> A [I]; for (I = 1; I ++) {OK = 0; min = int_max; For (Int J = 0; j <n; j ++) {if (I-A [J]> = 0) if (f [I-A [J] <k + 1) & (f [I-A [J]> 0) {OK = 1; if (F [I-A [J] + 1 <min) min = f [I-A [J] + 1 ;}} if (! OK) break; F [I] = min;} fout <I-1 <Endl; return 0 ;}