結果
問題 |
No.1170 Never Want to Walk
|
ユーザー |
|
提出日時 | 2024-04-08 10:20:53 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 70 ms / 2,000 ms |
コード長 | 2,016 bytes |
コンパイル時間 | 2,340 ms |
コンパイル使用メモリ | 198,492 KB |
最終ジャッジ日時 | 2025-02-20 22:56:51 |
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 37 |
ソースコード
#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"; }