Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.StringTokenizer;
  5.  
  6. public class InversionsShuffle implements Runnable {
  7.  
  8. BufferedReader in;
  9. PrintWriter out;
  10. StringTokenizer tok = new StringTokenizer("");
  11.  
  12. void init() throws FileNotFoundException {
  13. try {
  14. in = new BufferedReader(new FileReader("input.txt"));
  15. out = new PrintWriter("output.txt");
  16. } catch (Exception e) {
  17. in = new BufferedReader(new InputStreamReader(System.in));
  18. out = new PrintWriter(System.out);
  19. }
  20. }
  21.  
  22. String readString() throws IOException {
  23. while (!tok.hasMoreTokens()) {
  24. try {
  25. tok = new StringTokenizer(in.readLine(), " :");
  26. } catch (Exception e) {
  27. return null;
  28. }
  29. }
  30. return tok.nextToken();
  31. }
  32.  
  33. int readInt() throws IOException {
  34. return Integer.parseInt(readString());
  35. }
  36.  
  37. int[] readIntArray(int size) throws IOException {
  38. int[] res = new int[size];
  39. for (int i = 0; i < size; i++) {
  40. res[i] = readInt();
  41. }
  42. return res;
  43. }
  44.  
  45. long readLong() throws IOException {
  46. return Long.parseLong(readString());
  47. }
  48.  
  49. double readDouble() throws IOException {
  50. return Double.parseDouble(readString());
  51. }
  52.  
  53. <T> List<T>[] createGraphList(int size) {
  54. List<T>[] list = new List[size];
  55. for (int i = 0; i < size; i++) {
  56. list[i] = new ArrayList<>();
  57. }
  58. return list;
  59. }
  60.  
  61. public static void main(String[] args) {
  62. new Thread(null, new InversionsShuffle(), "", 1l * 200 * 1024 * 1024).start();
  63. }
  64.  
  65. long timeBegin, timeEnd;
  66.  
  67. void time() {
  68. timeEnd = System.currentTimeMillis();
  69. System.err.println("Time = " + (timeEnd - timeBegin));
  70. }
  71.  
  72. long memoryTotal, memoryFree;
  73.  
  74. void memory() {
  75. memoryFree = Runtime.getRuntime().freeMemory();
  76. System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
  77. + " KB");
  78. }
  79.  
  80. public void run() {
  81. try {
  82. timeBegin = System.currentTimeMillis();
  83. memoryTotal = Runtime.getRuntime().freeMemory();
  84. init();
  85. solve();
  86. out.close();
  87. if (System.getProperty("ONLINE_JUDGE") == null) {
  88. time();
  89. memory();
  90. }
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. System.exit(-1);
  94. }
  95. }
  96.  
  97. class Fenwick {
  98. long[] data;
  99. int n;
  100.  
  101. public Fenwick(int n) {
  102. this.n = n;
  103. data = new long[n];
  104. }
  105.  
  106. void add(int at, long val) {
  107. while (at < data.length) {
  108. data[at] += val;
  109. at |= at + 1;
  110. }
  111. }
  112.  
  113. long get(int at) {
  114. long res = 0;
  115. while (at >= 0) {
  116. res += data[at];
  117. at = (at & (at + 1)) - 1;
  118. }
  119. return res;
  120. }
  121. }
  122.  
  123. long calculateBaseInversionCount(int[] a) {
  124. long res = 0;
  125. Fenwick fenwick = new Fenwick(a.length + 1);
  126. for (int i = a.length - 1; i >= 0; i--) {
  127. res += fenwick.get(a[i]);
  128. fenwick.add(a[i], 1);
  129. }
  130. return res;
  131. }
  132.  
  133. double averageInversionsCount(int k) {
  134. return (double) k * (k - 1) / 4;
  135. }
  136.  
  137. void solve() throws IOException {
  138. int n = readInt();
  139. int[] a = readIntArray(n);
  140. Fenwick fenwick = new Fenwick(n + 1);
  141. double countOfSegments = (long) n * (n + 1) / 2;
  142. double expectedValueOfDifference = 0;
  143. long countInversionsOnSuffix = 0;
  144. double sumOfExpectedValues = 0;
  145. for (int i = n - 1; i >= 0; i--) {
  146. long lowerRight = fenwick.get(a[i]);
  147. countInversionsOnSuffix += lowerRight;
  148. sumOfExpectedValues += averageInversionsCount(n - i);
  149. expectedValueOfDifference += (sumOfExpectedValues - countInversionsOnSuffix) / countOfSegments;
  150. fenwick.add(a[i], n - i);
  151. }
  152. out.println(calculateBaseInversionCount(a) + expectedValueOfDifference);
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement