Submission #1100021

# Submission time Handle Problem Language Result Execution time Memory
1100021 2024-10-12T12:03:45 Z asli_bg Mecho (IOI09_mecho) C++11
100 / 100
145 ms 3932 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;

    //cout<<"DEEEEEG" sp deg<<endl;

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

        int adim=el.se;
        int x=el.fi.fi;
        int y=el.fi.se;
        //cout<<"control" sp adim sp x sp y<<endl;

        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;
            //DEBUG(sure);
            //DEBUG(yenix);
            //DEBUG(yeniy);
            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'){
                //hive.insert({i,j});
                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});
            }
        }
    }

    /*FOR(i,n){
        FOR(j,n) cout<<mark[i][j]<<' ';
        cout<<endl;
    }
    cout<<endl;*/

    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 344 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 61 ms 3620 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 604 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 2652 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 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 0 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 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 1 ms 2648 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 2652 KB Output is correct
36 Correct 3 ms 2908 KB Output is correct
37 Correct 4 ms 2908 KB Output is correct
38 Correct 22 ms 2908 KB Output is correct
39 Correct 4 ms 2920 KB Output is correct
40 Correct 5 ms 2964 KB Output is correct
41 Correct 28 ms 2964 KB Output is correct
42 Correct 5 ms 2908 KB Output is correct
43 Correct 6 ms 2908 KB Output is correct
44 Correct 34 ms 2908 KB Output is correct
45 Correct 6 ms 2908 KB Output is correct
46 Correct 6 ms 2908 KB Output is correct
47 Correct 43 ms 2908 KB Output is correct
48 Correct 6 ms 3164 KB Output is correct
49 Correct 7 ms 3164 KB Output is correct
50 Correct 55 ms 3164 KB Output is correct
51 Correct 10 ms 3164 KB Output is correct
52 Correct 8 ms 3240 KB Output is correct
53 Correct 62 ms 3164 KB Output is correct
54 Correct 8 ms 3164 KB Output is correct
55 Correct 10 ms 3220 KB Output is correct
56 Correct 71 ms 3164 KB Output is correct
57 Correct 12 ms 3732 KB Output is correct
58 Correct 12 ms 3420 KB Output is correct
59 Correct 82 ms 3564 KB Output is correct
60 Correct 11 ms 3676 KB Output is correct
61 Correct 13 ms 3676 KB Output is correct
62 Correct 101 ms 3792 KB Output is correct
63 Correct 86 ms 3784 KB Output is correct
64 Correct 145 ms 3924 KB Output is correct
65 Correct 139 ms 3672 KB Output is correct
66 Correct 101 ms 3672 KB Output is correct
67 Correct 93 ms 3676 KB Output is correct
68 Correct 30 ms 3676 KB Output is correct
69 Correct 26 ms 3676 KB Output is correct
70 Correct 23 ms 3676 KB Output is correct
71 Correct 20 ms 3676 KB Output is correct
72 Correct 15 ms 3676 KB Output is correct
73 Correct 21 ms 3932 KB Output is correct
74 Correct 41 ms 3928 KB Output is correct
75 Correct 41 ms 3932 KB Output is correct
76 Correct 41 ms 3928 KB Output is correct
77 Correct 48 ms 3932 KB Output is correct
78 Correct 52 ms 3672 KB Output is correct
79 Correct 35 ms 3676 KB Output is correct
80 Correct 42 ms 3676 KB Output is correct
81 Correct 53 ms 3672 KB Output is correct
82 Correct 37 ms 3928 KB Output is correct
83 Correct 58 ms 3672 KB Output is correct
84 Correct 52 ms 3676 KB Output is correct
85 Correct 46 ms 3676 KB Output is correct
86 Correct 56 ms 3676 KB Output is correct
87 Correct 54 ms 3676 KB Output is correct
88 Correct 58 ms 3672 KB Output is correct
89 Correct 59 ms 3676 KB Output is correct
90 Correct 62 ms 3812 KB Output is correct
91 Correct 59 ms 3672 KB Output is correct
92 Correct 62 ms 3676 KB Output is correct