結果

問題 No.1170 Never Want to Walk
ユーザー ks2mks2m
提出日時 2020-08-14 21:56:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 514 ms / 2,000 ms
コード長 1,659 bytes
コンパイル時間 2,828 ms
コンパイル使用メモリ 76,536 KB
実行使用メモリ 76,116 KB
最終ジャッジ日時 2024-04-18 21:41:58
合計ジャッジ時間 12,202 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
36,804 KB
testcase_01 AC 44 ms
36,880 KB
testcase_02 AC 43 ms
36,832 KB
testcase_03 AC 46 ms
37,096 KB
testcase_04 AC 44 ms
36,900 KB
testcase_05 AC 45 ms
37,232 KB
testcase_06 AC 47 ms
36,936 KB
testcase_07 AC 47 ms
37,196 KB
testcase_08 AC 48 ms
37,224 KB
testcase_09 AC 59 ms
37,008 KB
testcase_10 AC 49 ms
37,032 KB
testcase_11 AC 47 ms
37,140 KB
testcase_12 AC 60 ms
37,056 KB
testcase_13 AC 58 ms
37,204 KB
testcase_14 AC 71 ms
37,216 KB
testcase_15 AC 56 ms
37,128 KB
testcase_16 AC 55 ms
37,340 KB
testcase_17 AC 58 ms
37,040 KB
testcase_18 AC 60 ms
37,212 KB
testcase_19 AC 54 ms
37,328 KB
testcase_20 AC 51 ms
37,272 KB
testcase_21 AC 57 ms
37,376 KB
testcase_22 AC 70 ms
37,684 KB
testcase_23 AC 57 ms
37,348 KB
testcase_24 AC 54 ms
37,260 KB
testcase_25 AC 54 ms
36,960 KB
testcase_26 AC 53 ms
36,940 KB
testcase_27 AC 502 ms
61,924 KB
testcase_28 AC 485 ms
60,980 KB
testcase_29 AC 491 ms
62,424 KB
testcase_30 AC 514 ms
61,556 KB
testcase_31 AC 453 ms
61,752 KB
testcase_32 AC 474 ms
73,660 KB
testcase_33 AC 489 ms
64,888 KB
testcase_34 AC 476 ms
74,076 KB
testcase_35 AC 500 ms
76,116 KB
testcase_36 AC 491 ms
72,668 KB
testcase_37 AC 514 ms
73,036 KB
testcase_38 AC 491 ms
72,780 KB
権限があれば一括ダウンロードができます

ソースコード

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 = 1;
		for (int i = 0; i < n; i++) {
			if (x[r - 1] - x[i] >= a) {
				uf.union(i, r - 1);
				l = r - 1;
			}
			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