結果

問題 No.1170 Never Want to Walk
ユーザー NewGardenNewGarden
提出日時 2020-08-14 22:17:31
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 2,291 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 124,080 KB
実行使用メモリ 6,480 KB
最終ジャッジ日時 2023-07-31 22:48:09
合計ジャッジ時間 5,359 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 116 ms
5,944 KB
testcase_28 AC 113 ms
5,936 KB
testcase_29 AC 116 ms
6,312 KB
testcase_30 AC 116 ms
6,212 KB
testcase_31 AC 109 ms
5,952 KB
testcase_32 AC 104 ms
5,988 KB
testcase_33 AC 106 ms
5,892 KB
testcase_34 AC 105 ms
6,188 KB
testcase_35 AC 103 ms
6,144 KB
testcase_36 AC 102 ms
5,984 KB
testcase_37 AC 103 ms
5,936 KB
testcase_38 AC 107 ms
6,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(ll i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx=100010;
const ll mod=1e9+7;

struct UnionFind {  
  vector<int> par,sizes;                     // par[i]:iの親の番号(ex)par[3]=2 : 3の親が2
  UnionFind(int n) : par(n),sizes(n,1)  {    //最初は全てが根であるとして初期化
    for(int i=0; i<n; i++){ par[i]=i; }  
  }
  int root(int x) {                       //xが属する木の根を再帰で得る:root(x)={xの木の根}
    if(par[x]==x){ return x; }
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {                 // xとyの木を併合
    int rx = root(x);                        //xの根をrx
    int ry = root(y);                        //yの根をry
    if(rx == ry){ return; }                  //xとyの根が同じ(=同じ木にある)時はそのまま
    if(sizes[rx]<sizes[ry]){ swap(rx, ry); } //xの木をおおきくする
    par[ry] = rx; 
    sizes[rx] += sizes[ry];                  //sizes[ry]はもう使わない
  }
  bool check(int x, int y) {                 // x,yが属する木が同じならtrueを返す
    return root(x) == root(y);
  }
  int size(int x){                           //xが含まれる木の大きさを返す
    return sizes[root(x)];
  }
};

int main(){
  ll n,a,b;
  cin >> n >> a >> b;
  vector<ll> x(n);

  rep(i,n){
    cin >> x[i];
  }
  UnionFind uf(n);
  for(int i=0; i<n-1; i++){
    int j = lower_bound(x.begin(),x.end(),x[i]+a) - x.begin();
    int k = upper_bound(x.begin(),x.end(),x[i]+b) - x.begin();
    if(j==n) break;
    if(j==k) continue;
    uf.unite(i,j);
    for(int t=k-1; t>j; t--){
      if(uf.check(j,t)){
        break;
      } else {
        uf.unite(j,t);
      }
    }
  }
  rep(i,n){
    cout << uf.size(i) << ln;
  }
  return 0;
}
0