【题目描述:】

约翰遭受了重大的损失:蟑螂吃掉了他所有的干草,留下一群饥饿的牛.他乘着容量为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. HDU 1079 Calendar Game(规律博弈)

    题目链接:https://cn.vjudge.net/problem/HDU-1079 题目: Adam and Eve enter this year’s ACM International Col ...

  2. 子shell以及什么时候进入子shell

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 子shell的概念贯穿整个shell,写shell脚本时更是不 ...

  3. SQL 时间段转换格式

    ), ): :57AM ), ): ), ): ), ): ), ): ), ): ), ): ), ): , ), ): :: ), ): :::827AM ), ): ), ): ), ): ), ...

  4. jQuery中的事件绑定的几种方式

    jQuery目前有on(),bind(),delegate(),live()四种绑定方式,但是随着版本的不断更新,有的方式也相应的被淘汰掉 [band()方式绑定] 3.0版本之前的绑定方式比较常用的 ...

  5. css居中的方法

  6. js+springMVC 提交数组数据到后台

    1.ajax 代码 var ids =new Array(); $.ajax({ type: "POST", url: "/user/downReport", ...

  7. Android 开发学习笔记

    1.Genymotion 解决虚拟镜像下载速度特别慢的问题 http://blog.csdn.net/qing666888/article/details/51622762 2.

  8. SpringMVC的启动

    Spring MVC中的Servlet Spring MVC中Servlet一共有三个层次,分别是HttpServletBean.FrameworkServlet和DispatcherServlet. ...

  9. sqlserver 2017 docker安装(启动代理)

    从 Docker Hub 中拉出 SQL Server 2017 Linux 容器映像. docker pull microsoft/mssql-server-linux:2017-latest 运行 ...

  10. Jmeter中默认语言的显示

    1.临时性语言的设置 即设置后只对本次使用有效,重启后恢复默认语言 选择Options—>Choose Language—>选择其他语言(例如:Chinese(Simplified)简体中 ...