题目链接:/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
54 8 2 6 24 5 4 1 3
Output
7
Input
41 3 2 41 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;}