982758e5e4 2011-11-29 kinaba: #include <iostream> 982758e5e4 2011-11-29 kinaba: #include <sstream> 982758e5e4 2011-11-29 kinaba: #include <iomanip> 982758e5e4 2011-11-29 kinaba: #include <vector> 982758e5e4 2011-11-29 kinaba: #include <string> 982758e5e4 2011-11-29 kinaba: #include <map> 982758e5e4 2011-11-29 kinaba: #include <set> 982758e5e4 2011-11-29 kinaba: #include <algorithm> 982758e5e4 2011-11-29 kinaba: #include <numeric> 982758e5e4 2011-11-29 kinaba: #include <iterator> 982758e5e4 2011-11-29 kinaba: #include <functional> 982758e5e4 2011-11-29 kinaba: #include <complex> 982758e5e4 2011-11-29 kinaba: #include <queue> 982758e5e4 2011-11-29 kinaba: #include <stack> 982758e5e4 2011-11-29 kinaba: #include <cmath> 982758e5e4 2011-11-29 kinaba: #include <cassert> 982758e5e4 2011-11-29 kinaba: #include <cstring> 982758e5e4 2011-11-29 kinaba: #ifdef __GNUC__ 982758e5e4 2011-11-29 kinaba: #include <ext/hash_map> 982758e5e4 2011-11-29 kinaba: #define unordered_map __gnu_cxx::hash_map 982758e5e4 2011-11-29 kinaba: #else 982758e5e4 2011-11-29 kinaba: #include <unordered_map> 982758e5e4 2011-11-29 kinaba: #endif 982758e5e4 2011-11-29 kinaba: using namespace std; 982758e5e4 2011-11-29 kinaba: typedef long long LL; 982758e5e4 2011-11-29 kinaba: typedef complex<double> CMP; 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: class LongestSequence { public: 982758e5e4 2011-11-29 kinaba: int maxLength(vector <int> C) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: vector<int> P, N; 982758e5e4 2011-11-29 kinaba: for(int i=0; i<C.size(); ++i) 982758e5e4 2011-11-29 kinaba: if( C[i]<0 ) 982758e5e4 2011-11-29 kinaba: N.push_back(-C[i]); 982758e5e4 2011-11-29 kinaba: else 982758e5e4 2011-11-29 kinaba: P.push_back(+C[i]); 982758e5e4 2011-11-29 kinaba: sort(P.begin(), P.end()); 982758e5e4 2011-11-29 kinaba: sort(N.begin(), N.end()); 982758e5e4 2011-11-29 kinaba: if( P.empty() || N.empty() ) 982758e5e4 2011-11-29 kinaba: return -1; 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: int L = 0, R = 10000000; 982758e5e4 2011-11-29 kinaba: while( R-L > 1 ) { 982758e5e4 2011-11-29 kinaba: int C = (L+R)/2; 982758e5e4 2011-11-29 kinaba: (has_loop(N, P, C) ? R : L) = C; 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: return L; 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: bool has_loop(const vector<int>& N, const vector<int>& P, int MAX) 982758e5e4 2011-11-29 kinaba: { 982758e5e4 2011-11-29 kinaba: vector<bool> vis(MAX+1, false); 982758e5e4 2011-11-29 kinaba: queue<int> Q; 982758e5e4 2011-11-29 kinaba: Q.push(0); 982758e5e4 2011-11-29 kinaba: vis[0] = true; 982758e5e4 2011-11-29 kinaba: while( !Q.empty() ) { 982758e5e4 2011-11-29 kinaba: int v = Q.front(); 982758e5e4 2011-11-29 kinaba: Q.pop(); 982758e5e4 2011-11-29 kinaba: for(int i=0; i<N.size(); ++i) 982758e5e4 2011-11-29 kinaba: if( v - N[i] == 0 ) 982758e5e4 2011-11-29 kinaba: return true; 982758e5e4 2011-11-29 kinaba: else if( v-N[i] > 0 && !vis[v-N[i]] ) 982758e5e4 2011-11-29 kinaba: {vis[v-N[i]]=true; Q.push(v-N[i]);} 982758e5e4 2011-11-29 kinaba: for(int i=0; i<P.size(); ++i) 982758e5e4 2011-11-29 kinaba: if( v+P[i] <= MAX && !vis[v+P[i]] ) 982758e5e4 2011-11-29 kinaba: {vis[v+P[i]]=true; Q.push(v+P[i]);} 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: return false; 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: }; 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: // BEGIN CUT HERE 982758e5e4 2011-11-29 kinaba: #include <ctime> 982758e5e4 2011-11-29 kinaba: double start_time; string timer() 982758e5e4 2011-11-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 982758e5e4 2011-11-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 982758e5e4 2011-11-29 kinaba: { os << "{ "; 982758e5e4 2011-11-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 982758e5e4 2011-11-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 982758e5e4 2011-11-29 kinaba: void verify_case(const int& Expected, const int& Received) { 982758e5e4 2011-11-29 kinaba: bool ok = (Expected == Received); 982758e5e4 2011-11-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 982758e5e4 2011-11-29 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 982758e5e4 2011-11-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 982758e5e4 2011-11-29 kinaba: #define END verify_case(_, LongestSequence().maxLength(C));} 982758e5e4 2011-11-29 kinaba: int main(){ 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: CASE(0) 982758e5e4 2011-11-29 kinaba: int C_[] = {-2,3}; 982758e5e4 2011-11-29 kinaba: vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 982758e5e4 2011-11-29 kinaba: int _ = 3; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(1) 982758e5e4 2011-11-29 kinaba: int C_[] = {524}; 982758e5e4 2011-11-29 kinaba: vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 982758e5e4 2011-11-29 kinaba: int _ = -1; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(2) 982758e5e4 2011-11-29 kinaba: int C_[] = {1, -1}; 982758e5e4 2011-11-29 kinaba: vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 982758e5e4 2011-11-29 kinaba: int _ = 0; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(3) 982758e5e4 2011-11-29 kinaba: int C_[] = {11,-7}; 982758e5e4 2011-11-29 kinaba: vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 982758e5e4 2011-11-29 kinaba: int _ = 16; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(4) 982758e5e4 2011-11-29 kinaba: int C_[] = {-227,690,590,-524}; 982758e5e4 2011-11-29 kinaba: vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 982758e5e4 2011-11-29 kinaba: int _ = 713; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(5) 982758e5e4 2011-11-29 kinaba: int C_[] = {273,752,-738,-34,863,-21,-532,-824,-170,-130,206,346,836,149,-374,-807,55,-859,640,-103,266,-716,963,-274,-654,-429,-512,595,-253,-32,790,-884,-974,975,-493,-979,-548,423,-242,-443,-132,-868,-374,-854,-15,476,-950,968,740,799}; 982758e5e4 2011-11-29 kinaba: vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 982758e5e4 2011-11-29 kinaba: int _ = -1; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: CASE(6) 982758e5e4 2011-11-29 kinaba: int C_[] = {3,4,3,3,1,4,4,-5,3,1,5,5,3,2,-1,-5,-4,2,-4,2,-2,-2,1,-5,4,-3,-3,4,2,-1,2,-4,1,4,-4,-2,4,-5,0,-4,5,3,-5,1,0,0,4,5,-5,-5}; 982758e5e4 2011-11-29 kinaba: vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 982758e5e4 2011-11-29 kinaba: int _ = -1; 982758e5e4 2011-11-29 kinaba: END 982758e5e4 2011-11-29 kinaba: 982758e5e4 2011-11-29 kinaba: } 982758e5e4 2011-11-29 kinaba: // END CUT-1000 HERE