답안 #571022

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
571022 2022-06-01T05:01:04 Z f4t4nt Mecho (IOI09_mecho) C++
84 / 100
466 ms 16356 KB
#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ch = char;
using str = string;

#define pb push_back
#define elif else if
#define sz(C) (ll) C.size()
#define mp make_pair
#define mt make_tuple
#define flip(C) reverse(C.begin(), C.end())
#define ssort(C) sort(C.begin(), C.end())
#define rsort(C) sort(C.begin(), C.end(), greater<>())

#define FOR(x, e) for(ll  x = 0; x < (ll) e; x++)
#define FORR(x, e) for(ll x = (ll) e - 1; x >= 0; x--)
#define FOB(x, b, e) for(auto x = b; x < e; x++)
#define FOI(x, e, i) for(ll x = 0; x < (ll) e; x += (ll) i)
#define FOBI(x, b, e, i) for(ll x = (ll) b; x < (ll) e; x += (ll) i)
#define FORE(x, C) for(auto &x : C)

#ifdef LOCAL
#include "tester.cpp"
#define main test_main
extern istringstream fin;
extern ostringstream fout;
string test_file_name = "tests";
#define cin fin
#define cout fout
#endif

vector<int> di { -1, 0, 1, 0};
vector<int> dj { 0, -1, 0, 1};

void fill_b(ll &n, vector<vector<char>> &g, queue<pair<ll, ll>> &bfs_b, vector<vector<ll>> &d_b) {
    while(sz(bfs_b)) {
        ll i0 = bfs_b.front().first, j0 = bfs_b.front().second;
        bfs_b.pop();
        FOR(k, 4) {
            ll i1 = i0 + di[k], j1 = j0 + dj[k];
            if(i1 < 0 || i1 >= n || j1 < 0 || j1 >= n) continue;
            if(g[i1][j1] == 'T' || g[i1][j1] == 'D') continue;
            if(d_b[i1][j1] != 1e18) continue;
            d_b[i1][j1] = d_b[i0][j0] + 1;
            bfs_b.push({i1, j1});
        }
    }
}

bool valid(ll &mid, ll &n, ll &s, vector<vector<ll>> &d_b, vector<vector<char>> &g, queue<pair<ll, ll>> bfs_m, vector<vector<ll>> d_m) {
    while(sz(bfs_m)) {
        ll i0 = bfs_m.front().first, j0 = bfs_m.front().second, d = d_m[i0][j0] + 1;
        bfs_m.pop();
        FOR(k, 4) {
            ll i1 = i0 + di[k], j1 = j0 + dj[k];
            if(i1 < 0 || i1 >= n || j1 < 0 || j1 >= n) continue;
            if(g[i1][j1] == 'T') continue;
            if(d_m[i1][j1] != 1e18) continue;
            if(d_b[i1][j1] <= mid + d / s) continue;
            if(g[i1][j1] == 'D' ||
                (i1 > 0 && g[i1 - 1][j1] == 'D') ||
                (i1 < n - 1 && g[i1 + 1][j1] == 'D') ||
                (j1 > 0 && g[i1][j1 - 1] == 'D') ||
                (j1 < n - 1 && g[i1][j1 + 1] == 'D')) return true;
            d_m[i1][j1] = d;
            bfs_m.push({i1, j1});
        }
    }
    return false;
}

int main() {
    ll n, s, lo = 0, hi = 1e18;
    cin >> n >> s;
    vector<vector<char>> g(n, vector<char>(n));
    vector<vector<ll>> d_b(n, vector<ll>(n, 1e18)), d_m(n, vector<ll>(n, 1e18));
    queue<pair<ll, ll>> bfs_b, bfs_m;
    FOR(i, n) FOR(j, n) {
        cin >> g[i][j];
        if(g[i][j] == 'H') {
            bfs_b.push({i, j});
            d_b[i][j] = 0;
        }
        elif(g[i][j] == 'M') {
            bfs_m.push({i, j});
            d_m[i][j] = 0;
        }
    }
    fill_b(n, g, bfs_b, d_b);
    while(hi - lo > 0) {
        ll mid = lo + (hi - lo + 1) / 2;
        if(valid(mid, n, s, d_b, g, bfs_m, d_m)) lo = mid;
        else hi = mid - 1;
    }
    if(valid(lo, n, s, d_b, g, bfs_m, d_m)) cout << lo << '\n';
    else cout << "-1\n";
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 256 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 318 ms 16244 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Incorrect 2 ms 340 KB Output isn't correct
13 Incorrect 2 ms 340 KB Output isn't correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 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 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 33 ms 3340 KB Output is correct
34 Correct 31 ms 3340 KB Output is correct
35 Correct 72 ms 3356 KB Output is correct
36 Correct 49 ms 4460 KB Output is correct
37 Correct 46 ms 4448 KB Output is correct
38 Correct 74 ms 4272 KB Output is correct
39 Correct 74 ms 5448 KB Output is correct
40 Correct 57 ms 5472 KB Output is correct
41 Correct 96 ms 5456 KB Output is correct
42 Correct 73 ms 6576 KB Output is correct
43 Correct 69 ms 6644 KB Output is correct
44 Correct 151 ms 6648 KB Output is correct
45 Correct 81 ms 7848 KB Output is correct
46 Correct 97 ms 7908 KB Output is correct
47 Correct 197 ms 7916 KB Output is correct
48 Correct 128 ms 9232 KB Output is correct
49 Correct 128 ms 9252 KB Output is correct
50 Correct 215 ms 9448 KB Output is correct
51 Correct 141 ms 10844 KB Output is correct
52 Correct 150 ms 10856 KB Output is correct
53 Correct 271 ms 10952 KB Output is correct
54 Correct 164 ms 12528 KB Output is correct
55 Correct 167 ms 12496 KB Output is correct
56 Correct 333 ms 12476 KB Output is correct
57 Correct 211 ms 14276 KB Output is correct
58 Correct 189 ms 14292 KB Output is correct
59 Correct 312 ms 14328 KB Output is correct
60 Correct 246 ms 16252 KB Output is correct
61 Correct 231 ms 16304 KB Output is correct
62 Correct 402 ms 16164 KB Output is correct
63 Correct 359 ms 16228 KB Output is correct
64 Correct 466 ms 16156 KB Output is correct
65 Correct 465 ms 16212 KB Output is correct
66 Correct 391 ms 16144 KB Output is correct
67 Correct 344 ms 16356 KB Output is correct
68 Correct 282 ms 16168 KB Output is correct
69 Correct 295 ms 16244 KB Output is correct
70 Correct 234 ms 16212 KB Output is correct
71 Correct 253 ms 16196 KB Output is correct
72 Incorrect 275 ms 16204 KB Output isn't correct
73 Incorrect 301 ms 16212 KB Output isn't correct
74 Correct 279 ms 16204 KB Output is correct
75 Correct 310 ms 16352 KB Output is correct
76 Correct 286 ms 16192 KB Output is correct
77 Correct 320 ms 16284 KB Output is correct
78 Correct 371 ms 16216 KB Output is correct
79 Correct 291 ms 16168 KB Output is correct
80 Correct 343 ms 16200 KB Output is correct
81 Correct 304 ms 16336 KB Output is correct
82 Correct 276 ms 16284 KB Output is correct
83 Correct 369 ms 16184 KB Output is correct
84 Correct 336 ms 16216 KB Output is correct
85 Correct 404 ms 16288 KB Output is correct
86 Correct 343 ms 16244 KB Output is correct
87 Correct 393 ms 16200 KB Output is correct
88 Correct 323 ms 16252 KB Output is correct
89 Correct 423 ms 16192 KB Output is correct
90 Correct 348 ms 16284 KB Output is correct
91 Correct 351 ms 16224 KB Output is correct
92 Correct 340 ms 16252 KB Output is correct