Submission #621419

# Submission time Handle Problem Language Result Execution time Memory
621419 2022-08-03T18:47:02 Z abcisosm5 Mecho (IOI09_mecho) C++17
84 / 100
316 ms 2228 KB
#include <bits/stdc++.h>
using namespace std;
#define pi pair<int, int>
#define f first
#define s second

int N, S;
const int MN = 800;
char grid[MN][MN] = {{}};
vector<pi> hives;
pi M; pi D;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

void print(char (&v)[MN][MN]){
	for(int i = 0; i < N; i++){
		for(int j = 0; j < N; j++){
			if(v[i][j] < 1) cout << ".";
			else cout << v[i][j];
		} cout << endl;
	} cout << endl;
}

void movebees(queue<pair<pi,int>> (&b), char (&v)[MN][MN], int t){
	if(b.empty()) return; int start = b.front().s;
	for(int i = 0; i < t; i++){
		while(!b.empty() && b.front().s <= start + i){
			pair<pi, int> x = b.front(); b.pop();
			pi p = x.f; int index = x.s;
			for(int d = 0; d < 4; d++){
				int nx = p.f + dx[d]; int ny = p.s + dy[d];
				if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
				if(v[nx][ny] != 'B' && grid[nx][ny] == 'G'){
					v[nx][ny] = 'B';
					b.push({{nx, ny}, index+1});
				}
			}
		}
	}
}

bool movemecho(queue<pair<pi, int>> (&m), char (&v)[MN][MN]){
	if(m.empty()) return false; int start = m.front().s;
	for(int i = 0; i < S; i++){
		while(!m.empty() && m.front().s <= start + i){
			pair<pi, int> x = m.front(); m.pop();
			pi p = x.f; int index = x.s;
			if(v[p.f][p.s] == 'B') continue; //invalidate positions that are now occupied by bees
			for(int d = 0; d < 4; d++){
				int nx = p.f + dx[d]; int ny = p.s + dy[d];
				if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
				if(!v[nx][ny] && grid[nx][ny] != 'T'){
					v[nx][ny] = 'M';
					if(grid[nx][ny] == 'D') return true;
					m.push({{nx, ny}, index+1});
				}
			}
		}
	}
	return false;
}

bool works(int t){ //O(N^2)
	char visited[MN][MN] = {{}};
	queue<pair<pi, int>> bees;
	for(pi p : hives){
		bees.push({p,0});
		visited[p.first][p.second] = 'B'; 
	}
	movebees(bees, visited, t);

	queue<pair<pi, int>> mecho; mecho.push({M,0}); visited[M.f][M.s] = 'M';
	
	for(int i = 0; i < N*N/2 + N; i++){
		//first, move mecho S steps
		if(mecho.empty()) break;
		bool success = movemecho(mecho, visited);
		if(success) return true;

		//then, move bees 1 step
		movebees(bees, visited, 1);
	}

	return false;
}

int main(){
	//freopen("../input.in", "r", stdin);
	//freopen("../output.out", "w", stdout);
	cin.tie(0)->sync_with_stdio(0);

	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') M = {i, j};
			if(grid[i][j] == 'D') D = {i, j};
			if(grid[i][j] == 'H') hives.push_back({i,j});
		}
	}

	int lo = -1; int hi = N*N/2 + N; //amount of head start to bees

	while(lo < hi){ // in total, O(N^2 log N)
		int mid = lo + (hi - lo + 1) / 2;
		if(works(mid)){
			lo = mid;
		} else {
			hi = mid - 1;
		}
	}

	cout << lo;

	return 0;
}

Compilation message

mecho.cpp: In function 'void movebees(std::queue<std::pair<std::pair<int, int>, int> >&, char (&)[800][800], int)':
mecho.cpp:25:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   25 |  if(b.empty()) return; int start = b.front().s;
      |  ^~
mecho.cpp:25:24: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   25 |  if(b.empty()) return; int start = b.front().s;
      |                        ^~~
mecho.cpp: In function 'bool movemecho(std::queue<std::pair<std::pair<int, int>, int> >&, char (&)[800][800])':
mecho.cpp:43:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   43 |  if(m.empty()) return false; int start = m.front().s;
      |  ^~
mecho.cpp:43:30: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   43 |  if(m.empty()) return false; int start = m.front().s;
      |                              ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 852 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 1 ms 852 KB Output is correct
7 Correct 245 ms 1724 KB Output is correct
8 Correct 1 ms 852 KB Output is correct
9 Correct 1 ms 852 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 852 KB Output is correct
12 Incorrect 1 ms 980 KB Output isn't correct
13 Incorrect 2 ms 980 KB Output isn't correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 1 ms 852 KB Output is correct
16 Correct 1 ms 852 KB Output is correct
17 Correct 1 ms 852 KB Output is correct
18 Correct 1 ms 852 KB Output is correct
19 Correct 1 ms 852 KB Output is correct
20 Correct 1 ms 852 KB Output is correct
21 Correct 1 ms 980 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 980 KB Output is correct
24 Correct 1 ms 980 KB Output is correct
25 Correct 1 ms 980 KB Output is correct
26 Correct 1 ms 980 KB Output is correct
27 Correct 2 ms 980 KB Output is correct
28 Correct 1 ms 980 KB Output is correct
29 Correct 2 ms 980 KB Output is correct
30 Correct 2 ms 980 KB Output is correct
31 Correct 2 ms 980 KB Output is correct
32 Correct 1 ms 980 KB Output is correct
33 Correct 21 ms 1228 KB Output is correct
34 Correct 20 ms 1108 KB Output is correct
35 Correct 29 ms 1208 KB Output is correct
36 Correct 28 ms 1236 KB Output is correct
37 Correct 26 ms 1364 KB Output is correct
38 Correct 37 ms 1248 KB Output is correct
39 Correct 38 ms 1356 KB Output is correct
40 Correct 34 ms 1300 KB Output is correct
41 Correct 52 ms 1296 KB Output is correct
42 Correct 47 ms 1328 KB Output is correct
43 Correct 41 ms 1316 KB Output is correct
44 Correct 64 ms 1336 KB Output is correct
45 Correct 55 ms 1364 KB Output is correct
46 Correct 53 ms 1364 KB Output is correct
47 Correct 75 ms 1368 KB Output is correct
48 Correct 68 ms 1396 KB Output is correct
49 Correct 60 ms 1412 KB Output is correct
50 Correct 109 ms 1412 KB Output is correct
51 Correct 86 ms 1436 KB Output is correct
52 Correct 87 ms 1460 KB Output is correct
53 Correct 111 ms 1448 KB Output is correct
54 Correct 97 ms 1492 KB Output is correct
55 Correct 84 ms 1480 KB Output is correct
56 Correct 128 ms 1500 KB Output is correct
57 Correct 112 ms 1500 KB Output is correct
58 Correct 94 ms 1532 KB Output is correct
59 Correct 155 ms 1532 KB Output is correct
60 Correct 153 ms 1556 KB Output is correct
61 Correct 113 ms 1548 KB Output is correct
62 Correct 165 ms 1568 KB Output is correct
63 Correct 245 ms 1568 KB Output is correct
64 Correct 267 ms 1568 KB Output is correct
65 Correct 225 ms 1564 KB Output is correct
66 Correct 268 ms 1556 KB Output is correct
67 Correct 255 ms 1564 KB Output is correct
68 Correct 267 ms 1612 KB Output is correct
69 Correct 231 ms 1628 KB Output is correct
70 Correct 242 ms 1608 KB Output is correct
71 Correct 304 ms 1604 KB Output is correct
72 Incorrect 316 ms 1628 KB Output isn't correct
73 Incorrect 270 ms 2004 KB Output isn't correct
74 Correct 254 ms 2004 KB Output is correct
75 Correct 270 ms 2020 KB Output is correct
76 Correct 286 ms 2108 KB Output is correct
77 Correct 273 ms 2228 KB Output is correct
78 Correct 261 ms 1984 KB Output is correct
79 Correct 263 ms 1948 KB Output is correct
80 Correct 270 ms 1944 KB Output is correct
81 Correct 279 ms 1948 KB Output is correct
82 Correct 259 ms 1952 KB Output is correct
83 Correct 282 ms 1880 KB Output is correct
84 Correct 263 ms 2020 KB Output is correct
85 Correct 292 ms 1876 KB Output is correct
86 Correct 271 ms 1884 KB Output is correct
87 Correct 282 ms 1996 KB Output is correct
88 Correct 276 ms 1932 KB Output is correct
89 Correct 260 ms 1812 KB Output is correct
90 Correct 261 ms 1808 KB Output is correct
91 Correct 261 ms 1932 KB Output is correct
92 Correct 273 ms 1812 KB Output is correct