hdu2546 01背包
2015-01-11 18:21
162 查看
如题:http://acm.hdu.edu.cn/showproblem.php?pid=2546
当饭卡里的钱数不足5元 什么都不能买,直接输出钱数
否则
升序排序,max是最之前的菜的钱数
钱数先减去5,然后01背包在(1.。。n-1)个菜中计算最高花费,最后输出m+5-f[m]-max
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MN 1002
#define MV 1002
#define max(a,b)(a>b?a:b)
int c[MN];
int f[MV];
int n,m;
int cmp(const void * a,const void * b)
{
return (*(int *)a)-(*(int *)b);
}
int main()
{
while(scanf("%d",&n)&&n)
{
memset(f,0,sizeof(f));
int i,j;
for(i=1;i<=n;i++)
scanf("%d",&c[i]);
scanf("%d",&m);
qsort(c+1,n,sizeof(int),cmp);
if(m<5)
{
printf("%d\n",m);
continue;
}
m-=5;
int max=c
;
for(i=1;i<n;i++)
for(j=m;j>=c[i];j--)
f[j]=max(f[j],f[j-c[i]]+c[i]);
printf("%d\n",m+5-f[m]-max);
}
}
当饭卡里的钱数不足5元 什么都不能买,直接输出钱数
否则
升序排序,max是最之前的菜的钱数
钱数先减去5,然后01背包在(1.。。n-1)个菜中计算最高花费,最后输出m+5-f[m]-max
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MN 1002
#define MV 1002
#define max(a,b)(a>b?a:b)
int c[MN];
int f[MV];
int n,m;
int cmp(const void * a,const void * b)
{
return (*(int *)a)-(*(int *)b);
}
int main()
{
while(scanf("%d",&n)&&n)
{
memset(f,0,sizeof(f));
int i,j;
for(i=1;i<=n;i++)
scanf("%d",&c[i]);
scanf("%d",&m);
qsort(c+1,n,sizeof(int),cmp);
if(m<5)
{
printf("%d\n",m);
continue;
}
m-=5;
int max=c
;
for(i=1;i<n;i++)
for(j=m;j>=c[i];j--)
f[j]=max(f[j],f[j-c[i]]+c[i]);
printf("%d\n",m+5-f[m]-max);
}
}
相关文章推荐
- hdu2546 饭卡 01-背包问题
- hdu2546-01背包
- hdu2546 饭卡_特殊的01背包
- HDU2546 01背包
- 01背包 hdu2546 hdu2602
- hdu2546 01背包 重学背包
- ACM-01背包之饭卡——hdu2546
- hdu2546 饭卡(01背包水)
- hdu2546 01背包 重学背包
- hdu2546 01背包
- hdu2546 饭卡(01背包 dp)
- hdu2546 饭卡 01-背包问题
- hdu2546(01背包变形)
- HDU2546 01背包
- hdoj 1203 01背包变形
- 01背包问题Java实现
- 01背包
- Hdu 3236 Gift Hunting(01背包变形)
- 01背包问题
- 01背包经典