洛谷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. Dockerfil

    Dockerfile简介 dockerfile 是一个文本格式的配置文件, 用户可以使用 Dockerfile 来快速创建自定义的镜像,另外,使用Dockerfile去构建镜像好比使用pom去构建ma ...

  2. git push时出现 Username for 'https://github.com': 仅仅限于github

    使用git push origin master是出现如下问题:Username for 'https://github.com': 解决办法: git remote set-url origin g ...

  3. Windows环境Tomcat开启APR并配置http/2.0访问

    1.http/2.0需要开启https 参考:https://www.cnblogs.com/zhi-leaf/p/11978615.html 2.下载tcnative-1.dll文件 官网下载地址: ...

  4. ndt算法学习

    NDT算法原理: NDT算法的基本思想是先根据参考数据(reference scan)来构建多维变量的正态分布, 如果变换参数能使得两幅激光数据匹配的很好,那么变换点在参考系中的概率密度将会很大. 因 ...

  5. Debug 路漫漫-12:Python: ValueError: 'userid' is both an index level and a column label, which is ambiguous.

    啊,又遇到难题了 == 想要对两个 dataframe 做自然连接 merge,连接的key 为 “userid”,但是报错:ValueError: 'userid' is both an index ...

  6. LeetCode 203:移除链表元素 Remove LinkedList Elements

    删除链表中等于给定值 val 的所有节点. Remove all elements from a linked list of integers that have value val. 示例: 输入 ...

  7. OC 字典dictionaryWithObjectsAndKeys报错

    字典dictionaryWithObjectsAndKeys crash,也没有控制台打印输出: 解决方案!! 1.检查dictionaryWithObjectsAndKeys中的object key ...

  8. WPF中绘图(含调用GDI+)

    private void DrawStuff() { // //if (buffer == null) //{ // buffer = new RenderTargetBitmap((int)Back ...

  9. GAC 解释&路径

    GAC 全称是 Global Assembly Cache 作用是可以存放一些有很多程序都要用到的公共 Assembly ,例如 System.Data .System.Windows.Form 等等 ...

  10. MySQL基础(四)(子查询与链接)

    1.子查询简介 其中,所谓的“外层查询”并不是指“查找”,指的是所有SQL语句的统称:结构化查询语言(Structured Query Language),简称SQL. : 2.由比较运算符引发的子查 ...