Skip to content

Instantly share code, notes, and snippets.

@bryangarza
Created November 10, 2015 05:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryangarza/b73fe04c3de8d02f5f86 to your computer and use it in GitHub Desktop.
Save bryangarza/b73fe04c3de8d02f5f86 to your computer and use it in GitHub Desktop.
module FilterElems where
import Data.List
import Control.Monad
-- abstract tuple comparison
sortTup f =
case ord of
EQ -> snd f
_ -> ord
where ord = fst f
sortFst (a1,a2) (b1,b2) = sortTup ((compare a1 b1), (compare a2 b2))
sortSnd (a1,a2) (b1,b2) = sortTup ((compare a2 b2), (compare a1 b1))
eqSnd (_, x) (_, y) = x == y
minI = minimum . map fst
minIndex xs@(x:_) = (minI xs, snd x)
(|>) = flip ($)
infixl 0 |>
findReps k xs =
let xs' = xs |> zip [1..]
|> sortBy sortSnd
|> groupBy eqSnd
|> filter ((>= k) . length)
in case xs' of
[] -> [-1]
_ -> xs' |> map minIndex
|> sortBy sortFst
|> map snd
readIntList :: IO [Int]
readIntList = liftM (map read . words) getLine
main :: IO ()
main = do
t <- readLn
replicateM_ t $ do
[_,k] <- readIntList
as <- readIntList
let ans = findReps k as
putStrLn $ intercalate " " (map show ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment