当前位置:网站首页>7-5 最大子矩阵和问题
7-5 最大子矩阵和问题
2022-06-24 19:43:00 【白—】
7-5 最大子矩阵和问题
最大子矩阵和问题。给定m行n列的整数矩阵A,求矩阵A的一个子矩阵,使其元素之和最大。
输入格式:
第一行输入矩阵行数m和列数n(1≤m≤100,1≤n≤100),再依次输入m×n个整数。
输出格式:
输出第一行为最大子矩阵各元素之和,第二行为子矩阵在整个矩阵中行序号范围与列序号范围。
输入样例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
输出样例1:
输出第一行321表示子矩阵各元素之和,输出第二行2 4 1 6表示子矩阵的行序号从2到4,列序号从1到6
321
2 4 1 6
代码:
#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;//记录每个[i,j]矩阵的总和
}
int x1,x2,y1,y2;
for(int i=1;i<=m;i++)//一层循环,用于控制子矩阵的下边界
for(int j=1;j<=i;j++){
//二层循环,用于控制子矩阵的上边界
temp=0;
int yy=1;
for(int k=1;k<=n;k++){
//三层循环用于控制子矩阵的右边界
temp+=(dp[i][k]-dp[j-1][k]);
if(temp>ans)//如果值大于最大值则更新
{
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三
边栏推荐
- Simpledateformat concrete classes for formatting and parsing dates
- R语言dplyr包group_by函数和summarise_at函数计算dataframe计算不同分组的计数个数和均值(Summarise Data by Categorical Variable)
- golang convert map to json string
- F29oc analysis
- Websocket learning
- Idea creation module prompt already exists
- Case analysis: using "measurement" to improve enterprise R & D efficiency | ones talk
- Main cause of EMI - mold current
- [introduction to UVM== > episode_8] ~ sequence and sequencer, sequence hierarchy
- idea创建模块提示已存在
猜你喜欢

Learn about redlock

Idea creation module prompt already exists
![[JS] - [string - application] - learning notes](/img/dc/f35979b094f04c0ee13b3354c7741d.png)
[JS] - [string - application] - learning notes

第六章 网络学习相关技巧5(超参数验证)

Latest development of jetpack compose

Tech talk activity review kubernetes skills of cloud native Devops
15 lines of code using mathematical formulas in wangeditor V5

【js】-【链表-应用】-学习笔记

22map introduction and API

07_ Springboot for restful style
随机推荐
#22Map介绍与API
记录一下MySql update会锁定哪些范围的数据
[basic knowledge] ~ half adder & full adder
Financial management [4]
Collation of Digital IC design experience (II)
Laravel pagoda security configuration
Docker-mysql8-master-slave
R language uses GLM function to build Poisson log linear regression model, processes three-dimensional contingency table data to build saturation model, uses summary function to obtain model summary s
docker-mysql8-主从
数字IC设计经验整理(二)
Accounting standards for business enterprises application [5]
jar中没有主清单属性
Dig deep into MySQL - resolve the clustered index / secondary index / federated index of InnoDB storage engine
R语言使用MatchIt包进行倾向性匹配分析、使用match.data函数构建匹配后的样本集合、通过双样本t检验分析(双独立样本t检验)来判断倾向性评分匹配后样本中的所有协变量的平衡情况
Blogs personal blog test point (manual test)
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
03_ Spingboot core profile
Selection (026) - what is the output of the following code?
Idea creation module prompt already exists
Dig deep into MySQL - resolve the non clustered index of MyISAM storage engine