Submission #823118

# Submission time Handle Problem Language Result Execution time Memory
823118 2023-08-12T08:14:10 Z Magikarp4000 Mecho (IOI09_mecho) C++17
100 / 100
551 ms 11992 KB
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n' 
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;

const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
const int MAXN = 8e2+1;
int n,s;
string g[MAXN];
PII sta, en;
vector<PII> h;
int hn;
int mz[MAXN][MAXN], hz[MAXN][MAXN];
int md[MAXN][MAXN], hd[MAXN][MAXN];

bool check(int cur) {
    FOR(i,0,n) {
        FOR(j,0,n) {
            mz[i][j] = 0;
            hz[i][j] = 0;
            md[i][j] = INF;
            hd[i][j] = INF;
        }
    }
    queue<PII> q;
    FOR(i,0,hn) {
        q.push(h[i]);
        hd[h[i].S][h[i].F] = 0;
    }
    while (!q.empty()) {
        int x = q.front().F, y = q.front().S;
        q.pop();
        if (hz[y][x]) continue;
        hz[y][x] = 1;
        FOR(i,0,4) {
            int nx = x+dx[i], ny = y+dy[i];
            if (nx < 0 || nx > n || ny < 0 || ny > n) continue;
            if (g[ny][nx] == 'T' || g[ny][nx] == 'D') continue;
            if (hd[y][x]+1 < hd[ny][nx]) {
                hd[ny][nx] = hd[y][x]+1;
                q.push({nx,ny});
            }
        }
    }
    if (hd[sta.S][sta.F] <= cur) return 0;
    md[sta.S][sta.F] = 0;
    q.push(sta);
    while (!q.empty()) {
        int x = q.front().F, y = q.front().S;
        q.pop();
        if (mz[y][x]) continue;
        mz[y][x] = 1;
        FOR(i,0,4) {
            int nx = x+dx[i], ny = y+dy[i];
            if (nx < 0 || nx > n || ny < 0 || ny > n) continue;
            if (g[ny][nx] == 'T') continue;
            if (md[y][x]+1 < md[ny][nx] && hd[ny][nx]-cur > (md[y][x]+1)/s) {
                md[ny][nx] = md[y][x]+1;
                q.push({nx,ny});
            }
            else if (md[y][x]+1 < md[ny][nx] && hd[ny][nx]-cur == (md[y][x]+1)/s) {
                md[ny][nx] = md[y][x]+1;
                mz[ny][nx] = 1;
            }
        }
    }
    if (mz[en.S][en.F]) return 1;
    return 0;
}

signed main() {
    OPTM;
    cin >> n >> s;
    FOR(i,0,n) cin >> g[i];
    FOR(i,0,n) {
        FOR(j,0,n) {
            if (g[i][j] == 'M') sta = {j,i};
            if (g[i][j] == 'D') en = {j,i};
            if (g[i][j] == 'H') h.PB({j,i});
        }
    }
    hn = h.size();
    int l = 0, r = 1e7+1;
    while (l < r) {
        int mid = (l+r+1)/2;
        if (check(mid)) l = mid;
        else r = mid-1;
    }
    if (!check(0)) cout << -1;
    else cout << l << ln;;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 360 KB Output is correct
2 Correct 0 ms 352 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 434 ms 11796 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 480 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 2 ms 1108 KB Output is correct
13 Correct 2 ms 980 KB Output is correct
14 Correct 2 ms 1120 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 KB Output is correct
21 Correct 1 ms 868 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 996 KB Output is correct
24 Correct 1 ms 980 KB Output is correct
25 Correct 2 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 2 ms 1108 KB Output is correct
28 Correct 2 ms 1108 KB Output is correct
29 Correct 2 ms 1252 KB Output is correct
30 Correct 2 ms 1236 KB Output is correct
31 Correct 2 ms 1236 KB Output is correct
32 Correct 2 ms 1236 KB Output is correct
33 Correct 32 ms 4948 KB Output is correct
34 Correct 27 ms 4948 KB Output is correct
35 Correct 55 ms 4988 KB Output is correct
36 Correct 43 ms 5588 KB Output is correct
37 Correct 35 ms 5588 KB Output is correct
38 Correct 71 ms 5588 KB Output is correct
39 Correct 53 ms 6356 KB Output is correct
40 Correct 51 ms 6400 KB Output is correct
41 Correct 93 ms 6416 KB Output is correct
42 Correct 68 ms 7124 KB Output is correct
43 Correct 54 ms 7136 KB Output is correct
44 Correct 111 ms 7124 KB Output is correct
45 Correct 82 ms 7860 KB Output is correct
46 Correct 75 ms 7852 KB Output is correct
47 Correct 137 ms 7868 KB Output is correct
48 Correct 97 ms 8612 KB Output is correct
49 Correct 78 ms 8608 KB Output is correct
50 Correct 169 ms 8620 KB Output is correct
51 Correct 116 ms 9360 KB Output is correct
52 Correct 93 ms 9300 KB Output is correct
53 Correct 197 ms 9372 KB Output is correct
54 Correct 127 ms 10172 KB Output is correct
55 Correct 114 ms 10116 KB Output is correct
56 Correct 224 ms 10120 KB Output is correct
57 Correct 151 ms 10900 KB Output is correct
58 Correct 130 ms 10836 KB Output is correct
59 Correct 272 ms 10904 KB Output is correct
60 Correct 170 ms 11672 KB Output is correct
61 Correct 138 ms 11604 KB Output is correct
62 Correct 340 ms 11680 KB Output is correct
63 Correct 415 ms 11732 KB Output is correct
64 Correct 469 ms 11680 KB Output is correct
65 Correct 470 ms 11604 KB Output is correct
66 Correct 436 ms 11604 KB Output is correct
67 Correct 435 ms 11684 KB Output is correct
68 Correct 441 ms 11708 KB Output is correct
69 Correct 470 ms 11700 KB Output is correct
70 Correct 461 ms 11732 KB Output is correct
71 Correct 462 ms 11712 KB Output is correct
72 Correct 427 ms 11720 KB Output is correct
73 Correct 377 ms 11976 KB Output is correct
74 Correct 430 ms 11992 KB Output is correct
75 Correct 551 ms 11988 KB Output is correct
76 Correct 499 ms 11988 KB Output is correct
77 Correct 372 ms 11988 KB Output is correct
78 Correct 375 ms 11940 KB Output is correct
79 Correct 383 ms 11964 KB Output is correct
80 Correct 392 ms 11952 KB Output is correct
81 Correct 377 ms 11952 KB Output is correct
82 Correct 382 ms 11972 KB Output is correct
83 Correct 378 ms 11904 KB Output is correct
84 Correct 382 ms 11860 KB Output is correct
85 Correct 404 ms 11860 KB Output is correct
86 Correct 393 ms 11908 KB Output is correct
87 Correct 380 ms 11916 KB Output is correct
88 Correct 388 ms 11860 KB Output is correct
89 Correct 401 ms 11844 KB Output is correct
90 Correct 389 ms 11860 KB Output is correct
91 Correct 396 ms 11860 KB Output is correct
92 Correct 380 ms 11860 KB Output is correct