题目读完首先想到读入时前缀和处理一下,然后枚举首都左上角坐标即可,数据很弱所以可以通过,事实证明我的思路是对的.
为了保险还给ans开了个long long,燃鹅我还是年轻.
最后一个点还是把我卡爆了,原因竟然是题目没有提及的地块上的价值的数据范围.
数组开long long,搞定了.
有点水.
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int n, m, c; long long s[1010][1010]; long long ans = -100000000; int x, y; int main() { // freopen("in.txt", "r", stdin); cin >> n >> m >> c; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> s[i][j]; s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1]; } for (int i = 1; i + c - 1 <= n; i++) for (int j = 1; j + c - 1 <= m; j++) { long long val = (long long)s[i + c - 1][j + c - 1] - s[i - 1][j + c - 1] - s[i + c - 1][j - 1] + s[i - 1][j - 1]; if (ans < val){ x = i; y = j; ans = val; } } cout << x << ' ' << y << endl; return 0; }