Submission #986158

# Submission time Handle Problem Language Result Execution time Memory
986158 2024-05-19T22:38:36 Z sarthakgangwal Mecho (IOI09_mecho) C++14
100 / 100
194 ms 26260 KB
//This code is written by sarthakgangwal
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define tc int t;cin>>t;while(t--)
#define el "/n"
#define sp ' '
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define ll long long int
#define ld long double
#define vll vector<long long int>
#define vvll vector<vector<long long int>>
#define vvpll vector<vector<pair<long long int, long long int>>>
#define vpll vector<pair<long long int,long long int>>
#define vb vector<bool>
#define pb push_back
#define pll pair<long long int,long long int>
#define ff first
#define ss second
#define I(x) ll x;cin>>x
#define CC(x) cout << (x) << endl
#define mod 1000000007
#define PI 3.14159265
long long INF = 2e18;
#define vll_in(v,n) for(ll i=0;i<=n-1;i++){ ll x;cin>>x; v.push_back(x);}
#define vvll_in(mat,r,c) for(ll i=0;i<=r-1;i++){ vector<ll> v;for(ll j=0;j<=c-1;j++){ll x;cin>>x;v.push_back(x);};mat.push_back(v);}
void print(bool k){cout << (k ? "YES" : "NO") <<"\n";}
void print(vll k,string sep = " ",string endline = "\n") {for (ll &i : k) cout << i << sep; cout << endline;}
#define all(x) (x).begin(), (x).end()

/*-------------------------------------Imp Concepts---------------------------------------------->
48-57 -> 0-9
65-90 -> A-Z
97-122 -> a-z
(a +-* b) mod M = ((a mod M) +-* (b mod M)) mod M.

use ordered set using oset<ll> , 2 extra functions
.order_of_key(k) gives number of items strictly smaller than k
.find_by_order(k) gives iterator to kth element in set 0 indexing
rest all functionalities are same as stl set like lowerbound upperbound erase find etc

use ordered multiset using oset<pll>
search upper_bound({val,-1})
insert insert({val,index}) index used so same val can be inserted uniquely
delete .erase( .upper_bound({val,-1}))
-----------------------------CODE BEGIN :)-----------------------------------------*/
ll forest[802][802];
vector<vector<pll>> M_ini(802,vpll(802,{INF,INF}));
vector<vector<pll>> M;

ll mx,my;
ll dx,dy;
vpll bees;
ll n,s;

ll movy[4] = {-1,0,1,0};
ll movx[4] = {0,1,0,-1};
bool ok=0;
void bfs1(){
    queue<pll> q;
    for(pll p:bees){q.push(p);}

    while(!q.empty()){
        pll top = q.front();q.pop();
        ll x =top.ff, y=top.ss;
        for(int i=0;i<4;i++){
            ll xx=x+movx[i],yy=y+movy[i];
            if(forest[xx][yy]==-1){
                forest[xx][yy]=1+forest[x][y];
                q.push({xx,yy});
            }
        }
    }
    return;
}

bool bfs2(ll st){
    M=M_ini;
    if(forest[mx][my]<=st){return 0;}
    queue<pll> q;
    q.push({mx,my});
    M[mx][my]={st,0};
    while(!q.empty()){
        pll top = q.front();q.pop();
        ll x =top.ff, y=top.ss;
        ll tim=M[x][y].ff, stp=M[x][y].ss;
        for(int i=0;i<4;i++){
            ll xx=x+movx[i],yy=y+movy[i];
            if(xx==dx && yy==dy){ok=1;return 1;}
            if(M[xx][yy].ff==INF && forest[xx][yy]!=-2){
                if(stp<s-1){M[xx][yy]={tim,stp+1};}
                else{M[xx][yy]={tim+1,0};}

                if(M[xx][yy].ff<forest[xx][yy]){
                    q.push({xx,yy});
                }
                else{
                    M[xx][yy].ff=INF;M[xx][yy].ss=INF;
                }
            }
        }
    }
    return 0;
}

int main(){
    fast
    {
        cin>>n>>s;
        char x;
        for(ll i=0;i<802;i++){
            forest[0][i]=-2;
            forest[n+1][i]=-2;
            forest[801][i]=-2;
            forest[i][0]=-2;
            forest[i][n+1]=-2;
            forest[i][801]=-2;
        }
        for(ll i=1;i<=n;i++){
            for(ll j=1;j<=n;j++){
                cin>>x;
                if(x=='M'){mx=i;my=j;forest[i][j]=-1;}
                if(x=='G'){forest[i][j]=-1;}
                if(x=='H'){forest[i][j]=0;bees.pb({i,j});}
                if(x=='T'){forest[i][j]=-2;}
                if(x=='D'){forest[i][j]=-2;dx=i;dy=j;}
            }
        }

        bfs1();
        // for(ll i=1;i<=n;i++){
        //     for(ll j=1;j<=n;j++){
        //         cout<<forest[i][j]<<sp;
        //     }
        //     cout<<endl;
        // }
        // cout<<endl;
        
        // bfs2(1);
        // for(ll i=1;i<=n;i++){
        //     for(ll j=1;j<=n;j++){
        //         cout<<M[i][j].ff<<sp;
        //     }
        //     cout<<endl;
        // }
        // cout<<endl;

        ll l=0,h=6400000;
        while(l<h){
            // cout<<l<<sp<<h<<endl;
            ll m = (l+h)/2;
            if(m==l){if(bfs2(h)){l=h;}break;}
            if(bfs2(m)){l=m;}
            else{h=m-1;}
        }
        bfs2(l);
        if(!ok){CC(-1);}
        else{CC(l);}

    }
}
# Verdict Execution time Memory Grader output
1 Correct 30 ms 25180 KB Output is correct
2 Correct 36 ms 25176 KB Output is correct
3 Correct 30 ms 25364 KB Output is correct
4 Correct 33 ms 25180 KB Output is correct
5 Correct 31 ms 25176 KB Output is correct
6 Correct 32 ms 25376 KB Output is correct
7 Correct 97 ms 25684 KB Output is correct
8 Correct 31 ms 25180 KB Output is correct
9 Correct 38 ms 25428 KB Output is correct
10 Correct 32 ms 25180 KB Output is correct
11 Correct 41 ms 25176 KB Output is correct
12 Correct 35 ms 25436 KB Output is correct
13 Correct 32 ms 25432 KB Output is correct
14 Correct 32 ms 25436 KB Output is correct
15 Correct 34 ms 25436 KB Output is correct
16 Correct 33 ms 25180 KB Output is correct
17 Correct 31 ms 25180 KB Output is correct
18 Correct 33 ms 25176 KB Output is correct
19 Correct 32 ms 25180 KB Output is correct
20 Correct 31 ms 25392 KB Output is correct
21 Correct 33 ms 25648 KB Output is correct
22 Correct 32 ms 25180 KB Output is correct
23 Correct 30 ms 25176 KB Output is correct
24 Correct 31 ms 25436 KB Output is correct
25 Correct 30 ms 25432 KB Output is correct
26 Correct 34 ms 25436 KB Output is correct
27 Correct 33 ms 25436 KB Output is correct
28 Correct 34 ms 25680 KB Output is correct
29 Correct 33 ms 25436 KB Output is correct
30 Correct 33 ms 25432 KB Output is correct
31 Correct 31 ms 25436 KB Output is correct
32 Correct 31 ms 25436 KB Output is correct
33 Correct 34 ms 25708 KB Output is correct
34 Correct 34 ms 25692 KB Output is correct
35 Correct 53 ms 25692 KB Output is correct
36 Correct 34 ms 25692 KB Output is correct
37 Correct 35 ms 25688 KB Output is correct
38 Correct 62 ms 25692 KB Output is correct
39 Correct 34 ms 25688 KB Output is correct
40 Correct 35 ms 25688 KB Output is correct
41 Correct 75 ms 25940 KB Output is correct
42 Correct 34 ms 25688 KB Output is correct
43 Correct 37 ms 25688 KB Output is correct
44 Correct 71 ms 25692 KB Output is correct
45 Correct 38 ms 25436 KB Output is correct
46 Correct 39 ms 25648 KB Output is correct
47 Correct 83 ms 25688 KB Output is correct
48 Correct 38 ms 25432 KB Output is correct
49 Correct 37 ms 25436 KB Output is correct
50 Correct 94 ms 25432 KB Output is correct
51 Correct 40 ms 25684 KB Output is correct
52 Correct 40 ms 25436 KB Output is correct
53 Correct 99 ms 25560 KB Output is correct
54 Correct 40 ms 25436 KB Output is correct
55 Correct 38 ms 25436 KB Output is correct
56 Correct 111 ms 25432 KB Output is correct
57 Correct 45 ms 25596 KB Output is correct
58 Correct 39 ms 25436 KB Output is correct
59 Correct 130 ms 25436 KB Output is correct
60 Correct 42 ms 25692 KB Output is correct
61 Correct 40 ms 25692 KB Output is correct
62 Correct 147 ms 25936 KB Output is correct
63 Correct 120 ms 25852 KB Output is correct
64 Correct 172 ms 25680 KB Output is correct
65 Correct 194 ms 25692 KB Output is correct
66 Correct 154 ms 26260 KB Output is correct
67 Correct 135 ms 25584 KB Output is correct
68 Correct 68 ms 25680 KB Output is correct
69 Correct 68 ms 25680 KB Output is correct
70 Correct 64 ms 25692 KB Output is correct
71 Correct 58 ms 25688 KB Output is correct
72 Correct 50 ms 25688 KB Output is correct
73 Correct 56 ms 25680 KB Output is correct
74 Correct 77 ms 25684 KB Output is correct
75 Correct 97 ms 25940 KB Output is correct
76 Correct 82 ms 25692 KB Output is correct
77 Correct 74 ms 25680 KB Output is correct
78 Correct 84 ms 25688 KB Output is correct
79 Correct 74 ms 25688 KB Output is correct
80 Correct 76 ms 25688 KB Output is correct
81 Correct 86 ms 25656 KB Output is correct
82 Correct 71 ms 25624 KB Output is correct
83 Correct 92 ms 25688 KB Output is correct
84 Correct 102 ms 25936 KB Output is correct
85 Correct 101 ms 25680 KB Output is correct
86 Correct 92 ms 25692 KB Output is correct
87 Correct 95 ms 25692 KB Output is correct
88 Correct 101 ms 25940 KB Output is correct
89 Correct 94 ms 25692 KB Output is correct
90 Correct 94 ms 25692 KB Output is correct
91 Correct 97 ms 25768 KB Output is correct
92 Correct 98 ms 25688 KB Output is correct