Question 1209: Minimum number of stamps

Source: Internet
Author: User
1 // question 1209: Minimum number of stamps Title Description: 2 // There are several stamps that require the selection of the minimum number of stamps to generate a given total value. 3 // For example, if you have one, three, and four stamps, You need to score 10 points. Use three stamps: 3, 3, and 4. 4 # include "stdafx. H "5 # pragma warning (Disable: 4996) 6 # include <iostream> 7 # include <stdio. h> 8 # include <cstring> 9 # include <algorithm> 10 using namespace STD; 11 12 int v, N; 13 int W [1000]; 14 int DP [1000]; 15 # define INF 100000016 int min (int A, int B) 17 {18 return A> B? B: A; 19} 20 void solve () 21 {22 memset (DP, INF, sizeof (DP); 23 24 DP [0] = 0; 25 For (INT I = 1; I <= N; ++ I) // traverse Item 26 {27 for (Int J = V; j> = W [I]; -- j) // traverse the volume. The value is 1, that is, the number of stamps. We need to obtain the minimum value. 28 {29 DP [J] = min (DP [J], DP [J-W [I] + 1 ); 30} 31} 32 If (DP [v] >=1000) 33 cout <0 <Endl; 34 else35 cout <DP [v] <Endl; 36} 37 int main () 38 {39 freopen ("a.txt", "r", stdin); 40 while (CIN> V> N) 41 {42 for (INT I = 1; I <= N; ++ I) 43 {44 scanf ("% d", & W [I]); 45} 46 solve (); 47} 48 return 0; 49}

For the exactly-filled 01 backpack, note that f [0] = 0 is initialized. It indicates that when the backpack capacity is 0, no items can be put into the backpack status.

In addition, you must note that this question is the minimum number, that is, the minimum value of the item. Therefore, F [v] Must be initialized to an invalid state of infinity at the beginning.

Question 1209: Minimum number of stamps

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.