[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 ...
随机推荐
- python积累
python积累 一.逐渐积累 python逐渐积累 http://www.cnblogs.com/lx63blog/articles/6051526.html python积累_2 http://w ...
- maven project 更新总是jre-1.5
解决如下: <build> <plugins> <plugin> <groupId>org.apa ...
- NPOI操作excel
1.基本导出方法 private void ExportToExcel() { SaveFileDialog sdfExport = new SaveFileDialog(); sdfExport.F ...
- http之100-continue(转)
1.http 100-continue用于客户端在发送POST数据给服务器前,征询服务器情况,看服务器是否处理POST的数据,如果不处理,客户端则不上传POST数据,如果处理,则POST上传数据.在现 ...
- DBlink与同义词
DBLink就是数据库链接,而同义词就已经具体到某个用户下的表了 原文链接:http://www.linuxidc.com/Linux/2013-01/77579.htm 这里所需要的信息: 从MM库 ...
- 关于meta标签
一.Meta标签中的format-detection属性及含义 意为:格式检测 或许你会有这样的经历:当你在制作手机端的页面中,点击了没有加任何链接的格式的数字时,这时手机会进行自动拔号提示操作! 禁 ...
- mysql 简单优化方法
优化步骤:1.查看SQL是否可以优化.2.查看索引是否可以优化.3.查看表结构是否可以优化. show table status from databases like 'tablename%'; / ...
- $anchorScroll和$cache
1.$achorScroll服务---用于描点跳转 $scope.change = function(id){ $location.hash(id); $anchorScroll(); }; 只要给l ...
- Codeforces 740A. Alyona and copybooks 模拟
A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...
- POJ 3469 Dual Core CPU 最大流
划分成两个集合使费用最小,可以转成最小割,既最大流. //#pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...