結果

問題 No.334 門松ゲーム
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-05-20 06:49:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,928 bytes
コンパイル時間 1,569 ms
コンパイル使用メモリ 164,104 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 08:28:29
合計ジャッジ時間 2,318 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 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 2 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;

int N;
int a[12];
bool memo[1<<12];
bool done[1<<12];

bool isKadomatsu(int a, int b, int c) {
    if (a == b || b == c || c == a) return false;
    return (a - b) * (c - b) > 0;
}

bool dfs(int f) {
    if (done[f]) return memo[f];
    done[f] = true;

    bool ret = false;
    rep(i, N) {
        rep2(j, i + 1, N) {
            rep2(k, j + 1, N) {
                if (((f >> i) & 1) == 0) continue;
                if (((f >> j) & 1) == 0) continue;
                if (((f >> k) & 1) == 0) continue;
                if (isKadomatsu(a[i], a[j], a[k])) {
                    int nf = f;
                    nf -= (1 << i);
                    nf -= (1 << j);
                    nf -= (1 << k);
                    // 遷移先に1つでもfalseがあるなら、自身はtrue
                    ret |= !dfs(nf);
                }
            }
        }
    }

    return memo[f] = ret;
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> N;
    rep(i, N) cin >> a[i];
    if (dfs((1 << N) - 1)) {
        rep(i, N) {
            rep2(j, i + 1, N) {
                rep2(k, j + 1, N) {
                    int f = (1 << N) - 1;
                    f -= (1 << i);
                    f -= (1 << j);
                    f -= (1 << k);
                    if (!memo[f] && isKadomatsu(a[i], a[j], a[k])) {
                        cout << i << " " << j << " " << k << endl;
                        return 0;
                    }
                }
            }
        }
    } else {
        cout << -1 << endl;
    }
}
0