The number of times a number appears in the sorting array at 9 degrees 1349

Source: Internet
Author: User

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.