/
main.hs
33 lines (23 loc) · 838 Bytes
/
main.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
-- hello
module Main where
import Data.List as L
stringToInt::String->Int
stringToInt str = read str
getkdistpairs::[Int]->[Int]->Int->[(Int,Int)]
getkdistpairs _ [] _ = []
getkdistpairs (x:xs) (y:ys) k
| (x+k)==y = (x,y):getkdistpairs xs ys k
| (x+k)<y = getkdistpairs xs (y:ys) k
| (x+k)>y = getkdistpairs (x:xs) ys k
countkapart::[Int]->Int->Int
countkapart (v:vs) k = let aux1 = dropWhile (\x -> k > (x-v)) vs
aux = getkdistpairs (v:vs) aux1 k
in length aux
solve::Int->Int->[Int]->Int
solve n k vals = countkapart (L.sort vals) k
main =
do nkstr<-getLine
valsstr<-getLine
let [n,k] = map stringToInt $ words nkstr
vals = map stringToInt $ words valsstr
print $ solve n k vals