链接:
https://www.nowcoder.com/acm/contest/84/F
来源:牛客网
注意ll
来源:牛客网
注意ll
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
给定n个数,从中选出三个数,使得最大的那个减最小的那个的值小于等于d,问有多少种选法。
输入描述:
第一行两个整数n,d(1 <= n <= 100,000,1 <= d <= 1000,000,000); 第二行n个整数满足abs(ai) <= 1,000,000,000。数据保证a单调递增。
输出描述:
输出一个整数表示满足条件的选法。
示例1
输入
4 3 1 2 3 4
输出
4
示例2
输入
4 2 -3 -2 -1 0
输出
2
示例3
输入
5 19 1 10 20 30 50
输出
1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N =1e5+10;
int a[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("in.txt","r",stdin);
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
ll ans=0;
for(int i=0;i<n;i++){
ll x=upper_bound(a,a+n,a[i]+m)-(a+i+1);
//cout<<b[i]<<" ";
ans+=x*(x-1)/2;
}
cout<<ans<<endl;
//cout<<endl;
}