A. Points on Line
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
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
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}.

思路:因为给的序列是单调的,那么我么只要看当前点和头结点是否相差超过m,不超过的话sum+=C(i-l,2);

 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<stack>
8 using namespace std;
9 typedef long long LL;
10 LL ans[100005];
11 LL dp[100005];
12 int main(void)
13 {
14 int n,m;
15 while(scanf("%d %d",&n,&m)!=EOF)
16 {
17 int i,j;LL sum = 0;
18 for(i = 0;i < n;i++)
19 {
20 scanf("%lld",&ans[i]);
21 }
22 if(n <= 2)
23 printf("0\n");
24 else
25 {
26 int l = 0;
27 for(i = 2;i < n;i++)
28 {
29 while(ans[i]-ans[l]>m)
30 {
31 l++;
32 }
33 sum = sum + (LL)(i-l)*(LL)(i-l-1)/(LL)2;
34 }printf("%lld\n",sum);
35 }
36
37 }
38 return 0;
39 }

A. Points on Line的更多相关文章

  1. 【转】Points To Line

    原文地址 Python+Arcpy操作Points(.shp)转换至Polyline(.shp),仔细研读Points To Line (Data Management)说明,参数说明如下: Inpu ...

  2. codeforces 251A Points on Line(二分or单调队列)

    Description Little Petya likes points a lot. Recently his mom has presented him n points lying on th ...

  3. 查找表,Two Sum,15. 3Sum,18. 4Sum,16 3Sum Closest,149 Max points on line

    Two Sum: 解法一:排序后使用双索引对撞:O(nlogn)+O(n) = O(nlogn) , 但是返回的是排序前的指针. 解法二:查找表.将所有元素放入查找表, 之后对于每一个元素a,查找 t ...

  4. 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. N ...

  5. codeforces251A. Points on Line

    Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. N ...

  6. 【Henu ACM Round#19 D】 Points on Line

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑l..r这个区间. 且r是满足a[r]-a[l]<=d的最大的r 如果是第一个找到的区间,则直接累加C(r-l+1,3); ...

  7. POJ 3805 Separate Points (判断凸包相交)

    题目链接:POJ 3805 Problem Description Numbers of black and white points are placed on a plane. Let's ima ...

  8. Matlab_Graphics(1)_2D

    1.Add title ,axis Lables, and Legend to Graph: x=linspace(-*pi,2pi,); y1=sin(x); y2=cos(x); figure p ...

  9. (转)The Neural Network Zoo

    转自:http://www.asimovinstitute.org/neural-network-zoo/ THE NEURAL NETWORK ZOO POSTED ON SEPTEMBER 14, ...

随机推荐

  1. 巩固javaweb的第十九天

    巩固内容: 使用 form 元素 使用 form 元素封装要提交的信息 要向服务器提交信息,需要使用 form 元素,所有要提交的信息都应该在 form 内部.在 注册界面中,所有要输入的信息都位于 ...

  2. 巩固javaweb第十七天

    巩固内容: 文本域 文本域主要用于输入多行文字,如果输入的文字比较多,则可以采用文本域. 文本域的基本格式如下: <textarea rows="行数" name=" ...

  3. 学习java 7.6

    学习内容: 方法重写注意事项:子类不能重写父类的私有方法 子类的访问权限不比父类的低(父类默认,子类可以是默认也可以是public) java中继承的注意事项:java中类只支持单继承,java中类支 ...

  4. day16 循环导入、模块搜索路径、软件开发、包的使用

    day16 循环导入.模块搜索路径.软件开发.包的使用 1.循环导入 循环导入:循环导入问题指的是在一个模块加载/导入的过程中导入另外一个模块,而在另外一个模块中又返回来导入第一个模块中的名字,由于第 ...

  5. Output of C++ Program | Set 12

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  6. ReactiveCocoa操作方法-线程\时间

    ReactiveCocoa操作方法-线程 deliverOn: 内容传递切换到制定线程中,副作用在原来线程中,把在创建信号时block中的代码称之为副作用. subscribeOn: 内容传递和副作用 ...

  7. Bitmaps与优化

    1.有效的处理较大的位图 图像有各种不同的形状和大小.在许多情况下,它们往往比一个典型应用程序的用户界面(UI)所需要的资源更大. 读取一个位图的尺寸和类型: 为了从多种资源来创建一个位图,Bitma ...

  8. Linux基础命令---apachectl

    apachectl apachectl指令是apache http服务器的前端控制程序,可以协助控制apache服务的守护进程httpd. 此命令的适用范围:RedHat.RHEL.Ubuntu.Ce ...

  9. my40_MySQL锁概述之意向锁

    本文在锁概述的基础上,通常实验举例,详细地介绍了意向锁的原理. 锁范围  全局锁(global lock)表锁(table lock)行锁 (row lock) ROW LOCK的粒度LOCK_REC ...

  10. Ajax异步更新网页(使用原生JavaScript)

    一.页面代码 <!DOCTYPE html> <html> <head> <title>MyHtml.html</title> <me ...