UVa1554 - Binary Search

简介: UVa1554 - Binary Search

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 several datasets. Each datasets 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 each dataset 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 ≤ Bi) 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

importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
importjava.io.OutputStreamWriter;
importjava.io.StreamTokenizer;
importjava.io.IOException;
classMain{
publicStreamTokenizertokenizer;
publicPrintWritercout;
publicintn, l;
publicvoidinit()
    {
BufferedReadercin=newBufferedReader(newInputStreamReader(System.in));
tokenizer=newStreamTokenizer(cin);
cout=newPrintWriter(newOutputStreamWriter(System.out));
    }
publicbooleaninput() throwsIOException    {
tokenizer.nextToken();
if (tokenizer.ttype==StreamTokenizer.TT_EOF) returnfalse;
n= (int)tokenizer.nval;
tokenizer.nextToken();
l= (int)tokenizer.nval;
returntrue;
    }
publicbooleancheck(intx)
    {
intp=0, q=x-1, m;
intcnt=0;
while (p<=q) {
m= (p+q) >>1;
cnt++;
if (cnt>l) returnfalse;
if (m==n) returncnt==l;
if (m<n) p=m+1;
elseq=m-1;
        }
returnfalse;
    }
publicvoidsolve()
    {
int[] left=newint[10001], right=newint[10001];
intcnt=0;
booleanflag=false;
for (inti=n+1; i<10001; i++) {
if (check(i)) {
if (!flag) {
left[cnt] =i;
flag=true;
                }
            } else {
if (flag) {
right[cnt++] =i-1;
flag=false;
                }
            }
        }
if (flag) right[cnt++] =10000;
cout.println(cnt);
for (inti=0; i<cnt; i++) {
cout.println(left[i] +" "+right[i]);
        }
cout.flush();
    }
publicstaticvoidmain(String[] args) throwsIOException    {
Mainsolver=newMain();
solver.init();
while (solver.input()) {
solver.solve();
        }
    }
}
目录
相关文章
|
5月前
|
算法 索引
Binary Search
Binary Search “【5月更文挑战第21天】”
41 5
|
5月前
C. Binary Search
C. Binary Search
|
机器学习/深度学习 存储 C++
【PAT甲级 - C++题解】1064 Complete Binary Search Tree
【PAT甲级 - C++题解】1064 Complete Binary Search Tree
86 0
LeetCode 67. Add Binary
给定两个二进制字符串,返回它们的总和(也是二进制字符串)。 输入字符串都是非空的,只包含字符1或0。
71 0
LeetCode 67. Add Binary
Leetcode-Easy 543. Diameter of Binary Tree
Leetcode-Easy 543. Diameter of Binary Tree
85 0
Leetcode-Easy 543. Diameter of Binary Tree
|
算法 索引 C++
[LeetCode]--257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: [“1-&gt;2-&gt;5”, “1-&gt;3”] C
1252 0
[LeetCode]--226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original twe
1118 0
|
索引
leetcode Binary Search
leetcode 35 Search Insert Position Question Given a sorted array and a target value, return the index if the target is found.
1068 0