标签:
这道题的意思是给你K中邮票, 每种邮票有无数个, 你可以从中选取一些邮票, 但是所选取的邮票总数不能超过N个, 问用K种邮票能组成连续几个面值?完全背包的变体, 直接定义f[i][j]为前i种邮票组成j最少需要多少张,剩下的就看代码:
/* ID: m1500293 LANG: C++ PROG: stamps */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int stamps[55]; short f[2][2000000+2]; int main() { freopen("stamps.in", "r", stdin); freopen("stamps.out", "w", stdout); int K, N; scanf("%d%d", &K, &N); int maxstamps = 0; for(int i=1; i<=N; i++) { scanf("%d", &stamps[i]); maxstamps = max(maxstamps, stamps[i]); } memset(f, 0x3f, sizeof(f)); f[0][0] = 0; for(int i=1; i<=N; i++) { memset(f[i%2], 0x3f, sizeof(f[0])); for(int j=0; j<=maxstamps*K; j++) { if(j >= stamps[i]) f[i%2][j] = min(f[(i-1)%2][j], (short)(f[i%2][j-stamps[i]]+1)); else f[i%2][j] = f[(i-1)%2][j]; } } int res = 0; for(int j=1; j<=maxstamps*K; j++) { if(f[N%2][j] <= K) res = j; else break; } printf("%d\n", res); return 0; }
标签:
原文地址:http://www.cnblogs.com/xingxing1024/p/5080377.html