Submission #212402

# Submission time Handle Problem Language Result Execution time Memory
212402 2020-03-22T21:41:02 Z mode149256 Mecho (IOI09_mecho) C++14
84 / 100
249 ms 6408 KB
/*input
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT
*/
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;

#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"

const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;

template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }

template<typename T>
void print(vector<T> vec, string name = "") {
	cout << name;
	for (auto u : vec)
		cout << u << ' ';
	cout << '\n';
}

using vc = vector<char>;
int N, S;
vector<vc> sk;

vii hiveDis;

int dx[] = {0, 1, 0, -1};
int dy[] = { -1, 0, 1, 0};
pi dxy[] = {
	{dx[0], dy[0]},
	{dx[1], dy[1]},
	{dx[2], dy[2]},
	{dx[3], dy[3]}
};
pi mecho;
pi home;
vpi hives;

bool valid(pi a) {
	return a.x >= 0 and a.y >= 0 and a.x < N and a.y < N;
}

bool gali(int time) {
	vii dis(N, vi(N, MOD));

	dis[mecho.x][mecho.y] = 0;
	queue<pi> q;
	q.push(mecho);

	while (q.size()) {
		pi c = q.front(); q.pop();

		for (int i = 0; i < 4; ++i)
		{
			pi n = c + dxy[i];
			if (valid(n) and sk[n.x][n.y] != 'T') {
				if (sk[n.x][n.y] == 'D')
					return true;

				int newDis = dis[c.x][c.y] + 1;

				if (time + newDis / S < hiveDis[n.x][n.y] and newDis < dis[n.x][n.y]) {
					dis[n.x][n.y] = newDis;
					q.push(n);
				}
			}
		}
	}

	return false;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N >> S;
	sk = vector<vc>(N, vc(N));
	hiveDis = vii(N, vi(N, MOD));


	for (int i = 0; i < N; ++i)
	{
		for (int j = 0; j < N; ++j)
		{
			cin >> sk[i][j];
			if (sk[i][j] == 'M') mecho = {i, j};
			else if (sk[i][j] == 'D') home = {i, j};
			else if (sk[i][j] == 'H') hives.emplace_back(i, j);
		}
	}

	queue<pi> q;
	for (auto u : hives) {
		hiveDis[u.x][u.y] = 0;
		q.push(u);
	}

	while (q.size()) {
		pi c = q.front(); q.pop();

		for (int i = 0; i < 4; ++i)
		{
			pi nc = c + dxy[i];
			if (valid(nc) and sk[nc.x][nc.y] != 'T' and nc != home) {
				if (hiveDis[c.x][c.y] + 1 < hiveDis[nc.x][nc.y]) {
					hiveDis[nc.x][nc.y] = hiveDis[c.x][c.y] + 1;
					q.push(nc);
				}
			}
		}
	}

	if (!gali(0)) {
		return !printf("-1\n");
	}
	int l = 0;
	int h = MOD;
	int m;

	while (l < h) {
		m = (l + h + 1) / 2;
		if (gali(m))
			l = m;
		else
			h = m - 1;
	}
	printf("%d\n", l);
}

/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 154 ms 6184 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Incorrect 5 ms 384 KB Output isn't correct
13 Incorrect 5 ms 384 KB Output isn't correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 4 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 5 ms 384 KB Output is correct
30 Correct 5 ms 384 KB Output is correct
31 Correct 5 ms 384 KB Output is correct
32 Correct 5 ms 384 KB Output is correct
33 Correct 13 ms 1504 KB Output is correct
34 Correct 14 ms 1504 KB Output is correct
35 Correct 34 ms 1508 KB Output is correct
36 Correct 16 ms 1920 KB Output is correct
37 Correct 15 ms 1792 KB Output is correct
38 Correct 41 ms 1792 KB Output is correct
39 Correct 18 ms 2216 KB Output is correct
40 Correct 19 ms 2296 KB Output is correct
41 Correct 52 ms 2332 KB Output is correct
42 Correct 22 ms 2768 KB Output is correct
43 Correct 22 ms 2648 KB Output is correct
44 Correct 73 ms 2744 KB Output is correct
45 Correct 26 ms 3224 KB Output is correct
46 Correct 26 ms 3104 KB Output is correct
47 Correct 84 ms 3184 KB Output is correct
48 Correct 31 ms 3752 KB Output is correct
49 Correct 30 ms 3728 KB Output is correct
50 Correct 98 ms 3712 KB Output is correct
51 Correct 34 ms 4272 KB Output is correct
52 Correct 35 ms 4272 KB Output is correct
53 Correct 112 ms 4272 KB Output is correct
54 Correct 39 ms 4872 KB Output is correct
55 Correct 39 ms 4916 KB Output is correct
56 Correct 145 ms 4872 KB Output is correct
57 Correct 47 ms 5544 KB Output is correct
58 Correct 45 ms 5508 KB Output is correct
59 Correct 156 ms 5512 KB Output is correct
60 Correct 52 ms 6224 KB Output is correct
61 Correct 49 ms 6208 KB Output is correct
62 Correct 186 ms 6244 KB Output is correct
63 Correct 148 ms 6232 KB Output is correct
64 Correct 246 ms 6244 KB Output is correct
65 Correct 249 ms 6288 KB Output is correct
66 Correct 178 ms 6232 KB Output is correct
67 Correct 45 ms 6144 KB Output is correct
68 Correct 85 ms 6212 KB Output is correct
69 Correct 94 ms 6296 KB Output is correct
70 Correct 83 ms 6260 KB Output is correct
71 Correct 70 ms 6308 KB Output is correct
72 Incorrect 65 ms 6216 KB Output isn't correct
73 Incorrect 84 ms 6248 KB Output isn't correct
74 Correct 119 ms 6304 KB Output is correct
75 Correct 118 ms 6284 KB Output is correct
76 Correct 114 ms 6216 KB Output is correct
77 Correct 114 ms 6232 KB Output is correct
78 Correct 43 ms 6100 KB Output is correct
79 Correct 115 ms 6348 KB Output is correct
80 Correct 116 ms 6200 KB Output is correct
81 Correct 119 ms 6264 KB Output is correct
82 Correct 110 ms 6224 KB Output is correct
83 Correct 131 ms 6216 KB Output is correct
84 Correct 156 ms 6408 KB Output is correct
85 Correct 125 ms 6208 KB Output is correct
86 Correct 137 ms 6224 KB Output is correct
87 Correct 130 ms 6208 KB Output is correct
88 Correct 153 ms 6280 KB Output is correct
89 Correct 154 ms 6236 KB Output is correct
90 Correct 153 ms 6200 KB Output is correct
91 Correct 147 ms 6240 KB Output is correct
92 Correct 151 ms 6380 KB Output is correct