結果

問題 No.1285 ゴミ捨て
ユーザー kyo1kyo1
提出日時 2020-11-14 03:22:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 1,536 bytes
コンパイル時間 5,871 ms
コンパイル使用メモリ 204,436 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 07:20:51
合計ジャッジ時間 3,327 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 19 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 9 ms
4,380 KB
testcase_10 AC 17 ms
4,380 KB
testcase_11 AC 15 ms
4,380 KB
testcase_12 AC 12 ms
4,384 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 8 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 6 ms
4,380 KB
testcase_22 AC 19 ms
4,376 KB
testcase_23 AC 20 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

class DisjointSet {
 private:
  class Node {
    friend DisjointSet;

    std::size_t parent;
    std::size_t size;

    Node(const std::size_t parent, const std::size_t size) : parent(parent), size(size) {}
  };

  std::vector<Node> nodes;

 public:
  explicit DisjointSet(const std::size_t n) : nodes(n, Node(n, 1)) {}

  std::size_t size() const { return nodes.size(); }

  std::size_t size(const std::size_t x) { return nodes[root(x)].size; }

  std::size_t root(const std::size_t x) {
    if (nodes[x].parent == size()) return x;
    return nodes[x].parent = root(nodes[x].parent);
  }

  bool is_same(const std::size_t x, const std::size_t y) { return root(x) == root(y); }

  bool unite(const std::size_t x, const std::size_t y) {
    std::size_t rx = root(x), ry = root(y);
    if (rx == ry) return false;
    if (nodes[rx].size < nodes[ry].size) std::swap(rx, ry);
    nodes[rx].size += nodes[ry].size;
    nodes[ry].parent = rx;
    return true;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &&e : A) {
    cin >> e;
  }
  sort(A.begin(), A.end());
  int res = 0;
  vector<bool> B(N, false);
  while (true) {
    int i = 0;
    while (i < N && B[i]) {
      i++;
    }
    if (i == N) break;
    B[i] = true;
    res++;
    int j = i + 1;
    while (j < N) {
      if (!B[j] && A[i] + 1 < A[j]) {
        B[j] = true;
        i = j;
      }
      j++;
    }
  }
  cout << res << '\n';
  return 0;
}
0