当前位置:网站首页>7-5 maximal submatrix sum problem
7-5 maximal submatrix sum problem
2022-06-24 23:31:00 【White -】
7-5 Maximum submatrix sum problem
Maximum submatrix sum problem . Given m That's ok n The integer matrix of columns A, O matrix A A submatrix of , Maximize the sum of its elements .
Input format :
In the first row, enter the number of rows of the matrix m And number of columns n(1≤m≤100,1≤n≤100), And then type m×n It's an integer .
Output format :
The first row outputs the sum of the elements of the maximum submatrix , The second row is the row number range and column number range of the sub matrix in the whole matrix .
sample input 1:
5 6
60 3 -65 -92 32 -70
-41 14 -38 54 2 29
69 88 54 -77 -46 -49
97 -32 44 29 60 64
49 -48 -96 59 -52 25
sample output 1:
Output the first line 321 Represents the sum of the elements of a submatrix , Output the second line 2 4 1 6 Indicates that the row number of the submatrix is from 2 To 4, Column ordinal from 1 To 6
321
2 4 1 6
Code :
#include<stdio.h>
int dp[5050][5050];
int m,n,ans=-999999,temp,num;
int main(){
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++){
scanf("%d",&num);
dp[i][j]=dp[i-1][j]+num;// Record each [i,j] Sum of matrices
}
int x1,x2,y1,y2;
for(int i=1;i<=m;i++)// A cycle , Used to control the lower boundary of the submatrix
for(int j=1;j<=i;j++){
// A two-tier cycle , Used to control the upper boundary of submatrix
temp=0;
int yy=1;
for(int k=1;k<=n;k++){
// A three-level loop is used to control the right boundary of the submatrix
temp+=(dp[i][k]-dp[j-1][k]);
if(temp>ans)// If the value is greater than the maximum value, update
{
ans=temp;
x2=i;
y2=k;
x1=j;
y1=yy;
}
if(temp<0)
{
temp=0;
yy=k+1;
}
}
}
printf("%d\n",ans);
printf("%d %d %d %d",x1,x2,y1,y2);
}
202206222101 3、 ... and
边栏推荐
- 379. hide and seek
- R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、对匹配后的样本的不同分组对应的目标变量的均值进行Welch双样本t检验分析、双独立样本t检验
- Installation and deployment of ganglia
- Selective sort method
- Common regular expressions
- 慕思股份深交所上市:靠床垫和“洋老头”走红 市值224亿
- R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses exp function and coef function to obtain the corresponding odds rat
- [JS] - [linked list - application] - learning notes
- QT to place the form in the lower right corner of the desktop
- No main manifest attribute in jar
猜你喜欢

22map introduction and API

Jetpack Compose 最新进展

【js】-【数组应用】-学习笔记
![[basic knowledge] ~ half adder & full adder](/img/06/7f1ede65dca527c8630285b587a4ba.png)
[basic knowledge] ~ half adder & full adder

Mousse shares listed on Shenzhen Stock Exchange: becoming popular by mattress and "foreign old man", with a market value of 22.4 billion yuan

点的螺旋距离

选择类排序法

伪原创智能改写api百度-收录良好

Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine

Yyds dry goods counting uses xshell to implement agent function
随机推荐
Gocolly manual
[JS] - [tree] - learning notes
六大行数据治理现状盘点:治理架构、数据标准与数据中台(2022.04)
企业数据防泄露解决方案分享
[JS] - [stack, team - application] - learning notes
RT-thread使用rt-kprintf
Docker-mysql8-master-slave
Laravel user authorization
#22Map介绍与API
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用AIC函数比较两个模型的AIC值的差异(简单模型和复杂模型)
从客户端到服务器
QT to place the form in the lower right corner of the desktop
基于三维GIS开发的水电工程建设方案
7-8 梯云纵
【js】-【链表-应用】-学习笔记
376. 機器任務
[JS] - [array, stack, queue, linked list basics] - Notes
golang map clear
慕思股份深交所上市:靠床垫和“洋老头”走红 市值224亿
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses exp function and coef function to obtain the corresponding odds rat