Created
April 13, 2020 06:18
-
-
Save ayu-mushi/d8484bfb0b38deb18b9b9857a7d5f15f to your computer and use it in GitHub Desktop.
abc162_d の間違った(TLEする)実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Monoid (Sum(Sum)) | |
-- i をインクリメントする処理を分離できないか | |
--https://atcoder.jp/contests/abc162/tasks/abc162_d の間違った(TLEする)実装 | |
syouhi0 :: (Monoid m, Eq a) => Int -> (Int -> Int -> Int -> m) -> [a] -> m | |
syouhi0 i f [x,y] = mempty | |
syouhi0 i f (x:xs) = (syouhi1 (i+1) (f i) 0 x xs) `mappend` (syouhi0 (i+1) f xs) | |
syouhi1 :: (Monoid m, Eq a) => Int -> (Int -> Int -> m) -> Int -> a -> [a] -> m | |
syouhi1 i f diff consumed [x] = mempty | |
syouhi1 i f diff consumed (x:xs) = if x == consumed | |
then syouhi1 (i+1) f (diff+1) consumed xs | |
else syouhi2 (i+1) (f i) diff 0 x consumed xs `mappend` syouhi1 (i+1) f (diff+1) consumed xs | |
syouhi2 :: (Monoid m, Eq a) => Int -> (Int -> m) -> Int -> Int -> a -> a -> [a] -> m | |
syouhi2 i f diff_past diff_current consumed consumed2 [] = mempty | |
syouhi2 i f diff_past diff_current consumed consumed2 (x:xs) = if x == consumed || x==consumed2 | |
then syouhi2 (i+1) f diff_past (diff_current+1) consumed consumed2 xs | |
else if (diff_past /= diff_current) | |
then (f i) `mappend` (syouhi2 (i+1) f diff_past (diff_current+1) consumed consumed2 xs) | |
else syouhi2 (i+1) f diff_past (diff_current+1) consumed consumed2 xs | |
startSyouhi :: (Monoid m, Eq a) => (Int -> Int -> Int -> m) -> [a] -> m | |
startSyouhi = syouhi0 1 | |
main = do | |
n <- read<$>getLine :: IO Int | |
s<-getLine | |
print $ startSyouhi (\a b c -> [(a,b,c)]) s | |
print $ startSyouhi (\_ _ _-> Sum (1::Int)) s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment