Artifact Content
Not logged in

Artifact b81dbf246dfff5e4da87f65680aefc0607ca0704


#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 ImportantSequence { public:
	int getCount(vector <int> B, string operators)
	{
		vector< pair<LL,LL> > A;
		A.push_back( make_pair(1,0) );
		for(int i=0; i<B.size(); ++i)
		{
			LL a = A.back().first;
			LL b = A.back().second;
			LL c = B[i];
			if( operators[i] == '+' ) {
				// (ax+b) + (px+q) = c
				A.push_back( make_pair(-a, c-b) );
			} else {
				// (ax+b) - (px+q) = c
				A.push_back( make_pair(a, b-c) );
			}
		}

		static const LL INF = (1LL<<62);
		LL xMin = 1;
		LL xMax = INF;
		for(int i=0; i<A.size(); ++i)
		{
			LL a = A[i].first;
			LL b = A[i].second;
			// ax + b > 0
			if( a == +1 ) {
				// x > -b
				xMin = max(xMin, -b+1);
			} else {
				// -x > -b  == x < b
				xMax = min(xMax, b-1);
			}
		}

		if( xMax == INF )
			return -1;
		return (int)max(0LL, xMax-xMin+1);
	}
};

// 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(_, ImportantSequence().getCount(B, operators));}
int main(){

CASE(0)
	int B_[] = {3, -1, 7};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string operators = "+-+"; 
	int _ = 2; 
END
CASE(1)
	int B_[] = {1};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string operators = "-"; 
	int _ = -1; 
END
CASE(2)
	int B_[] = {1};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string operators = "+"; 
	int _ = 0; 
END
CASE(3)
	int B_[] = {10};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string operators = "+"; 
	int _ = 9; 
END
CASE(4)
	int B_[] = {540, 2012, 540, 2012, 540, 2012, 540};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string operators = "-+-+-+-"; 
	int _ = 1471; 
END
CASE(5)
	int B_[] = {1000000000, 1000000000, 1000000000, 1000000000, 1000000000,
	            1000000000, 1000000000, 1000000000, 1000000000, 1000000000,
				1000000000, 1000000000, 1000000000, 1000000000, 1000000000,
	            1000000000, 1000000000, 1000000000, 1000000000, 1000000000,
				1000000000, 1000000000, 1000000000, 1000000000, 1000000000,
	            1000000000, 1000000000, 1000000000, 1000000000, 1000000000,
				1000000000};
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string operators = "------------------------------+"; 
	int _ = -1; 
END
/*
CASE(6)
	int B_[] = ;
	  vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); 
	string operators = ; 
	int _ = ; 
END
*/
}
// END CUT HERE