結果
問題 |
No.1170 Never Want to Walk
|
ユーザー |
![]() |
提出日時 | 2020-08-17 11:19:38 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 953 ms / 2,000 ms |
コード長 | 2,052 bytes |
コンパイル時間 | 4,684 ms |
コンパイル使用メモリ | 77,448 KB |
実行使用メモリ | 76,584 KB |
最終ジャッジ日時 | 2024-10-11 10:55:13 |
合計ジャッジ時間 | 20,905 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 37 |
ソースコード
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; int last = 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 = Math.max(left, last); j < right; j++) { uft.unite(i, j); } last = right - 1; } 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)]; } } }