Submission #443333

# Submission time Handle Problem Language Result Execution time Memory
443333 2021-07-10T11:14:23 Z MohammadAghil Mecho (IOI09_mecho) C++14
95 / 100
189 ms 4492 KB
#include <bits/stdc++.h> 
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i,l,r) for(int i = (l); i < r; i++)
#define per(i,r,l) for(int i = (r); i >= l; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define ss second 
 
const ll mod = 998244353, maxn = 800 + 5, inf = 1e9 + 5;

struct node{
    int x, y, rem, eq;
    node(){}
    node(int x, int y, int r, int e):
        x(x), y(y), rem(r), eq(e){}
};

int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool bad[maxn][maxn], vis[maxn][maxn];
int dist_bee[maxn][maxn];

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, s; cin >> n >> s;
    pair<int, int> st, en;
    deque<pair<int, int>> hol;  
    rep(i,0,n){
        rep(j,0,n){
            dist_bee[i][j] = inf;
            char e; cin >> e;
            if(e == 'M'){
                st = {i, j};
            }
            if(e == 'D'){
                en = {i, j};
            }
            if(e == 'H'){
                hol.push_back({i, j});
                dist_bee[i][j] = 0;
            }
            if(e == 'H' || e == 'T') bad[i][j] = 1;
        }
    }
    while(sz(hol)){
        pair<int, int> p = hol.front(); hol.pop_front();
        rep(d,0,4){
            int i = p.ff + dx[d], j = p.ss + dy[d];
            if(i < 0 || i >= n || j < 0 || j >= n || dist_bee[i][j] != inf || bad[i][j]) continue;
            dist_bee[i][j] = dist_bee[p.ff][p.ss] + 1;
            hol.pb({i, j});
        }
    }
    dist_bee[en.ff][en.ss] = inf;
    auto chk = [&](int t) -> bool{
        if(dist_bee[st.ff][st.ss] <= t) return 0; 
        memset(vis, 0, sizeof vis);
        deque<node> q[2];
        bool act = 0;
        q[act].push_back(node(st.ff, st.ss, s, t + 1));
        vis[st.ff][st.ss] = 1;
        while(1){
            if(!sz(q[act])){
                act ^= 1;
            }
            if(!sz(q[act])) break;
            node p = q[act].front(); q[act].pop_front();
            rep(d,0,4){
                int i = p.x + dx[d], j = p.y + dy[d];
                if(i < 0 || i >= n || j < 0 || j >= n || vis[i][j] || bad[i][j] 
                    || p.eq > dist_bee[i][j] || (p.rem == 1 && p.eq == dist_bee[i][j])) continue;
                node r = node(i, j, p.rem - 1, p.eq);
                vis[i][j] = 1;
                if(!r.rem){
                    r.rem = s, r.eq++;
                    q[!act].pb(r);
                }else{
                    q[act].pb(r);
                }
            }
        }
        return vis[en.ff][en.ss];
    };
    int l = -1, r = 1e7;
    while(l + 1 < r){
        int mid = (l + r)/2;
        if(chk(mid)) l = mid;
        else r = mid;
    }
    cout << l;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 844 KB Output is correct
2 Correct 1 ms 972 KB Output is correct
3 Correct 1 ms 844 KB Output is correct
4 Correct 1 ms 844 KB Output is correct
5 Correct 1 ms 972 KB Output is correct
6 Correct 1 ms 972 KB Output is correct
7 Correct 114 ms 4192 KB Output is correct
8 Correct 1 ms 972 KB Output is correct
9 Correct 1 ms 972 KB Output is correct
10 Correct 1 ms 972 KB Output is correct
11 Correct 1 ms 972 KB Output is correct
12 Incorrect 1 ms 1100 KB Output isn't correct
13 Correct 1 ms 1100 KB Output is correct
14 Correct 1 ms 1100 KB Output is correct
15 Correct 1 ms 972 KB Output is correct
16 Correct 1 ms 972 KB Output is correct
17 Correct 1 ms 972 KB Output is correct
18 Correct 1 ms 972 KB Output is correct
19 Correct 1 ms 972 KB Output is correct
20 Correct 1 ms 972 KB Output is correct
21 Correct 1 ms 1100 KB Output is correct
22 Correct 1 ms 1100 KB Output is correct
23 Correct 1 ms 1100 KB Output is correct
24 Correct 1 ms 1100 KB Output is correct
25 Correct 1 ms 1100 KB Output is correct
26 Correct 1 ms 1100 KB Output is correct
27 Correct 1 ms 1100 KB Output is correct
28 Correct 1 ms 1100 KB Output is correct
29 Correct 1 ms 1100 KB Output is correct
30 Correct 1 ms 1228 KB Output is correct
31 Correct 1 ms 1228 KB Output is correct
32 Correct 1 ms 1228 KB Output is correct
33 Correct 6 ms 2340 KB Output is correct
34 Correct 6 ms 2252 KB Output is correct
35 Correct 28 ms 2332 KB Output is correct
36 Correct 6 ms 2508 KB Output is correct
37 Correct 6 ms 2508 KB Output is correct
38 Correct 36 ms 2512 KB Output is correct
39 Correct 10 ms 2636 KB Output is correct
40 Correct 8 ms 2636 KB Output is correct
41 Correct 44 ms 2712 KB Output is correct
42 Correct 10 ms 2820 KB Output is correct
43 Correct 10 ms 2808 KB Output is correct
44 Correct 57 ms 2924 KB Output is correct
45 Correct 13 ms 3020 KB Output is correct
46 Correct 14 ms 3128 KB Output is correct
47 Correct 69 ms 3112 KB Output is correct
48 Correct 13 ms 3276 KB Output is correct
49 Correct 13 ms 3324 KB Output is correct
50 Correct 80 ms 3312 KB Output is correct
51 Correct 15 ms 3404 KB Output is correct
52 Correct 16 ms 3404 KB Output is correct
53 Correct 100 ms 3404 KB Output is correct
54 Correct 20 ms 3660 KB Output is correct
55 Correct 21 ms 3696 KB Output is correct
56 Correct 140 ms 3700 KB Output is correct
57 Correct 27 ms 3788 KB Output is correct
58 Correct 25 ms 3896 KB Output is correct
59 Correct 140 ms 3896 KB Output is correct
60 Correct 20 ms 4044 KB Output is correct
61 Correct 19 ms 4108 KB Output is correct
62 Correct 172 ms 4104 KB Output is correct
63 Correct 126 ms 4216 KB Output is correct
64 Correct 189 ms 4084 KB Output is correct
65 Correct 186 ms 4092 KB Output is correct
66 Correct 188 ms 4076 KB Output is correct
67 Correct 138 ms 4096 KB Output is correct
68 Correct 57 ms 3996 KB Output is correct
69 Correct 61 ms 4112 KB Output is correct
70 Correct 44 ms 3996 KB Output is correct
71 Correct 49 ms 4040 KB Output is correct
72 Correct 45 ms 4120 KB Output is correct
73 Correct 39 ms 4292 KB Output is correct
74 Correct 69 ms 4300 KB Output is correct
75 Correct 75 ms 4332 KB Output is correct
76 Correct 72 ms 4492 KB Output is correct
77 Correct 73 ms 4300 KB Output is correct
78 Correct 91 ms 4344 KB Output is correct
79 Correct 62 ms 4292 KB Output is correct
80 Correct 72 ms 4336 KB Output is correct
81 Correct 90 ms 4344 KB Output is correct
82 Correct 79 ms 4408 KB Output is correct
83 Correct 96 ms 4300 KB Output is correct
84 Correct 83 ms 4300 KB Output is correct
85 Correct 84 ms 4292 KB Output is correct
86 Correct 104 ms 4300 KB Output is correct
87 Correct 97 ms 4188 KB Output is correct
88 Correct 95 ms 4248 KB Output is correct
89 Correct 97 ms 4184 KB Output is correct
90 Correct 130 ms 4128 KB Output is correct
91 Correct 110 ms 4244 KB Output is correct
92 Correct 107 ms 4140 KB Output is correct