結果

問題 No.334 門松ゲーム
ユーザー rogi52rogi52
提出日時 2022-10-03 23:20:21
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,600 bytes
コンパイル時間 2,610 ms
コンパイル使用メモリ 211,592 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-28 04:27:06
合計ジャッジ時間 3,218 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

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