9b65a86a4f 2011-05-03 kinaba: #include <iostream> 9b65a86a4f 2011-05-03 kinaba: #include <sstream> 9b65a86a4f 2011-05-03 kinaba: #include <iomanip> 9b65a86a4f 2011-05-03 kinaba: #include <vector> 9b65a86a4f 2011-05-03 kinaba: #include <string> 9b65a86a4f 2011-05-03 kinaba: #include <map> 9b65a86a4f 2011-05-03 kinaba: #include <set> 9b65a86a4f 2011-05-03 kinaba: #include <algorithm> 9b65a86a4f 2011-05-03 kinaba: #include <numeric> 9b65a86a4f 2011-05-03 kinaba: #include <iterator> 9b65a86a4f 2011-05-03 kinaba: #include <functional> 9b65a86a4f 2011-05-03 kinaba: #include <complex> 9b65a86a4f 2011-05-03 kinaba: #include <queue> 9b65a86a4f 2011-05-03 kinaba: #include <stack> 9b65a86a4f 2011-05-03 kinaba: #include <cmath> 9b65a86a4f 2011-05-03 kinaba: #include <cassert> 9b65a86a4f 2011-05-03 kinaba: #include <cstring> 9b65a86a4f 2011-05-03 kinaba: using namespace std; 9b65a86a4f 2011-05-03 kinaba: typedef long long LL; 9b65a86a4f 2011-05-03 kinaba: typedef complex<double> CMP; 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: template<typename T> 9b65a86a4f 2011-05-03 kinaba: struct DP2 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: const int N1, N2; 9b65a86a4f 2011-05-03 kinaba: vector<T> data; 9b65a86a4f 2011-05-03 kinaba: DP2(int N1, int N2, const T& t = T()) 9b65a86a4f 2011-05-03 kinaba: : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); } 9b65a86a4f 2011-05-03 kinaba: T& operator()(int i1, int i2) 9b65a86a4f 2011-05-03 kinaba: { return data[ (i1*N2)+i2 ]; } 9b65a86a4f 2011-05-03 kinaba: }; 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: class KingdomXEmergencyStaircase { public: 9b65a86a4f 2011-05-03 kinaba: long long determineCost(string leftBuilding, string rightBuilding, vector <int> cost) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: const LL oo = 1LL<<50; 9b65a86a4f 2011-05-03 kinaba: const int W = cost.size(); 9b65a86a4f 2011-05-03 kinaba: const int L = leftBuilding.find_last_of('Y')+1; 9b65a86a4f 2011-05-03 kinaba: const int R = rightBuilding.find_last_of('Y')+1; 9b65a86a4f 2011-05-03 kinaba: const int H = 2*max(L,R)+W; 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: LL best = oo; 9b65a86a4f 2011-05-03 kinaba: DP2<LL> dp(H+1, H+1, oo); 9b65a86a4f 2011-05-03 kinaba: for(int l=0; l<=H; l+=2) 9b65a86a4f 2011-05-03 kinaba: for(int r=0; r<=H; r+=2) 9b65a86a4f 2011-05-03 kinaba: if( abs(l-r)<=W && W<=l+r ) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: if( l==0 ) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: LL cover_cost = 0; 9b65a86a4f 2011-05-03 kinaba: for(int rr=r-2; rr>=0; rr-=2) 9b65a86a4f 2011-05-03 kinaba: if( rightBuilding[rr/2-1] == 'Y' ) 9b65a86a4f 2011-05-03 kinaba: cover_cost += cost[(r-rr)/2-1]; 9b65a86a4f 2011-05-03 kinaba: dp(l,r) = cost[W-1] + cover_cost; 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: else if( r==0 ) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: LL cover_cost = 0; 9b65a86a4f 2011-05-03 kinaba: for(int ll=l-2; ll>=0; ll-=2) 9b65a86a4f 2011-05-03 kinaba: if( leftBuilding[ll/2-1] == 'Y' ) 9b65a86a4f 2011-05-03 kinaba: cover_cost += cost[(l-ll)/2-1]; 9b65a86a4f 2011-05-03 kinaba: dp(l,r) = cost[W-1] + cover_cost; 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: else 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: const int mid = W/2 - (r-l)/2; 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: LL cur_best = oo; 9b65a86a4f 2011-05-03 kinaba: LL cover_cost = 0; 9b65a86a4f 2011-05-03 kinaba: for(int ll=l-2; ll>=0 && ll>=r-W; ll-=2) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: if( ll+2 != l && leftBuilding[(ll+2)/2-1]=='Y' ) 9b65a86a4f 2011-05-03 kinaba: cover_cost += cost[(l-ll-2)/2 - 1]; 9b65a86a4f 2011-05-03 kinaba: cur_best = min(cur_best, dp(ll,r)+cost[mid-1]+cover_cost); 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: cover_cost = 0; 9b65a86a4f 2011-05-03 kinaba: for(int rr=r-2; rr>=0 && rr>=l-W; rr-=2) 9b65a86a4f 2011-05-03 kinaba: { 9b65a86a4f 2011-05-03 kinaba: if( rr+2 != r && rightBuilding[(rr+2)/2-1]=='Y' ) 9b65a86a4f 2011-05-03 kinaba: cover_cost += cost[(r-rr-2)/2 - 1]; 9b65a86a4f 2011-05-03 kinaba: cur_best = min(cur_best, dp(l,rr)+cost[W-mid-1]+cover_cost); 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: dp(l,r) = cur_best; 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: if( 2*L<=l && 2*R<=r ) 9b65a86a4f 2011-05-03 kinaba: best = min(best, dp(l,r)); 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: return best; 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: }; 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: // BEGIN CUT HERE 9b65a86a4f 2011-05-03 kinaba: #include <ctime> 9b65a86a4f 2011-05-03 kinaba: double start_time; string timer() 9b65a86a4f 2011-05-03 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 9b65a86a4f 2011-05-03 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 9b65a86a4f 2011-05-03 kinaba: { os << "{ "; 9b65a86a4f 2011-05-03 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 9b65a86a4f 2011-05-03 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 9b65a86a4f 2011-05-03 kinaba: void verify_case(const long long& Expected, const long long& Received) { 9b65a86a4f 2011-05-03 kinaba: bool ok = (Expected == Received); 9b65a86a4f 2011-05-03 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 9b65a86a4f 2011-05-03 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 9b65a86a4f 2011-05-03 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 9b65a86a4f 2011-05-03 kinaba: #define END verify_case(_, KingdomXEmergencyStaircase().determineCost(leftBuilding, rightBuilding, cost));} 9b65a86a4f 2011-05-03 kinaba: int main(){ 9b65a86a4f 2011-05-03 kinaba: 9b65a86a4f 2011-05-03 kinaba: CASE(0) 9b65a86a4f 2011-05-03 kinaba: string leftBuilding = "YNYY"; 9b65a86a4f 2011-05-03 kinaba: string rightBuilding = "NNNY"; 9b65a86a4f 2011-05-03 kinaba: int cost_[] = {10, 40, 18, 25}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 9b65a86a4f 2011-05-03 kinaba: long long _ = 98LL; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: CASE(1) 9b65a86a4f 2011-05-03 kinaba: string leftBuilding = "N"; 9b65a86a4f 2011-05-03 kinaba: string rightBuilding = "Y"; 9b65a86a4f 2011-05-03 kinaba: int cost_[] = {1, 1000}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 9b65a86a4f 2011-05-03 kinaba: long long _ = 1000LL; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: CASE(2) 9b65a86a4f 2011-05-03 kinaba: string leftBuilding = "NNNNNNNY"; 9b65a86a4f 2011-05-03 kinaba: string rightBuilding = "NNNNNNNY"; 9b65a86a4f 2011-05-03 kinaba: int cost_[] = {1, 2}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 9b65a86a4f 2011-05-03 kinaba: long long _ = 17LL; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: CASE(3) 9b65a86a4f 2011-05-03 kinaba: string leftBuilding = "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN"; 9b65a86a4f 2011-05-03 kinaba: string rightBuilding = "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNY"; 9b65a86a4f 2011-05-03 kinaba: int cost_[] = {800000000,800000000,800000000,800000000,800000000,800000000,800000000,800000000}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 9b65a86a4f 2011-05-03 kinaba: long long _ = 6400000000LL; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: CASE(4) 9b65a86a4f 2011-05-03 kinaba: string leftBuilding = "NNY"; 9b65a86a4f 2011-05-03 kinaba: string rightBuilding = "NNYYY"; 9b65a86a4f 2011-05-03 kinaba: int cost_[] = {10000, 10, 40, 10000, 80, 80}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 9b65a86a4f 2011-05-03 kinaba: long long _ = 220LL; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: CASE(5) 9b65a86a4f 2011-05-03 kinaba: string leftBuilding = "YNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNY"; 9b65a86a4f 2011-05-03 kinaba: string rightBuilding = "YNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNYYNYNY"; 9b65a86a4f 2011-05-03 kinaba: int cost_[] = {958231113, 959939939, 999999999, 954321321, 1000000000, 998998998, 987987987, 999888777, 1000000000, 1000000000}; 9b65a86a4f 2011-05-03 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 9b65a86a4f 2011-05-03 kinaba: long long _ = 58212305211LL; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: /* 9b65a86a4f 2011-05-03 kinaba: CASE(6) 9b65a86a4f 2011-05-03 kinaba: string leftBuilding = ; 9b65a86a4f 2011-05-03 kinaba: string rightBuilding = ; 9b65a86a4f 2011-05-03 kinaba: int cost_[] = ; 9b65a86a4f 2011-05-03 kinaba: vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 9b65a86a4f 2011-05-03 kinaba: long long _ = LL; 9b65a86a4f 2011-05-03 kinaba: END 9b65a86a4f 2011-05-03 kinaba: */ 9b65a86a4f 2011-05-03 kinaba: } 9b65a86a4f 2011-05-03 kinaba: // END CUT HERE