Given a sequence of K integers { N1, N2, ..., N**K }. A continuous subsequence is defined to be { N**i, N**i+1, ..., N**j } where 1≤ijK. 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

思路

最大连续子序列和。这个题会卡一组数据。

input
4
0 0 0 -1 output
0 0 0
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int N;
int v[10000 + 10];
int s1, e1, s, e;
int sum = -1; int main(){
//input
cin >> N;
for(int i = 0; i < N; i++){
cin >> v[i];
}
//init
s1 = v[0];
e1 = v[0];
s = v[0];
e = v[N - 1];
//compute
int b = 0;
for(int i = 0; i < N; i++){
if(b >= 0){
b += v[i];
e1 = v[i];
}
else{
b = v[i];
e1 = v[i];
s1 = v[i];
}
if(sum < b){
s = s1;
e = e1;
sum = b;
}
}
sum = max(sum, 0);
cout << sum << " " << s << " " << e << endl; return 0;
}

最新文章

  1. LINUX下编译安装PHP各种报错大集合
  2. O365(世纪互联)SharePoint 之文档库使用小记
  3. arcgis将图片转成shp地图
  4. java与javac版本不一致问题
  5. android学习笔记28——Activity生命周期
  6. cocos2dx jsoncpp
  7. 自己总结的ruby on rails 查询方法
  8. Magnum Kuernetes源码分析(一)
  9. Python迭代器,生成器--精华中的精华
  10. (转载)Android出现“Read-only file system”解决办法
  11. ss-redir 的 iptables 配置(透明代理)
  12. Windows Server 2016-命令行方式管理Windows服务
  13. vue-router路由动态传参query和params的区别
  14. Spring Bean的ref属性和IoC注入集合
  15. 雅礼 noip2018 模拟赛 day3 T3
  16. HTML5 template元素
  17. Python面向对象-day07
  18. 如何获取ubuntu源码包里面的源码?
  19. QML中打印
  20. AD10中创建材料清单(BOM表)

热门文章

  1. selenium grid的使用
  2. 【Python】计算圆的面积
  3. ES6常用语法,面试应急专用!
  4. Visual Studio 2017:SQLite/SQL Server Compact ToolBox使用
  5. arcgis计算X坐标值、Y坐标值
  6. Java与Web前端发展前景及薪资对比
  7. python正则匹配次数,贪婪和非贪婪
  8. 实现手写体 mnist 数据集的识别任务
  9. Bugku-CTF之sql注入2 (全都tm过滤了绝望吗?)
  10. AcWing 851. spfa求最短路 边权可能为负数。 链表 队列