首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

CodeForces 251A 2分

2013-10-18 
CodeForces 251A 二分Little Petya likes points a lot. Recently his mom has presented him n points ly

CodeForces 251A 二分

Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance between the two 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 two integers: n and d (1?≤?n?≤?10n integers x10x-coordinates of the points that Petya has got.

It is guaranteed that the coordinates of the points in the input 

Output

Print a single integer — the number of groups of three points, where the distance between two farthest points doesn't exceed d.

Please do not use the 

Sample Input

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

Hint

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

In the seconds sample only 2 groups of three points meet our conditions: #include <stdio.h>#include <string.h>long long n, d;long long i, j;long long num[100005];long long ans = 0;long long find(long long start, long long i, long long j) {long long mid;while (i < j) {mid = (i + j) / 2;if (num[mid] - num[start] <= d) {i = mid + 1;}else {j = mid;}}mid = (i + j) / 2;if (num[mid] - num[start] > d)mid --;return mid;}int main() {scanf("%lld%lld", &n, &d);for (i = 0; i < n; i ++)scanf("%lld", &num[i]);for (i = 0; i < n - 2 ; i ++) {int j = find(i, i + 2, n - 1);if (j - i < 2) continue;ans += (j - i) * (j - i - 1) / 2;}printf("%lld\n", ans);return 0;}

热点排行