結果

問題 No.1285 ゴミ捨て
ユーザー ninoinuininoinui
提出日時 2020-11-14 05:23:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,575 bytes
コンパイル時間 2,025 ms
コンパイル使用メモリ 210,472 KB
実行使用メモリ 7,428 KB
最終ジャッジ日時 2023-09-30 04:45:17
合計ジャッジ時間 3,894 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

template <typename T, typename U> bool cmin(T &a, U b) { return a > b && (a = b, true); }
template <typename T, typename U> bool cmax(T &a, U b) { return a < b && (a = b, true); }

class UnionFind {
private:
  int N;
  vector<int> PS;

public:
  UnionFind() : N(0) {}
  UnionFind(int n) : N(n), PS(n, -1) {}
  bool unite(int a, int b) {
    int x = root(a), y = root(b);
    if (x == y) return false;
    if (-PS.at(x) < -PS.at(y)) swap(x, y);
    PS.at(x) += PS.at(y);
    PS.at(y) = x;
    return true;
  }
  bool same(int a, int b) { return root(a) == root(b); }
  int root(int a) {
    if (PS.at(a) < 0) return a;
    return PS.at(a) = root(PS.at(a));
  }
  int size(int a) { return -PS.at(root(a)); }
  vector<vector<int>> groups() {
    vector<int> B(N), S(N);
    for (int i = 0; i < N; i++) {
      B.at(i) = root(i);
      S.at(B.at(i))++;
    }
    vector<vector<int>> ret(N);
    for (int i = 0; i < N; i++) {
      ret.at(i).reserve(S.at(i));
    }
    for (int i = 0; i < N; i++) {
      ret.at(B.at(i)).push_back(i);
    }
    auto f = [&](const vector<int> &v) { return v.empty(); };
    ret.erase(remove_if(ret.begin(), ret.end(), f), ret.end());
    return ret;
  }
};

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) cin >> A.at(i);
  sort(A.begin(), A.end());
  UnionFind UF(N);
  for (int i = 0; i + 1 < N; i++) {
    if (A.at(i) + 1 < A.at(i + 1)) UF.unite(i, i + 1);
  }
  cout << UF.groups().size() << "\n";
}
0