-
Notifications
You must be signed in to change notification settings - Fork 1
/
cut-the-sticks.hs
35 lines (29 loc) · 903 Bytes
/
cut-the-sticks.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Control.Applicative
import Control.Monad
import System.IO
main :: IO ()
main = do
n_temp <- getLine
let n = read n_temp :: Int
arr_temp <- getLine
let arr = map read $ words arr_temp :: [Int]
arr1 = quicksort arr
len1 = length arr1
group1 = group' arr1
putStrLn (show len1)
forM_ [1..(length group1-1)] $ \a0 -> do
let group2 = drop a0 group1
len = length $ flatten group2
putStrLn (show len)
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
flatten xss = do xs <- xss
x <- xs
return x
group' :: (Eq a) => [a] -> [[a]]
group' [] = []
group' (x:xs) = (x : takeWhile (== x) xs) : group' (dropWhile (== x) xs)