結果
| 問題 |
No.2803 Bocching Star
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-07-21 01:14:00 |
| 言語 | Common Lisp (sbcl 2.5.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,971 bytes |
| コンパイル時間 | 79 ms |
| コンパイル使用メモリ | 40,136 KB |
| 実行使用メモリ | 38,548 KB |
| 最終ジャッジ日時 | 2024-07-21 01:14:03 |
| 合計ジャッジ時間 | 2,274 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | RE * 3 |
| other | RE * 35 |
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 21 JUL 2024 01:14:00 AM): ; wrote /home/judge/data/code/Main.fasl ; compilation finished in 0:00:00.028
ソースコード
#-swank
(unless (member :child-sbcl *features*)
(quit
:recklessly-p t
:unix-status
(process-exit-code
(run-program *runtime-pathname*
`("--control-stack-size" "1024MB"
"--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
"--eval" "(push :child-sbcl *features*)"
"--script" ,(namestring *load-pathname*))
:output t :error t :input t))))
(defun read-integers ()
(with-open-file (stream *standard-input* :direction :input :external-format :utf-8)
(loop for line = (read-line stream nil)
while line
append (mapcar #'parse-integer (split-sequence-from-string line)))))
(defun split-sequence-from-string (str)
(let ((start 0)
(len (length str))
(result '()))
(loop for i from 0 to len
when (or (eql i len) (char= (char str i) #\Space))
do (when (> i start)
(push (parse-integer (subseq str start i)) result))
(setf start (1+ i)))
(nreverse result)))
(defun union-find-make (n)
(let ((parent (make-array n :initial-contents (loop for i from 0 below n collect i)))
(rank (make-array n :initial-element 0)))
(values parent rank)))
(defun union-find-find (parent x)
(if (/= (aref parent x) x)
(setf (aref parent x) (union-find-find parent (aref parent x))))
(aref parent x))
(defun union-find-union (parent rank x y)
(let ((root-x (union-find-find parent x))
(root-y (union-find-find parent y)))
(unless (= root-x root-y)
(cond ((> (aref rank root-x) (aref rank root-y))
(setf (aref parent root-y) root-x))
((< (aref rank root-x) (aref rank root-y))
(setf (aref parent root-x) root-y))
(t
(setf (aref parent root-y) root-x)
(incf (aref rank root-x)))))))
(defun main ()
(let* ((data (read-integers))
(N (first data))
(S (second data))
(P (subseq data 2 (+ 2 N)))
(indexed-P (loop for i from 0 below N collect (cons (nth i P) i)))
(sorted-P (sort (copy-list indexed-P) #'< :key #'car))
(parent-rank (multiple-value-list (union-find-make N)))
(parent (nth 0 parent-rank))
(rank (nth 1 parent-rank))
(result '()))
(loop for i from 0 below (1- N)
for (val1 . idx1) = (nth i sorted-P)
for (val2 . idx2) = (nth (1+ i) sorted-P)
when (<= (- val2 val1) S)
do (union-find-union parent rank idx1 idx2))
(loop for i from 0 below N
for root = (union-find-find parent i)
if (= root i)
do (push (1+ i) result))
(setf result (sort result #'<))
(with-open-file (stream *standard-output* :direction :output :external-format :utf-8)
(format stream "~d~%" (length result))
(dolist (elem result)
(format stream "~d " elem))
(terpri stream))))
(main)