Submission #621421

# Submission time Handle Problem Language Result Execution time Memory
621421 2022-08-03T18:49:01 Z abcisosm5 Mecho (IOI09_mecho) C++17
84 / 100
331 ms 2132 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; 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; //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;
}
# 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 259 ms 1728 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 956 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 980 KB Output is correct
23 Correct 1 ms 980 KB Output is correct
24 Correct 2 ms 980 KB Output is correct
25 Correct 2 ms 980 KB Output is correct
26 Correct 2 ms 912 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 1000 KB Output is correct
30 Correct 2 ms 980 KB Output is correct
31 Correct 1 ms 980 KB Output is correct
32 Correct 1 ms 980 KB Output is correct
33 Correct 23 ms 1224 KB Output is correct
34 Correct 24 ms 1236 KB Output is correct
35 Correct 32 ms 1108 KB Output is correct
36 Correct 31 ms 1260 KB Output is correct
37 Correct 28 ms 1236 KB Output is correct
38 Correct 39 ms 1272 KB Output is correct
39 Correct 37 ms 1236 KB Output is correct
40 Correct 35 ms 1236 KB Output is correct
41 Correct 52 ms 1236 KB Output is correct
42 Correct 50 ms 1236 KB Output is correct
43 Correct 43 ms 1236 KB Output is correct
44 Correct 65 ms 1224 KB Output is correct
45 Correct 62 ms 1364 KB Output is correct
46 Correct 52 ms 1376 KB Output is correct
47 Correct 82 ms 1364 KB Output is correct
48 Correct 71 ms 1364 KB Output is correct
49 Correct 76 ms 1408 KB Output is correct
50 Correct 97 ms 1412 KB Output is correct
51 Correct 88 ms 1444 KB Output is correct
52 Correct 76 ms 1444 KB Output is correct
53 Correct 115 ms 1456 KB Output is correct
54 Correct 99 ms 1492 KB Output is correct
55 Correct 88 ms 1364 KB Output is correct
56 Correct 171 ms 1456 KB Output is correct
57 Correct 113 ms 1512 KB Output is correct
58 Correct 100 ms 1492 KB Output is correct
59 Correct 152 ms 1524 KB Output is correct
60 Correct 136 ms 1552 KB Output is correct
61 Correct 115 ms 1552 KB Output is correct
62 Correct 190 ms 1492 KB Output is correct
63 Correct 257 ms 1568 KB Output is correct
64 Correct 291 ms 1560 KB Output is correct
65 Correct 257 ms 1564 KB Output is correct
66 Correct 277 ms 1576 KB Output is correct
67 Correct 275 ms 1572 KB Output is correct
68 Correct 293 ms 1620 KB Output is correct
69 Correct 250 ms 1620 KB Output is correct
70 Correct 257 ms 1624 KB Output is correct
71 Correct 285 ms 1628 KB Output is correct
72 Incorrect 331 ms 1608 KB Output isn't correct
73 Incorrect 291 ms 2004 KB Output isn't correct
74 Correct 316 ms 2004 KB Output is correct
75 Correct 287 ms 2132 KB Output is correct
76 Correct 272 ms 2108 KB Output is correct
77 Correct 296 ms 2012 KB Output is correct
78 Correct 283 ms 1940 KB Output is correct
79 Correct 265 ms 1960 KB Output is correct
80 Correct 288 ms 2012 KB Output is correct
81 Correct 289 ms 1960 KB Output is correct
82 Correct 289 ms 2004 KB Output is correct
83 Correct 287 ms 1900 KB Output is correct
84 Correct 272 ms 1884 KB Output is correct
85 Correct 297 ms 1884 KB Output is correct
86 Correct 288 ms 2000 KB Output is correct
87 Correct 281 ms 1892 KB Output is correct
88 Correct 274 ms 1824 KB Output is correct
89 Correct 290 ms 1832 KB Output is correct
90 Correct 265 ms 1824 KB Output is correct
91 Correct 266 ms 1912 KB Output is correct
92 Correct 281 ms 1816 KB Output is correct