Submission #628762

# Submission time Handle Problem Language Result Execution time Memory
628762 2022-08-13T16:39:59 Z 54skyxenon Mecho (IOI09_mecho) Python 3
38 / 100
1000 ms 20148 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
 
    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 26 ms 3360 KB Output is correct
2 Correct 23 ms 3312 KB Output is correct
3 Correct 21 ms 3328 KB Output is correct
4 Correct 23 ms 3400 KB Output is correct
5 Correct 20 ms 3360 KB Output is correct
6 Correct 19 ms 3412 KB Output is correct
7 Execution timed out 1070 ms 11172 KB Time limit exceeded
8 Correct 20 ms 3380 KB Output is correct
9 Correct 23 ms 3388 KB Output is correct
10 Correct 22 ms 3364 KB Output is correct
11 Correct 23 ms 3284 KB Output is correct
12 Incorrect 29 ms 3408 KB Output isn't correct
13 Incorrect 36 ms 3400 KB Output isn't correct
14 Correct 51 ms 3392 KB Output is correct
15 Correct 25 ms 3404 KB Output is correct
16 Correct 25 ms 3308 KB Output is correct
17 Correct 20 ms 3404 KB Output is correct
18 Correct 23 ms 3412 KB Output is correct
19 Correct 29 ms 3400 KB Output is correct
20 Correct 30 ms 3376 KB Output is correct
21 Correct 31 ms 3388 KB Output is correct
22 Correct 30 ms 3404 KB Output is correct
23 Correct 24 ms 3408 KB Output is correct
24 Correct 34 ms 3368 KB Output is correct
25 Correct 38 ms 3500 KB Output is correct
26 Correct 41 ms 3516 KB Output is correct
27 Correct 41 ms 3420 KB Output is correct
28 Correct 30 ms 3512 KB Output is correct
29 Correct 29 ms 3460 KB Output is correct
30 Correct 43 ms 3432 KB Output is correct
31 Correct 31 ms 3480 KB Output is correct
32 Correct 34 ms 3476 KB Output is correct
33 Correct 283 ms 7560 KB Output is correct
34 Correct 304 ms 7496 KB Output is correct
35 Execution timed out 1079 ms 6912 KB Time limit exceeded
36 Correct 357 ms 8852 KB Output is correct
37 Correct 371 ms 8924 KB Output is correct
38 Execution timed out 1087 ms 8412 KB Time limit exceeded
39 Correct 437 ms 10292 KB Output is correct
40 Correct 485 ms 10284 KB Output is correct
41 Execution timed out 1091 ms 9976 KB Time limit exceeded
42 Correct 545 ms 12016 KB Output is correct
43 Correct 596 ms 12032 KB Output is correct
44 Execution timed out 1071 ms 11832 KB Time limit exceeded
45 Correct 703 ms 13676 KB Output is correct
46 Correct 756 ms 13700 KB Output is correct
47 Execution timed out 1079 ms 14144 KB Time limit exceeded
48 Correct 836 ms 15728 KB Output is correct
49 Correct 838 ms 15832 KB Output is correct
50 Execution timed out 1057 ms 12732 KB Time limit exceeded
51 Correct 894 ms 17888 KB Output is correct
52 Correct 951 ms 17860 KB Output is correct
53 Execution timed out 1087 ms 13516 KB Time limit exceeded
54 Execution timed out 1047 ms 20148 KB Time limit exceeded
55 Execution timed out 1080 ms 20032 KB Time limit exceeded
56 Execution timed out 1080 ms 14644 KB Time limit exceeded
57 Execution timed out 1038 ms 17296 KB Time limit exceeded
58 Execution timed out 1085 ms 17864 KB Time limit exceeded
59 Execution timed out 1089 ms 14796 KB Time limit exceeded
60 Execution timed out 1084 ms 18340 KB Time limit exceeded
61 Execution timed out 1093 ms 18248 KB Time limit exceeded
62 Execution timed out 1086 ms 14628 KB Time limit exceeded
63 Execution timed out 1086 ms 17400 KB Time limit exceeded
64 Execution timed out 1083 ms 16720 KB Time limit exceeded
65 Execution timed out 1089 ms 17764 KB Time limit exceeded
66 Execution timed out 1080 ms 17032 KB Time limit exceeded
67 Execution timed out 1087 ms 17128 KB Time limit exceeded
68 Execution timed out 1077 ms 10264 KB Time limit exceeded
69 Execution timed out 1086 ms 10360 KB Time limit exceeded
70 Execution timed out 1089 ms 10248 KB Time limit exceeded
71 Execution timed out 1085 ms 10168 KB Time limit exceeded
72 Execution timed out 1079 ms 10212 KB Time limit exceeded
73 Execution timed out 1087 ms 13768 KB Time limit exceeded
74 Execution timed out 1092 ms 13888 KB Time limit exceeded
75 Execution timed out 1087 ms 13900 KB Time limit exceeded
76 Execution timed out 1072 ms 13976 KB Time limit exceeded
77 Execution timed out 1087 ms 13972 KB Time limit exceeded
78 Execution timed out 1085 ms 12792 KB Time limit exceeded
79 Execution timed out 1089 ms 12944 KB Time limit exceeded
80 Execution timed out 1077 ms 13012 KB Time limit exceeded
81 Execution timed out 1087 ms 12844 KB Time limit exceeded
82 Execution timed out 1053 ms 12872 KB Time limit exceeded
83 Execution timed out 1085 ms 12736 KB Time limit exceeded
84 Execution timed out 1086 ms 12624 KB Time limit exceeded
85 Execution timed out 1074 ms 12692 KB Time limit exceeded
86 Execution timed out 1083 ms 12688 KB Time limit exceeded
87 Execution timed out 1086 ms 12876 KB Time limit exceeded
88 Execution timed out 1081 ms 11976 KB Time limit exceeded
89 Execution timed out 1052 ms 12036 KB Time limit exceeded
90 Execution timed out 1091 ms 11968 KB Time limit exceeded
91 Execution timed out 1085 ms 12128 KB Time limit exceeded
92 Execution timed out 1076 ms 12188 KB Time limit exceeded