题目链接:http://poj.org/problem?id=1742
http://acm.hdu.edu.cn/showproblem.php?pid=2844
题目分类:动态规划
代码:
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int n,m; bool flag[100009]; struct P { int a,b; }point[200]; int cmp(P X,P Y) { return X.a<Y.a; } int main() { while(scanf("%d %d",&n,&m)&&(n+m)) { memset(point,0,sizeof(point)); memset(flag,0,sizeof(flag)); for(int i=1;i<=n;i++) { scanf("%d",&point[i].a); } for(int i=1;i<=n;i++) { scanf("%d",&point[i].b); } sort(point+1,point+n+1,cmp); int sum[100005]; flag[0]=1; for(int i=1;i<=n;i++) { memset(sum,0,sizeof(sum)); for(int k=point[i].a;k<=m;k++) { if(flag[k-point[i].a]&&sum[k-point[i].a]<point[i].b&&!flag[k]) { sum[k] = sum[k-point[i].a] + 1; flag[k] = 1; } } } int ans=0; for(int i=1;i<=m;i++) { if(flag[i]) { //printf("sss== %d ",i); ans++; } } printf("%d ",ans); } return 0; }
#include<bits/stdc++.h> using namespace std; bool dp[100005]; int TmpVal[105]; int val[10005]; int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); int n, m; while(cin>>n>>m) { if(n==0 && m==0) break; memset(dp, 0, sizeof(dp)); for(int i=1;i<=n;i++) cin>>TmpVal[i]; int N = 0; for(int i=1;i<=n;i++) { int num; cin>>num; int j = 1; while(j<=num) { val[++N] = TmpVal[i] * j; num -= j; j = (j<<1); } if(num) { val[++N] = TmpVal[i] * num; } } dp[0] = 1; for(int i=1;i<=N;i++) { for(int j=m;j>=val[i];j--) { if(dp[j-val[i]]) dp[j] = 1; } } int ans = 0; for(int i=1;i<=m;i++) if(dp[i]) ans++; printf("%d ", ans); } return 0; }