Submission #436854

# Submission time Handle Problem Language Result Execution time Memory
436854 2021-06-25T05:34:16 Z adamjinwei Mecho (IOI09_mecho) C++14
76 / 100
493 ms 12476 KB
#include <bits/stdc++.h>
#define inf 100000000000000007
#define mod 1000000007
#define rnd() dist(rand_num)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#define int long long
using namespace std;
unsigned seed=std::chrono::system_clock::now().time_since_epoch().count();
mt19937 rand_num(seed);
uniform_int_distribution<int> dist(0,inf);
template <typename T> void read(T &x){
	x=0;char ch=getchar();int fh=1;
	while (ch<'0'||ch>'9'){if (ch=='-')fh=-1;ch=getchar();}
	while (ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	x*=fh;
}
template <typename T> void write(T x) {
	if (x<0) x=-x,putchar('-');
	if (x>9) write(x/10);
	putchar(x%10+'0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
const int dx[]={-1,0,1,0},dy[]={0,-1,0,1};
int n,s;
char mp[805][1005];
int bee[805][805];
int bear[805][805];
bool check(int mid)
{
	memset(bear,0x3f,sizeof(bear));
	queue<pair<int,int>> q;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(mp[i][j]=='M')
			{
				if(mid>=bee[i][j]) return false;
				bear[i][j]=mid;
				q.push(make_pair(i,j));
			}
	while(!q.empty())
	{
		pair<int,int> p=q.front();
		q.pop();
		for(int d=0;d<4;++d)
		{
			int xx=p.first+dx[d],yy=p.second+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&mp[xx][yy]!='T'&&mp[xx][yy]!='H'&&bear[p.first][p.second]+1<bee[xx][yy]&&bear[xx][yy]>bear[p.first][p.second]+1)
			{
				bear[xx][yy]=bear[p.first][p.second]+1;
				q.push(make_pair(xx,yy));
			}
		}
	}
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(mp[i][j]=='D')
			{
				if(bear[i][j]<inf) return true;
				return false;
			}
}
signed main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	read(n);read(s);
	for(int i=1;i<=n;++i)
		scanf("%s",mp[i]+1);
	memset(bee,0x3f,sizeof(bee));
	queue<pair<int,int>> q;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(mp[i][j]=='H')
			{
				bee[i][j]=0;
				q.push(make_pair(i,j));
			}
	while(!q.empty())
	{
		pair<int,int> p=q.front();
		q.pop();
		for(int d=0;d<4;++d)
		{
			int xx=p.first+dx[d],yy=p.second+dy[d];
			if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&(mp[xx][yy]=='G'||mp[xx][yy]=='M')&&bee[xx][yy]>bee[p.first][p.second]+s)
			{
				bee[xx][yy]=bee[p.first][p.second]+s;
				q.push(make_pair(xx,yy));
			}
		}
	}
	int l=0,r=5000000;
	while(l<r)
	{
		int mid=l+r+1>>1;
		if(check(mid)) l=mid;
		else r=mid-1;
	}
	if(check(l)) writeln(l/s);
	else puts("-1");
	return 0;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:99:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   99 |   int mid=l+r+1>>1;
      |           ~~~^~
mecho.cpp: In function 'bool check(long long int)':
mecho.cpp:35:23: warning: control reaches end of non-void function [-Wreturn-type]
   35 |  queue<pair<int,int>> q;
      |                       ^
mecho.cpp: In function 'int main()':
mecho.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |   scanf("%s",mp[i]+1);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 13 ms 10444 KB Output is correct
2 Correct 14 ms 10444 KB Output is correct
3 Correct 12 ms 10404 KB Output is correct
4 Correct 12 ms 10444 KB Output is correct
5 Correct 12 ms 10456 KB Output is correct
6 Correct 12 ms 10444 KB Output is correct
7 Correct 309 ms 12068 KB Output is correct
8 Correct 12 ms 10444 KB Output is correct
9 Correct 12 ms 10464 KB Output is correct
10 Correct 14 ms 10444 KB Output is correct
11 Correct 13 ms 10444 KB Output is correct
12 Correct 13 ms 10508 KB Output is correct
13 Correct 13 ms 10444 KB Output is correct
14 Correct 13 ms 10516 KB Output is correct
15 Correct 13 ms 10464 KB Output is correct
16 Correct 13 ms 10476 KB Output is correct
17 Correct 12 ms 10472 KB Output is correct
18 Correct 16 ms 10444 KB Output is correct
19 Correct 13 ms 10480 KB Output is correct
20 Correct 13 ms 10480 KB Output is correct
21 Correct 15 ms 10444 KB Output is correct
22 Correct 13 ms 10444 KB Output is correct
23 Correct 13 ms 10444 KB Output is correct
24 Correct 13 ms 10412 KB Output is correct
25 Correct 13 ms 10508 KB Output is correct
26 Correct 14 ms 10444 KB Output is correct
27 Correct 12 ms 10444 KB Output is correct
28 Correct 12 ms 10520 KB Output is correct
29 Correct 12 ms 10444 KB Output is correct
30 Correct 18 ms 10448 KB Output is correct
31 Correct 17 ms 10436 KB Output is correct
32 Correct 18 ms 10524 KB Output is correct
33 Correct 16 ms 10908 KB Output is correct
34 Incorrect 30 ms 10828 KB Output isn't correct
35 Correct 79 ms 10828 KB Output is correct
36 Correct 16 ms 10956 KB Output is correct
37 Incorrect 47 ms 10956 KB Output isn't correct
38 Correct 132 ms 10988 KB Output is correct
39 Correct 23 ms 11084 KB Output is correct
40 Incorrect 61 ms 10984 KB Output isn't correct
41 Correct 130 ms 11080 KB Output is correct
42 Correct 21 ms 11084 KB Output is correct
43 Incorrect 89 ms 11112 KB Output isn't correct
44 Correct 172 ms 11116 KB Output is correct
45 Correct 23 ms 11212 KB Output is correct
46 Incorrect 100 ms 11260 KB Output isn't correct
47 Correct 213 ms 11272 KB Output is correct
48 Correct 26 ms 11340 KB Output is correct
49 Incorrect 118 ms 11340 KB Output isn't correct
50 Correct 270 ms 11404 KB Output is correct
51 Correct 25 ms 11468 KB Output is correct
52 Incorrect 175 ms 11480 KB Output isn't correct
53 Correct 325 ms 11496 KB Output is correct
54 Correct 29 ms 11536 KB Output is correct
55 Incorrect 168 ms 11596 KB Output isn't correct
56 Correct 377 ms 11720 KB Output is correct
57 Correct 32 ms 11724 KB Output is correct
58 Incorrect 192 ms 11724 KB Output isn't correct
59 Correct 423 ms 11728 KB Output is correct
60 Correct 32 ms 11852 KB Output is correct
61 Incorrect 220 ms 11840 KB Output isn't correct
62 Correct 493 ms 11856 KB Output is correct
63 Correct 167 ms 11780 KB Output is correct
64 Correct 370 ms 11904 KB Output is correct
65 Correct 256 ms 11844 KB Output is correct
66 Correct 209 ms 11788 KB Output is correct
67 Correct 185 ms 11852 KB Output is correct
68 Correct 116 ms 11832 KB Output is correct
69 Correct 129 ms 11844 KB Output is correct
70 Correct 101 ms 11844 KB Output is correct
71 Correct 83 ms 11900 KB Output is correct
72 Correct 81 ms 11844 KB Output is correct
73 Correct 92 ms 12332 KB Output is correct
74 Correct 156 ms 12392 KB Output is correct
75 Correct 174 ms 12356 KB Output is correct
76 Correct 124 ms 12400 KB Output is correct
77 Correct 161 ms 12476 KB Output is correct
78 Correct 175 ms 12232 KB Output is correct
79 Correct 194 ms 12276 KB Output is correct
80 Correct 136 ms 12280 KB Output is correct
81 Correct 150 ms 12284 KB Output is correct
82 Correct 139 ms 12336 KB Output is correct
83 Correct 252 ms 12228 KB Output is correct
84 Correct 246 ms 12328 KB Output is correct
85 Correct 230 ms 12228 KB Output is correct
86 Correct 227 ms 12264 KB Output is correct
87 Correct 238 ms 12356 KB Output is correct
88 Correct 282 ms 12168 KB Output is correct
89 Correct 339 ms 12168 KB Output is correct
90 Correct 317 ms 12100 KB Output is correct
91 Correct 294 ms 12204 KB Output is correct
92 Correct 257 ms 12164 KB Output is correct