Advertisement
AdelKhalilov

Untitled

Jan 10th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Locale;
  3. import java.util.Random;
  4. import java.util.StringTokenizer;
  5.  
  6. public class F {
  7. String filename = "";//filename here, System.in/out if no file
  8.  
  9. FastScanner in;
  10. PrintWriter out;
  11. int[] a;
  12. long[] a_copy;
  13. int n;
  14. int s;
  15.  
  16. static long kth(long[] array, int l, int r, int k) {
  17. final Random random = new Random();
  18. int i = l;
  19. int j = r;
  20. long x = array[l + random.nextInt(r - l + 1)];
  21. while (i <= j) {
  22. while (array[i] < x) {
  23. i++;
  24. }
  25. while (array[j] > x) {
  26. j--;
  27. }
  28. if (i <= j) {
  29. long temp = array[i];
  30. array[i] = array[j];
  31. array[j] = temp;
  32. i++;
  33. j--;
  34. }
  35. }
  36. if (l <= k && k <= j) {
  37. return kth(array, l, j, k);
  38. }
  39. if (i <= k && k <= r) {
  40. return kth(array, i, r, k);
  41. }
  42. return array[k];
  43. }
  44.  
  45. long return_666(long rz) {
  46. for (int i = 0; i < a.length; i++) {
  47. a_copy[i] = a[i];
  48. }
  49. for (int i = 0; i < n; i++) {
  50. a_copy[i] = a_copy[i] - i * rz;
  51. }
  52. long s = kth(a_copy, 0, n - 1, n / 2);
  53. return s;
  54. }
  55.  
  56. long f(long raz) {
  57. long sum = 0;
  58. for (int i = 0; i < a.length; i++) {
  59. a_copy[i] = a[i];
  60. }
  61. for (int i = 0; i < n; i++) {
  62. a_copy[i] = a_copy[i] - (long) i * (long) raz;
  63. }
  64. long s = kth(a_copy, 0, n - 1, n / 2);
  65. for (int i = 0; i < n; i++) {
  66. long a = sum;
  67. long cur = Math.abs(a_copy[i] - s);
  68. sum += cur;
  69. if (sum - cur != a || sum - a != cur) {
  70. return Long.MAX_VALUE;
  71. }
  72. }
  73. return sum;
  74. }
  75.  
  76. void solve() {
  77. //your code here
  78. n = in.nextInt();
  79. a = new int[n];
  80. a_copy = new long[n];
  81. for (int i = 0; i < n; i++) {
  82. a[i] = in.nextInt();
  83. a_copy[i] = a[i];
  84. }
  85. int d = 0;
  86. s = 0;
  87. long l = -1000 * 1000 * 1000;
  88. long r = 1000 * 1000 * 1000;
  89. if (n <= 10000) {
  90. l = -2 * 1000 * 1000 * 1000 - 7;
  91. r = 2 * 1000 * 1000 * 1000 + 7;
  92. } else {
  93. if (n <= 100 * 1000) {
  94. l = -1000 * 10000 - 7;
  95. r = 1000 * 10000 + 1;
  96. } else {
  97. l = -100 * 1000;
  98. r = 100 * 1000;
  99. }
  100. }
  101. while (r - l > 1) {
  102. long m = (l + r) / 2;
  103. if (f(m + 1) < f(m)) {
  104. l = m;
  105. } else {
  106. r = m;
  107. }
  108. }
  109. out.print(return_666(l + 1) + " " + (l + 1));
  110. }
  111.  
  112. void run() throws IOException {
  113. InputStream input = System.in;
  114. OutputStream output = System.out;
  115. /* try {
  116. File f = new File(filename + ".in");
  117. if (f.exists() && f.canRead()) {
  118. input = new FileInputStream(f);
  119. output = new FileOutputStream(filename + ".out");
  120. }
  121. } catch (IOException e) {
  122. }*/
  123. in = new FastScanner(input);
  124. out = new PrintWriter(new BufferedOutputStream(output));
  125. solve();
  126. in.close();
  127. out.close();
  128. }
  129.  
  130. public static void main(String[] args) throws IOException {
  131. Locale.setDefault(Locale.US);
  132. new F().run();
  133. }
  134.  
  135. class FastScanner implements Closeable {
  136. private BufferedReader br;
  137. private StringTokenizer tokenizer;
  138.  
  139. public FastScanner(InputStream stream) throws FileNotFoundException {
  140. br = new BufferedReader(new InputStreamReader(stream));
  141. }
  142.  
  143. public String next() {
  144. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  145. try {
  146. tokenizer = new StringTokenizer(br.readLine());
  147. } catch (IOException e) {
  148. throw new RuntimeException(e);
  149. }
  150. }
  151. return
  152. tokenizer.nextToken();
  153. }
  154.  
  155. public String nextLine() {
  156. if (tokenizer == null || !tokenizer.hasMoreTokens()) {
  157. try {
  158. return br.readLine();
  159. } catch (IOException e) {
  160. throw new RuntimeException(e);
  161. }
  162. }
  163. return tokenizer.nextToken("\n");
  164. }
  165.  
  166. public int nextInt() {
  167. return Integer.parseInt(next());
  168. }
  169.  
  170. public long nextLong() {
  171. return Long.parseLong(next());
  172. }
  173.  
  174. public double nextDouble() {
  175. return
  176. Double.parseDouble(next());
  177. }
  178.  
  179. @Override
  180. public void close() throws IOException {
  181. br.close();
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement