codeforces 251A Points on Line 二分 or 单调队列

比赛时我用单调队列A的

View Code
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define LL __int64
int a, n, d;
LL ans;
queue <int> q;
int main() 
{
    int i, j;
    scanf("%d%d", &n, &d);
    for(i = 0; i < n; i++)
    {
        scanf("%d", &a);
        q.push(a);
        while(q.front()  < q.back() - d) q.pop();
        j = q.size()-1;
        ans += (LL)j * (j-1)/2;
    }
    printf("%I64d\n", ans);
}

二分

View Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL __int64
int d, n;
int a[100005];
LL ans;
int main()
{
    int i, j;
    scanf("%d%d", &n, &d);
    for(i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for(i = 1; i < n; i++)
        j = i - (lower_bound(a, a+i, a[i] - d) - a),
        ans += (LL)j *(j-1)/2;
    printf("%I64d\n", ans);
    return 0;
}

 

你可能感兴趣的:(codeforces 251A Points on Line 二分 or 单调队列)