[题目链接]
http://poj.org/problem?id=1742
[算法]
动态规划 + 贪心
[代码]
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 110 #define MAXM 100010 int i,j,n,m,cnt; int a[MAXN],c[MAXN],used[MAXM]; bool f[MAXM]; int main() { while (scanf("%d%d",&n,&m) && (n || m)) { memset(f,false,sizeof(f)); for (i = 1; i <= n; i++) scanf("%d",&a[i]); for (i = 1; i <= n; i++) scanf("%d",&c[i]); f[0] = true; for (i = 1; i <= n; i++) { for (j = 0; j <= m; j++) used[j] = 0; for (j = a[i]; j <= m; j++) { if (!f[j] && f[j - a[i]] && used[j - a[i]] < c[i]) { f[j] = true; used[j] = used[j - a[i]] + 1; } } } cnt = 0; for (i = 1; i <= m; i++) { if (f[i]) cnt++; } printf("%d ",cnt); } return 0; }