Submission #6035280


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 (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
;; unfinished.

(defun find-optimum (sequence predicate &key (start 0) end)
  "Returns an index x that satisfies (NOT (FUNCALL PREDICATE SEQUENCE[y]
SEQUENCE[x])) (i.e. SEQUENCE[x] >= SEQUENCE[y]) for all the indices y and
returns SEQUENCE[x] as the second value."
  (declare ((or null (integer 0 #.most-positive-fixnum)) end)
           ((integer 0 #.most-positive-fixnum) start)
           (function predicate)
           (sequence sequence))
  (etypecase sequence
    (list (error "Not implemented yet."))
    (vector
     (let ((end (or end (length sequence))))
       (unless (<= start end)
         (error "Can't find optimal value in null interval [~A, ~A)" start end))
       (let ((optimum (aref sequence 0))
             (index 0))
         (dotimes (i (length sequence) (values index optimum))
           (unless (funcall predicate optimum (aref sequence i))
             (setq optimum (aref sequence i)
                   index i))))))))

(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


;; 前処理:
;; すべての数がN以上になるまでシミュレーションする。
;; 主処理:
;; N+1回の操作後には、最大の数がN+1減ってほかの数が変化しないので
;; すべての数が2N以下になる操作回数が数えられる。
;; 後処理:
;; すべての数がN未満になるまでシミュレーションする。

(defun main ()
  (let* ((n (read))
         (as (make-array n :element-type 'fixnum))
         (res 0))
    (dotimes (i n) (setf (aref as i) (read)))
    ;; prelude
    (loop
      (when (or (every (lambda (a) (< a n)) as)
                (every (lambda (a) (>= a n)) as))
        (return))
      (let ((pos (find-optimum as #'>)))
        (incf res)
        (decf (aref as pos) n)
        (dotimes (i n)
          (unless (= i pos)
            (incf (aref as i))))))
    ;; main
    (dotimes (i n)
      (when (> (aref as i) (* 2 n))
        (let* ((delta (* (+ n 1) (ceiling (- (aref as i) (* 2 n)) (+ n 1))))
               (count (* (+ n 1) (floor delta (+ n 1)))))
          (incf res count)
          (decf (aref as i) delta))))
    ;; postlude
    (loop
      (when (every (lambda (a) (< a n)) as)
        (return))
      (let ((pos (find-optimum as #'>)))
        (incf res)
        (decf (aref as pos) n)
        (dotimes (i n)
          (unless (= i pos)
            (incf (aref as i))))))
    (println res)))

#-swank(main)

Submission Info

Submission Time
Task E - Decrease (Judge ver.)
User sansaqua
Language Common Lisp (SBCL 1.1.14)
Score 600
Code Size 3759 Byte
Status AC
Exec Time 71 ms
Memory 14948 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 5
AC × 35
Set Name Test Cases
Sample example0, example1, example2, example3, example4
All example0, example1, example2, example3, example4, maxrand0, maxrand1, maxrand2, maxrand3, maxrand4, maxrand5, maxrand6, maxrand7, maxrand8, maxrand9, rand0, rand1, rand2, rand3, rand4, rand5, rand6, rand7, rand8, rand9, small0, small1, small2, small3, small4, small5, small6, small7, small8, small9
Case Name Status Exec Time Memory
example0 AC 69 ms 14948 KiB
example1 AC 69 ms 14692 KiB
example2 AC 69 ms 14692 KiB
example3 AC 67 ms 14696 KiB
example4 AC 67 ms 14692 KiB
maxrand0 AC 67 ms 14688 KiB
maxrand1 AC 69 ms 14696 KiB
maxrand2 AC 67 ms 14692 KiB
maxrand3 AC 69 ms 14692 KiB
maxrand4 AC 69 ms 14692 KiB
maxrand5 AC 69 ms 14688 KiB
maxrand6 AC 67 ms 14696 KiB
maxrand7 AC 70 ms 14692 KiB
maxrand8 AC 68 ms 14692 KiB
maxrand9 AC 69 ms 14692 KiB
rand0 AC 71 ms 14696 KiB
rand1 AC 67 ms 14696 KiB
rand2 AC 68 ms 14692 KiB
rand3 AC 68 ms 14688 KiB
rand4 AC 67 ms 14692 KiB
rand5 AC 67 ms 14692 KiB
rand6 AC 67 ms 14692 KiB
rand7 AC 69 ms 14692 KiB
rand8 AC 68 ms 14692 KiB
rand9 AC 67 ms 14688 KiB
small0 AC 67 ms 14688 KiB
small1 AC 67 ms 14696 KiB
small2 AC 67 ms 14696 KiB
small3 AC 69 ms 14692 KiB
small4 AC 67 ms 14692 KiB
small5 AC 68 ms 14688 KiB
small6 AC 67 ms 14692 KiB
small7 AC 67 ms 14696 KiB
small8 AC 68 ms 14688 KiB
small9 AC 68 ms 14688 KiB