HDU 1081 To The Max - 最大字段和
题目大意:
求一个矩阵的最大子矩阵和。
题目分析:
刚开始考虑了一下dp方程的递推,但是不好转。简便的方法是预处理sum[i][j]表示第i行的前j个元素之和,之后\(n^3\)枚举子矩阵就可以了。
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 105;
int n, sum[N][N], a[N][N], mx;
typedef pair<int, int> P;
P dp[N][N];
int main(){
while(~scanf("%d", &n)){
mx = -99999999; memset(sum, 0, sizeof sum);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++){
scanf("%d", &a[i][j]);
sum[i][j] = sum[i][j - 1] + a[i][j];
}
// dp[1][1]
for(int i = 1; i <= n; i++)
for(int j = i; j <= n; j++){
int s = 0;
for(int k = 1; k <= n; k++){
if(s < 0) s = 0;
s += sum[k][j] - sum[k][i - 1];
if(s > mx) mx = s;
}
}
printf("%d\n", mx);
}
return 0;
}
HDU 1081 To The Max - 最大字段和的更多相关文章
- hdu 1081 To The Max(dp+化二维为一维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1081 To The Max Time Limit: 2000/1000 MS (Java/Others ...
- HDU 1081 To The Max【dp,思维】
HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...
- dp - 最大子矩阵和 - HDU 1081 To The Max
To The Max Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1081 Mean: 求N*N数字矩阵的最大子矩阵和. ana ...
- Hdu 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)
点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...
- HDU 1081 To The Max(动态规划)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- ACM HDU 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- hdu 1081 To The Max(二维压缩的最大连续序列)(最大矩阵和)
Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle ...
- HDU 1081 To The Max (dp)
题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
随机推荐
- css实现背景半透明文字不透明的效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Session丢失原因与解决方案
win2003 server下的IIS6默认设置下对每个运行在默认应用池中的工作者进程都会经过20多个小时后自动回收该进程, 造成保存在该进程中的session丢失. 因为Session,Appl ...
- UML学习总结(2)——StartUML 各种类图的例子
1.UML分为: 1)静态建模:系统基础和系统固定框架结构,这些图形往往是"静态"的. 类图(Class Diagram):常用来分析业务概念 用例图(Use Case Diagr ...
- python整除
1.'/'除号与c不同,单个'/'是浮点除,两个除号'//'才是整除
- swift开发多线程篇 - NSThread 线程相关简单说明(一些使用和注意点)
一 说明 本文涉及代码可以从https://github.com/HanGangAndHanMeimei/Code地址获得. 二 NSThread的基本使用和创建 1)基本用法(主线程|当前线程) 1 ...
- [转载]Surging Demo 项目之一
开发与运行环境 IDE Visual Stadio 2017/Visual Stadio 2019 Visual Stadio Core Docker 和 Docker-Compose 通过docke ...
- ftp 下载时防止从缓存中获取文件
//http://baike.baidu.com/link?url=QucJiA_Fg_-rJI9D4G4Z4687HG4CfhtmBUd5TlXrcWCeIEXCZxIh0TD7ng1wROAzAu ...
- [React] Create a Virtualized List with Auto Sizing Cells using react-virtualized and CellMeasurer
In this lesson we'll use CellMeasurer and CellMeasurerCache to automatically calculate and cache the ...
- ANDROID内存优化(大汇总——全)
写在最前: 本文的思路主要借鉴了2014年AnDevCon开发者大会的一个演讲PPT,加上把网上搜集的各种内存零散知识点进行汇总.挑选.简化后整理而成. 所以我将本文定义为一个工具类的文章,如果你在A ...
- PHP Filesystem 函数(文件系统函数)(每日一课的内容可以从php参考手册上面来)
PHP Filesystem 函数(文件系统函数)(每日一课的内容可以从php参考手册上面来) 一.总结 1.文件路径中的正反斜杠:当在 Unix 平台上规定路径时,正斜杠 (/) 用作目录分隔符.而 ...