浙大 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 ...
随机推荐
- java学习笔记-set
//东软实训前,要把java 看完. //2015年4月29日21:44:13 //list01 package com.alfredsun.first.main; import java.util. ...
- 微信小程序开发(1)
底限,HTML,CSS,JS得会 先过一下官方的文档:https://mp.weixin.qq.com/debug/wxadoc/introduction/index.html?t=20161230 ...
- php 内置函数 ( 随手能写出100个才算高级工程师 )
string 类型 去掉左右空格, trim()字符串的长度 strlen() float类型数组类型 count() 数组个数 资源类型 fopen() 大写转小写 strtolower()小写转大 ...
- sublime text3 用法
1. 使用lint进行语法及风格校验 jshint可以统一编码风格. 安装jshint的步骤: 1)ctrl+shift+p打开命令模式--->输入pcip回车--->输入sublimel ...
- web前端入门
看到很多同学在咨询:学习前端该怎么入门啊.推荐一下前端入门书籍啊什么的,作为一个过来人,我想告诉你一些小小技巧,避免走弯路: 1.先敲再学.如果你是零基础,就不要去每个标签,每个属性地去抠,因为里面有 ...
- FZU 2088 最长队名
Problem 2088 最长队名 Problem Description Jack所在的班级决定组团报名参加FZU校赛.为了体现班级的团结和睦,班长决定用班级所有人的名字连起来组成一个史上最长最醒 ...
- New : HTML5 中的新标签
基础 标签 描述 <!DOCTYPE> 定义文档类型. <html> 定义 HTML 文档. <title> 定义文档的标题. <body> 定义文档 ...
- 面试题-Java Web-网络通信
1.HTTP响应的结构是怎么样的? HTTP响应由三个部分组成:状态码(Status Code):描述了响应的状态.可以用来检查是否成功的完成了请求.请求失败的情况下,状态码可用来找出失败的原因.如果 ...
- CentOS单独编译安装PHP gd库扩展
注意:如果您已经编译安装过GD库,请重新编译安装php不带gd库成功后,执行以下操作 安装libpng wget http://jaist.dl.sourceforge.net/project/lib ...
- JSON 和 JSONP 两兄弟
项目中遇到这个新事物,转一篇不错的总结,原文 如今ajax威风凛凛 但说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决? 这两个问题目前都有不同 ...