AT_abc444_e
Gaochenxi103_QAQ · · 题解
做法
考虑类似双指针的思路,从小到大枚举左端点。然后维护右端点。因为当
那么对于一个
每次判断 multiset维护当前
赛时代码
::::success[Accepted code]
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=4e5+10;
int n,d,a[N],ans=0;
multiset<int>p;
bool check(int x)
{
auto mid=p.lower_bound(x);
auto midd=p.lower_bound(x-d+1);
if(mid!=p.end() && abs(*mid-x)<d)return false;
if(midd!=p.end() && abs(*midd-x)<d)return false;
return true;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>d;
for(int i=1;i<=n;i++)cin>>a[i];
int r=0;
for(int l=1;l<=n;l++)
{
if(r<l)
{
r=l;
p.clear();
p.insert(a[l]);
}
else
{
if(l!=1)p.erase(p.find(a[l-1]));
}
while(r<n && check(a[r+1]))
{
r++;
p.insert(a[r]);
}
// cout<<l<<" "<<r<<"\n";
ans+=(r-l+1);
}
cout<<ans;
return 0;
}
::::