
4 12 10 7 5 1
2
题解:
完全背包,无语了,当两个都是0结束,错了半天,硬是没发现;
代码:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
const int MAXN=100010;
int bag[MAXN];
struct Node{
int m,v;
};
Node dt[110];
int main(){
int N,T;
while(scanf("%d%d",&N,&T),N|T){
for(int i=0;i<N;i++)
scanf("%d",&dt[i].m),dt[i].v=1;
mem(bag,INF);
bag[0]=0;
for(int i=0;i<N;i++)
for(int j=dt[i].m;j<=T;j++){
bag[j]=min(bag[j],bag[j-dt[i].m]+dt[i].v);
}
int ans=INF;
for(int i=T;i>=0;i--){
if(bag[i]<INF){
ans=bag[i];
break;
}
}
printf("%d\n",ans);
}
return 0;
}
原文:http://www.cnblogs.com/handsomecui/p/4968751.html