結果

問題 No.2509 Beam Shateki
ユーザー nono00nono00
提出日時 2023-10-20 23:39:38
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 3,626 bytes
コンパイル時間 3,563 ms
コンパイル使用メモリ 266,412 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-20 23:39:45
合計ジャッジ時間 5,466 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 3 ms
4,348 KB
testcase_27 AC 3 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 3 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 2 ms
4,348 KB
testcase_42 AC 2 ms
4,348 KB
testcase_43 AC 3 ms
4,348 KB
testcase_44 AC 3 ms
4,348 KB
testcase_45 AC 2 ms
4,348 KB
testcase_46 AC 2 ms
4,348 KB
testcase_47 AC 3 ms
4,348 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
testcase_51 AC 2 ms
4,348 KB
testcase_52 AC 2 ms
4,348 KB
testcase_53 AC 2 ms
4,348 KB
testcase_54 AC 2 ms
4,348 KB
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 AC 2 ms
4,348 KB
testcase_58 AC 2 ms
4,348 KB
testcase_59 AC 2 ms
4,348 KB
testcase_60 AC 2 ms
4,348 KB
testcase_61 AC 2 ms
4,348 KB
testcase_62 AC 2 ms
4,348 KB
testcase_63 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

namespace nono {

struct Init {};

void solve([[maybe_unused]] const Init& init) {
    int h, w;
    std::cin >> h >> w;
    std::vector a(h, std::vector<int>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            std::cin >> a[i][j];
        }
    }

    int ans = 0;

    std::vector<int> row(h);
    std::vector<int> column(w);
    std::vector<int> naname1(h + w - 1);
    std::vector<int> naname2(h + w - 1);

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            row[i] += a[i][j];
            column[j] += a[i][j];
            naname1[i + j] += a[i][j];
            naname2[h - 1 - i + j] += a[i][j];
        }
    }

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            ans = std::max(ans, row[i] + column[j] - a[i][j]);
        }
    }

    for (int i = 0; i < h; i++) {
        for (int k = 0; k + 1 < h + w - 1; k++) {
            int j = k - i;
            if (0 <= j && j < w) {
                ans = std::max(ans, row[i] + naname1[k] - a[i][j]);
            } else {
                ans = std::max(ans, row[i] + naname1[k]);
            }
        }
    }

    for (int i = 0; i < h; i++) {
        for (int k = 0; k + 1 < h + w - 1; k++) {
            int j = k - h + 1 + i;
            if (0 <= j && j < w) {
                ans = std::max(ans, row[i] + naname2[k] - a[i][j]);
            } else {
                ans = std::max(ans, row[i] + naname2[k]);
            }
        }
    }

    for (int j = 0; j < w; j++) {
        for (int k = 0; k + 1 < h + w - 1; k++) {
            int i = k - j;
            if (0 <= i && i < h) {
                ans = std::max(ans, column[j] + naname1[k] - a[i][j]);
            } else {
                ans = std::max(ans, column[j] + naname1[k]);
            }
        }
    }

    for (int j = 0; j < w; j++) {
        for (int k = 0; k + 1 < h + w - 1; k++) {
            int i = h - 1 + j - k;
            if (0 <= i && i < h) {
                ans = std::max(ans, column[j] + naname2[k] - a[i][j]);
            } else {
                ans = std::max(ans, column[j] + naname2[k]);
            }
        }
    }

    for (int k1 = 0; k1 + 1 < h + w - 1; k1++) {
        for (int k2 = 0; k2 + 1 < h + w - 1; k2++) {
            bool flag = false;
            if ((k1 + k2 - h + 1) % 2 == 0) {
                int j = (k1 + k2 - h + 1) / 2;
                int i = k1 - j;
                if (0 <= i && i < h && 0 <= j && j < w) {
                    flag = true;
                    ans = std::max(ans, naname1[k1] + naname2[k2] - a[i][j]);
                }
            }
            if (!flag) {
                ans = std::max(ans, naname1[k1] + naname2[k2]);
            }
        }
    }

    std::ranges::sort(row, std::greater());
    std::ranges::sort(column, std::greater());
    std::ranges::sort(naname1, std::greater());
    std::ranges::sort(naname2, std::greater());

    ans = std::max(ans, row[0]);
    ans = std::max(ans, column[0]);
    ans = std::max(ans, naname1[0]);
    ans = std::max(ans, naname2[0]);
    if (row.size() > 1) ans = std::max(ans, row[0] + row[1]);
    if (column.size() > 1) ans = std::max(ans, column[0] + column[1]);
    if (naname1.size() > 1) ans = std::max(ans, naname1[0] + naname1[1]);
    if (naname2.size() > 1) ans = std::max(ans, naname2[0] + naname2[1]);

    std::cout << ans << std::endl;
}

}  //  namespace nono

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    std::cout << std::fixed << std::setprecision(16);

    int t = 1;
    nono::Init init;

    while (t--) nono::solve(init);
}
0