Sunday, 15 February 2015

Codeforces Round #153 (Div. 2) Points on Line

Problem Link: Points on Line

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int arr[MX];
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc,kk=1, n, d;
    cin>>n>>d;
    for(int i=0;i<n;i++) cin>>arr[i];
    long long ans=0;
    if(n<3)
    {
        cout<<0;
        return 0;
    }

    else
    {
        int lft=0, rgt=2;
        while(lft<n)
        {
            //rgt=lft+1;
            while(rgt<n && (arr[rgt]-arr[lft])<=d)
                rgt++;
            rgt--;
            long long num=rgt-lft;
            ans+= num*(num-1)/2;
            lft++;
        }
        cout<<ans;
    }


    return 0;
}

No comments:

Post a Comment