1146. Maximum 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 integerN on a line by itself indicating the size of the square two dimensional array. This is followed byN 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
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
15
 
Difficulty: 97
 
题意:求最大的子矩阵
分析:很经典的题。
知道最大子段和的做法。
然后枚举矩阵的上下界,按照最大子段和的做法做。
 
 
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
int n, arr[N][N];
int sum[N][N], p[N]; inline void Input()
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++) scanf("%d", &arr[i][j]);
} inline int Work(int *arr)
{
int ret = arr[], cnt = arr[];
for(int i = ; i <= n; i++)
{
if(cnt < ) cnt = arr[i];
else cnt += arr[i];
ret = max(ret, cnt);
}
return ret;
} inline void Solve()
{
int ans = -INF;
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
sum[i][j] = sum[i - ][j] + arr[i][j];
for(int i = ; i <= n; i++)
for(int j = i; j <= n; j++)
{
for(int k = ; k <= n; k++)
p[k] = sum[j][k] - sum[i - ][k];
int cnt = Work(p);
/*if(ans < cnt)
{
ans = cnt;
printf("%d %d %d\n", ans, i, j);
}*/
ans = max(ans, cnt);
} cout << ans << endl;
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

ural 1146. Maximum Sum的更多相关文章

  1. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  2. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

  3. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  4. URAL 1146 Maximum Sum(DP)

    Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...

  5. URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)

    点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...

  6. URAL 1146 Maximum Sum 最大子矩阵和

    题目:click here #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; con ...

  7. Timus 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  8. POJ2479 Maximum sum[DP|最大子段和]

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Des ...

  9. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

随机推荐

  1. Linux(Ubuntu)之设定开机自启动

    分两种:对自建脚本,对已安装服务 对已安装服务, 只需在/etc/rc.local中添加相应的启动命令,如: 重启后,即可自启动服务. 对自建脚本自启动: 创建脚本test,修改其权限为755,如: ...

  2. printf 与c的基本类型之间的关系

    型 書式 注意事項 ssize_t %zd size_t %zu intmax_t %jd uintmax_t %ju ptrdiff_t %t signed char %hhd unsigned c ...

  3. 项目配置laungchImage

  4. android DisplayMetrics 获取屏幕分辨率

    Android 提供DisplayMetircs 类可以很方便的获取分辨率.下面介绍 DisplayMetics 类: Andorid.util 包下的DisplayMetrics 类提供了一种关于显 ...

  5. ListView介绍

    原文:http://blog.csdn.net/qingye_love/article/details/13772391?utm_source=tuicool&utm_medium=refer ...

  6. ACM训练计划建议(写给本校acmer,欢迎围观和指正)

    ACM训练计划建议 From:freecode#  Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...

  7. freemarker 实现对URL的安全编码

    [#setting url_escaping_charset='utf-8'] ${yourstr?url}

  8. Oracle 10g Block Change Tracking特性

    Using Block Change Tracking to Improve Incremental Backup Performance 使用块改变跟踪改善增量备份的性能 The block cha ...

  9. ORA-1461 encountered when generating server alert SMG-3500

    Doc ID 461911.1 Patch 6602742 Applies to: Oracle Database - Enterprise Edition - Version 10.2.0.3 an ...

  10. Windows Live Writer技巧

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:今天的内容虽然和开发技术无关,却应该和喜欢写东西的技术人员有关,比如我所有的文章都是用 ...