Artifact Content
Not logged in

Artifact 8cb659246b1c63d78852cd13f00ef11e75237e08


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class CrossingTheRiver { public:
	string isItEvenPossible(int waterWidth, int landWidth, vector <int> blockHeight, int depth)
	{
		return solve(waterWidth, landWidth, blockHeight, depth) ? "POSSIBLE" : "IMPOSSIBLE";
	}

	bool solve(int WW, int LW, vector<int> B, int D)
	{
		if( count(B.begin(), B.end(), D) >= WW )
			return true;

		sort(B.begin(), B.end());
		for(int lastW=D;       lastW<=D+WW;       ++lastW)
		for(int lastL=lastW-D; lastL<=lastW-D+LW; ++lastL)
			if( solve2(B, WW, D, lastW, LW, lastW-D, lastL) )
				return true;
		return false;
	}

	bool solve2(vector<int> B, int WW, int W_min, int W_max, int LW, int L_min, int L_max)
	{
		for(int h=W_min+1; h<=W_max; ++h,--WW)
			if(!erase(B, h))
				return false;
		for(int h=L_min+1; h<=L_max; ++h,--LW)
			if(!erase(B, h))
				return false;

		int both = 0;
		for(int i=0; i<B.size(); ++i)
			if( W_min<=B[i] && B[i]<=W_max && L_min<=B[i] && B[i]<=L_max )
				both++;
			else if( W_min<=B[i] && B[i]<=W_max )
				--WW;
			else if( L_min<=B[i] && B[i]<=L_max )
				--LW;
		return (max(WW,0)+max(LW,0) <= both);
	}

	bool erase(vector<int>& v, int n)
	{
		vector<int>::iterator it = find(v.begin(), v.end(), n);
		if( it == v.end() )
			return false;
		v.erase(it);
		return true;
	}
};

// 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 string& Expected, const string& 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(_, CrossingTheRiver().isItEvenPossible(waterWidth, landWidth, blockHeight, depth));}
int main(){

CASE(0)
	int waterWidth = 3; 
	int landWidth = 3; 
	int blockHeight_[] = {3,4,4,5,5, 6};
	  vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 
	int depth = 2; 
	string _ = "POSSIBLE"; 
END
CASE(1)
	int waterWidth = 3; 
	int landWidth = 3; 
	int blockHeight_[] = {3,4,4,5,6, 6};
	  vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 
	int depth = 2; 
	string _ = "IMPOSSIBLE"; 
END
CASE(2)
	int waterWidth = 5; 
	int landWidth = 2; 
	int blockHeight_[] = {4,4,4,4,4};
	  vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 
	int depth = 4; 
	string _ = "POSSIBLE"; 
END
CASE(3)
	int waterWidth = 5; 
	int landWidth = 5; 
	int blockHeight_[] = {5,5,5,5,5,5, 2,3,4,4,6, 7, 3, 2};
	  vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 
	int depth = 2; 
	string _ = "POSSIBLE"; 
END
CASE(4)
	int waterWidth = 5; 
	int landWidth = 7; 
	int blockHeight_[] = {6,6,6,6,6,6,6, 6,6,6,6,6,7,8,9,10};
	  vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 
	int depth = 5; 
	string _ = "POSSIBLE"; 
END
CASE(5)
	int waterWidth = 1; 
	int landWidth = 1; 
	int blockHeight_[] = {5,3,4};
	  vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 
	int depth = 2; 
	string _ = "IMPOSSIBLE"; 
END
CASE(6)
	int waterWidth = 3; 
	int landWidth = 1; 
	int blockHeight_[] = {2, 3, 4, 5};
	  vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 
	int depth = 2; 
	string _ = "IMPOSSIBLE";
END
/*
CASE(7)
	int waterWidth = ; 
	int landWidth = ; 
	int blockHeight_[] = ;
	  vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 
	int depth = ; 
	string _ = ; 
END
*/
}
// END CUT HERE