当前位置:网站首页>Acwing4405. Statistical submatrix
Acwing4405. Statistical submatrix
2022-07-19 12:29:00 【Science and technology big pig】
Given a N×M Matrix A, Please count how many sub matrices there are ( Minimum 1×1, Maximum N×M) Satisfy that the sum of all numbers in the submatrix does not exceed the given integer K?
Input format
The first line contains three integers N,M and K.
after N Each line contains M It's an integer , Representative matrix A.
Output format
An integer represents the answer .
Data range
about 30% The data of ,N,M≤20,
about 70% The data of ,N,M≤100,
about 100% The data of ,1≤N,M≤500;0≤Aij≤1000;1≤K≤250000000.
sample input :
3 4 10
1 2 3 4
5 6 7 8
9 10 11 12
sample output :
19
Sample explanation
The submatrix satisfying the condition has 19, contain :
- The size is 1×1 There are 10 individual .
- The size is 1×2 There are 3 individual .
- The size is 1×3 There are 2 individual .
- The size is 1×4 There are 1 individual .
- The size is 2×1 There are 3 individual .
If you use it directly The prefix and + violence , The complexity will be O(N^4), Must be optimized
The way to optimize is :
1) Enumerating submatrix Left boundary i and Right border j,
2) use Quick pointer t enumeration The lower boundary of the submatrix , Slow pointer s maintain Upper boundary of submatrix (s ≤ t)
3) If the sum of the weights of the obtained submatrix Greater than k, Slow pointer s Forward , And the sum of submatrixes must be monotonic
4) Slow pointer s To move forward ( Pictured ), until The sum of submatrix No more than k, There is no need for the slow pointer to move forward , Because all widths of this submatrix are j - i + 1 The submatrix of ( in total t - s + 1 Kind of ) Must meet the requirements , Update the contribution of this situation to the answer t - s + 1; conversely , If the slow pointer s Transboundary (s > t), Do not operate , Directly enter the lower circulation

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
int a[N][N] , qz[N][N] , n , m;
long long int k , sum = 0;
int main()
{
cin >> n >> m >> k;
// Prefix and representation
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= m; j ++ ){
cin >> a[i][j];
//a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
qz[i][j] = a[i][j] + qz[i - 1][j] + qz[i][j - 1] - qz[i - 1][j - 1];
}
// Double pointer algorithm
for(int i = 1; i <= m; i ++){
for(int j = i; j <= m; j ++){
for(int s = 1, t = 1; t <= n; t ++ ){
while(s <= t && qz[t][j] - qz[s - 1][j] - qz[t][i - 1] + qz[s - 1][i - 1] > k) s ++;
if(s <= t) sum += t - s + 1;
}
}
}
cout << sum << endl;
return 0;
}
边栏推荐
猜你喜欢

2022-07-07:Spire.Office 7.7.2 for net 闪亮登场

NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices

第四天作業

Dynamic memory planning

HCIP(8)

ATT&CK实战系列——红队实战(—)

Gradient button function button drawing C language example

超声波传感器(CHx01) 学习笔记 Ⅲ - I2C读写操作
MIHA tour 2023 autumn recruitment officially begins ~ early approval has the opportunity to avoid written examination!

Leetcode 20. Valid parentheses
随机推荐
云犀&腾讯云达成战略合作,加速拓展全球直播市场
编辑技巧篇
2022 safety officer-c certificate title and answer
WAV和PCM的关系和区别
Leetcode 150. Evaluation of inverse Polish expression
String correlation function (II)
招生宣传-江南大学
新一代云数据库的引领者---AWS
C# . Net Yunnan rural credit national secret signature (SM2) brief analysis
How to run SH script file
OpenCV 教程 03: 如何跟踪视频中的某一对象
PPPoE拨号上网
Softmax和Cross-entropy是什么关系?
C # from introduction to mastery Part 1: C # overview and introduction
2022安全员-C证上岗证题目及答案
2022年低压电工考试题及在线模拟考试
数据库每日一题---第25天:银行账户概要 II
超声波传感器(CHx01) 学习笔记 Ⅲ - I2C读写操作
Acwing4405. 统计子矩阵
ATT&CK实战系列——红队实战(—)