Submission #500230

# Submission time Handle Problem Language Result Execution time Memory
500230 2021-12-30T13:32:00 Z Nartifact Mecho (IOI09_mecho) C++17
84 / 100
232 ms 17936 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define eb emplace_back
#define pii pair <int,int>
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int N = 888, inf = 1e15;
const int H[] = {1, 0, -1, 0};
const int C[] = {0, 1, 0, -1};
int n, s, cti = inf;
char a[N][N];
int t[N][N], f[N][N], m[N][N];
pii sta, fin;

bool valid (const int& x, const int& y)
{
    return x && y && x <= n && y <= n
          && a[x][y] != 'T';
}

bool ok (const int& tg)
{
    queue <pii> q; q.push(sta);
    forinc(i,1,n) forinc(j,1,n) f[i][j] = inf;
    if(tg >= t[sta.fi][sta.se]) return 0;
    f[sta.fi][sta.se] = tg;
    while(q.size()) {
        auto x = q.front(); q.pop();
        if(x == fin) return 1;
        forinc(i,0,3) {
            int nh = x.fi + H[i];
            int nc = x.se + C[i];
            if(valid(nh, nc) && f[nh][nc] > f[x.fi][x.se] + 1) {
                f[nh][nc] = f[x.fi][x.se] + 1;
                m[nh][nc] = (m[x.fi][x.se] + 1) % s;
                int diff = f[nh][nc] - tg - 1;
                diff /= s;
                diff += tg + 1;
                if(diff < t[nh][nc] || (diff == t[nh][nc] && m[nh][nc]))
                    q.push({nh, nc});
            }
        }
    }
    return 0;
}

void solve ()
{
    queue <pii> q;
    forinc(i,1,n) forinc(j,1,n) {
        t[i][j] = inf;
        if(a[i][j] == 'H') {
            q.push({i, j});
            t[i][j] = 0;
        }
        else if(a[i][j] == 'M') sta = {i, j};
        else if(a[i][j] == 'D') fin = {i, j};
    }

    // bfs bee
    while(q.size()) {
        auto x = q.front(); q.pop();
        forinc(i,0,3) {
            int nh = x.fi + H[i];
            int nc = x.se + C[i];
            if(valid(nh, nc) && t[nh][nc] > t[x.fi][x.se] + 1) {
                t[nh][nc] = t[x.fi][x.se] + 1;
                q.push({nh, nc});
            }
        }
    }

    // binary search
    int l = 0, r = n * n, ans = -1;
    while(l <= r) {
        int mid = l + r >> 1;
        if(ok(mid)) {
            ans = mid;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    cout << ans;
}

void read ()
{
    cin >> n >> s;
    forinc(i,1,n) forinc(j,1,n)
        cin >> a[i][j];
    solve();
}

main ()
{
	#define task "ioi09_mecho"
	cin.tie(0) -> sync_with_stdio(0);
	if(fopen (task ".inp", "r")) {
		freopen (task ".inp", "r", stdin);
		freopen (task ".out", "w", stdout);
	}
    read();
	return 0;
}

Compilation message

mecho.cpp: In function 'void solve()':
mecho.cpp:85:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   85 |         int mid = l + r >> 1;
      |                   ~~^~~
mecho.cpp: At global scope:
mecho.cpp:103:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  103 | main ()
      | ^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:108:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  108 |   freopen (task ".inp", "r", stdin);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:109:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |   freopen (task ".out", "w", stdout);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Correct 0 ms 332 KB Output is correct
4 Incorrect 0 ms 332 KB Output isn't correct
5 Correct 0 ms 332 KB Output is correct
6 Incorrect 0 ms 460 KB Output isn't correct
7 Incorrect 129 ms 15580 KB Output isn't correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 0 ms 460 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 0 ms 460 KB Output is correct
12 Incorrect 1 ms 1100 KB Output isn't correct
13 Correct 1 ms 844 KB Output is correct
14 Correct 1 ms 972 KB Output is correct
15 Correct 0 ms 460 KB Output is correct
16 Correct 1 ms 460 KB Output is correct
17 Correct 0 ms 588 KB Output is correct
18 Correct 0 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 1 ms 588 KB Output is correct
21 Correct 0 ms 716 KB Output is correct
22 Correct 1 ms 716 KB Output is correct
23 Correct 1 ms 844 KB Output is correct
24 Correct 1 ms 844 KB Output is correct
25 Correct 1 ms 844 KB Output is correct
26 Correct 1 ms 972 KB Output is correct
27 Correct 1 ms 972 KB Output is correct
28 Correct 1 ms 972 KB Output is correct
29 Correct 1 ms 972 KB Output is correct
30 Correct 1 ms 1100 KB Output is correct
31 Correct 1 ms 1100 KB Output is correct
32 Correct 1 ms 1100 KB Output is correct
33 Correct 6 ms 5324 KB Output is correct
34 Correct 7 ms 5452 KB Output is correct
35 Correct 31 ms 7756 KB Output is correct
36 Correct 7 ms 6200 KB Output is correct
37 Correct 10 ms 6220 KB Output is correct
38 Correct 32 ms 9012 KB Output is correct
39 Correct 10 ms 6988 KB Output is correct
40 Correct 9 ms 7052 KB Output is correct
41 Correct 41 ms 10084 KB Output is correct
42 Correct 10 ms 7628 KB Output is correct
43 Correct 10 ms 7756 KB Output is correct
44 Correct 70 ms 11128 KB Output is correct
45 Correct 18 ms 8392 KB Output is correct
46 Correct 17 ms 8480 KB Output is correct
47 Correct 63 ms 12236 KB Output is correct
48 Correct 16 ms 9164 KB Output is correct
49 Correct 16 ms 9200 KB Output is correct
50 Correct 82 ms 13336 KB Output is correct
51 Correct 20 ms 9984 KB Output is correct
52 Correct 17 ms 9900 KB Output is correct
53 Correct 126 ms 14380 KB Output is correct
54 Correct 27 ms 10624 KB Output is correct
55 Correct 20 ms 10700 KB Output is correct
56 Correct 122 ms 15512 KB Output is correct
57 Correct 24 ms 11424 KB Output is correct
58 Correct 23 ms 11444 KB Output is correct
59 Correct 152 ms 16548 KB Output is correct
60 Correct 26 ms 12088 KB Output is correct
61 Correct 27 ms 12108 KB Output is correct
62 Correct 165 ms 17608 KB Output is correct
63 Correct 122 ms 17564 KB Output is correct
64 Correct 232 ms 17672 KB Output is correct
65 Correct 169 ms 17592 KB Output is correct
66 Correct 141 ms 17548 KB Output is correct
67 Correct 134 ms 17312 KB Output is correct
68 Correct 61 ms 16884 KB Output is correct
69 Correct 64 ms 16960 KB Output is correct
70 Correct 49 ms 16672 KB Output is correct
71 Correct 55 ms 16596 KB Output is correct
72 Correct 41 ms 13044 KB Output is correct
73 Correct 47 ms 17936 KB Output is correct
74 Correct 100 ms 17368 KB Output is correct
75 Correct 96 ms 17572 KB Output is correct
76 Correct 87 ms 17448 KB Output is correct
77 Correct 82 ms 17256 KB Output is correct
78 Correct 87 ms 15212 KB Output is correct
79 Correct 60 ms 14520 KB Output is correct
80 Correct 75 ms 14660 KB Output is correct
81 Correct 84 ms 14900 KB Output is correct
82 Correct 71 ms 14884 KB Output is correct
83 Correct 101 ms 15172 KB Output is correct
84 Correct 91 ms 14996 KB Output is correct
85 Correct 127 ms 15144 KB Output is correct
86 Correct 97 ms 15172 KB Output is correct
87 Correct 100 ms 15192 KB Output is correct
88 Correct 110 ms 15616 KB Output is correct
89 Correct 124 ms 15232 KB Output is correct
90 Correct 126 ms 15212 KB Output is correct
91 Correct 128 ms 15452 KB Output is correct
92 Correct 169 ms 15412 KB Output is correct