Submission #42564516


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
import Data.Array.IO
-- mutable array
main = do
[n,k,q] <- bsGetLnInts
if n == k then main1 n q else main2 n k q
-- A
main1 n q = do
a <- newArray (1,n) 0 :: IO (IOArray Int Int)
foldM_ (\s _ -> do
x:y:_ <- bsGetLnInts
z <- readArray a x
writeArray a x y
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List

import qualified Data.IntMap as IM
import Data.Array.IO

-- mutable array版

main = do
  [n,k,q] <- bsGetLnInts
  if n == k then main1 n q else main2 n k q

-- Aとその合計を追跡する
main1 n q = do
  a <- newArray (1,n) 0 :: IO (IOArray Int Int)
  foldM_ (\s _ -> do
    x:y:_ <- bsGetLnInts
    z <- readArray a x
    writeArray a x y
    let s1 = s - z + y
    print s1
    return s1
    ) 0 [1..q]

main2 n k q = do
  a <- newArray (1,n) 0 :: IO (IOArray Int Int)
  foldM_ (\(lm, um, s0) _ -> do
    x:y:_ <- bsGetLnInts
-- ステップ1 : A[Xi]=Zを除く
    z <- readArray a x -- 指名された、消える値
    writeArray a x y
    let (lmax,_) = IM.findMax lm -- 下N-K個の最大値
    let (lm1, um1, s1) = if z <= lmax then (decr z lm, um, s0) else (decr lmax lm, incr lmax $ decr z um, s0 - z + lmax)
-- ステップ2 : Yを追加する
    let (umin,_) = IM.findMin um1 -- 上N個の最小値
    let (lm2, um2, s2) = if y <= umin then (incr y lm1, um1, s1) else (incr umin lm1, incr y $ decr umin um1, s1 - umin + y)
    print s2
    return (lm2, um2, s2)
    ) (IM.singleton 0 (n - k), IM.singleton 0 k, 0) [1..q]

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

-- 値を一つ追加
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 1555 Byte
Status AC
Exec Time 2384 ms
Memory 113508 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 6 ms 3700 KB
test_01.txt AC 2 ms 3636 KB
test_02.txt AC 432 ms 7176 KB
test_03.txt AC 488 ms 7780 KB
test_04.txt AC 239 ms 4620 KB
test_05.txt AC 354 ms 4824 KB
test_06.txt AC 409 ms 5276 KB
test_07.txt AC 431 ms 7216 KB
test_08.txt AC 418 ms 6788 KB
test_09.txt AC 299 ms 7748 KB
test_10.txt AC 2384 ms 78872 KB
test_11.txt AC 2332 ms 72224 KB
test_12.txt AC 415 ms 5716 KB
test_13.txt AC 1050 ms 16628 KB
test_14.txt AC 1527 ms 17752 KB
test_15.txt AC 2327 ms 64380 KB
test_16.txt AC 2138 ms 80780 KB
test_17.txt AC 769 ms 17316 KB
test_18.txt AC 369 ms 21956 KB
test_19.txt AC 436 ms 7728 KB
test_20.txt AC 490 ms 7844 KB
test_21.txt AC 823 ms 10444 KB
test_22.txt AC 885 ms 10524 KB
test_23.txt AC 1619 ms 82096 KB
test_24.txt AC 1785 ms 81572 KB
test_25.txt AC 1893 ms 112912 KB
test_26.txt AC 1907 ms 113508 KB
test_27.txt AC 1940 ms 108392 KB
test_28.txt AC 887 ms 19992 KB


2025-04-19 (Sat)
05:22:30 +00:00