Submission #581559

# Submission time Handle Problem Language Result Execution time Memory
581559 2022-06-22T18:14:35 Z Saeed_247 Mecho (IOI09_mecho) C++17
100 / 100
733 ms 6744 KB
#include <bits/stdc++.h>
using namespace std;
template<typename T> void dout(string name, T arg) {
    cerr << name << " = " << arg << "\e[39m" << endl;
}
template<typename T1, typename... T2> void dout(string names, T1 arg, T2... args) {
    cerr << names.substr(0, names.find(',')) << " = " << arg << " | ";
    dout(names.substr(names.find(',') + 2), args...);
}
#define debug(...) cerr << "\e[91m" << "[" << __LINE__ << ":" << __func__ << "]=> ", dout(#__VA_ARGS__,  __VA_ARGS__);

const int N = 800;
vector<string> field(N);
int n, s;

bool valid(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < n && (field[x][y] == 'G' || field[x][y] == 'M');
}

bool reached(int m, int b) {
    return m / s < b;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> n >> s;
    for (int i = 0; i < n; i++) {
        cin >> field[i];
    }

    vector<pair<int, int>> hives;
    pair<int, int> m, d;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (field[i][j] == 'M') {
                m = {i, j};
            }
            if (field[i][j] == 'H') {
                hives.push_back({i, j});
            }
            if (field[i][j] == 'D') {
                d = {i, j};
            }
        }
    }

    int dx[4] = {-1, 0, 1, 0};
    int dy[4] = {0, -1, 0, 1};

    int l = 0, r = n * n;
    while (l <= r) {
        vector<vector<bool>> bees_visited(n, vector<bool>(n));
		vector<vector<bool>> mecho_visited(n, vector<bool>(n));
		vector<vector<int>> bees_time(n, vector<int>(n));
		vector<vector<int>> mecho_time(n, vector<int>(n));
		queue<pair<int, int>> q;
        int mid = (l + r) / 2;

        for (auto [x, y] : hives) {
            q.push({x, y});
            bees_visited[x][y] = true;
        }
        while (!q.empty()) {
            auto [x, y] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                int xx = x + dx[i], yy = y + dy[i];
                if (valid(xx, yy) && !bees_visited[xx][yy]) {
                    bees_time[xx][yy] = bees_time[x][y] + 1;
                    q.push({xx, yy});
                    bees_visited[xx][yy] = true;
                }
            }
        }

        q.push(m);
        mecho_visited[m.first][m.second] = true;
        if (bees_time[m.first][m.second] <= mid) {
            q.pop();
        }
        while (!q.empty()) {
            auto [x, y] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) {
                int xx = x + dx[i], yy = y + dy[i];
                if (valid(xx, yy) && !mecho_visited[xx][yy] && reached(mecho_time[x][y] + 1, bees_time[xx][yy] - mid)) {
                    mecho_time[xx][yy] = mecho_time[x][y] + 1;
                    q.push({xx, yy});
                    mecho_visited[xx][yy] = true;
                }
            }
        }

        bool reach = false;
        for (int i = 0; i < 4; i++) {
            int xx = d.first + dx[i], yy = d.second + dy[i];
            if (valid(xx, yy) && reached(mecho_time[xx][yy], bees_time[xx][yy] - mid) && mecho_visited[xx][yy]) {
                reach = true;
            }
        }

        if (reach) {
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    cout << l - 1 << '\n';
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 637 ms 6424 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 2 ms 340 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 3 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 2 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 2 ms 408 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 46 ms 1532 KB Output is correct
34 Correct 41 ms 1556 KB Output is correct
35 Correct 80 ms 1532 KB Output is correct
36 Correct 53 ms 1876 KB Output is correct
37 Correct 71 ms 1884 KB Output is correct
38 Correct 108 ms 1876 KB Output is correct
39 Correct 67 ms 2400 KB Output is correct
40 Correct 71 ms 2280 KB Output is correct
41 Correct 162 ms 2260 KB Output is correct
42 Correct 88 ms 2712 KB Output is correct
43 Correct 112 ms 2708 KB Output is correct
44 Correct 166 ms 2644 KB Output is correct
45 Correct 107 ms 3244 KB Output is correct
46 Correct 118 ms 3208 KB Output is correct
47 Correct 227 ms 3180 KB Output is correct
48 Correct 120 ms 3740 KB Output is correct
49 Correct 133 ms 3892 KB Output is correct
50 Correct 274 ms 3744 KB Output is correct
51 Correct 150 ms 4468 KB Output is correct
52 Correct 165 ms 4312 KB Output is correct
53 Correct 328 ms 4304 KB Output is correct
54 Correct 178 ms 4948 KB Output is correct
55 Correct 195 ms 4936 KB Output is correct
56 Correct 373 ms 4932 KB Output is correct
57 Correct 200 ms 5612 KB Output is correct
58 Correct 220 ms 5628 KB Output is correct
59 Correct 491 ms 5636 KB Output is correct
60 Correct 234 ms 6396 KB Output is correct
61 Correct 263 ms 6404 KB Output is correct
62 Correct 465 ms 6320 KB Output is correct
63 Correct 578 ms 6324 KB Output is correct
64 Correct 733 ms 6396 KB Output is correct
65 Correct 605 ms 6352 KB Output is correct
66 Correct 580 ms 6388 KB Output is correct
67 Correct 558 ms 6356 KB Output is correct
68 Correct 552 ms 6356 KB Output is correct
69 Correct 522 ms 6356 KB Output is correct
70 Correct 460 ms 6472 KB Output is correct
71 Correct 572 ms 6356 KB Output is correct
72 Correct 503 ms 6356 KB Output is correct
73 Correct 568 ms 6744 KB Output is correct
74 Correct 541 ms 6612 KB Output is correct
75 Correct 626 ms 6616 KB Output is correct
76 Correct 602 ms 6732 KB Output is correct
77 Correct 562 ms 6616 KB Output is correct
78 Correct 516 ms 6580 KB Output is correct
79 Correct 539 ms 6584 KB Output is correct
80 Correct 503 ms 6588 KB Output is correct
81 Correct 533 ms 6580 KB Output is correct
82 Correct 508 ms 6704 KB Output is correct
83 Correct 570 ms 6540 KB Output is correct
84 Correct 562 ms 6568 KB Output is correct
85 Correct 597 ms 6548 KB Output is correct
86 Correct 545 ms 6660 KB Output is correct
87 Correct 607 ms 6536 KB Output is correct
88 Correct 555 ms 6484 KB Output is correct
89 Correct 588 ms 6488 KB Output is correct
90 Correct 577 ms 6488 KB Output is correct
91 Correct 553 ms 6484 KB Output is correct
92 Correct 571 ms 6484 KB Output is correct