結果

問題 No.451 575
ユーザー okaduki
提出日時 2016-12-07 00:36:17
言語 Haskell
(9.10.1)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 874 bytes
コンパイル時間 12,938 ms
コンパイル使用メモリ 180,440 KB
実行使用メモリ 70,656 KB
最終ジャッジ日時 2024-12-17 12:43:20
合計ジャッジ時間 16,773 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 29
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:25:29: warning: [GHC-63394] [-Wx-partial]
    In the use of ‘head’
    (imported from Prelude, but defined in GHC.List):
    "This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
   |
25 |   bs <- forM [1..n] $ \_ -> head <$> getInteger
   |                             ^^^^
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Applicative
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)
import Text.Printf
import Debug.Trace

readInts :: B.ByteString -> [Integer]
readInts = map (fst . fromJust . B.readInteger) . B.words

getInteger :: IO [Integer]
getInteger = liftM readInts B.getLine

solve _ [] _ r = r
solve i (x:xs) acc (l,r) = solve (i+1) xs acc' (l',r')
  where
    acc' = acc + (if (i+1) `mod` 4 < 2 then x else -x)
    (l',r') = if i `mod` 4 < 2
              then (l,min r acc')
              else (max l acc',r)
iter p = scanl (\(prv,f) x -> if f then (x-prv,False) else (prv-x,True)) (p,True)

main = do
  [n] <- getInteger
  bs <- forM [1..n] $ \_ -> head <$> getInteger
  let (l,r) = solve 0 bs 0 (-1,2000000000000000000)
  if l+1 < r && 1 < r
    then print (n+1) >> forM_ (iter (r-1) bs) (print . fst)
    else print (-1)
0