ddb24a372f 2012-10-19 kinaba: #include <iostream> ddb24a372f 2012-10-19 kinaba: #include <sstream> ddb24a372f 2012-10-19 kinaba: #include <iomanip> ddb24a372f 2012-10-19 kinaba: #include <vector> ddb24a372f 2012-10-19 kinaba: #include <string> ddb24a372f 2012-10-19 kinaba: #include <map> ddb24a372f 2012-10-19 kinaba: #include <set> ddb24a372f 2012-10-19 kinaba: #include <algorithm> ddb24a372f 2012-10-19 kinaba: #include <numeric> ddb24a372f 2012-10-19 kinaba: #include <iterator> ddb24a372f 2012-10-19 kinaba: #include <functional> ddb24a372f 2012-10-19 kinaba: #include <complex> ddb24a372f 2012-10-19 kinaba: #include <queue> ddb24a372f 2012-10-19 kinaba: #include <stack> ddb24a372f 2012-10-19 kinaba: #include <cmath> ddb24a372f 2012-10-19 kinaba: #include <cassert> ddb24a372f 2012-10-19 kinaba: using namespace std; ddb24a372f 2012-10-19 kinaba: typedef long long LL; ddb24a372f 2012-10-19 kinaba: typedef long double LD; ddb24a372f 2012-10-19 kinaba: typedef complex<LD> CMP; ddb24a372f 2012-10-19 kinaba: ddb24a372f 2012-10-19 kinaba: class Stamp { public: ddb24a372f 2012-10-19 kinaba: int getMinimumCost(string desiredColor, int stampCost, int pushCost) ddb24a372f 2012-10-19 kinaba: { ddb24a372f 2012-10-19 kinaba: const int N = desiredColor.size(); ddb24a372f 2012-10-19 kinaba: ddb24a372f 2012-10-19 kinaba: int best = 0x7fffffff; ddb24a372f 2012-10-19 kinaba: for(int L=1; L<=N; ++L) { ddb24a372f 2012-10-19 kinaba: vector<int> dp(N+1, 999); ddb24a372f 2012-10-19 kinaba: dp[0] = 0; ddb24a372f 2012-10-19 kinaba: for(int e=1; e<=N; ++e) { ddb24a372f 2012-10-19 kinaba: set<char> colors; ddb24a372f 2012-10-19 kinaba: for(int s=e-1; s>=0; --s) { ddb24a372f 2012-10-19 kinaba: if(desiredColor[s] != '*') ddb24a372f 2012-10-19 kinaba: colors.insert(desiredColor[s]); ddb24a372f 2012-10-19 kinaba: if(colors.size()<=1 && e-s>=L) { ddb24a372f 2012-10-19 kinaba: int nPush = (e-s+L-1)/L; ddb24a372f 2012-10-19 kinaba: dp[e] = min(dp[e], dp[s]+nPush); ddb24a372f 2012-10-19 kinaba: } ddb24a372f 2012-10-19 kinaba: } ddb24a372f 2012-10-19 kinaba: } ddb24a372f 2012-10-19 kinaba: best = min(best, L*stampCost + dp[N]*pushCost); ddb24a372f 2012-10-19 kinaba: } ddb24a372f 2012-10-19 kinaba: return best; ddb24a372f 2012-10-19 kinaba: } ddb24a372f 2012-10-19 kinaba: }; ddb24a372f 2012-10-19 kinaba: ddb24a372f 2012-10-19 kinaba: // BEGIN CUT HERE ddb24a372f 2012-10-19 kinaba: #include <ctime> ddb24a372f 2012-10-19 kinaba: double start_time; string timer() ddb24a372f 2012-10-19 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } ddb24a372f 2012-10-19 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) ddb24a372f 2012-10-19 kinaba: { os << "{ "; ddb24a372f 2012-10-19 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) ddb24a372f 2012-10-19 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } ddb24a372f 2012-10-19 kinaba: void verify_case(const int& Expected, const int& Received) { ddb24a372f 2012-10-19 kinaba: bool ok = (Expected == Received); ddb24a372f 2012-10-19 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; ddb24a372f 2012-10-19 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } ddb24a372f 2012-10-19 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); ddb24a372f 2012-10-19 kinaba: #define END verify_case(_, Stamp().getMinimumCost(desiredColor, stampCost, pushCost));} ddb24a372f 2012-10-19 kinaba: int main(){ ddb24a372f 2012-10-19 kinaba: ddb24a372f 2012-10-19 kinaba: CASE(0) ddb24a372f 2012-10-19 kinaba: string desiredColor = "RRGGBB"; ddb24a372f 2012-10-19 kinaba: int stampCost = 1; ddb24a372f 2012-10-19 kinaba: int pushCost = 1; ddb24a372f 2012-10-19 kinaba: int _ = 5; ddb24a372f 2012-10-19 kinaba: END ddb24a372f 2012-10-19 kinaba: CASE(1) ddb24a372f 2012-10-19 kinaba: string desiredColor = "R**GB*"; ddb24a372f 2012-10-19 kinaba: int stampCost = 1; ddb24a372f 2012-10-19 kinaba: int pushCost = 1; ddb24a372f 2012-10-19 kinaba: int _ = 5; ddb24a372f 2012-10-19 kinaba: END ddb24a372f 2012-10-19 kinaba: CASE(2) ddb24a372f 2012-10-19 kinaba: string desiredColor = "BRRB"; ddb24a372f 2012-10-19 kinaba: int stampCost = 2; ddb24a372f 2012-10-19 kinaba: int pushCost = 7; ddb24a372f 2012-10-19 kinaba: int _ = 30; ddb24a372f 2012-10-19 kinaba: END ddb24a372f 2012-10-19 kinaba: CASE(3) ddb24a372f 2012-10-19 kinaba: string desiredColor = "R*RR*GG"; ddb24a372f 2012-10-19 kinaba: int stampCost = 10; ddb24a372f 2012-10-19 kinaba: int pushCost = 58; ddb24a372f 2012-10-19 kinaba: int _ = 204; ddb24a372f 2012-10-19 kinaba: END ddb24a372f 2012-10-19 kinaba: CASE(4) ddb24a372f 2012-10-19 kinaba: string desiredColor = "*B**B**B*BB*G*BBB**B**B*"; ddb24a372f 2012-10-19 kinaba: int stampCost = 5; ddb24a372f 2012-10-19 kinaba: int pushCost = 2; ddb24a372f 2012-10-19 kinaba: int _ = 33; ddb24a372f 2012-10-19 kinaba: END ddb24a372f 2012-10-19 kinaba: CASE(5) ddb24a372f 2012-10-19 kinaba: string desiredColor = "*R*RG*G*GR*RGG*G*GGR***RR*GG"; ddb24a372f 2012-10-19 kinaba: int stampCost = 7; ddb24a372f 2012-10-19 kinaba: int pushCost = 1; ddb24a372f 2012-10-19 kinaba: int _ = 30; ddb24a372f 2012-10-19 kinaba: END ddb24a372f 2012-10-19 kinaba: /* ddb24a372f 2012-10-19 kinaba: CASE(6) ddb24a372f 2012-10-19 kinaba: string desiredColor = ; ddb24a372f 2012-10-19 kinaba: int stampCost = ; ddb24a372f 2012-10-19 kinaba: int pushCost = ; ddb24a372f 2012-10-19 kinaba: int _ = ; ddb24a372f 2012-10-19 kinaba: END ddb24a372f 2012-10-19 kinaba: CASE(7) ddb24a372f 2012-10-19 kinaba: string desiredColor = ; ddb24a372f 2012-10-19 kinaba: int stampCost = ; ddb24a372f 2012-10-19 kinaba: int pushCost = ; ddb24a372f 2012-10-19 kinaba: int _ = ; ddb24a372f 2012-10-19 kinaba: END ddb24a372f 2012-10-19 kinaba: */ ddb24a372f 2012-10-19 kinaba: } ddb24a372f 2012-10-19 kinaba: // END CUT HERE