Codeforces 251A. Points on line (math lower_bound)

Source: Internet
Author: User

Title Link: Http://codeforces.com/problemset/problem/251/A


Little Petya likes points a lot. Recently his mom had presented him N points lying on the line OX. Now Petya are wondering in what many ways he can choose three distinct points so that the distance between the farthest Of them doesn ' t exceed D.

Note that the order of the points inside the group of three chosen points doesn ' t matter.

Input

The first line contains and integers:NandD(1?≤? n. ≤?105; 1?≤? d. ≤?109 ). The next line containsNIntegers x1,? x 2,?...,? x N , their absolute value doesn ' t exceedTen9 -thex-coordinates of the points that Petya have got.

It is guaranteed, the coordinates of the points in the input strictly increase.

Output

Print a single integer-the number of groups of three points, where the distance between both farthest points doesn ' t exce Ed D.

%lld specifier to read or write 64-bit integers inс++. It is preferred to use the cin, cout streams or the %i64dspecifier.

Sample Test (s) input
4 31 2) 3 4
Output
4
Input
4 2-3-2-1 0
Output
2
Input
5 191 10 20 30 50
Output
1
Note

In the first sample any group of three points meets our conditions.

In the seconds sample is 2 groups of three points meet our conditions: { -3,-2,-1} and {-2,-1, 0}.

In the third sample is only one group does: {1, ten, +}.



Test instructions

Give N and K, and then give a set of size n, remove 3 numbers from the collection,
The difference between the maximum number and the minimum number is less than K.



The code is as follows:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace STD; #define LL __int64int Main () {    int n;    LL D;    LL a[100047];    while (~SCANF ("%d%i64d", &n,&d))    {for        (int i = 0; i < n; i++)        {            scanf ("%i64d", &a[i]); c12/>}        LL ans = 0;        for (int i = 1; i < n; i++)        {            LL tt = i (Lower_bound (a,a+i,a[i]-d)-a)-1;            ans+=tt* (tt+1)/2;//c n 2        }        printf ("%i64d\n", ans);    return 0;} /*4 31 2 3 44 2-3-2-1 05 191 10 20 30 50*/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 251A. Points on line (math lower_bound)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.