The next lecture in a high school requires two topics to be discussed. The ii-th topic is interesting by aiai units for the teacher and by bibi units for the students.
The pair of topics ii and jj (i<ji<j) is called good if ai+aj>bi+bjai+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 nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of topics.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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,…,bnb1,b2,…,bn (1≤bi≤1091≤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.
Examples
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
题意:
给你一个a数组和一个b数组,求满足ai+aj>bi+bj的组合数
思路:
ai+aj>bi+bj变形后为ai-bi+aj-bj>0可以定义一个c数组,让ci=ai-bi,只要找到ci+cj>0的组合数就好,另外由于数据太多,用两层循环一定超时,所以要分析一下;可以对c数组进行排序,然后看第一个和最后一个的和是否大于0,若大于0,则最后一个数和第一个数到最后一个数之间的任何一个数的和都大于0,则组合数要加上n-1,然后再比较第一个数和倒数第二个数的和,若小于0,则比较第二个数和倒数第二个数的和,以此类推;
代码:
#include"stdio.h"
#include"algorithm"
using namespace std;
int a[200010];
int main()
{
int m,b,i,x,y;
long long s=0;
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
{
scanf("%d",&b);
a[i]=a[i]-b;
}
sort(a,a+m);
x=0;
y=m-1;
while(x<y)
{
if(a[x]+a[y]>0)
{
if(x>=y)
break;
s+=(y-x);
y--;
}
else
x++;
}
printf("%lld",s);
return 0;
}