Submission #747806

# Submission time Handle Problem Language Result Execution time Memory
747806 2023-05-24T19:14:47 Z AverageAmogusEnjoyer Mecho (IOI09_mecho) C++17
100 / 100
160 ms 6392 KB
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) x.begin(), x.end() 
#define sz(x) (int) x.size()
#define pb push_back
#define ll long long
#define nl '\n'

/*

Mecho can only walk on grass and cannot go through trees or hives, and he can make at most S steps per minute.
every minute
he eats or 
he leaves immediately and takes up to S steps through the forest as described above

bees spread to grassy cells

trees = no one can go there

m = mecho, grassy cells => i will replace with g

g = grassy

d = mecho home; bees cannot go / pass here

h = hive

*/

const int N = 810;

char grid[N][N];
int stepsMecho[N][N], stepsBee[N][N];
int n, s;
array<int, 2> Mecho, MechoHouse;
vector<array<int, 2>> Bee;
vector<array<int,2>> moves = {{-1,0},{1,0},{0,-1},{0,1}};

struct m{
	bool isMecho;
	int x,y;
};

bool good(int x, int y) {
	return 0 <= x && x < n && 0 <= y && y < n && grid[x][y] != 'T';
}

bool isHome(int x, int y) {
	return x == MechoHouse[0] && y == MechoHouse[1];
}

void debug() {
	cout << "Mecho: " << nl;
	for (int i=0;i<n;i++) {
		for (int j=0;j<n;j++) {
			cout << stepsMecho[i][j] << " ";
		}
		cout << nl;
	}
	cout << "Bees: " << nl;
	for (int i=0;i<n;i++) {
		for (int j=0;j<n;j++) {
			cout << stepsBee[i][j] << " ";
		}
		cout << nl;
	}
}
/*
bool bs(int steps) {
	memset(stepsMecho,-1,sizeof(stepsMecho));
	memset(stepsBee,-1,sizeof(stepsBee));
	queue<m> q, q2;
	q2.push(m{true,Mecho[0],Mecho[1]});
	stepsMecho[Mecho[0]][Mecho[1]] = 0;
	for (auto [x,y]:Bee) {
		q.push(m{false,x,y});
		stepsBee[x][y]=0;
	}
	while(!q.empty()) {
		m v = q.front(); q.pop();
		if (stepsBee[v.x][v.y] == steps) { q2.push(v); continue; }
 		for (auto [addx,addy]: moves) {
			int nx = v.x + addx, ny = v.y + addy;
			if (good(nx,ny) && !isHome(nx,ny) && stepsBee[nx][ny] == -1) {
				q.push(m{false,nx,ny});
				stepsBee[nx][ny] = stepsBee[v.x][v.y]+1;
			}
		}
	}
	for (int i=0;i<n;i++)
		for (int j=0;j<n;j++)
			if (stepsBee[i][j] != -1)
				stepsBee[i][j] = 0;
	while(!q2.empty()) {
		m v = q2.front(); q2.pop();
		// if (steps==1) {
		// 	cout << v.isMecho << " " << v.x << " " << v.y << nl;
		// 	debug();
		// 	cout << nl;
		// }
		if (v.isMecho) {
			if (stepsBee[v.x][v.y] != -1 && stepsBee[v.x][v.y] <= (stepsMecho[v.x][v.y]+s-1)/s) { continue; }
			for (auto [addx,addy]: moves) {
				int nx = v.x + addx, ny = v.y + addy;
				if (good(nx,ny) && stepsMecho[nx][ny] == -1 && (stepsBee[nx][ny] == -1 || stepsBee[nx][ny] > (stepsMecho[v.x][v.y]+s)/s)) {
					if (isHome(nx,ny)) { return 1; }
					stepsMecho[nx][ny] = stepsMecho[v.x][v.y]+1;
					q2.push(m{true,nx,ny});
				}
			}
		} else {
			for (auto [addx,addy]: moves) {
				int nx = v.x + addx, ny = v.y + addy;
				if (good(nx,ny) && !isHome(nx,ny) && stepsBee[nx][ny] == -1) {
					q2.push(m{false,nx,ny});
					stepsBee[nx][ny] = stepsBee[v.x][v.y]+1;
				}
			}
		}
	}
	return 0;
}
*/

void reset() {
	memset(stepsMecho,-1,sizeof(stepsMecho));
}

bool bs(int steps) {
	if (stepsBee[Mecho[0]][Mecho[1]] != -1 && stepsBee[Mecho[0]][Mecho[1]] <= steps) return 0;
	reset();
	queue<array<int,2>> q;
	q.push(Mecho);
	stepsMecho[Mecho[0]][Mecho[1]] = 0;
	while(!q.empty()) {
		auto [x,y] = q.front(); q.pop();
		for (auto [px,py]:moves) {
			int nx = x + px, ny = y + py;
			if (good(nx,ny) && stepsMecho[nx][ny] == -1 && (stepsMecho[x][y] + 1) / s < (stepsBee[nx][ny] - steps)) {
				stepsMecho[nx][ny] = stepsMecho[x][y]+1;
				q.push({nx,ny});
			}
		}
	}
	for (auto [px,py]:moves) {
		int nx = MechoHouse[0] + px, ny = MechoHouse[1] + py;
		if (good(nx,ny) && stepsMecho[nx][ny] != -1) return 1;
	}
	return 0;
}

signed main() {
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	ios_base::sync_with_stdio(0); cin.tie(0);
	memset(stepsBee,-1,sizeof(stepsBee));	
	cin >> n >> s;
	for (int i=0;i<n;i++)
		for (int j=0;j<n;j++) {
			cin >> grid[i][j];
			if (grid[i][j]=='M') {
				grid[i][j] = 'G';
				Mecho = {i,j};
			}
			if (grid[i][j]=='H') {
				Bee.pb({i,j});
			}
			if (grid[i][j]=='D') {
				MechoHouse = {i,j};
			}
		}
	queue<array<int,2>> q;
	for (auto [x,y]:Bee) {
		q.push({x,y});
		stepsBee[x][y] = 0;
	}
	while(!q.empty()) {
		auto [x,y] = q.front(); q.pop();
		for (auto [px,py]:moves) {
			int nx = x + px, ny = y + py;
			if (good(nx,ny) && stepsBee[nx][ny] == -1 && !isHome(nx,ny)) {
				stepsBee[nx][ny] = stepsBee[x][y]+1;
				q.push({nx,ny});
			}
		}
	}
	int lo = 0, hi = 1e9;
	while(lo<=hi) {
		int mid = (lo+hi)/2;
		if (bs(mid)) {
			lo = mid + 1;
		} else {
			hi = mid - 1;
		}
	}
	cout << lo-1;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5332 KB Output is correct
2 Correct 3 ms 5460 KB Output is correct
3 Correct 3 ms 5460 KB Output is correct
4 Correct 2 ms 5460 KB Output is correct
5 Correct 2 ms 5460 KB Output is correct
6 Correct 2 ms 5460 KB Output is correct
7 Correct 73 ms 6100 KB Output is correct
8 Correct 3 ms 5460 KB Output is correct
9 Correct 3 ms 5460 KB Output is correct
10 Correct 3 ms 5460 KB Output is correct
11 Correct 3 ms 5460 KB Output is correct
12 Correct 3 ms 5460 KB Output is correct
13 Correct 3 ms 5460 KB Output is correct
14 Correct 3 ms 5460 KB Output is correct
15 Correct 3 ms 5460 KB Output is correct
16 Correct 2 ms 5460 KB Output is correct
17 Correct 3 ms 5460 KB Output is correct
18 Correct 2 ms 5460 KB Output is correct
19 Correct 4 ms 5460 KB Output is correct
20 Correct 3 ms 5460 KB Output is correct
21 Correct 3 ms 5460 KB Output is correct
22 Correct 2 ms 5460 KB Output is correct
23 Correct 3 ms 5460 KB Output is correct
24 Correct 3 ms 5460 KB Output is correct
25 Correct 3 ms 5460 KB Output is correct
26 Correct 3 ms 5460 KB Output is correct
27 Correct 3 ms 5460 KB Output is correct
28 Correct 3 ms 5460 KB Output is correct
29 Correct 3 ms 5460 KB Output is correct
30 Correct 3 ms 5460 KB Output is correct
31 Correct 3 ms 5460 KB Output is correct
32 Correct 3 ms 5460 KB Output is correct
33 Correct 7 ms 5716 KB Output is correct
34 Correct 6 ms 5716 KB Output is correct
35 Correct 22 ms 5736 KB Output is correct
36 Correct 6 ms 5716 KB Output is correct
37 Correct 7 ms 5776 KB Output is correct
38 Correct 23 ms 5704 KB Output is correct
39 Correct 9 ms 5812 KB Output is correct
40 Correct 8 ms 5696 KB Output is correct
41 Correct 29 ms 5716 KB Output is correct
42 Correct 8 ms 5856 KB Output is correct
43 Correct 8 ms 5844 KB Output is correct
44 Correct 40 ms 5844 KB Output is correct
45 Correct 10 ms 5844 KB Output is correct
46 Correct 9 ms 5844 KB Output is correct
47 Correct 44 ms 5888 KB Output is correct
48 Correct 12 ms 5844 KB Output is correct
49 Correct 13 ms 5940 KB Output is correct
50 Correct 60 ms 5924 KB Output is correct
51 Correct 14 ms 5972 KB Output is correct
52 Correct 14 ms 5972 KB Output is correct
53 Correct 68 ms 5960 KB Output is correct
54 Correct 16 ms 6016 KB Output is correct
55 Correct 19 ms 6000 KB Output is correct
56 Correct 70 ms 5896 KB Output is correct
57 Correct 16 ms 6056 KB Output is correct
58 Correct 14 ms 5972 KB Output is correct
59 Correct 90 ms 6048 KB Output is correct
60 Correct 17 ms 5972 KB Output is correct
61 Correct 18 ms 6080 KB Output is correct
62 Correct 99 ms 6024 KB Output is correct
63 Correct 96 ms 6084 KB Output is correct
64 Correct 160 ms 6084 KB Output is correct
65 Correct 153 ms 6100 KB Output is correct
66 Correct 108 ms 6100 KB Output is correct
67 Correct 99 ms 5960 KB Output is correct
68 Correct 44 ms 6004 KB Output is correct
69 Correct 48 ms 6112 KB Output is correct
70 Correct 32 ms 6224 KB Output is correct
71 Correct 33 ms 6008 KB Output is correct
72 Correct 31 ms 6008 KB Output is correct
73 Correct 32 ms 6348 KB Output is correct
74 Correct 49 ms 6368 KB Output is correct
75 Correct 53 ms 6384 KB Output is correct
76 Correct 50 ms 6276 KB Output is correct
77 Correct 52 ms 6392 KB Output is correct
78 Correct 66 ms 6360 KB Output is correct
79 Correct 46 ms 6348 KB Output is correct
80 Correct 54 ms 6364 KB Output is correct
81 Correct 59 ms 6240 KB Output is correct
82 Correct 49 ms 6260 KB Output is correct
83 Correct 62 ms 6312 KB Output is correct
84 Correct 57 ms 6196 KB Output is correct
85 Correct 55 ms 6204 KB Output is correct
86 Correct 65 ms 6324 KB Output is correct
87 Correct 58 ms 6316 KB Output is correct
88 Correct 64 ms 6348 KB Output is correct
89 Correct 65 ms 6220 KB Output is correct
90 Correct 68 ms 6148 KB Output is correct
91 Correct 63 ms 6228 KB Output is correct
92 Correct 74 ms 6144 KB Output is correct