結果

問題 No.1045 直方体大学
ユーザー MisterMister
提出日時 2020-05-01 21:59:12
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 1,655 bytes
コンパイル時間 782 ms
コンパイル使用メモリ 81,124 KB
実行使用メモリ 63,172 KB
最終ジャッジ日時 2023-08-26 10:55:37
合計ジャッジ時間 3,145 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 115 ms
62,912 KB
testcase_09 AC 108 ms
62,908 KB
testcase_10 AC 103 ms
63,172 KB
testcase_11 AC 108 ms
62,968 KB
testcase_12 AC 115 ms
63,036 KB
testcase_13 AC 137 ms
62,980 KB
testcase_14 AC 120 ms
62,916 KB
testcase_15 AC 134 ms
63,044 KB
testcase_16 AC 141 ms
62,920 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 361 ms
62,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class T>
std::vector<T> vec(int len, T elem) { return std::vector<T>(len, elem); }

void solve() {
    int n;
    std::cin >> n;

    auto ps = vec(n, vec(3, 0));
    for (auto& p : ps) {
        for (auto& x : p) {
            std::cin >> x;
        }
    }

    auto judge = [&](int ai, int aj, int bi, int bj) {
        int ax = ps[ai][(aj + 1) % 3], ay = ps[ai][(aj + 2) % 3];
        int bx = ps[bi][(bj + 1) % 3], by = ps[bi][(bj + 2) % 3];

        if (ax > ay) std::swap(ax, ay);
        if (bx > by) std::swap(bx, by);

        return bx <= ax && by <= ay;
    };

    auto dp = vec(1 << n, vec(n, vec(3, -1)));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 3; ++j) {
            dp[1 << i][i][j] = ps[i][j];
        }
    }

    int ans = 0;
    for (int b = 0; b < (1 << n); ++b) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < 3; ++j) {
                if (dp[b][i][j] == -1) continue;
                ans = std::max(ans, dp[b][i][j]);

                for (int ni = 0; ni < n; ++ni) {
                    if ((b >> ni) & 1) continue;
                    int nb = b | (1 << ni);

                    for (int nj = 0; nj < 3; ++nj) {
                        if (!judge(i, j, ni, nj)) continue;
                        dp[nb][ni][nj] = std::max(dp[nb][ni][nj],
                                                  dp[b][i][j] + ps[ni][nj]);
                    }
                }
            }
        }
    }

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

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0