CodeForces - 1324D Pair of Topics(思维+二分)

题目链接https://vjudge.net/contest/362265#problem/D
The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by aiai units for the teacher and by bi units for the students.
The pair of topics i and j (i<j) is called good if ai+aj>bi+bj (i.e. it is more interesting for the teacher).
Your task is to find the number of good pairs of topics.

Input
The first line of the input contains one integer n (2≤n≤2⋅105) — the number of topics.
The second line of the input contains nn integers a1,a2,…,an (1≤ai≤109), where aiai is the interestingness of the ii-th topic for the teacher.
The third line of the input contains nn integers b1,b2,…,bn (1≤bi≤109), where bibi is the interestingness of the ii-th topic for the students.
Output
Print one integer — the number of good pairs of topic.

Input

5
4 8 2 6 2
4 5 4 1 3

Output

7

Input

4
1 3 2 4
1 3 2 4

Output

0

题意
给定一个数n
第二行n个数,表示an
第三行n个数,表示bn
求满足i<j时,ai+aj>bi+bj的对数。

分析:
进行变形,ai-bi+aj-bj>0
用p[i]表示ai-bi
将p[i]从小到大排序。要满足p[i]+p[j]>0,其中一个肯定要大于0。假如p[i]>0,则p[j]只要大于等于-p[i]+1同时满足下标小于j即可。
代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=2*1e5+10;
int a[N],b[N],p[N];
int n;
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    for(int i=1; i<=n; i++)
        scanf("%d",&b[i]);
    for(int i=1; i<=n; i++)
        p[i]=a[i]-b[i];
    sort(p+1,p+1+n);
    int sum=0;
    for(int i=1; i<=n; i++)
    {
        if(p[i]<=0)
            continue;
        int pos=lower_bound(p+1,p+1+n,-p[i]+1)-p;
        sum+=i-pos;
    }
    printf("%d\n",sum);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值