HDU-1003 Max Sum-动态规划-难度2

Max Sum


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 129864    Accepted Submission(s): 30100




Problem 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


代码:

/*HDU 1003 Max Sum */
/*
 *其实就是求最大子数组之和,不过需要保存起始点和结束点
 */
#include <cstdio>
#include <iostream>
#include <climits>
using namespace std;

const int  MAXN = 100002;
int dp[MAXN], first[MAXN];//first[i]保存dp[i]的起始点的位置,结束点的位置是他自己i

int main()
{
#ifdef _LOCAL
	freopen("F://input.txt", "r", stdin);
#endif
	int T, n;
	cin >> T;
    for(int j = 1; j <= T; j++)
	{
		cin >> n;
		int temp, position = 0;
		dp[0] = INT_MIN; //初始化边界条件,有负数的最小值不能是0或-1
		for(int i = 1; i <= n; i++)
		{
			cin >> temp;
			if(dp[i - 1] >= 0)
			{
				dp[i] = dp[i - 1] + temp;
				first[i] = first[i - 1];
			}
			else
			{
				dp[i] = temp;
				first[i] = i;
			}
			dp[i] = dp[i - 1] > 0 ? dp[i - 1] + temp : temp;
			if(dp[i] > dp[position]) position = i;
		}
		if(j > 1) cout << endl;
		cout << "Case " << j << ":" << endl << dp[position] <<  " " 
			 << first[position] << " " << position << endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的海滨体育馆管理系统,源码+数据库+毕业论文+视频演示 本基于Spring Boot的海滨体育馆管理系统设计目标是实现海滨体育馆的信息化管理,提高管理效率,使得海滨体育馆管理工作规范化、高效化。 本文重点阐述了海滨体育馆管理系统的开发过程,以实际运用为开发背景,基于Spring Boot框架,运用了Java技术和MySQL作为系统数据库进行开发,充分保证系统的安全性和稳定性。本系统界面良好,操作简单方便,通过系统概述、系统分析、系统设计、数据库设计、系统测试这几个部分,详细的说明了系统的开发过程,最后并对整个开发过程进行了总结,实现了海滨体育馆相关信息管理的重要功能。 本系统的使用使管理人员从繁重的工作中解脱出来,实现无纸化办公,能够有效的提高海滨体育馆管理效率。 关键词:海滨体育馆管理,Java技术,MySQL数据库,Spring Boot框架 本基于Spring Boot的海滨体育馆管理系统主要实现了管理员功能模块和学生功能模块两大部分,这两大功能模块分别实现的功能如下: (1)管理员功能模块 管理员登录后可对系统进行全面管理操作,包括个人中心、学生管理、器材管理、器材借出管理、器材归还管理、器材分类管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理、灯光保修管理、体育论坛以及系统管理。 (2)学生功能模块 学生在系统前台可查看系统信息,包括首页、器材、体育论坛以及体育资讯等,没有账号的学生可进行注册操作,注册登录后主要功能模块包括个人中心、器材管理、器材借出管理、器材归还管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值