Guest User

Untitled

a guest
Dec 19th, 2016
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.util.Arrays;
  6. import java.util.Locale;
  7. import java.util.StringTokenizer;
  8.  
  9. public class InversionsDoubles {
  10.     private void solve() throws IOException {
  11.         int n = readInt();
  12.         int[] p = new int[n];
  13.         for (int i = 0; i < n; i++) {
  14.             p[i] = readInt() - 1;
  15.         }
  16.  
  17.         long inversions = 0;
  18.         FenwickTree invTree = new FenwickTree(n);
  19.         for (int i = n - 1; i >= 0; i--) {
  20.             inversions += invTree.sum(p[i]);
  21.             invTree.add(p[i], 1);
  22.         }
  23.  
  24.         double mult = 1.0 / n / (n + 1); // 2.0 instead of 1.0 for reverse
  25.         double add = 0;
  26.         double sub = 0;
  27.         FenwickTree sumTree = new FenwickTree(n);
  28.         for (int i = n - 1; i >= 0; i--) {
  29.             add += sumTree.sum(p[i], n - 1) * mult * (i + 1);
  30.             sub += sumTree.sum(p[i]) * mult * (i + 1);
  31.             sumTree.add(p[i], n - i);
  32.         }
  33.  
  34.         double ans = inversions + add - sub;
  35.  
  36.         out.printf(Locale.US, "%.15f\n", ans);
  37.     }
  38.  
  39.     private static class FenwickTree {
  40.         private int n;
  41.         private long[] t;
  42.  
  43.         private FenwickTree(int n) {
  44.             this.n = n;
  45.             this.t = new long[n + 1];
  46.         }
  47.  
  48.         private void add(int i, long d) {
  49.             i++;
  50.             while (i <= n) {
  51.                 t[i] += d;
  52.                 i += Integer.lowestOneBit(i);
  53.             }
  54.         }
  55.  
  56.         private long sum(int r) {
  57.             r++;
  58.             long ans = 0;
  59.             while (r > 0) {
  60.                 ans += t[r];
  61.                 r -= Integer.lowestOneBit(r);
  62.             }
  63.             return ans;
  64.         }
  65.  
  66.         private long sum(int l, int r) {
  67.             return sum(r) - sum(l - 1);
  68.         }
  69.  
  70.         @Override
  71.         public String toString() {
  72.             long[] s = new long[n];
  73.             for (int i = 0; i < n; i++) {
  74.                 s[i] = sum(i, i);
  75.             }
  76.             return Arrays.toString(s);
  77.         }
  78.     }
  79.  
  80.     //------------------------------------------------------------------------------
  81.     public static void main(String[] args) {
  82.         new InversionsDoubles().run();
  83.     }
  84.  
  85.     private void run() {
  86.         try {
  87.             initIO();
  88.             solve();
  89.             in.close();
  90.             out.close();
  91.         } catch (Throwable e) {
  92.             throw new RuntimeException(e);
  93.         }
  94.     }
  95.  
  96.     private BufferedReader in;
  97.     private StringTokenizer tok;
  98.     private PrintWriter out;
  99.  
  100.     private void initIO() throws IOException {
  101.         in = new BufferedReader(new InputStreamReader(System.in));
  102.         out = new PrintWriter(System.out);
  103. //        in = new BufferedReader(new FileReader(new File("input.txt")));
  104. //        out = new PrintWriter(new File("output.txt"));
  105.     }
  106.  
  107.     private String readString() throws IOException {
  108.         while (tok == null || !tok.hasMoreTokens()) {
  109.             tok = new StringTokenizer(in.readLine());
  110.         }
  111.         return tok.nextToken();
  112.     }
  113.  
  114.     @SuppressWarnings("unused")
  115.     private int readInt() throws IOException {
  116.         return Integer.parseInt(readString());
  117.     }
  118.  
  119.     @SuppressWarnings("unused")
  120.     private long readLong() throws IOException {
  121.         return Integer.parseInt(readString());
  122.     }
  123.  
  124.     @SuppressWarnings("unused")
  125.     private double readDouble() throws IOException {
  126.         return Double.parseDouble(readString());
  127.     }
  128. }
Add Comment
Please, Sign In to add comment