洛谷P3009 [USACO11JAN]利润Profits 题解

 https://www.luogu.org/problemnew/solution/P3009

JDOJ 2727: USACO 2011 Jan Silver 1.Profits

https://neooj.com/oldoj/problem.php?id=2727

题目描述

The cows have opened a new business, and Farmer John wants to see
how well they are doing. The business has been running for N (1 <=
N <= 100,000) days, and every day i the cows recorded their net
profit P_i (-1,000 <= P_i <= 1,000).

Farmer John wants to find the largest total profit that the cows
have made during any consecutive time period. (Note that a consecutive
time period can range in length from one day through N days.) Help
him by writing a program to calculate the largest sum of consecutive
profits.

输入

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains a single integer: P_i

输出

* Line 1: A single integer representing the value of the maximum sum
        of profits for any consecutive time period.

样例输入

7
-3
4
9
-2
-5
8
-3

样例输出

14

提示

OUTPUT DETAILS:

The maximum sum is obtained by taking the sum from the second through
the sixth number (4, 9, -2, -5, 8) => 14.

题意大致翻译如下:

奶牛们开始了新的生意,它们的主人约翰想知道它们到底能做得多好。这笔生意已经做了N(1≤N≤100,000)天,每天奶牛们都会记录下这一天的利润Pi(-1,000≤Pi≤1,000)。

约翰想要找到奶牛们在连续的时间期间所获得的最大的总利润。(注:连续时间的周期长度范围从第一天到第N天)。

请你写一个计算最大利润的程序来帮助他。

动态规划的基础题,相信大家都做过最大字段和的题目作为动归的入门题,其实这道题跟最大字段和的原理是一样的,奶牛在连续时间内获得的利润其实就是最大字段和,分析到这里就可以思路非常清晰地写出AC代码了:

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + ;
int dp[maxn],a[maxn],n;
int main()
{
cin>>n;
for(int i=;i<=n;++i)
cin>>a[i];
for(int i=;i<=n;++i)
dp[i] = max(dp[i-]+a[i],a[i]);
sort(dp+,dp+n+);
cout<<dp[n]<<endl;
return ;
}
 

USACO 利润Profits的更多相关文章

  1. [USACO11JAN]利润Profits

    题目描述 The cows have opened a new business, and Farmer John wants to see how well they are doing. The ...

  2. 洛谷 P3009 [USACO11JAN]利润Profits

    嗯... 题目链接:https://www.luogu.org/problemnew/show/P3009 这是DP的另一个功能,求最大子段和(最大子段和模板:https://www.luogu.or ...

  3. 【 USACO11JAN】 利润 【洛谷P3009】

    P3009 [USACO11JAN]利润Profits 题目描述 The cows have opened a new business, and Farmer John wants to see h ...

  4. 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)

    http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...

  5. [USACO精选] 第二章 动态规划(一)

    #4 公司利润 2014-01-16 这真的是动归?怎么觉得有点贪心的心态在.时间复杂度O(N),空间复杂度O(1),轻松加愉快!唯一要注意的是ANS一开始要赋负值,因为最终答案可能是负的. ; va ...

  6. usaco 地震 && 奶牛观光

    Usaco 地震: Description 一场地震把约翰家的牧场摧毁了,坚强的约翰决心重建家园.约翰已经重建了N个牧场, 现在他希望能修建一些道路把它们连接起来.研究地形之后,约翰发现可供修建的道路 ...

  7. [USACO]地震 (二分答案+最优比率生成树详解)

    题面:[USACO 2001 OPEN]地震 题目描述: 一场地震把约翰家的牧场摧毁了, 坚强的约翰决心重建家园. 约翰已经重建了N个牧场,现在他希望能修建一些道路把它们连接起来.研究地形之后,约翰发 ...

  8. [BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)

    [BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yu ...

  9. Mixing Milk 混合牛奶 USACO 贪心

    1009: 1.3.1 Mixing Milk 混合牛奶 时间限制: 1 Sec  内存限制: 128 MB提交: 9  解决: 9[提交] [状态] [讨论版] [命题人:外部导入] 题目描述 1. ...

随机推荐

  1. UAC简介

    用户帐户控制 (User Account Control) 是Windows Vista(及更高版本操作系统)中一组新的基础结构技术,可以帮助阻止恶意程序(有时也称为“恶意软件”)损坏系统,同时也可以 ...

  2. 解决Warning: Permanently added ' 192.168.1.230'(RSA) to the list of known hosts.

    前提 当我在刚安装的Red Hat Linux5.x系统中进行ssh 192.168.1.230 远程时,出现以下错误: Warning: Permanently added ' 192.168.1. ...

  3. keras和tensorflow搭建DNN、CNN、RNN手写数字识别

    MNIST手写数字集 MNIST是一个由美国由美国邮政系统开发的手写数字识别数据集.手写内容是0~9,一共有60000个图片样本,我们可以到MNIST官网免费下载,总共4个.gz后缀的压缩文件,该文件 ...

  4. Ubuntu中如何使得程序在后台运行

    Ubuntu中如何使得程序在后台运行 一.前言 在Ubuntu中有的程序启动需要执行改程序./bin目录下的文件,并且启动之后这个shell就不能使用和关闭了,非常的麻烦,因此就有了相应的命令来解决这 ...

  5. golang--单元测试综合实例

    实例说明: (1)一个Monster结构体,字段Name,Age,Skill (2)Monster有一个Store方法,可以将一个Monster对象序列化后保存在文件中: (3)Monster有一个R ...

  6. tensorflow之tf.shape()

    tf.shape()这个方法就相当于numpy当中shape属性. 下面通过列子来了解: 具体而言,tf.shape是用来获取张量的维度(shape).

  7. 如何配置Memcached高速缓存,加快wordpress的速度

    Memcached是什么 Memcached是一种高性能的分布式内存对象缓存系统.在动态应用,Memcached既能提高访问的速度,同时还减低了数据库的负载.DangaInteractive为提升Li ...

  8. RabbitMQ系列(一)rabbitmq简介

    ------------恢复内容开始------------ 参考:https://www.cnblogs.com/vipstone/p/9275256.html RabbitMQ简介 在介绍Rabb ...

  9. 面试官常问的Nginx的几个问题

    1.什么是Nginx? Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器 Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3 ...

  10. synchronized和volatile使用

    synchronized和volatile volatile :保证内存可见性,但是不保证原子性: synchronized:同步锁,既能保证内存可见性,又能保证原子性: synchronized实现 ...