ADDED SRM/558-U/1A.cpp Index: SRM/558-U/1A.cpp ================================================================== --- SRM/558-U/1A.cpp +++ SRM/558-U/1A.cpp @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +class Stamp { public: + int getMinimumCost(string desiredColor, int stampCost, int pushCost) + { + const int N = desiredColor.size(); + + int best = 0x7fffffff; + for(int L=1; L<=N; ++L) { + vector dp(N+1, 999); + dp[0] = 0; + for(int e=1; e<=N; ++e) { + set colors; + for(int s=e-1; s>=0; --s) { + if(desiredColor[s] != '*') + colors.insert(desiredColor[s]); + if(colors.size()<=1 && e-s>=L) { + int nPush = (e-s+L-1)/L; + dp[e] = min(dp[e], dp[s]+nPush); + } + } + } + best = min(best, L*stampCost + dp[N]*pushCost); + } + return best; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, Stamp().getMinimumCost(desiredColor, stampCost, pushCost));} +int main(){ + +CASE(0) + string desiredColor = "RRGGBB"; + int stampCost = 1; + int pushCost = 1; + int _ = 5; +END +CASE(1) + string desiredColor = "R**GB*"; + int stampCost = 1; + int pushCost = 1; + int _ = 5; +END +CASE(2) + string desiredColor = "BRRB"; + int stampCost = 2; + int pushCost = 7; + int _ = 30; +END +CASE(3) + string desiredColor = "R*RR*GG"; + int stampCost = 10; + int pushCost = 58; + int _ = 204; +END +CASE(4) + string desiredColor = "*B**B**B*BB*G*BBB**B**B*"; + int stampCost = 5; + int pushCost = 2; + int _ = 33; +END +CASE(5) + string desiredColor = "*R*RG*G*GR*RGG*G*GGR***RR*GG"; + int stampCost = 7; + int pushCost = 1; + int _ = 30; +END +/* +CASE(6) + string desiredColor = ; + int stampCost = ; + int pushCost = ; + int _ = ; +END +CASE(7) + string desiredColor = ; + int stampCost = ; + int pushCost = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/558-U/1B.cpp Index: SRM/558-U/1B.cpp ================================================================== --- SRM/558-U/1B.cpp +++ SRM/558-U/1B.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +class Ear { public: + long long getCount(vector redX, vector blueX, vector blueY) + { + vector rx, bx, by; + { + stringstream sin(accumulate(redX.begin(), redX.end(), string(""))); + for(int x; sin>>x; ) rx.push_back(x); + } + { + stringstream sin(accumulate(blueX.begin(), blueX.end(), string(""))); + for(int x; sin>>x; ) bx.push_back(x); + } + { + stringstream sin(accumulate(blueY.begin(), blueY.end(), string(""))); + for(int y; sin>>y; ) by.push_back(y); + } + int B = bx.size(); + + LL total = 0; + for(int p=0; p by[q]) + { + int px=bx[p], py=by[p], qx=bx[q], qy=by[q]; + LL a=0, b=0, c=0, d=0; + for(int i=0; i(z,px)) + ++a; + if(min(z,px)<=x && x(z,px)) + ++c; + if(max(z,px) +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const long long& Expected, const long long& 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(_, Ear().getCount(redX, blueX, blueY));} +int main(){ + +CASE(0) + string redX_[] = {"3 2 8 7"}; + vector redX(redX_, redX_+sizeof(redX_)/sizeof(*redX_)); + string blueX_[] = {"5 4"}; + vector blueX(blueX_, blueX_+sizeof(blueX_)/sizeof(*blueX_)); + string blueY_[] = {"2 4"}; + vector blueY(blueY_, blueY_+sizeof(blueY_)/sizeof(*blueY_)); + long long _ = 1LL; +END +CASE(1) + string redX_[] = {"3 2 8 7"}; + vector redX(redX_, redX_+sizeof(redX_)/sizeof(*redX_)); + string blueX_[] = {"2 8"}; + vector blueX(blueX_, blueX_+sizeof(blueX_)/sizeof(*blueX_)); + string blueY_[] = {"3 4"}; + vector blueY(blueY_, blueY_+sizeof(blueY_)/sizeof(*blueY_)); + long long _ = 0LL; +END +CASE(2) + string redX_[] = {"1 2 6 9"}; + vector redX(redX_, redX_+sizeof(redX_)/sizeof(*redX_)); + string blueX_[] = {"3 6 8 5"}; + vector blueX(blueX_, blueX_+sizeof(blueX_)/sizeof(*blueX_)); + string blueY_[] = {"1 5 4 3"}; + vector blueY(blueY_, blueY_+sizeof(blueY_)/sizeof(*blueY_)); + long long _ = 4LL; +END +CASE(3) + string redX_[] = {"10000"}; + vector redX(redX_, redX_+sizeof(redX_)/sizeof(*redX_)); + string blueX_[] = {"10000 9999"}; + vector blueX(blueX_, blueX_+sizeof(blueX_)/sizeof(*blueX_)); + string blueY_[] = {"10000 9999"}; + vector blueY(blueY_, blueY_+sizeof(blueY_)/sizeof(*blueY_)); + long long _ = 0LL; +END +CASE(4) + string redX_[] = {"100 2", "00", " 39", "9", " 800 900 9", "99"}; + vector redX(redX_, redX_+sizeof(redX_)/sizeof(*redX_)); + string blueX_[] = {"15", "0 250 ", "349"}; + vector blueX(blueX_, blueX_+sizeof(blueX_)/sizeof(*blueX_)); + string blueY_[] = {"2 3 1"}; + vector blueY(blueY_, blueY_+sizeof(blueY_)/sizeof(*blueY_)); + long long _ = 12LL; +END +CASE(5) + string redX_[] = {"1", " ", "2", " ", "3", " ", "4 5 6", " 7 8 9"}; + vector redX(redX_, redX_+sizeof(redX_)/sizeof(*redX_)); + string blueX_[] = {"4", " ", "5", " ", "6", " 7 ", "8"}; + vector blueX(blueX_, blueX_+sizeof(blueX_)/sizeof(*blueX_)); + string blueY_[] = {"1", " 2 ", "3 4", " 5"}; + vector blueY(blueY_, blueY_+sizeof(blueY_)/sizeof(*blueY_)); + long long _ = 204LL; +END +/* +CASE(6) + string redX_[] = ; + vector redX(redX_, redX_+sizeof(redX_)/sizeof(*redX_)); + string blueX_[] = ; + vector blueX(blueX_, blueX_+sizeof(blueX_)/sizeof(*blueX_)); + string blueY_[] = ; + vector blueY(blueY_, blueY_+sizeof(blueY_)/sizeof(*blueY_)); + long long _ = LL; +END +CASE(7) + string redX_[] = ; + vector redX(redX_, redX_+sizeof(redX_)/sizeof(*redX_)); + string blueX_[] = ; + vector blueX(blueX_, blueX_+sizeof(blueX_)/sizeof(*blueX_)); + string blueY_[] = ; + vector blueY(blueY_, blueY_+sizeof(blueY_)/sizeof(*blueY_)); + long long _ = LL; +END + */ +} +// END CUT HERE