POJ 1742
男人八题之一,多重背包可行性探讨,也算对当时背包问题多重背包部分学习的首尾,具体算法可以见崔天翼关于多重背包可行性O(VN)的算法,不过,据说可以利用队列优化,不过尝试了下,由于没有一个参考,不太明白优化的意思时间反而超了,所以暂时作罢
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <deque>
using namespace std;
const int maxn= 105;
const int maxm= 1e5+5;
int a[maxn], c[maxn];
int dv[maxm];
int main(int argc, char const *argv[])
{
int n, m;
while (2== scanf("%d %d", &n, &m) && n){
for (int i= 1; i<= n; ++i){
scanf("%d", a+i);
}
for (int i= 1; i<= n; ++i){
scanf("%d", c+i);
}
for (int j= 1; j<= m; ++j){
dv[j]= -1;
}
dv[0]= 0;
for (int i= 1; i<= n; ++i){
for (int j= 0; j<= m; ++j){
if (dv[j]>= 0){
dv[j]= c[i];
}
else{
dv[j]= -1;
}
}
int co= a[i];
int uj= m-co;
for (int j= 0; j<= uj; ++j){
if (dv[j]> 0){
dv[j+co]= max(dv[j+co], dv[j]-1);
}
}
}
int ans= 0;
for (int j= 1; j<= m; ++j){
if (dv[j]>= 0){
++ans;
}
}
printf("%d\n", ans);
}
return 0;
}