Submission #42515416


Source Code Expand

Copy
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List
import qualified Data.IntMap as IM
main = do
[n,k,q] <- bsGetLnInts
xys <- replicateM q bsGetLnInts
let ans = abc306e n k q xys
mapM_ print ans
bsGetLnInts :: IO [Int]
bsGetLnInts = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
abc306e :: Int -> Int -> Int -> [[Int]] -> [Int]
abc306e n k q xys
| n == k = snd $ mapAccumL step0 (a0,0) xys
| True = snd $ mapAccumL step initial xys
where
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List

import qualified Data.IntMap as IM

main = do
  [n,k,q] <- bsGetLnInts
  xys <- replicateM q bsGetLnInts
  let ans = abc306e n k q xys
  mapM_ print ans

bsGetLnInts :: IO [Int]
bsGetLnInts = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine

abc306e :: Int -> Int -> Int -> [[Int]] -> [Int]
abc306e n k q xys
  | n == k = snd $ mapAccumL step0 (a0,0) xys
  | True   = snd $ mapAccumL step initial xys
  where
    a0 = IM.fromAscList [(i,0) | i <- [1..n]]
    initial = (a0, IM.singleton 0 (n - k), IM.singleton 0 k, 0)

type State0 = (IM.IntMap Int, Int)

step0 :: State0 -> [Int] -> (State0, Int) -- Aとその合計を追跡する
step0 (a, s) (x:y:_) = ((IM.insert x y a, s1), s1)
  where
    s1 = s - a IM.! x + y

type State = (IM.IntMap Int, IM.IntMap Int, IM.IntMap Int, Int)

step :: State -> [Int] -> (State, Int) -- A, L, U, 上K個の合計を追跡する
step (am, lm, um, s0) (x:y:_) = ((am1,lm2,um2,s2), s2)
  where
    am1 = IM.insert x y am
-- ステップ1 : A[Xi]=Zを除く
    z = am IM.! x -- 指名された、消える値
    (lmax,_) = IM.findMax lm -- 下N-K個の最大値
    (lm1, um1, s1)
      | z <= lmax = (decr z lm, um, s0) -- ZがL側にあるとき、それを抜くだけ
      | otherwise = (decr lmax lm, incr lmax $ decr z um, s0 - z + lmax) -- zがU側にあるとき、それを抜いた分、Lの最大値をUへ移動させる
-- ステップ2 : Yを追加する
    (umin,_) = IM.findMin um1 -- 上N個の最小値
    (lm2, um2, s2)
      | y <= umin = (incr y lm1, um1, s1) -- YがL側に入るとき、入れるだけ
      | otherwise = (incr umin lm1, incr y $ decr umin um1, s1 - umin + y) -- YがU側に入るとき、押し出されるUminについて考慮する

-- 値を一つ追加
incr x im = IM.insertWith (+) x 1 im
-- 値を一つ抜く
decr x im = IM.update f x im
  where
    f 1 = Nothing
    f k = Just $ pred k

Submission Info

Submission Time
Task E - Best Performances
User joetheshootingst
Language Haskell (GHC 8.8.3)
Score 475
Code Size 2045 Byte
Status AC
Exec Time 5065 ms
Memory 295388 KB

Compile Error

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

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 1
AC × 29
Set Name Test Cases
Sample sample_01.txt
All sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt
Case Name Status Exec Time Memory
sample_01.txt AC 10 ms 3780 KB
test_01.txt AC 6 ms 3572 KB
test_02.txt AC 1269 ms 144724 KB
test_03.txt AC 1396 ms 153020 KB
test_04.txt AC 977 ms 137676 KB
test_05.txt AC 1119 ms 143844 KB
test_06.txt AC 1082 ms 144904 KB
test_07.txt AC 1175 ms 147072 KB
test_08.txt AC 1197 ms 151808 KB
test_09.txt AC 932 ms 151212 KB
test_10.txt AC 5065 ms 268868 KB
test_11.txt AC 4626 ms 258064 KB
test_12.txt AC 2120 ms 198432 KB
test_13.txt AC 2792 ms 211784 KB
test_14.txt AC 3773 ms 211700 KB
test_15.txt AC 4804 ms 245020 KB
test_16.txt AC 4631 ms 280548 KB
test_17.txt AC 2429 ms 207572 KB
test_18.txt AC 1392 ms 231132 KB
test_19.txt AC 1299 ms 150108 KB
test_20.txt AC 1363 ms 153784 KB
test_21.txt AC 2426 ms 197276 KB
test_22.txt AC 2509 ms 197188 KB
test_23.txt AC 3833 ms 291680 KB
test_24.txt AC 4035 ms 292060 KB
test_25.txt AC 4304 ms 293408 KB
test_26.txt AC 4439 ms 294340 KB
test_27.txt AC 4401 ms 295388 KB
test_28.txt AC 2661 ms 262104 KB


2025-04-24 (Thu)
11:48:16 +00:00