codeforce 895B XK Segments (结论)

来源:互联网 发布:php开源库存管理系统 编辑:程序博客网 时间:2024/05/22 07:06

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 longusing 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;}
原创粉丝点击