{-# LANGUAGE BangPatterns #-} {-# LANGUAGE FlexibleInstances #-} module Main where import Control.Monad import Control.Monad.Cont import Control.Monad.Fix import Control.Monad.ST import Control.Monad.State import Data.Bits import qualified Data.ByteString.Char8 as BSC8 import Data.Char import Data.Coerce import Data.IORef import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import Data.Word import qualified Data.Vector.Generic as VG import qualified Data.Vector.Generic.Mutable as VGM import qualified Data.Vector.Unboxed as VU import Unsafe.Coerce main :: IO () main = do n <- readLn :: IO Int a <- introSort <$> parseN1 n check <- newIORef True rep (n - 1) $ \i -> when (a VU.! i == a VU.! (i + 1) - 1) $ writeIORef check False b <- readIORef check if b then putStrLn "1" else putStrLn "2" 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 #-} parseN1 :: Int -> IO (VU.Vector Int) parseN1 n = VU.unfoldrN n (runCParser int) <$> BSC8.getContents {-# INLINE parseN1 #-} introSort :: (Ord a, VG.Vector v a) => v a -> v a introSort = introSortBy compare introSortBy :: VG.Vector v a => (a -> a -> Ordering) -> v a -> v a introSortBy cmp = VG.modify $ inplaceIntroSortBy cmp inplaceIntroSortBy :: VGM.MVector mv a => (a -> a -> Ordering) -> mv s a -> ST s () inplaceIntroSortBy cmp vec = do let depthLimit = 2 * floorLog2 (VGM.length vec) threshold = 16 fix `flip` depthLimit `flip` vec $ \loop !depth mv -> when (VGM.length mv > threshold) $ if depth > 0 then do pivot <- getMedian3Pivot cmp mv cut <- pivotPartition cmp mv pivot loop (depth - 1) (VGM.unsafeDrop cut mv) loop (depth - 1) (VGM.unsafeTake cut mv) else inplaceHeapSortBy cmp mv inplaceInsertionSortBy cmp vec where floorLog2 :: Int -> Int floorLog2 x = fromIntegral $ y `unsafeShiftR` 52 - 1023 where y :: Word64 y = unsafeCoerce (fromIntegral x :: Double) pivotPartition :: (VGM.MVector mv a) => (a -> a -> Ordering) -> mv s a -> a -> ST s Int pivotPartition cmp vec pivot = fix `flip` 0 `flip` VGM.length vec $ \loop !l !r -> do !l' <- flip fix l $ \loopL !i -> do x <- VGM.unsafeRead vec i case cmp x pivot of LT -> loopL (i + 1) _ -> return i !r' <- flip fix (r - 1) $ \loopR !i -> do x <- VGM.unsafeRead vec i case cmp pivot x of LT -> loopR (i - 1) _ -> return i if l' < r' then do VGM.unsafeSwap vec l' r' loop (l' + 1) r' else return l' {-# INLINE pivotPartition #-} getMedian3Pivot :: VGM.MVector mv a => (a -> a -> Ordering) -> mv s a -> ST s a getMedian3Pivot cmp vec = median cmp <$> VGM.unsafeRead vec 0 <*> VGM.unsafeRead vec (VGM.length vec `quot` 2) <*> VGM.unsafeRead vec (VGM.length vec - 1) {-# INLINE getMedian3Pivot #-} median :: (a -> a -> Ordering) -> a -> a -> a -> a median cmp x y z = case cmp x y of LT -> case cmp y z of LT -> y _ -> case cmp x z of LT -> z _ -> x _ -> case cmp x z of LT -> x _ -> case cmp y z of LT -> z _ -> y {-# INLINE median #-} inplaceInsertionSortBy :: VGM.MVector mv a => (a -> a -> Ordering) -> mv s a -> ST s () inplaceInsertionSortBy cmp vec = for 1 (VGM.length vec - 1) 1 $ \i -> do x <- VGM.unsafeRead vec i hd <- VGM.unsafeRead vec 0 case cmp hd x of LT -> flip fix i $ \loop !j -> do y <- VGM.unsafeRead vec (j - 1) case cmp x y of LT -> do VGM.unsafeWrite vec j y loop (j - 1) _ -> VGM.unsafeWrite vec j x _ -> flip fix i $ \loop !j -> if j > 0 then do VGM.unsafeRead vec (j - 1) >>= VGM.unsafeWrite vec j loop (j - 1) else VGM.unsafeWrite vec 0 x {-# INLINE inplaceInsertionSortBy #-} siftDown :: VGM.MVector mv a => (a -> a -> Ordering) -> Int -> mv s a -> ST s () siftDown cmp offset vec = do let !len = VGM.length vec flip fix offset $ \loop !parent -> do let !l = 2 * parent + 1 !r = l + 1 x <- VGM.unsafeRead vec parent when (l < len) $ do childL <- VGM.unsafeRead vec l if r < len then do childR <- VGM.unsafeRead vec r case cmp childL childR of LT -> when (cmp x childR == LT) $ do VGM.unsafeSwap vec parent r loop r _ -> when (cmp x childL == LT) $ do VGM.unsafeSwap vec parent l loop l else when (cmp x childL == LT) $ do VGM.unsafeSwap vec parent l loop l {-# INLINE siftDown #-} heapify :: VGM.MVector mv a => (a -> a -> Ordering) -> mv s a -> ST s () heapify cmp vec = rev (VGM.length vec `quot` 2) $ \i -> siftDown cmp i vec {-# INLINE heapify #-} inplaceHeapSortBy :: VGM.MVector mv a => (a -> a -> Ordering) -> mv s a -> ST s () inplaceHeapSortBy cmp vec = do heapify cmp vec flip fix (VGM.length vec - 1) $ \loop !i -> when (i > 0) $ do VGM.unsafeSwap vec 0 i siftDown cmp 0 $ VGM.unsafeTake i vec loop (i - 1) {-# INLINE inplaceHeapSortBy #-} -- | l -> x -> r, +d stream :: Monad m => Int -> Int -> Int -> VFSM.Stream m Int stream !l !r !d = VFSM.Stream step l where step x | x <= r = return $ VFSM.Yield x (x + d) | otherwise = return VFSM.Done {-# INLINE [0] step #-} {-# INLINE [1] stream #-} -- | 0 <= x < n, interval = 1 rep :: Monad m => Int -> (Int -> m ()) -> m () rep n = flip VFSM.mapM_ (stream 0 (n - 1) 1) {-# INLINE rep #-} -- | 0 <= x <= n, interval = 1 rep' :: Monad m => Int -> (Int -> m ()) -> m () rep' n = flip VFSM.mapM_ (stream 0 n 1) {-# INLINE rep' #-} -- | 1 <= x < n, interval = 1 rep1 :: Monad m => Int -> (Int -> m ()) -> m () rep1 n = flip VFSM.mapM_ (stream 1 (n - 1) 1) {-# INLINE rep1 #-} -- | 1 <= x <= n, interval = 1 rep1' :: Monad m => Int -> (Int -> m ()) -> m () rep1' n = flip VFSM.mapM_ (stream 1 n 1) {-# INLINE rep1' #-} -- | l <= x <= r, interval = d for :: Monad m => Int -> Int -> Int -> (Int -> m ()) -> m () for l r d = flip VFSM.mapM_ (stream l r d) {-# INLINE for #-} -- | r -> x -> l, -d streamR :: Monad m => Int -> Int -> Int -> VFSM.Stream m Int streamR !r !l !d = VFSM.Stream step r where step x | x >= l = return $ VFSM.Yield x (x - d) | otherwise = return VFSM.Done {-# INLINE [0] step #-} {-# INLINE [1] streamR #-} -- | n > x >= 0, interval = -1 rev :: Monad m => Int -> (Int -> m ()) -> m () rev n = flip VFSM.mapM_ (streamR (n - 1) 0 1) {-# INLINE rev #-} -- | n >= x >= 0, interval = -1 rev' :: Monad m => Int -> (Int -> m ()) -> m () rev' n = flip VFSM.mapM_ (streamR n 0 1) {-# INLINE rev' #-} -- | n > x >= 1, interval = -1 rev1 :: Monad m => Int -> (Int -> m ()) -> m () rev1 n = flip VFSM.mapM_ (streamR (n - 1) 1 1) {-# INLINE rev1 #-} -- | n >= x >= 1, interval = -1 rev1' :: Monad m => Int -> (Int -> m ()) -> m () rev1' n = flip VFSM.mapM_ (streamR n 1 1) {-# INLINE rev1' #-} -- | r >= x >= l, interval = -d forR :: Monad m => Int -> Int -> Int -> (Int -> m ()) -> m () forR r l d = flip VFSM.mapM_ (streamR r l d) {-# INLINE forR #-} -- | for (int i = l; f(i, p) <= r ; g(i, d)) streamG :: Monad m => Int -> Int -> (Int -> Int -> Int) -> Int -> (Int -> Int -> Int) -> Int -> VFSM.Stream m Int streamG l r f p g d = VFSM.Stream step l where step x | f x p <= r = return $ VFSM.Yield x (g x d) | otherwise = return VFSM.Done {-# INLINE [0] step #-} {-# INLINE [1] streamG #-} forG :: Monad m => Int -> Int -> (Int -> Int -> Int) -> Int -> (Int -> Int -> Int) -> Int -> (Int -> m ()) -> m () forG l r f p g d = flip VFSM.mapM_ (streamG l r f p g d) {-# INLINE forG #-} withBreakIO :: ((r -> ContT r IO b) -> ContT r IO r) -> IO r withBreakIO = flip runContT pure . callCC {-# INLINE withBreakIO #-} withBreakST :: ((r -> ContT r (ST s) b) -> ContT r (ST s) r) -> (ST s) r withBreakST = flip runContT pure . callCC {-# INLINE withBreakST #-}