PTA 01-复杂度2 Maximum Subsequence Sum (25分)
题目地址
https://pta.patest.cn/pta/test/16/exam/4/question/663
5-1 Maximum Subsequence Sum (25分)
Given a sequence of KK integers { N_1N1, N_2N2, ..., N_KNK }. A continuous subsequence is defined to be { N_iNi, N_{i+1}Ni+1, ..., N_jNj } where 1 \le i \le j \le K1≤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 KK (\le 10000≤10000). The second line contains KK 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 ii and jj (as shown by the sample case). If all the KK 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
/*评测结果
时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2017-07-08 15:45 正在评测 0 5-1 gcc 无 无
测试点结果
测试点 结果 得分/满分 用时(ms) 内存(MB)
测试点1 答案正确 13/13 2 1
测试点2 答案正确 2/2 1 1
测试点3 答案正确 2/2 1 1
测试点4 答案正确 2/2 1 1
测试点5 答案正确 2/2 1 1
测试点6 答案正确 2/2 1 1
测试点7 答案错误 0/1 1 1
测试点8 答案正确 1/1 3 1 不知道什么情况,卡了一个测试点
*/
#include<stdio.h>
int main()
{
int i,k;
int getNum,lastGetNum=-1,max=0;
int tempfirst=0,first=0,last=0;
int allNegtiveFlag=1,sum=0;
scanf("%d",&k);
for(i=0;i<k;i++)
{ scanf("%d",&getNum);
if(getNum>=0) allNegtiveFlag=0;
if (i==0) first=getNum;
if (sum==0 && getNum>0){
// if(lastGetNum!=0)
tempfirst=getNum;
}
sum+=getNum;
if (sum<0)
sum=0;
if (sum>max){
max=sum;
first=tempfirst;
last=getNum;
}
// lastGetNum=getNum;
if(i==k-1 && max==0 && allNegtiveFlag==1)
last=getNum; }
if(max==0 && allNegtiveFlag==0){
first=0;
last=0;
} printf("%d %d %d",max,first,last);
}
PTA 01-复杂度2 Maximum Subsequence Sum (25分)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum   (25分)
		01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ... 
- PAT - 测试  01-复杂度2 Maximum Subsequence Sum   (25分)
		1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ... 
- 01-复杂度2 Maximum Subsequence Sum (25 分)
		Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ... 
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
		1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ... 
- 1007 Maximum Subsequence Sum (25 分)
		1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ... 
- 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)
		Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ... 
- 01-复杂度2. Maximum Subsequence Sum (25)
		Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ... 
- 浙大数据结构课后习题  练习一 7-1 Maximum Subsequence Sum (25 分)
		Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ... 
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
		Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ... 
随机推荐
- 生产环境中使用脚本实现tomcat   start|status|stop|restart
			一.在实际生产环境中tomcat启动是在bin目录下采用自带脚本startup.sh启动:使用shutdown.sh关闭.如下图: 再如果对于新手来讲在不知道路径情况下重启是一件头痛的事情(注意没有r ... 
- RHEL 6.5----Nginx负载均衡
			实验环境 主机名 IP master 192.168.30.130 node-1 192.168.30.131 node-2 192.168.30.132 在master上安装 本次安装过程统一采用Y ... 
- ionic back 返回按钮不正常显示&&二级路由点击返回按钮失效无法返回到上一级页面的问题
			很多时候,app不只有一两级路由,还要三四级路由,但是在ionic中,给出的返回键三级或四级无法使用,所以得自定义方法设置返回. 直接贴代码: <ion-nav-buttons side=&qu ... 
- SpringBoot 2.x (7):拦截器
			类似以前SpringMVC的拦截器,但也有一些区别 SpringBoot的拦截器有两种方式: 第一种方式:过时的方式,适用于SpringBoot1.x的方式 package org.dreamtech ... 
- sql server 2012 从删库到跑路
			问题: 向sql server 2012单个数据库中导入1500万+条数据的时候,报错: 错误 0xc0202009: 数据流任务 1: SSIS 错误代码 DTS_E_OLEDBERROR.出现 O ... 
- AngularJS日期格式化
			本地化日期格式化:({{ today | date:'medium' }}) Mar 28, 2016 6:42:25 PM({{ today | date:'short' }}) 3/28 ... 
- Hadoop 安装过程中出现的问题
			1.hadoop-daemon.sh start namenode 启动失败 查看hadoop/logs 下面的日志 出现 2017-04-11 15:35:13,860 WARN org.apach ... 
- Android(java)学习笔记162:开发一个多界面的应用程序之两种意图
			1.两种意图: (1)显式意图: 在代码里面用intent设置要开启Activity的字节码.class文件: (2)隐式意图: Android(java)学习笔记218:开发一个多界面的应用程序之人 ... 
- hdu5739Fantasia(多校第二场1006) 割点+逆元
			Fantasia Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Proble ... 
- python之路——内置函数和匿名函数
			阅读目录 楔子 内置函数 匿名函数 本章小结 楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们 ... 
