Submission #570593

# Submission time Handle Problem Language Result Execution time Memory
570593 2022-05-30T15:11:13 Z iancu Abduction 2 (JOI17_abduction2) C++14
44 / 100
5000 ms 28632 KB
#include <bits/stdc++.h>

#define NORTH 0
#define SOUTH 1
#define EAST  2
#define WEST  3

using namespace std;

const int N = 5e4 + 5;

struct Node {
  int x, y;
  int val;
  int dir;
  long long dist;

  bool operator<(const Node& n) const {
    if (val != n.val)
      return val > n.val;
    //return make_tuple(x, y, dir) < make_tuple(n.x, n.y, n.dir);
  }
};

struct Hasher
{
  long long operator() (const Node& n) const
  {
      return n.dir ^ n.val ^ n.x ^ n.y;
  }
};
struct EqualFn
{
  bool operator() (const Node& n1, const Node& n2) const
  {
    return (n1.x == n2.x) && (n1.y == n2.y) && (n1.val == n2.val) && (n1.dir == n2.dir);
  }
};

int h, w;
int a[N], b[N];

long long solve(int x, int y, int dir) {
  unordered_map<Node, pair<int, int>, Hasher, EqualFn> nxt;
  map<pair<int, int>, long long> dp;
  int hl, hr, wl, wr;
  long long ans = 0;
  hl = hr = x;
  wl = wr = y;
  if (dir == 0) hl = max(hl - 1, 1);
  else if (dir == 1) hr = min(hr + 1, h);
  else if (dir == 2) wr = min(wr + 1, w);
  else wl = max(wl - 1, 1);
  priority_queue<Node> pq;
  pq.push({x, y, 0, dir, 0});
  while (!pq.empty()) {
    auto n = pq.top();
    //cout << n.x << " " << n.y << " " << n.dir << " " << n.dist << endl;
    pq.pop();
    ans = max(ans, n.dist);

    if (n.dir == NORTH && n.x == 1)
      continue;
    if (n.dir == SOUTH && n.x == h)
      continue;
    if (n.dir == EAST  && n.y == w)
      continue;
    if (n.dir == WEST  && n.y == 1)
      continue;

    if (n.dir == NORTH) {
      if (nxt.find(n) != nxt.end()) {
      } else {
          while (hl > 1 && a[hl] <= b[n.y])
            --hl;
          if (a[hl] <= b[n.y]) {
            ans = max(ans, n.dist + n.x - hl);
            continue;
          }
          nxt[n] = {hl, n.y};
      }
      if (dp[nxt[n]] >= n.dist + n.x - nxt[n].first)
        continue;
      dp[nxt[n]] = n.dist + n.x - nxt[n].first;
      pq.push({nxt[n].first, nxt[n].second, a[nxt[n].first], WEST, n.dist + n.x - nxt[n].first});
      pq.push({nxt[n].first, nxt[n].second, a[nxt[n].first], EAST, n.dist + n.x - nxt[n].first});
    } else if (n.dir == SOUTH) {
      if (nxt.find(n) == nxt.end()) {
        while (hr < h && a[hr] <= b[n.y])
          ++hr;
        if (a[hr] <= b[n.y]) {
          ans = max(ans, n.dist + hr - n.x);
          continue;
        }
        nxt[n] = {hr, n.y};
      }
      if (dp[nxt[n]] >= n.dist + nxt[n].first - n.x)
        continue;
      dp[nxt[n]] = n.dist + nxt[n].first - n.x;
      pq.push({nxt[n].first, nxt[n].second, a[nxt[n].first], WEST, n.dist + nxt[n].first - n.x});
      pq.push({nxt[n].first, nxt[n].second, a[nxt[n].first], EAST, n.dist + nxt[n].first - n.x});
    } else if (n.dir == EAST) {
      if (nxt.find(n) == nxt.end()) {
        while (wr < w && b[wr] <= a[n.x])
          ++wr;
        if (b[wr] <= a[n.x]) {
          ans = max(ans, n.dist + wr - n.y);
          continue;
        }
        nxt[n] = {n.x, wr};
      }
      if (dp[nxt[n]] >= n.dist + nxt[n].second - n.y)
        continue;
      dp[nxt[n]] = n.dist + nxt[n].second - n.y;
      pq.push({nxt[n].first, nxt[n].second, b[nxt[n].second], NORTH, n.dist + nxt[n].second - n.y});
      pq.push({nxt[n].first, nxt[n].second, b[nxt[n].second], SOUTH, n.dist + nxt[n].second - n.y});
    } else {
      if (nxt.find(n) == nxt.end()) {
        while (wl > 1 && b[wl] <= a[n.x])
          --wl;
        if (b[wl] <= a[n.x]) {
          ans = max(ans, n.dist + n.y - wl);
          continue;
        }
        nxt[n] = {n.x, wl};
      }
      if (dp[nxt[n]] >= n.dist + n.y - nxt[n].second)
        continue;
      dp[nxt[n]] = n.dist + n.y - nxt[n].second;
      pq.push({nxt[n].first, nxt[n].second, b[nxt[n].second], NORTH, n.dist + n.y - nxt[n].second});
      pq.push({nxt[n].first, nxt[n].second, b[nxt[n].second], SOUTH, n.dist + n.y - nxt[n].second});
    }
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  int q;
  cin >> h >> w >> q;
  for (int i = 1; i <= h; ++i)
    cin >> a[i];
  for (int i = 1; i <= w; ++i)
    cin >> b[i];
  while (q--) {
    int x, y;
    cin >> x >> y;
    long long ans = 0;
    for (int i = 0; i < 4; ++i)
      ans = max(ans, solve(x, y, i));
    cout << ans << "\n";
  }
  return 0;
}

Compilation message

abduction2.cpp: In member function 'bool Node::operator<(const Node&) const':
abduction2.cpp:22:3: warning: control reaches end of non-void function [-Wreturn-type]
   22 |   }
      |   ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 13 ms 852 KB Output is correct
20 Correct 22 ms 1108 KB Output is correct
21 Correct 16 ms 988 KB Output is correct
22 Correct 31 ms 1640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 13 ms 852 KB Output is correct
20 Correct 22 ms 1108 KB Output is correct
21 Correct 16 ms 988 KB Output is correct
22 Correct 31 ms 1640 KB Output is correct
23 Correct 14 ms 724 KB Output is correct
24 Correct 11 ms 724 KB Output is correct
25 Correct 15 ms 724 KB Output is correct
26 Correct 14 ms 668 KB Output is correct
27 Correct 10 ms 668 KB Output is correct
28 Correct 81 ms 5196 KB Output is correct
29 Correct 16 ms 1328 KB Output is correct
30 Correct 571 ms 16352 KB Output is correct
31 Correct 812 ms 21928 KB Output is correct
32 Correct 28 ms 1492 KB Output is correct
33 Correct 163 ms 5744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 340 KB Output is correct
2 Correct 10 ms 360 KB Output is correct
3 Correct 13 ms 340 KB Output is correct
4 Correct 11 ms 340 KB Output is correct
5 Correct 9 ms 360 KB Output is correct
6 Correct 92 ms 596 KB Output is correct
7 Correct 89 ms 572 KB Output is correct
8 Correct 840 ms 1632 KB Output is correct
9 Correct 957 ms 1564 KB Output is correct
10 Correct 1036 ms 1536 KB Output is correct
11 Correct 1590 ms 1872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 13 ms 852 KB Output is correct
20 Correct 22 ms 1108 KB Output is correct
21 Correct 16 ms 988 KB Output is correct
22 Correct 31 ms 1640 KB Output is correct
23 Correct 14 ms 724 KB Output is correct
24 Correct 11 ms 724 KB Output is correct
25 Correct 15 ms 724 KB Output is correct
26 Correct 14 ms 668 KB Output is correct
27 Correct 10 ms 668 KB Output is correct
28 Correct 81 ms 5196 KB Output is correct
29 Correct 16 ms 1328 KB Output is correct
30 Correct 571 ms 16352 KB Output is correct
31 Correct 812 ms 21928 KB Output is correct
32 Correct 28 ms 1492 KB Output is correct
33 Correct 163 ms 5744 KB Output is correct
34 Correct 10 ms 340 KB Output is correct
35 Correct 10 ms 360 KB Output is correct
36 Correct 13 ms 340 KB Output is correct
37 Correct 11 ms 340 KB Output is correct
38 Correct 9 ms 360 KB Output is correct
39 Correct 92 ms 596 KB Output is correct
40 Correct 89 ms 572 KB Output is correct
41 Correct 840 ms 1632 KB Output is correct
42 Correct 957 ms 1564 KB Output is correct
43 Correct 1036 ms 1536 KB Output is correct
44 Correct 1590 ms 1872 KB Output is correct
45 Correct 33 ms 844 KB Output is correct
46 Correct 38 ms 728 KB Output is correct
47 Correct 42 ms 656 KB Output is correct
48 Correct 43 ms 724 KB Output is correct
49 Correct 57 ms 724 KB Output is correct
50 Correct 3080 ms 7304 KB Output is correct
51 Correct 2394 ms 7360 KB Output is correct
52 Execution timed out 5096 ms 28632 KB Time limit exceeded
53 Halted 0 ms 0 KB -