Submission #536087

# Submission time Handle Problem Language Result Execution time Memory
536087 2022-03-12T10:24:21 Z ssense Mecho (IOI09_mecho) C++14
100 / 100
141 ms 7500 KB
#include <bits/stdc++.h>
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long  ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define nax 200005
#define pb push_back
#define sc second
#define fr first
#define int long long
#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//ssense
int n, s;
vector<vector<char>> grid;
vector<pair<int, int>> hives;
int startx, starty, endx, endy;
vector<vector<int>> bees;

bool get(int mins)
{
	vector<vector<bool>> vis;
	vis.assign(n, vector<bool>(n, false));
	queue<pair<pair<int, int>, int>> q;
	if(bees[startx][starty] <= s*mins)
	{
		return false;
	}
	q.push({{startx, starty}, mins*s});
	while(!q.empty())
	{
		int x = q.front().fr.fr;
		int y = q.front().fr.sc;
		int dist = q.front().sc;
		q.pop();
		if(x > 0 && !vis[x-1][y] && (grid[x-1][y] == 'G' || grid[x-1][y] == 'D') && dist+1 < bees[x-1][y])
		{
			vis[x-1][y] = true;
			q.push({{x-1, y}, dist+1});
		}
		if(x < n-1 && !vis[x+1][y] && (grid[x+1][y] == 'G' || grid[x+1][y] == 'D') && dist+1 < bees[x+1][y])
		{
			vis[x+1][y] = true;
			q.push({{x+1, y}, dist+1});
		}
		if(y > 0 && !vis[x][y-1] && (grid[x][y-1] == 'G' || grid[x][y-1] == 'D') && dist+1 < bees[x][y-1])
		{
			vis[x][y-1] = true;
			q.push({{x, y-1}, dist+1});
		}
		if(y < n-1 && !vis[x][y+1] && (grid[x][y+1] == 'G' || grid[x][y+1] == 'D') && dist+1 < bees[x][y+1])
		{
			vis[x][y+1] = true;
			q.push({{x, y+1}, dist+1});
		}
	}
	return vis[endx][endy];
}


void solve()
{
	cin >> n >> s;
	grid.assign(n, vector<char>(n));
	queue<pair<pair<int, int>, int>> q;
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			cin >> grid[i][j];
			if(grid[i][j] == 'M')
			{
				startx = i;
				starty = j;
			}
			if(grid[i][j] == 'H')
			{
				q.push({{i, j}, 0});
			}
			if(grid[i][j] == 'D')
			{
				endx = i;
				endy = j;
			}
		}
	}
	bees.assign(n, vector<int>(n, -1));
	while(!q.empty())
	{
		int x = q.front().fr.fr;
		int y = q.front().fr.sc;
		int dist = q.front().sc;
		q.pop();
		if(x > 0 && bees[x-1][y] == -1 && (grid[x-1][y] == 'G' || grid[x-1][y] == 'M'))
		{
			bees[x-1][y]=dist+s;
			q.push({{x-1, y}, dist+s});
		}
		if(x < n-1 && bees[x+1][y] == -1 && (grid[x+1][y] == 'G' || grid[x+1][y] == 'M'))
		{
			bees[x+1][y]=dist+s;
			q.push({{x+1, y}, dist+s});
		}
		if(y > 0 && bees[x][y-1] == -1 && (grid[x][y-1] == 'G' || grid[x][y-1] == 'M'))
		{
			bees[x][y-1]=dist+s;
			q.push({{x, y-1}, dist+s});
		}
		if(y < n-1 && bees[x][y+1] == -1 && (grid[x][y+1] == 'G' || grid[x][y+1] == 'M'))
		{
			bees[x][y+1]=dist+s;
			q.push({{x, y+1},dist+s});
		}
	}
	bees[endx][endy] = MX;
	int l = 0, r = 1e6;
	while(l <= r)
	{
		int mid = r+l>>1;
		if(get(mid))
		{
			l = mid+1;
		}
		else
		{
			r = mid-1;
		}
	}
	if(r >= 1e6-1)
	{
		cout << -1 << endl;
		return;
	}
	cout << r << endl;
}

int32_t main(){
	startt
	int t = 1;
	//cin >> t;
	while (t--) {
		solve();
	}
}

/*
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
THHHHHT
 
7 3
TTTTTTT
TGGGGGT
TGGGGGT
MGGGGGD
TGGGGGT
TGGGGGT
TGHHGGT

 */

Compilation message

mecho.cpp: In member function 'void prefix_sum::build(std::vector<long long int>)':
mecho.cpp:21:667: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 | int ceildiv(int one, int two) {if (one % two == 0) {return one / two;}else {return one / two + 1;}} int power(int n, int pow, int m) {if (pow == 0) return 1;if (pow % 2 == 0) {ll x = power(n, pow / 2, m);return (x * x) % m;}else return (power(n, pow - 1, m) * n) % m;} int gcd(int a, int b) { if (!b)return a; return gcd(b, a % b);} int factorial(int n, int mod) {if (n > 1)return (n * factorial(n - 1, mod)) % mod; else return 1;} int lcm(int a, int b) {return (a * b) / gcd(a, b);} vector<int> read(int n) {vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.pb(x);} return a;}struct prefix_sum{vint pref;void build(vint a){pref.pb(0);for(int i = 0; i < a.size(); i++){pref.pb(pref.back()+a[i]);}}int get(int l, int r){return pref[r]-pref[l-1];}};//ssense
      |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ~~^~~~~~~~~~
mecho.cpp: In function 'void solve()':
mecho.cpp:127:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  127 |   int mid = r+l>>1;
      |             ~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 67 ms 6916 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 320 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 316 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 320 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 320 KB Output is correct
32 Correct 1 ms 324 KB Output is correct
33 Correct 5 ms 1480 KB Output is correct
34 Correct 5 ms 1492 KB Output is correct
35 Correct 18 ms 1620 KB Output is correct
36 Correct 6 ms 1876 KB Output is correct
37 Correct 5 ms 1924 KB Output is correct
38 Correct 22 ms 1876 KB Output is correct
39 Correct 7 ms 2376 KB Output is correct
40 Correct 7 ms 2268 KB Output is correct
41 Correct 26 ms 2400 KB Output is correct
42 Correct 11 ms 2748 KB Output is correct
43 Correct 10 ms 2876 KB Output is correct
44 Correct 49 ms 2860 KB Output is correct
45 Correct 10 ms 3272 KB Output is correct
46 Correct 11 ms 3364 KB Output is correct
47 Correct 42 ms 3284 KB Output is correct
48 Correct 12 ms 3860 KB Output is correct
49 Correct 12 ms 3876 KB Output is correct
50 Correct 54 ms 3876 KB Output is correct
51 Correct 14 ms 4588 KB Output is correct
52 Correct 13 ms 4564 KB Output is correct
53 Correct 57 ms 4596 KB Output is correct
54 Correct 18 ms 5204 KB Output is correct
55 Correct 19 ms 5204 KB Output is correct
56 Correct 70 ms 5168 KB Output is correct
57 Correct 19 ms 5932 KB Output is correct
58 Correct 21 ms 5976 KB Output is correct
59 Correct 81 ms 5972 KB Output is correct
60 Correct 21 ms 6740 KB Output is correct
61 Correct 19 ms 6728 KB Output is correct
62 Correct 98 ms 6784 KB Output is correct
63 Correct 92 ms 6732 KB Output is correct
64 Correct 141 ms 6652 KB Output is correct
65 Correct 128 ms 6760 KB Output is correct
66 Correct 115 ms 6756 KB Output is correct
67 Correct 101 ms 6724 KB Output is correct
68 Correct 45 ms 6732 KB Output is correct
69 Correct 46 ms 6844 KB Output is correct
70 Correct 41 ms 6732 KB Output is correct
71 Correct 37 ms 6760 KB Output is correct
72 Correct 32 ms 6728 KB Output is correct
73 Correct 35 ms 7400 KB Output is correct
74 Correct 56 ms 7500 KB Output is correct
75 Correct 54 ms 7428 KB Output is correct
76 Correct 48 ms 7460 KB Output is correct
77 Correct 53 ms 7432 KB Output is correct
78 Correct 57 ms 7276 KB Output is correct
79 Correct 46 ms 7376 KB Output is correct
80 Correct 49 ms 7368 KB Output is correct
81 Correct 56 ms 7284 KB Output is correct
82 Correct 46 ms 7376 KB Output is correct
83 Correct 60 ms 7228 KB Output is correct
84 Correct 55 ms 7240 KB Output is correct
85 Correct 54 ms 7236 KB Output is correct
86 Correct 58 ms 7224 KB Output is correct
87 Correct 59 ms 7212 KB Output is correct
88 Correct 61 ms 7120 KB Output is correct
89 Correct 61 ms 7116 KB Output is correct
90 Correct 64 ms 7124 KB Output is correct
91 Correct 60 ms 7116 KB Output is correct
92 Correct 60 ms 7108 KB Output is correct