結果

問題 No.1170 Never Want to Walk
ユーザー k
提出日時 2021-04-19 00:39:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 370 ms / 2,000 ms
コード長 1,355 bytes
コンパイル時間 2,284 ms
コンパイル使用メモリ 207,704 KB
最終ジャッジ日時 2025-01-20 21:53:47
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

  int n, a, b;
  cin >> n >> a >> b;

  vector<int> x(n);
  for (int i = 0; i < n; i++)
    cin >> x[i];

  vector<int> g(n, -1);
  vector<int> cnt(n);
  
  set<pair<int, int> > r;
  for (int i = 0; i < n; i++)
    r.insert(make_pair(x[i], i));
  
  for (int v = 0; v < n; v++) {
    if (g[v] == -1) {
      queue<int> q;
      q.push(v);
      g[v] = v;
      ++cnt[v];
      r.erase(make_pair(x[v], v));
      
      while (!q.empty()) {
        int w = q.front();
        q.pop();

        // 左にあるもの
        auto ls = r.lower_bound(make_pair(x[w] - b, 0));
        auto lt = r.upper_bound(make_pair(x[w] - a, n));
        while (ls != lt) {
          int u = ls->second;
          q.push(u);
          g[u] = v;
          ++cnt[v];
          ++ls;
          r.erase(make_pair(x[u], u));
        }
        
        // 右にあるもの
        auto rs = r.lower_bound(make_pair(x[w] + a, 0));
        auto rt = r.upper_bound(make_pair(x[w] + b, n));
        while (rs != rt) {
          int u = rs->second;
          q.push(u);
          g[u] = v;
          ++cnt[v];
          ++rs;
          r.erase(make_pair(x[u], u));
        }
      }
    }
  }
  
  for (int i = 0; i < n; i++)
    cout << cnt[g[i]] << endl;
  
  return 0;
}
0