ADDED SRM/503-U/1A.cpp Index: SRM/503-U/1A.cpp ================================================================== --- SRM/503-U/1A.cpp +++ SRM/503-U/1A.cpp @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ToastXToast { public: + int bake(vector undertoasted, vector overtoasted) + { + vector< pair > t_o; + for(int i=0; i o; + for(int i=0; i +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(_, ToastXToast().bake(undertoasted, overtoasted));} +int main(){ + +CASE(0) + int undertoasted_[] = {2,4}; + vector undertoasted(undertoasted_, undertoasted_+sizeof(undertoasted_)/sizeof(*undertoasted_)); + int overtoasted_[] = {5,6,3}; + vector overtoasted(overtoasted_, overtoasted_+sizeof(overtoasted_)/sizeof(*overtoasted_)); + int _ = 2; +END +CASE(1) + int undertoasted_[] = {5}; + vector undertoasted(undertoasted_, undertoasted_+sizeof(undertoasted_)/sizeof(*undertoasted_)); + int overtoasted_[] = {4}; + vector overtoasted(overtoasted_, overtoasted_+sizeof(overtoasted_)/sizeof(*overtoasted_)); + int _ = -1; +END +CASE(2) + int undertoasted_[] = {1,2,3}; + vector undertoasted(undertoasted_, undertoasted_+sizeof(undertoasted_)/sizeof(*undertoasted_)); + int overtoasted_[] = {5,6,7}; + vector overtoasted(overtoasted_, overtoasted_+sizeof(overtoasted_)/sizeof(*overtoasted_)); + int _ = 1; +END +CASE(3) + int undertoasted_[] = {1,3,5}; + vector undertoasted(undertoasted_, undertoasted_+sizeof(undertoasted_)/sizeof(*undertoasted_)); + int overtoasted_[] = {2,4,6}; + vector overtoasted(overtoasted_, overtoasted_+sizeof(overtoasted_)/sizeof(*overtoasted_)); + int _ = 2; +END +/* +CASE(4) + int undertoasted_[] = ; + vector undertoasted(undertoasted_, undertoasted_+sizeof(undertoasted_)/sizeof(*undertoasted_)); + int overtoasted_[] = ; + vector overtoasted(overtoasted_, overtoasted_+sizeof(overtoasted_)/sizeof(*overtoasted_)); + int _ = ; +END +CASE(5) + int undertoasted_[] = ; + vector undertoasted(undertoasted_, undertoasted_+sizeof(undertoasted_)/sizeof(*undertoasted_)); + int overtoasted_[] = ; + vector overtoasted(overtoasted_, overtoasted_+sizeof(overtoasted_)/sizeof(*overtoasted_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/503-U/1B.cpp Index: SRM/503-U/1B.cpp ================================================================== --- SRM/503-U/1B.cpp +++ SRM/503-U/1B.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class KingdomXCitiesandVillages { public: + double determineLength(vector cityX, vector cityY, vector villageX, vector villageY) + { + const int C = cityX.size(); + const int V = villageX.size(); + double answer = 0.0; + + for(int i=0; i d; + for(int j=0; j +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 double& Expected, const double& Received) { + bool ok = (abs(Expected - Received) < 1e-9); + 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(_, KingdomXCitiesandVillages().determineLength(cityX, cityY, villageX, villageY));} +int main(){ + +CASE(0) + int cityX_[] = {3}; + vector cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); + int cityY_[] = {0}; + vector cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); + int villageX_[] = {3,3}; + vector villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); + int villageY_[] = {2,1}; + vector villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); + double _ = 2.5; +END +CASE(1) + int cityX_[] = {1,4,7,10}; + vector cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); + int cityY_[] = {5,5,5,5}; + vector cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); + int villageX_[] = {1,4,7,10}; + vector villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); + int villageY_[] = {4,4,4,4}; + vector villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); + double _ = 4.0; +END +CASE(2) + int cityX_[] = {1,2,3}; + vector cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); + int cityY_[] = {4,4,4}; + vector cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); + int villageX_[] = {4,5,6}; + vector villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); + int villageY_[] = {4,4,4}; + vector villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); + double _ = 4.166666666666667; +END +CASE(3) +int cityX_[] = {1}; + vector cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); + int cityY_[] = {1}; + vector cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); + int villageX_[] = {2}; + vector villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); + int villageY_[] = {1}; + vector villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); + double _ = 1; +END +/* +CASE(4) + int cityX_[] = ; + vector cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); + int cityY_[] = ; + vector cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); + int villageX_[] = ; + vector villageX(villageX_, villageX_+sizeof(villageX_)/sizeof(*villageX_)); + int villageY_[] = ; + vector villageY(villageY_, villageY_+sizeof(villageY_)/sizeof(*villageY_)); + double _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/503-U/1C-U.cpp Index: SRM/503-U/1C-U.cpp ================================================================== --- SRM/503-U/1C-U.cpp +++ SRM/503-U/1C-U.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +template +struct DP2 +{ + const int N1, N2; + vector data; + DP2(int N1, int N2, const T& t = T()) + : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); } + T& operator()(int i1, int i2) + { return data[ (i1*N2)+i2 ]; } +}; + +class KingdomXEmergencyStaircase { public: + long long determineCost(string leftBuilding, string rightBuilding, vector cost) + { + const LL oo = 1LL<<50; + const int W = cost.size(); + const int L = leftBuilding.find_last_of('Y')+1; + const int R = rightBuilding.find_last_of('Y')+1; + const int H = 2*max(L,R)+W; + + LL best = oo; + DP2 dp(H+1, H+1, oo); + for(int l=0; l<=H; l+=2) + for(int r=0; r<=H; r+=2) + if( abs(l-r)<=W && W<=l+r ) + { + if( l==0 ) + { + LL cover_cost = 0; + for(int rr=r-2; rr>=0; rr-=2) + if( rightBuilding[rr/2-1] == 'Y' ) + cover_cost += cost[(r-rr)/2-1]; + dp(l,r) = cost[W-1] + cover_cost; + } + else if( r==0 ) + { + LL cover_cost = 0; + for(int ll=l-2; ll>=0; ll-=2) + if( leftBuilding[ll/2-1] == 'Y' ) + cover_cost += cost[(l-ll)/2-1]; + dp(l,r) = cost[W-1] + cover_cost; + } + else + { + const int mid = W/2 - (r-l)/2; + + LL cur_best = oo; + LL cover_cost = 0; + for(int ll=l-2; ll>=0 && ll>=r-W; ll-=2) + { + if( ll+2 != l && leftBuilding[(ll+2)/2-1]=='Y' ) + cover_cost += cost[(l-ll-2)/2 - 1]; + cur_best = min(cur_best, dp(ll,r)+cost[mid-1]+cover_cost); + } + cover_cost = 0; + for(int rr=r-2; rr>=0 && rr>=l-W; rr-=2) + { + if( rr+2 != r && rightBuilding[(rr+2)/2-1]=='Y' ) + cover_cost += cost[(r-rr-2)/2 - 1]; + cur_best = min(cur_best, dp(l,rr)+cost[W-mid-1]+cover_cost); + } + dp(l,r) = cur_best; + } + if( 2*L<=l && 2*R<=r ) + best = min(best, dp(l,r)); + } + 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 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(_, KingdomXEmergencyStaircase().determineCost(leftBuilding, rightBuilding, cost));} +int main(){ + +CASE(0) + string leftBuilding = "YNYY"; + string rightBuilding = "NNNY"; + int cost_[] = {10, 40, 18, 25}; + vector cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); + long long _ = 98LL; +END +CASE(1) + string leftBuilding = "N"; + string rightBuilding = "Y"; + int cost_[] = {1, 1000}; + vector cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); + long long _ = 1000LL; +END +CASE(2) + string leftBuilding = "NNNNNNNY"; + string rightBuilding = "NNNNNNNY"; + int cost_[] = {1, 2}; + vector cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); + long long _ = 17LL; +END +CASE(3) + string leftBuilding = "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN"; + string rightBuilding = "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNY"; + int cost_[] = {800000000,800000000,800000000,800000000,800000000,800000000,800000000,800000000}; + vector cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); + long long _ = 6400000000LL; +END +CASE(4) + string leftBuilding = "NNY"; + string rightBuilding = "NNYYY"; + int cost_[] = {10000, 10, 40, 10000, 80, 80}; + vector cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); + long long _ = 220LL; +END +CASE(5) +string leftBuilding = "YNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNY"; +string rightBuilding = "YNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNY"; + int cost_[] = {958231113, 959939939, 999999999, 954321321, 1000000000, 998998998, 987987987, 999888777, 1000000000, 1000000000}; + vector cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); + long long _ = 58212305211LL; +END +/* +CASE(6) + string leftBuilding = ; + string rightBuilding = ; + int cost_[] = ; + vector cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); + long long _ = LL; +END +*/ +} +// END CUT HERE