[UCSD白板题] Binary Search
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的更多相关文章
- LeetCode算法题-Binary Search(Java实现)
这是悦乐书的第297次更新,第316篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第165题(顺位题号是704).给定n个元素的排序(按升序)整数数组nums和目标值,编 ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [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 ...
- [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 ...
- [UCSD白板题] Sorting: 3-Way Partition
Problem Introduction The goal in this problem is to redesign a given implementation of the randomize ...
随机推荐
- NodeJS搭建HTTPS服务器
[NodeJS搭建HTTPS服务器] http://cnodejs.org/topic/54745ac22804a0997d38b32d
- google map 计算地图面积方法
花了几个小时把js的google计算地图面积的算法改成了c# 的. class Program { static void Main(string[] args) { // a = new qq.ma ...
- Tcc学习笔记(二) 安装和配置
1.下载和编译 去repo.or.cz/tinycc.git下载最新的snapshot压缩包, 或者用git命令下载: git clone git://repo.or.cz/tinycc.git . ...
- MD5实现32位加密
好记性不如烂笔头,随手记记 附代码 public static void Main(string[] args) { Console.WriteLine("长度为" + UseMd ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->使用Spring Framework开发自己的应用程序
1.直接基于spring framework开发自己的应用程序: 1.1参考资料: Spring官网spring-framework.4.3.5.RELAESE的Reference Documenta ...
- database link远程链接数据库
--授权创建.删除dblink GRANT CREATE [PUBLIC] DATABASE LINK,DROP [PUBLIC] DATABASE LINK TO canco; --查看数据库GLO ...
- C#多线程实现方法
C#中实现线程应用开发的类都包含在了System.Threading命名空间中,比较常用的是Thread,ThreadPool类 Thread类构造函数作用是创建线程,有两个重载版本,一个带参数,一个 ...
- cefsharp在xp上运行
今天遇到一个坑.也是自己英语不足的体现.在xp上运行cefsharp.wpf. 查询了各种资料.按照说明一步一步的操作,都没有解决xp上运行cefsharp.wpf. 而且在xp上调试都不知道错误在哪 ...
- 2016年&2017年
2016年在IBM已经工作4年了,从门户项目到今年的保险行业灾备项目,从之前的技术到现在的项目推进,由面对机器工作到,跟更多的人打交道,工作继续进行着,希望今天的项目尽早结束. 由于工作的原因,今年回 ...
- jQuery Mobile 可折叠
可折叠的内容块 可折叠(Collapsibles)允许您隐藏或显示内容 - 对于存储部分信息很有用. 如需创建可折叠的内容块,请向某个容器分配 data-role="collapsible& ...