Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include <vector>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <ctime>
  6. #include <random>
  7.  
  8. using namespace std;
  9. using point = pair<int, int>;
  10. double MAX = 1000000000.0;
  11. double GLOBAL_minDist = MAX;
  12. pair<point, point> GLOBAL_nearestPoints;
  13.  
  14. bool Xcmp ( const point &c1, const point &c2 ) {
  15.   return ( c1.first < c2.first );
  16. }
  17.  
  18. bool Ycmp ( const point &c1, const point &c2 ) {
  19.   return ( c1.second < c2.second );
  20. }
  21.  
  22. inline ostream& operator<< ( ostream& os, const point& p ) {
  23.   return os << p.first << " " << p.second << "\n";
  24. }
  25.  
  26. inline ostream& operator<< ( ostream& os, const vector<point> &points ) {
  27.   for( auto itr = points.begin(); itr < points.end(); itr++ ) {
  28.     os << *itr;
  29.   }
  30.   return os;
  31. }
  32.  
  33. inline ostream& operator<< ( ostream& os, const pair<point, point> nearestPair ) {
  34.   return os << static_cast<int> (nearestPair.second.first) << " " << static_cast<int> (nearestPair.second.second) << "\n"
  35.             << static_cast<int> (nearestPair.first.first) << " " << static_cast<int> (nearestPair.first.second);
  36. }
  37.  
  38. inline double distance( const point a, const point b ) {
  39.   return sqrt( pow(( a.first - b.first ), 2 ) + pow( a.second - b.second, 2 ));
  40. }
  41.  
  42. void bruteCP( const vector<point> &Xs ) {
  43.   for( auto it = Xs.begin(); it < Xs.end() - 1; it++ ) {
  44.     for( auto it2 = it + 1; it2 < Xs.end(); it2++ ) {
  45.       double minDist = distance( *it, *it2 );
  46.       if( minDist < GLOBAL_minDist ) {
  47.         cout << minDist << "\n";
  48.         GLOBAL_minDist = minDist;
  49.         GLOBAL_nearestPoints = pair<point, point> ( *it, *it2 );
  50.       }
  51.     }
  52.   }
  53. }
  54.  
  55. void divConCP( const vector<point>& Xs, const vector<point>& Ys ) {
  56.   int Xsize = Xs.size();
  57.   if( Xsize <= 3 ) { bruteCP( Xs ); return; }
  58.  
  59.   int mid =  Xsize / 2;
  60.   int median = Xs[mid].first;
  61.  
  62.   vector<point> leftYs;
  63.   copy_if( Ys.begin(), Ys.end(), back_inserter(leftYs), [median](const point& point)
  64.           {return point.first <= median;} );
  65.   vector<point>leftXs;
  66.   leftXs.insert( leftXs.end(), Xs.begin(), Xs.begin() + mid );
  67.   divConCP( leftXs, leftYs );
  68.  
  69.   vector<point> rightYs, rightXs;
  70.   copy_if( Ys.begin(), Ys.end(), back_inserter(leftYs), [median](const point& point)
  71.           {return point.first > median;} );
  72.   rightXs.insert( rightXs.end(), Xs.begin() + mid, Xs.end() );
  73.   divConCP( rightXs, rightYs );
  74.  
  75.   vector<point> strip;
  76.   copy_if( Ys.begin(), Ys.end(), back_inserter(strip), [median, GLOBAL_minDist](const point& point)
  77.           {return abs(point.first - median) < GLOBAL_minDist;} );
  78.  
  79.   //vector<point> uniqStrip;
  80.   //unique_copy( strip.begin(), strip.end(), uniqStrip.begin() );
  81.  
  82.   for( auto itr = strip.begin(); itr < strip.end(); itr++ ) {
  83.     for( auto itr2 = itr + 1; itr2 < strip.end() && (*itr2).second < GLOBAL_minDist; itr2++ ) {
  84.       double minDist = distance( *itr, *itr2 );
  85.       if( minDist < GLOBAL_minDist) {
  86.         //cout << minDist << "\n";
  87.         //cout << *itr << " " << *itr2 << "\n";
  88.         GLOBAL_minDist = minDist;
  89.         GLOBAL_nearestPoints = pair<point, point> ( *itr, *itr2 );
  90.       }
  91.     }
  92.   }
  93. }
  94.  
  95. int main() {
  96.   int n, x, y;
  97.   vector<point> Xs, Ys;
  98. /*
  99.   cin >> n;
  100.   for( int i = 0; i < n; i++ ) {
  101.     cin >> x;
  102.     cin >> y;
  103.    // x = -i;
  104.    // y = -i;
  105.  
  106.     point xy( x, y );
  107.     Xs.push_back( xy );
  108.     Ys.push_back( xy );
  109.   }
  110. */
  111.     // DEBUG //
  112.  
  113.   n = 100000;
  114.   srand(time(0));
  115.   std::default_random_engine gen(time(0));
  116.   std::uniform_int_distribution<int> dis(-20000000, 20000000);
  117.   for( int i = 0; i < n - 2; i++ ) {
  118.     x = dis( gen );
  119.     y = dis( gen );
  120.     //x = i;
  121.     //y = i;
  122.     point xy( x, y );
  123.     Xs.push_back( xy );
  124.     Ys.push_back( xy );
  125.   }
  126.  
  127.     Xs.push_back( point( 20001, 20001 ));
  128.     Ys.push_back( point( 20001, 20001 ));
  129.     Xs.push_back( point( 20000, 20001 ));
  130.     Ys.push_back( point( 20000, 20001 ));
  131.  
  132.   // DEBUG //
  133.  
  134.   sort( Xs.begin(), Xs.end(), Xcmp );
  135.   sort( Ys.begin(), Ys.end(), Ycmp );
  136.  
  137.   divConCP( Xs, Ys );
  138.   //bruteCP( Xs );
  139.   cout << GLOBAL_minDist << "\n";
  140.   cout << GLOBAL_nearestPoints << "\n";
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement