Pagini recente » Cod sursa (job #2327011) | Cod sursa (job #1247722) | Cod sursa (job #1365173) | Cod sursa (job #2874810) | Cod sursa (job #1822789)
package temadividetap4;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class TemaDivideTAP4 {
static int n;
static List pointsX = new ArrayList<>();
static List V = new ArrayList<>();
static List points = new ArrayList<>();
static List pointsY = new ArrayList<>();
public static double distance(Point a, Point b) {
return (double)Math.sqrt((a.x - b.x) * (a.x - b.x) + ((a.y - b.y) * (a.y - b.y)));
}
public static void sortByX(List<? extends Point> points)
{
Collections.sort(points, new Comparator<Point>() {
public int compare(Point point1, Point point2)
{
if (point1.x < point2.x)
return -1;
if (point1.x > point2.x)
return 1;
return 0;
}
}
);
}
public static void sortByY(List<? extends Point> points)
{
Collections.sort(points, new Comparator<Point>() {
public int compare(Point point1, Point point2)
{
if (point1.y < point2.y)
return -1;
if (point1.y > point2.y)
return 1;
return 0;
}
}
);
}
public static Pair di(List<? extends Point> points) {
List<Point> pointsSortedByX = new ArrayList<Point>(points);
sortByX(pointsSortedByX);
List<Point> pointsSortedByY = new ArrayList<Point>(points);
sortByY(pointsSortedByY);
return di(pointsSortedByX, pointsSortedByY);
}
private static Pair di(List<? extends Point> pointsSortedByX, List<? extends Point> pointsSortedByY) {
if(pointsSortedByX.size() <= 3) {
int nr = pointsSortedByX.size();
if(nr < 2)
return null;
Pair pair = new Pair(pointsSortedByX.get(0), pointsSortedByX.get(1));
if(nr == 2) {
for (int i = 0; i < nr - 1; i++) {
Point point1 = pointsSortedByX.get(i);
for (int j = i + 1; j < nr; j++) {
Point point2 = pointsSortedByX.get(j);
double distance = distance(point1, point2);
if (distance < pair.distance)
pair.update(point1, point2, distance);
}
}
}
return pair;
}
int mijl = pointsSortedByX.size() / 2;
List<? extends Point> left = pointsSortedByX.subList(0, mijl);
List<? extends Point> right = pointsSortedByX.subList(mijl, pointsSortedByX.size());
List<Point> tempList = new ArrayList<Point>(left);
//sortByY(tempList);
Pair closest = di(left,tempList);
tempList.clear();
tempList.addAll(right);
//sortByY(tempList);
Pair closestRight = di(right,tempList);
tempList.clear();
if(closest.distance > closestRight.distance) {
closest = closestRight;
}
double shortestDistance = closest.distance;
double centerX = right.get(0).x;
for (Point point : pointsSortedByY)
if (Math.abs(centerX - point.x) < shortestDistance)
tempList.add(point);
for (int i = 0; i < tempList.size() - 1; i++) {
Point point1 = tempList.get(i);
for (int j = i + 1; j < tempList.size() && j - i < 8; j++) {
Point point2 = tempList.get(j);
if ((point2.y - point1.y) >= shortestDistance)
break;
double distance = distance(point1, point2);
if (distance < closest.distance) {
closest.update(point1, point2, distance);
shortestDistance = distance;
}
}
}
return closest;
}
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = null;
try {
sc = new Scanner(new File("cmap.in"));
if(sc.hasNextInt())
n = sc.nextInt();
for(int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
points.add(new Point(x,y));
pointsX.add(new Point(x,y));
}
}
catch(Exception ex) {
ex.printStackTrace();
}
finally {
sc.close();
}
Pair Closest = di(points);
System.out.println( Closest );
}
}