054a0e8c06 2014-09-26 kinaba: #include <iostream> 054a0e8c06 2014-09-26 kinaba: #include <sstream> 054a0e8c06 2014-09-26 kinaba: #include <iomanip> 054a0e8c06 2014-09-26 kinaba: #include <vector> 054a0e8c06 2014-09-26 kinaba: #include <string> 054a0e8c06 2014-09-26 kinaba: #include <map> 054a0e8c06 2014-09-26 kinaba: #include <set> 054a0e8c06 2014-09-26 kinaba: #include <algorithm> 054a0e8c06 2014-09-26 kinaba: #include <numeric> 054a0e8c06 2014-09-26 kinaba: #include <iterator> 054a0e8c06 2014-09-26 kinaba: #include <functional> 054a0e8c06 2014-09-26 kinaba: #include <complex> 054a0e8c06 2014-09-26 kinaba: #include <queue> 054a0e8c06 2014-09-26 kinaba: #include <stack> 054a0e8c06 2014-09-26 kinaba: #include <cmath> 054a0e8c06 2014-09-26 kinaba: #include <cassert> 054a0e8c06 2014-09-26 kinaba: #include <tuple> 054a0e8c06 2014-09-26 kinaba: using namespace std; 054a0e8c06 2014-09-26 kinaba: typedef long long LL; 054a0e8c06 2014-09-26 kinaba: typedef complex<double> CMP; 054a0e8c06 2014-09-26 kinaba: 054a0e8c06 2014-09-26 kinaba: class PeriodicJumping { public: 054a0e8c06 2014-09-26 kinaba: int minimalTime(int x, vector <int> jumpLengths) 054a0e8c06 2014-09-26 kinaba: { 054a0e8c06 2014-09-26 kinaba: if(x==0) 054a0e8c06 2014-09-26 kinaba: return 0; 054a0e8c06 2014-09-26 kinaba: if(x<0) 054a0e8c06 2014-09-26 kinaba: x=-x; 054a0e8c06 2014-09-26 kinaba: 054a0e8c06 2014-09-26 kinaba: vector<int> js; 054a0e8c06 2014-09-26 kinaba: js.insert(js.end(), jumpLengths.begin(), jumpLengths.end()); 054a0e8c06 2014-09-26 kinaba: js.insert(js.end(), jumpLengths.begin(), jumpLengths.end()); 054a0e8c06 2014-09-26 kinaba: const LL all_sum = accumulate(js.begin(), js.end(), 0LL); 054a0e8c06 2014-09-26 kinaba: 054a0e8c06 2014-09-26 kinaba: LL best = 1LL<<62; 054a0e8c06 2014-09-26 kinaba: for(int i=0; i<js.size(); ++i) 054a0e8c06 2014-09-26 kinaba: { 054a0e8c06 2014-09-26 kinaba: LL sum = accumulate(js.begin(), js.begin()+i+1, 0LL); 054a0e8c06 2014-09-26 kinaba: LL M = *max_element(js.begin(), js.begin()+i+1); 054a0e8c06 2014-09-26 kinaba: LL min_repr = max(0LL, M*2-sum); 054a0e8c06 2014-09-26 kinaba: 054a0e8c06 2014-09-26 kinaba: // [min_repr, sum] in the first round. 054a0e8c06 2014-09-26 kinaba: if(min_repr<=x && x<=sum) 054a0e8c06 2014-09-26 kinaba: best = min<LL>(best, i+1); 054a0e8c06 2014-09-26 kinaba: // [0, sum+K*all_sum] in the Kth round. 054a0e8c06 2014-09-26 kinaba: LL K = max(1LL, (x-sum+all_sum-1)/all_sum); 054a0e8c06 2014-09-26 kinaba: best = min<LL>(best, K*js.size()+i+1); 054a0e8c06 2014-09-26 kinaba: } 054a0e8c06 2014-09-26 kinaba: return int(best); 054a0e8c06 2014-09-26 kinaba: } 054a0e8c06 2014-09-26 kinaba: }; 054a0e8c06 2014-09-26 kinaba: 054a0e8c06 2014-09-26 kinaba: // BEGIN CUT HERE 054a0e8c06 2014-09-26 kinaba: #include <ctime> 054a0e8c06 2014-09-26 kinaba: double start_time; string timer() 054a0e8c06 2014-09-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 054a0e8c06 2014-09-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 054a0e8c06 2014-09-26 kinaba: { os << "{ "; 054a0e8c06 2014-09-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 054a0e8c06 2014-09-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 054a0e8c06 2014-09-26 kinaba: void verify_case(const int& Expected, const int& Received) { 054a0e8c06 2014-09-26 kinaba: bool ok = (Expected == Received); 054a0e8c06 2014-09-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 054a0e8c06 2014-09-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 054a0e8c06 2014-09-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 054a0e8c06 2014-09-26 kinaba: #define END verify_case(_, PeriodicJumping().minimalTime(x, jumpLengths));} 054a0e8c06 2014-09-26 kinaba: int main(){ 054a0e8c06 2014-09-26 kinaba: 054a0e8c06 2014-09-26 kinaba: CASE(0) 054a0e8c06 2014-09-26 kinaba: int x = 15; 054a0e8c06 2014-09-26 kinaba: int jumpLengths_[] = {1,2,3,4,5,6,7,8,9,10}; 054a0e8c06 2014-09-26 kinaba: vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 054a0e8c06 2014-09-26 kinaba: int _ = 5; 054a0e8c06 2014-09-26 kinaba: END 054a0e8c06 2014-09-26 kinaba: CASE(1) 054a0e8c06 2014-09-26 kinaba: int x = 5; 054a0e8c06 2014-09-26 kinaba: int jumpLengths_[] = {5}; 054a0e8c06 2014-09-26 kinaba: vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 054a0e8c06 2014-09-26 kinaba: int _ = 1; 054a0e8c06 2014-09-26 kinaba: END 054a0e8c06 2014-09-26 kinaba: CASE(2) 054a0e8c06 2014-09-26 kinaba: int x = 1; 054a0e8c06 2014-09-26 kinaba: int jumpLengths_[] = {10}; 054a0e8c06 2014-09-26 kinaba: vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 054a0e8c06 2014-09-26 kinaba: int _ = 2; 054a0e8c06 2014-09-26 kinaba: END 054a0e8c06 2014-09-26 kinaba: CASE(3) 054a0e8c06 2014-09-26 kinaba: int x = -10; 054a0e8c06 2014-09-26 kinaba: int jumpLengths_[] = {2,3,4,500,6,7,8}; 054a0e8c06 2014-09-26 kinaba: vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 054a0e8c06 2014-09-26 kinaba: int _ = 11; 054a0e8c06 2014-09-26 kinaba: END 054a0e8c06 2014-09-26 kinaba: CASE(4) 054a0e8c06 2014-09-26 kinaba: int x = -1000000000; 054a0e8c06 2014-09-26 kinaba: int jumpLengths_[] = {1}; 054a0e8c06 2014-09-26 kinaba: vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 054a0e8c06 2014-09-26 kinaba: int _ = 1000000000; 054a0e8c06 2014-09-26 kinaba: END 054a0e8c06 2014-09-26 kinaba: CASE(5) 054a0e8c06 2014-09-26 kinaba: int x = 0; 054a0e8c06 2014-09-26 kinaba: int jumpLengths_[] = {19911120}; 054a0e8c06 2014-09-26 kinaba: vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 054a0e8c06 2014-09-26 kinaba: int _ = 0; 054a0e8c06 2014-09-26 kinaba: END 054a0e8c06 2014-09-26 kinaba: /* 054a0e8c06 2014-09-26 kinaba: CASE(6) 054a0e8c06 2014-09-26 kinaba: int x = ; 054a0e8c06 2014-09-26 kinaba: int jumpLengths_[] = ; 054a0e8c06 2014-09-26 kinaba: vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 054a0e8c06 2014-09-26 kinaba: int _ = ; 054a0e8c06 2014-09-26 kinaba: END 054a0e8c06 2014-09-26 kinaba: CASE(7) 054a0e8c06 2014-09-26 kinaba: int x = ; 054a0e8c06 2014-09-26 kinaba: int jumpLengths_[] = ; 054a0e8c06 2014-09-26 kinaba: vector <int> jumpLengths(jumpLengths_, jumpLengths_+sizeof(jumpLengths_)/sizeof(*jumpLengths_)); 054a0e8c06 2014-09-26 kinaba: int _ = ; 054a0e8c06 2014-09-26 kinaba: END 054a0e8c06 2014-09-26 kinaba: */ 054a0e8c06 2014-09-26 kinaba: } 054a0e8c06 2014-09-26 kinaba: // END CUT HERE