Cod sursa(job #2969254)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 22 ianuarie 2023 19:17:14
Problema Cele mai apropiate puncte din plan Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <set>
#define int long double
using namespace std;

ifstream cin( "cmap.in" );
ofstream cout( "cmap.out" );

#define x first
#define y second
inline bool cmpByY( pair<int, int> A, pair<int, int> B) { return A.y == B.y ? A.x < B.x : A.y < B.y; }
set<pair<int, int>, decltype(cmpByY)*> s(cmpByY);

int dis( pair<int, int> A, pair<int, int> B ) {
	return ( (int)A.x - B.x ) * ( A.x - B.x ) + ( (int)A.y - B.y ) * ( A.y - B.y );
}

const int32_t MAX = 1e5 + 3;
pair<int, int> v[ MAX ];
int32_t n;

signed main()
{
	cin >> n;
	for( int32_t i = 0; i < n; i++ )
		cin >> v[ i ].x >> v[ i ].y;

	sort( v, v + n, []( pair<int, int> A, pair<int, int> B ) {
			return A.x == B.x ? A.y < B.y : A.x < B.x;
		} );

	int32_t poz = 0;
	int pp;
	int dMin = dis( v[ 0 ], v[ 1 ] );
	for( int32_t i = 0; i < n; i++ ) {
		while( ((int)v[ i ].x - v[ poz ].x) * (v[ i ].x - v[ poz ].x) > dMin )
			s.erase( v[ poz++ ] );

		for( auto it = s.lower_bound( { -2e9, v[ i ].y - dMin } ); it != s.end() && (*it).y <= v[ i ].y + dMin; it++ ) {
			if( ( pp = dis( v[ i ], (*it) ) ) < dMin )
				dMin = pp;
		}
		s.insert( v[ i ] );
	}

	cout << fixed << setprecision( 6 ) << (int)sqrt( dMin ) << '\n';
    return 0;
}