AlenAntonelli

Yet another 2D Walking 1066/F

Oct 18th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. /// http://codeforces.com/problemset/problem/1066/F - 18/10/18
  2.  
  3. #include <iostream>
  4. #include <map>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. #define f first
  9. #define s second
  10.  
  11. #define cx f
  12. #define cy s
  13. #define ll long long
  14.  
  15. using namespace std;
  16.  
  17. map <int, int> mapa;
  18.  
  19. int DarId (int num)
  20. {
  21.     if ( !mapa[num] )
  22.         mapa[num] = mapa.size();
  23.     return mapa[num];
  24. }
  25.  
  26. ll Dist( const pair<int, int> *A, const pair <int, int> *B )
  27. {
  28.     return abs(A->cx - B->cx) + abs(A->cy - B->cy);
  29. }
  30.  
  31. int main()
  32. {
  33.     int n;
  34.     cin>>n;
  35.  
  36.     vector< pair<int, pair<int, int> > > ving (n);
  37.  
  38.     int x, y;
  39.     for (int i=0; i<n; i++)
  40.     {
  41.         cin >> x >> y;
  42.  
  43.         ving[i].s.cx = x;
  44.         ving[i].s.cy = y;
  45.         ving[i].f = max (x, y);
  46.     }
  47.     sort( ving.begin(), ving.end() );
  48.  
  49.     vector < pair< pair<int,int> , pair<int,int> > > v (2*100005);
  50.     v[0] = { {0,0}, {0,0} };
  51.  
  52.     int id;
  53.     for (int i=0; i<n; i++)
  54.     {
  55.         pair <int, int> cord = ving[i].s;
  56.  
  57.         id = DarId( max(cord.cx, cord.cy) );
  58.  
  59.         pair <int, int> A = v[id].f;
  60.         pair <int, int> B = v[id].s;
  61.  
  62.         if ( !A.cx && !A.cy )                        ///me guardo el punto de ese nivel que esté mas mas cerca de la derecha (menor X)
  63.             A = cord;
  64.         else if ( cord.cx < A.cx )
  65.             A = cord;
  66.         else if (( cord.cx == A.cx ) && ( cord.cy > A.cy ))
  67.             A = cord;
  68.  
  69.         if ( !B.cx && !B.cy )                        ///me guardo el punto de ese nivel que esté mas mas cerca de abajo (menor Y)
  70.             B = cord;
  71.         else if ( cord.cy < B.cy )
  72.             B = cord;
  73.         else if (( cord.cy == B.cy ) && ( cord.cx > B.cx ))
  74.             B = cord;
  75.  
  76.         v[id].f  = A;
  77.         v[id].s = B;
  78.     }
  79.  
  80.     v.resize( 1+mapa.size() );
  81.  
  82.     ll ant_CtA =0, ant_CtB=0;
  83.     ll CtA =0, CtB=0;
  84.  
  85.     for ( id=1; id<v.size(); id++)
  86.     {
  87.         pair<int,int> *A = &v[id].f;
  88.         pair<int,int> *B = &v[id].s;
  89.  
  90.         pair<int,int> *ant_A = &v[id-1].f;
  91.         pair<int,int> *ant_B = &v[id-1].s;
  92.  
  93.         CtA = Dist(A, B) + min( ant_CtA + Dist(ant_B, A) , ant_CtB + Dist(ant_A, A) );
  94.         CtB = Dist(A, B) + min( ant_CtA + Dist(ant_B, B) , ant_CtB + Dist(ant_A, B) );
  95.  
  96.         ant_CtA = CtA;
  97.         ant_CtB = CtB;
  98.     }
  99.     cout<<min(ant_CtA, ant_CtB);
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment