连续子数组的最大和/1007. Maximum Subsequence Sum (25)
题目描述
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<vector>
- #include<stdio.h>
- using namespace std;
- int dp[],s[]={};
- int main()
- {
- int n,tem;
- scanf("%d",&n);
- vector<int> vv;
- bool is = ;
- for(int i = ;i < n ;++i)
- {
- scanf("%d",&tem);
- if(tem >= )
- is = ;
- vv.push_back(tem);
- }
- if(is)
- {
- printf("0 %d %d\n",vv[],vv[n-]);
- return ;
- }
- dp[] = vv[];
- for(int i = ;i < n ;++i)
- {
- if(dp[i-] > )
- {
- dp[i] = dp[i-] + vv[i];
- s[i] = s[i-];
- }
- else
- {
- dp[i] = vv[i];
- s[i] = i;
- }
- }
- int MAX = -;
- int end;
- for(int i = ;i < n ;++i)
- {
- if(dp[i] > MAX)
- {
- MAX = dp[i];
- end = i;
- }
- }
- printf("%d %d %d\n",MAX,vv[s[end]],vv[end]);
- return ;
- }
连续子数组的最大和/1007. Maximum Subsequence Sum (25)的更多相关文章
- 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 (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 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 define ...
随机推荐
- iOS 虚拟机测试出现的相关问题
一.报红 1.问题描述:自己在工程目录下新建文件夹 包其他文件夹下的.h 和.m文件拖到该文件夹 再删除工程中的报红文件复制进去新文件夹 运行报错:No such file or directory: ...
- 机房收费系统之处理流程图与DFD图
一直想把自己画的图拿出来与大家分享,不过说来惭愧,一是图还没画完,二是图画的确实不咋地, 对于机房收费系统还是感到混乱,图画的不好大家多多指正. 系统处理流程图: DFD图
- how to build a paper's architecture?
1. problem? what's the problem? then do some extension of the problem. 2. related works ,which means ...
- ionic中的ion-content与ion-scroll
ion-content形成上下结构,上面固定,下层可滑动 首先要设置ion-content不可滑动:<ion-content class="has-subheader" sc ...
- [转]c# xml.Load() locking file on disk causing errors
本文转自:http://stackoverflow.com/questions/1812598/c-sharp-xml-load-locking-file-on-disk-causing-errors ...
- Java多线程之线程池
现在是多核的时代,面向多核的编程很重要,因此基于java的并发和多线程开发非常重要. 线程池是于队列密切相关的,其中队列保存了所有等待执行的任务.工作者线程的任务很简单:从队列中获取一个任务,执行任务 ...
- hdu-5700 区间交(二分+树状数组)
题目链接: 区间交 Problem Description 小A有一个含有n个非负整数的数列与mm个区间.每个区间可以表示为li,ri. 它想选择其中k个区间, 使得这些区间的交的那些 ...
- 初识 Asp.Net内置对象之Server对象
Server对象 Server对象定义了一个于Web服务器相关联的类提供对服务器上的方法和属性的访问,用于访问服务器上的资源. Server对象的常用属性 属性 MarhineName 获取服务器 ...
- ASP.NET缓存全解析7:第三方分布式缓存解决方案 Memcached和Cacheman 转自网络原文作者李天平
Memcached — 分布式缓存系统 1.Memcached是什么? Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.Memcached通过在内 ...
- ef codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题.
需要在库程序包管理器里依次执行以下命令: 1.启用迁移功能:Enable-Migrations -ContextTypeName MvcMovie.Models.MovieDbContext 2.建立 ...