您的位置:首页 > 其它

BZOJ1606: [Usaco2008 Dec]Hay For Sale 购买干草

2017-10-11 15:57 465 查看
标签:背包DP
Description
    约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草.  顿因有H(1≤H≤5000)包干草,每一包都有它的体积Vi(l≤Vi≤C).约翰只能整包购买,
他最多可以运回多少体积的干草呢?
Input
    第1行输入C和H,之后H行一行输入一个Vi.
Output
 
    最多的可买干草体积.
Sample Input
7 3  //总体积为7,用3个物品来背包

2

6

5

The wagon holds 7 volumetric units; threebales are offered for sale with

volumes of 2, 6, and 5 units, respectively.

Sample Output
7  //最大可以背出来的体积

HINT
Buying the two smaller bales fills the wagon.
 

Code

#include<bits/stdc++.h>
#define LL long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
using namespace std;
inline LL read()
{
LL f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=5e4+6;
int n,v,a[maxn],f[maxn];

int main()
{
v=read(),n=read();
rep(i,1,n)a[i]=read();
f[0]=1;
rep(i,1,n)
dep(j,v,a[i])
if(f[j-a[i]])f[j]=1;
dep(j,v,0)
if(f[j]){cout<<j<<endl;return 0;}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bzoj 动态规划 dp