|
|
back to boardtime limit exceed on test 5?but i used binary search!Can everybody explain me! Posted by Aydar 4 Apr 2011 22:07 import java.io.*; import java.util.Scanner; public class problem1196 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int sum=0; int value=0; int[] A=new int [n]; boolean[] map =new boolean[n]; for(int i=0;i<n;i++){ A[i]=sc.nextInt(); } int m=sc.nextInt(); int[] B=new int[m]; for (int i=0;i<m;i++){ B[i]=sc.nextInt(); } for (int i=0;i<m;i++){ value=B[i]; if (BinarySearch(A,0,n-1,value)!=0) sum++; } System.out.println(sum); } public static long BinarySearch(int[] Mas,int left,int right,int value){ int half; while(left<=right){ half=(left+right)/2; if (value==Mas[half]) return Mas[half]; else if (value<Mas[half]) right=half-1; else left=half+1; } return 0; }} |
|
|