[Usaco 3.1.6] stamps

Source: Internet
Author: User
[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.

[Format] program name: stampsinput format :( file stamps. In)
Row 3: 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. The end of the file: N integers, 15 in each line, list the face values of all N stamps. The face values cannot exceed 10000.
Output Format :( file stamps. out)

A separate row and an integer. The number of free stamps attached to a single row is no more than that of K stamps in a continuous set starting from 1.

[Analysis]

For general DP, pay attention to the questions.

1 # include <iostream> 2 # include <cstdio> 3 # include <cmath> 4 # include <cstring> 5 # include <algorithm> 6 const int maxn = 50 + 10; 7 const int INF = 200 + 10; 8 using namespace STD; 9 int s [maxn], F [2000000 + 10]; 10 int main () 11 {12 int K, n, I, j; 13 // file operation 14 freopen ("stamps. in "," r ", stdin); 15 freopen (" stamps. out "," W ", stdout); 16 scanf (" % d ", & K, & N); 17 for (I = 1; I <= N; I ++) scanf ("% d", & S [I]); 18 F [0] = 0; 19 for (I = 1; I <= 2000000; I ++) 20 {21 f [I] = inf; 22 for (j = 1; j <= N; j ++) if (I-s [J]> = 0) f [I] = min (F [I], F [I-s [J] + 1 ); 23 if (F [I]> K) break; 24} 25 printf ("% d", I-1); 26 return 0; 27}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.