結果
問題 | No.9 モンスターのレベル上げ |
ユーザー | sansaqua |
提出日時 | 2019-11-10 04:41:32 |
言語 | Common Lisp (sbcl 2.3.8) |
結果 |
AC
|
実行時間 | 384 ms / 5,000 ms |
コード長 | 11,251 bytes |
コンパイル時間 | 202 ms |
コンパイル使用メモリ | 48,640 KB |
実行使用メモリ | 78,720 KB |
最終ジャッジ日時 | 2024-09-15 04:54:49 |
合計ジャッジ時間 | 4,370 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 11 ms
25,216 KB |
testcase_01 | AC | 11 ms
25,344 KB |
testcase_02 | AC | 384 ms
78,592 KB |
testcase_03 | AC | 297 ms
66,944 KB |
testcase_04 | AC | 162 ms
48,256 KB |
testcase_05 | AC | 110 ms
40,704 KB |
testcase_06 | AC | 45 ms
30,848 KB |
testcase_07 | AC | 12 ms
25,472 KB |
testcase_08 | AC | 57 ms
32,768 KB |
testcase_09 | AC | 368 ms
77,056 KB |
testcase_10 | AC | 12 ms
25,216 KB |
testcase_11 | AC | 319 ms
78,720 KB |
testcase_12 | AC | 296 ms
78,592 KB |
testcase_13 | AC | 272 ms
78,592 KB |
testcase_14 | AC | 370 ms
76,928 KB |
testcase_15 | AC | 338 ms
73,600 KB |
testcase_16 | AC | 17 ms
26,240 KB |
testcase_17 | AC | 217 ms
56,576 KB |
testcase_18 | AC | 184 ms
52,480 KB |
testcase_19 | AC | 15 ms
25,984 KB |
コンパイルメッセージ
; compiling file "/home/judge/data/code/Main.lisp" (written 15 SEP 2024 04:54:44 AM): ; file: /home/judge/data/code/Main.lisp ; in: DEFINE-BINARY-HEAP HEAP ; (DEFINE-BINARY-HEAP HEAP :ORDER ; (LAMBDA (X Y) ; (DECLARE ((CONS UINT32 UINT32) X Y)) ; (IF (= (CAR X) (CAR Y)) ; (< (CDR X) (CDR Y)) ; (< (CAR X) (CAR Y)))) ; :ELEMENT-TYPE (CONS UINT32 UINT32)) ; --> DEFSTRUCT PROGN SB-C:XDEFUN PROGN SB-IMPL::%DEFUN SB-IMPL::%DEFUN ; --> SB-INT:NAMED-LAMBDA FUNCTION ; ==> ; (MAKE-ARRAY (1+ SIZE) :ELEMENT-TYPE '(CONS UINT32 UINT32)) ; ; caught STYLE-WARNING: ; The default initial element 0 is not a (CONS UINT32 UINT32). ; See also: ; The ANSI Standard, Function MAKE-ARRAY ; The ANSI Standard, Function UPGRADED-ARRAY-ELEMENT-TYPE ; --> DEFUN PROGN SB-IMPL::%DEFUN SB-IMPL::%DEFUN SB-INT:NAMED-LAMBDA FUNCTION ; --> BLOCK SYMBOL-MACROLET WHEN IF SETF LET SB-KERNEL:THE* ADJUST-ARRAY * ; ==> ; 1 ; ; note: doing unsigned word to integer coercion (cost 20), for: ; the first result of inline ASH ; ; compilation unit finished ; caught 1 STYLE-WARNING condition ; printed 1 note ; wrote /home/judge/data/code/Main.fasl ; compilation finished in 0:00:00.099
ソースコード
;; -*- coding: utf-8 -*- (eval-when (:compile-toplevel :load-toplevel :execute) (sb-int:defconstant-eqx OPT #+swank '(optimize (speed 3) (safety 2)) #-swank '(optimize (speed 3) (safety 0) (debug 0)) #'equal) #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t) #-swank (set-dispatch-macro-character ;; enclose the form with VALUES to avoid the capture by LOOP macro #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t))))) #+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax) #-swank (disable-debugger) ; for CS Academy ;; BEGIN_INSERTED_CONTENTS (declaim (ftype (function * (values fixnum &optional)) read-fixnum)) (defun read-fixnum (&optional (in *standard-input*)) (macrolet ((%read-byte () `(the (unsigned-byte 8) #+swank (char-code (read-char in nil #\Nul)) #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil)))) (let* ((minus nil) (result (loop (let ((byte (%read-byte))) (cond ((<= 48 byte 57) (return (- byte 48))) ((zerop byte) ; #\Nul (error "Read EOF or #\Nul.")) ((= byte #.(char-code #\-)) (setf minus t))))))) (declare ((integer 0 #.most-positive-fixnum) result)) (loop (let* ((byte (%read-byte))) (if (<= 48 byte 57) (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result)))) (return (if minus (- result) result)))))))) ;;; ;;; Binary heap ;;; (define-condition heap-empty-error (simple-error) ((heap :initarg :heap :reader heap-empty-error-heap)) (:report (lambda (condition stream) (format stream "Attempted to pop empty heap ~W" (heap-empty-error-heap condition))))) (defmacro define-binary-heap (name &key (order '#'>) (element-type 'fixnum)) "Defines the binary heap specialized for the given order and the element type. This macro defines a structure of the name NAME and relevant functions: MAKE-<NAME>, <NAME>-PUSH, <NAME>-POP, <NAME>-REINITIALIZE, <NAME>-EMPTY-P, <NAME>-COUNT, and <NAME>-PEEK." (check-type name symbol) (let* ((string-name (string name)) (fname-push (intern (format nil "~A-PUSH" string-name))) (fname-pop (intern (format nil "~A-POP" string-name))) (fname-reinitialize (intern (format nil "~A-REINITIALIZE" string-name))) (fname-empty-p (intern (format nil "~A-EMPTY-P" string-name))) (fname-count (intern (format nil "~A-COUNT" string-name))) (fname-peek (intern (format nil "~A-PEEK" string-name))) (fname-make (intern (format nil "MAKE-~A" string-name))) (acc-position (intern (format nil "~A-POSITION" string-name))) (acc-data (intern (format nil "~A-DATA" string-name)))) `(progn (defstruct (,name (:constructor ,fname-make (size &aux (data ,(if (eql element-type '*) `(make-array (1+ size)) `(make-array (1+ size) :element-type ',element-type)))))) (data #() :type (simple-array ,element-type (*))) (position 1 :type (integer 1 #.most-positive-fixnum))) (declaim #+sbcl (sb-ext:maybe-inline ,fname-push)) (defun ,fname-push (obj heap) "Adds OBJ to the end of HEAP." (declare (optimize (speed 3)) (type ,name heap)) (symbol-macrolet ((position (,acc-position heap))) (when (>= position (length (,acc-data heap))) (setf (,acc-data heap) (adjust-array (,acc-data heap) (* position 2)))) (let ((data (,acc-data heap))) (declare ((simple-array ,element-type (*)) data)) (labels ((update (pos) (declare (optimize (speed 3) (safety 0))) (unless (= pos 1) (let ((parent-pos (ash pos -1))) (when (funcall ,order (aref data pos) (aref data parent-pos)) (rotatef (aref data pos) (aref data parent-pos)) (update parent-pos)))))) (setf (aref data position) obj) (update position) (incf position) heap)))) (declaim #+sbcl (sb-ext:maybe-inline ,fname-pop)) (defun ,fname-pop (heap) "Removes and returns the element at the top of HEAP." (declare (optimize (speed 3)) (type ,name heap)) (symbol-macrolet ((position (,acc-position heap))) (let ((data (,acc-data heap))) (declare ((simple-array ,element-type (*)) data)) (labels ((update (pos) (declare (optimize (speed 3) (safety 0)) ((integer 1 #.most-positive-fixnum) pos)) (let* ((child-pos1 (+ pos pos)) (child-pos2 (1+ child-pos1))) (when (<= child-pos1 position) (if (<= child-pos2 position) (if (funcall ,order (aref data child-pos1) (aref data child-pos2)) (unless (funcall ,order (aref data pos) (aref data child-pos1)) (rotatef (aref data pos) (aref data child-pos1)) (update child-pos1)) (unless (funcall ,order (aref data pos) (aref data child-pos2)) (rotatef (aref data pos) (aref data child-pos2)) (update child-pos2))) (unless (funcall ,order (aref data pos) (aref data child-pos1)) (rotatef (aref data pos) (aref data child-pos1)))))))) (when (= position 1) (error 'heap-empty-error :heap heap)) (prog1 (aref data 1) (decf position) (setf (aref data 1) (aref data position)) (update 1)))))) (declaim (inline ,fname-reinitialize)) (defun ,fname-reinitialize (heap) "Makes HEAP empty." (setf (,acc-position heap) 1) heap) (declaim (inline ,fname-empty-p)) (defun ,fname-empty-p (heap) "Returns true iff HEAP is empty." (= 1 (,acc-position heap))) (declaim (inline ,fname-count)) (defun ,fname-count (heap) "Returns the current number of the elements in HEAP." (- (,acc-position heap) 1)) (declaim (inline ,fname-peek)) (defun ,fname-peek (heap) "Returns the topmost element of HEAP." (if (= 1 (,acc-position heap)) (error 'heap-empty-error :heap heap) (aref (,acc-data heap) 1)))))) (defmacro dbg (&rest forms) #+swank (if (= (length forms) 1) `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms)) `(format *error-output* "~A => ~A~%" ',forms `(,,@forms))) #-swank (declare (ignore forms))) (defmacro define-int-types (&rest bits) `(progn ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits) ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits))) (define-int-types 2 4 7 8 15 16 31 32 62 63 64) (declaim (inline println)) (defun println (obj &optional (stream *standard-output*)) (let ((*read-default-float-format* 'double-float)) (prog1 (princ obj stream) (terpri stream)))) (defconstant +mod+ 1000000007) ;;; ;;; Body ;;; (define-binary-heap heap :order (lambda (x y) (declare ((cons uint32 uint32) x y)) (if (= (car x) (car y)) (< (cdr x) (cdr y)) (< (car x) (car y)))) :element-type (cons uint32 uint32)) (defun heap-copy (heap) (declare (optimize (speed 3) (safety 0))) (let ((result (sb-mop:class-prototype (find-class 'heap)))) (setf (heap-data result) (copy-seq (heap-data heap)) (heap-position result) (heap-position heap)) result)) (defun main () (let* ((n (read)) (bs (make-array (* 2 n) :element-type 'uint31)) (original-que (make-heap n))) (dotimes (i n) (heap-push (cons (read-fixnum) 0) original-que)) (dotimes (i n) (setf (aref bs i) (read-fixnum) (aref bs (+ i n)) (aref bs i))) (labels ((calc (start) (let ((res 0) (que (heap-copy original-que))) (loop for i from start below (+ start n) for b = (aref bs i) for (level . count) = (heap-pop que) do (heap-push (cons (+ level (ash b -1)) (+ count 1)) que) (setq res (max res (+ count 1)))) res))) (println (loop for i below n minimize (calc i)))))) #-swank (main) ;;; ;;; Test and benchmark ;;; #+swank (defun io-equal (in-string out-string &key (function #'main) (test #'equal)) "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if the string output to *STANDARD-OUTPUT* is equal to OUT-STRING." (labels ((ensure-last-lf (s) (if (eql (uiop:last-char s) #\Linefeed) s (uiop:strcat s uiop:+lf+)))) (funcall test (ensure-last-lf out-string) (with-output-to-string (out) (let ((*standard-output* out)) (with-input-from-string (*standard-input* (ensure-last-lf in-string)) (funcall function))))))) #+swank (defun get-clipbrd () (with-output-to-string (out) (run-program "C:/msys64/usr/bin/cat.exe" '("/dev/clipboard") :output out))) #+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname)) #+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*)) #+swank (defun run (&optional thing (out *standard-output*)) "THING := null | string | symbol | pathname null: run #'MAIN using the text on clipboard as input. string: run #'MAIN using the string as input. symbol: alias of FIVEAM:RUN!. pathname: run #'MAIN using the text file as input." (let ((*standard-output* out)) (etypecase thing (null (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd))) (main))) (string (with-input-from-string (*standard-input* (delete #\Return thing)) (main))) (symbol (5am:run! thing)) (pathname (with-open-file (*standard-input* thing) (main)))))) #+swank (defun gen-dat () (uiop:with-output-file (out *dat-pathname* :if-exists :supersede) (format out ""))) #+swank (defun bench (&optional (out (make-broadcast-stream))) (time (run *dat-pathname* out)))