[POJ1050]To the Max

试题描述

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*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.

输入

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 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 the sum of the maximal sub-rectangle.

输入示例

 - -    -
- - - -

输出示例


数据规模及约定

见“输入

题解

预处理前缀和,然后 O(n4) 大暴力。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 110
int n, S[maxn][maxn]; int main() {
n = read();
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) S[i][j] = S[i-1][j] + S[i][j-1] - S[i-1][j-1] + read(); int ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
for(int x = i; x <= n; x++)
for(int y = j; y <= n; y++) {
ans = max(ans, S[x][y] - S[i-1][y] - S[x][j-1] + S[i-1][j-1]);
} printf("%d\n", ans); return 0;
}

[POJ1050]To the Max的更多相关文章

  1. [POJ1050] To the Max 及最大子段和与最大矩阵和的求解方法

    最大子段和 Ο(n) 的时间求出价值最大的子段 #include<cstdio> #include<iostream> using namespace std; int n,m ...

  2. [POJ1050]To the Max (矩阵,最大连续子序列和)

    数据弱,暴力过 题意 N^N的矩阵,求最大子矩阵和 思路 悬线?不需要.暴力+前缀和过 代码 //poj1050 //n^4暴力 #include<algorithm> #include& ...

  3. POJ1050 To the Max 最大子矩阵

    POJ1050 给定一个矩阵,求和最大的子矩阵. 将每一列的值进行累加,枚举起始行和结束行,然后就可以线性优化了 复杂度O(n^3) #include<cstdio> #include&l ...

  4. [POJ1050]To the Max(最大子矩阵,DP)

    题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...

  5. (线性dp 最大子段和 最大子矩阵和)POJ1050 To the Max

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54338   Accepted: 28752 Desc ...

  6. poj1050 To the Max(降维dp)

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49351   Accepted: 26142 Desc ...

  7. [POJ1050]To the Max(最大子段和)

    题目链接 http://poj.org/problem?id=1050 题意 求最大子矩阵和. 题解 即求二维的最大子段和.二维数组sumRec[I][j]存储原始数组数据rec[0][j] to r ...

  8. POJ1723,1050,HDU4864题解(贪心)

    POJ1723 Soldiers 思维题. 考虑y坐标,简单的货舱选址问题,选择中位数即可. 再考虑x坐标,由于直接研究布置方法非常困难,可以倒着想:不管如何移动,最后的坐标总是相邻的,且根据贪心的思 ...

  9. 【poj1050】 To the Max

    http://poj.org/problem?id=1050 (题目链接) 题意 求二维最大子矩阵 Solution 数据好像很水,N最大才100,N^4大暴力都可以随便水过. 其实有N^3的做法.枚 ...

随机推荐

  1. express的session函数

    key:这个表示session返回来的cookie的键值, 我们整理一下哈: 这个是我们没有清缓存然后刷新了一下哈,对比的结果,发现session保存的数据中,只是expires这个改变了 { &qu ...

  2. css自定义字体

    @font-face { font-family: 华文隶书; src: url( ../font/STLITI.eot ); /* IE */ src: url( ../font/STLITI.tt ...

  3. WPF中RadioButton的分组

    当界面上出现多组Radiobutton时,将所有的Radiobutton写在同一个Grid里面,导致系统认为所有的Radiobutton是同一组,造成选择混乱,解决的方法: 1.要为属于同个组的Rad ...

  4. ADHelper C#域用户操作(转)

    using System; using System.Collections.Generic; using System.DirectoryServices; using System.Linq; u ...

  5. C基础--C语言的数组

    数组的定义: 一.正确写法: 1.int ages[5]--定义了一个5个长度的int类型的数组 2.int ages[]={1,2,3,4,5};--定义了一个5个长度的int类型的数组,并且初始化 ...

  6. Xunit

    Attributes Note: This table was written back when xUnit.net 1.0 has shipped, and needs to be updated ...

  7. 泛——复习js高级第三版

    1:本地存储的几种方法: (1)cookie: (2)localStorage //园子的自动保存就用了本地存储 (3)sessionStorage (4)globalStorage (5)index ...

  8. xml_MathML的基本知识点__这东西要自己实践最好

    1 : <mi> 一般的字符串 2: <mo> 操作字符串 <mo> ( </mo> <mo>∑</mo> 3:<mn&g ...

  9. hdu3487 伸展树(区间搬移 区间旋转)

    对于区间旋转使用lazy思想就能解决.然后对于区间搬移,先把a-1结点做根,b+1作为它的右孩子,这样ch[ch[root][1]][0]就是区间[a,b],现将他取出. 然后在将当前的树伸展,把c结 ...

  10. session共享

    Nginx或者Squit反向代理到两台tomcat服务器 tomcat使用memcached tomcat连接memcached工具 cp session/*.jar /usr/local/tomca ...