(UVA) The Playboy Chimp - Solution

I used Binary Search to solve this problem.


import java.io.*;
import java.util.*;

class Main {
    public static int[] ladies;
    public static boolean found;
    public static int index;
   
    public static int binarySearchLo(int lo, int hi, int chimpHeight) {
        if (lo > hi) {
            if (found) {
                return index-1;
            }
            else {
                return hi;
            }
        }

        int mi = (lo+hi)/2;
        if (ladies[mi] > chimpHeight) {
            return binarySearchLo(lo, mi-1, chimpHeight);
        }
        else if (ladies[mi] < chimpHeight) {
            return binarySearchLo(mi+1, hi, chimpHeight);
        }
        else {
            found = true;
            index = mi;
            return binarySearchLo(lo, mi-1, chimpHeight);
        }
    }
   
    public static int binarySearchHi(int lo, int hi, int chimpHeight) {
        if (lo > hi) {
            if (found) {
                return index+1;
            }
            else {
                return lo;
            }
        }
       
        int mi = (lo+hi)/2;
        if (ladies[mi] > chimpHeight) {
            return binarySearchHi(lo, mi-1, chimpHeight);
        }
        else if (ladies[mi] < chimpHeight) {
            return binarySearchHi(mi+1, hi, chimpHeight);
        }
        else {
            found = true;
            index = mi;
            return binarySearchHi(mi+1, hi, chimpHeight);
        }
    }
   
    public static int reader (BufferedReader br) throws NumberFormatException, IOException {
        int n;
        int resp = 0;
        
        while (true) {
            n = br.read();
            if (n >= '0' && n <= '9') break;
        }
        while (true) {
            resp = resp*10 + n-'0';
            n = br.read();
            if (n < '0' || n > '9') break;
        }
        
        return resp;
    }
   
    public static void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        int numLadies = reader(br);
        ladies = new int[numLadies];
        for (int i = 0; i < numLadies; i++) {
            ladies[i] = reader(br);
        }
       
        int numChimp = reader(br);
        for (int i = 0; i < numChimp; i++) {
            int chimpHeight = reader(br);
            found = false;
            index = -1;
            int lo = binarySearchLo(0, numLadies-1, chimpHeight);
            found = false;
            index = -1;
            int hi = binarySearchHi(0, numLadies-1, chimpHeight);
           
            if ((lo < 0 || lo >= numLadies) && (hi >= numLadies || hi < 0)) {
                System.out.println("X X");
            }
            else if (lo < 0) {
                System.out.println("X " + ladies[hi]);
            }
            else if (hi >= numLadies) {
                System.out.println(ladies[lo] + " X");
            }
            else {
                System.out.println(ladies[lo] + " " + ladies[hi]);
            }
        }
       
       
                                             
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução