답안 #491038

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
491038 2021-11-30T05:07:15 Z sberens Mecho (IOI09_mecho) C++17
73 / 100
1000 ms 6488 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;
            }
        }
    }
    cout << discrete_binary_search<int>([&](int t) -> bool {
        queue<ii> bees, bear;
        vvi seenbees(n, vi(n)), seenbear(n, vi(n));
        for (auto [r, c] : hives) {
            bees.push({r, c});
            seenbees[r][c] = 1;
        }
        bear.push({mr, mc});
        seenbear[mr][mc] = 1;

        auto upd_bees = [&]() -> void {
            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 && seenbees[nr][nc] == 0 && (g[nr][nc] == 'G' || g[nr][nc] == 'M')) {
                        nextbees.push({nr, nc});
                        seenbees[nr][nc] = 1;
                    }
                }
            }
            swap(bees, nextbees);
//            bees = nextbees;
        };
        F0R(_, t) {
            upd_bees();
            if (bees.empty()) break;
        }
        while (!bees.empty()) {
            F0R(_, s) {
                queue<ii> nextbear;
                while (!bear.empty()) {
                    assert(bear.size() < n * n);
                    auto [r, c] = bear.front(); bear.pop();
                    if (seenbees[r][c] == 1) continue;
                    if (r == dr && c == dc) return false;
                    for (auto [nr, nc] : adjc(r, c)) {
                        if (0 <= nr && nr < n && 0 <= nc && nc < n && seenbear[nr][nc] == 0 && (g[nr][nc] == 'G' || g[nr][nc] == 'D') && seenbees[nr][nc] == 0) {
                            nextbear.push({nr, nc});
                            seenbear[nr][nc] = 1;
                        }
                    }
                }
                swap(bear, nextbear);
                if (bear.empty()) return true;
//                bear = nextbear;
            }
            upd_bees();
        }
        return true;
    }, 0, n * n) - 1 << '\n';

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 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 Execution timed out 1086 ms 6152 KB Time limit exceeded
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 3 ms 332 KB Output is correct
13 Correct 3 ms 332 KB Output is correct
14 Correct 5 ms 356 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 2 ms 204 KB Output is correct
22 Correct 3 ms 332 KB Output is correct
23 Correct 3 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 4 ms 352 KB Output is correct
26 Correct 3 ms 332 KB Output is correct
27 Correct 4 ms 332 KB Output is correct
28 Correct 3 ms 332 KB Output is correct
29 Correct 4 ms 332 KB Output is correct
30 Correct 4 ms 332 KB Output is correct
31 Correct 5 ms 332 KB Output is correct
32 Correct 5 ms 368 KB Output is correct
33 Correct 168 ms 1432 KB Output is correct
34 Correct 138 ms 1420 KB Output is correct
35 Correct 160 ms 1412 KB Output is correct
36 Correct 214 ms 1764 KB Output is correct
37 Correct 196 ms 1872 KB Output is correct
38 Correct 243 ms 1740 KB Output is correct
39 Correct 271 ms 2108 KB Output is correct
40 Correct 219 ms 2148 KB Output is correct
41 Correct 305 ms 2132 KB Output is correct
42 Correct 341 ms 2616 KB Output is correct
43 Correct 309 ms 2616 KB Output is correct
44 Correct 360 ms 2560 KB Output is correct
45 Correct 416 ms 3168 KB Output is correct
46 Correct 358 ms 3020 KB Output is correct
47 Correct 483 ms 3024 KB Output is correct
48 Correct 520 ms 3576 KB Output is correct
49 Correct 447 ms 3652 KB Output is correct
50 Correct 546 ms 3540 KB Output is correct
51 Correct 605 ms 4180 KB Output is correct
52 Correct 550 ms 4172 KB Output is correct
53 Correct 622 ms 4100 KB Output is correct
54 Correct 739 ms 4776 KB Output is correct
55 Correct 596 ms 4736 KB Output is correct
56 Correct 756 ms 4704 KB Output is correct
57 Correct 816 ms 5436 KB Output is correct
58 Correct 715 ms 5528 KB Output is correct
59 Correct 833 ms 5344 KB Output is correct
60 Correct 962 ms 6060 KB Output is correct
61 Correct 907 ms 6200 KB Output is correct
62 Correct 997 ms 6084 KB Output is correct
63 Correct 979 ms 6144 KB Output is correct
64 Correct 905 ms 6032 KB Output is correct
65 Correct 918 ms 6140 KB Output is correct
66 Correct 928 ms 6036 KB Output is correct
67 Correct 952 ms 6044 KB Output is correct
68 Correct 919 ms 6068 KB Output is correct
69 Correct 740 ms 6064 KB Output is correct
70 Correct 865 ms 6068 KB Output is correct
71 Correct 806 ms 6068 KB Output is correct
72 Execution timed out 1010 ms 6072 KB Time limit exceeded
73 Execution timed out 1022 ms 6488 KB Time limit exceeded
74 Execution timed out 1098 ms 6384 KB Time limit exceeded
75 Execution timed out 1087 ms 6476 KB Time limit exceeded
76 Execution timed out 1099 ms 6344 KB Time limit exceeded
77 Execution timed out 1082 ms 6352 KB Time limit exceeded
78 Execution timed out 1093 ms 6400 KB Time limit exceeded
79 Execution timed out 1097 ms 6220 KB Time limit exceeded
80 Execution timed out 1097 ms 6416 KB Time limit exceeded
81 Execution timed out 1091 ms 6416 KB Time limit exceeded
82 Execution timed out 1096 ms 6248 KB Time limit exceeded
83 Execution timed out 1090 ms 6316 KB Time limit exceeded
84 Execution timed out 1076 ms 6360 KB Time limit exceeded
85 Execution timed out 1090 ms 6356 KB Time limit exceeded
86 Execution timed out 1088 ms 6384 KB Time limit exceeded
87 Execution timed out 1080 ms 6268 KB Time limit exceeded
88 Execution timed out 1055 ms 6332 KB Time limit exceeded
89 Execution timed out 1088 ms 6288 KB Time limit exceeded
90 Execution timed out 1093 ms 6220 KB Time limit exceeded
91 Execution timed out 1091 ms 6340 KB Time limit exceeded
92 Execution timed out 1091 ms 6220 KB Time limit exceeded