Submission #484688

# Submission time Handle Problem Language Result Execution time Memory
484688 2021-11-05T04:22:57 Z Dirii Mecho (IOI09_mecho) C++14
40 / 100
368 ms 16144 KB
// #pragma GCC target("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define cll const ll
#define lp(a, b, c) for(ll a = b; a <= c; ++a)
#define lpd(a, b, c) for(ll a = b; a >= c; --a)
#define vec(a) vector<a>
#define pp(a, b) pair<a, b>
#define EACHCASE lpd(cs, read(), 1)
#define Fname "f"
using namespace std;

template <typename T> inline void Read(T &x){
    x = 0; char c;
    ll dau = 1;
    while(!isdigit(c = getchar())) if(c == '-') dau = -1;
    do{
        x = x * 10 + c - '0';
    } while (isdigit(c = getchar()));
    x *= dau;
}

ll read(){
    ll tmp;
    cin >> tmp;
    return tmp;
}

void giuncute(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

void OF(){
    if(fopen(Fname".inp", "r")){
        freopen(Fname".inp", "r", stdin);
        freopen(Fname".out", "w", stdout);
    } else if(fopen(Fname".in", "r")){
        freopen(Fname".in", "r", stdin);
        freopen(Fname".out", "w", stdout);
    }
}

cll mxn = 805, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
ll n, s, a[mxn][mxn], d[mxn][mxn], rest[mxn][mxn];
pp(ll, ll) st, fi;

// void bfs(ll i, ll j, ll step, const ll &ti){
//     // if(step == s) return;
//     // lp(t, 0, 3){
//     //     ll ii = i + dx[t], jj = j + dy[t];
//     //     if(a[ii][jj] == -1 || d[ii][jj]) continue;
//     //     d[ii][jj] = ti;
//     //     if(a[ii][jj] > ti) q.push({ii, jj});
//     //     dfs(ii, jj, step + 1, ti);
//     // }
//     queue<pp(ll, ll)> qq[2];
//     qq[0].push({i, j});
//     while(qq[0].size()){
//         if(++step > s) break;
//         while(qq[0].size()){
//             ll u = qq[0].front().first, v = qq[0].front().second;
//             // if(i == 4 && j == 5) cerr << u << ' ' << v << '\n';
//             qq[0].pop();
//             lp(t, 0, 3){
//                 ll nu = u + dx[t], nv = v + dy[t];
//                 if(a[nu][nv] == -1 || (d[nu][nv] != d[u][v] && d[nu][nv] != 0)) continue;
//                 d[nu][nv] = d[u][v];
//                 if(a[nu][nv] > d[nu][nv]) q.push({nu, nv});
//                 qq[1].push({nu, nv});
//             }
//         }
//         swap(qq[0], qq[1]);
//     }
// }

bool check(ll timestart){
    lp(i, 1, n) lp(j, 1, n) d[i][j] = rest[i][j] = 0;
    d[st.first][st.second] = timestart, rest[st.first][st.second] = s;
    priority_queue<pp(ll, ll)> q;
    q.push(st);
    while(q.size()){
        ll u = q.top().first, v = q.top().second;
        q.pop();
        lp(i, 0, 3){
            ll nu = u + dx[i], nv = v + dy[i];
            if(a[nu][nv] == -1) continue;
            if(d[nu][nv] == d[u][v]){
                if(rest[nu][nv] < rest[u][v] - 1){
                    rest[nu][nv] = rest[u][v] - 1;
                    q.push({nu, nv});
                } 
            } else if(!d[nu][nv]){
                if(rest[u][v] == 1){
                    if(a[nu][nv] > d[u][v] + 1){
                        d[nu][nv] = d[u][v] + 1, rest[nu][nv] = s;
                        q.push({nu, nv});
                    }
                } else{
                    if(a[nu][nv] > d[u][v]){
                        d[nu][nv] = d[u][v], rest[nu][nv] = rest[u][v] - 1;
                        q.push({nu, nv});
                    }
                }
            }
        }
    }

    // while(q.size()){
    //     ll u = q.front().first, v = q.front().second;
    //     q.pop();
    //     lp(i, 0, 3){
    //         ll nu = u + dx[i], nv = v + dy[i];
    //         if(a[nu][nv] == -1 || (d[nu][nv] != 0 && d[nu][nv] != d[u][v] + 1)) continue;
    //         d[nu][nv] = d[u][v] + 1;
    //         // if(u == 4 && v == 4) cerr << nu << ' ' << nv << ' ' << d[nu][nv] << '\n';
    //         if(a[nu][nv] > d[nu][nv]) q.push({nu, nv});
    //         bfs(nu, nv, 1, d[nu][nv]);
    //     }   
    // }
    // lp(i, 1, n){
    //     lp(j, 1, n) cerr << d[i][j] << ' ';
    //     cerr << '\n';
    // }
    if(!d[fi.first][fi.second]) 
        return 0;
    return 1;
}

int main(){
    giuncute();
    #ifndef ONLINE_JUDGE
    OF();
    #endif
    memset(a, -1, sizeof a);
    memset(d, -1, sizeof d);
    cin >> n >> s;
    queue<pp(ll, ll)> q;
    lp(i, 1, n) lp(j, 1, n){
        char c;
        cin >> c;
        a[i][j] = 0;
        if(c == 'M') st = {i, j};
        else if(c == 'D') fi = {i, j};
        else if(c == 'T') a[i][j] = -1;
        else if(c == 'H') q.push({i, j}), a[i][j] = 1;
    }
    while(q.size()){
        ll u = q.front().first, v = q.front().second;
        q.pop();
        lp(i, 0, 3){
            ll nu = u + dx[i], nv = v + dy[i];
            if(a[nu][nv] || (nu == fi.first && nv == fi.second)) continue;
            a[nu][nv] = a[u][v] + 1;
            q.push({nu, nv});
        }
    }
    a[fi.first][fi.second] = 1e16;
    // lp(i, 1, n){
    //     lp(j, 1, n) cerr << a[i][j] << ' ';
    //     cerr << '\n';
    // }
    // cerr << check(2);
    ll l = 1, r = n * n, ans = -1; 
    while(l <= r){
        ll mid = (l + r) >> 1;
        if(check(mid)) ans = mid, l = mid + 1;
        else r = mid - 1;
    }
    if(ans != -1) ans -= 1;
    cout << ans;
}

Compilation message

mecho.cpp: In function 'void OF()':
mecho.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen(Fname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |         freopen(Fname".in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |         freopen(Fname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10444 KB Output is correct
2 Incorrect 4 ms 10444 KB Output isn't correct
3 Correct 5 ms 10444 KB Output is correct
4 Correct 4 ms 10444 KB Output is correct
5 Correct 4 ms 10444 KB Output is correct
6 Correct 4 ms 10444 KB Output is correct
7 Incorrect 301 ms 15720 KB Output isn't correct
8 Correct 5 ms 10444 KB Output is correct
9 Correct 5 ms 10472 KB Output is correct
10 Correct 4 ms 10444 KB Output is correct
11 Correct 4 ms 10444 KB Output is correct
12 Incorrect 4 ms 10700 KB Output isn't correct
13 Incorrect 4 ms 10700 KB Output isn't correct
14 Correct 5 ms 10700 KB Output is correct
15 Correct 4 ms 10468 KB Output is correct
16 Correct 5 ms 10444 KB Output is correct
17 Correct 5 ms 10464 KB Output is correct
18 Correct 4 ms 10444 KB Output is correct
19 Correct 5 ms 10572 KB Output is correct
20 Correct 5 ms 10572 KB Output is correct
21 Correct 5 ms 10572 KB Output is correct
22 Correct 5 ms 10572 KB Output is correct
23 Correct 5 ms 10648 KB Output is correct
24 Correct 5 ms 10700 KB Output is correct
25 Correct 4 ms 10700 KB Output is correct
26 Correct 5 ms 10700 KB Output is correct
27 Correct 5 ms 10700 KB Output is correct
28 Correct 4 ms 10700 KB Output is correct
29 Correct 6 ms 10780 KB Output is correct
30 Correct 4 ms 10700 KB Output is correct
31 Correct 5 ms 10700 KB Output is correct
32 Correct 6 ms 10700 KB Output is correct
33 Correct 10 ms 12620 KB Output is correct
34 Correct 10 ms 12644 KB Output is correct
35 Incorrect 63 ms 12640 KB Output isn't correct
36 Correct 11 ms 12896 KB Output is correct
37 Correct 12 ms 12876 KB Output is correct
38 Incorrect 84 ms 12936 KB Output isn't correct
39 Correct 16 ms 13284 KB Output is correct
40 Correct 14 ms 13260 KB Output is correct
41 Incorrect 105 ms 13292 KB Output isn't correct
42 Correct 16 ms 13536 KB Output is correct
43 Correct 16 ms 13516 KB Output is correct
44 Incorrect 131 ms 13648 KB Output isn't correct
45 Correct 19 ms 13900 KB Output is correct
46 Correct 19 ms 13900 KB Output is correct
47 Incorrect 142 ms 13980 KB Output isn't correct
48 Correct 27 ms 14180 KB Output is correct
49 Correct 22 ms 14248 KB Output is correct
50 Incorrect 187 ms 14284 KB Output isn't correct
51 Correct 26 ms 14560 KB Output is correct
52 Correct 34 ms 14436 KB Output is correct
53 Incorrect 222 ms 14580 KB Output isn't correct
54 Correct 32 ms 14912 KB Output is correct
55 Correct 33 ms 14824 KB Output is correct
56 Incorrect 259 ms 14876 KB Output isn't correct
57 Correct 37 ms 15072 KB Output is correct
58 Correct 39 ms 15180 KB Output is correct
59 Incorrect 310 ms 15328 KB Output isn't correct
60 Correct 42 ms 15428 KB Output is correct
61 Correct 44 ms 15460 KB Output is correct
62 Incorrect 346 ms 15552 KB Output isn't correct
63 Correct 210 ms 15508 KB Output is correct
64 Correct 368 ms 15588 KB Output is correct
65 Correct 294 ms 15432 KB Output is correct
66 Correct 239 ms 15584 KB Output is correct
67 Correct 225 ms 15436 KB Output is correct
68 Correct 84 ms 15460 KB Output is correct
69 Correct 85 ms 15520 KB Output is correct
70 Correct 67 ms 15648 KB Output is correct
71 Correct 71 ms 15464 KB Output is correct
72 Incorrect 50 ms 15528 KB Output isn't correct
73 Incorrect 86 ms 15940 KB Output isn't correct
74 Incorrect 253 ms 16048 KB Output isn't correct
75 Incorrect 164 ms 15996 KB Output isn't correct
76 Incorrect 131 ms 16144 KB Output isn't correct
77 Correct 150 ms 16064 KB Output is correct
78 Correct 213 ms 15996 KB Output is correct
79 Incorrect 158 ms 16012 KB Output isn't correct
80 Incorrect 160 ms 15972 KB Output isn't correct
81 Incorrect 195 ms 16000 KB Output isn't correct
82 Incorrect 173 ms 16012 KB Output isn't correct
83 Incorrect 219 ms 15812 KB Output isn't correct
84 Incorrect 351 ms 15920 KB Output isn't correct
85 Incorrect 229 ms 15912 KB Output isn't correct
86 Incorrect 210 ms 15912 KB Output isn't correct
87 Incorrect 228 ms 15920 KB Output isn't correct
88 Incorrect 221 ms 15812 KB Output isn't correct
89 Incorrect 364 ms 15864 KB Output isn't correct
90 Incorrect 302 ms 15820 KB Output isn't correct
91 Incorrect 286 ms 15788 KB Output isn't correct
92 Incorrect 258 ms 15908 KB Output isn't correct