4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<typename T> 4fd800b3a8 2011-02-23 kinaba: struct DP5 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int N1, N2, N3, N4, N5; 4fd800b3a8 2011-02-23 kinaba: vector<T> data; 4fd800b3a8 2011-02-23 kinaba: DP5(int N1, int N2, int N3, int N4, int N5, const T& t = T()) 4fd800b3a8 2011-02-23 kinaba: : N1(N1), N2(N2), N3(N3), N4(N4), N5(N5), data(N1*N2*N3*N4*N5, t) {} 4fd800b3a8 2011-02-23 kinaba: T& operator()(int i1, int i2, int i3, int i4, int i5) 4fd800b3a8 2011-02-23 kinaba: { return data[ ((((i1*N2)+i2)*N3+i3)*N4+i4)*N5+i5 ]; } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class BoxesArrangement { public: 4fd800b3a8 2011-02-23 kinaba: int maxUntouched(string boxes) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int NA = count( boxes.begin(), boxes.end(), 'A' ); 4fd800b3a8 2011-02-23 kinaba: int NB = count( boxes.begin(), boxes.end(), 'B' ); 4fd800b3a8 2011-02-23 kinaba: int NC = count( boxes.begin(), boxes.end(), 'C' ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: DP5<int> dp(NA+1, NB+1, NC+1, 3, 3); 4fd800b3a8 2011-02-23 kinaba: for(int na=NA; na>=0; --na) 4fd800b3a8 2011-02-23 kinaba: for(int nb=NB; nb>=0; --nb) 4fd800b3a8 2011-02-23 kinaba: for(int nc=NC; nc>=0; --nc) 4fd800b3a8 2011-02-23 kinaba: for(int c=0; c<3; ++c) 4fd800b3a8 2011-02-23 kinaba: for(int l=0; l<3; ++l) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int n = na+nb+nc; 4fd800b3a8 2011-02-23 kinaba: if( n == boxes.size() ) 4fd800b3a8 2011-02-23 kinaba: continue; 4fd800b3a8 2011-02-23 kinaba: int m = -1; 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<3; ++x) { 4fd800b3a8 2011-02-23 kinaba: int nna = na+(x==0); 4fd800b3a8 2011-02-23 kinaba: int nnb = nb+(x==1); 4fd800b3a8 2011-02-23 kinaba: int nnc = nc+(x==2); 4fd800b3a8 2011-02-23 kinaba: int nl = (x==c ? l+1 : 1); 4fd800b3a8 2011-02-23 kinaba: if( nna<=NA && nnb<=NB && nnc<=NC && nl<3 && dp(nna,nnb,nnc,x,nl)>=0 ) 4fd800b3a8 2011-02-23 kinaba: m = max(m, (boxes[n]=='A'+x) + dp(nna,nnb,nnc,x,nl)); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: dp(na,nb,nc,c,l) = m; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return dp(0,0,0,0,0); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, BoxesArrangement().maxUntouched(boxes));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: string boxes = "AAABBBCCC"; 4fd800b3a8 2011-02-23 kinaba: int _ = 6; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: string boxes = "AAAAAAAACBB"; 4fd800b3a8 2011-02-23 kinaba: int _ = 7; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: string boxes = "CCCCCB"; 4fd800b3a8 2011-02-23 kinaba: int _ = -1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: string boxes = "BAACAABAACAAA"; 4fd800b3a8 2011-02-23 kinaba: int _ = 5; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: string boxes = "CBBABA"; 4fd800b3a8 2011-02-23 kinaba: int _ = 6; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE