后缀和
N, L = map(int, input().split())
arr = list(map(int, input().split()))
n = 100010
d = [0] * n
dd = [0] * n
for i in range(N):
d[arr[i]] += 1
dd[-1] = d[-1]
for i in range(n-1, 0, -1):
dd[i - 1] = dd[i] + d[i - 1]
ans = 0
for i in range(N+1, 0, -1):
if dd[i] + min(d[i - 1], L) >= i:
ans = i
break
print(ans)
二分
N, L = map(int, input().split())
arr = list(map(int, input().split()))
def acq(h, L):
count = 0
reL = L
for i in range(N):
if arr[i] >= h:
count += 1
elif count < h and reL > 0 and arr[i] >= h - 1:
reL -= 1
count += 1
return count >= h
left, right = 0, N
while left < right:
mid = (left + right + 1) // 2
if acq(mid, L):
left = mid
else:
right = mid - 1
print(left)
双指针
N, L = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
ans = 0
for i in range(0, N):
j = i - L + 1
while j > 0 and arr[j - 1] < i + 1:
j -= 1
if arr[i] >= i and i - j + 1 <= L:
ans = i+1
print(ans)
贪心
N, L = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort()
ans = []
l, r = 0, 0
while r < N:
if arr[l] == arr[r]:
r += 1
elif arr[l] + 1 < arr[r]:
ans.append(min(N - l, N - r + L, N - l - 1))
l += 1
else:
ans.append(min(N - l, N - r + L, 1 + arr[l]))
l += 1
if r >= N:
break
if not ans:
if L >= N:
print(arr[0] + 1)
else:
print(max(arr[0], L))
else:
print(max(ans))