4cc63e9faa 2012-10-11 kinaba: #include <iostream> 4cc63e9faa 2012-10-11 kinaba: #include <sstream> 4cc63e9faa 2012-10-11 kinaba: #include <iomanip> 4cc63e9faa 2012-10-11 kinaba: #include <vector> 4cc63e9faa 2012-10-11 kinaba: #include <string> 4cc63e9faa 2012-10-11 kinaba: #include <map> 4cc63e9faa 2012-10-11 kinaba: #include <set> 4cc63e9faa 2012-10-11 kinaba: #include <algorithm> 4cc63e9faa 2012-10-11 kinaba: #include <numeric> 4cc63e9faa 2012-10-11 kinaba: #include <iterator> 4cc63e9faa 2012-10-11 kinaba: #include <functional> 4cc63e9faa 2012-10-11 kinaba: #include <complex> 4cc63e9faa 2012-10-11 kinaba: #include <queue> 4cc63e9faa 2012-10-11 kinaba: #include <stack> 4cc63e9faa 2012-10-11 kinaba: #include <cmath> 4cc63e9faa 2012-10-11 kinaba: #include <cassert> 4cc63e9faa 2012-10-11 kinaba: using namespace std; 4cc63e9faa 2012-10-11 kinaba: typedef long long LL; 4cc63e9faa 2012-10-11 kinaba: typedef long double LD; 4cc63e9faa 2012-10-11 kinaba: typedef complex<LD> CMP; 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: class FoxAndMountainEasy { public: 4cc63e9faa 2012-10-11 kinaba: string possible(int n, int h0, int hn, string history) 4cc63e9faa 2012-10-11 kinaba: { 4cc63e9faa 2012-10-11 kinaba: int z = 0; 4cc63e9faa 2012-10-11 kinaba: int min_z = 0; 4cc63e9faa 2012-10-11 kinaba: for(int i=0; i<history.size(); ++i) { 4cc63e9faa 2012-10-11 kinaba: z += (history[i]=='U' ? +1 : -1); 4cc63e9faa 2012-10-11 kinaba: min_z = min(z, min_z); 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: for(int x=0; x+history.size()<=n; ++x) 4cc63e9faa 2012-10-11 kinaba: { 4cc63e9faa 2012-10-11 kinaba: int hx = h0 + x; 4cc63e9faa 2012-10-11 kinaba: if(hx+min_z < 0) 4cc63e9faa 2012-10-11 kinaba: continue; 4cc63e9faa 2012-10-11 kinaba: int hy = hx + z; 4cc63e9faa 2012-10-11 kinaba: if( possible(hn-hy, n-(x+history.size())) ) 4cc63e9faa 2012-10-11 kinaba: return "YES"; 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: return "NO"; 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: bool possible(int dh, int k) 4cc63e9faa 2012-10-11 kinaba: { 4cc63e9faa 2012-10-11 kinaba: if((k+dh)%2 != 0) 4cc63e9faa 2012-10-11 kinaba: return false; 4cc63e9faa 2012-10-11 kinaba: int u = (k+dh)/2; 4cc63e9faa 2012-10-11 kinaba: int d = k - u; 4cc63e9faa 2012-10-11 kinaba: return 0<=u && u<=k && u+d==k && u-d==dh; 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: }; 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: // BEGIN CUT HERE 4cc63e9faa 2012-10-11 kinaba: #include <ctime> 4cc63e9faa 2012-10-11 kinaba: double start_time; string timer() 4cc63e9faa 2012-10-11 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4cc63e9faa 2012-10-11 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4cc63e9faa 2012-10-11 kinaba: { os << "{ "; 4cc63e9faa 2012-10-11 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4cc63e9faa 2012-10-11 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4cc63e9faa 2012-10-11 kinaba: void verify_case(const string& Expected, const string& Received) { 4cc63e9faa 2012-10-11 kinaba: bool ok = (Expected == Received); 4cc63e9faa 2012-10-11 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4cc63e9faa 2012-10-11 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4cc63e9faa 2012-10-11 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4cc63e9faa 2012-10-11 kinaba: #define END verify_case(_, FoxAndMountainEasy().possible(n, h0, hn, history));} 4cc63e9faa 2012-10-11 kinaba: int main(){ 4cc63e9faa 2012-10-11 kinaba: 4cc63e9faa 2012-10-11 kinaba: CASE(0) 4cc63e9faa 2012-10-11 kinaba: int n = 4; 4cc63e9faa 2012-10-11 kinaba: int h0 = 0; 4cc63e9faa 2012-10-11 kinaba: int hn = 4; 4cc63e9faa 2012-10-11 kinaba: string history = "UU"; 4cc63e9faa 2012-10-11 kinaba: string _ = "YES"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(1) 4cc63e9faa 2012-10-11 kinaba: int n = 4; 4cc63e9faa 2012-10-11 kinaba: int h0 = 0; 4cc63e9faa 2012-10-11 kinaba: int hn = 4; 4cc63e9faa 2012-10-11 kinaba: string history = "D"; 4cc63e9faa 2012-10-11 kinaba: string _ = "NO"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(2) 4cc63e9faa 2012-10-11 kinaba: int n = 4; 4cc63e9faa 2012-10-11 kinaba: int h0 = 100000; 4cc63e9faa 2012-10-11 kinaba: int hn = 100000; 4cc63e9faa 2012-10-11 kinaba: string history = "DDU"; 4cc63e9faa 2012-10-11 kinaba: string _ = "YES"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(3) 4cc63e9faa 2012-10-11 kinaba: int n = 4; 4cc63e9faa 2012-10-11 kinaba: int h0 = 0; 4cc63e9faa 2012-10-11 kinaba: int hn = 0; 4cc63e9faa 2012-10-11 kinaba: string history = "DDU"; 4cc63e9faa 2012-10-11 kinaba: string _ = "NO"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(4) 4cc63e9faa 2012-10-11 kinaba: int n = 20; 4cc63e9faa 2012-10-11 kinaba: int h0 = 20; 4cc63e9faa 2012-10-11 kinaba: int hn = 20; 4cc63e9faa 2012-10-11 kinaba: string history = "UDUDUDUDUD"; 4cc63e9faa 2012-10-11 kinaba: string _ = "YES"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(5) 4cc63e9faa 2012-10-11 kinaba: int n = 20; 4cc63e9faa 2012-10-11 kinaba: int h0 = 0; 4cc63e9faa 2012-10-11 kinaba: int hn = 0; 4cc63e9faa 2012-10-11 kinaba: string history = "UUUUUUUUUU"; 4cc63e9faa 2012-10-11 kinaba: string _ = "YES"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(6) 4cc63e9faa 2012-10-11 kinaba: int n = 20; 4cc63e9faa 2012-10-11 kinaba: int h0 = 0; 4cc63e9faa 2012-10-11 kinaba: int hn = 0; 4cc63e9faa 2012-10-11 kinaba: string history = "UUUUUUUUUUU"; 4cc63e9faa 2012-10-11 kinaba: string _ = "NO"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(7) 4cc63e9faa 2012-10-11 kinaba: int n = 4; 4cc63e9faa 2012-10-11 kinaba: int h0 = 3; 4cc63e9faa 2012-10-11 kinaba: int hn = 1; 4cc63e9faa 2012-10-11 kinaba: string history = "U"; 4cc63e9faa 2012-10-11 kinaba: string _ = "YES"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(8) 4cc63e9faa 2012-10-11 kinaba: int n = 1; 4cc63e9faa 2012-10-11 kinaba: int h0 = 0; 4cc63e9faa 2012-10-11 kinaba: int hn = 1; 4cc63e9faa 2012-10-11 kinaba: string history = "U"; 4cc63e9faa 2012-10-11 kinaba: string _ = "YES"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: CASE(8) 4cc63e9faa 2012-10-11 kinaba: int n = 2; 4cc63e9faa 2012-10-11 kinaba: int h0 = 0; 4cc63e9faa 2012-10-11 kinaba: int hn = 0; 4cc63e9faa 2012-10-11 kinaba: string history = "DU"; 4cc63e9faa 2012-10-11 kinaba: string _ = "NO"; 4cc63e9faa 2012-10-11 kinaba: END 4cc63e9faa 2012-10-11 kinaba: } 4cc63e9faa 2012-10-11 kinaba: // END CUT HERE