Advertisement
ec1117

Untitled

Jun 30th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 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 balancing{
  7. public static final String TASKNAME = "balancing";
  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. HashMap<Integer,HashSet<Integer>> set=new HashMap<Integer,HashSet<Integer>>();
  23. static int n;
  24. static int pre[]=new int[1000001];
  25. public void solve(int testNumber, InputReader in, PrintWriter out) {
  26. n=in.nextInt();
  27. for(int i=0;i<n;i++) {
  28. int x=in.nextInt();
  29. int y=in.nextInt();
  30. pre[x]++;
  31. if(set.containsKey(y)) {
  32. set.get(y).add(x);
  33. }
  34. else {
  35. set.put(y, new HashSet<Integer>());
  36. set.get(y).add(x);
  37. }
  38. }
  39. for(int i=1;i<pre.length;i++) {
  40. pre[i]+=pre[i-1];
  41. }
  42. db.debug("set", set);
  43. int l=1;
  44. int r=n+1;
  45. while(l!=r) {
  46. int mid=l+(r-l)/2;//search for if mid is possible as minimum
  47. if(works(mid)) {
  48. r=mid;
  49. }
  50. else {
  51. l=mid+1;
  52. }
  53. }
  54. out.println(l);
  55. }
  56. private boolean works(int mid) {
  57. bit bit=new bit(1000000);
  58. int cnt=0;//number of points below line, quadrant 3 and 4
  59. for(int y:set.keySet()) {//horizontal line to get all points below
  60. for(int x:set.get(y)) {
  61. bit.update(x-1, 1);
  62. cnt++;
  63. }
  64. int l=1;
  65. int r=1000001;
  66. while(l!=r) {//searching for best vertical line
  67. int mid2=(l+r+1)/2;
  68. int temp=(int) bit.sum(mid2+1);//finding max number under mid in quadrant 3
  69. if(temp<=mid && pre[Math.min(mid2, pre.length-1)]-temp<=mid) {
  70. l=mid2;
  71. }
  72. else {
  73. r=mid2-1;
  74. }
  75. }
  76.  
  77. int temp2=(int) bit.sum(l+1);//quad 3
  78. int temp3=pre[Math.min(l, pre.length-1)]-temp2;//quadrant 2
  79. int temp4=n-(temp3)-cnt;//quad 1
  80. // System.out.println(y+" "+l+" "+cnt+" "+mid+" "+temp2+" "+(cnt-temp2)+" "+temp3+" "+temp4);
  81. if(Math.max(temp2, Math.max(cnt-temp2, Math.max(temp3, temp4)))<=mid) {
  82. // System.out.println(mid);
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. }
  89. static class bit {
  90. public int[] tree;
  91. public bit(int n) {
  92. tree = new int[n+5];
  93. }
  94. public void update(int index, int val) {
  95. index++;
  96. while(index < tree.length) {
  97. tree[index] += val;
  98. index += index & -index;
  99. }
  100. }
  101. public long sum(int i) {
  102. long ans = 0;
  103. while (i > 0) {
  104. ans += tree[i];
  105. i = i - (i & (-i));
  106. }
  107. return ans;
  108. }
  109.  
  110. public long queryRange(int i, int j) {
  111. return sum(j) - sum(i - 1);
  112. }
  113. }
  114. static class Pair implements Comparable<Pair>{
  115. int x;
  116. int y;
  117. Pair(int a, int b){
  118. x=a;
  119. y=b;
  120. }
  121. @Override
  122. public int compareTo(Pair arg0) {
  123. if(arg0.x!=x)return x-arg0.x;
  124. return y-arg0.y;
  125. }
  126. }
  127. static class Triple implements Comparable<Triple>{
  128. int x;
  129. int y;
  130. int z;
  131. Triple(int a, int b, int c){
  132. x=a;
  133. y=b;
  134. z=c;
  135. }
  136. @Override
  137. public int compareTo(Triple arg0) {
  138. if(arg0.x!=x)return x-arg0.x;
  139. return y-arg0.y;
  140. }
  141. }
  142.  
  143. static class InputReader {
  144. public BufferedReader reader;
  145. public StringTokenizer tokenizer;
  146.  
  147. public InputReader(InputStream stream) {
  148. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  149. tokenizer = null;
  150. }
  151.  
  152. public String next() {
  153. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  154. try {
  155. tokenizer = new StringTokenizer(reader.readLine());
  156. } catch (IOException e) {
  157. throw new RuntimeException(e);
  158. }
  159. }
  160. return tokenizer.nextToken();
  161. }
  162.  
  163. public int nextInt() {
  164. return Integer.parseInt(next());
  165. }
  166. public long nextLong() {
  167. return Long.parseLong(next());
  168. }
  169. public int[] nextIntArr(int n) {
  170. int arr[]=new int[n];
  171. for(int i=0;i<n;i++) {
  172. arr[i]=this.nextInt();
  173. }
  174. return arr;
  175. }
  176. }
  177. public static class Debug {
  178. private boolean allowDebug;
  179.  
  180. public Debug(boolean allowDebug) {
  181. this.allowDebug = allowDebug;
  182. }
  183.  
  184. private void outputName(String name) {
  185. System.out.print(name + " = ");
  186. }
  187.  
  188. public void debug(String name, int x) {
  189. if (!allowDebug) {
  190. return;
  191. }
  192.  
  193. outputName(name);
  194. System.out.println("" + x);
  195. }
  196.  
  197. public void debug(String name, long x) {
  198. if (!allowDebug) {
  199. return;
  200. }
  201. outputName(name);
  202. System.out.println("" + x);
  203. }
  204.  
  205. public void debug(String name, double x) {
  206. if (!allowDebug) {
  207. return;
  208. }
  209. outputName(name);
  210. System.out.println("" + x);
  211. }
  212.  
  213. public void debug(String name, int[] x) {
  214. if (!allowDebug) {
  215. return;
  216. }
  217. outputName(name);
  218. System.out.println(Arrays.toString(x));
  219. }
  220.  
  221. public void debug(String name, long[] x) {
  222. if (!allowDebug) {
  223. return;
  224. }
  225. outputName(name);
  226. System.out.println(Arrays.toString(x));
  227. }
  228.  
  229. public void debug(String name, double[] x) {
  230. if (!allowDebug) {
  231. return;
  232. }
  233. outputName(name);
  234. System.out.println(Arrays.toString(x));
  235. }
  236.  
  237. public void debug(String name, Object x) {
  238. if (!allowDebug) {
  239. return;
  240. }
  241. outputName(name);
  242. System.out.println("" + x);
  243. }
  244.  
  245. public void debug(String name, Object... x) {
  246. if (!allowDebug) {
  247. return;
  248. }
  249. outputName(name);
  250. System.out.println(Arrays.deepToString(x));
  251. }
  252. }
  253. }
  254. //if(cnt>2*mid) {//saves time
  255. //return false;
  256. //}
  257. //if(n-cnt>2*mid) {//saves time
  258. //continue;
  259. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement