답안 #553459

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
553459 2022-04-25T22:03:51 Z SharpEdged Mecho (IOI09_mecho) C++17
84 / 100
156 ms 6368 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 {
		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 0 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 0 ms 212 KB Output is correct
7 Correct 92 ms 6200 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 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 1 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 340 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 0 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 1 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 4 ms 1440 KB Output is correct
34 Correct 5 ms 1364 KB Output is correct
35 Correct 27 ms 1364 KB Output is correct
36 Correct 4 ms 1748 KB Output is correct
37 Correct 4 ms 1748 KB Output is correct
38 Correct 26 ms 1772 KB Output is correct
39 Correct 5 ms 2164 KB Output is correct
40 Correct 5 ms 2132 KB Output is correct
41 Correct 35 ms 2148 KB Output is correct
42 Correct 7 ms 2516 KB Output is correct
43 Correct 7 ms 2516 KB Output is correct
44 Correct 46 ms 2516 KB Output is correct
45 Correct 7 ms 2996 KB Output is correct
46 Correct 8 ms 3028 KB Output is correct
47 Correct 64 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 71 ms 3596 KB Output is correct
51 Correct 10 ms 4180 KB Output is correct
52 Correct 10 ms 4052 KB Output is correct
53 Correct 104 ms 4132 KB Output is correct
54 Correct 17 ms 4700 KB Output is correct
55 Correct 14 ms 4748 KB Output is correct
56 Correct 100 ms 4684 KB Output is correct
57 Correct 13 ms 5332 KB Output is correct
58 Correct 12 ms 5332 KB Output is correct
59 Correct 108 ms 5396 KB Output is correct
60 Correct 13 ms 5972 KB Output is correct
61 Correct 14 ms 5972 KB Output is correct
62 Correct 156 ms 6096 KB Output is correct
63 Correct 105 ms 6092 KB Output is correct
64 Correct 151 ms 6092 KB Output is correct
65 Correct 139 ms 6084 KB Output is correct
66 Correct 133 ms 6080 KB Output is correct
67 Correct 98 ms 6096 KB Output is correct
68 Correct 41 ms 6100 KB Output is correct
69 Correct 41 ms 6104 KB Output is correct
70 Correct 37 ms 6104 KB Output is correct
71 Correct 33 ms 6120 KB Output is correct
72 Incorrect 29 ms 6100 KB Output isn't correct
73 Incorrect 71 ms 6360 KB Output isn't correct
74 Correct 58 ms 6356 KB Output is correct
75 Correct 64 ms 6356 KB Output is correct
76 Correct 58 ms 6368 KB Output is correct
77 Correct 68 ms 6356 KB Output is correct
78 Correct 93 ms 6324 KB Output is correct
79 Correct 57 ms 6328 KB Output is correct
80 Correct 57 ms 6228 KB Output is correct
81 Correct 68 ms 6324 KB Output is correct
82 Correct 53 ms 6304 KB Output is correct
83 Correct 87 ms 6304 KB Output is correct
84 Correct 71 ms 6292 KB Output is correct
85 Correct 76 ms 6296 KB Output is correct
86 Correct 78 ms 6296 KB Output is correct
87 Correct 79 ms 6284 KB Output is correct
88 Correct 99 ms 6244 KB Output is correct
89 Correct 81 ms 6236 KB Output is correct
90 Correct 94 ms 6236 KB Output is correct
91 Correct 97 ms 6228 KB Output is correct
92 Correct 84 ms 6244 KB Output is correct