Submission #484689

# Submission time Handle Problem Language Result Execution time Memory
484689 2021-11-05T04:24:47 Z Dirii Mecho (IOI09_mecho) C++14
84 / 100
238 ms 16056 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;
    queue<pp(ll, ll)> q;
    q.push(st);
    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) 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 Correct 4 ms 10444 KB Output is correct
3 Correct 5 ms 10444 KB Output is correct
4 Correct 4 ms 10444 KB Output is correct
5 Correct 5 ms 10444 KB Output is correct
6 Correct 4 ms 10444 KB Output is correct
7 Correct 99 ms 15688 KB Output is correct
8 Correct 4 ms 10444 KB Output is correct
9 Correct 4 ms 10444 KB Output is correct
10 Correct 4 ms 10444 KB Output is correct
11 Correct 5 ms 10444 KB Output is correct
12 Incorrect 5 ms 10700 KB Output isn't correct
13 Incorrect 4 ms 10700 KB Output isn't correct
14 Correct 7 ms 10756 KB Output is correct
15 Correct 4 ms 10444 KB Output is correct
16 Correct 4 ms 10444 KB Output is correct
17 Correct 5 ms 10572 KB Output is correct
18 Correct 5 ms 10572 KB Output is correct
19 Correct 4 ms 10572 KB Output is correct
20 Correct 4 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 10700 KB Output is correct
24 Correct 4 ms 10700 KB Output is correct
25 Correct 5 ms 10700 KB Output is correct
26 Correct 4 ms 10700 KB Output is correct
27 Correct 6 ms 10700 KB Output is correct
28 Correct 5 ms 10700 KB Output is correct
29 Correct 4 ms 10700 KB Output is correct
30 Correct 4 ms 10700 KB Output is correct
31 Correct 4 ms 10700 KB Output is correct
32 Correct 5 ms 10700 KB Output is correct
33 Correct 10 ms 12644 KB Output is correct
34 Correct 10 ms 12620 KB Output is correct
35 Correct 24 ms 12620 KB Output is correct
36 Correct 11 ms 12900 KB Output is correct
37 Correct 11 ms 12876 KB Output is correct
38 Correct 32 ms 12908 KB Output is correct
39 Correct 13 ms 13260 KB Output is correct
40 Correct 13 ms 13260 KB Output is correct
41 Correct 38 ms 13320 KB Output is correct
42 Correct 15 ms 13516 KB Output is correct
43 Correct 16 ms 13640 KB Output is correct
44 Correct 47 ms 13548 KB Output is correct
45 Correct 18 ms 13948 KB Output is correct
46 Correct 20 ms 13924 KB Output is correct
47 Correct 55 ms 13836 KB Output is correct
48 Correct 21 ms 14156 KB Output is correct
49 Correct 26 ms 14156 KB Output is correct
50 Correct 67 ms 14244 KB Output is correct
51 Correct 26 ms 14540 KB Output is correct
52 Correct 31 ms 14564 KB Output is correct
53 Correct 100 ms 14556 KB Output is correct
54 Correct 32 ms 14876 KB Output is correct
55 Correct 31 ms 14884 KB Output is correct
56 Correct 104 ms 14796 KB Output is correct
57 Correct 37 ms 15180 KB Output is correct
58 Correct 38 ms 15184 KB Output is correct
59 Correct 120 ms 15192 KB Output is correct
60 Correct 45 ms 15448 KB Output is correct
61 Correct 45 ms 15436 KB Output is correct
62 Correct 143 ms 15436 KB Output is correct
63 Correct 123 ms 15432 KB Output is correct
64 Correct 238 ms 15456 KB Output is correct
65 Correct 168 ms 15492 KB Output is correct
66 Correct 139 ms 15492 KB Output is correct
67 Correct 133 ms 15460 KB Output is correct
68 Correct 70 ms 15548 KB Output is correct
69 Correct 67 ms 15520 KB Output is correct
70 Correct 57 ms 15556 KB Output is correct
71 Correct 65 ms 15456 KB Output is correct
72 Incorrect 51 ms 15564 KB Output isn't correct
73 Incorrect 60 ms 16056 KB Output isn't correct
74 Correct 70 ms 16000 KB Output is correct
75 Correct 81 ms 15948 KB Output is correct
76 Correct 82 ms 16004 KB Output is correct
77 Correct 75 ms 15940 KB Output is correct
78 Correct 92 ms 15956 KB Output is correct
79 Correct 62 ms 15936 KB Output is correct
80 Correct 71 ms 15992 KB Output is correct
81 Correct 80 ms 15948 KB Output is correct
82 Correct 77 ms 15964 KB Output is correct
83 Correct 90 ms 15912 KB Output is correct
84 Correct 83 ms 15812 KB Output is correct
85 Correct 92 ms 16036 KB Output is correct
86 Correct 87 ms 15884 KB Output is correct
87 Correct 89 ms 15820 KB Output is correct
88 Correct 95 ms 15908 KB Output is correct
89 Correct 93 ms 15812 KB Output is correct
90 Correct 98 ms 15816 KB Output is correct
91 Correct 94 ms 15816 KB Output is correct
92 Correct 107 ms 15788 KB Output is correct