Cod sursa(job #1545679)

Utilizator PrickleOlimpiu Marinas Prickle Data 6 decembrie 2015 22:25:15
Problema Cele mai apropiate puncte din plan Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 1.64 kb
package cmap;

import java.util.*;
import java.io.*;

class Punct {
	public int x, y;

	Punct(int x, int y) {
		this.x = x;
		this.y = y;
	}

	long distanta(Punct B) {
		return (B.x - x) * (B.x - x) + (B.y - y) * (B.y - y);
	}
}

class PunctXComparator implements Comparator<Punct> {
	public int compare(Punct A, Punct B) {
		return B.x - A.x;
	}
}
class Cmap {
	public static void main(String arg[]) {

			try {
	            BufferedReader br = new BufferedReader(new FileReader("cmap.in"));
				Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("cmap.out")));
	            StringTokenizer stok;
	            int n = Integer.parseInt(br.readLine());
				Punct[] puncte = new Punct[n];
	            for(int i = 0; i<n; i++)
	            {
	                stok = new StringTokenizer(br.readLine());
	                puncte[i] = new Punct(Integer.parseInt(stok.nextToken()), Integer.parseInt(stok.nextToken()));
	            }
	            br.close();
				Arrays.sort(puncte, new PunctXComparator());
				double solutie = closest(puncte);
				writer.write(String.format("%.8g%n",solutie));
				writer.close();
				} catch (IOException e) {
	            System.out.println(e.getMessage());
			}
	}

	public static double closest(Punct[] puncte) {
		long solution = Long.MAX_VALUE;
		int end=puncte.length;
		int frame = 4;
		for (int i = 0; i < end-1; ++i) {
			for (int j = i + 1, end2 = (i + frame > end ? end : i + frame); j < end2; ++j) {
				if (solution > puncte[i].distanta(puncte[j])) {
					solution = puncte[i].distanta(puncte[j]);
				}
			}
		}
		return Math.sqrt(solution);
	}
}