比赛 2024.5.23练习赛 评测结果 WAAAA
题目名称 不重叠正方形 最终得分 80
用户昵称 zxhhh 运行时间 1.617 s
代码语言 C++ 内存使用 40.45 MiB
提交时间 2024-05-23 19:35:06
显示代码纯文本
#include <bits/stdc++.h>

using namespace std;
const int N = 1005; 
typedef long long ll; 
int n, m, a[N][N]; 
ll s[N][N], v[N][N], f[N][4], ans, g[N][N][2], p[N]; 

void solve (int op) {
    memset(f, -0x3f, sizeof f); memset(g, -0x3f, sizeof g); 
    for (int i = 1;i <= n;i++) {
        for (int j = 1;j <= n;j++) {
            s[i][j] = s[i-1][j]+s[i][j-1]-s[i-1][j-1]; 
            if (op == 0) s[i][j] += a[i][j]; 
            if (op == 1) s[i][j] += a[n-i+1][n-j+1]; 
            if (op == 2) s[i][j] += a[n-i+1][j]; 
            if (op == 3) s[i][j] += a[i][n-j+1]; 
            if (i >= m && j >= m) v[i][j] = s[i][j]-s[i-m][j]-s[i][j-m]+s[i-m][j-m]; 
            else v[i][j] = -1e18; 
        }
    }
    for (int i = n;i >= 1;i--) {
        for (int j = 1;j <= n;j++) {
            g[i][j][0] = max({v[i][j], g[i+1][j][0], g[i][j-1][0]}); 
        }
        for (int j = n;j >= 1;j--) {
            g[i][j][1] = max({v[i][j], g[i+1][j][1], g[i][j+1][1]}); 
        }
    } 
    for (int i = 1;i <= n;i++) f[i][0] = 0; 
    for (int i = m;i <= n;i++) {
        ll k = 0; 
        for (int j = m;j <= n;j++) {
            k = max(k, v[i][j]);
        }
        for (int j = 1;j <= 3;j++) f[i][j] = max(f[i-1][j], f[i-m][j-1]+k); 
    }   
    ans = max(ans, f[n][3]); 
    for (int i = m;i <= n;i++) {
        p[i] = p[i-1]; 
        for (int j = m;j <= n;j++) {
            p[i] = max(p[i], v[i][j]); 
        }
        for (int j = m;j <= n;j++) {
            ll k = v[i][j]+p[i-m]+max(j-m >= m ? g[i][j-m][0] : -1e18, j+m <= n ? g[i][j+m][1] : -1e18);
            ans = max(ans, k);  
        }
    }
}

int main () {
    freopen("zfx.in", "r", stdin); 
    freopen("zfx.out", "w", stdout); 
    cin >> n >> m; 
    for (int i = 1;i <= n;i++) {
        for (int j = 1;j <= n;j++) {
            cin >> a[i][j]; 
        }
    }
    solve(0); 
    solve(1);
    solve(2); 
    solve(3); 
    cout << ans; 
    return 0; 
}