Artifact Content
Not logged in

Artifact 7bd18a955be38deaf7f3bcccd29d14680721ab7f


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class PeriodicJumping { public:
	int minimalTime(int x, vector <int> jumpLengths)
	{
		if(x==0)
			return 0;
		if(x<0)
			x=-x;

		vector<int> js;
		js.insert(js.end(), jumpLengths.begin(), jumpLengths.end());
		js.insert(js.end(), jumpLengths.begin(), jumpLengths.end());
		const LL all_sum = accumulate(js.begin(), js.end(), 0LL);

		LL best = 1LL<<62;
		for(int i=0; i<js.size(); ++i)
		{
			LL sum = accumulate(js.begin(), js.begin()+i+1, 0LL);
			LL M = *max_element(js.begin(), js.begin()+i+1);
			LL min_repr = max(0LL, M*2-sum);

			// [min_repr, sum] in the first round.
			if(min_repr<=x && x<=sum)
				best = min<LL>(best, i+1);
			// [0, sum+K*all_sum] in the Kth round.
			LL K = max(1LL, (x-sum+all_sum-1)/all_sum);
			best = min<LL>(best, K*js.size()+i+1);
		}
		return int(best);
	}
};

// 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(_, PeriodicJumping().minimalTime(x, jumpLengths));}
int main(){

CASE(0)
	int x = 15; 
	int jumpLengths_[] = {1,2,3,4,5,6,7,8,9,10};
	  vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 
	int _ = 5; 
END
CASE(1)
	int x = 5; 
	int jumpLengths_[] = {5};
	  vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 
	int _ = 1; 
END
CASE(2)
	int x = 1; 
	int jumpLengths_[] = {10};
	  vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 
	int _ = 2; 
END
CASE(3)
	int x = -10; 
	int jumpLengths_[] = {2,3,4,500,6,7,8};
	  vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 
	int _ = 11; 
END
CASE(4)
	int x = -1000000000; 
	int jumpLengths_[] = {1};
	  vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 
	int _ = 1000000000; 
END
CASE(5)
	int x = 0; 
	int jumpLengths_[] = {19911120};
	  vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 
	int _ = 0; 
END
/*
CASE(6)
	int x = ; 
	int jumpLengths_[] = ;
	  vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 
	int _ = ; 
END
CASE(7)
	int x = ; 
	int jumpLengths_[] = ;
	  vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 
	int _ = ; 
END
*/
}
// END CUT HERE