Submission #629669

# Submission time Handle Problem Language Result Execution time Memory
629669 2022-08-14T21:31:28 Z 54skyxenon Mecho (IOI09_mecho) PyPy 3
23 / 100
1000 ms 65536 KB
# https://oj.uz/problem/view/IOI09_mecho
 
from collections import deque
 
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))

def valid_square(x, y):
	return 0 <= x < n and 0 <= y < n and (forest[x][y] == 'G' or forest[x][y] == 'M')

def mecho_reached(mecho_dis, bees_dis):
    return mecho_dis // s < bees_dis

def ok(eating_time):
    bees_visited = [[False] * n for _ in range(n)]
    mecho_visited = [[False] * n for _ in range(n)]
    bees_time = [[0] * n for _ in range(n)]
    mecho_time = [[0] * n for _ in range(n)]

    Q = deque()

    # move bees
    for hive_x, hive_y in hives:
        Q.append((hive_x, hive_y))
        bees_visited[hive_x][hive_y] = True

    while Q:
        x, y = Q.popleft()

        for nx, ny in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
            if valid_square(nx, ny) and not bees_visited[nx][ny]:
                bees_time[nx][ny] = bees_time[x][y] + 1    
                Q.append((nx, ny))
                bees_visited[nx][ny] = True
    
    # move Mecho
    Q.append(start)
    mecho_visited[start[0]][start[1]] = True
    if bees_time[start[0]][start[1]] <= eating_time:
        Q.popleft()
    
    while Q:
        x, y = Q.popleft()

        for nx, ny in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]:
            '''
            check if mecho reaces node[x][y] before the bees by dividing the time mecho takes to reach a node by s, since mecho walks s steps at a time.
            subtract the eating time from the time taken for the bees to reach the node, because that time was used by mecho for eating
            '''
            if valid_square(nx, ny) and not mecho_visited[nx][ny] and mecho_reached(mecho_time[x][y] + 1, bees_time[nx][ny] - eating_time):
                mecho_visited[nx][ny] = True
                Q.append((nx, ny))
                mecho_time[nx][ny] = mecho_time[x][y] + 1

    # check if mecho reached a node surrounding his cave before the bees
    for nx, ny in [(end[0] + 1, end[1]), (end[0] - 1, end[1]), (end[0], end[1] + 1), (end[0], end[1] - 1)]:
        if valid_square(nx, ny) and mecho_reached(mecho_time[nx][ny], bees_time[nx][ny] - eating_time) and mecho_visited[nx][ny]:
            return True

    return False
 
def bs(lo, hi):
    while lo < hi:
        mid = (lo + hi) // 2
 
        if ok(mid):
            lo = mid
        else:
            hi = mid - 1

    if lo > hi:
        return -1
 
    return lo
 
print(bs(0, n ** 2))
# Verdict Execution time Memory Grader output
1 Execution timed out 1086 ms 29848 KB Time limit exceeded
2 Execution timed out 1089 ms 29184 KB Time limit exceeded
3 Correct 42 ms 18668 KB Output is correct
4 Execution timed out 1090 ms 29480 KB Time limit exceeded
5 Correct 50 ms 18544 KB Output is correct
6 Execution timed out 1083 ms 30056 KB Time limit exceeded
7 Runtime error 640 ms 65536 KB Execution killed with signal 9
8 Correct 56 ms 20008 KB Output is correct
9 Execution timed out 1095 ms 31096 KB Time limit exceeded
10 Execution timed out 1088 ms 30752 KB Time limit exceeded
11 Correct 61 ms 20216 KB Output is correct
12 Correct 83 ms 22184 KB Output is correct
13 Correct 69 ms 21660 KB Output is correct
14 Correct 118 ms 23552 KB Output is correct
15 Correct 58 ms 20264 KB Output is correct
16 Execution timed out 1087 ms 29860 KB Time limit exceeded
17 Correct 71 ms 20820 KB Output is correct
18 Execution timed out 1090 ms 30200 KB Time limit exceeded
19 Correct 70 ms 20824 KB Output is correct
20 Execution timed out 1089 ms 30076 KB Time limit exceeded
21 Execution timed out 1094 ms 27720 KB Time limit exceeded
22 Correct 90 ms 22884 KB Output is correct
23 Execution timed out 1094 ms 27464 KB Time limit exceeded
24 Correct 91 ms 22604 KB Output is correct
25 Correct 88 ms 23868 KB Output is correct
26 Execution timed out 1095 ms 27716 KB Time limit exceeded
27 Execution timed out 1089 ms 27592 KB Time limit exceeded
28 Correct 87 ms 23972 KB Output is correct
29 Execution timed out 1092 ms 27840 KB Time limit exceeded
30 Execution timed out 1095 ms 27856 KB Time limit exceeded
31 Execution timed out 1051 ms 27504 KB Time limit exceeded
32 Execution timed out 1056 ms 27680 KB Time limit exceeded
33 Execution timed out 1077 ms 31644 KB Time limit exceeded
34 Correct 322 ms 31860 KB Output is correct
35 Execution timed out 1093 ms 30988 KB Time limit exceeded
36 Execution timed out 1083 ms 33928 KB Time limit exceeded
37 Correct 420 ms 33952 KB Output is correct
38 Execution timed out 1099 ms 33516 KB Time limit exceeded
39 Correct 439 ms 36724 KB Output is correct
40 Correct 491 ms 34444 KB Output is correct
41 Execution timed out 1046 ms 37120 KB Time limit exceeded
42 Execution timed out 1095 ms 39448 KB Time limit exceeded
43 Correct 587 ms 39428 KB Output is correct
44 Execution timed out 1089 ms 40424 KB Time limit exceeded
45 Correct 602 ms 39920 KB Output is correct
46 Execution timed out 1101 ms 41340 KB Time limit exceeded
47 Execution timed out 1089 ms 43080 KB Time limit exceeded
48 Execution timed out 1089 ms 43816 KB Time limit exceeded
49 Execution timed out 1100 ms 46292 KB Time limit exceeded
50 Execution timed out 1086 ms 46784 KB Time limit exceeded
51 Correct 891 ms 43924 KB Output is correct
52 Execution timed out 1099 ms 49244 KB Time limit exceeded
53 Execution timed out 1091 ms 50648 KB Time limit exceeded
54 Execution timed out 1096 ms 51520 KB Time limit exceeded
55 Execution timed out 1062 ms 50492 KB Time limit exceeded
56 Execution timed out 1099 ms 55044 KB Time limit exceeded
57 Execution timed out 1090 ms 50376 KB Time limit exceeded
58 Execution timed out 1062 ms 59536 KB Time limit exceeded
59 Execution timed out 1086 ms 52888 KB Time limit exceeded
60 Execution timed out 1099 ms 53860 KB Time limit exceeded
61 Execution timed out 1099 ms 58392 KB Time limit exceeded
62 Execution timed out 1100 ms 60804 KB Time limit exceeded
63 Execution timed out 1097 ms 61312 KB Time limit exceeded
64 Execution timed out 1090 ms 60244 KB Time limit exceeded
65 Execution timed out 1081 ms 53948 KB Time limit exceeded
66 Execution timed out 1099 ms 61528 KB Time limit exceeded
67 Execution timed out 1100 ms 53416 KB Time limit exceeded
68 Runtime error 423 ms 65536 KB Execution killed with signal 9
69 Runtime error 481 ms 65536 KB Execution killed with signal 9
70 Runtime error 427 ms 65536 KB Execution killed with signal 9
71 Runtime error 433 ms 65536 KB Execution killed with signal 9
72 Runtime error 422 ms 65536 KB Execution killed with signal 9
73 Execution timed out 1094 ms 65224 KB Time limit exceeded
74 Execution timed out 1094 ms 65468 KB Time limit exceeded
75 Execution timed out 1088 ms 65172 KB Time limit exceeded
76 Execution timed out 1083 ms 65484 KB Time limit exceeded
77 Execution timed out 1083 ms 65464 KB Time limit exceeded
78 Runtime error 959 ms 65536 KB Execution killed with signal 9
79 Runtime error 388 ms 65536 KB Execution killed with signal 9
80 Execution timed out 1098 ms 65412 KB Time limit exceeded
81 Runtime error 917 ms 65536 KB Execution killed with signal 9
82 Runtime error 948 ms 65536 KB Execution killed with signal 9
83 Execution timed out 1075 ms 65532 KB Time limit exceeded
84 Runtime error 408 ms 65536 KB Execution killed with signal 9
85 Runtime error 387 ms 65536 KB Execution killed with signal 9
86 Execution timed out 1095 ms 65340 KB Time limit exceeded
87 Execution timed out 1099 ms 65440 KB Time limit exceeded
88 Runtime error 375 ms 65536 KB Execution killed with signal 9
89 Runtime error 369 ms 65536 KB Execution killed with signal 9
90 Execution timed out 1095 ms 65536 KB Time limit exceeded
91 Runtime error 562 ms 65536 KB Execution killed with signal 9
92 Runtime error 968 ms 65536 KB Execution killed with signal 9