AcWing LIS相关问题 187 导弹防御系统

'''
DFS枚举所有可能的元素放到上升序列或者下降序列的情况
'''

ans = [0x7fffffff]  # 最小序列总数
up = [0] * 60       # 上升序列的尾数数值
down = [0] * 60     # 下降序列的尾数数值

# 当前安排第i个数值,上升序列有up_cnt个,下降序列有down_cnt个情况下最少的总序列个数
def dfs(arr, i, up_cnt, down_cnt):
    if up_cnt + down_cnt >= ans[0]:
        return

    if i == len(arr):
        ans[0] = min(ans[0], up_cnt + down_cnt)
        return

    # 第i个数安排到上升序列里面
    pos = 0
    while pos < up_cnt and up[pos] > arr[i]:
        pos += 1

    old_up_val = up[pos]
    old_up_cnt = up_cnt

    up[pos] = arr[i]
    if pos == up_cnt:
        up_cnt += 1

    dfs(arr, i+ 1, up_cnt, down_cnt)

    up[pos] = old_up_val
    up_cnt = old_up_cnt

    # 第i个数安排到下降序列里面
    pos = 0
    while pos < down_cnt and down[pos] < arr[i]:
        pos += 1

    old_down_val = down[pos]
    old_down_cnt = down_cnt
    down[pos] = arr[i]
    if pos == down_cnt:
        down_cnt += 1

    dfs(arr, i + 1, up_cnt, down_cnt)

    down[pos] = old_down_val
    down_cnt = old_down_cnt


while True:
    N = int(input())
    if N == 0:
        break
    arr = list(map(int, input().split()))

    ans[0] = 0x7fffffff
    dfs(arr, 0, 0, 0)
    print(ans[0])

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值