Submission #10365003


Source Code Expand

Copy
(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 being captured 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
(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 v-symmetric-p (mat)
  (destructuring-bind (h w) (array-dimensions mat)
    (dotimes (i h)
      (dotimes (j w)
        (unless (eql (aref mat i j) (aref mat (- h 1 i) j))
          (return-from v-symmetric-p nil)))))
  t)

(defun h-symmetric-p (mat)
  (destructuring-bind (h w) (array-dimensions mat)
    (dotimes (i h)
      (dotimes (j w)
        (unless (eql (aref mat i j) (aref mat i (- w 1 j)))
          (return-from h-symmetric-p nil)))))
  t)
(defun main ()
  (let* ((h (read))
         (w (read))
         (a (read))
         (b (read))
         (plan (make-array (list h w) :element-type 'bit))
         (ns-plan (make-array (list h w) :element-type 'bit :initial-element 0))
         (we-plan (make-array (list h w) :element-type 'bit :initial-element 0)))
    (dotimes (i h)
      (let ((line (read-line)))
        (dotimes (j w)
          (when (char= #\S (aref line j))
            (setf (aref plan i j) 1)))))
    #>plan
    (let ((v4 0)
          (v3 0)
          (v2-vertical 0)
          (v2-horizontal 0)
          (res 0))
      (dotimes (i h)
        (dotimes (j w)
          (let ((sum (+ (aref plan i j)
                        (aref plan (- h 1 i) j)
                        (aref plan i (- w 1 j))
                        (aref plan (- h 1 i) (- w 1 j)))))
            (case sum
              (4 (incf v4))
              (3 (incf v3))
              (2 (cond ((or (= (aref plan i j) (aref plan (- h 1 i) j))
                            (= (aref plan i (- w 1 j)) (aref plan (- h 1 i) (- w 1 j))))
                        (incf v2-vertical))
                       ((or (= (aref plan i j) (aref plan i (- w 1 j)))
                            (= (aref plan (- h 1 i) j) (aref plan (- h 1 i) (- w 1 j))))
                        (incf v2-horizontal))))))))
      (dbg v4 v3 v2-vertical v2-horizontal)
      (assert (and (zerop (mod v4 4))
                   (zerop (mod v3 4))
                   (zerop (mod v2-vertical 4))
                   (zerop (mod v2-horizontal 4))))
      (setq v4 (floor v4 4)
            v3 (floor v3 4)
            v2-vertical (floor v2-vertical 4)
            v2-horizontal (floor v2-horizontal 4))
      (incf res (* v4 (+ a b (max a b))))
      (incf res (+ a b)) ; for the last increment
      (incf res (max (* a (+ v3 v2-vertical))
                     (* b (+ v3 v2-horizontal))))
      (when (v-symmetric-p plan)
        (decf res a))
      (when (h-symmetric-p plan)
        (decf res b))
      (println res))))

#-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 "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

#+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)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4 4
2 5
....
.SS.
..S.
....
"
    "12
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2 2
4 7
.S
S.
"
    "11
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4 8
9 13
.SS.....
.SS.....
.SS.....
.SS.....
"
    "49
")))

Submission Info

Submission Time
Task D - 石
User sansaqua
Language Common Lisp (SBCL 1.1.14)
Score 600
Code Size 6333 Byte
Status
Exec Time 265 ms
Memory 32228 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
× 3
× 37
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.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, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
01.txt 265 ms 32228 KB
02.txt 100 ms 20964 KB
03.txt 103 ms 20968 KB
04.txt 100 ms 20968 KB
05.txt 100 ms 20960 KB
06.txt 101 ms 20964 KB
07.txt 99 ms 20960 KB
08.txt 98 ms 20964 KB
09.txt 100 ms 20968 KB
10.txt 102 ms 20964 KB
11.txt 99 ms 20960 KB
12.txt 99 ms 20968 KB
13.txt 99 ms 20964 KB
14.txt 99 ms 20968 KB
15.txt 99 ms 20964 KB
16.txt 99 ms 20964 KB
17.txt 100 ms 20968 KB
18.txt 100 ms 20968 KB
19.txt 99 ms 20964 KB
20.txt 99 ms 20960 KB
21.txt 100 ms 20968 KB
22.txt 101 ms 20964 KB
23.txt 98 ms 20964 KB
24.txt 99 ms 20960 KB
25.txt 94 ms 20960 KB
26.txt 99 ms 20964 KB
27.txt 100 ms 20964 KB
28.txt 98 ms 20968 KB
29.txt 99 ms 20964 KB
30.txt 99 ms 20964 KB
31.txt 96 ms 20964 KB
32.txt 98 ms 20960 KB
33.txt 97 ms 20968 KB
34.txt 98 ms 20964 KB
sample_01.txt 95 ms 18920 KB
sample_02.txt 93 ms 18920 KB
sample_03.txt 92 ms 18920 KB