zoukankan      html  css  js  c++  java
  • 【BZOJ】1606: [Usaco2008 Dec]Hay For Sale(背包)

    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 //最大可以背出来的体积
  • 相关阅读:
    16. Vue 登录存储
    JS 10位、13位时间戳转日期
    14.Vue 定义全局函数
    13.Vue+Element UI实现复制内容
    12.Vue+Element UI 获取input的值
    11.Vue安装Axios及使用
    Layui入手
    MongoDB自启动设置
    sql数据统计
    sql查询总结
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3949092.html
Copyright © 2011-2022 走看看