結果

問題 No.482 あなたの名は
ユーザー alpha_virginisalpha_virginis
提出日時 2017-02-14 01:36:02
言語 Haskell
(9.8.2)
結果
AC  
実行時間 1,282 ms / 2,000 ms
コード長 2,140 bytes
コンパイル時間 6,494 ms
コンパイル使用メモリ 161,604 KB
実行使用メモリ 223,428 KB
最終ジャッジ日時 2023-08-28 16:27:06
合計ジャッジ時間 21,191 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,100 KB
testcase_01 AC 2 ms
7,100 KB
testcase_02 AC 2 ms
7,124 KB
testcase_03 AC 3 ms
7,076 KB
testcase_04 AC 2 ms
7,056 KB
testcase_05 AC 3 ms
6,956 KB
testcase_06 AC 2 ms
7,048 KB
testcase_07 AC 3 ms
8,036 KB
testcase_08 AC 3 ms
7,700 KB
testcase_09 AC 3 ms
7,188 KB
testcase_10 AC 3 ms
7,732 KB
testcase_11 AC 3 ms
7,880 KB
testcase_12 AC 3 ms
8,012 KB
testcase_13 AC 3 ms
7,616 KB
testcase_14 AC 3 ms
7,544 KB
testcase_15 AC 1,282 ms
223,328 KB
testcase_16 AC 1,262 ms
223,192 KB
testcase_17 AC 1,252 ms
223,296 KB
testcase_18 AC 1,262 ms
223,428 KB
testcase_19 AC 1,252 ms
223,296 KB
testcase_20 AC 1,262 ms
223,232 KB
testcase_21 AC 1,272 ms
223,344 KB
testcase_22 AC 1,282 ms
223,232 KB
testcase_23 AC 1,252 ms
223,220 KB
testcase_24 AC 1,263 ms
223,272 KB
testcase_25 AC 1,263 ms
223,216 KB
testcase_26 AC 1,252 ms
223,316 KB
testcase_27 AC 1,251 ms
223,224 KB
testcase_28 AC 1,262 ms
223,220 KB
testcase_29 AC 1,043 ms
218,096 KB
testcase_30 AC 3 ms
7,052 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

{-# 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

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)

insert' :: Int -> a -> Tree a -> Tree a
insert' 0 y (Node l _ r) = Node l y r
insert' i y (Node l x r)
  | mod i 2 == 1 = Node l' x r
  | otherwise = Node l x r'
    where i' = div i 2
          l' = (insert' i' y l)
          r' = (insert' (i' - 1) y 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 --

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 = C.pack $ show diff
solve n k xs = if diff <= k && mod diff 2 == mod k 2 then yes else no
  where
    xs' = listArray (1,n) xs
    visited = initTree False
    (_, count) = f n xs' visited 0
    diff = count

f :: Int -> Array Int Int -> Tree Bool -> Int -> (Tree Bool, Int)
f 0 xs visited count = (visited, count)
f i xs visited count = if visited `at` i == 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 -> Tree Bool -> Int -> (Tree Bool, 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' = insert' i True visited
0