codeforces B. XK Segments

解决一个算法问题,找出数组中满足特定条件的有序数对数量。对于数组a和整数x,找到所有满足ai≤aj且恰好有k个整数y(ai≤y≤aj且y能被x整除)的不同有序数对(i,j)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. XK Segments
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 ai ≤ aj and there are exactly k integers y such that ai ≤ y ≤ aj 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).

Input

The first line contains 3 integers n, x, k (1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), where n is the size of the array a and x and k are numbers from the statement.

The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.

Output

Print one integer — the answer to the problem.

Examples
input
4 2 1
1 3 5 7
output
3
input
4 2 0
5 3 1 7
output
4
input
5 3 1
3 3 3 3 3
output
25
Note

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.

思路:先对整体从小到大排序,然后枚举每个a[i],寻找符合条件的上限值和下限值,然后用lower_bound求这个a[i]的方案数

#include<iostream>
#include<cstdlib>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
#define maxn 100005
#define INF 1000000009
using namespace std;
long long n,x,k;
long long a[maxn];
int main()
{
	ios_base::sync_with_stdio(false);
	while(cin>>n)
	{
		cin>>x>>k;
		for(int i=0;i<n;i++)
		{
			cin>>a[i]; 
		}
		sort(a,a+n);
		long long cnt=0;
		for(int i=0;i<n;i++)
		{
			long long l=max(((a[i]-1)/x+k)*x,a[i]);//下限,当k=0时,a[i]可能更大,所以用max 
			long long r=((a[i]-1)/x+k+1)*x;//上限 
			cnt+=lower_bound(a,a+n,r)-lower_bound(a,a+n,l);
		}
		cout << cnt << endl;
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值