【 USACO11JAN】 利润 【洛谷P3009】
P3009 [USACO11JAN]利润Profits
题目描述
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.
输入输出样例
7
-3
4
9
-2
-5
8
-3
14
说明
The maximum sum is obtained by taking the sum from the second through the sixth number (4, 9, -2, -5, 8) => 14.
这道题很裸,但是思想和方法非常重要,尽管比较简单。
不给详细解释,请大家仔细研读代码,如有不明,私信或评论或Q我(568251782)均可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib> const int MAXN = 100000 + 10;
const int INF = 99999999; int n,ans;
int num[MAXN]; int main()
{
scanf("%d", &n);
ans = -1*INF;
for(int i= 1;i <= n;i++)
{
scanf("%d", &num[i]);
if(num[i-1] > 0)
{
num[i] += num[i-1];
}
ans = std::max(ans, num[i]);
}
printf("%d", ans);
return 0;
}
【 USACO11JAN】 利润 【洛谷P3009】的更多相关文章
- 洛谷 P3009 [USACO11JAN]利润Profits
嗯... 题目链接:https://www.luogu.org/problemnew/show/P3009 这是DP的另一个功能,求最大子段和(最大子段和模板:https://www.luogu.or ...
- 洛谷 P1273 有线电视网
2016-05-31 13:25:45 题目链接: 洛谷 P1273 有线电视网 题目大意: 在一棵给定的带权树上取尽量多的叶子节点,使得sigma(val[选择的叶子节点])-sigma(cost[ ...
- 洛谷 P2762 太空飞行计划问题 P3410 拍照【最大权闭合子图】题解+代码
洛谷 P2762 太空飞行计划问题 P3410 拍照[最大权闭合子图]题解+代码 最大权闭合子图 定义: 如果对于一个点集合,其中任何一个点都不能到达此集合以外的点,这就叫做闭合子图.每个点都有一个权 ...
- 洛谷 P2317 [HNOI2005]星际贸易 解题报告
P2317 [HNOI2005]星际贸易 题目描述 输入输出格式 输入格式: 输出格式: 如果可以找到这样的方案,那么输出文件output.txt中包含两个整数X和Y.X表示贸易额,Y表示净利润并且两 ...
- 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling 题解
P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...
- 【00NOIP普及组】税收与补贴问题(信息学奥赛一本通 1911)( 洛谷 1023)
[题目描述] 每样商品的价格越低,其销量就会相应增大.现已知某种商品的成本及其在若干价位上的销量(产品不会低于成本销售),并假设相邻价位间销量的变化是线性的且在价格高于给 定的最高价位后,销量以某固定 ...
- 【线型DP】洛谷P2066 机器分配
[线型DP]洛谷P2066 机器分配 标签(空格分隔): 线型DP [题目] 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配 ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
随机推荐
- light oj 1098 数学规律
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
- TCP/IP四层模型和OSI七层模型(模型分层的作用是什么)
TCP/IP四层模型和OSI七层模型的概念(模型分层的作用是什么) 一.总结 一句话总结: 减轻问题的复杂程度,一旦网络发生故障,可迅速定位故障所处层次,便于查找和纠错: 在各层分别定义标准接口,使具 ...
- java使用stream流批量读取并合并文件,避免File相关类导致单文件过大造成的内存溢出。
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...
- <meta>标记
<meta>的主要作用: 是提供网页的元素信息 属性: http-equiv: 功能: 默认http协议文件头信息,当信息从服务器端传到客户端时,让浏览器正确的是显示, http_equi ...
- PAT甲级——A1006 Sign In and Sign Out
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- MyBatis注解开发-@Insert和@InsertProvider(转)
@Insert和@InsertProvider都是用来在实体类的Mapper类里注解保存方法的SQL语句.不同的是,@Insert是直接配置SQL语句,而@InsertProvider则是通过SQL工 ...
- Jmeter性能测试(第三篇)
一.调通脚本(以json串Post接口为例)添加聚合报告(线程组->添加->监听器->聚合报告)并调试好需要压测的脚本,如下已经调通的P_C_B151就是我需要压测的脚本 二.设置场 ...
- 分布式配置中心(Spring Cloud Config)
真有意思的一个问题,我先把我遇到的写一次 ,今天学习Spring Cloud Config 新建了三个module ,eureka-server,config-server,config-clien ...
- STL与泛型编程-第一周笔记-Geekband
1, 模板观念与函数模板 简单模板: template< typename T > T Function( T a, T b) {- } 类模板: template struct Obje ...
- 经典分类CNN模型系列其五:Inception v2与Inception v3
经典分类CNN模型系列其五:Inception v2与Inception v3 介绍 Inception v2与Inception v3被作者放在了一篇paper里面,因此我们也作为一篇blog来对其 ...