답안 #1047504

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1047504 2024-08-07T13:44:30 Z Nonoze Mecho (IOI09_mecho) C++17
100 / 100
872 ms 47756 KB
/*
*	Author: Nonoze
*	Created: Wednesday 07/08/2024
*/
#include <bits/stdc++.h>
using namespace std;

#ifndef _IN_LOCAL
	#define dbg(...)
#endif

#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)

template<typename T> void read(T& x, bool write=0, bool write2=1, bool nothing=1) { if (write && write2) cout << x << endl; else if (write) cout << x << ' '; else cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p, bool write=0, bool nothing=1, bool write2=1) { read(p.first, write, 0), read(p.second, write, 0); if (write && write2) cout << endl;}
template<typename T> void read(vector<T>& v, bool write=0) { for (auto& x : v) read(x, write, 0, 1); if (write) cout << endl; }
template<typename T1, typename T2> void read(T1& x, T2& y, bool write=0) { read(x, write), read(y, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z, bool write=0) { read(x, write), read(y, write), read(z, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz, bool write=0) { read(x, write), read(y, write), read(z, write), read(zz, write); if (write) cout << endl; }
template<typename T> void print(T x) { read(x, 1); }
template<typename T1, typename T2> void print(T1 x, T2 y) { read(x, y, 1); }
template<typename T1, typename T2, typename T3> void print(T1 x, T2 y, T3 z) { read(x, y, z, 1); }
template<typename T1, typename T2, typename T3, typename T4> void print(T1 x, T2 y, T3 z, T4 zz) { read(x, y, z, zz, 1); }

#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)

#define int long long
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;



void solve();

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int tt=1;
	// cin >> tt;
	while(tt--) solve();
	return 0;
}




int n, k, s, t;
vector<int> h;
vector<vector<int>> adj;

bool ok(int wait) {
	vector<bool> bees(n, 0), visited(n, 0); visited[s]=1;
	for (auto u: h) bees[u]=1;
	deque<pair<int, pair<int, int>>> qbees;
	deque<pair<int, pair<int, int>>> q2; q2.push_back({s, {0, 0}});
	for (auto u: h) qbees.push_back({u, {0, 0}});
	while (!q2.empty()) {
		bool bee=1;
		if (qbees.empty() || qbees.front().se.se>=q2.front().se.se) bee=0, swap(qbees, q2);
		int u=qbees.front().fi, nb=qbees.front().se.fi, time=qbees.front().se.se; qbees.pop_front();
		if (!bee) swap(qbees, q2);
		if (!bee && time<wait) {
			if (!bees[u]) q2.push_back({u, {0, time+1}});
			continue;
		}
		if (bees[u] && !bee) continue;
		for (auto v: adj[u]) {
			if (bees[v] || (!bee && visited[v])) continue;
			if (bee && t==v) continue;
			if (bee) bees[v]=1;
			else visited[v]=1;
			if (!bee && v==t) return 1;
			if (!bee) {
				if (nb+1>=k) q2.push_back({v, {0, time+1}});
				else q2.push_back({v, {nb+1, time}});
			} else qbees.push_back({v, {0, time+1}});
		}
	}
	return 0;
}


void solve() {
	read(n, k);
	vector<string> a(n); read(a);
	adj.clear(), adj.resize(n*n);
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) {
			if (a[i][j]=='T') continue;
			int act=i*n+j;
			if (i && a[i-1][j]!='T') {
				int nxt=(i-1)*n+j;
				adj[act].pb(nxt), adj[nxt].pb(act);
			}
			if (j && a[i][j-1]!='T') {
				int nxt=i*n+j-1;
				adj[act].pb(nxt), adj[nxt].pb(act);
			}
			if (a[i][j]=='M') s=act;
			if (a[i][j]=='D') t=act;
			if (a[i][j]=='H') h.pb(act);
		}
	}
	n*=n;
	int ans=-1, l=0, r=n;
	while (l<=r) {
		int mid=(l+r)/2;
		if (ok(mid)) ans=mid, l=mid+1;
		else r=mid-1;
	}
	cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 687 ms 47276 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 33 ms 5468 KB Output is correct
34 Correct 23 ms 5468 KB Output is correct
35 Correct 32 ms 7512 KB Output is correct
36 Correct 44 ms 6996 KB Output is correct
37 Correct 31 ms 7000 KB Output is correct
38 Correct 35 ms 9560 KB Output is correct
39 Correct 54 ms 8796 KB Output is correct
40 Correct 38 ms 8796 KB Output is correct
41 Correct 48 ms 11864 KB Output is correct
42 Correct 71 ms 10840 KB Output is correct
43 Correct 46 ms 10844 KB Output is correct
44 Correct 61 ms 14684 KB Output is correct
45 Correct 100 ms 12892 KB Output is correct
46 Correct 55 ms 12888 KB Output is correct
47 Correct 78 ms 17496 KB Output is correct
48 Correct 98 ms 15196 KB Output is correct
49 Correct 69 ms 15196 KB Output is correct
50 Correct 104 ms 20800 KB Output is correct
51 Correct 123 ms 17756 KB Output is correct
52 Correct 84 ms 17752 KB Output is correct
53 Correct 180 ms 24408 KB Output is correct
54 Correct 156 ms 20572 KB Output is correct
55 Correct 95 ms 20568 KB Output is correct
56 Correct 160 ms 28252 KB Output is correct
57 Correct 170 ms 23780 KB Output is correct
58 Correct 116 ms 23644 KB Output is correct
59 Correct 199 ms 32272 KB Output is correct
60 Correct 212 ms 26932 KB Output is correct
61 Correct 130 ms 26972 KB Output is correct
62 Correct 211 ms 36936 KB Output is correct
63 Correct 372 ms 30504 KB Output is correct
64 Correct 460 ms 30480 KB Output is correct
65 Correct 435 ms 30292 KB Output is correct
66 Correct 397 ms 30504 KB Output is correct
67 Correct 406 ms 30500 KB Output is correct
68 Correct 394 ms 30480 KB Output is correct
69 Correct 350 ms 30544 KB Output is correct
70 Correct 341 ms 30556 KB Output is correct
71 Correct 388 ms 30508 KB Output is correct
72 Correct 463 ms 30552 KB Output is correct
73 Correct 100 ms 47540 KB Output is correct
74 Correct 658 ms 47572 KB Output is correct
75 Correct 700 ms 47632 KB Output is correct
76 Correct 694 ms 47756 KB Output is correct
77 Correct 663 ms 47444 KB Output is correct
78 Correct 775 ms 47592 KB Output is correct
79 Correct 754 ms 47460 KB Output is correct
80 Correct 795 ms 47496 KB Output is correct
81 Correct 706 ms 47436 KB Output is correct
82 Correct 779 ms 47508 KB Output is correct
83 Correct 773 ms 47292 KB Output is correct
84 Correct 743 ms 47268 KB Output is correct
85 Correct 802 ms 47208 KB Output is correct
86 Correct 778 ms 47280 KB Output is correct
87 Correct 762 ms 47284 KB Output is correct
88 Correct 838 ms 47224 KB Output is correct
89 Correct 617 ms 47288 KB Output is correct
90 Correct 757 ms 47072 KB Output is correct
91 Correct 727 ms 47216 KB Output is correct
92 Correct 872 ms 47188 KB Output is correct