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 ;
}

codeforces 251A Points on Line(二分or单调队列)的更多相关文章

  1. BZOJ_3316_JC loves Mkk_ 二分答案 + 单调队列

    BZOJ_3316_JC loves Mkk_ 二分答案 + 单调队列 题意: 分析: 拆成链,二分答案,奇偶两个单调队列维护最大子段和,记录方案. 代码: #include <cstdio&g ...

  2. [NOIP2017普及组]跳房子(二分,单调队列优化dp)

    [NOIP2017普及组]跳房子 题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一. 跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画 nn 个格子, ...

  3. 感染(low)bfs 、感染(mid) 二分、感染(high) 二分 + 维护单调 队列去除无用的点

    感染(low) Description n户人家住在一条直线上,从左往右依次编号为1,2,-,n.起初,有m户人家感染了COVID-19,而接下来的每天感染的人家都会感染他家左右两家的人,问t天后总共 ...

  4. Codeforces Beta Round #6 (Div. 2 Only) 单调队列

    题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...

  5. HDU 5089 Assignment(rmq+二分 或 单调队列)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  6. codeforces 1077F2. Pictures with Kittens (hard version)单调队列+dp

    被队友催着上(xun)分(lian),div3挑战一场蓝,大号给基佬紫了,结果从D开始他开始疯狂教我做人??表演如何AKdiv3???? 比赛场上:A 2 分钟,B题蜜汁乱计数,结果想得绕进去了20多 ...

  7. BZOJ5090 组题 BZOJ2017年11月月赛 二分答案 单调队列

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ5090 11月月赛A题 题意概括 给出n个数. 求连续区间(长度大于等于k)最大平均值. 题解 这题 ...

  8. [acmm week12]二分+dp+单调队列

    1004 抄作业         Time Limit: 1sec    Memory Limit:256MB Description Zfree虽然平时很爱学习,但是他迫于生活所迫(比如设计cpu实 ...

  9. 【BZOJ 2806】 2806: [Ctsc2012]Cheat (SAM+二分+DP+单调队列)

    2806: [Ctsc2012]Cheat Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1262  Solved: 643 Description ...

随机推荐

  1. phpcms v9 get调用标签整理

    常见的phpcms标签调用  1.调用本系统单条数据(调用ID为1的信息,标题长度不超过25个汉字,显示更新日期): "select * from phpcms_content where  ...

  2. phpcms 标签解析

    phpcms 每个pc标签对应modules控制器下一个 tag.class类 比如 {pc:content action="position" posid="2&quo ...

  3. nginx 编译选项

    内容有些多,一眼看来难免头昏脑胀,但坚持看完,相信你一定会有所收获. nginx参数: --prefix= 指向安装目录 --sbin-path 指向(执行)程序文件(nginx) --conf-pa ...

  4. sql语句中出现笛卡尔乘积

    没有join条件导致笛卡尔乘积 学过线性代数的人都知道,笛卡尔乘积通俗的说,就是两个集合中的每一个成员,都与对方集合中的任意一个成员有关联.可以想象,在SQL查询中,如果对两张表join查询而没有jo ...

  5. 转:[译]Autoprefixer:一个以最好的方式处理浏览器前缀的后处理程序

    原文来自于:http://www.cnblogs.com/aNd1coder/archive/2013/08/12/3252690.html Autoprefixer解析CSS文件并且添加浏览器前缀到 ...

  6. VmWare问题解决(网络变更后虚拟主机无法上网)

    安装 Vmware,并新建一个虚拟机后,当时做好配置(NAT模式)后,虚拟机能够正常上网. 然后将电脑带到另一个地理位置办公时,却无法上网. 本篇探讨问题的缘由和几种解决方式. 一.缘由解析 仅探讨上 ...

  7. C#,Java,C++中的finally关键字

    博客原文:http://hankjin.blog.163.com/blog/static/33731937201031511305338/ 先说C++,标准C++不支持finally, 如果要实现fi ...

  8. Maximum Submatrix 2

    Codeforces Round #221 (Div. 1) B:http://codeforces.com/problemset/problem/375/B 题意:给你一个n*m的0,1矩阵,你可以 ...

  9. 可重入与线程安全(大多数Qt类是可重入,非线程安全的)

    可重入与线程安全 在Qt文档中,术语“可重入”与“线程安全”被用来说明一个函数如何用于多线程程序.假如一个类的任何函数在此类的多个不同的实例上,可以被多个线程同时调用,那么这个类被称为是“可重入”的. ...

  10. ♫【JS基础】壹零零壹

    如何面试一个前端开发者? function spacify(str) { return str.split('').join(' ') } console.log(spacify('hello wor ...