Submission #986146

# Submission time Handle Problem Language Result Execution time Memory
986146 2024-05-19T21:47:14 Z sarthakgangwal Mecho (IOI09_mecho) C++14
79 / 100
165 ms 26204 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};
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){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]=-1;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;
        // CC(bfs2(1));
        // for(ll i=1;i<=n;i++){
        //     for(ll j=1;j<=n;j++){
        //         cout<<M[i][j].ff<<sp;
        //     }
        //     cout<<endl;
        // }
        
        ll l=0,h=forest[dx][dy];
        while(l<h){
            ll m = (l+h)/2;
            if(m==l){if(bfs2(h)){l=h;}break;}
            if(bfs2(m)){l=m;}
            else{h=m-1;}
        }
        CC(l);

    }
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 25176 KB Output is correct
2 Correct 10 ms 25180 KB Output is correct
3 Correct 10 ms 25180 KB Output is correct
4 Correct 11 ms 25180 KB Output is correct
5 Correct 11 ms 25180 KB Output is correct
6 Correct 10 ms 25180 KB Output is correct
7 Correct 75 ms 25684 KB Output is correct
8 Incorrect 11 ms 25176 KB Output isn't correct
9 Correct 12 ms 25180 KB Output is correct
10 Correct 15 ms 25296 KB Output is correct
11 Correct 13 ms 25180 KB Output is correct
12 Incorrect 9 ms 25432 KB Output isn't correct
13 Correct 12 ms 25428 KB Output is correct
14 Incorrect 11 ms 25436 KB Output isn't correct
15 Correct 15 ms 25228 KB Output is correct
16 Correct 15 ms 25180 KB Output is correct
17 Correct 17 ms 25340 KB Output is correct
18 Correct 16 ms 25180 KB Output is correct
19 Correct 18 ms 25180 KB Output is correct
20 Correct 15 ms 25176 KB Output is correct
21 Correct 17 ms 25428 KB Output is correct
22 Correct 16 ms 25432 KB Output is correct
23 Correct 18 ms 25436 KB Output is correct
24 Correct 17 ms 25436 KB Output is correct
25 Correct 20 ms 25436 KB Output is correct
26 Correct 18 ms 25436 KB Output is correct
27 Correct 19 ms 25432 KB Output is correct
28 Correct 18 ms 25460 KB Output is correct
29 Correct 19 ms 25464 KB Output is correct
30 Correct 19 ms 25472 KB Output is correct
31 Correct 20 ms 25432 KB Output is correct
32 Correct 19 ms 25436 KB Output is correct
33 Correct 26 ms 25692 KB Output is correct
34 Correct 25 ms 25692 KB Output is correct
35 Correct 20 ms 25692 KB Output is correct
36 Correct 28 ms 25692 KB Output is correct
37 Correct 26 ms 25692 KB Output is correct
38 Correct 22 ms 25692 KB Output is correct
39 Correct 29 ms 25692 KB Output is correct
40 Correct 27 ms 25836 KB Output is correct
41 Correct 25 ms 25692 KB Output is correct
42 Correct 29 ms 25692 KB Output is correct
43 Correct 32 ms 25692 KB Output is correct
44 Correct 26 ms 25908 KB Output is correct
45 Correct 31 ms 25860 KB Output is correct
46 Correct 29 ms 25692 KB Output is correct
47 Correct 30 ms 25692 KB Output is correct
48 Correct 33 ms 25692 KB Output is correct
49 Correct 36 ms 25936 KB Output is correct
50 Correct 34 ms 25776 KB Output is correct
51 Correct 34 ms 25684 KB Output is correct
52 Correct 34 ms 25816 KB Output is correct
53 Correct 36 ms 25688 KB Output is correct
54 Correct 35 ms 25692 KB Output is correct
55 Correct 32 ms 25888 KB Output is correct
56 Correct 52 ms 26084 KB Output is correct
57 Correct 36 ms 25916 KB Output is correct
58 Correct 35 ms 25936 KB Output is correct
59 Correct 45 ms 25948 KB Output is correct
60 Correct 36 ms 25948 KB Output is correct
61 Correct 35 ms 26096 KB Output is correct
62 Correct 51 ms 25944 KB Output is correct
63 Correct 103 ms 25940 KB Output is correct
64 Correct 165 ms 25948 KB Output is correct
65 Correct 163 ms 26116 KB Output is correct
66 Correct 139 ms 25940 KB Output is correct
67 Incorrect 110 ms 25948 KB Output isn't correct
68 Correct 48 ms 26204 KB Output is correct
69 Correct 49 ms 25948 KB Output is correct
70 Correct 46 ms 25944 KB Output is correct
71 Correct 47 ms 25936 KB Output is correct
72 Correct 39 ms 26164 KB Output is correct
73 Correct 35 ms 26204 KB Output is correct
74 Correct 54 ms 26164 KB Output is correct
75 Correct 52 ms 26204 KB Output is correct
76 Correct 59 ms 26160 KB Output is correct
77 Correct 53 ms 26196 KB Output is correct
78 Incorrect 53 ms 26204 KB Output isn't correct
79 Correct 49 ms 26196 KB Output is correct
80 Correct 63 ms 25948 KB Output is correct
81 Correct 54 ms 26148 KB Output is correct
82 Correct 60 ms 26168 KB Output is correct
83 Correct 60 ms 26180 KB Output is correct
84 Correct 56 ms 25940 KB Output is correct
85 Correct 73 ms 26192 KB Output is correct
86 Correct 64 ms 26192 KB Output is correct
87 Correct 72 ms 26196 KB Output is correct
88 Correct 55 ms 26024 KB Output is correct
89 Correct 67 ms 26044 KB Output is correct
90 Correct 66 ms 26192 KB Output is correct
91 Correct 70 ms 25948 KB Output is correct
92 Correct 65 ms 25980 KB Output is correct