题解 | #合唱队# 二分查找 O(nlogn)

合唱队

https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4

import bisect

n = int(input())

a = list(map(int, input().split()))
lt = [0] * n
rt = [0] * n

st = []  # st[i]表示 所有递增子序列,可以排第i位同学的最低身高;可以证明st自身也是是递增的
for i in range(n):
    idx = bisect.bisect_left(st, a[i])
    if idx == len(st):
        st.append(a[i])
    st[idx] = a[i]   # 更新可以排第i位同学的最低身高
    lt[i] = i - idx

st = []
for i in range(n - 1, -1, -1):
    idx = bisect.bisect_left(st, a[i])
    if idx == len(st):
        st.append(a[i])
    st[idx] = a[i]
    rt[i] = n - 1 - i - idx
ans = 3000
for i in range(n):
    ans = min(ans, lt[i] + rt[i])
print(ans)

全部评论

相关推荐

点赞 评论 收藏
分享
想申请延毕了,找工作找到崩溃,越找就越想摆烂,还有25届的和我一样感受吗?
码农索隆:没事哒,好兄弟,慢慢来,调整心态,车到山前必有路,感到迷茫的时候,多抬头看看
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务