結果

問題 No.1170 Never Want to Walk
ユーザー ks2mks2m
提出日時 2020-08-14 21:49:04
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,584 bytes
コンパイル時間 4,196 ms
コンパイル使用メモリ 76,696 KB
実行使用メモリ 74,232 KB
最終ジャッジ日時 2024-04-18 21:32:24
合計ジャッジ時間 12,321 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
42,460 KB
testcase_01 AC 52 ms
37,180 KB
testcase_02 AC 57 ms
37,180 KB
testcase_03 AC 55 ms
37,208 KB
testcase_04 AC 56 ms
37,364 KB
testcase_05 AC 55 ms
37,184 KB
testcase_06 AC 57 ms
37,280 KB
testcase_07 AC 57 ms
36,864 KB
testcase_08 AC 56 ms
37,308 KB
testcase_09 AC 56 ms
37,220 KB
testcase_10 AC 55 ms
37,304 KB
testcase_11 AC 56 ms
37,168 KB
testcase_12 AC 69 ms
37,448 KB
testcase_13 AC 79 ms
37,680 KB
testcase_14 AC 69 ms
37,692 KB
testcase_15 AC 67 ms
37,496 KB
testcase_16 AC 66 ms
37,648 KB
testcase_17 AC 72 ms
37,732 KB
testcase_18 AC 68 ms
37,132 KB
testcase_19 AC 69 ms
37,836 KB
testcase_20 AC 67 ms
37,284 KB
testcase_21 AC 72 ms
37,684 KB
testcase_22 AC 123 ms
38,924 KB
testcase_23 AC 99 ms
38,416 KB
testcase_24 AC 122 ms
39,028 KB
testcase_25 AC 122 ms
38,984 KB
testcase_26 AC 116 ms
39,212 KB
testcase_27 AC 550 ms
61,920 KB
testcase_28 AC 547 ms
60,948 KB
testcase_29 AC 520 ms
62,116 KB
testcase_30 AC 536 ms
61,904 KB
testcase_31 AC 556 ms
61,540 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int a = Integer.parseInt(sa[1]);
		int b = Integer.parseInt(sa[2]);
		sa = br.readLine().split(" ");
		int[] x = new int[n];
		for (int i = 0; i < n; i++) {
			x[i] = Integer.parseInt(sa[i]);
		}
		br.close();

		UnionFind uf = new UnionFind(n);
		int l = 0;
		int r = 0;
		for (int i = 0; i < n; i++) {
			while (l < n && x[l] - x[i] < a) {
				l++;
			}
			r = l;
			while (r < n && x[r] - x[i] <= b) {
				uf.union(i, r);
				r++;
			}
		}

		PrintWriter pw = new PrintWriter(System.out);
		for (int i = 0; i < n; i++) {
			pw.println(uf.size(i));
		}
		pw.flush();
	}

	static class UnionFind {
		int[] parent, size;

		UnionFind(int n) {
			parent = new int[n];
			size = new int[n];
			for (int i = 0; i < n; i++) {
				parent[i] = i;
				size[i] = 1;
			}
		}

		void union(int x, int y) {
			int px = find(x);
			int py = find(y);
			if (px != py) {
				parent[px] = py;
				size[py] += size[px];
			}
		}

		int find(int x) {
			if (parent[x] == x) {
				return x;
			}
			parent[x] = find(parent[x]);
			return parent[x];
		}

		/**
		 * xとyが同一連結成分か
		 */
		boolean same(int x, int y) {
			return find(x) == find(y);
		}

		/**
		 * xを含む連結成分のサイズ
		 */
		int size(int x) {
			return size[find(x)];
		}
	}
}
0