結果

問題 No.334 門松ゲーム
ユーザー rogi52rogi52
提出日時 2022-10-03 23:20:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,600 bytes
コンパイル時間 2,300 ms
コンパイル使用メモリ 210,080 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-27 21:02:27
合計ジャッジ時間 3,472 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N; cin >> N;
    vector<int> K(N);
    rep(i,N) cin >> K[i];

    vector<vector<int>> G(1 << N);
    enum {WIN, LOSE};
    vector<int> dp(1 << N, -1);
    rep(S,1<<N) {
        int bcnt = __builtin_popcount(S);
        if((N - bcnt) % 3 == 0) {
            vector<int> id;
            rep(i,N) if(S & (1 << i)) id.push_back(i);
            int n = id.size();
            int LOSE_CNT = 0;
            rep(k,n)rep(j,k)rep(i,j) {
                int a = id[i], b = id[j], c = id[k];
                auto [mi, ma] = minmax({K[a], K[b], K[c]});
                if(K[a] != K[b] && K[b] != K[c] && K[c] != K[a] && (mi == K[b] || ma == K[b])) {
                    if(dp[S - (1 << a) - (1 << b) - (1 << c)] == LOSE) LOSE_CNT++;
                }
            }
            dp[S] = (LOSE_CNT >= 1 ? WIN : LOSE);
        }
    }

    if(dp[(1 << N) - 1] == WIN) {
        tuple<int,int,int> ans = {1e9, 1e9, 1e9};
        rep(c,N)rep(b,c)rep(a,b) {
            auto [mi, ma] = minmax({K[a], K[b], K[c]});
            if(K[a] != K[b] && K[b] != K[c] && K[c] != K[a] && (mi == K[b] || ma == K[b])) {
                if(dp[((1 << N) - 1) - (1 << a) - (1 << b) - (1 << c)] == LOSE) {
                    ans = min(ans, tuple<int,int,int>{a, b, c});
                }
            }
        }
        
        auto [a, b, c] = ans;
        cout << a << " " << b << " " << c << endl;
    } else {
        cout << -1 << endl;
    }
}
0