浙大 pat 1007题解
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
#include"iostream"
#include "string"
#include<vector>
#include"algorithm"
#include "stdlib.h"
#include "cmath"
using namespace std;
struct Node
{
int num;
int start;
int sum;
};
vector <Node> d;
int main()
{
int k;
cin >> k;
d.resize(k);
for(int i=0;i<k;i++)
{
cin >> d[i].num;
}
d[0].sum = d[0].num;
d[0].start = 0;
for(int i=1;i<k;i++)
{
if(d[i-1].sum >= 0)
{
d[i].sum = d[i-1].sum+d[i].num;//当前项的和=前一项的和加当前的数
d[i].start = d[i-1].start;//当前项的起点=前一项的起点
}
else
{
d[i].sum = d[i].num;
d[i].start = i;
}
}
int max = d[0].sum;
int temp=0,i;
for( i=0;i<k;i++)
{
if(d[i].sum>max)
{
max = d[i].sum;
temp = i;
}
}
if(max<0)
cout<<"0"<<" "<<d[0].num<<" "<<d[k-1].num<<endl;
else
cout<<d[temp].sum<<" "<<d[d[temp].start].num<<" "<<d[temp].num<<endl;
return 0;
}
浙大 pat 1007题解的更多相关文章
- 浙大pat 1035题解
1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...
- 浙大pat 1025题解
1025. PAT Ranking (25) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...
- 浙大pat 1011题解
With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...
- 浙大PAT 7-06 题解
#include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> ...
- 浙大pat 1012题解
1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...
- 浙大 pat 1003 题解
1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- 浙大 pat 1038 题解
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 浙大 pat 1047题解
1047. Student List for Course (25) 时间限制 400 ms 内存限制 64000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 浙大pat 1054 题解
1054. The Dominant Color (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard Behind the scen ...
随机推荐
- 【NOIP2014】Day1题解+代码
Day1 T1 签到题,模拟一下随便写就能过. 不过小心像我一样表打错傻逼的调了10min. #include <algorithm> #include <iostream> ...
- hdu 4342 History repeat itself(数学题)
题目链接:hdu 4342 History repeat itself 题意: 让你找第a个非完全平方数m,并且求前m个数的开方向下取整的和. 题解: 第一个问题: 假设第a个非平方数是X,X前面有n ...
- Tensorflow (1)
'tf.placeholder' or 'tf.Variable' The difference is that with tf.Variable you have to provide an ini ...
- 定位(position)
position :属性规定元素的定位类型 语法: position : static | absolute | fixed | relative JavaScript语法:object.style. ...
- JS 获取网页的宽高
网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...
- 对于行高(line-height)的一些理解
刚一开始学习这个特性的时候,总是出一些当时看起来很奇怪的问题.现在决定重新整理一下.毕竟使用css,十行揉在一起凑出效果是一种使用,知道为什么会有这种效果也是一种使用.我们需要做一些测试,所以首先需要 ...
- QLibraryInfo
读取 qt.conf 文件, 获取 Qt Library 的信息. 通常会在以下三个路径查找conf文件: :/qt/etc/qt.conf(使用资源系统时) ...
- [Q]升级/重新获取授权步骤
若因需要升级或授权文件失效(重装系统或其他原因),在服务期内可通过下面的步骤操作. 注:168元版提供2年升级及售后支持,118元版的提供1升级及售后支持. 步骤如下: 1. 重新获取CAD批量打图精 ...
- UNIX基础--Manual Pages
联机手册 Manual Pages 最详细的使用说明文档莫过于 FreeBSD 里的联机手册了. 几乎每一个程序都会附上一份简短说明, 以介绍这个程序的的基本功能以及参数的用法. 我们能通过 man ...
- es6语法
let定义变量,特性: 1,不允许重复定义 2,不存在预解析 3,变量存在于会块级作用域 即{}内部 const : 定义常量,常量的值不能修改,若常量是对象 对象下的属性可修改. 解构赋值语法: 数 ...