Skip to content

Commit c9ec65c

Browse files
committed
solve problems
1 parent a65f97b commit c9ec65c

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import math
2+
3+
4+
def dist(a, b):
5+
return math.hypot(a[0] - b[0], a[1] - b[1])
6+
7+
8+
def can_escape(croc, D):
9+
"""判断鳄鱼 croc=(x,y) 能否一步跳到岸"""
10+
x, y = croc
11+
return 50 - max(abs(x), abs(y)) <= D
12+
13+
14+
n, d = list(map(int, input().split()))
15+
crocs = []
16+
for _ in range(n):
17+
x, y = list(map(int, input().split()))
18+
crocs.append((x, y))
19+
20+
# 直接从岛跳到岸
21+
if d >= 42.5: # 50 - 7.5
22+
print("Yes")
23+
24+
# 找第一跳可达的鳄鱼
25+
first = [i for i, (x, y) in enumerate(crocs) if dist((0, 0), (x, y)) <= d + 7.5]
26+
27+
visited = [False] * n
28+
queue = first[:]
29+
30+
is_escaped = False
31+
while queue and not is_escaped:
32+
i = queue.pop()
33+
if visited[i]:
34+
continue
35+
visited[i] = True
36+
37+
if can_escape(crocs[i], d):
38+
is_escaped = True
39+
40+
if not is_escaped:
41+
for j in range(n):
42+
if not visited[j] and dist(crocs[i], crocs[j]) <= d:
43+
queue.append(j)
44+
45+
print("Yes" if is_escaped else "No")
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import heapq
2+
3+
4+
def build_wpl(freq):
5+
pq = list(freq.values())
6+
heapq.heapify(pq)
7+
wpl = 0
8+
while len(pq) > 1:
9+
a = heapq.heappop(pq)
10+
b = heapq.heappop(pq)
11+
wpl += a + b
12+
heapq.heappush(pq, a + b)
13+
return wpl
14+
15+
16+
def is_prefix_free(codes):
17+
sorted_codes = sorted(codes.values())
18+
for i in range(len(sorted_codes) - 1):
19+
if sorted_codes[i + 1].startswith(sorted_codes[i]):
20+
return False
21+
return True
22+
23+
24+
n = int(input())
25+
freq = {}
26+
data = input().split()
27+
for i in range(0, len(data), 2):
28+
freq[data[i]] = int(data[i + 1])
29+
30+
WPL_min = build_wpl(freq)
31+
32+
m = int(input())
33+
for _ in range(m):
34+
codes = {}
35+
ok = True
36+
cur_wpl = 0
37+
for _ in range(n):
38+
c, code = input().split()
39+
codes[c] = code
40+
cur_wpl += len(code) * freq.get(c, 0)
41+
42+
# condition 1:all characters are present
43+
if len(codes) != n:
44+
ok = False
45+
# condition 2:WPL is minimum
46+
if cur_wpl != WPL_min:
47+
ok = False
48+
# condition 3:prefix-free
49+
if ok and not is_prefix_free(codes):
50+
ok = False
51+
print("Yes" if ok else "No")

0 commit comments

Comments
 (0)