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排序,编号为奇的块和偶 ...
随机推荐
- Swift3.0-基本运算符
一.简介 运算符是检查.改变.合并值的特殊符号或者短语.在本篇文章中只介绍基本运算符,Swift中包含的高级运算符(比如溢出运算符)不在其中.Swift中的运算符和OC中的运算法还是有比较大的区别的, ...
- HashMap中推荐使用entrySet方式遍历Map类集合KV而不是keySet方式遍历
我们先来做一个测试 public class HashMapTest { private HashMap<String, String> map = new HashMap<> ...
- nginx 性能优化的概述及在CPU资源方面的处理
nginx的性能优化的概述 软件层面的提升硬件的使用率 增大CPU的利用率 增大内存的利用率 增大磁盘IO利用率 增大网络带宽利用率 提升硬件规格 网卡:万兆网卡.例如10G.25G.40G等 磁盘: ...
- Spring源码试读--BeanFactory模拟实现
动机 现在Springboot越来越便捷,如果简单的Spring应用,已无需再配置xml文件,基本可以实现全注解,即使是SpringCloud的那套东西,也都可以通过yaml配置完成.最近一年一直在用 ...
- Java基础 -1.4
标识符与关键字 对于标识符的组成在Java之中的定义如下:由字母.数字._.$ 组成 其中不能使用Java的保留字(关键字) 其中 $ 一般都有特殊的含义 不建议出现在自己所编写的代码上 关键字 是系 ...
- Linux 下面搭建KMS服务器
1. 下载安装Linux 版的KMS服务软件 # wget https://github.com/Wind4/vlmcsd/releases/download/svn1111/binaries.tar ...
- 【PAT甲级】1029 Median (25 分)
题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...
- 使用eclipse部署springcloud config从GitHub上获取配置内容出现错误:Auth fail
Eclipse点击Window > Preferences > General > Network Connections > SSH2 点击"Key Managem ...
- 119、Java中String类之通过isEmpty判断是否为空字符串
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- excel2003 颜色筛选问题
"excel2003中,添加辅助列,用定义名称的方法得到对应的颜色号,然后对辅助列进行排序: 颜色单元格在A列,选中B1,插入->名称->定义,输入a,下面输入公式 =get.c ...