Artifact Content
Not logged in

Artifact 89c00d40736da67fcb98d8838a1055fc5be4d7b2


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename T>
struct DP5
{
	int N1, N2, N3, N4, N5;
	vector<T> data;
	DP5(int N1, int N2, int N3, int N4, int N5, const T& t = T())
		: N1(N1), N2(N2), N3(N3), N4(N4), N5(N5), data(N1*N2*N3*N4*N5, t) {}
	T& operator()(int i1, int i2, int i3, int i4, int i5)
		{ return data[ ((((i1*N2)+i2)*N3+i3)*N4+i4)*N5+i5 ]; }
};


class BoxesArrangement { public:
	int maxUntouched(string boxes) 
	{
		int NA = count( boxes.begin(), boxes.end(), 'A' );
		int NB = count( boxes.begin(), boxes.end(), 'B' );
		int NC = count( boxes.begin(), boxes.end(), 'C' );

		DP5<int> dp(NA+1, NB+1, NC+1, 3, 3);
		for(int na=NA; na>=0; --na)
		for(int nb=NB; nb>=0; --nb)
		for(int nc=NC; nc>=0; --nc)
		for(int c=0; c<3; ++c)
		for(int l=0; l<3; ++l)
		{
			int n = na+nb+nc;
			if( n == boxes.size() )
				continue;
			int m = -1;
			for(int x=0; x<3; ++x) {
				int nna = na+(x==0);
				int nnb = nb+(x==1);
				int nnc = nc+(x==2);
				int nl  = (x==c ? l+1 : 1);
				if( nna<=NA && nnb<=NB && nnc<=NC && nl<3 && dp(nna,nnb,nnc,x,nl)>=0 )
					m = max(m, (boxes[n]=='A'+x) + dp(nna,nnb,nnc,x,nl));
			}
			dp(na,nb,nc,c,l) = m;
		}

		return dp(0,0,0,0,0);
	}
};

// 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 int& Expected, const int& 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(_, BoxesArrangement().maxUntouched(boxes));}
int main(){

CASE(0)
	string boxes = "AAABBBCCC"; 
	int _ = 6; 
END
CASE(1)
	string boxes = "AAAAAAAACBB"; 
	int _ = 7; 
END
CASE(2)
	string boxes = "CCCCCB"; 
	int _ = -1; 
END
CASE(3)
	string boxes = "BAACAABAACAAA"; 
	int _ = 5; 
END
CASE(4)
	string boxes = "CBBABA"; 
	int _ = 6; 
END
}
// END CUT HERE