QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#376846#3161. Another Coin Weighing Puzzleckiseki#WA 1ms3804kbC++204.5kb2024-04-04 17:35:212024-04-04 17:35:22

Judging History

你现在查看的是最新测评结果

  • [2024-04-04 17:35:22]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3804kb
  • [2024-04-04 17:35:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define all(x) begin(x), end(x)
#ifdef CKISEKI
#define safe cerr << __PRETTY_FUNCTION__ << " line " << __LINE__ << "\n";
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
#include <experimental/iterator>
void orange_(auto s, auto L, auto R) {
  cerr << "\e[1;33m[ " << s << " ] = [ ";
  using namespace experimental;
  copy(L, R, make_ostream_joiner(cerr, ", "));
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

class DSU {
  vector<int> a;
public:
  DSU(int n) : a(n) {
    iota(all(a), 0);
  }
  int query(int x) {
    if (a[x] != x)
      a[x] = query(a[x]);
    return a[x];
  }
  void join(int x, int y) {
    debug("join", x, y);
    a[query(x)] = query(y);
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, m;
  cin >> n >> m; 
  vector<string> a(n);
  for (auto &ai : a)
    cin >> ai;

  constexpr int INF = 1 << 30;

  DSU dsu(n * m * 2 + 1);

  auto go = [&](int x, int y, int dx, int dy, int o) {
    debug("go", x, y, dx, dy);
    while (true) {
      x += dx, y += dy;
      if (x < 0 or x >= n) {
        x -= dx;
        dx = -dx;
      }
      if (y < 0 or y >= m) {
        y -= dy;
        dy = -dy;
      }
      debug("now", x, y);

      const int t = (x * m + y) * 2;
      if (a[x][y] == '/') {
        swap(dx, dy);
        dx = -dx, dy = -dy;
      } else if (a[x][y] == '\\') {
        swap(dx, dy);
      } else if (a[x][y] == '#') {
        dsu.join(o, n * m * 2);
        break;
      } else if (a[x][y] == 'V') {
        dsu.join(o, t + (dx == 0));
        dsu.join(o ^ 1, t + (dx != 0));
        break;
      } else if (a[x][y] == 'H') {
        dsu.join(o, t + (dy == 0));
        dsu.join(o ^ 1, t + (dy != 0));
        break;
      }
    }
  };

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      const int o = (i * m + j) * 2;
      if (a[i][j] == 'V') {
        go(i, j, -1, 0, o);
        go(i, j, 1, 0, o);
        go(i, j, 0, -1, o + 1);
        go(i, j, 0, 1, o + 1);
      } else if (a[i][j] == 'H') {
        go(i, j, -1, 0, o + 1);
        go(i, j, 1, 0, o + 1);
        go(i, j, 0, -1, o);
        go(i, j, 0, 1, o);
      }
    }
  }

  vector<int> cost(n * m * 2 + 1);

  debug(dsu.query(n * m * 2));
  cost[dsu.query(n * m * 2)] = INF;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == 'V' or a[i][j] == 'H') {
        const int o = (i * m + j) * 2;
        debug(i, j, o, dsu.query(o), o + 1, dsu.query(o + 1));
        if (dsu.query(o) == dsu.query(o + 1)) {
          cout << "-1\n";
          return 0;
        }
        cost[dsu.query(o + 1)]++;
      }
    }
  }

  int64_t ans = 0;

  vector<bool> done(n * m * 2 + 1);

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == 'V' or a[i][j] == 'H') {
        const int o = (i * m + j) * 2;
        if (not done[dsu.query(o)]) {
          debug(i, j, cost[dsu.query(o)], cost[dsu.query(o + 1)]);
          ans += min(cost[dsu.query(o)], cost[dsu.query(o + 1)]);
          done[dsu.query(o)] = true;
          done[dsu.query(o + 1)] = true;
        }
      }
    }
  }

  cout << ans << '\n';

  return 0;
}

/*
1 3
H#H
("go", x, y, dx, dy) = (go, 0, 0, -1, 0)
("now", x, y) = (now, 0, 0)
("join", x, y) = (join, 1, 1)
("join", x, y) = (join, 0, 0)
("go", x, y, dx, dy) = (go, 0, 0, 1, 0)
("now", x, y) = (now, 0, 0)
("join", x, y) = (join, 1, 1)
("join", x, y) = (join, 0, 0)
("go", x, y, dx, dy) = (go, 0, 0, 0, -1)
("now", x, y) = (now, 0, 0)
("join", x, y) = (join, 0, 0)
("join", x, y) = (join, 1, 1)
("go", x, y, dx, dy) = (go, 0, 0, 0, 1)
("now", x, y) = (now, 0, 1)
("join", x, y) = (join, 0, 6)
("go", x, y, dx, dy) = (go, 0, 2, -1, 0)
("now", x, y) = (now, 0, 2)
("join", x, y) = (join, 5, 5)
("join", x, y) = (join, 4, 4)
("go", x, y, dx, dy) = (go, 0, 2, 1, 0)
("now", x, y) = (now, 0, 2)
("join", x, y) = (join, 5, 5)
("join", x, y) = (join, 4, 4)
("go", x, y, dx, dy) = (go, 0, 2, 0, -1)
("now", x, y) = (now, 0, 1)
("join", x, y) = (join, 4, 6)
("go", x, y, dx, dy) = (go, 0, 2, 0, 1)
("now", x, y) = (now, 0, 2)
("join", x, y) = (join, 4, 4)
("join", x, y) = (join, 5, 5)
(dsu.query(n * m * 2)) = (6)
(i, j, o, dsu.query(o), o + 1, dsu.query(o + 1)) = (0, 0, 0, 6, 1, 1)
(i, j, o, dsu.query(o), o + 1, dsu.query(o + 1)) = (0, 2, 4, 6, 5, 5)
(i, j, cost[dsu.query(o)], cost[dsu.query(o + 1)]) = (0, 0, 1073741824, 1)
1
*/

詳細信息

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3804kb

input:

2 1

output:

0

result:

wrong answer 1st lines differ - expected: '9', found: '0'