Submission #990092

# Submission time Handle Problem Language Result Execution time Memory
990092 2024-05-29T14:37:27 Z bachhoangxuan Mecho (IOI09_mecho) C++17
100 / 100
122 ms 11768 KB
// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const long long inf=1e18;
const int mod=998244353;
const int maxn=805;
const int B=650;
const int maxs=655;
const int maxm=200005;
const int maxq=1000005;
const int maxl=25;
const int maxa=1000000;
const int root=3;
int power(int a,int n){
    int res=1;
    while(n){
        if(n&1) res=res*a%mod;
        a=a*a%mod;n>>=1;
    }
    return res;
}
const int iroot=power(3,mod-2);
const int base=101;

int sx,sy,tx,ty;
int n,S,d[maxn][maxn],dd[maxn][maxn];
char c[maxn][maxn];
int dx[]={0,0,1,-1},
    dy[]={1,-1,0,0};

bool check(int k){
    memset(dd,-1,sizeof(dd));
    dd[sx][sy]=k*S;
    queue<pii> q;
    q.push({sx,sy});
    //cout << "check " << k << '\n';
    while(!q.empty()){
        auto [x,y]=q.front();q.pop();
        //cout << x << ' ' << y << ' ' << dd[x][y] << '\n';
        if(x==tx && y==ty) return true;
        if(dd[x][y]/S>=d[x][y]) continue;
        for(int t=0;t<4;t++){
            int xt=x+dx[t],yt=y+dy[t];
            if(xt<=0 || yt<=0 || xt>n || yt>n || c[xt][yt]=='T' || dd[xt][yt]!=-1) continue;
            dd[xt][yt]=dd[x][y]+1;
            q.push({xt,yt});
        }
    }
    return false;
}

void solve(){
    cin >> n >> S;
    memset(d,-1,sizeof(d));
    queue<pii> q;
    for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){
        cin >> c[i][j];
        if(c[i][j]=='H'){
            d[i][j]=0;
            q.push({i,j});
        }
        else if(c[i][j]=='M') sx=i,sy=j;
        else if(c[i][j]=='D') tx=i,ty=j;
    }
    while(!q.empty()){
        auto [x,y]=q.front();q.pop();
        //cout << x << ' ' << y << ' ' << d[x][y] << '\n';
        for(int t=0;t<4;t++){
            int xt=x+dx[t],yt=y+dy[t];
            if(xt<=0 || yt<=0 || xt>n || yt>n || c[xt][yt]=='T' || d[xt][yt]!=-1 || c[xt][yt]=='D') continue;
            d[xt][yt]=d[x][y]+1;
            q.push({xt,yt});
        }
    }
    int l=0,r=n*n,res=-1;
    while(l<=r){
        int mid=(l+r)>>1;
        if(check(mid)) res=mid,l=mid+1;
        else r=mid-1;
    }
    cout << res << '\n';
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int test=1;//cin >> test;
    while(test--) solve();
}

# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 11064 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 2 ms 10844 KB Output is correct
5 Correct 2 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 50 ms 11448 KB Output is correct
8 Correct 2 ms 10844 KB Output is correct
9 Correct 3 ms 10844 KB Output is correct
10 Correct 2 ms 11068 KB Output is correct
11 Correct 2 ms 10844 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 2 ms 11100 KB Output is correct
14 Correct 2 ms 11100 KB Output is correct
15 Correct 2 ms 10844 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 2 ms 10844 KB Output is correct
21 Correct 2 ms 10844 KB Output is correct
22 Correct 2 ms 10844 KB Output is correct
23 Correct 2 ms 11100 KB Output is correct
24 Correct 2 ms 10976 KB Output is correct
25 Correct 3 ms 11096 KB Output is correct
26 Correct 3 ms 11100 KB Output is correct
27 Correct 2 ms 11100 KB Output is correct
28 Correct 3 ms 11116 KB Output is correct
29 Correct 2 ms 11100 KB Output is correct
30 Correct 2 ms 11100 KB Output is correct
31 Correct 2 ms 11100 KB Output is correct
32 Correct 3 ms 10968 KB Output is correct
33 Correct 5 ms 11100 KB Output is correct
34 Correct 5 ms 11236 KB Output is correct
35 Correct 14 ms 11100 KB Output is correct
36 Correct 5 ms 11236 KB Output is correct
37 Correct 9 ms 11140 KB Output is correct
38 Correct 17 ms 11240 KB Output is correct
39 Correct 6 ms 11096 KB Output is correct
40 Correct 6 ms 11100 KB Output is correct
41 Correct 29 ms 11100 KB Output is correct
42 Correct 7 ms 11100 KB Output is correct
43 Correct 7 ms 11100 KB Output is correct
44 Correct 27 ms 11248 KB Output is correct
45 Correct 8 ms 11100 KB Output is correct
46 Correct 7 ms 11240 KB Output is correct
47 Correct 30 ms 11096 KB Output is correct
48 Correct 13 ms 11096 KB Output is correct
49 Correct 8 ms 11100 KB Output is correct
50 Correct 36 ms 11100 KB Output is correct
51 Correct 10 ms 11100 KB Output is correct
52 Correct 10 ms 11100 KB Output is correct
53 Correct 44 ms 11236 KB Output is correct
54 Correct 11 ms 11100 KB Output is correct
55 Correct 11 ms 11236 KB Output is correct
56 Correct 51 ms 11100 KB Output is correct
57 Correct 12 ms 11096 KB Output is correct
58 Correct 11 ms 11160 KB Output is correct
59 Correct 58 ms 11236 KB Output is correct
60 Correct 13 ms 11100 KB Output is correct
61 Correct 12 ms 11232 KB Output is correct
62 Correct 65 ms 11240 KB Output is correct
63 Correct 71 ms 11228 KB Output is correct
64 Correct 122 ms 11228 KB Output is correct
65 Correct 94 ms 11100 KB Output is correct
66 Correct 77 ms 11244 KB Output is correct
67 Correct 74 ms 11100 KB Output is correct
68 Correct 33 ms 11096 KB Output is correct
69 Correct 33 ms 11096 KB Output is correct
70 Correct 25 ms 11100 KB Output is correct
71 Correct 29 ms 11096 KB Output is correct
72 Correct 22 ms 11296 KB Output is correct
73 Correct 23 ms 11612 KB Output is correct
74 Correct 35 ms 11756 KB Output is correct
75 Correct 43 ms 11624 KB Output is correct
76 Correct 38 ms 11620 KB Output is correct
77 Correct 46 ms 11768 KB Output is correct
78 Correct 46 ms 11608 KB Output is correct
79 Correct 30 ms 11612 KB Output is correct
80 Correct 37 ms 11612 KB Output is correct
81 Correct 42 ms 11736 KB Output is correct
82 Correct 37 ms 11612 KB Output is correct
83 Correct 47 ms 11612 KB Output is correct
84 Correct 42 ms 11612 KB Output is correct
85 Correct 47 ms 11608 KB Output is correct
86 Correct 45 ms 11612 KB Output is correct
87 Correct 59 ms 11608 KB Output is correct
88 Correct 47 ms 11356 KB Output is correct
89 Correct 55 ms 11356 KB Output is correct
90 Correct 49 ms 11352 KB Output is correct
91 Correct 48 ms 11564 KB Output is correct
92 Correct 52 ms 11356 KB Output is correct