Package COM. wangzhu. njupt; import Java. io. bufferedinputstream; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. streamtokenizer;/*** find the number of occurrences in the sorted array ** @ classname: main1349 * @ description: todo * @ author Wang Zhu * @ date 5:02:27 **/public class main1349 {Private Static final int Len = 1000001; Private Static int [] arr = new int [Len]; /*** @ Param ARGs * @ throws ioex Ception */public static void main (string [] ARGs) throws ioexception {// system. setin (New fileinputstream ("data. in "); streamtokenizer in = new streamtokenizer (New bufferedinputstream (system. in); While (in. nexttoken ()! = Streamtokenizer. tt_eof) {int n = (INT) in. nval; For (INT I = 0; I <n; I ++) {In. nexttoken (); arr [I] = (INT) in. nval;} In. nexttoken (); int t = (INT) in. nval; For (INT I = 0; I <t; I ++) {In. nexttoken (); int M = (INT) in. nval; int Index = binarysearch (n, m); int COUNT = 0; If (index! =-1) {count ++; Int J = index-1; while (j> = 0 & arr [j --] = m) {count ++ ;} j = index + 1; while (j <n & arr [J ++] = m) {count ++ ;}} system. out. println (count) ;}}/ *** Binary Search ** @ Param Len * @ Param key * @ return */Private Static int binarysearch (INT Len, int key) {int left = 0, Right = len-1; while (left <= right) {int mid = (left + right)> 1; if (ARR [Mid]> key) {right = mid-1;} else if (ARR [Mid] <key) {left = Mid + 1 ;} else {return mid ;}} return-1 ;}}
The number of times a number appears in the sorting array at 9 degrees 1349