http://www.lydsy.com/JudgeOnline/problem.php?id=1606

越来越水了T_T

这题两种做法,一个正规01背包,价值就是体积

还有一种是非正规背包,即

if(f[j-v[i]]) f[j]=1

然后从大向小扫出第一个f[i]==1的,i就是答案

越来越水了啊。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=50000;
int f[N], V, v; int main() {
read(V); int n=getint();
while(n--) {
read(v);
for(int i=V; i>=v; --i)
f[i]=max(f[i], f[i-v]+v);
}
print(f[V]);
return 0;
}

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 //最大可以背出来的体积

【BZOJ】1606: [Usaco2008 Dec]Hay For Sale(背包)的更多相关文章

  1. BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草( dp )

    -------------------------------------------------------------------- #include<cstdio> #include ...

  2. 01背包 || BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草 || Luogu P2925 [USACO08DEC]干草出售Hay For Sale

    题面:P2925 [USACO08DEC]干草出售Hay For Sale 题解:无 代码: #include<cstdio> #include<cstring> #inclu ...

  3. bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草【01背包】

    在洛谷上被卡常了一个点! 就是裸的01背包咯 为啥我在刷水题啊 #include<iostream> #include<cstdio> #include<algorith ...

  4. bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草

    Description     约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草.  顿因有H(1≤H≤5000)包 ...

  5. BZOJ——1606: [Usaco2008 Dec]Hay For Sale 购买干草

    http://www.lydsy.com/JudgeOnline/problem.php?id=1606 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1 ...

  6. BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草(动态规划)

    裸的背包= =,没什么好说的= = CODE: #include<cstdio>#include<iostream>#include<algorithm>#incl ...

  7. 1606: [Usaco2008 Dec]Hay For Sale 购买干草

    Description     约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为C(1≤C≤50000)个单位的马车,去顿因家买一些干草.  顿因有H(1≤H≤5000)包 ...

  8. bzoj1606[Usaco2008 Dec]Hay For Sale 购买干草(01背包)

    1606: [Usaco2008 Dec]Hay For Sale 购买干草 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1240  Solved: 9 ...

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

    1606: [Usaco2008 Dec]Hay For Sale 购买干草 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 612  Solved: 46 ...

随机推荐

  1. 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题

    1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...

  2. postgresql 函数demo

    create or replace function refresh_product_usage() returns void as $$ declare rec record; sub_rec re ...

  3. CPU tick counter

    #define rdtscll(val) \ __asm__ __volatile__ ("rdtsc" : "=A" (val)) example #incl ...

  4. win7 64位安装redis 及Redis Desktop Manager使用

    写基于dapper的一套自动化程序,看到 mgravell的另一个项目,StackExchange.Redis,之前在.NET上用过一段时间redis,不过一直是其它的驱动开发包,这个根据作者介绍,是 ...

  5. linux 下查看某个端口是否被占用

    lsof -i:端口号 转自: http://my.oschina.net/u/193184/blog/146885

  6. Nth to Last Node in List

    Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Exam ...

  7. 59. 总结篇:数组中N(n=1,2,3)个只出现一次的数字[find N numbers which appear only once in array]

    [本文链接] http://www.cnblogs.com/hellogiser/p/find-n-numbers-which-appear-only-once-in-array.html [题目] ...

  8. php 301

    2013年7月1日 13:35:45 PHP在301跳转的时候,如果是跨域跳转,记着把要跳转到的URL添上"http://"

  9. linux下跳板机跟客户端之间无密码登陆

    创建证书: [root@lnmp src]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which ...

  10. KVM中Linux虚拟机的硬盘添加方法

    [root@cache01 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_roo ...