01-复杂度2 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. 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 (≤). 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<cstdio>
const int maxn = ;
int a[maxn] = {};
int dp[maxn] = {};
int s[maxn] = {}; int main(){
int n;
scanf("%d",&n);
bool flag = false;
for(int i = ; i < n; i++){
scanf("%d",&a[i]);
if(a[i] >= ) flag = true;
}
if(!flag){
printf("0 %d %d",a[],a[n-]);
return ;
}
//scanf("%d",&n);
dp[] = a[];
for(int i = ; i < n; i++){
if(dp[i-] + a[i] >= a[i]){
dp[i] = dp[i-]+a[i];
s[i] = s[i-];
}else{
dp[i] = a[i];
s[i] = i;
}
}
int max = dp[];
int k = ;
for(int i = ; i < n; i++){
if(dp[i] > max){
max = dp[i];
k = i;
}
}
printf("%d %d %d",max,a[s[k]],a[k]);
return ;
}
01-复杂度2 Maximum Subsequence Sum (25 分)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PTA 01-复杂度2 Maximum Subsequence Sum (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum (25分) Given ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
随机推荐
- 我不是一名UX设计师,你也不是
以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 最近几年,用户体验这个术语开始出现.而且随着它的发展,我们也见证了用户体验设计师的崛起. 每隔几个月 ...
- Spring Boot☞ 使用freemarker模板引擎渲染web视图
效果图 代码 package com.wls.integrateplugs.hello.controller; /** * Created by wls on 2017/8/24. */ import ...
- 4619 Warm up 2
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ][]; ...
- 假设字符串类似这样的aba和aab,abc和bca就相等,现在随便给你二组字符串,请编程比较他们看是否相等
public static boolean stringSame(String str1,String str2){ if(str1.length() != str2.length()){//先判断长 ...
- 公众号js-sdk简
后台代码 jssdk.PHP——因sae的不可写环境,故需要将accesstoken和JsApiTicket存入数据库. 数据库字段:id(int 3),accesstoken(string 255) ...
- 部署图像深度学习Web网站
1. 内网穿透 2. 深度学习Web化 https://www.cnblogs.com/haolujun/p/9778939.html
- WSAStartup函数
函数WSAStartup 一.WSAStartup函数 int WSAStartup ( ...
- 一类适合初学者的DP:最大子段和与最大子矩阵
最近在水简单DP题,遇到了两道层层递进的DP题,于是记录一下 一.最大子段和 题意: 给出一个长度为n(n<=1e5)的序列,求连续子段的最大值 比如说2 3 -4 5 的最大值是6 而 2 ...
- Ubuntu下使用UFW,以及CentOS7的默认防火墙firewalld
UFW是一个简化版的iptables,基于iptables,配置比iptables简单 默认UFW是关闭状态,即Ubuntu默认打开所有端口,比较危险. 检测状态 ufw status 设置默认状态, ...
- Verilog HDL VGA Driver for Xilinx Nexys 4 适用于Nexys4的VGA驱动
/* * Function: VGA Timing Generator * Author: Liutianchen * Date: 2016-12-5 * Version: 6.0 * Environ ...