codeforce251A

  sol:每次加进一个新元素就找到左边最远的满足条件的元素,然后累计组合数

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef int ll;
inline ll read()
{
    ll s=0; bool f=0; char ch=' ';
    while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
    while(isdigit(ch)) {s=(s<<3)+(s<<1)+(ch^48); ch=getchar();}
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0) {putchar('-'); x=-x;}
    if(x<10) {putchar(x+'0'); return;}
    write(x/10); putchar((x%10)+'0');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=100005;
int n,m,a[N];
priority_queue<int,vector<int>,less<int> >que;
signed main()
{
    int i,wn,wq,ws,wwx=0;
    R(n); R(m);
    for(i=1;i<=n;i++) R(a[i]);
    if(n<3)
    {
        puts("0");
        return 0;
    }
    sort(a+1,a+n+1);
    wn=1; wq=3;
    if(a[3]-a[1]<=m) wwx=1;
    for(wq=4;wq<=n;wq++)
    {
        while(a[wq]-a[wn]>m) wn++;
        if(wn>wq-2) continue;
        ws=wq-wn;
        wwx+=ws*(ws-1)/2;
    }
    Wl(wwx);
    return 0;
}
View Code

 

posted @ 2021-10-25 23:02  yccdu  阅读(9)  评论(0编辑  收藏  举报