【题目描述:】

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

他最多可以运回多少体积的干草呢?

【输入格式:】

  • Line 1: Two space-separated integers: C and H

  • Lines 2..H+1: Each line describes the volume of a single bale: V_i

【输出格式:】

  • Line 1: A single integer which is the greatest volume of hay FJ can purchase given the list of bales for sale and constraints.



[算法分析:]

一看就是01背包,但是交上板子以后TLE了,加了炒鸡快读才刚好1000ms卡过

于是考虑优化算法:

类似于搜索的剪枝,对f[j]进行处理的时候,如果f[j]=c(最大容量)了,那输出c直接结束程序。

  • 优化前:1000ms

  • 优化后:108ms



[Code:]

#include<iostream>
#include<cstdio>
#define re register
using namespace std; const int MAXN = 5000 + 1;
const int MAXC = 50000 + 1; int n, c, v[MAXN];
int f[MAXC]; inline char gc()
{
static char buff[1000000],*S=buff,*T=buff;
return S==T&&(T=(S=buff)+fread(buff,1,1000000,stdin),S==T)?EOF:*S++;
} inline int read() {
int x = 0; char ch = gc();
while(!isdigit(ch)) ch = gc();
while(isdigit(ch))
x = (x << 3) + (x << 1) + ch - 48, ch = gc();
return x;
} int main() {
c = read(), n = read();
for(re int i=1; i<=n; ++i)
v[i] = read();
for(re int i=1; i<=n; ++i)
for(re int j=c; j>=v[i]; --j) {
f[j] = max(f[j], f[j-v[i]]+v[i]);
if(f[j] == c) {
printf("%d", c);
return 0;
}
}
printf("%d", f[c]);
}

【洛谷】【动态规划/01背包】P2925 [USACO08DEC]干草出售Hay For Sale的更多相关文章

  1. bzoj1606 / P2925 [USACO08DEC]干草出售Hay For Sale(01背包)

    P2925 [USACO08DEC]干草出售Hay For Sale 简化版01背包(连价值都免了) 直接逆推解决 #include<iostream> #include<cstdi ...

  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. 洛谷P2925 [USACO08DEC]干草出售Hay For Sale

    题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...

  4. 洛谷 P2925 [USACO08DEC]干草出售Hay For Sale

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P2925 这是一道简单的01背包问题,但是按照正常的01背包来做会TLE一个点,所以要加一个特判(见代 ...

  5. P2925 [USACO08DEC]干草出售Hay For Sale 题解

    \(\Huge{dp第一题}\) 题目描述 农民john面临一个很可怕的事实,因为防范失措他存储的所有稻草给澳大利亚蟑螂吃光了,他将面临没有稻草喂养奶牛的局面.在奶牛断粮之前,john拉着他的马车到农 ...

  6. 【洛谷P2925 [USACO08DEC]干草出售Hay For Sale】

    题意翻译 题目描述 农民john面临一个很可怕的事实,因为防范失措他存储的所有稻草给澳大利亚蟑螂吃光了,他将面临没有稻草喂养奶牛的局面.在奶牛断粮之前,john拉着他的马车到农民Don的农场中买一些稻 ...

  7. 洛谷——P2925 [USACO08DEC]干草出售Hay For Sale

    https://www.luogu.org/problem/show?pid=2925 题目描述 Farmer John suffered a terrible loss when giant Aus ...

  8. P2925 [USACO08DEC]干草出售Hay For Sale

    传送门 把每体积的干草价值看成一,就变成求最大价值 直接上背包就行了 注意优化常数 #include<iostream> #include<cstdio> #include&l ...

  9. AC日记——[USACO08DEC]干草出售Hay For Sale 洛谷 P2925

    题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...

随机推荐

  1. [LeetCode解题报告] 502. IPO

    题目描述 假设 LeetCode 即将开始其 IPO.为了以更高的价格将股票卖给风险投资公司,LeetCode希望在 IPO 之前开展一些项目以增加其资本. 由于资源有限,它只能在 IPO 之前完成最 ...

  2. ajax与文件上传

    一.ajax ajax(Asynchronous JavaScript And XML):异步JavaScript和XML,即使用JavaScript语句与服务器进行异步交互,传输的数据为XML(也可 ...

  3. Shell 实例:备份最后一天内所有修改过的文件

    在一个"tarball"中(经过 tar 和 gzip 处理过的文件)备份最后 24 小时之内当前目录下所有修改的文件. 程序代码如下: #!/bin/bash BACKUPFIL ...

  4. Android开发day-01

    http://note.youdao.com/noteshare?id=b7f0d55c1e5eab20bb47e5c58e683611

  5. sql语句中left join和inner join中的on与where的区别分析

    关于SQL SERVER的表联接查询INNER JOIN .LEFT JOIN和RIGHT JOIN,经常会用到ON和WHERE的条件查询,以前用的时候有时是凭感觉的,总是没有搞清楚,今日亲自测试了下 ...

  6. postgresql-10.1-3-windows-x64 安装之后,起动pgAdmin 4问题(win10)

    运行pgAdmin出现”pgAdmin 4  the application server could not be contant“ 窗口. 参考:https://stackoverflow.com ...

  7. javascript: checked 不可全选

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. linux学习笔记-目录结构(2)

    2./usr的意义与内容 依据FHS的基本定义,/usr里面放置的数据属于可分享的与不可变动的. usr是UNIX Software Resource的缩写,即UNIX操作系统软件资源所放置的目录,而 ...

  9. Visual Flow 简介

    Visual Flow(流) Salesforce提供了几种自动化流程工具,其中的Visual Flow(流)可以用来实现用户界面和逻辑,并对数据进行CRUD(Create 创建,Read 读取,Up ...

  10. Keras 中 TimeDistributed 和 TimeDistributedDense 理解

    From the offical code: class TimeDistributed(Wrapper): """This wrapper applies a laye ...