結果

問題 No.334 門松ゲーム
ユーザー koyumeishikoyumeishi
提出日時 2016-01-15 22:49:49
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,810 bytes
コンパイル時間 768 ms
コンパイル使用メモリ 96,252 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-19 19:20:32
合計ジャッジ時間 1,272 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0