【题目描述:】

约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为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. Mysql常用单词

    Create 建造,创造Data数据Database数据库(缩写db)If如果Not没有Exists存在Table表Drop 去掉Delete删除Show展示Select选择Default默认Uniq ...

  3. Ado.Net实体数据模型EF,如何在代码中添加数据库连接密码

    在创建EF模型的时候,VS2013提示说“在连接字符串中存储敏感数据可能有安全风险”,于是我选择了在代码中添加,可是如何通过代码添加呢? 我在网上百度了下,没有人说的清楚直观. 假设我们创建了一个名字 ...

  4. Gulp Error: Cannot find module 'jshint/src/cli'

    I'm following sitepoint's An introduction to Gulp.js, but I'm stuck on step four, when I try to run  ...

  5. Linux中ansible批量管理软件部署及剧本编写

    服务器版本信息: Centos6.9 [root@db02 ~]# uname -a Linux db02 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29: ...

  6. 【github&&git】5、使用Git拉取GitLab上的项目

    一.安装Git(windows版.其他平台参阅) 去Git的官网,下载安装包,安装时,一路默认 二.配置Git 2.1 在任意地方,创建一个文件夹,保证该文件夹的目录全部是英文 2.2 打开新建的文件 ...

  7. curl 发送 post 请求

    curl -i -X POST -H 'Content-type':'application/json' -d '{"keyWord":"雅诗兰黛"," ...

  8. html常用标签整理

    html文档结构 <!DOCTYPE html> <html lang="zh-CN"> #这个lang表示语言,zh-CN中文的意思,整个文档的内容以中文 ...

  9. ArcGIS Server Rest 认证过程分析

    1. http://192.168.1.220:6080/arcgis/admin/login?redirect= Request URL: http://192.168.1.220:6080/arc ...

  10. 安卓开发_关于WebView使用链接时调用浏览器显示的问题

    在我们的实际开发中,我们用到WebView就是为了在自己的APP中的某个部分来显示指定网页的效果. 但是在学习的过程中,我发现一个问题: 有的网页使用WebView控件显示出来以后,再点击网页中的某个 ...