Sdut 3376 Data structure experiment find 4:2 points find

Source: Internet
Author: User

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

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.