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.ExamplesInput
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
思路:看到的时候知道是类似双指针,但是等式转化却开始转化错了
我开始转化是转化成ai-bi>bj-aj,然后想预处理两个差,进行一些操作。发现不能搞并且这样还是O(n^2)
正确思路:转化成(ai-bi)+(aj-bj)>0来做;转化到这一步i和j就是同一个序列的操作了。也就是ai-bi的差值实际上和aj-bj的差值是一样的序列里面的,就可以O(n)处理问题了
启发:等式枚举的转化要往变成同一序列去转化
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=2e5+100;
typedef long long LL;
LL a[maxn],b[maxn];
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
LL n;cin>>n;
for(LL i=1;i<=n;i++) cin>>a[i];
for(LL i=1;i<=n;i++) cin>>b[i];
for(LL i=1;i<=n;i++){
b[i]=a[i]-b[i];
}
sort(b+1,b+1+n);
LL l=1;LL r=n;
//双指针
LL sum=0;
while(l<r){
if(b[r]+b[l]>0){
sum+=r-l;
r--;
}
else l++;
}
cout<<sum<<endl;
return 0;
}