Submission #646057

# Submission time Handle Problem Language Result Execution time Memory
646057 2022-09-28T14:34:03 Z ymm Mecho (IOI09_mecho) C++17
100 / 100
170 ms 9564 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 810;
char forest[N][N];
int near_bee[N][N];
int bhi, bhj, bsi, bsj;
ll n, s;


bool can_go_bear(int i, int j)
{
	return 0 <= i && i < n && 0 <= j && j < n
	       && forest[i][j] != 'T';
}
bool can_go_bee(int i, int j)
{
	return 0 <= i && i < n && 0 <= j && j < n
	       && forest[i][j] != 'T' && forest[i][j] != 'D';
}

void bfs_bee()
{
	queue<pii> q;
	memset(near_bee, -1, sizeof(near_bee));
	Loop (i,0,n) Loop (j,0,n) {
		if (forest[i][j] == 'H') {
			near_bee[i][j] = 0;
			q.push({i, j});
		}
	}
	while (q.size()) {
		auto [i, j] = q.front();
		q.pop();
		int d = near_bee[i][j];
		for (auto [x_, y_] : {pii{0,1}, {0,-1}, {1,0}, {-1,0}}) {
			int x = i+x_, y = j+y_;
			if (!can_go_bee(x, y))
				continue;
			if (near_bee[x][y] != -1)
				continue;
			near_bee[x][y] = d+1;
			q.push({x, y});
		}
	}
}

void find_bear()
{
	Loop (i,0,n) Loop (j,0,n) {
		if (forest[i][j] == 'D') {
			bhi = i;
			bhj = j;
		}
		if (forest[i][j] == 'M') {
			bsi = i;
			bsj = j;
		}
	}
}

bool bfs_bear(int wait)
{
	if (wait >= near_bee[bsi][bsj])
		return 0;
	static ll dis[N][N];
	memset(dis, -1, sizeof(dis));
	queue<pii> q;
	q.push({bsi, bsj});
	dis[bsi][bsj] = (ll)wait * s;
	while (q.size()) {
		auto [i, j] = q.front();
		q.pop();
		ll d = dis[i][j];
		for (auto [x_, y_] : {pii{0,1}, {0,-1}, {1,0}, {-1,0}}) {
			int x = i+x_, y = j+y_;
			if (!can_go_bear(x, y))
				continue;
			if (near_bee[x][y] != -1
			    && near_bee[x][y] <= (d+1)/s)
				continue;
			if (dis[x][y] != -1)
				continue;
			dis[x][y] = d+1;
			q.push({x, y});
		}
	}
	return dis[bhi][bhj] != -1;
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n >> s;
	Loop (i,0,n)
		cin >> forest[i];
	bfs_bee();
	find_bear();
	int l = -1, r = near_bee[bsi][bsj]-1;
	while (l < r) {
		int mid = (l+r+1)/2;
		if (bfs_bear(mid))
			l = mid;
		else
			r = mid-1;
	}
	cout << l << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8008 KB Output is correct
2 Correct 5 ms 8020 KB Output is correct
3 Correct 4 ms 8020 KB Output is correct
4 Correct 4 ms 8020 KB Output is correct
5 Correct 4 ms 8020 KB Output is correct
6 Correct 4 ms 8020 KB Output is correct
7 Correct 142 ms 9380 KB Output is correct
8 Correct 4 ms 8020 KB Output is correct
9 Correct 4 ms 8020 KB Output is correct
10 Correct 4 ms 8008 KB Output is correct
11 Correct 4 ms 8008 KB Output is correct
12 Correct 4 ms 8020 KB Output is correct
13 Correct 3 ms 8020 KB Output is correct
14 Correct 5 ms 8016 KB Output is correct
15 Correct 5 ms 8020 KB Output is correct
16 Correct 5 ms 8020 KB Output is correct
17 Correct 5 ms 8020 KB Output is correct
18 Correct 5 ms 8020 KB Output is correct
19 Correct 6 ms 8020 KB Output is correct
20 Correct 5 ms 8020 KB Output is correct
21 Correct 5 ms 8020 KB Output is correct
22 Correct 5 ms 8004 KB Output is correct
23 Correct 5 ms 8020 KB Output is correct
24 Correct 5 ms 8072 KB Output is correct
25 Correct 6 ms 8008 KB Output is correct
26 Correct 5 ms 8020 KB Output is correct
27 Correct 5 ms 8020 KB Output is correct
28 Correct 8 ms 8036 KB Output is correct
29 Correct 6 ms 8052 KB Output is correct
30 Correct 5 ms 8088 KB Output is correct
31 Correct 5 ms 8020 KB Output is correct
32 Correct 5 ms 8020 KB Output is correct
33 Correct 9 ms 8404 KB Output is correct
34 Correct 9 ms 8404 KB Output is correct
35 Correct 31 ms 8404 KB Output is correct
36 Correct 12 ms 8508 KB Output is correct
37 Correct 10 ms 8500 KB Output is correct
38 Correct 30 ms 8404 KB Output is correct
39 Correct 10 ms 8588 KB Output is correct
40 Correct 10 ms 8532 KB Output is correct
41 Correct 42 ms 8540 KB Output is correct
42 Correct 11 ms 8672 KB Output is correct
43 Correct 11 ms 8572 KB Output is correct
44 Correct 45 ms 8660 KB Output is correct
45 Correct 12 ms 8660 KB Output is correct
46 Correct 15 ms 8748 KB Output is correct
47 Correct 53 ms 8660 KB Output is correct
48 Correct 12 ms 8788 KB Output is correct
49 Correct 15 ms 8796 KB Output is correct
50 Correct 78 ms 8852 KB Output is correct
51 Correct 19 ms 8916 KB Output is correct
52 Correct 15 ms 8920 KB Output is correct
53 Correct 88 ms 8916 KB Output is correct
54 Correct 15 ms 9036 KB Output is correct
55 Correct 17 ms 9048 KB Output is correct
56 Correct 89 ms 9056 KB Output is correct
57 Correct 18 ms 9172 KB Output is correct
58 Correct 20 ms 9044 KB Output is correct
59 Correct 126 ms 9040 KB Output is correct
60 Correct 20 ms 9172 KB Output is correct
61 Correct 20 ms 9172 KB Output is correct
62 Correct 137 ms 9280 KB Output is correct
63 Correct 100 ms 9276 KB Output is correct
64 Correct 170 ms 9276 KB Output is correct
65 Correct 149 ms 9164 KB Output is correct
66 Correct 112 ms 9280 KB Output is correct
67 Correct 100 ms 9280 KB Output is correct
68 Correct 40 ms 9308 KB Output is correct
69 Correct 35 ms 9184 KB Output is correct
70 Correct 32 ms 9304 KB Output is correct
71 Correct 29 ms 9292 KB Output is correct
72 Correct 32 ms 9292 KB Output is correct
73 Correct 40 ms 9556 KB Output is correct
74 Correct 61 ms 9428 KB Output is correct
75 Correct 58 ms 9552 KB Output is correct
76 Correct 64 ms 9556 KB Output is correct
77 Correct 62 ms 9548 KB Output is correct
78 Correct 80 ms 9400 KB Output is correct
79 Correct 64 ms 9528 KB Output is correct
80 Correct 61 ms 9428 KB Output is correct
81 Correct 61 ms 9528 KB Output is correct
82 Correct 58 ms 9528 KB Output is correct
83 Correct 108 ms 9488 KB Output is correct
84 Correct 94 ms 9564 KB Output is correct
85 Correct 72 ms 9548 KB Output is correct
86 Correct 79 ms 9484 KB Output is correct
87 Correct 69 ms 9488 KB Output is correct
88 Correct 103 ms 9428 KB Output is correct
89 Correct 108 ms 9432 KB Output is correct
90 Correct 93 ms 9424 KB Output is correct
91 Correct 89 ms 9436 KB Output is correct
92 Correct 84 ms 9436 KB Output is correct