您的位置:首页 > 其它

codeforce 895B XK Segments (结论)

2017-12-05 21:54 344 查看
the link : http://codeforces.com/contest/895/problem/B

this is a clever question ,witch tell you some N

numbers and k,x and wish you find how many pair of number ai,aj that there are exactly k of them within the range [ai,aj] can be divide evenly by x…

sorry for my terrible English,so here is a conclusion: in the range of [l,r] ,there are r/x - (l-1)/x of numbers that can be divided by x.

then we can sort the array first and for each ai as l, find minimum and maximum r that r/x - (l-1)/x equals to k

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100005];
int n;
ll x,k;
int main()
{
ios::sync_with_stdio(0);
cin>>n>>x>>k;
for(int i  = 1;i<=n;i++)
{
cin>>a[i];
//aa[i] = a[i]/x;
}
ll ans = 0;
sort(a+1,a+n+1);
//sort(aa+1,aa+n+1);
for(int i = 1;i<=n;i++)
{
ll d = max(a[i], ((a[i]-1)/x + k)*x);
ll u = ((a[i]-1)/x +k+1)*x -1;
ans += max(0,upper_bound(a+1,a+n+1,u) - lower_bound(a+1,a+n+1,d));
}
cout<<ans<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: