HDU 1003 && HDU 1081(最大子列和,最大子矩阵和).

最大子列和,及其扩展最大子矩阵和,都是比较经典的dp,把这两题写在一起,共大家参考学习。

~~~~

lz弱菜啊,到现在还只能写这么水的DP...orz。

~~~~

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

http://acm.hdu.edu.cn/showproblem.php?pid=1081

题意应该比较好理解,不解释了。

1003:

#include<cstdio>
#include<iostream>
#include<cstring>
#define INF 1111
#define N 111111
using namespace std;

int a[N],dp[N];
int main()
{
    int t,T=0;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        dp[n]=a[n];
        if(T) puts("");
        printf("Case %d:\n",++T);
        int ans=dp[n],l=n,r=n;
        for(int i=n-1;i>=1;i--)
        {
            dp[i]=max(dp[i+1]+a[i],a[i]);   //状态转移
            if(ans<=dp[i])
            {
                ans=dp[i];
                l=i;    //记录最大子序列区间的左端点
            }
        }
        int s=0;
        for(int i=l;i<=n;i++)   //根据左端点找右端点
        {
            s += a[i];
            if(s == ans)
            {
                r=i;
                break;
            }
        }
        printf("%d %d %d\n",ans,l,r);
    }
}

!!DP部分也可以这样写。

for(i=1;i<n;i++)
{
    scanf("%d",&a);
    if(t<0) t=a;
    else t=t+a;
    if(ans<t) ans=t;
}

~~~~

1081:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 111
#define INF 0x3f3f3f3f
using namespace std;

int n;
int g[N][N],sub[N],f[N];
int dp()
{
    int Max=-INF;
    f[n-1]=sub[n-1];
    for(int i=n-2;i>=0;i--)
    {
        f[i]=max(f[i+1]+sub[i],sub[i]);
        Max=max(Max,f[i]);
    }
    return Max;
}
int qyj()
{
    int ans=-INF;
    for(int i=0;i<n;i++)   //枚举行
    {
        memset(sub,0,sizeof(sub)); //~~
        for(int j=i;j<n;j++)
        {
            for(int k=0;k<n;k++)
            {
                sub[k] += g[j][k];  //子矩阵
            }
            int sum=dp();   //~~
            ans=max(ans,sum);
        }
    }
    return ans;
}
int main()
{
    while(scanf("%d",&n)==1)
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&g[i][j]);
        int ans=qyj();
        printf("%d\n",ans);
    }
    return 0;
}

1081不理解的可以手写画画得到子矩阵的那段代码、

HDU 1003 && HDU 1081(最大子列和,最大子矩阵和).,布布扣,bubuko.com

时间: 2024-08-11 22:20:19

HDU 1003 && HDU 1081(最大子列和,最大子矩阵和).的相关文章

HDU - 1003 - Max Sum &amp;&amp; POJ - 1050 - To the Max (经典DP问题)

题目传送:HDU - 1003 思路:最大子序列和 dp[i]= a[i]   (dp[i-1]<0) dp[i]= dp[i-1]+a[i]   (dp[i-1]>=0) AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include

dp 动态规划 hdu 1003 1087

动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和,其实就是分成 以0结尾的序列 以1结尾的序列 以2结尾的序列 ... 以n结尾的序列 所以以n结尾的序列的最大值就是以n-1结尾的序列的最大值+n的值 或最大值是n的值 关系式: d[i]=max(d[i-1]+a[i],a[i]) d[i]为以i结尾的序列和的最大值,a[i]为第i个数的值 #in

【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)

问题描述:       连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, -4, 13 },最大和为20. =============================================================== 问题分析: 1.首先最朴素的方法是暴力 O(n^3) 直接两个for循环枚举子序列的首尾,然后再来个循环计算序列的和,每次更新和的最大值.

sum max(hdu 1003)

观察可以发现,0,1,2,--,n结尾的分组中, maxsum a[0] = a[0] maxsum a[1] = max( a[0] + a[1] ,a[1])  = max( maxsum a[0] + a[1] ,a[1]) maxsum a[2] = max( max ( a[0] + a[1] + a[2],a[1] + a[2] ),a[2]) = max(  max( a[0] + a[1] ,a[1]) + a[2] , a[2]) = max(  maxsum a[1] + a

HDU 1003 Max Sum(DP)

题目地址:HDU 1003 DP好弱..先补补DP的基础... 这题就是记录起始点与终止点,然后每当发现一个更大的,就更新.从左往右扫一遍就行了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #

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)

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

[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

2014多校第七场1003 || HDU 4937 Lucky Number

题目链接 题意 : 给定一个十进制n,让你转化成某个进制的数,让这个数只包含3 4 5 6这些数字,这个进制就成为n的幸运数字,输出有多少幸运数字,例如19,5进制表示是34,所以5是19的一个幸运数. 思路 : 以下思路有这里提供 先考虑特殊情况,所情况下会有无穷个?只有n=3,4,5,6的时候,因为这几个数在大于n的进制下都是他本身..注意特殊情况不包括33,343这些(我一开始就死在这里了,wa了三次).因为33在34进制下就不是33了(类似于10在16进制下就是A了). 我们知道n=a0