最大子矩阵和
来源:互联网 发布:大麦盒子直播软件 编辑:程序博客网 时间:2024/04/30 05:01
Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 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.<br><br>As an example, the maximal sub-rectangle of the array:<br><br>0 -2 -7 0<br>9 2 -6 2<br>-4 1 -4 1<br>-1 8 0 -2<br><br>is in the lower left corner:<br><br>9 2<br>-4 1<br>-1 8<br><br>and has a sum of 15.<br>
Input
The input consists of an N x 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].<br>
Output
Output the sum of the maximal sub-rectangle.<br>
Sample Input
40 -2 -7 0 9 2 -6 2-4 1 -4 1 -18 0 -2
Sample Output
15
问题分析:(因为没有多组输入 WA。。。)
每次枚举子矩阵的最上行u和最下行d,再把这个子矩阵每一列的值相加(压缩成一行),压缩成一个一维数组,对于这个数组求其最大连续字段和,这样就相当于把把所有最上的行为u并且最下的行为d的最大子矩阵和求出来了。
AC代码:
#include <bits/stdc++.h>#define N 135using namespace std;int fun(int b[N],int n){ int i,max,c; c=0,max=-100000; for(i=1;i<=n;i++) { if(c>0) c=c+b[i]; else c=b[i]; if(max<c) max=c; } return max;}int main(){ ios::sync_with_stdio(false); int i,j,n,max,sum,k; int a[N][N],b[N]; while(cin>>n) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) cin>>a[i][j]; max=-100000; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) b[j]=0; for(j=i;j<=n;j++) { for(k=1;k<=n;k++) b[k]+=a[j][k]; sum=fun(b,n); if(max<sum) max=sum; } } cout<<max<<endl; } return 0;}
阅读全文
0 0
- 最大子矩阵和
- 最大子矩阵和
- 最大子矩阵和
- 最大子矩阵和
- 最大子矩阵和
- 最大子矩阵和
- 最大子矩阵和
- 最大子矩阵和
- 最大子矩阵和
- 最大矩阵子和
- 最大子矩阵和
- 最大子矩阵和
- 最大和子矩阵
- 最大和子矩阵
- 最大子矩阵和
- 最大和子矩阵
- 最大子矩阵和
- 最大子矩阵和
- Prototype功能预览三:没有价值的MyBatis/iBatis
- HTML基础之事件
- hdu2023 求平均成绩 (C语言)
- Java_基础—递归
- scrapy shell的settings user_agent问题
- 最大子矩阵和
- java 方法 methodA(int ... ids)
- 机器学习实战学习_____(2)决策树
- nyoj 236 心急的C小加
- 模拟(Joint Stacks,HDU 5818)
- QT-单例程序 重新启动
- linux下nginx和tomcat负载均衡配置
- 深度网络模型压缩DEEP COMPRESSION
- linux 根据 进程名 获取 对应 PID 来 判断 使用 内存情况