hdu 1003 Max Sum 简单动态规划

很简单的动态规划但ac率低于四分之一了,状态转移方程:

dp[i]=max(dp[i-1]+a[i],a[i])

注意几点:

case 之间有空格

输入的最小负数为-1000

有多组答案找出第一个的意思是,从头便利,得到第一个最大的和就输出被,然后break;

/*************************************************************************
	> File Name: hdu1231.cpp
	> Author: yang
	> Mail:[email protected]
	> Created Time: 2014年08月24日 星期日 20:01:52
 ************************************************************************/

#include<iostream>
#include<stdio.h>
#include<memory.h>
using namespace std;
#define N 100005
int main(){
	int n;
	int a[N],dp[N],start[N],t,p=1;
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		int flag=0,cou=0;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			dp[i]=0;
		}
		int x=1;
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++){
			if(dp[i-1]+a[i]>=a[i])
				dp[i]=dp[i-1]+a[i];
			else{
				x=i;
				dp[i]=a[i];
			}
			start[i]=x;
		}
		cou=-1002;
		printf("Case %d:\n",p++);
		for(int i=1;i<=n;i++)
			if(dp[i]>cou)
				cou=dp[i];
		for(int i=1;i<=n;i++){
			if(cou==dp[i]){
				cout<<dp[i]<<" "<<start[i]<<" "<<i<<endl;
				break;
			}
		}
		if(t!=0)
			cout<<endl;

	}
}
时间: 2024-11-03 21:20:13

hdu 1003 Max Sum 简单动态规划的相关文章

HDU 1003 Max Sum【动态规划求最大子序列和详解 】

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 59365 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

HDU - 1003 Max Sum (思维 || 动态规划)

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

hdu 1003 Max Sum 简单DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 转移方程:dp[i]=max(dp[i-1]+a[i],a[i]) 虽然是dp 但不用真的申请一个dp数组 #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <cmath> #include <cstring> #in

HDU 1003——Max Sum(动态规划)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:历遍所有数字,找出最大字段和. 解题思路: t和n:记录循环次数和每一段有多少个数字 temp,now,max:temp存放临时读取的变量,now代表现在和,max代表当前最大和,如果前面相加后是负数,而后一位是正数,则更新起点位置. 代码如下: 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 in

HDU 1003 Max Sum (动规)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 141547    Accepted Submission(s): 32929 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU 1003 Max Sum 最大连续子序列的和

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

[ACM] hdu 1003 Max Sum(最大子段和模型)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 135262    Accepted Submission(s): 31311 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU 1003 Max Sum(dp,最大连续子序列和)

Max Sum 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

HDU 1003 Max Sum(最大子列和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 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