結果

問題 No.1170 Never Want to Walk
ユーザー tenten
提出日時 2020-08-17 11:10:12
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 1,984 bytes
コンパイル時間 2,434 ms
コンパイル使用メモリ 77,324 KB
実行使用メモリ 69,840 KB
最終ジャッジ日時 2024-10-11 13:26:20
合計ジャッジ時間 17,050 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30 TLE * 1 -- * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
        int[] arr = new int[n + 1];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        arr[n] = Integer.MAX_VALUE;
        int left = 0;
        int right = 0;
        UnionFindTree uft = new UnionFindTree(n);
        for (int i = 0; i < n; i++) {
            while (arr[left] - arr[i] < a) {
                left++;
            }
            if (left == n) {
                break;
            }
            while(arr[right] - arr[i] <= b) {
                right++;
            }
            for (int j = left; j < right; j++) {
                uft.unite(i, j);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(uft.getCount(i)).append("\n");
        }
        System.out.print(sb);
   }
   
   static class UnionFindTree {
       int[] parents;
       int[] counts;
       
       public UnionFindTree(int size) {
           parents = new int[size];
           counts = new int[size];
           for (int i = 0; i < size; i++) {
               parents[i] = i;
               counts[i] = 1;
           }
       }
       
       public int find(int x) {
           if (x == parents[x]) {
               return x;
           } else {
               return parents[x] = find(parents[x]);
           }
       }
       
       public boolean same(int x, int y) {
           return find(x) == find(y);
       }
       
       public void unite(int x, int y) {
           if (!same(x, y)) {
               int xx =find(x);
               int yy = find(y);
               parents[xx] = yy;
               counts[yy] += counts[xx];
           }
       }
       
       public int getCount(int x) {
           return counts[find(x)];
       }
   }
}
0