Submission #491061

# Submission time Handle Problem Language Result Execution time Memory
491061 2021-11-30T06:25:07 Z sberens Mecho (IOI09_mecho) C++17
100 / 100
637 ms 6228 KB
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
template<typename K> using hset = gp_hash_table<K, null_type>;
template<typename K, typename V> using hmap = gp_hash_table<K, V>;


using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define smax(x, y) (x = max(x, y))
#define smin(x, y) (x = min(x, y))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i,0,a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i,0,a)


using ll = long long;
using ld = long double;

template<typename T>
using vv = vector<vector<T>>;

using vi = vector<int>;
using ii = array<int, 2>;
using vii = vector<ii>;
using vvi = vv<int>;

using vll = vector<ll>;
using l2 = array<ll, 2>;
using vl2 = vector<l2>;
using vvll = vv<ll>;

template<typename T>
using minq = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using maxq = priority_queue<T>;

const ll M = 1000000007;
const ll infll = M * M;

template<typename IN>
IN discrete_binary_search(function<bool(IN)> predicate, IN low = 0, IN high = numeric_limits<IN>::max()) {
    while (low < high) {
        IN middle = low + (high - low) / 2; // todo std::midpoint in cpp 2020
        if (predicate(middle))
            high = middle;
        else low = middle + 1;
    }
    return low;
}

vii dirs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

vii adjc(int x, int y) {
    vii res;
    for (auto [dx, dy] : dirs)
        res.pb({x + dx, y + dy});
    return res;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, s;
    cin >> n >> s;
    vv<char> g(n, vector<char>(n));
    int mr, mc, dr, dc;
    vii hives;
    F0R(i, n) {
        F0R(j, n) {
            char x;
            cin >> x;
            g[i][j] = x;
            if (x == 'M') {
                mr = i;
                mc = j;
            } else if (x == 'H') {
                hives.pb({i, j});
            } else if (x == 'D') {
                dr = i;
                dc = j;
            }
        }
    }
    vvi beetime(n, vi(n, M));
    queue<ii> bees;
    for (auto [r, c] : hives) {
        bees.push({r, c});
        beetime[r][c] = 0;
    }

    F0R(t, n * n) {
        queue<ii> nextbees;
        while (!bees.empty()) {
            assert(bees.size() < n * n);
            auto [r, c] = bees.front(); bees.pop();
            for (auto [nr, nc] : adjc(r, c)) {
                if (0 <= nr && nr < n && 0 <= nc && nc < n && beetime[nr][nc] == M && (g[nr][nc] == 'G' || g[nr][nc] == 'M')) {
                    nextbees.push({nr, nc});
                    beetime[nr][nc] = t + 1;
                }
            }
        }
        swap(bees, nextbees);
    }

    cout << discrete_binary_search<int>([&](int startt) -> bool {
        vvi seen(n, vi(n));
        queue<ii> bear;
        bear.push({mr, mc});
        seen[mr][mc] = 1;
        if (beetime[mr][mc] <= startt) return true;
        FOR(t, startt, n * n) {
            F0R(_, s) {
                queue<ii> nextbear;
                while (!bear.empty()) {
                    auto [r, c] = bear.front(); bear.pop();
                    if (r == dr && c == dc) return false;
                    if (beetime[r][c] == t) continue;
                    for (auto [nr, nc] : adjc(r, c)) {
                        if (0 <= nr && nr < n && 0 <= nc && nc < n && (g[nr][nc] == 'G' || g[nr][nc] == 'D') && seen[nr][nc] == 0 && beetime[nr][nc] > t) {
                            nextbear.push({nr, nc});
                            seen[nr][nc] = 1;
                        }
                    }
                }
                swap(bear, nextbear);
                if (bear.empty()) return true;
            }
        }
        return true;

    }, 0, n * n) - 1 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 412 ms 6120 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 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 2 ms 332 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 332 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 2 ms 332 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 17 ms 1428 KB Output is correct
34 Correct 19 ms 1424 KB Output is correct
35 Correct 91 ms 1412 KB Output is correct
36 Correct 23 ms 1768 KB Output is correct
37 Correct 25 ms 1764 KB Output is correct
38 Correct 154 ms 1792 KB Output is correct
39 Correct 28 ms 2140 KB Output is correct
40 Correct 31 ms 2128 KB Output is correct
41 Correct 159 ms 2164 KB Output is correct
42 Correct 36 ms 2548 KB Output is correct
43 Correct 39 ms 2612 KB Output is correct
44 Correct 195 ms 2592 KB Output is correct
45 Correct 44 ms 2992 KB Output is correct
46 Correct 46 ms 2996 KB Output is correct
47 Correct 264 ms 3068 KB Output is correct
48 Correct 51 ms 3600 KB Output is correct
49 Correct 54 ms 3524 KB Output is correct
50 Correct 261 ms 3564 KB Output is correct
51 Correct 67 ms 4244 KB Output is correct
52 Correct 64 ms 4156 KB Output is correct
53 Correct 325 ms 4092 KB Output is correct
54 Correct 70 ms 4760 KB Output is correct
55 Correct 77 ms 4744 KB Output is correct
56 Correct 426 ms 4724 KB Output is correct
57 Correct 86 ms 5364 KB Output is correct
58 Correct 86 ms 5376 KB Output is correct
59 Correct 481 ms 5516 KB Output is correct
60 Correct 96 ms 6068 KB Output is correct
61 Correct 99 ms 6084 KB Output is correct
62 Correct 637 ms 6064 KB Output is correct
63 Correct 346 ms 6068 KB Output is correct
64 Correct 542 ms 6056 KB Output is correct
65 Correct 526 ms 6228 KB Output is correct
66 Correct 383 ms 6052 KB Output is correct
67 Correct 425 ms 6080 KB Output is correct
68 Correct 176 ms 6104 KB Output is correct
69 Correct 153 ms 6208 KB Output is correct
70 Correct 160 ms 6072 KB Output is correct
71 Correct 132 ms 6080 KB Output is correct
72 Correct 117 ms 6116 KB Output is correct
73 Correct 155 ms 6144 KB Output is correct
74 Correct 263 ms 6088 KB Output is correct
75 Correct 293 ms 6036 KB Output is correct
76 Correct 286 ms 6100 KB Output is correct
77 Correct 306 ms 6224 KB Output is correct
78 Correct 358 ms 6008 KB Output is correct
79 Correct 232 ms 6100 KB Output is correct
80 Correct 279 ms 6124 KB Output is correct
81 Correct 323 ms 6088 KB Output is correct
82 Correct 261 ms 6220 KB Output is correct
83 Correct 362 ms 6096 KB Output is correct
84 Correct 304 ms 6168 KB Output is correct
85 Correct 329 ms 6104 KB Output is correct
86 Correct 326 ms 6084 KB Output is correct
87 Correct 376 ms 6076 KB Output is correct
88 Correct 417 ms 6084 KB Output is correct
89 Correct 406 ms 6072 KB Output is correct
90 Correct 371 ms 6088 KB Output is correct
91 Correct 391 ms 6088 KB Output is correct
92 Correct 354 ms 6096 KB Output is correct