Submission #500071

# Submission time Handle Problem Language Result Execution time Memory
500071 2021-12-30T10:02:18 Z Nartifact Mecho (IOI09_mecho) C++14
22 / 100
159 ms 12696 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;
char a[N][N];
int t[N][N], mec[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) mec[i][j] = 0;
    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) && !mec[nh][nc]) {
                int gap = (mec[x.fi][x.se] + 1) / s;
                if((mec[x.fi][x.se] + 1) % s) gap++;
                if(gap < t[nh][nc] - tg) {
                    mec[nh][nc] = mec[x.fi][x.se] + 1;
                    q.push({nh, nc});
                }
            }
        }
        if(x == sta) mec[sta.fi][sta.se] = -1;
    }
    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
    t[fin.fi][fin.se] = inf;
    int l = 0, r = n * 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:84:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |         int mid = l + r >> 1;
      |                   ~~^~~
mecho.cpp: At global scope:
mecho.cpp:102:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  102 | main ()
      | ^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:107:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |   freopen (task ".inp", "r", stdin);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
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 ".out", "w", stdout);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Incorrect 0 ms 332 KB Output isn't correct
6 Incorrect 0 ms 332 KB Output isn't correct
7 Incorrect 90 ms 12368 KB Output isn't correct
8 Correct 0 ms 460 KB Output is correct
9 Incorrect 0 ms 460 KB Output isn't correct
10 Incorrect 0 ms 460 KB Output isn't correct
11 Incorrect 0 ms 460 KB Output isn't correct
12 Incorrect 1 ms 844 KB Output isn't correct
13 Correct 1 ms 716 KB Output is correct
14 Correct 1 ms 844 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Incorrect 0 ms 460 KB Output isn't correct
17 Correct 1 ms 460 KB Output is correct
18 Incorrect 1 ms 460 KB Output isn't correct
19 Correct 0 ms 588 KB Output is correct
20 Incorrect 0 ms 588 KB Output isn't correct
21 Correct 1 ms 716 KB Output is correct
22 Incorrect 1 ms 716 KB Output isn't correct
23 Correct 1 ms 716 KB Output is correct
24 Incorrect 1 ms 716 KB Output isn't correct
25 Correct 1 ms 844 KB Output is correct
26 Incorrect 1 ms 844 KB Output isn't correct
27 Correct 1 ms 972 KB Output is correct
28 Incorrect 1 ms 972 KB Output isn't correct
29 Correct 1 ms 972 KB Output is correct
30 Incorrect 1 ms 972 KB Output isn't correct
31 Correct 1 ms 972 KB Output is correct
32 Incorrect 1 ms 972 KB Output isn't correct
33 Correct 6 ms 5312 KB Output is correct
34 Incorrect 5 ms 5324 KB Output isn't correct
35 Incorrect 21 ms 5324 KB Output isn't correct
36 Correct 9 ms 6220 KB Output is correct
37 Incorrect 7 ms 6220 KB Output isn't correct
38 Incorrect 26 ms 6248 KB Output isn't correct
39 Correct 8 ms 6924 KB Output is correct
40 Incorrect 8 ms 6860 KB Output isn't correct
41 Incorrect 33 ms 6936 KB Output isn't correct
42 Correct 11 ms 7628 KB Output is correct
43 Incorrect 12 ms 7728 KB Output isn't correct
44 Incorrect 43 ms 7712 KB Output isn't correct
45 Correct 12 ms 8396 KB Output is correct
46 Incorrect 12 ms 8356 KB Output isn't correct
47 Incorrect 49 ms 8448 KB Output isn't correct
48 Correct 15 ms 9212 KB Output is correct
49 Incorrect 17 ms 9116 KB Output isn't correct
50 Incorrect 68 ms 9204 KB Output isn't correct
51 Correct 17 ms 9932 KB Output is correct
52 Incorrect 20 ms 9864 KB Output isn't correct
53 Incorrect 78 ms 9944 KB Output isn't correct
54 Correct 18 ms 10580 KB Output is correct
55 Incorrect 19 ms 10668 KB Output isn't correct
56 Incorrect 99 ms 10572 KB Output isn't correct
57 Correct 22 ms 11340 KB Output is correct
58 Incorrect 22 ms 11320 KB Output isn't correct
59 Incorrect 114 ms 11328 KB Output isn't correct
60 Correct 24 ms 12044 KB Output is correct
61 Incorrect 23 ms 12064 KB Output isn't correct
62 Incorrect 128 ms 12156 KB Output isn't correct
63 Incorrect 94 ms 12104 KB Output isn't correct
64 Incorrect 155 ms 12224 KB Output isn't correct
65 Incorrect 159 ms 12140 KB Output isn't correct
66 Correct 132 ms 12072 KB Output is correct
67 Correct 108 ms 12140 KB Output is correct
68 Incorrect 51 ms 12188 KB Output isn't correct
69 Incorrect 60 ms 12152 KB Output isn't correct
70 Incorrect 50 ms 12100 KB Output isn't correct
71 Incorrect 40 ms 12196 KB Output isn't correct
72 Correct 36 ms 12160 KB Output is correct
73 Correct 45 ms 12688 KB Output is correct
74 Incorrect 63 ms 12696 KB Output isn't correct
75 Incorrect 77 ms 12688 KB Output isn't correct
76 Incorrect 65 ms 12644 KB Output isn't correct
77 Incorrect 73 ms 12580 KB Output isn't correct
78 Correct 76 ms 12572 KB Output is correct
79 Incorrect 60 ms 12548 KB Output isn't correct
80 Incorrect 68 ms 12620 KB Output isn't correct
81 Incorrect 83 ms 12636 KB Output isn't correct
82 Incorrect 76 ms 12596 KB Output isn't correct
83 Incorrect 94 ms 12512 KB Output isn't correct
84 Incorrect 81 ms 12552 KB Output isn't correct
85 Incorrect 105 ms 12504 KB Output isn't correct
86 Incorrect 93 ms 12560 KB Output isn't correct
87 Incorrect 92 ms 12548 KB Output isn't correct
88 Incorrect 87 ms 12408 KB Output isn't correct
89 Incorrect 90 ms 12364 KB Output isn't correct
90 Incorrect 86 ms 12384 KB Output isn't correct
91 Incorrect 96 ms 12456 KB Output isn't correct
92 Incorrect 85 ms 12396 KB Output isn't correct