hdu-2546 饭卡
http://acm.hdu.edu.cn/showproblem.php?pid=254
6
0-1背包问题
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int cmp(int a,int b){
return a<b;
}
int main(){
int n;
int price[1200],dp[1200];
int i,j,k,t,m;
while(~scanf("%d",&n),n){
memset(price,0,sizeof(price));
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
scanf("%d",&price[i]);
scanf("%d",&m);
sort(price,price+n,cmp);
if(m<5){
printf("%d\n",m);
continue;
}
m-=5;
for(i=0;i<n-1;i++){
for(j=m;j>=price[i];j--)
dp[j]=max(dp[j],dp[j-price[i]]+price[i]);
}
// printf("%d\n",dp[n-1]);
printf("%d\n",m+5-dp[m]-price[n-1]);
}
return 0;
}
6

浙公网安备 33010602011771号