Day8 - A - Points on Line 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 ≤ 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 %I64d specifier.
Examples
4 3
1 2 3 4
4
4 2
-3 -2 -1 0
2
5 19
1 10 20 30 50
1
Note
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}.
思路:维护双指针,每次右指针移一位,左指针到达最远距离,因为是递增序,两指针不减,统计答案时,每次定右指针,次数就是C(区间长度-1)(2),组合数
typedef long long LL;
const int maxm = 1e5+;
int buf[maxm];
int main() {
ios::sync_with_stdio(false), cin.tie();
int n, d;
cin >> n >> d;
for(int i = ; i < n; ++i)
cin >> buf[i];
LL ans = ;
int l = ;
for(int i = ; i < n; ++i) {
while(buf[i] - buf[l] > d) l++;
ans += (LL)(i-l) * (i-l-) / ;
}
cout << ans << "\n";
return ;
}
Day8 - A - Points on Line CodeForces - 251A的更多相关文章
- 【转】Points To Line
原文地址 Python+Arcpy操作Points(.shp)转换至Polyline(.shp),仔细研读Points To Line (Data Management)说明,参数说明如下: Inpu ...
- Psychos in a Line CodeForces - 319B (单调栈的应用)
Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...
- A. Points on Line
A. Points on Line time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces 251A Points on Line(二分or单调队列)
Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...
- 查找表,Two Sum,15. 3Sum,18. 4Sum,16 3Sum Closest,149 Max points on line
Two Sum: 解法一:排序后使用双索引对撞:O(nlogn)+O(n) = O(nlogn) , 但是返回的是排序前的指针. 解法二:查找表.将所有元素放入查找表, 之后对于每一个元素a,查找 t ...
- codeforces251A. Points on Line
Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. N ...
- 二分三元组 CodeForces - 251A
题目链接: https://vjudge.net/problem/35188/origin 题目大意: 要求你找到一个 i < j < k时有 a[k]-a[i] <= d的组的个数 ...
- A - Points and Segments CodeForces - 429E
题解: 方法非常巧妙的一道题 首先考虑要求全部为0怎么做 发现是个欧拉回路的问题(很巧妙) 直接dfs一遍就可以了 而这道题 要求是-1,1,0 我们可以先离散化 完了之后判断每个点被奇数还是偶数条边 ...
- codeforces 576c// Points on Plane// Codeforces Round #319(Div. 1)
题意:有n个点,找到一个顺序走遍这n个点,并且曼哈顿距离不超过25e8. 由于给的点坐标都在0-1e6之间.将x轴分成1000*1000,即1000长度为1块.将落在同一块的按y排序,编号为奇的块和偶 ...
随机推荐
- [Misc] ZSH 常用快捷键
安装 zsh 终端执行 brew install zsh 终端执行 vim ~/.bash_profile 命令,打开 .bash_profile 文件 如果没有 vim,请自行安装 在打开的文件中, ...
- 笔记-pymysq-exception
笔记-pymysq-exception 1. exception DB API中定义了一些数据库操作的错误及异常,下表列出了这些错误和异常: 基本上是继承了Exception基类,没有添加额外功能. ...
- kvm的分层控制
第五层 virsh virt-manager(和libvirtd)利用了libvirt-api virsh pool-list --all virsh pool-define xxx/xml会 ...
- 【剑指Offer面试编程题】题目1354:和为S的连续正数序列--九度OJ
题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久, ...
- 一起探讨Go 语言为什么能成功?
导读 两位创造者Rob Pike和Robert Griesemer一起探讨了Go成功的原因. 常言道,历史不会重演,但总会惊人的相似. 如果您想创建一种编程语言,多向那些有经验的人士学习,他们有很多可 ...
- RESTFul Client入门实例
client.html文件内容为: <!DOCTYPE html> <html> <head> <title>RESTFul Client test p ...
- Gamma 矫正
参考如下链接: https://www.zhihu.com/question/27467127
- ABC154F - Many Many Paths
梦回高中,定义的f(i,j)为从(0,0)到(i,j)一共有多少条路可以选择,易知我们要做i+j次选择,其中有i次是选择x轴,剩下的是y轴,所以f(i,j)=C(i+j,i)=C(i+j,j),给你一 ...
- 堡垒机安装google-authenticator
公司线上的使用机器不能让用户随意的登陆,所以就不能让开发随意的登陆到生产的机器的.于是就打算使用google-auth的验证方式呢. 如果google-auth的方式. 搭建google-authen ...
- 图形数据写入数据库,Filletream
图形数据写入数据库 用FileStream对象读模式打开图形文件 Dim 文件对象 As New FileStream(图形文件名, FileMode.Open, FileAccess.Read) 定 ...