P2036 - [蓝桥杯2022初赛] 统计子矩阵 - New Online Judgehttp://oj.ecustacm.cn/problem.php?id=2036暴力能过80%的数据。。。
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll a[512][512] = { 0 };
ll sum[512][512] = { 0 }; // sum[i][j]表示从a[1][1]到a[i][j]的和
int main() {
// system("chcp 65001");
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("C:/Users/zhaochen/Desktop/input.txt", "r", stdin);
int n, m, k;
cin >> n >> m >> k;
ll ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) { // 左上角
for (int p = i; p <= n; p++) {
for (int q = j; q <= m; q++) { // 右下角
ll t = sum[p][q] - sum[p][j - 1] - sum[i - 1][q] + sum[i - 1][j - 1];
if (t <= k) {
ans++;
} else {
break;
}
}
}
}
}
cout << ans;
return 0;
}
|