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 ...
随机推荐
- 反卷积Deconvolution
反卷积(转置卷积.空洞卷积(微步卷积))近几年用得较多,本篇博客主要是介绍一下反卷积,尤其是怎么计算反卷积(选择反卷积的相关参数) 图1 空洞卷积(微步卷积)的例子,其中下面的图是输入,上面的图是输出 ...
- Hibernate5笔记1--Hibernate简介和第一个程序
Hibernate简介: Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hib ...
- no libsigar-amd64-linux.so in java.library.path 解决方法
关于sigar的介绍可以参考这边博文 :https://www.cnblogs.com/luoruiyuan/p/5603771.html 在Linux上运行java程序时出现 no libsigar ...
- listen系统调用
/* * Perform a listen. Basically, we allow the protocol to do anything * necessary for a listen, and ...
- Linux USB驱动学习总结(三)---- USB鼠标的加载、初始化和通信过程
1.usbmouse的定义:usb鼠标既包含usb设备(usb_device)的属性也包含input输入设备(input_dev)的属性 struct usb_mouse { ];///USB鼠标设备 ...
- avalonJS-源码阅读(一)
写angularJS源码阅读系列的时候,写的太垃圾了.一个月后看,真心不忍直视,以后有机会的话得重写.这次写avalonJS,希望能在代码架构层面多些一点,少上源码.多写思路. avalon暴露句柄方 ...
- 吾修叫板微软,QMVC说比MVC5快!
前段时间发一篇文章,是关于QMVC介绍的文章,有网友建议写篇关于测试结果的文章.毕竟QMVC是开源的,并且是倾向于性能级开发的,因为我实在不喜欢MVC5还采用跟web form一样的开发风格,不停的封 ...
- Django模型和ORM
一.ORM ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是 ...
- openlayers常用操作
1.坐标转换 根据当前坐标系与目标坐标系进行转换ol.proj.transform(coordinate, source, destination); //coordinate:数组:source: ...
- 【h5标签转小程序标签】小程序使用wxParse解析html教程
一.先下载所需文件,下载地址:https://pan.baidu.com/s/1umZO9uI24zUTRd7VqaWbAg ,下载完毕后会得到一个wxParse文件夹,后面会用到: 二.先拷贝cs ...