码迷,mamicode.com
首页 > 其他好文 > 详细

[Usaco2008 Dec]Hay For Sale 购买干草

时间:2018-02-04 00:29:06      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:algorithm   desc   etc   UI   最大   har   offer   它的   const   

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

这题没什么好说的了,一个裸的01背包,设f[i]代表能否凑出体积i,f数组为bool类型,然后转移就是f[i]=f[i]||f[[i-x];

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<‘0‘||ch>‘9‘;ch=getchar())  if (ch==‘-‘)    f=-1;
    for (;ch>=‘0‘&&ch<=‘9‘;ch=getchar())   x=(x<<3)+(x<<1)+ch-‘0‘;
    return x*f;
}
inline void print(int x){
    if (x>=10) print(x/10);
    putchar(x%10+‘0‘);
}
const int N=1e4;
bool f[N+10];
int main(){
    int n=read(),m=read();
    f[0]=1;//什么都不选是成立的
    for (int i=1;i<=m;i++){
        int x=read();
        for (int j=n;j>=x;j--)  f[j]=f[j]||f[j-x];
        //f[j]从f[j]转移过来代表不选x,从f[j-x]转移过来代表选x,j一定要从n开始倒着枚举,否则x会被选很多次(给不会DP的小朋友写的注释)
    }
    while (!f[n])   n--;//找到一个可行的最大的值
    printf("%d\n",n);
    return 0;
}

[Usaco2008 Dec]Hay For Sale 购买干草

标签:algorithm   desc   etc   UI   最大   har   offer   它的   const   

原文地址:https://www.cnblogs.com/Wolfycz/p/8411113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!