Cod sursa(job #1821358)

Utilizator EhtRalpmetFMI Ardei Claudiu-Alexandru EhtRalpmet Data 2 decembrie 2016 23:03:08
Problema Cele mai apropiate puncte din plan Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 1.57 kb
import java.io.File;
import java.io.*;
import java.util.*;



class p4{
	public static void main(String args[]) throws IOException{
		try (Writer pw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("date.out"), "utf-8"))) {
			Scanner sc = new Scanner(System.in);
			Scanner sci = new Scanner( new File("date.in"));
			
			
			
			class plan{
				class pair{
					int x,y;
				}
				int n;
				double dMin = 1e9;
				List<pair> v;
				
				
				plan(){
					n = sci.nextInt();
					v = new ArrayList<pair>();
					for(int i=0;i<n;i++){
						pair aux = new pair();
						aux.x = sci.nextInt();
						aux.y = sci.nextInt();
						v.add(aux);
					}
					Collections.sort(v, (a, b) -> a.x - b.x);
				}
				
				double getMinDist(int l, int r){
					if(l == r){		///nu cred ca ar trebui sa intre pe ramura asta :-?
						return 0;
					}
					if(r - l == 1){
						return sqrt((v.get(l).x-v.get(r).x)*(v.get(l).x-v.get(r).x) + (v.get(l).y-v.get(r).y)*(v.get(l).y-v.get(r).y));
					}
					if(r-l == 2){
						return min(min(sqrt((v.get(l).x-v.get(r).x)*(v.get(l).x-v.get(r).x) + (v.get(l).y-v.get(r).y)*(v.get(l).y-v.get(r).y)),sqrt((v.get(l).x-v.get(r-1).x)*(v.get(l).x-v.get(r-1).x) + (v.get(l).y-v.get(r-1).y)*(v.get(l).y-v.get(r-1).y))),sqrt((v.get(l+1).x-v.get(r).x)*(v.get(l+1).x-v.get(r).x) + (v.get(l+1).y-v.get(r).y)*(v.get(l+1).y-v.get(r).y)));
					}
					return -1;
				}
			}
			
			plan a = new plan();
			pw.write(""+a.getMinDist(0, a.n-1));
			
			pw.close();
		}
		catch(Exception e){
			System.out.println(""+e);
		}
	}
}