当前位置:网站首页>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
边栏推荐
- websocket学习
- 【js】-【数组、栈、队列、链表基础】-笔记
- The R language uses the matchit package for propensity matching analysis and match The data function constructs the matched sample set, and performs Welch double sample t-test analysis and double inde
- Mousse shares listed on Shenzhen Stock Exchange: becoming popular by mattress and "foreign old man", with a market value of 22.4 billion yuan
- Idea creation module prompt already exists
- RT thread uses RT kprintf
- From client to server
- Simpledateformat concrete classes for formatting and parsing dates
- 斐波那契
- Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine
猜你喜欢

Yyds dry goods inventory tells us 16 common usage scenarios of redis at one go

Detailed explanation of online group chat and dating platform project (servlet implementation)

#22Map介绍与API

中学校园IP网络广播系统解决方案-校园数字IP广播系统方案设计指南
Unveiling the secrets of the Winter Olympics | smartbi's partners supported the "front and back" of the Beijing Winter Olympics

Ganglia 的安装与部署

Actipro WPF Controls 2022.1.2

InnoDB, the storage engine of MySQL Architecture Principle_ Redo log and binlog

Simpledateformat concrete classes for formatting and parsing dates

宁德时代定增450亿:高瓴认购30亿 曾毓群仍控制23%股权
随机推荐
Installing IBM CPLEX academic edition | CONDA installing CPLEX
7-7 求解众数问题
idea创建模块提示已存在
Écoutez le fichier markdown et mettez à jour Hot next. Page JS
372. 棋盘覆盖
Dig deep into MySQL - resolve the clustered index / secondary index / federated index of InnoDB storage engine
golang map clear
257. detention of offenders
The dplyr package select function of R language moves the specified data column in the dataframe data to the first column (the first column) in the dataframe data column
【js】-【数组应用】-学习笔记
Super detailed cookie addition, deletion, modification and query
[JS] - [array application] - learning notes
websocket学习
Laravel creates a service layer
Chapter VI skills related to e-learning 5 (super parameter verification)
Blogs personal blog test point (manual test)
中学校园IP网络广播系统解决方案-校园数字IP广播系统方案设计指南
7-2 求解买股票问题
Laravel user authorization
Online group chat and dating platform test point