Minimum number of stamps
-
Title Description:
-
There are a number of stamps which require a minimum number of stamps to be selected to form a given total value.
For example, there are 1 points, 3 points, 3 points, 3 points, 4 points five stamps, which require 10 points, then 3 stamps are used: 3 points, 3 points, 4 points.
-
Input:
-
There are multiple sets of data, for each group of data, the first is the demand for a m,m<100 stamp value. Then there is a number n,n〈20, which indicates that there are N stamps. Next is a n positive integer representing the face value of the n stamps, in ascending order.
-
Output:
-
For each set of data, it is possible to sum up the minimum number of stamps in total M. If there is no solution, output 0.
-
Sample input:
-
1051 3 3) 3 4
-
Sample output:
-
3
Source Code:
#include <iostream>#defineINF 0x7fffffffusing namespacestd;structstamp{intweight; intvalue;}; intdp[ the]; Stamp arr[100000]; intMinval (intAintb) { returnA<b?a:b;} intMain () {intn,m; while(cin>>n>>m) { for(intI=1; i<=m;++i) {cin>>Arr[i].weight; Arr[i].value=1; } dp[0]=0; for(intI=1; i<=n;++i) dp[i]=INF; for(intI=1; i<=m;++i) { for(intj=n;j>=arr[i].weight;--j) { if(dp[j-arr[i].weight]!=INF) Dp[j]=minval (dp[j],dp[j-arr[i].weight]+arr[i].value); } } if(dp[n]==INF) cout<<0<<Endl; Elsecout<<dp[n]<<Endl; } return 0;} /************************************************************** problem:1209 User:lcyvino language:c++ Re sult:accepted time:160 Ms Memory:2300 kb****************************************************************/
-
Topic 1209: Minimum number of stamps