5749d4c594 2012-07-09 kinaba: #include <iostream> 5749d4c594 2012-07-09 kinaba: #include <sstream> 5749d4c594 2012-07-09 kinaba: #include <iomanip> 5749d4c594 2012-07-09 kinaba: #include <vector> 5749d4c594 2012-07-09 kinaba: #include <string> 5749d4c594 2012-07-09 kinaba: #include <map> 5749d4c594 2012-07-09 kinaba: #include <set> 5749d4c594 2012-07-09 kinaba: #include <algorithm> 5749d4c594 2012-07-09 kinaba: #include <numeric> 5749d4c594 2012-07-09 kinaba: #include <iterator> 5749d4c594 2012-07-09 kinaba: #include <functional> 5749d4c594 2012-07-09 kinaba: #include <complex> 5749d4c594 2012-07-09 kinaba: #include <queue> 5749d4c594 2012-07-09 kinaba: #include <stack> 5749d4c594 2012-07-09 kinaba: #include <cmath> 5749d4c594 2012-07-09 kinaba: #include <cassert> 5749d4c594 2012-07-09 kinaba: using namespace std; 5749d4c594 2012-07-09 kinaba: typedef long long LL; 5749d4c594 2012-07-09 kinaba: typedef long double LD; 5749d4c594 2012-07-09 kinaba: typedef complex<LD> CMP; 5749d4c594 2012-07-09 kinaba: 5749d4c594 2012-07-09 kinaba: class CrossingTheRiver { public: 5749d4c594 2012-07-09 kinaba: string isItEvenPossible(int waterWidth, int landWidth, vector <int> blockHeight, int depth) 5749d4c594 2012-07-09 kinaba: { 5749d4c594 2012-07-09 kinaba: return solve(waterWidth, landWidth, blockHeight, depth) ? "POSSIBLE" : "IMPOSSIBLE"; 5749d4c594 2012-07-09 kinaba: } 5749d4c594 2012-07-09 kinaba: 5749d4c594 2012-07-09 kinaba: bool solve(int WW, int LW, vector<int> B, int D) 5749d4c594 2012-07-09 kinaba: { 5749d4c594 2012-07-09 kinaba: if( count(B.begin(), B.end(), D) >= WW ) 5749d4c594 2012-07-09 kinaba: return true; 5749d4c594 2012-07-09 kinaba: 5749d4c594 2012-07-09 kinaba: sort(B.begin(), B.end()); 5749d4c594 2012-07-09 kinaba: for(int lastW=D; lastW<=D+WW; ++lastW) 5749d4c594 2012-07-09 kinaba: for(int lastL=lastW-D; lastL<=lastW-D+LW; ++lastL) 5749d4c594 2012-07-09 kinaba: if( solve2(B, WW, D, lastW, LW, lastW-D, lastL) ) 5749d4c594 2012-07-09 kinaba: return true; 5749d4c594 2012-07-09 kinaba: return false; 5749d4c594 2012-07-09 kinaba: } 5749d4c594 2012-07-09 kinaba: 5749d4c594 2012-07-09 kinaba: bool solve2(vector<int> B, int WW, int W_min, int W_max, int LW, int L_min, int L_max) 5749d4c594 2012-07-09 kinaba: { 5749d4c594 2012-07-09 kinaba: for(int h=W_min+1; h<=W_max; ++h,--WW) 5749d4c594 2012-07-09 kinaba: if(!erase(B, h)) 5749d4c594 2012-07-09 kinaba: return false; 5749d4c594 2012-07-09 kinaba: for(int h=L_min+1; h<=L_max; ++h,--LW) 5749d4c594 2012-07-09 kinaba: if(!erase(B, h)) 5749d4c594 2012-07-09 kinaba: return false; 5749d4c594 2012-07-09 kinaba: 5749d4c594 2012-07-09 kinaba: int both = 0; 5749d4c594 2012-07-09 kinaba: for(int i=0; i<B.size(); ++i) 5749d4c594 2012-07-09 kinaba: if( W_min<=B[i] && B[i]<=W_max && L_min<=B[i] && B[i]<=L_max ) 5749d4c594 2012-07-09 kinaba: both++; 5749d4c594 2012-07-09 kinaba: else if( W_min<=B[i] && B[i]<=W_max ) 5749d4c594 2012-07-09 kinaba: --WW; 5749d4c594 2012-07-09 kinaba: else if( L_min<=B[i] && B[i]<=L_max ) 5749d4c594 2012-07-09 kinaba: --LW; 5749d4c594 2012-07-09 kinaba: return (max(WW,0)+max(LW,0) <= both); 5749d4c594 2012-07-09 kinaba: } 5749d4c594 2012-07-09 kinaba: 5749d4c594 2012-07-09 kinaba: bool erase(vector<int>& v, int n) 5749d4c594 2012-07-09 kinaba: { 5749d4c594 2012-07-09 kinaba: vector<int>::iterator it = find(v.begin(), v.end(), n); 5749d4c594 2012-07-09 kinaba: if( it == v.end() ) 5749d4c594 2012-07-09 kinaba: return false; 5749d4c594 2012-07-09 kinaba: v.erase(it); 5749d4c594 2012-07-09 kinaba: return true; 5749d4c594 2012-07-09 kinaba: } 5749d4c594 2012-07-09 kinaba: }; 5749d4c594 2012-07-09 kinaba: 5749d4c594 2012-07-09 kinaba: // BEGIN CUT HERE 5749d4c594 2012-07-09 kinaba: #include <ctime> 5749d4c594 2012-07-09 kinaba: double start_time; string timer() 5749d4c594 2012-07-09 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 5749d4c594 2012-07-09 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 5749d4c594 2012-07-09 kinaba: { os << "{ "; 5749d4c594 2012-07-09 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 5749d4c594 2012-07-09 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 5749d4c594 2012-07-09 kinaba: void verify_case(const string& Expected, const string& Received) { 5749d4c594 2012-07-09 kinaba: bool ok = (Expected == Received); 5749d4c594 2012-07-09 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 5749d4c594 2012-07-09 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 5749d4c594 2012-07-09 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 5749d4c594 2012-07-09 kinaba: #define END verify_case(_, CrossingTheRiver().isItEvenPossible(waterWidth, landWidth, blockHeight, depth));} 5749d4c594 2012-07-09 kinaba: int main(){ 5749d4c594 2012-07-09 kinaba: 5749d4c594 2012-07-09 kinaba: CASE(0) 5749d4c594 2012-07-09 kinaba: int waterWidth = 3; 5749d4c594 2012-07-09 kinaba: int landWidth = 3; 5749d4c594 2012-07-09 kinaba: int blockHeight_[] = {3,4,4,5,5, 6}; 5749d4c594 2012-07-09 kinaba: vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 5749d4c594 2012-07-09 kinaba: int depth = 2; 5749d4c594 2012-07-09 kinaba: string _ = "POSSIBLE"; 5749d4c594 2012-07-09 kinaba: END 5749d4c594 2012-07-09 kinaba: CASE(1) 5749d4c594 2012-07-09 kinaba: int waterWidth = 3; 5749d4c594 2012-07-09 kinaba: int landWidth = 3; 5749d4c594 2012-07-09 kinaba: int blockHeight_[] = {3,4,4,5,6, 6}; 5749d4c594 2012-07-09 kinaba: vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 5749d4c594 2012-07-09 kinaba: int depth = 2; 5749d4c594 2012-07-09 kinaba: string _ = "IMPOSSIBLE"; 5749d4c594 2012-07-09 kinaba: END 5749d4c594 2012-07-09 kinaba: CASE(2) 5749d4c594 2012-07-09 kinaba: int waterWidth = 5; 5749d4c594 2012-07-09 kinaba: int landWidth = 2; 5749d4c594 2012-07-09 kinaba: int blockHeight_[] = {4,4,4,4,4}; 5749d4c594 2012-07-09 kinaba: vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 5749d4c594 2012-07-09 kinaba: int depth = 4; 5749d4c594 2012-07-09 kinaba: string _ = "POSSIBLE"; 5749d4c594 2012-07-09 kinaba: END 5749d4c594 2012-07-09 kinaba: CASE(3) 5749d4c594 2012-07-09 kinaba: int waterWidth = 5; 5749d4c594 2012-07-09 kinaba: int landWidth = 5; 5749d4c594 2012-07-09 kinaba: int blockHeight_[] = {5,5,5,5,5,5, 2,3,4,4,6, 7, 3, 2}; 5749d4c594 2012-07-09 kinaba: vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 5749d4c594 2012-07-09 kinaba: int depth = 2; 5749d4c594 2012-07-09 kinaba: string _ = "POSSIBLE"; 5749d4c594 2012-07-09 kinaba: END 5749d4c594 2012-07-09 kinaba: CASE(4) 5749d4c594 2012-07-09 kinaba: int waterWidth = 5; 5749d4c594 2012-07-09 kinaba: int landWidth = 7; 5749d4c594 2012-07-09 kinaba: int blockHeight_[] = {6,6,6,6,6,6,6, 6,6,6,6,6,7,8,9,10}; 5749d4c594 2012-07-09 kinaba: vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 5749d4c594 2012-07-09 kinaba: int depth = 5; 5749d4c594 2012-07-09 kinaba: string _ = "POSSIBLE"; 5749d4c594 2012-07-09 kinaba: END 5749d4c594 2012-07-09 kinaba: CASE(5) 5749d4c594 2012-07-09 kinaba: int waterWidth = 1; 5749d4c594 2012-07-09 kinaba: int landWidth = 1; 5749d4c594 2012-07-09 kinaba: int blockHeight_[] = {5,3,4}; 5749d4c594 2012-07-09 kinaba: vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 5749d4c594 2012-07-09 kinaba: int depth = 2; 5749d4c594 2012-07-09 kinaba: string _ = "IMPOSSIBLE"; 5749d4c594 2012-07-09 kinaba: END 5749d4c594 2012-07-09 kinaba: CASE(6) 5749d4c594 2012-07-09 kinaba: int waterWidth = 3; 5749d4c594 2012-07-09 kinaba: int landWidth = 1; 5749d4c594 2012-07-09 kinaba: int blockHeight_[] = {2, 3, 4, 5}; 5749d4c594 2012-07-09 kinaba: vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 5749d4c594 2012-07-09 kinaba: int depth = 2; 5749d4c594 2012-07-09 kinaba: string _ = "IMPOSSIBLE"; 5749d4c594 2012-07-09 kinaba: END 5749d4c594 2012-07-09 kinaba: /* 5749d4c594 2012-07-09 kinaba: CASE(7) 5749d4c594 2012-07-09 kinaba: int waterWidth = ; 5749d4c594 2012-07-09 kinaba: int landWidth = ; 5749d4c594 2012-07-09 kinaba: int blockHeight_[] = ; 5749d4c594 2012-07-09 kinaba: vector <int> blockHeight(blockHeight_, blockHeight_+sizeof(blockHeight_)/sizeof(*blockHeight_)); 5749d4c594 2012-07-09 kinaba: int depth = ; 5749d4c594 2012-07-09 kinaba: string _ = ; 5749d4c594 2012-07-09 kinaba: END 5749d4c594 2012-07-09 kinaba: */ 5749d4c594 2012-07-09 kinaba: } 5749d4c594 2012-07-09 kinaba: // END CUT HERE