codeforces 251A(普通队列or单调队列or二分)

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 %I64d specifier.

Example

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

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

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

普通队列:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=2e5+5;
int a[maxn];
int main()
{
    int n,d;
    while(~scanf("%d%d",&n,&d))
    {
        LL cnt=0;
        LL ans=0;
        queue<LL>q;
        LL x;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&x);
            q.push(x);
            cnt++;
            while(x-q.front()>d)
            {
                q.pop();
                cnt--;
            }
            if(x-q.front()<=d)
            {
                ans+=(cnt-1)*(cnt-2)/2;//组合数C(n-1,2);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}

单调队列:

#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int maxn=2e5+5;
int a[maxn];
int main()
{
    int n,d;
    while(~scanf("%d%d",&n,&d))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        int st1=1,ed1=0;
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            ed1++;
            while(ed1>=st1&&a[ed1]-a[st1]>d)
            st1++;
            int j=ed1-st1;
            ans+=(LL)j*(j-1)/2;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

二分:

#include<bits/stdc++.h>
const int maxn=100005;
int a[maxn];
typedef long long LL;
using namespace std;
int main()
{
    int n;
    LL ans=0,d;
    while(cin>>n>>d)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<n;i++)
        {
            LL j=i-(lower_bound(a,a+i,a[i]-d)-a);
            ans+=j*(j-1)/2;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童话ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值