結果

問題 No.482 あなたの名は
ユーザー くれちー
提出日時 2017-02-25 09:33:13
言語 Haskell
(9.10.1)
結果
TLE  
実行時間 -
コード長 1,189 bytes
コンパイル時間 6,163 ms
コンパイル使用メモリ 226,304 KB
実行使用メモリ 170,112 KB
最終ジャッジ日時 2024-06-11 14:53:45
合計ジャッジ時間 10,669 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12 TLE * 1 -- * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Prelude hiding (tail, length, map, splitAt, (++), (!!))
import Data.Vector

(!!) :: Vector a -> Int -> a
v !! x = v ! (pred x)

swap :: Vector a -> Int -> Int -> Vector a
swap xs a b = fst t1 ++ singleton (xs !! j) ++ fst t2 ++ singleton (xs !! i) ++ t3
    where
        i  = min a b
        j  = max a b
        t1 = splitAt (pred i) xs
        t2 = splitAt (pred $ j - i) (tail . snd $ t1)
        t3 = tail . snd $ t2

rep :: Integral a => Vector Int -> Vector Int -> Int -> a -> a
rep ds idxs i cnt
    | i > length ds = cnt
    | ds !! i == i  = rep ds idxs (succ i) cnt
    | otherwise     = rep (swap ds i (idxs !! i)) (swap idxs (ds !! i) (ds !! (idxs !! i))) (succ i) (succ cnt)

solve :: Integral a => Int -> a -> Vector Int -> Bool
solve n k ds
    | cnt <= k && even (k - cnt) = True
    | otherwise                  = False
    where
        Just idxs = fmap (fmap succ) . sequenceA $ (\x -> findIndex (== x)) <$> generate n succ <*> pure ds
        cnt       = rep ds idxs 1 0

main = do
    [n, k] <- fmap read . words <$> getLine
    ds     <- fromList . fmap read . words <$> getLine
    putStrLn $ case solve n k ds of
        True  -> "YES"
        False -> "NO"
0