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.
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.
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.
4 3
1 2 3 4
4
4 2
-3 -2 -1 0
2
5 19
1 10 20 30 50
1
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
codeforces251A. Points on Line的更多相关文章
- 【转】Points To Line
原文地址 Python+Arcpy操作Points(.shp)转换至Polyline(.shp),仔细研读Points To Line (Data Management)说明,参数说明如下: Inpu ...
- 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 ...
- 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 ...
- 【Henu ACM Round#19 D】 Points on Line
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 考虑l..r这个区间. 且r是满足a[r]-a[l]<=d的最大的r 如果是第一个找到的区间,则直接累加C(r-l+1,3); ...
- POJ 3805 Separate Points (判断凸包相交)
题目链接:POJ 3805 Problem Description Numbers of black and white points are placed on a plane. Let's ima ...
- 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 ...
- (转)The Neural Network Zoo
转自:http://www.asimovinstitute.org/neural-network-zoo/ THE NEURAL NETWORK ZOO POSTED ON SEPTEMBER 14, ...
随机推荐
- 如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢
如果数据库上的row格式是mixed或者mixed的格式,如何对比两台数据库服务器上的数据是否一致呢
- ctfhub技能树—文件上传—双写后缀
双写后缀绕过 用于只将文件后缀名,例如"php"字符串过滤的场合: 例如:上传时将Burpsuite截获的数据包中文件名[evil.php]改为[evil.pphphp],那么过滤 ...
- 解决ROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table study_record( id int(11) not null
之前一直用的好好的,突然就出现了这个错误: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tha ...
- C# 中的动态类型
翻译自 Camilo Reyes 2018年10月15日的文章 <Working with the Dynamic Type in C#> [1] .NET 4 中引入了动态类型.动态对象 ...
- celery应用
celery---分布式任务队列 Celery是一个简单,灵活且可靠的分布式系统,可以处理大量消息,同时为操作提供维护该系统所需的工具. Celery是一个基于python开发的模块,可以帮助我们对任 ...
- 【AtCoder Beginner Contest 181】A~F题解
越学越菜系列 于2020.11.2,我绿了(错乱) A - Heavy Rotation 签到题,奇数Black,偶数White. code: #include<bits/stdc++.h> ...
- 安卓开发视频教程!想找工作的你还不看这份资料就晚了!Android校招面试指南
前言 准备面试其实已经准备了挺久了,当时打算面试准备了差不多以后,跟公司谈谈涨薪的事情,谈不拢的话,就年后直接找其他的公司.谁想到婚假还没休完,老板就在公司宣布了撤出上海的决定,愿意去深圳的就去,不愿 ...
- VMware虚拟机提示“以独占方式锁定此配置文件失败”!!!
VMware异常关闭后再次启动提示"以独占方式锁定此配置文件失败"!!! 前几日在使用VMware虚拟机的时候,虚拟机突然出现了卡顿,然后就把电脑关机了重启,结果再次打开虚拟机的时 ...
- 关于BI测试
BI测试: BI是从数据接入.数据准备.数据分析.数据可视化到数bai据分发应用的一系列过程,目的是为了辅助企业高效决策.而报表虽然最终也实现了数据可视化,但是对于数据分析的维度.深度.颗粒度.实时性 ...
- Prometheus+Grafana通过kafka_exporter监控kafka
Prometheus+Grafana通过kafka_exporter监控kafka 一.暴露 kafka-metric 方式 二.jmx_exporter方式 2.1 下载jmx_prometheus ...