답안 #553460

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
553460 2022-04-25T22:11:34 Z SharpEdged Mecho (IOI09_mecho) C++17
84 / 100
156 ms 6364 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') 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;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 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 6188 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Incorrect 1 ms 340 KB Output isn't correct
13 Incorrect 0 ms 340 KB Output isn't correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 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 0 ms 212 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 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 3 ms 1364 KB Output is correct
34 Correct 4 ms 1364 KB Output is correct
35 Correct 20 ms 1448 KB Output is correct
36 Correct 4 ms 1788 KB Output is correct
37 Correct 4 ms 1748 KB Output is correct
38 Correct 27 ms 1748 KB Output is correct
39 Correct 5 ms 2132 KB Output is correct
40 Correct 6 ms 2132 KB Output is correct
41 Correct 34 ms 2164 KB Output is correct
42 Correct 7 ms 2516 KB Output is correct
43 Correct 9 ms 2472 KB Output is correct
44 Correct 47 ms 2516 KB Output is correct
45 Correct 6 ms 3028 KB Output is correct
46 Correct 6 ms 3060 KB Output is correct
47 Correct 54 ms 3028 KB Output is correct
48 Correct 8 ms 3540 KB Output is correct
49 Correct 12 ms 3540 KB Output is correct
50 Correct 69 ms 3600 KB Output is correct
51 Correct 10 ms 4112 KB Output is correct
52 Correct 11 ms 4136 KB Output is correct
53 Correct 76 ms 4140 KB Output is correct
54 Correct 13 ms 4752 KB Output is correct
55 Correct 15 ms 4692 KB Output is correct
56 Correct 94 ms 4756 KB Output is correct
57 Correct 12 ms 5332 KB Output is correct
58 Correct 12 ms 5396 KB Output is correct
59 Correct 115 ms 5392 KB Output is correct
60 Correct 13 ms 6100 KB Output is correct
61 Correct 14 ms 6100 KB Output is correct
62 Correct 130 ms 6100 KB Output is correct
63 Correct 98 ms 6096 KB Output is correct
64 Correct 156 ms 6080 KB Output is correct
65 Correct 150 ms 6104 KB Output is correct
66 Correct 128 ms 6076 KB Output is correct
67 Correct 103 ms 6092 KB Output is correct
68 Correct 46 ms 6100 KB Output is correct
69 Correct 41 ms 6100 KB Output is correct
70 Correct 37 ms 6100 KB Output is correct
71 Correct 31 ms 6124 KB Output is correct
72 Incorrect 30 ms 6096 KB Output isn't correct
73 Incorrect 63 ms 6356 KB Output isn't correct
74 Correct 58 ms 6356 KB Output is correct
75 Correct 67 ms 6356 KB Output is correct
76 Correct 63 ms 6364 KB Output is correct
77 Correct 71 ms 6356 KB Output is correct
78 Correct 92 ms 6228 KB Output is correct
79 Correct 54 ms 6332 KB Output is correct
80 Correct 61 ms 6228 KB Output is correct
81 Correct 64 ms 6228 KB Output is correct
82 Correct 52 ms 6320 KB Output is correct
83 Correct 73 ms 6288 KB Output is correct
84 Correct 68 ms 6288 KB Output is correct
85 Correct 68 ms 6304 KB Output is correct
86 Correct 84 ms 6288 KB Output is correct
87 Correct 75 ms 6284 KB Output is correct
88 Correct 77 ms 6228 KB Output is correct
89 Correct 79 ms 6236 KB Output is correct
90 Correct 87 ms 6232 KB Output is correct
91 Correct 88 ms 6248 KB Output is correct
92 Correct 88 ms 6240 KB Output is correct