답안 #1097092

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097092 2024-10-06T03:41:45 Z InvMOD Mecho (IOI09_mecho) C++14
100 / 100
142 ms 14880 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}

const int N = 1e3+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};

int n, s, d[N][N], vis[N][N];
char a[N][N];

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

bool good(int time){
    pi st, ed;
    FOR(i, 1, n){
        FOR(j, 1, n){
            if(a[i][j] == 'M'){
                st = make_pair(i,j);
                vis[i][j] = 0;
            }
            else{
                if(a[i][j] == 'D'){
                    ed = make_pair(i, j);
                }
                vis[i][j] = 1e9;
            }
        }
    }

    /* broke test without checking
        7 3
        GTGGGGG
        GTGGGGG
        GTGGGGG
        HMGGGGD
        GTGGGGG
        GTGGGGG
        GTGGGGG
    */

    if(time >= d[st.fi][st.se]) return false;

    queue<pair<int,int>> q;
    q.push(st);

    while(sz(q)){
        pi e = q.front(); q.pop();

        int x = e.fi, y = e.se;
        for(int i = 0; i < 4; i++){
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(init(xx, yy) && vis[xx][yy] == 1e9 && a[xx][yy] != 'H'){
                    // it will always walking in 1 minute :v
                int n_dist = (vis[x][y] + 1) / s;
                // in some point means in the end of that minute so it's have to be time_bear < time_bees
                if(n_dist < d[xx][yy] - time){
                    vis[xx][yy] = vis[x][y] + 1;
                    q.push(make_pair(xx, yy));
                }
            }
        }
    }

    return vis[ed.fi][ed.se] != 1e9;
}

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

    queue<pair<int,int>> q;

    FOR(i, 1, n){
        FOR(j, 1, n){
            if(a[i][j] == 'H'){
                q.push(make_pair(i,j));
            }
            else d[i][j] = 1e9;
        }
    }

    while(sz(q)){
        pi e = q.front(); q.pop();

        int x = e.fi, y = e.se;
        for(int i = 0; i < 4; i++){
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(init(xx, yy) && d[xx][yy] == 1e9 && a[xx][yy] != 'D'){
                d[xx][yy] = d[x][y] + 1;
                q.push(make_pair(xx, yy));
            }
        }
    }

    int l = -1, r = 1e9;
    while(l+1 < r){
        int m = l+r>>1;
        if(good(m)){
            l = m;
        }
        else r = m;
    }
    cout << l <<"\n";
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP","r",stdin);
        freopen(name".OUT","w",stdout);
    }

    int t = 1; //cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message

mecho.cpp: In function 'void solve()':
mecho.cpp:127:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  127 |         int m = l+r>>1;
      |                 ~^~
mecho.cpp: In function 'int main()':
mecho.cpp:144:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 464 KB Output is correct
7 Correct 87 ms 14560 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 0 ms 604 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 0 ms 604 KB Output is correct
12 Correct 0 ms 860 KB Output is correct
13 Correct 0 ms 860 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 1 ms 632 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 1 ms 860 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 0 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 860 KB Output is correct
27 Correct 1 ms 1116 KB Output is correct
28 Correct 1 ms 1116 KB Output is correct
29 Correct 1 ms 1116 KB Output is correct
30 Correct 1 ms 1116 KB Output is correct
31 Correct 1 ms 1116 KB Output is correct
32 Correct 1 ms 1116 KB Output is correct
33 Correct 8 ms 5480 KB Output is correct
34 Correct 7 ms 5468 KB Output is correct
35 Correct 18 ms 5468 KB Output is correct
36 Correct 9 ms 6488 KB Output is correct
37 Correct 8 ms 6724 KB Output is correct
38 Correct 23 ms 6736 KB Output is correct
39 Correct 11 ms 7772 KB Output is correct
40 Correct 11 ms 7768 KB Output is correct
41 Correct 30 ms 7768 KB Output is correct
42 Correct 13 ms 9052 KB Output is correct
43 Correct 12 ms 9052 KB Output is correct
44 Correct 38 ms 8924 KB Output is correct
45 Correct 15 ms 9816 KB Output is correct
46 Correct 19 ms 10072 KB Output is correct
47 Correct 47 ms 9952 KB Output is correct
48 Correct 19 ms 10844 KB Output is correct
49 Correct 17 ms 10844 KB Output is correct
50 Correct 55 ms 10860 KB Output is correct
51 Correct 27 ms 11612 KB Output is correct
52 Correct 21 ms 11608 KB Output is correct
53 Correct 67 ms 11612 KB Output is correct
54 Correct 27 ms 12632 KB Output is correct
55 Correct 25 ms 12548 KB Output is correct
56 Correct 80 ms 12592 KB Output is correct
57 Correct 26 ms 13404 KB Output is correct
58 Correct 24 ms 13524 KB Output is correct
59 Correct 85 ms 13404 KB Output is correct
60 Correct 31 ms 14164 KB Output is correct
61 Correct 28 ms 14152 KB Output is correct
62 Correct 97 ms 14172 KB Output is correct
63 Correct 95 ms 14208 KB Output is correct
64 Correct 142 ms 14172 KB Output is correct
65 Correct 139 ms 14108 KB Output is correct
66 Correct 104 ms 14132 KB Output is correct
67 Correct 96 ms 14164 KB Output is correct
68 Correct 52 ms 14160 KB Output is correct
69 Correct 53 ms 14160 KB Output is correct
70 Correct 43 ms 14396 KB Output is correct
71 Correct 44 ms 14164 KB Output is correct
72 Correct 39 ms 14168 KB Output is correct
73 Correct 46 ms 14788 KB Output is correct
74 Correct 64 ms 14636 KB Output is correct
75 Correct 67 ms 14676 KB Output is correct
76 Correct 60 ms 14740 KB Output is correct
77 Correct 64 ms 14672 KB Output is correct
78 Correct 67 ms 14756 KB Output is correct
79 Correct 55 ms 14680 KB Output is correct
80 Correct 58 ms 14680 KB Output is correct
81 Correct 65 ms 14672 KB Output is correct
82 Correct 56 ms 14832 KB Output is correct
83 Correct 72 ms 14672 KB Output is correct
84 Correct 66 ms 14880 KB Output is correct
85 Correct 65 ms 14880 KB Output is correct
86 Correct 69 ms 14848 KB Output is correct
87 Correct 68 ms 14684 KB Output is correct
88 Correct 79 ms 14672 KB Output is correct
89 Correct 72 ms 14684 KB Output is correct
90 Correct 75 ms 14676 KB Output is correct
91 Correct 74 ms 14596 KB Output is correct
92 Correct 73 ms 14676 KB Output is correct