Max Sum (动态规划)
Given a sequence a[1]a[2]a[3]......a[n] your job is to calculate the max sum of a sub-sequence. For example given (6-154-7) the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow each line starts with a number N(1<=N<=100000) then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case you should output two lines. The first line is "Case #:" # means the number of the test case. The second line contains three integers the Max Sum in the sequence the start position of the sub-sequence the end position of the sub-sequence. If there are more than one result output the first one. Output a blank line between two cases.
Sample Input
copy
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Sample Output
copy
Case 1: 14 1 4 Case 2: 7 1 6
#include <iostream> #include <algorithm> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <map> #include <vector> #include <set> #include <queue> #include <stack> #include <cmath> #define lli long long #define mem(s,t) memset(s,t,sizeof(s)) #define ok return 0; #define rep(x) for(int i=0;i<x;i++) cin>>a[i]; #define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); using namespace std; int a[100000+5]; int main() { TLE; int t,n,m,k,l,r,ll,rr,en; lli mx,cnt,ans,now; cin>>t; for(int tt=1; tt<=t; tt++) { cin>>n; rep(n); l=r=cnt=ans=ll=rr=en=0; mx=-10000000000; for(int i=0; i<n; i++) { if(cnt>=0) { now = cnt+a[i]; r = i; } else { now = a[i]; l = r = i; } if(now >= mx) { mx = now; ll = l ; rr = r; } cnt = now; } //cout<<mx<<endl; cout<<"Case "<<tt<<":"<<endl; cout<<mx<<" "<<ll+1<<" "<<rr+1<<endl;; if(tt!=t) cout<<endl; } ok; }
原文地址:https://www.cnblogs.com/Shallow-dream/p/11568012.html
- 剑指OFFER之用两个栈实现队列(九度OJ1512)
- 简单的客户机服务器投射模拟
- 使用gcc编译gdb调试
- 剑指OFFER之第一个只出现一次的字符(九度OJ1283)
- c++中类长度解析
- 剑指OFFER之丑数(九度OJ1214)
- 剑指OFFER之把数组排成最小的数(九度OJ1504)
- 剑指OFFER之从1到n中出现1的次数(九度OJ1373)
- 剑指OFFER之最大子向量和(连续子数组的最大和)(九度OJ1372)
- 剑指OFFER之最小的K个数(九度OJ1371)
- 剑指OFFER之数组中出现次数超过一半的数字(九度OJ1370)
- 如何成为一名10x的数据分析师?
- 肥料生产商全套三拼在手:拟融资1.47亿元
- 《外媒Cointelegraph专访Qtum量子链Jordan Earls》—浅谈目前Dapps的主要问题
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- PHP 实现Redis发布订阅消息及时通讯
- 简单几步,用云开发搞定短信验证码登录
- 重要的进程就让Supervisor 来守护吧!
- 机器学习基础:令你事半功倍的pipeline处理机制
- django 中如何将字典变量传给template视图层的JS
- Spring第三天:Spring的AOP的注解开发、Spring的声明式事务、JdbcTemplate
- Spring Boot中集成Slf4j 与Logback
- 一文搞定 Linux 常用高频命令
- 推荐一款科研必备的Python数据可视化神器——PyQtGraph
- 机器学习基础:可视化方式理解决策树剪枝
- 神级代码注释-这次是来搞笑的
- Gremlin 图查询概述
- JS,PHP,Python,Java对JSON数据的处理
- 基于Canal与Flink实现数据实时增量同步(二)
- Spring第四天:SSH的整合、HibernateTemplate的使用、OpenSessionInViewFilter的使用