PTA_Maximum Subsequence Sum
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的更多相关文章
- 【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 ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- Maxmum subsequence sum problem
We have a lot of ways to solve the maximum subsequence sum problem, but different ways take differen ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 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 ...
- Solutions for the Maximum Subsequence Sum Problem
The maximum subarray problem is the task of finding the contiguous subarray within a one-dimensional ...
随机推荐
- uniapp-时间组件
可以选择年月日时分秒 示例文件 my-datetime.zip 文档:https://ext.dcloud.net.cn/plugin?id=5603
- spark闭包检查
spark在执行算子时,如果算子内部用到了外部(Driver)端的对象或变量,就一定会出现闭包:spark在执行算子之前会进行闭包检查,也就是对外部对象或变量进行序列化检查:
- 《Rust权威指南》学习笔记——8.通用集合类型
Rust通用集合类型 动态数组Vec 字符串String 和&str 哈希映射HashMap
- git常见问题集合
注1:问题总结来自于实际使用,关于搜到的资料链接一并粘贴; 场景1:GIT本地代码处于detached HEAD的情况(又称游离状态)的解决办法; 问题:有时候git由于一些操作的问题出现了detac ...
- WEB的安全性测试要素【转】
原文链接:http://www.cnblogs.com/zgqys1980/archive/2009/05/13/1455710.html WEB的安全性测试要素 1.SQL Injection(SQ ...
- react 基础知识
基础知识 css-module react 将js转为vdom,react-dom将vdom转为dom // 外面一层是一个动态值,里面的表示的是对象 <img src={logo} style ...
- vant-ui经验
1.van-checkbox搭配van-cell使用,点击圆圈区域,不触发定义的click函数. 解决:给van-checkbox添加一层div,添加click事件:van-checkbox也添加cl ...
- HDLbits——Exams/m2014 q4k
//四级移位寄存器 module top_module ( input clk, input resetn, // synchronous reset input in, output reg out ...
- Hub
public class StreamHub : Hub { public ChannelReader<string> ReadLogStream() { var channel = Ch ...
- 记录:安装nginx
练习的项目,数据都是跨域获取,上线后就不能再获取到数据,就用到nginx来做代理 注意点: 我用的是阿里云轻量服务器,防火墙在默认情况下是把80端口占据了,然而,安装nginx后,ngi ...