Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4
#include<cstdio>
const int maxn = ;
int a[maxn] = {};
int dp[maxn] = {};
int s[maxn] = {}; int main(){
int n;
scanf("%d",&n);
bool flag = false;
for(int i = ; i < n; i++){
scanf("%d",&a[i]);
if(a[i] >= ) flag = true;
}
if(!flag){
printf("0 %d %d",a[],a[n-]);
return ;
}
//scanf("%d",&n);
dp[] = a[];
for(int i = ; i < n; i++){
if(dp[i-] + a[i] >= a[i]){
dp[i] = dp[i-]+a[i];
s[i] = s[i-];
}else{
dp[i] = a[i];
s[i] = i;
}
}
int max = dp[];
int k = ;
for(int i = ; i < n; i++){
if(dp[i] > max){
max = dp[i];
k = i;
}
}
printf("%d %d %d",max,a[s[k]],a[k]);
return ;
}

01-复杂度2 Maximum Subsequence Sum (25 分)的更多相关文章

  1. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  2. PTA 01-复杂度2 Maximum Subsequence Sum (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum   (25分) Given ...

  3. PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)

    1​​, N2N_2N​2​​, ..., NKN_KN​K​​ }. A continuous subsequence is defined to be { NiN_iN​i​​, Ni+1N_{i ...

  4. 01-复杂度2 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  5. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  6. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  7. 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  8. 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  9. 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  10. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

随机推荐

  1. 修改字段注释modify

    alter table test1 modify 字段名 类型 comment '修改后的字段注释'; ALTER TABLE tc_activity_miaosha MODIFY `validity ...

  2. python 文件操作(二)

    一. 文件的读写 1.找到文件 文件路径:./test.py 2.打开文件 open('文件路径','模式') 模式: r,   以只读的方式打开 w,  打开一个文件只用于写入,如文件已存在,直接重 ...

  3. css实现水平伸缩菜单

    ul li a{transition:width 500ms ease;} a:hover{width:*;} 高度向上延伸用height:*;margin-top:-*px;//负值实现向上

  4. 一起做RGB-D SLAM (5)

    第五讲 Visual Odometry (视觉里程计) 2016.11 更新 把原文的SIFT替换成了ORB,这样你可以在没有nonfree模块下使用本程序了. 去掉了cv::cv2Eigen函数,因 ...

  5. 'for each' statements are only available if source level is 5.0

    在用foreach的时候,出现以下错误: 错误:Syntax error, 'for each' statements are only available if source level is 5. ...

  6. 删除emacs临时文件

    emacs编辑文件后产生的带小尾巴(~)的文件,你有没有强迫症,要删之而后快rm -rf *~ 你有没有不小心敲成了rm -rf ~,然后爽快的按下了回车,然后欲哭无泪,哈哈- 其实,可以设置在ema ...

  7. A Multi-Sensorial Simultaneous Localization and Mapping (SLAM) System for Low-Cost Micro Aerial Vehicles in GPS-Denied Environments

    A Multi-Sensorial Simultaneous Localization and Mapping (SLAM) System for Low-Cost Micro Aerial Vehi ...

  8. SOAP协议初级指南 (三)

    独立元素 在SOAP中, 一个独立元素表示至少被一个多引用存取元素引用的类型的实例.所有的独立元素用soap:id属性作标记,而且这个属性的值在整个SOAP envelope中必须是唯一的.独立的元素 ...

  9. SPARK_sql加载,hive以及jdbc使用

    sql加载 格式  或者下面这种直接json加载 或者下面这种spark的text加载 以及rdd的加载 上述记得配置文件加入.mastrt("local")或者spark://m ...

  10. Gym - 100989H (贪心)

    After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...