Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.io.BufferedReader;
- import java.awt.Point;
- public class balancing{
- public static final String TASKNAME = "balancing";
- public static long mod= 1000000007;
- public static Debug db;
- public static void main(String[] args) throws IOException {
- // InputReader in = new InputReader(System.in);
- // PrintWriter out = new PrintWriter(System.out);
- InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
- Autocompletion solver = new Autocompletion();
- db=new Debug(System.getSecurityManager()==null);
- solver.solve(1, in, out);
- out.close();
- }
- static class Autocompletion {
- HashMap<Integer,HashSet<Integer>> set=new HashMap<Integer,HashSet<Integer>>();
- static int n;
- static int pre[]=new int[1000001];
- public void solve(int testNumber, InputReader in, PrintWriter out) {
- n=in.nextInt();
- for(int i=0;i<n;i++) {
- int x=in.nextInt();
- int y=in.nextInt();
- pre[x]++;
- if(set.containsKey(y)) {
- set.get(y).add(x);
- }
- else {
- set.put(y, new HashSet<Integer>());
- set.get(y).add(x);
- }
- }
- for(int i=1;i<pre.length;i++) {
- pre[i]+=pre[i-1];
- }
- db.debug("set", set);
- int l=1;
- int r=n+1;
- while(l!=r) {
- int mid=l+(r-l)/2;//search for if mid is possible as minimum
- if(works(mid)) {
- r=mid;
- }
- else {
- l=mid+1;
- }
- }
- out.println(l);
- }
- private boolean works(int mid) {
- bit bit=new bit(1000000);
- int cnt=0;//number of points below line, quadrant 3 and 4
- for(int y:set.keySet()) {//horizontal line to get all points below
- for(int x:set.get(y)) {
- bit.update(x-1, 1);
- cnt++;
- }
- int l=1;
- int r=1000001;
- while(l!=r) {//searching for best vertical line
- int mid2=(l+r+1)/2;
- int temp=(int) bit.sum(mid2+1);//finding max number under mid in quadrant 3
- if(temp<=mid && pre[Math.min(mid2, pre.length-1)]-temp<=mid) {
- l=mid2;
- }
- else {
- r=mid2-1;
- }
- }
- int temp2=(int) bit.sum(l+1);//quad 3
- int temp3=pre[Math.min(l, pre.length-1)]-temp2;//quadrant 2
- int temp4=n-(temp3)-cnt;//quad 1
- // System.out.println(y+" "+l+" "+cnt+" "+mid+" "+temp2+" "+(cnt-temp2)+" "+temp3+" "+temp4);
- if(Math.max(temp2, Math.max(cnt-temp2, Math.max(temp3, temp4)))<=mid) {
- // System.out.println(mid);
- return true;
- }
- }
- return false;
- }
- }
- static class bit {
- public int[] tree;
- public bit(int n) {
- tree = new int[n+5];
- }
- public void update(int index, int val) {
- index++;
- while(index < tree.length) {
- tree[index] += val;
- index += index & -index;
- }
- }
- public long sum(int i) {
- long ans = 0;
- while (i > 0) {
- ans += tree[i];
- i = i - (i & (-i));
- }
- return ans;
- }
- public long queryRange(int i, int j) {
- return sum(j) - sum(i - 1);
- }
- }
- static class Pair implements Comparable<Pair>{
- int x;
- int y;
- Pair(int a, int b){
- x=a;
- y=b;
- }
- @Override
- public int compareTo(Pair arg0) {
- if(arg0.x!=x)return x-arg0.x;
- return y-arg0.y;
- }
- }
- static class Triple implements Comparable<Triple>{
- int x;
- int y;
- int z;
- Triple(int a, int b, int c){
- x=a;
- y=b;
- z=c;
- }
- @Override
- public int compareTo(Triple arg0) {
- if(arg0.x!=x)return x-arg0.x;
- return y-arg0.y;
- }
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public int[] nextIntArr(int n) {
- int arr[]=new int[n];
- for(int i=0;i<n;i++) {
- arr[i]=this.nextInt();
- }
- return arr;
- }
- }
- public static class Debug {
- private boolean allowDebug;
- public Debug(boolean allowDebug) {
- this.allowDebug = allowDebug;
- }
- private void outputName(String name) {
- System.out.print(name + " = ");
- }
- public void debug(String name, int x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, long x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, double x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, int[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, long[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, double[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, Object x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, Object... x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.deepToString(x));
- }
- }
- }
- //if(cnt>2*mid) {//saves time
- //return false;
- //}
- //if(n-cnt>2*mid) {//saves time
- //continue;
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement