Submission #5665748


Source Code Expand

;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;;;                                        ;
;;; Memoization macro
;;; 

;; TODO: detailed documentation

;; Usage example:
;; (with-cache (:hash-table :test #'equal :key #'cons)
;;   (defun ...))
;; (with-cache (:array (10 10 * 10) :initial-element -1 :element-type 'fixnum)
;;   (defun foo (a b c d) ...)) ; => C is ignored.
;; (with-cache (:array (10 10) :initial-element -1 :element-type 'fixnum :debug t)
;;   (defun foo (x y) ...)) ; executes with trace of foo


;; FIXME: *RECURSION-DEPTH* should be included within the macro.
(declaim (type (integer 0 #.most-positive-fixnum) *recursion-depth*))
(defparameter *recursion-depth* 0)

(defmacro with-cache (cache-attribs def-form)
  (let* ((cache-attribs (if (atom cache-attribs) (list cache-attribs) cache-attribs))
         (cache-type (first cache-attribs))
         (dimensions-with-* (when (eql cache-type :array) (second cache-attribs)))
         (dimensions (remove '* dimensions-with-*))
         (rank (length dimensions))
         (rest-attribs (ecase cache-type
                         (:hash-table (cdr cache-attribs))
                         (:array (cddr cache-attribs))))
         (key (prog1 (getf rest-attribs :key) (remf rest-attribs :key)))
         (debug (prog1 (getf rest-attribs :debug) (remf rest-attribs :debug)))
         (cache-form (case cache-type
                       (:hash-table `(make-hash-table ,@rest-attribs))
                       (:array `(make-array (list ,@dimensions) ,@rest-attribs))))
         (initial-element (when (eql cache-type :array)
                            (assert (member :initial-element rest-attribs))
                            (getf rest-attribs :initial-element))))
    (let ((cache (gensym))
          (value (gensym))
	  (present-p (gensym))
          (name-alias (gensym))
	  (args-lst (gensym))
          (indices (loop repeat rank collect (gensym))))
      (labels ((debug (name args obj)
                 (let ((value (gensym)))
                   (if debug
                       `(progn
                          (format t "~A~A: (~A ~{~A~^ ~}) =>~%"
                                  (make-string *recursion-depth*
                                               :element-type 'base-char
                                               :initial-element #\ )
                                  *recursion-depth*
                                  ',name
                                  (list ,@args))
                          (let ((,value (let ((*recursion-depth* (1+ *recursion-depth*)))
                                          ,obj)))
                            (format t "~A~A: (~A ~{~A~^ ~}) => ~A~%"
                                    (make-string *recursion-depth*
                                               :element-type 'base-char
                                               :initial-element #\ )
                                    *recursion-depth*
                                    ',name
                                    (list ,@args)
                                    ,value)
                            ,value))
                       obj)))
               (make-cache-check-form (cache-type name args)
                 (debug name
                        args
                        (case cache-type
                          (:hash-table
                           `(let ((,args-lst (funcall ,(or key #'list) ,@args)))
                              (multiple-value-bind (,value ,present-p)
                                  (gethash ,args-lst ,cache)
                                (if ,present-p
                                    ,value
                                    (setf (gethash ,args-lst ,cache)
                                          (,name-alias ,@args))))))
                          (:array
                           (let ((memoized-args (loop for dimension in dimensions-with-*
                                                      for arg in args
                                                      unless (eql dimension '*)
                                                      collect arg)))
                             (if key
                                 `(multiple-value-bind ,indices
                                      (funcall ,key ,@memoized-args)
                                    (let ((,value (aref ,cache ,@indices)))
                                      (if (eql ,initial-element ,value)
                                          (setf (aref ,cache ,@indices)
                                                (,name-alias ,@args))
                                          ,value)))
                                 `(let ((,value (aref ,cache ,@memoized-args)))
                                    (if (eql ,initial-element ,value)
                                        (setf (aref ,cache ,@memoized-args)
                                              (,name-alias ,@args))
                                        ,value))))))))
               (make-reset-form (cache-type)
                 (case cache-type
                   (:hash-table `(setf ,cache (make-hash-table ,@rest-attribs)))
                   (:array `(prog1 nil
                              (fill (array-storage-vector ,cache) ,initial-element)))))
               (make-reset-name (name)
                 (intern (format nil "RESET-~A" (symbol-name name))))
               (extract-declarations (body)
                 (remove-if-not (lambda (form) (eql 'declare (car form))) body)))
        (ecase (car def-form)
          ((defun)
           (destructuring-bind (_ name args &body body) def-form
             (declare (ignore _))
             `(let ((,cache ,cache-form))
                (defun ,(make-reset-name name) () ,(make-reset-form cache-type))
                (defun ,name ,args
                  ,@(extract-declarations body)
                  (labels ((,name-alias ,args ,@body))
                    (declare (inline ,name-alias))
                    ,(make-cache-check-form cache-type name args))))))
          ((nlet)
           (destructuring-bind (_ name bindings &body body) def-form
             (declare (ignore _))
             `(let ((,cache ,cache-form))
                (nlet ,name ,bindings
                  ,@(extract-declarations body)
                  ,(let ((args (mapcar (lambda (x) (if (atom x) x (car x))) bindings)))
                     `(labels ((,name-alias ,args ,@body))
                        (declare (inline ,name-alias))
                        ,(make-cache-check-form cache-type name args)))))))
          ((labels flet)
           (destructuring-bind (_ definitions &body labels-body) def-form
             (declare (ignore _))
             (destructuring-bind (name args &body body) (car definitions)
               `(let ((,cache ,cache-form))
                  (,(car def-form)
                   ((,(make-reset-name name) () ,(make-reset-form cache-type))
                    (,name ,args
                           ,@(extract-declarations body)
                           (labels ((,name-alias ,args ,@body))
                             (declare (inline ,name-alias))
                             ,(make-cache-check-form cache-type name args)))
                    ,@(cdr definitions))
                   (declare (ignorable #',(make-reset-name name)))
                   ,@labels-body))))))))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (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
                                  ;; (return-from read-fixnum 0)
                                  (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))))))))

(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

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (ss (make-array n :element-type 'int32))
         (res 0))
    (declare (uint31 n) (fixnum res))
    (dotimes (i n) (setf (aref ss i) (read-fixnum)))
    (with-cache (:hash-table :test #'equal :key #'cons)
      (labels ((dp (k c)
                 (declare (uint31 k c)
                          (values fixnum))
                 (if (= k 1)
                     0
                     (+ (dp (- k 1) c)
                        (aref ss (- n 1 (* (- k 1) c)))
                        (aref ss (* (- k 1) c))))))
        (loop for k from 2 below n
              do (loop for c from 1 to (- (ceiling (- n 1) k) 1)
                       do (multiple-value-bind (quot rem) (floor (- n 1) c)
                            (unless (and (zerop rem)
                                         (<= 2 quot (* 2 (- k 1))))
                              (setf res (max res (dp k c)))))))
        (println res)))))

#-swank(main)

Submission Info

Submission Time
Task F - Frog Jump
User sansaqua
Language Common Lisp (SBCL 1.1.14)
Score 600
Code Size 10883 Byte
Status AC
Exec Time 435 ms
Memory 115300 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 75
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, 49.txt, 50.txt, 51.txt, 52.txt, 53.txt, 54.txt, 55.txt, 56.txt, 57.txt, 58.txt, 59.txt, 60.txt, 61.txt, 62.txt, 63.txt, 64.txt, 65.txt, 66.txt, 67.txt, 68.txt, 69.txt, 70.txt, 71.txt, 72.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 289 ms 28644 KiB
02.txt AC 75 ms 16868 KiB
03.txt AC 74 ms 16868 KiB
04.txt AC 74 ms 16872 KiB
05.txt AC 74 ms 16864 KiB
06.txt AC 74 ms 16872 KiB
07.txt AC 74 ms 16872 KiB
08.txt AC 74 ms 16872 KiB
09.txt AC 74 ms 16872 KiB
10.txt AC 75 ms 16868 KiB
11.txt AC 74 ms 16868 KiB
12.txt AC 74 ms 16864 KiB
13.txt AC 74 ms 16872 KiB
14.txt AC 75 ms 16868 KiB
15.txt AC 113 ms 43492 KiB
16.txt AC 267 ms 115300 KiB
17.txt AC 392 ms 115172 KiB
18.txt AC 192 ms 76256 KiB
19.txt AC 390 ms 115176 KiB
20.txt AC 386 ms 115172 KiB
21.txt AC 407 ms 115172 KiB
22.txt AC 412 ms 115172 KiB
23.txt AC 402 ms 115172 KiB
24.txt AC 404 ms 115168 KiB
25.txt AC 417 ms 115168 KiB
26.txt AC 421 ms 115172 KiB
27.txt AC 420 ms 115172 KiB
28.txt AC 250 ms 115168 KiB
29.txt AC 258 ms 115172 KiB
30.txt AC 288 ms 115172 KiB
31.txt AC 300 ms 115172 KiB
32.txt AC 362 ms 115176 KiB
33.txt AC 391 ms 115172 KiB
34.txt AC 360 ms 115172 KiB
35.txt AC 392 ms 115176 KiB
36.txt AC 399 ms 115168 KiB
37.txt AC 417 ms 115168 KiB
38.txt AC 407 ms 115168 KiB
39.txt AC 404 ms 115168 KiB
40.txt AC 397 ms 115172 KiB
41.txt AC 416 ms 115172 KiB
42.txt AC 414 ms 115168 KiB
43.txt AC 403 ms 115172 KiB
44.txt AC 420 ms 115176 KiB
45.txt AC 365 ms 115172 KiB
46.txt AC 415 ms 115176 KiB
47.txt AC 435 ms 115168 KiB
48.txt AC 414 ms 115168 KiB
49.txt AC 425 ms 115172 KiB
50.txt AC 434 ms 115172 KiB
51.txt AC 427 ms 115168 KiB
52.txt AC 426 ms 115172 KiB
53.txt AC 423 ms 115172 KiB
54.txt AC 421 ms 115172 KiB
55.txt AC 429 ms 115176 KiB
56.txt AC 421 ms 115172 KiB
57.txt AC 422 ms 115176 KiB
58.txt AC 415 ms 115172 KiB
59.txt AC 417 ms 115172 KiB
60.txt AC 415 ms 115176 KiB
61.txt AC 420 ms 115172 KiB
62.txt AC 435 ms 115168 KiB
63.txt AC 430 ms 115172 KiB
64.txt AC 415 ms 115172 KiB
65.txt AC 419 ms 115176 KiB
66.txt AC 418 ms 115172 KiB
67.txt AC 313 ms 115172 KiB
68.txt AC 323 ms 115172 KiB
69.txt AC 329 ms 115172 KiB
70.txt AC 325 ms 115168 KiB
71.txt AC 328 ms 115172 KiB
72.txt AC 323 ms 115172 KiB
s1.txt AC 75 ms 16872 KiB
s2.txt AC 75 ms 16868 KiB
s3.txt AC 75 ms 16872 KiB