深信服:笔试题(20190918)

打怪升级

先按照怪物等级排序,提取出相应的索引。那么问题就化简为求索引序列中从起始点依次途径各个点最终到达索引序列的末端所经过的边。在源点到目标点的搜索路径中,采用广度优先遍历的方式去搜索,注意-是不允许通过,0是可以通过的。

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
    container = deque()
    container.append([(si, sj)])
    visited = set()
    visited.add((si, sj))
    step = 0
    row, col = len(seq), len(seq[0])
    while True:
        if not container:
            return False
        current = container.popleft()
        value = []
        for cur in current:
            ci, cj = cur
            if ci - 1 >= 0 and seq[ci-1][cj] != '-' and seq[ci-1][cj] not in visited:
                if (ci-1, cj) not in visited:
                    value.append((ci-1, cj))
                    visited.add((ci-1, cj))
            if ci + 1 < row and seq[ci+1][cj] != '-' and seq[ci+1][cj] not in visited:
                if (ci + 1, cj) not in visited:
                    value.append((ci+1, cj))
                    visited.add((ci+1, cj))
            if cj - 1 >= 0 and seq[ci][cj-1] != '-' and seq[ci][cj-1] not in visited:
                if (ci, cj-1) not in visited:
                    value.append((ci, cj-1))
                    visited.add((ci, cj-1))
            if cj + 1 < col and seq[ci][cj+1] != '-' and seq[ci][cj+1] not in visited:
                if (ci, cj+1) not in visited:
                    value.append((ci, cj+1))
                    visited.add((ci, cj+1))
        if value:
            container.append(value)
        step += 1
        if (ti, tj) in visited:
            return step
        # print('Before: ', value, visited, container, step)


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日)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值