import java.awt.Point; import java.io.PrintStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.PriorityQueue; import java.util.Scanner; /** * Solution to Hopscotch with n up to 1000. */ public class hopscotch1000_vanb { /** The input. */ private static Scanner sc; /** The output. */ private static PrintStream ps; /** * A comparator to compare points by X coordinate. */ private class ByX implements Comparator { /** * Compare two points by X coordinate. * * @param a A point * @param b Another point * @return Standard for compareTo */ @Override public int compare( Point a, Point b ) { return a.x-b.x; } } /** * A comparator to compare points by Y coordinate. */ private class ByY implements Comparator { /** * Compare two points by Y coordinate. * * @param a A point * @param b Another point * @return Standard for compareTo */ @Override public int compare( Point a, Point b ) { return a.y-b.y; } } /** * Find the closest point. * * @param p the p * @param points the points * @param comparator the comparator * @return the point */ private Point findClosestPoint( Point p, Point points[], Comparator comparator ) { // Use binary search to find the closest point in a list of points. int index = Arrays.binarySearch( points, p, comparator ); // Java's binarySearch returns the index on an exact match, -(insertionpoint-1) if no exact match if( index<0 ) { // If no exact match, then this is the index of the first value in the list greater than p index = -(index+1); // It could be off the end of the list if p>everything in the list if( index>=points.length ) --index; // Otherwise, we have to also look at the item immediately previous (if it exists). else if( index>0 ) { // Are we checking X coords? if( comparator instanceof ByX ) { if( Math.abs(points[index-1].x-p.x)[] cells = new ArrayList[k+1]; for( int i=1; i<=k; i++ ) cells[i] = new ArrayList(); // Also for each i, we'll keep a list of those points sorted by X coord, and also by Y coord. Point[][] cellsByX = new Point[k+1][]; Point[][] cellsByY = new Point[k+1][]; // distances[i][j] will be the minimum distance from some 1 cell to cell (i,j) int distances[][] = new int[n][n]; // Read the matrix, populate cells[], and put a signal value in distances[i][j] for( int i=0; i