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 ...
随机推荐
- JVM中类加载子系统
1.类加载子系统中有哪些常用类加载器? 1.BootStrapClassLoader(负责加载基础类库中的类,例如Object,String....) 2.ExtClassLoader(负责加载扩展类 ...
- solt废弃,报错解决方法
1.饿了么组件库给出得文字提示框 写到项目里之后报错 提示 solt已经废弃 <el-tooltip placement="top"> <div slot=&qu ...
- Jmeter 实现Json格式接口测试
接口Request Headers中的Content-Type和和charset 在"HTTP请求"中添加UTF-8 在"HTTP信息头管理器"中添加Conte ...
- Tushare金融大数据开放社区 - 数据抽取案例学习
进入平台介绍 扫码立即注册,更多大数据等你来探索 ! 案例: 导入tushare import tushare as ts 这里注意, tushare版本需大于1.2.10 设置token ts.se ...
- kafka日志数据清理策略
vim /kafka/server.properties # 日志清理策略优先级是谁先满足条件. # 保留7天的日志数据 log.retention.hours=168 # 日志数据总大小保留100G ...
- 阿里云部署OSS对接TP项目
1.配置文件写入参数 domain为阿里云oss存储实例要绑定的域名 2.获取accesskeyId和secret 注册用户 出现下图,选择"开始使用子用户Access Key" ...
- vue 项目中笔记-持续更新
9.class常用操作: :class="packUp ? 'search-form-btn active fr' : 'search-form-btn fr'" :class=& ...
- tp3.2 写入日志
function logs($content, $subDir = ''){ $subDir = trim($subDir, '/\\'); if (empty($content)) return f ...
- px、em、rem的区别css单位换算
在对页面进行自适应时,查阅相关资料了解到,通常有两种方式将px转换为em或者rem,下面就上述三者之间的联系进行讲解: em与px转换 一般浏览器默认1em=16px,通过设置font-size大小来 ...
- [BOM]前端解析cookie为对象
参考解析页面传参 var cookie_str = document.cookie var cookie_arr = cookie_str.split("; ") var cook ...