Cod sursa(job #2066705)

Utilizator InsomniaccTrocan Eduard-Valentin Insomniacc Data 15 noiembrie 2017 12:42:11
Problema Cele mai apropiate puncte din plan Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.38 kb
// ClosestTwoPoints.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <fstream>
#include <math.h>
#include <iomanip>

#define infl 0x7fffffffffffffffLL

using namespace std;

typedef long long llong;

struct Point
{
	int x, y;
};

void interclas(Point *X, int st, int mij, int dr)
{
	Point  *aux = new Point[dr - st + 1];
	int i = st, j = mij + 1, k = 0;
	while (i <= mij && j <= dr)
	{
		if (X[i].y < X[j].y)
			aux[k++] = X[i++];
		else
			aux[k++] = X[j++];
	}

	if(i>mij)
		while (j<=dr)
			aux[k++] = X[j++];
	else
		while (i <= mij)
			aux[k++] = X[j++];

	for (size_t i = 0; i < k; i++)
	{
		X[st + i] = aux[i];
	}
	delete[] aux;
}

bool increasingX(const Point a, const Point b)
{
	return a.x < b.x;
}

bool increasingY(const Point a, const Point b)
{
	return a.y < b.y;
}

llong min(const llong &a, const llong &b)
{
	return (a < b) ? a : b;
}

llong distance2(const Point a, const Point b)
{
	llong dx = (b.x - a.x);
	llong dy = (b.y - a.y);
	return dx*dx + dy*dy;
}

llong closestDistance(int st, int dr, Point *X,Point &first,Point &second)
{
	if (dr - st < 3)
	{
		sort(X + st, X + dr, increasingY);		// Se sorteaza elementele crescator dupa ordonata
		llong mind = infl;				// Distanta minima dintre doua puncte din X[st...dr]
		int pos1 = -1, pos2 = -1;

		for (size_t i = 0; i < dr; i++)
		{
			for (size_t j = i+1; j <= dr; j++)
			{
				llong distaux = distance2(X[i], X[j]);
				if (distaux < mind)
				{
					mind = distaux;
					pos1 = i;
					pos2 = j;
				}

			}
		}
		first = X[pos1];
		second = X[pos2];
		return mind;	
	}
	else
	{
		// Divide
		int mij = (st + dr) / 2;

		Point f1, f2, f3, s1, s2, s3;
		
		llong d1 = closestDistance(st, mij, X, f1, s1);
		llong d2 = closestDistance(mij + 1, dr, X, f2, s2);
		llong delta = min(d1, d2);
		llong d3 = infl;

		// Ne uitam in banda
		size_t nrOfCandidates = dr - st + 1,k = 0;
		Point *candidates = new Point[nrOfCandidates];

		for (size_t i = st; i <= dr; i++)
			if (abs(X[i].x - mij) <= delta)
				candidates[k++] = X[i];


		for (size_t i = 0; i < nrOfCandidates-1; i++)
		{

			for (size_t j = i + 1; j < nrOfCandidates; j++)
			{
				llong aux = distance2(candidates[i], candidates[j]);
				if (aux < d3)
				{
					f3 = candidates[i];
					s3 = candidates[j];
					d3 = aux;
				}

				if (aux > delta)
					break;
			}
		}
		delete[] candidates;

		// Stabilim care e distanta minima si punctele ce o formeaza
		if (min(delta, d3) == delta)
		{
			if (delta == d1)
			{
				first = f1;
				second = s1;
				return d1;
			}
			else
			{
				first = f2;
				second = s2;
				return d2;
			}
		}
		else
		{
			first = f3;
			second = s3;
			return d3;
		}
	}

}

int main()
{
	int n;
	ifstream fin("cmap.in");
	ofstream fout("cmap.out");
	fin >> n;
	Point *arr_x = new Point[n];
	
	int xx, yy;

	for (int i = 0; i < n; i++)
	{
		fin >> xx >> yy;
		arr_x[i].x = xx;
		arr_x[i].y = yy;
	}
	
	fin.close();

	sort(arr_x, arr_x + n, increasingX);
	
	Point p1, p2;
	double result = sqrt(closestDistance(0, n - 1, arr_x, p1, p2));
	fout << fixed << setprecision(6) << result  << endl;

	//cout << "(" << p1.x << "," << p1.y << ") , " << "(" << p2.x << "," << p2.y << ")\n";
	delete[] arr_x;
    return 0;
}