题解 | 选药

选药

https://www.nowcoder.com/practice/f55f93b3560248f4b4532490206df398

考核知识点:排序、库函数二分

核心在于处理查询。由于查询次数较多且查询范围较大,直接遍历所有药品会超时。因此,需要对药品数据进行预处理,容易想到前缀最大值预处理+二分查找。

n = int(input())
drugs = []
for i in range(n):
  a, b = map(int, input().split(" "))
  drugs.append((a, b))
drugs.sort(key=lambda x: (-x[1], x[0]))
q = int(input())
for i in range(q):
  x = int(input())
  result = -1
  for j in drugs:
    if j[0] <= x:
      result = j[1]
      break
  print(result)

全部评论
点赞 回复 分享
发布于 11-26 13:30 河北

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务