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 = 100100;
int a[maxn] = {0};
int dp[maxn] = {0};
int s[maxn] = {0};int main(){int n;scanf("%d",&n);bool flag = false;for(int i = 0; i < n; i++){scanf("%d",&a[i]);if(a[i] >= 0) flag = true;}if(!flag){printf("0 %d %d",a[0],a[n-1]);return 0;}//scanf("%d",&n);dp[0] = a[0];for(int i = 1; i < n; i++){if(dp[i-1] + a[i] >= a[i]){dp[i] = dp[i-1]+a[i];s[i] = s[i-1];}else{dp[i] = a[i];s[i] = i;}}int max = dp[0];int k = 0;for(int i = 1; i < n; i++){if(dp[i] > max){max = dp[i];k = i;}}printf("%d %d %d",max,a[s[k]],a[k]);return 0;
}

 

转载于:https://www.cnblogs.com/wanghao-boke/p/10548671.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385090.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

进制转换习题

题目&#xff1a;进制转换 解法&#xff1a; #include <iostream> #include <vector> #include <algorithm> using namespace std; long long nums, k;void solution(long long nums, long long k) {vector<int> res;while(nums){long long curr nu…

02-线性结构2 一元多项式的乘法与加法运算 (20 分)

设计函数分别求两个一元多项式的乘积与和。 输入格式: 输入分2行&#xff0c;每行分别先给出多项式非零项的个数&#xff0c;再以指数递降方式输入一个多项式非零项系数和指数&#xff08;绝对值均为不超过1000的整数&#xff09;。数字间以空格分隔。 输出格式: 输出分2行&…

《Leetcode | 02》

序号题目类型标记 863. 二叉树中所有距离为 K 的结点 ★ 94. 二叉树的中序遍历 ★ 102. 二叉树的层次遍历 144. 二叉树的前序遍历 450. 删除二叉搜索树中的节点 701. 二叉搜索树中的插入操作 700. 二叉搜索树中的搜索 108. 将有序数组转换为二叉搜索树 701. …

C++基础:各种输入方法总结

输入原理简述&#xff1a; 程序的输入都建有一个缓冲区&#xff0c;即输入缓冲区。每次输入过程是这样的&#xff0c;当一次键盘输入结束时会将输入的数据存入输入缓冲区&#xff0c;而cin函数直接从输入缓冲区中取数据。正因为cin函数是直接从缓冲区取数据的&#xff0c;所以…

04-树7 二叉搜索树的操作集 (30 分)

本题要求实现给定二叉搜索树的5种常用操作。 函数接口定义&#xff1a; BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementType X ); Position FindMin( BinTree BST ); Position FindMax( B…

02-线性结构3 Reversing Linked List (25 分)

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K3, then you must output 3→2→1→6→5→4; if K4, you must output 4→3→2→1→5→6. Input Specifi…

03-树1 树的同构 (25 分)

给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2&#xff0c;则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的&#xff0c;因为我们把其中一棵树的结点A、B、G的左右孩子互换后&#xff0c;就得到另外一棵树。而图2就不是同构的。 图1 图2 现给定两棵…

【树】104. 二叉树的最大深度

题目 104. 二叉树的最大深度 方法1 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr),…

Leetcode 206. 反转链表

206. 反转链表 解法1 class Solution { public:ListNode *reverseList(ListNode *head){if(!head || !head->next)return head;ListNode *p;p reverseList(head->next);head->next->next head;head->next nullptr;return p;} };解法2 /*** Definition for…

05-树7 堆中的路径 (25 分)

将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i&#xff0c;打印从H[i]到根结点的路径。 输入格式: 每组测试第1行包含2个正整数N和M(≤)&#xff0c;分别是插入元素的个数、以及需要打印的路径条数。下一行给出区间[-10000, 10000]内的N个要被插入一个初…

Leetcode 124.二叉树中的最大路径

解法1 解法 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* …

《UNIX环境高级编程》目录

第一章&#xff1a;UNIX标准及实现 01 函数perror、strerror 第三章&#xff1a;文件I/O 01 C库函数 02 文件描述符、函数open和openat 03 函数read、write、lseek 04 函数dup和dup2 第四章&#xff1a;文件和目录 01 函数stat、fstat、fstatat和lstat 02 函数umask 03 函…

06-图1 列出连通集 (25 分)

给定一个有N个顶点和E条边的无向图&#xff0c;请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时&#xff0c;假设我们总是从编号最小的顶点出发&#xff0c;按编号递增的顺序访问邻接点。 输入格式: 输入第1行给出2个整数N(0)和E&#xff0c;分别是图的…

牛客网算法题题解

序号题目语言标记1 C解题报告 2 3 4字符串归一化C解题报告

06-图3 六度空间 (30 分)

“六度空间”理论又称作“六度分隔&#xff08;Six Degrees of Separation&#xff09;”理论。这个理论可以通俗地阐述为&#xff1a;“你和任何一个陌生人之间所间隔的人不会超过六个&#xff0c;也就是说&#xff0c;最多通过五个人你就能够认识任何一个陌生人。”如图1所示…

Linux网络编程目录

UNIX网络编程目录 1. TCP三次握手的第三次的 ack包丢失会怎样&#xff1f; 2. inux网络编程“惊群”问题总结

Linux高性能服务器编程

一、文件IO、标准IO 1. 2. 函数dup和dup2 三、进程 1. fork、vfork、clone 2. 函数wait、waitpid、孤儿进程、僵尸进程 3. 进程组 4. 会话 四、信号 1. 函数signal、sigaction 2. 函信号SIGCHLD 3. 函数kill、raise、abort、alarm 4. 信号集、sigprocmask、sigpending 五、…

07-图4 哈利·波特的考试 (25 分)

哈利波特要考试了&#xff0c;他需要你的帮助。这门课学的是用魔咒将一种动物变成另一种动物的本事。例如将猫变成老鼠的魔咒是haha&#xff0c;将老鼠变成鱼的魔咒是hehe等等。反方向变化的魔咒就是简单地将原来的魔咒倒过来念&#xff0c;例如ahah可以将老鼠变成猫。另外&…

C++ Primer (二)目录

第十五章&#xff1a;面向对象程序设计 1. 虚函数/2. 虚函数表剖析&#xff08;一&#xff09;3. 虚函数表剖析&#xff08;二&#xff09;4. 虚函数表剖析&#xff08;三&#xff09;

07-图6 旅游规划 (25 分)

有了一张自驾旅游路线图&#xff0c;你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序&#xff0c;帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的&#xff0c;那么需要输出最便宜的一条路径。 输入格式: 输…