先按照怪物等級排序,提取出相應的索引。那麼問題就化簡為求索引序列中從起始點依次途徑各個點最終到達索引序列的末端所經過的邊。在源點到目標點的搜尋路徑中,採用廣度優先遍歷的方式去搜尋,注意-
是不允許通過,0
是可以通過的。
import sys
from collections import deque
defbeat_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]
notin
'0-'
:[sj]
)(si, sj)
) change =
[value.index(vu)
for vu in
(sorted
(value))]
target =
[index[ci]
for ci in change]
res =
0 pi, pj =0,
0for tu in target:
flag = find(seq, pi, pj, tu[0]
, tu[1]
)# print((pi, pj), tu, flag)
if pi ==
0and pj ==0:
pi, pj =0,
0 pi, pj = tu[0]
, tu[1]
ifnot flag:
return-1
if flag ==-1
:continue
res += flag
return res
deffind
(seq, si, sj, ti, tj)
:if si == ti and sj == tj:
return-1
container = deque()[
(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>=
0and seq[ci-1]
[cj]
!='-'
and seq[ci-1]
[cj]
notin visited:
if(ci-
1, cj)
notin visited:
(ci-
1, cj)
) visited.add(
(ci-
1, cj)
)if ci +
1< row and seq[ci+1]
[cj]
!='-'
and seq[ci+1]
[cj]
notin visited:
if(ci +
1, cj)
notin visited:
(ci+
1, cj)
) visited.add(
(ci+
1, cj)
)if cj -
1>=
0and seq[ci]
[cj-1]
!='-'
and seq[ci]
[cj-1]
notin visited:
if(ci, cj-1)
notin visited:
(ci, cj-1)
) visited.add(
(ci, cj-1)
)if cj +
1< col and seq[ci]
[cj+1]
!='-'
and seq[ci]
[cj+1]
notin visited:
if(ci, cj+1)
notin visited:
(ci, cj+1)
) visited.add(
(ci, cj+1)
)if value:
step +=1if
(ti, tj)
in visited:
return step
# print('before: ', value, visited, container, step)
if __name__ ==
'__main__'
: seq =
for line in sys.stdin:
).split())
res = beat_monster(seq)
print
(res)
'''1 2 3
- - 4
7 6 5
1 2 4
- - -
5 6 7
'''
起初打算用二分查詢的方法來求出最佳值,上界和下界分別為序列的長度和和值除以包容量。但考慮到分配上的問題,其實跟遍歷迴圈好像差不多時間複雜度。結果沒想到,這個測試用例有點懸,直接用下界提交,通過了83.33%。
import math
defteam_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
33 2 2 1
3'''
要注意對字串的長度序列求集合排除相同值的影響,後面就是建堆來求第n大的值
。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:
-vu)
res =
iflen
(value)
< num:
return res
for _ in
range
(num-1)
: res =
return res
if __name__ ==
'__main__'
: seq =
list
(input()
.strip(
).split())
num =
int(
input()
) res = nth_sub_string(seq, num)
(res)
'''abc ab a
2abc ab ab
3'''
(最近更新:2023年09月18日)
深信服筆試題
題很多,共8頁 1.sort int array,int n 排序,讓小於等於0的數放在左邊,大於0得數放在右邊,要求時間複雜度為o n 2.假設有1001個棋子,你和乙個人拿這些棋子,誰拿到最後乙個,算誰贏,每次只能拿1,2,4個棋子。你先出手,是否有把握必贏,如果沒有,說明為什麼。3.編寫ato...
深信服筆試題
做了一波深信服的筆試題 題目難度定義成一般吧,程式設計題前兩道還好,第一道考察的是格式化字串,第二道考察的是階乘。第三道。忘了 其餘都是選擇題,知識點也比較廣泛,考察的知識點我把我記住的例下來 1 行內元素 a,b,br,em,font,i,img,input,label,select,span.塊...
深信服筆試題
1.子串模糊匹配 var readline require readline rl readline.createinte ce var k 2 var inputs rl.on line function data function main a,b var pattern newregexp r...