当前位置:网站首页>Niuke network: maximum submatrix
Niuke network: maximum submatrix
2022-07-02 22:56:00 【lsgoose】

1. The prefix and
Use a prefix of violence and , This submatrix , We can use the coordinates of the upper left corner and the lower right corner to determine , When we read data , Use an array to store (1,1) It's the upper left corner ,(i,j) Is the sum of the matrix in the lower right corner .
sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+a[i][j]
Then we fix the upper left corner (x,y), Traverse all the lower right corners (i,j)( Including the upper left corner itself ), Then the matrix sum of the two can be solved in this way :
sum = sum[i][j]-sum[x-1][j]-sum[i][y-1]+sum[x-1][y-1]
#include<bits/stdc++.h>
using namespace std;
const int N=101;
int sum[N][N];
int n;
void get(int x, int y, int &res){
for(int i=x;i<=n;++i){
for(int j=y;j<=n;++j){
int t=sum[i][j]-sum[x-1][j]-sum[i][y-1]+sum[x-1][y-1];
if(t>res){
res=t;
}
}
}
}
int main(){
cin>>n;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
int t;
cin>>t;
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+t;
}
}
int res=0x80000001;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
get(i,j,res);
}
}
cout<<res<<endl;
}2. Dynamic programming && The prefix and
dp The idea is to find the prefix and of each column when reading the data .
Then fix two lines , Ergodic column . The process of traversing a column is a bit like finding the maximum continuous sum of a one-dimensional array . Select the maximum value in the traversal process .
The time complexity here is O(n^3), Compared with the one above O(n^4), Using prefix sum will reduce the complexity of finding the sum of matrices .
#include <bits/stdc++.h>
using namespace std;
const int N=510,inf=1e18;
typedef long long ll;
ll a[N][N],sum[N][N],dp[N];
int main()
{
ios::sync_with_stdio(false);
int n;cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
sum[i][j]=sum[i-1][j]+a[i][j]; // The first j Before column i Line prefix and
}
}
ll mx=-inf;
for(int k1=1;k1<=n;k1++) // That's ok Upper end point
{
for(int k2=k1;k2<=n;k2++) // That's ok Lower point
{
//dp[0]=0;
for(int i=1;i<=n;i++) // Column
{
ll tmp=sum[k2][i]-sum[k1-1][i]; // The first i Column [k1,k2] The sum of the trips
dp[i]=max(dp[i-1]+tmp,tmp); // The maximum sub segment sum method
mx=max(mx,dp[i]);
}
}
}
printf("%lld\n",mx);
return 0;
}边栏推荐
- easyclick,ec权朗网络验证源码
- Hanging mirror security won four global infosec awards on rsac2022
- Go 4 modes Singleton
- Freshman learning sharing
- 杰理之样机无触摸,拆机之后重新安装变正常【篇】
- NC24325 [USACO 2012 Mar S]Flowerpot
- [leetcode] there are duplicate elements [217]
- Rails 3 activerecord: sort by association count - rails 3 activerecord: order by count on Association
- Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
- Jatpack------LiveData
猜你喜欢

性能优化----严苛模式

Share 10 JS closure interview questions (diagrams), come in and see how many you can answer correctly

Wait to solve the zombie process
![[foreign journal] sleep and weight loss](/img/81/42dcfae19e72a0bc761cb7a40fe5d5.jpg)
[foreign journal] sleep and weight loss

Graphic view frame
![P7072 [CSP-J2020] 直播获奖](/img/bc/fcbc2b1b9595a3bd31d8577aba9b8b.png)
P7072 [CSP-J2020] 直播获奖

中国信通院、清华大学、腾讯安全,云原生安全产学研用强强联合!

Qt QProgressBar详解

MySQL reset password, forget password, reset root password, reset MySQL password

E-commerce system microservice architecture
随机推荐
P1007 独木桥
解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
Higher order operation of bits
Unity publishes a method of webgl playing sound
boot actuator - prometheus使用
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
Qt QSplitter拆分器
World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
Golang's learning route
`Usage of ${}`
Jerry's modification does not require long press the boot function [chapter]
Oracle-PL/SQL编程
The threshold value of fusing proportion cannot be changed with sentinel, and setting the slow call proportion has no effect
Graphic view frame
`${}`的用法
DTM distributed transaction manager PHP collaboration client V0.1 beta release!!!
钟薛高回应产品1小时不化:含固体成分 融化不能变成水
go 多线程数据搜索
Uniapp wechat login returns user name and Avatar
Jatpack------LiveData