Submission #1117295

# Submission time Handle Problem Language Result Execution time Memory
1117295 2024-11-23T08:52:01 Z DON_F Mecho (IOI09_mecho) C++14
100 / 100
166 ms 6904 KB
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define all(x) x.begin(), x.end()
#define L(i, b, e) for (int i = b; i < e; ++i)
#define R(i, b, e) for (int i = b; i >= e; --i)
#define pb emplace_back
#define vi vector<int>
#define sz(x) ((int) x.size())
const int N = 8e2 + 7, Mx = 1e9 + 9;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int n, s;
string a[N];
int dis1[N][N], dis2[N][N];
pair<int, int> st, d;
bool in(int x, int y){
    return (x >= 0 && y >= 0 && x < n && y < n && a[x][y] != 'T');
}
int work(int m){
    L(i, 0, n)L(j, 0, n)dis2[i][j] = Mx;
    queue<tuple<int, int, int>> q;
    q.push({st.first, st.second, s});
    dis2[st.first][st.second] = m;
    if (dis2[st.first][st.second] >= dis1[st.first][st.second])return false;
    while (!q.empty()){
        auto [x, y, p] = q.front();
        q.pop();
        if (p == 0 && dis2[x][y] >= dis1[x][y])continue;
        if (p == 0)p = s;
        L(i, 0, 4){
            int nx = x + dx[i], ny = y + dy[i];
            if (in(nx, ny) && dis2[x][y] + (p == s) <= dis1[nx][ny] && dis2[nx][ny] == Mx && a[nx][ny] != 'H'){
                q.push({nx, ny, p - 1});
                dis2[nx][ny] = dis2[x][y] + (p == s);
            }
        }
    }
    return (dis2[d.first][d.second] != Mx);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> s;
    L(i, 0, n){
        cin >> a[i];
    }
    L(i, 0, n)L(j, 0, n)dis1[i][j] = dis2[i][j] = Mx;
    queue<pair<int, int>> q;
    L(i, 0, n){
        L(j, 0, n){
            if (a[i][j] == 'H'){
                q.push({i, j});
                dis1[i][j] = 0;
            }else if (a[i][j] == 'M'){
                st = {i, j};
            }else if (a[i][j] == 'D'){
                d = {i, j};
            }
        }
    }
    while (!q.empty()){
        auto [x, y] = q.front();
        q.pop();
        L(i, 0, 4){
            int nx = x + dx[i], ny = y + dy[i];
            if (in(nx, ny) && a[nx][ny] != 'D' && dis1[nx][ny] == Mx){
                q.push({nx, ny});
                dis1[nx][ny] = dis1[x][y] + 1;
            }
        }
    }
    int lo = 0, hi = n * n, ans = -1;
    while (lo <= hi){
        int mid = lo + (hi - lo) / 2;
        if (work(mid)){
            ans = mid;
            lo = mid + 1;
        }else{
            hi = mid - 1;
        }
    }
    cout << ans;
}



Compilation message

mecho.cpp: In function 'int work(int)':
mecho.cpp:26:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   26 |         auto [x, y, p] = q.front();
      |              ^
mecho.cpp: In function 'int main()':
mecho.cpp:63:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   63 |         auto [x, y] = q.front();
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 58 ms 6328 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 720 KB Output is correct
13 Correct 1 ms 592 KB Output is correct
14 Correct 1 ms 2640 KB Output is correct
15 Correct 1 ms 2384 KB Output is correct
16 Correct 1 ms 592 KB Output is correct
17 Correct 1 ms 592 KB Output is correct
18 Correct 1 ms 760 KB Output is correct
19 Correct 1 ms 592 KB Output is correct
20 Correct 1 ms 592 KB Output is correct
21 Correct 1 ms 592 KB Output is correct
22 Correct 1 ms 760 KB Output is correct
23 Correct 1 ms 592 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 2 ms 848 KB Output is correct
26 Correct 1 ms 848 KB Output is correct
27 Correct 1 ms 848 KB Output is correct
28 Correct 1 ms 848 KB Output is correct
29 Correct 1 ms 848 KB Output is correct
30 Correct 1 ms 848 KB Output is correct
31 Correct 1 ms 848 KB Output is correct
32 Correct 1 ms 848 KB Output is correct
33 Correct 5 ms 2640 KB Output is correct
34 Correct 5 ms 2640 KB Output is correct
35 Correct 16 ms 2640 KB Output is correct
36 Correct 6 ms 3320 KB Output is correct
37 Correct 6 ms 3196 KB Output is correct
38 Correct 20 ms 3196 KB Output is correct
39 Correct 7 ms 3408 KB Output is correct
40 Correct 7 ms 3408 KB Output is correct
41 Correct 27 ms 3556 KB Output is correct
42 Correct 8 ms 3832 KB Output is correct
43 Correct 10 ms 3828 KB Output is correct
44 Correct 34 ms 4088 KB Output is correct
45 Correct 10 ms 4176 KB Output is correct
46 Correct 10 ms 4176 KB Output is correct
47 Correct 37 ms 4276 KB Output is correct
48 Correct 10 ms 4432 KB Output is correct
49 Correct 10 ms 5200 KB Output is correct
50 Correct 43 ms 4676 KB Output is correct
51 Correct 13 ms 4944 KB Output is correct
52 Correct 13 ms 4944 KB Output is correct
53 Correct 56 ms 5048 KB Output is correct
54 Correct 14 ms 5432 KB Output is correct
55 Correct 14 ms 5200 KB Output is correct
56 Correct 65 ms 5456 KB Output is correct
57 Correct 19 ms 5712 KB Output is correct
58 Correct 16 ms 5996 KB Output is correct
59 Correct 75 ms 5816 KB Output is correct
60 Correct 17 ms 6224 KB Output is correct
61 Correct 20 ms 5968 KB Output is correct
62 Correct 84 ms 6224 KB Output is correct
63 Correct 86 ms 5968 KB Output is correct
64 Correct 166 ms 6220 KB Output is correct
65 Correct 112 ms 6232 KB Output is correct
66 Correct 90 ms 5968 KB Output is correct
67 Correct 86 ms 5968 KB Output is correct
68 Correct 36 ms 6224 KB Output is correct
69 Correct 44 ms 6224 KB Output is correct
70 Correct 28 ms 6224 KB Output is correct
71 Correct 32 ms 6224 KB Output is correct
72 Correct 26 ms 6224 KB Output is correct
73 Correct 24 ms 6520 KB Output is correct
74 Correct 41 ms 6480 KB Output is correct
75 Correct 53 ms 6480 KB Output is correct
76 Correct 43 ms 6480 KB Output is correct
77 Correct 50 ms 6480 KB Output is correct
78 Correct 52 ms 6224 KB Output is correct
79 Correct 35 ms 6904 KB Output is correct
80 Correct 45 ms 6224 KB Output is correct
81 Correct 47 ms 6396 KB Output is correct
82 Correct 42 ms 6224 KB Output is correct
83 Correct 62 ms 6224 KB Output is correct
84 Correct 55 ms 6224 KB Output is correct
85 Correct 55 ms 6224 KB Output is correct
86 Correct 53 ms 6224 KB Output is correct
87 Correct 53 ms 6224 KB Output is correct
88 Correct 55 ms 6224 KB Output is correct
89 Correct 52 ms 6400 KB Output is correct
90 Correct 60 ms 6736 KB Output is correct
91 Correct 59 ms 6224 KB Output is correct
92 Correct 63 ms 6224 KB Output is correct