我试图用Python解决这个cp问题,但由于某些原因,对于非常大的值,答案是错误的,例如这个输入:
5
3949 45969 294854 9848573 2147483647
5
10000 6 2147483647 4959 5949583
可接受输出:
3949 45969
X 3949
9848573 X
3949 45969
294854 9848573
我的输出:
X 294854
X 294854
9848573 X
X 294854
45969 9848573
我的代码:
def bs(target, search_space):
l, r = 0, len(search_space) - 1
while l <= r:
m = (l + r) >> 1
if target == search_space[m]:
return m - 1, m + 1
elif target > search_space[m]:
l = m + 1
else:
r = m - 1
return r, l
n = int(input())
f_heights = list(set([int(a) for a in input().split()]))
q = int(input())
heights = [int(b) for b in input().split()]
for h in heights:
a, b = bs(h, f_heights)
print(f_heights[a] if a >= 0 else 'X', f_heights[b] if b < len(f_heights) else 'X')
任何帮助都将不胜感激!