結果
| 問題 |
No.1170 Never Want to Walk
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2024-08-05 17:09:32 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 516 ms / 2,000 ms |
| コード長 | 2,754 bytes |
| コンパイル時間 | 3,200 ms |
| コンパイル使用メモリ | 90,172 KB |
| 実行使用メモリ | 83,068 KB |
| 最終ジャッジ日時 | 2024-08-05 17:09:49 |
| 合計ジャッジ時間 | 15,650 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 37 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
int[] points = new int[n];
for (int i = 0; i < n; i++) {
points[i] = sc.nextInt();
}
int left = 0;
int right = 0;
UnionFindTree uft = new UnionFindTree(n);
for (int i = 0; i < n; i++) {
while (left < n && points[left] - points[i] < a) {
left++;
}
if (left >= n) {
break;
}
while (right < n && points[right] - points[i] <= b) {
right++;
}
for (int j = left; j < right; j++) {
uft.unite(i, j);
}
left = right - 1;
}
String result = IntStream.range(0, n)
.mapToObj(i -> String.valueOf(uft.getCount(i)))
.collect(Collectors.joining("\n"));
System.out.println(result);
}
static class UnionFindTree {
int[] parents;
int[] counts;
public UnionFindTree(int size) {
parents = IntStream.range(0, size).toArray();
counts = new int[size];
Arrays.fill(counts, 1);
}
public int find(int x) {
return x== parents[x] ? x : (parents[x] = find(parents[x]));
}
public void unite(int x, int y) {
int xx = find(x);
int yy = find(y);
if (xx != yy) {
parents[xx] = yy;
counts[yy] += counts[xx];
}
}
public int getCount(int x) {
return counts[find(x)];
}
}
}
class Scanner {
BufferedReader br;
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
try {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
return st.nextToken();
}
}
}
tenten