Submission #883211

#TimeUsernameProblemLanguageResultExecution timeMemory
883211MilosMilutinovicAbduction 2 (JOI17_abduction2)C++14
44 / 100
5098 ms17196 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w, q; cin >> h >> w >> q; vector<int> a(h); for (int i = 0; i < h; i++) { cin >> a[i]; } vector<int> b(w); for (int i = 0; i < w; i++) { cin >> b[i]; } auto Go = [&](int x, int y, int dir) { if (dir == 0) { // up if (x == 0) { return make_pair(x, y); } x--; while (x > 0 && a[x] < b[y]) { x -= 1; } return make_pair(x, y); } if (dir == 1) { // down if (x == h - 1) { return make_pair(x, y); } x += 1; while (x + 1 < h && a[x] < b[y]) { x += 1; } return make_pair(x, y); } if (dir == 2) { // left if (y == 0) { return make_pair(x, y); } y -= 1; while (y > 0 && a[x] > b[y]) { y -= 1; } return make_pair(x, y); } if (dir == 3) { // right if (y == w - 1) { return make_pair(x, y); } y += 1; while (y + 1 < w && a[x] > b[y]) { y += 1; } return make_pair(x, y); } assert(false); return make_pair(-1, -1); }; vector<map<pair<int, int>, bool>> was(4); vector<map<pair<int, int>, int>> dp(4); function<int(int, int, int)> Solve = [&](int x, int y, int dir) { if (dir == 0) { if (a[x] < b[y]) { return 0; } } if (dir == 1) { if (a[x] < b[y]) { return 0; } } if (dir == 2) { if (a[x] > b[y]) { return 0; } } if (dir == 3) { if (a[x] > b[y]) { return 0; } } pair<int, int> curr = {x, y}; if (was[dir][curr]) { return dp[dir][curr]; } was[dir][curr] = true; if (dir == 0 || dir == 1) { { pair<int, int> nxt = Go(x, y, 2); if (nxt.first != x || nxt.second != y) { dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 2) + abs(nxt.first - x) + abs(nxt.second - y)); } } { pair<int, int> nxt = Go(x, y, 3); if (nxt.first != x || nxt.second != y) { dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 3) + abs(nxt.first - x) + abs(nxt.second - y)); } } } if (dir == 2 || dir == 3) { { pair<int, int> nxt = Go(x, y, 0); if (nxt.first != x || nxt.second != y) { dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 0) + abs(nxt.first - x) + abs(nxt.second - y)); } } { pair<int, int> nxt = Go(x, y, 1); if (nxt.first != x || nxt.second != y) { dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 1) + abs(nxt.first - x) + abs(nxt.second - y)); } } } return dp[dir][curr]; }; while (q--) { int x, y; cin >> x >> y; --x; --y; int res = 1; for (int dir = 0; dir < 4; dir++) { pair<int, int> cell = Go(x, y, dir); int nx = cell.first; int ny = cell.second; if (nx != x || ny != y) { res = max(res, Solve(nx, ny, dir) + abs(x - nx) + abs(y - ny)); } } cout << res << '\n'; for (int dir = 0; dir < 4; dir++) { was[dir].clear(); dp[dir].clear(); } } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...