-
Notifications
You must be signed in to change notification settings - Fork 137
/
Solution.hs
40 lines (32 loc) · 1.25 KB
/
Solution.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
36
37
38
39
40
import qualified Data.Map as Map
import qualified Data.List as List
data PNumber = PNumber {
number :: Int,
position :: Int,
counter :: Int
} deriving (Eq, Ord, Show)
toPNumbers xs = createPNumbers xs 0
createPNumbers [] _ = []
createPNumbers (x:xs) position = (PNumber x position 1) : (createPNumbers xs (position + 1))
countPNumbers xs = foldl (\acc x -> insertPNumber acc x) Map.empty xs
insertPNumber acc x = case (Map.lookup (number x) acc) of
Just pNumber -> Map.insert (number pNumber) (PNumber (number pNumber) (position pNumber) ((counter pNumber) + 1)) acc
Nothing -> Map.insert (number x) x acc
filterPNumbers k xs = List.filter (\x -> (counter x) >= k) ls
where ls = Map.elems xs
sortPNumbers xs = List.sortBy (\x y -> if (position x) < (position y) then LT else GT) xs
printPNumbers [] = do putStrLn "-1"
printPNumbers xs = do
mapM_ (\x ->
putStr $ show (number x) ++ " "
) xs
putStrLn ""
main = do
q <- fmap (read :: String -> Int) getLine
mapM_ (\_ -> do
nk <- fmap (map (read :: String -> Int) . words) getLine
let n = nk !! 0
let k = nk !! 1
numbers <- fmap (map (read :: String -> Int) . words) getLine
printPNumbers $ sortPNumbers $ filterPNumbers k $ countPNumbers $ toPNumbers numbers
) [1..q]