File Annotation
Not logged in
b57cc94f1b 2011-10-08        kinaba: #include <iostream>
b57cc94f1b 2011-10-08        kinaba: #include <sstream>
b57cc94f1b 2011-10-08        kinaba: #include <iomanip>
b57cc94f1b 2011-10-08        kinaba: #include <vector>
b57cc94f1b 2011-10-08        kinaba: #include <string>
b57cc94f1b 2011-10-08        kinaba: #include <map>
b57cc94f1b 2011-10-08        kinaba: #include <set>
b57cc94f1b 2011-10-08        kinaba: #include <algorithm>
b57cc94f1b 2011-10-08        kinaba: #include <numeric>
b57cc94f1b 2011-10-08        kinaba: #include <iterator>
b57cc94f1b 2011-10-08        kinaba: #include <functional>
b57cc94f1b 2011-10-08        kinaba: #include <complex>
b57cc94f1b 2011-10-08        kinaba: #include <queue>
b57cc94f1b 2011-10-08        kinaba: #include <stack>
b57cc94f1b 2011-10-08        kinaba: #include <cmath>
b57cc94f1b 2011-10-08        kinaba: #include <cassert>
b57cc94f1b 2011-10-08        kinaba: #include <cstring>
b57cc94f1b 2011-10-08        kinaba: using namespace std;
b57cc94f1b 2011-10-08        kinaba: typedef long long LL;
b57cc94f1b 2011-10-08        kinaba: typedef complex<double> CMP;
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: template<typename T>
b57cc94f1b 2011-10-08        kinaba: class IdGen
b57cc94f1b 2011-10-08        kinaba: {
b57cc94f1b 2011-10-08        kinaba: 	map<T, int> v2id_;
b57cc94f1b 2011-10-08        kinaba: 	vector<T>   id2v_;
b57cc94f1b 2011-10-08        kinaba: public:
b57cc94f1b 2011-10-08        kinaba: 	int v2id(const T& v) {
b57cc94f1b 2011-10-08        kinaba: 		if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); }
b57cc94f1b 2011-10-08        kinaba: 		return v2id_[v];
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: 	const T& id2v(int i) const { return id2v_[i]; }
b57cc94f1b 2011-10-08        kinaba: 	int size() const { return id2v_.size(); }
b57cc94f1b 2011-10-08        kinaba: };
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: template<typename Vert>
b57cc94f1b 2011-10-08        kinaba: class SCC
b57cc94f1b 2011-10-08        kinaba: {
b57cc94f1b 2011-10-08        kinaba: public:
b57cc94f1b 2011-10-08        kinaba: 	IdGen<Vert> idgen;
b57cc94f1b 2011-10-08        kinaba: 	vector< vector<int> > G;
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: public:
b57cc94f1b 2011-10-08        kinaba: 	void addVert( Vert s_ )
b57cc94f1b 2011-10-08        kinaba: 	{
b57cc94f1b 2011-10-08        kinaba: 		int s = idgen.v2id(s_);
b57cc94f1b 2011-10-08        kinaba: 		if( s >= G.size() ) G.resize(s+1);
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 	void addEdge( Vert s_, Vert t_ )
b57cc94f1b 2011-10-08        kinaba: 	{
b57cc94f1b 2011-10-08        kinaba: 		int s = idgen.v2id(s_), t = idgen.v2id(t_);
b57cc94f1b 2011-10-08        kinaba: 		if( max(s,t) >= G.size() ) G.resize(max(s,t)+1);
b57cc94f1b 2011-10-08        kinaba: 		G[s].push_back(t);
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 	void scc()
b57cc94f1b 2011-10-08        kinaba: 	{
b57cc94f1b 2011-10-08        kinaba: 		int N = G.size();
b57cc94f1b 2011-10-08        kinaba: 		dfs_no.assign(N, 0);
b57cc94f1b 2011-10-08        kinaba: 		dfs_lo.assign(N, 0);
b57cc94f1b 2011-10-08        kinaba: 		pending = stack<int>();
b57cc94f1b 2011-10-08        kinaba: 		scc_id.assign(N, -1);
b57cc94f1b 2011-10-08        kinaba: 		scc_list.clear();
b57cc94f1b 2011-10-08        kinaba: 		scc_children.clear();
b57cc94f1b 2011-10-08        kinaba: 		for(int v=0; v<N; ++v)
b57cc94f1b 2011-10-08        kinaba: 			dfs(v);
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 	vector<int> scc_id; // which scc does the vert belong to?
b57cc94f1b 2011-10-08        kinaba: 	vector< vector<int> > scc_list;     // list of nodes in the scc
b57cc94f1b 2011-10-08        kinaba: 	vector< vector<int> > scc_children; // forest relationship of sccs
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: private:
b57cc94f1b 2011-10-08        kinaba: 	int no;
b57cc94f1b 2011-10-08        kinaba: 	vector<int> dfs_no; // 1-origin dfs-visit ID
b57cc94f1b 2011-10-08        kinaba: 	vector<int> dfs_lo; // the least ID in the vert's scc
b57cc94f1b 2011-10-08        kinaba: 	stack<int> pending; // current unclassigied verts
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 	void dfs(int v)
b57cc94f1b 2011-10-08        kinaba: 	{
b57cc94f1b 2011-10-08        kinaba: 		// visit v if not yet
b57cc94f1b 2011-10-08        kinaba: 		if( dfs_no[v] )
b57cc94f1b 2011-10-08        kinaba: 			return;
b57cc94f1b 2011-10-08        kinaba: 		dfs_no[v] = dfs_lo[v] = ++no;
b57cc94f1b 2011-10-08        kinaba: 		pending.push(v);
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 		// visit children
b57cc94f1b 2011-10-08        kinaba: 		for(int i=0; i<G[v].size(); ++i) {
b57cc94f1b 2011-10-08        kinaba: 			int u = G[v][i];
b57cc94f1b 2011-10-08        kinaba: 			dfs( u );
b57cc94f1b 2011-10-08        kinaba: 			if( scc_id[u] == -1 )
b57cc94f1b 2011-10-08        kinaba: 				dfs_lo[v] = min( dfs_lo[v], dfs_lo[u] );
b57cc94f1b 2011-10-08        kinaba: 		}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 		// when v is the representative of scc
b57cc94f1b 2011-10-08        kinaba: 		if( dfs_no[v] == dfs_lo[v] ) {
b57cc94f1b 2011-10-08        kinaba: 			vector<int> scc;
b57cc94f1b 2011-10-08        kinaba: 			for(;;) {
b57cc94f1b 2011-10-08        kinaba: 				int w = pending.top(); pending.pop();
b57cc94f1b 2011-10-08        kinaba: 				scc.push_back(w);
b57cc94f1b 2011-10-08        kinaba: 				scc_id[w] = scc_list.size();
b57cc94f1b 2011-10-08        kinaba: 				if( w == v ) break;
b57cc94f1b 2011-10-08        kinaba: 			}
b57cc94f1b 2011-10-08        kinaba: 			scc_list.push_back(scc);
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 			set<int> children;
b57cc94f1b 2011-10-08        kinaba: 			for(int j=0; j<scc.size(); ++j)
b57cc94f1b 2011-10-08        kinaba: 				for(int i=0; i<G[scc[j]].size(); ++i)
b57cc94f1b 2011-10-08        kinaba: 					children.insert( scc_id[G[scc[j]][i]] );
b57cc94f1b 2011-10-08        kinaba: 			children.erase(scc_id[v]);
b57cc94f1b 2011-10-08        kinaba: 			scc_children.push_back( vector<int>(children.begin(), children.end()) );
b57cc94f1b 2011-10-08        kinaba: 		}
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: };
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: class ImpossibleGame { public:
b57cc94f1b 2011-10-08        kinaba: 	static LL C(LL n, LL k) {
b57cc94f1b 2011-10-08        kinaba: 		LL v = 1;
b57cc94f1b 2011-10-08        kinaba: 		for(int i=1; i<=k; ++i)
b57cc94f1b 2011-10-08        kinaba: 			v = v * (n+1-i) / i;
b57cc94f1b 2011-10-08        kinaba: 		return v;
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 	static LL weight(LL a, LL b, LL c, LL d) {
b57cc94f1b 2011-10-08        kinaba: 		return C(a+b+c+d,a) * C(b+c+d,b) * C(c+d,c);
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 	long long getMinimum(int k, vector <string> before, vector <string> after)
b57cc94f1b 2011-10-08        kinaba: 	{
b57cc94f1b 2011-10-08        kinaba: 		vector< pair< vector<int>, vector<int> > > dif;
b57cc94f1b 2011-10-08        kinaba: 		for(int i=0; i<after.size(); ++i)
b57cc94f1b 2011-10-08        kinaba: 		{
b57cc94f1b 2011-10-08        kinaba: 			vector<int> be(4), af(4);
b57cc94f1b 2011-10-08        kinaba: 			for(int j=0; j<before[i].size(); ++j) be[before[i][j]-'A'] ++;
b57cc94f1b 2011-10-08        kinaba: 			for(int j=0; j< after[i].size(); ++j) af[ after[i][j]-'A'] ++;
b57cc94f1b 2011-10-08        kinaba: 			dif.push_back( make_pair(be,af) );
b57cc94f1b 2011-10-08        kinaba: 		}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 		SCC< vector<int> > G;
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 		vector<int> v(4);
b57cc94f1b 2011-10-08        kinaba: 		for(v[0]=0; v[0]<=k; ++v[0])
b57cc94f1b 2011-10-08        kinaba: 		for(v[1]=0; v[0]+v[1]<=k; ++v[1])
b57cc94f1b 2011-10-08        kinaba: 		for(v[2]=0; v[0]+v[1]+v[2]<=k; ++v[2])
b57cc94f1b 2011-10-08        kinaba: 		{
b57cc94f1b 2011-10-08        kinaba: 			v[3] = k - v[0] - v[1] - v[2];
b57cc94f1b 2011-10-08        kinaba: 			G.addVert(v);
b57cc94f1b 2011-10-08        kinaba: 			for(int i=0; i<dif.size(); ++i)
b57cc94f1b 2011-10-08        kinaba: 			{
b57cc94f1b 2011-10-08        kinaba: 				bool dame = false;
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 				vector<int> u = v;
b57cc94f1b 2011-10-08        kinaba: 				for(int k=0; k<4; ++k)
b57cc94f1b 2011-10-08        kinaba: 				{
b57cc94f1b 2011-10-08        kinaba: 					if( (u[k]-=dif[i].first[k]) < 0 )
b57cc94f1b 2011-10-08        kinaba: 						dame = true;
b57cc94f1b 2011-10-08        kinaba: 					u[k] += dif[i].second[k];
b57cc94f1b 2011-10-08        kinaba: 				}
b57cc94f1b 2011-10-08        kinaba: 				if( !dame )
b57cc94f1b 2011-10-08        kinaba: 					G.addEdge(v, u);
b57cc94f1b 2011-10-08        kinaba: 			}
b57cc94f1b 2011-10-08        kinaba: 		}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 		G.scc();
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 		LL ans = 0;
b57cc94f1b 2011-10-08        kinaba: 		for(int v=0; v<G.scc_list.size(); ++v)
b57cc94f1b 2011-10-08        kinaba: 			ans = max(ans, rec(v, G));
b57cc94f1b 2011-10-08        kinaba: 		return ans;
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: 	map<int, LL> memo;
b57cc94f1b 2011-10-08        kinaba: 	LL rec(int v, SCC< vector<int> >& G)
b57cc94f1b 2011-10-08        kinaba: 	{
b57cc94f1b 2011-10-08        kinaba: 		if( memo.count(v) )
b57cc94f1b 2011-10-08        kinaba: 			return memo[v];
b57cc94f1b 2011-10-08        kinaba: 		LL mx = 0;
b57cc94f1b 2011-10-08        kinaba: 		for(int i=0; i<G.scc_children[v].size(); ++i)
b57cc94f1b 2011-10-08        kinaba: 			mx = max(mx, rec(G.scc_children[v][i], G));
b57cc94f1b 2011-10-08        kinaba: 		for(int i=0; i<G.scc_list[v].size(); ++i) {
b57cc94f1b 2011-10-08        kinaba: 			const vector<int>& str = G.idgen.id2v(G.scc_list[v][i]);
b57cc94f1b 2011-10-08        kinaba: 			mx += weight(str[0], str[1], str[2], str[3]);
b57cc94f1b 2011-10-08        kinaba: 		}
b57cc94f1b 2011-10-08        kinaba: 		return memo[v] = mx;
b57cc94f1b 2011-10-08        kinaba: 	}
b57cc94f1b 2011-10-08        kinaba: };
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: // BEGIN CUT HERE
b57cc94f1b 2011-10-08        kinaba: #include <ctime>
b57cc94f1b 2011-10-08        kinaba: double start_time; string timer()
b57cc94f1b 2011-10-08        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
b57cc94f1b 2011-10-08        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
b57cc94f1b 2011-10-08        kinaba:  { os << "{ ";
b57cc94f1b 2011-10-08        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
b57cc94f1b 2011-10-08        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
b57cc94f1b 2011-10-08        kinaba: void verify_case(const long long& Expected, const long long& Received) {
b57cc94f1b 2011-10-08        kinaba:  bool ok = (Expected == Received);
b57cc94f1b 2011-10-08        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
b57cc94f1b 2011-10-08        kinaba:  cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
b57cc94f1b 2011-10-08        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
b57cc94f1b 2011-10-08        kinaba: #define END	 verify_case(_, ImpossibleGame().getMinimum(k, before, after));}
b57cc94f1b 2011-10-08        kinaba: int main(){
b57cc94f1b 2011-10-08        kinaba: 
b57cc94f1b 2011-10-08        kinaba: CASE(0)
b57cc94f1b 2011-10-08        kinaba: 	int k = 1;
b57cc94f1b 2011-10-08        kinaba: 	string before_[] = { "A" };
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_));
b57cc94f1b 2011-10-08        kinaba: 	string after_[] = { "B" };
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_));
b57cc94f1b 2011-10-08        kinaba: 	long long _ = 2LL;
b57cc94f1b 2011-10-08        kinaba: END
b57cc94f1b 2011-10-08        kinaba: CASE(1)
b57cc94f1b 2011-10-08        kinaba: 	int k = 2;
b57cc94f1b 2011-10-08        kinaba: 	string before_[] = { "A", "A", "D" };
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_));
b57cc94f1b 2011-10-08        kinaba: 	string after_[] = { "B", "C", "D" };
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_));
b57cc94f1b 2011-10-08        kinaba: 	long long _ = 5LL;
b57cc94f1b 2011-10-08        kinaba: END
b57cc94f1b 2011-10-08        kinaba: CASE(2)
b57cc94f1b 2011-10-08        kinaba: 	int k = 2;
b57cc94f1b 2011-10-08        kinaba: 	string before_[] = { "B", "C", "D" };
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_));
b57cc94f1b 2011-10-08        kinaba: 	string after_[] = { "C", "D", "B" };
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_));
b57cc94f1b 2011-10-08        kinaba: 	long long _ = 9LL;
b57cc94f1b 2011-10-08        kinaba: END
b57cc94f1b 2011-10-08        kinaba: CASE(3)
b57cc94f1b 2011-10-08        kinaba: 	int k = 6;
b57cc94f1b 2011-10-08        kinaba: 	string before_[] = { "AABBC", "AAAADA", "AAACA", "CABAA", "AAAAAA", "BAAAA" };
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_));
b57cc94f1b 2011-10-08        kinaba: 	string after_[] = { "AACCB", "DAAABC", "AAAAD", "ABCBA", "AABAAA", "AACAA" };
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_));
b57cc94f1b 2011-10-08        kinaba: 	long long _ = 499LL;
b57cc94f1b 2011-10-08        kinaba: END
b57cc94f1b 2011-10-08        kinaba: CASE(4)
b57cc94f1b 2011-10-08        kinaba: 	int k = 7;
b57cc94f1b 2011-10-08        kinaba: 	string before_[] = {"BDBBB", "ACBBBBC", "BBBBC", "CCACCCB", "CC", "BBBBBBD", "BBDBBAB", "DBBCBB", "DBABABB", "BBBBABB", "BBBBB", "BBABB", "BBBBBDB", "ABDC", "BDBBDCB", "BBBBBAC", "BBBDBC", "BBBBBBA", "BBBBBBB", "BACCBB", "ABBDABB", "DC", "ABBDBB", "ACBCABB"};
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_));
b57cc94f1b 2011-10-08        kinaba: 	string after_[] = {"BADBC", "ACDBCBB", "BADCB", "BBBDBBC", "BD", "BBACCBD", "BBADBDB", "BBBBCC", "BBBCBBB", "BCBDCBD", "ABBBA", "DDBAB", "BCBDBDB", "BBCB", "BBCBBBB", "BABCBDB", "BBCCBD", "BBBBBCB", "DBCBADC", "BBBACA", "ABBABBC", "DB", "DBBCBB", "BBDBBBB"};
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_));
b57cc94f1b 2011-10-08        kinaba: 	long long _ = 7112LL;
b57cc94f1b 2011-10-08        kinaba: END
b57cc94f1b 2011-10-08        kinaba: /*
b57cc94f1b 2011-10-08        kinaba: CASE(5)
b57cc94f1b 2011-10-08        kinaba: 	int k = ;
b57cc94f1b 2011-10-08        kinaba: 	string before_[] = ;
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> before(before_, before_+sizeof(before_)/sizeof(*before_));
b57cc94f1b 2011-10-08        kinaba: 	string after_[] = ;
b57cc94f1b 2011-10-08        kinaba: 	  vector <string> after(after_, after_+sizeof(after_)/sizeof(*after_));
b57cc94f1b 2011-10-08        kinaba: 	long long _ = LL;
b57cc94f1b 2011-10-08        kinaba: END
b57cc94f1b 2011-10-08        kinaba: */
b57cc94f1b 2011-10-08        kinaba: }
b57cc94f1b 2011-10-08        kinaba: // END CUT HERE