To the Max(矩阵压缩)
2024-10-18 22:33:01
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.
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
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 ;
}
最新文章
- 对于System.exit(0)和System.exit(1)的一般理解
- jquery的bind跟on绑定事件的区别
- Java并发编程:Lock
- Guava学习笔记:简化异常处理的Throwables类
- 【caffe】无法找到gpu/mxGPUArray.h: No such file or directory
- zookeeper清除日志文件工具
- 如何用chrome插件使上网生活更美好-持续更新
- LYK 与实验室
- App应用与思考
- FileGeodatabase和PersonalGeodatabase与ArcSDEGeodatabase三种数据库比较.
- Java中关于OOM的场景及解决方法
- C# DateTime类,TimeSpan类
- 如何优化运行在webkit上的web app
- SNS团队Beta阶段第一次站立会议(2017.05.22)
- 区别samtools faid产生的.fai文件功能和bwa index 产生的四个文件的功能
- ubuntu16.04安装nvidia驱动及CUDA+cudnn
- Android-Java-类与对象的关系
- SQLServer 里面的 DDL,DML,DCL,TCL
- 学习Kali Linux必须知道的几点
- 【驱动】网卡驱动&#183;linux内核网络分层结构
热门文章
- javascript事件小结(事件处理程序方式)--javascript高级程序设计笔记
- jQuery on()方法绑定动态元素的点击事件无效
- objective -c こだわり
- c语言:从一组数据中选出可以组成三角形并且周长最长的三个数(简单)
- 面向对象程序设计-C++_课时16子类父类关系
- 截取字符串一之substring
- 单页web应用开发流程
- 茴香豆的第五种写法---设置ExpandableListView系统自带图标按下效果
- Android Afinal框架学习(二) FinalActivity 一个IOC框架
- 发现一个名为“Douyu”的国人项目