Submission #553461

# Submission time Handle Problem Language Result Execution time Memory
553461 2022-04-25T22:13:19 Z SharpEdged Mecho (IOI09_mecho) C++17
100 / 100
195 ms 6356 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define LSB(x) ((x) & -(x))
#define sz(x) (int)(x.size())
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[DEBUG] "<<__func__<<": "<<__LINE__<<", [" << #x << "] = ["; _print(x);
#else
#define dbg(x...)
#endif

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
    
    size_t operator()(string x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        uint64_t res = 0;
        for (int i = 0; i < sz(x); i++){
			res ^= x[i] + FIXED_RANDOM;
			res = splitmix64(res);
		}
        return res;
    }
};

const ll MOD = 1e9 + 7;
const ll INFLL = 1e18 + 5;
const int INF = 1e9 + 5;

char nl = '\n';

ll modPow(ll n, ll k){
	if (k == 0) return 1;
	ll res = modPow(n, k / 2);
	res = res * res % MOD;
	if (k & 1) return res * n % MOD;
	return res;
}

const int mxN = 51;

int dr[4] = {0, 0, 1, -1};
int dc[4] = {1, -1, 0, 0};

void TestCase(){
	int n, m;
	cin >> n >> m;
	vector<string> grid(n);
	vector<vector<int>> bad(n, vector<int>(n, INF));
	vector<vector<int>> dist(n, vector<int>(n, INF));
	pair<int, int> s, e;
	queue<pair<int, int>> q;
	for (int i = 0; i < n; i++){
		cin >> grid[i];
		for (int j = 0; j < n; j++){
			if (grid[i][j] == 'H') {
				bad[i][j] = 0;
				q.push({i, j});
			} else if (grid[i][j] == 'M'){
				s = {i, j};
			} else if (grid[i][j] == 'D'){
				e = {i, j};
			}
		}
	}
	auto inside = [&](int i, int j) -> bool { return i >= 0 && i < n && j >= 0 && j < n; };
	while (!q.empty()){
		auto now = q.front(); q.pop();
		for (int i = 0; i < 4; i++){
			int nr = now.f + dr[i];
			int nc = now.s + dc[i];
			if (!inside(nr, nc) || (grid[nr][nc] != 'G' && grid[nr][nc] != 'M')) continue;
			if (bad[nr][nc] == INF){
				bad[nr][nc] = bad[now.f][now.s] + 1;
				q.push({nr, nc});
			}
		}
	}
	auto check = [&](int mins) -> bool {
		if (bad[s.f][s.s] <= mins) return 0;
		dist.assign(n, vector<int>(n, INF));
		q.push(s);
		dist[s.f][s.s] = 0;
		while (!q.empty()){
			auto now = q.front(); q.pop();
			int elap = mins + (dist[now.f][now.s] + 1) / m;
			for (int i = 0; i < 4; i++){
				int nr = now.f + dr[i];
				int nc = now.s + dc[i];
				if (!inside(nr, nc) || (grid[nr][nc] != 'G' && grid[nr][nc] != 'D') || elap >= bad[nr][nc]) continue;
				if (dist[nr][nc] == INF){
					dist[nr][nc] = dist[now.f][now.s] + 1;
					q.push({nr, nc});
				}
			}
		}
		return dist[e.f][e.s] != INF;
	};
	
	int lo = 0, hi = 1e6;
	int ans = -1;
	while (lo <= hi){
		int mid = (lo + hi) / 2;
		if (check(mid)){
			lo = mid+1;
			ans = mid;
		} else {
			hi = mid-1;
		}
	}
	cout << ans << nl;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int T = 1;
	//cin >> T;
	while (T--){
		TestCase();
	}
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 90 ms 6196 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 356 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 4 ms 1364 KB Output is correct
34 Correct 4 ms 1364 KB Output is correct
35 Correct 21 ms 1452 KB Output is correct
36 Correct 4 ms 1748 KB Output is correct
37 Correct 4 ms 1748 KB Output is correct
38 Correct 29 ms 1796 KB Output is correct
39 Correct 5 ms 2132 KB Output is correct
40 Correct 8 ms 2132 KB Output is correct
41 Correct 34 ms 2160 KB Output is correct
42 Correct 7 ms 2516 KB Output is correct
43 Correct 11 ms 2516 KB Output is correct
44 Correct 56 ms 2584 KB Output is correct
45 Correct 10 ms 3028 KB Output is correct
46 Correct 6 ms 3028 KB Output is correct
47 Correct 60 ms 3028 KB Output is correct
48 Correct 8 ms 3540 KB Output is correct
49 Correct 8 ms 3540 KB Output is correct
50 Correct 63 ms 3540 KB Output is correct
51 Correct 9 ms 4052 KB Output is correct
52 Correct 12 ms 4140 KB Output is correct
53 Correct 99 ms 4052 KB Output is correct
54 Correct 18 ms 4752 KB Output is correct
55 Correct 19 ms 4752 KB Output is correct
56 Correct 87 ms 4692 KB Output is correct
57 Correct 15 ms 5332 KB Output is correct
58 Correct 12 ms 5332 KB Output is correct
59 Correct 131 ms 5452 KB Output is correct
60 Correct 19 ms 5972 KB Output is correct
61 Correct 19 ms 6092 KB Output is correct
62 Correct 138 ms 6096 KB Output is correct
63 Correct 96 ms 6092 KB Output is correct
64 Correct 161 ms 6084 KB Output is correct
65 Correct 195 ms 6192 KB Output is correct
66 Correct 129 ms 6072 KB Output is correct
67 Correct 100 ms 6076 KB Output is correct
68 Correct 49 ms 6100 KB Output is correct
69 Correct 45 ms 6120 KB Output is correct
70 Correct 52 ms 6100 KB Output is correct
71 Correct 40 ms 6108 KB Output is correct
72 Correct 29 ms 6120 KB Output is correct
73 Correct 44 ms 6356 KB Output is correct
74 Correct 56 ms 6356 KB Output is correct
75 Correct 65 ms 6356 KB Output is correct
76 Correct 64 ms 6272 KB Output is correct
77 Correct 74 ms 6352 KB Output is correct
78 Correct 99 ms 6228 KB Output is correct
79 Correct 70 ms 6324 KB Output is correct
80 Correct 58 ms 6324 KB Output is correct
81 Correct 82 ms 6336 KB Output is correct
82 Correct 59 ms 6324 KB Output is correct
83 Correct 99 ms 6300 KB Output is correct
84 Correct 87 ms 6292 KB Output is correct
85 Correct 82 ms 6284 KB Output is correct
86 Correct 84 ms 6288 KB Output is correct
87 Correct 91 ms 6300 KB Output is correct
88 Correct 128 ms 6232 KB Output is correct
89 Correct 102 ms 6236 KB Output is correct
90 Correct 96 ms 6236 KB Output is correct
91 Correct 89 ms 6236 KB Output is correct
92 Correct 83 ms 6232 KB Output is correct