Submission #626070

# Submission time Handle Problem Language Result Execution time Memory
626070 2022-08-11T07:46:12 Z 54skyxenon Mecho (IOI09_mecho) Python 3
19 / 100
1000 ms 19616 KB
# https://oj.uz/problem/view/IOI09_mecho

from collections import deque
from math import ceil

n, s = map(int, input().split())

forest = []
for _ in range(n):
    forest.append(input())

start = None
end = None
hives = []

for i in range(n):
    for j in range(n):
        if forest[i][j] == 'M':
            start = (i, j)
        if forest[i][j] == 'D':
            end = (i, j)
        if forest[i][j] == 'H':
            hives.append((i, j))

dist_hive = [[float('inf')] * n for _ in range(n)]

hives_Q = deque()
for i, j in hives:
    dist_hive[i][j] = 0
    hives_Q.append((i, j, 0))

while hives_Q:
    r, c, depth = hives_Q.popleft()
    for nr, nc in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]:
        if 0 <= nr < n and 0 <= nc < n and forest[nr][nc] == 'G' and dist_hive[nr][nc] == float('inf'):
            hives_Q.append((nr, nc, depth + 1))
            dist_hive[nr][nc] = depth + 1

def bs(lo, hi):    
    global dist_hive

    def ok(midd):
        seen = [[False] * n for _ in range(n)]
        seen[start[0]][start[1]] = True

        mecho_Q = deque([(start[0], start[1], 0)])
        while mecho_Q:
            r, c, depth = mecho_Q.popleft()
            if (r, c) == end:
                return True

            for nr, nc in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]:
                if 0 <= nr < n and 0 <= nc < n and forest[nr][nc] in {'G', 'D'} and (midd + ((depth + 1) // s)) < dist_hive[nr][nc] and not seen[nr][nc]:
                    seen[nr][nc] = True
                    mecho_Q.append((nr, nc, depth + 1))

        return False

    while lo < hi:
        mid = (lo + hi) // 2

        if ok(mid):
            lo = mid + 1
        else:
            hi = mid - 1

    return lo - 1

print(bs(0, n ** 2))
# Verdict Execution time Memory Grader output
1 Correct 17 ms 3284 KB Output is correct
2 Incorrect 17 ms 3332 KB Output isn't correct
3 Correct 15 ms 3284 KB Output is correct
4 Incorrect 20 ms 3372 KB Output isn't correct
5 Incorrect 17 ms 3284 KB Output isn't correct
6 Incorrect 17 ms 3372 KB Output isn't correct
7 Execution timed out 1091 ms 10636 KB Time limit exceeded
8 Correct 16 ms 3284 KB Output is correct
9 Incorrect 17 ms 3412 KB Output isn't correct
10 Correct 18 ms 3356 KB Output is correct
11 Correct 19 ms 3400 KB Output is correct
12 Incorrect 24 ms 3424 KB Output isn't correct
13 Incorrect 26 ms 3448 KB Output isn't correct
14 Correct 44 ms 3436 KB Output is correct
15 Correct 17 ms 3380 KB Output is correct
16 Correct 17 ms 3344 KB Output is correct
17 Correct 18 ms 3364 KB Output is correct
18 Correct 18 ms 3500 KB Output is correct
19 Correct 19 ms 3336 KB Output is correct
20 Correct 19 ms 3412 KB Output is correct
21 Correct 21 ms 3436 KB Output is correct
22 Incorrect 21 ms 3404 KB Output isn't correct
23 Correct 21 ms 3412 KB Output is correct
24 Correct 26 ms 3424 KB Output is correct
25 Incorrect 29 ms 3496 KB Output isn't correct
26 Incorrect 27 ms 3412 KB Output isn't correct
27 Correct 25 ms 3388 KB Output is correct
28 Incorrect 28 ms 3412 KB Output isn't correct
29 Correct 26 ms 3412 KB Output is correct
30 Incorrect 29 ms 3404 KB Output isn't correct
31 Correct 29 ms 3528 KB Output is correct
32 Incorrect 33 ms 3428 KB Output isn't correct
33 Incorrect 275 ms 7476 KB Output isn't correct
34 Correct 295 ms 7388 KB Output is correct
35 Execution timed out 1080 ms 6824 KB Time limit exceeded
36 Correct 357 ms 8644 KB Output is correct
37 Incorrect 397 ms 8668 KB Output isn't correct
38 Execution timed out 1086 ms 8220 KB Time limit exceeded
39 Correct 470 ms 10132 KB Output is correct
40 Incorrect 486 ms 10208 KB Output isn't correct
41 Execution timed out 1081 ms 9872 KB Time limit exceeded
42 Incorrect 594 ms 11660 KB Output isn't correct
43 Incorrect 568 ms 11608 KB Output isn't correct
44 Execution timed out 1086 ms 11608 KB Time limit exceeded
45 Correct 656 ms 13504 KB Output is correct
46 Correct 723 ms 13344 KB Output is correct
47 Execution timed out 1088 ms 13716 KB Time limit exceeded
48 Correct 813 ms 15248 KB Output is correct
49 Correct 809 ms 15268 KB Output is correct
50 Execution timed out 1094 ms 12340 KB Time limit exceeded
51 Correct 926 ms 17324 KB Output is correct
52 Correct 949 ms 17324 KB Output is correct
53 Execution timed out 1090 ms 13540 KB Time limit exceeded
54 Execution timed out 1022 ms 19616 KB Time limit exceeded
55 Execution timed out 1094 ms 19524 KB Time limit exceeded
56 Execution timed out 1089 ms 14120 KB Time limit exceeded
57 Execution timed out 1089 ms 17020 KB Time limit exceeded
58 Execution timed out 1089 ms 17096 KB Time limit exceeded
59 Execution timed out 1082 ms 14524 KB Time limit exceeded
60 Execution timed out 1094 ms 16968 KB Time limit exceeded
61 Execution timed out 1091 ms 17212 KB Time limit exceeded
62 Execution timed out 1094 ms 14704 KB Time limit exceeded
63 Execution timed out 1088 ms 16716 KB Time limit exceeded
64 Execution timed out 1096 ms 16920 KB Time limit exceeded
65 Execution timed out 1082 ms 16772 KB Time limit exceeded
66 Execution timed out 1094 ms 16600 KB Time limit exceeded
67 Execution timed out 1084 ms 16456 KB Time limit exceeded
68 Execution timed out 1088 ms 9672 KB Time limit exceeded
69 Execution timed out 1086 ms 9548 KB Time limit exceeded
70 Execution timed out 1054 ms 9676 KB Time limit exceeded
71 Execution timed out 1088 ms 9568 KB Time limit exceeded
72 Execution timed out 1088 ms 9632 KB Time limit exceeded
73 Execution timed out 1094 ms 13200 KB Time limit exceeded
74 Execution timed out 1099 ms 13224 KB Time limit exceeded
75 Execution timed out 1061 ms 13320 KB Time limit exceeded
76 Execution timed out 1086 ms 13160 KB Time limit exceeded
77 Execution timed out 1088 ms 13272 KB Time limit exceeded
78 Execution timed out 1097 ms 12220 KB Time limit exceeded
79 Execution timed out 1095 ms 12388 KB Time limit exceeded
80 Execution timed out 1098 ms 12364 KB Time limit exceeded
81 Execution timed out 1093 ms 12236 KB Time limit exceeded
82 Execution timed out 1093 ms 12380 KB Time limit exceeded
83 Execution timed out 1069 ms 12056 KB Time limit exceeded
84 Execution timed out 1085 ms 12076 KB Time limit exceeded
85 Execution timed out 1094 ms 12064 KB Time limit exceeded
86 Execution timed out 1087 ms 12136 KB Time limit exceeded
87 Execution timed out 1087 ms 12076 KB Time limit exceeded
88 Execution timed out 1084 ms 11408 KB Time limit exceeded
89 Execution timed out 1083 ms 11376 KB Time limit exceeded
90 Execution timed out 1092 ms 11364 KB Time limit exceeded
91 Execution timed out 1087 ms 11504 KB Time limit exceeded
92 Execution timed out 1081 ms 11596 KB Time limit exceeded