当前位置:网站首页>2036: [蓝桥杯2022初赛] 统计子矩阵(二维前缀和,一维前缀和)
2036: [蓝桥杯2022初赛] 统计子矩阵(二维前缀和,一维前缀和)
2022-07-17 22:49:00 【疯疯癫癫才自由】
2036: [蓝桥杯2022初赛] 统计子矩阵
内存限制:256 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:外部导入
提交:310 通过:74
题目描述
给定一个 N × M 的矩阵A,请你统计有多少个子矩阵(最小 1 × 1,最大 N × M) 满足:
子矩阵中所有数的和不超过给定的整数K?
输入格式
第一行包含三个整数N, M 和K.
之后 N 行每行包含 M 个整数,代表矩阵A.
30%的测试数据:1≤N,M≤20;
70%的测试数据:1≤N,M≤100;
100%的测试数据:1≤N,M≤500;0≤Aij≤1000;1≤K≤250000000。
输出格式
一个整数代表答案。
输入样例 复制
3 4 10
1 2 3 4
5 6 7 8
9 10 11 12输出样例 复制
19数据范围与提示
满足条件的子矩阵一共有19,包含:
大小为1 × 1 的有10 个。
大小为1 × 2 的有3 个。
大小为1 × 3 的有2 个。
大小为1 × 4 的有1 个。
大小为2 × 1 的有3 个。
可笑的是,第一次看到这个题,写了一个六层循环,吓死人!
对于这种二维矩阵,求和,可以用dp[i][j]来表示matr[0][0]到matr[i][j]的和,
这样能简便后面求和的步骤,减少两阶的时间复杂度,这是二维前缀和。
同时,一维前缀和怎么表示呢?dp[i][j]表示matr[0][j]到matr[i][j]的和,这只表示
第j列前面i行的和。所以这就是一维前缀和和二位前缀和的区别
/**对于这种二维矩阵,求和,可以用dp[i][j]来表示matr[0][0]到matr[i][j]的和,
这样能简便后面求和的步骤,减少两阶的时间复杂度,这是二维前缀和。
同时,一维前缀和怎么表示呢?dp[i][j]表示matr[0][j]到matr[i][j]的和,这只表示
第j列前面i行的和。所以这就是一维前缀和和二位前缀和的区别
*/
/**二维前缀和求解*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[502][502]={0};
int main()
{
memset(dp,0,sizeof(dp));
int n,m,top;
scanf("%d%d%d",&n,&m,&top);
int val;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
scanf("%d",&val);
dp[i][j]=dp[i][j-1]+dp[i-1][j]-dp[i-1][j-1]+val;
};
long long sum=0; //sum还必须定义成long long,否则百分之二十答案错误
// for(int i=1;i<=n;++i) //四重循环明显超时,百度看的"双指针求解"
// {
// for(int j=1;j<=m;++j)
// {
// for(int k=i;k<=n;++k)
// {
// for(int h=j;h<=m;++h)
// {
// if(dp[k][h]-dp[k][j-1]-dp[i-1][h]+dp[i-1][j-1]<=top)
// sum+=1;
// else break;
// }
// }
// }
// }
for(int i=1;i<=n;++i) //百度看的"双指针求解"
for(int j=i;j<=n;++j) /**现在才看明白,就是tow point*/
for(int col_l=1,col_r=1;col_r<=m;++col_r)
{
while(col_l<=col_r&&(dp[j][col_r]-dp[j][col_l-1]-dp[i-1][col_r]+dp[i-1][col_l-1])>top)
++col_l;
if(col_l<=col_r)
sum+=col_r-col_l+1;
}
cout << sum << endl;
return 0;
}/**一维前缀和求解*/
/**一维前缀和求解*/
/**
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int dp[502][502]={0};
int main()
{
memset(dp,0,sizeof(dp));
int n,m,top;
scanf("%d%d%d",&n,&m,&top);
int val;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
scanf("%d",&dp[i][j]);
dp[i][j]+=dp[i-1][j];
};
long long result=0; //result还必须定义成long long,否则百分之二十答案错误
for(int i=1;i<=n;++i) //百度看的"双指针求解"
for(int j=i;j<=n;++j) //现在才看明白,就是tow point
for(int col_l=1,col_r=1,sum=0;col_r<=m;++col_r)
{
sum+=dp[j][col_r]-dp[i-1][col_r];
while(sum>top)
{
sum-=dp[j][col_l]-dp[i-1][col_l];
++col_l;
}
if(col_l<=col_r)
result+=col_r-col_l+1;
}
cout << result << endl;
return 0;
}
*/边栏推荐
- Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
- Several points to be analyzed in the domestic fpga/dsp/zynq scheme
- Internship is a barrier to enter the society
- 2021.07.13【B站】是这样崩的
- FPGA (VGA Protocol Implementation)
- 【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用
- GYM103660H. Distance
- State machine exercise
- Istio XDS配置生成实现
- [code attached] how to realize handwritten digit recognition with hog+svm
猜你喜欢

模块1 作业

Li Hongyi machine learning -- return to July 13, 2022

Wechat applet 7 cloud storage

ObjectARX -- implementation of custom circle

PCIe Cameralink signal generator (Cameralink image analog source)

3438. Number system conversion

Wechat applet 9 release code

SBOM(Software Bill of Materials,软件物料清单)

CSRF protection mechanism

06_服务调用Feign
随机推荐
1.3.1 全排列问题
Re understanding of Fourier transform
How to read and save point cloud data with numpy
SBOM(Software Bill of Materials,软件物料清单)
3438. 数制转换
A - Play on Words
07_服务注册与发现总结
原始套接字
2021.07.13 [station B] collapsed like this
Google Earth Engine——无人机影像进行分类处理
Redis高频面试题完整版
Leetcode 1275. Trouver le vainqueur de "Jingzi"
Wechat applet 7 cloud storage
session management
现场可程式化逻辑闸阵列 FPGA
JVM常用调优配置参数
天勤第九章课后习题代码
第1章 预备知识
微信小程序合集
微信小程序7-云存储