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

计算满足特定区间包含条件的数对数量
博客围绕一个历史问题展开,要求从给定的n个整数中找出满足特定条件的无序数对数量。条件是任选两个数x、y,使|x - y|到|x + y|的区间包含|x|到|y|的区间。思路是将数全换成正数,通过二分法求符合范围的数的个数。

C. A Tale of Two Lands

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The legend of the foundation of Vectorland talks of two integers xx and yy. Centuries ago, the array king placed two markers at points |x||x|and |y||y| 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 |x−y||x−y| and |x+y||x+y| 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 |z||z| denotes the absolute value of zz.

Now, Jose is stuck on a question of his history exam: "What are the values of xx and yy?" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to nn integers a1,a2,…,ana1,a2,…,an. Now, he wants to know the number of unordered pairs formed by two different elements from these nn integers such that the legend could be true if xx and yy 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 nn (2≤n≤2⋅1052≤n≤2⋅105)  — the number of choices.

The second line contains nn pairwise distinct integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the choices Jose is considering.

Output

Print a single integer number — the number of unordered pairs {x,y}{x,y} 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}{2,5}, the situation looks as follows, with the Arrayland markers at |2|=2|2|=2 and |5|=5|5|=5, while the Vectorland markers are located at |2−5|=3|2−5|=3 and |2+5|=7|2+5|=7:

The legend is not true in this case, because the interval [2,3][2,3] is not conquered by Vectorland. For the pair {5,−3}{5,−3} the situation looks as follows, with Arrayland consisting of the interval [3,5][3,5] and Vectorland consisting of the interval [2,8][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}{2,−3}, for a total of two pairs.

In the second sample, the only pair is {3,6}{3,6}, and the situation looks as follows:

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

题意:任选两个数x,y要求 |x−y||x−y| 到 |x+y||x+y| 这个区间要包含|x|到|y|,问这样的对有多少个(不计位置重复对)

思路:全部换成正数,不会影响。

           给出一个x,那么符合范围的y应该是x/2(向上取整)到x*2(向下取整)。二分求出这个区间符合数的个数

           

#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
long long a[200010];
long long n;
long long panl(long long x,long long l,long long r)
{
    while(l<=r)
    {
        long long mid=(l+r)/2;
        if(a[mid]<x) l=mid+1;
        else r=mid-1;
    }
    return r;
}
long long panr(long long x,long long l,long long r)
{
    while(l<=r)
    {
        long long mid=(l+r)/2;
        if(a[mid]<=x) l=mid+1;
        else r=mid-1;
    }
    return l;
}
int main()
{
    scanf("%I64d",&n);
    for(long long i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
        a[i]=abs(a[i]);
    }
    sort(a+1,a+n+1);
    long long ans=0;
    for(long long i=1;i<=n;i++)
    {
        long long x=a[i]%2==0?a[i]/2:a[i]/2+1;
        long long y=a[i]*2;
//        printf("%I64d %I64d\n",panr(y,1,n),panl(x,1,n));
        ans+=(panr(y,1,n)-panl(x,1,n)-1);
    }
    printf("%I64d\n",(ans-n)/2);
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值