結果

問題 No.334 門松ゲーム
ユーザー ShibuyapShibuyap
提出日時 2020-07-21 22:00:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,448 bytes
コンパイル時間 2,041 ms
コンパイル使用メモリ 200,180 KB
実行使用メモリ 5,680 KB
最終ジャッジ日時 2023-08-30 02:56:12
合計ジャッジ時間 3,357 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,544 KB
testcase_01 AC 2 ms
5,404 KB
testcase_02 AC 2 ms
5,388 KB
testcase_03 AC 2 ms
5,340 KB
testcase_04 AC 2 ms
5,336 KB
testcase_05 AC 2 ms
5,420 KB
testcase_06 AC 2 ms
5,488 KB
testcase_07 AC 2 ms
5,368 KB
testcase_08 AC 2 ms
5,368 KB
testcase_09 AC 2 ms
5,456 KB
testcase_10 AC 2 ms
5,464 KB
testcase_11 AC 2 ms
5,416 KB
testcase_12 AC 2 ms
5,680 KB
testcase_13 AC 2 ms
5,576 KB
testcase_14 AC 2 ms
5,440 KB
testcase_15 AC 2 ms
5,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

int n;
int dp[1<<20];
int keep[1<<20][3];
int a[50];

void dfs(int x){
    bitset<30> f(x);
    dp[x] = -1;
    rep(i,n){
        if(f[i]==0)continue;
        srep(j,i+1,n){
            if(f[j]==0)continue;
            srep(k,j+1,n){
                if(f[k]==0)continue;

                if(a[i]==a[j]||a[j]==a[k]||a[k]==a[i])continue;
                if(a[i]<a[j]&&a[j]<a[k])continue;
                if(a[i]>a[j]&&a[j]>a[k])continue;

                int y = x - (1<<i) - (1<<j) - (1<<k);
                if(dp[y] == 0) dfs(y);
                if(dp[y] == -1){
                    dp[x] = 1;
                    keep[x][0] = i;
                    keep[x][1] = j;
                    keep[x][2] = k;
                    break;
                }
            }
            if(dp[x] == 1)break;
        }
        if(dp[x] == 1)break;
    }
}

int main() {
    cin >> n;
    rep(i,n)cin >> a[i];

    dfs((1<<n)-1);

    if(dp[(1<<n)-1] == -1){
        cout << -1 << endl;
    }else{
        rep(j,3){
            cout << keep[(1<<n)-1][j];
            if(j == 2) cout << endl;
            else cout << ' ';
        }
    }
    return 0;
}
 
 
0