Data structure experiment find 4:2 points findTime limit:20ms Memory limit:65536kb Submit Statisticproblem Description
In the increment sequence of a given non-repeating element, find the same element as the given keyword, and, if present, the output is found, there is no output-1.
Input
A set of input data, the first line of input data entered two positive integers n (n < = 10^6) and m (M < = 10^4), n is the number of data elements in the array, followed by successive input n positive integers, the input data to ensure that the sequence is incremented.
The M-line then enters m to find the keyword key
Output
If an element equal to the keyword key can be found in a given sequence, the output bit sequence (ordinal starts at 0), otherwise output-1.
Example Input
8 34 6 8 9 13 20 21 226817
Example Output
12-1
DQE:
Using while loop to achieve binary search, the size of the subject data is slightly larger, the algorithm time critical, the server pressure may appear tle, not optimized algorithm.
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 intMain ()7 {8 intf[1000010];9 intn,m,k,i;Ten while(SCANF ("%d%d", &n,&m)! =EOF) One { A for(i=0; i<n;i++) -scanf"%d", f+i); - while(m--) the { - BOOLflag=true; -scanf"%d",&k); - intL=0, r=n-1; + while(l<=R) - { + intLl= (L+R)/2; A if(f[ll]==k) at { -printf"%d\n", ll); -flag=false; - Break; - } - Else if(f[ll]>k) in { -r=ll-1; to } + Else -l=ll+1; the } * if(flag) $printf"-1\n");Panax Notoginseng } - } the return 0; + } A the /*************************************************** + User Name: * * * - result:accepted $ Take time:28ms $ Take memory:544kb - Submit time:2016-11-10 22:55:11 - ****************************************************/
Sdut 3376 Data structure experiment find 4:2 points find