1. 程式人生 > >codeforces 251A(普通佇列or單調佇列or二分)

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