結果

問題 No.1170 Never Want to Walk
ユーザー otoshigootoshigo
提出日時 2024-04-08 10:20:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 2,016 bytes
コンパイル時間 6,729 ms
コンパイル使用メモリ 205,500 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-04-08 10:25:16
合計ジャッジ時間 10,468 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 1 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 52 ms
7,936 KB
testcase_28 AC 50 ms
7,808 KB
testcase_29 AC 53 ms
7,936 KB
testcase_30 AC 53 ms
7,936 KB
testcase_31 AC 51 ms
7,808 KB
testcase_32 AC 50 ms
7,936 KB
testcase_33 AC 51 ms
7,808 KB
testcase_34 AC 52 ms
7,936 KB
testcase_35 AC 51 ms
7,936 KB
testcase_36 AC 50 ms
7,808 KB
testcase_37 AC 50 ms
7,936 KB
testcase_38 AC 52 ms
8,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

struct union_find {
public:
    explicit union_find() {}
    explicit union_find(int N) : n(N), par(N, -1), siz(N, 1) {}
    int find(int x) {
        assert(0 <= x && x < n);
        if (par[x] == -1) return x;
        par[x] = find(par[x]);
        return par[x];
    }
    int same(int x, int y) {
        assert(0 <= x && x < n);
        assert(0 <= y && y < n);
        return find(x) == find(y);
    }
    int size(int x) {
        assert(0 <= x && x < n);
        return siz[find(x)];
    }
    bool unite(int x, int y) {
        assert(0 <= x && x < n);
        assert(0 <= y && y < n);
        x = find(x);
        y = find(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) std::swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

private:
    int n;
    std::vector<int> par, siz;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,A,B;
    cin>>N>>A>>B;
    vector<int>X(N);
    for(int i=0;i<N;i++)cin>>X[i];
    vector<int>P(N),Q(N);
    int r=0;
    for(int l=0;l<N;l++){
        chmax(r,l);
        while(r<N&&X[r]-X[l]<A)r++;
        P[l]=r;
    }
    r=0;
    for(int l=0;l<N;l++){
        chmax(r,l);
        while(r<N&&X[r]-X[l]<=B)r++;
        Q[l]=r;
    }
    vector<int>I(N);
    for(int i=0;i<N;i++){
        if(P[i]<Q[i]){
            I[P[i]]++;
            I[Q[i]-1]--;
        }
    }
    for(int i=0;i<N-1;i++)I[i+1]+=I[i];
    union_find uf(N);
    for(int i=0;i<N;i++){
        if(i+1<N&&I[i]>0)uf.unite(i,i+1);
        if(P[i]<Q[i])uf.unite(i,P[i]);
    }
    for(int i=0;i<N;i++)cout<<uf.size(i)<<"\n";
}
0