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 ...
随机推荐
- JS动态获取 Url 参数
此操作主要用于动态 ajax 请求 1.首先封装一个函数 GetRequest(),能动态获取到 url 问号"?"后的所有参数 , function GetRequest() { ...
- memcached 和 redis 性能测试比对
网上很多关于memcached 和 redis 区别的介绍,大部分都是说redis比memcached支持的数据类型多的话题,而性能比对确很少,我专门针对两者进行了性能测试比对. 测试内容如下: 两者 ...
- Paper代写:别让段落结尾拉低你的分数
为了达到paper写作格式和字数要求,学生往往会在段末做一件事:总结.都不算是一个很长的段落.本来就写不了多少论证的内容,我们还强制加一个总结句,不仅占用了我们论证的篇幅,而且显得多余(段首的主题句已 ...
- 【Unity】双击物体
using UnityEngine; using System.Collections; using System; public class Click_yushe : MonoBehaviour ...
- Echarts学习宝典
1,可以使用百度图说中的图表代码复制到option中,还可在百度图说中以可视化的方式编辑数据,调整参数和编辑样式.(也可借助百度图说更改部分样式) 2,借助网站 https://echarts.bai ...
- ubuntu安装discourse论坛----结合在apache服务上建立虚拟主机
指导操作:https://github.com/discourse/discourse/blob/master/docs/INSTALL-cloud.md 一.先安装 Docker / Git: wg ...
- MQTT v5 (MQTT 5.0) 新特性介绍
https://blog.csdn.net/mrpre/article/details/87267400 背景 MQTT v3.1.1 作为一个经典的版本,一般能够满足大部分需求:为了避免落后,我们也 ...
- CC26XX开发
[CC26XX开发] 2016-01-26 [CC2650 入门] CC2650 sensortag入门 http://processors.wiki.ti.com/index.php/CC2650_ ...
- 洛谷 P2031 脑力达人之分割字串
题目传送门 解题思路: f[i]表示到第i位可获得的最大分割次数,对于每个f[i]都可由其符合条件的前缀转移过来,条件就是当前串除了前缀的剩余字符里有所给单词,然后一看,这不是在剩余字符里找有没有所给 ...
- redis cluster 添加/删除节点操作
RedisCluster 添加/删除节点 添加节点新配置两个测试节点8008和9009 [root@--- ~]# /usr/local/redis-/bin/redis-server /u02/re ...