結果

問題 No.2509 Beam Shateki
ユーザー nono00nono00
提出日時 2023-10-20 22:57:20
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 8,220 bytes
コンパイル時間 3,185 ms
コンパイル使用メモリ 266,656 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-20 21:16:28
合計ジャッジ時間 49,198 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 123 ms
5,376 KB
testcase_04 AC 124 ms
5,376 KB
testcase_05 AC 123 ms
5,376 KB
testcase_06 AC 125 ms
5,376 KB
testcase_07 AC 124 ms
5,376 KB
testcase_08 AC 126 ms
5,376 KB
testcase_09 AC 124 ms
5,376 KB
testcase_10 AC 125 ms
5,376 KB
testcase_11 AC 126 ms
5,376 KB
testcase_12 AC 130 ms
5,376 KB
testcase_13 AC 1,827 ms
5,376 KB
testcase_14 AC 1,823 ms
5,376 KB
testcase_15 AC 1,801 ms
5,376 KB
testcase_16 AC 1,902 ms
5,376 KB
testcase_17 AC 1,823 ms
5,376 KB
testcase_18 AC 1,798 ms
5,376 KB
testcase_19 AC 1,831 ms
5,376 KB
testcase_20 AC 1,816 ms
5,376 KB
testcase_21 AC 1,833 ms
5,376 KB
testcase_22 AC 1,846 ms
5,376 KB
testcase_23 AC 1,846 ms
5,376 KB
testcase_24 AC 1,814 ms
5,376 KB
testcase_25 AC 1,837 ms
5,376 KB
testcase_26 AC 1,821 ms
5,376 KB
testcase_27 AC 1,827 ms
5,376 KB
testcase_28 AC 1,843 ms
5,376 KB
testcase_29 AC 1,850 ms
5,376 KB
testcase_30 AC 1,854 ms
5,376 KB
testcase_31 AC 1,813 ms
6,944 KB
testcase_32 AC 1,792 ms
6,940 KB
testcase_33 AC 10 ms
6,944 KB
testcase_34 AC 268 ms
6,940 KB
testcase_35 AC 762 ms
6,940 KB
testcase_36 AC 69 ms
6,940 KB
testcase_37 AC 6 ms
6,940 KB
testcase_38 AC 75 ms
6,940 KB
testcase_39 AC 63 ms
6,940 KB
testcase_40 AC 336 ms
6,944 KB
testcase_41 AC 2 ms
6,944 KB
testcase_42 AC 102 ms
6,940 KB
testcase_43 AC 964 ms
6,944 KB
testcase_44 AC 1,221 ms
6,940 KB
testcase_45 AC 62 ms
6,940 KB
testcase_46 AC 127 ms
6,940 KB
testcase_47 AC 1,207 ms
6,944 KB
testcase_48 AC 208 ms
6,940 KB
testcase_49 AC 391 ms
6,944 KB
testcase_50 AC 576 ms
6,940 KB
testcase_51 AC 9 ms
6,944 KB
testcase_52 AC 287 ms
6,940 KB
testcase_53 AC 2 ms
6,944 KB
testcase_54 AC 1 ms
6,940 KB
testcase_55 AC 1 ms
6,940 KB
testcase_56 AC 2 ms
6,940 KB
testcase_57 AC 1 ms
6,940 KB
testcase_58 AC 2 ms
6,944 KB
testcase_59 AC 1 ms
6,940 KB
testcase_60 AC 1 ms
6,944 KB
testcase_61 AC 1 ms
6,944 KB
testcase_62 WA -
testcase_63 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <initializer_list>
#include <vector>

namespace nono {

template <typename T>
class Matrix {
  public:
    Matrix() = default;
    Matrix(int row, int column): row_(row), column_(column), data_(row, std::vector<T>(column)) {}
    Matrix(int row, int column, T elem): row_(row), column_(column), data_(row, std::vector<T>(column, elem)) {}
    explicit Matrix(const std::vector<std::vector<T>>& data)
        : row_(data.size()),
          column_(data.front().size()),
          data_(data) {
        for (int i = 0; i < row_; i++) {}
    }
    Matrix(std::initializer_list<std::vector<T>> init): data_(init.begin(), init.end()) {
        row_ = data_.size();
        column_ = data_.front().size();
        for (int i = 0; i < row_; i++) {}
    }

    const std::vector<T>& operator[](const int pos) const {
        return data_[pos];
    }

    std::vector<T>& operator[](const int pos) {
        return data_[pos];
    }

    Matrix& operator+=(const T rhs) {
        for (int i = 0; i < row_; i++) {
            for (int j = 0; j < column_; j++) {
                data_[i][j] += rhs;
            }
        }
        return *this;
    }

    Matrix& operator-=(const T rhs) {
        for (int i = 0; i < row_; i++) {
            for (int j = 0; j < column_; j++) {
                data_[i][j] -= rhs;
            }
        }
        return *this;
    }

    Matrix& operator*=(const T rhs) {
        for (int i = 0; i < row_; i++) {
            for (int j = 0; j < column_; j++) {
                data_[i][j] *= rhs;
            }
        }
        return *this;
    }

    Matrix& operator/=(const T rhs) {
        for (int i = 0; i < row_; i++) {
            for (int j = 0; j < column_; j++) {
                data_[i][j] /= rhs;
            }
        }
        return *this;
    }

    friend Matrix operator+(const Matrix& lhs, const T rhs) {
        return Matrix(lhs) += rhs;
    }
    friend Matrix operator+(const T lhs, const Matrix& rhs) {
        return Matrix(rhs) += lhs;
    }
    friend Matrix operator-(const Matrix& lhs, const T rhs) {
        return Matrix(lhs) -= rhs;
    }
    friend Matrix operator-(const T lhs, const Matrix& rhs) {
        return Matrix(rhs) -= lhs;
    }
    friend Matrix operator*(const Matrix& lhs, const T rhs) {
        return Matrix(lhs) *= rhs;
    }
    friend Matrix operator*(const T lhs, const Matrix& rhs) {
        return Matrix(rhs) *= lhs;
    }
    friend Matrix operator/(const Matrix& lhs, const T rhs) {
        return Matrix(lhs) /= rhs;
    }
    friend Matrix operator/(const T lhs, const Matrix& rhs) {
        return Matrix(rhs) /= lhs;
    }

    Matrix& operator+=(const Matrix& rhs) {
        for (int i = 0; i < row_; i++) {
            for (int j = 0; j < column_; j++) {
                data_[i][j] += rhs.data_[i][j];
            }
        }
        return *this;
    }

    Matrix& operator-=(const Matrix& rhs) {
        for (int i = 0; i < row_; i++) {
            for (int j = 0; j < column_; j++) {
                data_[i][j] -= rhs.data_[i][j];
            }
        }
        return *this;
    }

    Matrix& operator*=(const Matrix& rhs) {
        std::vector<std::vector<T>> mat(row_, std::vector<T>(rhs.column_));
        for (int i = 0; i < row_; i++) {
            for (int k = 0; k < column_; k++) {
                for (int j = 0; j < rhs.column_; j++) {
                    mat[i][j] += data_[i][k] * rhs.data_[k][j];
                }
            }
        }
        data_ = std::move(mat);
        column_ = rhs.column_;
        return *this;
    }

    friend Matrix operator+(const Matrix& lhs, const Matrix& rhs) {
        return Matrix(lhs) += rhs;
    }
    friend Matrix operator-(const Matrix& lhs, const Matrix& rhs) {
        return Matrix(lhs) -= rhs;
    }
    friend Matrix operator*(const Matrix& lhs, const Matrix& rhs) {
        return Matrix(lhs) *= rhs;
    }

    [[nodiscard]] Matrix pow(long long exp) const {
        Matrix result(row_, column_);
        Matrix base(*this);
        for (int i = 0; i < row_; i++) {
            result[i][i] = static_cast<T>(1);
        }
        while (exp > 0) {
            if (exp & 1) {
                result *= base;
            }
            base *= base;
            exp >>= 1;
        }
        return result;
    }

    [[nodiscard]] Matrix rotate() {
        std::vector<std::vector<T>> result(column_, std::vector<T>(row_));
        for (int i = 0; i < row_; i++) {
            for (int j = 0; j < column_; j++) {
                result[j][row_ - i - 1] = data_[i][j];
            }
        }
        return Matrix(result);
    }

    [[nodiscard]] Matrix transpose() {
        std::vector<std::vector<T>> result(column_, std::vector<T>(row_));
        for (int i = 0; i < row_; i++) {
            for (int j = 0; j < column_; j++) {
                result[j][i] = data_[i][j];
            }
        }
        return Matrix(result);
    }

    int row() {
        return row_;
    }

    int column() {
        return column_;
    }

  private:
    int row_, column_;
    std::vector<std::vector<T>> data_;
};

}  //  namespace nono

namespace nono {

struct Init {};

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

    int ans = 0;

    for (int a = 0; a < 2; a++) {
        for (int b = 0; b < 4; b++) {
            int r = grid.row();
            int c = grid.column();

            for (int i = 0; i < r; i++) {
                int now = 0;
                for (int j = 0; j < c; j++) {
                    now += grid[i][j];
                }

                for (int k = 0; k + 1 < r + c; k++) {
                    int sum = now;
                    for (int x = 0; x < r; x++) {
                        for (int y = 0; y < c; y++) {
                            if (x + y == k && x != i) {
                                sum += grid[x][y];
                            }
                        }
                    }
                    ans = std::max(ans, sum);
                }
            }

            grid = grid.rotate();
        }

        auto temp = grid;
        int r = grid.row();
        int c = grid.column();
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                grid[i][j] = temp[i][c - j - 1];
            }
        }
    }

    std::vector<int> row(h);
    std::vector<int> column(w);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            row[i] += grid[i][j];
            column[j] += grid[i][j];
        }
    }

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

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

    if (h > 1) {
        ans = std::max(ans, row[0] + row[1]);
    }
    if (w > 1) {
        ans = std::max(ans, column[0] + column[1]);
    }

    for (int a = 0; a < 2; a++) {
        int r = grid.row();
        int c = grid.column();
        std::vector<int> naname(r + c - 1);
        for (int i = 0; i < r; i++) {
            for (int j = 0; j < c; j++) {
                naname[i + j] += grid[i][j];
            }
        }
        std::ranges::sort(naname, std::greater());
        if (r + c - 1 > 1) {
            ans = std::max(ans, naname[0] + naname[1]);
        }
        grid = grid.rotate();
    }

    for (int i = -h; i < 2 * h; i++) {
        for (int j = -w; j < 2 * w; j++) {
            int now = 0;
            for (int k = 0; k < h; k++) {
                for (int l = 0; l < w; l++) {
                    if (std::abs(k - i) == std::abs(l - j)) {
                        now += grid[k][l];
                    }
                }
            }
            ans = std::max(ans, now);
        }
    }

    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