/
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
高橋君は N 人の生徒が受けたテストの点数を管理しています。青木先生から「自分以外の生徒の中に、点数の差の絶対値がちょうど K である相手がいる生徒は何人いるか数えてほしい」と頼まれました。
N 人の生徒にはそれぞれ 1 から N までの番号がついており、生徒 i のテストの点数は A_i 点です(A_i は 0 以上の整数)。なお、同じ点数の生徒が複数いることもあります。
生徒 i が「条件を満たす生徒」であるとは、次を満たす生徒 j(1 \le j \le N, j \neq i)が少なくとも 1 人存在することを言います。
|A_i - A_j| = K
すなわち、生徒 i と生徒 j の点数の差の絶対値がちょうど K であるような、自分とは異なる生徒 j が存在すればよいです。
例えば K = 0 の場合、生徒 i が条件を満たすのは、自分と同じ点数の別の生徒(i とは異なる番号の生徒)が少なくとも 1 人存在するときです。
条件を満たす生徒の人数を求めてください。条件を満たすペアの組数ではなく、条件を満たす生徒が全部で何人いるかを答えることに注意してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq 10^9
- 0 \leq A_i \leq 10^9(1 \leq i \leq N)
- 入力はすべて整数である。
入力
N K A_1 A_2 \dots A_N
- 1 行目には、生徒の人数を表す整数 N と、点数の差の条件を表す非負整数 K が、スペース区切りで与えられる。
- 2 行目には、各生徒のテストの点数を表す整数 A_1, A_2, \dots, A_N が、スペース区切りで与えられる。
出力
条件を満たす生徒の人数を 1 行で出力せよ。
入力例 1
5 2 1 3 5 7 2
出力例 1
4
入力例 2
4 10 1 2 3 4
出力例 2
0
入力例 3
10 0 5 5 3 3 3 7 7 1 2 4
出力例 3
7
入力例 4
15 5 10 15 20 25 30 35 40 3 8 13 18 23 28 33 38
出力例 4
15
入力例 5
1 0 1000000000
出力例 5
0
Score : 300 pts
Problem Statement
Takahashi is managing the test scores of N students. Teacher Aoki has asked him: "How many students have at least one other student whose score differs from theirs by exactly K?"
The N students are numbered from 1 to N, and student i has a test score of A_i points (A_i is a non-negative integer). Note that multiple students may have the same score.
Student i is said to be a "student satisfying the condition" if there exists at least one student j (1 \le j \le N, j \neq i) such that:
|A_i - A_j| = K
In other words, there must exist a student j different from student i such that the absolute difference between the scores of student i and student j is exactly K.
For example, when K = 0, student i satisfies the condition if and only if there exists at least one other student (with a different index from i) who has the same score.
Find the number of students who satisfy the condition. Note that you should output the total number of students satisfying the condition, not the number of pairs that satisfy the condition.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq 10^9
- 0 \leq A_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N K A_1 A_2 \dots A_N
- The first line contains an integer N representing the number of students and a non-negative integer K representing the score difference condition, separated by a space.
- The second line contains integers A_1, A_2, \dots, A_N representing the test scores of each student, separated by spaces.
Output
Output the number of students satisfying the condition in a single line.
Sample Input 1
5 2 1 3 5 7 2
Sample Output 1
4
Sample Input 2
4 10 1 2 3 4
Sample Output 2
0
Sample Input 3
10 0 5 5 3 3 3 7 7 1 2 4
Sample Output 3
7
Sample Input 4
15 5 10 15 20 25 30 35 40 3 8 13 18 23 28 33 38
Sample Output 4
15
Sample Input 5
1 0 1000000000
Sample Output 5
0