不想写标程啊, 转成切比雪夫距离暴力就能过啦。。 复杂度n * m * k, 其实复杂度能处理到n * m
#include#define LL long long#define LD long double#define ull unsigned long long#define fi first#define se second#define mk make_pair#define PLL pair #define PLI pair #define PII pair #define SZ(x) ((int)x.size())#define ALL(x) (x).begin(), (x).end()#define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 2000 + 7;const int inf = 0x3f3f3f3f;const LL INF = 0x3f3f3f3f3f3f3f3f;const int mod = 1e9 + 7;const double eps = 1e-8;const double PI = acos(-1);template inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}template inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}template inline bool chkmax(T& a, S b) { return a < b ? a = b, true : false;}template inline bool chkmin(T& a, S b) { return a > b ? a = b, true : false;}int n, m, k;int a[N][N];LL b[N][N];inline LL getVal(int X1, int X2, int Y1, int Y2) { return b[X2][Y2] - b[X2][Y1 - 1] - b[X1 - 1][Y2] + b[X1 - 1][Y1 - 1];}int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { scanf("%d", &a[i][j]); b[i + j][i - j + m] = a[i][j]; } } for(int i = 1; i <= n + m; i++) for(int j = 1; j <= n + m; j++) b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1]; LL ans = -INF, ret = 0; int ansx, ansy, x, y; for(int i = k; i <= n - k + 1; i++) { for(int j = k; j <= m - k + 1; j++) { x = i + j, y = i - j + m; ret = 0; for(int z = 0; z < k; z++) ret += getVal(x - z, x + z, y - z, y + z); if(chkmax(ans, ret)) ansx = i, ansy = j; } } printf("%d %d\n", ansx, ansy); return 0;}/**/