Codeforces 251A Points on line (binary or monotone queue)

Source: Internet
Author: User
Tags cmath

Description

Little Petya likes points a lot. Recently his mom had presented him N points lying on the line OX. Now Petya are wondering in what many ways he can choose three distinct points so that the distance between the 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 the integers: n and D (1≤ n ≤105; 1≤ D ≤10 9). The next line contains n integers x1, x2, ..., x c17>n, their absolute value doesn ' t exceed ten9-the x-coordinates of the points that Petya has got.

It is guaranteed, 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 both farthest points doesn ' t exce Ed D.

%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 is 2 groups of three points meet our conditions: { -3,-2,-1} and {-2,-1, 0}.

In the third sample is only one group does: {1, ten, +}.

Test instructions: Given an ascending sequence, find out 3 numbers from this sequence, and the difference between the maximum and the minimum is less than or equal to the number of K.

Idea: A bit of a recursive feeling, each added in a number, only consider the contribution of this number, for example, add in the 5th number, assuming that the preceding four numbers meet the requirements, then 5 of the contribution is to remove two from the preceding 4 number of total, so is C 4 2, this is a combination of numbers. So consider each number and add up the combined number of each number.

Code one (monotone queue);

#include <cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespaceStd;typedefLong Longll;Const intMAXN =100010;intA[MAXN];intQ[MAXN];intMain () {intN, D; scanf ("%d%d", &n, &d); ll ans=0; intFront =1, tail =0;  for(inti =0; I < n; i++) {scanf ("%d", &A[i]); q[++tail] =A[i];  while(Tail >= front && Q[front] < a[i]-D) front++; intj = Tail-Front; Ans+ = (LL) J * (J-1) /2;//C N 2} printf ("%i64d\n", ans); return 0;}

Code two (two points):

#include <cstdio>#include<iostream>#include<cstring>#include<cmath>#include<cstdlib>#include<algorithm>using namespaceStd;typedefLong Longll;Const intMAXN =100010;intA[MAXN];intMain () {intN, D; scanf ("%d%d", &n, &d); ll ans=0;  for(inti =0; I < n; i++) scanf ("%d", &A[i]);  for(inti =1; I < n; i++)    {        intj = i-(Lower_bound (A, A + I, a[i]-D)-a); Ans+ = (LL) J * (J-1) /2; } printf ("%i64d\n", ans); return 0;}

Codeforces 251A Points on line (binary or monotone queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.