1 second
256 megabytes
standard input
standard output
While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array
a
and integer
x
. He should find the number of different ordered pairs of indexes
(
i
,
j
)
such that
a
i
≤
a
j
and there are exactly
k
integers
y
such that
a
i
≤
y
≤
a
j
and
y
is divisible by
x
.
In this problem it is meant that pair
(
i
,
j
)
is equal to
(
j
,
i
)
only if
i
is equal to
j
. For example pair
(1, 2)
is not the same as
(2, 1)
.
The first line contains 3 integers
n
,
x
,
k
(
1 ≤
n
≤ 10
5
, 1 ≤
x
≤ 10
9
, 0 ≤
k
≤ 10
9
), where
n
is the size of the array
a
and
x
and
k
are numbers from the statement.
The second line contains
n
integers
a
i
(
1 ≤
a
i
≤ 10
9
) — the elements of the array
a
.
Print one integer — the answer to the problem.
4 2 1 1 3 5 7
3
4 2 0 5 3 1 7
4
5 3 1 3 3 3 3 3
25
In first sample there are only three suitable pairs of indexes —
(1, 2), (2, 3), (3, 4)
.
In second sample there are four suitable pairs of indexes
(1, 1), (2, 2), (3, 3), (4, 4)
.
In third sample every pair
(
i
,
j
)
is suitable, so the answer is
5 * 5 = 25
.
————————————————————————————————————————————————————————
题目的意思是给出若干个数,问多少二元组满足他们之间的x的倍数有k个
思路:先排序,然后枚举每个位置,二分查找合法区间端点
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 60100
#define MAXM 1000100
LL a[100006];
int main()
{
int n, x, k;
while (~scanf("%d%d%d", &n, &x, &k))
{
for (int i = 0; i < n; i++)
scanf("%lld", &a[i]);
sort(a, a + n);
LL sum = 0;
for (int i = 0; i < n; i++)
{
int l = lower_bound(a , a + n, max(a[i],((a[i] - 1) / x + k)*x))-a;
int r = upper_bound(a , a + n, ((a[i] - 1) / x + (k + 1))*x - 1) - a - 1;
sum += 1LL*(r - l + 1);
}
printf("%lld\n", sum);
}
return 0;
}