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: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<typename T> 4fd800b3a8 2011-02-23 kinaba: struct RMQ 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > rm; 4fd800b3a8 2011-02-23 kinaba: vector<T> d; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: RMQ( const vector<T>& d ) : d(d) { 4fd800b3a8 2011-02-23 kinaba: int n = d.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // rm[k][x] = z s.t. d[z] is the minimum in [x, x+2^k) 4fd800b3a8 2011-02-23 kinaba: rm.push_back( vector<int>(n) ); 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x<n; ++x) 4fd800b3a8 2011-02-23 kinaba: rm[0][x] = x; 4fd800b3a8 2011-02-23 kinaba: for(int k=1; (1<<k)<=n; ++k) { 4fd800b3a8 2011-02-23 kinaba: rm.push_back( rm[k-1] ); 4fd800b3a8 2011-02-23 kinaba: for(int x=0; x+(1<<k-1)<n; ++x) 4fd800b3a8 2011-02-23 kinaba: if( d[rm[k][x]] > d[rm[k-1][x + (1<<k-1)]] ) 4fd800b3a8 2011-02-23 kinaba: rm[k][x] = rm[k-1][x + (1<<k-1)]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int operator()(int L, int R) const { // inclusive 4fd800b3a8 2011-02-23 kinaba: int k=0; 4fd800b3a8 2011-02-23 kinaba: for(; L+(1<<k) < R-(1<<k)+1; ++k) {} 4fd800b3a8 2011-02-23 kinaba: LL i = rm[k][L]; 4fd800b3a8 2011-02-23 kinaba: LL j = rm[k][R-(1<<k)+1]; 4fd800b3a8 2011-02-23 kinaba: return (d[i]<=d[j] ? i : j); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class BuildingAdvertise 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: long long getMaxArea(vector <int> hseed, int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // input 4fd800b3a8 2011-02-23 kinaba: LL j = 0; 4fd800b3a8 2011-02-23 kinaba: LL m = hseed.size(); 4fd800b3a8 2011-02-23 kinaba: vector<LL> h(n); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) { 4fd800b3a8 2011-02-23 kinaba: h[i] = hseed[j]; 4fd800b3a8 2011-02-23 kinaba: LL s = (j+1)%m; 4fd800b3a8 2011-02-23 kinaba: hseed[j] = ( ( hseed[j] ^ hseed[s] ) + 13 ) % 835454957; 4fd800b3a8 2011-02-23 kinaba: j = s; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // solve 4fd800b3a8 2011-02-23 kinaba: return rec(RMQ<LL>(h), h, 0, n-1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL rec(const RMQ<LL>& rmq, vector<LL>& h, LL L, LL R) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL result = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: stack< pair<LL,LL> > S; 4fd800b3a8 2011-02-23 kinaba: S.push( make_pair(L,R) ); 4fd800b3a8 2011-02-23 kinaba: while( !S.empty() ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: L = S.top().first; 4fd800b3a8 2011-02-23 kinaba: R = S.top().second; 4fd800b3a8 2011-02-23 kinaba: S.pop(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL C = rmq(L, R); 4fd800b3a8 2011-02-23 kinaba: if( L < C ) S.push( make_pair(L,C-1) ); 4fd800b3a8 2011-02-23 kinaba: if( C < R ) S.push( make_pair(C+1,R) ); 4fd800b3a8 2011-02-23 kinaba: result = max(result, (R-L+1) * h[C]); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return result; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } 4fd800b3a8 2011-02-23 kinaba: private: 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: void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: void test_case_0() { int Arr0[] = {3,6,5,6,2,4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; long long Arg2 = 15LL; verify_case(0, Arg2, getMaxArea(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { int Arr0[] = {5,0,7,0,2,6,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; long long Arg2 = 7LL; verify_case(1, Arg2, getMaxArea(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { int Arr0[] = {1048589,2097165}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100000; long long Arg2 = 104858900000LL; verify_case(2, Arg2, getMaxArea(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_3() { int Arr0[] = {1,7,2,5,3,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; long long Arg2 = 8LL; verify_case(3, Arg2, getMaxArea(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { BuildingAdvertise().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE