当前位置:网站首页>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;
}
边栏推荐
- 海思3559万能平台搭建:在截获的YUV图像上旋转操作
- Go语言sqlx库操作SQLite3数据库增删改查
- 解决Chrome浏览器和Edeg浏览器主页被篡改的方法
- Jerry's modification does not require long press the boot function [chapter]
- 【洛谷P1541】乌龟棋【DP】
- Oracle PL / SQL programming
- 解决 excel 文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED
- 'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
- Mask R-CNN
- PHP optimizes SQL queries in foreach
猜你喜欢
Higher order operation of bits
Mathematical modeling -- graph and network models and methods (I)
[LeetCode] 数组中的第K个最大元素【215】
牛客网:最大子矩阵
Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
P1007 single log bridge
Comprehensively analyze the logic of the shared purchase business model? How sharing purchase empowers Enterprises
PMP项目整合管理
P1007 独木桥
NC24325 [USACO 2012 Mar S]Flowerpot
随机推荐
杰理之修改不需要长按开机功能【篇】
Il n'est pas nécessaire d'appuyer longtemps sur la fonction de démarrage pour modifier Jelly [chapitre]
Jatpack------LiveData
What is the'function'keyword used in some bash scripts- What is the 'function' keyword used in some bash scripts?
Qt QScrollArea
Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
[LeetCode] 回文数【9】
Go 4 modes Singleton
P7072 [csp-j2020] live broadcast Award
go 多线程数据搜索
Unity publishes a method of webgl playing sound
成功改变splunk 默认URL root path
stop slave卡住--事务的事件没有复制完整
Golang面试整理 三 简历如何书写
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
高并发介绍及应对
数据分析学习记录(二)---响应曲面法及Design-Expert的简单使用
To myself who is about to work
JS获取display为none的隐藏元素的宽度和高度的解决方案
Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED