洛谷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. <BackTracking> permutation 254 47 60

    254. Factor Combinations class Solution { public List<List<Integer>> getFactors(int n) { ...

  2. 【新特性速递】树表格结构由单层 TR 改为 TR-TD-TABLE 层级嵌套!

    由于历史原因,在之前实现树表格时,我们有点偷懒,本来应该是层级嵌套的树结构,被我们硬生生的拉平了,请看: 可以看到,basic目录的子节点和basic是在同一级别的,因为此目录尚未展开,所以这些子节点 ...

  3. python-9-列表的增删改查

    前言 本节是:列表(list)的增删改查.什么是列表? 列表(list)是最常用的Python数据类型,它可以作为一个方括号[]内的逗号分割值出现.如:[1,5,"b"] 一.增 ...

  4. linux 文件详细信息

    权限列: 权限列中一共 10 个字符,第一个字符代表文件类型,后边9 个字符 3 个为一组表示不同用户的权限: 第一字符表示文件类型: [d]:目录 [-]:普通文件 [l]:连接档 [b]:设备文件 ...

  5. mycat 白话配置

    1.server.xml <user name="root" defaultAccount="true"> <property name=&q ...

  6. Lambda 表达式构建初级示例(不完整)

    直接贴代码了: using System; using System.Collections.Generic; using System.Linq; using System.Linq.Express ...

  7. Neo4j 第十一篇:Cypher函数

    Cypher函数是对图进行查询和操作的重要工具. 一,谓词函数 谓词函数返回true或者false,主要用于检查是否存在或满足特定的条件. 1,Exists 如果指定的模式存在于图中,或者特定的属性存 ...

  8. C# 协变、逆变

    微软官方概述: 在C#中,协变和逆变能够实现数组类型.委托类型和泛型类型参数的隐式引用转换.协变保留分配兼容性,逆变则与之相反. 协变:能够使用与原始指定的派生类型相比,派生程度更大的类型. 逆变:能 ...

  9. jmeter返回结果出现乱码

    这是我的问题: 请求的百度地址,返回的数据是中文乱码 去jmeter安装目录的bin下: 修改配置文件jmeter.properties 右键打开 ,查找:sampleresult.default.e ...

  10. MTSC2019大会日程重磅发布,腾讯WeTest独家Topic大揭秘!

    WeTest 导读 中国移动互联网测试开发大会 Mobile Testing Summit China(简称 MTSC)是由国内最大的移动测试技术社区 TesterHome 发起的软件测试行业技术会议 ...