codeforces251A. Points on Line

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 test(s)

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 second

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#define inf 2000000000
#define PI acos(-1.0)
#define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;

int a[100100],n,d;
int minx[100100][30];
int maxx[100100][30];
void init_RMQ(int n)
{
	rep(i,1,n)maxx[i][0]=minx[i][0]=a[i];
	for(int j = 1; j < 20; ++j)
	for(int i = 1; i <= n; ++i)
		if(i + (1 << j) - 1 <= n)
		{
			maxx[i][j] = max(maxx[i][j - 1], maxx[i + (1 << (j - 1))][j - 1]);
			minx[i][j] = min(minx[i][j - 1], minx[i + (1 << (j - 1))][j - 1]);
		}
}
int getmax(int x,int y){
	if(x>y)swap(x,y);
	int k=log((y-x+1)*1.0)/log(2.0);
	return max(maxx[x][k],maxx[y-(1<<k)+1][k]);
}
int getmin(int x,int y){
	if(x>y)swap(x,y);
	int k=log((y-x+1)*1.0)/log(2.0);
	return min(minx[x][k],minx[y-(1<<k)+1][k]);
}

int main(){
	scanf("%d%d",&n,&d);
	rep(i,1,n)scanf("%d",&a[i]);
	init_RMQ(n);
	int l=1;
	int r=3;
	long long ans=0;
	rep(i,3,n){
		r=i;
		while(getmax(l,r)-getmin(l,r)>d)l++;
		if(r-l+1>=3){
			//printf("%d %d\n",l,r);
			ans+=(r-l)*1LL*(r-l-1)/2LL;
		}
	}
	printf("%I64d\n",ans);
}

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

这题也可以用rmq做,然后依次枚举l,r

时间: 2024-10-03 03:28:49

codeforces251A. Points on Line的相关文章

CodeForces 251A. Points on Line(数学 lower_bound )

题目链接:http://codeforces.com/problemset/problem/251/A 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 be

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

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 e

【Henu ACM Round#19 D】 Points on Line

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑l..r这个区间. 且r是满足a[r]-a[l]<=d的最大的r 如果是第一个找到的区间,则直接累加C(r-l+1,3); 然后l++ 然后考虑这个区间之后的下一个区间[l+1,R],这里R是满足a[R]-a[l+1]<=d的最大的R 如果R==r的话,l=l+1,continue; 否则. 如果l+1>r的话累加C(R-(l+1)+1,3); 如果l+1<=r的话 显然l..r和l+1..R之间有一个公共的部

CF251A Points on Line 题解 二分

题目链接:http://codeforces.com/problemset/problem/251/A 题目描述 一条直线上有 \(n\) 个点,他们的坐标分别是 \(x_1,x_2, \cdots ,x_n\) .没有两个点的坐标相同. 你可以从这些点中挑选出三个点,使得三个点的最大距离不超过 \(d\) . 请求出满足条件的三个点的方案数. 输入格式 输入的第一行包含两个整数 \(n\) 和 \(d\) ( \(1 \le n \le 10^5; 1 \le d \le 10^9\) ).

《Image Warping Using Few Anchor Points and Radial Function》论文实现

<Image Warping Using Few Anchor Points and Radial Functions>achive based OpenCv .Eigen and Qt (vector blod) paper [paper Download](Image Warping Using Few Anchor Points and Radial Functions "paper Download") Tools Eigen OpenCV Concepts rad

RANSAC

RANSAC是"RANdom SAmple Consensus(随机抽样一致)"的缩写.它可以从一组包含"局外点"的观测数据集中,通过迭代方式估计数学模型的参数.它是一种不确定的算法--它有一定的概率得出一个合理的结果:为了提高概率必须提高迭代次数.该算法最早由Fischler和Bolles于1981年提出.核心思想就是随机性和假设性,随机性用于减少计算,循环次数是利用正确数据出现的概率.所谓的假设性,就是说随机抽出来的数据都认为是正确的,并以此去计算其他点,获得其

Heap Only Tuples (HOT)

Introduction ------------ The Heap Only Tuple (HOT) feature eliminates redundant index entries and allows the re-use of space taken by DELETEd or obsoleted UPDATEd tuples without performing a table-wide vacuum. It does this by allowing single-page va

ubuntu下vtkOrientationMarkerWidget使用案例

今天看代码看到小坐标,觉得还不错,找了一下资料 使用的头文件是vtkOrientationMarkerWidget.h 具体使用如下,来源http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Interaction/Widgets/Testing/Cxx/TestOrientationMarkerWidget.cxx;h=60042d7965f318d9907dd1b502e60ff01a82a13e;hb=HEAD: /*==================

谱聚类--SpectralClustering

谱聚类一般会先对两两样本间求相似度, 然后根据相似度矩阵求出拉普拉斯矩阵,然后将每个样本映射到拉普拉斯矩阵特诊向量中,最后使用k-means聚类. scikit-learn开源包中已经有现成的接口可以使用,具体见 http://scikit-learn.org/dev/modules/generated/sklearn.cluster.SpectralClustering.html#sklearn.cluster.SpectralClustering 写了一个测试例子 构造二维空间样本点, #!