当前位置:网站首页>Two dimensional prefix and
Two dimensional prefix and
2022-07-28 11:12:00 【Boletb】
1. Preprocessing
First, calculate the upper left coordinate as (x1,y1) The coordinates in the lower right corner are (x2,y2) The sum of the data in the rectangle You have to preprocess first
The purpose is to get from (1,1) To (i,j) The formula of the sum of the data in the rectangle

namely sum[ i ][ j ]=sum[ i-1 ][ j ]+sum[ i ][ j-1 ]-sum[ i-1 ][ j-1]+a[ i ][ j ];
2. Get the formula
After preprocessing, we can calculate

Finally, we conclude that the sum of this matrix is :
sum[ x2 ][ y2 ] - sum[ x2 ][ y1-1 ]-sum[ x1-1 ][ y2 ]+sum[ x1-1 ][ y1-1 ];
Be careful : Because when we preprocess and finally get the formula , stay sum In the subscript of [ x1-1 ] perhaps [ y1-1 ], So when we read the whole matrix, the row and column coordinates are from 1 Start instead of 0.
Next is an introduction to the two-dimensional prefix and
Maximum weighted rectangle - Luogu
And the code when I did this problem myself
#include<stdio.h>
#include<math.h>
#define N 130
int a[N][N];
int sum[N][N];
int max=0,p=0;
int main()
{
int n;
scanf("%d",&n);
int i=0,j=0;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
scanf("%d",&a[i][j]);
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+a[i][j];
// Here is pretreatment , obtain sum[i][j] Calculation formula .
}
}
int x1,y1,x2,y2;
// Here we need to use four variables to judge the coordinates .x1 and y1 Is the coordinate of the upper left corner of the rectangle ,x2 and y2 Is the coordinate of the upper right corner of the rectangle
for(x1=1;x1<n;x1++){
for(y1=1;y1<n;y1++){
for(x2=x1;x2<=n;x2++){
for(y2=y1;y2<=n;y2++){
p=sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1];
max=(max>p?max:p);
// Pick a maximum value from these matrix sums
}
}
}
}
printf("%d",max);
return 0;
}边栏推荐
- Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
- Purchase, sale and inventory software suitable for small and medium-sized enterprises to solve five major problems
- Arduino Basics
- 11_ UE4 advanced_ Change male characters to female characters and modify the animation
- 剑指 Offer 35. 复杂链表的复制
- Explanation of JDBC classes
- 理解Oracle的几个概念
- 学会这些分析方法及模型,遇到问题不再没思路
- 分体式测斜探头安装要点及注意事项
- 用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
猜你喜欢

CRM+零代码:轻松实现企业信息化

什么是WordPress

Bc35 NB module at instruction development summary

盘点:令人心动的数据可视化图表

11_ UE4 advanced_ Change male characters to female characters and modify the animation

10_ UE4 advanced_ Add fall and cast actions

Learn how to do e-commerce data analysis (with operation analysis index framework)

leetcode:1300. 转变数组后最接近目标值的数组和【二分】

6. MapReduce custom partition implementation

I use the applet container to improve the efficiency of mobile R & D by 5 times!
随机推荐
构建快捷开发IDE:VisualSVN+Sublime+Visual Studio 2013+QuickEasyFTPServer
leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】
图片滑动特效
做数据分析,你还不懂RFM分析方法(模型)?
The use of C language linked list
CGAL compilation error
JSON preliminary understanding
判断数码管是共阳极还是共阴极
Select without the order by clause, the order of the returned results is not reliable
Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
const与指针的组合使用
盘点:令人心动的数据可视化图表
Inventory: 144 free learning websites, the most complete collection of resources in the whole network
Inventory: 6 books teach you the necessary skills for career promotion
3. MapReduce explanation and source code analysis
JS - modify the key name of the object in the array
Network file system service (NFS)
盘点:144个免费学习网站,全网最全资源合集
I use the applet container to improve the efficiency of mobile R & D by 5 times!
Software designers ask 20 questions before the exam, pay attention!!