結果

問題 No.366 ロボットソート
ユーザー ducktailducktail
提出日時 2018-05-29 12:37:03
言語 Haskell
(9.8.2)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 724 bytes
コンパイル時間 11,017 ms
コンパイル使用メモリ 163,528 KB
実行使用メモリ 11,720 KB
最終ジャッジ日時 2023-09-12 20:23:35
合計ジャッジ時間 8,533 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,128 KB
testcase_01 AC 2 ms
7,180 KB
testcase_02 AC 2 ms
7,168 KB
testcase_03 AC 3 ms
6,968 KB
testcase_04 AC 3 ms
7,044 KB
testcase_05 AC 2 ms
7,028 KB
testcase_06 AC 3 ms
7,196 KB
testcase_07 AC 2 ms
7,032 KB
testcase_08 AC 2 ms
7,092 KB
testcase_09 AC 3 ms
7,152 KB
testcase_10 AC 2 ms
7,456 KB
testcase_11 AC 4 ms
9,288 KB
testcase_12 AC 5 ms
9,900 KB
testcase_13 AC 5 ms
10,448 KB
testcase_14 AC 6 ms
11,368 KB
testcase_15 AC 4 ms
9,356 KB
testcase_16 AC 5 ms
10,360 KB
testcase_17 AC 7 ms
11,308 KB
testcase_18 AC 12 ms
11,636 KB
testcase_19 AC 7 ms
11,288 KB
testcase_20 AC 2 ms
7,052 KB
testcase_21 AC 12 ms
11,664 KB
testcase_22 AC 12 ms
11,720 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 #

import Control.Applicative ((<$>), (<*>))
import Data.List (transpose, foldl', sort)
import Data.List.Split (chunksOf)

main :: IO ()
main = solve <$> f <*> f >>= print
  where f = map read <$> words <$> getLine

solve :: [Int] -> [Int] -> Int
solve [_, k] = g . foldr f (0, []) . transpose . chunksOf k
  where f xs (ct, ls) = let (ct', ls') = (swapCount xs, sort xs)
                        in (ct + ct', ls' : ls)
        g (ct, xs) | (isSorted . concat . transpose) xs = ct
                   | otherwise = -1

swapCount = fst . foldl' f (0, [])
  where f (ct, ys) x = let ct' = length . filter (x<) $ ys
                       in (ct + ct', x:ys)

isSorted :: [Int] -> Bool
isSorted xs = and $ zipWith (<) xs (tail xs)
0