hdu-------1081To The Max
To The Max
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7681 Accepted Submission(s): 3724
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
Source
这道题个人觉得是到灰常好的题目,适合不同层次的人做,求的是最大连续子矩阵和....
方法一:
简单的模拟即可。首先求出每个阶段只和,然后得到这个状态,然后进行矩阵规划,求最大值即可
代码浅显易懂:
1 #include<cstdio>
2 #include<cstring>
3 #include<cstdlib>
4 #include<iostream>
5 using namespace std;
6 int arr[101][101];
7 int sum[101][101];
8 int main()
9 {
10 int nn,i,j,ans,temp;
11 // freopen("test.in","r",stdin);
12 while(scanf("%d",&nn)!=EOF)
13 {
14 ans=0;
15 memset(sum,0,sizeof(sum));
16 for(i=1;i<=nn;i++)
17 {
18 for(j=1;j<=nn;j++)
19 {
20 scanf("%d",&arr[i][j]);
21 sum[i][j]+=arr[i][j]+sum[i][j-1]; //求出每一层逐步之和
22 }
23 }
24 for(i=2;i<=nn;i++)
25 {
26 for(j=1;j<=nn;j++)
27 {
28 sum[i][j]+=sum[i-1][j]; //在和的基础上,逐步求出最大和
29 }
30 }
31 ans=0;
32 for(int rr=1;rr<=nn;rr++)
33 {
34 for(int cc=1;cc<=nn;cc++)
35 {
36 for(i=rr;i<=nn;i++)
37 {
38 for(j=cc;j<=nn;j++)
39 {
40 temp=sum[i][j]-sum[i][cc-1]-sum[rr-1][j]+sum[rr-1][cc-1];
41 if(ans<temp) ans=temp;
42 }
43 }
44 }
45 }
46 printf("%dn",ans);
47 }
48 return 0;
49 }
方法二:
采用状态压缩的方式进行DP求解最大值......!
代码:
1 /*基于一串连续数字进行求解的扩展*/
2 /*coder Gxjun 1081*/
3 #include<iostream>
4 #include<cstdio>
5 #include<cstring>
6 #include<cstdlib>
7 using namespace std;
8 const int nn=101;
9 int arr[nn][nn],sum[nn][nn];
10 int main()
11 {
12 int n,i,j,maxc,res,ans;
13 // freopen("test.in","r",stdin);
14 while(scanf("%d",&n)!=EOF)
15 {
16 memset(sum,0,sizeof(sum));
17 for(i=1;i<=n;i++)
18 for(j=1;j<=n;j++)
19 {
20 scanf("%d",&arr[i][j]);
21 sum[j][i]=sum[j][i-1]+arr[i][j];
22 }
23 ans=0;
24 for(i=1;i<=n;i++){
25
26 for(j=i;j<=n;j++)
27 {
28 res=maxc=0;
29 for(int k=1;k<=n;k++)
30 {
31 maxc+=sum[k][j]-sum[k][i-1];
32 if(maxc<=0) maxc=0;
33 else
34 if(maxc>res) res=maxc;
35 }
36 if(ans<res) ans=res;
37 }
38 }
39 printf("%dn",ans);
40 }
41 return 0;
42 }
- CSS 特殊属性介绍之 pointer-events
- 【java开发系列】—— 集合使用方法
- Elasticsearch安装
- 【java开发系列】—— 深克隆和浅克隆
- 【java开发系列】—— JDOM创建、修改、删除、读取XML文件
- AngularJs ng-route路由详解
- JAVA获取txt文件内容
- 关于 CSS 反射倒影的研究思考
- 【Javascript】—— 1 方法function的高级特性
- Oracle基础知识-SQL简单命令
- 【java开发系列】—— spring简单入门示例
- JavaMelody应用监控使用指南
- 记录安装oracle的那些事(四)之oracle 缺包安包
- 【面试虐菜】—— JAVA面试题(1)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- java.util.concurrent.TimeoutException: 的解决!
- HTTP Status 503 - Server is shutting down or failed to initialize
- LinkedHashMap 源码剖析
- 基于SSM框架与Maven的CRUD案例
- Java 8的这个新特性,你用了吗?
- JSP+Servlet项目整合
- springboot 整合 Mybatis、JPA、Redis
- 数据库中设置列/字段自增(Oracle和Mysql)
- 数据库中日期的插入(Oracle和Mysql)
- 浅谈在进行jsp页面编程时,路径问题的解决(绝对路径与相对路径)
- 浅谈springMVC中,中文乱码的显示问题(持续更新)
- 数据库的优化方法,一般人我不告诉他(建议收藏)
- idea 远程调试
- 嘘!高效快速的刷访问量(偷偷收藏)
- 进行JDBC操作时,连接参数的配置(附DBUtil工具类)