結果
| 問題 | No.482 あなたの名は |
| コンテスト | |
| ユーザー |
alpha_virginis
|
| 提出日時 | 2017-02-18 00:18:28 |
| 言語 | Haskell (9.10.1) |
| 結果 |
AC
|
| 実行時間 | 598 ms / 2,000 ms |
| コード長 | 2,519 bytes |
| 記録 | |
| コンパイル時間 | 6,097 ms |
| コンパイル使用メモリ | 181,056 KB |
| 実行使用メモリ | 116,096 KB |
| 最終ジャッジ日時 | 2024-12-30 03:06:07 |
| 合計ジャッジ時間 | 15,588 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 28 |
コンパイルメッセージ
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
ソースコード
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Maybe
import qualified Data.ByteString.Char8 as C
import Control.Monad(forM_)
import Data.Int
import Data.List
import Data.Array
import Data.Word
import Data.Bits
yes :: C.ByteString
yes = "YES"
no :: C.ByteString
no = "NO"
getInts :: IO [Int]
getInts = do
ss <- C.getLine
return $ map (fst . fromJust . C.readInt) $ C.words ss
getIntss :: Int -> IO [[Int]]
getIntss 0 = return []
getIntss n = do
xs <- getInts
xss <- getIntss (n - 1)
return $ xs : xss
-- end standard template --
-- begin tree template --
data Tree a = Node (Tree a) a (Tree a)
initTree :: a -> Tree a
initTree x = Node (initTree x) x (initTree x)
update' :: Int -> (a -> a) -> Tree a -> Tree a
update' 0 f (Node l x r) = Node l (f x) r
update' i f (Node l x r)
| mod i 2 == 1 = Node l' x r
| otherwise = Node l x r'
where i' = div i 2
l' = (update' i' f l)
r' = (update' (i' - 1) f r)
at :: Tree a -> Int -> a
at (Node _ x _) 0 = x
at (Node l _ r) i
| mod i 2 == 1 = at l i'
| otherwise = at r (i' - 1)
where i' = div i 2
-- end tree template --
-- begin boolean flags template --
type Flags = Tree Word64
initFlags :: Flags
initFlags = initTree 0
setFlag :: Int -> Flags -> Flags
setFlag i flags = update' (i `shiftR` 6) f flags
where f = \x -> x .|. (1 `shiftL` (i .&. 0x3f))
getFlag :: Int -> Flags -> Bool
getFlag i flags = ((flags `at` (i `shiftR` 6)) .&. (1 `shiftL` (i .&. 0x3f))) /= 0
-- end boolean flags template --
main :: IO ()
main = do
[n,k] <- getInts
xs <- getInts
let ss = solve n k xs
C.putStrLn ss
solve :: Int -> Int -> [Int] -> C.ByteString
solve n k xs = if diff <= k && mod diff 2 == mod k 2 then yes else no
where
xs' = listArray (1,n) xs
visited = initFlags
(_, count) = f n xs' visited 0
diff = count
f :: Int -> Array Int Int -> Flags -> Int -> (Flags, Int)
f 0 xs visited count = (visited, count)
f i xs visited count = if getFlag (toEnum i) visited == True
then f (i - 1) xs visited count
else f (i - 1) xs visited' (count + count')
where
(visited', count') = g i i xs visited 0
g :: Int -> Int -> Array Int Int -> Flags -> Int -> (Flags, Int)
g s i xs visited count = if (xs ! i) == s
then (visited', count)
else g s (xs ! i) xs visited' (count + 1)
where visited' = setFlag i visited
alpha_virginis