結果

問題 No.1170 Never Want to Walk
ユーザー shobonvipshobonvip
提出日時 2020-08-14 22:58:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,098 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 39,060 KB
最終ジャッジ日時 2024-10-10 16:18:20
合計ジャッジ時間 15,210 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
17,828 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 29 ms
10,752 KB
testcase_04 AC 30 ms
10,752 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 30 ms
10,880 KB
testcase_07 AC 29 ms
10,880 KB
testcase_08 AC 29 ms
10,880 KB
testcase_09 AC 29 ms
10,880 KB
testcase_10 AC 30 ms
10,880 KB
testcase_11 AC 30 ms
10,880 KB
testcase_12 AC 33 ms
10,880 KB
testcase_13 AC 35 ms
11,008 KB
testcase_14 AC 36 ms
11,008 KB
testcase_15 AC 34 ms
11,008 KB
testcase_16 AC 35 ms
11,008 KB
testcase_17 AC 36 ms
10,880 KB
testcase_18 AC 33 ms
10,752 KB
testcase_19 AC 33 ms
11,008 KB
testcase_20 AC 36 ms
10,880 KB
testcase_21 AC 33 ms
10,880 KB
testcase_22 AC 499 ms
10,880 KB
testcase_23 AC 168 ms
11,008 KB
testcase_24 AC 497 ms
10,752 KB
testcase_25 AC 495 ms
10,880 KB
testcase_26 AC 348 ms
10,880 KB
testcase_27 AC 1,599 ms
31,952 KB
testcase_28 AC 1,649 ms
31,648 KB
testcase_29 AC 1,422 ms
32,532 KB
testcase_30 AC 1,631 ms
32,316 KB
testcase_31 AC 1,538 ms
31,844 KB
testcase_32 TLE -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
class UnionFind:
	"""
	class UnionFind
	https://note.nkmk.me/python-union-find/
	
	order O(log(n))
	
	n = the number of elements
	parents = the list that contains "parents" of x. be careful that it doesn't contain "root" of x.
	"""
	def __init__(self, n):
		"""
		make UnionFind
		:param n: the number of elements
		"""
		self.n = n
		self.parents = [-1] * n
	
	def find(self, x):
		"""
		:param x: an element
		:return: the root containing x
		"""
		if self.parents[x] < 0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]
	
	def union(self, x, y):
		"""
		:param x, y: an element
		"""
		# root
		x = self.find(x)
		y = self.find(y)
		if x == y:
			# already same group so do nothing
			return
		if self.parents[x] > self.parents[y]:
			# the root should be min of group
			x, y = y, x
		# remind that x, y is the root, x < y, then, y unions to x.
		self.parents[x] += self.parents[y]
		# and then y's parent is x.
		self.parents[y] = x
	def size(self, x):
		# return the size of group
		return -self.parents[self.find(x)]
	
	def same(self, x, y):
		# return whether the x, y are in same group
		return self.find(x) == self.find(y)
	
	def members(self, x):
		# return members of group
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i) == root]
	
	def roots(self):
		# return all roots
		return [i for i, x in enumerate(self.parents) if x < 0]
	
	def group_count(self):
		# return how many groups
		return len(self.roots())
	
	def all_group_members(self):
		# return all members of all groups
		return {r: self.members(r) for r in self.roots()}

n,a,b = map(int,input().split())
x = list(map(int,input().split()))
x.sort()
union = UnionFind(n)

for i in range(n):
	targ1 = bisect.bisect_right(x, x[i]+b)
	targ2 = bisect.bisect_left(x, x[i]+a)
	targ3 = bisect.bisect_right(x, x[i]-a)
	targ4 = bisect.bisect_left(x, x[i]-b)
	if targ1 - targ2 > 0:
		for j in range(targ2, targ1):
			union.union(i, j)
	if targ3 - targ4 > 0:
		for j in range(targ4, targ3):
			union.union(i, j)

for i in range(n):
	print(union.size(i))
0