Artifact Content
Not logged in

Artifact 852235c2e0a91bd672810bd7fa256379fc7bcde8


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename T>
struct DP2
{
	const int N1, N2;
	vector<T> 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 <int> 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<LL> 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 <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::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 <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 
	long long _ = 98LL; 
END
CASE(1)
	string leftBuilding = "N"; 
	string rightBuilding = "Y"; 
	int cost_[] = {1, 1000};
	  vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 
	long long _ = 1000LL; 
END
CASE(2)
	string leftBuilding = "NNNNNNNY"; 
	string rightBuilding = "NNNNNNNY"; 
	int cost_[] = {1, 2};
	  vector <int> 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 <int> 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 <int> 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 <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 
	long long _ = 58212305211LL; 
END
/*
CASE(6)
	string leftBuilding = ; 
	string rightBuilding = ; 
	int cost_[] = ;
	  vector <int> cost(cost_, cost_+sizeof(cost_)/sizeof(*cost_)); 
	long long _ = LL; 
END
*/
}
// END CUT HERE