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

题意:求子矩阵的和的最大值

思路:把多维转化为一维,只要会一维的就简单了。。。

To The Max

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

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
View Code

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define Max(x,y) x>y?x:y
 4 #define INF -0x7fffffff/2
 5 int max_sub(int x[],int n)
 6 {
 7     int max=x[0];
 8     int sum=0;
 9     int i;
10     for(i=0;i<n;i++)
11     {
12         sum+=x[i];
13         if(sum<0)
14         {
15             sum=0;
16         }
17         if(max<sum)
18         {
19             max=sum;
20         }
21     }
22
23     return max;
24 }
25 int  main()
26 {
27     int t;
28     int i,j,k;
29     int aa[105][105];
30     int bb[105];
31     int maxx;
32
33     while(~scanf("%d",&t))
34     {
35         maxx=INF;
36         for(i=0;i<t;i++)
37         {
38             for(j=0;j<t;j++)
39             {
40                 scanf("%d",&aa[i][j]);
41             }
42         }
43
44         for(i=0;i<t;i++)
45         {
46             memset(bb,0,sizeof(bb));
47             for(j=i;j<t;j++)
48             {
49                 for(k=0;k<t;k++)
50                 {
51                     bb[k]+=aa[j][k];
52                 }
53
54                 maxx=maxx>max_sub(bb,t)?maxx:max_sub(bb,t);
55             }
56         }
57         printf("%d\n",maxx);
58     }
59 }

转载于:https://www.cnblogs.com/1114250779boke/archive/2012/08/05/2624312.html

hdu 1081To The Max相关推荐

  1. HDU 1024:Max Sum Plus Plus(DP)

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

  2. 【HDU - 1024 】Max Sum Plus Plus (dp及优化,最大m子段和)

    题干: Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, ...

  3. 2021-01-25广州大学ACM寒假训练赛解题心得

    https://vjudge.net/contest/419545 目录 A - Airplane AtCoder - abc129_a B - Balance AtCoder - abc129_b ...

  4. hdu 1081 To The Max(最大子段和的升级版,二维)

    http://acm.hdu.edu.cn/showproblem.php?pid=1081 一维最大字段和:dp [ i ] = max ( dp[ i-1 ]  , 0 ) + a [ i ] ; ...

  5. hdu 1003 Max Sum 解题报告

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem Description Given a sequence a[1],a[2],a[3 ...

  6. HDU 1003——Max Sum(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:历遍所有数字,找出最大字段和. 解题思路: t和n:记录循环次数和每一段有多少个数字 ...

  7. HDU 2993 MAX Average Problem(斜率优化DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...

  8. I love max and multiply HDU - 6971(详细解答)

    I love max and multiply HDU - 6971 题意: 数组a和b,现在构造一个数组c,使得c[k]=max(a[i] * b[j]) , i&j>=k 求数组c的 ...

  9. Max Sum Plus Plus HDU - 1024

    Max Sum Plus Plus HDU - 1024 题意: 给你n个数,选m个子段,各个子段连续且不相交,长度可以为1,设maxn为各个子区间的和,求最大的maxn. 题解: 设dp[i][j] ...

最新文章

  1. 红帽企业版Linux成为Linux下的.NET Core的参考平台
  2. linux npm环境变量,linux配置npm环境变量
  3. ubunt18.04LTS+vscode+anaconda3下的python+C++调试
  4. 车险赔付率分析报告_机动车辆保险赔付率高的原因分析及对策研究
  5. 重庆高考成绩查询2021时间几号,2021重庆高考时间是几号
  6. 【Flink Forward Asia 2021】活动报告出炉,实时即未来!
  7. 将一幅图像转换为灰度图
  8. ubuntu 16.04 安装和卸载postman
  9. MySQL innodb存储引擎的数据存储结构
  10. 计算机数据采集管理系统的结构和功能,生产数据采集系统结构、功能及特点
  11. 用Matlab作函数的图像
  12. PowerBuilder/PB常用备忘
  13. 科技百咖 | 华途少帅谢永胜眼中的数据安全治理
  14. 惊了,深圳房价比北京还高。。。
  15. P1014 [NOIP1999 普及组] Cantor 表
  16. c++程序设计基础-类与对象:类的定义
  17. [含lw+开题报告+源码等]SSM酒店管理系统|旅店管理[包运行成功]
  18. c语言第二版课后答案pdf,数据结构(C语言版)第2版习题答案—严蔚敏.pdf
  19. LPK木马分析-02
  20. SQL Server使用代码创建数据库主文件日志文件

热门文章

  1. 十面阿里,七面头条,你猜我进阿里没?
  2. 看漫画就能学SQL,简直太cool了
  3. Java 5~11各个版本新特性史上最全总结
  4. 史上最良心程序员,在代码注释里,告诉这家公司有多坑
  5. 不懂卷积神经网络?别怕,看完这几张萌图你就明白了
  6. SpringBoot(1.5.6.RELEASE)源码解析(一)
  7. Linux:shell脚本中实现变量自增的几种方式
  8. 操作系统:用户态和核心态的区别
  9. 什么是Scanner?next()和hasNext() ? nextLine()和hasNextLine()?
  10. IDEA_Spring Data JPA有关报错Cannot resolve table 'XXX'