1146. Maximum Sum

Time limit: 0.5 second
Memory limit: 64 MB
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. 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. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.
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-hand corner and has the sum of 15.

Input

The input consists of an N × N array of integers. The input begins with a single positive integerN on a line by itself indicating the size of the square two dimensional array. This is followed byN 2 integers separated by white-space (newlines and spaces). These N 2 integers make up the array in row-major order (i.e., all numbers on the first row, left-to-right, then all numbers on 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

The output is the sum of the maximal sub-rectangle.

Sample

input output
  1. 4
  2. 0 -2 -7 0
  3. 9 2 -6 2
  4. -4 1 -4 1
  5. -1 8 0 -2
  1. 15

最大子矩阵。很经典的问题哈哈

压缩 然后最大连续子序列  dp[i]=dp[i-1]<0?a[i]:dp[i-1]+a[i]

一开始压缩的时候没用前缀和,n^4 貌似过不了,后来用前缀和优化到n^3

下面代码中dp 的空间也可以优化,这里没有优化.

  1. /* ***********************************************
  2. Author :guanjun
  3. Created Time :2016/10/7 13:50:13
  4. File Name :timus1146.cpp
  5. ************************************************ */
  6. #include <bits/stdc++.h>
  7. #define ull unsigned long long
  8. #define ll long long
  9. #define mod 90001
  10. #define INF 0x3f3f3f3f
  11. #define maxn 10010
  12. #define cle(a) memset(a,0,sizeof(a))
  13. const ull inf = 1LL << ;
  14. const double eps=1e-;
  15. using namespace std;
  16. priority_queue<int,vector<int>,greater<int> >pq;
  17. struct Node{
  18. int x,y;
  19. };
  20. struct cmp{
  21. bool operator()(Node a,Node b){
  22. if(a.x==b.x) return a.y> b.y;
  23. return a.x>b.x;
  24. }
  25. };
  26.  
  27. bool cmp(int a,int b){
  28. return a>b;
  29. }
  30. int a[][],n;
  31. int sum[][];
  32. int dp[];
  33. int main()
  34. {
  35. #ifndef ONLINE_JUDGE
  36. //freopen("in.txt","r",stdin);
  37. #endif
  38. //freopen("out.txt","w",stdout);
  39. while(scanf("%d",&n)!=EOF){
  40. cle(sum);
  41. for(int i=;i<=n;i++){
  42. for(int j=;j<=n;j++){
  43. scanf("%d",&a[i][j]);
  44. sum[i][j]=sum[i][j-]+a[i][j];
  45. }
  46. }
  47. int Max=-INF;
  48. //dp 求最大连续子序列 dp[i]代表以i为结尾的最大连续子序列的长度
  49. for(int i=;i<=n;i++){
  50. for(int j=;j<=i;j++){
  51. cle(dp);
  52. for(int k=;k<=n;k++){
  53. int tmp=sum[k][i]-sum[k][j-];
  54. if(dp[k-]<){
  55. dp[k]=tmp;
  56. }
  57. else dp[k]=tmp+dp[k-];
  58. Max=max(dp[k],Max);
  59. }
  60. }
  61. }
  62. cout<<Max<<endl;
  63. }
  64. return ;
  65. }

Timus 1146. Maximum Sum的更多相关文章

  1. ural 1146. Maximum Sum

    1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...

  2. 最大子矩阵和 URAL 1146 Maximum Sum

    题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...

  3. ural 1146. Maximum Sum(动态规划)

    1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...

  4. URAL 1146 Maximum Sum(最大子矩阵的和 DP)

    Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...

  5. URAL 1146 Maximum Sum(DP)

    Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...

  6. URAL 1146 Maximum Sum & HDU 1081 To The Max (DP)

    点我看题目 题意 : 给你一个n*n的矩阵,让你找一个子矩阵要求和最大. 思路 : 这个题都看了好多天了,一直不会做,今天娅楠美女给讲了,要转化成一维的,也就是说每一列存的是前几列的和,也就是说 0 ...

  7. URAL 1146 Maximum Sum 最大子矩阵和

    题目:click here #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; con ...

  8. POJ2479 Maximum sum[DP|最大子段和]

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39599   Accepted: 12370 Des ...

  9. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

随机推荐

  1. arx刷新图形界面

    actrTransactionManager->flushGraphics(); acedUpdateDisplay();

  2. vi 命令学习(二)

    [选中文本(可视模式)] v 可视模式 从光标位置开始按正常模式选择文本 V 可视行模式 选中光标经过的完整行 ctrl + v 可视块模式 垂直方向选中文本 [ 撤销和恢复撤销] u undo 撤销 ...

  3. Python爬虫:抓取手机APP的数据

    摘要 大多数APP里面返回的是json格式数据,或者一堆加密过的数据 .这里以超级课程表APP为例,抓取超级课程表里用户发的话题. 1.抓取APP数据包 表单: 表单中包括了用户名和密码,当然都是加密 ...

  4. HDU多校Round 3

    Solved:4 rank:268 C. Dynamic Graph Matching  状压DP一下 #include <stdio.h> #include <algorithm& ...

  5. Python isalpha() 方法检测字符串是否只由字母组成。

    Python isalpha() 方法检测字符串是否只由字母组成.

  6. eBPF监控工具bcc系列五工具funccount

    eBPF监控工具bcc系列五工具funccount funccount函数可以通过匹配来跟踪函数,tracepoints 或USDT探针.例如所有以vfs_ 开头的内核函数. ./funccount ...

  7. Stuts2学习——HelloWorld

    这两天从对Struts一窍不通到成功运行HelloWorld,在SSH这条路上迈出了第一步. 下面我把我的第一个Struts程序放上来: 一.新建web project,配置文件等准备工作 1. 新建 ...

  8. 队列的头函数使用C++

    queue queue模板类的定义在<queue>头文件中. 与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的 ...

  9. Python函数: any()和all()的用法

    版权声明:本文为博主原创文章,未经允许不得转载 引子 平常的文本处理工作中,我经常会遇到这么一种情况:用python判断一个string是否包含一个list里的元素. 这时候使用python的内置函数 ...

  10. Uva 10730 Antiarithmetic?

    uva 10730 题意:给出一列数字,如果其中存在长度大于等于3的等差数列,输出no,不存在就输出yes 这道题标定了数列长度n,而且这n个数数据范围是从0到n-1,没有相同的数,这就给我们枚举提供 ...