Submission #500015

# Submission time Handle Problem Language Result Execution time Memory
500015 2021-12-30T09:24:04 Z Nartifact Mecho (IOI09_mecho) C++14
22 / 100
198 ms 12692 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], 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] = inf;
    mec[sta.fi][sta.se] = 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] > mec[x.fi][x.se] + 1) {
                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});
                }
            }
        }
    }
    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};
            t[i][j] = -1;
        }
        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;
    }
    if(ans == 0) ans = -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:88:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   88 |         int mid = l + r >> 1;
      |                   ~~^~~
mecho.cpp: At global scope:
mecho.cpp:107:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  107 | main ()
      | ^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:112:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  112 |   freopen (task ".inp", "r", stdin);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:113:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |   freopen (task ".out", "w", stdout);
      |   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Output isn't correct
2 Incorrect 0 ms 332 KB Output isn't correct
3 Incorrect 0 ms 332 KB Output isn't correct
4 Incorrect 0 ms 332 KB Output isn't correct
5 Incorrect 0 ms 332 KB Output isn't correct
6 Incorrect 1 ms 332 KB Output isn't correct
7 Incorrect 109 ms 12484 KB Output isn't correct
8 Correct 0 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 1 ms 460 KB Output is correct
12 Incorrect 1 ms 844 KB Output isn't correct
13 Incorrect 1 ms 716 KB Output isn't correct
14 Correct 1 ms 844 KB Output is correct
15 Incorrect 1 ms 460 KB Output isn't correct
16 Correct 0 ms 460 KB Output is correct
17 Incorrect 1 ms 460 KB Output isn't correct
18 Correct 0 ms 460 KB Output is correct
19 Incorrect 1 ms 588 KB Output isn't correct
20 Correct 1 ms 588 KB Output is correct
21 Incorrect 1 ms 716 KB Output isn't correct
22 Correct 1 ms 716 KB Output is correct
23 Incorrect 1 ms 716 KB Output isn't correct
24 Correct 1 ms 716 KB Output is correct
25 Incorrect 1 ms 844 KB Output isn't correct
26 Correct 1 ms 844 KB Output is correct
27 Incorrect 1 ms 972 KB Output isn't correct
28 Correct 1 ms 972 KB Output is correct
29 Incorrect 1 ms 972 KB Output isn't correct
30 Correct 1 ms 972 KB Output is correct
31 Incorrect 1 ms 972 KB Output isn't correct
32 Correct 1 ms 972 KB Output is correct
33 Incorrect 5 ms 5324 KB Output isn't correct
34 Correct 6 ms 5324 KB Output is correct
35 Correct 20 ms 5384 KB Output is correct
36 Incorrect 7 ms 6220 KB Output isn't correct
37 Correct 8 ms 6248 KB Output is correct
38 Correct 25 ms 6252 KB Output is correct
39 Incorrect 13 ms 6988 KB Output isn't correct
40 Correct 10 ms 6988 KB Output is correct
41 Correct 38 ms 6860 KB Output is correct
42 Incorrect 11 ms 7696 KB Output isn't correct
43 Correct 11 ms 7628 KB Output is correct
44 Correct 43 ms 7628 KB Output is correct
45 Incorrect 14 ms 8396 KB Output isn't correct
46 Correct 13 ms 8404 KB Output is correct
47 Correct 51 ms 8516 KB Output is correct
48 Incorrect 14 ms 9164 KB Output isn't correct
49 Correct 15 ms 9116 KB Output is correct
50 Correct 61 ms 9164 KB Output is correct
51 Incorrect 17 ms 9932 KB Output isn't correct
52 Correct 17 ms 9808 KB Output is correct
53 Correct 77 ms 9928 KB Output is correct
54 Incorrect 20 ms 10672 KB Output isn't correct
55 Correct 19 ms 10584 KB Output is correct
56 Correct 112 ms 10660 KB Output is correct
57 Incorrect 22 ms 11412 KB Output isn't correct
58 Correct 22 ms 11312 KB Output is correct
59 Correct 112 ms 11400 KB Output is correct
60 Incorrect 25 ms 12108 KB Output isn't correct
61 Correct 29 ms 12156 KB Output is correct
62 Correct 127 ms 12100 KB Output is correct
63 Incorrect 105 ms 12096 KB Output isn't correct
64 Correct 198 ms 12132 KB Output is correct
65 Correct 148 ms 12108 KB Output is correct
66 Incorrect 132 ms 12092 KB Output isn't correct
67 Correct 120 ms 12132 KB Output is correct
68 Incorrect 56 ms 12140 KB Output isn't correct
69 Correct 56 ms 12188 KB Output is correct
70 Correct 43 ms 12192 KB Output is correct
71 Correct 47 ms 12104 KB Output is correct
72 Incorrect 40 ms 12140 KB Output isn't correct
73 Incorrect 76 ms 12612 KB Output isn't correct
74 Correct 67 ms 12656 KB Output is correct
75 Correct 74 ms 12644 KB Output is correct
76 Correct 66 ms 12648 KB Output is correct
77 Correct 69 ms 12692 KB Output is correct
78 Correct 81 ms 12628 KB Output is correct
79 Correct 55 ms 12612 KB Output is correct
80 Correct 61 ms 12628 KB Output is correct
81 Correct 70 ms 12624 KB Output is correct
82 Correct 64 ms 12620 KB Output is correct
83 Incorrect 82 ms 12524 KB Output isn't correct
84 Correct 76 ms 12484 KB Output is correct
85 Correct 79 ms 12492 KB Output is correct
86 Correct 78 ms 12552 KB Output is correct
87 Correct 87 ms 12548 KB Output is correct
88 Incorrect 82 ms 12412 KB Output isn't correct
89 Correct 84 ms 12412 KB Output is correct
90 Correct 85 ms 12420 KB Output is correct
91 Correct 88 ms 12356 KB Output is correct
92 Correct 93 ms 12460 KB Output is correct