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 main()
{
int a[maxn];
int dp[maxn];
int s[maxn]; int n;
bool flag = false;
scanf("%d",&n);
for ( int i = ; i < n; i++ )
{
scanf("%d",&a[i]);
if ( !flag && ( a[i] >= ) )
{
flag = true;
}
} if ( !flag )
{
printf("0 %d %d",a[],a[n-]);
}
else
{
dp[] = a[];
s[] = ;
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 k = -;
int max = -;
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. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

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

  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. 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)

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

  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. 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)

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

  9. 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. C#关键字:访问修饰符

    一.访问修饰符 访问修饰符有public.private.protected.internal和protected internal.它们是修饰在类型(类.接口.委托.结构和枚举)和类型成员(字段.属 ...

  2. [转]Sublime Text 3安装Json格式化插件

    1.安装Package control 首先需要安装Package control,如果已经安装请跳过此步骤.按Ctrl+Shift+p打开命令搜索框,输入PC,点击图中条目安装,如下图:   成功后 ...

  3. python入学代码

    liwenhu=100 if liwenhu>=90: print("你很棒") elif liwenhu>=80: print("你很不错") e ...

  4. 【开发笔记】- Java写入、读取文本

    文本写入 public static void createFile(String input) throws IOException { //设置文件路径 String filePath = &qu ...

  5. Typora-yes:typora最舒适的使用-优化主题+图床服务+自动上传图片插件

    转载注明出处:https://www.cnblogs.com/nreg/p/11992678.html,谢谢 开源项目下载:https://github.com/nreg/typora-yes 云盘: ...

  6. 归并排序python实现源码

    将开发过程经常用到的一些代码片段收藏起来,下面的资料是关于归并排序python实现的代码,应该能对码农们有一些用. def mergesort(arr): if len(arr) == 1: retu ...

  7. 给 linux redis 设置密码

    在redis.conf 找到 下面添加一行: 这样的话,密码就可以设置成123456,然后重启redis就可以了.

  8. day 06 预科

    目录 if判断 if判断习题 for循环 for循环习题 微信机器人 if判断 # 一条狗朝你过来了,你会干吗? 判断: 如果狗是大长腿牵来的狗--->打招呼:如果是条疯狗,跑. # if:如果 ...

  9. Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW(转)

    转载:https://www.cnblogs.com/yxwkf/p/4840675.html 一.Telnet 采用Telnet用来訪问远程计算机的TCP/IP协议以控制你的网络设备,相当于在离开某 ...

  10. fastJSON的常用方法总结

    fastJSON的常用方法总结 fastJSON中常用的对象是JSON,JSONArray,JSONObject三个对象.常用的方法如对象转为JSON字符串,JSON字符串转为对象,JSON字符串转为 ...