D. Pair of Topics
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by ai 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 n integers a1,a2,…,an (1≤ai≤109), where ai is the interestingness of the i-th topic for the teacher.
The third line of the input contains n integers b1,b2,…,bn (1≤bi≤109), where bi is the interestingness of the i-th topic for the students.
Output
Print one integer — the number of good pairs of topic.
Examples
inputCopy
5
4 8 2 6 2
4 5 4 1 3
outputCopy
7
inputCopy
4
1 3 2 4
1 3 2 4
outputCopy
0
题意:就是找题中那个式子满足的对数有多少
思路:移向一下考虑 (ai-bi)+(aj-bj)>0
其实也就是对于相同位置的a和b进行作差为c 然后看后面有没有下标j使得当前位置i
ci+cj>0即可
比如第一个样例 作差后 0 3 -2 5 -1
题上有说要j>i 我们排序后 只看后面有多少满足即可
排序后-2 -1 0 3 5
对于-2 有 3 5 使得和大于0
对于-1有 3 5
对于0 有3 5
对于3 有5
所以答案是7
因为排序了 如果当前是正数 直接加上后面的个数即可 如果非正数 二分一下一个下标j使得ai+aj>0 那么满足的个数就是n-j+1
#include<bits/stdc++.h>
using namespace std;
#define ll long long;
ll a[200005],b[200005],
ll cha[200005];
int main()
{
int n;cin>>n;
vector<int> a(n+1),b(n+1);
repd(i,1,n) cin>>a[i];
repd(i,1,n) cin>>b[i];
vector<ll> c(n+1);
repd(i,1,n) c[i]=a[i]-b[i];
sort(c.begin()+1,c.end());
ll ans=0;
repd(i,1,n){
if(c[i]<=0){
int k=upper_bound(c.begin()+1,c.end(),-c[i])-c.begin();
ans+=n-k+1;
}
else ans+=n-i;
}
cout<<ans<<endl;
return 0;
}