Submission #784324

# Submission time Handle Problem Language Result Execution time Memory
784324 2023-07-16T02:51:45 Z Anthony_Liu Mecho (IOI09_mecho) C++11
100 / 100
832 ms 6696 KB
#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("O3, unroll-loops")
//#pragma GCC target("avx2, bmi, bmi2, lzcnt, popcnt")
#define f first
#define s second
#define ll long long
#define pb push_back 
#define pi pair <int,int>
#define vi vector <int>
#define size(x) (int)(x).size()
#define all(x) x.begin(), x.end()
void setIO(string name = "") {
	cin.tie(0)->sync_with_stdio(0);
	if (size(name)) {
		freopen((name + ".in").c_str(), "r", stdin);
		freopen((name + ".out").c_str(), "w", stdout);
	}
}

int N, S;
pi mecho;
pi home;
	
char grid[801][801];
vector <pi> hives;

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool valid(pi v) {
	if (v.f < 0 || v.s < 0 || v.f >= N || v.s >= N) return false;
	if (grid[v.f][v.s] == 'T' || grid[v.f][v.s] == 'D' || grid[v.f][v.s] == 'H') return false;
	return true;
}

bool calc(int eat) {
	
	queue <pi> q;
	vector <vi> dist1(801, vi(801, INT_MAX)); //dist from hive
	vector <vi> dist2(801, vi(801, INT_MAX)); //dist from mecho
	
	//calc hive dist
	for (auto i : hives) {
		q.push(i);
		dist1[i.f][i.s] = 0;
	}
	
	while (!q.empty()) {
		pi u = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i++) {
			pi v = {u.f + dx[i], u.s + dy[i]};
			if (!valid(v)) continue;
			
			if (dist1[v.f][v.s] > dist1[u.f][u.s] + 1) {
				dist1[v.f][v.s] = dist1[u.f][u.s] + 1;
				q.push(v);
			}
		}
	}
	
	//calc mecho dist
	q.push(mecho);
	dist2[mecho.f][mecho.s] = 0;
	if (dist1[mecho.f][mecho.s] <= eat) return false;
	
	while (!q.empty()) {
		pi u = q.front();
		q.pop();
		
		for (int i = 0; i < 4; i++) {
			pi v = {u.f + dx[i], u.s + dy[i]};
			if (!valid(v)) continue;
			
			if (dist2[v.f][v.s] > dist2[u.f][u.s] + 1) {
				int md = dist2[u.f][u.s] + 1;
				int bd = dist1[v.f][v.s] - eat;
				if (md / S >= bd) continue;
				
				dist2[v.f][v.s] = dist2[u.f][u.s] + 1;
				q.push(v);
			}
		}
	}
	
	for (int i = 0; i < 4; i++) 
	{
		int nx = home.f + dx[i], ny = home.s + dy[i];
		if (!valid({nx, ny})) continue;
		
		int md = dist2[nx][ny];
		int bd = dist1[nx][ny] - eat;
		if (md / S >= bd) continue;
		
		if (dist2[nx][ny] != INT_MAX) 
			return true;
	}
	return false;
}

int main()
{
	setIO();
	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') mecho = {i, j};
			else if (grid[i][j] == 'D') home = {i, j};
			else if (grid[i][j] == 'H') {
				hives.pb({i, j});
			}
		}
	}
		
	int l = 0, r = N * N;
	int ans = -1;
	
	while (l <= r) {
		int m = l + (r - l) / 2;
		if (calc(m)) {
			ans = m;
			l = m + 1;
		}
		else r = m - 1;
	}
	
	cout << ans << '\n'; 
	return 0;
}

Compilation message

mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:16:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   freopen((name + ".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:17:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |   freopen((name + ".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5380 KB Output is correct
2 Correct 9 ms 5428 KB Output is correct
3 Correct 6 ms 5380 KB Output is correct
4 Correct 7 ms 5396 KB Output is correct
5 Correct 10 ms 5424 KB Output is correct
6 Correct 15 ms 5428 KB Output is correct
7 Correct 792 ms 6392 KB Output is correct
8 Correct 13 ms 5424 KB Output is correct
9 Correct 11 ms 5452 KB Output is correct
10 Correct 11 ms 5436 KB Output is correct
11 Correct 11 ms 5472 KB Output is correct
12 Correct 18 ms 5496 KB Output is correct
13 Correct 6 ms 5440 KB Output is correct
14 Correct 7 ms 5452 KB Output is correct
15 Correct 13 ms 5472 KB Output is correct
16 Correct 11 ms 5432 KB Output is correct
17 Correct 12 ms 5444 KB Output is correct
18 Correct 18 ms 5488 KB Output is correct
19 Correct 13 ms 5472 KB Output is correct
20 Correct 12 ms 5460 KB Output is correct
21 Correct 16 ms 5500 KB Output is correct
22 Correct 15 ms 5460 KB Output is correct
23 Correct 16 ms 5508 KB Output is correct
24 Correct 15 ms 5508 KB Output is correct
25 Correct 19 ms 5516 KB Output is correct
26 Correct 19 ms 5520 KB Output is correct
27 Correct 24 ms 5500 KB Output is correct
28 Correct 20 ms 5556 KB Output is correct
29 Correct 20 ms 5508 KB Output is correct
30 Correct 19 ms 5524 KB Output is correct
31 Correct 17 ms 5536 KB Output is correct
32 Correct 19 ms 5524 KB Output is correct
33 Correct 75 ms 5824 KB Output is correct
34 Correct 76 ms 5832 KB Output is correct
35 Correct 126 ms 5796 KB Output is correct
36 Correct 95 ms 5884 KB Output is correct
37 Correct 101 ms 5892 KB Output is correct
38 Correct 178 ms 5896 KB Output is correct
39 Correct 111 ms 5984 KB Output is correct
40 Correct 123 ms 5992 KB Output is correct
41 Correct 232 ms 6120 KB Output is correct
42 Correct 138 ms 6064 KB Output is correct
43 Correct 147 ms 6060 KB Output is correct
44 Correct 277 ms 6048 KB Output is correct
45 Correct 172 ms 6096 KB Output is correct
46 Correct 182 ms 6116 KB Output is correct
47 Correct 339 ms 6100 KB Output is correct
48 Correct 188 ms 6128 KB Output is correct
49 Correct 216 ms 6176 KB Output is correct
50 Correct 393 ms 6140 KB Output is correct
51 Correct 233 ms 6180 KB Output is correct
52 Correct 237 ms 6200 KB Output is correct
53 Correct 517 ms 6288 KB Output is correct
54 Correct 284 ms 6228 KB Output is correct
55 Correct 271 ms 6352 KB Output is correct
56 Correct 568 ms 6212 KB Output is correct
57 Correct 300 ms 6252 KB Output is correct
58 Correct 302 ms 6248 KB Output is correct
59 Correct 666 ms 6320 KB Output is correct
60 Correct 362 ms 6340 KB Output is correct
61 Correct 343 ms 6320 KB Output is correct
62 Correct 742 ms 6376 KB Output is correct
63 Correct 670 ms 6336 KB Output is correct
64 Correct 825 ms 6316 KB Output is correct
65 Correct 733 ms 6276 KB Output is correct
66 Correct 682 ms 6296 KB Output is correct
67 Correct 667 ms 6432 KB Output is correct
68 Correct 636 ms 6300 KB Output is correct
69 Correct 657 ms 6304 KB Output is correct
70 Correct 593 ms 6320 KB Output is correct
71 Correct 629 ms 6304 KB Output is correct
72 Correct 575 ms 6300 KB Output is correct
73 Correct 671 ms 6696 KB Output is correct
74 Correct 716 ms 6588 KB Output is correct
75 Correct 776 ms 6580 KB Output is correct
76 Correct 731 ms 6624 KB Output is correct
77 Correct 725 ms 6672 KB Output is correct
78 Correct 753 ms 6600 KB Output is correct
79 Correct 696 ms 6596 KB Output is correct
80 Correct 710 ms 6592 KB Output is correct
81 Correct 733 ms 6600 KB Output is correct
82 Correct 712 ms 6564 KB Output is correct
83 Correct 768 ms 6528 KB Output is correct
84 Correct 742 ms 6612 KB Output is correct
85 Correct 791 ms 6592 KB Output is correct
86 Correct 752 ms 6504 KB Output is correct
87 Correct 751 ms 6520 KB Output is correct
88 Correct 773 ms 6576 KB Output is correct
89 Correct 774 ms 6556 KB Output is correct
90 Correct 800 ms 6460 KB Output is correct
91 Correct 781 ms 6448 KB Output is correct
92 Correct 832 ms 6440 KB Output is correct