Submission #884662

# Submission time Handle Problem Language Result Execution time Memory
884662 2023-12-08T01:25:42 Z xjonwang Mecho (IOI09_mecho) C++17
100 / 100
170 ms 6388 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define ar array

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define pll pair<ll, ll>
#define pii pair<int, int>
#define f first
#define s second

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)

template<class T> bool umin(T& a, const T& b) {
	return b<a?a=b, 1:0;
}
template<class T> bool umax(T& a, const T& b) { 
	return a<b?a=b, 1:0;
} 

ll FIRSTTRUE(function<bool(ll)> f, ll lb, ll rb) {
	while(lb<rb) {
		ll mb=(lb+rb)/2;
		f(mb)?rb=mb:lb=mb+1; 
	} 
	return lb;
}
ll LASTTRUE(function<bool(ll)> f, ll lb, ll rb) {
	while(lb<rb) {
		ll mb=(lb+rb+1)/2;
		f(mb)?lb=mb:rb=mb-1; 
	} 
	return lb;
}

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class A, class B> void read(pair<A, B>& x);
template<class T> void read(T& x) {
	cin >> x;
}
void read(double& d) {
	string t;
	read(t);
	d=stod(t);
}
void read(long double& d) {
	string t;
	read(t);
	d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
	read(h);
	read(t...);
}
template<class A> void read(vt<A>& x) {
	EACH(a, x)
		read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
	EACH(a, x)
		read(a);
}
template<class A, class B> void read(pair<A, B>& x) {
	cin >> x.first >> x.second;
}


string to_string(char c) {
	return string(1, c);
}
string to_string(bool b) {
	return b?"true":"false";
}
string to_string(const char* s) {
	return string(s);
}
string to_string(string s) {
	return s;
}
string to_string(vt<bool> v) {
	string res;
	FOR(sz(v))
		res+=char('0'+v[i]);
	return res;
}

template<size_t S> string to_string(bitset<S> b) {
	string res;
	FOR(S)
		res+=char('0'+b[i]);
	return res;
}
template<class T> string to_string(T v) {
    bool f=1;
    string res;
    EACH(x, v) {
		if(!f)
			res+=' ';
		f=0;
		res+=to_string(x);
	}
    return res;
}
template<class A, class B> string to_string(pair<A, B>& x) {
	return to_string(x.first) + ' ' + to_string(x.second);
}

template<class A> void write(A x) {
	cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) { 
	write(h);
	write(t...);
}
void print() {
	write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) { 
	write(h);
	if(sizeof...(t))
		write(' ');
	print(t...);
}

int dr[4]={1, -1, 0, 0}, dc[4]={0, 0, 1, -1};

int n, k, m1, m2, d1, d2;
vt<vt<int>> bd;
vt<vt<char>> g;

bool check(int x) {
	if (x>=bd[m1][m2]) return false;
	vt<vt<int>> dist(n, vt<int>(n, -1));
	queue<pii> q;
	dist[m1][m2]=0, q.push({m1, m2});
	while (sz(q)) {
		pii v=q.front(); q.pop();
		FOR(4) {
			int r=v.f+dr[i], c=v.s+dc[i];
			if (r>=0 && r<n && c>=0 && c<n && g[r][c]!='T' && dist[r][c]==-1 && (dist[v.f][v.s]+1)/k+x<bd[r][c]) {
				if (r==d1 && c==d2) return true;
				dist[r][c]=dist[v.f][v.s]+1;
				q.push({r, c});
			}
		}
	}
	return false;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	read(n, k);
	g.assign(n, vt<char>(n)), bd.assign(n, vt<int>(n, -1));
	vt<pii> h;
	char x;
	FOR(n) {
		FOR(j, n) {
			read(x);
			g[i][j]=x;
			if (x=='M') m1=i, m2=j;
			else if (x=='D') d1=i, d2=j;
			else if (x=='H') h.pb({i, j});
		}
	}
	queue<pii> q;
	EACH(p, h) bd[p.f][p.s]=0, q.push({p.f, p.s});
	while (sz(q)) {
		pii v=q.front(); q.pop();
		FOR(4) {
			int r=v.f+dr[i], c=v.s+dc[i];
			if (r>=0 && r<n && c>=0 && c<n && g[r][c]!='T' && g[r][c]!='D' && bd[r][c]==-1) {
				bd[r][c]=bd[v.f][v.s]+1;
				q.push({r, c});
			}
		}
	}
	bd[d1][d2]=INT_MAX;
	int l=-1, r=1e6;
	for (int i=r-l; i>0; i/=2) while (l+i<=r && check(l+i)) l+=i;
	print(l);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 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 93 ms 6224 KB Output is correct
8 Correct 0 ms 348 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 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 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 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 4 ms 1560 KB Output is correct
34 Correct 5 ms 1568 KB Output is correct
35 Correct 19 ms 1560 KB Output is correct
36 Correct 5 ms 1908 KB Output is correct
37 Correct 5 ms 1884 KB Output is correct
38 Correct 25 ms 1912 KB Output is correct
39 Correct 7 ms 2248 KB Output is correct
40 Correct 6 ms 2248 KB Output is correct
41 Correct 32 ms 2236 KB Output is correct
42 Correct 9 ms 2740 KB Output is correct
43 Correct 9 ms 2700 KB Output is correct
44 Correct 42 ms 2864 KB Output is correct
45 Correct 10 ms 3176 KB Output is correct
46 Correct 10 ms 3192 KB Output is correct
47 Correct 52 ms 3180 KB Output is correct
48 Correct 12 ms 3716 KB Output is correct
49 Correct 11 ms 3700 KB Output is correct
50 Correct 62 ms 3700 KB Output is correct
51 Correct 14 ms 4188 KB Output is correct
52 Correct 13 ms 4276 KB Output is correct
53 Correct 75 ms 4468 KB Output is correct
54 Correct 17 ms 4900 KB Output is correct
55 Correct 16 ms 4860 KB Output is correct
56 Correct 86 ms 4868 KB Output is correct
57 Correct 17 ms 5500 KB Output is correct
58 Correct 18 ms 5508 KB Output is correct
59 Correct 97 ms 5492 KB Output is correct
60 Correct 20 ms 6200 KB Output is correct
61 Correct 18 ms 6192 KB Output is correct
62 Correct 126 ms 6172 KB Output is correct
63 Correct 91 ms 6152 KB Output is correct
64 Correct 168 ms 6180 KB Output is correct
65 Correct 162 ms 6388 KB Output is correct
66 Correct 170 ms 6192 KB Output is correct
67 Correct 92 ms 6192 KB Output is correct
68 Correct 47 ms 6216 KB Output is correct
69 Correct 50 ms 6196 KB Output is correct
70 Correct 35 ms 6192 KB Output is correct
71 Correct 35 ms 6204 KB Output is correct
72 Correct 30 ms 6184 KB Output is correct
73 Correct 32 ms 6224 KB Output is correct
74 Correct 62 ms 6240 KB Output is correct
75 Correct 66 ms 6232 KB Output is correct
76 Correct 60 ms 6236 KB Output is correct
77 Correct 68 ms 6232 KB Output is correct
78 Correct 60 ms 6240 KB Output is correct
79 Correct 64 ms 6220 KB Output is correct
80 Correct 69 ms 6248 KB Output is correct
81 Correct 65 ms 6212 KB Output is correct
82 Correct 48 ms 6252 KB Output is correct
83 Correct 70 ms 6236 KB Output is correct
84 Correct 69 ms 6232 KB Output is correct
85 Correct 73 ms 6236 KB Output is correct
86 Correct 84 ms 6224 KB Output is correct
87 Correct 83 ms 6200 KB Output is correct
88 Correct 71 ms 6124 KB Output is correct
89 Correct 83 ms 6296 KB Output is correct
90 Correct 90 ms 6208 KB Output is correct
91 Correct 86 ms 6236 KB Output is correct
92 Correct 90 ms 6192 KB Output is correct