1007 Maximum Subsequence Sum (25)(25 point(s))
problem
Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, ..., N~j~ } 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
tip
求最大子序列和。
anwser
#include<bits/stdc++.h>
using namespace std;
#define Max 10001
#define INF 0x3f3f3f3f
int K, num[Max], neg = 0;
int main(){
// freopen("test.txt", "r", stdin);
cin>>K;
for(int i = 0; i < K; i++){
cin>>num[i];
if(num[i] < 0) neg++;
}
int sum = 0, firstIndex = 0, lastIndex = K-1, maxSum = 0, temp = 0;
for(int i = 0; i < K; i++){
sum += num[i];
if(maxSum < sum || (sum == 0 && lastIndex == K-1)) {
maxSum = sum;
temp = firstIndex;
lastIndex = i;
}
if(sum <= 0){
sum = 0;
firstIndex = i+1;
}
}
if(neg == K) {
temp = 0;
lastIndex = K-1;
}
cout<<maxSum<<" "<<num[temp]<<" "<<num[lastIndex];
return 0;
}
/*
10
-10 1 2 3 4 -5 -23 3 7 -21
*/
experience
- 数据有毒。
- 所有数都为正的情况,以及0与负数的情况,有点冲突。
1007 Maximum Subsequence Sum (25)(25 point(s))的更多相关文章
- 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 ...
- 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 ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 1007 Maximum Subsequence Sum (PAT(Advance))
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
随机推荐
- 【leetcode 简单】 第七十九题 区域和检索 - 数组不可变
给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点. 示例: 给定 nums = [-2, 0, 3, -5, 2, -1],求和函数 ...
- python Linux flask uwsgi nginx 在centos7.3部署
0.直接上uwsgi和nginx安装命令 linux 安装uwsgi yum groupinstall "Development tools" yum install zlib-d ...
- php实现异步请求
PHP开启异步多线程执行脚本 装载自:http://www.cnblogs.com/clphp/p/4913214.html 场景要求 客户端调用服务器a.php接口,需要执行一个长达5s-20s不 ...
- 差分约束系统+输出路径(I - Advertisement POJ - 1752 )
题目链接:https://cn.vjudge.net/contest/276233#problem/I 题目大意:输入k和n,然后输入n行,每一次输入两个数,代表开端和结尾,如果这个区间内点的个数大于 ...
- C++ Primer 5th 第17章 标准库特殊设施
C++新标准库提供了很多新功能,它们更加强大和易用. tuple类型 tuple是一种类似pair的模板,pair可以用来保存一对逻辑上有关联的元素对.但与pair不同的是,pair只能存储两个成员, ...
- 查看sql语句加锁信息
问题: 最近使用quartz集群,总是报deadlock问题,所以需要查看一下执行的sql导致的加锁冲突. 步骤: 1.在要测试的库中创建指定表innodb_lock_monitor create t ...
- python网络编程-socket样例
socket样例 一:只能一个客户端发送一个信息. 客户端 # -*- coding:utf-8 -*- __author__ = 'shisanjun' import socket sock=soc ...
- 浅谈js设计模式之单例模式
单例模式的定义是:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式是一种常用的模式,有一些对象我们往往只需要一个,比如线程池.全局缓存.浏览器中的 window 对象等.在 JavaS ...
- java基础75 xpth技术(网页知识)
1.xpth技术 1.1.xpath的作用 主要用于快速获取所需的节点对象. list<Node> selectNodes("xpath"); 查询多个节点对象 ...
- 汇编看C函数调用
http://blog.csdn.net/wishfly/article/details/5022008 简单的函数调用,通过简单的函数调用反汇编可以清楚了解如下 1.栈到底是什么,如何操纵栈的? ...