結果

問題 No.1170 Never Want to Walk
ユーザー sapphire__15sapphire__15
提出日時 2022-08-08 04:08:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 354 ms / 2,000 ms
コード長 3,840 bytes
コンパイル時間 3,200 ms
コンパイル使用メモリ 207,820 KB
実行使用メモリ 5,592 KB
最終ジャッジ日時 2023-10-17 22:25:38
合計ジャッジ時間 9,948 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 4 ms
4,348 KB
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 4 ms
4,348 KB
testcase_26 AC 4 ms
4,348 KB
testcase_27 AC 337 ms
5,592 KB
testcase_28 AC 334 ms
5,364 KB
testcase_29 AC 345 ms
5,592 KB
testcase_30 AC 345 ms
5,592 KB
testcase_31 AC 340 ms
5,592 KB
testcase_32 AC 340 ms
5,592 KB
testcase_33 AC 337 ms
5,364 KB
testcase_34 AC 347 ms
5,592 KB
testcase_35 AC 342 ms
5,592 KB
testcase_36 AC 335 ms
5,592 KB
testcase_37 AC 336 ms
5,592 KB
testcase_38 AC 354 ms
5,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,n) for(int i = 0; i < int(n); i++)
#define per(i,n) for(int i = (n) - 1; 0 <= i; i--)
#define rep2(i,l,r) for(int i = (l); i < int(r); i++)
#define per2(i,l,r) for(int i = (r) - 1; int(l) <= i; i--)
#define MM << " " <<
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

// const ll MOD = 1'000'000'007;
const ll MOD = 998'244'353;

///////////////////////////////////////////////////////////////

template <class T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
using maxheap = priority_queue<T>;

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}


class UnionFind {
    int _n;
    int _size;
    std::vector<int> par_size;

    public:
    UnionFind() :_n(0), _size(0){}
    UnionFind(int n) : _n(n), _size(n), par_size(n, -1) {}

    int unite(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        a = root(a), b = root(b);
        if(a == b) return a;
        _size--;
        if(-par_size[a] < -par_size[b]) {
            par_size[b] += par_size[a];
            return par_size[a] = b;
        }
        else {
            par_size[a] += par_size[b];
            return par_size[b] = a;
        }
    }

    int root(int a) {
        assert(0 <= a && a < _n);
        if(par_size[a] < 0) return a;
        return par_size[a] = root(par_size[a]); 
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return root(a) == root(b);
    }

    int size(int a) {
        assert(0 <= a && a< _n);
        return -par_size[root(a)];
    }

    int size() {return _size;}

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = root(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }
};

int main() {
    int n, a, b; cin >> n >> a >> b;
    vector<int> da(n);
    rep(i,n) cin >> da[i];
    vector<int> ps(n+1);
    da.eb(INT_MAX-10);
    UnionFind uf(n);
    int f = 0, g = 0;
    rep(i,n) {
        while(da[f] - da[i] < a) {
            f++;
        }
        while(da[g] - da[i] <= b) {
            g++;
        }
        if(da[f] - da[i] <= b) {
            ps[f]++;
            uf.unite(i, f);
            ps[g-1]--;
            uf.unite(i, g-1);
        }
    }
    
    rep(i,n) ps[i+1] += ps[i];
    rep2(i,0,n-1) if(ps[i] > 0) uf.unite(i, i+1);
    rep(i,n) cout << uf.size(i) << endl;
}
0