Luo Gu P2725 Stamps Stamps
https://www.luogu.org/problem/P2725
JDOJ 1797: Stamps Stamps
https://neooj.com:8082/oldoj/problem.php?id=1797
Description
Given a set of N denominations of stamps (e.g., {1, 3,}), and an upper limit K - K represents a stamp can be affixed on the envelope calculated maximum postage continuously posted from 1 to M.
E.g. , assuming there are 3 points and 1 point stamps; you can stick easily up to five posted stamps 1 to 5 minutes postage (1 minute by the stamps on the line), the next postage is not difficult:
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, the use of 5 or 1 min 3 min 14 min stamp impossible to put postage. Thus, for both the stamps and the set upper limit K = 5, the answer is M = 13.
Input
Line 1: two integers, K and NK (1 <= K <= 200) is the total number of available stamps .N (1 <= N <= 50) is the number of postage stamp.
End of the line 2 .. File: N integers, each row 15, lists all of the N denomination stamps, face value not more than 10,000.
Output
Line 1: an integer from 1 pm start contiguous set of available no more than the number of postage stamps K posted.
Sample Input
Sample Output
#include <cstdio> #include <algorithm> the using namespace STD; int K, n-; int DP [ 2000001 ]; // DP [i] represents the number of denominations of stamps when the minimum needed i int main () { Scanf ( " % D% D " , & K, & n-); for ( int I = . 1 ; I <= 2000001 ; I ++ ) DP [I] = 2147483647 ; DP [ 0 ] = 0 ; for ( int I = . 1 ; I <= n; i ++) { int a; scanf("%d",&a); for(int j=a;j<=2000001;j++) if(dp[j-a]+1<=k) dp[j]=min(dp[j],dp[j-a]+1); } for(int i=1;i<=2000001;i++) if(dp[i]==2147483647) { printf("%d",i-1); return 0; } }
JDOJ
#include<iostream> #include<algorithm> #include<cstring> #define maxv 2000005 #define inf 0x3f3f3f3f using namespace std; int n,k,ans,mx,c[55]; int dp[maxv]; int main() { cin>>k>>n; memset(dp,inf,sizeof(dp)); dp[0]=0; for(int i=1;i<=n;i++) cin>>c[i]; sort(c+1,c+n+1); for(int i=1;i<=n;i++) { mx=c[i]*k; for(int j=c[i];j<=mx;j++) if(dp[j-c[i]]<min(k,dp[j]-1)) dp[j]=dp[j-c[i]]+1; } for(int i=1;i<maxv;i++) { if(dp[i]!=inf) ans++; else break; } Cout << years; return 0 ; }