博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[USACO11JAN]利润Profits
阅读量:4663 次
发布时间:2019-06-09

本文共 1527 字,大约阅读时间需要 5 分钟。

题目描述

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特判)

代碼實現:

1 #include
2 #include
3 using namespace std; 4 int n,a,b,c,l=-3000,f[2][2]; 5 int main(){ 6 scanf("%d",&n); 7 for(int i=1;i<=n;i++){ 8 scanf("%d",&a); 9 b=i%2;c=(b+1)%2;l=max(l,a);10 f[b][0]=max(f[c][0],f[c][1]);11 f[b][1]=max(f[c][1]+a,a);12 }13 if(l>=0) printf("%d\n",max(f[n%2][0],f[n%2][1]));14 else printf("%d\n",l);15 return 0;16 }

還記得嗎?

转载于:https://www.cnblogs.com/J-william/p/6131251.html

你可能感兴趣的文章
Loadrunner的stock和web协议对应的事务检查点
查看>>
mvc扩展HtmlHelper功能
查看>>
codeforces #541 D. Gourmet choice(拓扑+并查集)
查看>>
ocilib linux编译安装
查看>>
linux 链接库说明
查看>>
基于本地文件系统的LocalDB
查看>>
黑马程序员 java基础加强--类加载器
查看>>
Win10环境下安装Django
查看>>
[Leetcode] Permutations
查看>>
mysqlbinlog flashback 5.6完全使用手册与原理
查看>>
1-1 07:输出浮点数
查看>>
软考知识点梳理--项目机会研究
查看>>
不同分布的转换问题(2016.11.18)
查看>>
ASP.NET页面使用JQuery EasyUI生成Dialog后台取值为空
查看>>
EDM营销之双十一最新实战营销指南汇总
查看>>
SpringBoot系列——mail
查看>>
到处都是jQuery选择器的年代,不了解它们的性能,行吗?
查看>>
SDN第三次上机作业
查看>>
用信号量进程同步与互斥
查看>>
labview状态机
查看>>