Submission #1103072

# Submission time Handle Problem Language Result Execution time Memory
1103072 2024-10-19T22:42:37 Z ef10 Mecho (IOI09_mecho) C++17
100 / 100
163 ms 8016 KB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;

int N,S;
string M[1001];
struct node {
	int hd;
	int md;
};
bool v[1001][1001];
node all[1001][1001];
pair<int,int> m;

bool work(int e) {
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			all[i][j].md = -1;
		}
	}

	queue<pair<int,int> > q;
	q.push(m);
	all[m.first][m.second].md = 0;
	if (all[m.first][m.second].hd != -1 && all[m.first][m.second].hd <= e) return false;
	while (!q.empty()) {
		auto c = q.front();
						//cout << "handling " << c.first << "/" << c.second << "/" << all[c.first][c.second].md << endl;
		int cost = all[c.first][c.second].md+1;
		q.pop();
			for (auto i : {-1,1}) {
				for (int j = 0; j <= 1; j++) {
					int ti = j ? c.first : c.first+i;
					int tj = j ? c.second+i : c.second;
					if (ti < 0 || ti >= N || tj < 0 || tj >= N) continue;
					//cout << "          " << ti << "/" << tj << " " << endl;
					if (M[ti][tj] == 'T') continue;
					if (all[ti][tj].md >= 0 && all[ti][tj].md <= cost) continue;
					if (all[ti][tj].hd >= 0 && all[ti][tj].hd <= cost/S+e) continue;
					if (M[ti][tj] == 'D') return true;
					//cout << "          " << ti << "/" << tj << " passed" << endl;
					all[ti][tj].md = cost;
						q.push(make_pair(ti,tj));
						//cout << "       add q " << ti << "/" << tj << "/" << cost << endl;
				}
			}
		/*for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				cout << all[i][j].md << " ";
			}
			cout << endl;
		}*/
	}
	return false;
}

int solve() {
	int lo = 0, hi = N*N;
		// if none of the values in the range work, return lo - 1
	lo--;
	while (lo < hi) {
		// find the middle of the current range (rounding up)
		int mid = lo + (hi - lo + 1) / 2;
		//cout << "mid: " << mid << ", " << lo << "/" << hi << endl;
		if (work(mid)) {
			//cout << " worked" << endl;
			// if mid works, then all numbers smaller than mid also work
			lo = mid;
		} else {
			//cout << " not work" << endl;
			// if mid does not work, greater values would not work either
			hi = mid - 1;
		}
	}
	return lo;
}

int main() {
	cin >> N >> S;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			all[i][j].hd = -1;
		}
	}
	queue<pair<int,int> > q;
	for (int i = 0; i < N; i++) {
		cin >> M[i];
		for (int j = 0; j < N; j++) {
			if (M[i][j] == 'H') {
				all[i][j].hd = 0;
				q.push(make_pair(i,j));
			} else if (M[i][j] == 'M') {
				m = make_pair(i,j);
			}
		}
	}
	while (!q.empty()) {
		auto c = q.front();
		int cost = all[c.first][c.second].hd+1;
		q.pop();
		for (auto i : {-1,1}) {
			for (int j = 0; j <= 1; j++) {
				int ti = j ? c.first : c.first+i;
				int tj = j ? c.second+i : c.second;
				if (ti < 0 || ti >= N || tj < 0 || tj >= N) continue;
				if (M[ti][tj]=='T' || M[ti][tj]=='D' || (all[ti][tj].hd!=-1 && all[ti][tj].hd<=cost)) continue;
				all[ti][tj].hd = cost;
				q.push(make_pair(ti,tj));
				//cout << "add " << ti << "/" << tj << endl;
			}
		}
	}
	/*for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cout << all[i][j].hd << " ";
		}
		cout << endl;
	}*/
	int r = solve();
	cout << r << endl;
	//cout << work(3) << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 75 ms 7672 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 592 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
13 Correct 1 ms 2640 KB Output is correct
14 Correct 1 ms 2384 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 1 ms 2556 KB Output is correct
19 Correct 1 ms 2384 KB Output is correct
20 Correct 1 ms 2384 KB Output is correct
21 Correct 1 ms 2384 KB Output is correct
22 Correct 1 ms 2384 KB Output is correct
23 Correct 1 ms 2384 KB Output is correct
24 Correct 1 ms 2384 KB Output is correct
25 Correct 1 ms 2384 KB Output is correct
26 Correct 1 ms 2384 KB Output is correct
27 Correct 1 ms 2384 KB Output is correct
28 Correct 1 ms 2640 KB Output is correct
29 Correct 1 ms 2640 KB Output is correct
30 Correct 1 ms 2640 KB Output is correct
31 Correct 1 ms 2640 KB Output is correct
32 Correct 1 ms 2640 KB Output is correct
33 Correct 6 ms 4892 KB Output is correct
34 Correct 6 ms 4688 KB Output is correct
35 Correct 16 ms 4904 KB Output is correct
36 Correct 8 ms 4856 KB Output is correct
37 Correct 7 ms 4688 KB Output is correct
38 Correct 29 ms 5100 KB Output is correct
39 Correct 8 ms 4856 KB Output is correct
40 Correct 8 ms 4944 KB Output is correct
41 Correct 25 ms 4832 KB Output is correct
42 Correct 11 ms 5400 KB Output is correct
43 Correct 11 ms 5200 KB Output is correct
44 Correct 33 ms 5200 KB Output is correct
45 Correct 14 ms 7248 KB Output is correct
46 Correct 12 ms 7248 KB Output is correct
47 Correct 37 ms 7248 KB Output is correct
48 Correct 17 ms 7248 KB Output is correct
49 Correct 16 ms 7248 KB Output is correct
50 Correct 48 ms 7372 KB Output is correct
51 Correct 17 ms 7248 KB Output is correct
52 Correct 17 ms 7248 KB Output is correct
53 Correct 57 ms 7424 KB Output is correct
54 Correct 20 ms 7248 KB Output is correct
55 Correct 19 ms 7248 KB Output is correct
56 Correct 71 ms 7248 KB Output is correct
57 Correct 26 ms 7504 KB Output is correct
58 Correct 21 ms 7504 KB Output is correct
59 Correct 72 ms 7504 KB Output is correct
60 Correct 26 ms 7504 KB Output is correct
61 Correct 23 ms 7504 KB Output is correct
62 Correct 86 ms 7572 KB Output is correct
63 Correct 85 ms 7504 KB Output is correct
64 Correct 163 ms 7504 KB Output is correct
65 Correct 119 ms 7504 KB Output is correct
66 Correct 95 ms 7504 KB Output is correct
67 Correct 91 ms 7504 KB Output is correct
68 Correct 49 ms 7516 KB Output is correct
69 Correct 48 ms 7504 KB Output is correct
70 Correct 42 ms 7504 KB Output is correct
71 Correct 42 ms 7504 KB Output is correct
72 Correct 33 ms 7504 KB Output is correct
73 Correct 34 ms 7844 KB Output is correct
74 Correct 47 ms 7760 KB Output is correct
75 Correct 55 ms 7760 KB Output is correct
76 Correct 50 ms 7760 KB Output is correct
77 Correct 52 ms 7828 KB Output is correct
78 Correct 69 ms 7760 KB Output is correct
79 Correct 43 ms 7760 KB Output is correct
80 Correct 48 ms 7760 KB Output is correct
81 Correct 54 ms 7760 KB Output is correct
82 Correct 49 ms 7772 KB Output is correct
83 Correct 61 ms 8016 KB Output is correct
84 Correct 55 ms 7760 KB Output is correct
85 Correct 69 ms 7776 KB Output is correct
86 Correct 59 ms 7760 KB Output is correct
87 Correct 58 ms 7760 KB Output is correct
88 Correct 60 ms 7504 KB Output is correct
89 Correct 61 ms 7676 KB Output is correct
90 Correct 62 ms 7504 KB Output is correct
91 Correct 61 ms 7504 KB Output is correct
92 Correct 74 ms 7504 KB Output is correct