Submission #3752556


Source Code Expand

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Maybe
import Data.List
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Control.Monad
import Control.Monad.ST
-- import Debug.Trace
trace _ a = a

solve :: Int -> [(String,Int)] -> Int
solve n cas = revNums VU.! n
  where

{--
k :: [0..2n-1]
i :: [0..n]
bwTbl ! (enc k col i)
  = # { p | k < p, (col,j) is located at p, i >= j }
--}    

  bwTbl :: VU.Vector Int
  bwTbl = trace ("bwTbl = " ++ show bwTbl0) bwTbl0

  bwTbl0 = VU.create $ do
    bwv <- VUM.new ((2*n+1)*2*(n+1))
    forM_ [0..n] $ \i -> do
      VUM.write bwv (enc 0 0 i) i
      VUM.write bwv (enc 0 1 i) i
    forM_ (zip [1..] cas) $ \(k, (c, a)) -> do
      forM_ [0..n] $ \i -> do
        forM_ [0,1] $ \col -> do
          x <- VUM.read bwv (enc (k-1) col i)
          let y | col == color c && i >= a = x-1
                | otherwise = x
          VUM.write bwv (enc k col i) y
    return bwv
  enc  k col i = k * (2*(n+1)) + col * (n+1) + i
  bwT k col i = trace ("bwT " ++ show (k,col,i,ans)) ans
    where ans = bwT0 k col i
  bwT0 k col i = bwTbl VU.! (enc (k+1) col i)

  posTbl :: VU.Vector Int
  posTbl = VU.create $ do
    vec <- VUM.new (2*(n+1))
    forM_ (zip [0..] cas) $ \(k, (c,a)) -> do
      VUM.write vec (encP (color c) a) k
    return vec
  encP col a = col * (n+1) + a
  posT col a = trace ("posT " ++ show (col,a,ans)) ans
    where ans = posT0 col a
  posT0 col a = posTbl VU.! (encP col a)
    
  revNums = foldl op (VU.replicate (n+1) 0) [1..2*n]
    where
    op prev m = trace ("op " ++ show (prev, m, a)) a
      where a = op0 prev m

    op0 prev m = VU.create $ do
      vec <- VUM.replicate (n+1) 0
      forM_ indices $ \i -> do
        VUM.write vec i (min (putBlack i) (putWhite i))
      when (m <= n) $ do
        VUM.write vec 0 (putWhite 0)
        VUM.write vec m (putBlack m)
      return vec
      where
        indices | m <= n = [1..m-1]
                | otherwise = [m-n..n]
        putBlack i = (prev VU.! (i-1)) + (right (posT 0 i) (i-1) (m-i))
        putWhite i = (prev VU.! i) + (right (posT 1 (m-i)) i (m-i-1))
        right k bn wn = bwT k 0 bn + bwT k 1 wn

color s | head s == 'B' = 0
        | otherwise     = 1

readBInt :: B.ByteString -> Int
readBInt = fst . fromJust . B.readInt

tmain :: B.ByteString -> Int
tmain cont =
  let remLines0 = map B.words (B.lines cont)
      [bs_n]:remLines1 = remLines0
      n = readBInt bs_n
      cas = map (\[x1,x2] -> (B.unpack x1,readBInt x2)) remLines1
  in solve n cas

outAnswer :: Int -> IO ()
outAnswer = putStrLn . show

main :: IO ()
main = outAnswer . tmain =<< B.getContents

Submission Info

Submission Time
Task E - Sorted and Sorted
User yamate11
Language Haskell (GHC 7.10.3)
Score 600
Code Size 2860 Byte
Status AC
Exec Time 784 ms
Memory 197756 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 36
Set Name Test Cases
Sample 0_000.txt, 0_001.txt, 0_002.txt
All 0_000.txt, 0_001.txt, 0_002.txt, 1_003.txt, 1_004.txt, 1_005.txt, 1_006.txt, 1_007.txt, 1_008.txt, 1_009.txt, 1_010.txt, 1_011.txt, 1_012.txt, 1_013.txt, 1_014.txt, 1_015.txt, 1_016.txt, 1_017.txt, 1_018.txt, 1_019.txt, 1_020.txt, 1_021.txt, 1_022.txt, 1_023.txt, 1_024.txt, 1_025.txt, 1_026.txt, 1_027.txt, 1_028.txt, 1_029.txt, 1_030.txt, 1_031.txt, 1_032.txt, 1_033.txt, 1_034.txt, 1_035.txt
Case Name Status Exec Time Memory
0_000.txt AC 1 ms 508 KB
0_001.txt AC 1 ms 508 KB
0_002.txt AC 1 ms 508 KB
1_003.txt AC 1 ms 508 KB
1_004.txt AC 1 ms 508 KB
1_005.txt AC 1 ms 508 KB
1_006.txt AC 1 ms 508 KB
1_007.txt AC 1 ms 508 KB
1_008.txt AC 1 ms 508 KB
1_009.txt AC 1 ms 508 KB
1_010.txt AC 1 ms 508 KB
1_011.txt AC 1 ms 508 KB
1_012.txt AC 1 ms 508 KB
1_013.txt AC 1 ms 508 KB
1_014.txt AC 608 ms 161404 KB
1_015.txt AC 81 ms 27900 KB
1_016.txt AC 447 ms 117244 KB
1_017.txt AC 180 ms 49788 KB
1_018.txt AC 4 ms 1788 KB
1_019.txt AC 59 ms 21372 KB
1_020.txt AC 511 ms 144252 KB
1_021.txt AC 197 ms 62204 KB
1_022.txt AC 94 ms 32508 KB
1_023.txt AC 431 ms 115964 KB
1_024.txt AC 693 ms 191356 KB
1_025.txt AC 780 ms 197628 KB
1_026.txt AC 778 ms 197628 KB
1_027.txt AC 778 ms 196860 KB
1_028.txt AC 780 ms 196860 KB
1_029.txt AC 783 ms 197628 KB
1_030.txt AC 784 ms 196860 KB
1_031.txt AC 782 ms 197628 KB
1_032.txt AC 696 ms 196860 KB
1_033.txt AC 695 ms 197756 KB
1_034.txt AC 701 ms 197628 KB
1_035.txt AC 695 ms 196860 KB