Submission #492199

# Submission time Handle Problem Language Result Execution time Memory
492199 2021-12-05T23:28:38 Z Evang Mecho (IOI09_mecho) C++17
100 / 100
442 ms 6668 KB
#include <bits/stdc++.h>
using namespace std;

#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#define dout(x)
#endif

#define mp(a, b) make_pair(a, b)
#define mt(args...) make_tuple(args)
#define pb(x) push_back(x)
#define eb(args...) emplace_back(args)
#define ff first
#define ss second
#define DIE exit(0)

template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp>
using pq = std::priority_queue<T, vc<T>, Comp>;
template<typename T>
using maxpq = pq<T, less<T>>;
template<typename T>
using minpq = pq<T, greater<T>>;

using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vc<int>;
using vll = vc<ll>;
using vpi = vc<pi>;
using vpll = vc<pll>;
using str = string;

constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int inf = 0x3f3f3f3f;
constexpr ll llinf = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------


const int N = 805;
int n, s, b[N][N], m[N][N];
int dr[] = {0, 0, -1, 1};
int dc[] = {-1, 1, 0, 0};
char a[N][N];
pi st;
vpi h;

bool good(int r, int c){
	return 0 <= r && r < n && 0 <= c && c < n;
}

bool ok(int t){
	dout(t);
	memset(b, -1, sizeof b);
	memset(m, -1, sizeof m);
	queue<pi> q;
	for(auto x: h){
		q.push(x);
		b[x.ff][x.ss] = 0;
	}

	while(!q.empty()){
		auto[r, c] = q.front();

		if(b[r][c]==t)
			break;
		q.pop();

		for(int i = 0; i < 4; ++i){
			int r2 = r + dr[i];
			int c2 = c + dc[i];
			if(good(r2, c2)&&(a[r2][c2]=='M'||a[r2][c2]=='G')&&b[r2][c2]==-1){
				b[r2][c2] = b[r][c] + 1;
				q.emplace(r2, c2);
			}
		}
	}

	#ifdef _DEBUG
	clog << "Line " << __LINE__ << ":\n";
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j)
			clog << b[i][j] << sp;
		clog << el;
	}
	clog << el;
	#endif

	queue<pi> q2;  // mecho
	q2.push(st);
	m[st.ff][st.ss] = 0;
	int t2 = t;
	while(!q2.empty()){
		t2++;
		while(!q2.empty()){
			auto[r, c] = q2.front();
			if(m[r][c]==(t2-t)*s)
				break;
			q2.pop();
			if(~b[r][c])
				continue;
			for(int i = 0; i < 4; ++i){
				int r2 = r + dr[i];
				int c2 = c + dc[i];
				if(good(r2, c2)&&b[r2][c2]==-1&&m[r2][c2]==-1&&
						(a[r2][c2]=='G'||a[r2][c2]=='D')){
					if(a[r2][c2]=='D')
						return 1;
					m[r2][c2] = m[r][c] + 1;
					q2.emplace(r2, c2);
				}
			}
		}
		#ifdef _DEBUG
		clog << "Line " << __LINE__ << ":\n";
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < n; ++j)
				clog << m[i][j] << sp;
			clog << el;
		}
		clog << el;
		#endif
		while(!q.empty()){
			auto[r, c] = q.front();

			if(b[r][c]==t2)
				break;
			q.pop();
			for(int i = 0; i < 4; ++i){
				int r2 = r + dr[i];
				int c2 = c + dc[i];

				if(good(r2, c2)&&b[r2][c2]==-1&&(a[r2][c2]=='G'||a[r2][c2]=='M')){
					b[r2][c2] = b[r][c] + 1;
					q.emplace(r2, c2);
				}
			}
		}
	}
	return 0;
}

signed main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cout << fixed; clog << fixed;
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("debug.txt", "w", stderr);
#endif

	cin >> n >> s;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j){
			cin >> a[i][j];
			if(a[i][j]=='M')
				st = {i, j};
			else if(a[i][j]=='H')
				h.eb(i, j);
		}

	#ifdef _DEBUG
	clog << "Line " << __LINE__ << ":\n";
	for(int i = 0; i < n; ++i){
		for(int j = 0; j < n; ++j)
			clog << a[i][j];
		clog << el;
	}
	clog << el;
	#endif

	#ifdef _DEBUG
	clog << "Line " << __LINE__ << ":\n";
	for(auto[x, y]: h)
		clog << x << sp << y << el;
	#endif
	dout(st.ff);
	dout(st.ss);

	int ans = -1;
	for(int u = int(1e6); u > 0; u /= 2)
		while(ans + u <= int(1e6) && ok(ans + u))
			ans += u;
	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5324 KB Output is correct
2 Correct 5 ms 5324 KB Output is correct
3 Correct 5 ms 5324 KB Output is correct
4 Correct 5 ms 5324 KB Output is correct
5 Correct 5 ms 5376 KB Output is correct
6 Correct 7 ms 5324 KB Output is correct
7 Correct 367 ms 6376 KB Output is correct
8 Correct 5 ms 5308 KB Output is correct
9 Correct 6 ms 5324 KB Output is correct
10 Correct 6 ms 5324 KB Output is correct
11 Correct 5 ms 5324 KB Output is correct
12 Correct 8 ms 5324 KB Output is correct
13 Correct 6 ms 5324 KB Output is correct
14 Correct 7 ms 5324 KB Output is correct
15 Correct 6 ms 5308 KB Output is correct
16 Correct 5 ms 5324 KB Output is correct
17 Correct 6 ms 5324 KB Output is correct
18 Correct 5 ms 5324 KB Output is correct
19 Correct 6 ms 5324 KB Output is correct
20 Correct 6 ms 5324 KB Output is correct
21 Correct 7 ms 5324 KB Output is correct
22 Correct 6 ms 5324 KB Output is correct
23 Correct 6 ms 5324 KB Output is correct
24 Correct 6 ms 5324 KB Output is correct
25 Correct 6 ms 5324 KB Output is correct
26 Correct 8 ms 5324 KB Output is correct
27 Correct 7 ms 5448 KB Output is correct
28 Correct 6 ms 5444 KB Output is correct
29 Correct 8 ms 5324 KB Output is correct
30 Correct 7 ms 5452 KB Output is correct
31 Correct 8 ms 5452 KB Output is correct
32 Correct 7 ms 5452 KB Output is correct
33 Correct 43 ms 5708 KB Output is correct
34 Correct 41 ms 5768 KB Output is correct
35 Correct 49 ms 5700 KB Output is correct
36 Correct 49 ms 5848 KB Output is correct
37 Correct 56 ms 5836 KB Output is correct
38 Correct 61 ms 5852 KB Output is correct
39 Correct 64 ms 5836 KB Output is correct
40 Correct 61 ms 5828 KB Output is correct
41 Correct 75 ms 5924 KB Output is correct
42 Correct 80 ms 6008 KB Output is correct
43 Correct 91 ms 6016 KB Output is correct
44 Correct 95 ms 5964 KB Output is correct
45 Correct 84 ms 6056 KB Output is correct
46 Correct 89 ms 5964 KB Output is correct
47 Correct 112 ms 5964 KB Output is correct
48 Correct 113 ms 6092 KB Output is correct
49 Correct 114 ms 6100 KB Output is correct
50 Correct 134 ms 6108 KB Output is correct
51 Correct 130 ms 6136 KB Output is correct
52 Correct 123 ms 6144 KB Output is correct
53 Correct 162 ms 6164 KB Output is correct
54 Correct 155 ms 6092 KB Output is correct
55 Correct 144 ms 6184 KB Output is correct
56 Correct 179 ms 6180 KB Output is correct
57 Correct 175 ms 6220 KB Output is correct
58 Correct 164 ms 6216 KB Output is correct
59 Correct 211 ms 6220 KB Output is correct
60 Correct 174 ms 6256 KB Output is correct
61 Correct 176 ms 6348 KB Output is correct
62 Correct 234 ms 6216 KB Output is correct
63 Correct 301 ms 6264 KB Output is correct
64 Correct 339 ms 6268 KB Output is correct
65 Correct 334 ms 6264 KB Output is correct
66 Correct 442 ms 6264 KB Output is correct
67 Correct 291 ms 6220 KB Output is correct
68 Correct 323 ms 6300 KB Output is correct
69 Correct 305 ms 6296 KB Output is correct
70 Correct 308 ms 6292 KB Output is correct
71 Correct 306 ms 6340 KB Output is correct
72 Correct 440 ms 6340 KB Output is correct
73 Correct 291 ms 6596 KB Output is correct
74 Correct 374 ms 6512 KB Output is correct
75 Correct 389 ms 6576 KB Output is correct
76 Correct 364 ms 6516 KB Output is correct
77 Correct 398 ms 6668 KB Output is correct
78 Correct 304 ms 6528 KB Output is correct
79 Correct 377 ms 6480 KB Output is correct
80 Correct 395 ms 6532 KB Output is correct
81 Correct 334 ms 6536 KB Output is correct
82 Correct 326 ms 6532 KB Output is correct
83 Correct 324 ms 6496 KB Output is correct
84 Correct 358 ms 6508 KB Output is correct
85 Correct 368 ms 6408 KB Output is correct
86 Correct 371 ms 6616 KB Output is correct
87 Correct 356 ms 6476 KB Output is correct
88 Correct 312 ms 6348 KB Output is correct
89 Correct 351 ms 6432 KB Output is correct
90 Correct 367 ms 6436 KB Output is correct
91 Correct 371 ms 6428 KB Output is correct
92 Correct 356 ms 6348 KB Output is correct