打卡记录
给小朋友们分糖果 II(容斥原理 + 隔板法)
链接
def c2(n):return n * (n - 1) // 2 if n > 1 else 0class Solution:def distributeCandies(self, n: int, limit: int) -> int:return c2(n + 2) - 3 * c2(n - limit + 1) + 3 * c2(n - 2 * limit) - c2(n - 3 * limit - 1)
Woodcutters(贪心 or DP)
链接
import sysn = int(input())
if n == 1:print(1)sys.exit(0)
elif n == 2:print(2)sys.exit(0)tree = [list(map(int, input().split())) for _ in range(n)]
tree.sort()ans = 2
for i in range(1, n - 1):if tree[i][0] - tree[i][1] > tree[i - 1][0]:ans += 1elif tree[i][0] + tree[i][1] < tree[i + 1][0]:ans += 1tree[i][0] += tree[i][1]
print(ans)