{-# LANGUAGE BangPatterns #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TupleSections #-} module Main where import Control.Monad import Control.Monad.Fix import Control.Monad.ST import Control.Monad.State import Data.Bits import Data.Bool import Data.Char import Data.Coerce import qualified Data.Foldable as F import Data.Word import Unsafe.Coerce import qualified Data.ByteString.Char8 as BSC8 import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import qualified Data.Vector.Generic as VG import qualified Data.Vector.Generic.Mutable as VGM import qualified Data.Vector.Unboxed as VU import qualified Data.Vector.Unboxed.Mutable as VUM main :: IO () main = do xs <- seqInput 4 putStrLn $ bool "No" "Yes" (solve xs) solve :: VU.Vector Int -> Bool solve xs = runST $ do let ys = stoogeSort xs a = ys VU.! 0 b = ys VU.! 1 c = ys VU.! 2 d = ys VU.! 3 return ((a + 1 == b) && (b + 1 == c) && (c + 1 == d)) type CParser a = StateT BSC8.ByteString Maybe a runCParser :: CParser a -> BSC8.ByteString -> Maybe (a, BSC8.ByteString) runCParser = runStateT {-# INLINE runCParser #-} int :: CParser Int int = coerce $ BSC8.readInt . BSC8.dropWhile isSpace {-# INLINE int #-} seqInput :: Int -> IO (VU.Vector Int) seqInput n = VU.unfoldrN n (runCParser int) <$> BSC8.getLine {-# INLINE seqInput #-} stoogeSort :: (Ord a, VG.Vector v a) => v a -> v a stoogeSort = stoogeSortBy compare stoogeSortBy :: (Ord a, VG.Vector v a) => (a -> a -> Ordering) -> v a -> v a stoogeSortBy cmp = VG.modify $ fix $ \loop vec -> do item1 <- VGM.unsafeRead vec 0 item2 <- VGM.unsafeRead vec (VGM.length vec - 1) when (cmp item1 item2 == LT) $ VGM.unsafeSwap vec 0 (VGM.length vec - 1) when (VGM.length vec > 2) $ do loop (VGM.unsafeTake ((VGM.length vec `div` 3) * 2) vec) loop (VGM.unsafeDrop (VGM.length vec `div` 3) vec) loop (VGM.unsafeTake ((VGM.length vec `div` 3) * 2) vec) {-# INLINE stoogeSortBy #-}