답안 #647667

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
647667 2022-10-03T14:11:34 Z ghostwriter Mecho (IOI09_mecho) C++14
100 / 100
590 ms 21856 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
#define int long long
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int N = 801;
const vpi dxy = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int n, s, d[N][N], d1[N][N];
char a[N][N], a1[N][N];
bool check(int tm) {
    FOR(i, 1, n)
    FOR(j, 1, n)
        a1[i][j] = a[i][j];
    vpi index;
    pi home;
    FOR(i, 1, n)
    FOR(j, 1, n)
        if (a[i][j] == 'H')
            index.pb({i, j});
    FOR(i, 1, tm) {
        vpi index1;
        EACH(j, index) {
            int x = j.st, y = j.nd;
            EACH(z, dxy) {
                int x1 = x + z.st, y1 = y + z.nd;
                if (x1 < 1 || x1 > n || y1 < 1 || y1 > n) continue;
                if (a1[x1][y1] != 'H' && a1[x1][y1] != 'D' && a1[x1][y1] != 'T') {
                    a1[x1][y1] = 'H';
                    index1.pb({x1, y1});
                }
            }
        }
        swap(index, index1);
    }
    pi ph = {0, 0}, ps = {0, 0};
    FOR(i, 1, n)
    FOR(j, 1, n)
        if (a1[i][j] == 'D') ph = {i, j};
        else if (a1[i][j] == 'M') ps = {i, j};
    if (!ph.st || !ps.st) return 0;
    memset(d, -1, sizeof d);
    queue<pi> q;
    FOR(i, 1, n)
    FOR(j, 1, n)
        if (a1[i][j] == 'H') {
            d[i][j] = 0;
            q.push({i, j});
        }
    WHILE(!q.empty()) {
        int x = q.ft().st, y = q.ft().nd;
        q.pop();
        EACH(i, dxy) {
            int x1 = x + i.st, y1 = y + i.nd;
            if (x1 < 1 || x1 > n || y1 < 1 || y1 > n || a1[x1][y1] == 'T' || a1[x1][y1] == 'D' || d[x1][y1] != -1) continue;
            d[x1][y1] = d[x][y] + 1;
            q.push({x1, y1});
        }
    }
    memset(d1, -1, sizeof d1);
    d1[ps.st][ps.nd] = 0;
    q.push(ps);
    WHILE(!q.empty()) {
        int x = q.ft().st, y = q.ft().nd;
        q.pop();
        EACH(i, dxy) {
            int x1 = x + i.st, y1 = y + i.nd;
            if (x1 < 1 || x1 > n || y1 < 1 || y1 > n || a1[x1][y1] == 'T' || d1[x1][y1] != -1 || (d[x1][y1] != -1 && (d1[x][y] + 1) / s >= d[x1][y1])) continue;
            d1[x1][y1] = d1[x][y] + 1;
            q.push({x1, y1});
        }
    }
    return d1[ph.st][ph.nd] != -1? 1 : 0;
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
    cin >> n >> s;
    FOR(i, 1, n)
    FOR(j, 1, n)
        cin >> a[i][j];
    int l = 0, r = n * n, ans = -1;
    WHILE(l <= r) {
        int mid = l + (r - l) / 2;
        if (check(mid)) {
            ans = mid;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    cout << ans;
    return 0;
}
/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message

mecho.cpp: In function 'bool check(long long int)':
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:57:5: note: in expansion of macro 'FOR'
   57 |     FOR(i, 1, n)
      |     ^~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:58:5: note: in expansion of macro 'FOR'
   58 |     FOR(j, 1, n)
      |     ^~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:62:5: note: in expansion of macro 'FOR'
   62 |     FOR(i, 1, n)
      |     ^~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:63:5: note: in expansion of macro 'FOR'
   63 |     FOR(j, 1, n)
      |     ^~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:66:5: note: in expansion of macro 'FOR'
   66 |     FOR(i, 1, tm) {
      |     ^~~
mecho.cpp:37:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   37 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mecho.cpp:68:9: note: in expansion of macro 'EACH'
   68 |         EACH(j, index) {
      |         ^~~~
mecho.cpp:37:31: warning: unnecessary parentheses in declaration of 'z' [-Wparentheses]
   37 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mecho.cpp:70:13: note: in expansion of macro 'EACH'
   70 |             EACH(z, dxy) {
      |             ^~~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:82:5: note: in expansion of macro 'FOR'
   82 |     FOR(i, 1, n)
      |     ^~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:83:5: note: in expansion of macro 'FOR'
   83 |     FOR(j, 1, n)
      |     ^~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:89:5: note: in expansion of macro 'FOR'
   89 |     FOR(i, 1, n)
      |     ^~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:90:5: note: in expansion of macro 'FOR'
   90 |     FOR(j, 1, n)
      |     ^~~
mecho.cpp:37:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   37 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mecho.cpp:98:9: note: in expansion of macro 'EACH'
   98 |         EACH(i, dxy) {
      |         ^~~~
mecho.cpp:37:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   37 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mecho.cpp:111:9: note: in expansion of macro 'EACH'
  111 |         EACH(i, dxy) {
      |         ^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:125:5: note: in expansion of macro 'FOR'
  125 |     FOR(i, 1, n)
      |     ^~~
mecho.cpp:33:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   33 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mecho.cpp:126:5: note: in expansion of macro 'FOR'
  126 |     FOR(j, 1, n)
      |     ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 10324 KB Output is correct
2 Correct 8 ms 10324 KB Output is correct
3 Correct 5 ms 10324 KB Output is correct
4 Correct 7 ms 10324 KB Output is correct
5 Correct 6 ms 10324 KB Output is correct
6 Correct 8 ms 10396 KB Output is correct
7 Correct 410 ms 18764 KB Output is correct
8 Correct 8 ms 10324 KB Output is correct
9 Correct 8 ms 10320 KB Output is correct
10 Correct 8 ms 10324 KB Output is correct
11 Correct 7 ms 10324 KB Output is correct
12 Correct 9 ms 10464 KB Output is correct
13 Correct 6 ms 10452 KB Output is correct
14 Correct 10 ms 10452 KB Output is correct
15 Correct 11 ms 10324 KB Output is correct
16 Correct 8 ms 10324 KB Output is correct
17 Correct 11 ms 10324 KB Output is correct
18 Correct 9 ms 10424 KB Output is correct
19 Correct 12 ms 10432 KB Output is correct
20 Correct 7 ms 10324 KB Output is correct
21 Correct 13 ms 10452 KB Output is correct
22 Correct 12 ms 10452 KB Output is correct
23 Correct 13 ms 10476 KB Output is correct
24 Correct 10 ms 10384 KB Output is correct
25 Correct 14 ms 10504 KB Output is correct
26 Correct 16 ms 10452 KB Output is correct
27 Correct 14 ms 10504 KB Output is correct
28 Correct 17 ms 10524 KB Output is correct
29 Correct 15 ms 10528 KB Output is correct
30 Correct 16 ms 10532 KB Output is correct
31 Correct 14 ms 10452 KB Output is correct
32 Correct 16 ms 10488 KB Output is correct
33 Correct 90 ms 11872 KB Output is correct
34 Correct 74 ms 11808 KB Output is correct
35 Correct 64 ms 11092 KB Output is correct
36 Correct 91 ms 12312 KB Output is correct
37 Correct 101 ms 12212 KB Output is correct
38 Correct 97 ms 11488 KB Output is correct
39 Correct 116 ms 12736 KB Output is correct
40 Correct 119 ms 12712 KB Output is correct
41 Correct 111 ms 11704 KB Output is correct
42 Correct 165 ms 13132 KB Output is correct
43 Correct 156 ms 13232 KB Output is correct
44 Correct 129 ms 12104 KB Output is correct
45 Correct 193 ms 13640 KB Output is correct
46 Correct 166 ms 13692 KB Output is correct
47 Correct 196 ms 11468 KB Output is correct
48 Correct 212 ms 14200 KB Output is correct
49 Correct 203 ms 14264 KB Output is correct
50 Correct 199 ms 11692 KB Output is correct
51 Correct 253 ms 14972 KB Output is correct
52 Correct 231 ms 14812 KB Output is correct
53 Correct 248 ms 12020 KB Output is correct
54 Correct 314 ms 15608 KB Output is correct
55 Correct 306 ms 15556 KB Output is correct
56 Correct 262 ms 12356 KB Output is correct
57 Correct 359 ms 16116 KB Output is correct
58 Correct 337 ms 16304 KB Output is correct
59 Correct 378 ms 12688 KB Output is correct
60 Correct 410 ms 17032 KB Output is correct
61 Correct 368 ms 16888 KB Output is correct
62 Correct 347 ms 13040 KB Output is correct
63 Correct 442 ms 13536 KB Output is correct
64 Correct 590 ms 17864 KB Output is correct
65 Correct 499 ms 17872 KB Output is correct
66 Correct 473 ms 14220 KB Output is correct
67 Correct 471 ms 17856 KB Output is correct
68 Correct 391 ms 15532 KB Output is correct
69 Correct 431 ms 16824 KB Output is correct
70 Correct 390 ms 16732 KB Output is correct
71 Correct 445 ms 16880 KB Output is correct
72 Correct 379 ms 17628 KB Output is correct
73 Correct 250 ms 13376 KB Output is correct
74 Correct 426 ms 20940 KB Output is correct
75 Correct 500 ms 20700 KB Output is correct
76 Correct 408 ms 21212 KB Output is correct
77 Correct 448 ms 20760 KB Output is correct
78 Correct 442 ms 21172 KB Output is correct
79 Correct 417 ms 21856 KB Output is correct
80 Correct 390 ms 21420 KB Output is correct
81 Correct 395 ms 21008 KB Output is correct
82 Correct 456 ms 21692 KB Output is correct
83 Correct 413 ms 19072 KB Output is correct
84 Correct 474 ms 19836 KB Output is correct
85 Correct 459 ms 19136 KB Output is correct
86 Correct 426 ms 21596 KB Output is correct
87 Correct 446 ms 19412 KB Output is correct
88 Correct 409 ms 19624 KB Output is correct
89 Correct 434 ms 19300 KB Output is correct
90 Correct 436 ms 19192 KB Output is correct
91 Correct 437 ms 19284 KB Output is correct
92 Correct 453 ms 19260 KB Output is correct