# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
802749 |
2023-08-02T14:00:00 Z |
_VIBE |
Mecho (IOI09_mecho) |
C++17 |
|
352 ms |
65536 KB |
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define pll pair<ll,ll>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
typedef tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>ordered_multiset;
#define ugp gp_hash_table<ll,ll,custom_hash>
#define umap unordered_map<ll,ll,custom_hash>
#define pb push_back
#define len(v) ((ll)(v.size()))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ff first
#define ss second
#define inp(a) for(auto &x:a) cin>>x;
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vs vector<string>
#define vpll vector<pair<ll,ll>>
#define pn cout<<"NO\n"
#define py cout<<"YES\n"
#define mid(s,e) s+(e-s)/2
#define endl '\n'
const ll MAX_SIZE = 200005;
const ll ninf = (-1)*(1ll<<60);
const ll inf = 1ll<<60;
const ll mod = 1000000007;
//typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
ll setbits(ll x){
return __builtin_popcountll(x);
}
ll tz(ll x){
return __builtin_ctz(x);
}
ll fastpow(ll a ,ll b){
ll res=1;
while(b){
if(b&1) res*=a;
a*=a;
b/=2;
}
return res;
}
//bitset<1000001> isprime;
//void seive(){
// isprime.set();
// isprime[1]=0;
// for(ll i=4;i<MAX_SIZE;i+=2) isprime[i]=false;
//
// for(ll i=3;i<MAX_SIZE;i+=2){
// if(isprime[i]){
// ll j=i;
// while(j*i<MAX_SIZE){
// isprime[j*i]=false;
// j+=2;
// }
// }
// }
//
//}
ll t[4*MAX_SIZE];
int x,y;
int n,step_size;
int tx,ty;
vector<vector<char>> g;
vector<vector<int>> timer;
bool isvalid1(int i,int j){
if(i<0 || i>=n || j<0 || j>=n) return false;
if(g[i][j]=='H' || g[i][j]=='T') return false;
return true;
}
vector<pair<int,int>> movem={{1,0},{-1,0},{0,1},{0,-1}};
bool check(int time){
queue<pair<pair<int,int>,int>> q;
q.push({{x,y},0});
vector<vector<bool>> vis(n,vector<bool>(n,false));
while(!q.empty()){
pair<pair<int,int>,int> p=q.front();
q.pop();
int i=p.ff.ff,j=p.ff.ss;
int s=p.ss;
if(timer[i][j]<=time+s/step_size) continue;
vis[i][j]=true;
for(auto p1:movem){
int i1=i+p1.ff;
int j1=j+p1.ss;
if(isvalid1(i1,j1)){
q.push({{i1,j1},s+1});
if(i1==tx && j1==ty) return true;
}
}
}
return false;
}
bool isvalid(int i,int j){
if(i<0 || i>=n || j<0 || j>=n) return false;
if(g[i][j]=='T' || g[i][j]=='D') return false;
return true;
}
void VIBE_KA_CODE(ll TC)
{
cin>>n>>step_size;
g.resize(n,vector<char>(n));
timer.resize(n,vector<int>(n,1e9));
queue<pair<int,int>> q;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>g[i][j];
if(g[i][j]=='M'){
x=i;y=j;
}
if(g[i][j]=='H'){
q.push({i,j});
timer[i][j]=0;
}
if(g[i][j]=='D'){
tx=i;ty=j;
}
}
}
while(!q.empty()){
pair<int,int> p=q.front();
q.pop();
for(auto p1:movem){
int i1=p.ff+p1.ff;
int j1=p.ss+p1.ss;
if(isvalid(i1,j1) && timer[i1][j1]>timer[p.ff][p.ss]+1){
q.push({i1,j1});
timer[i1][j1]=timer[p.ff][p.ss]+1;
}
}
}
int low=0,high=timer[x][y];
int ans=-1;
while(low<=high){
int m=mid(low,high);
if(check(m)){
ans=m;
low=m+1;
}
else{
high=m-1;
}
}
cout<<ans;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
#ifdef ONLINEJUDGE
freopen("input.txt","r",stdin); //can need to change file . this one for taking input
freopen("output.txt","w",stdout); // this one for output
#endif
//seive();
ll Tc=1;
// cin>>Tc;
for(ll tc=1;tc<=Tc;tc++)
{
VIBE_KA_CODE(tc);
}
return 0;
}
/*
1. Initialize all variables! Arrays etc.
2. Min of Max and Max of Min, such type of problems usually require Binary Search
3. Use to_string and stoi,atoi for conversions to suitable types.
4. For setting precision of n digits after decimal, use cout<<fixed;cout<<setprecision(n); before output, or at start of code if all outputs have same precision(include ios and iomanip header files.
5. Instead os using ceil(a/b), use (a+b-1)/b or (a-1)/b+1.
6. For char to int, subtract '0', for int to char add'0'
*/
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:276:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
276 | freopen("Error.txt", "w", stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
1108 KB |
Output is correct |
7 |
Runtime error |
119 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Runtime error |
103 ms |
65536 KB |
Execution killed with signal 9 |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Runtime error |
104 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Runtime error |
99 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Runtime error |
97 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Runtime error |
352 ms |
65536 KB |
Execution killed with signal 9 |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Runtime error |
137 ms |
65536 KB |
Execution killed with signal 9 |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Runtime error |
133 ms |
65536 KB |
Execution killed with signal 9 |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Runtime error |
131 ms |
65536 KB |
Execution killed with signal 9 |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Runtime error |
136 ms |
65536 KB |
Execution killed with signal 9 |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Runtime error |
132 ms |
65536 KB |
Execution killed with signal 9 |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Runtime error |
139 ms |
65536 KB |
Execution killed with signal 9 |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Runtime error |
131 ms |
65536 KB |
Execution killed with signal 9 |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Runtime error |
135 ms |
65536 KB |
Execution killed with signal 9 |
33 |
Correct |
3 ms |
1108 KB |
Output is correct |
34 |
Runtime error |
133 ms |
65536 KB |
Execution killed with signal 9 |
35 |
Runtime error |
132 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Correct |
6 ms |
1264 KB |
Output is correct |
37 |
Runtime error |
135 ms |
65536 KB |
Execution killed with signal 9 |
38 |
Runtime error |
160 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Correct |
5 ms |
1492 KB |
Output is correct |
40 |
Runtime error |
134 ms |
65536 KB |
Execution killed with signal 9 |
41 |
Runtime error |
148 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Correct |
7 ms |
1852 KB |
Output is correct |
43 |
Runtime error |
146 ms |
65536 KB |
Execution killed with signal 9 |
44 |
Runtime error |
139 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Correct |
9 ms |
2148 KB |
Output is correct |
46 |
Runtime error |
146 ms |
65536 KB |
Execution killed with signal 9 |
47 |
Runtime error |
138 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Correct |
9 ms |
2516 KB |
Output is correct |
49 |
Runtime error |
143 ms |
65536 KB |
Execution killed with signal 9 |
50 |
Runtime error |
164 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
10 ms |
2900 KB |
Output is correct |
52 |
Runtime error |
144 ms |
65536 KB |
Execution killed with signal 9 |
53 |
Runtime error |
140 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
12 ms |
3284 KB |
Output is correct |
55 |
Runtime error |
142 ms |
65536 KB |
Execution killed with signal 9 |
56 |
Runtime error |
138 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
13 ms |
3796 KB |
Output is correct |
58 |
Runtime error |
137 ms |
65536 KB |
Execution killed with signal 9 |
59 |
Runtime error |
142 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
15 ms |
4164 KB |
Output is correct |
61 |
Runtime error |
142 ms |
65536 KB |
Execution killed with signal 9 |
62 |
Runtime error |
143 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Runtime error |
151 ms |
65536 KB |
Execution killed with signal 9 |
64 |
Runtime error |
145 ms |
65536 KB |
Execution killed with signal 9 |
65 |
Runtime error |
150 ms |
65536 KB |
Execution killed with signal 9 |
66 |
Runtime error |
150 ms |
65536 KB |
Execution killed with signal 9 |
67 |
Runtime error |
149 ms |
65536 KB |
Execution killed with signal 9 |
68 |
Runtime error |
152 ms |
65536 KB |
Execution killed with signal 9 |
69 |
Runtime error |
151 ms |
65536 KB |
Execution killed with signal 9 |
70 |
Runtime error |
154 ms |
65536 KB |
Execution killed with signal 9 |
71 |
Runtime error |
153 ms |
65536 KB |
Execution killed with signal 9 |
72 |
Runtime error |
161 ms |
65536 KB |
Execution killed with signal 9 |
73 |
Runtime error |
114 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
117 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
127 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
116 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
115 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
114 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
117 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
114 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
112 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
119 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
112 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
114 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
112 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
117 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
116 ms |
65536 KB |
Execution killed with signal 9 |