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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/dl962454/article/details/63251567

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 cincout 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个数,最大值与最小值的差值小于等于k的有多少种。

思路:每加进来一个数,只考虑这个数的贡献,例如,加进来第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;//组合数Cn-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;
}

猜你喜欢

转载自blog.csdn.net/dl962454/article/details/63251567