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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.io.IOException;

class Main
{
	public StreamTokenizer tokenizer;
	public PrintWriter cout;
	public int n, l;
	
	public void init()
	{
		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
		tokenizer = new StreamTokenizer(cin);
		
		cout = new PrintWriter(new OutputStreamWriter(System.out));
	}
	
	public boolean input() throws IOException
	{
		
		tokenizer.nextToken();
		if (tokenizer.ttype == StreamTokenizer.TT_EOF) return false;
		
		n = (int)tokenizer.nval;
		
		tokenizer.nextToken();
		l = (int)tokenizer.nval;
		
		return true;
	}
	
	public boolean check(int x)
	{
		int p = 0, q = x - 1, m;
		int cnt = 0;
		
		while (p <= q) {
			m = (p + q) >> 1;
			cnt++;
			
			if (cnt > l) return false;
			
			if (m == n) return cnt == l;
			
			if (m < n) p = m + 1;
			else q = m - 1;
		}
		
		return false;
	}
	
	public void solve()
	{
		int[] left = new int[10001], right = new int[10001];
		int cnt = 0;
		
		boolean flag = false;
		
		for (int i = 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 (int i = 0; i < cnt; i++) {
			cout.println(left[i] + " " + right[i]);
		}
		cout.flush();
	}
	
	public static void main(String[] args) throws IOException
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
		
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值