Submission #799625

# Submission time Handle Problem Language Result Execution time Memory
799625 2023-07-31T16:48:36 Z I_Love_EliskaM_ Mecho (IOI09_mecho) C++14
100 / 100
276 ms 17280 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define forn(i,n) for(int i=0;i<n;++i)
#define pi pair<int,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vii(a,n) vector<int>a(n);forn(i,n)cin>>a[i];

const int inf=1e18;

const int N=800;
string a[N];
int d[N][N];
int p[N][N];
int vis[N][N];

void clear(int n) {
	forn(i,n) forn(j,n) d[i][j]=0;
}
void reset(int n) {
	forn(i,n) forn(j,n) vis[i][j]=0;
}

struct z {
	int f,s,d,d1,d2;
};

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void solve() {

	int n,s; cin>>n>>s;
	forn(i,n) cin>>a[i];

	forn(i,n) forn(j,n) p[i][j]=inf;
	queue<z> q;
	forn(i,n) forn(j,n) if (a[i][j]=='H') {
		q.push({i,j,0});
	}

	while (q.size()) {
		auto it=q.front(); q.pop();
		int x=it.f, y=it.s, d=it.d;
		if (x<0 || y<0) continue;
		if (x>=n || y>=n) continue;
		if (vis[x][y]) continue;
		if (a[x][y]=='T' || a[x][y]=='D') continue;
		vis[x][y]=1;
		p[x][y]=d;
		q.push({x-1,y,d+1});
		q.push({x+1,y,d+1});
		q.push({x,y-1,d+1});
		q.push({x,y+1,d+1});
	}

	int l=-1, r=1e9;
	pi D;
	forn(i,n) forn(j,n) if (a[i][j]=='D') D={i,j};
	while (l<r) {
		int mid=(l+r+1)>>1;
		reset(n);
		forn(i,n) forn(j,n) if (a[i][j]=='M') {
			q.push({i,j,0,0,0});
		}

		while (q.size()) {
			auto it=q.front(); q.pop();
			int x=it.f, y=it.s, d=it.d, d1=it.d1, d2=it.d2;
			//cout<<mid<<": "<<x<<' '<<y<<' '<<d<<' '<<vis[x][y]<<' '<<p[x][y]<<' '<<a[x][y]<<'\n';
			if (x<0 || y<0) continue;
			if (x>=n || y>=n) continue;
			if (vis[x][y]) continue;
			if (a[x][y]=='T') continue;
			if (p[x][y]<mid+(d+s-1)/s) continue;
			if (p[x][y]==mid+(d+s-1)/s) {
				if (d%s) {

				} else {
					continue;
				}
			}
			vis[x][y]=1;
			q.push({x-1,y,d+1});
			q.push({x+1,y,d+1});
			q.push({x,y-1,d+1});
			q.push({x,y+1,d+1});
		}
		if (vis[D.f][D.s]) l=mid;
		else r=mid-1;
	}
	cout<<r<<'\n';

}

int32_t main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	solve();
}

Compilation message

mecho.cpp: In function 'void solve()':
mecho.cpp:72:32: warning: unused variable 'd1' [-Wunused-variable]
   72 |    int x=it.f, y=it.s, d=it.d, d1=it.d1, d2=it.d2;
      |                                ^~
mecho.cpp:72:42: warning: unused variable 'd2' [-Wunused-variable]
   72 |    int x=it.f, y=it.s, d=it.d, d1=it.d1, d2=it.d2;
      |                                          ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 360 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 187 ms 13808 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 408 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
13 Correct 1 ms 852 KB Output is correct
14 Correct 2 ms 980 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 612 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 596 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 1 ms 872 KB Output is correct
28 Correct 1 ms 872 KB Output is correct
29 Correct 1 ms 1108 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 980 KB Output is correct
32 Correct 1 ms 980 KB Output is correct
33 Correct 10 ms 4988 KB Output is correct
34 Correct 12 ms 4996 KB Output is correct
35 Correct 46 ms 4960 KB Output is correct
36 Correct 11 ms 5588 KB Output is correct
37 Correct 12 ms 5624 KB Output is correct
38 Correct 59 ms 5744 KB Output is correct
39 Correct 15 ms 6356 KB Output is correct
40 Correct 18 ms 6356 KB Output is correct
41 Correct 75 ms 6484 KB Output is correct
42 Correct 15 ms 7132 KB Output is correct
43 Correct 16 ms 7112 KB Output is correct
44 Correct 100 ms 7224 KB Output is correct
45 Correct 20 ms 7764 KB Output is correct
46 Correct 21 ms 7848 KB Output is correct
47 Correct 122 ms 7880 KB Output is correct
48 Correct 25 ms 8508 KB Output is correct
49 Correct 26 ms 8608 KB Output is correct
50 Correct 153 ms 8724 KB Output is correct
51 Correct 28 ms 9240 KB Output is correct
52 Correct 31 ms 9360 KB Output is correct
53 Correct 182 ms 9480 KB Output is correct
54 Correct 34 ms 10104 KB Output is correct
55 Correct 39 ms 10076 KB Output is correct
56 Correct 205 ms 10388 KB Output is correct
57 Correct 32 ms 10828 KB Output is correct
58 Correct 33 ms 10760 KB Output is correct
59 Correct 229 ms 10968 KB Output is correct
60 Correct 38 ms 11664 KB Output is correct
61 Correct 43 ms 11656 KB Output is correct
62 Correct 267 ms 11832 KB Output is correct
63 Correct 163 ms 11724 KB Output is correct
64 Correct 276 ms 11732 KB Output is correct
65 Correct 264 ms 11736 KB Output is correct
66 Correct 188 ms 11724 KB Output is correct
67 Correct 165 ms 11720 KB Output is correct
68 Correct 96 ms 12264 KB Output is correct
69 Correct 80 ms 12244 KB Output is correct
70 Correct 57 ms 12268 KB Output is correct
71 Correct 62 ms 12200 KB Output is correct
72 Correct 55 ms 12196 KB Output is correct
73 Correct 96 ms 17280 KB Output is correct
74 Correct 139 ms 17216 KB Output is correct
75 Correct 149 ms 17236 KB Output is correct
76 Correct 138 ms 17232 KB Output is correct
77 Correct 158 ms 17244 KB Output is correct
78 Correct 147 ms 16588 KB Output is correct
79 Correct 119 ms 16588 KB Output is correct
80 Correct 129 ms 16596 KB Output is correct
81 Correct 148 ms 16552 KB Output is correct
82 Correct 131 ms 16616 KB Output is correct
83 Correct 166 ms 15760 KB Output is correct
84 Correct 157 ms 15824 KB Output is correct
85 Correct 150 ms 15840 KB Output is correct
86 Correct 161 ms 15836 KB Output is correct
87 Correct 157 ms 15820 KB Output is correct
88 Correct 170 ms 14848 KB Output is correct
89 Correct 179 ms 14852 KB Output is correct
90 Correct 181 ms 14912 KB Output is correct
91 Correct 171 ms 14932 KB Output is correct
92 Correct 174 ms 14948 KB Output is correct