Artifact Content
Not logged in

Artifact 38ff0d11552da11a6cb3a5a18d8e9f7bfe36970e


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class LongestSequence { public:
	int maxLength(vector <int> C)
	{
		vector<int> P, N;
		for(int i=0; i<C.size(); ++i)
			if( C[i]<0 )
				N.push_back(-C[i]);
			else
				P.push_back(+C[i]);
		sort(P.begin(), P.end());
		sort(N.begin(), N.end());
		if( P.empty() || N.empty() )
			return -1;

		int L = 0, R = 10000000;
		while( R-L > 1 ) {
			int C = (L+R)/2;
			(has_loop(N, P, C) ? R : L) = C;
		}
		return L;
	}

	bool has_loop(const vector<int>& N, const vector<int>& P, int MAX)
	{
		vector<bool> vis(MAX+1, false);
		queue<int> Q;
		Q.push(0);
		vis[0] = true;
		while( !Q.empty() ) {
			int v = Q.front();
			Q.pop();
			for(int i=0; i<N.size(); ++i)
				if( v - N[i] == 0 )
					return true;
				else if( v-N[i] > 0 && !vis[v-N[i]] )
					{vis[v-N[i]]=true; Q.push(v-N[i]);}
			for(int i=0; i<P.size(); ++i)
				if( v+P[i] <= MAX && !vis[v+P[i]] )
					{vis[v+P[i]]=true; Q.push(v+P[i]);}
		}
		return false;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, LongestSequence().maxLength(C));}
int main(){

CASE(0)
	int C_[] = {-2,3};
	  vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 
	int _ = 3; 
END
CASE(1)
	int C_[] = {524};
	  vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 
	int _ = -1; 
END
CASE(2)
	int C_[] = {1, -1};
	  vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 
	int _ = 0; 
END
CASE(3)
	int C_[] = {11,-7};
	  vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 
	int _ = 16; 
END
CASE(4)
	int C_[] = {-227,690,590,-524};
	  vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 
	int _ = 713; 
END
CASE(5)
	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};
	  vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 
	int _ = -1; 
END
CASE(6)
	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};
	  vector <int> C(C_, C_+sizeof(C_)/sizeof(*C_)); 
	int _ = -1; 
END

}
// END CUT-1000 HERE