【经典DP类型】 最大连续子序列和
来源:互联网 发布:java 版本 大小 编辑:程序博客网 时间:2024/05/22 13:43
Description
HenryFour has a number of stones which have different values from -4444 to 4444. He puts N stones in a line and wants to find the max partial value of these N stones.
Assume the values of the N stones in line are: v1, v2, v3, v4, ..., vN. The partial vaule of stones from Lth stone to Rth stone (1 ≤ L ≤ R ≤ N) is the sum of all the stones between them. i.e. PartialV(L, R) = v[L] + v[L+1] + .... + v[R] (1 ≤ L ≤ R ≤ N)
Since the number of stones (N) is very very large, it is quite difficult for HenryFour to find the max partial value. So could you develop a programme to find out the answer for him?
Assume the values of the N stones in line are: v1, v2, v3, v4, ..., vN. The partial vaule of stones from Lth stone to Rth stone (1 ≤ L ≤ R ≤ N) is the sum of all the stones between them. i.e. PartialV(L, R) = v[L] + v[L+1] + .... + v[R] (1 ≤ L ≤ R ≤ N)
Since the number of stones (N) is very very large, it is quite difficult for HenryFour to find the max partial value. So could you develop a programme to find out the answer for him?
Input
There are several test cases in the input data. The first line contains a positive integer T (1 ≤ T ≤ 14), specifying the number ot test cases. Then there are T lines. Each of these T lines contains a positive number N followed by N integers which indicate the values of the N stones in line.
1 ≤ N ≤ 1,000,000
-4444 ≤ v[i] ≤ 4444
1 ≤ N ≤ 1,000,000
-4444 ≤ v[i] ≤ 4444
Output
Your program is to write to standard output. For each test case, print one line with three numbers seperated by one blank: P L R. P is the max partial value of the N stones in line. L and R indicate the position of the partial stones. If there are several Ls and Rs that have the same value PartialV(Li, Ri) = P, please output the minimum pair. For pair (Li, Ri) and (Lj, Rj), we define (Li, Ri) < (Lj, Rj) if and only if: Li < Lj or (Li == Lj and Ri < Rj)
Sample Input
34 32 -39 -30 -288 1 2 3 -10 1 -1 5 110 14 -12 -8 -13 3 5 42 -24 -32 -12
Sample Output
32 1 16 1 350 5 7
Hint
Huge input and output,scanf and printf are recommended.
大致题意:找出子串中和最大的,并且标注区间, 套路题;
AC代码:
#include <iostream>using namespace std;int main(){ int n,m,i,l ,r ,y; long long max,p1,p2,now,temp; cin>>n; while(n--) { cin>>m>>temp; now=max=temp; r=l=p1=p2=1; for(i=2; i<=m; i++) { cin>>temp; if(temp>now+temp) { now=temp; p1=i; } else { now+=temp; } if(now>max) { l=p1; r=i; max=now; } } cout<<max<<" "<<l<<" "<<r<<endl; } 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求连续子序列最大和)
- C# 断点不赋值问题
- 写一个函数判断存储空间是大端还是小端
- bzoj3503【CQOI2014】和谐矩阵
- JavaScript常用互动方法
- cocos2d-x 2.x打飞机(三)
- 【经典DP类型】 最大连续子序列和
- leetcode: 226. Invert Binary Tree
- windows下bison安装使用
- 程序员的生活和职业,保证快乐的基础上,再谈工作和学习
- PHP的单个文件上传、多个单文件上传、多文件上传
- [转] 考拉BP神经网络的matlab实现学习历程(三)
- 借助Streaming用三种语言编写MapReduce
- 设计模式之策略模式
- windows下xml安装