【USACO 3.1】Stamps (完全背包)

2020-06-02 11:13:15 浏览数 (1)

题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200。

题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出dp[j]=min(dp[j],dp[j-a] 1)。j从a到k*10000顺序枚举,因为类似于完全背包。

代码语言:javascript复制
/*
TASK:stamps
LANG:C  
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#define INF 0x3f3f3f3f
using namespace std;
int n,k;
int dp[2000005];
int main(){
    freopen("stamps.in","r",stdin);
    freopen("stamps.out","w",stdout);
    memset(dp,INF,sizeof dp);
    dp[0]=0;
    scanf("%d%d",&k,&n);
    int a;
    for(int i=1;i<=n;i  ){
        scanf("%d",&a);
        for(int j=a;j<=k*10000;j  )
            dp[j]=min(dp[j],dp[j-a] 1);
    }
    for(int i=1;;i  )
        if(dp[i]>k){
            printf("%dn",i-1);
            break;
        }
    /*
   Test 1: TEST OK [0.011 secs, 11992 KB]
   Test 2: TEST OK [0.011 secs, 11992 KB]
   Test 3: TEST OK [0.011 secs, 11992 KB]
   Test 4: TEST OK [0.011 secs, 11992 KB]
   Test 5: TEST OK [0.011 secs, 11992 KB]
   Test 6: TEST OK [0.011 secs, 11992 KB]
   Test 7: TEST OK [0.000 secs, 11992 KB]
   Test 8: TEST OK [0.011 secs, 11992 KB]
   Test 9: TEST OK [0.011 secs, 11992 KB]
   Test 10: TEST OK [0.076 secs, 11992 KB]
   Test 11: TEST OK [0.216 secs, 11992 KB]
   Test 12: TEST OK [0.076 secs, 11992 KB]
   Test 13: TEST OK [0.119 secs, 11992 KB]
    */
    return 0;
}

0 人点赞