Ural-1146Maximum Sum-最大子矩阵
| Time limit: 0.5 second | Memory limit: 64 MB |
|---|
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. 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. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.
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-hand corner and has the sum of 15.
Input
The input consists of an N × 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 white-space (newlines and spaces). These N 2 integers make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on 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
The output is the sum of the maximal sub-rectangle.
Sample
| input | output |
|---|---|
| 4 | 15 |
| 0 -2 -7 0 | |
| 9 2 -6 2 | |
| -4 1 -4 1 | |
| -1 8 0 -2 |
最大子矩阵和:以前做过一维的最大连续和,现在换成二维,而思路还是类似,不过要提前将二维的转化为一维,处理起来就是很方便,求出每一行的前缀和,然后枚举列,即可求出最大子矩阵和。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int a[110][110];
int ans ;
int main()
{
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);//前缀和
a[i][j]+=a[i][j-1];
}
}
ans = -10000000;
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)//枚举列每一次求第i列到第j列之间的最大和
{
int Max = -10000000;
int sum = 0;
for(int k=1;k<=n;k++)
{
sum+=(a[k][j]-a[k][i-1]);
Max = max(Max,sum);
if(sum<0)
{
sum = 0;
}
}
ans = max(ans,Max);
}
}
printf("%d\n",ans);
}
return 0;
}
Ural-1146Maximum Sum-最大子矩阵的更多相关文章
- URAL 1146 Maximum Sum 最大子矩阵和
题目:click here #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; con ...
- Ural 1068 - Sum
Your task is to find the sum of all integer numbers lying between 1 and N inclusive. Input The input ...
- URAL 1658. Sum of Digits(DP)
题目链接 隔了一年零三个月,重新刷URAL,这题挺麻烦的输出路径.输出路径挺扯的,乱写了写乱改改就A了...我本来想用很靠谱,记录每一条路径的,然后输出最小的,结果Tle,然后我使劲水水又过了一组,发 ...
- URAL 1658 Sum of Digits
URAL 1658 思路: dp+记录路径 状态:dp[i][j]表示s1为i,s2为j的最小位数 初始状态:dp[0][0]=0 状态转移:dp[i][j]=min(dp[i-k][j-k*k]+1 ...
- 长乐国庆集训Day5-2
T1 彩虹 题目 [题目描述] Mr.Raju和他的一个大家庭外出度假,他们想要乘着彩虹欣赏周围的景色,但是这样最会有一些问题. 在他们家族中,如果一个人想要骑上彩虹,那么他喜欢的所有人和喜欢他的所有 ...
- Java实现 LeetCode 363 矩形区域不超过 K 的最大数值和
363. 矩形区域不超过 K 的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,- ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵
18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...
随机推荐
- iOS 因为reason: 'Pushing the same view controller instance more than once is not supported而奔溃(下)
这个问题是什么意思呢,之前遇到过几次,但程序再次打开时没有问题,也就没有重视,今天又遇到了,无法忍受啊. 控制台报的错误是:"不支持多次推入相同的视图控制器实例". 什么原因造成的 ...
- Node.js 学习资源
这篇文章编译整理自Stack Overflow的一个如何开始学习Node.js的Wiki帖,这份资源列表在SO上面浏览接近60万次,数千个收藏和顶.特意整理发布到这里,其中添加了部分中文参考资料. 学 ...
- zabbix agent安装与配置篇
Zabbix监控windows部署安装 Zabbix agent 在windows上安装部署 (1)手工安装zabbix agent客户端 1. 下载与解压 地址: http://www.zabb ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
- Python开发【十一章】:RabbitMQ队列
RabbitMQ队列 rabbitMQ是消息队列:想想之前的我们学过队列queue:threading queue(线程queue,多个线程之间进行数据交互).进程queue(父进程与子进程进行交互或 ...
- Windows Server 2008 R2域控组策略设置禁用USB
问题: Windows Server 2008 R2域控服务器如何禁用客户端使用USB移动存储(客户端操作系统需要 Windows Vista以上的操作系统,XP以下的操作系统不能禁用USB移动存储) ...
- Python之路-python(堡垒机)
运维堡垒机开发 前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多人觉得,堡垒机就是跳板机,其实这个认识是不全面的,跳板功能只是堡垒机所具备的 ...
- .NET编译项目时出现《此实现不是 Windows 平台 FIPS 验证的加密算法的一部分》处理方法
有用户提出在编译代码时出现源文件“D:\.......ervice.cs”未能打开(“此实现不是 Windows 平台 FIPS 验证的加密算法的一部分.”)的问题,如下图所示: 对于上面的问题,只需 ...
- HTTP和HTTPS的区别
1.HTTP和HTTPS的概念 超文本传输协议HTTP(Hypertext transfer protocol)是一种详细规定了浏览器和万维网服务器之间相互通信的规则,通过因特网传送万维网文档的数据传 ...
- poj 3190 Stall Reservations
http://poj.org/problem?id=3190 Stall Reservations Time Limit: 1000MS Memory Limit: 65536K Total Su ...