当前位置:网站首页>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
边栏推荐
- R language uses the aggregate function of epidisplay package to split numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and customi
- Pseudo original intelligent rewriting API Baidu - good collection
- 379. hide and seek
- 7-6 铺设油井管道
- Actipro WPF Controls 2022.1.2
- [introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy
- No main manifest attribute in jar
- golang map clear
- Harmonyos accessing database instances (3) -- use ORM bee to test how good harmonyos is
- golang convert map to json string
猜你喜欢
22map introduction and API
Idea creation module prompt already exists
Huawei machine learning service speech recognition function enables applications to paint "sound" and color
华为机器学习服务语音识别功能,让应用绘“声”绘色
Mousse shares listed on Shenzhen Stock Exchange: becoming popular by mattress and "foreign old man", with a market value of 22.4 billion yuan
Chapter VI skills related to e-learning 5 (super parameter verification)
#22Map介绍与API
Still using simpledateformat for time formatting? Be careful of project collapse
Quickly build KVM virtual machine on # yyds dry goods inventory # physical machine
From client to server
随机推荐
Morris遍历
R language uses the aggregate function of epidisplay package to split numerical variables into different subsets based on factor variables, calculate the summary statistics of each subset, and customi
MySQL semi sync replication
golang convert map to json string
websocket学习
golang convert json string to map
SQL -convert function
Spark's wide dependence and narrow dependence yyds dry goods inventory
Installation and deployment of ganglia
[JS] - [array, stack, queue, linked list basics] - Notes
監聽 Markdown 文件並熱更新 Next.js 頁面
SimpleDateFormat 格式化和解析日期的具体类
Notes for laravel model
Laravel add helper file
378. 骑士放置
From client to server
Dig deep into MySQL - resolve the difference between clustered and non clustered indexes
R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用summary函数获取模型汇总统计信息、解读模型系数交互作用及其显著性
Binary lookup array subscript
Huawei machine learning service speech recognition function enables applications to paint "sound" and color