-- #101409よりも高速であるはずのHaskell解 import Control.Applicative ((<$>)) import qualified Data.IntSet as S import qualified Data.ByteString.Lazy.Char8 as C import Data.List (sort) import Data.Maybe (fromJust) -- 与えられた数列の階差数列を求める関数 -- WriterによるPython解と同様の考え方による diff :: Num a => [a] -> [a] diff xs = zipWith (-) (tail xs) (init xs) -- 与えられた数列が二条件を同時に満たすかを判定する関数 isKamo :: [Int] -> Bool isKamo = f . S.fromList . diff . sort where f s = S.size s == 1 && S.notMember 0 s -- 遅延ByteStringからIntを読み取る関数 unsafeReadInt :: C.ByteString -> Int unsafeReadInt = fst . fromJust . C.readInt main :: IO () main = do -- 入力全体を読み取り、単語ごとに分割してそれぞれ整数に変換する -- 最初の要素であるNは使わないので、tailで捨てる -- Stringではなく遅延ByteStringで入力を受け取り、高速化を図る xs <- fmap unsafeReadInt . tail . C.words <$> C.getContents :: IO [Int] putStrLn $ if isKamo xs then "YES" else "NO"