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] = i\);
  • 当 \(f[i] \ge 0\) 时,\(s[i] = s[i-1]\)

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int T, n, a[maxn], f[maxn], anss, anst, s[maxn], ans;
int main() {
    scanf("%d", &T);
    for (int cas = 1; cas <= T; cas ++) {
        if (cas > 1) puts("");
        scanf("%d", &n);
        for (int i = 1; i <= n; i ++) scanf("%d", a+i);
        ans = f[1] = a[1];
        anss = anst = s[1] = 1;
        for (int i = 2; i <= n; i ++) {
            if (f[i-1] >= 0) {
                f[i] = f[i-1] + a[i];
                s[i] = s[i-1];
            }
            else {
                f[i] = a[i];
                s[i] = i;
            }
            if (f[i] > ans) {
                ans = f[i];
                anss = s[i];
                anst = i;
            }
        }
        printf("Case %d:\n", cas);
        printf("%d %d %d\n", ans, anss, anst);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/quanjun/p/12189728.html

时间: 2024-11-06 11:41:25

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 - 最大连续区间和 - 计算开头和结尾

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

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(求最大字段和)

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

1003 Max Sum(动态规划)

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

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【动态规划求最大子序列和详解 】

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

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

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