每日一道算法题 Day6 of PAT 1007 Maximum Subsequence Sum (25分)

1007 Maximum Subsequence Sum (25分)

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.

Sample Input:

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

我去,今天网课尤其多,特别是体育课,录视频够呛,下午又看了两部电影,等我想起来要写题都已经九点了,害,事贵有恒,还是继续来做题。

今天在写题的时候看了下这道题的通过率,以为是一道很难的题,后来发现主要是好好读题,然后就是对于输入的数据要有包容性

题意大概是:从一个数列中,挑出一个连续子序列,使得该子序列在所有子序列中元素的加和值最大

这种题,不知道别人是怎么做的,我第一反应,遍历,先是遍历获得对于每一个起始点,能构成元素加和值最大的停止点,然后从这些子序列中找出加和值最大的即可

Sample Output:

10 1 4

Attention:

  • 如果输入的数组里都是复数,题目要求最后输出的sum为0,起始点就输出总序列的起始点即可,这里需要在最后输出的时候做一个判断

Details:

因为要考虑有全为负数的情形,所以我们在设置中间变量,进行加和比较的时候,这个变量赋初值不能像往常一样赋值为0,而是应该赋一个极小值(hhhhh,前几天学到的极大值设置加个负号即可)

Code:

#include <bits/stdc++.h>
using namespace std;
#define verysmall -0x3f3f3f3f
vector<int> maxTemp(10000);
vector<int> stop(10000);
vector<int> Seq;
int len=0;
int main(){
	cin>>len;
	for(int i=0;i<len;i++){
		int intermdia;
		cin>>intermdia;
		Seq.push_back(intermdia);
	} 
	
	for(int i=0;i<len;i++){
		int Sum=0;
		maxTemp[i]=verysmall;
		for(int j=i;j<len;j++){
			Sum+=Seq[j];
			if(Sum>maxTemp[i]){
				maxTemp[i]=Sum;
				stop[i]=j;
			}
		}
	}
	int temp=verysmall;
	int index;
	for(int i=0;i<len;i++){
		if(temp<maxTemp[i]){
			temp=maxTemp[i];
			index=i;
		}
	}
	if(temp<0)
		cout<<"0 "<<Seq[0]<<" "<<Seq[len-1];
	else
		cout<<temp<<" "<<Seq[index]<<" "<<Seq[stop[index]];
} 

Summary and harvest

(for my current level)

  • vector初始分配空间
  1. vector 构造函数
    like this: vector< int > info(100000); in this place, 10000 is the size of vector< int > info
  2. vector resize()函数
    like this: info.resize(2000);
    [vector resize() reserve()this: info.resize(2000);
    vector resize() reserve()辨析
  • 先构思题目的存储结构?Or先构思题目的算法结构?

今天我在写代码的时候想清楚了这件事,之前我都是先构思题目的存储结构,导致到最后思路很乱,脑子和精力都被各种存储结构给弄晕了,
但是,今天我一反常态,先从题目的算法结构入手,直接写数据的处理过程,缺少变量的也不着急,不是先去处理变量的声明、赋初值、分配空间,
而是只是想一个名字、想一个变量类型,然后直接开用,等主要的处理部分的代码都写好了,
再按照这部分算法的代码去进行变量的声明和赋初值、分配空间等一系列任务,就不会造成变量的冗余这种现象的发生,
要知道,以前我在写代码的时候最后总有一步是用来删除无用变量的,
总之,我的大脑还是很吃先算法结构后存储结构(落实到代码的顺序)这一套的,大家如果刚入门和我之前有同样困惑的话,不妨可以试试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值