Submission #155785

# Submission time Handle Problem Language Result Execution time Memory
155785 2019-09-30T14:41:00 Z m1sch3f Mecho (IOI09_mecho) C++17
100 / 100
795 ms 4404 KB

#include <bits/stdc++.h>
using namespace std;

// types - only for stuff used a lot 
using ll = long long;
#define vv vector
#define Pp pair

// IO
#define get(x) scanf("%d",&x)
#define getl(x) scanf("%lld",&x);

// Operations
#define pb push_back
#define pob pop_back
#define sz(a) int(a.size()) 
#define re(a,b) a=max(a,b) // relax
#define ri(a,b) a=min(a,b) // relaxi

// Debugging

#ifndef LOCAL
#define cerr if (0) cerr
#else
#define cerr cout
#endif

#define print(arr,n) {for (int _ = 0; _ < n; _++) cerr<<arr[_]<<" "; cerr << endl; }
#define printv(vec)  {for (int _ : vec) cerr<<_<<" "; cerr<<endl;}

const int mod = 1e9+7, oo = 1e9;
const ll loo = 1e18;

// Functions 
ll modpow(ll a, ll b) {
	ll ans = 1; // faster modpow than recursive
	for (; b; b/=2,a=a*a%mod)
		if (b&1) ans = (ans*a)%mod;
	return ans;
}
ll gcd(ll a, ll b) {
	while (a) b%=a,swap(a,b);
	return b;
}
#define bitcount __builtin_popcountll
#define f(i,a,b) for (int i = a; i < b; i++)
#define fr(i,a,b) for (int i = b-1; i >= a; i--)


/* 

   ALRIGHT HACKERS, THIS IS WHERE THE ACTUAL CODE BEGINS

 */

const bool DEBUG = 1;

using pii = pair<int,int>;
#define IN(i,a,b) (a<=i&&i<=b)

const int N = 1000;

int n,s;
vector<pii> bees;
bool wall[N][N],visR[N][N],visB[N][N];
pii st, fin;

int di[] = {1,-1,0,0};
int dj[] = {0,0,1,-1};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifdef LOCAL
	if (DEBUG) freopen("input.txt", "r", stdin);
	if (DEBUG) freopen("output.txt", "w", stdout);
	clock_t start = clock();
#endif

	cin>>n>>s;
	f(i,0,n) f(j,0,n) wall[i][j] = 0;
	f(i,0,n) f(j,0,n) {
		char c; cin>>c;
		if (c == 'T')
			wall[i][j] = 1;
		else if (c == 'M')
			st = {i,j};
		else if (c == 'D')
			fin = {i,j};
		else if (c == 'H') 
			bees.pb({i,j});
	}
	int lo = 0, hi = N*N*2;
	auto valid = [&](int i, int j) {
		return IN(i,0,n-1) && IN(j,0,n-1) && !wall[i][j] && !visR[i][j];
	};
	auto fu = [&](int T) {
		f(i,0,n) f(j,0,n) visB[i][j] = visR[i][j] = 0;

		vector<pii> bear, bee;	
		bear.pb(st);
		visB[st.first][st.second] = 1;
		for (pii b : bees) {
			bee.pb(b);
			visR[b.first][b.second] = 1;
		}

		while (bear.size()||bee.size()) {
			// bear gets to move first
			if (T--<=0) { // eat for T rounds
				f(_,0,s) {
					vector<pii> nxt;
					for (pii pp : bear) {
						int i = pp.first, j = pp.second;
						if (visR[i][j])
							continue;
						f(k,0,4) {
							int ni = i+di[k], nj = j+dj[k];
							if (valid(ni,nj) && !visB[ni][nj]) {
								visB[ni][nj] = 1;
								nxt.pb({ni,nj});	
							}
						}	
					}
					bear = nxt;
				}
			}
			// then bee move
			vector<pii> nxt;
			for (pii pp : bee) {
				int i = pp.first, j = pp.second;
				f(k,0,4) {
					int ni = i+di[k], nj = j+dj[k];
					if (valid(ni,nj) && pii(ni,nj) != fin) {
						visR[ni][nj] = 1;
						nxt.pb({ni,nj});
					}
				}
			}
			bee = nxt;
		}
		return visB[fin.first][fin.second];
	};
	while (lo<=hi) {
		int mid = lo+hi>>1;
		if (fu(mid))
			lo = mid+1;
		else
			hi = mid-1;
	}
	cout << hi << endl;

#ifdef LOCAL
	cout << setprecision(12) << (long double)(clock()-start) / CLOCKS_PER_SEC << endl;
#endif
	return 0;
}


Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:147:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid = lo+hi>>1;
             ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 348 KB Output is correct
2 Correct 15 ms 376 KB Output is correct
3 Correct 15 ms 376 KB Output is correct
4 Correct 16 ms 376 KB Output is correct
5 Correct 15 ms 376 KB Output is correct
6 Correct 15 ms 376 KB Output is correct
7 Correct 495 ms 3352 KB Output is correct
8 Correct 16 ms 380 KB Output is correct
9 Correct 17 ms 376 KB Output is correct
10 Correct 16 ms 504 KB Output is correct
11 Correct 16 ms 376 KB Output is correct
12 Correct 17 ms 508 KB Output is correct
13 Correct 18 ms 504 KB Output is correct
14 Correct 19 ms 504 KB Output is correct
15 Correct 16 ms 376 KB Output is correct
16 Correct 16 ms 376 KB Output is correct
17 Correct 16 ms 376 KB Output is correct
18 Correct 16 ms 376 KB Output is correct
19 Correct 16 ms 376 KB Output is correct
20 Correct 16 ms 376 KB Output is correct
21 Correct 16 ms 504 KB Output is correct
22 Correct 17 ms 504 KB Output is correct
23 Correct 17 ms 504 KB Output is correct
24 Correct 17 ms 504 KB Output is correct
25 Correct 18 ms 572 KB Output is correct
26 Correct 18 ms 508 KB Output is correct
27 Correct 18 ms 504 KB Output is correct
28 Correct 18 ms 632 KB Output is correct
29 Correct 19 ms 504 KB Output is correct
30 Correct 19 ms 596 KB Output is correct
31 Correct 20 ms 504 KB Output is correct
32 Correct 19 ms 632 KB Output is correct
33 Correct 102 ms 1400 KB Output is correct
34 Correct 132 ms 1500 KB Output is correct
35 Correct 106 ms 1528 KB Output is correct
36 Correct 128 ms 1568 KB Output is correct
37 Correct 140 ms 1656 KB Output is correct
38 Correct 133 ms 1528 KB Output is correct
39 Correct 161 ms 1656 KB Output is correct
40 Correct 193 ms 1656 KB Output is correct
41 Correct 162 ms 1660 KB Output is correct
42 Correct 196 ms 1912 KB Output is correct
43 Correct 291 ms 1868 KB Output is correct
44 Correct 201 ms 1912 KB Output is correct
45 Correct 235 ms 2012 KB Output is correct
46 Correct 261 ms 2040 KB Output is correct
47 Correct 238 ms 2296 KB Output is correct
48 Correct 290 ms 2192 KB Output is correct
49 Correct 355 ms 2168 KB Output is correct
50 Correct 285 ms 2300 KB Output is correct
51 Correct 323 ms 2296 KB Output is correct
52 Correct 449 ms 2296 KB Output is correct
53 Correct 345 ms 2296 KB Output is correct
54 Correct 369 ms 2444 KB Output is correct
55 Correct 604 ms 2552 KB Output is correct
56 Correct 389 ms 2492 KB Output is correct
57 Correct 415 ms 2680 KB Output is correct
58 Correct 477 ms 2552 KB Output is correct
59 Correct 443 ms 2808 KB Output is correct
60 Correct 451 ms 2680 KB Output is correct
61 Correct 566 ms 2808 KB Output is correct
62 Correct 502 ms 2936 KB Output is correct
63 Correct 646 ms 2756 KB Output is correct
64 Correct 795 ms 2800 KB Output is correct
65 Correct 678 ms 2936 KB Output is correct
66 Correct 670 ms 2884 KB Output is correct
67 Correct 600 ms 2896 KB Output is correct
68 Correct 555 ms 3004 KB Output is correct
69 Correct 566 ms 2996 KB Output is correct
70 Correct 545 ms 2884 KB Output is correct
71 Correct 543 ms 2996 KB Output is correct
72 Correct 554 ms 2888 KB Output is correct
73 Correct 464 ms 4060 KB Output is correct
74 Correct 491 ms 4108 KB Output is correct
75 Correct 488 ms 4224 KB Output is correct
76 Correct 488 ms 4404 KB Output is correct
77 Correct 490 ms 4168 KB Output is correct
78 Correct 468 ms 3796 KB Output is correct
79 Correct 488 ms 3936 KB Output is correct
80 Correct 480 ms 3876 KB Output is correct
81 Correct 499 ms 3988 KB Output is correct
82 Correct 467 ms 3892 KB Output is correct
83 Correct 517 ms 3928 KB Output is correct
84 Correct 524 ms 3868 KB Output is correct
85 Correct 514 ms 3828 KB Output is correct
86 Correct 522 ms 3840 KB Output is correct
87 Correct 518 ms 3628 KB Output is correct
88 Correct 523 ms 3812 KB Output is correct
89 Correct 536 ms 3776 KB Output is correct
90 Correct 506 ms 3648 KB Output is correct
91 Correct 537 ms 3788 KB Output is correct
92 Correct 511 ms 3860 KB Output is correct