PAT 甲级 1007 Maximum Subsequence Sum
https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168
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
时间复杂度:$O(n)$
代码:
#include <bits/stdc++.h>
using namespace std; int a[11111];
int dp[11111]; int main() {
int n;
scanf("%d", &n);
int ans = 0, temp = 0, cnt = 0, sum = 0;
for(int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
for(int i = 1; i <= n; i ++) {
if(a[i] < 0)
sum ++;
}
if(sum == n)
printf("0 %d %d\n", a[1], a[n]);
else {
if(n == 1)
printf("%d %d %d\n", a[n], a[n], a[n]);
else {
for(int i = 0; i < n; i ++) {
dp[i + 1] = max(a[i + 1], a[i + 1] + dp[i]);
if(dp[i + 1] > ans) {
temp = i + 1;
ans = dp[i + 1];
}
}
for(int i = temp; i >= 1 && dp[i] >= 0; i --)
cnt = i;
printf("%d %d %d\n", ans, a[cnt], a[temp]);
}
}
return 0;
}
PAT 甲级 1007 Maximum Subsequence Sum的更多相关文章
- 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 ...
- PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】
题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...
- 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 ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT甲级——A1007 Maximum Subsequence Sum
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 1007 Maximum Subsequence Sum (PAT(Advance))
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
随机推荐
- code ELIFECYCLE (代码周期)
问题:build 不成功 解决:新建一个dist 文件,没有自动新建dist 文件 问题 :npm run dev 时候 解决:
- ab工具测试 swoole 和 ngixn+php-fpm 的并发对比
测试样例: 执行的一条sql记录的1w次插入分两组: 一组用nginx+pfm 来执行, 一组用swoole 来执行 公平性保证前提: @1.为了保证公平性, 在nginx里把 access_log, ...
- leetcode笔记(六)740. Delete and Earn
题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...
- JS对象和数组在谷歌浏览器中引用存储的表现
大家都知道JS的数据分为基本类型和引用类型.具体什么不说了,今天主要说说对象和数组作为引用类型在谷歌浏览器中的表现. 首先,问题是这么发现的.我在控制台使用console打印了一个数组,然后对数组进行 ...
- winform窗体传值和动态添加控件
1.跳转窗体时传值 //将要显示的页面实例化 RoleMenuForm rmf = new RoleMenuForm(); try { //在此给RoleMenuForm 窗体中的变量roleId传值 ...
- 三、css篇
#这里强烈推荐一本书<css世界>,css第一书. #上面的层叠顺序得记住. 1.align-items justify-content 是flex(弹性盒模型)必须要会的属性,alig ...
- MR执行流程
1.Map任务处理 1.1 读取HDFS中的文件.每一行解析成一个<k,v>.每一个键值对调用一次map函数. <0,hello you> <10,hello me& ...
- zabbix监控nginx服务状态
nginx需要安装--with-http_stub_status_module模块 $ nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 ...
- Python3 利用pip安装BeautifulSoup4模块(Windows版)
一.找到Python3的安装文件夹 二.将路径复制 三.Windows10 打开Windows PowerShell(管理员).Windows 8.8.1.7使用cmd 切换到相应目录 四.此目录下的 ...
- xampps 不能配置非安装目录虚拟主机解决方案
今天将前几天安装好的xampps配置下,准备开始php开发之旅,在我信心满满的将工作目录定在非安装目录上(安装目录在:D:\Program Files\xampps\apache\htdocs 我将 ...