Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.StringTokenizer;
- public class InversionsShuffle implements Runnable {
- BufferedReader in;
- PrintWriter out;
- StringTokenizer tok = new StringTokenizer("");
- void init() throws FileNotFoundException {
- try {
- in = new BufferedReader(new FileReader("input.txt"));
- out = new PrintWriter("output.txt");
- } catch (Exception e) {
- in = new BufferedReader(new InputStreamReader(System.in));
- out = new PrintWriter(System.out);
- }
- }
- String readString() throws IOException {
- while (!tok.hasMoreTokens()) {
- try {
- tok = new StringTokenizer(in.readLine(), " :");
- } catch (Exception e) {
- return null;
- }
- }
- return tok.nextToken();
- }
- int readInt() throws IOException {
- return Integer.parseInt(readString());
- }
- int[] readIntArray(int size) throws IOException {
- int[] res = new int[size];
- for (int i = 0; i < size; i++) {
- res[i] = readInt();
- }
- return res;
- }
- long readLong() throws IOException {
- return Long.parseLong(readString());
- }
- double readDouble() throws IOException {
- return Double.parseDouble(readString());
- }
- <T> List<T>[] createGraphList(int size) {
- List<T>[] list = new List[size];
- for (int i = 0; i < size; i++) {
- list[i] = new ArrayList<>();
- }
- return list;
- }
- public static void main(String[] args) {
- new Thread(null, new InversionsShuffle(), "", 1l * 200 * 1024 * 1024).start();
- }
- long timeBegin, timeEnd;
- void time() {
- timeEnd = System.currentTimeMillis();
- System.err.println("Time = " + (timeEnd - timeBegin));
- }
- long memoryTotal, memoryFree;
- void memory() {
- memoryFree = Runtime.getRuntime().freeMemory();
- System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
- + " KB");
- }
- public void run() {
- try {
- timeBegin = System.currentTimeMillis();
- memoryTotal = Runtime.getRuntime().freeMemory();
- init();
- solve();
- out.close();
- if (System.getProperty("ONLINE_JUDGE") == null) {
- time();
- memory();
- }
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(-1);
- }
- }
- class Fenwick {
- long[] data;
- int n;
- public Fenwick(int n) {
- this.n = n;
- data = new long[n];
- }
- void add(int at, long val) {
- while (at < data.length) {
- data[at] += val;
- at |= at + 1;
- }
- }
- long get(int at) {
- long res = 0;
- while (at >= 0) {
- res += data[at];
- at = (at & (at + 1)) - 1;
- }
- return res;
- }
- }
- long calculateBaseInversionCount(int[] a) {
- long res = 0;
- Fenwick fenwick = new Fenwick(a.length + 1);
- for (int i = a.length - 1; i >= 0; i--) {
- res += fenwick.get(a[i]);
- fenwick.add(a[i], 1);
- }
- return res;
- }
- double averageInversionsCount(int k) {
- return (double) k * (k - 1) / 4;
- }
- void solve() throws IOException {
- int n = readInt();
- int[] a = readIntArray(n);
- Fenwick fenwick = new Fenwick(n + 1);
- double countOfSegments = (long) n * (n + 1) / 2;
- double expectedValueOfDifference = 0;
- long countInversionsOnSuffix = 0;
- double sumOfExpectedValues = 0;
- for (int i = n - 1; i >= 0; i--) {
- long lowerRight = fenwick.get(a[i]);
- countInversionsOnSuffix += lowerRight;
- sumOfExpectedValues += averageInversionsCount(n - i);
- expectedValueOfDifference += (sumOfExpectedValues - countInversionsOnSuffix) / countOfSegments;
- fenwick.add(a[i], n - i);
- }
- out.println(calculateBaseInversionCount(a) + expectedValueOfDifference);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement