Description

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 ≤ 105; 1 ≤ d ≤ 109). The next line contains n integers x1, x2, ..., xn, their absolute value doesn't exceed 109 — the x-coordinates of the points that Petya has got.

It is guaranteed that 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 two farthest points doesn't exceed d.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.

Sample Input

Input
4 3
1 2 3 4
Output
4
Input
4 2
-3 -2 -1 0
Output
2
Input
5 19
1 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: {-3, -2, -1} and {-2, -1, 0}.

In the third sample only one group does: {1, 10, 20}.

题意:给定一个递增的数列,求出从这个数列中取出3个数,最大值与最小值的差值小于等于k的有多少种。

思路:有点递推的感觉,每加进来一个数,只考虑这个数的贡献,例如,加进来第5个数的时候,假设前面四个数都符合要求,那么5的贡献就是从前面4个数当中取出两个来的总数,所以就是C 4 2, 这是个组合数。所以考虑每一个数,把每个数的组合数加起来就是了

代码一(单调队列);

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn = ;
int a[maxn];
int q[maxn];
int main()
{
int n, d;
scanf("%d %d", &n, &d);
ll ans = ;
int front = , tail = ;
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
q[++tail] = a[i];
while (tail >= front && q[front] < a[i] - d) front++;
int j = tail - front;
ans += (ll)j * (j - ) / ;//C n 2
}
printf("%I64d\n", ans);
return ;
}

代码二(二分):

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn = ;
int a[maxn];
int main()
{
int n, d;
scanf("%d %d", &n, &d);
ll ans = ;
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
for (int i = ; i < n; i++)
{
int j = i - (lower_bound(a, a + i, a[i] - d) - a);
ans += (ll)j * (j - ) / ;
}
printf("%I64d\n", ans);
return ;
}

最新文章

  1. appscan 对api的手工检测
  2. maven错误解决一:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile)
  3. 有用C函数集锦
  4. JAVA Web 之 struts2文件上传下载演示(一)(转)
  5. 《第一行代码》学习笔记35-服务Service(2)
  6. phoneGap开发环境搭建(android)
  7. JavaBean技术的一些讲解
  8. bootstrap 导航栏鼠标悬停显示下拉菜单
  9. DSAPI Wifi热点的扫描与连接
  10. SQL随记(三)
  11. PG数据库——视图
  12. 数据库无法启动ORA-01034: ORACLE not available
  13. lightoj1259 线性筛的另一种写法 v变成bool标记数组
  14. 旅行商问题(TSP)、最长路径问题与哈密尔顿回路之间的联系(归约)
  15. http认证中的nonce与timestamp解释
  16. Remove duplicates from array II
  17. SQLSERVER 分区表实战
  18. Instruments(性能调优 12.3)
  19. BeanCreationException报错启动不起来(jar包冲突)
  20. Red Hat Enterprise Linux 7.4上安装Oracle 11.2.0.4

热门文章

  1. K - 计算球体积
  2. Windows Phone 8 SQL Server CE 数据库
  3. C语言连接MySql数据库
  4. temp 临时文件
  5. 【转】常见 jar包详解
  6. 以Facebook为案例剖析科技公司应有的工具文化
  7. SSH:Connection closed by foreign host
  8. Android Wear开发 - 卡片通知 - 第二节 : 自定义Wear卡片样式
  9. 如何定义让两个div横向排列
  10. Java super与this用法解析