Submission #484686

# Submission time Handle Problem Language Result Execution time Memory
484686 2021-11-05T04:16:58 Z Dirii Mecho (IOI09_mecho) C++14
11 / 100
386 ms 16772 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] == 0){
                    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';
    // }
    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 Incorrect 4 ms 10444 KB Output isn't correct
2 Incorrect 5 ms 10444 KB Output isn't correct
3 Incorrect 4 ms 10444 KB Output isn't correct
4 Incorrect 6 ms 10472 KB Output isn't correct
5 Correct 5 ms 10428 KB Output is correct
6 Incorrect 5 ms 10440 KB Output isn't correct
7 Incorrect 335 ms 16332 KB Output isn't correct
8 Incorrect 6 ms 10444 KB Output isn't correct
9 Correct 4 ms 10468 KB Output is correct
10 Incorrect 4 ms 10468 KB Output isn't correct
11 Incorrect 5 ms 10444 KB Output isn't correct
12 Incorrect 5 ms 10724 KB Output isn't correct
13 Incorrect 5 ms 10632 KB Output isn't correct
14 Incorrect 5 ms 10700 KB Output isn't correct
15 Incorrect 5 ms 10460 KB Output isn't correct
16 Correct 5 ms 10444 KB Output is correct
17 Incorrect 7 ms 10472 KB Output isn't correct
18 Correct 5 ms 10572 KB Output is correct
19 Incorrect 5 ms 10572 KB Output isn't correct
20 Correct 5 ms 10572 KB Output is correct
21 Incorrect 7 ms 10572 KB Output isn't correct
22 Correct 5 ms 10572 KB Output is correct
23 Incorrect 5 ms 10592 KB Output isn't correct
24 Correct 5 ms 10700 KB Output is correct
25 Incorrect 5 ms 10700 KB Output isn't correct
26 Correct 5 ms 10700 KB Output is correct
27 Incorrect 5 ms 10700 KB Output isn't correct
28 Correct 5 ms 10700 KB Output is correct
29 Incorrect 5 ms 10728 KB Output isn't correct
30 Correct 5 ms 10700 KB Output is correct
31 Incorrect 5 ms 10828 KB Output isn't correct
32 Correct 5 ms 10724 KB Output is correct
33 Incorrect 11 ms 12748 KB Output isn't correct
34 Correct 11 ms 12748 KB Output is correct
35 Incorrect 62 ms 12808 KB Output isn't correct
36 Incorrect 12 ms 13040 KB Output isn't correct
37 Correct 12 ms 13132 KB Output is correct
38 Incorrect 82 ms 13192 KB Output isn't correct
39 Incorrect 14 ms 13388 KB Output isn't correct
40 Correct 14 ms 13420 KB Output is correct
41 Incorrect 105 ms 13516 KB Output isn't correct
42 Incorrect 17 ms 13772 KB Output isn't correct
43 Correct 21 ms 13772 KB Output is correct
44 Incorrect 145 ms 13908 KB Output isn't correct
45 Incorrect 18 ms 14164 KB Output isn't correct
46 Correct 19 ms 14232 KB Output is correct
47 Incorrect 154 ms 14396 KB Output isn't correct
48 Incorrect 22 ms 14540 KB Output isn't correct
49 Correct 25 ms 14604 KB Output is correct
50 Incorrect 242 ms 14736 KB Output isn't correct
51 Incorrect 26 ms 14984 KB Output isn't correct
52 Correct 30 ms 14956 KB Output is correct
53 Incorrect 235 ms 15024 KB Output isn't correct
54 Incorrect 37 ms 15272 KB Output isn't correct
55 Correct 43 ms 15292 KB Output is correct
56 Incorrect 272 ms 15364 KB Output isn't correct
57 Incorrect 37 ms 15624 KB Output isn't correct
58 Correct 46 ms 15692 KB Output is correct
59 Incorrect 322 ms 15808 KB Output isn't correct
60 Incorrect 48 ms 16076 KB Output isn't correct
61 Correct 45 ms 16120 KB Output is correct
62 Incorrect 349 ms 16180 KB Output isn't correct
63 Incorrect 286 ms 16104 KB Output isn't correct
64 Correct 382 ms 16068 KB Output is correct
65 Incorrect 322 ms 16212 KB Output isn't correct
66 Incorrect 280 ms 16212 KB Output isn't correct
67 Incorrect 279 ms 16068 KB Output isn't correct
68 Incorrect 87 ms 16188 KB Output isn't correct
69 Correct 91 ms 16184 KB Output is correct
70 Incorrect 76 ms 16196 KB Output isn't correct
71 Incorrect 72 ms 16180 KB Output isn't correct
72 Incorrect 50 ms 16072 KB Output isn't correct
73 Incorrect 84 ms 16708 KB Output isn't correct
74 Incorrect 264 ms 16772 KB Output isn't correct
75 Incorrect 173 ms 16676 KB Output isn't correct
76 Incorrect 121 ms 16676 KB Output isn't correct
77 Incorrect 154 ms 16712 KB Output isn't correct
78 Incorrect 222 ms 16616 KB Output isn't correct
79 Incorrect 161 ms 16588 KB Output isn't correct
80 Incorrect 195 ms 16744 KB Output isn't correct
81 Incorrect 201 ms 16604 KB Output isn't correct
82 Incorrect 176 ms 16632 KB Output isn't correct
83 Incorrect 231 ms 16460 KB Output isn't correct
84 Incorrect 358 ms 16580 KB Output isn't correct
85 Incorrect 216 ms 16456 KB Output isn't correct
86 Incorrect 216 ms 16540 KB Output isn't correct
87 Incorrect 238 ms 16452 KB Output isn't correct
88 Incorrect 231 ms 16440 KB Output isn't correct
89 Incorrect 386 ms 16444 KB Output isn't correct
90 Incorrect 303 ms 16460 KB Output isn't correct
91 Incorrect 299 ms 16440 KB Output isn't correct
92 Incorrect 250 ms 16444 KB Output isn't correct