题目描述

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.

牛们开了家新公司,这家公司已经运作了N天,财务报表显示第i天获得的利润为Pi , 有些天的利润可能是个负数。约翰想给奶牛公司写个新闻报道,以吹嘘她们的业绩。于是他 想知道,这家公司在哪一段连续的日子里,利润总和是最大的。

输入输出格式

输入格式:

  • 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.

输入输出样例

输入样例#1:

7
-3
4
9
-2
-5
8
-3
输出样例#1:

14

说明

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

簡單動規。(因為方程沒法應付全負的情況,加了個l特判)

代碼實現:

 #include<cstdio>
#include<iostream>
using namespace std;
int n,a,b,c,l=-,f[][];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a);
b=i%;c=(b+)%;l=max(l,a);
f[b][]=max(f[c][],f[c][]);
f[b][]=max(f[c][]+a,a);
}
if(l>=) printf("%d\n",max(f[n%][],f[n%][]));
else printf("%d\n",l);
return ;
}

還記得某種數列問題嗎?

[USACO11JAN]利润Profits的更多相关文章

  1. 洛谷 P3009 [USACO11JAN]利润Profits

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

  2. USACO 利润Profits

    洛谷P3009 [USACO11JAN]利润Profits 题解  https://www.luogu.org/problemnew/solution/P3009 JDOJ 2727: USACO 2 ...

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

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

  4. 从键盘输入当月利润I,求应发放奖金总数?

    企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%:20万到40万之间时 ...

  5. Qt带来的是更加低廉的开发成本和学习成本,对于很多小公司而言,这种优势足以让他们获得更大的利润空间 good

    不能单纯从技术上来看待这个问题,Qt本来是小众的开发平台,个人认为,它的出现只是解决特性场景的特定问题,Qt带来的是更加低廉的开发成本和学习成本,对于很多小公司而言,这种优势足以让他们获得更大的利润空 ...

  6. ecshop 后台添加 成本价 利润

    ecshop后台admin中的商品操作php文件,goods.php替换为下面的代码, 还要在数据库商品本店售价后门添加  cost  字段 为 商品成本价 ecs_goods表中添加  cost  ...

  7. Algorithm --> 投资组和求最大利润

    投资组和求最大利润 题目: 投资人出资一笔费用mount,投资给不同的公司(A,B,C....),求最大获取利润? 例如:投资400百万,给出两家公司A和B: 1.如果投资一百万给A公司,投资3百万给 ...

  8. [USACO11JAN]大陆议会The Continental Cowngress_2-sat

    [USACO11JAN]大陆议会The Continental Cowngress_2-sat 题意: 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会. 议会以“每头 ...

  9. 『战略游戏 最大利润 树形DP』

    通过两道简单的例题,我们来重新认识树形DP. 战略游戏(luoguP1026) Description Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要 ...

随机推荐

  1. _x、__x、__x__含义与区别

    _x是一种弱表示,它用在类中的属性或方法,表示是private属性,希望外部使用者不要直接调用它.但它只是暗示,没有任何限制性措施. private属性主要推荐的还是这种方式,因为Python的设计理 ...

  2. 标准库源码--wsgi服务器

    功能模块化带来可自由组装的便利: 使用python的mixin特性装配 class ThreadHTTPServer(ThreadingMixin, HTTPServer): pass 上面表示1个请 ...

  3. POJ 2559 Program C

    Submit Status Practice POJ 2559 Description A histogram is a polygon composed of a sequence of recta ...

  4. ubuntu 添加源

    edit file :  /etc/apt/sources.list add: deb http://mirrors.163.com/ubuntu/ intrepid main restricted ...

  5. JS随鼠标坐标移动,显示浮动层内容

    在表单等项目中往往会遇到类似于“备注”.“说明”等100个字内的内容需要显示. 这些内容如果全部呈现开,会影响布局和美观,确又没有必要设计一个层或是一个页面. 那么,我们可以把这些内容放到浮动层中,鼠 ...

  6. matlab 画框(一)

    matlab进行图像处理之后,很多时候需要在图像上画出矩形框:如,调用matlab的某个检测函数,得到结果之后,往往需要将检测结果的矩形框画在图像上,直观.方便的进行查看:下面的代码就是这个目的: f ...

  7. 《Java实验四》

    //实验4--附录一代码 public class PassValueTest { //静态代码块,类一加载就执行的部分. //所以运行这个程序会输出 class loding static { Sy ...

  8. Android开发中的ANR问题如何避免?

    在Android中, Activity Manager 和 Window Manager system services 会监控每个程序的运行,当程序出现如下三种种情况的时候就会弹出ANR的提示对话框 ...

  9. Iterator之java.util.ConcurrentModificationException

    在运行以下代码时,会报java.util.ConcurrentModificationException异常, public class Demo { public static void main( ...

  10. R函数是对A方法的封装

    $user = new UserController;  ===      $user=A("User"); $user = new UserController; $user-& ...