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 ...
随机推荐
- WebSocket服务
package com.sxsoft.admin.Component; import com.alibaba.fastjson.JSON; import io.netty.handler.codec. ...
- 【git】3.1git分支-分支简介
资料来源 (1) https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%AE%80%E4%BB%8B ...
- WPF BackSpace 回退到上一个页面
在Wpf程序中,有时候点击到某些控件后,再按下[BackSpace]键,画面会回到上一个 TextBox可能自己处理了,所以没有这一个现象. 解决方案是: 在App.xaml.cs 的 Initial ...
- MySQL Delete 表数据后,磁盘空间并未释放,为什么?
有开发小哥咨询了一个问题,记录一下处理过程分享给有需要的朋友.问题如下:MySQL数据库中有几张表增删比较频繁.数据变动剧烈且数据量大,导致数据增长过快,磁盘占用多.为了节约成本,定期进行数据备份,并 ...
- 在Vue中实现app拍照-选取本地图库-图片上传成功后预览
基于Vue和uni-app实现手机app的功能实现和打包.拍照功能和选取本地图片使用的是HTML5的API 实现. 我为测试这个功能使用node写了个本地服务器,对于手机调试,可以通过连接同一个无线网 ...
- 【C++复习】5.7 多文件结构与编译预处理命令
1.C++项目结构 C++程序的一般组织架构 类声明文件(.h文件) 类实现文件(.cpp文件) 类的使用文件(main()所在的.cpp文件) 用工程组合各文件 2.编译链接 编译链接过程 3.外部 ...
- Xpath 常用语法展示
非标准代码处理 from lxml import etree #导入lxml 中erree模块 parser = etree.HTMLParser(encoding="utf-8" ...
- 【C学习笔记】day1-1 打印100~200之间的素数
#include<stdio.h> int sushu(int input) { int m = 0; for (int i = 1; i <= input; i++) { if ( ...
- windows下git bash不显示中文问题解决
问题: 在git bash下输入git log,查看log记录的时候中文显示不出来 解决方案: 网上查了很多解决方法,逐个尝试 1.尝试 git config --global core.quotep ...
- Swift中 堆(heap)和栈(stack)的区别
1.内存空间分为堆空间和栈空间 2.堆->引用类型(对象.函数.闭包) 栈->值类型(结构体.枚举.元组) 3.值类型赋值->深拷贝 引用类型赋值->浅拷贝 let a = ...