CF_251A_PointsOnLine

一个二分搜索的问题。

由于只有1维所以距离计算非常容易,而最大距离显然出现在两边。

给定一个起始位置搜索小于等于终点的最远位置即可。

然后是一个组合数 c(2,ne-ns)。

另外这个题目显然超int。 最后鄙视自己最开始数组开小了。

#include <iostream>
#include <stdio.h>
using namespace std;

typedef long long LL;

const int M=100005;//数组大小也能看错 还错那么多次!

LL s[M];

LL bs(LL st,LL n,LL k)
{
    LL lo=st,hi=n-1;
    LL mid;
    while(lo<=hi)
    {
        mid=lo+(hi-lo)/2;
        if(s[mid]==k)    //符合条件=
            return mid;
        else if(s[mid]<k)
            lo=mid+1;
        else
            hi=mid-1;
    }
    return lo-1;         //符合条件<
}

int main()
{
    LL n,d;
    LL co;

    while(cin>>n>>d)
    {
        co=0;
        for(int i=0;i<n;i++)
        {
            cin>>s[i];
        }
        for(int i=0;i<n-2;i++)  //以i位置为起始的数组个数
        {
            LL tmp=bs(i,n,d+s[i])-i;
            //cout<<i<<" "<<bs(i,n,d+s[i])<<endl;
            co+=tmp*(tmp-1)/2;
        }
        cout<<co<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值