Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ficha2;
- import java.io.IOException;
- import java.util.*;
- import static java.lang.Math.abs;
- class Coord implements Comparable<Coord>{
- int x,y;
- public Coord(int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public String toString(){
- return this.x + "," + this.y + "|";
- }
- @Override
- public int compareTo(Coord o) {
- if (o.x < this.x) return 1;
- else if (o.x > this.x) return -1;
- else return 0;
- }
- public int compareToY(Coord o){
- if(o.y <this.y) return 1;
- else if(o.y > this.y) return -1;
- return 0;
- }
- }
- class CoordY implements Comparable<CoordY>{
- int x,y;
- public CoordY(int x, int y) {
- this.x = x;
- this.y = y;
- }
- @Override
- public String toString(){
- return this.x + "," + this.y + "|";
- }
- @Override
- public int compareTo(CoordY o) {
- if (o.y < this.y) return 1;
- else if (o.y > this.y) return -1;
- else return 0;
- }
- }
- /**
- *
- * @author j_mig_000
- */
- public class Ficha2 {
- static String readLn(int maxLg) { //utility function to read from stdin
- byte lin[] = new byte[maxLg];
- int lg = 0, car = -1;
- String line = "";
- try {
- while (lg < maxLg) {
- car = System.in.read();
- if ((car < 0) || (car == '\n')) {
- break;
- }
- lin[lg++] += car;
- }
- } catch (IOException e) {
- return (null);
- }
- if ((car < 0) && (lg == 0)) {
- return (null); // eof
- }
- return (new String(lin, 0, lg));
- }
- static float dividir(float min,Coord[]array,int original){
- if((array.length/2)<=10){
- return distancia(min,array);
- }
- Coord[] array1 = Arrays.copyOfRange(array, 0,array.length/2);
- Coord[] array2 = Arrays.copyOfRange(array,((array.length)/2),array.length);
- float esquerda = dividir(min,array1,original);
- float direita = dividir(min,array2,original);
- if(esquerda<=direita){
- min = esquerda;
- }
- else{
- min = direita;
- }
- CoordY[] Final = new CoordY[array.length];
- int j=0;
- for(int i=0;i<array.length;i++){
- if(abs(array[array.length/2].x - array[i].x) <= min){
- CoordY coord = new CoordY((array[i].x),(array[i].y));
- Final[j] = coord;
- j++;
- }
- }
- CoordY[] Final2 = new CoordY[array.length];
- int size=0;
- for(int i=0;i<array.length;i++){
- Final2[i] = Final[i];
- }
- for(int i=0;i<array.length;i++){
- if(Final2[i] != null){
- size++;
- }
- }
- CoordY[] Final3 = new CoordY[size];
- for(int i=0;i<size;i++){
- Final3[i] = Final2[i];
- }
- Arrays.sort(Final3);
- float temp2 = distanciaY(min, Final3,Final3.length,0);
- if(temp2 <= min){
- return temp2;
- }
- else{
- return min;
- }
- }
- static float distancia(float min, Coord []array){
- Coord x,y;
- float temp,distancia = Integer.MAX_VALUE;
- for(int i = 0;i<array.length; i ++ ){
- x = array[i];
- for (int j = 0; j < array.length; j ++) {
- if(j!= i){
- y = array[j];
- temp = (float) Math.sqrt((Math.pow(y.x-x.x,2) + Math.pow(y.y - x.y,2)));
- if(temp <= min){
- min = temp;
- }
- }
- }
- }
- return min;
- }
- static float distanciaY(float min, CoordY []array,int totais,int index){
- CoordY x,y;
- float temp,distancia = Integer.MAX_VALUE;
- for(int i = index;i < totais; i ++ ){
- x = array[i];
- for (int j = index; j < totais; j ++) {
- if(i!=j){
- y = array[j];
- temp = (float) Math.sqrt((Math.pow(y.x-x.x,2) + Math.pow(y.y - x.y,2)));
- if(temp > min){
- return min;
- }
- if(temp < min){
- min = temp;
- }
- }
- }
- }
- return min;
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- int contador = 0;
- String linha;
- String tamanho = readLn(200);
- String[] numeros;
- int[] array = new int[Integer.parseInt(tamanho) * 2];
- Coord[] resultado = new Coord[Integer.parseInt(tamanho)];
- while (contador < (Integer.parseInt(tamanho))*2) {
- linha = readLn(500);
- numeros = linha.split(" ");
- for(int i=0;i<numeros.length;i++){
- array[contador] = Integer.parseInt(numeros[i]);
- contador ++;
- }
- }
- for(int i = 0; i < array.length;i += 2 ){
- Coord coord = new Coord(array[i],array[i+1]);
- resultado[i/2] = coord;
- }
- Arrays.sort(resultado);
- float distancia = dividir(Float.MAX_VALUE,resultado,resultado.length);
- System.out.printf("%.3f\n" ,distancia);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement