CF_251A_PointsOnLine

来源:互联网 发布:mac windows7 iso下载 编辑:程序博客网 时间:2024/06/09 09:16

一个二分搜索的问题。

由于只有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