BZOJ 1606 [Usaco2008 Dec]Hay For Sale 购买干草 DP---背包

来源:互联网 发布:谷歌软件下载 编辑:程序博客网 时间:2024/05/29 03:57

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; three bales 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.

Source



传送门
就感觉是背包问题但是好别扭……
哦。。哦对了!
就是背包的体积可行性问题。
一开始dp[0]=true,
然后若dp[x]=true,dp[x+v[u]]=true。

然后其实有一丝贪心成分吧……分组还是不放贪心里了……。
最后就从大到小搜索到第一个为true的值,,
这个值就是最大的可以放的体积。

怎么感觉自己跑得有些慢……


#include<bits/stdc++.h>using namespace std;int read(){    int x=0,f=1;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 N=50005;int C,H;bool f[N];int main(){C=read(),H=read();int x; f[0]=1;for (int i=1;i<=H;i++){x=read();for (int j=0;j<=C-x;j++)f[j+x]|=f[j];}for (int i=C;~i;i--)if (f[i]){printf("%d\n",i);break;}return 0;}

原创粉丝点击