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();
        }
    }
}
kgduu
+关注
目录
打赏
0
0
0
0
0
分享
相关文章
Java Exception异常信息怎么打印、记录,几种方式自己选
Java Exception异常信息怎么打印、记录,几种方式自己选
828 0
Java Exception异常信息怎么打印、记录,几种方式自己选
火狐浏览器怎么禁用javascript
我们经常会在上网的时候遇到很多禁止了鼠标右键的网页,而那些内容却是我们非常喜欢的,不管是文字或插图都想保存到本地以便以后查看,那我们应该怎样来破解这样的限制呢?通过火狐浏览器禁用javascript就可以做到哦!
1597 0
大语言模型对时间序列预测真的有用吗?
我们已经看到了语言模型的巨大进步,但时间序列任务,如预测呢?今天我们推荐一篇论文,对现有的语言模型和时间序列做了深入的研究。将探讨了是否可以从大型语言模型(LLMs)中获益于时间序列(TS)预测。
165 11
SpringBoot核心特性——异步任务和定时任务那些事
前言 通常情况下,SpringMVC接收到请求后会将请求具体分发给单个线程进行处理。如果请求处理中涉及到比较耗时的操作,为了能更快地将响应返回给用户,那么就需要将耗时的业务操作交由别的线程进行异步处理,而SpringBoot已经为我们提供了这样的实现。
676 2
SpringBoot核心特性——异步任务和定时任务那些事
一文读懂MySQL查询语句的执行过程
一文读懂MySQL查询语句的执行过程
398 0
Android C++系列:JNI中的线程操作
第四个参数为线程启动程序的参数,也就是函数的参数,如果不需要传递参数,它可以为 NULL 。 pthread_create 函数如果执行成功了则返回 0 ,如果返回其他错误代码。
370 0
postgre分页查询报错:ERROR: LIMIT #,# syntax is not supported 建议:Use separate LIMIT and OFFSET clauses
postgre分页查询报错:ERROR: LIMIT #,# syntax is not supported 建议:Use separate LIMIT and OFFSET clauses
525 0
postgre分页查询报错:ERROR: LIMIT #,# syntax is not supported 建议:Use separate LIMIT and OFFSET clauses
Windows 也可以像 macOS 一样美观,收好这 16 个美化技巧
Windows 也可以像 macOS 一样美观,收好这 16 个美化技巧
1065 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等