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 多行字符串

    javascript 字符串多行 平时一般使用 字符串+,或者[].join('')的方式 同事推荐了这样的形式 ExceptionDivHtml="<div class='gameI ...

  2. Flink资料(6) -- 如何添加一个新的Operator

    false false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {mso-style-n ...

  3. NET Core 的 Views

    NET Core 十种方式扩展你的 Views 原文地址:http://asp.net-hacker.rocks/2016/02/18/extending-razor-views.html作者:Jür ...

  4. 製程能力介紹(SPC introduction) ─ Cp之製程能力解釋

    Cp之製程能力解釋 從常態分配的特性來看,在群體中 ±3σ(標準差) 之範圍內的值,應包含群體全部的 99.73%.也就是說,若以 6σ為單位,就可以代表整個分布的範圍,但是有 0.27% (2700 ...

  5. Adobe Acrobat 9 Pro 简体中文正式版(免激活)

    软件语言:简体中文版本性质:官方正式版(免激活,非破解) Mac & PC [SN]: 1118-4018-6583-4956-2486-7805 修改 Abcpy.ini 可实现免序列号免激 ...

  6. Xamarin生成的APK大小分析

    原文:Xamarin生成的APK大小分析 刚接触Xamarin都会被Xamarin的售价吓一跳,另外就是它生成的APK大小,官方也有相关的说明,这里加上自己的理解同意讲解下: 以下是针对Android ...

  7. WPF:构建应用程序

    原文 http://www.cnblogs.com/free722/archive/2011/11/13/2247455.html WPF相关的项目内容包含在App.Xaml和Window1.xaml ...

  8. Android 检测SD卡应用

    Android 检测SD卡应用 //                                    Environment.MEDIA_MOUNTED // sd卡在手机上正常使用状态  // ...

  9. 项目代码摘抄,dot的用法之1

    function searchTags() { var list = $('#tags-list-select option:selected').val(); console.log(list); ...

  10. VB6关于判断模态窗体的问题

    模态窗体也有人叫模式窗体,是否为模态窗体由Show方法的参数决定: 语法 object.Show style, ownerform Show 方法的语法包含下列部分: 部分 描述 object 可选的 ...