永发信息网

sicilly二分查找

答案:2  悬赏:0  手机版
解决时间 2021-02-16 18:19
Description
Binary search is a search algorithm usually used on a sorted sequence to quickly find an element with a given value. The algorithm works by repeatedly comparing the sought value with some element from the sequence (the pivot), then discarding as much of the sequence as possible based on the comparison and the assumption that the sequence is sorted. Note that in the standard algorithm, the pivot element is as close as possible to the middle of the sequence, but we'll remove that restriction.

The pseudocode for performing binary search on a sequence S, looking for value X is as follows:

1 binary_search(S, X) 2
while S is not empty 3
choose an element from S as the pivot 4
if pivot = X, return true (X was found) 5
else if pivot < X, remove pivot and all elements before it from S 6
else remove pivot and all elements after it from S 7
end while 8
return false (X was not found)In this problem we will evaluate how binary search performs on data that isn't necessarily sorted. An element of S is said to be binary searchable if, regardless of how the pivot is chosen on line 3 whenever the line is executed, the above algorithm returns true.

Given a sequence containing distinct integers, output the number of elements from the sequence that are binary searchable.

Input
Input may contain many cases.

The first line of each case is the length(between 0 and 100, inclusive) of the sequence. The second line contains the elements in the sequence, seperated by one space.

Output
For each case, output the number of elements from the sequence that are binary searchable, in one line.

Sample Input
Copy sample input to clipboard
3
1 3 2
7
3 2 1 10 23 22 21
6
1 5 7 11 12 18
6
5 4 3 2 1 0Sample Output
1
1
6
0

不用把代码写出来,说说算法吧,怎样才能做到说取的pivot是随机的?
最佳答案
可以用三点中值法模拟。
全部回答
没看懂什么意思?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
应用程序发生异常 unknown software exceptio
那个在淘宝买番薯是真的还是假的
魏都区许昌蓝天造型(三八路店)地址是什么,有
南海区爱意家居用品公司在什么地方啊,我要过
1929-1933年,资本主义世界爆发了一场空前规
鑫源x1升级版坐高多少
本命年办公室位置似乎风水不好,求高手解答
看看这几个国家属于哪个州?
依安县齐齐哈尔王氏熟食店我想知道这个在什么
丽景商务酒店怎么去啊,有知道地址的么
2017年上半年好看的gl小说合集?
23岁初产妇,孕9周,外周血全血象均降低,骨
道里区哈尔滨艾皙黛尔(紫金城店)这个地址怎么
小童学童装天宝店地址有知道的么?有点事想过
加一笔,变成另一个字:火,去。 改一笔,变
推荐资讯
阎家岭村在什么地方啊,我要过去处理事情
郭包肉怎么做?
用简便方法计算:48x5x125
今天跟我姐去玩,朋友都夸我姐身材不错。看不
一到十二月分别是什么花
金威设计装修工程有限公司地址有知道的么?有
几年一个闰年
如何评价2016nba扣篮大赛扎克拉文的表现
SJ与sm公司的合约什么时候到期?
乐府最初是主管什么的官府
农村盖房子建一层,求设计图纸。坐南朝北的宅
暑假写事作文500字
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?