結果

問題 No.1170 Never Want to Walk
ユーザー AnchorBluesAnchorBlues
提出日時 2023-03-04 19:06:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 6,980 bytes
コンパイル時間 2,010 ms
コンパイル使用メモリ 181,348 KB
実行使用メモリ 7,160 KB
最終ジャッジ日時 2023-10-18 04:39:33
合計ジャッジ時間 5,167 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 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 3 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 70 ms
6,896 KB
testcase_28 AC 70 ms
6,896 KB
testcase_29 AC 72 ms
7,160 KB
testcase_30 AC 72 ms
7,160 KB
testcase_31 AC 70 ms
6,896 KB
testcase_32 AC 82 ms
6,896 KB
testcase_33 AC 85 ms
6,896 KB
testcase_34 AC 83 ms
7,160 KB
testcase_35 AC 84 ms
7,160 KB
testcase_36 AC 81 ms
6,896 KB
testcase_37 AC 83 ms
6,896 KB
testcase_38 AC 86 ms
7,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;

const ll INF = 1LL << 60;

template <class T>
void chmax(T& a, T b) {
    if (b > a) a = b;
}
template <class T>
void chmin(T& a, T b) {
    if (b < a) a = b;
}

template <typename T, typename S>
std::ostream& operator<<(std::ostream& os, const pair<T, S>& x) noexcept {
    return os << "(" << x.first << ", " << x.second << ")";
}

template <typename T>
void print_vector(vector<T> a) {
    cout << '[';
    for (int i = 0; i < a.size(); i++) {
        cout << a[i];
        if (i != a.size() - 1) {
            cout << ", ";
        }
    }
    cout << ']' << endl;
}

class UnionFind {
   public:
    // コンストラクタ
    UnionFind();
    UnionFind(int);

    // 根を求める
    int root(int x) const;

    // x と y が同じグループに属するかどうか (根が一致するかどうか)
    bool issame(int x, int y) const;

    // x を含むグループと y を含むグループとを併合する
    bool unite(int x, int y);

    // 要素の追加。まだ追加されていない要素だったらその要素だけから成る集合を作る
    void add_elem(int x);
    // x を含むグループのサイズ
    int size(int x) const;

    // 島の個数
    int n_unions() const;

    // 要素xがすでに追加されているかどうかを判定
    bool is_added(int x) const;

   private:
    // 経路圧縮する。根を返す
    int path_compression(int x);
    std::vector<int> parents;
    std::vector<int> sizes;
};

// コンストラクタ
UnionFind::UnionFind() {}

UnionFind::UnionFind(int N) {
    parents = std::vector<int>(N, -1);
    sizes = std::vector<int>(N, -1);
}

// 根を求める
int UnionFind::root(int x) const {
    if (parents[x] == -1) return x;  // x が根の場合は x を返す

    return root(parents[x]);
}

// x と y が同じグループに属するかどうか (根が一致するかどうか)
bool UnionFind::issame(int x, int y) const { return root(x) == root(y); }

// x を含むグループと y を含むグループとを併合する
bool UnionFind::unite(int x, int y) {
    // x, y をそれぞれ根まで移動する
    if (!is_added(x)) sizes[x] = 1;
    if (!is_added(y)) sizes[y] = 1;
    int root_x = path_compression(x);
    int root_y = path_compression(y);

    // すでに同じグループのときは何もしない
    if (root_x == root_y) return false;

    if (!is_added(root_x)) sizes[root_x] = 1;
    if (!is_added(root_y)) sizes[root_y] = 1;

    // union by size (y 側のサイズが小さくなるようにする)
    if (sizes[root_x] < sizes[root_y]) std::swap(root_x, root_y);

    // y を x の子とする
    parents[root_y] = root_x;
    sizes[root_x] += sizes[root_y];
    return true;
}

void UnionFind::add_elem(int x) {
    // すでに追加されていたら何もしない
    if (is_added(x)) return;
    sizes[x] = 1;
}

// x を含むグループのサイズ。x が追加されていない要素の場合は「1」を返す
int UnionFind::size(int x) const {
    if (is_added(x)) return sizes[root(x)];
    return 1;
}

// 島の個数
int UnionFind::n_unions() const {
    std::unordered_set<int> s;
    for (size_t i = 0; i < sizes.size(); i++)
        if (is_added(i)) s.insert(root(i));

    return s.size();
}

// 経路圧縮をしながら根を求める
int UnionFind::path_compression(int x) {
    if (parents[x] == -1) return x;  // x が根の場合は x を返す
    parents[x] = path_compression(parents[x]);
    return parents[x];
}

bool UnionFind::is_added(int x) const { return sizes[x] != -1; }

typedef ll idx;
typedef ll element;
typedef std::function<element(idx)> Query;

class BinarySearch {
   public:
    // コンストラクタ
    BinarySearch();
    // [left, right)の範囲で探索する
    BinarySearch(Query, idx left, idx right, bool ascending = true);

    // 要素xが存在するかどうかを判定し、存在したらそのインデックスを、存在しなかったら-1を返す
    idx find(element x) const;

    // 要素xの個数をカウントする
    ll count(element x) const;

    // ascending = true のとき: f(idx)>=xを満たす最小のidxを求める
    // ascending = false のとき: f(idx)<xを満たす最小のidxを求める
    // (いずれにしても、idxを小さい順から投げていったときにboolが反転する場所のidx)
    idx bound(element x) const;

   private:
    Query query_;
    idx left_;
    idx right_;
    bool ascending_;
};

// コンストラクタ
BinarySearch::BinarySearch() {}

BinarySearch::BinarySearch(Query query, idx left, idx right, bool ascending) {
    query_ = query;
    left_ = left;
    right_ = right;
    ascending_ = ascending;
}

idx BinarySearch::find(element x) const {
    idx left = left_;
    idx right = right_ - 1;

    while (right >= left) {
        idx mid = left + (right - left) / 2;
        element ret = query_(mid);
        if (ret == x)
            return mid;
        else if (!ascending_ ^ (ret > x))
            right = mid - 1;
        else
            left = mid + 1;
    }
    return -1;
}

ll BinarySearch::count(element x) const {
    idx a = bound(x);
    idx b = bound(x + 1);
    return std::abs(a - b);
}

idx BinarySearch::bound(element x) const {
    idx ng = left_ - 1;
    idx ok = right_;

    while (abs(ok - ng) > 1) {
        idx mid = (ok + ng) / 2;

        if (!ascending_ ^ (query_(mid) >= x))
            ok = mid;
        else
            ng = mid;
    }
    return ok;
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    ll N, A, B;
    cin >> N >> A >> B;
    vector<ll> X(N);
    for (int i = 0; i < N; i++) {
        cin >> X[i];
    }
    auto uf = UnionFind(N);
    vector<int> whites(N, 1);
    auto h = [&](ll x) { return whites[x]; };
    for (int i = 0; i < N; i++) {
        ll x = X[i];
        auto itb = upper_bound(X.begin(), X.end(), x + B);
        auto ita = lower_bound(X.begin(), X.end(), x + A);
        auto ib = distance(X.begin(), itb);
        auto ia = distance(X.begin(), ita);
        if (ia == ib) continue;
        if (ia == N) continue;
        uf.unite(i, ia);
        whites[i] = 0;
        auto bs = BinarySearch(h, ia, ib);
        auto ix = bs.bound(1);
        // std::cout << ia << ' ' << ib << "\n";
        // std::cout << ix << "\n";
        if (ia == ix) {
            for (int j = ia; j < ib - 1; j++) {
                uf.unite(j, j + 1);
                whites[j] = 0;
            }
        } else {
            for (int j = ix - 1; j < ib - 1; j++) {
                uf.unite(j, j + 1);
                whites[j] = 0;
            }
        }
    }
    for (int i = 0; i < N; i++) {
        std::cout << uf.size(i) << "\n";
    }
    return 0;
}
0