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. synchronized与CAS

    参考:java3y<对线面试官> synchronized synchronized是⼀种互斥锁,⼀次只能允许⼀个线程进⼊被锁住的代码块synchronized是Java的⼀个关键字,它能 ...

  2. 询问chatGPT的一些问题

  3. python单机版自动化测试框架源代码(selenium+Appium+requests+unittest+Excel用例+HTMLTestRunner报告)

    一.自动化测试框架: 1.框架和项目源代码下载 https://gitee.com/rmtic/autoTest 说明:框架可以支持web界面UI.安卓Android,ios苹果.接口API等自动化测 ...

  4. Vue源码主体分析

    vue的响应式和react的函数式编程思想是很不同的. vue最基本的原理是对getter和setter的代理模式.以及观察者模式. 当数据发生变化时,通知观察者. 而watcher是通过触发数据的g ...

  5. fix: because the volume group on the selected device also consist of physical volumes on other devices

    because the volume group on the selected device also consist of physical volumes on other devices 目标 ...

  6. iOS的一些性能优化

    1. 卡顿优化-CPU 尽量使用轻量级的对象,比如用不到事件处理的地方,可以考虑使用CALayer取代UIView 不要频繁地调用UIView的相关属性,比如frame.bounds.tranform ...

  7. python监控文件变化

    网址: https://blog.csdn.net/qq_40223983/article/details/102889329 起步在python中文件监控主要有两个库,一个是pyinotify,一个 ...

  8. 前端之JavaScript(Js)基础

    JavaScript,简称Js HTML三把利剑之一,浏览器可解析Js,看着和Java像,实则和Java并无关系,和Python.Go.C++等都是一门独立的语言. 一种轻量级的编程语言. 可插入 H ...

  9. vue把后端传的数字 转成中文显示在页面

  10. JS学习-PromiseWorker

    PromiseWorker PromiseWorker是一个ChromeWorker,调用而不是postMessage()发送消息,而是调用post(),它返回一个Promise. PromiseWo ...