結果

問題 No.334 門松ゲーム
ユーザー nenuonnenuon
提出日時 2017-07-01 18:27:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,646 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 96,684 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 08:04:24
合計ジャッジ時間 1,619 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
typedef long long ll;

// bit でとったとってないを表現
// その状態をsとする

int N;
int K[12];

bool reg[1<<12];
tuple<int, int, int> dp[1<<12]; // 1手目
const int inf = 1e9;

bool kadomatsu(int x, int y, int z) {
  if(x == y || y == z || z == x) return false;
  if(x > y && y < z) return true;
  if(x < y && y > z) return true;
  return false;
}

tuple<int, int, int> f(int s) {
  if(reg[s]) return dp[s];

  tuple<int, int, int> ret {inf, inf, inf};
  FOR(i,0,N) FOR(j,i+1,N) FOR(k,j+1,N) {
    if (s & (1 << i)) continue; // 取られていたらcontinue
    if (s & (1 << j)) continue;
    if (s & (1 << k)) continue;
    if (kadomatsu(K[i], K[j], K[k])) {
      auto val = f(s | (1 << i) | (1 << j) | (1 << k));
      if(get<0>(val) == inf){
        chmin(ret, make_tuple(i, j, k));
      }
    }
  }
  reg[s] = true;
  dp[s] = ret;
  return ret;
}

int main(){
  cin >> N;
  FOR(i,0,N) cin >> K[i];
  FOR(i,0,1<<12) reg[i] = false;
  auto ans = f(0);
  if(get<0>(ans) == inf){
    cout << -1 << endl;
  } else {
    int x, y, z;
    tie(x, y, z) = ans;
    printf("%d %d %d\n", x, y, z);
  }
  return 0;
}
0