(UVA) Where is the Marble - Solution 2
The only difference between this solution and Solution 1 is that here I am doing the Binary Search recursively.
import java.io.*;
import java.util.*;
class Main {
public static int binarySearch(int[] marbles, int lo, int hi, int query, int index) {
if (lo > hi) {
return index;
}
else {
int mi = (lo+hi)/2;
if (marbles[mi] > query) {
return binarySearch(marbles, lo, mi-1, query, index);
}
else if (marbles[mi] < query) {
return binarySearch(marbles, mi+1, hi, query, index);
}
else {
return binarySearch(marbles, lo, mi-1, query, mi);
}
}
}
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));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numMarbles = reader(br);
int numQueries = reader(br);
int numCase = 0;
while (numMarbles != 0 || numQueries != 0) {
int[] marbles = new int[numMarbles];
for (int i = 0; i < numMarbles; i++) {
marbles[i] = reader(br);
}
Arrays.sort(marbles);
bw.write("CASE# " + ++numCase +":\n");
for (int i = 0; i < numQueries; i++) {
int query = reader(br);
int index = binarySearch(marbles, 0, numMarbles-1, query, -1);
if (index != -1) {
bw.write(query + " found at " + (index+1) + "\n");
}
else {
bw.write(query + " not found\n");
}
}
numMarbles = reader(br);
numQueries = reader(br);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.io.*;
import java.util.*;
class Main {
public static int binarySearch(int[] marbles, int lo, int hi, int query, int index) {
if (lo > hi) {
return index;
}
else {
int mi = (lo+hi)/2;
if (marbles[mi] > query) {
return binarySearch(marbles, lo, mi-1, query, index);
}
else if (marbles[mi] < query) {
return binarySearch(marbles, mi+1, hi, query, index);
}
else {
return binarySearch(marbles, lo, mi-1, query, mi);
}
}
}
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));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numMarbles = reader(br);
int numQueries = reader(br);
int numCase = 0;
while (numMarbles != 0 || numQueries != 0) {
int[] marbles = new int[numMarbles];
for (int i = 0; i < numMarbles; i++) {
marbles[i] = reader(br);
}
Arrays.sort(marbles);
bw.write("CASE# " + ++numCase +":\n");
for (int i = 0; i < numQueries; i++) {
int query = reader(br);
int index = binarySearch(marbles, 0, numMarbles-1, query, -1);
if (index != -1) {
bw.write(query + " found at " + (index+1) + "\n");
}
else {
bw.write(query + " not found\n");
}
}
numMarbles = reader(br);
numQueries = reader(br);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment