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问题善于把复杂问题简单化,过程异途同归;
代码:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX(x,y)(x>y?x:y)
  4. const int INF=-0x3f3f3f3f;
  5. const int MAXN=;
  6. int ans,N;
  7. void maxline(int *a){
  8. int sum=;
  9. for(int i=;i<=N;i++){
  10. // printf("%d ",a[i]);
  11. if(sum>)sum+=a[i];//保证sum+a[i]>a[i];
  12. else sum=a[i];
  13. // printf("%d\n",sum);
  14. ans=MAX(ans,sum);
  15. }
  16. }
  17. int main(){
  18. int s[MAXN],dp[MAXN],map[MAXN][MAXN];
  19. while(~scanf("%d",&N)){
  20. int temp;
  21. ans=INF;
  22. for(int i=;i<=N;i++)
  23. for(int j=;j<=N;j++)
  24. scanf("%d",&map[i][j]);
  25. for(int i=;i<=N;i++){
  26. for(int j=i;j<=N;j++){
  27. if(j-i)for(int k=;k<=N;k++)
  28. map[i][k]+=map[j][k];
  29. maxline(map[i]);//这里是i代表每列从i行到j行的元素和
  30. }
  31. }
  32. printf("%d\n",ans);
  33. }
  34. return ;
  35. }

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. ArrayList与LinkedList时间复杂度之对比

    package ArrayList; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections ...

  2. 考察printf函数返回值

    最近偶然间见了这样一道题:  #include<stdio.h> int main() { ; printf("%d\n",printf("%d", ...

  3. DLR、ASTER GDEM、SRTM3、GMTED2010等5种全球高程数据对比

      目前网上有多种全球高程数据,国内在这方面也进步喜人,资源三号目前已放出来的卫片给了不少惊喜,其高程数值得期待.但资源三号的高程数据放出来还需等待,过去两三年全球高程数据发展相当迅猛,DLR放出了相 ...

  4. 製程能力介紹(SPC introduction) ─ 製程能力改善及評估

    如何改善製程能力 參考下面常態分配圖,原製程能力不足,其製成品有一定比率超出下限規格,其改善對策有二: 縮小製程變異,也就是改善Cp,提高Cp的值. 移動製程中心,也就是改善Ck,減小Ck的值. 就技 ...

  5. poj 3273 Monthly Expense (二分搜索,最小化最大值)

    题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...

  6. CodeForces 135C C. Zero-One

    题目 题意: 一个01串,AB两个人轮流删去一个字符,直到只剩两个,A先手.最后剩的两位组成一个二进制数,A要使其最小,B要使其最大. 有一些部分不知道原来是什么,用?表示,求所有的可能里,最后剩下的 ...

  7. netbeans字体与颜色配置模板相关网站

    NetBeans Themes -Color Schemes of the NetBeans IDE NetBeans ThemeBuilder

  8. freemaker

    FreeMarker模板文件主要由如下4个部分组成:  1,文本:直接输出的部分  2,注释:<#-- ... -->格式部分,不会输出  3,插值:即${...}或#{...}格式的部分 ...

  9. GWT RPC机制

    GWT RPC GWT RPCRemote Procedure Calls GWT: Google Web Toolkit的缩写,有了 GWT可以使用 Java 编程语言编写 AJAX 前端,然后 G ...

  10. USACO Section 4.3 Buy low,Buy lower(LIS)

    第一眼看到题目,感觉水水的,不就是最长下降子序列嘛!然后写……就呵呵了..要判重,还要高精度……判重我是在计算中加入各种判断.这道题比看上去麻烦一点,但其实还好吧.. #include<cstd ...