Advertisement
ec1117

Untitled

Jun 21st, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.io.BufferedReader;
  4. import java.awt.Point;
  5.  
  6. public class sort{
  7. public static final String TASKNAME = "sort";
  8. public static long mod= 1000000007;
  9. public static Debug db;
  10.  
  11. public static void main(String[] args) throws IOException {
  12. InputReader in = new InputReader(System.in);
  13. PrintWriter out = new PrintWriter(System.out);
  14. // InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
  15. // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
  16. Autocompletion solver = new Autocompletion();
  17. db=new Debug(System.getSecurityManager()==null);
  18. solver.solve(1, in, out);
  19. out.close();
  20. }
  21. static class Autocompletion {
  22.  
  23. public void solve(int testNumber, InputReader in, PrintWriter out) {
  24. int n=in.nextInt();
  25. Pair[] arr=new Pair[n];
  26. for(int i=0;i<n;i++) {
  27. arr[i]=new Pair(in.nextInt(),i);
  28. }
  29. Arrays.sort(arr);
  30. int news[]=new int[n];
  31. for(int i=0;i<n;i++) {
  32. news[arr[i].y]=i;
  33. }
  34. // db.debug("news", news);
  35. bit bit=new bit(n);
  36. int ret=0;
  37. for(int i=0;i<n;i++) {
  38. bit.update(news[i], 1);
  39. ret=(int) Math.max(ret, i+1-bit.sum(i));
  40.  
  41. }
  42. out.println(ret);
  43. }
  44. }
  45. static class bit {
  46. public int[] tree;
  47. public bit(int n) {
  48. tree = new int[n+5];
  49. }
  50. public void update(int index, int val) {
  51. index++;
  52. while(index < tree.length) {
  53. tree[index] += val;
  54. index += index & -index;
  55. }
  56. }
  57. public long sum(int i) {
  58. long ans = 0;
  59. while (i > 0) {
  60. ans += tree[i];
  61. i = i - (i & (-i));
  62. }
  63. return ans;
  64. }
  65.  
  66. public long queryRange(int i, int j) {
  67. return sum(j) - sum(i - 1);
  68. }
  69. }
  70. static class Pair implements Comparable<Pair>{
  71. int x;
  72. int y;
  73. Pair(int a, int b){
  74. x=a;
  75. y=b;
  76. }
  77. @Override
  78. public int compareTo(Pair arg0) {
  79. if(arg0.x!=x)return x-arg0.x;
  80. return y-arg0.y;
  81. }
  82. }
  83.  
  84. static class InputReader {
  85. public BufferedReader reader;
  86. public StringTokenizer tokenizer;
  87.  
  88. public InputReader(InputStream stream) {
  89. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  90. tokenizer = null;
  91. }
  92.  
  93. public String next() {
  94. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  95. try {
  96. tokenizer = new StringTokenizer(reader.readLine());
  97. } catch (IOException e) {
  98. throw new RuntimeException(e);
  99. }
  100. }
  101. return tokenizer.nextToken();
  102. }
  103.  
  104. public int nextInt() {
  105. return Integer.parseInt(next());
  106. }
  107. public long nextLong() {
  108. return Long.parseLong(next());
  109. }
  110. public int[] nextIntArr(int n) {
  111. int arr[]=new int[n];
  112. for(int i=0;i<n;i++) {
  113. arr[i]=this.nextInt();
  114. }
  115. return arr;
  116. }
  117. }
  118. public static class Debug {
  119. private boolean allowDebug;
  120.  
  121. public Debug(boolean allowDebug) {
  122. this.allowDebug = allowDebug;
  123. }
  124.  
  125. private void outputName(String name) {
  126. System.out.print(name + " = ");
  127. }
  128.  
  129. public void debug(String name, int x) {
  130. if (!allowDebug) {
  131. return;
  132. }
  133.  
  134. outputName(name);
  135. System.out.println("" + x);
  136. }
  137.  
  138. public void debug(String name, long x) {
  139. if (!allowDebug) {
  140. return;
  141. }
  142. outputName(name);
  143. System.out.println("" + x);
  144. }
  145.  
  146. public void debug(String name, double x) {
  147. if (!allowDebug) {
  148. return;
  149. }
  150. outputName(name);
  151. System.out.println("" + x);
  152. }
  153.  
  154. public void debug(String name, int[] x) {
  155. if (!allowDebug) {
  156. return;
  157. }
  158. outputName(name);
  159. System.out.println(Arrays.toString(x));
  160. }
  161.  
  162. public void debug(String name, long[] x) {
  163. if (!allowDebug) {
  164. return;
  165. }
  166. outputName(name);
  167. System.out.println(Arrays.toString(x));
  168. }
  169.  
  170. public void debug(String name, double[] x) {
  171. if (!allowDebug) {
  172. return;
  173. }
  174. outputName(name);
  175. System.out.println(Arrays.toString(x));
  176. }
  177.  
  178. public void debug(String name, Object x) {
  179. if (!allowDebug) {
  180. return;
  181. }
  182. outputName(name);
  183. System.out.println("" + x);
  184. }
  185.  
  186. public void debug(String name, Object... x) {
  187. if (!allowDebug) {
  188. return;
  189. }
  190. outputName(name);
  191. System.out.println(Arrays.deepToString(x));
  192. }
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement