Maximum Subsequence Sum

简介: 最大连续子列和问题,在此给出题解 (浙大PTA https://pintia.cn/problem-sets/16/problems/665)

Maximum Subsequence Sum

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

输入样例:

10
-10 1 2 3 4 -5 -23 3 7 -21

输出样例:

10 1 4

二、题解

c代码

#include <stdio.h>
#define N 10010

int s[N], sum = -1;

int main() {
    int k;
    scanf("%d", &k);
    for (int i = 0; i < k; i++)  scanf("%d", &s[i]);
    
    int cnt = 0, head = 0, rear = 0;
    for (int i = 0, j = 0; i < k; i++) {
        cnt += s[i];
        if(cnt >= 0) {
            if(cnt > sum) {    //临时最大值大于统计的最大值,进行迭代
                sum = cnt;
                head = j;      //最大子列的头坐标与尾坐标也更新
                rear = i;
            }
        } else {        //临时最大值为负,置零重新统计,头坐标临时标记也更新
            cnt = 0;
            j = i + 1;
        }
    }
    
    if(sum >= 0)  printf("%d %d %d\n", sum, s[head], s[rear]);
    else printf("0 %d %d\n", s[0], s[k - 1]);
    
    return 0;
}
相关文章
【精讲】PCIe基础篇——PCIe传输速率计算
我们在接触PCIe设备时会接触到一个词,传输速率,比如8GT/s。或者有人问你设备的带宽多少?通信速率多少?本文带你了解下。
9403 0
【精讲】PCIe基础篇——PCIe传输速率计算
|
机器学习/深度学习 数据安全/隐私保护 UED
淘宝图片搜索接口开发指南:从图像识别到商品匹配的全流程实现
图片搜索技术极大提升了电商用户体验。本文详解淘宝图片搜索接口的实现原理与开发实战,涵盖预处理、特征提取、比对与结果返回等核心流程,并提供可复用代码。内容还包括常见错误处理、合规性开发注意事项及多种扩展应用场景,助力开发者快速构建高效、合规的图片搜索功能。
淘宝图片搜索接口开发指南:从图像识别到商品匹配的全流程实现
|
数据采集 Web App开发 测试技术
使用Selenium调试Edge浏览器的常见问题与解决方案
在互联网数据采集领域,Selenium常用于自动化网页爬取。针对使用Edge浏览器时遇到的启动远程调试失败、访问受限及代理IP设置等问题,本文提供了解决方案。通过特定命令启动Edge的远程调试模式,并利用Python脚本配合Selenium库,可实现代理IP、User-Agent的设定及Cookie管理等高级功能,有效提升爬虫稳定性和隐蔽性。遵循步骤配置后,即可顺畅执行自动化测试任务。
4031 1
使用Selenium调试Edge浏览器的常见问题与解决方案
|
存储 编译器 程序员
【C++ 文件操作与字符串处理】从文件读取到内容分割的全方位指南
【C++ 文件操作与字符串处理】从文件读取到内容分割的全方位指南
1592 6
proteus常用元件图示和名称介绍
proteus常用元件图示和名称介绍
7122 0
proteus常用元件图示和名称介绍
|
机器学习/深度学习 算法 TensorFlow
TensorFlow 2keras开发深度学习模型实例:多层感知器(MLP),卷积神经网络(CNN)和递归神经网络(RNN)
TensorFlow 2keras开发深度学习模型实例:多层感知器(MLP),卷积神经网络(CNN)和递归神经网络(RNN)
|
Web App开发 前端开发 JavaScript
BootStrap, React, Vue的比较
目前, 个人了解的前端比较流行的框架是三个:     BootStrap, React, Vue 想要为公司选一个作为接下来的前端技术研发方向, 因此作了一番调查, 有点浅见.
4443 0
|
8天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17267 63