洛谷p1719(最大加权矩形)

二维前缀和啊

 

 知道这,接下来就好办了,三个数组分别记录就好

代码;

#include<bits/stdc++.h>
#define  ll long long
const int maxn=1300;
const int inf=-0x3f3f3f;

using namespace std;
int mp[maxn][maxn];
int qz[maxn][maxn],sum[maxn][maxn];

void solve()
{
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            cin>>mp[i][j];
            qz[i][j]=qz[i][j-1]+mp[i][j];//前缀和
            sum[i][j]=qz[i][j]+sum[i-1][j];//一个矩阵的和
        }
    }
    int mx=inf;
    for(int x1=1; x1<=n; x1++)
      for(int y1=1; y1<=n; y1++)
        for(int x2=1; x2<=n; x2++)
          for(int y2=1; y2<=n; y2++)
          {
              if(x1>x2||y1>y2) continue;
              mx=max(mx,sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1]);
          }
    cout<<mx; 
    return ;
}

int main()
{
    solve();
    return 0;
}

 原题链接:https://www.luogu.com.cn/problem/P1719

posted on 2020-03-15 17:06  mmn  阅读(227)  评论(0编辑  收藏  举报