結果

問題 No.1170 Never Want to Walk
ユーザー kappybarkappybar
提出日時 2020-08-14 22:05:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 387 ms / 2,000 ms
コード長 2,234 bytes
コンパイル時間 1,698 ms
コンパイル使用メモリ 167,432 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 21:52:33
合計ジャッジ時間 7,767 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 377 ms
5,376 KB
testcase_28 AC 366 ms
5,376 KB
testcase_29 AC 387 ms
5,376 KB
testcase_30 AC 376 ms
5,376 KB
testcase_31 AC 369 ms
5,376 KB
testcase_32 AC 362 ms
5,376 KB
testcase_33 AC 347 ms
5,376 KB
testcase_34 AC 354 ms
5,376 KB
testcase_35 AC 359 ms
5,376 KB
testcase_36 AC 353 ms
5,376 KB
testcase_37 AC 360 ms
5,376 KB
testcase_38 AC 363 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

struct Unionfind{
    vector<int> parents;
    int n;
    Unionfind(int n):n(n),parents(n,-1){ }

    int find(int x){
        if(parents[x] < 0) return x;
        else return parents[x] = find(parents[x]);
    }

    void unite(int x,int y){
        x = find(x);
        y = find(y);

        if(x==y) return;
        if(parents[x] > parents[y]) swap(x,y);
        parents[x] += parents[y];
        parents[y] = x;
    }  

   int size(int x){
       return -parents[find(x)];
   }

   bool same(int x,int y){
       return find(x)==find(y);
   }

   vector<int> members(int x){
       int root = find(x);
       vector<int> member;
       for(int i=0;i<n;i++){
           if(find(i)==root ){
               member.push_back(i);
           }
       }
      return member;
  }

  int group_cnt(){
      int c = 0;
      rep(i,n){
          if(parents[i] < 0) ++c;
      }
      return c;
  }
};

int main(){
    int n,a,b;
    cin >> n >> a >> b;
    Unionfind uf(n);

    vector<int> x(n);
    rep(i,n) cin >> x[i];
    rep(i,n-1){
        bool find = false;
        auto it1 = lower_bound(x.begin(),x.end(),x[i+1]-b);
        auto it2 = lower_bound(x.begin(),x.end(),x[i+1]+a);
        //cout << it1-x.begin() << " " << it2-x.begin() << endl;
        if(it1!=x.end()){
            if(*it1 <= x[i]-a) find = true;
        }
        if(it2!=x.end()){
            if(*it2 <= x[i]+b) find = true;
        }
        if(find){
            uf.unite(i,i+1);
        }
    }
    /*
    rep(i,n){
        cout << uf.size(i) << endl;
    }
    */
    rep(i,n){
        auto it1 = lower_bound(x.begin(),x.end(),x[i]+a);
        auto it2 = lower_bound(x.begin(),x.end(),x[i]-b);
        if(it1!=x.end()){
            if(*it1 <= x[i]+b) uf.unite(i,it1-x.begin());
        }
        if(it2!=x.end()){
            if(*it2 <= x[i]-a) uf.unite(i,it2-x.begin());
        }
    }
    rep(i,n) cout << uf.size(i) << endl;
    return 0;
}
0