Submission #417569

# Submission time Handle Problem Language Result Execution time Memory
417569 2021-06-04T01:06:45 Z nkato Mecho (IOI09_mecho) C++17
84 / 100
221 ms 6788 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!

using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;

using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;

#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define fi first
#define se second

// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front

#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }

// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;

tcT> bool ckmin(T& a, const T& b) {
	return b < a ? a = b, 1 : 0; } // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
	return a < b ? a = b, 1 : 0; }

#define tcTUU tcT, class ...U

// TO_STRING
#define ts to_string
str ts(char c) { return str(1,c); }
str ts(const char* s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) {
	#ifdef LOCAL
		return b ? "true" : "false";
	#else
		return ts((int)b);
	#endif
}
tcTU> str ts(pair<T,U> p);
tcT> str ts(T v) { // containers with begin(), end()
	#ifdef LOCAL
		bool fst = 1; str res = "{";
		for (const auto& x: v) {
			if (!fst) res += ", ";
			fst = 0; res += ts(x);
		}
		res += "}"; return res;
	#else
		bool fst = 1; str res = "";
		for (const auto& x: v) {
			if (!fst) res += " ";
			fst = 0; res += ts(x);
		}
		return res;

	#endif
}
tcTU> str ts(pair<T,U> p) {
	#ifdef LOCAL
		return "("+ts(p.fi)+", "+ts(p.se)+")";
	#else
		return ts(p.fi)+" "+ts(p.se);
	#endif
}

// OUTPUT
tcT> void pr(T x) { cout << ts(x); }
tcTUU> void pr(const T& t, const U&... u) {
	pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
tcTUU> void ps(const T& t, const U&... u) {
	pr(t); if (sizeof...(u)) pr(" "); ps(u...); }

// DEBUG
void DBG() { cerr << "]" << endl; }
tcTUU> void DBG(const T& t, const U&... u) {
	cerr << ts(t); if (sizeof...(u)) cerr << ", ";
	DBG(u...); }
#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
	#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
	#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
		 << __FUNCTION__  << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
#else
	#define dbg(...) 0
	#define chk(...) 0
#endif

void setPrec() { cout << fixed << setprecision(15); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
	unsyncIO(); setPrec();
	// cin.exceptions(cin.failbit);
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
}

const int nax = 805;
str f[nax]; // [state #][row][col]
int dst[nax][nax][2]; // [row][col][0 for mecho, 1 for bee]
int n, s; 
pi m; // mecho starting pos

void reset() {
	F0R(i, nax) {
		F0R(j, nax) {
			dst[i][j][0] = -1;
		}
	}
}


bool check(int x, int y, bool mecho) {
	if (x < 0 || x >= n || y < 0 || y >= n) return 0;
	if (f[y][x] != 'G' && !(mecho && f[y][x] == 'D')) return 0;
	return 1;
}

bool tri(int t) {
	queue<pi> q;
	dst[m.se][m.fi][0] = t;
	q.push(m);
	while(!q.empty()) {
		pi u = q.ft;
		// dbg(u, dst[u.se][u.fi][0]);
		if (f[u.se][u.fi] == 'D') {
			return true;
		}
		q.pop();
		F0R(i, 4) {
			pi v = mp(u.fi+dx[i], u.se+dy[i]);
			if (check(v.fi, v.se, 1) && dst[v.se][v.fi][0] == -1) {
				dst[v.se][v.fi][0] = dst[u.se][u.fi][0]+1;
				if (dst[v.se][v.fi][1] > dst[v.se][v.fi][0]) {
					q.push(v);
				}
			}
		}
	}
	return false;
}

void bfs(vpi h) {
	queue<pi> q;
	trav(x, h) {
		q.push(x);
		dst[x.se][x.fi][1] = 0;
	}
	while(!q.empty()) {
		pi u = q.ft;
		q.pop();
		F0R(i, 4) {
			pi v = mp(u.fi+dx[i], u.se+dy[i]);
			if (check(v.fi, v.se, 0) && dst[v.se][v.fi][1] == -1) {
				dst[v.se][v.fi][1] = dst[u.se][u.fi][1]+s;
				q.push(v);
			}
		}
	}
}

int main() {
	setIO(); 

	F0R(i, nax) F0R(j, nax) F0R(k, 2) dst[i][j][k] = -1;

	cin >> n >> s;
	F0R(i, n) cin >> f[i];

	vpi h;
	F0R(i, n) {
		F0R(j, n) {
			if (f[i][j] == 'H') h.pb(mp(j, i));
			if (f[i][j] == 'M') {
				m = {j, i};
				f[i][j] = 'G';
			}
			if (f[i][j] == 'D') dst[i][j][1] = (1e9+10);
		}
	}

	// BEE BFS
	bfs(h);


	// BEAR BINARY SEARCH
	int lo = -1, hi = 2*n*n;
	while(lo < hi) {
		dbg(lo, hi);
		int mid = (lo+hi+1)/2;
		reset();
		if (tri(mid*s)) lo = mid;
		else hi = mid-1;
	}	
	ps(lo);
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:127:19: warning: statement has no effect [-Wunused-value]
  127 |  #define dbg(...) 0
      |                   ^
mecho.cpp:235:3: note: in expansion of macro 'dbg'
  235 |   dbg(lo, hi);
      |   ^~~
mecho.cpp: In function 'void setIn(str)':
mecho.cpp:134:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
      |                     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'void setOut(str)':
mecho.cpp:135:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 5512 KB Output is correct
2 Correct 6 ms 5324 KB Output is correct
3 Correct 6 ms 5324 KB Output is correct
4 Correct 6 ms 5408 KB Output is correct
5 Correct 6 ms 5412 KB Output is correct
6 Correct 7 ms 5408 KB Output is correct
7 Correct 92 ms 6568 KB Output is correct
8 Correct 7 ms 5412 KB Output is correct
9 Correct 7 ms 5324 KB Output is correct
10 Correct 7 ms 5336 KB Output is correct
11 Correct 7 ms 5412 KB Output is correct
12 Incorrect 9 ms 5408 KB Output isn't correct
13 Incorrect 9 ms 5324 KB Output isn't correct
14 Correct 8 ms 5324 KB Output is correct
15 Correct 7 ms 5324 KB Output is correct
16 Correct 7 ms 5408 KB Output is correct
17 Correct 7 ms 5416 KB Output is correct
18 Correct 8 ms 5324 KB Output is correct
19 Correct 8 ms 5324 KB Output is correct
20 Correct 7 ms 5408 KB Output is correct
21 Correct 8 ms 5324 KB Output is correct
22 Correct 8 ms 5416 KB Output is correct
23 Correct 8 ms 5412 KB Output is correct
24 Correct 8 ms 5408 KB Output is correct
25 Correct 9 ms 5416 KB Output is correct
26 Correct 9 ms 5324 KB Output is correct
27 Correct 9 ms 5412 KB Output is correct
28 Correct 9 ms 5324 KB Output is correct
29 Correct 9 ms 5324 KB Output is correct
30 Correct 9 ms 5324 KB Output is correct
31 Correct 9 ms 5424 KB Output is correct
32 Correct 9 ms 5324 KB Output is correct
33 Correct 12 ms 5552 KB Output is correct
34 Correct 12 ms 5536 KB Output is correct
35 Correct 29 ms 5580 KB Output is correct
36 Correct 13 ms 5744 KB Output is correct
37 Correct 18 ms 5708 KB Output is correct
38 Correct 36 ms 5716 KB Output is correct
39 Correct 13 ms 5788 KB Output is correct
40 Correct 14 ms 5824 KB Output is correct
41 Correct 46 ms 5808 KB Output is correct
42 Correct 14 ms 5812 KB Output is correct
43 Correct 14 ms 5896 KB Output is correct
44 Correct 55 ms 5836 KB Output is correct
45 Correct 15 ms 5964 KB Output is correct
46 Correct 15 ms 5964 KB Output is correct
47 Correct 64 ms 6016 KB Output is correct
48 Correct 17 ms 6152 KB Output is correct
49 Correct 16 ms 6152 KB Output is correct
50 Correct 73 ms 6136 KB Output is correct
51 Correct 17 ms 6252 KB Output is correct
52 Correct 20 ms 6240 KB Output is correct
53 Correct 86 ms 6188 KB Output is correct
54 Correct 18 ms 6308 KB Output is correct
55 Correct 19 ms 6348 KB Output is correct
56 Correct 102 ms 6184 KB Output is correct
57 Correct 19 ms 6348 KB Output is correct
58 Correct 20 ms 6392 KB Output is correct
59 Correct 115 ms 6340 KB Output is correct
60 Correct 20 ms 6348 KB Output is correct
61 Correct 20 ms 6324 KB Output is correct
62 Correct 132 ms 6448 KB Output is correct
63 Correct 118 ms 6476 KB Output is correct
64 Correct 221 ms 6456 KB Output is correct
65 Correct 161 ms 6456 KB Output is correct
66 Correct 133 ms 6348 KB Output is correct
67 Correct 128 ms 6476 KB Output is correct
68 Correct 54 ms 6432 KB Output is correct
69 Correct 55 ms 6444 KB Output is correct
70 Correct 43 ms 6500 KB Output is correct
71 Correct 47 ms 6468 KB Output is correct
72 Incorrect 36 ms 6476 KB Output isn't correct
73 Incorrect 47 ms 6708 KB Output isn't correct
74 Correct 62 ms 6776 KB Output is correct
75 Correct 75 ms 6732 KB Output is correct
76 Correct 73 ms 6788 KB Output is correct
77 Correct 68 ms 6776 KB Output is correct
78 Correct 82 ms 6724 KB Output is correct
79 Correct 56 ms 6672 KB Output is correct
80 Correct 68 ms 6736 KB Output is correct
81 Correct 73 ms 6744 KB Output is correct
82 Correct 65 ms 6732 KB Output is correct
83 Correct 88 ms 6744 KB Output is correct
84 Correct 76 ms 6684 KB Output is correct
85 Correct 83 ms 6708 KB Output is correct
86 Correct 81 ms 6688 KB Output is correct
87 Correct 80 ms 6692 KB Output is correct
88 Correct 86 ms 6604 KB Output is correct
89 Correct 89 ms 6604 KB Output is correct
90 Correct 92 ms 6644 KB Output is correct
91 Correct 87 ms 6624 KB Output is correct
92 Correct 96 ms 6604 KB Output is correct