PAT 解题报告 1007. Maximum Subsequence Sum (25)
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
题目描述:
求最大连续子段和并且输出该最大字段和序列的第一个和最后一个元素
算法分析:
思路1、暴力搜索
O(N2)的时间复杂度,K<10000,不超时
int sum = , mx = -INF, s, t;
for (int i=; i<K; i++) {
sum = ;
for (int j=i; j<K; j++) {
sum += n[j];
if (sum > mx) {
mx = sum;
s = i;t = j;
}
}
}
printf("%d %d %d", mx,n[s], n[t]);
思路2、扫描
经典DP问题, 基于这样一个事实:保存一个最大字段和以及一个当前子段和, 如果当前字段和大于当前最大字段和, 那么更新这个最大字段和, 如果当前字段和为负数的时候, 直接把当前字段和甚设置成0, 求最大字段和算法如下:
int MaxSum(int A[], int N) {
int currentSum = ;
int maxSum = ;
for(int i = ; i < N; ++i) {
currentSum += A[i];
if(currentSum > maxSum) maxSum = currentSum;
else if(currentSum < ) currentSum = ;
}
return maxSum;
}
由于这个题目里面还需要保存最大子段和的第一个和第二个元素, 加一些额外的变量记录以及在对应的更新maxSum和把currentSum设置成0的时候对应进行维护就行了. 算法复杂度O(N)
注意点:
当序列中有0但是其他都是负数的时候, 不是输出真个序列的第一个和最后一个元素,而是输出第一个0. 比如3 -1 0 -1 应当输出 0 0 0
PAT 解题报告 1007. Maximum Subsequence Sum (25)的更多相关文章
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)
简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are sup ...
- 【PAT甲级】1007 Maximum Subsequence Sum (25 分)
题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...
- 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 ...
- PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 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 ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
随机推荐
- Redis学习手册(Key操作命令)
一.概述: 在该系列的前几篇博客中,主要讲述的是与Redis数据类型相关的命令,如String.List.Set.Hashes和Sorted-Set.这些命 令都具有一个共同点,即所有的操作都是针对与 ...
- IT运维的五大基础知识
IT运维的五大基础知识 | 浏览:331 | 更新:2014-09-25 11:36 IT运维对于很多企业都很重要,接下来运维的一些基础知识天天客服IT运维总监龙少文,就给大家介绍下IT运维的基础知识 ...
- 解决Ecshop因为动态ip问题登录后台自动退出
解决Ecshop因为动态ip问题登录后台自动退出 PHP 铁匠 2年前 (2013-07-21) 1130℃ 0评论 修改lib_base.php文件real_ip()函数,添加以下代码即可解 ...
- 如何开启mysql计划事件
如何开启mysql计划事件 (2012-07-26 12:21:23) 转载▼ 标签: mysql 事件计划 it 分类: MySQL 首先在sql中查询计划事件的状态:SHOW VARIABLES ...
- Rails--default_scope
Example: default_scope where("agents.deleted = ?", false)
- Jquery scrollTop animate 實現動態滾動到頁面頂部
這個方法之前都是用的錨點實現的,但是效果僵硬,動感不足! 之後參考了一些網站,發現都是用的js,於是自己想到用jquery 來做一個插件也來實現以下這個小功能. $.fn.backTop = func ...
- a single statement, not multiple statements
http://dev.mysql.com/doc/refman/5.7/en/prepare.html Statement names are not case sensitive. preparab ...
- 迷宫dfs
#include<stdio.h>int mov1[4]={0,0,1,-1};int mov2[4]={1,-1,0,0};int map[5][5]={0,1,0,0,1, ...
- linux卸载php
卸载: rpm -qa | grep php 所以正确的卸载顺序是:# rpm -e php-mysql-5.1.6-27.el5_5.3 --allmatches #同名全部卸载# rpm -e ...
- 取url的键值对,location的search:从?开始的字符串
function urlArgs(){ var args=""; var query=location.search.substring(1);//去除问号 var pairs=q ...