Submission #626056

# Submission time Handle Problem Language Result Execution time Memory
626056 2022-08-11T07:37:38 Z 54skyxenon Mecho (IOI09_mecho) Python 3
38 / 100
1000 ms 17312 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

    if lo > hi:
        return -1

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

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

    return lo

print(bs(0, n ** 2))
# Verdict Execution time Memory Grader output
1 Correct 17 ms 3412 KB Output is correct
2 Correct 17 ms 3412 KB Output is correct
3 Correct 18 ms 3324 KB Output is correct
4 Correct 17 ms 3392 KB Output is correct
5 Correct 17 ms 3412 KB Output is correct
6 Correct 18 ms 3364 KB Output is correct
7 Execution timed out 1086 ms 10704 KB Time limit exceeded
8 Correct 16 ms 3332 KB Output is correct
9 Correct 17 ms 3364 KB Output is correct
10 Correct 17 ms 3412 KB Output is correct
11 Correct 17 ms 3404 KB Output is correct
12 Incorrect 24 ms 3452 KB Output isn't correct
13 Incorrect 27 ms 3440 KB Output isn't correct
14 Correct 42 ms 3436 KB Output is correct
15 Correct 17 ms 3384 KB Output is correct
16 Correct 18 ms 3388 KB Output is correct
17 Correct 19 ms 3324 KB Output is correct
18 Correct 24 ms 3348 KB Output is correct
19 Correct 18 ms 3408 KB Output is correct
20 Correct 19 ms 3432 KB Output is correct
21 Correct 19 ms 3412 KB Output is correct
22 Correct 20 ms 3352 KB Output is correct
23 Correct 25 ms 3420 KB Output is correct
24 Correct 23 ms 3432 KB Output is correct
25 Correct 24 ms 3524 KB Output is correct
26 Correct 26 ms 3440 KB Output is correct
27 Correct 25 ms 3396 KB Output is correct
28 Correct 29 ms 3372 KB Output is correct
29 Correct 29 ms 3440 KB Output is correct
30 Correct 32 ms 3408 KB Output is correct
31 Correct 31 ms 3520 KB Output is correct
32 Correct 33 ms 3492 KB Output is correct
33 Correct 286 ms 7644 KB Output is correct
34 Correct 302 ms 7404 KB Output is correct
35 Execution timed out 1089 ms 6796 KB Time limit exceeded
36 Correct 363 ms 8700 KB Output is correct
37 Correct 376 ms 8680 KB Output is correct
38 Execution timed out 1093 ms 8124 KB Time limit exceeded
39 Correct 509 ms 10136 KB Output is correct
40 Correct 496 ms 10104 KB Output is correct
41 Execution timed out 1085 ms 9808 KB Time limit exceeded
42 Correct 576 ms 11644 KB Output is correct
43 Correct 584 ms 11632 KB Output is correct
44 Execution timed out 1092 ms 11608 KB Time limit exceeded
45 Correct 747 ms 13372 KB Output is correct
46 Correct 774 ms 13348 KB Output is correct
47 Execution timed out 1087 ms 10916 KB Time limit exceeded
48 Correct 809 ms 15260 KB Output is correct
49 Correct 872 ms 15228 KB Output is correct
50 Execution timed out 1083 ms 9856 KB Time limit exceeded
51 Execution timed out 1075 ms 17304 KB Time limit exceeded
52 Execution timed out 1073 ms 17312 KB Time limit exceeded
53 Execution timed out 1066 ms 11260 KB Time limit exceeded
54 Execution timed out 1071 ms 13616 KB Time limit exceeded
55 Execution timed out 1088 ms 14888 KB Time limit exceeded
56 Execution timed out 1090 ms 11480 KB Time limit exceeded
57 Execution timed out 1089 ms 15620 KB Time limit exceeded
58 Execution timed out 1096 ms 14816 KB Time limit exceeded
59 Execution timed out 1101 ms 11652 KB Time limit exceeded
60 Execution timed out 1096 ms 16352 KB Time limit exceeded
61 Execution timed out 1084 ms 16224 KB Time limit exceeded
62 Execution timed out 1096 ms 13280 KB Time limit exceeded
63 Execution timed out 1093 ms 16456 KB Time limit exceeded
64 Execution timed out 1095 ms 16740 KB Time limit exceeded
65 Execution timed out 1072 ms 16076 KB Time limit exceeded
66 Execution timed out 1033 ms 14768 KB Time limit exceeded
67 Execution timed out 1091 ms 15704 KB Time limit exceeded
68 Execution timed out 1033 ms 9560 KB Time limit exceeded
69 Execution timed out 1068 ms 9556 KB Time limit exceeded
70 Execution timed out 1098 ms 9588 KB Time limit exceeded
71 Execution timed out 1088 ms 9548 KB Time limit exceeded
72 Execution timed out 1092 ms 9548 KB Time limit exceeded
73 Execution timed out 1088 ms 13156 KB Time limit exceeded
74 Execution timed out 1085 ms 13228 KB Time limit exceeded
75 Execution timed out 1080 ms 13260 KB Time limit exceeded
76 Execution timed out 1090 ms 13204 KB Time limit exceeded
77 Execution timed out 1099 ms 13204 KB Time limit exceeded
78 Execution timed out 1098 ms 12284 KB Time limit exceeded
79 Execution timed out 1093 ms 12204 KB Time limit exceeded
80 Execution timed out 1093 ms 12320 KB Time limit exceeded
81 Execution timed out 1100 ms 12272 KB Time limit exceeded
82 Execution timed out 1096 ms 12236 KB Time limit exceeded
83 Execution timed out 1093 ms 12200 KB Time limit exceeded
84 Execution timed out 1101 ms 12116 KB Time limit exceeded
85 Execution timed out 1098 ms 12108 KB Time limit exceeded
86 Execution timed out 1090 ms 12148 KB Time limit exceeded
87 Execution timed out 1091 ms 12064 KB Time limit exceeded
88 Execution timed out 1078 ms 11388 KB Time limit exceeded
89 Execution timed out 1088 ms 11372 KB Time limit exceeded
90 Execution timed out 1090 ms 11368 KB Time limit exceeded
91 Execution timed out 1091 ms 11400 KB Time limit exceeded
92 Execution timed out 1086 ms 11396 KB Time limit exceeded