結果
問題 | No.334 門松ゲーム |
ユーザー | koyumeishi |
提出日時 | 2016-01-15 22:49:49 |
言語 | C++11 (gcc 11.4.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,810 bytes |
コンパイル時間 | 888 ms |
コンパイル使用メモリ | 95,644 KB |
実行使用メモリ | 4,348 KB |
最終ジャッジ日時 | 2023-10-19 23:25:57 |
合計ジャッジ時間 | 1,472 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge11 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
4,348 KB |
testcase_01 | AC | 2 ms
4,348 KB |
testcase_02 | AC | 2 ms
4,348 KB |
testcase_03 | AC | 2 ms
4,348 KB |
testcase_04 | AC | 1 ms
4,348 KB |
testcase_05 | AC | 1 ms
4,348 KB |
testcase_06 | AC | 2 ms
4,348 KB |
testcase_07 | AC | 1 ms
4,348 KB |
testcase_08 | AC | 2 ms
4,348 KB |
testcase_09 | AC | 2 ms
4,348 KB |
testcase_10 | AC | 2 ms
4,348 KB |
testcase_11 | AC | 2 ms
4,348 KB |
testcase_12 | AC | 2 ms
4,348 KB |
testcase_13 | AC | 2 ms
4,348 KB |
testcase_14 | AC | 2 ms
4,348 KB |
testcase_15 | AC | 2 ms
4,348 KB |
ソースコード
#include <iostream> #include <vector> #include <cstdio> #include <sstream> #include <map> #include <string> #include <algorithm> #include <queue> #include <cmath> #include <functional> #include <set> #include <ctime> #include <random> using namespace std; template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;} template<class T> istream& operator , (istream& is, T& val){ return is >> val;} template<class T> ostream& operator << (ostream& os, vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"\n":" ");return os;} template<class T> ostream& operator , (ostream& os, T& val){ return os << " " << val;} template<class T> ostream& operator >> (ostream& os, T& val){ return os << " " << val;} bool is_kadomatsu_sequence(vector<int> x){ if(x.size() != 3) return false; if(x[0] < x[1] && x[1] > x[2] && x[2] != x[0]) return true; if(x[0] > x[1] && x[1] < x[2] && x[2] != x[0]) return true; return false; } int main(){ int n; cin >> n; vector<int> k(n); cin >> k; vector<int> ans; vector<int> memo(1<<n, 0); function<int(int,int)> dfs = [&](int s, int t){ if(memo[s] != 0) return memo[s]; vector<int> arr = {}; int val = -t; for(int a=0; a<n; a++){ if(((s>>a)&1) == 0) continue; for(int b=a+1; b<n; b++){ if(((s>>b)&1) == 0) continue; for(int c=b+1; c<n; c++){ if(((s>>c)&1) == 0) continue; if(val == t) break; if(is_kadomatsu_sequence({k[a],k[b],k[c]})){ int tmp = dfs(s^((1<<a)|(1<<b)|(1<<c)), t*-1); if(t == tmp){ val = tmp; arr = {a,b,c}; } } } } } if(s==(1<<n)-1){ ans = arr; } return memo[s] = val; }; dfs((1<<n)-1, 1); if(memo[(1<<n)-1] == 1){ cout << ans; }else{ cout << -1; } return 0; }