結果

問題 No.1170 Never Want to Walk
ユーザー hedwig100hedwig100
提出日時 2020-08-14 21:55:30
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 2,397 bytes
コンパイル時間 1,939 ms
コンパイル使用メモリ 169,332 KB
実行使用メモリ 5,632 KB
最終ジャッジ日時 2024-04-18 21:40:21
合計ジャッジ時間 5,126 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 87 ms
5,504 KB
testcase_28 AC 85 ms
5,504 KB
testcase_29 AC 90 ms
5,504 KB
testcase_30 AC 88 ms
5,632 KB
testcase_31 AC 86 ms
5,632 KB
testcase_32 AC 73 ms
5,504 KB
testcase_33 AC 82 ms
5,632 KB
testcase_34 AC 75 ms
5,504 KB
testcase_35 AC 75 ms
5,504 KB
testcase_36 AC 72 ms
5,504 KB
testcase_37 AC 73 ms
5,504 KB
testcase_38 AC 76 ms
5,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

struct UnionFind {
    int N;
    vector<int> parents;

    UnionFind(int _N) : N(_N){
        parents.assign(N,-1);
    }

    int find(int x) { //xの親を返す
        if (parents[x] < 0) return x;
        return parents[x] = find(parents[x]);
    }
    
    void unite(int x, int y) {  //xとyの含むグループを併合
        int px = find(x);
        int py = find(y);
        if (parents[px] > parents[py]) swap(px,py); 
        if (px != py) {
            parents[px] += parents[py];
            parents[py] = px;
        }     
    }

    bool same(int x, int y) { //x,yが同じグループにいるか判定
        return find(x) == find(y);
    }

    int size(int x) { //xと同じグループのメンバーの個数
        return -parents[find(x)];
    }
    
    vector<int> root() {//ufの根を列挙
        vector<int> res;
        for (int i = 0; i < N; i ++) {
            if (parents[i] < 0) res.push_back(i);
        }
        return res;
    }

    int group_count() { //ufのグループの数を数える
        int cnt = 0;
        for (int i = 0; i < N; i ++) {
            if (parents[i] < 0) cnt ++;
        }
        return cnt;
    }
};

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

    int N; 
    ll A,B; cin >> N >> B >> A;
    vector<ll> X(N);
    rep(i,N) cin >> X[i];
    
    UnionFind uf(N);
    rep(i,N) {
        int l = lower_bound(X.begin(),X.end(),X[i] - A) - X.begin();
        if (l < N && X[l] <= X[i] - B) uf.unite(i,l);
        int r = lower_bound(X.begin(),X.end(),X[i] + B) - X.begin();
        if (r < N && X[r] <= X[i] + A) uf.unite(i,r);
    }
    rep(i,N - 1) {
        int l = lower_bound(X.begin(),X.end(),X[i + 1] - A) - X.begin();
        if (l < N && X[l] <= X[i] - B) uf.unite(i,i + 1);
        int r = lower_bound(X.begin(),X.end(),X[i + 1] + B) - X.begin();
        if (r < N && X[r] <= X[i] + A) uf.unite(i,i + 1);
    }
    rep(i,N) cout << uf.size(i) << '\n';
    return 0;
}
0