結果

問題 No.1268 Fruit Rush 2
ユーザー finefine
提出日時 2020-10-23 22:57:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,674 bytes
コンパイル時間 1,736 ms
コンパイル使用メモリ 172,460 KB
実行使用メモリ 5,516 KB
最終ジャッジ日時 2023-09-28 17:27:01
合計ジャッジ時間 4,246 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,500 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 31 ms
4,888 KB
testcase_17 AC 27 ms
4,696 KB
testcase_18 AC 29 ms
4,764 KB
testcase_19 AC 28 ms
4,624 KB
testcase_20 AC 28 ms
4,628 KB
testcase_21 AC 27 ms
4,576 KB
testcase_22 AC 30 ms
4,948 KB
testcase_23 AC 31 ms
4,840 KB
testcase_24 AC 28 ms
4,676 KB
testcase_25 AC 27 ms
4,732 KB
testcase_26 AC 45 ms
5,424 KB
testcase_27 AC 44 ms
5,364 KB
testcase_28 AC 45 ms
5,468 KB
testcase_29 AC 44 ms
5,480 KB
testcase_30 AC 45 ms
5,472 KB
testcase_31 AC 39 ms
5,424 KB
testcase_32 AC 40 ms
5,472 KB
testcase_33 AC 46 ms
5,376 KB
testcase_34 AC 26 ms
5,496 KB
testcase_35 AC 45 ms
5,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr char newl = '\n';

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

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

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

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    sort(a.begin(), a.end());

    UnionFind uf(n);
    ll ans = n;
    for (int i = n - 1; i > 0; i--) {
        for (int j = i + 1; j < n; j++) {
            if (a[j] > a[i] + 2) break;
            if (a[j] < a[i] + 2) continue;
            uf.unite(i, j);
            break;
        }
        if (a[i] - a[i - 1] != 1) continue;
        ll cur = a[i];
        ans += uf.size(i);
    }
    cout << ans << newl;

    return 0;
}
0