链接:

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

这道题使用到的算法是:预处理+最大连续子串和

如果会做最大连续子串和,那么理解这题就相对简单一些,若不知道最大连续子串和,建议先看一下这两题:

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

http://www.cnblogs.com/YY56/p/4855766.html

To The Max

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

Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.

As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2

is in the lower left corner:

9 2
-4 1
-1 8

and has a sum of 15.

 
Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
 
Output
Output the sum of the maximal sub-rectangle.
 
Sample Input
4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1
8 0 -2
 
Sample Output
15
 
之前一直不理解虽知道是dp,却不知这是从何而来的,如何计算,

代码1:

#include<stdio.h>
#include<string.h>
#include<stdlib.h> #define N 200
#define oo 0x3f3f3f3f int a[N][N], dp[N][N]; int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i, j, j1, j2; memset(dp, , sizeof(dp));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
scanf("%d", &a[i][j]);
dp[i][j] = dp[i][j-] + a[i][j]; /// dp[i][j] i 代表的是第 i 行,j 代表的是这行前 j 个数的和
} int S = ;
for(j1=; j1<=n; j1++)
for(j2=j1; j2<=n; j2++)
{ /** * i 很明显代表的是行数
* j1 从第几列开始
* j2 从第几列结束 **/ int mx=, my=; for(i=; i<=n; i++)
{
mx += dp[i][j2] - dp[i][j1-]; /// mx 代表的是前 i 行里,从第j1-1列到j2列的和(相当于矩阵了) if(mx>=)
{
if(mx>my) my = mx; /// my 记录的是前 i 行里,从第j1-1列到第j2列矩阵的最大和
}
else mx = ;
}
if(my>=S) S = my; /// S 里面存的肯定是在所有矩阵中取最大值
} printf("%d\n", S);
} return ;
}

代码2:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max2(a,b) (a>b?a:b) #define N 110
#define INF 0xfffffff int a[N][N], b[N][N][N]; int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
int i, j, k, x, max1; memset(a, , sizeof(a));
memset(b, , sizeof(b)); for(i=; i<=n; i++)
for(j=; j<=n; j++)
scanf("%d", &a[i][j]); max1=-INF;
for(i=; i<=n; i++)
for(j=; j<=n; j++)
for(x=, k=j; k>; k--)
{
x += a[i][k]; b[i][j][k] = max2(b[i][j][k], b[i-][j][k]) + x; if(b[i][j][k]>max1)
max1 = b[i][j][k];
} printf("%d\n", max1);
}
return ;
}

题目比较水暴力也可以过

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; #define met(a,b) (memset(a,b,sizeof(a)))
#define N 110
#define INF 0xffffff int a[N][N], sum[N][N]; int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i, j, i1, j1, Max=-INF; met(a, );
met(sum, ); for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
scanf("%d", &a[i][j]);
} for(i=; i<=n; i++)
for(j=; j<=n; j++)
sum[i][j] = sum[i-][j]+sum[i][j-]-sum[i-][j-] + a[i][j]; for(i=; i<=n; i++)
for(j=; j<=n; j++)
for(i1=i+; i1<=n; i1++)
for(j1=j+; j1<=n; j1++)
{
Max = max(Max, sum[i1][j1]-sum[i1][j]-sum[i][j1]+sum[i][j]);
} printf("%d\n", Max);
}
return ;
}

(DP)To The Max --HDU -- 1081的更多相关文章

  1. 2017百度之星资格赛 1003:度度熊与邪恶大魔王(DP)

    .navbar-nav > li.active > a { background-image: none; background-color: #058; } .navbar-invers ...

  2. Tour(dp)

    Tour(dp) 给定平面上n(n<=1000)个点的坐标(按照x递增的顺序),各点x坐标不同,且均为正整数.请设计一条路线,从最左边的点出发,走到最右边的点后再返回,要求除了最左点和最右点之外 ...

  3. 最长公共子序列长度(dp)

    /// 求两个字符串的最大公共子序列长度,最长公共子序列则并不要求连续,但要求前后顺序(dp) #include <bits/stdc++.h> using namespace std; ...

  4. Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)

    Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...

  5. Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)

    Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...

  6. Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)

    Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...

  7. Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)

    Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...

  8. Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)

    Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...

  9. Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV)

    Leetcode之动态规划(DP)专题-188. 买卖股票的最佳时机 IV(Best Time to Buy and Sell Stock IV) 股票问题: 121. 买卖股票的最佳时机 122. ...

随机推荐

  1. 记录tomcat的完整日志

    Tomcat报的错太含糊了,什么错都没报出来,只提示了Error listenerStart.为了调试,我们要获得更详细的日志.可以在WEB-INF/classes目录下新建一个文件叫logging. ...

  2. Visual Studio环境变量、工作目录、vc++目录、 命令等 的配置和作用

    在调试 Visual Studio 2008 程序时,经常有一些动态链接库(即 dll 文件)需要加载到工程里,这样才能依赖第三方库进行程序调试. 这些动态链接库,往往都是测试版本或是开发中的版本,或 ...

  3. 学习笔记之Tips for Macbook

    写给Mac新手的入门指南 - 威锋网 https://mp.weixin.qq.com/s/pqmqGZhNwevx57KeLnzZmg https://bbs.feng.com/read-htm-t ...

  4. Oracle 和 MySQL的区别(不完整)

    1. Oracle对单双引号要求的很死,一般不准用双引号,不然会报错.MySQL 单双引号都可以. 2. 事务提交方式:Oracle 默认手动提交,MySQL 默认自动提交. 3. 分页:MySQL有 ...

  5. 同一客户端使用多份SSH Key

    创建或添加如下内容: Host example1.com HostName realname.example.com IdentityFile ~/.ssh/example1_rsa # 私钥 Hos ...

  6. sqoop导出到mysql中文乱码问题总结、utf8、gbk

    sqoop导出到mysql中文乱码问题总结.utf8.gbk 今天使用sqoop1.4.5版本的(hadoop使用cdh5.4)因为乱码问题很是头痛半天.下面进行一一总结 命令: [root@sdzn ...

  7. 3_bootsrap布局容器

    3.布局容器 BootStrap必须需要至少一个布局容器,才能为页面内容进行封装和方便的样式控制. 相当于一个画板. 帮助手册位置:全局CSS样式------->概览------->布局容 ...

  8. php自动生成mysql的触发代码。

    如果公司里有上百个表要做触发器,如果手动写代码的话.很累,所以今天写了一个小程序, <?php $dbname = 'test';//数据库 $tab1 = 'user'; //执行的表 $ta ...

  9. ScheduledThreadPoolExecutor 线程池调度 使用

    package other; import java.util.concurrent.Callable; import java.util.concurrent.Executors; import j ...

  10. C++全总结

    // CPPTEST.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include & ...