Advertisement
OverSkillers

ConquerDivide 2 Shortest Points

Feb 27th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.95 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package ficha2;
  8.  
  9.  
  10. import java.io.IOException;
  11. import java.util.*;
  12. import static java.lang.Math.abs;
  13.  
  14. class Coord implements Comparable<Coord>{
  15.     int x,y;
  16.  
  17.     public Coord(int x, int y) {
  18.         this.x = x;
  19.         this.y = y;
  20.     }
  21.  
  22.     @Override
  23.     public String toString(){
  24.         return this.x + "," + this.y + "|";
  25.     }
  26.  
  27.     @Override
  28.     public int compareTo(Coord o) {
  29.         if (o.x < this.x) return 1;
  30.         else if (o.x > this.x) return -1;
  31.         else return 0;
  32.     }
  33.    
  34.     public int compareToY(Coord o){
  35.         if(o.y <this.y) return 1;
  36.         else if(o.y > this.y) return -1;
  37.         return 0;
  38.     }
  39. }
  40.  
  41.  
  42. class CoordY implements Comparable<CoordY>{
  43.     int x,y;
  44.  
  45.     public CoordY(int x, int y) {
  46.         this.x = x;
  47.         this.y = y;
  48.     }
  49.  
  50.     @Override
  51.     public String toString(){
  52.         return this.x + "," + this.y + "|";
  53.     }
  54.  
  55.     @Override
  56.     public int compareTo(CoordY o) {
  57.         if (o.y < this.y) return 1;
  58.         else if (o.y > this.y) return -1;
  59.         else return 0;
  60.     }
  61. }
  62.  
  63.  
  64. /**
  65.  *
  66.  * @author j_mig_000
  67.  */
  68.  
  69. public class Ficha2 {
  70.    
  71.    
  72.     static String readLn(int maxLg) { //utility function to read from stdin
  73.  
  74.         byte lin[] = new byte[maxLg];
  75.         int lg = 0, car = -1;
  76.         String line = "";
  77.         try {
  78.             while (lg < maxLg) {
  79.                 car = System.in.read();
  80.                 if ((car < 0) || (car == '\n')) {
  81.                     break;
  82.                 }
  83.                 lin[lg++] += car;
  84.             }
  85.         } catch (IOException e) {
  86.             return (null);
  87.         }
  88.         if ((car < 0) && (lg == 0)) {
  89.             return (null); // eof
  90.         }
  91.         return (new String(lin, 0, lg));
  92.     }
  93.    
  94.     static float dividir(float min,Coord[]array,int original){
  95.        
  96.         if((array.length/2)<=10){
  97.             return distancia(min,array);
  98.         }
  99.        
  100.         Coord[] array1 = Arrays.copyOfRange(array, 0,array.length/2);
  101.         Coord[] array2 = Arrays.copyOfRange(array,((array.length)/2),array.length);
  102.  
  103.         float esquerda = dividir(min,array1,original);
  104.         float direita = dividir(min,array2,original);
  105.        
  106.         if(esquerda<=direita){
  107.             min = esquerda;
  108.         }
  109.         else{
  110.             min = direita;
  111.         }
  112.        
  113.         CoordY[] Final = new CoordY[array.length];        
  114.        
  115.         int j=0;
  116.         for(int i=0;i<array.length;i++){
  117.             if(abs(array[array.length/2].x - array[i].x) <= min){
  118.                 CoordY coord = new CoordY((array[i].x),(array[i].y));
  119.                 Final[j] = coord;
  120.                 j++;
  121.             }
  122.         }
  123.        
  124.         CoordY[] Final2 = new CoordY[array.length];
  125.         int size=0;
  126.        
  127.         for(int i=0;i<array.length;i++){
  128.                 Final2[i] = Final[i];
  129.             }
  130.        
  131.         for(int i=0;i<array.length;i++){
  132.                 if(Final2[i] != null){
  133.                     size++;
  134.                 }
  135.             }
  136.        
  137.         CoordY[] Final3 = new CoordY[size];
  138.        
  139.         for(int i=0;i<size;i++){
  140.                 Final3[i] = Final2[i];
  141.             }
  142.        
  143.         Arrays.sort(Final3);
  144.        
  145.         float temp2 = distanciaY(min, Final3,Final3.length,0);
  146.         if(temp2 <= min){
  147.             return temp2;
  148.         }
  149.         else{
  150.             return min;
  151.         }
  152.          
  153.     }
  154.    
  155.     static float distancia(float min, Coord []array){
  156.        
  157.         Coord x,y;
  158.         float temp,distancia = Integer.MAX_VALUE;
  159.        
  160.         for(int i = 0;i<array.length; i ++ ){
  161.             x = array[i];
  162.             for (int j = 0; j < array.length; j ++) {
  163.                 if(j!= i){
  164.                 y = array[j];                
  165.                 temp = (float) Math.sqrt((Math.pow(y.x-x.x,2) + Math.pow(y.y - x.y,2)));
  166.                 if(temp <= min){
  167.                     min = temp;
  168.                 }
  169.                 }
  170.             }
  171.         }
  172.         return min;
  173.     }
  174.    
  175.  
  176.     static float distanciaY(float min, CoordY []array,int totais,int index){
  177.        
  178.         CoordY x,y;
  179.         float temp,distancia = Integer.MAX_VALUE;
  180.        
  181.         for(int i = index;i < totais; i ++ ){
  182.             x = array[i];
  183.             for (int j = index; j < totais; j ++) {
  184.                 if(i!=j){
  185.                 y = array[j];                
  186.                 temp = (float) Math.sqrt((Math.pow(y.x-x.x,2) + Math.pow(y.y - x.y,2)));
  187.                 if(temp > min){
  188.                     return min;
  189.                 }
  190.                 if(temp < min){
  191.                     min = temp;
  192.                 }
  193.                 }
  194.             }
  195.         }
  196.         return min;
  197.     }
  198.    
  199.    
  200.  
  201.     /**
  202.      * @param args the command line arguments
  203.      */
  204.     public static void main(String[] args) {
  205.         // TODO code application logic here
  206.         int contador = 0;
  207.         String linha;
  208.         String tamanho = readLn(200);
  209.         String[] numeros;
  210.         int[] array = new int[Integer.parseInt(tamanho) * 2];
  211.         Coord[] resultado = new Coord[Integer.parseInt(tamanho)];
  212.        
  213.         while (contador < (Integer.parseInt(tamanho))*2) {
  214.             linha = readLn(500);
  215.             numeros = linha.split(" ");
  216.             for(int i=0;i<numeros.length;i++){
  217.                 array[contador] = Integer.parseInt(numeros[i]);
  218.                 contador ++;
  219.             }
  220.         }
  221.        
  222.         for(int i = 0; i < array.length;i += 2 ){
  223.             Coord coord = new Coord(array[i],array[i+1]);
  224.             resultado[i/2] = coord;
  225.         }
  226.        
  227.         Arrays.sort(resultado);
  228.        
  229.         float distancia = dividir(Float.MAX_VALUE,resultado,resultado.length);
  230.         System.out.printf("%.3f\n" ,distancia);            
  231.     }              
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement