Artifact Content
Not logged in

Artifact 111eb02783edc7594ebe8552e035436bd8a3c965


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class CandidatesSelection { public:
	string possible(vector <string> score, vector <int> result)
	{
		return solve(score, result) ? "Possible" : "Impossible";
	}

	bool solve(vector <string> score, vector <int> result)
	{
		const int N = score.size();
		const int S = score[0].size();

		// po[a][b] : bitmask of skill set that makes a<b.
		vector<vector<LL>> po(N, vector<LL>(N));
		for(int a=0; a<N; ++a)
		for(int b=0; b<N; ++b) if(a!=b)
		{
			LL mask = 0;
			for(int s=0; s<S; ++s)
				if(score[a][s] <= score[b][s])
					mask |= 1LL<<s;
			if(a<b)
				mask |= 1LL<<S;
			po[a][b] = mask;
		}

		vector<vector<int>> now(1, result);

		function<bool(LL,vector<vector<int>>)> rec;
		rec = [&](LL used, vector<vector<int>> obl) {
			LL pos = ~0LL;
			for(auto& oi: obl)
				for(int i=0; i+1<oi.size(); ++i)
					pos &= po[oi[i]][oi[i+1]];
			if(pos & (1LL<<S)) // original order is enough.
				return true;

			bool changed = false;
			for(int s=0; s<S; ++s) if(pos & ~used & (1LL<<s)) {
				vector<vector<int>> next;
				for(auto& oi: obl) {
					vector<int> cur;
					for(int o: oi) {
						if(!cur.empty() && score[cur.back()][s] < score[o][s]) {
							changed = true;
							if(cur.size()>=2)
								next.push_back(cur);
							cur.clear();
						}
						cur.push_back(o);
					}
					if(cur.size()>=2)
						next.push_back(cur);
				}
				obl = std::move(next);
			}
			return changed && rec(used|pos, obl);
		};
		return rec(0, now);
	}

};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, CandidatesSelection().possible(score, result));}
int main(){

CASE(0)
	string score_[] = {"CC", "AA", "BB"};
	  vector <string> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int result_[] = {1,2,0};
	  vector <int> result(result_, result_+sizeof(result_)/sizeof(*result_)); 
	string _ = "Possible"; 
END
CASE(1)
	string score_[] = {"BAB", "ABB", "AAB", "ABA"};
	  vector <string> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int result_[] = {2,0,1,3};
	  vector <int> result(result_, result_+sizeof(result_)/sizeof(*result_)); 
	string _ = "Possible"; 
END
CASE(2)
	string score_[] = {"BAB", "ABB", "AAB", "ABA"};
	  vector <string> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int result_[] = {0, 1, 3, 2};
	  vector <int> result(result_, result_+sizeof(result_)/sizeof(*result_)); 
	string _ = "Impossible"; 
END
CASE(3)
	string score_[] = {"AAA", "ZZZ"};
	  vector <string> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int result_[] = {1, 0};
	  vector <int> result(result_, result_+sizeof(result_)/sizeof(*result_)); 
	string _ = "Impossible"; 
END
CASE(4)
	string score_[] = {"ZZZ", "AAA"};
	  vector <string> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int result_[] = {0, 1};
	  vector <int> result(result_, result_+sizeof(result_)/sizeof(*result_)); 
	string _ = "Possible"; 
END
CASE(5)
	string score_[] = {"ZYYYYX","YXZYXY","ZZZZXX","XZXYYX","ZZZYYZ","ZZXXYZ","ZYZZXZ","XZYYZX"};
	  vector <string> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int result_[] = {3,7,1,0,2,5,6,4};
	  vector <int> result(result_, result_+sizeof(result_)/sizeof(*result_)); 
	string _ = "Possible"; 
END
/*
CASE(6)
	string score_[] = ;
	  vector <string> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int result_[] = ;
	  vector <int> result(result_, result_+sizeof(result_)/sizeof(*result_)); 
	string _ = ; 
END
CASE(7)
	string score_[] = ;
	  vector <string> score(score_, score_+sizeof(score_)/sizeof(*score_)); 
	int result_[] = ;
	  vector <int> result(result_, result_+sizeof(result_)/sizeof(*result_)); 
	string _ = ; 
END
*/
}
// END CUT HERE