結果

問題 No.334 門松ゲーム
ユーザー t98slidert98slider
提出日時 2022-08-22 22:44:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,416 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 181,568 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 13:31:44
合計ジャッジ時間 2,821 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n;
    cin >> n;
    vector<int> dp(1 << n), a(n);
    for(auto &&v:a)cin >> v;
    vector<bool> A(1 << n);
    for(int i = 0; i < n; i++){
        for(int j = 0; j < i; j++){
            for(int k = 0; k < j; k++){
                if(a[i] != a[k]){
                    if(a[j] < a[i] && a[j] < a[k])A[(1 << i) | (1 << j) | (1 << k)] = true;
                    if(a[j] > a[i] && a[j] > a[k])A[(1 << i) | (1 << j) | (1 << k)] = true;
                }
            }
        }
    }
    for(int i = 0; i < (1 << n); i++){
        if(__builtin_popcount(i) <= 2)continue;
        vector<int> b;
        for(int j = i; j > 0; j = (j - 1) & i){
            if(A[j])b.push_back(dp[i ^ j]);
        }
        sort(b.begin(), b.end());
        int v = 0;
        while(binary_search(b.begin(), b.end(), v))v++;
        dp[i] = v;
    }
    if(dp.back() == 0){
        cout << -1 << endl;
        return 0;
    }
    int S = (1 << n) - 1;
    vector<vector<int>> b;
    for(int j = S; j > 0; j = (j - 1) & S){
        if(A[j] && dp[S ^ j] == 0){
            b.push_back({});
            for(int k = 0; k < n; k++){
                if(j >> k & 1)b.back().push_back(k);
            }
        }
    }
    sort(b.begin(), b.end());
    for(int i = 0; i < 3; i++){
        if(i)cout << " ";
        cout << b[0][i];
    }
    cout << '\n';
}
0