Problem Introduction

In this problem, you will implemented the binary search algorithm that allows searching very efficiently(even huge) lists provided that the list is sorted.

Problem Description

Task.The goal in this code problem is to implement the binary search algorithm.

Input Format.The first line of the input contains an integer \(n\) and a sequence \(a_0<a_1<\cdots<a_{n-1}\) of \(n\) pairwise distinct positive integers in increasing order. The next line contains an integer \(k\) and \(k\) positive integers \(b_0,b_1,\cdots,b_{k-1}\).

Constraints.\(1 \leq n,k \leq 10^5; 1 \leq a_i \leq 10^9\) for all \(0 \leq i < n; 1 \leq b_j \leq 10^9\) for all \(0 \leq j \leq k\).

Output Format.For all \(i\) from \(0\) to \(k-1\),output an index \(0 \leq j \leq n-1\) such that \(a_j=b_i\) or \(-1\) if there is no such index.

Sample 1.
Input:

5 1 5 8 12 13
5 8 1 23 1 11

Output:

2 0 -1 0 -1

Solution

# Uses python3
import sys

def binary_search(a, x):
    left, right = 0, len(a)
    while left < right:
        mid = left + (right - left) // 2
        if x == a[mid]:
            return mid
        elif x < a[mid]:
            right = mid
        else:
            left = mid + 1
    return -1

def linear_search(a, x):
    for i in range(len(a)):
        if a[i] == x:
            return i
    return -1

if __name__ == '__main__':
    input = sys.stdin.read()
    data = list(map(int, input.split()))
    n = data[0]
    m = data[n + 1]
    a = data[1 : n + 1]
    for x in data[n + 2:]:
        print(binary_search(a, x), end = ' ')

[UCSD白板题] Binary Search的更多相关文章

  1. LeetCode算法题-Binary Search(Java实现)

    这是悦乐书的第297次更新,第316篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第165题(顺位题号是704).给定n个元素的排序(按升序)整数数组nums和目标值,编 ...

  2. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  3. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  4. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  5. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  6. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  7. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  8. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  9. [UCSD白板题] Sorting: 3-Way Partition

    Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...

随机推荐

  1. SQL语句中=null和is null

    平时经常会遇到这两种写法:IS NOT NULL与!=NULL.也经常会遇到数据库有符合条件!=NULL的数据,但是返回为空集合.实际上,是由于对二者使用区别理解不透彻. 默认情况下,推荐使用 IS ...

  2. php js => splice 数组 插入 功能

    php    array_splice 手册详解 array_splice   - 把数组中的一部分去掉并用其它值取代 参数 input 输入的数组. offset 如果 offset 为正,则从 i ...

  3. shell编程之运算符

    declare声明变量类型 declare    [+ / -] [选项]  变量名 - :给变量设定类型属性 + :取消变量的类型属性 -a :将变量声明为数组型 -i :将变量声明为整数型 -x ...

  4. 一个的unity学习系列的博客

    1.http://my.csdn.net/caoboya 2.http://my.csdn.net/OnafioO

  5. Python全栈之路目录结构

    基础 1.Python全栈之路-----基础篇 2.Python全栈之路---运算符与基本的数据结构 3.Python全栈之路3--set集合--三元运算--深浅拷贝--初识函数 4.Python全栈 ...

  6. wcf 由 http 更改为 https 返回404,没有终结点在侦听可以接受消息的

    首先wcf项目在使用http时是没问题的. WCF有http更改为https之后,返回 没有终结点在侦听可以接受消息 需要修改wcf服务端及客户端 服务端更改代码 <binding maxRec ...

  7. js 事件绑定

    事件绑定的方式 1.  DOM  元素行内绑定 <div onclick="alert(1)"></div>   2. js on+eventType do ...

  8. php中ajax调用出错的问题

    最近用一些php,客户的使用jquery ajax的时候经常报错 ajax报错内容:readyState:4 status:"OK",statusText:"OK&quo ...

  9. DailyRollingFileAppender的使用

    DailyRollingFileAppender是日志记录软件包Log4J中的一个Appender,它能够按一定的频度滚动日志记录文件. 如果您不熟悉Log4J,建议阅读一下 使用Log4j进行日志记 ...

  10. 发布以NLog作为日记工具的ASP.NET站点到IIS注意事项

    一.可以通过在Web.Config文件中添加节点来配置,或是直接将NLog.config放在Web.config所在目录 二.通过节点的fileName属性指定日志文件规则时,可以使用${basedi ...