Given a sequence of K integers { N1​, N2​, ..., NK​ }. A continuous subsequence is defined to be { Ni​, Ni+1​, ..., Nj​ } where 1≤i≤j≤K. 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 (≤10000). 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 <stdio.h>
#include <stdlib.h> // 思路 在线搜索 int main(){
long k;
int t;
int a,b,c;
int flag = 0;
int Maxsize = 0;
int sum = 0;
int f = 0;
scanf("%ld",&k);
while (k--) {
scanf("%d",&t);
if(f == 0){
c = a = t;
f = 1;
}
sum = sum + t;
if(sum < 0){
sum = 0;
flag = 0;
}else{
if(sum == 0){
f = 4;
}
if(flag == 0){
a = t;
flag = 1;
}
}
if(sum > Maxsize){
f = 2;
Maxsize = sum;
c = a;
b = t;
}
} if(f == 2){
printf("%d %d %d",Maxsize,c,b);
}else if(f == 4){
printf("%d %d %d",Maxsize,a,a);
}else{
printf("%d %d %d",Maxsize,c,t);
} return 0;
}

PTA_Maximum Subsequence Sum的更多相关文章

  1. 【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广

    3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 174  Solved: 9 ...

  2. Algorithm for Maximum Subsequence Sum z

    MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...

  3. Maxmum subsequence sum problem

    We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...

  4. 中国大学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​​ }. ...

  5. PAT1007:Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  6. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  7. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

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

  9. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  10. Solutions for the Maximum Subsequence Sum Problem

    The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional ...

随机推荐

  1. 内容类型框架-ContentType 模型

    参考Django官方文档 ContentTypeManager¶ classContentTypeManager¶ ContentType 还有一个自定义管理器, ContentTypeManager ...

  2. execsnoop

    作用 execsnoop通过ftrace实时监控进程的exec()行为,输出短时进程的信息,包括进程 PID.父进程 PID.命令行参数以及执行的结果. 安装execsnoop git clone - ...

  3. File.createTempFile()

    File.createTempFile()的两个参数,是前缀和后缀,临时文件的文件名,会在前缀和后缀中各截取一部分,再拼接上随机数进行生成: 推荐手动指定目录,尽量指定到项目路径下,默认的磁盘位置,有 ...

  4. Linux&Android相关常用命令汇总记录

    Linux&Android相关常用命令汇总记录 0@Linux&Android系统命令行下如何查看命令的帮助信息: command --help 1@在Linux系统中,设备分为三类, ...

  5. cudnn Backend API注意事项

    一.在包含多个节点的图中,不支持in-place node.(如果图只包含一个节点,支持in-place node) Note that graphs with more than one opera ...

  6. [*]Is L2 Physics-Informed Loss Always Suitable for Training Physics-Informed Neural Network?

    NeurIPS 2022 本篇工作对PINN中的物理损失进行了探究,作者认为L2损失并不总是适用于训练PINN.并从PDE解的稳定性角度给出了理论性的说明.读了这篇文章,感觉自己的毕业论文做的十分浅显 ...

  7. google filament pbr

    https://google.github.io/filament/Filament.md.html

  8. ref、reactive、toRef、toRefs使用与区别

    reactive 传参:reactive(arg),arg只能是对象 arg为普通对象 返回响应式对象,不管层级多深,都能响应 使用:获取数据值的时候直接获取,不需要加.value 特点:解构.扩展运 ...

  9. PNAS:微生物组分析揭示人类皮肤的独特性

    https://blog.csdn.net/woodcorpse/article/details/87989663

  10. PostgreSQL 解析json字段

    一.解析json数组 select json_array_elements(lv_num_json)->'l1' lv,json_array_elements(lv_num_json)-> ...