动态规划~校赛H题~MAX sum

本文介绍了一个经典的MaxSum问题,旨在找出给定整数序列中连续子序列的最大和及其位置。通过示例说明了如何确定子序列的最大和及起止位置,并提供了一段详细的实现代码。

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



Problem H: Max Sum


Description

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

Input

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output

For each test case, you should output two lines.

The first line is "Case #:", # means the number of the test case.

The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence.

If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
Case 1:
14 1 4

Case 2:
7 1 6
生气
此题题意是读入T串数字,并且在每串数字中寻找和最大且连续的那一个子数串,输出最大和,和子数串的起始和结束位置,例如读入1,2,-4,3,5,-8。那么此串数中和最大的子串即为3,5,她们的和为8,起始位置为5,结束位置为6;例如读入-1,-1,-1,-1,-1、那么此串中最大的子串和即为-1,起始位置为1,结束位置也为1;(因为题目中有说如果有多个符合条件的话,从最开始出输出)如 0 6 -1 1 -6 7 -5,输出结果为,7,1,6可以很明显看出前五位的和为0,但是计算起始位置的时候还是将其计入了;
此题的思路是从上一位开始与下一位累加,并且判断这个和与前几位中MAX的大小关系,如果此和大于MAX的话,即用此和代替MAX。
如1 2 -4 3 5 -8 1 ;
第一位1:MAX=1,MAX>0,begin=1;end=1,sum=1;
第二位2:MAX=MAX>1+2?max:1+2,begin=1;end=2,sum=3;
第三位-4:sum=3-4=-1,-1<0<max=3;begin=1.end=2,sum=0;(因为到第三位的时候和已经小于0了,所以不再需要考虑小于0的部分,应该将sum的值变成0,然后继续下一项求和)
第四位3:sum=0+3>MAX,将begin=4,end=4,max=3;
第五位5:sum=3+5=8;sum>max;即将max的值变成8,begin=4,end=5;
第六位-8;sum=0;sum<max;end和begin,MAX的值不变;
第七位1;sum=1>0;sum<max;end和begin的值也不变;
只要输出MAX和begin和end的值既可以完成;
附上代码:
#include <iostream>
using namespace std;
int main()
 {
    int T,N,num,startP,endP;
     cin>>T;
    for(int k=0;k<T;k++)
     {
             cin>>N;
            int max=-1001,sum=0,temp=1;
            for(int i=0;i<N;i++)
             {
                             cin>>num;
                             sum+=num;
                            if(sum>max)
                             {
                                       max=sum;
                                       startP=temp;
                                       endP=i+1;
                             }
                            if(sum<0)
                             {
                                     sum=0;
                                     temp=i+2;
                             }
             }
             cout<<"Case "<<k+1<<":"<<endl<<max<<" "<<startP<<" "<<endP<<endl;
            if(k!=T-1) cout<<endl;
     
     }
 }
、、、这份代码并不是我写的,呵呵,借鉴,因为个人觉得写的比较好。
这题也许更接近模拟吧,就是把每一步的走法模拟清楚,然后再写程序实现吧。
有人说这题类属于动态规划,贪心,看过优快云上大神们的做法,感觉也没有太大的区别,总体思路都差不多、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值