4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class BestView { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int numberOfBuildings(vector <int> heights) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int maxCnt = -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int N = heights.size(); 4fd800b3a8 2011-02-23 kinaba: for(int from=0; from<N; ++from) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int cnt = 0; 4fd800b3a8 2011-02-23 kinaba: for(int to=0; to<N; ++to) if(to != from) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: bool hidden = false; 4fd800b3a8 2011-02-23 kinaba: for(int h=min(from,to)+1; h<max(from,to); ++h) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL X1 = from; 4fd800b3a8 2011-02-23 kinaba: LL Y1 = heights[from]; 4fd800b3a8 2011-02-23 kinaba: LL X2 = to; 4fd800b3a8 2011-02-23 kinaba: LL Y2 = heights[to]; 4fd800b3a8 2011-02-23 kinaba: LL x = h; 4fd800b3a8 2011-02-23 kinaba: LL y = heights[h]; 4fd800b3a8 2011-02-23 kinaba: // hide = (Y2-Y1)/(X2-X1)*(x-X1)+Y1 <= y 4fd800b3a8 2011-02-23 kinaba: if( X2-X1 > 0 ) { 4fd800b3a8 2011-02-23 kinaba: if( (Y2-Y1)*(x-X1) + Y1*(X2-X1) <= y * (X2-X1) ) 4fd800b3a8 2011-02-23 kinaba: hidden = true; 4fd800b3a8 2011-02-23 kinaba: } else { 4fd800b3a8 2011-02-23 kinaba: if( (Y2-Y1)*(x-X1) + Y1*(X2-X1) >= y * (X2-X1) ) 4fd800b3a8 2011-02-23 kinaba: hidden = true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if(!hidden) cnt++; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: maxCnt = max(maxCnt, cnt); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return maxCnt; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {10}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, BestView().numberOfBuildings(heights)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {5,5,5,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, BestView().numberOfBuildings(heights)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {1,2,7,3,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 4; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, BestView().numberOfBuildings(heights)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {1,5,3,2,6,3,2,6,4,2,5,7,3,1,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 7; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, BestView().numberOfBuildings(heights)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {1000000000,999999999,999999998,999999997,999999996,1,2,3,4,5}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 6; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, BestView().numberOfBuildings(heights)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int heights_[] = {244645169,956664793,752259473,577152868,605440232,569378507,111664772,653430457,454612157,397154317}; 4fd800b3a8 2011-02-23 kinaba: vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 4fd800b3a8 2011-02-23 kinaba: int RetVal = 7; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, BestView().numberOfBuildings(heights)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: