Cod sursa(job #1673604)

Utilizator roxana.maria3Roxana Domnisoru roxana.maria3 Data 3 aprilie 2016 23:18:47
Problema Cautare binara Scor 30
Compilator java Status done
Runda Arhiva educationala Marime 3.21 kb
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main{
	private int N, M;
	private int[] vector;
	
	public void print(){
		for(Integer nr: vector)
			System.out.print(nr + " ");
		System.out.println();
	}
	
	private int maxIndex(int st, int end, int x){
		
		if(st > end)
			return -1;
		int mid = st + (end-st)/2;
		
		if(vector[mid] == x){
			int pos = mid;
			while(vector[pos] == x)
				pos++;
			return pos;
		}
		
		else if(x < vector[mid])
			return maxIndex(st, mid-1, x);
		
		return maxIndex(mid+1, end, x);
	}
	
	private int maxIndexMin(int st, int end, int x){
		
		int mid = st + (end-st)/2;
		
		if(vector[mid] == x){
			int pos = mid;
			while(pos < N && vector[pos] == x)
				pos++;
			return pos;
		}
		
		else if( mid+1 == N || (vector[mid] < x && vector[mid+1] > x))
			return mid+1;
		
		else if(x < vector[mid])
			return maxIndexMin(st, mid-1, x);
		
		return maxIndexMin(mid+1, end, x);
		
	}
	
private int minIndex(int st, int end, int x){
		
		int mid = st + (end-st)/2;
		
		if(vector[mid] == x){
			int pos = mid;
			while(pos>0 && vector[pos] == x)
				pos--;
			return pos+2;
		}
		
		else if(mid==0|| (vector[mid] > x && vector[mid-1] < x))
			return mid+1;
		
		else if(x < vector[mid])
			return minIndex(st, mid-1, x);
		
		return minIndex(mid+1, end, x);
		
	}
	public void read(){
		try{
			BufferedReader in = new BufferedReader(new FileReader("cautbin.in"));
			PrintWriter out = new PrintWriter("cautbin.out");
			
			
			String line = in.readLine();
			N = Integer.valueOf(line);
			
			line = in.readLine();
			vector = new int[N];
			String[] nums = line.split(" ");
			for(int i=0; i<N; i++)
				vector[i] = Integer.valueOf(nums[i]);
			
			line = in.readLine();
			M = Integer.valueOf(line);

			for(int i=0; i<M; i++){
				line = in.readLine();
				nums = line.split(" ");
				int act = Integer.valueOf(nums[0]);
				int x = Integer.valueOf(nums[1]);
				switch(act){
				case 0:{
					int pos = this.maxIndex(0, N-1, x);
					out.println(pos);
					break;
				}
				case 1:{
					int pos = this.maxIndexMin(0, N-1, x);
					out.println(pos);
					break;
				}
				case 2:{
					int pos = this.minIndex(0, N-1, x);
					out.println(pos);
					break;
				}
				}
			}
			
			in.close();
			out.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public void read2(){
		try{
			Scanner in = new Scanner(new FileReader("cautbin.in"));
			PrintWriter out = new PrintWriter("cautbin.out");
			
			
			N = in.nextInt();
			
			vector = new int[N];
			for(int i=0; i<N; i++)
				vector[i] = in.nextInt();
			
			M = in.nextInt();

			for(int i=0; i<M; i++){
				int act = in.nextInt();
				int x = in.nextInt();
				switch(act){
				case 0:{
					int pos = this.maxIndex(0, N-1, x);
					out.println(pos);
					break;
				}
				case 1:{
					int pos = this.maxIndexMin(0, N-1, x);
					out.println(pos);
					break;
				}
				case 2:{
					int pos = this.minIndex(0, N-1, x);
					out.println(pos);
					break;
				}
				}
			}
			
			in.close();
			out.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Main test = new Main();
		test.read2();
	//	test.print();
	}
}