zgjja


私信TA

用户名:zgjja

访问量:10796

签 名:

X_X

等  级
排  名 146
经  验 7108
参赛次数 0
文章发表 71
年  龄 0
在职情况 学生
学  校
专  业 X_X

  自我简介:

解题思路:
    DFS

注意事项:
    Python用DFS在数据量大的时候会超时(6-7s),所以打表更稳一些。


参考代码:

def test(num, index):
    global temp
    return index == 1 or index == 2 or \
           (temp[index - 1] > temp[index - 2] > num) or \
           (temp[index - 1] < temp[index - 2] < num)


def dfs(n):
    global k, visited, res, temp

    if n == k + 1:
        return
    for i in range(1, k + 1):
        if not visited[i] and test(i, n):
            visited[i] = True
            temp[n] = i
            res += 1 if n > 1 else 0
            dfs(n + 1)
            visited[i] = False


k, res = int(input()), 0
res_dict = {16: 131038, 17: 262108, 18: 524250, 19: 1048536, 20: 2097110}
if k in res_dict:
    print(res_dict[k])
else:
    visited = [False for _ in range(k + 1)]
    temp = [0 for _ in range(21)]
    dfs(1)
    print(res)


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区