https://pintia.cn/problem-sets/994805342720868352/problems/994805514284679168

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } 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的更多相关文章

  1. 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 ...

  2. PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...

  3. 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 ...

  4. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  5. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  6. PAT甲级——A1007 Maximum Subsequence Sum

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  7. python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)

    python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...

  8. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  9. 1007 Maximum Subsequence Sum (PAT(Advance))

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

随机推荐

  1. iOS之UIKeyboardType 11种键盘图片展示

    UIKeyboardTypeDefault      UIKeyboardTypeASCIICapable  ==  UIKeyboardTypeAlphabet      UIKeyboardTyp ...

  2. Qt数据库编程1

    Qt中数据编程主要分为以下两点:1.利用qt提供类 访问数据库或者成为简单的数据库编程2.数据库编程中引入model/view编程模型 qt中数据库编程的步骤: 1.加载数据库驱动 QSqlDatab ...

  3. 【CodeForces 803 C】Maximal GCD(GCD+思维)

    You are given positive integer number n. You should create such strictly increasingsequence of k pos ...

  4. Maven 运行启动时****找不到符号*com.xxx.user.java

    Maven 运行启动时****找不到符号*com.xxx.user.java maven项目更改后没有安装 (install) 重新安装解决问题!

  5. 添加fileinfo扩展

    首先声明:笔者用的是军哥的lnmp一键安装包!链接地址:https://lnmp.org 打开upgrade_php.sh配置文件[文件所在位置:~/lnmp1.4/include/upgrade_p ...

  6. 高级同步器:交换器Exchanger

    引自:https://blog.csdn.net/Dason_yu/article/details/79764467 一.定义每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹 ...

  7. MYSQL 版本5.7.24 sql_mode=only_full_group_by问题

    具体出错提示: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggr ...

  8. Hive优化之谓词下推

    Hive优化之谓词下推 解释 Hive谓词下推(Predicate pushdown) 关系型数据库借鉴而来,关系型数据中谓词下推到外部数据库用以减少数据传输 基本思想:尽可能早的处理表达式 属于逻辑 ...

  9. 2.5 进程控制之wait函数

    一.绪论 一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的PCB还保留着,内核在其中保存了一些信息:如果 是正常终止则保存着退出状态,如果是异常终止则保存着导致该进程终止的信号是 ...

  10. keil5 mdk调用外部编辑器notepad++、sublime3、VSCode总结

    1.打开keil主界面,点击菜单栏Tools菜单,选择如下图所示的选项. 2.点击如下图所示的菜单上红笔标注的地方,给这个工具命名,如notepad++.sublime3.vscode等,如下图, 并 ...