当前位置:网站首页>二维前缀和
二维前缀和
2022-07-28 09:46:00 【Boletb】
1.预处理
首先要计算左上角坐标为(x1,y1)右下角坐标为(x2,y2)的矩形内数据的和要先进行预处理
目的是为了得出从(1,1)到(i,j)的矩形内数据的和的公式

即sum[ i ][ j ]=sum[ i-1 ][ j ]+sum[ i ][ j-1 ]-sum[ i-1 ][ j-1]+a[ i ][ j ];
2.得公式
经过预处理之后我们就可以进行计算

最后我们得出此矩阵的和为:
sum[ x2 ][ y2 ] - sum[ x2 ][ y1-1 ]-sum[ x1-1 ][ y2 ]+sum[ x1-1 ][ y1-1 ];
注意:因为我们在预处理和最后得出公式的时候,在sum的下标中都有[ x1-1 ]或者[ y1-1 ],所以我们在读取整个矩阵时行列坐标均从1开始而非0。
接下来是一道二维前缀和的入门题目
和我自己做这道题时的代码
#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];
//这里是预处理,得出sum[i][j]的计算公式。
}
}
int x1,y1,x2,y2;
//这里要用四个变量循环判断坐标。x1和y1是矩形的左上角坐标,x2和y2是矩形的右上角坐标
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);
//从这些矩阵和中挑出一个最大值
}
}
}
}
printf("%d",max);
return 0;
}边栏推荐
- Mobile number, fixed line regular expression
- Can multithreading optimize program performance?
- TCP Basics
- Judge whether the string is palindrome
- Mock.js
- Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
- ASP. Net core 6 framework unveiling example demonstration [29]: building a file server
- Sizebasedtriggingpolicy introduction
- 【MySQL】Got an error reading communication packets
- The high temperature continues, and public transport enterprises carry out special safety training
猜你喜欢

在Plato Farm新经济模型下,如何在游戏中获取更多MARK

3 minutes to tell you how to become a hacker | zero foundation to hacker getting started guide, you only need to master these five abilities

B2B2C系统亮点是什么?如何助力珠宝首饰企业打造全渠道多商户商城管理体系

【学习笔记】border与period

高温持续,公交企业开展安全专项培训

WPF布局之控件随着窗口等比放大缩小,适应多分辨率满屏填充应用

arthas使用教程

深度学习必懂的 13 种概率分布

Linux操作系统(Centos7)安装MySQL

并查集
随机推荐
redis的基础知识
为报复公司解雇,我更改了项目的所有代码注释!
3 minutes to tell you how to become a hacker | zero foundation to hacker getting started guide, you only need to master these five abilities
Being on duty less than 8 hours a day and being dismissed? Tencent's former employees recovered 13million overtime pay, etc., and the court won a compensation of 90000 in the final judgment
刚获融资的Espresso Systems,知识产权与团队道德双双陷入困境
使用IdentityServer出现过SameSite Cookie这个问题吗?
Can multithreading optimize program performance?
【MySQL】查询多个ID返回字符串拼接
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
【JZOF】14剪绳子
能够遍历一个文件夹下的所有文件和子文件夹
Boss: there are too many systems in the company. Can we realize account interworking?
Espresso systems, which has just obtained financing, has both intellectual property rights and team ethics in trouble
Weekly report on July 27, 2022
How to get more marks in the game under the new economic model of Plato farm
【MySQL】Got an error reading communication packets
这种动态规划你见过吗——状态机动态规划之股票问题(中)
Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
Illustrate three mainstream enterprise architecture models (recommended collection!)
SeekTiger生态通证STI 新进展,4月14日登录 ZB