Grapevine



//By kyoo [mailto:kyoo_16@hotmail.com kyoo_16@hotmail.com] import java.io.*; import java.util.*; public class Grapevine { public static int[][] getIndices(int[][] m,int L,int M){ int[][] indices= new int[m.length][2]; for (int i = 0; i < m.length; i++){ int pos=Arrays.binarySearch(m[i], L); if(pos>=0){ for (int j = pos; j >=0 && m[i][j]==L; j--)pos=j; indices[i][0]=pos; } else indices[i][0]=-pos-1; pos=Arrays.binarySearch(m[i], M); if(pos>=0){ for (int j = pos; j < m[i].length && m[i][j]==M; j++)pos=j; indices[i][1]=pos+1; } else indices[i][1]=-pos-1; } return indices; } public static int maximos(int[][] m,int L,int U){ int max=0; int[][] pos= getIndices(m,L,U); //System.out.println(L+" - "+U+" "+pos[0][0]+" "+pos[0][1]); if(pos[0][0]< pos[0][1]) max=1; for (int i = 1; i < m.length|| max+i=contador+1) contador++; else break; } max=Math.max(max,contador); } return max; } public static void main(String[] args) throws IOException { //BufferedReader leer = new BufferedReader(new FileReader("grape.in")); BufferedReader leer = new BufferedReader(new InputStreamReader(System.in)); //PrintWriter esc= new PrintWriter("grape2.out"); String[] d=leer.readLine.split(" "); int f=Integer.parseInt(d[0]); int c=Integer.parseInt(d[1]); while(f!=0 && c!=0){ int m[][]= new int[f][c]; for (int i = 0; i < f; i++){ d=leer.readLine.split(" "); for (int j = 0; j < c; j++) m[i][j]=Integer.parseInt(d[j]); } int q=Integer.parseInt(leer.readLine); for (int i = 0; i < q; i++){ d=leer.readLine.split(" "); //esc.println(maximos(m,Integer.parseInt(d[0]),Integer.parseInt(d[1]))); System.out.println(maximos(m,Integer.parseInt(d[0]),Integer.parseInt(d[1]))); } //esc.println("-"); System.out.println("-"); d=leer.readLine.split(" "); f=Integer.parseInt(d[0]); c=Integer.parseInt(d[1]); } //esc.close; } }