結果

問題 No.1170 Never Want to Walk
ユーザー noritakenoritake
提出日時 2020-08-14 23:17:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,890 bytes
コンパイル時間 1,969 ms
コンパイル使用メモリ 169,660 KB
実行使用メモリ 13,884 KB
最終ジャッジ日時 2024-04-18 23:08:07
合計ジャッジ時間 11,309 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 349 ms
6,940 KB
testcase_28 AC 330 ms
6,940 KB
testcase_29 AC 347 ms
6,944 KB
testcase_30 AC 351 ms
6,940 KB
testcase_31 AC 349 ms
6,940 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<char> vc;
#define INF __INT32_MAX__
#define LINF __LONG_LONG_MAX__

/**
 * UnionFind(重みなし)
 */
class UnionFind
{
    private:
        vector<long long> parents;

    public:
        UnionFind(long long N)
        {
            this->parents = vector<long long>(N, -1);
        }

        long long size(long long A)
        {
            return -1 * parents[this->root(A)];
        }

        long long root(long long A)
        {
            if (this->parents[A] < 0) return A;

            this->parents[A] = this->root(this->parents[A]);
            return this->parents[A];
        }

        bool unite(long long A, long long B)
        {
            long long ra = this->root(A);
            long long rb = this->root(B);

            if (ra == rb) return false;

            if (this->size(ra) > this->size(rb)) {
                this->parents[ra] += this->parents[rb];
                this->parents[rb] = ra;
            } else {
                this->parents[rb] += this->parents[ra];
                this->parents[ra] = rb;
            }
            return true;
        }

        bool isUnite(long long A, long long B)
        {
            return this->root(A) == this->root(B);
        }

        void show()
        {
            for (int i = 0; i < this->parents.size(); i++) {
                this->root(i); // 親の更新を行う
                cout << "i: " << i << ", parents[i]: " << this->parents[i] << endl;
            }
        }
};

int main() {
    ll N, A, B; cin >> N >> A >> B;

    vl X(N + 2); rep(i, N) cin >> X[i + 1];
    X[0] = 0; X[N + 1] = LINF;

    UnionFind uni(N);

    for (int i = 0; i < N; i++) {
        int l = i;
        int r = N + 2;

        while (r - l >= 1) {
            int mid = (l + r) / 2;
            ll d = X[mid] - X[i + 1];
            if (d >= A) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        int ra = r;

        l = i; r = N + 2;
        while (r - l >= 1) {
            int mid = (l + r) / 2;
            ll d = X[mid] - X[i + 1];
            if (d > B) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        int rb = r;

        if (uni.isUnite(i, ra) && uni.isUnite(i, rb - 1)) continue;

        for (ll j = ra; j < rb; j++) {
            if (!uni.isUnite(i, j - 1)) uni.unite(i, j - 1);
        }
        // cout << "i: " << (i + 1) << ", X[i]: " << X[i + 1] << ", ra: " << ra << ", X[ra]: " << X[ra] << ", rb: " << rb << ", X[rb]: " << X[rb] << endl;
        // cout << endl;
    }
// uni.show();
    for (ll i = 0; i < N; i++) {
        cout << uni.size(i) << endl;
    }
}
0