比赛 |
国庆欢乐赛3 |
评测结果 |
ATTTT |
题目名称 |
不重叠正方形 |
最终得分 |
20 |
用户昵称 |
淮淮清子 |
运行时间 |
8.003 s |
代码语言 |
C++ |
内存使用 |
15.68 MiB |
提交时间 |
2025-10-05 11:02:47 |
显示代码纯文本
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 1005;
ll a[MAXN][MAXN], sum[MAXN][MAXN];
int n, m, s;
ll S(int x, int y) {
int x2 = x + m - 1, y2 = y + m - 1;
return sum[x2][y2] - sum[x - 1][y2] - sum[x2][y - 1] + sum[x - 1][y - 1];
}
bool check(int x1, int y1, int x2, int y2){
int x1r = x1 + m - 1, y1b = y1 + m - 1;
int x2r = x2 + m - 1, y2b = y2 + m - 1;
return x1r < x2 || x2r < x1 || y1b < y2 || y2b < y1;
}
int main(){
freopen("zfx.in", "r", stdin);
freopen("zfx.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
s = n - m + 1;
if(s < 3){
cout << 0 << '\n';
return 0;
}
for(int i = 1;i <= n;i ++){
ll res = 0;
for(int j = 1;j <= n;j ++){
cin >> a[i][j];
res += a[i][j];
sum[i][j] = sum[i - 1][j] + res;
}
}
ll maxx = 0;
for(int x1 = 1;x1 <= s;x1 ++){
for(int y1 = 1;y1 <= s;y1 ++){
ll s1 = S(x1, y1);
for(int x2 = 1;x2 <= s;x2 ++){
for(int y2 = 1;y2 <= s;y2 ++){
if(!check(x1, y1, x2, y2)) continue;
ll s2 = S(x2, y2);
for(int x3 = 1;x3 <= s;x3 ++){
for(int y3 = 1;y3 <= s;y3 ++){
if(!check(x1, y1, x3, y3) || !check(x2, y2, x3, y3)) continue;
maxx = max(maxx, s1 + s2 + S(x3, y3));
}
}
}
}
}
}
cout << maxx << '\n';
return 0;
}