22650_BinarySearc

2022-5-16 18:22| 发布者: Hocassian| 查看: 21| 评论: 0|原作者: 肇庆学院ACM合集

摘要:
C:\Users\Administrator\Downloads\2019-10-12-10-14-5-89506821103000-Problem List-采集的数据-后羿采集器.html

Pro.ID

22650

Title

Binary Search

Title链接

http://10.20.2.8/oj/exercise/problem?problem_id=22650

AC

0

Submit

0

Ratio

-

时间&空间限制

  • Time Limit: 1500/500 MS (Java/Others)     Memory Limit: 20000/10000 K (Java/Others)
  • 描述

    The program fragment below performs binary search of an integer number in an array that is sorted in a nondescending order: 

    Pascal (file "sproc.pas") C (file "sproc.c")
     const
      MAXN = 10000;
    var
      A: array[0..MAXN-1] of integer;
      N: integer;
    
    procedure BinarySearch(x: integer);
    var
      p, q, i, L: integer;
    begin
      p := 0;   { Left border of the search  }
      q := N-1; { Right border of the search }
      L := 0;   { Comparison counter         }
      while p <= q do begin
        i := (p + q) div 2;
        inc(L);
        if A[i] = x then begin
          writeln('Found item i = ', i,
            ' in L = ', L, ' comparisons');
          exit
        end;
        if x < A[i] then
          q := i - 1
        else
          p := i + 1
      end
    end;
     #define MAXN 10000
    
    int A[MAXN];
    int N;
    
    void BinarySearch(int x)
    {
      int p, q, i, L;
    
      p = 0;   /* Left border of the search  */
      q = N-1; /* Right border of the search */
      L = 0;   /* Comparison counter         */
      while (p <= q) {
        i = (p + q) / 2;
        ++L;
        if (A[i] == x) {
          printf("Found item i = %d"
            " in L = %d comparisons\n", i, L);
          return;
        }
        if (x < A[i])
          q = i - 1;
        else
          p = i + 1;
      }
    }


    Before BinarySearch was called, N was set to some integer number from 1 to 10000 inclusive and array A was filled with a nondescending integer sequence. 

    It is known that the procedure has terminated with the message "Found item i = XXX in L = XXX comparisons" with some known values of i and L. 

    Your task is to write a program that finds all possible values of N that could lead to such message. However, the number of possible values of N can be quite big. Thus, you are asked to group all consecutive Ns into intervals and write down only first and last value in each interval.

    输入

    The input file consists of a single line with two integers i and L ( 0 <= i < 10000 and 1 <= L <= 14 ), separated by a space.

    输出

    Description
    The program fragment below performs binary search of an integer number in an array that is sorted in a nondescending order: 

    Pascal (file "sproc.pas") C (file "sproc.c")
     const
      MAXN = 10000;
    var
      A: array[0..MAXN-1] of integer;
      N: integer;
    
    procedure BinarySearch(x: integer);
    var
      p, q, i, L: integer;
    begin
      p := 0;   { Left border of the search  }
      q := N-1; { Right border of the search }
      L := 0;   { Comparison counter         }
      while p <= q do begin
        i := (p + q) div 2;
        inc(L);
        if A[i] = x then begin
          writeln('Found item i = ', i,
            ' in L = ', L, ' comparisons');
          exit
        end;
        if x < A[i] then
          q := i - 1
        else
          p := i + 1
      end
    end;
     #define MAXN 10000
    
    int A[MAXN];
    int N;
    
    void BinarySearch(int x)
    {
      int p, q, i, L;
    
      p = 0;   /* Left border of the search  */
      q = N-1; /* Right border of the search */
      L = 0;   /* Comparison counter         */
      while (p <= q) {
        i = (p + q) / 2;
        ++L;
        if (A[i] == x) {
          printf("Found item i = %d"
            " in L = %d comparisons\n", i, L);
          return;
        }
        if (x < A[i])
          q = i - 1;
        else
          p = i + 1;
      }
    }


    Before BinarySearch was called, N was set to some integer number from 1 to 10000 inclusive and array A was filled with a nondescending integer sequence. 

    It is known that the procedure has terminated with the message "Found item i = XXX in L = XXX comparisons" with some known values of i and L. 

    Your task is to write a program that finds all possible values of N that could lead to such message. However, the number of possible values of N can be quite big. Thus, you are asked to group all consecutive Ns into intervals and write down only first and last value in each interval.
    Input
    The input file consists of a single line with two integers i and L ( 0 <= i < 10000 and 1 <= L <= 14 ), separated by a space.
    Output
    On the first line of the output file write the single integer number K representing the total number of intervals for possible values of N. Then K lines shall follow listing those intervals in an ascending order. Each line shall contain two integers Ai and Bi ( Ai <= B) separated by a space, representing first and last value of the interval. 

    If there are no possible values of N exist, then the output file shall contain the single 0.
    Sample Input
    10 3
    Sample Output
    4
    12 12
    17 18
    29 30
    87 94
    Source

    样例输入

    10 3

    样例输出

    4
    12 12
    17 18
    29 30
    87 94

    作者


    路过

    雷人

    握手

    鲜花

    鸡蛋

    最新评论

    返回顶部