深信服:笔试题(20190918)

血红的双手。 提交于 2019-11-30 02:12:38

打怪升级

先按照怪物等级排序,提取出相应的索引。那么问题就化简为求索引序列中从起始点依次途径各个点最终到达索引序列的末端所经过的边。代码逻辑有点问题,时间有限,提交通过率为50%。

import sys
from collections import deque


def beat_monster(seq):
    if not seq:
        return 0
    value, index = [], []
    for si in range(len(seq)):
        for sj in range(len(seq[0])):
            if seq[si][sj] not in '0-':
                value.append(seq[si][sj])
                index.append((si, sj))
    change = [value.index(vu) for vu in (sorted(value))]
    target = [index[ci] for ci in change]
    res = 0
    pi, pj = 0, 0
    for tu in target:
        flag = find(seq, pi, pj, tu[0], tu[1])
        # print((pi, pj), tu, flag)
        if pi == 0 and pj == 0:
            pi, pj = 0, 0
        pi, pj = tu[0], tu[1]
        if not flag:
            return -1
        if flag == -1:
            continue
        res += flag
    return res


def find(seq, si, sj, ti, tj):
    if si == ti and sj == tj:
        return -1
    current = deque()
    current.append((si, sj))
    visited = set()
    visited.add((si, sj))
    step = 0
    row, col = len(seq), len(seq[0])
    while current:
        ci, cj = current.popleft()
        if ci - 1 >= 0 and seq[ci-1][cj] not in '-0' and seq[ci-1][cj] not in visited:
            if (ci-1, cj) not in visited:
                current.append((ci-1, cj))
            visited.add((ci-1, cj))
        if ci + 1 < row and seq[ci+1][cj] not in '-0' and seq[ci+1][cj] not in visited:
            if (ci + 1, cj) not in visited:
                current.append((ci+1, cj))
            visited.add((ci+1, cj))
        if cj - 1 >= 0 and seq[ci][cj-1] not in '-0' and seq[ci][cj-1] not in visited:
            if (ci, cj-1) not in visited:
                current.append((ci, cj-1))
            visited.add((ci, cj-1))
        if cj + 1 < col and seq[ci][cj+1] not in '-0' and seq[ci][cj+1] not in visited:
            if (ci, cj+1) not in visited:
                current.append((ci, cj+1))
            visited.add((ci, cj+1))
        step += 1
        if (ti, tj) in visited:
            return step
        # print('-', current, visited, step, (ti, tj))
    return False


if __name__ == '__main__':
    seq = []
    for line in sys.stdin:
        seq.append(line.strip().split())
    res = beat_monster(seq)
    print(res)

'''
1 2 3
- - 4
7 6 5

1 2 4
- - -
5 6 7
'''

团队搬家

起初打算用二分查找的方法来求出最佳值,上界和下界分别为序列的长度和和值除以包容量。但考虑到分配上的问题,其实跟遍历循环好像差不多时间复杂度。结果没想到,这个测试用例有点悬,直接用下界提交,通过了83.33%。

import math


def team_move_house(seq, num):
    if not seq:
        return 0
    left, right = math.ceil(sum(seq)/num), len(seq)
    return left


if __name__ == '__main__':
    seq = list(map(int, input().strip().split()))
    num = int(input().strip())
    res = team_move_house(seq, num)
    print(res)

'''
1 2
3

3 2 2 1
3
'''

第N长的子串长度

要注意对字符串的长度序列求集合排除相同值的影响,后面就是建堆来求第N大的值

from heapq import heappush, heappop


def nth_sub_string(seq, num):
    if not seq:
        return 0
    value = list(map(len, seq))
    value = list(set(value))
    # print(value)
    cur = []
    for vu in value:
        heappush(cur, -vu)
    res = -heappop(cur)
    if len(value) < num:
        return res
    for _ in range(num-1):
        res = -heappop(cur)
    return res


if __name__ == '__main__':
    seq = list(input().strip().split())
    num = int(input())
    res = nth_sub_string(seq, num)
    print(res)

'''
abc ab a
2

abc ab ab
3
'''

(最近更新:2019年09月18日)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!