To the Max

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 2
Problem Description
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.
 
Input
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
Output the sum of the maximal sub-rectangle.
 
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1

8 0 -2

 
Sample Output
15
题解:dp问题善于把复杂问题简单化,过程异途同归;
代码:
 #include<stdio.h>
#include<string.h>
#define MAX(x,y)(x>y?x:y)
const int INF=-0x3f3f3f3f;
const int MAXN=;
int ans,N;
void maxline(int *a){
int sum=;
for(int i=;i<=N;i++){
// printf("%d ",a[i]);
if(sum>)sum+=a[i];//保证sum+a[i]>a[i];
else sum=a[i];
// printf("%d\n",sum);
ans=MAX(ans,sum);
}
}
int main(){
int s[MAXN],dp[MAXN],map[MAXN][MAXN];
while(~scanf("%d",&N)){
int temp;
ans=INF;
for(int i=;i<=N;i++)
for(int j=;j<=N;j++)
scanf("%d",&map[i][j]);
for(int i=;i<=N;i++){
for(int j=i;j<=N;j++){
if(j-i)for(int k=;k<=N;k++)
map[i][k]+=map[j][k];
maxline(map[i]);//这里是i代表每列从i行到j行的元素和
}
}
printf("%d\n",ans);
}
return ;
}

To the Max(矩阵压缩)的更多相关文章

  1. C++实现矩阵压缩

    C++实现矩阵压缩 转置运算时一种最简单的矩阵运算.对于一个m*n的矩阵M,他的转置矩阵T是一个n*m的矩阵,且T(i,j) = M(j,i). 一个稀疏矩阵的转置矩阵仍然是稀疏矩阵. 矩阵转置 方案 ...

  2. [Swust OJ 589]--吃西瓜(三维矩阵压缩)

    题目链接:http://acm.swust.edu.cn/problem/589/ Time limit(ms): 2000 Memory limit(kb): 65535   Description ...

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

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

  4. Poj 3318 Matrix Multiplication( 矩阵压缩)

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18928   Accepted: ...

  5. 【矩阵压缩】 poj 1050

    题意:给一个矩阵,里面有正负数,求子矩阵和的最大值 #include <iostream> #include <cstdio> #include <stdlib.h> ...

  6. 矩阵压缩写法 scipy spark.ml.linalg里都有,CRS,CCS

    CRS 表示:Compressed Row Storage CCS 表示:Compressed Column Storage CRS的表示参考: https://blog.csdn.net/buptf ...

  7. HDU 1081 To The Max (dp)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  8. 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 ...

  9. HDU 1081 To The Max【dp,思维】

    HDU 1081 题意:给定二维矩阵,求数组的子矩阵的元素和最大是多少. 题解:这个相当于求最大连续子序列和的加强版,把一维变成了二维. 先看看一维怎么办的: int getsum() { ; int ...

随机推荐

  1. Javascript中的attribute和property分析

    attribute和property这两个单词,都有属性的意思,attribute有属性.特质的意思,property则有性质,性能的意思. 首先需要明确的是,在规范中,读取和设置attribute的 ...

  2. ListView的简单使用和性能优化

    起源:ListView是Android开发中使用最广泛的一种控件,它以垂直列表的形式显示所有列表项. 创建ListView有两种方式: ☆ 直接使用ListView进行创建. ☆让Activity继承 ...

  3. Some good questions

    (一)#include <stdio.h>#include <stdlib.h>void getmemory(char *p){ p=(char *) malloc(100); ...

  4. 限制Inactive物料录入BOM

    应用 Oracle Bill Of   Materiel 层 Level Function 函数名 Funcgtion Name CUX_BOMFDBOM 表单名 Form Name CUXBOMFD ...

  5. npm 常用命令

    npm install xxx 安装模块npm install xxx@1.1.1   安装1.1.1版本的xxxnpm install xxx -g 将模块安装到全局环境中.npm ls 查看安装的 ...

  6. bootstrap栅格系统的div高度怎样定?

    不能直接写px,不同设置分辨不同,div宽度始终是满屏的,如果高度指定像素大小,div就不能保证长宽比例恰当了. 解决方法: height:0px;padding-bottom:100%

  7. 反恐训练营(LCS)

    反恐训练营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  8. 简单的Session登录

    Login前台页面 <form id="form1" action ="" method="post"> <input t ...

  9. MATLAB一句总结

    MATLAB使用过程中的一些小总结: 1.sqrt函数的输入参数应为double类型: 2.im2bw把图像转换为二值图像: 3.double类型的图片必须转换为uint8类型后才能用imshow显示 ...

  10. 指定hive输出格式

    0.11版本以前: sed -e 's/\x01/|/g' file 0.11版本以后: insert overwrite local directory '/opt/aimcpro/libc/tes ...