链接:

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:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. #define N 200
  6. #define oo 0x3f3f3f3f
  7.  
  8. int a[N][N], dp[N][N];
  9.  
  10. int main()
  11. {
  12. int n;
  13.  
  14. while(scanf("%d", &n)!=EOF)
  15. {
  16. int i, j, j1, j2;
  17.  
  18. memset(dp, , sizeof(dp));
  19. for(i=; i<=n; i++)
  20. for(j=; j<=n; j++)
  21. {
  22. scanf("%d", &a[i][j]);
  23. dp[i][j] = dp[i][j-] + a[i][j]; /// dp[i][j] i 代表的是第 i 行,j 代表的是这行前 j 个数的和
  24. }
  25.  
  26. int S = ;
  27. for(j1=; j1<=n; j1++)
  28. for(j2=j1; j2<=n; j2++)
  29. {
  30.  
  31. /**
  32.  
  33. * i 很明显代表的是行数
  34. * j1 从第几列开始
  35. * j2 从第几列结束
  36.  
  37. **/
  38.  
  39. int mx=, my=;
  40.  
  41. for(i=; i<=n; i++)
  42. {
  43. mx += dp[i][j2] - dp[i][j1-]; /// mx 代表的是前 i 行里,从第j1-1列到j2列的和(相当于矩阵了)
  44.  
  45. if(mx>=)
  46. {
  47. if(mx>my) my = mx; /// my 记录的是前 i 行里,从第j1-1列到第j2列矩阵的最大和
  48. }
  49. else mx = ;
  50. }
  51. if(my>=S) S = my; /// S 里面存的肯定是在所有矩阵中取最大值
  52. }
  53.  
  54. printf("%d\n", S);
  55. }
  56.  
  57. return ;
  58. }

代码2:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define max2(a,b) (a>b?a:b)
  5.  
  6. #define N 110
  7. #define INF 0xfffffff
  8.  
  9. int a[N][N], b[N][N][N];
  10.  
  11. int main()
  12. {
  13. int n;
  14. while(scanf("%d", &n)!=EOF)
  15. {
  16. int i, j, k, x, max1;
  17.  
  18. memset(a, , sizeof(a));
  19. memset(b, , sizeof(b));
  20.  
  21. for(i=; i<=n; i++)
  22. for(j=; j<=n; j++)
  23. scanf("%d", &a[i][j]);
  24.  
  25. max1=-INF;
  26. for(i=; i<=n; i++)
  27. for(j=; j<=n; j++)
  28. for(x=, k=j; k>; k--)
  29. {
  30. x += a[i][k];
  31.  
  32. b[i][j][k] = max2(b[i][j][k], b[i-][j][k]) + x;
  33.  
  34. if(b[i][j][k]>max1)
  35. max1 = b[i][j][k];
  36. }
  37.  
  38. printf("%d\n", max1);
  39. }
  40. return ;
  41. }

题目比较水暴力也可以过

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. #define met(a,b) (memset(a,b,sizeof(a)))
  9. #define N 110
  10. #define INF 0xffffff
  11.  
  12. int a[N][N], sum[N][N];
  13.  
  14. int main()
  15. {
  16. int n;
  17.  
  18. while(scanf("%d", &n)!=EOF)
  19. {
  20. int i, j, i1, j1, Max=-INF;
  21.  
  22. met(a, );
  23. met(sum, );
  24.  
  25. for(i=; i<=n; i++)
  26. for(j=; j<=n; j++)
  27. {
  28. scanf("%d", &a[i][j]);
  29. }
  30.  
  31. for(i=; i<=n; i++)
  32. for(j=; j<=n; j++)
  33. sum[i][j] = sum[i-][j]+sum[i][j-]-sum[i-][j-] + a[i][j];
  34.  
  35. for(i=; i<=n; i++)
  36. for(j=; j<=n; j++)
  37. for(i1=i+; i1<=n; i1++)
  38. for(j1=j+; j1<=n; j1++)
  39. {
  40. Max = max(Max, sum[i1][j1]-sum[i1][j]-sum[i][j1]+sum[i][j]);
  41. }
  42.  
  43. printf("%d\n", Max);
  44. }
  45. return ;
  46. }

(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. bzoj2004公交线路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2004 好美妙的矩阵乘. 思考: 0.在一个序列上.所以考虑dp. 1.p<=10,k& ...

  2. Linux下Moodle平台的快速安装方案

    一种快速安装与配置Moodle平台的方案,基本步骤: 1.选择与安装Linux系统 2.配置网络,开启shh和网络端口 3.一键安装集成环境(使用oneinstack方案) 4.服务器配置,端口和PH ...

  3. SpringCloud初体验:三、Feign 服务间调用(FeignClient)、负载均衡(Ribbon)、容错/降级处理(Hystrix)

    FeignOpenFeign Feign是一种声明式.模板化的HTTP客户端. 看了解释过后,可以理解为他是一种 客户端 配置实现的策略,它实现 服务间调用(FeignClient).负载均衡(Rib ...

  4. SQL中禁用trigger

    SQL中禁用所有trigger   编写人:CC阿爸 2014-6-15 在日常SQL数据库的操作中,如何快速的禁用所有trigger? --禁用某个表上的所有触发器 ALTER  

  5. Makefile中进行宏定义-***

    实际上是gcc命令支持-D宏定义,相当于C中的全局#define: gcc -D name gcc -D name=definition Makefile中可以定义变量(和宏很像),但是是给make解 ...

  6. appium -ios-安卓 获取元素时 配置参数的方法

    iPhone_5S{ "automationName": "XCUITest", "platformName": "iOS&quo ...

  7. xlrd库的使用

  8. FilenameFilter 文件名过滤

    public static final FilenameFilter JSON_CONFIG_FILE_FILTER = new FilenameFilter() {         @Overrid ...

  9. 什么是ODBC ?

    ODBC(Open Database Connectivity,开放数据库互连)是微软公司开放服务结构(WOSA,Windows Open Services Architecture)中有关数据库的一 ...

  10. selenium+python自动化83-pip安装selenium报Read time out HTTPSConnectionPool(host='pypi.python.org' port443)

    遇到问题 1.有些小伙伴在用pip安装selenium时候报 Read time out HTTPSConnectionPool(host='pypi.python.org' port443) 2.估 ...