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. wsimport命令讲解

    wsimport是JDK自带的工具,主要功能是根据服务端生成的WSDL文件创建客户端支持代码.生成java客户端代码常使用的命令参数说明: 参数 说明 -p 定义客户端生成类的包名称 -s 指定客户端 ...

  2. Win7如何显示/隐藏Administrator账号

    为了保证安全,windows7的administrator账户默认在登录时是不显示的. 在登录页面显示administrator账户的方法: 1. 选择"开始"菜单->&qu ...

  3. java写入和写出EXCEL(含源代码)

    这两天帮老师做一个数据库,将所有实验交易的数据导入到数据库中,但是不想天天在实验室里面待着,气氛太压抑,就想着先把数据读进EXCEL中,哪天带到实验室导进去 数据原来是这样的,不同的实验有一个专门的文 ...

  4. 一份不错的vue.js基础笔记!!!!

    第一章 Vue.js是什么? Vue(法语)同view(英语) Vue.js是一套构建用户界面(view)的MVVM框架.Vue.js的核心库只关注视图层,并且非常容易学习,非常容易与其他库或已有的项 ...

  5. 寒冰王座 hdu 1248(背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1248 #include <stdio.h> #include <stdlib.h> #i ...

  6. [刘阳Java]_MyBatis_动态SQL标签用法_第7讲

    1.MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. 2.MyBatis中用于实现动态SQL的元素主要有 if choose(when,otherwi ...

  7. Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    背景 昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下. 主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用. ...

  8. notepad++ 配置Python 调试环境 实用版

    一. 安装python 1. 下载python 2.7版本并安装: 2. 在安装到自定义python的时候选择 add python to ptah项:

  9. 【python】GTK 例子

    # encoding:utf-8 # box_multi_button_data.py import pygtk, gtk def destroy(widget, data=None): gtk.ma ...

  10. Matlab(1) -- Matlab清屏命令

    这里介绍 matlab 中三种不同的清理变量.显示窗口或图形的命令. 一.比较重要的清理:清除变量命令:clear 说明:运行m文件之前一般都需要该命令,否则可能出错. 二.比较常用的清理:清理当前命 ...