8cbd8685a8 2019-07-19 kinaba: #include <iostream> 8cbd8685a8 2019-07-19 kinaba: #include <sstream> 8cbd8685a8 2019-07-19 kinaba: #include <iomanip> 8cbd8685a8 2019-07-19 kinaba: #include <vector> 8cbd8685a8 2019-07-19 kinaba: #include <string> 8cbd8685a8 2019-07-19 kinaba: #include <map> 8cbd8685a8 2019-07-19 kinaba: #include <set> 8cbd8685a8 2019-07-19 kinaba: #include <algorithm> 8cbd8685a8 2019-07-19 kinaba: #include <numeric> 8cbd8685a8 2019-07-19 kinaba: #include <iterator> 8cbd8685a8 2019-07-19 kinaba: #include <functional> 8cbd8685a8 2019-07-19 kinaba: #include <complex> 8cbd8685a8 2019-07-19 kinaba: #include <queue> 8cbd8685a8 2019-07-19 kinaba: #include <stack> 8cbd8685a8 2019-07-19 kinaba: #include <cmath> 8cbd8685a8 2019-07-19 kinaba: #include <cassert> 8cbd8685a8 2019-07-19 kinaba: #include <tuple> 8cbd8685a8 2019-07-19 kinaba: using namespace std; 8cbd8685a8 2019-07-19 kinaba: typedef long long LL; 8cbd8685a8 2019-07-19 kinaba: typedef complex<double> CMP; 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: class TwoLadders { public: 8cbd8685a8 2019-07-19 kinaba: long long minSteps(int sx, int lx1, int lx2, vector <int> X, vector <int> Y) 8cbd8685a8 2019-07-19 kinaba: { 8cbd8685a8 2019-07-19 kinaba: map<int, vector<int>> y2x; 8cbd8685a8 2019-07-19 kinaba: for (int i = 0; i < X.size(); ++i) 8cbd8685a8 2019-07-19 kinaba: y2x[Y[i]].push_back(X[i]); 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: map<int, pair<int, int>> y2mM; 8cbd8685a8 2019-07-19 kinaba: for (auto kv : y2x) 8cbd8685a8 2019-07-19 kinaba: y2mM[kv.first] = make_pair( 8cbd8685a8 2019-07-19 kinaba: *min_element(kv.second.begin(), kv.second.end()), 8cbd8685a8 2019-07-19 kinaba: *max_element(kv.second.begin(), kv.second.end()) 8cbd8685a8 2019-07-19 kinaba: ); 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: auto cur = y2mM.begin(); 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: // best 2 move candidates in the prvious level 8cbd8685a8 2019-07-19 kinaba: int p1 = sx; 8cbd8685a8 2019-07-19 kinaba: int p2 = sx; 8cbd8685a8 2019-07-19 kinaba: LL b1 = 0; 8cbd8685a8 2019-07-19 kinaba: LL b2 = 0; 8cbd8685a8 2019-07-19 kinaba: int yy = 0; 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: if(cur->first == 0) { 8cbd8685a8 2019-07-19 kinaba: int m = cur->second.first; 8cbd8685a8 2019-07-19 kinaba: int M = cur->second.second; 8cbd8685a8 2019-07-19 kinaba: ++cur; 8cbd8685a8 2019-07-19 kinaba: p1 = m; 8cbd8685a8 2019-07-19 kinaba: b1 = abs(sx - M) + abs(M - m); 8cbd8685a8 2019-07-19 kinaba: p2 = M; 8cbd8685a8 2019-07-19 kinaba: b2 = abs(sx - m) + abs(M - m); 8cbd8685a8 2019-07-19 kinaba: } 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: for (; cur != y2mM.end(); ++cur) { 8cbd8685a8 2019-07-19 kinaba: int ydiff = cur->first - yy; 8cbd8685a8 2019-07-19 kinaba: yy = cur->first; 8cbd8685a8 2019-07-19 kinaba: int m = cur->second.first; 8cbd8685a8 2019-07-19 kinaba: int M = cur->second.second; 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: int prev_p1 = p1; 8cbd8685a8 2019-07-19 kinaba: int prev_p2 = p2; 8cbd8685a8 2019-07-19 kinaba: LL prev_b1 = b1; 8cbd8685a8 2019-07-19 kinaba: LL prev_b2 = b2; 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: p1 = m; 8cbd8685a8 2019-07-19 kinaba: b1 = min({ 8cbd8685a8 2019-07-19 kinaba: prev_b1 + ydiff + abs(prev_p1 - lx1) + abs(lx1 - M) + abs(M - m), 8cbd8685a8 2019-07-19 kinaba: prev_b1 + ydiff + abs(prev_p1 - lx2) + abs(lx2 - M) + abs(M - m), 8cbd8685a8 2019-07-19 kinaba: prev_b2 + ydiff + abs(prev_p2 - lx1) + abs(lx1 - M) + abs(M - m), 8cbd8685a8 2019-07-19 kinaba: prev_b2 + ydiff + abs(prev_p2 - lx2) + abs(lx2 - M) + abs(M - m), 8cbd8685a8 2019-07-19 kinaba: }); 8cbd8685a8 2019-07-19 kinaba: p2 = M; 8cbd8685a8 2019-07-19 kinaba: b2 = min({ 8cbd8685a8 2019-07-19 kinaba: prev_b1 + ydiff + abs(prev_p1 - lx1) + abs(lx1 - m) + abs(M - m), 8cbd8685a8 2019-07-19 kinaba: prev_b1 + ydiff + abs(prev_p1 - lx2) + abs(lx2 - m) + abs(M - m), 8cbd8685a8 2019-07-19 kinaba: prev_b2 + ydiff + abs(prev_p2 - lx1) + abs(lx1 - m) + abs(M - m), 8cbd8685a8 2019-07-19 kinaba: prev_b2 + ydiff + abs(prev_p2 - lx2) + abs(lx2 - m) + abs(M - m), 8cbd8685a8 2019-07-19 kinaba: }); 8cbd8685a8 2019-07-19 kinaba: } 8cbd8685a8 2019-07-19 kinaba: return min(b1, b2); 8cbd8685a8 2019-07-19 kinaba: } 8cbd8685a8 2019-07-19 kinaba: }; 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: // BEGIN CUT HERE 8cbd8685a8 2019-07-19 kinaba: #include <ctime> 8cbd8685a8 2019-07-19 kinaba: double start_time; string timer() 8cbd8685a8 2019-07-19 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 8cbd8685a8 2019-07-19 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 8cbd8685a8 2019-07-19 kinaba: { os << "{ "; 8cbd8685a8 2019-07-19 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 8cbd8685a8 2019-07-19 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 8cbd8685a8 2019-07-19 kinaba: void verify_case(const long long& Expected, const long long& Received) { 8cbd8685a8 2019-07-19 kinaba: bool ok = (Expected == Received); 8cbd8685a8 2019-07-19 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 8cbd8685a8 2019-07-19 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 8cbd8685a8 2019-07-19 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 8cbd8685a8 2019-07-19 kinaba: #define END verify_case(_, TwoLadders().minSteps(sx, lx1, lx2, X, Y));} 8cbd8685a8 2019-07-19 kinaba: int main(){ 8cbd8685a8 2019-07-19 kinaba: 8cbd8685a8 2019-07-19 kinaba: CASE(0) 8cbd8685a8 2019-07-19 kinaba: int sx = 10; 8cbd8685a8 2019-07-19 kinaba: int lx1 = 0; 8cbd8685a8 2019-07-19 kinaba: int lx2 = 100; 8cbd8685a8 2019-07-19 kinaba: int X_[] = {47, 42}; 8cbd8685a8 2019-07-19 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 8cbd8685a8 2019-07-19 kinaba: int Y_[] = {0, 0}; 8cbd8685a8 2019-07-19 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 8cbd8685a8 2019-07-19 kinaba: long long _ = 37LL; 8cbd8685a8 2019-07-19 kinaba: END 8cbd8685a8 2019-07-19 kinaba: CASE(1) 8cbd8685a8 2019-07-19 kinaba: int sx = 10; 8cbd8685a8 2019-07-19 kinaba: int lx1 = 8; 8cbd8685a8 2019-07-19 kinaba: int lx2 = 11; 8cbd8685a8 2019-07-19 kinaba: int X_[] = {10, 12}; 8cbd8685a8 2019-07-19 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 8cbd8685a8 2019-07-19 kinaba: int Y_[] = {1, 1}; 8cbd8685a8 2019-07-19 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 8cbd8685a8 2019-07-19 kinaba: long long _ = 5LL; 8cbd8685a8 2019-07-19 kinaba: END 8cbd8685a8 2019-07-19 kinaba: CASE(2) 8cbd8685a8 2019-07-19 kinaba: int sx = 10; 8cbd8685a8 2019-07-19 kinaba: int lx1 = 8; 8cbd8685a8 2019-07-19 kinaba: int lx2 = 42; 8cbd8685a8 2019-07-19 kinaba: int X_[] = {10, 12}; 8cbd8685a8 2019-07-19 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 8cbd8685a8 2019-07-19 kinaba: int Y_[] = {1, 1}; 8cbd8685a8 2019-07-19 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 8cbd8685a8 2019-07-19 kinaba: long long _ = 7LL; 8cbd8685a8 2019-07-19 kinaba: END 8cbd8685a8 2019-07-19 kinaba: CASE(3) 8cbd8685a8 2019-07-19 kinaba: int sx = 10; 8cbd8685a8 2019-07-19 kinaba: int lx1 = 8; 8cbd8685a8 2019-07-19 kinaba: int lx2 = 42; 8cbd8685a8 2019-07-19 kinaba: int X_[] = {10, 100, 12}; 8cbd8685a8 2019-07-19 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 8cbd8685a8 2019-07-19 kinaba: int Y_[] = {1, 0, 1}; 8cbd8685a8 2019-07-19 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 8cbd8685a8 2019-07-19 kinaba: long long _ = 181LL; 8cbd8685a8 2019-07-19 kinaba: END 8cbd8685a8 2019-07-19 kinaba: CASE(4) 8cbd8685a8 2019-07-19 kinaba: int sx = 500000000; 8cbd8685a8 2019-07-19 kinaba: int lx1 = 1; 8cbd8685a8 2019-07-19 kinaba: int lx2 = 999999999; 8cbd8685a8 2019-07-19 kinaba: int X_[] = {0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000}; 8cbd8685a8 2019-07-19 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 8cbd8685a8 2019-07-19 kinaba: int Y_[] = {1, 1, 2, 2, 3, 3, 4, 4, 7, 7, 999999947, 999999947, 900000047, 900000047}; 8cbd8685a8 2019-07-19 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 8cbd8685a8 2019-07-19 kinaba: long long _ = 8499999959LL; 8cbd8685a8 2019-07-19 kinaba: END 8cbd8685a8 2019-07-19 kinaba: /* 8cbd8685a8 2019-07-19 kinaba: CASE(5) 8cbd8685a8 2019-07-19 kinaba: int sx = ; 8cbd8685a8 2019-07-19 kinaba: int lx1 = ; 8cbd8685a8 2019-07-19 kinaba: int lx2 = ; 8cbd8685a8 2019-07-19 kinaba: int X_[] = ; 8cbd8685a8 2019-07-19 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 8cbd8685a8 2019-07-19 kinaba: int Y_[] = ; 8cbd8685a8 2019-07-19 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 8cbd8685a8 2019-07-19 kinaba: long long _ = LL; 8cbd8685a8 2019-07-19 kinaba: END 8cbd8685a8 2019-07-19 kinaba: CASE(6) 8cbd8685a8 2019-07-19 kinaba: int sx = ; 8cbd8685a8 2019-07-19 kinaba: int lx1 = ; 8cbd8685a8 2019-07-19 kinaba: int lx2 = ; 8cbd8685a8 2019-07-19 kinaba: int X_[] = ; 8cbd8685a8 2019-07-19 kinaba: vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); 8cbd8685a8 2019-07-19 kinaba: int Y_[] = ; 8cbd8685a8 2019-07-19 kinaba: vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); 8cbd8685a8 2019-07-19 kinaba: long long _ = LL; 8cbd8685a8 2019-07-19 kinaba: END 8cbd8685a8 2019-07-19 kinaba: */ 8cbd8685a8 2019-07-19 kinaba: } 8cbd8685a8 2019-07-19 kinaba: // END CUT HERE