AtCoder abc458

関連記事

1. A

;; (chomp "chemotherapy" 3) ;=> "mother"
(defun chomp (s n)
  (subseq s n (- (length s) n)))

(defun main ()
  (princ (chomp (read-line) (read))))

#-swank(main)Code language: Lisp (lisp)
1. A

2. B – Count Adjacent Cells

(defun print-adjacent-cells (h w)
  (cond
    ((= 1 h w) (print-one-cell))
    ((= h 1) (print-row-cells w))
    ((= w 1) (print-column-cells h))
    (t (loop for i from 1 to h
             do (loop for j from 1 to w
                      for a = (if (or (= i 1) (= i h)) -1 0)
                      for b = (if (or (= j 1) (= j w)) -1 0)
                      do (princ (+ 4 a b))
                         (princ #\space))
                (terpri)))))

(defun print-one-cell ()
  (princ 0))

(defun print-row-cells (w)
  (loop for j from 1 to w
        for b = (if (or (= j 1) (= j w)) -1 0)
        do (princ (+ 2 b))
           (princ #\space)))

(defun print-column-cells (h)
  (loop for i from 1 to h
        for a = (if (or (= i 1) (= i h)) -1 0)
        do (princ (+ 2 a))
           (terpri)))

(defun main ()
  (print-adjacent-cells (read) (read)))

#-swank(main)Code language: Lisp (lisp)
2. B – Count Adjacent Cells

3. C

;; (center-c-substrings "ABCCA") ;=> 5
(defun center-c-substrings (s)
  (let ((c-pos (loop for ch across s
                     for i from 1
                     when (eql ch #\C)
                       collect i))
        (len (length s)))
    (loop for pos in c-pos
          sum (min pos (- (1+ len) pos)))))

(defun main ()
  (princ (center-c-substrings (read-line))))
#-swank(main)

"
c-pos = 3,4
len = 5

pos 3 -> pat = 3 min (5-3+1) , (3)
pos 4 -> pat = 2 min (5-4+1) , (4)
"Code language: Lisp (lisp)
3. C

4. D – Chalkboard Median(TLE22)

まずは、素朴な解をつくりました。

(defun list-median/naive (X Qn)
  (loop for (a b) in Qn
        with lst = (list X)
        do (push a lst)
           (push b lst)
           (setf lst (sort lst #'<))
           (princ (nth (/ (1- (length lst)) 2) lst))
           (terpri)))

(defun main ()
  (let* ((x (read))
         (q (read))
         (Qn (loop repeat q collect (list (read) (read)))))
    (list-median/naive x Qn))
  )
#-swank(main)

;; (list-median/naive 5 '((2 3) (1 2) (8 9)))Code language: Lisp (lisp)
4. D – Chalkboard Median(TLE22)

4.1. 左右に分けてみた(TLE20)

; (cadr '(1 2 3)) ; 2
(defun list-median/2 (X Qn)
  (loop with left = nil
        with right = nil
        with center = X
        for q in Qn
        for a = (min (car q) (cadr q))
        for b = (max (car q) (cadr q))
        do (cond
             ((< x a) (progn
                        (push x left)
                        (push a right)
                        (push b right)
                        (setf right (sort right #'<))
                        (setf x (pop right))))
             ((< b x) (progn
                        (push x right)
                        (push a left)
                        (push b left)
                        (setf left (sort left #'>))
                        (setf x (pop left))))
             (t (progn
                  (push a left)
                  (push b right))))
           (princ x)
           (terpri)))

(defun main ()
  (let* ((x (read))
         (q (read))
         (Qn (loop repeat q collect (list (read) (read)))))
    (list-median/2 x Qn))
  )
#-swank(main)Code language: Lisp (lisp)
4.1. 左右に分けてみた(TLE20)

Code language: Lisp (lisp)

5.

Code language: Lisp (lisp)

6. E – Count 123

Code language: Lisp (lisp)

7.

Code language: Lisp (lisp)

8.

Code language: Lisp (lisp)