Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// http://codeforces.com/problemset/problem/1066/F - 18/10/18
- #include <iostream>
- #include <map>
- #include <vector>
- #include <algorithm>
- #define f first
- #define s second
- #define cx f
- #define cy s
- #define ll long long
- using namespace std;
- map <int, int> mapa;
- int DarId (int num)
- {
- if ( !mapa[num] )
- mapa[num] = mapa.size();
- return mapa[num];
- }
- ll Dist( const pair<int, int> *A, const pair <int, int> *B )
- {
- return abs(A->cx - B->cx) + abs(A->cy - B->cy);
- }
- int main()
- {
- int n;
- cin>>n;
- vector< pair<int, pair<int, int> > > ving (n);
- int x, y;
- for (int i=0; i<n; i++)
- {
- cin >> x >> y;
- ving[i].s.cx = x;
- ving[i].s.cy = y;
- ving[i].f = max (x, y);
- }
- sort( ving.begin(), ving.end() );
- vector < pair< pair<int,int> , pair<int,int> > > v (2*100005);
- v[0] = { {0,0}, {0,0} };
- int id;
- for (int i=0; i<n; i++)
- {
- pair <int, int> cord = ving[i].s;
- id = DarId( max(cord.cx, cord.cy) );
- pair <int, int> A = v[id].f;
- pair <int, int> B = v[id].s;
- if ( !A.cx && !A.cy ) ///me guardo el punto de ese nivel que esté mas mas cerca de la derecha (menor X)
- A = cord;
- else if ( cord.cx < A.cx )
- A = cord;
- else if (( cord.cx == A.cx ) && ( cord.cy > A.cy ))
- A = cord;
- if ( !B.cx && !B.cy ) ///me guardo el punto de ese nivel que esté mas mas cerca de abajo (menor Y)
- B = cord;
- else if ( cord.cy < B.cy )
- B = cord;
- else if (( cord.cy == B.cy ) && ( cord.cx > B.cx ))
- B = cord;
- v[id].f = A;
- v[id].s = B;
- }
- v.resize( 1+mapa.size() );
- ll ant_CtA =0, ant_CtB=0;
- ll CtA =0, CtB=0;
- for ( id=1; id<v.size(); id++)
- {
- pair<int,int> *A = &v[id].f;
- pair<int,int> *B = &v[id].s;
- pair<int,int> *ant_A = &v[id-1].f;
- pair<int,int> *ant_B = &v[id-1].s;
- CtA = Dist(A, B) + min( ant_CtA + Dist(ant_B, A) , ant_CtB + Dist(ant_A, A) );
- CtB = Dist(A, B) + min( ant_CtA + Dist(ant_B, B) , ant_CtB + Dist(ant_A, B) );
- ant_CtA = CtA;
- ant_CtB = CtB;
- }
- cout<<min(ant_CtA, ant_CtB);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment