POJ 1050:To the Max
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 43241 | Accepted: 22934 |
Description
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
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
题意是给定一个矩阵,求其子矩阵的最大和。
这题也是弄得相当郁闷,一开始暴力,结果预料之中的TLE。然后试了一下dp,结果还MLE。。。郁闷得不行。
然后看了别人的思路,发现可以二维变一维,想了想忽然恍然大悟。
将每一列的加起来,就是一维了。枚举不同行即可。之前怎么做的这次怎么求。
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#pragma warning(disable:4996)
using namespace std; int value[250][250];
int value2[250];
int dp[250]; int main()
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout); int N,i,j,h,k,g,f;
int ans=-100;
scanf("%d",&N); memset(dp,0,sizeof(dp));
memset(value2,0,sizeof(value2)); for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
{
scanf("%d",&value[i][j]);
ans=max(ans,value[i][j]);
}
} for(i=1;i<=N;i++)
{
for(h=i;h<=N;h++)
{
for(k=1;k<=N;k++)
{
value2[k] += value[h][k];
dp[k] = max(dp[k-1]+value2[k],value2[k]);
ans = max(ans,dp[k]);
}
memset(dp,0,sizeof(dp));
}
memset(value2,0,sizeof(value2));
} cout<<ans<<endl;
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1050:To the Max的更多相关文章
- (POJ - 1050)To the Max 最大连续子矩阵和
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- poj - 1050 - To the Max(dp)
题意:一个N * N的矩阵,求子矩阵的最大和(N <= 100, -127 <= 矩阵元素 <= 127). 题目链接:http://poj.org/problem?id=1050 ...
- poj 1050 To the Max(最大子矩阵之和)
http://poj.org/problem?id=1050 我们已经知道求最大子段和的dp算法 参考here 也可参考编程之美有关最大子矩阵和部分. 然后将这个扩大到二维就是这道题.顺便说一下,有 ...
- POJ 1050 To the Max 最详细的解题报告
题目来源:To the Max 题目大意:给定一个N*N的矩阵,求该矩阵中的某一个矩形,该矩形内各元素之和最大,即最大子矩阵问题. 解题方法:最大子序列之和的扩展 解题步骤: 1.定义一个N*N的矩阵 ...
- 九度oj 题目1050:完数
题目1050:完数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8778 解决:3612 题目描述: 求1-n内的完数,所谓的完数是这样的数,它的所有因子相加等于它自身,比如6有3个因子 ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- 页面上有3个输入框:分别为max,min,num;三个按钮:分别为生成,排序,去重;在输入框输入三个数字后,先点击生成按钮,生成一个数组长度为num,值为max到min之间的随机整数点击排序,对当前数组进行排序,点击去重,对当前数组进行去重。 每次点击之后使结果显示在控制台
<!DOCTYPE html> <html> <head> <!-- 页面上有3个输入框:分别为max,min,num:三个按钮:分别为生成,排序,去重: 在 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 1050 To the Max -- 动态规划
题目地址:http://poj.org/problem?id=1050 Description Given a two-dimensional array of positive and negati ...
随机推荐
- IDEA中如何部署tomcat
1.添加tomcat 2.添加tomcat所依赖的war包 test009.war包刚开始可能不存在,这个时候需要将maven项目进行打包,然后点击“+”之后就会出现一个和项目名同名的war包,选中就 ...
- Day1-Luogu-2085
题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Ai*x^2+Bi*x+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个 ...
- poj1861 network(并查集+kruskal最小生成树
题目地址:http://poj.org/problem?id=1861 题意:输入点数n和边数n,m组边(点a,点b,a到b的权值).要求单条边权值的最大值最小,其他无所谓(所以多解:(.输出单条边最 ...
- Wincc V7.3SE安装截图
打开某个工程出错,能力所限,找不到问题,没能解决
- APP自动化测试获取包名的两种方法
获取包名的两种方法: 一.通过aapt获取 1.进入aapt.exe所在路径 2.在地址栏输入cmd回车,打开dos命令窗口. 3.在命令窗口输入 aapt dump badging 拖入apk 回车 ...
- java 牌型种数
牌型种数 小明被劫持到X赌城,被迫与其他3人玩牌. 一副扑克牌(去掉大小王牌,共52张),均匀发给4个人,每个人13张. 这时,小明脑子里突然冒出一个问题: 如果不考虑花色,只考虑点数,也不考虑自己得 ...
- MIT课程
8.02 Physics II (电磁学基础) Introduction to electromagnetism and electrostatics: electric charge, Coulo ...
- Ethernet IP TCP UDP 协议头部格式
The Ethernet header structure is shown in the illustration below: 以太网头部14 bytes Destination Source L ...
- day02-Python运维开发基础
1. Number 数据类型 2. 容器数据类型-字符串 """ 语法: "字符串" % (值1,值2 ... ) 占位符: %d 整型占位符 %f ...
- elasticsearch kibana logstash(ELK)的安装集成应用
官网关于kibana的学习指导网址是:https://www.elastic.co/guide/en/kibana/current/index.html Kibana是一个开源的分析和可视化平台,设计 ...