読者です 読者をやめる 読者になる 読者になる

SchemeでABC(その2)

前回の続き。C問題にチャレンジ
abc036.contest.atcoder.jp

 (define (accumulate op initial sequence)
  (if (null? sequence)
      initial
      (op (car sequence)
          (accumulate op initial (cdr sequence )))))
(define nil '())
(define (toNil . x) nil)
(define (toUndef . x) (undefined))
(define (map p sequence) (accumulate (lambda (x y) (cons (p x) y)) nil sequence ))
(define (read-lines n)
  (if (= n 1)
      (list (read))
      (append (list (read)) (read-lines (- n 1)))))
(define (reverse l)
  (if (= (length l) 1)
    (list (car l))
    (append (reverse (cdr l)) (list (car l)))))
(define (print-list ls)
  (map (lambda (x) (display x) (newline)) (reverse ls)))
(define (find-list n ls)
  (define (find-acc lis count)
    (if (= n (car lis))
        count
        (find-acc (cdr lis) (+ count 1))))
  (find-acc ls 0))
(define (get-answer a)
  (define b (sort (delete-duplicates a)))
  (accumulate (lambda (x y) (append (list (find-list x b)) y)) nil a))
(define (add-hash-table ht ls)
  (define (add-hash-table-acc ls count)
    (if (null? ls)
        (undefined)
        (toUndef (toNil (hash-table-put! ht (car ls) count)) (add-hash-table-acc (cdr ls) (+ count 1)))))
  (add-hash-table-acc ls 0))
(define (print-hash-table ht ls)
  (if (null? ls)
      (undefined)
      (toUndef (toNil (display (hash-table-get ht (car ls)))) (toNil (newline)) (print-hash-table ht (cdr ls)))))

(define n (read))
(define a (read-lines n))
(define b (sort (delete-duplicates a)))
(define ht (make-hash-table 'eqv?))
(add-hash-table ht b)
(print-hash-table ht a)

これで
Submission #701486 - AtCoder Beginner Contest 036 | AtCoder
こうなった。
N<=10^3までなら通るけど10^5までだとギリギリまだ遅いらしい。
悲しい。誰か助けて。