PAT1007:Maximum Subsequence Sum

本文探讨了最大子序列和问题的解决方案,采用动态规划思想,通过遍历序列并维护最大和及其对应的起始和结束索引,最终输出最大子序列的和及边界元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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.

Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4

思路

DP的思想。
这道题最关键点在于:确定最大值子序列的左右索引left和right。那么:
1.从左到右遍历序列时,如果遍历到第i个数时的累加和已经小于0,那么说明从这个数左边开始的所有可能的左半部序列到这个数的和都已经小于0,对于这个数右半部分的序列只减不增,还不如舍弃掉。因此最大和子序列肯定只会在这个数右边。所以暂存下新的左索引templeft = i + 1。
2.当遍历到第i个数时当前最大值tempsum已经大于输出最大值maxsum时,maxsum = tempsum,那么需要更新左右索引left = templeft 和 right = i.
3.对于遍历后maxsum < 0,说明整个序列只可能全是负数,不存在最大正数和的情况,根据题目要求令maxsum = 0,左右索引即为元序列的左右索引。

代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int N;
    while(cin >> N)
    {
      vector<int> nums(N);
      int left = 0,right = N - 1,maxsum = -233,tempsum = 0,templeft = 0;
      for(int i = 0;i < N;i++)
      {
          cin >> nums[i];
          tempsum += nums[i];
          if(tempsum < 0)
          {
              tempsum = 0;
              templeft = i + 1;
          }
          else if (tempsum > maxsum)
          {
              left = templeft;
              maxsum = tempsum;
              right = i;
          }

      }
      if(maxsum < 0)
        maxsum = 0;
      cout << maxsum << " " << nums[left] << " " << nums[right] << endl;
    }
}

 

转载于:https://www.cnblogs.com/0kk470/p/7643173.html

1. 用户与权限管理模块 角色管理: 学生:查看实验室信息、预约设备、提交耗材申请、参与安全考核 教师:管理课题组预约、审批学生耗材申请、查看本课题组使用记录 管理员:设备全生命周期管理、审核预约、耗材采购与分发、安全检查 用户操作: 登录认证:统一身份认证(对接学号 / 工号系统,模拟实现),支持密码重置 信息管理:学生 / 教师维护个人信息(联系方式、所属院系),管理员管理所有用户 权限控制:不同角色仅可见对应功能(如学生不可删除设备信息) 2. 实验室与设备管理模块 实验室信息管理: 基础信息:实验室编号、名称、位置、容纳人数、开放时间、负责人 功能分类:按学科(计算机实验室 / 电子实验室 / 化学实验室)标记,关联可开展实验类型 状态展示:实时显示当前使用人数、设备运行状态(正常 / 故障) 设备管理: 设备档案:名称、型号、规格、购置日期、单价、生产厂家、存放位置、责任人 全生命周期管理: 入库登记:管理员录入新设备信息,生成唯一资产编号 维护记录:记录维修、校准、保养信息(时间、内容、执行人) 报废处理:登记报废原因、时间,更新设备状态为 "已报废" 设备查询:支持按名称、型号、状态多条件检索,显示设备当前可用情况 3. 预约与使用模块 预约管理: 预约规则:学生可预约未来 7 天内的设备 / 实验室,单次最长 4 小时(可设置) 预约流程:选择实验室→选择设备→选择时间段→提交申请(需填写实验目的) 审核机制:普通实验自动通过,高危实验(如化学实验)需教师审核 使用记录: 签到 / 签退:到达实验室后扫码签到,离开时签退,系统自动记录实际使用时长 使用登记:填写实验内容、设备运行情况(正常 / 异常),异常情况需详细描述 违规管理:迟到 15 分钟自动取消预约,多次违规限制预约权限 4. 耗材与安全管理模块 耗材管理: 耗材档案:名称、规格、数量、存放位置、
### 最大子序列和问题的解决方法 最大子序列和问题是经典的算法问题之一,目标是从给定数组中找到一个连续子序列,使得该子序列中的元素之和达到最大值。以下是基于动态规划的思想实现的一个高效解决方案。 #### 动态规划法 通过维护两个变量 `current_sum` 和 `max_sum` 来记录当前子序列的最大和以及全局范围内的最大和。遍历整个数组一次即可完成计算: ```python def max_subsequence_sum(nums): current_sum = 0 max_sum = float('-inf') # 初始化为负无穷大 for num in nums: current_sum = max(num, current_sum + num) # 更新当前子序列和 max_sum = max(max_sum, current_sum) # 更新全局最大和 return max_sum ``` 上述代码的时间复杂度为 \(O(n)\),其中 \(n\) 是输入列表的长度[^1]。 #### 示例运行 假设我们有如下输入数据: ```python nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] result = max_subsequence_sum(nums) print(result) # 输出应为6 (子序列为 [4,-1,2,1]) ``` 此方法的核心在于每次迭代都决定是否将当前数加入到现有子序列或者重新开始一个新的子序列。 #### 非连续子序列的情况 如果允许选取非连续的子序列,则可以采用贪心策略来解决问题。对于这个问题的具体实现方式已经在 JavaScript 的例子中有体现。然而,在 Python 中可以通过简单的排序加累加操作快速得到结果: ```python def non_contiguous_max_subsequence_sum(nums): positive_nums = sorted([num for num in nums if num > 0], reverse=True) total = sum(positive_nums) return total if total != 0 else max(nums) # 测试用例 nums = [7, 2, -8, 4, 10, -2] result = non_contiguous_max_subsequence_sum(nums) print(result) # 应输出23 ``` 这里需要注意的是当所有数值均为负数时需单独处理以确保返回最大的单个元素作为结果。 ### 结论 无论是针对连续还是非连续情况下的最大子序列求和问题都可以借助不同的优化手段有效解决。前者依赖于线性的扫描过程而后者则可能涉及更复杂的逻辑判断或额外的数据结构支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值