Submission #712048

# Submission time Handle Problem Language Result Execution time Memory
712048 2023-03-18T02:49:10 Z Narka Mecho (IOI09_mecho) C++14
100 / 100
192 ms 5904 KB
#include<bits/stdc++.h>
using namespace std ;
# define F first
# define S second
# define int long long
# define in insert
# define pb push_back
# define pob pop_back
# define INF INT_MAX
# define INFL 1e18 + 10 
# define mod 1000000007
# define vi vector<int>
# define pii pair<int , int>
# define vvi vector< vector<int> >
# define vpi vector<pair<int, int>> 
# define all(v) v.begin() , v.end()
# define debug(x) cerr << '\n' << #x << " = " << x << '\n' ;
# define rep( i , s , e ) for( int i = s ; i <= e ; i++ )
# define repR( i , e , s ) for( int i = e ; i >= s ; i-- )
# define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
# define endl "\n"
int mp[801][801];
vector<array<int, 2>> hives;
int st[2], nd[2];
int n, s;
queue<array<int, 2>> q;

bool safe(int i, int j){
  if(mp[i][j] == -2) return false;
  if(j < n && j >= 0 && i < n && i >= 0) return true;
  return false; 
}
 
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

void attack(){
  for(auto x : hives){
    rep(i, 0, 3){
      int k = x[0] + dx[i];
      int y = x[1] + dy[i];
      if(safe(k , y) && mp[k][y] != 1){
        mp[k][y] = 1;
        q.push({k, y});
      }
    }
  }
  while(!q.empty()){
    array<int, 2> t = q.front(); q.pop();
    rep(i, 0, 3){
      int x = t[0] + dx[i];
      int y = t[1] + dy[i];
      if(safe(x, y)){
        if(mp[x][y] == 0) mp[x][y] = mp[t[0]][t[1]] + 1, q.push({x, y});
      } 
    }
  }
}

bool is_can(int v){
  vector<vector<bool>> vis(805, vector<bool>(805, false));
  q.push({st[0], st[1]});
  if(mp[st[0]][st[1]] <= v) return false;
  vis[st[0]][st[1]] = true;
  int t = 1, b = 0, stp = 1, c = 0;
  while(!q.empty()){
    array<int, 2> tt = q.front(); q.pop(), t--;  
    rep(i, 0, 3){
     int x = tt[0] + dx[i];
     int y = tt[1] + dy[i];
     if(x == nd[0] && y == nd[1]) return true;
     if(safe(x, y)&& !vis[x][y] && stp + v <= mp[x][y] ){
      if(c+1 == s && stp + v == mp[x][y]) continue; 
      b++;
      q.push({x, y});
      vis[x][y] = true;
     } 
    }
    if(t == 0){
      t = b; c++; b = 0;
    }
    if(c >= s){
      c = 0; stp++;
    }
  }
  return false;
}

void solve(){
  cin >> n >> s;
  rep(i, 0, n-1){
    string a;
    cin >> a;
    rep(j, 0, n-1){
      if(a[j] == 'T') mp[i][j] = -2;
      else if(a[j] == 'M') st[0] = i, st[1] = j;
      else if(a[j] == 'H') mp[i][j] = 1, hives.pb({i, j});
      else if(a[j] == 'D') mp[i][j] = -2, nd[0] = i, nd[1] = j;
    }
  }
  attack();
  int l = 0, r = 1e9, res = INF;
  while(l <= r){
    int md = (l+r)>>1;
    queue<array<int, 2>> empty;
    swap( q, empty );
    if(is_can(md)){
      res = md;
      l = md+1;
    }else{
      r = md-1;
    }
  }
  if(res == INF) cout << -1 << endl;
  else cout << res << endl;
}

signed main(){
  solve();
  return 0;
} 
# Verdict Execution time Memory Grader output
1 Correct 4 ms 340 KB Output is correct
2 Correct 4 ms 436 KB Output is correct
3 Correct 4 ms 340 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 4 ms 340 KB Output is correct
6 Correct 6 ms 468 KB Output is correct
7 Correct 100 ms 5472 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 5 ms 468 KB Output is correct
10 Correct 5 ms 480 KB Output is correct
11 Correct 5 ms 468 KB Output is correct
12 Correct 6 ms 596 KB Output is correct
13 Correct 4 ms 652 KB Output is correct
14 Correct 4 ms 704 KB Output is correct
15 Correct 4 ms 468 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 4 ms 468 KB Output is correct
18 Correct 4 ms 468 KB Output is correct
19 Correct 4 ms 468 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
21 Correct 4 ms 596 KB Output is correct
22 Correct 5 ms 596 KB Output is correct
23 Correct 4 ms 636 KB Output is correct
24 Correct 6 ms 596 KB Output is correct
25 Correct 4 ms 596 KB Output is correct
26 Correct 4 ms 596 KB Output is correct
27 Correct 4 ms 612 KB Output is correct
28 Correct 5 ms 596 KB Output is correct
29 Correct 5 ms 724 KB Output is correct
30 Correct 5 ms 724 KB Output is correct
31 Correct 5 ms 724 KB Output is correct
32 Correct 5 ms 724 KB Output is correct
33 Correct 9 ms 2516 KB Output is correct
34 Correct 9 ms 2620 KB Output is correct
35 Correct 24 ms 2624 KB Output is correct
36 Correct 10 ms 2872 KB Output is correct
37 Correct 9 ms 2900 KB Output is correct
38 Correct 32 ms 2900 KB Output is correct
39 Correct 11 ms 3184 KB Output is correct
40 Correct 12 ms 3156 KB Output is correct
41 Correct 41 ms 3252 KB Output is correct
42 Correct 13 ms 3540 KB Output is correct
43 Correct 12 ms 3564 KB Output is correct
44 Correct 48 ms 3540 KB Output is correct
45 Correct 14 ms 3816 KB Output is correct
46 Correct 16 ms 3776 KB Output is correct
47 Correct 58 ms 3884 KB Output is correct
48 Correct 21 ms 4180 KB Output is correct
49 Correct 16 ms 4064 KB Output is correct
50 Correct 76 ms 4188 KB Output is correct
51 Correct 20 ms 4436 KB Output is correct
52 Correct 24 ms 4488 KB Output is correct
53 Correct 77 ms 4492 KB Output is correct
54 Correct 20 ms 4696 KB Output is correct
55 Correct 20 ms 4828 KB Output is correct
56 Correct 94 ms 4816 KB Output is correct
57 Correct 26 ms 5072 KB Output is correct
58 Correct 23 ms 5048 KB Output is correct
59 Correct 110 ms 5076 KB Output is correct
60 Correct 27 ms 5324 KB Output is correct
61 Correct 24 ms 5404 KB Output is correct
62 Correct 121 ms 5352 KB Output is correct
63 Correct 107 ms 5452 KB Output is correct
64 Correct 178 ms 5432 KB Output is correct
65 Correct 192 ms 5436 KB Output is correct
66 Correct 120 ms 5440 KB Output is correct
67 Correct 110 ms 5324 KB Output is correct
68 Correct 52 ms 5332 KB Output is correct
69 Correct 50 ms 5436 KB Output is correct
70 Correct 38 ms 5464 KB Output is correct
71 Correct 39 ms 5332 KB Output is correct
72 Correct 39 ms 5384 KB Output is correct
73 Correct 47 ms 5904 KB Output is correct
74 Correct 65 ms 5888 KB Output is correct
75 Correct 71 ms 5900 KB Output is correct
76 Correct 102 ms 5900 KB Output is correct
77 Correct 99 ms 5868 KB Output is correct
78 Correct 77 ms 5836 KB Output is correct
79 Correct 61 ms 5836 KB Output is correct
80 Correct 68 ms 5844 KB Output is correct
81 Correct 87 ms 5760 KB Output is correct
82 Correct 70 ms 5772 KB Output is correct
83 Correct 92 ms 5752 KB Output is correct
84 Correct 79 ms 5724 KB Output is correct
85 Correct 82 ms 5672 KB Output is correct
86 Correct 100 ms 5716 KB Output is correct
87 Correct 93 ms 5744 KB Output is correct
88 Correct 90 ms 5636 KB Output is correct
89 Correct 86 ms 5632 KB Output is correct
90 Correct 91 ms 5692 KB Output is correct
91 Correct 94 ms 5588 KB Output is correct
92 Correct 94 ms 5640 KB Output is correct