# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1094990 | Rafat | Mecho (IOI09_mecho) | C++14 | 126 ms | 7244 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <time.h>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// to erase in multiset-> less_equal<T> and
// s.erase(s.find_by_order(s.order_of_key(x)))
// lower_bound(x)=>(cannot use the stl lower_bound function)
// ll idx = s.order_of_key(x)
// if(idx == s.size()) -> no lower_bound
// else lb = *s.find_by_order(idx) // as 0-indexing
// idx-1 will give highest value which is strictly less than x
// for upper_bound->do the same with (x+1)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef tuple<ll, ll, ll> t64;
typedef vector<t64> vt64;
typedef vector<vt64> vvt64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef vector<vector<p32> > vvp32;
typedef vector<bool> vb;
ll mod = 1e9+7, MOD = 998244353;
double eps = 1e-12;
// #define forn(i,e) for(ll i = 0; i < e; i++)
#define FOR(s, e, i) for(int i = s; i <= e; i++)
// #define rforn(i,s) for(ll i = s; i >= 0; i--)
#define ROF(s ,e, i) for(int i = s; i >= e; i--)
#define coutAll(A) for(auto asdafas : A) cout << asdafas << " "; cout << "\n";
#define foutAll(A) for(auto asdafas : A) fout << asdafas << " "; cout << "\n";
#define cinAll(A) for(auto &asdafas : A) cin >> asdafas;
#define finAll(A) for(auto &asdafas : A) fin >> asdafas;
#define minpq priority_queue<ll, v64, greater<ll>>
#define maxpq priority_queue<ll>
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
ll inf = LLONG_MAX;
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<ll, ll> pll;
typedef pair<ll, ll> pii;
//#define MAXN 1000000
struct info{
int i, j, dist;
bool isbear;
// info(int x, int y, bool b) : i(x), j(y), dist(0), isbear(b){}
};
void solve(int it)
{
ll n, s;
cin >> n >> s;
vector<string>A(n);
cinAll(A);
int bi, bj, hi, hj;
vp32 hives;
queue<p32>q;
vv32 dist_bee(n, v32(n, 1e9));
FOR(0, n - 1, i){
FOR(0, n - 1, j){
if(A[i][j] == 'D'){
hi = i;
hj = j;
continue;
}
if(A[i][j] == 'M'){
bi = i;
bj = j;
continue;
}
if(A[i][j] == 'H'){
hives.emplace_back(i, j);
q.push({i, j});
dist_bee[i][j] = 0;
}
}
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
while(!q.empty()){
auto u = q.front();
q.pop();
FOR(0, 3, i){
int ni = u.fi + dx[i];
int nj = u.se + dy[i];
if(ni < 0) continue;
if(nj < 0) continue;
if(ni > n-1) continue;
if(nj > n-1) continue;
if(A[ni][nj] == 'T') continue;
if(A[ni][nj] == 'D') continue;
if(dist_bee[ni][nj] != 1e9) continue;
dist_bee[ni][nj] = dist_bee[u.fi][u.se] + 1;
q.push({ni, nj});
}
}
auto ok = [&](ll rest)->bool{
q.push({bi, bj});
vv32 dist(n, v32(n, 1e9));
dist[bi][bj] = 0;
if(rest >= dist_bee[bi][bj]) return false;
while(!q.empty()){
auto u = q.front();
q.pop();
FOR(0, 3, i){
int ni = u.fi + dx[i];
int nj = u.se + dy[i];
if(ni < 0) continue;
if(nj < 0) continue;
if(ni > n-1) continue;
if(nj > n-1) continue;
if(A[ni][nj] == 'T') continue;
if(dist[ni][nj] != 1e9) continue;
if((dist[u.fi][u.se] + 1)/s+rest >= dist_bee[ni][nj]) continue;
if(A[ni][nj] == 'D') return true;
dist[ni][nj] = dist[u.fi][u.se] + 1;
q.push({ni, nj});
}
}
return false;
};
int l = 0, r = 1e6, ans = -1;
while(l <= r){
ll mid = (l + r) / 2;
if(ok(mid)){
ans = mid;
l = mid + 1;
}else{
r = mid - 1;
}
}
cout << ans;
}
int main()
{
fast_cin();
ll t = 1;
// cin >> t;
for(int it=1; it<=t; it++)
{
//cout << "Case " << it << ": ";
solve(it);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |