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题解的更多相关文章

  1. 浙大pat 1035题解

    1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...

  2. 浙大pat 1025题解

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...

  3. 浙大pat 1011题解

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...

  4. 浙大PAT 7-06 题解

    #include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> ...

  5. 浙大pat 1012题解

    1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...

  6. 浙大 pat 1003 题解

    1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  7. 浙大 pat 1038 题解

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  8. 浙大 pat 1047题解

    1047. Student List for Course (25) 时间限制 400 ms 内存限制 64000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  9. 浙大pat 1054 题解

    1054. The Dominant Color (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard Behind the scen ...

随机推荐

  1. Token注解防止表单的重复提交

    注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...

  2. 如何安装chrome扩展?比如json-handle插件如何安装

    chrome插件安装方法: 方式一,在线安装 直接插到json-handle地址,添加即可 https://chrome.google.com/webstore/detail/json-handle/ ...

  3. C#深复制与浅复制

    C#深复制与浅复制 C#中对于数据的复制机制虽然简单但是容易让人误解.C#数据类型大体分为值类型(value type)与引用类型(reference type).对于值类型数据,复制的时候直接将数据 ...

  4. usaco月赛,2017.1总结

    T1:跳舞的奶牛 大致题意:一个体积为k的舞台能够同时容纳k只奶牛一起跳舞,他们每头奶牛的跳舞时间不同,如果有一只奶牛跳完了第k+1头奶牛就会立刻上场跳舞,当所有奶牛跳完舞以后我们认为这次表演结束.现 ...

  5. Python 清理HTML标签类似PHP的strip_tags函数功能(二)

    没有发现Python 有现成的类似功能模块,所以昨天写了个简单的 strip_tags 但还有些问题,今天应用到采集上时进行了部分功能的完善, 1. 对自闭和标签处理 2. 以及对标签参数的过滤 fr ...

  6. php遍历目录下文件,并读取内容

    <?php echo "<h2>遍历目录下文件,并读取内容</h2><br>\n"; function listDir($dir) { i ...

  7. Java NIO Channel和Buffer

    Java NIO Channel和Buffer @author ixenos Channel和Buffer的关系 1.NIO速度的提高来自于所使用的结构更接近于OS执行I/O的方式:通道和缓冲器: 2 ...

  8. ios 显示其他app的购买页面

    using UnityEngine; using System.Collections; using System.Runtime.InteropServices ; public class IOS ...

  9. 深入理解java回调机制

    Callback的定义 一般在程序中执行回调函数是,是知道回调函数是预留给系统调用的,而且知道该函数的调用时机. 比如说android应用定义一个button对象,并给按钮添加一个监听事件," ...

  10. Java JDBC Batch

    Java批量处理数据 import java.sql.Connection; import java.sql.PreparedStatement; //import String sql = &quo ...