今天在做猿辅导的笔试题,分组对话时,调试了很久都找不出问题。
开始我是一行读一个测试用例然后进行处理,但一直AC不了
链接:https://www.nowcoder.com/questionTerminal/c52ad4ee63784ad8b26fa94e4f9cc26e 来源:牛客网 import heapq def solve(nums): heap = [] for num in nums: if num > 0: heapq.heappush(heap, -num) ans = 0 while len(heap) >= 3: a = -heapq.heappop(heap) b = -heapq.heappop(heap) c = -heapq.heappop(heap) ans += 1 if a - 1 > 0: heapq.heappush(heap, -(a - 1)) if b - 1 > 0: heapq.heappush(heap, -(b - 1)) if c - 1 > 0: heapq.heappush(heap, -(c - 1)) print(ans) n = int(input()) for i in range(n): line = list(map(int, input().split())) solve(line[1:])
后面把所有行串到一起之后分处理就能AC了,有人知道是什么原因吗?
n = int(input()) nums = ' '.join([line for line in sys.stdin]).split() i = 0 for _ in range(n): length = int(nums[i]) solve(nums[i+1:i+length+1]) i = i + length + 1
全部评论
(0) 回帖