解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾

2017-09-06 21:32:22

writer:pprp

可以作为一个模板

/*
@theme: hdu1003 Max Sum
@writer:pprp
@end:21:26
@declare:连续区间最大和
@data:2017/9/6
*/

#include <bits/stdc++.h>

using namespace std;

int main()
{
    //freopen("in.txt","r",stdin);
    int cas;
    cin >> cas;
    for(int l = 1; l <= cas; l++)
    {
        int n;
        cin >> n;
        int tmp = 0, ans = -999, data2;
        int op = 1, ed = 1, k = 1;

        for(int i = 1 ; i <= n ; i++)
        {
            cin >> data2;
            tmp += data2;

            if(ans < tmp)//两个顺序不能调换,因为op这里要用上一个k
            {
                ans = tmp;
                ed = i;
                op = k;
            }

            if(tmp < 0)
            {
                tmp = 0;
                k = i + 1;
            }
        }
        if(l != 1)
            cout << endl;

        cout << "Case " << l << ":" << endl;

        cout << ans << " " << op <<  " " << ed << endl;
    }

    return 0;
}
时间: 2024-12-18 20:33:38

解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾的相关文章

HDU1003 Max Sum

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意:给你一组数字,求出最大的字段和. 思路:这是一个经典的dp题目,定义数组a储存一组数字,a[j]为ji个数,dp[j]表示已j结尾的最大字段和,那么dp[j]=max(dp[j-1]+a[j],dp[j]). 例如: a[]       6   -1   5    4    -7 dp[]     6    5   10  14    7 代码如下: #include <iostream

HDU1003 Max Sum 最大子序列和的问题【四种算法分析+实现】

就拿杭电OJ上的第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 + 4 = 14. Input The fi

HDU1003 Max Sum 解题报告

目录 题目信息 Problem Description Input Output Sample Input Sample Output 题解 思路 源代码 题目信息 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

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

hdu1003 Max Sum(经典dp )

A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 thi

HDU1003 Max Sum(最大连续子序列和)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1003 简单dp,状态转移方程:sum[i] = max{sum[i-1]+a[i],a[i]}. (sum[i]记录以a[i]为子序列末端的最大连续和.) 对于a[i]这个数字,我们考虑是否将它选入之前连续的序列. 如果选,状态变为sum[i-1]+a[i] ; 如果不选,则从此开始一个新的序列,故和为a[i]. 1 #include<cstdio> 2 int main() 3 { 4 int T,

HDU1003 Max Sum(求最大字段和)

事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:

ACM学习历程—HDU1003 Max Sum(dp &amp;&amp; 最大子序列和)

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 inte

HDU1003 Max Sum 题解 动态规划 最大字段和扩展

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意: 求解一个序列的最大字段和,已经最前面的那个最大子段的起止坐标. 解题思路: 定义状态 \(f[i]\) 为以 \(a[i]\) 结尾的最大字段和,则有状态转移方程: \[f[i] = \max(0, f[i-1]) + a[i]\] 同时定义状态 \(s[i]\) 表示以 \(a[i]\) 结尾的最大字段的最左边元素的坐标,则有: 当 \(f[i] \lt 0\) 时,\(s[i]