poj 1050 To the Max
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 45906 | Accepted: 24276 |
Description
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
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
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2
Sample Output
15
Source
翻译:
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵。
比如,如下4 * 4的矩阵
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2的最大子矩阵是
9 2
-4 1
-1 8这个子矩阵的大小是15。
- 输入
- 输入是一个N * N的矩阵。输入的第一行给出N (0 < N <= 100)。再后面的若干行中,依次(首先从左到右给出第一行的N个整数,再从左到右给出第二行的N个整数……)给出矩阵中的N2个整数,整数之间由空白字符分隔(空格或者空行)。已知矩阵中整数的范围都在[-127, 127]。
- 输出
- 输出最大子矩阵的大小。
- 样例输入
-
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1 8 0 -2 - 样例输出
-
15 第一种
/*准备一个数组F[i,j]来存到第i,j格时的矩阵和(类似于前缀和)
对于一个子矩阵[x1,y1,x2,y2]//x1,x2代表左上角和右下角的横坐标
有 S[x1,y1,x2,y2] = f[x2,y2] - f[x1-1,y2] - f[x2,y1-1] + f[x1,y1];*/
#include<cstdio>
#include<iostream>
using namespace std;
#define N 101
int ans=-0x7f,n,a[N][N],f[N][N];
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
scanf("%d",&a[i][j]);
f[i][j]=f[i-][j]+f[i][j-]-f[i-][j-]+a[i][j];
}
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
for(int k=;k+i<=n;k++)
for(int l=;l+j<=n;l++){
int xx=i+k,yy=j+l;
ans=max(ans,f[xx][yy]-f[i-][yy]-f[xx][j-]+f[i-][j-]);
}
printf("%d\n",ans);
return ;
}
第二种
#include<cstdio>
#include<iostream>
using namespace std;
#define N 101
int a[N][N],n,ans=;
int main(){
scanf("%d",&n);
for(int i=,x;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&x),a[i][j]=a[i-][j]+x;//列前缀和
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++){
int tmp=;
for(int k=;k<=n;k++){
int num=a[j][k]-a[i-][k];
if(tmp>) tmp+=num;
else tmp=num;
ans=max(ans,tmp);
}
}
printf("%d\n",ans);
return ;
}
poj 1050 To the Max的更多相关文章
- POJ 1050 To the Max 最大子矩阵和(二维的最大字段和)
传送门: http://poj.org/problem?id=1050 To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- POJ 1050 To the Max 暴力,基础知识 难度:0
http://poj.org/problem?id=1050 设sum[i][j]为从(1,1)到(i,j)的矩形中所有数字之和 首先处理出sum[i][j],此时左上角为(x1,y1),右下角为(x ...
- POJ 1050 To the Max -- 动态规划
题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...
- poj 1050 To the Max (简单dp)
题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- [ACM_动态规划] POJ 1050 To the Max ( 动态规划 二维 最大连续和 最大子矩阵)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- poj 1050 To the Max(最大子矩阵之和,基础DP题)
To the Max Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 38573Accepted: 20350 Descriptio ...
随机推荐
- 2015年第3本(英文第2本):Daughter of Deceit
书名:Daughter of Deceit 作者: Victoria Holt (维多丽亚·荷特) 单词数:12万 词汇量:6000 首万词不重复词数:1700 蓝思值:570 阅读时间:1月12日- ...
- swift GCD使用指南
swift GCD使用指南 Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适 ...
- GCC编译器使用
一.GCC简介 通常所说的GCC是GUN Compiler Collection的简称,除了编译程序之外,它还含其他相关工具,所以它能把易于人类使用的高级语言编写的源代码构建成计算机能够直接执行的二进 ...
- Android 设置EditText光标Curso颜色及粗细
在android的输入框里,如果要修改光标的颜色及粗细步骤如下两步即可搞定: 1.在资源文件drawable下新建一个光标控制color_cursor.xml <?xml version=&qu ...
- 基础学习day12--多线程一线程之间的通信和常用方法
一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信. 等待/唤醒机制涉及的方法: 1. wait():让线程处于冻结状态,被wa ...
- GCD中的dispatch_set_target_queue的用法及作用
(一),使用dispatch_set_target_queue更改Dispatch Queue的执行优先级 dispatch_queue_create函数生成的DisPatch Queue不管是Ser ...
- 一些在IOS中关于JS、H5开发的网站
1.JSPatch 2.
- QA:java.lang.RuntimeException:java.io.FileNotFoundException:Resource nexus-maven-repository-index.properties does not exist.
QA:java.lang.RuntimeException:java.io.FileNotFoundException:Resource nexus-maven-repository-index.pr ...
- Effective Java 35 Prefer annotations to naming patterns
Disadvantages of naming patterns Typographical errors may result in silent failures. There is no way ...
- Effective Java 75 Consider using a custom serialized form
Principle Do not accept the default serialized form without first considering whether it is appropri ...