Cod sursa(job #2969261)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 22 ianuarie 2023 19:20:43
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>
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);

long long dis( pair<int, int> A, pair<int, int> B ) {
	return ( (long long)A.x - B.x ) * ( A.x - B.x ) + ( (long long)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;
	long long pp;
	long long dMin = dis( v[ 0 ], v[ 1 ] );
	for( int32_t i = 0; i < n; i++ ) {
		int DD = sqrt( dMin );
		while( v[ i ].x - v[ poz ].x > DD )
			s.erase( v[ poz++ ] );

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

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