結果

問題 No.1767 BLUE to RED
ユーザー simansiman
提出日時 2021-11-29 17:24:47
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 2,739 bytes
コンパイル時間 7,058 ms
コンパイル使用メモリ 106,856 KB
実行使用メモリ 18,212 KB
最終ジャッジ日時 2023-09-15 08:48:50
合計ジャッジ時間 18,073 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 187 ms
11,652 KB
testcase_10 AC 251 ms
13,552 KB
testcase_11 AC 197 ms
11,564 KB
testcase_12 AC 186 ms
11,652 KB
testcase_13 AC 287 ms
17,328 KB
testcase_14 AC 358 ms
17,352 KB
testcase_15 AC 339 ms
17,656 KB
testcase_16 AC 344 ms
18,212 KB
testcase_17 AC 342 ms
17,348 KB
testcase_18 AC 342 ms
17,384 KB
testcase_19 AC 343 ms
17,152 KB
testcase_20 AC 345 ms
17,980 KB
testcase_21 AC 344 ms
17,864 KB
testcase_22 AC 347 ms
17,132 KB
testcase_23 AC 344 ms
17,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 200000;
vector<int> parent;
vector<int> _rank;
vector<int> _size;

class UnionFind {
public:
  UnionFind(int n) {
    for (int i = 0; i < n; ++i) {
      parent.push_back(i);
      _rank.push_back(0);
      _size.push_back(1);
    }
  }

  int find(int x) {
    if (parent[x] == x) {
      return x;
    } else {
      return parent[x] = find(parent[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;

    if (_rank[x] < _rank[y]) {
      parent[x] = y;
      _size[y] += _size[x];
    } else {
      parent[y] = x;
      _size[x] += _size[y];
      if (_rank[x] == _rank[y]) ++_rank[x];
    }
  }

  bool same(int x, int y) {
    return find(x) == find(y);
  }

  int size(int x) {
    return _size[find(x)];
  }
};

struct Node {
  int a_idx;
  int b_idx;
  int dist;

  Node(int a_idx = -1, int b_idx = -1, int dist = -1) {
    this->a_idx = a_idx;
    this->b_idx = b_idx;
    this->dist = dist;
  }

  bool operator>(const Node &n) const {
    return dist > n.dist;
  }
};

int main() {
  int N, M;
  cin >> N >> M;

  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
  }

  vector<int> B(M);
  for (int i = 0; i < M; ++i) {
    cin >> B[i];
  }
  priority_queue <Node, vector<Node>, greater<Node>> pque;

  for (int i = 0; i < N; ++i) {
    int a = A[i];
    auto it = lower_bound(B.begin(), B.end(), a);
    int idx;

    if (it == B.end()) {
      idx = B.size() - 1;
    } else {
      idx = it - B.begin();
    }

    // fprintf(stderr, "idx: %d, a: %d, b: %d\n", idx, a, B[idx]);
    pque.push(Node(i, idx, abs(a - B[idx])));
    if (0 <= idx - 1) {
      pque.push(Node(i, idx - 1, abs(a - B[idx - 1])));
    }
    if (idx + 1 < M) {
      pque.push(Node(i, idx + 1, abs(a - B[idx + 1])));
    }
  }

  UnionFind uf(N + 1);
  bool checked[M];
  memset(checked, false, sizeof(checked));
  ll ans = 0;

  while (not pque.empty()) {
    Node node = pque.top();
    pque.pop();

    if (checked[node.b_idx]) continue;
    checked[node.b_idx] = true;
    ans += node.dist;

    // fprintf(stderr, "%d - %d\n", B[node.b_idx], node.dist);

    if (0 <= node.b_idx - 1 && not checked[node.b_idx - 1]) {
      pque.push(Node(node.a_idx, node.b_idx - 1, abs(B[node.b_idx] - B[node.b_idx - 1])));
    }
    if (node.b_idx + 1 < M && not checked[node.b_idx + 1]) {
      pque.push(Node(node.a_idx, node.b_idx + 1, abs(B[node.b_idx] - B[node.b_idx + 1])));
    }
  }

  cout << ans << endl;

  return 0;
}
0