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 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 Input

Input

4 31 2 3 4

Output

4

Input

4 2-3 -2 -1 0

Output

2

Input

5 191 10 20 30 50

Output

1

Hint

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

题意:给定一个递增的数列,求出从这个数列中取出3个数,最大值与最小值的差值小于等于k的有多少种。

思路:有点递推的感觉,每加进来一个数,只考虑这个数的贡献,例如,加进来第5个数的时候,假设前面四个数都符合要求,那么5的贡献就是从前面4个数当中取出两个来的总数,所以就是C 4 2, 这是个组合数。所以考虑每一个数,把每个数的组合数加起来就是了

代码一(单调队列);

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 100010;
int a[maxn];
int q[maxn];
int main()
{
    int n, d;
    scanf("%d %d", &n, &d);
    ll ans = 0;
    int front = 1, tail = 0;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        q[++tail] = a[i];
        while (tail >= front && q[front] < a[i] - d) front++;
        int j = tail - front;
        ans += (ll)j * (j - 1) / 2;//C n 2
    }
    printf("%I64d\n", ans);
    return 0;
}

代码二(二分):

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 100010;
int a[maxn];
int main()
{
    int n, d;
    scanf("%d %d", &n, &d);
    ll ans = 0;
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i < n; i++)
    {
        int j = i - (lower_bound(a, a + i, a[i] - d) - a);
        ans += (ll)j * (j - 1) / 2;
    }
    printf("%I64d\n", ans);
    return 0;
}
时间: 2024-08-10 19:11:05

codeforces 251A Points on Line(二分or单调队列)的相关文章

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

HDU 5089 Assignment(rmq+二分 或 单调队列)

Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 557    Accepted Submission(s): 280 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fr

BZOJ 3316 JC loves Mkk 二分答案+单调队列

题目大意:给定一个环,要求在这个环上截取长度为偶数且在[L,R]区间内的一段,要求平均值最大 看到环果断倍增 看到平均值最大果断二分答案 看到长度[L,R]果断单调队列 对数组维护一个前缀和,对前缀和维护单调递增的单调队列 每扫过一个数sum[i],将sum[i-L]加入单调队列,再把距离i超过R的点删掉 长度为偶数?对奇数位置和偶数位置分别维护一个单调队列即可 每次找到大于0的子串之后记录一下分母再退出就行了 #include <cstdio> #include <cstring>

【BZOJ 2806】 2806: [Ctsc2012]Cheat (SAM+二分+DP+单调队列)

2806: [Ctsc2012]Cheat Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1262  Solved: 643 Description Input 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库的行数接下来M行的01串,表示标准作文库接下来N行的01串,表示N篇作文 Output N行,每行一个整数,表示这篇作文的Lo 值. Sample Input 1 2 10110 000001110 1011001100 Sam

bzoj3316 JC loves Mkk 二分答案 单调队列

链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3316 题意:给出一个环,求出长度在$L~R$之间任意偶数的一个序列,使得这个序列的平均值最大. 看到分数就知道这题不可做-- 好啦言归正传--这道题应该怎么做呢--直接上$PoPoQQQ$大爷的语录: 看到环果断倍增 看到平均值最大果断二分答案 看到长度[L,R]果断单调队列 没错就是这样--平均值这个东西其实就是要这么瞎搞--二分答案,然后给每个元素"咔嚓"一下去掉二分的值,之后

codeforces 1077F2. Pictures with Kittens (hard version)单调队列+dp

div3挑战一场蓝,大号给基佬紫了,结果从D开始他开始疯狂教我做人??表演如何AKdiv3???? 比赛场上:A 2 分钟,B题蜜汁乱计数,结果想得绕进去了20多分钟,至此GG,C秒出,D...还在想怎么做就告诉我二分答案,好那继续秒出.他看F我看E,没思路互换,F题都读了半天,其实就是我YY的题意,然后发现还是没N^3dp的想法,发现并不是区间dp,其实之前我E想到了枚举等差数列的项数,只是发现找不了起点(那不是找终点就好了).基佬紫:枚举项数啊,我????然后他写完我就发现自己又傻了,rus

bzoj 2806: [Ctsc2012]Cheat【广义SAM+二分+dp+单调队列】

把模板串建一个广义SAM 然后在线查询,每次在SAM上预处理出一个a[i]表示i位置向前最多能匹配多长的模板串 二分答案L,dp判断,设f[i]为·~i有几个匹配,转移显然是f[i]=max{f[i-1],f[j]+i-j(i-a[i]<=j<=i-L)},根据性质,i-a[i]是单调的(或者直接看a[i]的预处理过程也能看出来),所以这个dp可以用单调队列优化转移,最后判断是否f[n]>=L*0.9 #include<iostream> #include<cstdio

[bzoj2806][Ctsc2012]Cheat(后缀自动机(SAM)+二分答案+单调队列优化dp)

偷懒直接把bzoj的网页内容ctrlcv过来了 2806: [Ctsc2012]Cheat Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1943  Solved: 1004[Submit][Status][Discuss] Description Input 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库的行数接下来M行的01串,表示标准作文库接下来N行的01串,表示N篇作文 Output N行,每行一个整数,表示这篇作文的Lo

送礼物(二分答案+单调队列)

QUESTION: JYY和CX的结婚纪念日即将到来,JYY来到萌萌开的礼品店选购纪念礼物.萌萌的礼品店很神奇,所有出售的礼物都按照特定的顺序都排成一列,而且相邻的礼物之间有一种神秘的美感.于是,JYY决定从中挑选连续的一些礼物,但究竟选 哪些呢?假设礼品店一共有\(N\)件礼物排成一列,每件礼物都有它的美观度.排在第\(i\)(\(1\leq i \leq N\))个位置的礼物美观度为正整数\(A_i\).JYY决定选出其中连续的一段,即编号为礼物\(i\),\(i+1\),....,\(j-