【DP经典系列】最大连续子序列和
来源:互联网 发布:java 版本 大小 编辑:程序博客网 时间:2024/06/08 09:03
Description
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,-1,5,4,-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
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
Sample Output
Case 1:14 1 4Case 2:7 1 6
题意:仍旧是最大连续子序列和;
注意输出格式就行;
AC代码:
#include <bits/stdc++.h>#define ll long longusing namespace std ;int main(){ int l , r , maxi , temp , now ; int p1, p2 ; int t ; cin>> t ; for(int cas = 1 ; cas <=t ; cas++) { int n ; cin>>n>>temp ; now=maxi=temp; r=l=p1=p2=1; for(int i=2; i<=n; i++) { cin>>temp; if(temp>now+temp) { now=temp; p1=i; } else { now+=temp; } if(now>maxi) { l=p1; r=i; maxi=now; } } printf("Case %d:\n",cas); printf("%d %d %d\n",maxi , l , r); if(cas!=t) printf("\n"); } return 0 ;}
0 0
- 【DP经典系列】最大连续子序列和
- 【经典DP】最大连续子序列和
- 最大连续子序列和(经典DP) 之 hdu 1231 最大连续子序列
- 动态规划dp经典题目:最大连续子序列和
- 【经典DP类型】 最大连续子序列和
- 最大连续子序列和(dp经典)
- HDU1003(最大连续子序列和DP)
- ZOJ1074 最大和子矩阵 DP最大连续子序列
- HDU1231 最大连续子序列【最大子段和+DP】
- HDU 1231 最大连续子序列(经典题目,DP)
- 最大连续子序列+dp
- 最大连续子序列(DP)
- 【dp】最大连续子序列
- hdu 1003/poj 1050 连续子序列最大和经典基础dp
- HDU 1003 Max Sum(最大连续子序列和 经典DP)
- HDU-1003-Max Sum(dp经典问题-最大连续子序列和)
- 经典 - 最大和/最大积连续子序列问题
- HDU--1231 : 最大连续子序列 (DP求连续子序列最大和)
- [转] 考拉BP神经网络的matlab实现学习历程(三)
- 借助Streaming用三种语言编写MapReduce
- 设计模式之策略模式
- windows下xml安装
- bzoj3504【CQOI2014】危桥
- 【DP经典系列】最大连续子序列和
- 铺地砖
- 机器学习之SVM算法(一)KKT条件
- windows下Perl安装使用
- 如何在安装好myecplise后添加新的jdk
- bzoj3505【CQOI2014】数三角形
- windows下diff安装使用
- Swift 项目中可能用到的第三方框架
- 求助一题C#作业