Submission #1100022

# Submission time Handle Problem Language Result Execution time Memory
1100022 2024-10-12T12:04:45 Z asli_bg Mecho (IOI09_mecho) C++11
100 / 100
138 ms 3956 KB
#pragma GCC optimize("O3,unroll-loops")

#include<bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define inset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>

//#define int long long

#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define FOR(i,a) for(int i=0;i<(a);i++)
#define FORE(i,a,b) for(int i=(a);i<(b);i++)

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vii;

#define cont(x) for(auto el:x) {cout<<el<<' ';} cout<<endl;
//#define endl '\n'
#define contp(x) for(auto el:x) {cout<<el.fi<<'-'<<el.se<<' ';} cout<<endl;
#define pb push_back

#define sp <<' '<<

#define DEBUG(x) {cout<<(#x) sp x<<endl;}

const int MAXN=805;
const int INF=1e9;

int n,s;
vector<string> grid;
int mark[MAXN][MAXN];

pii bas,son;
int yon1[4]={1,-1,0,0};
int yon2[4]={0,0,-1,1};
//sag, sol, asagi, yukari

bool isvalid(int x,int y,bool f){
    if(x>=n or y>=n or x<0 or y<0) return false;
    if(f){
        if(grid[x][y]!='G' and grid[x][y]!='M') return false;
        if(mark[x][y]) return false;
    }
    else{
        if(grid[x][y]!='G' and grid[x][y]!='D') return false;
    }
    return true;
}

bool check(int deg){
    //düz shortest path bfs
    vector<vb> vis(n+1, vb(n+1,false));
    queue<pair<pii,int>> q;

    if(mark[bas.fi][bas.se]<deg) return false;

    q.push({bas,0});
    vis[bas.fi][bas.se]=true;

    while(!q.empty()){
        auto el=q.front();
        q.pop();

        int adim=el.se;
        int x=el.fi.fi;
        int y=el.fi.se;

        FOR(i,4){
            int yenix=x+yon1[i];
            int yeniy=y+yon2[i];

            if(!isvalid(yenix,yeniy,false)) continue;
            if(vis[yenix][yeniy]) continue;
            
            int sure=adim/s;
            if(mark[yenix][yeniy]<deg+sure) continue;
            if((adim+1)%s==0){
                if(mark[yenix][yeniy]==deg+sure) continue;   
            }

            q.push({{yenix,yeniy},adim+1});
            vis[yenix][yeniy]=true;
        }
    }

    return vis[son.fi][son.se];
}

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

    cin>>n>>s;
    grid.resize(n);
    FOR(i,n) cin>>grid[i];
    queue<pii> q;

    int say=0;
    FOR(i,n){
        FOR(j,n){
            if(grid[i][j]=='H'){
                q.push({i,j});
                mark[i][j]=0;
            }
            else if(grid[i][j]=='M') {
                bas={i,j};
                say++;
            }
            else if(grid[i][j]=='D'){
                son={i,j};
                mark[i][j]=INF;
            }
            else say++;
        }
    }

    int mx=0;

    while(!q.empty()){
        auto el=q.front();
        q.pop();

        FOR(i,4){
            int yenix=el.fi+yon1[i];
            int yeniy=el.se+yon2[i];
            if(isvalid(yenix,yeniy,true)){
                mark[yenix][yeniy]=mark[el.fi][el.se]+1;
                mx=max(mx,mark[yenix][yeniy]);
                q.push({yenix,yeniy});
            }
        }
    }

    int l=0;
    int r=mx+1;
    while(l+1<r){
        int mid=(l+r)/2;
        if(check(mid)) l=mid;
        else r=mid;
    }

    cout<<l-1<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 348 KB Output is correct
7 Correct 62 ms 3808 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 2652 KB Output is correct
26 Correct 1 ms 2652 KB Output is correct
27 Correct 0 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 0 ms 2652 KB Output is correct
30 Correct 1 ms 2652 KB Output is correct
31 Correct 1 ms 2652 KB Output is correct
32 Correct 1 ms 2652 KB Output is correct
33 Correct 3 ms 2652 KB Output is correct
34 Correct 3 ms 2652 KB Output is correct
35 Correct 16 ms 2720 KB Output is correct
36 Correct 3 ms 2908 KB Output is correct
37 Correct 3 ms 2652 KB Output is correct
38 Correct 22 ms 2908 KB Output is correct
39 Correct 3 ms 2904 KB Output is correct
40 Correct 4 ms 2960 KB Output is correct
41 Correct 26 ms 2908 KB Output is correct
42 Correct 4 ms 2904 KB Output is correct
43 Correct 5 ms 2908 KB Output is correct
44 Correct 32 ms 3028 KB Output is correct
45 Correct 5 ms 2908 KB Output is correct
46 Correct 7 ms 3076 KB Output is correct
47 Correct 44 ms 3088 KB Output is correct
48 Correct 6 ms 3164 KB Output is correct
49 Correct 7 ms 2908 KB Output is correct
50 Correct 53 ms 3084 KB Output is correct
51 Correct 7 ms 3164 KB Output is correct
52 Correct 8 ms 3152 KB Output is correct
53 Correct 60 ms 3228 KB Output is correct
54 Correct 8 ms 3160 KB Output is correct
55 Correct 10 ms 3164 KB Output is correct
56 Correct 70 ms 3224 KB Output is correct
57 Correct 9 ms 3420 KB Output is correct
58 Correct 11 ms 3420 KB Output is correct
59 Correct 83 ms 3420 KB Output is correct
60 Correct 10 ms 3676 KB Output is correct
61 Correct 12 ms 3796 KB Output is correct
62 Correct 93 ms 3676 KB Output is correct
63 Correct 78 ms 3676 KB Output is correct
64 Correct 138 ms 3676 KB Output is correct
65 Correct 136 ms 3676 KB Output is correct
66 Correct 96 ms 3672 KB Output is correct
67 Correct 89 ms 3672 KB Output is correct
68 Correct 30 ms 3700 KB Output is correct
69 Correct 27 ms 3680 KB Output is correct
70 Correct 25 ms 3676 KB Output is correct
71 Correct 20 ms 3708 KB Output is correct
72 Correct 17 ms 3804 KB Output is correct
73 Correct 21 ms 3956 KB Output is correct
74 Correct 45 ms 3928 KB Output is correct
75 Correct 41 ms 3932 KB Output is correct
76 Correct 55 ms 3928 KB Output is correct
77 Correct 44 ms 3932 KB Output is correct
78 Correct 51 ms 3676 KB Output is correct
79 Correct 34 ms 3672 KB Output is correct
80 Correct 45 ms 3676 KB Output is correct
81 Correct 51 ms 3932 KB Output is correct
82 Correct 38 ms 3676 KB Output is correct
83 Correct 63 ms 3676 KB Output is correct
84 Correct 52 ms 3676 KB Output is correct
85 Correct 46 ms 3676 KB Output is correct
86 Correct 55 ms 3676 KB Output is correct
87 Correct 62 ms 3868 KB Output is correct
88 Correct 58 ms 3676 KB Output is correct
89 Correct 58 ms 3676 KB Output is correct
90 Correct 63 ms 3676 KB Output is correct
91 Correct 59 ms 3676 KB Output is correct
92 Correct 68 ms 3672 KB Output is correct