Why second approach is wrong: from bisect import bisectright from collections import…
- User
Why second approach is wrong: from bisect import bisect_right from collections import defaultdict
t = int(input()) for _ in range(t): n, d, k = map(int, input().split()) jobs = [tuple(map(int, input().split())) for _ in range(k)]
diff = defaultdict(int)
for start, end in jobs: diff[start - d + 1] += 1 diff[end + 1] -= 1
sorted_diff = sorted(diff.items()) diff = {start: count for start, count in sorted_diff}
pos, prefix = [], [] curr = 0 for start, count in diff.items(): curr += count pos.append(start) prefix.append(curr)
max_overlaps, min_overlaps = float('-inf'), float('inf') brother, mother = 0, 0 for i in range(1, n - d + 2): idx = bisect_right(pos, i) count = prefix[idx - 1] if count > max_overlaps: max_overlaps = count brother = i if count < min_overlaps: min_overlaps = count mother = i
print(brother, mother) Approach 2: from bisect import bisect_right
t = int(input()) for _ in range(t): n, d, k = map(int, input().split()) jobs = [tuple(map(int, input().split())) for _ in range(k)]
starts, ends = [], []
for start, end in jobs: starts.append(max(1, start - d + 1)) ends.append(min(n, end + 1))
starts.sort() ends.sort()
min_overlaps, max_overlaps = float('inf'), -float('inf') brother, mother = -1, -1 for i in range(1, n - d + 1): curr = bisect_right(starts, i) - bisect_right(ends, i) if max_overlaps < curr:
codebrother, max_overlaps = i, curr if min_overlaps > curr: mother, min_overlaps = i, currprint(brother, mother) How can I fix it?
- Assistant