[topic] リストの並び

数値のリストがあります。リストのサイズは N です。
ls[0]がリストの先頭要素、ls[N-1]が最後の要素です。

問題: 入力として与えられたリストが次の条件を満たすかどうかを求めてください。

for (int i = 0; i < N-1; i++) {
  ls[i] != ls[i+1] である
  ls[i] < ls[i+1] なら、ls[i] < ls[k] である(for all k = i+1 to N-1)
  ls[i] > ls[i+1] なら、ls[i] > ls[k] である(for all k = i+1 to N-1)
}

N の範囲は 3 <= N <= 50000 です。実行例を示します。

> check([1, 5, 4, 2])
True
> check([1, 2, 3])
True
> check([825, 102, 811, 140, 812, 125, 263])
False
> check([824, 102, 811, 140, 810, 155, 263])
True
> check([5, 4, 3, 2, 1])
True
> check([4, 2, 5, 3, 1])
False

Posted feedbacks - Scheme

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
(use srfi-1)

(define (main args)
  (print
   (let loop ((xs (map string->number (cdr args))))
     (or (null? xs)
         (null? (cdr xs))
         (let ((a (car xs))
               (b (cadr xs)))
           (and (not (= a b))
                (every (cute (if (< a b) < >) a <>) (cddr xs))
                (loop (cdr xs)))))))
  0)

Index

Feed

Other

Link

Pathtraq

loading...