Submission #406421

# Submission time Handle Problem Language Result Execution time Memory
406421 2021-05-17T14:56:41 Z Falcon Mecho (IOI09_mecho) C++17
100 / 100
252 ms 6252 KB
#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)

#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }

#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }

#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


using ll = long long;

template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, s; cin >> n >> s;
    vector<string> grid(n); rep(i, n) cin >> grid[i];

    vector<vector<int>> bee_dist = [&]{
        vector<vector<int>> dist(n, vector<int>(n, -1));

        queue<pair<int, int>> q;
        rep(i, n) rep(j, n) if(grid[i][j] == 'H')
            q.push({i, j}), dist[i][j] = 0;

        auto add = [&](int x, int y, int d) {
            if(x < 0 || y < 0 || x >= n || y >= n)
                return;
            
            if(grid[x][y] != 'G' && grid[x][y] != 'M')
                return;

            if(~dist[x][y])
                return;

            q.push({x, y});
            dist[x][y] = d + s; 
        };

        while(!q.empty()) {
            auto [i, j] = q.front(); q.pop();
            add(i + 1, j, dist[i][j]);
            add(i, j + 1, dist[i][j]);
            add(i - 1, j, dist[i][j]);
            add(i, j - 1, dist[i][j]);
        }

        return dist;
    }();

    auto check = [&](int t) {
        t = t * s;
        

        queue<pair<int, int>> q;
        vector<vector<int>> dist(n, vector<int>(n, -1));
        rep(i, n) rep(j, n) if(grid[i][j] == 'M') {
            if(~bee_dist[i][j] && bee_dist[i][j] <= t)
                return false;
            q.push({i, j});
            dist[i][j] = t;
        }

        auto add = [&](int x, int y, int d) {
            if(x < 0 || y < 0 || x >= n || y >= n)
                return;

            if(grid[x][y] != 'G' && grid[x][y] != 'D')
                return;

            if(~dist[x][y])
                return;
            
            if(~bee_dist[x][y] && d + 1 >= bee_dist[x][y])
                return;

            q.push({x, y});
            dist[x][y] = d + 1; 
        };

        while(!q.empty()) {
            auto [i, j] = q.front(); q.pop();
            if(grid[i][j] == 'D')
                return true;

            add(i + 1, j, dist[i][j]);
            add(i - 1, j, dist[i][j]);
            add(i, j + 1, dist[i][j]);
            add(i, j - 1, dist[i][j]);
        }

        return false;
     };

    if(!check(0))
        return cout << -1 << '\n', 0;

    int t{};
    for(int k{1 << __lg(n * n)}; k > 0; k >>= 1)
        if(t + k <= n * n && check(t + k))
            t += k;

    cout << t << '\n';
    
    
    #ifdef DEBUG
    dbg::dout << "\nExecution time: "
              << clock() * 1000 / CLOCKS_PER_SEC 
              << "ms" << endl;
    #endif
    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 152 ms 6096 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 456 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 7 ms 1428 KB Output is correct
34 Correct 8 ms 1428 KB Output is correct
35 Correct 30 ms 1436 KB Output is correct
36 Correct 9 ms 1784 KB Output is correct
37 Correct 10 ms 1776 KB Output is correct
38 Correct 37 ms 1776 KB Output is correct
39 Correct 11 ms 2148 KB Output is correct
40 Correct 15 ms 2160 KB Output is correct
41 Correct 48 ms 2164 KB Output is correct
42 Correct 14 ms 2640 KB Output is correct
43 Correct 16 ms 2588 KB Output is correct
44 Correct 58 ms 2584 KB Output is correct
45 Correct 19 ms 3036 KB Output is correct
46 Correct 19 ms 3100 KB Output is correct
47 Correct 74 ms 3052 KB Output is correct
48 Correct 21 ms 3640 KB Output is correct
49 Correct 25 ms 3612 KB Output is correct
50 Correct 99 ms 3720 KB Output is correct
51 Correct 24 ms 4180 KB Output is correct
52 Correct 27 ms 4164 KB Output is correct
53 Correct 126 ms 4132 KB Output is correct
54 Correct 29 ms 4812 KB Output is correct
55 Correct 31 ms 4784 KB Output is correct
56 Correct 144 ms 4772 KB Output is correct
57 Correct 33 ms 5424 KB Output is correct
58 Correct 37 ms 5424 KB Output is correct
59 Correct 150 ms 5412 KB Output is correct
60 Correct 36 ms 6132 KB Output is correct
61 Correct 43 ms 6168 KB Output is correct
62 Correct 178 ms 6116 KB Output is correct
63 Correct 147 ms 6220 KB Output is correct
64 Correct 252 ms 6088 KB Output is correct
65 Correct 227 ms 6148 KB Output is correct
66 Correct 194 ms 6092 KB Output is correct
67 Correct 34 ms 5964 KB Output is correct
68 Correct 81 ms 6220 KB Output is correct
69 Correct 76 ms 6096 KB Output is correct
70 Correct 88 ms 6104 KB Output is correct
71 Correct 66 ms 6112 KB Output is correct
72 Correct 56 ms 6100 KB Output is correct
73 Correct 56 ms 6228 KB Output is correct
74 Correct 99 ms 6120 KB Output is correct
75 Correct 98 ms 6172 KB Output is correct
76 Correct 107 ms 6168 KB Output is correct
77 Correct 96 ms 6120 KB Output is correct
78 Correct 32 ms 6080 KB Output is correct
79 Correct 97 ms 6108 KB Output is correct
80 Correct 101 ms 6200 KB Output is correct
81 Correct 109 ms 6252 KB Output is correct
82 Correct 102 ms 6120 KB Output is correct
83 Correct 116 ms 6152 KB Output is correct
84 Correct 111 ms 6096 KB Output is correct
85 Correct 127 ms 6112 KB Output is correct
86 Correct 119 ms 6132 KB Output is correct
87 Correct 121 ms 6108 KB Output is correct
88 Correct 117 ms 6128 KB Output is correct
89 Correct 131 ms 6112 KB Output is correct
90 Correct 129 ms 6120 KB Output is correct
91 Correct 127 ms 6104 KB Output is correct
92 Correct 145 ms 6128 KB Output is correct