比赛 2024.5.23练习赛 评测结果 WTTTT
题目名称 不重叠正方形 最终得分 0
用户昵称 liuyiche 运行时间 4.000 s
代码语言 C++ 内存使用 17.04 MiB
提交时间 2024-05-23 21:26:44
显示代码纯文本
    #include <bits/stdc++.h>
     
    using namespace std;
    
    typedef long long ll;
    
    int n, m;
    ll ans = 0;
    ll a[1005][1005];
    ll f[1005][1005];
    struct node
    {
        ll w;
        int x, y;
        int p;
    };
    vector<node> v;
    
    inline void dfs(int step, int cnt, ll num)
    {
        if(step >= v.size())
            return;
        if(cnt == 3)
        {
            ans = max(ans,num);
            //cout << num << " ";
            return;
        }
        dfs(step+1,cnt,num);
        if(v[step].p == 0)
        {
            int c = 1;
            for(int i = v[step].y+1; i <= min(n-m+1,v[step].y+m-1); ++i,++c)
            v[step+c].p++;
            for(int i = 1; i < m; ++i)
            {
                if(step+i*(n-m+1) >= v.size())
                    break;
                v[step+i*(n-m+1)].p++;
            }
            dfs(step+1,cnt+1,num+v[step].w);
            c = 1;
            for(int i = v[step].y+1; i <= min(n-m+1,v[step].y+m-1); ++i,++c)
            v[step+c].p--;
            for(int i = 1; i < m; ++i)
            {
                if(step+i*(n-m+1) >= v.size())
                    break;
                v[step+i*(n-m+1)].p--;
            }
        }
    } 
    int main()
    {
        freopen("zfx.in","r",stdin);
        freopen("zfx.out","w",stdout);
     
        ios::sync_with_stdio(false);
        cin.tie(0);  cout.tie(0);
     
        cin >> n >> m;
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                cin >> a[i][j];
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                f[i][j] = f[i-1][j]+f[i][j-1]-f[i-1][j-1]+a[i][j];
        
        for(int i = 1; i+m-1 <= n; ++i)
            for(int j = 1; j+m-1 <= n; ++j)
            {
                node tmp;
                tmp.w = f[i+m-1][j+m-1]-f[i+m-1][j-1]-f[i-1][j+m-1]+f[i-1][j-1], tmp.x = i, tmp.y = j, tmp.p = 0;
                v.push_back(tmp);    
            }
        /*for(int i = 0; i < v.size(); ++i)
            cout << v[i].w << " ";  */  
        dfs(0,0,0);
        
        cout << ans;
     
        return 0;
    }