qwerty787788

J

Sep 27th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class J2 {
  5.     FastScanner in;
  6.     PrintWriter out;
  7.  
  8.     class Obj implements Comparable<Obj> {
  9.         int id;
  10.         double value;
  11.  
  12.         public Obj(int id, double value) {
  13.             super();
  14.             this.id = id;
  15.             this.value = value;
  16.         }
  17.  
  18.         @Override
  19.         public int compareTo(Obj o) {
  20.             return Double.compare(value, o.value);
  21.         }
  22.  
  23.     }
  24.  
  25.     void solve() {
  26.         int tc = in.nextInt();
  27.         Random rnd = new Random(123);
  28.         for (int t = 0; t < tc; t++) {
  29.             int n = in.nextInt();
  30.             int[] x = new int[n];
  31.             int[] y = new int[n];
  32.             for (int i = 0; i < n; i++) {
  33.                 x[i] = in.nextInt();
  34.                 y[i] = in.nextInt();
  35.             }
  36.             double x1 = rnd.nextDouble(), y1 = rnd.nextDouble();
  37.             double d = Math.hypot(x1, y1);
  38.             x1 /= d;
  39.             y1 /= d;
  40.             Obj[] a = new Obj[n];
  41.             for (int i = 0; i < n; i++) {
  42.                 a[i] = new Obj(i, x1 * y[i] - y1 * x[i]);
  43.             }
  44.             Arrays.sort(a);
  45.             long[] answer = new long[n];
  46.             for (int i = 0; i < n; i++) {
  47.                 int id = a[i].id;
  48.                 answer[id] = Long.MAX_VALUE;
  49.                 for (int dx1 = -1; dx1 < 2; dx1 += 2)
  50.                     for (int j = i + dx1; j >= 0 && j < n; j += dx1) {
  51.                         double dd = Math.abs(a[i].value - a[j].value);
  52.                         if (dd * dd - 5 > answer[id]) {
  53.                             break;
  54.                         }
  55.                         long dx = x[id] - x[a[j].id], dy = y[id] - y[a[j].id];
  56.                         answer[id] = Math.min(answer[id], dx * dx + dy * dy);
  57.                     }
  58.             }
  59.             for (int i = 0; i < n; i++) {
  60.                 out.println(answer[i]);
  61.             }
  62.         }
  63.     }
  64.  
  65.     void run() {
  66.         try {
  67.             in = new FastScanner(new File("j.in"));
  68.             out = new PrintWriter(new File("j.out"));
  69.  
  70.             solve();
  71.  
  72.             out.close();
  73.         } catch (FileNotFoundException e) {
  74.             e.printStackTrace();
  75.         }
  76.     }
  77.  
  78.     void runIO() {
  79.  
  80.         in = new FastScanner(System.in);
  81.         out = new PrintWriter(System.out);
  82.  
  83.         solve();
  84.  
  85.         out.close();
  86.     }
  87.  
  88.     class FastScanner {
  89.         BufferedReader br;
  90.         StringTokenizer st;
  91.  
  92.         public FastScanner(File f) {
  93.             try {
  94.                 br = new BufferedReader(new FileReader(f));
  95.             } catch (FileNotFoundException e) {
  96.                 e.printStackTrace();
  97.             }
  98.         }
  99.  
  100.         public FastScanner(InputStream f) {
  101.             br = new BufferedReader(new InputStreamReader(f));
  102.         }
  103.  
  104.         String next() {
  105.             while (st == null || !st.hasMoreTokens()) {
  106.                 String s = null;
  107.                 try {
  108.                     s = br.readLine();
  109.                 } catch (IOException e) {
  110.                     e.printStackTrace();
  111.                 }
  112.                 if (s == null)
  113.                     return null;
  114.                 st = new StringTokenizer(s);
  115.             }
  116.             return st.nextToken();
  117.         }
  118.  
  119.         boolean hasMoreTokens() {
  120.             while (st == null || !st.hasMoreTokens()) {
  121.                 String s = null;
  122.                 try {
  123.                     s = br.readLine();
  124.                 } catch (IOException e) {
  125.                     e.printStackTrace();
  126.                 }
  127.                 if (s == null)
  128.                     return false;
  129.                 st = new StringTokenizer(s);
  130.             }
  131.             return true;
  132.         }
  133.  
  134.         int nextInt() {
  135.             return Integer.parseInt(next());
  136.         }
  137.  
  138.         long nextLong() {
  139.             return Long.parseLong(next());
  140.         }
  141.  
  142.         double nextDouble() {
  143.             return Double.parseDouble(next());
  144.         }
  145.     }
  146.  
  147.     public static void main(String[] args) {
  148.         new J2().runIO();
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment