A Tale of Two Lands (二分答案) Codeforces Round #561 (Div. 2)

 

The legend of the foundation of Vectorland talks of two integers ?and ?. Centuries ago, the array king placed two markers at points |?| and |?| on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points |?−?| and |?+?|and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland.

Here |?|denotes the absolute value of ?.Now, Jose is stuck on a question of his history exam: "What are the values of ?

and ??" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to ? integers ?1,?2,…,??. Now, he wants to know the number of unordered pairs formed by two different elements from these ? integers such that the legend could be true if ? and ?were equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true.

Input

The first line contains a single integer ?(2≤?≤2⋅105)  — the number of choices.The second line contains ?pairwise distinct integers ?1,?2,…,?? (−109≤??≤109) — the choices Jose is considering.

Output

Print a single integer number — the number of unordered pairs {?,?}formed by different numbers from Jose's choices that could make the legend true.

Examples

Input

Copy

3
2 5 -3

Output

Copy

2

Input

Copy

2
3 6

Output

Copy

1

Note

Consider the first sample. For the pair {2,5}, the situation looks as follows, with the Arrayland markers at |2|=2 and |5|=5, while the Vectorland markers are located at |2−5|=3 and |2+5|=7:

The legend is not true in this case, because the interval [2,3]is not conquered by Vectorland. For the pair {5,−3} the situation looks as follows, with Arrayland consisting of the interval [3,5] and Vectorland consisting of the interval [2,8]:

As Vectorland completely contains Arrayland, the legend is true. It can also be shown that the legend is true for the pair {2,−3}

, for a total of two pairs.In the second sample, the only pair is {3,6}, and the situation looks as follows:

Note that even though Arrayland and Vectorland share 3  as endpoint, we still consider Arrayland to be completely inside of Vectorland.

显然数组元素正负对结果无影响

设 r > l ,我们发现Arrayland的领土边界为[ l , r ],而Vectorland的领土范围为

[ r - l , r + l ],显然 l + r ≥ l,所以右边是一定满足的,那么我们只需要考虑左端的情况,为了使r - l <= l,变形一下显然需要满足r <= 2 × l,显然原序列的顺序对答案无影响,所以我们将原序列排序,从头开始枚举每个 l (即左端点),显而易见,我们的 l 只需要在a数组的[i+1,n-1]范围内二分找出最大的w,使得  aw ≤ ai × 2,因为原数组排序后的有序性,显而易见a数组中下标小于等于w,大于i的元素都可以作为r,那么当前ai对于答案的贡献就是w−i。 

#include<bits/stdc++.h>
using namespace std;
int a[200005];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]<0)
        {
            a[i]=-a[i];
        }
    }
    sort(a,a+n);
    long long sum=0;
    for(int i=0;i<n;i++)
    {
        int l=i+1;
        int r=n-1;
        int k=a[i]*2;
        int w=-1;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(a[mid]<=k)
            {
                l=mid+1;
                w=mid;
            }else
            {
                r=mid-1;
            }
        }
        if(w!=-1)
        {
            sum+=w-i;
        }
    }
    cout<<sum<<endl;
    return 0;
}

本文参考:https://www.cnblogs.com/linzhengmin/p/10887668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值