Submission #19501713


Source Code Expand

{-# LANGUAGE DatatypeContexts #-}
import           Control.Exception           (assert)
import           Control.Monad
import           Control.Monad.Primitive
import qualified Control.Monad.ST            as ST
import qualified Data.Array.IO               as IO
import qualified Data.Array.ST               as ST
import qualified Data.Array.Unboxed          as A
import           Data.Bits
import qualified Data.ByteString.Char8       as BS
import           Data.Char
import           Data.Foldable
import           Data.List
import qualified Data.Map.Strict             as Map
import           Data.Maybe
import qualified Data.Sequence               as Seq
import qualified Data.Set                    as Set
import qualified Data.Vector                 as VB
import qualified Data.Vector.Mutable         as VBM
import qualified Data.Vector.Unboxed         as V
import           Data.Vector.Unboxed.Base
import qualified Data.Vector.Unboxed.Mutable as VM
import           Debug.Trace

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine
getIntVec n = V.unfoldrN n (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

readInteger = fst . fromJust . BS.readInteger
readIntegerList = map readInteger . BS.words
getInteger = readInteger <$> BS.getLine
getIntegerList = readIntegerList <$> BS.getLine

inf :: Int
inf = 10^18

main = do
  [h, w, k] <- getIntList

  ss <- VB.replicateM h $ BS.getLine
  let getC y x | y >= 0 && x >= 0 = digitToInt $ (ss VB.! y) `BS.index` x
               | otherwise = 0

  let popc bm res | bm == 0 = res
                  | bm .&. 1 == 1 = popc (bm `shiftR` 1) (res+1)
                  | otherwise = popc (bm `shiftR` 1) res

      getRow i bm = getRow' i bm 0 0 []
      getRow' i bm j c res | j >= h = (c:res)
                           | otherwise = let bj = (bm `shiftR` j) .&. 1
                                             cji = getC j i
                                             c' = c + cji
                                         in if bj == 1
                                            then getRow' i bm (j+1) 0 (c':res)
                                            else getRow' i bm (j+1) c' res

      addRow []     []     res = Just (reverse res)
      addRow (x:xs) (y:ys) res | x + y <= k = addRow xs ys ((x+y):res)
                               | otherwise = Nothing

      solve' bm crow i res ubound
        | i >= w = res
        | otherwise = let nrow = getRow i bm
                          over = any (> k) nrow
                          crow' = addRow crow nrow []
                      in case () of
                           () | over -> inf
                              | isJust crow' -> solve' bm (fromJust crow') (i+1) res ubound
                              | res <= ubound -> solve' bm nrow (i+1) (res+1) ubound
                              | otherwise -> inf

      solve :: Int -> Int -> Int
      solve bm res
        | bm < 0 = res
        | otherwise = let r = solve' bm (getRow (-1) bm) 0 (popc bm 0) res
                          res' = min r res
                      in solve (bm-1) res'

  print $ solve (1 `shiftL` (h-1)) inf

Submission Info

Submission Time
Task E - Dividing Chocolate
User unnohideyuki
Language Haskell (GHC 8.8.3)
Score 500
Code Size 3284 Byte
Status AC
Exec Time 113 ms
Memory 5188 KiB

Compile Error

Loaded package environment from /home/contestant/.ghc/x86_64-linux-8.8.3/environments/default

Main.hs:1:14: warning:
    -XDatatypeContexts is deprecated: It was widely considered a misfeature, and has been removed from the Haskell language.
  |
1 | {-# LANGUAGE DatatypeContexts #-}
  |              ^^^^^^^^^^^^^^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 19
Set Name Test Cases
Sample sample_01, sample_02, sample_03
All max_01, max_02, max_03, max_04, max_05, max_06, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09, random_10, sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
max_01 AC 113 ms 5188 KiB
max_02 AC 4 ms 5060 KiB
max_03 AC 106 ms 5112 KiB
max_04 AC 104 ms 5104 KiB
max_05 AC 94 ms 5072 KiB
max_06 AC 4 ms 4948 KiB
random_01 AC 2 ms 4320 KiB
random_02 AC 9 ms 5068 KiB
random_03 AC 33 ms 4996 KiB
random_04 AC 5 ms 5060 KiB
random_05 AC 29 ms 5096 KiB
random_06 AC 4 ms 5112 KiB
random_07 AC 3 ms 4944 KiB
random_08 AC 16 ms 5068 KiB
random_09 AC 36 ms 4980 KiB
random_10 AC 8 ms 5044 KiB
sample_01 AC 2 ms 4104 KiB
sample_02 AC 2 ms 3852 KiB
sample_03 AC 2 ms 4064 KiB