qwerty787788

Random vectors

Jan 25th, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class CF {
  5.  
  6.     class Thing implements Comparable<Thing> {
  7.         int id;
  8.         double d;
  9.  
  10.         Thing(int id, double d) {
  11.             this.id = id;
  12.             this.d = d;
  13.         }
  14.  
  15.         @Override
  16.         public int compareTo(Thing o) {
  17.             return Double.compare(o.d, d);
  18.         }
  19.  
  20.     }
  21.  
  22.     void solve() {
  23.         int n = in.nextInt();
  24.         int[] x = new int[n];
  25.         int[] y = new int[n];
  26.         for (int i = 0; i < n; i++) {
  27.             x[i] = in.nextInt();
  28.             y[i] = in.nextInt();
  29.         }
  30.         long[] ans = new long[n + 1];
  31.         Random rnd = new Random(123);
  32.         Thing[] a = new Thing[n];
  33.         for (int i = 0; i < n; i++)
  34.             a[i] = new Thing(i, 0);
  35.         for (int ITER = 0; ITER < 5000; ITER++) {
  36.             double angle = rnd.nextDouble() * Math.PI * 2;
  37.             double x1 = Math.cos(angle);
  38.             double y1 = Math.sin(angle);
  39.             for (int i = 0; i < n; i++) {
  40.                 a[i].id = i;
  41.                 a[i].d = x1 * x[i] + y1 * y[i];
  42.             }
  43.             Arrays.sort(a);
  44.             long nowX = 0, nowY = 0;
  45.             for (int i = 0; i < n; i++) {
  46.                 nowX += x[a[i].id];
  47.                 nowY += y[a[i].id];
  48.                 long dist = nowX * nowX + nowY * nowY;
  49.                 ans[i + 1] = Math.max(ans[i + 1], dist);
  50.             }
  51.         }
  52.         for (int i = 1; i <= n; i++) {
  53.             out.println(Math.sqrt(ans[i]));
  54.         }
  55.     }
  56.  
  57.     FastScaner in;
  58.     PrintWriter out;
  59.  
  60.     void run() {
  61.         in = new FastScaner(System.in);
  62.         out = new PrintWriter(System.out);
  63.  
  64.         solve();
  65.  
  66.         out.close();
  67.     }
  68.  
  69.     void runWithFiles() {
  70.         in = new FastScaner(new File("input.txt"));
  71.         try {
  72.             out = new PrintWriter(new File("output.txt"));
  73.         } catch (FileNotFoundException e) {
  74.             e.printStackTrace();
  75.         }
  76.  
  77.         solve();
  78.  
  79.         out.close();
  80.     }
  81.  
  82.     public static void main(String[] args) {
  83.         Locale.setDefault(Locale.US);
  84.         new CF().run();
  85.     }
  86.  
  87.     class FastScaner {
  88.         BufferedReader br;
  89.         StringTokenizer st;
  90.  
  91.         FastScaner(InputStream is) {
  92.             br = new BufferedReader(new InputStreamReader(is));
  93.         }
  94.  
  95.         FastScaner(File f) {
  96.             try {
  97.                 br = new BufferedReader(new FileReader(f));
  98.             } catch (FileNotFoundException e) {
  99.                 e.printStackTrace();
  100.             }
  101.         }
  102.  
  103.         String next() {
  104.             while (st == null || !st.hasMoreElements()) {
  105.                 String s = null;
  106.                 try {
  107.                     s = br.readLine();
  108.                 } catch (IOException e) {
  109.                     e.printStackTrace();
  110.                 }
  111.                 if (s == null)
  112.                     return null;
  113.                 st = new StringTokenizer(s);
  114.             }
  115.             return st.nextToken();
  116.         }
  117.  
  118.         int nextInt() {
  119.             return Integer.parseInt(next());
  120.         }
  121.  
  122.         long nextLong() {
  123.             return Long.parseLong(next());
  124.         }
  125.  
  126.         double nextDouble() {
  127.             return Double.parseDouble(next());
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment