2021 年 08 月 22 日晚 19:00 至 20:30,总共 3 道题,时间不够,只看了第 2、3 题(多多的心情值、第一个幸运儿),并且只有第 3 题通过了 10% 的测试用例。但是非常奇怪的是,我本地的测试用例都通过了,所以想恳求各位大佬帮忙看看代码有什么问题,或者如果有做了相同的题目的同学欢迎一起交流,非常感谢!
多多的秒杀活动
截图没有保留下来。
多多的心情值
def solve(P, Q): if Q < P: return int((P - Q + 1) / 2) elif Q == P: return 0 else: day = 0 while True: if (P * 2) > Q: break P *= 2 day += 1 return day + (Q - P) def test(): test_cases = [ # P, Q Expected ((3, 4), 1), ((10, 1), 5), ((2, 8), 2), ((2, 15), 9), ((2, 16), 3), ] for test_case in test_cases: args = test_case[0] expected = test_case[1] result = solve(*args) print(expected, result) assert expected == result def main(): import sys n = int(sys.stdin.readline().strip()) for i in range(n): P, Q = [int(item)for item in sys.stdin.readline().strip().split(" ")] print(solve(P, Q)) test()
第一个幸运儿
import random def solve(N, K, D, Q, L, P): seats = [0 for i in range(N)] for day in range(K): seats[P[day]] = L[day] # Check if the big brother value exceed for i in range(len(seats)): l = sum(seats[max(i - D, 0): min(i + D + 1, len(seats))]) if l >= Q: return day return -1 def random_test(): test_cases = [] for _ in range(10): N = random.randint(5, 10) K = random.randint(1, N) D = random.randint(1, 10) Q = random.randint(10, 100) P = list(range(K)) random.shuffle(P) L = [random.randint(1, 100) for _ in range(K)] test_cases.append(((N, K, D, Q, L, P), 0)) test(test_cases) def test_manual(): test_cases = [ # N, K, D, Q, L, P, Expected ((5, 5, 1, 7, [5, 2, 3, 2, 3], [4, 0, 1, 2, 3]), 3), ((5, 4, 1, 19, [9, 1, 8, 10], [3, 1, 2, 0]), 3), ((4, 3, 0, 245, [196, 12, 6], [2, 1, 0]), -1), ((4, 3, 0, 245, [256, 12, 6], [2, 1, 0]), 0), ((4, 3, 1, 245, [240, 12, 6], [2, 1, 0]), 1), ((4, 3, 1, 245, [230, 12, 6], [2, 1, 0]), 2), ] test(test_cases) def test(test_cases): for test_case in test_cases: args = test_case[0] expected = test_case[1] result = solve(*args) assert expected == result def main(): import sys n = int(sys.stdin.readline().strip()) for i in range(n): N, K, D, Q = [int(item)for item in sys.stdin.readline().strip().split(" ")] L = [0 for i in range(K)] P = [0 for i in range(K)] for k in range(K): L[k], P[k] = [int(item)for item in sys.stdin.readline().strip().split(" ")] print(solve(N, K, D, Q, L, P)) test_manual()
全部评论
(1) 回帖