Codeforces Beta Round 22 (Div. 2 Only) B. Bargaining Table 题解 前缀和

Bargaining Table

题目描述

Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob’s office room is a rectangular room n × m n×m n×m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn’t want to change or rearrange anything, that’s why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.

输入格式

The first line contains 2 space-separated numbers n n n and m m m ( 1 < = n , m < = 25 1<=n,m<=25 1<=n,m<=25 ) — the office room dimensions. Then there follow n n n lines with m m m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It’s guaranteed that at least one square meter in the room is free.

输出格式

Output one number — the maximum possible perimeter of a bargaining table for Bob’s office room.

题面翻译

给定一个 n × m n \times m n×m 的矩阵,求全 0 0 0 子矩阵的周长最大值。

样例 #1

样例输入 #1

3 3
000
010
000

样例输出 #1

8

样例 #2

样例输入 #2

5 4
1100
0000
0000
0000
0000

样例输出 #2

16

原题

Codeforces——传送门
洛谷——传送门

思路&代码

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, m;
    cin >> n >> m;
    // 存矩形图,将字符'1'和'0'转化为数字1和0存储,便于计算
    vector<vector<char>> a(n + 1, vector<char>(m + 1, 0));
    vector<vector<int>> g(n + 1, vector<int>(m + 1, 0));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            g[i][j] = (a[i][j] == '1') ? 1 : 0;
        }
    // 求两次前缀和,先每一行求前缀和,再每一列求前缀和
    vector<vector<int>> p(n + 1, vector<int>(m + 1, 0));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            p[i][j] = p[i][j - 1] + g[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            p[i][j] = p[i - 1][j] + p[i][j];
    // 遍历谈判桌的左上顶点和右下顶点,确定长方形区域后判断其是否空闲
    int ans = 0;
    for (int sx = 1; sx <= n; sx++)
        for (int sy = 1; sy <= m; sy++)
            for (int ex = sx; ex <= n; ex++)
                for (int ey = sy; ey <= m; ey++)
                {
                    // 两次前缀和还原为区域总和
                    int sum1 = p[ex][sy - 1] - p[sx - 1][sy - 1];
                    int sum2 = p[ex][ey] - p[sx - 1][ey];
                    if (sum2 - sum1 == 0) // 若该长方形区域空闲,用其周长更新答案
                    {
                        int C = 2 * (ex - sx + 1) + 2 * (ey - sy + 1); // 计算周长
                        ans = max(ans, C);
                    }
                }

    cout << ans << '\n';

    return 0;
}
  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值